| 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 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 32 | #include "partnotes.h" |
| 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) { |
| 183 | uint64_t firstBlock, firstInLargest, lastBlock, sector; |
| 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 = " |
| 210 | << firstInLargest << ") or {+-}size{KMGT}: "; |
| 211 | do { |
| 212 | sector = GetSectorNum(firstBlock, lastBlock, firstInLargest, prompt2.str()); |
| 213 | } while (IsFree(sector) == 0); |
| 214 | Align(§or); // Align sector to correct multiple |
| 215 | firstBlock = sector; |
| 216 | |
| 217 | // Get last block for new partitions... |
| 218 | lastBlock = FindLastInFree(firstBlock); |
| 219 | prompt3 << "Last sector (" << firstBlock << "-" << lastBlock << ", default = " |
| 220 | << lastBlock << ") or {+-}size{KMGT}: "; |
| 221 | do { |
| 222 | sector = GetSectorNum(firstBlock, lastBlock, lastBlock, prompt3.str()); |
| 223 | } while (IsFree(sector) == 0); |
| 224 | lastBlock = sector; |
| 225 | |
| 226 | firstFreePart = GPTData::CreatePartition(partNum, firstBlock, lastBlock); |
| 227 | partitions[partNum].ChangeType(); |
| 228 | partitions[partNum].SetDefaultDescription(); |
| srs5694 | fad0642 | 2010-02-19 17:19:17 -0500 | [diff] [blame] | 229 | } else { |
| 230 | cout << "No free sectors available\n"; |
| 231 | } // if/else |
| 232 | } // GPTDataTextUI::CreatePartition() |
| 233 | |
| 234 | // Interactively delete a partition (duh!) |
| 235 | void GPTDataTextUI::DeletePartition(void) { |
| 236 | int partNum; |
| 237 | uint32_t low, high; |
| 238 | ostringstream prompt; |
| 239 | |
| 240 | if (GetPartRange(&low, &high) > 0) { |
| 241 | prompt << "Partition number (" << low + 1 << "-" << high + 1 << "): "; |
| 242 | partNum = GetNumber(low + 1, high + 1, low, prompt.str()); |
| 243 | GPTData::DeletePartition(partNum - 1); |
| 244 | } else { |
| 245 | cout << "No partitions\n"; |
| 246 | } // if/else |
| 247 | } // GPTDataTextUI::DeletePartition() |
| 248 | |
| 249 | // Prompt user for a partition number, then change its type code |
| 250 | // using ChangeGPTType(struct GPTPartition*) function. |
| 251 | void GPTDataTextUI::ChangePartType(void) { |
| 252 | int partNum; |
| 253 | uint32_t low, high; |
| srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 254 | |
| srs5694 | fad0642 | 2010-02-19 17:19:17 -0500 | [diff] [blame] | 255 | if (GetPartRange(&low, &high) > 0) { |
| 256 | partNum = GetPartNum(); |
| 257 | partitions[partNum].ChangeType(); |
| 258 | } else { |
| 259 | cout << "No partitions\n"; |
| 260 | } // if/else |
| 261 | } // GPTDataTextUI::ChangePartType() |
| 262 | |
| 263 | // Partition attributes seem to be rarely used, but I want a way to |
| 264 | // adjust them for completeness.... |
| 265 | void GPTDataTextUI::SetAttributes(uint32_t partNum) { |
| 266 | Attributes theAttr; |
| srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 267 | |
| srs5694 | fad0642 | 2010-02-19 17:19:17 -0500 | [diff] [blame] | 268 | theAttr.SetAttributes(partitions[partNum].GetAttributes()); |
| 269 | theAttr.DisplayAttributes(); |
| 270 | theAttr.ChangeAttributes(); |
| 271 | partitions[partNum].SetAttributes(theAttr.GetAttributes()); |
| 272 | } // GPTDataTextUI::SetAttributes() |
| 273 | |
| 274 | // Ask user for two partition numbers and swap them in the table. Note that |
| 275 | // this just reorders table entries; it doesn't adjust partition layout on |
| 276 | // the disk. |
| 277 | // Returns 1 if successful, 0 if not. (If user enters identical numbers, it |
| 278 | // counts as successful.) |
| 279 | int GPTDataTextUI::SwapPartitions(void) { |
| 280 | int partNum1, partNum2, didIt = 0; |
| 281 | uint32_t low, high; |
| 282 | ostringstream prompt; |
| 283 | GPTPart temp; |
| 284 | |
| 285 | if (GetPartRange(&low, &high) > 0) { |
| 286 | partNum1 = GetPartNum(); |
| srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 287 | if (high >= numParts - 1) |
| srs5694 | fad0642 | 2010-02-19 17:19:17 -0500 | [diff] [blame] | 288 | high = 0; |
| srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 289 | prompt << "New partition number (1-" << numParts |
| srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 290 | << ", default " << high + 2 << "): "; |
| srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 291 | partNum2 = GetNumber(1, numParts, high + 2, prompt.str()) - 1; |
| srs5694 | fad0642 | 2010-02-19 17:19:17 -0500 | [diff] [blame] | 292 | didIt = GPTData::SwapPartitions(partNum1, partNum2); |
| 293 | } else { |
| 294 | cout << "No partitions\n"; |
| 295 | } // if/else |
| 296 | return didIt; |
| 297 | } // GPTDataTextUI::SwapPartitionNumbers() |
| 298 | |
| 299 | // This function destroys the on-disk GPT structures. Returns 1 if the user |
| 300 | // confirms destruction, 0 if the user aborts or if there's a disk error. |
| 301 | int GPTDataTextUI::DestroyGPTwPrompt(void) { |
| 302 | int allOK = 1; |
| 303 | |
| 304 | if ((apmFound) || (bsdFound)) { |
| 305 | cout << "WARNING: APM or BSD disklabel structures detected! This operation could\n" |
| 306 | << "damage any APM or BSD partitions on this disk!\n"; |
| 307 | } // if APM or BSD |
| 308 | cout << "\a\aAbout to wipe out GPT on " << device << ". Proceed? "; |
| 309 | if (GetYN() == 'Y') { |
| 310 | if (DestroyGPT()) { |
| 311 | // Note on below: Touch the MBR only if the user wants it completely |
| 312 | // blanked out. Version 0.4.2 deleted the 0xEE partition and re-wrote |
| 313 | // the MBR, but this could wipe out a valid MBR that the program |
| 314 | // had subsequently discarded (say, if it conflicted with older GPT |
| 315 | // structures). |
| 316 | cout << "Blank out MBR? "; |
| 317 | if (GetYN() == 'Y') { |
| 318 | DestroyMBR(); |
| 319 | } else { |
| 320 | cout << "MBR is unchanged. You may need to delete an EFI GPT (0xEE) partition\n" |
| 321 | << "with fdisk or another tool.\n"; |
| 322 | } // if/else |
| 323 | } else allOK = 0; // if GPT structures destroyed |
| 324 | } else allOK = 0; // if user confirms destruction |
| 325 | return (allOK); |
| 326 | } // GPTDataTextUI::DestroyGPTwPrompt() |
| 327 | |
| 328 | // Get partition number from user and then call ShowPartDetails(partNum) |
| 329 | // to show its detailed information |
| 330 | void GPTDataTextUI::ShowDetails(void) { |
| 331 | int partNum; |
| 332 | uint32_t low, high; |
| srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 333 | |
| srs5694 | fad0642 | 2010-02-19 17:19:17 -0500 | [diff] [blame] | 334 | if (GetPartRange(&low, &high) > 0) { |
| 335 | partNum = GetPartNum(); |
| 336 | ShowPartDetails(partNum); |
| 337 | } else { |
| 338 | cout << "No partitions\n"; |
| 339 | } // if/else |
| 340 | } // GPTDataTextUI::ShowDetails() |
| 341 | |
| 342 | // Create a hybrid MBR -- an ugly, funky thing that helps GPT work with |
| 343 | // OSes that don't understand GPT. |
| 344 | void GPTDataTextUI::MakeHybrid(void) { |
| 345 | uint32_t partNums[3]; |
| 346 | char line[255]; |
| 347 | char* junk; |
| srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 348 | int numPartsToCvt, i, j, mbrNum, bootable = 0; |
| srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 349 | unsigned int hexCode = 0; |
| 350 | struct PartInfo *newNote; |
| 351 | PartNotes notes; |
| srs5694 | fad0642 | 2010-02-19 17:19:17 -0500 | [diff] [blame] | 352 | char eeFirst = 'Y'; // Whether EFI GPT (0xEE) partition comes first in table |
| 353 | |
| 354 | cout << "\nWARNING! Hybrid MBRs are flaky and potentially dangerous! If you decide not\n" |
| 355 | << "to use one, just hit the Enter key at the below prompt and your MBR\n" |
| 356 | << "partition table will be untouched.\n\n\a"; |
| 357 | |
| 358 | // Now get the numbers of up to three partitions to add to the |
| 359 | // hybrid MBR.... |
| 360 | cout << "Type from one to three GPT partition numbers, separated by spaces, to be\n" |
| 361 | << "added to the hybrid MBR, in sequence: "; |
| 362 | junk = fgets(line, 255, stdin); |
| srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 363 | numPartsToCvt = sscanf(line, "%d %d %d", &partNums[0], &partNums[1], &partNums[2]); |
| srs5694 | fad0642 | 2010-02-19 17:19:17 -0500 | [diff] [blame] | 364 | |
| srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 365 | if (numPartsToCvt > 0) { |
| srs5694 | fad0642 | 2010-02-19 17:19:17 -0500 | [diff] [blame] | 366 | cout << "Place EFI GPT (0xEE) partition first in MBR (good for GRUB)? "; |
| 367 | eeFirst = GetYN(); |
| 368 | } // if |
| 369 | |
| srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 370 | for (i = 0; i < numPartsToCvt; i++) { |
| srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 371 | newNote = new struct PartInfo; |
| 372 | j = newNote->gptPartNum = partNums[i] - 1; |
| srs5694 | fad0642 | 2010-02-19 17:19:17 -0500 | [diff] [blame] | 373 | mbrNum = i + (eeFirst == 'Y'); |
| 374 | cout << "\nCreating entry for GPT partition #" << j + 1 |
| 375 | << " (MBR partition #" << mbrNum + 1 << ")\n"; |
| srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 376 | newNote->hexCode = GetMBRTypeCode(partitions[j].GetHexType() / 256); |
| 377 | newNote->firstLBA = partitions[j].GetFirstLBA(); |
| 378 | newNote->lastLBA = partitions[j].GetLastLBA(); |
| 379 | newNote->type = PRIMARY; |
| srs5694 | fad0642 | 2010-02-19 17:19:17 -0500 | [diff] [blame] | 380 | cout << "Set the bootable flag? "; |
| 381 | if (GetYN() == 'Y') |
| srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 382 | newNote->active = 1; |
| 383 | else |
| 384 | newNote->active = 0; |
| 385 | notes.AddToEnd(newNote); |
| srs5694 | fad0642 | 2010-02-19 17:19:17 -0500 | [diff] [blame] | 386 | } // for |
| 387 | |
| srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 388 | if (numPartsToCvt > 0) { // User opted to create a hybrid MBR.... |
| srs5694 | fad0642 | 2010-02-19 17:19:17 -0500 | [diff] [blame] | 389 | // Create EFI protective partition that covers the start of the disk. |
| 390 | // If this location (covering the main GPT data structures) is omitted, |
| 391 | // Linux won't find any partitions on the disk. |
| srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 392 | newNote = new struct PartInfo; |
| 393 | newNote->gptPartNum = MBR_EFI_GPT; |
| srs5694 | 61768bc | 2010-07-04 01:54:00 -0400 | [diff] [blame^] | 394 | newNote->firstLBA = 1; |
| srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 395 | newNote->active = 0; |
| 396 | newNote->hexCode = 0xEE; |
| 397 | newNote->type = PRIMARY; |
| 398 | // newNote firstLBA and lastLBA are computed later... |
| 399 | if (eeFirst == 'Y') { |
| 400 | notes.AddToStart(newNote); |
| 401 | } else { |
| 402 | notes.AddToEnd(newNote); |
| 403 | } // else |
| srs5694 | fad0642 | 2010-02-19 17:19:17 -0500 | [diff] [blame] | 404 | protectiveMBR.SetHybrid(); |
| 405 | |
| 406 | // ... and for good measure, if there are any partition spaces left, |
| 407 | // optionally create another protective EFI partition to cover as much |
| 408 | // space as possible.... |
| srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 409 | if (notes.GetNumPrimary() < 4) { // unused entry.... |
| 410 | cout << "\nUnused partition space(s) found. Use one to protect more partitions? "; |
| 411 | if (GetYN() == 'Y') { |
| 412 | while ((hexCode <= 0) || (hexCode > 255)) { |
| 413 | cout << "Enter an MBR hex code (EE is EFI GPT, but may confuse MacOS): "; |
| 414 | // Comment on above: Mac OS treats disks with more than one |
| 415 | // 0xEE MBR partition as MBR disks, not as GPT disks. |
| 416 | junk = fgets(line, 255, stdin); |
| 417 | sscanf(line, "%x", &hexCode); |
| 418 | if (line[0] == '\n') |
| 419 | hexCode = 0x00; |
| 420 | } // while |
| 421 | newNote = new struct PartInfo; |
| 422 | newNote->gptPartNum = MBR_EFI_GPT; |
| 423 | newNote->active = 0; |
| 424 | newNote->hexCode = hexCode; |
| 425 | newNote->type = PRIMARY; |
| 426 | // newNote firstLBA and lastLBA are computed later... |
| 427 | notes.AddToEnd(newNote); |
| 428 | } // if (GetYN() == 'Y') |
| 429 | } // if unused entry |
| 430 | PartsToMBR(notes); |
| srs5694 | fad0642 | 2010-02-19 17:19:17 -0500 | [diff] [blame] | 431 | if (bootable > 0) |
| 432 | protectiveMBR.SetPartBootable(bootable); |
| srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 433 | } // if (numPartsToCvt > 0) |
| srs5694 | fad0642 | 2010-02-19 17:19:17 -0500 | [diff] [blame] | 434 | } // GPTDataTextUI::MakeHybrid() |
| 435 | |
| srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 436 | // Assign GPT partitions to primary or logical status for conversion. The |
| 437 | // function first presents a suggested layout with as many logicals as |
| 438 | // possible, but gives the user the option to override this suggestion. |
| 439 | // Returns the number of partitions assigned (0 if problems or if the |
| 440 | // user aborts) |
| 441 | int GPTDataTextUI::AssignPrimaryOrLogical(PartNotes & notes) { |
| 442 | int i, partNum, allOK = 1, changesWanted = 1, countedParts, numPrimary = 0, numLogical = 0; |
| 443 | int newNumParts; // size of GPT table |
| 444 | |
| 445 | // Sort and resize the GPT table. Resized to clear the sector before the |
| 446 | // first available sector, if possible, so as to enable turning the |
| 447 | // first partition into a logical, should that be desirable/necessary. |
| 448 | SortGPT(); |
| 449 | countedParts = newNumParts = CountParts(); |
| 450 | i = blockSize / GPT_SIZE; |
| 451 | if ((newNumParts % i) != 0) { |
| 452 | newNumParts = ((newNumParts / i) + 1) * i; |
| 453 | } // if |
| 454 | SetGPTSize(newNumParts); |
| 455 | |
| 456 | // Takes notes on existing partitions: Create an initial assignment as |
| 457 | // primary or logical, set default MBR types, and then make it legal |
| 458 | // (drop partitions as required to fit in the MBR and as logicals). |
| srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 459 | allOK = (notes.PassPartitions(partitions, this, numParts, blockSize) == (int) numParts); |
| srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 460 | for (i = 0; i < countedParts; i++) |
| 461 | notes.SetMbrHexType(i, partitions[i].GetHexType() / 255); |
| 462 | notes.MakeItLegal(); |
| 463 | |
| 464 | while (allOK && changesWanted) { |
| 465 | notes.ShowSummary(); |
| 466 | cout << "\n"; |
| 467 | partNum = GetNumber(-1, countedParts, -2, |
| 468 | "Type partition to change, 0 to accept, -1 to abort: "); |
| 469 | switch (partNum) { |
| 470 | case -1: |
| 471 | allOK = 0; |
| 472 | break; |
| 473 | case 0: |
| 474 | changesWanted = 0; |
| 475 | break; |
| 476 | default: |
| 477 | allOK = notes.MakeChange(partNum - 1); |
| 478 | break; |
| 479 | } // switch |
| 480 | } // while |
| 481 | |
| 482 | i = 0; |
| 483 | if (allOK) |
| 484 | for (i = 0; i < countedParts; i++) { |
| 485 | switch (notes.GetType(i)) { |
| 486 | case PRIMARY: |
| 487 | numPrimary++; |
| 488 | break; |
| 489 | case LOGICAL: |
| 490 | numLogical++; |
| 491 | break; |
| 492 | } // switch |
| 493 | if (notes.GetActiveStatus(i)) |
| 494 | protectiveMBR.SetPartBootable(i); |
| 495 | } // for |
| 496 | |
| 497 | if (numPrimary > 4) { |
| 498 | cerr << "Warning! More than four primary partitions in " |
| 499 | << "GPTDataTextUI::AssignPrimaryOrLogical()!\n"; |
| 500 | allOK = 0; |
| 501 | } // if |
| 502 | return (allOK * (numPrimary + numLogical)); |
| 503 | } // GPTDataTextUI::AssignPrimaryOrLogical() |
| 504 | |
| 505 | // Convert the GPT to MBR form, storing partitions in the protectiveMBR |
| 506 | // variable. This function is necessarily limited; it may not be able to |
| 507 | // convert all partitions, depending on the disk size and available space |
| 508 | // before each partition (one free sector is required to create a logical |
| 509 | // partition, which are necessary to convert more than four partitions). |
| 510 | // Returns the number of converted partitions; if this value |
| srs5694 | fad0642 | 2010-02-19 17:19:17 -0500 | [diff] [blame] | 511 | // is over 0, the calling function should call DestroyGPT() to destroy |
| srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 512 | // the GPT data, call SaveMBR() to save the MBR, and then exit. |
| srs5694 | fad0642 | 2010-02-19 17:19:17 -0500 | [diff] [blame] | 513 | int GPTDataTextUI::XFormToMBR(void) { |
| srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 514 | int numToConvert, numReallyConverted = 0; |
| srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 515 | int origNumParts; |
| 516 | PartNotes notes; |
| 517 | GPTPart *tempGptParts; |
| 518 | uint32_t i; |
| srs5694 | fad0642 | 2010-02-19 17:19:17 -0500 | [diff] [blame] | 519 | |
| srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 520 | // Back up partition array, since we'll be sorting it and we want to |
| 521 | // be able to restore it in case the user aborts.... |
| srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 522 | origNumParts = numParts; |
| 523 | tempGptParts = new GPTPart[numParts]; |
| 524 | for (i = 0; i < numParts; i++) |
| srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 525 | tempGptParts[i] = partitions[i]; |
| 526 | |
| srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 527 | numToConvert = AssignPrimaryOrLogical(notes); |
| srs5694 | fad0642 | 2010-02-19 17:19:17 -0500 | [diff] [blame] | 528 | |
| srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 529 | if (numToConvert > 0) { |
| 530 | numReallyConverted = PartsToMBR(notes); |
| 531 | if (numReallyConverted != numToConvert) { |
| 532 | cerr << "Error converting partitions to MBR; tried to convert " |
| 533 | << numToConvert << " partitions,\nbut converted " << numReallyConverted |
| 534 | << ". Aborting operation!\n"; |
| 535 | numReallyConverted = 0; |
| 536 | protectiveMBR.MakeProtectiveMBR(); |
| 537 | } // if/else |
| 538 | } // if |
| srs5694 | fad0642 | 2010-02-19 17:19:17 -0500 | [diff] [blame] | 539 | |
| srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 540 | // A problem or the user aborted; restore backup of unsorted and |
| 541 | // original-sized partition table and delete the sorted and |
| 542 | // resized one; otherwise free the backup table's memory.... |
| 543 | if (numReallyConverted == 0) { |
| 544 | delete[] partitions; |
| 545 | partitions = tempGptParts; |
| 546 | SetGPTSize(origNumParts); |
| srs5694 | fad0642 | 2010-02-19 17:19:17 -0500 | [diff] [blame] | 547 | } else { |
| srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 548 | delete[] tempGptParts; |
| 549 | } // if |
| 550 | return numReallyConverted; |
| srs5694 | fad0642 | 2010-02-19 17:19:17 -0500 | [diff] [blame] | 551 | } // GPTDataTextUI::XFormToMBR() |
| 552 | |
| 553 | /********************************************************************* |
| 554 | * * |
| 555 | * The following doesn't really belong in the class, since it's MBR- * |
| 556 | * specific, but it's also user I/O-related, so I want to keep it in * |
| 557 | * this file.... * |
| 558 | * * |
| 559 | *********************************************************************/ |
| 560 | |
| 561 | // Get an MBR type code from the user and return it |
| 562 | int GetMBRTypeCode(int defType) { |
| 563 | char line[255]; |
| 564 | char* junk; |
| 565 | int typeCode; |
| 566 | |
| 567 | cout.setf(ios::uppercase); |
| 568 | cout.fill('0'); |
| 569 | do { |
| 570 | cout << "Enter an MBR hex code (default " << hex; |
| 571 | cout.width(2); |
| 572 | cout << defType << "): " << dec; |
| 573 | junk = fgets(line, 255, stdin); |
| 574 | if (line[0] == '\n') |
| 575 | typeCode = defType; |
| 576 | else |
| 577 | sscanf(line, "%x", &typeCode); |
| 578 | } while ((typeCode <= 0) || (typeCode > 255)); |
| 579 | cout.fill(' '); |
| 580 | return typeCode; |
| srs5694 | 1c6f8b0 | 2010-02-21 11:09:20 -0500 | [diff] [blame] | 581 | } // GetMBRTypeCode |