srs5694 | fad0642 | 2010-02-19 17:19:17 -0500 | [diff] [blame] | 1 | /* |
srs5694 | 699941e | 2011-03-21 21:33:57 -0400 | [diff] [blame^] | 2 | Copyright (C) 2010-2011 <Roderick W. Smith> |
srs5694 | fad0642 | 2010-02-19 17:19:17 -0500 | [diff] [blame] | 3 | |
| 4 | This program is free software; you can redistribute it and/or modify |
| 5 | it under the terms of the GNU General Public License as published by |
| 6 | the Free Software Foundation; either version 2 of the License, or |
| 7 | (at your option) any later version. |
| 8 | |
| 9 | This program is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | GNU General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU General Public License along |
| 15 | with this program; if not, write to the Free Software Foundation, Inc., |
| 16 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 17 | |
| 18 | */ |
| 19 | |
| 20 | /* This class implements an interactive text-mode interface atop the |
| 21 | GPTData class */ |
| 22 | |
| 23 | #include <string.h> |
| 24 | #include <errno.h> |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 25 | #include <stdint.h> |
srs5694 | 61768bc | 2010-07-04 01:54:00 -0400 | [diff] [blame] | 26 | #include <limits.h> |
srs5694 | fad0642 | 2010-02-19 17:19:17 -0500 | [diff] [blame] | 27 | #include <iostream> |
| 28 | #include <sstream> |
srs5694 | 1c6f8b0 | 2010-02-21 11:09:20 -0500 | [diff] [blame] | 29 | #include <cstdio> |
srs5694 | fad0642 | 2010-02-19 17:19:17 -0500 | [diff] [blame] | 30 | #include "attributes.h" |
| 31 | #include "gpttext.h" |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 32 | #include "support.h" |
srs5694 | fad0642 | 2010-02-19 17:19:17 -0500 | [diff] [blame] | 33 | |
| 34 | using namespace std; |
| 35 | |
| 36 | /******************************************* |
| 37 | * * |
| 38 | * GPTDataText class and related structures * |
| 39 | * * |
| 40 | ********************************************/ |
| 41 | |
| 42 | GPTDataTextUI::GPTDataTextUI(void) : GPTData() { |
| 43 | } // default constructor |
| 44 | |
| 45 | GPTDataTextUI::GPTDataTextUI(string filename) : GPTData(filename) { |
| 46 | } // constructor passing filename |
| 47 | |
| 48 | GPTDataTextUI::~GPTDataTextUI(void) { |
| 49 | } // default destructor |
| 50 | |
| 51 | /********************************************************************* |
| 52 | * * |
| 53 | * The following functions are extended (interactive) versions of * |
| 54 | * simpler functions in the base class.... * |
| 55 | * * |
| 56 | *********************************************************************/ |
| 57 | |
| 58 | // Overridden function; calls base-class function and then makes |
| 59 | // additional queries of the user, if the base-class function can't |
| 60 | // decide what to do. |
| 61 | WhichToUse GPTDataTextUI::UseWhichPartitions(void) { |
| 62 | WhichToUse which; |
| 63 | MBRValidity mbrState; |
| 64 | int answer; |
| 65 | |
| 66 | which = GPTData::UseWhichPartitions(); |
| 67 | if ((which != use_abort) || beQuiet) |
| 68 | return which; |
| 69 | |
| 70 | // If we get past here, it means that the non-interactive tests were |
| 71 | // inconclusive, so we must ask the user which table to use.... |
| 72 | mbrState = protectiveMBR.GetValidity(); |
| 73 | |
| 74 | if ((state == gpt_valid) && (mbrState == mbr)) { |
| 75 | cout << "Found valid MBR and GPT. Which do you want to use?\n"; |
| 76 | answer = GetNumber(1, 3, 2, " 1 - MBR\n 2 - GPT\n 3 - Create blank GPT\n\nYour answer: "); |
| 77 | if (answer == 1) { |
| 78 | which = use_mbr; |
| 79 | } else if (answer == 2) { |
| 80 | which = use_gpt; |
| 81 | cout << "Using GPT and creating fresh protective MBR.\n"; |
| 82 | } else which = use_new; |
| 83 | } // if |
| 84 | |
| 85 | // Nasty decisions here -- GPT is present, but corrupt (bad CRCs or other |
| 86 | // problems) |
| 87 | if (state == gpt_corrupt) { |
| 88 | if ((mbrState == mbr) || (mbrState == hybrid)) { |
| 89 | cout << "Found valid MBR and corrupt GPT. Which do you want to use? (Using the\n" |
| 90 | << "GPT MAY permit recovery of GPT data.)\n"; |
| 91 | answer = GetNumber(1, 3, 2, " 1 - MBR\n 2 - GPT\n 3 - Create blank GPT\n\nYour answer: "); |
| 92 | if (answer == 1) { |
| 93 | which = use_mbr; |
| 94 | } else if (answer == 2) { |
| 95 | which = use_gpt; |
| 96 | } else which = use_new; |
| 97 | } else if (mbrState == invalid) { |
| 98 | cout << "Found invalid MBR and corrupt GPT. What do you want to do? (Using the\n" |
| 99 | << "GPT MAY permit recovery of GPT data.)\n"; |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 100 | answer = GetNumber(1, 2, 1, " 1 - Use current GPT\n 2 - Create blank GPT\n\nYour answer: "); |
srs5694 | fad0642 | 2010-02-19 17:19:17 -0500 | [diff] [blame] | 101 | if (answer == 1) { |
| 102 | which = use_gpt; |
| 103 | } else which = use_new; |
| 104 | } // if/else/else |
| 105 | } // if (corrupt GPT) |
| 106 | |
| 107 | return which; |
| 108 | } // UseWhichPartitions() |
| 109 | |
| 110 | // Ask the user for a partition number; and prompt for verification |
| 111 | // if the requested partition isn't of a known BSD type. |
| 112 | // Lets the base-class function do the work, and returns its value (the |
| 113 | // number of converted partitions). |
| 114 | int GPTDataTextUI::XFormDisklabel(void) { |
| 115 | uint32_t partNum; |
| 116 | uint16_t hexCode; |
| 117 | int goOn = 1, numDone = 0; |
| 118 | BSDData disklabel; |
| 119 | |
| 120 | partNum = GetPartNum(); |
| 121 | |
| 122 | // Now see if the specified partition has a BSD type code.... |
| 123 | hexCode = partitions[partNum].GetHexType(); |
| 124 | if ((hexCode != 0xa500) && (hexCode != 0xa900)) { |
| 125 | cout << "Specified partition doesn't have a disklabel partition type " |
| 126 | << "code.\nContinue anyway? "; |
| 127 | goOn = (GetYN() == 'Y'); |
| 128 | } // if |
| 129 | |
| 130 | if (goOn) |
| 131 | numDone = GPTData::XFormDisklabel(partNum); |
| 132 | |
| 133 | return numDone; |
| 134 | } // GPTData::XFormDisklabel(int i) |
| 135 | |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 136 | |
srs5694 | fad0642 | 2010-02-19 17:19:17 -0500 | [diff] [blame] | 137 | /********************************************************************* |
| 138 | * * |
| 139 | * Begin functions that obtain information from the users, and often * |
| 140 | * do something with that information (call other functions) * |
| 141 | * * |
| 142 | *********************************************************************/ |
| 143 | |
| 144 | // Prompts user for partition number and returns the result. |
| 145 | uint32_t GPTDataTextUI::GetPartNum(void) { |
| 146 | uint32_t partNum; |
| 147 | uint32_t low, high; |
| 148 | ostringstream prompt; |
| 149 | |
| 150 | if (GetPartRange(&low, &high) > 0) { |
| 151 | prompt << "Partition number (" << low + 1 << "-" << high + 1 << "): "; |
| 152 | partNum = GetNumber(low + 1, high + 1, low, prompt.str()); |
| 153 | } else partNum = 1; |
| 154 | return (partNum - 1); |
| 155 | } // GPTDataTextUI::GetPartNum() |
| 156 | |
| 157 | // What it says: Resize the partition table. (Default is 128 entries.) |
| 158 | void GPTDataTextUI::ResizePartitionTable(void) { |
| 159 | int newSize; |
| 160 | ostringstream prompt; |
| 161 | uint32_t curLow, curHigh; |
| 162 | |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 163 | cout << "Current partition table size is " << numParts << ".\n"; |
srs5694 | fad0642 | 2010-02-19 17:19:17 -0500 | [diff] [blame] | 164 | GetPartRange(&curLow, &curHigh); |
| 165 | curHigh++; // since GetPartRange() returns numbers starting from 0... |
| 166 | // There's no point in having fewer than four partitions.... |
| 167 | if (curHigh < (blockSize / GPT_SIZE)) |
| 168 | curHigh = blockSize / GPT_SIZE; |
| 169 | prompt << "Enter new size (" << curHigh << " up, default " << NUM_GPT_ENTRIES << "): "; |
| 170 | newSize = GetNumber(4, 65535, 128, prompt.str()); |
| 171 | if (newSize < 128) { |
| 172 | cout << "Caution: The partition table size should officially be 16KB or larger,\n" |
| 173 | << "which works out to 128 entries. In practice, smaller tables seem to\n" |
| 174 | << "work with most OSes, but this practice is risky. I'm proceeding with\n" |
| 175 | << "the resize, but you may want to reconsider this action and undo it.\n\n"; |
| 176 | } // if |
| 177 | SetGPTSize(newSize); |
| 178 | } // GPTDataTextUI::ResizePartitionTable() |
| 179 | |
| 180 | // Interactively create a partition |
| 181 | void GPTDataTextUI::CreatePartition(void) { |
srs5694 | 5a08175 | 2010-09-24 20:39:41 -0400 | [diff] [blame] | 182 | uint64_t firstBlock, firstInLargest, lastBlock, sector, origSector; |
srs5694 | fad0642 | 2010-02-19 17:19:17 -0500 | [diff] [blame] | 183 | uint32_t firstFreePart = 0; |
| 184 | ostringstream prompt1, prompt2, prompt3; |
| 185 | int partNum; |
| 186 | |
| 187 | // Find first free partition... |
| 188 | while (partitions[firstFreePart].GetFirstLBA() != 0) { |
| 189 | firstFreePart++; |
| 190 | } // while |
| 191 | |
| 192 | if (((firstBlock = FindFirstAvailable()) != 0) && |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 193 | (firstFreePart < numParts)) { |
srs5694 | fad0642 | 2010-02-19 17:19:17 -0500 | [diff] [blame] | 194 | lastBlock = FindLastAvailable(); |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 195 | firstInLargest = FindFirstInLargest(); |
| 196 | |
| 197 | // Get partition number.... |
| 198 | do { |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 199 | prompt1 << "Partition number (" << firstFreePart + 1 << "-" << numParts |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 200 | << ", default " << firstFreePart + 1 << "): "; |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 201 | partNum = GetNumber(firstFreePart + 1, numParts, |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 202 | firstFreePart + 1, prompt1.str()) - 1; |
| 203 | if (partitions[partNum].GetFirstLBA() != 0) |
| 204 | cout << "partition " << partNum + 1 << " is in use.\n"; |
| 205 | } while (partitions[partNum].GetFirstLBA() != 0); |
| 206 | |
| 207 | // Get first block for new partition... |
| 208 | prompt2 << "First sector (" << firstBlock << "-" << lastBlock << ", default = " |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 209 | << firstInLargest << ") or {+-}size{KMGTP}: "; |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 210 | do { |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 211 | sector = GetSectorNum(firstBlock, lastBlock, firstInLargest, blockSize, prompt2.str()); |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 212 | } while (IsFree(sector) == 0); |
srs5694 | 5a08175 | 2010-09-24 20:39:41 -0400 | [diff] [blame] | 213 | origSector = sector; |
| 214 | if (Align(§or)) { |
| 215 | cout << "Information: Moved requested sector from " << origSector << " to " |
| 216 | << sector << " in\norder to align on " << sectorAlignment |
| 217 | << "-sector boundaries.\n"; |
| 218 | if (!beQuiet) |
| 219 | cout << "Use 'l' on the experts' menu to adjust alignment\n"; |
| 220 | } // if |
| 221 | // Align(§or); // Align sector to correct multiple |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 222 | firstBlock = sector; |
| 223 | |
| 224 | // Get last block for new partitions... |
| 225 | lastBlock = FindLastInFree(firstBlock); |
| 226 | prompt3 << "Last sector (" << firstBlock << "-" << lastBlock << ", default = " |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 227 | << lastBlock << ") or {+-}size{KMGTP}: "; |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 228 | do { |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 229 | sector = GetSectorNum(firstBlock, lastBlock, lastBlock, blockSize, prompt3.str()); |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 230 | } while (IsFree(sector) == 0); |
| 231 | lastBlock = sector; |
| 232 | |
| 233 | firstFreePart = GPTData::CreatePartition(partNum, firstBlock, lastBlock); |
| 234 | partitions[partNum].ChangeType(); |
| 235 | partitions[partNum].SetDefaultDescription(); |
srs5694 | fad0642 | 2010-02-19 17:19:17 -0500 | [diff] [blame] | 236 | } else { |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 237 | if (firstFreePart >= numParts) |
| 238 | cout << "No table partition entries left\n"; |
| 239 | else |
| 240 | cout << "No free sectors available\n"; |
srs5694 | fad0642 | 2010-02-19 17:19:17 -0500 | [diff] [blame] | 241 | } // if/else |
| 242 | } // GPTDataTextUI::CreatePartition() |
| 243 | |
| 244 | // Interactively delete a partition (duh!) |
| 245 | void GPTDataTextUI::DeletePartition(void) { |
| 246 | int partNum; |
| 247 | uint32_t low, high; |
| 248 | ostringstream prompt; |
| 249 | |
| 250 | if (GetPartRange(&low, &high) > 0) { |
| 251 | prompt << "Partition number (" << low + 1 << "-" << high + 1 << "): "; |
| 252 | partNum = GetNumber(low + 1, high + 1, low, prompt.str()); |
| 253 | GPTData::DeletePartition(partNum - 1); |
| 254 | } else { |
| 255 | cout << "No partitions\n"; |
| 256 | } // if/else |
| 257 | } // GPTDataTextUI::DeletePartition() |
| 258 | |
| 259 | // Prompt user for a partition number, then change its type code |
| 260 | // using ChangeGPTType(struct GPTPartition*) function. |
| 261 | void GPTDataTextUI::ChangePartType(void) { |
| 262 | int partNum; |
| 263 | uint32_t low, high; |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 264 | |
srs5694 | fad0642 | 2010-02-19 17:19:17 -0500 | [diff] [blame] | 265 | if (GetPartRange(&low, &high) > 0) { |
| 266 | partNum = GetPartNum(); |
| 267 | partitions[partNum].ChangeType(); |
| 268 | } else { |
| 269 | cout << "No partitions\n"; |
| 270 | } // if/else |
| 271 | } // GPTDataTextUI::ChangePartType() |
| 272 | |
srs5694 | 815fb65 | 2011-03-18 12:35:56 -0400 | [diff] [blame] | 273 | // Prompt user for a partition number, then change its unique |
| 274 | // GUID. |
| 275 | void GPTDataTextUI::ChangeUniqueGuid(void) { |
| 276 | int partNum; |
| 277 | uint32_t low, high; |
| 278 | string guidStr; |
| 279 | |
| 280 | if (GetPartRange(&low, &high) > 0) { |
| 281 | partNum = GetPartNum(); |
| 282 | cout << "Enter the partition's new unique GUID ('R' to randomize): "; |
| 283 | guidStr = ReadString(); |
| 284 | if ((guidStr.length() >= 32) || (guidStr[0] == 'R') || (guidStr[0] == 'r')) { |
| 285 | SetPartitionGUID(partNum, (GUIDData) guidStr); |
| 286 | cout << "New GUID is " << partitions[partNum].GetUniqueGUID() << "\n"; |
| 287 | } else { |
| 288 | cout << "GUID is too short!\n"; |
| 289 | } // if/else |
| 290 | } else |
| 291 | cout << "No partitions\n"; |
| 292 | } // GPTDataTextUI::ChangeUniqueGuid() |
| 293 | |
srs5694 | fad0642 | 2010-02-19 17:19:17 -0500 | [diff] [blame] | 294 | // Partition attributes seem to be rarely used, but I want a way to |
| 295 | // adjust them for completeness.... |
| 296 | void GPTDataTextUI::SetAttributes(uint32_t partNum) { |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 297 | partitions[partNum].SetAttributes(); |
srs5694 | fad0642 | 2010-02-19 17:19:17 -0500 | [diff] [blame] | 298 | } // GPTDataTextUI::SetAttributes() |
| 299 | |
srs5694 | 699941e | 2011-03-21 21:33:57 -0400 | [diff] [blame^] | 300 | // Prompts the user for a partition name and sets the partition's |
| 301 | // name. Returns 1 on success, 0 on failure (invalid partition |
| 302 | // number). (Note that the function skips prompting when an |
| 303 | // invalid partition number is detected.) |
| 304 | int GPTDataTextUI::SetName(uint32_t partNum) { |
| 305 | UnicodeString theName = ""; |
| 306 | int retval = 1; |
| 307 | |
| 308 | if (IsUsedPartNum(partNum)) { |
| 309 | cout << "Enter name: "; |
| 310 | theName = ReadUString(); |
| 311 | partitions[partNum].SetName(theName); |
| 312 | } else { |
| 313 | cerr << "Invalid partition number (" << partNum << ")\n"; |
| 314 | retval = 0; |
| 315 | } // if/else |
| 316 | |
| 317 | return retval; |
| 318 | } // GPTDataTextUI::SetName() |
| 319 | |
srs5694 | fad0642 | 2010-02-19 17:19:17 -0500 | [diff] [blame] | 320 | // Ask user for two partition numbers and swap them in the table. Note that |
| 321 | // this just reorders table entries; it doesn't adjust partition layout on |
| 322 | // the disk. |
| 323 | // Returns 1 if successful, 0 if not. (If user enters identical numbers, it |
| 324 | // counts as successful.) |
| 325 | int GPTDataTextUI::SwapPartitions(void) { |
| 326 | int partNum1, partNum2, didIt = 0; |
| 327 | uint32_t low, high; |
| 328 | ostringstream prompt; |
| 329 | GPTPart temp; |
| 330 | |
| 331 | if (GetPartRange(&low, &high) > 0) { |
| 332 | partNum1 = GetPartNum(); |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 333 | if (high >= numParts - 1) |
srs5694 | fad0642 | 2010-02-19 17:19:17 -0500 | [diff] [blame] | 334 | high = 0; |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 335 | prompt << "New partition number (1-" << numParts |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 336 | << ", default " << high + 2 << "): "; |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 337 | partNum2 = GetNumber(1, numParts, high + 2, prompt.str()) - 1; |
srs5694 | fad0642 | 2010-02-19 17:19:17 -0500 | [diff] [blame] | 338 | didIt = GPTData::SwapPartitions(partNum1, partNum2); |
| 339 | } else { |
| 340 | cout << "No partitions\n"; |
| 341 | } // if/else |
| 342 | return didIt; |
| 343 | } // GPTDataTextUI::SwapPartitionNumbers() |
| 344 | |
| 345 | // This function destroys the on-disk GPT structures. Returns 1 if the user |
| 346 | // confirms destruction, 0 if the user aborts or if there's a disk error. |
| 347 | int GPTDataTextUI::DestroyGPTwPrompt(void) { |
| 348 | int allOK = 1; |
| 349 | |
| 350 | if ((apmFound) || (bsdFound)) { |
| 351 | cout << "WARNING: APM or BSD disklabel structures detected! This operation could\n" |
| 352 | << "damage any APM or BSD partitions on this disk!\n"; |
| 353 | } // if APM or BSD |
| 354 | cout << "\a\aAbout to wipe out GPT on " << device << ". Proceed? "; |
| 355 | if (GetYN() == 'Y') { |
| 356 | if (DestroyGPT()) { |
| 357 | // Note on below: Touch the MBR only if the user wants it completely |
| 358 | // blanked out. Version 0.4.2 deleted the 0xEE partition and re-wrote |
| 359 | // the MBR, but this could wipe out a valid MBR that the program |
| 360 | // had subsequently discarded (say, if it conflicted with older GPT |
| 361 | // structures). |
| 362 | cout << "Blank out MBR? "; |
| 363 | if (GetYN() == 'Y') { |
| 364 | DestroyMBR(); |
| 365 | } else { |
| 366 | cout << "MBR is unchanged. You may need to delete an EFI GPT (0xEE) partition\n" |
| 367 | << "with fdisk or another tool.\n"; |
| 368 | } // if/else |
| 369 | } else allOK = 0; // if GPT structures destroyed |
| 370 | } else allOK = 0; // if user confirms destruction |
| 371 | return (allOK); |
| 372 | } // GPTDataTextUI::DestroyGPTwPrompt() |
| 373 | |
| 374 | // Get partition number from user and then call ShowPartDetails(partNum) |
| 375 | // to show its detailed information |
| 376 | void GPTDataTextUI::ShowDetails(void) { |
| 377 | int partNum; |
| 378 | uint32_t low, high; |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 379 | |
srs5694 | fad0642 | 2010-02-19 17:19:17 -0500 | [diff] [blame] | 380 | if (GetPartRange(&low, &high) > 0) { |
| 381 | partNum = GetPartNum(); |
| 382 | ShowPartDetails(partNum); |
| 383 | } else { |
| 384 | cout << "No partitions\n"; |
| 385 | } // if/else |
| 386 | } // GPTDataTextUI::ShowDetails() |
| 387 | |
| 388 | // Create a hybrid MBR -- an ugly, funky thing that helps GPT work with |
| 389 | // OSes that don't understand GPT. |
| 390 | void GPTDataTextUI::MakeHybrid(void) { |
| 391 | uint32_t partNums[3]; |
srs5694 | 5a60853 | 2011-03-17 13:53:01 -0400 | [diff] [blame] | 392 | string line; |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 393 | int numPartsToCvt, i, j, mbrNum = 0; |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 394 | unsigned int hexCode = 0; |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 395 | MBRPart hybridPart; |
| 396 | MBRData hybridMBR; |
srs5694 | fad0642 | 2010-02-19 17:19:17 -0500 | [diff] [blame] | 397 | char eeFirst = 'Y'; // Whether EFI GPT (0xEE) partition comes first in table |
| 398 | |
srs5694 | 058d4a5 | 2010-10-12 12:42:47 -0400 | [diff] [blame] | 399 | cout << "\nWARNING! Hybrid MBRs are flaky and dangerous! If you decide not to use one,\n" |
| 400 | << "just hit the Enter key at the below prompt and your MBR partition table will\n" |
| 401 | << "be untouched.\n\n\a"; |
srs5694 | fad0642 | 2010-02-19 17:19:17 -0500 | [diff] [blame] | 402 | |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 403 | hybridMBR.SetDisk(&myDisk); |
srs5694 | fad0642 | 2010-02-19 17:19:17 -0500 | [diff] [blame] | 404 | // Now get the numbers of up to three partitions to add to the |
| 405 | // hybrid MBR.... |
| 406 | cout << "Type from one to three GPT partition numbers, separated by spaces, to be\n" |
| 407 | << "added to the hybrid MBR, in sequence: "; |
srs5694 | 5a60853 | 2011-03-17 13:53:01 -0400 | [diff] [blame] | 408 | line = ReadString(); |
| 409 | numPartsToCvt = sscanf(line.c_str(), "%d %d %d", &partNums[0], &partNums[1], &partNums[2]); |
srs5694 | fad0642 | 2010-02-19 17:19:17 -0500 | [diff] [blame] | 410 | |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 411 | if (numPartsToCvt > 0) { |
srs5694 | fad0642 | 2010-02-19 17:19:17 -0500 | [diff] [blame] | 412 | cout << "Place EFI GPT (0xEE) partition first in MBR (good for GRUB)? "; |
| 413 | eeFirst = GetYN(); |
| 414 | } // if |
| 415 | |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 416 | for (i = 0; i < numPartsToCvt; i++) { |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 417 | j = partNums[i] - 1; |
srs5694 | 058d4a5 | 2010-10-12 12:42:47 -0400 | [diff] [blame] | 418 | if (partitions[j].IsUsed()) { |
| 419 | mbrNum = i + (eeFirst == 'Y'); |
| 420 | cout << "\nCreating entry for GPT partition #" << j + 1 |
| 421 | << " (MBR partition #" << mbrNum + 1 << ")\n"; |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 422 | hybridPart.SetType(GetMBRTypeCode(partitions[j].GetHexType() / 256)); |
| 423 | hybridPart.SetLocation(partitions[j].GetFirstLBA(), partitions[j].GetLengthLBA()); |
| 424 | hybridPart.SetInclusion(PRIMARY); |
srs5694 | 058d4a5 | 2010-10-12 12:42:47 -0400 | [diff] [blame] | 425 | cout << "Set the bootable flag? "; |
| 426 | if (GetYN() == 'Y') |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 427 | hybridPart.SetStatus(1); |
srs5694 | 058d4a5 | 2010-10-12 12:42:47 -0400 | [diff] [blame] | 428 | else |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 429 | hybridPart.SetStatus(0); |
| 430 | hybridPart.SetInclusion(PRIMARY); |
srs5694 | 058d4a5 | 2010-10-12 12:42:47 -0400 | [diff] [blame] | 431 | } else { |
srs5694 | 058d4a5 | 2010-10-12 12:42:47 -0400 | [diff] [blame] | 432 | cerr << "\nGPT partition #" << j + 1 << " does not exist; skipping.\n"; |
| 433 | } // if/else |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 434 | hybridMBR.AddPart(mbrNum, hybridPart); |
srs5694 | fad0642 | 2010-02-19 17:19:17 -0500 | [diff] [blame] | 435 | } // for |
| 436 | |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 437 | if (numPartsToCvt > 0) { // User opted to create a hybrid MBR.... |
srs5694 | fad0642 | 2010-02-19 17:19:17 -0500 | [diff] [blame] | 438 | // Create EFI protective partition that covers the start of the disk. |
| 439 | // If this location (covering the main GPT data structures) is omitted, |
| 440 | // Linux won't find any partitions on the disk. |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 441 | hybridPart.SetLocation(1, hybridMBR.FindLastInFree(1)); |
| 442 | hybridPart.SetStatus(0); |
| 443 | hybridPart.SetType(0xEE); |
| 444 | hybridPart.SetInclusion(PRIMARY); |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 445 | // newNote firstLBA and lastLBA are computed later... |
| 446 | if (eeFirst == 'Y') { |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 447 | hybridMBR.AddPart(0, hybridPart); |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 448 | } else { |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 449 | hybridMBR.AddPart(3, hybridPart); |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 450 | } // else |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 451 | hybridMBR.SetHybrid(); |
srs5694 | fad0642 | 2010-02-19 17:19:17 -0500 | [diff] [blame] | 452 | |
| 453 | // ... and for good measure, if there are any partition spaces left, |
| 454 | // optionally create another protective EFI partition to cover as much |
| 455 | // space as possible.... |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 456 | if (hybridMBR.CountParts() < 4) { // unused entry.... |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 457 | cout << "\nUnused partition space(s) found. Use one to protect more partitions? "; |
| 458 | if (GetYN() == 'Y') { |
srs5694 | 5a60853 | 2011-03-17 13:53:01 -0400 | [diff] [blame] | 459 | cout << "Note: Default is 0xEE, but this may confuse Mac OS X.\n"; |
| 460 | // Comment on above: Mac OS treats disks with more than one |
| 461 | // 0xEE MBR partition as MBR disks, not as GPT disks. |
| 462 | hexCode = GetMBRTypeCode(0xEE); |
| 463 | hybridMBR.MakeBiggestPart(3, hexCode); |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 464 | } // if (GetYN() == 'Y') |
| 465 | } // if unused entry |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 466 | protectiveMBR = hybridMBR; |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 467 | } // if (numPartsToCvt > 0) |
srs5694 | fad0642 | 2010-02-19 17:19:17 -0500 | [diff] [blame] | 468 | } // GPTDataTextUI::MakeHybrid() |
| 469 | |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 470 | // Convert the GPT to MBR form, storing partitions in the protectiveMBR |
| 471 | // variable. This function is necessarily limited; it may not be able to |
| 472 | // convert all partitions, depending on the disk size and available space |
| 473 | // before each partition (one free sector is required to create a logical |
| 474 | // partition, which are necessary to convert more than four partitions). |
| 475 | // Returns the number of converted partitions; if this value |
srs5694 | fad0642 | 2010-02-19 17:19:17 -0500 | [diff] [blame] | 476 | // is over 0, the calling function should call DestroyGPT() to destroy |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 477 | // the GPT data, call SaveMBR() to save the MBR, and then exit. |
srs5694 | fad0642 | 2010-02-19 17:19:17 -0500 | [diff] [blame] | 478 | int GPTDataTextUI::XFormToMBR(void) { |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 479 | uint32_t i; |
srs5694 | fad0642 | 2010-02-19 17:19:17 -0500 | [diff] [blame] | 480 | |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 481 | protectiveMBR.EmptyMBR(); |
| 482 | for (i = 0; i < numParts; i++) { |
| 483 | if (partitions[i].IsUsed()) { |
| 484 | protectiveMBR.MakePart(i, partitions[i].GetFirstLBA(), |
| 485 | partitions[i].GetLengthLBA(), |
| 486 | partitions[i].GetHexType() / 0x0100, 0); |
| 487 | } // if |
| 488 | } // for |
| 489 | protectiveMBR.MakeItLegal(); |
| 490 | return protectiveMBR.DoMenu(); |
srs5694 | fad0642 | 2010-02-19 17:19:17 -0500 | [diff] [blame] | 491 | } // GPTDataTextUI::XFormToMBR() |
| 492 | |
srs5694 | 699941e | 2011-03-21 21:33:57 -0400 | [diff] [blame^] | 493 | /******************************** |
| 494 | * * |
| 495 | * Non-class support functions. * |
| 496 | * * |
| 497 | ********************************/ |
| 498 | |
| 499 | // GetMBRTypeCode() doesn't really belong in the class, since it's MBR- |
| 500 | // specific, but it's also user I/O-related, so I want to keep it in |
| 501 | // this file.... |
srs5694 | fad0642 | 2010-02-19 17:19:17 -0500 | [diff] [blame] | 502 | |
| 503 | // Get an MBR type code from the user and return it |
| 504 | int GetMBRTypeCode(int defType) { |
srs5694 | 5a60853 | 2011-03-17 13:53:01 -0400 | [diff] [blame] | 505 | string line; |
srs5694 | fad0642 | 2010-02-19 17:19:17 -0500 | [diff] [blame] | 506 | int typeCode; |
| 507 | |
| 508 | cout.setf(ios::uppercase); |
| 509 | cout.fill('0'); |
| 510 | do { |
| 511 | cout << "Enter an MBR hex code (default " << hex; |
| 512 | cout.width(2); |
| 513 | cout << defType << "): " << dec; |
srs5694 | 5a60853 | 2011-03-17 13:53:01 -0400 | [diff] [blame] | 514 | line = ReadString(); |
| 515 | if (line[0] == '\0') |
srs5694 | fad0642 | 2010-02-19 17:19:17 -0500 | [diff] [blame] | 516 | typeCode = defType; |
| 517 | else |
srs5694 | 5a60853 | 2011-03-17 13:53:01 -0400 | [diff] [blame] | 518 | typeCode = StrToHex(line, 0); |
srs5694 | fad0642 | 2010-02-19 17:19:17 -0500 | [diff] [blame] | 519 | } while ((typeCode <= 0) || (typeCode > 255)); |
| 520 | cout.fill(' '); |
| 521 | return typeCode; |
srs5694 | 1c6f8b0 | 2010-02-21 11:09:20 -0500 | [diff] [blame] | 522 | } // GetMBRTypeCode |
srs5694 | 699941e | 2011-03-21 21:33:57 -0400 | [diff] [blame^] | 523 | |
| 524 | // Note: ReadUString() is here rather than in support.cc so that the ICU |
| 525 | // libraries need not be linked to fixparts. |
| 526 | |
| 527 | // Reads a Unicode string from stdin, returning it as an ICU-style string. |
| 528 | // Note that the returned string will NOT include the carriage return |
| 529 | // entered by the user. Relies on the ICU constructor from a string |
| 530 | // encoded in the current codepage to work. |
| 531 | UnicodeString ReadUString(void) { |
| 532 | return ReadString().c_str(); |
| 533 | } // ReadUString() |
| 534 | |