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