blob: 31d05b587bdbe16428408728112dbe795cc1fc79 [file] [log] [blame]
srs56943860cbe2011-09-10 20:29:53 -04001/*
2 Implementation of GPTData class derivative with popt-based command
3 line processing
4 Copyright (C) 2010-2011 Roderick W. Smith
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>
26#include <popt.h>
27#include "gptcl.h"
28
29GPTDataCL::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
37GPTDataCL::GPTDataCL(string filename) {
38} // GPTDataCL constructor with filename
39
40GPTDataCL::~GPTDataCL(void) {
41} // GPTDataCL destructor
42
43void 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
srs56940741fa22013-01-09 12:55:40 -050052} // GPTDataCL::LoadBackupFile()
srs56943860cbe2011-09-10 20:29:53 -040053
srs5694d1b11e82011-09-18 21:12:28 -040054// 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
srs56943860cbe2011-09-10 20:29:53 -040063int GPTDataCL::DoOptions(int argc, char* argv[]) {
64 GPTData secondDevice;
65 int opt, numOptions = 0, saveData = 0, neverSaveData = 0;
66 int partNum = 0, saveNonGPT = 1, retval = 0, pretend = 0;
srs5694e842bc12012-02-03 11:27:05 -050067 uint64_t low, high, startSector, endSector, sSize;
srs56943860cbe2011-09-10 20:29:53 -040068 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 {
75 {"attributes", 'A', POPT_ARG_STRING, &attributeOperation, 'A', "operate on partition attributes", "list|[partnum:show|or|nand|xor|=|set|clear|toggle|get[:bitnum|hexbitmask]]"},
76 {"set-alignment", 'a', POPT_ARG_INT, &alignment, 'a', "set sector alignment", "value"},
77 {"backup", 'b', POPT_ARG_STRING, &backupFile, 'b', "backup GPT to file", "file"},
78 {"change-name", 'c', POPT_ARG_STRING, &partName, 'c', "change partition's name", "partnum:name"},
79 {"recompute-chs", 'C', POPT_ARG_NONE, NULL, 'C', "recompute CHS values in protective/hybrid MBR", ""},
80 {"delete", 'd', POPT_ARG_INT, &deletePartNum, 'd', "delete a partition", "partnum"},
81 {"display-alignment", 'D', POPT_ARG_NONE, NULL, 'D', "show number of sectors per allocation block", ""},
82 {"move-second-header", 'e', POPT_ARG_NONE, NULL, 'e', "move second header to end of disk", ""},
83 {"end-of-largest", 'E', POPT_ARG_NONE, NULL, 'E', "show end of largest free block", ""},
84 {"first-in-largest", 'f', POPT_ARG_NONE, NULL, 'f', "show start of the largest free block", ""},
85 {"first-aligned-in-largest", 'F', POPT_ARG_NONE, NULL, 'F', "show start of the largest free block, aligned", ""},
86 {"mbrtogpt", 'g', POPT_ARG_NONE, NULL, 'g', "convert MBR to GPT", ""},
87 {"randomize-guids", 'G', POPT_ARG_NONE, NULL, 'G', "randomize disk and partition GUIDs", ""},
88 {"hybrid", 'h', POPT_ARG_STRING, &hybrids, 'h', "create hybrid MBR", "partnum[:partnum...]"},
89 {"info", 'i', POPT_ARG_INT, &infoPartNum, 'i', "show detailed information on partition", "partnum"},
90 {"load-backup", 'l', POPT_ARG_STRING, &backupFile, 'l', "load GPT backup from file", "file"},
91 {"list-types", 'L', POPT_ARG_NONE, NULL, 'L', "list known partition types", ""},
92 {"gpttombr", 'm', POPT_ARG_STRING, &mbrParts, 'm', "convert GPT to MBR", "partnum[:partnum...]"},
93 {"new", 'n', POPT_ARG_STRING, &newPartInfo, 'n', "create new partition", "partnum:start:end"},
94 {"largest-new", 'N', POPT_ARG_INT, &largestPartNum, 'N', "create largest possible new partition", "partnum"},
95 {"clear", 'o', POPT_ARG_NONE, NULL, 'o', "clear partition table", ""},
96 {"print", 'p', POPT_ARG_NONE, NULL, 'p', "print partition table", ""},
97 {"pretend", 'P', POPT_ARG_NONE, NULL, 'P', "make changes in memory, but don't write them", ""},
98 {"transpose", 'r', POPT_ARG_STRING, &twoParts, 'r', "transpose two partitions", "partnum:partnum"},
99 {"replicate", 'R', POPT_ARG_STRING, &outDevice, 'R', "replicate partition table", "device_filename"},
100 {"sort", 's', POPT_ARG_NONE, NULL, 's', "sort partition table entries", ""},
101 {"resize-table", 'S', POPT_ARG_INT, &tableSize, 'S', "resize partition table", "numparts"},
102 {"typecode", 't', POPT_ARG_STRING, &typeCode, 't', "change partition type code", "partnum:{hexcode|GUID}"},
103 {"transform-bsd", 'T', POPT_ARG_INT, &bsdPartNum, 'T', "transform BSD disklabel partition to GPT", "partnum"},
104 {"partition-guid", 'u', POPT_ARG_STRING, &partGUID, 'u', "set partition GUID", "partnum:guid"},
105 {"disk-guid", 'U', POPT_ARG_STRING, &diskGUID, 'U', "set disk GUID", "guid"},
106 {"verify", 'v', POPT_ARG_NONE, NULL, 'v', "check partition table integrity", ""},
107 {"version", 'V', POPT_ARG_NONE, NULL, 'V', "display version information", ""},
108 {"zap", 'z', POPT_ARG_NONE, NULL, 'z', "zap (destroy) GPT (but not MBR) data structures", ""},
109 {"zap-all", 'Z', POPT_ARG_NONE, NULL, 'Z', "zap (destroy) GPT and MBR data structures", ""},
110 POPT_AUTOHELP { NULL, 0, 0, NULL, 0 }
111 };
112
113 // Create popt context...
114 poptCon = poptGetContext(NULL, argc, (const char**) argv, theOptions, 0);
srs56940741fa22013-01-09 12:55:40 -0500115
srs56943860cbe2011-09-10 20:29:53 -0400116 poptSetOtherOptionHelp(poptCon, " [OPTION...] <device>");
srs56940741fa22013-01-09 12:55:40 -0500117
srs56943860cbe2011-09-10 20:29:53 -0400118 if (argc < 2) {
119 poptPrintUsage(poptCon, stderr, 0);
srs5694d1b11e82011-09-18 21:12:28 -0400120 return 1;
srs56943860cbe2011-09-10 20:29:53 -0400121 }
srs56940741fa22013-01-09 12:55:40 -0500122
srs56943860cbe2011-09-10 20:29:53 -0400123 // Do one loop through the options to find the device filename and deal
124 // with options that don't require a device filename, to flag destructive
125 // (o, z, or Z) options, and to flag presence of an
126 while ((opt = poptGetNextOpt(poptCon)) > 0) {
127 switch (opt) {
128 case 'A':
129 cmd = GetString(attributeOperation, 1);
130 if (cmd == "list")
131 Attributes::ListAttributes();
132 break;
133 case 'L':
134 typeHelper.ShowAllTypes();
135 break;
136 case 'P':
137 pretend = 1;
138 break;
139 case 'V':
140 cout << "GPT fdisk (sgdisk) version " << GPTFDISK_VERSION << "\n\n";
141 break;
142 default:
143 break;
144 } // switch
145 numOptions++;
146 } // while
srs56940741fa22013-01-09 12:55:40 -0500147
srs56943860cbe2011-09-10 20:29:53 -0400148 // Assume first non-option argument is the device filename....
149 device = (char*) poptGetArg(poptCon);
150 poptResetContext(poptCon);
srs56940741fa22013-01-09 12:55:40 -0500151
srs56943860cbe2011-09-10 20:29:53 -0400152 if (device != NULL) {
153 JustLooking(); // reset as necessary
154 BeQuiet(); // Tell called functions to be less verbose & interactive
155 if (LoadPartitions((string) device)) {
156 if ((WhichWasUsed() == use_mbr) || (WhichWasUsed() == use_bsd))
157 saveNonGPT = 0; // flag so we don't overwrite unless directed to do so
158 sSize = GetBlockSize();
159 while ((opt = poptGetNextOpt(poptCon)) > 0) {
160 switch (opt) {
161 case 'A': {
162 if (cmd != "list") {
163 partNum = (int) GetInt(attributeOperation, 1) - 1;
164 if ((partNum >= 0) && (partNum < (int) GetNumParts())) {
165 switch (ManageAttributes(partNum, GetString(attributeOperation, 2),
166 GetString(attributeOperation, 3))) {
167 case -1:
168 saveData = 0;
169 neverSaveData = 1;
170 break;
171 case 1:
172 JustLooking(0);
173 saveData = 1;
174 break;
175 default:
176 break;
177 } // switch
178 } else {
179 cerr << "Error: Invalid partition number " << partNum + 1 << "\n";
180 saveData = 0;
181 neverSaveData = 1;
182 } // if/else reasonable partition #
183 } // if (cmd != "list")
184 break;
185 } // case 'A':
186 case 'a':
187 SetAlignment(alignment);
188 break;
189 case 'b':
190 SaveGPTBackup(backupFile);
191 free(backupFile);
192 break;
193 case 'c':
194 JustLooking(0);
195 partNum = (int) GetInt(partName, 1) - 1;
srs56940741fa22013-01-09 12:55:40 -0500196 if ((partNum >= 0) && (partNum < (int) GetNumParts())) {
197 name = GetString(partName, 2);
198 if (SetName(partNum, (UnicodeString) name.c_str())) {
199 saveData = 1;
200 } else {
201 cerr << "Unable to set partition " << partNum + 1
202 << "'s name to '" << GetString(partName, 2) << "'!\n";
203 neverSaveData = 1;
204 } // if/else
205 free(partName);
206 }
srs56943860cbe2011-09-10 20:29:53 -0400207 break;
208 case 'C':
209 JustLooking(0);
210 RecomputeCHS();
211 saveData = 1;
212 break;
213 case 'd':
214 JustLooking(0);
215 if (DeletePartition(deletePartNum - 1) == 0) {
216 cerr << "Error " << errno << " deleting partition!\n";
217 neverSaveData = 1;
218 } else saveData = 1;
219 break;
220 case 'D':
221 cout << GetAlignment() << "\n";
222 break;
223 case 'e':
224 JustLooking(0);
225 MoveSecondHeaderToEnd();
226 saveData = 1;
227 break;
228 case 'E':
229 cout << FindLastInFree(FindFirstInLargest()) << "\n";
230 break;
231 case 'f':
232 cout << FindFirstInLargest() << "\n";
233 break;
234 case 'F':
235 temp = FindFirstInLargest();
236 Align(&temp);
237 cout << temp << "\n";
238 break;
239 case 'g':
240 JustLooking(0);
241 saveData = 1;
242 saveNonGPT = 1;
243 break;
244 case 'G':
245 JustLooking(0);
246 saveData = 1;
247 RandomizeGUIDs();
248 break;
249 case 'h':
250 JustLooking(0);
251 if (BuildMBR(hybrids, 1) == 1)
252 saveData = 1;
253 break;
254 case 'i':
255 ShowPartDetails(infoPartNum - 1);
256 break;
257 case 'l':
258 LoadBackupFile(backupFile, saveData, neverSaveData);
259 free(backupFile);
260 break;
261 case 'L':
262 break;
263 case 'm':
264 JustLooking(0);
265 if (BuildMBR(mbrParts, 0) == 1) {
266 if (!pretend) {
267 if (SaveMBR()) {
268 DestroyGPT();
269 } else
270 cerr << "Problem saving MBR!\n";
271 } // if
272 saveNonGPT = 0;
273 pretend = 1; // Not really, but works around problem if -g is used with this...
274 saveData = 0;
275 } // if
276 break;
277 case 'n':
278 JustLooking(0);
279 partNum = (int) GetInt(newPartInfo, 1) - 1;
280 if (partNum < 0)
281 partNum = FindFirstFreePart();
282 low = FindFirstInLargest();
283 high = FindLastInFree(low);
284 startSector = IeeeToInt(GetString(newPartInfo, 2), sSize, low, high, low);
285 endSector = IeeeToInt(GetString(newPartInfo, 3), sSize, startSector, high, high);
286 if (CreatePartition(partNum, startSector, endSector)) {
287 saveData = 1;
288 } else {
289 cerr << "Could not create partition " << partNum + 1 << " from "
290 << startSector << " to " << endSector << "\n";
291 neverSaveData = 1;
292 } // if/else
293 free(newPartInfo);
294 break;
295 case 'N':
296 JustLooking(0);
297 startSector = FindFirstInLargest();
298 endSector = FindLastInFree(startSector);
299 if (largestPartNum < 0)
300 largestPartNum = FindFirstFreePart();
301 if (CreatePartition(largestPartNum - 1, startSector, endSector)) {
302 saveData = 1;
303 } else {
304 cerr << "Could not create partition " << largestPartNum << " from "
305 << startSector << " to " << endSector << "\n";
306 neverSaveData = 1;
307 } // if/else
308 break;
309 case 'o':
310 JustLooking(0);
311 ClearGPTData();
312 saveData = 1;
313 break;
314 case 'p':
315 DisplayGPTData();
316 break;
317 case 'P':
318 pretend = 1;
319 break;
320 case 'r':
321 JustLooking(0);
322 uint64_t p1, p2;
323 p1 = GetInt(twoParts, 1) - 1;
324 p2 = GetInt(twoParts, 2) - 1;
325 if (SwapPartitions((uint32_t) p1, (uint32_t) p2) == 0) {
326 neverSaveData = 1;
327 cerr << "Cannot swap partitions " << p1 + 1 << " and " << p2 + 1 << "\n";
328 } else saveData = 1;
329 break;
330 case 'R':
331 secondDevice = *this;
332 secondDevice.SetDisk(outDevice);
333 secondDevice.JustLooking(0);
334 if (!secondDevice.SaveGPTData(1))
335 retval = 8;
336 break;
337 case 's':
338 JustLooking(0);
339 SortGPT();
340 saveData = 1;
341 break;
342 case 'S':
343 JustLooking(0);
344 if (SetGPTSize(tableSize) == 0)
345 neverSaveData = 1;
346 else
347 saveData = 1;
348 break;
349 case 't':
350 JustLooking(0);
351 partNum = (int) GetInt(typeCode, 1) - 1;
srs56940741fa22013-01-09 12:55:40 -0500352 if ((partNum >= 0) && (partNum < (int) GetNumParts())) {
353 typeHelper = GetString(typeCode, 2);
354 if ((typeHelper != (GUIDData) "00000000-0000-0000-0000-000000000000") &&
355 (ChangePartType(partNum, typeHelper))) {
356 saveData = 1;
357 } else {
358 cerr << "Could not change partition " << partNum + 1
359 << "'s type code to " << GetString(typeCode, 2) << "!\n";
360 neverSaveData = 1;
361 } // if/else
srs56943860cbe2011-09-10 20:29:53 -0400362 free(typeCode);
srs56940741fa22013-01-09 12:55:40 -0500363 }
srs56943860cbe2011-09-10 20:29:53 -0400364 break;
365 case 'T':
366 JustLooking(0);
367 XFormDisklabel(bsdPartNum - 1);
368 saveData = 1;
369 break;
370 case 'u':
371 JustLooking(0);
372 saveData = 1;
srs56940741fa22013-01-09 12:55:40 -0500373 partNum = (int) GetInt(partGUID, 1) - 1;
374 if ((partNum >= 0) && (partNum < (int) GetNumParts())) {
375 SetPartitionGUID(partNum, GetString(partGUID, 2).c_str());
376 }
srs56943860cbe2011-09-10 20:29:53 -0400377 break;
378 case 'U':
379 JustLooking(0);
380 saveData = 1;
381 SetDiskGUID(diskGUID);
382 break;
383 case 'v':
384 Verify();
385 break;
386 case 'z':
387 if (!pretend) {
388 DestroyGPT();
389 } // if
390 saveNonGPT = 0;
391 saveData = 0;
392 break;
393 case 'Z':
394 if (!pretend) {
395 DestroyGPT();
396 DestroyMBR();
397 } // if
398 saveNonGPT = 0;
399 saveData = 0;
400 break;
401 default:
402 cerr << "Unknown option (-" << opt << ")!\n";
403 break;
404 } // switch
405 } // while
406 } else { // if loaded OK
407 poptResetContext(poptCon);
408 // Do a few types of operations even if there are problems....
409 while ((opt = poptGetNextOpt(poptCon)) > 0) {
410 switch (opt) {
411 case 'l':
412 LoadBackupFile(backupFile, saveData, neverSaveData);
413 cout << "Information: Loading backup partition table; will override earlier problems!\n";
414 free(backupFile);
415 retval = 0;
416 break;
417 case 'o':
418 JustLooking(0);
419 ClearGPTData();
420 saveData = 1;
421 cout << "Information: Creating fresh partition table; will override earlier problems!\n";
422 retval = 0;
423 break;
424 case 'v':
425 cout << "Verification may miss some problems or report too many!\n";
426 Verify();
427 break;
428 case 'z':
429 if (!pretend) {
430 DestroyGPT();
431 } // if
432 saveNonGPT = 0;
433 saveData = 0;
434 break;
435 case 'Z':
436 if (!pretend) {
437 DestroyGPT();
438 DestroyMBR();
439 } // if
440 saveNonGPT = 0;
441 saveData = 0;
442 break;
443 } // switch
444 } // while
445 retval = 2;
446 } // if/else loaded OK
447 if ((saveData) && (!neverSaveData) && (saveNonGPT) && (!pretend)) {
448 SaveGPTData(1);
449 }
450 if (saveData && (!saveNonGPT)) {
451 cout << "Non-GPT disk; not saving changes. Use -g to override.\n";
452 retval = 3;
453 } // if
454 if (neverSaveData) {
455 cerr << "Error encountered; not saving changes.\n";
456 retval = 4;
457 } // if
458 } // if (device != NULL)
459 poptFreeContext(poptCon);
460 return retval;
461} // GPTDataCL::DoOptions()
462
463// Create a hybrid or regular MBR from GPT data structures
464int GPTDataCL::BuildMBR(char* argument, int isHybrid) {
465 int numParts, allOK = 1, i, origPartNum;
466 MBRPart newPart;
467 BasicMBRData newMBR;
468
469 if (argument != NULL) {
470 numParts = CountColons(argument) + 1;
471 if (numParts <= (4 - isHybrid)) {
472 newMBR.SetDisk(GetDisk());
473 for (i = 0; i < numParts; i++) {
474 origPartNum = GetInt(argument, i + 1) - 1;
475 if (IsUsedPartNum(origPartNum)) {
476 newPart.SetInclusion(PRIMARY);
477 newPart.SetLocation(operator[](origPartNum).GetFirstLBA(),
478 operator[](origPartNum).GetLengthLBA());
479 newPart.SetStatus(0);
480 newPart.SetType((uint8_t)(operator[](origPartNum).GetHexType() / 0x0100));
481 newMBR.AddPart(i + isHybrid, newPart);
482 } else {
483 cerr << "Partition " << origPartNum << " does not exist! Aborting operation!\n";
484 allOK = 0;
485 } // if/else
486 } // for
487 if (isHybrid) {
488 newPart.SetInclusion(PRIMARY);
489 newPart.SetLocation(1, newMBR.FindLastInFree(1));
490 newPart.SetStatus(0);
491 newPart.SetType(0xEE);
492 newMBR.AddPart(0, newPart);
493 } // if
494 SetProtectiveMBR(newMBR);
495 } else allOK = 0;
496 } else allOK = 0;
497 if (!allOK)
498 cerr << "Problem creating MBR!\n";
499 return allOK;
500} // GPTDataCL::BuildMBR()
501
502// Returns the number of colons in argument string, ignoring the
503// first character (thus, a leading colon is ignored, as GetString()
504// does).
505int CountColons(char* argument) {
506 int num = 0;
srs56940741fa22013-01-09 12:55:40 -0500507
srs56943860cbe2011-09-10 20:29:53 -0400508 while ((argument[0] != '\0') && (argument = strchr(&argument[1], ':')))
509 num++;
srs56940741fa22013-01-09 12:55:40 -0500510
srs56943860cbe2011-09-10 20:29:53 -0400511 return num;
512} // GPTDataCL::CountColons()
513
514// Extract integer data from argument string, which should be colon-delimited
515uint64_t GetInt(const string & argument, int itemNum) {
516 uint64_t retval;
srs56940741fa22013-01-09 12:55:40 -0500517
srs56943860cbe2011-09-10 20:29:53 -0400518 istringstream inString(GetString(argument, itemNum));
519 inString >> retval;
520 return retval;
521} // GPTDataCL::GetInt()
522
523// Extract string data from argument string, which should be colon-delimited
524// If string begins with a colon, that colon is skipped in the counting. If an
525// invalid itemNum is specified, returns an empty string.
526string GetString(string argument, int itemNum) {
527 size_t startPos = 0, endPos = 0;
528 string retVal = "";
529 int foundLast = 0;
530 int numFound = 0;
srs56940741fa22013-01-09 12:55:40 -0500531
srs56943860cbe2011-09-10 20:29:53 -0400532 if (argument[0] == ':')
533 argument.erase(0, 1);
534 while ((numFound < itemNum) && (!foundLast)) {
535 endPos = argument.find(':', startPos);
536 numFound++;
537 if (endPos == string::npos) {
538 foundLast = 1;
539 endPos = argument.length();
540 } else if (numFound < itemNum) {
541 startPos = endPos + 1;
542 } // if/elseif
543 } // while
544 if ((numFound == itemNum) && (numFound > 0))
545 retVal = argument.substr(startPos, endPos - startPos);
srs56940741fa22013-01-09 12:55:40 -0500546
srs56943860cbe2011-09-10 20:29:53 -0400547 return retVal;
548} // GetString()