srs5694 | 1e09372 | 2010-01-05 00:14:19 -0500 | [diff] [blame] | 1 | // sgdisk.cc |
srs5694 | 73ba479 | 2010-01-12 18:18:17 -0500 | [diff] [blame] | 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. |
srs5694 | 1e09372 | 2010-01-05 00:14:19 -0500 | [diff] [blame] | 6 | // |
srs5694 | 73ba479 | 2010-01-12 18:18:17 -0500 | [diff] [blame] | 7 | // by Rod Smith, project began February 2009; sgdisk begun January 2010. |
srs5694 | 1e09372 | 2010-01-05 00:14:19 -0500 | [diff] [blame] | 8 | |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 9 | /* This program is copyright (c) 2009-2011 by Roderick W. Smith. It is distributed |
srs5694 | 1e09372 | 2010-01-05 00:14:19 -0500 | [diff] [blame] | 10 | under the terms of the GNU GPL version 2, as detailed in the COPYING file. */ |
| 11 | |
Jeff Sharkey | d4e73c9 | 2015-02-28 20:21:52 -0800 | [diff] [blame^] | 12 | #include <iostream> |
| 13 | #include <fstream> |
| 14 | #include <string.h> |
| 15 | #include <string> |
| 16 | #include <iostream> |
| 17 | #include <sstream> |
| 18 | #include <errno.h> |
srs5694 | a17fe69 | 2011-09-10 20:30:20 -0400 | [diff] [blame] | 19 | #include "gptcl.h" |
Jeff Sharkey | d4e73c9 | 2015-02-28 20:21:52 -0800 | [diff] [blame^] | 20 | #include <fcntl.h> |
srs5694 | 73ba479 | 2010-01-12 18:18:17 -0500 | [diff] [blame] | 21 | |
| 22 | using namespace std; |
srs5694 | 1e09372 | 2010-01-05 00:14:19 -0500 | [diff] [blame] | 23 | |
| 24 | #define MAX_OPTIONS 50 |
| 25 | |
Jeff Sharkey | d4e73c9 | 2015-02-28 20:21:52 -0800 | [diff] [blame^] | 26 | /* |
| 27 | * Dump partition details in a machine readable format: |
| 28 | * |
| 29 | * DISK [mbr|gpt] [guid] |
| 30 | * PART [n] [type] [guid] |
| 31 | */ |
| 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() << 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 | |
srs5694 | 73ba479 | 2010-01-12 18:18:17 -0500 | [diff] [blame] | 88 | int main(int argc, char *argv[]) { |
Jeff Sharkey | d4e73c9 | 2015-02-28 20:21:52 -0800 | [diff] [blame^] | 89 | 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 | } |