blob: b64e83f0d8b5ded30225e8c099ba9fad22589bb9 [file] [log] [blame]
srs56943860cbe2011-09-10 20:29:53 -04001/*
2 Implementation of GPTData class derivative with popt-based command
3 line processing
Roderick W. Smithe3ee7332013-09-24 12:56:11 -04004 Copyright (C) 2010-2013 Roderick W. Smith
srs56943860cbe2011-09-10 20:29:53 -04005
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':
Roderick W. Smithe3ee7332013-09-24 12:56:11 -0400134 typeHelper.ShowAllTypes(0);
srs56943860cbe2011-09-10 20:29:53 -0400135 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();
srs5694f5dfbfa2013-02-14 20:47:14 -0500283 Align(&low);
srs56943860cbe2011-09-10 20:29:53 -0400284 high = FindLastInFree(low);
285 startSector = IeeeToInt(GetString(newPartInfo, 2), sSize, low, high, low);
286 endSector = IeeeToInt(GetString(newPartInfo, 3), sSize, startSector, high, high);
287 if (CreatePartition(partNum, startSector, endSector)) {
288 saveData = 1;
289 } else {
290 cerr << "Could not create partition " << partNum + 1 << " from "
291 << startSector << " to " << endSector << "\n";
292 neverSaveData = 1;
293 } // if/else
294 free(newPartInfo);
295 break;
296 case 'N':
297 JustLooking(0);
298 startSector = FindFirstInLargest();
srs5694f5dfbfa2013-02-14 20:47:14 -0500299 Align(&startSector);
srs56943860cbe2011-09-10 20:29:53 -0400300 endSector = FindLastInFree(startSector);
301 if (largestPartNum < 0)
302 largestPartNum = FindFirstFreePart();
303 if (CreatePartition(largestPartNum - 1, startSector, endSector)) {
304 saveData = 1;
305 } else {
306 cerr << "Could not create partition " << largestPartNum << " from "
307 << startSector << " to " << endSector << "\n";
308 neverSaveData = 1;
309 } // if/else
310 break;
311 case 'o':
312 JustLooking(0);
313 ClearGPTData();
314 saveData = 1;
315 break;
316 case 'p':
317 DisplayGPTData();
318 break;
319 case 'P':
320 pretend = 1;
321 break;
322 case 'r':
323 JustLooking(0);
324 uint64_t p1, p2;
325 p1 = GetInt(twoParts, 1) - 1;
326 p2 = GetInt(twoParts, 2) - 1;
327 if (SwapPartitions((uint32_t) p1, (uint32_t) p2) == 0) {
328 neverSaveData = 1;
329 cerr << "Cannot swap partitions " << p1 + 1 << " and " << p2 + 1 << "\n";
330 } else saveData = 1;
331 break;
332 case 'R':
333 secondDevice = *this;
334 secondDevice.SetDisk(outDevice);
335 secondDevice.JustLooking(0);
336 if (!secondDevice.SaveGPTData(1))
337 retval = 8;
338 break;
339 case 's':
340 JustLooking(0);
341 SortGPT();
342 saveData = 1;
343 break;
344 case 'S':
345 JustLooking(0);
346 if (SetGPTSize(tableSize) == 0)
347 neverSaveData = 1;
348 else
349 saveData = 1;
350 break;
351 case 't':
352 JustLooking(0);
353 partNum = (int) GetInt(typeCode, 1) - 1;
srs56940741fa22013-01-09 12:55:40 -0500354 if ((partNum >= 0) && (partNum < (int) GetNumParts())) {
355 typeHelper = GetString(typeCode, 2);
356 if ((typeHelper != (GUIDData) "00000000-0000-0000-0000-000000000000") &&
357 (ChangePartType(partNum, typeHelper))) {
358 saveData = 1;
359 } else {
360 cerr << "Could not change partition " << partNum + 1
361 << "'s type code to " << GetString(typeCode, 2) << "!\n";
362 neverSaveData = 1;
363 } // if/else
srs56943860cbe2011-09-10 20:29:53 -0400364 free(typeCode);
srs56940741fa22013-01-09 12:55:40 -0500365 }
srs56943860cbe2011-09-10 20:29:53 -0400366 break;
367 case 'T':
368 JustLooking(0);
369 XFormDisklabel(bsdPartNum - 1);
370 saveData = 1;
371 break;
372 case 'u':
373 JustLooking(0);
374 saveData = 1;
srs56940741fa22013-01-09 12:55:40 -0500375 partNum = (int) GetInt(partGUID, 1) - 1;
376 if ((partNum >= 0) && (partNum < (int) GetNumParts())) {
377 SetPartitionGUID(partNum, GetString(partGUID, 2).c_str());
378 }
srs56943860cbe2011-09-10 20:29:53 -0400379 break;
380 case 'U':
381 JustLooking(0);
382 saveData = 1;
383 SetDiskGUID(diskGUID);
384 break;
385 case 'v':
386 Verify();
387 break;
388 case 'z':
389 if (!pretend) {
390 DestroyGPT();
391 } // if
392 saveNonGPT = 0;
393 saveData = 0;
394 break;
395 case 'Z':
396 if (!pretend) {
397 DestroyGPT();
398 DestroyMBR();
399 } // if
400 saveNonGPT = 0;
401 saveData = 0;
402 break;
403 default:
404 cerr << "Unknown option (-" << opt << ")!\n";
405 break;
406 } // switch
407 } // while
408 } else { // if loaded OK
409 poptResetContext(poptCon);
410 // Do a few types of operations even if there are problems....
411 while ((opt = poptGetNextOpt(poptCon)) > 0) {
412 switch (opt) {
413 case 'l':
414 LoadBackupFile(backupFile, saveData, neverSaveData);
415 cout << "Information: Loading backup partition table; will override earlier problems!\n";
416 free(backupFile);
417 retval = 0;
418 break;
419 case 'o':
420 JustLooking(0);
421 ClearGPTData();
422 saveData = 1;
423 cout << "Information: Creating fresh partition table; will override earlier problems!\n";
424 retval = 0;
425 break;
426 case 'v':
427 cout << "Verification may miss some problems or report too many!\n";
428 Verify();
429 break;
430 case 'z':
431 if (!pretend) {
432 DestroyGPT();
433 } // if
434 saveNonGPT = 0;
435 saveData = 0;
436 break;
437 case 'Z':
438 if (!pretend) {
439 DestroyGPT();
440 DestroyMBR();
441 } // if
442 saveNonGPT = 0;
443 saveData = 0;
444 break;
445 } // switch
446 } // while
447 retval = 2;
448 } // if/else loaded OK
449 if ((saveData) && (!neverSaveData) && (saveNonGPT) && (!pretend)) {
450 SaveGPTData(1);
451 }
452 if (saveData && (!saveNonGPT)) {
453 cout << "Non-GPT disk; not saving changes. Use -g to override.\n";
454 retval = 3;
455 } // if
456 if (neverSaveData) {
457 cerr << "Error encountered; not saving changes.\n";
458 retval = 4;
459 } // if
460 } // if (device != NULL)
461 poptFreeContext(poptCon);
462 return retval;
463} // GPTDataCL::DoOptions()
464
465// Create a hybrid or regular MBR from GPT data structures
466int GPTDataCL::BuildMBR(char* argument, int isHybrid) {
467 int numParts, allOK = 1, i, origPartNum;
468 MBRPart newPart;
469 BasicMBRData newMBR;
470
471 if (argument != NULL) {
472 numParts = CountColons(argument) + 1;
473 if (numParts <= (4 - isHybrid)) {
474 newMBR.SetDisk(GetDisk());
475 for (i = 0; i < numParts; i++) {
476 origPartNum = GetInt(argument, i + 1) - 1;
477 if (IsUsedPartNum(origPartNum)) {
478 newPart.SetInclusion(PRIMARY);
479 newPart.SetLocation(operator[](origPartNum).GetFirstLBA(),
480 operator[](origPartNum).GetLengthLBA());
481 newPart.SetStatus(0);
482 newPart.SetType((uint8_t)(operator[](origPartNum).GetHexType() / 0x0100));
483 newMBR.AddPart(i + isHybrid, newPart);
484 } else {
485 cerr << "Partition " << origPartNum << " does not exist! Aborting operation!\n";
486 allOK = 0;
487 } // if/else
488 } // for
489 if (isHybrid) {
490 newPart.SetInclusion(PRIMARY);
491 newPart.SetLocation(1, newMBR.FindLastInFree(1));
492 newPart.SetStatus(0);
493 newPart.SetType(0xEE);
494 newMBR.AddPart(0, newPart);
495 } // if
496 SetProtectiveMBR(newMBR);
497 } else allOK = 0;
498 } else allOK = 0;
499 if (!allOK)
500 cerr << "Problem creating MBR!\n";
501 return allOK;
502} // GPTDataCL::BuildMBR()
503
504// Returns the number of colons in argument string, ignoring the
505// first character (thus, a leading colon is ignored, as GetString()
506// does).
507int CountColons(char* argument) {
508 int num = 0;
srs56940741fa22013-01-09 12:55:40 -0500509
srs56943860cbe2011-09-10 20:29:53 -0400510 while ((argument[0] != '\0') && (argument = strchr(&argument[1], ':')))
511 num++;
srs56940741fa22013-01-09 12:55:40 -0500512
srs56943860cbe2011-09-10 20:29:53 -0400513 return num;
514} // GPTDataCL::CountColons()
515
516// Extract integer data from argument string, which should be colon-delimited
517uint64_t GetInt(const string & argument, int itemNum) {
518 uint64_t retval;
srs56940741fa22013-01-09 12:55:40 -0500519
srs56943860cbe2011-09-10 20:29:53 -0400520 istringstream inString(GetString(argument, itemNum));
521 inString >> retval;
522 return retval;
523} // GPTDataCL::GetInt()
524
525// Extract string data from argument string, which should be colon-delimited
526// If string begins with a colon, that colon is skipped in the counting. If an
527// invalid itemNum is specified, returns an empty string.
528string GetString(string argument, int itemNum) {
529 size_t startPos = 0, endPos = 0;
530 string retVal = "";
531 int foundLast = 0;
532 int numFound = 0;
srs56940741fa22013-01-09 12:55:40 -0500533
srs56943860cbe2011-09-10 20:29:53 -0400534 if (argument[0] == ':')
535 argument.erase(0, 1);
536 while ((numFound < itemNum) && (!foundLast)) {
537 endPos = argument.find(':', startPos);
538 numFound++;
539 if (endPos == string::npos) {
540 foundLast = 1;
541 endPos = argument.length();
542 } else if (numFound < itemNum) {
543 startPos = endPos + 1;
544 } // if/elseif
545 } // while
546 if ((numFound == itemNum) && (numFound > 0))
547 retVal = argument.substr(startPos, endPos - startPos);
srs56940741fa22013-01-09 12:55:40 -0500548
srs56943860cbe2011-09-10 20:29:53 -0400549 return retVal;
550} // GetString()