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