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 #include <unistd.h>
22 
23 using namespace std;
24 
25 #define MAX_OPTIONS 50
26 
27 /*
28  * Dump partition details in a machine readable format:
29  *
30  * DISK [mbr|gpt] [guid]
31  * PART [n] [type] [guid]
32  */
android_dump(char * device)33 static int android_dump(char* device) {
34     BasicMBRData mbrData;
35     GPTData gptData;
36     GPTPart partData;
37     int numParts = 0;
38     stringstream res;
39 
40     /* Silence noisy underlying library */
41     int stdout = dup(STDOUT_FILENO);
42     int silence = open("/dev/null", 0);
43     dup2(silence, STDOUT_FILENO);
44     dup2(silence, STDERR_FILENO);
45 
46     if (!mbrData.ReadMBRData((string) device)) {
47         cerr << "Failed to read MBR" << endl;
48         return 8;
49     }
50 
51     switch (mbrData.GetValidity()) {
52     case mbr:
53         res << "DISK mbr" << endl;
54         for (int i = 0; i < MAX_MBR_PARTS; i++) {
55             if (mbrData.GetLength(i) > 0) {
56                 res << "PART " << (i + 1) << " " << hex
57                         << (int) mbrData.GetType(i) << dec << endl;
58             }
59         }
60         break;
61     case gpt:
62         gptData.JustLooking();
63         if (!gptData.LoadPartitions((string) device)) {
64             cerr << "Failed to read GPT" << endl;
65             return 9;
66         }
67 
68         res << "DISK gpt " << gptData.GetDiskGUID() << endl;
69         numParts = gptData.GetNumParts();
70         for (int i = 0; i < numParts; i++) {
71             partData = gptData[i];
72             if (partData.GetFirstLBA() > 0) {
73                 res << "PART " << (i + 1) << " " << partData.GetType() << " "
74                         << partData.GetUniqueGUID() << " "
75                         << partData.GetDescription() << endl;
76             }
77         }
78         break;
79     default:
80         cerr << "Unknown partition table" << endl;
81         return 10;
82     }
83 
84     /* Write our actual output */
85     string resString = res.str();
86     write(stdout, resString.c_str(), resString.length());
87     return 0;
88 }
89 
main(int argc,char * argv[])90 int main(int argc, char *argv[]) {
91     for (int i = 0; i < argc; i++) {
92         if (!strcmp("--android-dump", argv[i])) {
93             return android_dump(argv[i + 1]);
94         }
95     }
96 
97     GPTDataCL theGPT;
98     return theGPT.DoOptions(argc, argv);
99 }
100