2
// Program modelled after Linux fdisk, but it manipulates GPT partitions
3
// rather than MBR partitions.
5
// by Rod Smith, project began February 2009
7
/* This program is copyright (c) 2009 by Roderick W. Smith. It is distributed
8
under the terms of the GNU GPL version 2, as detailed in the COPYING file. */
18
// Function prototypes....
19
// int ReadPartitions(char* filename, struct GPTData* theGPT);
20
void MainMenu(char* filename, struct GPTData* theGPT);
21
void ShowCommands(void);
22
void ExpertsMenu(char* filename, struct GPTData* theGPT);
23
void ShowExpertCommands(void);
24
void RecoveryMenu(char* filename, struct GPTData* theGPT);
25
void ShowRecoveryCommands(void);
27
int main(int argc, char* argv[]) {
32
printf("GPT fdisk (gdisk) version 0.5.1\n\n");
34
if (argc == 2) { // basic usage
36
doMore = theGPT.LoadPartitions(argv[1]);
38
MainMenu(argv[1], &theGPT);
41
} else if (argc == 3) { // usage with "-l" option
43
if (strcmp(argv[1], "-l") == 0) {
45
} else if (strcmp(argv[2], "-l") == 0) {
47
} else { // 3 arguments, but none is "-l"
48
fprintf(stderr, "Usage: %s [-l] device_file\n", argv[0]);
51
doMore = theGPT.LoadPartitions(device);
52
if (doMore) theGPT.DisplayGPTData();
56
fprintf(stderr, "Usage: %s [-l] device_file\n", argv[0]);
60
// Accept a command and execute it. Returns only when the user
61
// wants to exit (such as after a 'w' or 'q' command).
62
void MainMenu(char* filename, struct GPTData* theGPT) {
63
char command, line[255], buFile[255];
66
uint32_t temp1, temp2;
69
printf("\nCommand (? for help): ");
70
fgets(line, 255, stdin);
71
sscanf(line, "%c", &command);
74
printf("Enter backup filename to save: ");
75
fgets(line, 255, stdin);
76
sscanf(line, "%s", &buFile);
77
theGPT->SaveGPTBackup(buFile);
80
if (theGPT->GetPartRange(&temp1, &temp2) > 0)
81
theGPT->SetName(theGPT->GetPartNum());
83
printf("No partitions\n");
86
theGPT->DeletePartition();
89
theGPT->ShowDetails();
92
typeHelper.ShowTypes();
95
theGPT->CreatePartition();
98
printf("This option deletes all partitions and creates a new "
99
"protective MBR.\nProceed? ");
100
if (GetYN() == 'Y') {
101
theGPT->ClearGPTData();
102
theGPT->MakeProtectiveMBR();
106
theGPT->DisplayGPTData();
112
RecoveryMenu(filename, theGPT);
117
printf("You may need to edit /etc/fstab and/or your boot loader configuration!\n");
120
theGPT->ChangePartType();
123
if (theGPT->Verify() > 0) { // problems found
124
printf("You may be able to correct the problems by using options on the experts\n"
125
"menu (press 'x' at the command prompt). Good luck!\n");
129
if (theGPT->SaveGPTData() == 1)
133
ExpertsMenu(filename, theGPT);
143
void ShowCommands(void) {
144
printf("b\tback up GPT data to a file\n");
145
printf("c\tchange a partition's name\n");
146
printf("d\tdelete a partition\n");
147
printf("i\tshow detailed information on a partition\n");
148
printf("l\tlist known partition types\n");
149
printf("n\tadd a new partition\n");
150
printf("o\tcreate a new empty GUID partition table (GPT)\n");
151
printf("p\tprint the partition table\n");
152
printf("q\tquit without saving changes\n");
153
printf("r\trecovery and transformation options (experts only)\n");
154
printf("s\tsort partitions\n");
155
printf("t\tchange a partition's type code\n");
156
printf("v\tverify disk\n");
157
printf("w\twrite table to disk and exit\n");
158
printf("x\textra functionality (experts only)\n");
159
printf("?\tprint this menu\n");
162
// Accept a recovery & transformation menu command. Returns only when the user
163
// issues an exit command, such as 'w' or 'q'.
164
void RecoveryMenu(char* filename, struct GPTData* theGPT) {
165
char command, line[255], buFile[255];
166
PartTypes typeHelper;
171
printf("\nrecovery/transformation command (? for help): ");
172
fgets(line, 255, stdin);
173
sscanf(line, "%c", &command);
176
theGPT->RebuildMainHeader();
179
printf("Warning! This will probably do weird things if you've converted an MBR to\n"
180
"GPT form and haven't yet saved the GPT! Proceed? ");
182
theGPT->LoadSecondTableAsMain();
185
theGPT->RebuildSecondHeader();
188
printf("Warning! This will probably do weird things if you've converted an MBR to\n"
189
"GPT form and haven't yet saved the GPT! Proceed? ");
191
theGPT->LoadMainTable();
194
printf("Warning! This will destroy the currently defined partitions! Proceed? ");
195
if (GetYN() == 'Y') {
196
if (theGPT->LoadMBR(filename) == 1) { // successful load
197
theGPT->XFormPartitions();
199
printf("Problem loading MBR! GPT is untouched; regenerating protective MBR!\n");
200
theGPT->MakeProtectiveMBR();
205
temp1 = theGPT->XFormToMBR();
207
printf("Converted %d partitions. Finalize and exit? ", temp1);
208
if (GetYN() == 'Y') {
209
if (theGPT->DestroyGPT(0) > 0)
212
theGPT->MakeProtectiveMBR();
213
printf("Note: New protective MBR created.\n");
218
theGPT->MakeHybrid();
221
theGPT->ShowDetails();
224
printf("Enter backup filename to load: ");
225
fgets(line, 255, stdin);
226
sscanf(line, "%s", &buFile);
227
theGPT->LoadGPTBackup(buFile);
230
MainMenu(filename, theGPT);
234
theGPT->DisplayMBRData();
237
theGPT->DisplayGPTData();
243
theGPT->XFormDisklabel();
249
if (theGPT->SaveGPTData() == 1) {
254
ExpertsMenu(filename, theGPT);
258
ShowRecoveryCommands();
264
void ShowRecoveryCommands(void) {
265
printf("b\tuse backup GPT header (rebuilding main)\n");
266
printf("c\tload backup partition table from disk (rebuilding main)\n");
267
printf("d\tuse main GPT header (rebuilding backup)\n");
268
printf("e\tload main partition table from disk (rebuilding backup)\n");
269
printf("f\tload MBR and build fresh GPT from it\n");
270
printf("g\tconvert GPT into MBR and exit\n");
271
printf("h\tmake hybrid MBR\n");
272
printf("i\tshow detailed information on a partition\n");
273
printf("l\tload partition data from a backup file\n");
274
printf("m\treturn to main menu\n");
275
printf("o\tprint protective MBR data\n");
276
printf("p\tprint the partition table\n");
277
printf("q\tquit without saving changes\n");
278
printf("t\ttransform BSD disklabel partition\n");
279
printf("v\tverify disk\n");
280
printf("w\twrite table to disk and exit\n");
281
printf("x\textra functionality (experts only)\n");
282
printf("?\tprint this menu\n");
283
} // ShowRecoveryCommands()
285
// Accept an experts' menu command. Returns only after the user
286
// selects an exit command, such as 'w' or 'q'.
287
void ExpertsMenu(char* filename, struct GPTData* theGPT) {
288
char command, line[255];
289
PartTypes typeHelper;
291
uint32_t temp1, temp2;
295
printf("\nExpert command (? for help): ");
296
fgets(line, 255, stdin);
297
sscanf(line, "%c", &command);
300
if (theGPT->GetPartRange(&temp1, &temp2) > 0)
301
theGPT->SetAttributes(theGPT->GetPartNum());
303
printf("No partitions\n");
306
if (theGPT->GetPartRange(&temp1, &temp2) > 0) {
307
pn = theGPT->GetPartNum();
308
printf("Enter the partition's new unique GUID:\n");
309
theGPT->SetPartitionGUID(pn, GetGUID());
310
} else printf("No partitions\n");
313
printf("Relocating backup data structures to the end of the disk\n");
314
theGPT->MoveSecondHeaderToEnd();
317
printf("Enter the disk's unique GUID:\n");
318
theGPT->SetDiskGUID(GetGUID());
321
theGPT->ShowDetails();
324
MainMenu(filename, theGPT);
328
theGPT->MakeProtectiveMBR();
331
theGPT->DisplayMBRData();
334
theGPT->DisplayGPTData();
340
RecoveryMenu(filename, theGPT);
344
theGPT->ResizePartitionTable();
350
if (theGPT->SaveGPTData() == 1) {
355
if (theGPT->DestroyGPT() == 1) {
360
ShowExpertCommands();
366
void ShowExpertCommands(void) {
367
printf("a\tset attributes\n");
368
printf("c\tchange partition GUID\n");
369
printf("e\trelocate backup data structures to the end of the disk\n");
370
printf("g\tchange disk GUID\n");
371
printf("i\tshow detailed information on a partition\n");
372
printf("m\treturn to main menu\n");
373
printf("n\tcreate a new protective MBR\n");
374
printf("o\tprint protective MBR data\n");
375
printf("p\tprint the partition table\n");
376
printf("q\tquit without saving changes\n");
377
printf("r\trecovery and transformation options (experts only)\n");
378
printf("s\tresize partition table\n");
379
printf("v\tverify disk\n");
380
printf("w\twrite table to disk and exit\n");
381
printf("z\tzap (destroy) GPT data structures and exit\n");
382
printf("?\tprint this menu\n");
383
} // ShowExpertCommands()