9
9
/* This program is copyright (c) 2009-2011 by Roderick W. Smith. It is distributed
10
10
under the terms of the GNU GPL version 2, as detailed in the COPYING file. */
23
#include "parttypes.h"
24
#include "attributes.h"
26
14
using namespace std;
28
16
#define MAX_OPTIONS 50
30
int BuildMBR(GPTData& theGPT, char* argument, int isHybrid);
31
int CountColons(char* argument);
33
// Extract colon-separated fields from a string....
34
uint64_t GetInt(const string & argument, int itemNum);
35
string GetString(string argument, int itemNum);
38
18
int main(int argc, char *argv[]) {
39
GPTData theGPT, secondDevice;
42
int opt, numOptions = 0, saveData = 0, neverSaveData = 0;
43
int partNum = 0, deletePartNum = 0, infoPartNum = 0, bsdPartNum = 0, largestPartNum = 0;
45
uint32_t gptPartNum = 0;
46
int alignment = DEFAULT_ALIGNMENT, retval = 0, pretend = 0;
47
uint32_t tableSize = 128;
48
uint64_t startSector, endSector;
49
uint64_t temp; // temporary variable; free to use in any case
50
char *attributeOperation = NULL;
52
char *newPartInfo = NULL, *typeCode = NULL, *partName = NULL;
53
char *backupFile = NULL, *twoParts = NULL, *hybrids = NULL, *mbrParts;
54
char *partGUID = NULL, *diskGUID = NULL, *outDevice = NULL;
55
string cmd, typeGUID, name;
59
struct poptOption theOptions[] =
61
{"attributes", 'A', POPT_ARG_STRING, &attributeOperation, 'A', "operate on partition attributes", "list|[partnum:show|or|nand|xor|=|set|clear|toggle|get[:bitnum|hexbitmask]]"},
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"},
65
{"recompute-chs", 'C', POPT_ARG_NONE, NULL, 'C', "recompute CHS values in protective/hybrid MBR", ""},
66
{"delete", 'd', POPT_ARG_INT, &deletePartNum, 'd', "delete a partition", "partnum"},
67
{"display-alignment", 'D', POPT_ARG_NONE, NULL, 'D', "show number of sectors per allocation block", ""},
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", ""},
71
{"first-aligned-in-largest", 'F', POPT_ARG_NONE, NULL, 'F', "show start of the largest free block, aligned", ""},
72
{"mbrtogpt", 'g', POPT_ARG_NONE, NULL, 'g', "convert MBR to GPT", ""},
73
{"randomize-guids", 'G', POPT_ARG_NONE, NULL, 'G', "randomize disk and partition GUIDs", ""},
74
{"hybrid", 'h', POPT_ARG_STRING, &hybrids, 'h', "create hybrid MBR", "partnum[:partnum...]"},
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", ""},
78
{"gpttombr", 'm', POPT_ARG_STRING, &mbrParts, 'm', "convert GPT to MBR", "partnum[:partnum...]"},
79
{"new", 'n', POPT_ARG_STRING, &newPartInfo, 'n', "create new partition", "partnum:start:end"},
80
{"largest-new", 'N', POPT_ARG_INT, &largestPartNum, 'N', "create largest possible new partition", "partnum"},
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", ""},
84
{"transpose", 'r', POPT_ARG_STRING, &twoParts, 'r', "transpose two partitions", "partnum:partnum"},
85
{"replicate", 'R', POPT_ARG_STRING, &outDevice, 'R', "replicate partition table", "device_filename"},
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"},
88
{"typecode", 't', POPT_ARG_STRING, &typeCode, 't', "change partition type code", "partnum:{hexcode|GUID}"},
89
{"transform-bsd", 'T', POPT_ARG_INT, &bsdPartNum, 'T', "transform BSD disklabel partition to GPT", "partnum"},
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"},
92
{"verify", 'v', POPT_ARG_NONE, NULL, 'v', "check partition table integrity", ""},
93
{"version", 'V', POPT_ARG_NONE, NULL, 'V', "display version information", ""},
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", ""},
96
POPT_AUTOHELP { NULL, 0, 0, NULL, 0 }
99
// Create popt context...
100
poptCon = poptGetContext(NULL, argc, (const char**) argv, theOptions, 0);
102
poptSetOtherOptionHelp(poptCon, " [OPTION...] <device>");
105
poptPrintUsage(poptCon, stderr, 0);
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) {
114
cmd = GetString(attributeOperation, 1);
116
Attributes::ListAttributes();
119
typeHelper.ShowAllTypes();
125
cout << "GPT fdisk (sgdisk) version " << GPTFDISK_VERSION << "\n\n";
133
// Assume first non-option argument is the device filename....
134
device = (char*) poptGetArg(poptCon);
135
poptResetContext(poptCon);
137
if (device != NULL) {
138
theGPT.JustLooking(); // reset as necessary
139
theGPT.BeQuiet(); // Tell called functions to be less verbose & interactive
140
if (theGPT.LoadPartitions((string) device)) {
141
if ((theGPT.WhichWasUsed() == use_mbr) || (theGPT.WhichWasUsed() == use_bsd))
142
saveNonGPT = 0; // flag so we don't overwrite unless directed to do so
143
sSize = theGPT.GetBlockSize();
144
while ((opt = poptGetNextOpt(poptCon)) > 0) {
148
partNum = (int) GetInt(attributeOperation, 1) - 1;
149
if ((partNum >= 0) && (partNum < (int) theGPT.GetNumParts())) {
150
switch (theGPT.ManageAttributes(partNum, GetString(attributeOperation, 2),
151
GetString(attributeOperation, 3))) {
157
theGPT.JustLooking(0);
164
cerr << "Error: Invalid partition number " << partNum + 1 << "\n";
167
} // if/else reasonable partition #
168
} // if (cmd != "list")
172
theGPT.SetAlignment(alignment);
175
theGPT.SaveGPTBackup(backupFile);
179
theGPT.JustLooking(0);
180
partNum = (int) GetInt(partName, 1) - 1;
181
name = GetString(partName, 2);
182
if (theGPT.SetName(partNum, (UnicodeString) name.c_str())) {
185
cerr << "Unable to set partition " << partNum + 1
186
<< "'s name to '" << GetString(partName, 2) << "'!\n";
192
theGPT.JustLooking(0);
193
theGPT.RecomputeCHS();
197
theGPT.JustLooking(0);
198
if (theGPT.DeletePartition(deletePartNum - 1) == 0) {
199
cerr << "Error " << errno << " deleting partition!\n";
204
cout << theGPT.GetAlignment() << "\n";
207
theGPT.JustLooking(0);
208
theGPT.MoveSecondHeaderToEnd();
212
cout << theGPT.FindLastInFree(theGPT.FindFirstInLargest()) << "\n";
215
cout << theGPT.FindFirstInLargest() << "\n";
218
temp = theGPT.FindFirstInLargest();
220
cout << temp << "\n";
223
theGPT.JustLooking(0);
228
theGPT.JustLooking(0);
230
theGPT.RandomizeGUIDs();
233
theGPT.JustLooking(0);
234
if (BuildMBR(theGPT, hybrids, 1) == 1)
238
theGPT.ShowPartDetails(infoPartNum - 1);
241
if (theGPT.LoadGPTBackup((string) backupFile) == 1) {
242
theGPT.JustLooking(0);
247
cerr << "Error loading backup file!\n";
254
theGPT.JustLooking(0);
255
if (BuildMBR(theGPT, mbrParts, 0) == 1) {
257
if (theGPT.SaveMBR()) {
260
cerr << "Problem saving MBR!\n";
263
pretend = 1; // Not really, but works around problem if -g is used with this...
268
theGPT.JustLooking(0);
269
partNum = (int) GetInt(newPartInfo, 1) - 1;
271
partNum = theGPT.FindFirstFreePart();
272
low = theGPT.FindFirstInLargest();
273
high = theGPT.FindLastInFree(low);
274
startSector = IeeeToInt(GetString(newPartInfo, 2), sSize, low, high, low);
275
endSector = IeeeToInt(GetString(newPartInfo, 3), sSize, startSector, high, high);
276
if (theGPT.CreatePartition(partNum, startSector, endSector)) {
279
cerr << "Could not create partition " << partNum + 1 << " from "
280
<< startSector << " to " << endSector << "\n";
286
theGPT.JustLooking(0);
287
startSector = theGPT.FindFirstInLargest();
288
endSector = theGPT.FindLastInFree(startSector);
289
if (largestPartNum < 0)
290
largestPartNum = theGPT.FindFirstFreePart();
291
if (theGPT.CreatePartition(largestPartNum - 1, startSector, endSector)) {
294
cerr << "Could not create partition " << largestPartNum << " from "
295
<< startSector << " to " << endSector << "\n";
300
theGPT.JustLooking(0);
301
theGPT.ClearGPTData();
305
theGPT.DisplayGPTData();
311
theGPT.JustLooking(0);
313
p1 = GetInt(twoParts, 1) - 1;
314
p2 = GetInt(twoParts, 2) - 1;
315
if (theGPT.SwapPartitions((uint32_t) p1, (uint32_t) p2) == 0) {
317
cerr << "Cannot swap partitions " << p1 + 1 << " and " << p2 + 1 << "\n";
321
secondDevice = theGPT;
322
secondDevice.SetDisk(outDevice);
323
secondDevice.JustLooking(0);
324
secondDevice.SaveGPTData(1);
327
theGPT.JustLooking(0);
332
theGPT.JustLooking(0);
333
if (theGPT.SetGPTSize(tableSize) == 0)
339
theGPT.JustLooking(0);
340
partNum = (int) GetInt(typeCode, 1) - 1;
341
typeHelper = GetString(typeCode, 2);
342
if ((typeHelper != (GUIDData) "00000000-0000-0000-0000-000000000000") &&
343
(theGPT.ChangePartType(partNum, typeHelper))) {
346
cerr << "Could not change partition " << partNum + 1
347
<< "'s type code to " << GetString(typeCode, 2) << "!\n";
353
theGPT.JustLooking(0);
354
theGPT.XFormDisklabel(bsdPartNum - 1);
358
theGPT.JustLooking(0);
360
gptPartNum = (int) GetInt(partGUID, 1) - 1;
361
theGPT.SetPartitionGUID(gptPartNum, GetString(partGUID, 2).c_str());
364
theGPT.JustLooking(0);
366
theGPT.SetDiskGUID(diskGUID);
387
cerr << "Unknown option (-" << opt << ")!\n";
391
if ((saveData) && (!neverSaveData) && (saveNonGPT) && (!pretend)) {
392
theGPT.SaveGPTData(1);
394
if (saveData && (!saveNonGPT)) {
395
cout << "Non-GPT disk; not saving changes. Use -g to override.\n";
399
cerr << "Error encountered; not saving changes.\n";
402
} else { // if loaded OK
403
poptResetContext(poptCon);
404
// Do a few types of operations even if there are problems....
405
while ((opt = poptGetNextOpt(poptCon)) > 0) {
408
cout << "Verification may miss some problems or report too many!\n";
429
} // if/else loaded OK
430
} // if (device != NULL)
431
poptFreeContext(poptCon);
20
return theGPT.DoOptions(argc, argv);
436
// Create a hybrid or regular MBR from GPT data structures
437
int BuildMBR(GPTData & theGPT, char* argument, int isHybrid) {
438
int numParts, allOK = 1, i, origPartNum;
442
if ((&theGPT != NULL) && (argument != NULL)) {
443
numParts = CountColons(argument) + 1;
444
if (numParts <= (4 - isHybrid)) {
445
newMBR.SetDisk(theGPT.GetDisk());
446
for (i = 0; i < numParts; i++) {
447
origPartNum = GetInt(argument, i + 1) - 1;
448
if (theGPT.IsUsedPartNum(origPartNum)) {
449
newPart.SetInclusion(PRIMARY);
450
newPart.SetLocation(theGPT[origPartNum].GetFirstLBA(),
451
theGPT[origPartNum].GetLengthLBA());
452
newPart.SetStatus(0);
453
newPart.SetType((uint8_t)(theGPT[origPartNum].GetHexType() / 0x0100));
454
newMBR.AddPart(i + isHybrid, newPart);
456
cerr << "Partition " << origPartNum << " does not exist! Aborting operation!\n";
461
newPart.SetInclusion(PRIMARY);
462
newPart.SetLocation(1, newMBR.FindLastInFree(1));
463
newPart.SetStatus(0);
464
newPart.SetType(0xEE);
465
newMBR.AddPart(0, newPart);
467
theGPT.SetProtectiveMBR(newMBR);
471
cerr << "Problem creating MBR!\n";
475
// Returns the number of colons in argument string, ignoring the
476
// first character (thus, a leading colon is ignored, as GetString()
478
int CountColons(char* argument) {
481
while ((argument[0] != '\0') && (argument = strchr(&argument[1], ':')))
487
// Extract integer data from argument string, which should be colon-delimited
488
uint64_t GetInt(const string & argument, int itemNum) {
491
istringstream inString(GetString(argument, itemNum));
496
// Extract string data from argument string, which should be colon-delimited
497
// If string begins with a colon, that colon is skipped in the counting. If an
498
// invalid itemNum is specified, returns an empty string.
499
string GetString(string argument, int itemNum) {
500
size_t startPos = 0, endPos = 0;
505
if (argument[0] == ':')
506
argument.erase(0, 1);
507
while ((numFound < itemNum) && (!foundLast)) {
508
endPos = argument.find(':', startPos);
510
if (endPos == string::npos) {
512
endPos = argument.length();
513
} else if (numFound < itemNum) {
514
startPos = endPos + 1;
517
if ((numFound == itemNum) && (numFound > 0))
518
retVal = argument.substr(startPos, endPos - startPos);