xref: /aosp_15_r20/external/gptfdisk/sgdisk.cc (revision 57696d54d05c64fd1b1787f8371dbcf104911cfb)
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 hybrid:
62     case gpt:
63         gptData.JustLooking();
64         if (!gptData.LoadPartitions((string) device)) {
65             cerr << "Failed to read GPT" << endl;
66             return 9;
67         }
68 
69         res << "DISK gpt " << gptData.GetDiskGUID() << endl;
70         numParts = gptData.GetNumParts();
71         for (int i = 0; i < numParts; i++) {
72             partData = gptData[i];
73             if (partData.GetFirstLBA() > 0) {
74                 res << "PART " << (i + 1) << " " << partData.GetType() << " "
75                         << partData.GetUniqueGUID() << " "
76                         << partData.GetDescription() << endl;
77             }
78         }
79         break;
80     default:
81         cerr << "Unknown partition table" << endl;
82         return 10;
83     }
84 
85     /* Write our actual output */
86     string resString = res.str();
87     write(stdout, resString.c_str(), resString.length());
88     return 0;
89 }
90 
main(int argc,char * argv[])91 int main(int argc, char *argv[]) {
92     for (int i = 0; i < argc; i++) {
93         if (!strcmp("--android-dump", argv[i])) {
94             return android_dump(argv[i + 1]);
95         }
96     }
97 
98     GPTDataCL theGPT;
99     return theGPT.DoOptions(argc, argv);
100 }
101