1 // sgdisk.cc
2 // 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.
6 //
7 // by Rod Smith, project began February 2009; sgdisk begun January 2010.
8 
9 /* This program is copyright (c) 2009-2011 by Roderick W. Smith. It is distributed
10   under the terms of the GNU GPL version 2, as detailed in the COPYING file. */
11 
12 #include <iostream>
13 #include <fstream>
14 #include <string.h>
15 #include <string>
16 #include <iostream>
17 #include <sstream>
18 #include <errno.h>
19 #include "gptcl.h"
20 #include <fcntl.h>
21 
22 using namespace std;
23 
24 #define MAX_OPTIONS 50
25 
26 /*
27  * Dump partition details in a machine readable format:
28  *
29  * DISK [mbr|gpt] [guid]
30  * PART [n] [type] [guid]
31  */
android_dump(char * device)32 static 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() << " "
74                         << partData.GetDescription() << endl;
75             }
76         }
77         break;
78     default:
79         cerr << "Unknown partition table" << endl;
80         return 10;
81     }
82 
83     /* Write our actual output */
84     string resString = res.str();
85     write(stdout, resString.c_str(), resString.length());
86     return 0;
87 }
88 
main(int argc,char * argv[])89 int main(int argc, char *argv[]) {
90     for (int i = 0; i < argc; i++) {
91         if (!strcmp("--android-dump", argv[i])) {
92             return android_dump(argv[i + 1]);
93         }
94     }
95 
96     GPTDataCL theGPT;
97     return theGPT.DoOptions(argc, argv);
98 }
99