blob: 1892e2dd0a3f3fb33511ede38bcb998db179db80 [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>
Dan Albert8fc83652015-08-12 15:52:37 -070021#include <unistd.h>
srs569473ba4792010-01-12 18:18:17 -050022
23using namespace std;
srs56941e093722010-01-05 00:14:19 -050024
25#define MAX_OPTIONS 50
26
Jeff Sharkeyd4e73c92015-02-28 20:21:52 -080027/*
28 * Dump partition details in a machine readable format:
29 *
30 * DISK [mbr|gpt] [guid]
31 * PART [n] [type] [guid]
32 */
33static 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() << " "
Jeff Sharkeya76ec612015-03-22 20:37:15 -070074 << partData.GetUniqueGUID() << " "
75 << partData.GetDescription() << endl;
Jeff Sharkeyd4e73c92015-02-28 20:21:52 -080076 }
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
srs569473ba4792010-01-12 18:18:17 -050090int main(int argc, char *argv[]) {
Jeff Sharkeyd4e73c92015-02-28 20:21:52 -080091 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}