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