srs5694 | 3860cbe | 2011-09-10 20:29:53 -0400 | [diff] [blame] | 1 | /* |
| 2 | Implementation of GPTData class derivative with popt-based command |
| 3 | line processing |
Roderick W. Smith | 820d1d0 | 2014-02-20 13:25:22 -0500 | [diff] [blame] | 4 | Copyright (C) 2010-2014 Roderick W. Smith |
srs5694 | 3860cbe | 2011-09-10 20:29:53 -0400 | [diff] [blame] | 5 | |
| 6 | This program is free software; you can redistribute it and/or modify |
| 7 | it under the terms of the GNU General Public License as published by |
| 8 | the Free Software Foundation; either version 2 of the License, or |
| 9 | (at your option) any later version. |
| 10 | |
| 11 | This program is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | GNU General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License along |
| 17 | with this program; if not, write to the Free Software Foundation, Inc., |
| 18 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | */ |
| 20 | |
| 21 | #include <string.h> |
| 22 | #include <string> |
| 23 | #include <iostream> |
| 24 | #include <sstream> |
| 25 | #include <errno.h> |
Aurimas Liutikas | 74b7490 | 2016-05-10 18:53:54 -0700 | [diff] [blame] | 26 | #include <popt.h> |
srs5694 | 3860cbe | 2011-09-10 20:29:53 -0400 | [diff] [blame] | 27 | #include "gptcl.h" |
| 28 | |
| 29 | GPTDataCL::GPTDataCL(void) { |
| 30 | attributeOperation = backupFile = partName = hybrids = newPartInfo = NULL; |
| 31 | mbrParts = twoParts = outDevice = typeCode = partGUID = diskGUID = NULL; |
| 32 | alignment = DEFAULT_ALIGNMENT; |
| 33 | deletePartNum = infoPartNum = largestPartNum = bsdPartNum = 0; |
| 34 | tableSize = GPT_SIZE; |
| 35 | } // GPTDataCL constructor |
| 36 | |
| 37 | GPTDataCL::GPTDataCL(string filename) { |
| 38 | } // GPTDataCL constructor with filename |
| 39 | |
| 40 | GPTDataCL::~GPTDataCL(void) { |
| 41 | } // GPTDataCL destructor |
| 42 | |
| 43 | void GPTDataCL::LoadBackupFile(string backupFile, int &saveData, int &neverSaveData) { |
| 44 | if (LoadGPTBackup(backupFile) == 1) { |
| 45 | JustLooking(0); |
| 46 | saveData = 1; |
| 47 | } else { |
| 48 | saveData = 0; |
| 49 | neverSaveData = 1; |
| 50 | cerr << "Error loading backup file!\n"; |
| 51 | } // else |
srs5694 | 0741fa2 | 2013-01-09 12:55:40 -0500 | [diff] [blame] | 52 | } // GPTDataCL::LoadBackupFile() |
srs5694 | 3860cbe | 2011-09-10 20:29:53 -0400 | [diff] [blame] | 53 | |
srs5694 | d1b11e8 | 2011-09-18 21:12:28 -0400 | [diff] [blame] | 54 | // Perform the actions specified on the command line. This is necessarily one |
| 55 | // monster of a function! |
| 56 | // Returns values: |
| 57 | // 0 = success |
| 58 | // 1 = too few arguments |
| 59 | // 2 = error when reading partition table |
| 60 | // 3 = non-GPT disk and no -g option |
| 61 | // 4 = unable to save changes |
| 62 | // 8 = disk replication operation (-R) failed |
srs5694 | 3860cbe | 2011-09-10 20:29:53 -0400 | [diff] [blame] | 63 | int GPTDataCL::DoOptions(int argc, char* argv[]) { |
| 64 | GPTData secondDevice; |
| 65 | int opt, numOptions = 0, saveData = 0, neverSaveData = 0; |
Roderick W. Smith | d28495e | 2014-03-02 11:37:20 -0500 | [diff] [blame] | 66 | int partNum = 0, newPartNum = -1, saveNonGPT = 1, retval = 0, pretend = 0; |
srs5694 | e842bc1 | 2012-02-03 11:27:05 -0500 | [diff] [blame] | 67 | uint64_t low, high, startSector, endSector, sSize; |
srs5694 | 3860cbe | 2011-09-10 20:29:53 -0400 | [diff] [blame] | 68 | uint64_t temp; // temporary variable; free to use in any case |
| 69 | char *device; |
| 70 | string cmd, typeGUID, name; |
| 71 | PartType typeHelper; |
| 72 | |
| 73 | struct poptOption theOptions[] = |
| 74 | { |
Aurimas Liutikas | 74b7490 | 2016-05-10 18:53:54 -0700 | [diff] [blame] | 75 | {"attributes", 'A', POPT_ARG_STRING, &attributeOperation, 'A', "operate on partition attributes", |
| 76 | "list|[partnum:show|or|nand|xor|=|set|clear|toggle|get[:bitnum|hexbitmask]]"}, |
srs5694 | 3860cbe | 2011-09-10 20:29:53 -0400 | [diff] [blame] | 77 | {"set-alignment", 'a', POPT_ARG_INT, &alignment, 'a', "set sector alignment", "value"}, |
| 78 | {"backup", 'b', POPT_ARG_STRING, &backupFile, 'b', "backup GPT to file", "file"}, |
| 79 | {"change-name", 'c', POPT_ARG_STRING, &partName, 'c', "change partition's name", "partnum:name"}, |
| 80 | {"recompute-chs", 'C', POPT_ARG_NONE, NULL, 'C', "recompute CHS values in protective/hybrid MBR", ""}, |
| 81 | {"delete", 'd', POPT_ARG_INT, &deletePartNum, 'd', "delete a partition", "partnum"}, |
| 82 | {"display-alignment", 'D', POPT_ARG_NONE, NULL, 'D', "show number of sectors per allocation block", ""}, |
| 83 | {"move-second-header", 'e', POPT_ARG_NONE, NULL, 'e', "move second header to end of disk", ""}, |
| 84 | {"end-of-largest", 'E', POPT_ARG_NONE, NULL, 'E', "show end of largest free block", ""}, |
| 85 | {"first-in-largest", 'f', POPT_ARG_NONE, NULL, 'f', "show start of the largest free block", ""}, |
| 86 | {"first-aligned-in-largest", 'F', POPT_ARG_NONE, NULL, 'F', "show start of the largest free block, aligned", ""}, |
| 87 | {"mbrtogpt", 'g', POPT_ARG_NONE, NULL, 'g', "convert MBR to GPT", ""}, |
| 88 | {"randomize-guids", 'G', POPT_ARG_NONE, NULL, 'G', "randomize disk and partition GUIDs", ""}, |
| 89 | {"hybrid", 'h', POPT_ARG_STRING, &hybrids, 'h', "create hybrid MBR", "partnum[:partnum...]"}, |
| 90 | {"info", 'i', POPT_ARG_INT, &infoPartNum, 'i', "show detailed information on partition", "partnum"}, |
Greg Hartman | 2c2deeb | 2016-04-21 18:20:25 -0700 | [diff] [blame] | 91 | {"skip-sync", 'j', POPT_ARG_NONE, NULL, 'j', "Don't atempt to sync and update the parittion table", ""}, |
srs5694 | 3860cbe | 2011-09-10 20:29:53 -0400 | [diff] [blame] | 92 | {"load-backup", 'l', POPT_ARG_STRING, &backupFile, 'l', "load GPT backup from file", "file"}, |
| 93 | {"list-types", 'L', POPT_ARG_NONE, NULL, 'L', "list known partition types", ""}, |
| 94 | {"gpttombr", 'm', POPT_ARG_STRING, &mbrParts, 'm', "convert GPT to MBR", "partnum[:partnum...]"}, |
| 95 | {"new", 'n', POPT_ARG_STRING, &newPartInfo, 'n', "create new partition", "partnum:start:end"}, |
| 96 | {"largest-new", 'N', POPT_ARG_INT, &largestPartNum, 'N', "create largest possible new partition", "partnum"}, |
| 97 | {"clear", 'o', POPT_ARG_NONE, NULL, 'o', "clear partition table", ""}, |
Aurimas Liutikas | 74b7490 | 2016-05-10 18:53:54 -0700 | [diff] [blame] | 98 | {"print-mbr", 'O', POPT_ARG_NONE, NULL, 'O', "print MBR partition table", ""}, |
srs5694 | 3860cbe | 2011-09-10 20:29:53 -0400 | [diff] [blame] | 99 | {"print", 'p', POPT_ARG_NONE, NULL, 'p', "print partition table", ""}, |
| 100 | {"pretend", 'P', POPT_ARG_NONE, NULL, 'P', "make changes in memory, but don't write them", ""}, |
| 101 | {"transpose", 'r', POPT_ARG_STRING, &twoParts, 'r', "transpose two partitions", "partnum:partnum"}, |
| 102 | {"replicate", 'R', POPT_ARG_STRING, &outDevice, 'R', "replicate partition table", "device_filename"}, |
| 103 | {"sort", 's', POPT_ARG_NONE, NULL, 's', "sort partition table entries", ""}, |
| 104 | {"resize-table", 'S', POPT_ARG_INT, &tableSize, 'S', "resize partition table", "numparts"}, |
| 105 | {"typecode", 't', POPT_ARG_STRING, &typeCode, 't', "change partition type code", "partnum:{hexcode|GUID}"}, |
| 106 | {"transform-bsd", 'T', POPT_ARG_INT, &bsdPartNum, 'T', "transform BSD disklabel partition to GPT", "partnum"}, |
| 107 | {"partition-guid", 'u', POPT_ARG_STRING, &partGUID, 'u', "set partition GUID", "partnum:guid"}, |
| 108 | {"disk-guid", 'U', POPT_ARG_STRING, &diskGUID, 'U', "set disk GUID", "guid"}, |
| 109 | {"verify", 'v', POPT_ARG_NONE, NULL, 'v', "check partition table integrity", ""}, |
| 110 | {"version", 'V', POPT_ARG_NONE, NULL, 'V', "display version information", ""}, |
| 111 | {"zap", 'z', POPT_ARG_NONE, NULL, 'z', "zap (destroy) GPT (but not MBR) data structures", ""}, |
| 112 | {"zap-all", 'Z', POPT_ARG_NONE, NULL, 'Z', "zap (destroy) GPT and MBR data structures", ""}, |
Aurimas Liutikas | fcad060 | 2016-05-10 19:16:10 -0700 | [diff] [blame] | 113 | POPT_AUTOHELP { NULL, 0, 0, NULL, 0, NULL, NULL } |
srs5694 | 3860cbe | 2011-09-10 20:29:53 -0400 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | // Create popt context... |
| 117 | poptCon = poptGetContext(NULL, argc, (const char**) argv, theOptions, 0); |
srs5694 | 0741fa2 | 2013-01-09 12:55:40 -0500 | [diff] [blame] | 118 | |
srs5694 | 3860cbe | 2011-09-10 20:29:53 -0400 | [diff] [blame] | 119 | poptSetOtherOptionHelp(poptCon, " [OPTION...] <device>"); |
srs5694 | 0741fa2 | 2013-01-09 12:55:40 -0500 | [diff] [blame] | 120 | |
srs5694 | 3860cbe | 2011-09-10 20:29:53 -0400 | [diff] [blame] | 121 | if (argc < 2) { |
| 122 | poptPrintUsage(poptCon, stderr, 0); |
srs5694 | d1b11e8 | 2011-09-18 21:12:28 -0400 | [diff] [blame] | 123 | return 1; |
srs5694 | 3860cbe | 2011-09-10 20:29:53 -0400 | [diff] [blame] | 124 | } |
srs5694 | 0741fa2 | 2013-01-09 12:55:40 -0500 | [diff] [blame] | 125 | |
srs5694 | 3860cbe | 2011-09-10 20:29:53 -0400 | [diff] [blame] | 126 | // Do one loop through the options to find the device filename and deal |
| 127 | // with options that don't require a device filename, to flag destructive |
Roderick W. Smith | d28495e | 2014-03-02 11:37:20 -0500 | [diff] [blame] | 128 | // (o, z, or Z) options, and to flag presence of a --pretend/-P option |
srs5694 | 3860cbe | 2011-09-10 20:29:53 -0400 | [diff] [blame] | 129 | while ((opt = poptGetNextOpt(poptCon)) > 0) { |
| 130 | switch (opt) { |
| 131 | case 'A': |
| 132 | cmd = GetString(attributeOperation, 1); |
| 133 | if (cmd == "list") |
| 134 | Attributes::ListAttributes(); |
| 135 | break; |
| 136 | case 'L': |
Roderick W. Smith | e3ee733 | 2013-09-24 12:56:11 -0400 | [diff] [blame] | 137 | typeHelper.ShowAllTypes(0); |
srs5694 | 3860cbe | 2011-09-10 20:29:53 -0400 | [diff] [blame] | 138 | break; |
| 139 | case 'P': |
| 140 | pretend = 1; |
| 141 | break; |
| 142 | case 'V': |
| 143 | cout << "GPT fdisk (sgdisk) version " << GPTFDISK_VERSION << "\n\n"; |
| 144 | break; |
| 145 | default: |
| 146 | break; |
| 147 | } // switch |
| 148 | numOptions++; |
| 149 | } // while |
srs5694 | 0741fa2 | 2013-01-09 12:55:40 -0500 | [diff] [blame] | 150 | |
srs5694 | 3860cbe | 2011-09-10 20:29:53 -0400 | [diff] [blame] | 151 | // Assume first non-option argument is the device filename.... |
| 152 | device = (char*) poptGetArg(poptCon); |
| 153 | poptResetContext(poptCon); |
srs5694 | 0741fa2 | 2013-01-09 12:55:40 -0500 | [diff] [blame] | 154 | |
srs5694 | 3860cbe | 2011-09-10 20:29:53 -0400 | [diff] [blame] | 155 | if (device != NULL) { |
| 156 | JustLooking(); // reset as necessary |
| 157 | BeQuiet(); // Tell called functions to be less verbose & interactive |
| 158 | if (LoadPartitions((string) device)) { |
| 159 | if ((WhichWasUsed() == use_mbr) || (WhichWasUsed() == use_bsd)) |
| 160 | saveNonGPT = 0; // flag so we don't overwrite unless directed to do so |
| 161 | sSize = GetBlockSize(); |
| 162 | while ((opt = poptGetNextOpt(poptCon)) > 0) { |
| 163 | switch (opt) { |
| 164 | case 'A': { |
| 165 | if (cmd != "list") { |
| 166 | partNum = (int) GetInt(attributeOperation, 1) - 1; |
Roderick W. Smith | d28495e | 2014-03-02 11:37:20 -0500 | [diff] [blame] | 167 | if (partNum < 0) |
| 168 | partNum = newPartNum; |
srs5694 | 3860cbe | 2011-09-10 20:29:53 -0400 | [diff] [blame] | 169 | if ((partNum >= 0) && (partNum < (int) GetNumParts())) { |
| 170 | switch (ManageAttributes(partNum, GetString(attributeOperation, 2), |
| 171 | GetString(attributeOperation, 3))) { |
| 172 | case -1: |
| 173 | saveData = 0; |
| 174 | neverSaveData = 1; |
| 175 | break; |
| 176 | case 1: |
| 177 | JustLooking(0); |
| 178 | saveData = 1; |
| 179 | break; |
| 180 | default: |
| 181 | break; |
| 182 | } // switch |
| 183 | } else { |
| 184 | cerr << "Error: Invalid partition number " << partNum + 1 << "\n"; |
| 185 | saveData = 0; |
| 186 | neverSaveData = 1; |
| 187 | } // if/else reasonable partition # |
| 188 | } // if (cmd != "list") |
| 189 | break; |
| 190 | } // case 'A': |
| 191 | case 'a': |
| 192 | SetAlignment(alignment); |
| 193 | break; |
| 194 | case 'b': |
| 195 | SaveGPTBackup(backupFile); |
| 196 | free(backupFile); |
| 197 | break; |
| 198 | case 'c': |
Roderick W. Smith | d28495e | 2014-03-02 11:37:20 -0500 | [diff] [blame] | 199 | cout << "Setting name!\n"; |
srs5694 | 3860cbe | 2011-09-10 20:29:53 -0400 | [diff] [blame] | 200 | JustLooking(0); |
| 201 | partNum = (int) GetInt(partName, 1) - 1; |
Roderick W. Smith | d28495e | 2014-03-02 11:37:20 -0500 | [diff] [blame] | 202 | if (partNum < 0) |
| 203 | partNum = newPartNum; |
| 204 | cout << "partNum is " << partNum << "\n"; |
srs5694 | 0741fa2 | 2013-01-09 12:55:40 -0500 | [diff] [blame] | 205 | if ((partNum >= 0) && (partNum < (int) GetNumParts())) { |
Roderick W. Smith | d28495e | 2014-03-02 11:37:20 -0500 | [diff] [blame] | 206 | cout << "REALLY setting name!\n"; |
srs5694 | 0741fa2 | 2013-01-09 12:55:40 -0500 | [diff] [blame] | 207 | name = GetString(partName, 2); |
| 208 | if (SetName(partNum, (UnicodeString) name.c_str())) { |
| 209 | saveData = 1; |
| 210 | } else { |
| 211 | cerr << "Unable to set partition " << partNum + 1 |
| 212 | << "'s name to '" << GetString(partName, 2) << "'!\n"; |
| 213 | neverSaveData = 1; |
| 214 | } // if/else |
| 215 | free(partName); |
| 216 | } |
srs5694 | 3860cbe | 2011-09-10 20:29:53 -0400 | [diff] [blame] | 217 | break; |
| 218 | case 'C': |
| 219 | JustLooking(0); |
| 220 | RecomputeCHS(); |
| 221 | saveData = 1; |
| 222 | break; |
| 223 | case 'd': |
| 224 | JustLooking(0); |
| 225 | if (DeletePartition(deletePartNum - 1) == 0) { |
| 226 | cerr << "Error " << errno << " deleting partition!\n"; |
| 227 | neverSaveData = 1; |
| 228 | } else saveData = 1; |
| 229 | break; |
| 230 | case 'D': |
| 231 | cout << GetAlignment() << "\n"; |
| 232 | break; |
| 233 | case 'e': |
| 234 | JustLooking(0); |
| 235 | MoveSecondHeaderToEnd(); |
| 236 | saveData = 1; |
| 237 | break; |
| 238 | case 'E': |
| 239 | cout << FindLastInFree(FindFirstInLargest()) << "\n"; |
| 240 | break; |
| 241 | case 'f': |
| 242 | cout << FindFirstInLargest() << "\n"; |
| 243 | break; |
| 244 | case 'F': |
| 245 | temp = FindFirstInLargest(); |
| 246 | Align(&temp); |
| 247 | cout << temp << "\n"; |
| 248 | break; |
| 249 | case 'g': |
| 250 | JustLooking(0); |
| 251 | saveData = 1; |
| 252 | saveNonGPT = 1; |
| 253 | break; |
| 254 | case 'G': |
| 255 | JustLooking(0); |
| 256 | saveData = 1; |
| 257 | RandomizeGUIDs(); |
| 258 | break; |
| 259 | case 'h': |
| 260 | JustLooking(0); |
| 261 | if (BuildMBR(hybrids, 1) == 1) |
| 262 | saveData = 1; |
| 263 | break; |
| 264 | case 'i': |
Jeff Sharkey | d761ff5 | 2015-02-28 19:18:39 -0800 | [diff] [blame] | 265 | ShowPartDetails(infoPartNum - 1); |
srs5694 | 3860cbe | 2011-09-10 20:29:53 -0400 | [diff] [blame] | 266 | break; |
Greg Hartman | 2c2deeb | 2016-04-21 18:20:25 -0700 | [diff] [blame] | 267 | case 'j': |
| 268 | TurnOffSyncing(); |
| 269 | break; |
srs5694 | 3860cbe | 2011-09-10 20:29:53 -0400 | [diff] [blame] | 270 | case 'l': |
| 271 | LoadBackupFile(backupFile, saveData, neverSaveData); |
| 272 | free(backupFile); |
| 273 | break; |
| 274 | case 'L': |
| 275 | break; |
| 276 | case 'm': |
| 277 | JustLooking(0); |
| 278 | if (BuildMBR(mbrParts, 0) == 1) { |
| 279 | if (!pretend) { |
| 280 | if (SaveMBR()) { |
| 281 | DestroyGPT(); |
| 282 | } else |
| 283 | cerr << "Problem saving MBR!\n"; |
| 284 | } // if |
| 285 | saveNonGPT = 0; |
| 286 | pretend = 1; // Not really, but works around problem if -g is used with this... |
| 287 | saveData = 0; |
| 288 | } // if |
| 289 | break; |
| 290 | case 'n': |
| 291 | JustLooking(0); |
Roderick W. Smith | d28495e | 2014-03-02 11:37:20 -0500 | [diff] [blame] | 292 | newPartNum = (int) GetInt(newPartInfo, 1) - 1; |
| 293 | if (newPartNum < 0) |
| 294 | newPartNum = FindFirstFreePart(); |
srs5694 | 3860cbe | 2011-09-10 20:29:53 -0400 | [diff] [blame] | 295 | low = FindFirstInLargest(); |
srs5694 | f5dfbfa | 2013-02-14 20:47:14 -0500 | [diff] [blame] | 296 | Align(&low); |
srs5694 | 3860cbe | 2011-09-10 20:29:53 -0400 | [diff] [blame] | 297 | high = FindLastInFree(low); |
| 298 | startSector = IeeeToInt(GetString(newPartInfo, 2), sSize, low, high, low); |
| 299 | endSector = IeeeToInt(GetString(newPartInfo, 3), sSize, startSector, high, high); |
Roderick W. Smith | d28495e | 2014-03-02 11:37:20 -0500 | [diff] [blame] | 300 | if (CreatePartition(newPartNum, startSector, endSector)) { |
srs5694 | 3860cbe | 2011-09-10 20:29:53 -0400 | [diff] [blame] | 301 | saveData = 1; |
| 302 | } else { |
Roderick W. Smith | d28495e | 2014-03-02 11:37:20 -0500 | [diff] [blame] | 303 | cerr << "Could not create partition " << newPartNum + 1 << " from " |
| 304 | << startSector << " to " << endSector << "\n"; |
srs5694 | 3860cbe | 2011-09-10 20:29:53 -0400 | [diff] [blame] | 305 | neverSaveData = 1; |
| 306 | } // if/else |
| 307 | free(newPartInfo); |
| 308 | break; |
| 309 | case 'N': |
| 310 | JustLooking(0); |
| 311 | startSector = FindFirstInLargest(); |
srs5694 | f5dfbfa | 2013-02-14 20:47:14 -0500 | [diff] [blame] | 312 | Align(&startSector); |
srs5694 | 3860cbe | 2011-09-10 20:29:53 -0400 | [diff] [blame] | 313 | endSector = FindLastInFree(startSector); |
Aurimas Liutikas | 74b7490 | 2016-05-10 18:53:54 -0700 | [diff] [blame] | 314 | if (largestPartNum <= 0) |
| 315 | largestPartNum = FindFirstFreePart() + 1; |
srs5694 | 3860cbe | 2011-09-10 20:29:53 -0400 | [diff] [blame] | 316 | if (CreatePartition(largestPartNum - 1, startSector, endSector)) { |
| 317 | saveData = 1; |
| 318 | } else { |
| 319 | cerr << "Could not create partition " << largestPartNum << " from " |
| 320 | << startSector << " to " << endSector << "\n"; |
| 321 | neverSaveData = 1; |
| 322 | } // if/else |
| 323 | break; |
| 324 | case 'o': |
| 325 | JustLooking(0); |
| 326 | ClearGPTData(); |
| 327 | saveData = 1; |
| 328 | break; |
Aurimas Liutikas | 74b7490 | 2016-05-10 18:53:54 -0700 | [diff] [blame] | 329 | case 'O': |
| 330 | DisplayMBRData(); |
| 331 | break; |
srs5694 | 3860cbe | 2011-09-10 20:29:53 -0400 | [diff] [blame] | 332 | case 'p': |
| 333 | DisplayGPTData(); |
| 334 | break; |
| 335 | case 'P': |
| 336 | pretend = 1; |
| 337 | break; |
| 338 | case 'r': |
| 339 | JustLooking(0); |
| 340 | uint64_t p1, p2; |
| 341 | p1 = GetInt(twoParts, 1) - 1; |
| 342 | p2 = GetInt(twoParts, 2) - 1; |
| 343 | if (SwapPartitions((uint32_t) p1, (uint32_t) p2) == 0) { |
| 344 | neverSaveData = 1; |
| 345 | cerr << "Cannot swap partitions " << p1 + 1 << " and " << p2 + 1 << "\n"; |
| 346 | } else saveData = 1; |
| 347 | break; |
| 348 | case 'R': |
| 349 | secondDevice = *this; |
| 350 | secondDevice.SetDisk(outDevice); |
| 351 | secondDevice.JustLooking(0); |
| 352 | if (!secondDevice.SaveGPTData(1)) |
| 353 | retval = 8; |
| 354 | break; |
| 355 | case 's': |
| 356 | JustLooking(0); |
| 357 | SortGPT(); |
| 358 | saveData = 1; |
| 359 | break; |
| 360 | case 'S': |
| 361 | JustLooking(0); |
| 362 | if (SetGPTSize(tableSize) == 0) |
| 363 | neverSaveData = 1; |
| 364 | else |
| 365 | saveData = 1; |
| 366 | break; |
| 367 | case 't': |
| 368 | JustLooking(0); |
| 369 | partNum = (int) GetInt(typeCode, 1) - 1; |
Roderick W. Smith | d28495e | 2014-03-02 11:37:20 -0500 | [diff] [blame] | 370 | if (partNum < 0) |
| 371 | partNum = newPartNum; |
srs5694 | 0741fa2 | 2013-01-09 12:55:40 -0500 | [diff] [blame] | 372 | if ((partNum >= 0) && (partNum < (int) GetNumParts())) { |
| 373 | typeHelper = GetString(typeCode, 2); |
| 374 | if ((typeHelper != (GUIDData) "00000000-0000-0000-0000-000000000000") && |
| 375 | (ChangePartType(partNum, typeHelper))) { |
| 376 | saveData = 1; |
| 377 | } else { |
| 378 | cerr << "Could not change partition " << partNum + 1 |
| 379 | << "'s type code to " << GetString(typeCode, 2) << "!\n"; |
| 380 | neverSaveData = 1; |
| 381 | } // if/else |
srs5694 | 3860cbe | 2011-09-10 20:29:53 -0400 | [diff] [blame] | 382 | free(typeCode); |
srs5694 | 0741fa2 | 2013-01-09 12:55:40 -0500 | [diff] [blame] | 383 | } |
srs5694 | 3860cbe | 2011-09-10 20:29:53 -0400 | [diff] [blame] | 384 | break; |
| 385 | case 'T': |
| 386 | JustLooking(0); |
| 387 | XFormDisklabel(bsdPartNum - 1); |
| 388 | saveData = 1; |
| 389 | break; |
| 390 | case 'u': |
| 391 | JustLooking(0); |
| 392 | saveData = 1; |
srs5694 | 0741fa2 | 2013-01-09 12:55:40 -0500 | [diff] [blame] | 393 | partNum = (int) GetInt(partGUID, 1) - 1; |
Roderick W. Smith | d28495e | 2014-03-02 11:37:20 -0500 | [diff] [blame] | 394 | if (partNum < 0) |
| 395 | partNum = newPartNum; |
srs5694 | 0741fa2 | 2013-01-09 12:55:40 -0500 | [diff] [blame] | 396 | if ((partNum >= 0) && (partNum < (int) GetNumParts())) { |
| 397 | SetPartitionGUID(partNum, GetString(partGUID, 2).c_str()); |
| 398 | } |
srs5694 | 3860cbe | 2011-09-10 20:29:53 -0400 | [diff] [blame] | 399 | break; |
| 400 | case 'U': |
| 401 | JustLooking(0); |
| 402 | saveData = 1; |
| 403 | SetDiskGUID(diskGUID); |
| 404 | break; |
| 405 | case 'v': |
| 406 | Verify(); |
| 407 | break; |
| 408 | case 'z': |
| 409 | if (!pretend) { |
| 410 | DestroyGPT(); |
| 411 | } // if |
Aurimas Liutikas | 74b7490 | 2016-05-10 18:53:54 -0700 | [diff] [blame] | 412 | saveNonGPT = 1; |
srs5694 | 3860cbe | 2011-09-10 20:29:53 -0400 | [diff] [blame] | 413 | saveData = 0; |
| 414 | break; |
| 415 | case 'Z': |
| 416 | if (!pretend) { |
| 417 | DestroyGPT(); |
| 418 | DestroyMBR(); |
| 419 | } // if |
Aurimas Liutikas | 74b7490 | 2016-05-10 18:53:54 -0700 | [diff] [blame] | 420 | saveNonGPT = 1; |
srs5694 | 3860cbe | 2011-09-10 20:29:53 -0400 | [diff] [blame] | 421 | saveData = 0; |
| 422 | break; |
| 423 | default: |
| 424 | cerr << "Unknown option (-" << opt << ")!\n"; |
| 425 | break; |
| 426 | } // switch |
| 427 | } // while |
| 428 | } else { // if loaded OK |
| 429 | poptResetContext(poptCon); |
| 430 | // Do a few types of operations even if there are problems.... |
| 431 | while ((opt = poptGetNextOpt(poptCon)) > 0) { |
| 432 | switch (opt) { |
| 433 | case 'l': |
| 434 | LoadBackupFile(backupFile, saveData, neverSaveData); |
| 435 | cout << "Information: Loading backup partition table; will override earlier problems!\n"; |
| 436 | free(backupFile); |
| 437 | retval = 0; |
| 438 | break; |
| 439 | case 'o': |
| 440 | JustLooking(0); |
| 441 | ClearGPTData(); |
| 442 | saveData = 1; |
| 443 | cout << "Information: Creating fresh partition table; will override earlier problems!\n"; |
| 444 | retval = 0; |
| 445 | break; |
| 446 | case 'v': |
| 447 | cout << "Verification may miss some problems or report too many!\n"; |
| 448 | Verify(); |
| 449 | break; |
| 450 | case 'z': |
| 451 | if (!pretend) { |
| 452 | DestroyGPT(); |
| 453 | } // if |
Aurimas Liutikas | 74b7490 | 2016-05-10 18:53:54 -0700 | [diff] [blame] | 454 | saveNonGPT = 1; |
srs5694 | 3860cbe | 2011-09-10 20:29:53 -0400 | [diff] [blame] | 455 | saveData = 0; |
| 456 | break; |
| 457 | case 'Z': |
| 458 | if (!pretend) { |
| 459 | DestroyGPT(); |
| 460 | DestroyMBR(); |
| 461 | } // if |
Aurimas Liutikas | 74b7490 | 2016-05-10 18:53:54 -0700 | [diff] [blame] | 462 | saveNonGPT = 1; |
srs5694 | 3860cbe | 2011-09-10 20:29:53 -0400 | [diff] [blame] | 463 | saveData = 0; |
| 464 | break; |
| 465 | } // switch |
| 466 | } // while |
| 467 | retval = 2; |
| 468 | } // if/else loaded OK |
| 469 | if ((saveData) && (!neverSaveData) && (saveNonGPT) && (!pretend)) { |
Aurimas Liutikas | 74b7490 | 2016-05-10 18:53:54 -0700 | [diff] [blame] | 470 | if (!SaveGPTData(1)) |
| 471 | retval = 4; |
srs5694 | 3860cbe | 2011-09-10 20:29:53 -0400 | [diff] [blame] | 472 | } |
| 473 | if (saveData && (!saveNonGPT)) { |
| 474 | cout << "Non-GPT disk; not saving changes. Use -g to override.\n"; |
| 475 | retval = 3; |
| 476 | } // if |
| 477 | if (neverSaveData) { |
| 478 | cerr << "Error encountered; not saving changes.\n"; |
| 479 | retval = 4; |
| 480 | } // if |
| 481 | } // if (device != NULL) |
| 482 | poptFreeContext(poptCon); |
| 483 | return retval; |
| 484 | } // GPTDataCL::DoOptions() |
| 485 | |
| 486 | // Create a hybrid or regular MBR from GPT data structures |
| 487 | int GPTDataCL::BuildMBR(char* argument, int isHybrid) { |
| 488 | int numParts, allOK = 1, i, origPartNum; |
| 489 | MBRPart newPart; |
| 490 | BasicMBRData newMBR; |
Roderick W. Smith | 820d1d0 | 2014-02-20 13:25:22 -0500 | [diff] [blame] | 491 | |
srs5694 | 3860cbe | 2011-09-10 20:29:53 -0400 | [diff] [blame] | 492 | if (argument != NULL) { |
| 493 | numParts = CountColons(argument) + 1; |
| 494 | if (numParts <= (4 - isHybrid)) { |
| 495 | newMBR.SetDisk(GetDisk()); |
| 496 | for (i = 0; i < numParts; i++) { |
| 497 | origPartNum = GetInt(argument, i + 1) - 1; |
Roderick W. Smith | a345a92 | 2014-02-22 12:12:32 -0500 | [diff] [blame] | 498 | if (IsUsedPartNum(origPartNum) && (partitions[origPartNum].IsSizedForMBR() == MBR_SIZED_GOOD)) { |
srs5694 | 3860cbe | 2011-09-10 20:29:53 -0400 | [diff] [blame] | 499 | newPart.SetInclusion(PRIMARY); |
| 500 | newPart.SetLocation(operator[](origPartNum).GetFirstLBA(), |
| 501 | operator[](origPartNum).GetLengthLBA()); |
| 502 | newPart.SetStatus(0); |
| 503 | newPart.SetType((uint8_t)(operator[](origPartNum).GetHexType() / 0x0100)); |
| 504 | newMBR.AddPart(i + isHybrid, newPart); |
| 505 | } else { |
Roderick W. Smith | 820d1d0 | 2014-02-20 13:25:22 -0500 | [diff] [blame] | 506 | cerr << "Original partition " << origPartNum + 1 << " does not exist or is too big! Aborting operation!\n"; |
srs5694 | 3860cbe | 2011-09-10 20:29:53 -0400 | [diff] [blame] | 507 | allOK = 0; |
| 508 | } // if/else |
| 509 | } // for |
| 510 | if (isHybrid) { |
| 511 | newPart.SetInclusion(PRIMARY); |
| 512 | newPart.SetLocation(1, newMBR.FindLastInFree(1)); |
| 513 | newPart.SetStatus(0); |
| 514 | newPart.SetType(0xEE); |
| 515 | newMBR.AddPart(0, newPart); |
| 516 | } // if |
Roderick W. Smith | a345a92 | 2014-02-22 12:12:32 -0500 | [diff] [blame] | 517 | if (allOK) |
| 518 | SetProtectiveMBR(newMBR); |
srs5694 | 3860cbe | 2011-09-10 20:29:53 -0400 | [diff] [blame] | 519 | } else allOK = 0; |
| 520 | } else allOK = 0; |
| 521 | if (!allOK) |
| 522 | cerr << "Problem creating MBR!\n"; |
| 523 | return allOK; |
| 524 | } // GPTDataCL::BuildMBR() |
| 525 | |
| 526 | // Returns the number of colons in argument string, ignoring the |
| 527 | // first character (thus, a leading colon is ignored, as GetString() |
| 528 | // does). |
| 529 | int CountColons(char* argument) { |
| 530 | int num = 0; |
srs5694 | 0741fa2 | 2013-01-09 12:55:40 -0500 | [diff] [blame] | 531 | |
srs5694 | 3860cbe | 2011-09-10 20:29:53 -0400 | [diff] [blame] | 532 | while ((argument[0] != '\0') && (argument = strchr(&argument[1], ':'))) |
| 533 | num++; |
srs5694 | 0741fa2 | 2013-01-09 12:55:40 -0500 | [diff] [blame] | 534 | |
srs5694 | 3860cbe | 2011-09-10 20:29:53 -0400 | [diff] [blame] | 535 | return num; |
| 536 | } // GPTDataCL::CountColons() |
| 537 | |
| 538 | // Extract integer data from argument string, which should be colon-delimited |
| 539 | uint64_t GetInt(const string & argument, int itemNum) { |
| 540 | uint64_t retval; |
srs5694 | 0741fa2 | 2013-01-09 12:55:40 -0500 | [diff] [blame] | 541 | |
srs5694 | 3860cbe | 2011-09-10 20:29:53 -0400 | [diff] [blame] | 542 | istringstream inString(GetString(argument, itemNum)); |
| 543 | inString >> retval; |
| 544 | return retval; |
| 545 | } // GPTDataCL::GetInt() |
| 546 | |
| 547 | // Extract string data from argument string, which should be colon-delimited |
| 548 | // If string begins with a colon, that colon is skipped in the counting. If an |
| 549 | // invalid itemNum is specified, returns an empty string. |
| 550 | string GetString(string argument, int itemNum) { |
| 551 | size_t startPos = 0, endPos = 0; |
| 552 | string retVal = ""; |
| 553 | int foundLast = 0; |
| 554 | int numFound = 0; |
srs5694 | 0741fa2 | 2013-01-09 12:55:40 -0500 | [diff] [blame] | 555 | |
srs5694 | 3860cbe | 2011-09-10 20:29:53 -0400 | [diff] [blame] | 556 | if (argument[0] == ':') |
| 557 | argument.erase(0, 1); |
| 558 | while ((numFound < itemNum) && (!foundLast)) { |
| 559 | endPos = argument.find(':', startPos); |
| 560 | numFound++; |
| 561 | if (endPos == string::npos) { |
| 562 | foundLast = 1; |
| 563 | endPos = argument.length(); |
| 564 | } else if (numFound < itemNum) { |
| 565 | startPos = endPos + 1; |
| 566 | } // if/elseif |
| 567 | } // while |
| 568 | if ((numFound == itemNum) && (numFound > 0)) |
| 569 | retVal = argument.substr(startPos, endPos - startPos); |
srs5694 | 0741fa2 | 2013-01-09 12:55:40 -0500 | [diff] [blame] | 570 | |
srs5694 | 3860cbe | 2011-09-10 20:29:53 -0400 | [diff] [blame] | 571 | return retVal; |
| 572 | } // GetString() |