srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1 | // gdisk.cc |
| 2 | // Program modelled after Linux fdisk, but it manipulates GPT partitions |
| 3 | // rather than MBR partitions. |
| 4 | // |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 5 | // by Rod Smith, project began February 2009 |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 6 | |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame^] | 7 | /* This program is copyright (c) 2009-2011 by Roderick W. Smith. It is distributed |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 8 | under the terms of the GNU GPL version 2, as detailed in the COPYING file. */ |
| 9 | |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 10 | #include <stdio.h> |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 11 | //#include <getopt.h> |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 12 | #include <string.h> |
| 13 | #include <string> |
| 14 | #include <iostream> |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 15 | #include <sstream> |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 16 | #include <locale> |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 17 | #include "mbr.h" |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 18 | #include "gpttext.h" |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 19 | #include "support.h" |
| 20 | |
| 21 | // Function prototypes.... |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 22 | void MainMenu(string filename, GPTDataTextUI* theGPT); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 23 | void ShowCommands(void); |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 24 | void ExpertsMenu(string filename, GPTDataTextUI* theGPT); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 25 | void ShowExpertCommands(void); |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 26 | void RecoveryMenu(string filename, GPTDataTextUI* theGPT); |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 27 | void ShowRecoveryCommands(void); |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 28 | void WinWarning(void); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 29 | |
| 30 | int main(int argc, char* argv[]) { |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 31 | GPTDataTextUI theGPT; |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame^] | 32 | size_t i; |
| 33 | char *device = NULL; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 34 | |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 35 | cout << "GPT fdisk (gdisk) version " << GPTFDISK_VERSION << "\n\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 36 | |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 37 | if (!SizesOK()) |
| 38 | exit(1); |
| 39 | |
| 40 | switch (argc) { |
| 41 | case 1: |
| 42 | WinWarning(); |
| 43 | cout << "Type device filename, or press <Enter> to exit: "; |
| 44 | device = new char[255]; |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame^] | 45 | if (!fgets(device, 255, stdin)) { |
| 46 | cerr << "Critical error! Failed fgets() in main()!\n"; |
| 47 | exit(1); |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 48 | } // if |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame^] | 49 | i = strlen(device); |
| 50 | if (i && device[i - 1] == '\n') |
| 51 | device[i - 1] = '\0'; |
| 52 | if (*device && theGPT.LoadPartitions(device)) |
| 53 | MainMenu(device, &theGPT); |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 54 | delete[] device; |
| 55 | break; |
| 56 | case 2: // basic usage |
| 57 | WinWarning(); |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame^] | 58 | if (theGPT.LoadPartitions(argv[1])) |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 59 | MainMenu(argv[1], &theGPT); |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 60 | break; |
| 61 | case 3: // usage with "-l" option |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 62 | if (strcmp(argv[1], "-l") == 0) { |
| 63 | device = argv[2]; |
| 64 | } else if (strcmp(argv[2], "-l") == 0) { |
| 65 | device = argv[1]; |
| 66 | } else { // 3 arguments, but none is "-l" |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 67 | cerr << "Usage: " << argv[0] << " [-l] device_file\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 68 | } // if/elseif/else |
| 69 | if (device != NULL) { |
srs5694 | 5d58fe0 | 2010-01-03 20:57:08 -0500 | [diff] [blame] | 70 | theGPT.JustLooking(); |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame^] | 71 | if (theGPT.LoadPartitions((string) device)) |
| 72 | theGPT.DisplayGPTData(); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 73 | } // if |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 74 | break; |
| 75 | default: |
| 76 | cerr << "Usage: " << argv[0] << " [-l] device_file\n"; |
| 77 | break; |
| 78 | } // switch |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 79 | } // main |
| 80 | |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 81 | // Accept a command and execute it. Returns only when the user |
| 82 | // wants to exit (such as after a 'w' or 'q' command). |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 83 | void MainMenu(string filename, GPTDataTextUI* theGPT) { |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame^] | 84 | char line[255], buFile[255]; |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 85 | int goOn = 1; |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 86 | PartType typeHelper; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 87 | uint32_t temp1, temp2; |
| 88 | |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 89 | do { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 90 | cout << "\nCommand (? for help): "; |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame^] | 91 | if (!fgets(line, 255, stdin)) { |
| 92 | cerr << "Critical error! Failed fgets() in MainMenu()!\n"; |
| 93 | exit(1); |
| 94 | } // if |
| 95 | switch (*line) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 96 | case '\n': |
| 97 | break; |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 98 | case 'b': case 'B': |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 99 | cout << "Enter backup filename to save: "; |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame^] | 100 | if (!fgets(line, 255, stdin)) { |
| 101 | exit(1); |
| 102 | cerr << "Critical error! Failed fgets() in MainMenu()!\n"; |
| 103 | } // if |
| 104 | sscanf(line, "%s", buFile); |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 105 | theGPT->SaveGPTBackup(buFile); |
| 106 | break; |
| 107 | case 'c': case 'C': |
| 108 | if (theGPT->GetPartRange(&temp1, &temp2) > 0) |
| 109 | theGPT->SetName(theGPT->GetPartNum()); |
| 110 | else |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 111 | cout << "No partitions\n"; |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 112 | break; |
| 113 | case 'd': case 'D': |
| 114 | theGPT->DeletePartition(); |
| 115 | break; |
| 116 | case 'i': case 'I': |
| 117 | theGPT->ShowDetails(); |
| 118 | break; |
| 119 | case 'l': case 'L': |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 120 | typeHelper.ShowAllTypes(); |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 121 | break; |
| 122 | case 'n': case 'N': |
| 123 | theGPT->CreatePartition(); |
| 124 | break; |
| 125 | case 'o': case 'O': |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 126 | cout << "This option deletes all partitions and creates a new protective MBR.\n" |
| 127 | << "Proceed? "; |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 128 | if (GetYN() == 'Y') { |
| 129 | theGPT->ClearGPTData(); |
| 130 | theGPT->MakeProtectiveMBR(); |
| 131 | } // if |
| 132 | break; |
| 133 | case 'p': case 'P': |
| 134 | theGPT->DisplayGPTData(); |
| 135 | break; |
| 136 | case 'q': case 'Q': |
| 137 | goOn = 0; |
| 138 | break; |
| 139 | case 'r': case 'R': |
| 140 | RecoveryMenu(filename, theGPT); |
| 141 | goOn = 0; |
| 142 | break; |
| 143 | case 's': case 'S': |
| 144 | theGPT->SortGPT(); |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 145 | cout << "You may need to edit /etc/fstab and/or your boot loader configuration!\n"; |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 146 | break; |
| 147 | case 't': case 'T': |
| 148 | theGPT->ChangePartType(); |
| 149 | break; |
| 150 | case 'v': case 'V': |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 151 | theGPT->Verify(); |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 152 | break; |
| 153 | case 'w': case 'W': |
| 154 | if (theGPT->SaveGPTData() == 1) |
| 155 | goOn = 0; |
| 156 | break; |
| 157 | case 'x': case 'X': |
| 158 | ExpertsMenu(filename, theGPT); |
| 159 | goOn = 0; |
| 160 | break; |
| 161 | default: |
| 162 | ShowCommands(); |
| 163 | break; |
| 164 | } // switch |
| 165 | } while (goOn); |
| 166 | } // MainMenu() |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 167 | |
| 168 | void ShowCommands(void) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 169 | cout << "b\tback up GPT data to a file\n"; |
| 170 | cout << "c\tchange a partition's name\n"; |
| 171 | cout << "d\tdelete a partition\n"; |
| 172 | cout << "i\tshow detailed information on a partition\n"; |
| 173 | cout << "l\tlist known partition types\n"; |
| 174 | cout << "n\tadd a new partition\n"; |
| 175 | cout << "o\tcreate a new empty GUID partition table (GPT)\n"; |
| 176 | cout << "p\tprint the partition table\n"; |
| 177 | cout << "q\tquit without saving changes\n"; |
| 178 | cout << "r\trecovery and transformation options (experts only)\n"; |
| 179 | cout << "s\tsort partitions\n"; |
| 180 | cout << "t\tchange a partition's type code\n"; |
| 181 | cout << "v\tverify disk\n"; |
| 182 | cout << "w\twrite table to disk and exit\n"; |
| 183 | cout << "x\textra functionality (experts only)\n"; |
| 184 | cout << "?\tprint this menu\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 185 | } // ShowCommands() |
| 186 | |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 187 | // Accept a recovery & transformation menu command. Returns only when the user |
| 188 | // issues an exit command, such as 'w' or 'q'. |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 189 | void RecoveryMenu(string filename, GPTDataTextUI* theGPT) { |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame^] | 190 | char line[255], buFile[255]; |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 191 | uint32_t temp1, numParts; |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 192 | int goOn = 1; |
| 193 | |
| 194 | do { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 195 | cout << "\nRecovery/transformation command (? for help): "; |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame^] | 196 | if (!fgets(line, 255, stdin)) { |
| 197 | cerr << "Critical error! Failed fgets() in RecoveryMenu()!\n"; |
| 198 | exit(1); |
| 199 | } // if |
| 200 | switch (*line) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 201 | case '\n': |
| 202 | break; |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 203 | case 'b': case 'B': |
| 204 | theGPT->RebuildMainHeader(); |
| 205 | break; |
| 206 | case 'c': case 'C': |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 207 | cout << "Warning! This will probably do weird things if you've converted an MBR to\n" |
| 208 | << "GPT form and haven't yet saved the GPT! Proceed? "; |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 209 | if (GetYN() == 'Y') |
| 210 | theGPT->LoadSecondTableAsMain(); |
| 211 | break; |
| 212 | case 'd': case 'D': |
| 213 | theGPT->RebuildSecondHeader(); |
| 214 | break; |
| 215 | case 'e': case 'E': |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 216 | cout << "Warning! This will probably do weird things if you've converted an MBR to\n" |
| 217 | << "GPT form and haven't yet saved the GPT! Proceed? "; |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 218 | if (GetYN() == 'Y') |
| 219 | theGPT->LoadMainTable(); |
| 220 | break; |
| 221 | case 'f': case 'F': |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 222 | cout << "Warning! This will destroy the currently defined partitions! Proceed? "; |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 223 | if (GetYN() == 'Y') { |
| 224 | if (theGPT->LoadMBR(filename) == 1) { // successful load |
| 225 | theGPT->XFormPartitions(); |
| 226 | } else { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 227 | cout << "Problem loading MBR! GPT is untouched; regenerating protective MBR!\n"; |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 228 | theGPT->MakeProtectiveMBR(); |
| 229 | } // if/else |
| 230 | } // if |
| 231 | break; |
| 232 | case 'g': case 'G': |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 233 | numParts = theGPT->GetNumParts(); |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 234 | temp1 = theGPT->XFormToMBR(); |
| 235 | if (temp1 > 0) { |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 236 | cout << "\nConverted " << temp1 << " partitions. Finalize and exit? "; |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 237 | if (GetYN() == 'Y') { |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 238 | if ((theGPT->DestroyGPT() > 0) && (theGPT->SaveMBR())) |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 239 | goOn = 0; |
| 240 | } else { |
| 241 | theGPT->MakeProtectiveMBR(); |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 242 | theGPT->SetGPTSize(numParts); |
| 243 | cout << "Note: New protective MBR created\n\n"; |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 244 | } // if/else |
| 245 | } // if |
| 246 | break; |
| 247 | case 'h': case 'H': |
| 248 | theGPT->MakeHybrid(); |
| 249 | break; |
| 250 | case 'i': case 'I': |
| 251 | theGPT->ShowDetails(); |
| 252 | break; |
| 253 | case 'l': case 'L': |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 254 | cout << "Enter backup filename to load: "; |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame^] | 255 | if (!fgets(line, 255, stdin)) { |
| 256 | cerr << "Critical error! Failed fgets() in RecoveryMenu()!\n"; |
| 257 | exit(1); |
| 258 | } // if |
| 259 | sscanf(line, "%s", buFile); |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 260 | theGPT->LoadGPTBackup(buFile); |
| 261 | break; |
| 262 | case 'm': case 'M': |
| 263 | MainMenu(filename, theGPT); |
| 264 | goOn = 0; |
| 265 | break; |
| 266 | case 'o': case 'O': |
| 267 | theGPT->DisplayMBRData(); |
| 268 | break; |
| 269 | case 'p': case 'P': |
| 270 | theGPT->DisplayGPTData(); |
| 271 | break; |
| 272 | case 'q': case 'Q': |
| 273 | goOn = 0; |
| 274 | break; |
| 275 | case 't': case 'T': |
| 276 | theGPT->XFormDisklabel(); |
| 277 | break; |
| 278 | case 'v': case 'V': |
| 279 | theGPT->Verify(); |
| 280 | break; |
| 281 | case 'w': case 'W': |
| 282 | if (theGPT->SaveGPTData() == 1) { |
| 283 | goOn = 0; |
| 284 | } // if |
| 285 | break; |
| 286 | case 'x': case 'X': |
| 287 | ExpertsMenu(filename, theGPT); |
| 288 | goOn = 0; |
| 289 | break; |
| 290 | default: |
| 291 | ShowRecoveryCommands(); |
| 292 | break; |
| 293 | } // switch |
| 294 | } while (goOn); |
| 295 | } // RecoveryMenu() |
| 296 | |
| 297 | void ShowRecoveryCommands(void) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 298 | cout << "b\tuse backup GPT header (rebuilding main)\n"; |
| 299 | cout << "c\tload backup partition table from disk (rebuilding main)\n"; |
| 300 | cout << "d\tuse main GPT header (rebuilding backup)\n"; |
| 301 | cout << "e\tload main partition table from disk (rebuilding backup)\n"; |
| 302 | cout << "f\tload MBR and build fresh GPT from it\n"; |
| 303 | cout << "g\tconvert GPT into MBR and exit\n"; |
| 304 | cout << "h\tmake hybrid MBR\n"; |
| 305 | cout << "i\tshow detailed information on a partition\n"; |
| 306 | cout << "l\tload partition data from a backup file\n"; |
| 307 | cout << "m\treturn to main menu\n"; |
| 308 | cout << "o\tprint protective MBR data\n"; |
| 309 | cout << "p\tprint the partition table\n"; |
| 310 | cout << "q\tquit without saving changes\n"; |
| 311 | cout << "t\ttransform BSD disklabel partition\n"; |
| 312 | cout << "v\tverify disk\n"; |
| 313 | cout << "w\twrite table to disk and exit\n"; |
| 314 | cout << "x\textra functionality (experts only)\n"; |
| 315 | cout << "?\tprint this menu\n"; |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 316 | } // ShowRecoveryCommands() |
| 317 | |
| 318 | // Accept an experts' menu command. Returns only after the user |
| 319 | // selects an exit command, such as 'w' or 'q'. |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 320 | void ExpertsMenu(string filename, GPTDataTextUI* theGPT) { |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame^] | 321 | GPTData secondDevice; |
| 322 | char line[255], *device; |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 323 | uint32_t pn, temp1, temp2; |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame^] | 324 | int goOn = 1; |
| 325 | size_t i; |
srs5694 | 5a08175 | 2010-09-24 20:39:41 -0400 | [diff] [blame] | 326 | char guidStr[255]; |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 327 | GUIDData aGUID; |
srs5694 | a8582cf | 2010-03-19 14:21:59 -0400 | [diff] [blame] | 328 | ostringstream prompt; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 329 | |
| 330 | do { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 331 | cout << "\nExpert command (? for help): "; |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame^] | 332 | if (!fgets(line, 255, stdin)) { |
| 333 | cerr << "Critical error! Failed fgets() in ExpertsMenu()!\n"; |
| 334 | exit(1); |
| 335 | } // if |
| 336 | switch (*line) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 337 | case '\n': |
| 338 | break; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 339 | case 'a': case 'A': |
| 340 | if (theGPT->GetPartRange(&temp1, &temp2) > 0) |
| 341 | theGPT->SetAttributes(theGPT->GetPartNum()); |
| 342 | else |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 343 | cout << "No partitions\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 344 | break; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 345 | case 'c': case 'C': |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 346 | if (theGPT->GetPartRange(&temp1, &temp2) > 0) { |
| 347 | pn = theGPT->GetPartNum(); |
srs5694 | 5a08175 | 2010-09-24 20:39:41 -0400 | [diff] [blame] | 348 | cout << "Enter the partition's new unique GUID ('R' to randomize): "; |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame^] | 349 | if (!fgets(guidStr, 255, stdin)) { |
| 350 | cerr << "Critical error! Failed fgets() in ExpertsMenu()!\n"; |
| 351 | exit(1); |
| 352 | } // if |
srs5694 | 5a08175 | 2010-09-24 20:39:41 -0400 | [diff] [blame] | 353 | if ((strlen(guidStr) >= 33) || (guidStr[0] == 'R') || (guidStr[0] == 'r')) { |
| 354 | theGPT->SetPartitionGUID(pn, (GUIDData) guidStr); |
| 355 | cout << "New GUID is " << theGPT->operator[](pn).GetUniqueGUID() << "\n"; |
| 356 | } else { |
| 357 | cout << "GUID is too short!\n"; |
| 358 | } // if/else |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 359 | } else cout << "No partitions\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 360 | break; |
srs5694 | 1d1448a | 2009-12-31 21:20:19 -0500 | [diff] [blame] | 361 | case 'd': case 'D': |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 362 | cout << "Partitions will begin on " << theGPT->GetAlignment() |
| 363 | << "-sector boundaries.\n"; |
srs5694 | 1d1448a | 2009-12-31 21:20:19 -0500 | [diff] [blame] | 364 | break; |
srs5694 | 8bb7876 | 2009-11-24 15:43:49 -0500 | [diff] [blame] | 365 | case 'e': case 'E': |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 366 | cout << "Relocating backup data structures to the end of the disk\n"; |
srs5694 | 247657a | 2009-11-26 18:36:12 -0500 | [diff] [blame] | 367 | theGPT->MoveSecondHeaderToEnd(); |
srs5694 | 8bb7876 | 2009-11-24 15:43:49 -0500 | [diff] [blame] | 368 | break; |
srs5694 | 9ba5421 | 2010-05-18 23:24:02 -0400 | [diff] [blame] | 369 | case 'f': case 'F': |
| 370 | theGPT->RandomizeGUIDs(); |
| 371 | break; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 372 | case 'g': case 'G': |
srs5694 | 5a08175 | 2010-09-24 20:39:41 -0400 | [diff] [blame] | 373 | cout << "Enter the disk's unique GUID ('R' to randomize): "; |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame^] | 374 | if (!fgets(guidStr, 255, stdin)) { |
| 375 | cerr << "Critical error! Failed fgets() in ExpertsMenu()!\n"; |
| 376 | exit(1); |
| 377 | } // if |
srs5694 | 5a08175 | 2010-09-24 20:39:41 -0400 | [diff] [blame] | 378 | if ((strlen(guidStr) >= 33) || (guidStr[0] == 'R') || (guidStr[0] == 'r')) { |
| 379 | theGPT->SetDiskGUID((GUIDData) guidStr); |
| 380 | cout << "The new disk GUID is " << theGPT->GetDiskGUID() << "\n"; |
| 381 | } else |
| 382 | cout << "GUID is too short!\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 383 | break; |
srs5694 | 9ba5421 | 2010-05-18 23:24:02 -0400 | [diff] [blame] | 384 | case 'h': case 'H': |
| 385 | theGPT->RecomputeCHS(); |
| 386 | break; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 387 | case 'i': case 'I': |
| 388 | theGPT->ShowDetails(); |
| 389 | break; |
srs5694 | 1d1448a | 2009-12-31 21:20:19 -0500 | [diff] [blame] | 390 | case 'l': case 'L': |
srs5694 | a8582cf | 2010-03-19 14:21:59 -0400 | [diff] [blame] | 391 | prompt.seekp(0); |
| 392 | prompt << "Enter the sector alignment value (1-" << MAX_ALIGNMENT << ", default = " |
| 393 | << DEFAULT_ALIGNMENT << "): "; |
| 394 | temp1 = GetNumber(1, MAX_ALIGNMENT, DEFAULT_ALIGNMENT, prompt.str()); |
srs5694 | 1d1448a | 2009-12-31 21:20:19 -0500 | [diff] [blame] | 395 | theGPT->SetAlignment(temp1); |
| 396 | break; |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 397 | case 'm': case 'M': |
| 398 | MainMenu(filename, theGPT); |
| 399 | goOn = 0; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 400 | break; |
| 401 | case 'n': case 'N': |
| 402 | theGPT->MakeProtectiveMBR(); |
| 403 | break; |
| 404 | case 'o': case 'O': |
| 405 | theGPT->DisplayMBRData(); |
| 406 | break; |
| 407 | case 'p': case 'P': |
| 408 | theGPT->DisplayGPTData(); |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 409 | break; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 410 | case 'q': case 'Q': |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame^] | 411 | goOn = 0; |
| 412 | break; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 413 | case 'r': case 'R': |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 414 | RecoveryMenu(filename, theGPT); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 415 | goOn = 0; |
| 416 | break; |
| 417 | case 's': case 'S': |
| 418 | theGPT->ResizePartitionTable(); |
| 419 | break; |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame^] | 420 | case 't': case 'T': |
| 421 | theGPT->SwapPartitions(); |
| 422 | break; |
srs5694 | f9312b0 | 2010-07-06 15:39:51 -0400 | [diff] [blame] | 423 | case 'u': case 'U': |
| 424 | cout << "Type device filename, or press <Enter> to exit: "; |
| 425 | device = new char[255]; |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame^] | 426 | if (!fgets(device, 255, stdin)) { |
| 427 | cerr << "Critical error! Failed fgets() in ExpertsMenu()!\n"; |
| 428 | exit(1); |
srs5694 | f9312b0 | 2010-07-06 15:39:51 -0400 | [diff] [blame] | 429 | } // if |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame^] | 430 | i = strlen(device); |
| 431 | if (i && device[i - 1] == '\n') |
| 432 | device[i - 1] = '\0'; |
| 433 | if (*device && strlen(device) > 0) { |
| 434 | secondDevice = *theGPT; |
| 435 | secondDevice.SetFile(device); |
| 436 | secondDevice.SaveGPTData(0); |
| 437 | } // if |
srs5694 | f9312b0 | 2010-07-06 15:39:51 -0400 | [diff] [blame] | 438 | delete[] device; |
| 439 | break; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 440 | case 'v': case 'V': |
| 441 | theGPT->Verify(); |
| 442 | break; |
| 443 | case 'w': case 'W': |
| 444 | if (theGPT->SaveGPTData() == 1) { |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 445 | goOn = 0; |
| 446 | } // if |
| 447 | break; |
srs5694 | c0ca8f8 | 2009-08-20 21:35:25 -0400 | [diff] [blame] | 448 | case 'z': case 'Z': |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 449 | if (theGPT->DestroyGPTwPrompt() == 1) { |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 450 | goOn = 0; |
srs5694 | c0ca8f8 | 2009-08-20 21:35:25 -0400 | [diff] [blame] | 451 | } |
| 452 | break; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 453 | default: |
| 454 | ShowExpertCommands(); |
| 455 | break; |
| 456 | } // switch |
| 457 | } while (goOn); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 458 | } // ExpertsMenu() |
| 459 | |
| 460 | void ShowExpertCommands(void) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 461 | cout << "a\tset attributes\n"; |
| 462 | cout << "c\tchange partition GUID\n"; |
| 463 | cout << "d\tdisplay the sector alignment value\n"; |
| 464 | cout << "e\trelocate backup data structures to the end of the disk\n"; |
| 465 | cout << "g\tchange disk GUID\n"; |
srs5694 | 9ba5421 | 2010-05-18 23:24:02 -0400 | [diff] [blame] | 466 | cout << "h\trecompute CHS values in protective/hybrid MBR\n"; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 467 | cout << "i\tshow detailed information on a partition\n"; |
| 468 | cout << "l\tset the sector alignment value\n"; |
| 469 | cout << "m\treturn to main menu\n"; |
| 470 | cout << "n\tcreate a new protective MBR\n"; |
| 471 | cout << "o\tprint protective MBR data\n"; |
| 472 | cout << "p\tprint the partition table\n"; |
| 473 | cout << "q\tquit without saving changes\n"; |
| 474 | cout << "r\trecovery and transformation options (experts only)\n"; |
| 475 | cout << "s\tresize partition table\n"; |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 476 | cout << "t\ttranspose two partition table entries\n"; |
srs5694 | f9312b0 | 2010-07-06 15:39:51 -0400 | [diff] [blame] | 477 | cout << "u\tReplicate partition table on new device\n"; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 478 | cout << "v\tverify disk\n"; |
| 479 | cout << "w\twrite table to disk and exit\n"; |
| 480 | cout << "z\tzap (destroy) GPT data structures and exit\n"; |
| 481 | cout << "?\tprint this menu\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 482 | } // ShowExpertCommands() |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 483 | |
| 484 | // On Windows, display a warning and ask whether to continue. If the user elects |
| 485 | // not to continue, exit immediately. |
| 486 | void WinWarning(void) { |
| 487 | #ifdef _WIN32 |
| 488 | cout << "\a************************************************************************\n" |
| 489 | << "Most versions of Windows cannot boot from a GPT disk, and most varieties\n" |
| 490 | << "prior to Vista cannot read GPT disks. Therefore, you should exit now\n" |
| 491 | << "unless you understand the implications of converting MBR to GPT, editing\n" |
| 492 | << "an existing GPT disk, or creating a new GPT disk layout!\n" |
| 493 | << "************************************************************************\n\n"; |
| 494 | cout << "Are you SURE you want to continue? "; |
| 495 | if (GetYN() != 'Y') |
| 496 | exit(0); |
| 497 | #endif |
| 498 | } // WinWarning() |