blob: aafb76446647c0b34c575f8604b264f04e5e0048 [file] [log] [blame]
srs56941e093722010-01-05 00:14:19 -05001// sgdisk.cc
srs569473ba4792010-01-12 18:18:17 -05002// Command-line-based version of gdisk. This program is named after sfdisk,
3// and it can serve a similar role (easily scripted, etc.), but it's used
4// strictly via command-line arguments, and it doesn't bear much resemblance
5// to sfdisk in actual use.
srs56941e093722010-01-05 00:14:19 -05006//
srs569473ba4792010-01-12 18:18:17 -05007// by Rod Smith, project began February 2009; sgdisk begun January 2010.
srs56941e093722010-01-05 00:14:19 -05008
srs569464cbd172011-03-01 22:03:54 -05009/* This program is copyright (c) 2009-2011 by Roderick W. Smith. It is distributed
srs56941e093722010-01-05 00:14:19 -050010 under the terms of the GNU GPL version 2, as detailed in the COPYING file. */
11
Jeff Sharkeyd4e73c92015-02-28 20:21:52 -080012#include <iostream>
13#include <fstream>
14#include <string.h>
15#include <string>
16#include <iostream>
17#include <sstream>
18#include <errno.h>
srs5694a17fe692011-09-10 20:30:20 -040019#include "gptcl.h"
Jeff Sharkeyd4e73c92015-02-28 20:21:52 -080020#include <fcntl.h>
srs569473ba4792010-01-12 18:18:17 -050021
22using namespace std;
srs56941e093722010-01-05 00:14:19 -050023
24#define MAX_OPTIONS 50
25
Jeff Sharkeyd4e73c92015-02-28 20:21:52 -080026/*
27 * Dump partition details in a machine readable format:
28 *
29 * DISK [mbr|gpt] [guid]
30 * PART [n] [type] [guid]
31 */
32static int android_dump(char* device) {
33 BasicMBRData mbrData;
34 GPTData gptData;
35 GPTPart partData;
36 int numParts = 0;
37 stringstream res;
38
39 /* Silence noisy underlying library */
40 int stdout = dup(STDOUT_FILENO);
41 int silence = open("/dev/null", 0);
42 dup2(silence, STDOUT_FILENO);
43 dup2(silence, STDERR_FILENO);
44
45 if (!mbrData.ReadMBRData((string) device)) {
46 cerr << "Failed to read MBR" << endl;
47 return 8;
48 }
49
50 switch (mbrData.GetValidity()) {
51 case mbr:
52 res << "DISK mbr" << endl;
53 for (int i = 0; i < MAX_MBR_PARTS; i++) {
54 if (mbrData.GetLength(i) > 0) {
55 res << "PART " << (i + 1) << " " << hex
56 << (int) mbrData.GetType(i) << dec << endl;
57 }
58 }
59 break;
60 case gpt:
61 gptData.JustLooking();
62 if (!gptData.LoadPartitions((string) device)) {
63 cerr << "Failed to read GPT" << endl;
64 return 9;
65 }
66
67 res << "DISK gpt " << gptData.GetDiskGUID() << endl;
68 numParts = gptData.GetNumParts();
69 for (int i = 0; i < numParts; i++) {
70 partData = gptData[i];
71 if (partData.GetFirstLBA() > 0) {
72 res << "PART " << (i + 1) << " " << partData.GetType() << " "
73 << partData.GetUniqueGUID() << endl;
74 }
75 }
76 break;
77 default:
78 cerr << "Unknown partition table" << endl;
79 return 10;
80 }
81
82 /* Write our actual output */
83 string resString = res.str();
84 write(stdout, resString.c_str(), resString.length());
85 return 0;
86}
87
srs569473ba4792010-01-12 18:18:17 -050088int main(int argc, char *argv[]) {
Jeff Sharkeyd4e73c92015-02-28 20:21:52 -080089 for (int i = 0; i < argc; i++) {
90 if (!strcmp("--android-dump", argv[i])) {
91 return android_dump(argv[i + 1]);
92 }
93 }
94
95 GPTDataCL theGPT;
96 return theGPT.DoOptions(argc, argv);
97}