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 | |
srs5694 | 1e09372 | 2010-01-05 00:14:19 -0500 | [diff] [blame] | 12 | #include <stdio.h> |
srs5694 | 73ba479 | 2010-01-12 18:18:17 -0500 | [diff] [blame] | 13 | #include <popt.h> |
| 14 | #include <errno.h> |
srs5694 | df9d363 | 2011-01-08 18:33:24 -0500 | [diff] [blame] | 15 | #include <stdint.h> |
srs5694 | 9a46b04 | 2011-03-15 00:34:10 -0400 | [diff] [blame] | 16 | #include <string.h> |
srs5694 | df9d363 | 2011-01-08 18:33:24 -0500 | [diff] [blame] | 17 | #include <string> |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 18 | #include <iostream> |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 19 | #include <sstream> |
srs5694 | 1e09372 | 2010-01-05 00:14:19 -0500 | [diff] [blame] | 20 | #include "mbr.h" |
| 21 | #include "gpt.h" |
| 22 | #include "support.h" |
srs5694 | 73ba479 | 2010-01-12 18:18:17 -0500 | [diff] [blame] | 23 | #include "parttypes.h" |
srs5694 | 9ddc14b | 2010-08-22 22:44:42 -0400 | [diff] [blame] | 24 | #include "attributes.h" |
srs5694 | 73ba479 | 2010-01-12 18:18:17 -0500 | [diff] [blame] | 25 | |
| 26 | using namespace std; |
srs5694 | 1e09372 | 2010-01-05 00:14:19 -0500 | [diff] [blame] | 27 | |
| 28 | #define MAX_OPTIONS 50 |
| 29 | |
srs5694 | 5a08175 | 2010-09-24 20:39:41 -0400 | [diff] [blame] | 30 | int BuildMBR(GPTData& theGPT, char* argument, int isHybrid); |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 31 | int CountColons(char* argument); |
srs5694 | 1e09372 | 2010-01-05 00:14:19 -0500 | [diff] [blame] | 32 | |
srs5694 | 5a60853 | 2011-03-17 13:53:01 -0400 | [diff] [blame] | 33 | // Extract colon-separated fields from a string.... |
| 34 | uint64_t GetInt(const string & argument, int itemNum); |
| 35 | string GetString(string argument, int itemNum); |
| 36 | |
| 37 | |
srs5694 | 73ba479 | 2010-01-12 18:18:17 -0500 | [diff] [blame] | 38 | int main(int argc, char *argv[]) { |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 39 | GPTData theGPT, secondDevice; |
srs5694 | df9d363 | 2011-01-08 18:33:24 -0500 | [diff] [blame] | 40 | uint32_t sSize; |
| 41 | uint64_t low, high; |
srs5694 | e321d44 | 2010-01-29 17:44:04 -0500 | [diff] [blame] | 42 | int opt, numOptions = 0, saveData = 0, neverSaveData = 0; |
srs5694 | 9ba5421 | 2010-05-18 23:24:02 -0400 | [diff] [blame] | 43 | int partNum = 0, deletePartNum = 0, infoPartNum = 0, bsdPartNum = 0, largestPartNum = 0; |
| 44 | int saveNonGPT = 1; |
| 45 | uint32_t gptPartNum = 0; |
srs5694 | a8582cf | 2010-03-19 14:21:59 -0400 | [diff] [blame] | 46 | int alignment = DEFAULT_ALIGNMENT, retval = 0, pretend = 0; |
srs5694 | 73ba479 | 2010-01-12 18:18:17 -0500 | [diff] [blame] | 47 | uint32_t tableSize = 128; |
| 48 | uint64_t startSector, endSector; |
srs5694 | 5a08175 | 2010-09-24 20:39:41 -0400 | [diff] [blame] | 49 | uint64_t temp; // temporary variable; free to use in any case |
srs5694 | 9ddc14b | 2010-08-22 22:44:42 -0400 | [diff] [blame] | 50 | char *attributeOperation = NULL; |
srs5694 | 815fb65 | 2011-03-18 12:35:56 -0400 | [diff] [blame^] | 51 | char *device; |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 52 | char *newPartInfo = NULL, *typeCode = NULL, *partName = NULL; |
| 53 | char *backupFile = NULL, *twoParts = NULL, *hybrids = NULL, *mbrParts; |
srs5694 | f9312b0 | 2010-07-06 15:39:51 -0400 | [diff] [blame] | 54 | char *partGUID = NULL, *diskGUID = NULL, *outDevice = NULL; |
srs5694 | 327129e | 2010-09-22 01:07:31 -0400 | [diff] [blame] | 55 | string cmd, typeGUID; |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 56 | PartType typeHelper; |
srs5694 | 1e09372 | 2010-01-05 00:14:19 -0500 | [diff] [blame] | 57 | |
srs5694 | 73ba479 | 2010-01-12 18:18:17 -0500 | [diff] [blame] | 58 | poptContext poptCon; |
| 59 | struct poptOption theOptions[] = |
| 60 | { |
srs5694 | 9ddc14b | 2010-08-22 22:44:42 -0400 | [diff] [blame] | 61 | {"attributes", 'A', POPT_ARG_STRING, &attributeOperation, 'A', "operate on partition attributes", "list|[partnum:show|or|nand|xor|=|set|clear|toggle|get[:bitnum|hexbitmask]]"}, |
srs5694 | 73ba479 | 2010-01-12 18:18:17 -0500 | [diff] [blame] | 62 | {"set-alignment", 'a', POPT_ARG_INT, &alignment, 'a', "set sector alignment", "value"}, |
| 63 | {"backup", 'b', POPT_ARG_STRING, &backupFile, 'b', "backup GPT to file", "file"}, |
| 64 | {"change-name", 'c', POPT_ARG_STRING, &partName, 'c', "change partition's name", "partnum:name"}, |
srs5694 | 9ba5421 | 2010-05-18 23:24:02 -0400 | [diff] [blame] | 65 | {"recompute-chs", 'C', POPT_ARG_NONE, NULL, 'C', "recompute CHS values in protective/hybrid MBR", ""}, |
srs5694 | 73ba479 | 2010-01-12 18:18:17 -0500 | [diff] [blame] | 66 | {"delete", 'd', POPT_ARG_INT, &deletePartNum, 'd', "delete a partition", "partnum"}, |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 67 | {"display-alignment", 'D', POPT_ARG_NONE, NULL, 'D', "show number of sectors per allocation block", ""}, |
srs5694 | 73ba479 | 2010-01-12 18:18:17 -0500 | [diff] [blame] | 68 | {"move-second-header", 'e', POPT_ARG_NONE, NULL, 'e', "move second header to end of disk", ""}, |
| 69 | {"end-of-largest", 'E', POPT_ARG_NONE, NULL, 'E', "show end of largest free block", ""}, |
| 70 | {"first-in-largest", 'f', POPT_ARG_NONE, NULL, 'f', "show start of the largest free block", ""}, |
srs5694 | 5a08175 | 2010-09-24 20:39:41 -0400 | [diff] [blame] | 71 | {"first-aligned-in-largest", 'F', POPT_ARG_NONE, NULL, 'F', "show start of the largest free block, aligned", ""}, |
srs5694 | 73ba479 | 2010-01-12 18:18:17 -0500 | [diff] [blame] | 72 | {"mbrtogpt", 'g', POPT_ARG_NONE, NULL, 'g', "convert MBR to GPT", ""}, |
srs5694 | 9ba5421 | 2010-05-18 23:24:02 -0400 | [diff] [blame] | 73 | {"randomize-guids", 'G', POPT_ARG_NONE, NULL, 'G', "randomize disk and partition GUIDs", ""}, |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 74 | {"hybrid", 'h', POPT_ARG_STRING, &hybrids, 'h', "create hybrid MBR", "partnum[:partnum...]"}, |
srs5694 | 73ba479 | 2010-01-12 18:18:17 -0500 | [diff] [blame] | 75 | {"info", 'i', POPT_ARG_INT, &infoPartNum, 'i', "show detailed information on partition", "partnum"}, |
| 76 | {"load-backup", 'l', POPT_ARG_STRING, &backupFile, 'l', "load GPT backup from file", "file"}, |
| 77 | {"list-types", 'L', POPT_ARG_NONE, NULL, 'L', "list known partition types", ""}, |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 78 | {"gpttombr", 'm', POPT_ARG_STRING, &mbrParts, 'm', "convert GPT to MBR", "partnum[:partnum...]"}, |
srs5694 | 73ba479 | 2010-01-12 18:18:17 -0500 | [diff] [blame] | 79 | {"new", 'n', POPT_ARG_STRING, &newPartInfo, 'n', "create new partition", "partnum:start:end"}, |
srs5694 | 9ba5421 | 2010-05-18 23:24:02 -0400 | [diff] [blame] | 80 | {"largest-new", 'N', POPT_ARG_INT, &largestPartNum, 'N', "create largest possible new partition", "partnum"}, |
srs5694 | 73ba479 | 2010-01-12 18:18:17 -0500 | [diff] [blame] | 81 | {"clear", 'o', POPT_ARG_NONE, NULL, 'o', "clear partition table", ""}, |
| 82 | {"print", 'p', POPT_ARG_NONE, NULL, 'p', "print partition table", ""}, |
| 83 | {"pretend", 'P', POPT_ARG_NONE, NULL, 'P', "make changes in memory, but don't write them", ""}, |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 84 | {"transpose", 'r', POPT_ARG_STRING, &twoParts, 'r', "transpose two partitions", "partnum:partnum"}, |
srs5694 | f9312b0 | 2010-07-06 15:39:51 -0400 | [diff] [blame] | 85 | {"replicate", 'R', POPT_ARG_STRING, &outDevice, 'R', "replicate partition table", "device_filename"}, |
srs5694 | 73ba479 | 2010-01-12 18:18:17 -0500 | [diff] [blame] | 86 | {"sort", 's', POPT_ARG_NONE, NULL, 's', "sort partition table entries", ""}, |
| 87 | {"resize-table", 'S', POPT_ARG_INT, &tableSize, 'S', "resize partition table", "numparts"}, |
srs5694 | 327129e | 2010-09-22 01:07:31 -0400 | [diff] [blame] | 88 | {"typecode", 't', POPT_ARG_STRING, &typeCode, 't', "change partition type code", "partnum:{hexcode|GUID}"}, |
srs5694 | 73ba479 | 2010-01-12 18:18:17 -0500 | [diff] [blame] | 89 | {"transform-bsd", 'T', POPT_ARG_INT, &bsdPartNum, 'T', "transform BSD disklabel partition to GPT", "partnum"}, |
srs5694 | 9ba5421 | 2010-05-18 23:24:02 -0400 | [diff] [blame] | 90 | {"partition-guid", 'u', POPT_ARG_STRING, &partGUID, 'u', "set partition GUID", "partnum:guid"}, |
| 91 | {"disk-guid", 'U', POPT_ARG_STRING, &diskGUID, 'U', "set disk GUID", "guid"}, |
srs5694 | 73ba479 | 2010-01-12 18:18:17 -0500 | [diff] [blame] | 92 | {"verify", 'v', POPT_ARG_NONE, NULL, 'v', "check partition table integrity", ""}, |
| 93 | {"version", 'V', POPT_ARG_NONE, NULL, 'V', "display version information", ""}, |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 94 | {"zap", 'z', POPT_ARG_NONE, NULL, 'z', "zap (destroy) GPT (but not MBR) data structures", ""}, |
| 95 | {"zap-all", 'Z', POPT_ARG_NONE, NULL, 'Z', "zap (destroy) GPT and MBR data structures", ""}, |
srs5694 | 73ba479 | 2010-01-12 18:18:17 -0500 | [diff] [blame] | 96 | POPT_AUTOHELP { NULL, 0, 0, NULL, 0 } |
| 97 | }; |
| 98 | |
| 99 | // Create popt context... |
| 100 | poptCon = poptGetContext(NULL, argc, (const char**) argv, theOptions, 0); |
| 101 | |
| 102 | poptSetOtherOptionHelp(poptCon, " [OPTION...] <device>"); |
| 103 | |
| 104 | if (argc < 2) { |
| 105 | poptPrintUsage(poptCon, stderr, 0); |
| 106 | exit(1); |
| 107 | } |
| 108 | |
| 109 | // Do one loop through the options to find the device filename and deal |
| 110 | // with options that don't require a device filename.... |
| 111 | while ((opt = poptGetNextOpt(poptCon)) > 0) { |
| 112 | switch (opt) { |
srs5694 | 9ddc14b | 2010-08-22 22:44:42 -0400 | [diff] [blame] | 113 | case 'A': |
| 114 | cmd = GetString(attributeOperation, 1); |
| 115 | if (cmd == "list") |
| 116 | Attributes::ListAttributes(); |
| 117 | break; |
srs5694 | 73ba479 | 2010-01-12 18:18:17 -0500 | [diff] [blame] | 118 | case 'L': |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 119 | typeHelper.ShowAllTypes(); |
srs5694 | 1e09372 | 2010-01-05 00:14:19 -0500 | [diff] [blame] | 120 | break; |
srs5694 | 73ba479 | 2010-01-12 18:18:17 -0500 | [diff] [blame] | 121 | case 'P': |
| 122 | pretend = 1; |
srs5694 | 1e09372 | 2010-01-05 00:14:19 -0500 | [diff] [blame] | 123 | break; |
srs5694 | 73ba479 | 2010-01-12 18:18:17 -0500 | [diff] [blame] | 124 | case 'V': |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 125 | cout << "GPT fdisk (sgdisk) version " << GPTFDISK_VERSION << "\n\n"; |
srs5694 | 1e09372 | 2010-01-05 00:14:19 -0500 | [diff] [blame] | 126 | break; |
| 127 | default: |
srs5694 | 1e09372 | 2010-01-05 00:14:19 -0500 | [diff] [blame] | 128 | break; |
| 129 | } // switch |
srs5694 | 73ba479 | 2010-01-12 18:18:17 -0500 | [diff] [blame] | 130 | numOptions++; |
| 131 | } // while |
srs5694 | 1e09372 | 2010-01-05 00:14:19 -0500 | [diff] [blame] | 132 | |
srs5694 | 73ba479 | 2010-01-12 18:18:17 -0500 | [diff] [blame] | 133 | // Assume first non-option argument is the device filename.... |
| 134 | device = (char*) poptGetArg(poptCon); |
| 135 | poptResetContext(poptCon); |
srs5694 | 1e09372 | 2010-01-05 00:14:19 -0500 | [diff] [blame] | 136 | |
srs5694 | 73ba479 | 2010-01-12 18:18:17 -0500 | [diff] [blame] | 137 | if (device != NULL) { |
| 138 | theGPT.JustLooking(); // reset as necessary |
| 139 | theGPT.BeQuiet(); // Tell called functions to be less verbose & interactive |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 140 | if (theGPT.LoadPartitions((string) device)) { |
srs5694 | 73ba479 | 2010-01-12 18:18:17 -0500 | [diff] [blame] | 141 | if ((theGPT.WhichWasUsed() == use_mbr) || (theGPT.WhichWasUsed() == use_bsd)) |
| 142 | saveNonGPT = 0; // flag so we don't overwrite unless directed to do so |
srs5694 | df9d363 | 2011-01-08 18:33:24 -0500 | [diff] [blame] | 143 | sSize = theGPT.GetBlockSize(); |
srs5694 | 73ba479 | 2010-01-12 18:18:17 -0500 | [diff] [blame] | 144 | while ((opt = poptGetNextOpt(poptCon)) > 0) { |
| 145 | switch (opt) { |
srs5694 | 9ddc14b | 2010-08-22 22:44:42 -0400 | [diff] [blame] | 146 | case 'A': { |
| 147 | if (cmd != "list") { |
| 148 | partNum = (int) GetInt(attributeOperation, 1) - 1; |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 149 | if ((partNum >= 0) && (partNum < (int) theGPT.GetNumParts())) { |
| 150 | switch (theGPT.ManageAttributes(partNum, GetString(attributeOperation, 2), |
| 151 | GetString(attributeOperation, 3))) { |
| 152 | case -1: |
| 153 | saveData = 0; |
| 154 | neverSaveData = 1; |
| 155 | break; |
| 156 | case 1: |
| 157 | theGPT.JustLooking(0); |
| 158 | saveData = 1; |
| 159 | break; |
| 160 | default: |
| 161 | break; |
| 162 | } // switch |
| 163 | } else { |
| 164 | cerr << "Error: Invalid partition number " << partNum + 1 << "\n"; |
| 165 | saveData = 0; |
| 166 | neverSaveData = 1; |
| 167 | } // if/else reasonable partition # |
| 168 | } // if (cmd != "list") |
srs5694 | 9ddc14b | 2010-08-22 22:44:42 -0400 | [diff] [blame] | 169 | break; |
| 170 | } // case 'A': |
srs5694 | 73ba479 | 2010-01-12 18:18:17 -0500 | [diff] [blame] | 171 | case 'a': |
| 172 | theGPT.SetAlignment(alignment); |
| 173 | break; |
| 174 | case 'b': |
| 175 | theGPT.SaveGPTBackup(backupFile); |
| 176 | free(backupFile); |
| 177 | break; |
| 178 | case 'c': |
| 179 | theGPT.JustLooking(0); |
| 180 | partNum = (int) GetInt(partName, 1) - 1; |
srs5694 | 5a60853 | 2011-03-17 13:53:01 -0400 | [diff] [blame] | 181 | if (theGPT.SetName(partNum, (UnicodeString) GetString(partName, 2).c_str())) { |
srs5694 | 73ba479 | 2010-01-12 18:18:17 -0500 | [diff] [blame] | 182 | saveData = 1; |
| 183 | } else { |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 184 | cerr << "Unable to set partition " << partNum + 1 |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 185 | << "'s name to '" << GetString(partName, 2) << "'!\n"; |
srs5694 | 73ba479 | 2010-01-12 18:18:17 -0500 | [diff] [blame] | 186 | neverSaveData = 1; |
| 187 | } // if/else |
| 188 | free(partName); |
| 189 | break; |
srs5694 | f9312b0 | 2010-07-06 15:39:51 -0400 | [diff] [blame] | 190 | case 'C': |
| 191 | theGPT.JustLooking(0); |
| 192 | theGPT.RecomputeCHS(); |
| 193 | saveData = 1; |
| 194 | break; |
srs5694 | 73ba479 | 2010-01-12 18:18:17 -0500 | [diff] [blame] | 195 | case 'd': |
| 196 | theGPT.JustLooking(0); |
| 197 | if (theGPT.DeletePartition(deletePartNum - 1) == 0) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 198 | cerr << "Error " << errno << " deleting partition!\n"; |
srs5694 | 73ba479 | 2010-01-12 18:18:17 -0500 | [diff] [blame] | 199 | neverSaveData = 1; |
| 200 | } else saveData = 1; |
| 201 | break; |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 202 | case 'D': |
srs5694 | 8a4ddfc | 2010-03-21 19:05:49 -0400 | [diff] [blame] | 203 | cout << theGPT.GetAlignment() << "\n"; |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 204 | break; |
srs5694 | 73ba479 | 2010-01-12 18:18:17 -0500 | [diff] [blame] | 205 | case 'e': |
| 206 | theGPT.JustLooking(0); |
| 207 | theGPT.MoveSecondHeaderToEnd(); |
| 208 | saveData = 1; |
| 209 | break; |
| 210 | case 'E': |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 211 | cout << theGPT.FindLastInFree(theGPT.FindFirstInLargest()) << "\n"; |
srs5694 | 73ba479 | 2010-01-12 18:18:17 -0500 | [diff] [blame] | 212 | break; |
| 213 | case 'f': |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 214 | cout << theGPT.FindFirstInLargest() << "\n"; |
srs5694 | 73ba479 | 2010-01-12 18:18:17 -0500 | [diff] [blame] | 215 | break; |
srs5694 | 5a08175 | 2010-09-24 20:39:41 -0400 | [diff] [blame] | 216 | case 'F': |
| 217 | temp = theGPT.FindFirstInLargest(); |
| 218 | theGPT.Align(&temp); |
| 219 | cout << temp << "\n"; |
| 220 | break; |
srs5694 | 73ba479 | 2010-01-12 18:18:17 -0500 | [diff] [blame] | 221 | case 'g': |
| 222 | theGPT.JustLooking(0); |
| 223 | saveData = 1; |
| 224 | saveNonGPT = 1; |
| 225 | break; |
srs5694 | 9ba5421 | 2010-05-18 23:24:02 -0400 | [diff] [blame] | 226 | case 'G': |
| 227 | theGPT.JustLooking(0); |
| 228 | saveData = 1; |
| 229 | theGPT.RandomizeGUIDs(); |
| 230 | break; |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 231 | case 'h': |
| 232 | theGPT.JustLooking(0); |
srs5694 | 5a08175 | 2010-09-24 20:39:41 -0400 | [diff] [blame] | 233 | if (BuildMBR(theGPT, hybrids, 1) == 1) |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 234 | saveData = 1; |
| 235 | break; |
srs5694 | 73ba479 | 2010-01-12 18:18:17 -0500 | [diff] [blame] | 236 | case 'i': |
| 237 | theGPT.ShowPartDetails(infoPartNum - 1); |
| 238 | break; |
| 239 | case 'l': |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 240 | if (theGPT.LoadGPTBackup((string) backupFile) == 1) { |
| 241 | theGPT.JustLooking(0); |
srs5694 | 73ba479 | 2010-01-12 18:18:17 -0500 | [diff] [blame] | 242 | saveData = 1; |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 243 | } else { |
srs5694 | 73ba479 | 2010-01-12 18:18:17 -0500 | [diff] [blame] | 244 | saveData = 0; |
| 245 | neverSaveData = 1; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 246 | cerr << "Error loading backup file!\n"; |
srs5694 | 73ba479 | 2010-01-12 18:18:17 -0500 | [diff] [blame] | 247 | } // else |
| 248 | free(backupFile); |
| 249 | break; |
| 250 | case 'L': |
| 251 | break; |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 252 | case 'm': |
| 253 | theGPT.JustLooking(0); |
srs5694 | 5a08175 | 2010-09-24 20:39:41 -0400 | [diff] [blame] | 254 | if (BuildMBR(theGPT, mbrParts, 0) == 1) { |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 255 | if (!pretend) { |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 256 | if (theGPT.SaveMBR()) { |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 257 | theGPT.DestroyGPT(); |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 258 | } else |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 259 | cerr << "Problem saving MBR!\n"; |
| 260 | } // if |
| 261 | saveNonGPT = 0; |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 262 | pretend = 1; // Not really, but works around problem if -g is used with this... |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 263 | saveData = 0; |
| 264 | } // if |
| 265 | break; |
srs5694 | 73ba479 | 2010-01-12 18:18:17 -0500 | [diff] [blame] | 266 | case 'n': |
| 267 | theGPT.JustLooking(0); |
| 268 | partNum = (int) GetInt(newPartInfo, 1) - 1; |
srs5694 | df9d363 | 2011-01-08 18:33:24 -0500 | [diff] [blame] | 269 | if (partNum < 0) |
| 270 | partNum = theGPT.FindFirstFreePart(); |
| 271 | low = theGPT.FindFirstInLargest(); |
| 272 | high = theGPT.FindLastInFree(low); |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 273 | startSector = IeeeToInt(GetString(newPartInfo, 2), sSize, low, high, low); |
| 274 | endSector = IeeeToInt(GetString(newPartInfo, 3), sSize, startSector, high, high); |
srs5694 | 73ba479 | 2010-01-12 18:18:17 -0500 | [diff] [blame] | 275 | if (theGPT.CreatePartition(partNum, startSector, endSector)) { |
| 276 | saveData = 1; |
| 277 | } else { |
srs5694 | df9d363 | 2011-01-08 18:33:24 -0500 | [diff] [blame] | 278 | cerr << "Could not create partition " << partNum + 1 << " from " |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 279 | << startSector << " to " << endSector << "\n"; |
srs5694 | 73ba479 | 2010-01-12 18:18:17 -0500 | [diff] [blame] | 280 | neverSaveData = 1; |
| 281 | } // if/else |
| 282 | free(newPartInfo); |
| 283 | break; |
srs5694 | 9ba5421 | 2010-05-18 23:24:02 -0400 | [diff] [blame] | 284 | case 'N': |
| 285 | theGPT.JustLooking(0); |
| 286 | startSector = theGPT.FindFirstInLargest(); |
| 287 | endSector = theGPT.FindLastInFree(startSector); |
srs5694 | df9d363 | 2011-01-08 18:33:24 -0500 | [diff] [blame] | 288 | if (largestPartNum < 0) |
| 289 | largestPartNum = theGPT.FindFirstFreePart(); |
srs5694 | 9ba5421 | 2010-05-18 23:24:02 -0400 | [diff] [blame] | 290 | if (theGPT.CreatePartition(largestPartNum - 1, startSector, endSector)) { |
| 291 | saveData = 1; |
| 292 | } else { |
| 293 | cerr << "Could not create partition " << largestPartNum << " from " |
| 294 | << startSector << " to " << endSector << "\n"; |
| 295 | neverSaveData = 1; |
| 296 | } // if/else |
| 297 | break; |
srs5694 | 73ba479 | 2010-01-12 18:18:17 -0500 | [diff] [blame] | 298 | case 'o': |
| 299 | theGPT.JustLooking(0); |
| 300 | theGPT.ClearGPTData(); |
| 301 | saveData = 1; |
| 302 | break; |
| 303 | case 'p': |
| 304 | theGPT.DisplayGPTData(); |
| 305 | break; |
| 306 | case 'P': |
| 307 | pretend = 1; |
| 308 | break; |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 309 | case 'r': |
| 310 | theGPT.JustLooking(0); |
| 311 | uint64_t p1, p2; |
| 312 | p1 = GetInt(twoParts, 1) - 1; |
| 313 | p2 = GetInt(twoParts, 2) - 1; |
| 314 | if (theGPT.SwapPartitions((uint32_t) p1, (uint32_t) p2) == 0) { |
| 315 | neverSaveData = 1; |
| 316 | cerr << "Cannot swap partitions " << p1 + 1 << " and " << p2 + 1 << "\n"; |
| 317 | } else saveData = 1; |
| 318 | break; |
srs5694 | 9ba5421 | 2010-05-18 23:24:02 -0400 | [diff] [blame] | 319 | case 'R': |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 320 | secondDevice = theGPT; |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 321 | secondDevice.SetDisk(outDevice); |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 322 | secondDevice.JustLooking(0); |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 323 | secondDevice.SaveGPTData(1); |
srs5694 | 9ba5421 | 2010-05-18 23:24:02 -0400 | [diff] [blame] | 324 | break; |
srs5694 | 73ba479 | 2010-01-12 18:18:17 -0500 | [diff] [blame] | 325 | case 's': |
| 326 | theGPT.JustLooking(0); |
| 327 | theGPT.SortGPT(); |
| 328 | saveData = 1; |
| 329 | break; |
| 330 | case 'S': |
| 331 | theGPT.JustLooking(0); |
| 332 | if (theGPT.SetGPTSize(tableSize) == 0) |
| 333 | neverSaveData = 1; |
| 334 | else |
| 335 | saveData = 1; |
| 336 | break; |
| 337 | case 't': |
| 338 | theGPT.JustLooking(0); |
| 339 | partNum = (int) GetInt(typeCode, 1) - 1; |
srs5694 | 82f3f0b | 2010-09-22 10:50:24 -0400 | [diff] [blame] | 340 | typeHelper = GetString(typeCode, 2); |
| 341 | if ((typeHelper != (GUIDData) "00000000-0000-0000-0000-000000000000") && |
| 342 | (theGPT.ChangePartType(partNum, typeHelper))) { |
srs5694 | 73ba479 | 2010-01-12 18:18:17 -0500 | [diff] [blame] | 343 | saveData = 1; |
| 344 | } else { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 345 | cerr << "Could not change partition " << partNum + 1 |
srs5694 | 327129e | 2010-09-22 01:07:31 -0400 | [diff] [blame] | 346 | << "'s type code to " << GetString(typeCode, 2) << "!\n"; |
srs5694 | 73ba479 | 2010-01-12 18:18:17 -0500 | [diff] [blame] | 347 | neverSaveData = 1; |
| 348 | } // if/else |
| 349 | free(typeCode); |
| 350 | break; |
| 351 | case 'T': |
| 352 | theGPT.JustLooking(0); |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 353 | theGPT.XFormDisklabel(bsdPartNum - 1); |
srs5694 | 73ba479 | 2010-01-12 18:18:17 -0500 | [diff] [blame] | 354 | saveData = 1; |
| 355 | break; |
srs5694 | 9ba5421 | 2010-05-18 23:24:02 -0400 | [diff] [blame] | 356 | case 'u': |
| 357 | theGPT.JustLooking(0); |
| 358 | saveData = 1; |
| 359 | gptPartNum = (int) GetInt(partGUID, 1) - 1; |
| 360 | theGPT.SetPartitionGUID(gptPartNum, GetString(partGUID, 2).c_str()); |
| 361 | break; |
| 362 | case 'U': |
| 363 | theGPT.JustLooking(0); |
| 364 | saveData = 1; |
| 365 | theGPT.SetDiskGUID(diskGUID); |
| 366 | break; |
srs5694 | 73ba479 | 2010-01-12 18:18:17 -0500 | [diff] [blame] | 367 | case 'v': |
| 368 | theGPT.Verify(); |
| 369 | break; |
| 370 | case 'z': |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 371 | if (!pretend) { |
| 372 | theGPT.DestroyGPT(); |
| 373 | } // if |
srs5694 | 73ba479 | 2010-01-12 18:18:17 -0500 | [diff] [blame] | 374 | saveNonGPT = 0; |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 375 | saveData = 0; |
| 376 | break; |
| 377 | case 'Z': |
| 378 | if (!pretend) { |
| 379 | theGPT.DestroyGPT(); |
| 380 | theGPT.DestroyMBR(); |
| 381 | } // if |
| 382 | saveNonGPT = 0; |
| 383 | saveData = 0; |
srs5694 | 73ba479 | 2010-01-12 18:18:17 -0500 | [diff] [blame] | 384 | break; |
| 385 | default: |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 386 | cerr << "Unknown option (-" << opt << ")!\n"; |
srs5694 | 73ba479 | 2010-01-12 18:18:17 -0500 | [diff] [blame] | 387 | break; |
| 388 | } // switch |
| 389 | } // while |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 390 | if ((saveData) && (!neverSaveData) && (saveNonGPT) && (!pretend)) { |
srs5694 | 73ba479 | 2010-01-12 18:18:17 -0500 | [diff] [blame] | 391 | theGPT.SaveGPTData(1); |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 392 | } |
srs5694 | 73ba479 | 2010-01-12 18:18:17 -0500 | [diff] [blame] | 393 | if (saveData && (!saveNonGPT)) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 394 | cout << "Non-GPT disk; not saving changes. Use -g to override.\n"; |
srs5694 | 73ba479 | 2010-01-12 18:18:17 -0500 | [diff] [blame] | 395 | retval = 3; |
| 396 | } // if |
| 397 | if (neverSaveData) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 398 | cerr << "Error encountered; not saving changes.\n"; |
srs5694 | 73ba479 | 2010-01-12 18:18:17 -0500 | [diff] [blame] | 399 | retval = 4; |
| 400 | } // if |
| 401 | } else { // if loaded OK |
srs5694 | 9ddc14b | 2010-08-22 22:44:42 -0400 | [diff] [blame] | 402 | poptResetContext(poptCon); |
| 403 | // Do a few types of operations even if there are problems.... |
| 404 | while ((opt = poptGetNextOpt(poptCon)) > 0) { |
| 405 | switch (opt) { |
| 406 | case 'v': |
srs5694 | 327129e | 2010-09-22 01:07:31 -0400 | [diff] [blame] | 407 | cout << "Verification may miss some problems or report too many!\n"; |
srs5694 | 9ddc14b | 2010-08-22 22:44:42 -0400 | [diff] [blame] | 408 | theGPT.Verify(); |
| 409 | break; |
| 410 | case 'z': |
| 411 | if (!pretend) { |
| 412 | theGPT.DestroyGPT(); |
| 413 | } // if |
| 414 | saveNonGPT = 0; |
| 415 | saveData = 0; |
| 416 | break; |
| 417 | case 'Z': |
| 418 | if (!pretend) { |
| 419 | theGPT.DestroyGPT(); |
| 420 | theGPT.DestroyMBR(); |
| 421 | } // if |
| 422 | saveNonGPT = 0; |
| 423 | saveData = 0; |
| 424 | break; |
| 425 | } // switch |
| 426 | } // while |
srs5694 | 73ba479 | 2010-01-12 18:18:17 -0500 | [diff] [blame] | 427 | retval = 2; |
| 428 | } // if/else loaded OK |
| 429 | } // if (device != NULL) |
| 430 | poptFreeContext(poptCon); |
srs5694 | 1e09372 | 2010-01-05 00:14:19 -0500 | [diff] [blame] | 431 | |
srs5694 | 73ba479 | 2010-01-12 18:18:17 -0500 | [diff] [blame] | 432 | return retval; |
| 433 | } // main |
srs5694 | 1e09372 | 2010-01-05 00:14:19 -0500 | [diff] [blame] | 434 | |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 435 | // Create a hybrid or regular MBR from GPT data structures |
srs5694 | 5a08175 | 2010-09-24 20:39:41 -0400 | [diff] [blame] | 436 | int BuildMBR(GPTData & theGPT, char* argument, int isHybrid) { |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 437 | int numParts, allOK = 1, i, origPartNum; |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 438 | MBRPart newPart; |
| 439 | BasicMBRData newMBR; |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 440 | |
srs5694 | 5a08175 | 2010-09-24 20:39:41 -0400 | [diff] [blame] | 441 | if ((&theGPT != NULL) && (argument != NULL)) { |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 442 | numParts = CountColons(argument) + 1; |
| 443 | if (numParts <= (4 - isHybrid)) { |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 444 | newMBR.SetDisk(theGPT.GetDisk()); |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 445 | for (i = 0; i < numParts; i++) { |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 446 | origPartNum = GetInt(argument, i + 1) - 1; |
srs5694 | 815fb65 | 2011-03-18 12:35:56 -0400 | [diff] [blame^] | 447 | if (theGPT.IsUsedPartNum(origPartNum)) { |
| 448 | newPart.SetInclusion(PRIMARY); |
| 449 | newPart.SetLocation(theGPT[origPartNum].GetFirstLBA(), |
| 450 | theGPT[origPartNum].GetLengthLBA()); |
| 451 | newPart.SetStatus(0); |
| 452 | newPart.SetType((uint8_t)(theGPT[origPartNum].GetHexType() / 0x0100)); |
| 453 | newMBR.AddPart(i + isHybrid, newPart); |
| 454 | } else { |
| 455 | cerr << "Partition " << origPartNum << " does not exist! Aborting operation!\n"; |
| 456 | allOK = 0; |
| 457 | } // if/else |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 458 | } // for |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 459 | if (isHybrid) { |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 460 | newPart.SetInclusion(PRIMARY); |
| 461 | newPart.SetLocation(1, newMBR.FindLastInFree(1)); |
| 462 | newPart.SetStatus(0); |
| 463 | newPart.SetType(0xEE); |
| 464 | newMBR.AddPart(0, newPart); |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 465 | } // if |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 466 | theGPT.SetProtectiveMBR(newMBR); |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 467 | } else allOK = 0; |
| 468 | } else allOK = 0; |
| 469 | if (!allOK) |
| 470 | cerr << "Problem creating MBR!\n"; |
| 471 | return allOK; |
| 472 | } // BuildMBR() |
| 473 | |
srs5694 | 9a46b04 | 2011-03-15 00:34:10 -0400 | [diff] [blame] | 474 | // Returns the number of colons in argument string, ignoring the |
| 475 | // first character (thus, a leading colon is ignored, as GetString() |
| 476 | // does). |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 477 | int CountColons(char* argument) { |
srs5694 | 9a46b04 | 2011-03-15 00:34:10 -0400 | [diff] [blame] | 478 | int num = 0; |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 479 | |
srs5694 | 9a46b04 | 2011-03-15 00:34:10 -0400 | [diff] [blame] | 480 | while ((argument[0] != '\0') && (argument = strchr(&argument[1], ':'))) |
| 481 | num++; |
| 482 | |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 483 | return num; |
| 484 | } // CountColons() |
srs5694 | 5a60853 | 2011-03-17 13:53:01 -0400 | [diff] [blame] | 485 | |
| 486 | // Extract integer data from argument string, which should be colon-delimited |
| 487 | uint64_t GetInt(const string & argument, int itemNum) { |
| 488 | uint64_t retval; |
| 489 | |
| 490 | istringstream inString(GetString(argument, itemNum)); |
| 491 | inString >> retval; |
| 492 | return retval; |
| 493 | } // GetInt() |
| 494 | |
| 495 | // Extract string data from argument string, which should be colon-delimited |
| 496 | // If string begins with a colon, that colon is skipped in the counting. If an |
| 497 | // invalid itemNum is specified, returns an empty string. |
| 498 | string GetString(string argument, int itemNum) { |
| 499 | size_t startPos = 0, endPos = 0; |
| 500 | string retVal = ""; |
| 501 | int foundLast = 0; |
| 502 | int numFound = 0; |
| 503 | |
| 504 | if (argument[0] == ':') |
| 505 | argument.erase(0, 1); |
| 506 | while ((numFound < itemNum) && (!foundLast)) { |
| 507 | endPos = argument.find(':', startPos); |
| 508 | numFound++; |
| 509 | if (endPos == string::npos) { |
| 510 | foundLast = 1; |
| 511 | endPos = argument.length(); |
| 512 | } else if (numFound < itemNum) { |
| 513 | startPos = endPos + 1; |
| 514 | } // if/elseif |
| 515 | } // while |
| 516 | if ((numFound == itemNum) && (numFound > 0)) |
| 517 | retVal = argument.substr(startPos, endPos - startPos); |
| 518 | |
| 519 | return retVal; |
| 520 | } // GetString() |