1cf200d32Sopenharmony_ci/* 2cf200d32Sopenharmony_ci Copyright (C) 2010-2024 <Roderick W. Smith> 3cf200d32Sopenharmony_ci 4cf200d32Sopenharmony_ci This program is free software; you can redistribute it and/or modify 5cf200d32Sopenharmony_ci it under the terms of the GNU General Public License as published by 6cf200d32Sopenharmony_ci the Free Software Foundation; either version 2 of the License, or 7cf200d32Sopenharmony_ci (at your option) any later version. 8cf200d32Sopenharmony_ci 9cf200d32Sopenharmony_ci This program is distributed in the hope that it will be useful, 10cf200d32Sopenharmony_ci but WITHOUT ANY WARRANTY; without even the implied warranty of 11cf200d32Sopenharmony_ci MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12cf200d32Sopenharmony_ci GNU General Public License for more details. 13cf200d32Sopenharmony_ci 14cf200d32Sopenharmony_ci You should have received a copy of the GNU General Public License along 15cf200d32Sopenharmony_ci with this program; if not, write to the Free Software Foundation, Inc., 16cf200d32Sopenharmony_ci 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 17cf200d32Sopenharmony_ci 18cf200d32Sopenharmony_ci*/ 19cf200d32Sopenharmony_ci 20cf200d32Sopenharmony_ci/* This class implements an interactive text-mode interface atop the 21cf200d32Sopenharmony_ci GPTData class */ 22cf200d32Sopenharmony_ci 23cf200d32Sopenharmony_ci#include <string.h> 24cf200d32Sopenharmony_ci#include <errno.h> 25cf200d32Sopenharmony_ci#include <stdint.h> 26cf200d32Sopenharmony_ci#include <limits.h> 27cf200d32Sopenharmony_ci#include <iostream> 28cf200d32Sopenharmony_ci#include <fstream> 29cf200d32Sopenharmony_ci#include <sstream> 30cf200d32Sopenharmony_ci#include <cstdio> 31cf200d32Sopenharmony_ci#include "attributes.h" 32cf200d32Sopenharmony_ci#include "gpttext.h" 33cf200d32Sopenharmony_ci#include "support.h" 34cf200d32Sopenharmony_ci 35cf200d32Sopenharmony_ciusing namespace std; 36cf200d32Sopenharmony_ci 37cf200d32Sopenharmony_ci/******************************************** 38cf200d32Sopenharmony_ci * * 39cf200d32Sopenharmony_ci * GPTDataText class and related structures * 40cf200d32Sopenharmony_ci * * 41cf200d32Sopenharmony_ci ********************************************/ 42cf200d32Sopenharmony_ci 43cf200d32Sopenharmony_ciGPTDataTextUI::GPTDataTextUI(void) : GPTData() { 44cf200d32Sopenharmony_ci} // default constructor 45cf200d32Sopenharmony_ci 46cf200d32Sopenharmony_ciGPTDataTextUI::GPTDataTextUI(string filename) : GPTData(filename) { 47cf200d32Sopenharmony_ci} // constructor passing filename 48cf200d32Sopenharmony_ci 49cf200d32Sopenharmony_ciGPTDataTextUI::~GPTDataTextUI(void) { 50cf200d32Sopenharmony_ci} // default destructor 51cf200d32Sopenharmony_ci 52cf200d32Sopenharmony_ci/********************************************************************* 53cf200d32Sopenharmony_ci * * 54cf200d32Sopenharmony_ci * The following functions are extended (interactive) versions of * 55cf200d32Sopenharmony_ci * simpler functions in the base class.... * 56cf200d32Sopenharmony_ci * * 57cf200d32Sopenharmony_ci *********************************************************************/ 58cf200d32Sopenharmony_ci 59cf200d32Sopenharmony_ci// Overridden function; calls base-class function and then makes 60cf200d32Sopenharmony_ci// additional queries of the user, if the base-class function can't 61cf200d32Sopenharmony_ci// decide what to do. 62cf200d32Sopenharmony_ciWhichToUse GPTDataTextUI::UseWhichPartitions(void) { 63cf200d32Sopenharmony_ci WhichToUse which; 64cf200d32Sopenharmony_ci MBRValidity mbrState; 65cf200d32Sopenharmony_ci int answer; 66cf200d32Sopenharmony_ci 67cf200d32Sopenharmony_ci which = GPTData::UseWhichPartitions(); 68cf200d32Sopenharmony_ci if ((which != use_abort) || beQuiet) 69cf200d32Sopenharmony_ci return which; 70cf200d32Sopenharmony_ci 71cf200d32Sopenharmony_ci // If we get past here, it means that the non-interactive tests were 72cf200d32Sopenharmony_ci // inconclusive, so we must ask the user which table to use.... 73cf200d32Sopenharmony_ci mbrState = protectiveMBR.GetValidity(); 74cf200d32Sopenharmony_ci 75cf200d32Sopenharmony_ci if ((state == gpt_valid) && (mbrState == mbr)) { 76cf200d32Sopenharmony_ci cout << "Found valid MBR and GPT. Which do you want to use?\n"; 77cf200d32Sopenharmony_ci answer = GetNumber(1, 3, 2, " 1 - MBR\n 2 - GPT\n 3 - Create blank GPT\n\nYour answer: "); 78cf200d32Sopenharmony_ci if (answer == 1) { 79cf200d32Sopenharmony_ci which = use_mbr; 80cf200d32Sopenharmony_ci } else if (answer == 2) { 81cf200d32Sopenharmony_ci which = use_gpt; 82cf200d32Sopenharmony_ci cout << "Using GPT and creating fresh protective MBR.\n"; 83cf200d32Sopenharmony_ci } else which = use_new; 84cf200d32Sopenharmony_ci } // if 85cf200d32Sopenharmony_ci 86cf200d32Sopenharmony_ci // Nasty decisions here -- GPT is present, but corrupt (bad CRCs or other 87cf200d32Sopenharmony_ci // problems) 88cf200d32Sopenharmony_ci if (state == gpt_corrupt) { 89cf200d32Sopenharmony_ci if ((mbrState == mbr) || (mbrState == hybrid)) { 90cf200d32Sopenharmony_ci cout << "Found valid MBR and corrupt GPT. Which do you want to use? (Using the\n" 91cf200d32Sopenharmony_ci << "GPT MAY permit recovery of GPT data.)\n"; 92cf200d32Sopenharmony_ci answer = GetNumber(1, 3, 2, " 1 - MBR\n 2 - GPT\n 3 - Create blank GPT\n\nYour answer: "); 93cf200d32Sopenharmony_ci if (answer == 1) { 94cf200d32Sopenharmony_ci which = use_mbr; 95cf200d32Sopenharmony_ci } else if (answer == 2) { 96cf200d32Sopenharmony_ci which = use_gpt; 97cf200d32Sopenharmony_ci } else which = use_new; 98cf200d32Sopenharmony_ci } else if (mbrState == invalid) { 99cf200d32Sopenharmony_ci cout << "Found invalid MBR and corrupt GPT. What do you want to do? (Using the\n" 100cf200d32Sopenharmony_ci << "GPT MAY permit recovery of GPT data.)\n"; 101cf200d32Sopenharmony_ci answer = GetNumber(1, 2, 1, " 1 - Use current GPT\n 2 - Create blank GPT\n\nYour answer: "); 102cf200d32Sopenharmony_ci if (answer == 1) { 103cf200d32Sopenharmony_ci which = use_gpt; 104cf200d32Sopenharmony_ci } else which = use_new; 105cf200d32Sopenharmony_ci } // if/else/else 106cf200d32Sopenharmony_ci } // if (corrupt GPT) 107cf200d32Sopenharmony_ci 108cf200d32Sopenharmony_ci return which; 109cf200d32Sopenharmony_ci} // UseWhichPartitions() 110cf200d32Sopenharmony_ci 111cf200d32Sopenharmony_ci// Ask the user for a partition number; and prompt for verification 112cf200d32Sopenharmony_ci// if the requested partition isn't of a known BSD type. 113cf200d32Sopenharmony_ci// Lets the base-class function do the work, and returns its value (the 114cf200d32Sopenharmony_ci// number of converted partitions). 115cf200d32Sopenharmony_ciint GPTDataTextUI::XFormDisklabel(void) { 116cf200d32Sopenharmony_ci uint32_t partNum; 117cf200d32Sopenharmony_ci uint16_t hexCode; 118cf200d32Sopenharmony_ci int goOn = 1, numDone = 0; 119cf200d32Sopenharmony_ci BSDData disklabel; 120cf200d32Sopenharmony_ci 121cf200d32Sopenharmony_ci partNum = GetPartNum(); 122cf200d32Sopenharmony_ci 123cf200d32Sopenharmony_ci // Now see if the specified partition has a BSD type code.... 124cf200d32Sopenharmony_ci hexCode = partitions[partNum].GetHexType(); 125cf200d32Sopenharmony_ci if ((hexCode != 0xa500) && (hexCode != 0xa900)) { 126cf200d32Sopenharmony_ci cout << "Specified partition doesn't have a disklabel partition type " 127cf200d32Sopenharmony_ci << "code.\nContinue anyway? "; 128cf200d32Sopenharmony_ci goOn = (GetYN() == 'Y'); 129cf200d32Sopenharmony_ci } // if 130cf200d32Sopenharmony_ci 131cf200d32Sopenharmony_ci if (goOn) 132cf200d32Sopenharmony_ci numDone = GPTData::XFormDisklabel(partNum); 133cf200d32Sopenharmony_ci 134cf200d32Sopenharmony_ci return numDone; 135cf200d32Sopenharmony_ci} // GPTDataTextUI::XFormDisklabel(void) 136cf200d32Sopenharmony_ci 137cf200d32Sopenharmony_ci 138cf200d32Sopenharmony_ci/********************************************************************* 139cf200d32Sopenharmony_ci * * 140cf200d32Sopenharmony_ci * Begin functions that obtain information from the users, and often * 141cf200d32Sopenharmony_ci * do something with that information (call other functions) * 142cf200d32Sopenharmony_ci * * 143cf200d32Sopenharmony_ci *********************************************************************/ 144cf200d32Sopenharmony_ci 145cf200d32Sopenharmony_ci// Prompts user for partition number and returns the result. Returns "0" 146cf200d32Sopenharmony_ci// (the first partition) if none are currently defined. 147cf200d32Sopenharmony_ciuint32_t GPTDataTextUI::GetPartNum(void) { 148cf200d32Sopenharmony_ci uint32_t partNum; 149cf200d32Sopenharmony_ci uint32_t low, high; 150cf200d32Sopenharmony_ci ostringstream prompt; 151cf200d32Sopenharmony_ci 152cf200d32Sopenharmony_ci if (GetPartRange(&low, &high) > 0) { 153cf200d32Sopenharmony_ci prompt << "Partition number (" << low + 1 << "-" << high + 1 << "): "; 154cf200d32Sopenharmony_ci partNum = GetNumber(low + 1, high + 1, low, prompt.str()); 155cf200d32Sopenharmony_ci } else partNum = 1; 156cf200d32Sopenharmony_ci return (partNum - 1); 157cf200d32Sopenharmony_ci} // GPTDataTextUI::GetPartNum() 158cf200d32Sopenharmony_ci 159cf200d32Sopenharmony_ci// What it says: Resize the partition table. (Default is 128 entries.) 160cf200d32Sopenharmony_civoid GPTDataTextUI::ResizePartitionTable(void) { 161cf200d32Sopenharmony_ci int newSize; 162cf200d32Sopenharmony_ci ostringstream prompt; 163cf200d32Sopenharmony_ci uint32_t curLow, curHigh; 164cf200d32Sopenharmony_ci 165cf200d32Sopenharmony_ci cout << "Current partition table size is " << numParts << ".\n"; 166cf200d32Sopenharmony_ci GetPartRange(&curLow, &curHigh); 167cf200d32Sopenharmony_ci curHigh++; // since GetPartRange() returns numbers starting from 0... 168cf200d32Sopenharmony_ci // There's no point in having fewer than four partitions.... 169cf200d32Sopenharmony_ci if (curHigh < (blockSize / GPT_SIZE)) 170cf200d32Sopenharmony_ci curHigh = blockSize / GPT_SIZE; 171cf200d32Sopenharmony_ci prompt << "Enter new size (" << curHigh << " up, default " << NUM_GPT_ENTRIES << "): "; 172cf200d32Sopenharmony_ci newSize = GetNumber(4, 65535, 128, prompt.str()); 173cf200d32Sopenharmony_ci if (newSize < 128) { 174cf200d32Sopenharmony_ci cout << "Caution: The partition table size should officially be 16KB or larger,\n" 175cf200d32Sopenharmony_ci << "which works out to 128 entries. In practice, smaller tables seem to\n" 176cf200d32Sopenharmony_ci << "work with most OSes, but this practice is risky. I'm proceeding with\n" 177cf200d32Sopenharmony_ci << "the resize, but you may want to reconsider this action and undo it.\n\n"; 178cf200d32Sopenharmony_ci } // if 179cf200d32Sopenharmony_ci SetGPTSize(newSize); 180cf200d32Sopenharmony_ci} // GPTDataTextUI::ResizePartitionTable() 181cf200d32Sopenharmony_ci 182cf200d32Sopenharmony_ci// Move the main partition table (to enable some SoC boot loaders to place 183cf200d32Sopenharmony_ci// code at sector 2, for instance). 184cf200d32Sopenharmony_civoid GPTDataTextUI::MoveMainTable(void) { 185cf200d32Sopenharmony_ci uint64_t newStart, pteSize = GetTableSizeInSectors(); 186cf200d32Sopenharmony_ci uint64_t maxValue = FindFirstUsedLBA() - pteSize; 187cf200d32Sopenharmony_ci ostringstream prompt; 188cf200d32Sopenharmony_ci 189cf200d32Sopenharmony_ci if (maxValue == UINT64_MAX - pteSize) 190cf200d32Sopenharmony_ci maxValue = FindLastAvailable() - pteSize; 191cf200d32Sopenharmony_ci cout << "Currently, main partition table begins at sector " << mainHeader.partitionEntriesLBA 192cf200d32Sopenharmony_ci << " and ends at sector " << mainHeader.partitionEntriesLBA + pteSize - 1 << "\n"; 193cf200d32Sopenharmony_ci prompt << "Enter new starting location (2 to " << maxValue << "; default is 2; 1 to abort): "; 194cf200d32Sopenharmony_ci newStart = GetNumber(1, maxValue, 2, prompt.str()); 195cf200d32Sopenharmony_ci if (newStart != 1) { 196cf200d32Sopenharmony_ci GPTData::MoveMainTable(newStart); 197cf200d32Sopenharmony_ci } else { 198cf200d32Sopenharmony_ci cout << "Aborting change!\n"; 199cf200d32Sopenharmony_ci } // if 200cf200d32Sopenharmony_ci} // GPTDataTextUI::MoveMainTable() 201cf200d32Sopenharmony_ci 202cf200d32Sopenharmony_ci// Move the backup partition table. 203cf200d32Sopenharmony_civoid GPTDataTextUI::MoveSecondTable(void) { 204cf200d32Sopenharmony_ci uint64_t newStart, pteSize = GetTableSizeInSectors(); 205cf200d32Sopenharmony_ci uint64_t minValue = FindLastUsedLBA() + 1; 206cf200d32Sopenharmony_ci uint64_t maxValue = diskSize - 1 - pteSize; 207cf200d32Sopenharmony_ci ostringstream prompt; 208cf200d32Sopenharmony_ci 209cf200d32Sopenharmony_ci cout << "Currently, backup partition table begins at sector " << secondHeader.partitionEntriesLBA 210cf200d32Sopenharmony_ci << " and ends at\n" 211cf200d32Sopenharmony_ci << "sector " << secondHeader.partitionEntriesLBA + pteSize - 1 << "\n"; 212cf200d32Sopenharmony_ci prompt << "Enter new starting location (" << minValue << " to " << maxValue << 213cf200d32Sopenharmony_ci "; default is " << maxValue << "; 1 to abort): "; 214cf200d32Sopenharmony_ci newStart = GetNumber(minValue, maxValue, maxValue, prompt.str()); 215cf200d32Sopenharmony_ci if (newStart != secondHeader.partitionEntriesLBA) { 216cf200d32Sopenharmony_ci GPTData::MoveSecondTable(newStart); 217cf200d32Sopenharmony_ci } else { 218cf200d32Sopenharmony_ci cout << "Aborting change!\n"; 219cf200d32Sopenharmony_ci } // if 220cf200d32Sopenharmony_ci} // GPTDataTextUI::MoveSecondTable() 221cf200d32Sopenharmony_ci 222cf200d32Sopenharmony_ci// Interactively create a partition 223cf200d32Sopenharmony_civoid GPTDataTextUI::CreatePartition(void) { 224cf200d32Sopenharmony_ci uint64_t firstBlock, firstInLargest, lastBlock, sector, origSector, lastAligned; 225cf200d32Sopenharmony_ci uint32_t firstFreePart = 0; 226cf200d32Sopenharmony_ci ostringstream prompt1, prompt2, prompt3; 227cf200d32Sopenharmony_ci int partNum; 228cf200d32Sopenharmony_ci 229cf200d32Sopenharmony_ci // Find first free partition... 230cf200d32Sopenharmony_ci while (partitions[firstFreePart].GetFirstLBA() != 0) { 231cf200d32Sopenharmony_ci firstFreePart++; 232cf200d32Sopenharmony_ci } // while 233cf200d32Sopenharmony_ci 234cf200d32Sopenharmony_ci if (((firstBlock = FindFirstAvailable()) != 0) && 235cf200d32Sopenharmony_ci (firstFreePart < numParts)) { 236cf200d32Sopenharmony_ci lastBlock = FindLastAvailable(); 237cf200d32Sopenharmony_ci firstInLargest = FindFirstInLargest(); 238cf200d32Sopenharmony_ci Align(&firstInLargest); 239cf200d32Sopenharmony_ci 240cf200d32Sopenharmony_ci // Get partition number.... 241cf200d32Sopenharmony_ci prompt1 << "Partition number (" << firstFreePart + 1 << "-" << numParts 242cf200d32Sopenharmony_ci << ", default " << firstFreePart + 1 << "): "; 243cf200d32Sopenharmony_ci do { 244cf200d32Sopenharmony_ci partNum = GetNumber(firstFreePart + 1, numParts, 245cf200d32Sopenharmony_ci firstFreePart + 1, prompt1.str()) - 1; 246cf200d32Sopenharmony_ci if (partitions[partNum].GetFirstLBA() != 0) 247cf200d32Sopenharmony_ci cout << "partition " << partNum + 1 << " is in use.\n"; 248cf200d32Sopenharmony_ci } while (partitions[partNum].GetFirstLBA() != 0); 249cf200d32Sopenharmony_ci 250cf200d32Sopenharmony_ci // Get first block for new partition... 251cf200d32Sopenharmony_ci prompt2 << "First sector (" << firstBlock << "-" << lastBlock << ", default = " 252cf200d32Sopenharmony_ci << firstInLargest << ") or {+-}size{KMGTP}: "; 253cf200d32Sopenharmony_ci do { 254cf200d32Sopenharmony_ci sector = GetSectorNum(firstBlock, lastBlock, firstInLargest, prompt2.str()); 255cf200d32Sopenharmony_ci } while (IsFree(sector) == 0); 256cf200d32Sopenharmony_ci origSector = sector; 257cf200d32Sopenharmony_ci if (Align(§or)) { 258cf200d32Sopenharmony_ci cout << "Information: Moved requested sector from " << origSector << " to " 259cf200d32Sopenharmony_ci << sector << " in\norder to align on " << sectorAlignment 260cf200d32Sopenharmony_ci << "-sector boundaries.\n"; 261cf200d32Sopenharmony_ci if (!beQuiet) 262cf200d32Sopenharmony_ci cout << "Use 'l' on the experts' menu to adjust alignment\n"; 263cf200d32Sopenharmony_ci } // if 264cf200d32Sopenharmony_ci firstBlock = sector; 265cf200d32Sopenharmony_ci 266cf200d32Sopenharmony_ci // Get last block for new partitions... 267cf200d32Sopenharmony_ci lastBlock = FindLastInFree(firstBlock, false); 268cf200d32Sopenharmony_ci lastAligned = FindLastInFree(firstBlock, true); 269cf200d32Sopenharmony_ci prompt3 << "Last sector (" << firstBlock << "-" << lastBlock << ", default = " 270cf200d32Sopenharmony_ci << lastAligned << ") or {+-}size{KMGTP}: "; 271cf200d32Sopenharmony_ci do { 272cf200d32Sopenharmony_ci sector = GetSectorNum(firstBlock, lastBlock, lastAligned, prompt3.str()); 273cf200d32Sopenharmony_ci } while (IsFree(sector) == 0); 274cf200d32Sopenharmony_ci lastBlock = sector; 275cf200d32Sopenharmony_ci 276cf200d32Sopenharmony_ci GPTData::CreatePartition(partNum, firstBlock, lastBlock); 277cf200d32Sopenharmony_ci partitions[partNum].ChangeType(); 278cf200d32Sopenharmony_ci partitions[partNum].SetDefaultDescription(); 279cf200d32Sopenharmony_ci } else { 280cf200d32Sopenharmony_ci if (firstFreePart >= numParts) 281cf200d32Sopenharmony_ci cout << "No table partition entries left\n"; 282cf200d32Sopenharmony_ci else 283cf200d32Sopenharmony_ci cout << "No free sectors available\n"; 284cf200d32Sopenharmony_ci } // if/else 285cf200d32Sopenharmony_ci} // GPTDataTextUI::CreatePartition() 286cf200d32Sopenharmony_ci 287cf200d32Sopenharmony_ci// Interactively delete a partition (duh!) 288cf200d32Sopenharmony_civoid GPTDataTextUI::DeletePartition(void) { 289cf200d32Sopenharmony_ci int partNum; 290cf200d32Sopenharmony_ci uint32_t low, high; 291cf200d32Sopenharmony_ci ostringstream prompt; 292cf200d32Sopenharmony_ci 293cf200d32Sopenharmony_ci if (GetPartRange(&low, &high) > 0) { 294cf200d32Sopenharmony_ci prompt << "Partition number (" << low + 1 << "-" << high + 1 << "): "; 295cf200d32Sopenharmony_ci partNum = GetNumber(low + 1, high + 1, low, prompt.str()); 296cf200d32Sopenharmony_ci GPTData::DeletePartition(partNum - 1); 297cf200d32Sopenharmony_ci } else { 298cf200d32Sopenharmony_ci cout << "No partitions\n"; 299cf200d32Sopenharmony_ci } // if/else 300cf200d32Sopenharmony_ci} // GPTDataTextUI::DeletePartition() 301cf200d32Sopenharmony_ci 302cf200d32Sopenharmony_ci// Prompt user for a partition number, then change its type code 303cf200d32Sopenharmony_civoid GPTDataTextUI::ChangePartType(void) { 304cf200d32Sopenharmony_ci int partNum; 305cf200d32Sopenharmony_ci uint32_t low, high; 306cf200d32Sopenharmony_ci 307cf200d32Sopenharmony_ci if (GetPartRange(&low, &high) > 0) { 308cf200d32Sopenharmony_ci partNum = GetPartNum(); 309cf200d32Sopenharmony_ci partitions[partNum].ChangeType(); 310cf200d32Sopenharmony_ci } else { 311cf200d32Sopenharmony_ci cout << "No partitions\n"; 312cf200d32Sopenharmony_ci } // if/else 313cf200d32Sopenharmony_ci} // GPTDataTextUI::ChangePartType() 314cf200d32Sopenharmony_ci 315cf200d32Sopenharmony_ci// Prompt user for a partition number, then change its unique 316cf200d32Sopenharmony_ci// GUID. 317cf200d32Sopenharmony_civoid GPTDataTextUI::ChangeUniqueGuid(void) { 318cf200d32Sopenharmony_ci int partNum; 319cf200d32Sopenharmony_ci uint32_t low, high; 320cf200d32Sopenharmony_ci string guidStr; 321cf200d32Sopenharmony_ci 322cf200d32Sopenharmony_ci if (GetPartRange(&low, &high) > 0) { 323cf200d32Sopenharmony_ci partNum = GetPartNum(); 324cf200d32Sopenharmony_ci cout << "Enter the partition's new unique GUID ('R' to randomize): "; 325cf200d32Sopenharmony_ci guidStr = ReadString(); 326cf200d32Sopenharmony_ci if ((guidStr.length() >= 32) || (guidStr[0] == 'R') || (guidStr[0] == 'r')) { 327cf200d32Sopenharmony_ci SetPartitionGUID(partNum, (GUIDData) guidStr); 328cf200d32Sopenharmony_ci cout << "New GUID is " << partitions[partNum].GetUniqueGUID() << "\n"; 329cf200d32Sopenharmony_ci } else { 330cf200d32Sopenharmony_ci cout << "GUID is too short!\n"; 331cf200d32Sopenharmony_ci } // if/else 332cf200d32Sopenharmony_ci } else 333cf200d32Sopenharmony_ci cout << "No partitions\n"; 334cf200d32Sopenharmony_ci} // GPTDataTextUI::ChangeUniqueGuid() 335cf200d32Sopenharmony_ci 336cf200d32Sopenharmony_ci// Partition attributes seem to be rarely used, but I want a way to 337cf200d32Sopenharmony_ci// adjust them for completeness.... 338cf200d32Sopenharmony_civoid GPTDataTextUI::SetAttributes(uint32_t partNum) { 339cf200d32Sopenharmony_ci partitions[partNum].SetAttributes(); 340cf200d32Sopenharmony_ci} // GPTDataTextUI::SetAttributes() 341cf200d32Sopenharmony_ci 342cf200d32Sopenharmony_ci// Prompts the user for a partition name and sets the partition's 343cf200d32Sopenharmony_ci// name. Returns 1 on success, 0 on failure (invalid partition 344cf200d32Sopenharmony_ci// number). (Note that the function skips prompting when an 345cf200d32Sopenharmony_ci// invalid partition number is detected.) 346cf200d32Sopenharmony_ciint GPTDataTextUI::SetName(uint32_t partNum) { 347cf200d32Sopenharmony_ci UnicodeString theName = ""; 348cf200d32Sopenharmony_ci int retval = 1; 349cf200d32Sopenharmony_ci 350cf200d32Sopenharmony_ci if (IsUsedPartNum(partNum)) { 351cf200d32Sopenharmony_ci cout << "Enter name: "; 352cf200d32Sopenharmony_ci#ifdef USE_UTF16 353cf200d32Sopenharmony_ci theName = ReadUString(); 354cf200d32Sopenharmony_ci#else 355cf200d32Sopenharmony_ci theName = ReadString(); 356cf200d32Sopenharmony_ci#endif 357cf200d32Sopenharmony_ci partitions[partNum].SetName(theName); 358cf200d32Sopenharmony_ci } else { 359cf200d32Sopenharmony_ci cerr << "Invalid partition number (" << partNum << ")\n"; 360cf200d32Sopenharmony_ci retval = 0; 361cf200d32Sopenharmony_ci } // if/else 362cf200d32Sopenharmony_ci 363cf200d32Sopenharmony_ci return retval; 364cf200d32Sopenharmony_ci} // GPTDataTextUI::SetName() 365cf200d32Sopenharmony_ci 366cf200d32Sopenharmony_ci// Enable the user to byte-swap the name of the partition. Used to correct 367cf200d32Sopenharmony_ci// partition names damaged by incorrect byte order, as could be created by 368cf200d32Sopenharmony_ci// GPT fdisk 1.0.7 and earlier on big-endian systems, and perhaps other tools. 369cf200d32Sopenharmony_civoid GPTDataTextUI::ReverseName(uint32_t partNum) { 370cf200d32Sopenharmony_ci int swapBytes; 371cf200d32Sopenharmony_ci 372cf200d32Sopenharmony_ci cout << "Current name is: " << partitions[partNum].GetDescription() << "\n"; 373cf200d32Sopenharmony_ci partitions[partNum].ReverseNameBytes(); 374cf200d32Sopenharmony_ci cout << "Byte-swapped name is: " << partitions[partNum].GetDescription() << "\n"; 375cf200d32Sopenharmony_ci cout << "Do you want to byte-swap the name? "; 376cf200d32Sopenharmony_ci swapBytes = (GetYN() == 'Y'); 377cf200d32Sopenharmony_ci // Already swapped for display, so undo if necessary.... 378cf200d32Sopenharmony_ci if (!swapBytes) 379cf200d32Sopenharmony_ci partitions[partNum].ReverseNameBytes(); 380cf200d32Sopenharmony_ci} // GPTDataTextUI::ReverseName() 381cf200d32Sopenharmony_ci 382cf200d32Sopenharmony_ci// Ask user for two partition numbers and swap them in the table. Note that 383cf200d32Sopenharmony_ci// this just reorders table entries; it doesn't adjust partition layout on 384cf200d32Sopenharmony_ci// the disk. 385cf200d32Sopenharmony_ci// Returns 1 if successful, 0 if not. (If user enters identical numbers, it 386cf200d32Sopenharmony_ci// counts as successful.) 387cf200d32Sopenharmony_ciint GPTDataTextUI::SwapPartitions(void) { 388cf200d32Sopenharmony_ci int partNum1, partNum2, didIt = 0; 389cf200d32Sopenharmony_ci uint32_t low, high; 390cf200d32Sopenharmony_ci ostringstream prompt; 391cf200d32Sopenharmony_ci GPTPart temp; 392cf200d32Sopenharmony_ci 393cf200d32Sopenharmony_ci if (GetPartRange(&low, &high) > 0) { 394cf200d32Sopenharmony_ci partNum1 = GetPartNum(); 395cf200d32Sopenharmony_ci if (high >= numParts - 1) 396cf200d32Sopenharmony_ci high = 0; 397cf200d32Sopenharmony_ci prompt << "New partition number (1-" << numParts 398cf200d32Sopenharmony_ci << ", default " << high + 2 << "): "; 399cf200d32Sopenharmony_ci partNum2 = GetNumber(1, numParts, high + 2, prompt.str()) - 1; 400cf200d32Sopenharmony_ci didIt = GPTData::SwapPartitions(partNum1, partNum2); 401cf200d32Sopenharmony_ci } else { 402cf200d32Sopenharmony_ci cout << "No partitions\n"; 403cf200d32Sopenharmony_ci } // if/else 404cf200d32Sopenharmony_ci return didIt; 405cf200d32Sopenharmony_ci} // GPTDataTextUI::SwapPartitionNumbers() 406cf200d32Sopenharmony_ci 407cf200d32Sopenharmony_ci// This function destroys the on-disk GPT structures. Returns 1 if the user 408cf200d32Sopenharmony_ci// confirms destruction, 0 if the user aborts or if there's a disk error. 409cf200d32Sopenharmony_ciint GPTDataTextUI::DestroyGPTwPrompt(void) { 410cf200d32Sopenharmony_ci int allOK = 1; 411cf200d32Sopenharmony_ci 412cf200d32Sopenharmony_ci if ((apmFound) || (bsdFound)) { 413cf200d32Sopenharmony_ci cout << "WARNING: APM or BSD disklabel structures detected! This operation could\n" 414cf200d32Sopenharmony_ci << "damage any APM or BSD partitions on this disk!\n"; 415cf200d32Sopenharmony_ci } // if APM or BSD 416cf200d32Sopenharmony_ci cout << "\a\aAbout to wipe out GPT on " << device << ". Proceed? "; 417cf200d32Sopenharmony_ci if (GetYN() == 'Y') { 418cf200d32Sopenharmony_ci if (DestroyGPT()) { 419cf200d32Sopenharmony_ci // Note on below: Touch the MBR only if the user wants it completely 420cf200d32Sopenharmony_ci // blanked out. Version 0.4.2 deleted the 0xEE partition and re-wrote 421cf200d32Sopenharmony_ci // the MBR, but this could wipe out a valid MBR that the program 422cf200d32Sopenharmony_ci // had subsequently discarded (say, if it conflicted with older GPT 423cf200d32Sopenharmony_ci // structures). 424cf200d32Sopenharmony_ci cout << "Blank out MBR? "; 425cf200d32Sopenharmony_ci if (GetYN() == 'Y') { 426cf200d32Sopenharmony_ci DestroyMBR(); 427cf200d32Sopenharmony_ci } else { 428cf200d32Sopenharmony_ci cout << "MBR is unchanged. You may need to delete an EFI GPT (0xEE) partition\n" 429cf200d32Sopenharmony_ci << "with fdisk or another tool.\n"; 430cf200d32Sopenharmony_ci } // if/else 431cf200d32Sopenharmony_ci } else allOK = 0; // if GPT structures destroyed 432cf200d32Sopenharmony_ci } else allOK = 0; // if user confirms destruction 433cf200d32Sopenharmony_ci return (allOK); 434cf200d32Sopenharmony_ci} // GPTDataTextUI::DestroyGPTwPrompt() 435cf200d32Sopenharmony_ci 436cf200d32Sopenharmony_ci// Get partition number from user and then call ShowPartDetails(partNum) 437cf200d32Sopenharmony_ci// to show its detailed information 438cf200d32Sopenharmony_civoid GPTDataTextUI::ShowDetails(void) { 439cf200d32Sopenharmony_ci int partNum; 440cf200d32Sopenharmony_ci uint32_t low, high; 441cf200d32Sopenharmony_ci 442cf200d32Sopenharmony_ci if (GetPartRange(&low, &high) > 0) { 443cf200d32Sopenharmony_ci partNum = GetPartNum(); 444cf200d32Sopenharmony_ci ShowPartDetails(partNum); 445cf200d32Sopenharmony_ci } else { 446cf200d32Sopenharmony_ci cout << "No partitions\n"; 447cf200d32Sopenharmony_ci } // if/else 448cf200d32Sopenharmony_ci} // GPTDataTextUI::ShowDetails() 449cf200d32Sopenharmony_ci 450cf200d32Sopenharmony_ci// Create a hybrid MBR -- an ugly, funky thing that helps GPT work with 451cf200d32Sopenharmony_ci// OSes that don't understand GPT. 452cf200d32Sopenharmony_civoid GPTDataTextUI::MakeHybrid(void) { 453cf200d32Sopenharmony_ci uint32_t partNums[3] = {0, 0, 0}; 454cf200d32Sopenharmony_ci string line; 455cf200d32Sopenharmony_ci int numPartsToCvt = 0, numConverted = 0, i, j, mbrNum = 0; 456cf200d32Sopenharmony_ci unsigned int hexCode = 0; 457cf200d32Sopenharmony_ci MBRPart hybridPart; 458cf200d32Sopenharmony_ci MBRData hybridMBR; 459cf200d32Sopenharmony_ci char eeFirst = 'Y'; // Whether EFI GPT (0xEE) partition comes first in table 460cf200d32Sopenharmony_ci 461cf200d32Sopenharmony_ci cout << "\nWARNING! Hybrid MBRs are flaky and dangerous! If you decide not to use one,\n" 462cf200d32Sopenharmony_ci << "just hit the Enter key at the below prompt and your MBR partition table will\n" 463cf200d32Sopenharmony_ci << "be untouched.\n\n\a"; 464cf200d32Sopenharmony_ci 465cf200d32Sopenharmony_ci // Use a local MBR structure, copying from protectiveMBR to keep its 466cf200d32Sopenharmony_ci // boot loader code intact.... 467cf200d32Sopenharmony_ci hybridMBR = protectiveMBR; 468cf200d32Sopenharmony_ci hybridMBR.EmptyMBR(0); 469cf200d32Sopenharmony_ci 470cf200d32Sopenharmony_ci // Now get the numbers of up to three partitions to add to the 471cf200d32Sopenharmony_ci // hybrid MBR.... 472cf200d32Sopenharmony_ci cout << "Type from one to three GPT partition numbers, separated by spaces, to be\n" 473cf200d32Sopenharmony_ci << "added to the hybrid MBR, in sequence: "; 474cf200d32Sopenharmony_ci line = ReadString(); 475cf200d32Sopenharmony_ci istringstream inLine(line); 476cf200d32Sopenharmony_ci do { 477cf200d32Sopenharmony_ci inLine >> partNums[numPartsToCvt]; 478cf200d32Sopenharmony_ci if (partNums[numPartsToCvt] > 0) 479cf200d32Sopenharmony_ci numPartsToCvt++; 480cf200d32Sopenharmony_ci } while (!inLine.eof() && (numPartsToCvt < 3)); 481cf200d32Sopenharmony_ci 482cf200d32Sopenharmony_ci if (numPartsToCvt > 0) { 483cf200d32Sopenharmony_ci cout << "Place EFI GPT (0xEE) partition first in MBR (good for GRUB)? "; 484cf200d32Sopenharmony_ci eeFirst = GetYN(); 485cf200d32Sopenharmony_ci } // if 486cf200d32Sopenharmony_ci 487cf200d32Sopenharmony_ci for (i = 0; i < numPartsToCvt; i++) { 488cf200d32Sopenharmony_ci j = partNums[i] - 1; 489cf200d32Sopenharmony_ci if (partitions[j].IsUsed() && (partitions[j].IsSizedForMBR() != MBR_SIZED_BAD)) { 490cf200d32Sopenharmony_ci mbrNum = i + (eeFirst == 'Y'); 491cf200d32Sopenharmony_ci cout << "\nCreating entry for GPT partition #" << j + 1 492cf200d32Sopenharmony_ci << " (MBR partition #" << mbrNum + 1 << ")\n"; 493cf200d32Sopenharmony_ci hybridPart.SetType(GetMBRTypeCode(partitions[j].GetHexType() / 256)); 494cf200d32Sopenharmony_ci hybridPart.SetLocation(partitions[j].GetFirstLBA(), partitions[j].GetLengthLBA()); 495cf200d32Sopenharmony_ci hybridPart.SetInclusion(PRIMARY); 496cf200d32Sopenharmony_ci cout << "Set the bootable flag? "; 497cf200d32Sopenharmony_ci if (GetYN() == 'Y') 498cf200d32Sopenharmony_ci hybridPart.SetStatus(0x80); 499cf200d32Sopenharmony_ci else 500cf200d32Sopenharmony_ci hybridPart.SetStatus(0x00); 501cf200d32Sopenharmony_ci hybridPart.SetInclusion(PRIMARY); 502cf200d32Sopenharmony_ci if (partitions[j].IsSizedForMBR() == MBR_SIZED_IFFY) 503cf200d32Sopenharmony_ci WarnAboutIffyMBRPart(j + 1); 504cf200d32Sopenharmony_ci numConverted++; 505cf200d32Sopenharmony_ci } else { 506cf200d32Sopenharmony_ci cerr << "\nGPT partition #" << j + 1 << " does not exist or is too big; skipping.\n"; 507cf200d32Sopenharmony_ci } // if/else 508cf200d32Sopenharmony_ci hybridMBR.AddPart(mbrNum, hybridPart); 509cf200d32Sopenharmony_ci } // for 510cf200d32Sopenharmony_ci 511cf200d32Sopenharmony_ci if (numConverted > 0) { // User opted to create a hybrid MBR.... 512cf200d32Sopenharmony_ci // Create EFI protective partition that covers the start of the disk. 513cf200d32Sopenharmony_ci // If this location (covering the main GPT data structures) is omitted, 514cf200d32Sopenharmony_ci // Linux won't find any partitions on the disk. 515cf200d32Sopenharmony_ci hybridPart.SetLocation(1, hybridMBR.FindLastInFree(1)); 516cf200d32Sopenharmony_ci hybridPart.SetStatus(0); 517cf200d32Sopenharmony_ci hybridPart.SetType(0xEE); 518cf200d32Sopenharmony_ci hybridPart.SetInclusion(PRIMARY); 519cf200d32Sopenharmony_ci // newNote firstLBA and lastLBA are computed later... 520cf200d32Sopenharmony_ci if (eeFirst == 'Y') { 521cf200d32Sopenharmony_ci hybridMBR.AddPart(0, hybridPart); 522cf200d32Sopenharmony_ci } else { 523cf200d32Sopenharmony_ci hybridMBR.AddPart(numConverted, hybridPart); 524cf200d32Sopenharmony_ci } // else 525cf200d32Sopenharmony_ci hybridMBR.SetHybrid(); 526cf200d32Sopenharmony_ci 527cf200d32Sopenharmony_ci // ... and for good measure, if there are any partition spaces left, 528cf200d32Sopenharmony_ci // optionally create another protective EFI partition to cover as much 529cf200d32Sopenharmony_ci // space as possible.... 530cf200d32Sopenharmony_ci if (hybridMBR.CountParts() < 4) { // unused entry.... 531cf200d32Sopenharmony_ci cout << "\nUnused partition space(s) found. Use one to protect more partitions? "; 532cf200d32Sopenharmony_ci if (GetYN() == 'Y') { 533cf200d32Sopenharmony_ci cout << "Note: Default is 0xEE, but this may confuse Mac OS X.\n"; 534cf200d32Sopenharmony_ci // Comment on above: Mac OS treats disks with more than one 535cf200d32Sopenharmony_ci // 0xEE MBR partition as MBR disks, not as GPT disks. 536cf200d32Sopenharmony_ci hexCode = GetMBRTypeCode(0xEE); 537cf200d32Sopenharmony_ci hybridMBR.MakeBiggestPart(3, hexCode); 538cf200d32Sopenharmony_ci } // if (GetYN() == 'Y') 539cf200d32Sopenharmony_ci } // if unused entry 540cf200d32Sopenharmony_ci protectiveMBR = hybridMBR; 541cf200d32Sopenharmony_ci } else { 542cf200d32Sopenharmony_ci cout << "\nNo partitions converted; original protective/hybrid MBR is unmodified!\n"; 543cf200d32Sopenharmony_ci } // if/else (numConverted > 0) 544cf200d32Sopenharmony_ci} // GPTDataTextUI::MakeHybrid() 545cf200d32Sopenharmony_ci 546cf200d32Sopenharmony_ci// Convert the GPT to MBR form, storing partitions in the protectiveMBR 547cf200d32Sopenharmony_ci// variable. This function is necessarily limited; it may not be able to 548cf200d32Sopenharmony_ci// convert all partitions, depending on the disk size and available space 549cf200d32Sopenharmony_ci// before each partition (one free sector is required to create a logical 550cf200d32Sopenharmony_ci// partition, which are necessary to convert more than four partitions). 551cf200d32Sopenharmony_ci// Returns the number of converted partitions; if this value 552cf200d32Sopenharmony_ci// is over 0, the calling function should call DestroyGPT() to destroy 553cf200d32Sopenharmony_ci// the GPT data, call SaveMBR() to save the MBR, and then exit. 554cf200d32Sopenharmony_ciint GPTDataTextUI::XFormToMBR(void) { 555cf200d32Sopenharmony_ci uint32_t i; 556cf200d32Sopenharmony_ci 557cf200d32Sopenharmony_ci protectiveMBR.EmptyMBR(0); 558cf200d32Sopenharmony_ci for (i = 0; i < numParts; i++) { 559cf200d32Sopenharmony_ci if (partitions[i].IsUsed()) { 560cf200d32Sopenharmony_ci if (partitions[i].IsSizedForMBR() == MBR_SIZED_IFFY) 561cf200d32Sopenharmony_ci WarnAboutIffyMBRPart(i + 1); 562cf200d32Sopenharmony_ci // Note: MakePart() checks for oversized partitions, so don't 563cf200d32Sopenharmony_ci // bother checking other IsSizedForMBR() return values.... 564cf200d32Sopenharmony_ci protectiveMBR.MakePart(i, partitions[i].GetFirstLBA(), 565cf200d32Sopenharmony_ci partitions[i].GetLengthLBA(), 566cf200d32Sopenharmony_ci partitions[i].GetHexType() / 0x0100, 0); 567cf200d32Sopenharmony_ci } // if 568cf200d32Sopenharmony_ci } // for 569cf200d32Sopenharmony_ci protectiveMBR.MakeItLegal(); 570cf200d32Sopenharmony_ci return protectiveMBR.DoMenu(); 571cf200d32Sopenharmony_ci} // GPTDataTextUI::XFormToMBR() 572cf200d32Sopenharmony_ci 573cf200d32Sopenharmony_ci// Obtains a sector number, between low and high, from the 574cf200d32Sopenharmony_ci// user, accepting values prefixed by "+" to add sectors to low, 575cf200d32Sopenharmony_ci// or the same with "K", "M", "G", "T", or "P" as suffixes to add 576cf200d32Sopenharmony_ci// kibibytes, mebibytes, gibibytes, tebibytes, or pebibytes, 577cf200d32Sopenharmony_ci// respectively. If a "-" prefix is used, use the high value minus 578cf200d32Sopenharmony_ci// the user-specified number of sectors (or KiB, MiB, etc.). Use the 579cf200d32Sopenharmony_ci// def value as the default if the user just hits Enter. 580cf200d32Sopenharmony_ciuint64_t GPTDataTextUI::GetSectorNum(uint64_t low, uint64_t high, uint64_t def, 581cf200d32Sopenharmony_ci const string & prompt) { 582cf200d32Sopenharmony_ci uint64_t response; 583cf200d32Sopenharmony_ci char line[255]; 584cf200d32Sopenharmony_ci 585cf200d32Sopenharmony_ci do { 586cf200d32Sopenharmony_ci cout << prompt; 587cf200d32Sopenharmony_ci cin.getline(line, 255); 588cf200d32Sopenharmony_ci if (!cin.good()) 589cf200d32Sopenharmony_ci exit(5); 590cf200d32Sopenharmony_ci response = IeeeToInt(line, blockSize, low, high, sectorAlignment, def); 591cf200d32Sopenharmony_ci } while ((response < low) || (response > high)); 592cf200d32Sopenharmony_ci return response; 593cf200d32Sopenharmony_ci} // GPTDataTextUI::GetSectorNum() 594cf200d32Sopenharmony_ci 595cf200d32Sopenharmony_ci/****************************************************** 596cf200d32Sopenharmony_ci * * 597cf200d32Sopenharmony_ci * Display informational messages for the user.... * 598cf200d32Sopenharmony_ci * * 599cf200d32Sopenharmony_ci ******************************************************/ 600cf200d32Sopenharmony_ci 601cf200d32Sopenharmony_ci// Although an MBR partition that begins below sector 2^32 and is less than 2^32 sectors in 602cf200d32Sopenharmony_ci// length is technically legal even if it ends above the 2^32-sector mark, such a partition 603cf200d32Sopenharmony_ci// tends to confuse a lot of OSes, so warn the user about such partitions. This function is 604cf200d32Sopenharmony_ci// called by XFormToMBR() and MakeHybrid(); it's a separate function just to consolidate the 605cf200d32Sopenharmony_ci// lengthy message in one place. 606cf200d32Sopenharmony_civoid GPTDataTextUI::WarnAboutIffyMBRPart(int partNum) { 607cf200d32Sopenharmony_ci cout << "\a\nWarning! GPT partition " << partNum << " ends after the 2^32 sector mark! The partition\n" 608cf200d32Sopenharmony_ci << "begins before this point, and is smaller than 2^32 sectors. This is technically\n" 609cf200d32Sopenharmony_ci << "legal, but will confuse some OSes. The partition IS being added to the MBR, but\n" 610cf200d32Sopenharmony_ci << "if your OS misbehaves or can't see the partition, the partition may simply be\n" 611cf200d32Sopenharmony_ci << "unusable in that OS and may need to be resized or omitted from the MBR.\n\n"; 612cf200d32Sopenharmony_ci} // GPTDataTextUI::WarnAboutIffyMBRPart() 613cf200d32Sopenharmony_ci 614cf200d32Sopenharmony_ci/********************************************************************* 615cf200d32Sopenharmony_ci * * 616cf200d32Sopenharmony_ci * The following functions provide the main menus for the gdisk * 617cf200d32Sopenharmony_ci * program.... * 618cf200d32Sopenharmony_ci * * 619cf200d32Sopenharmony_ci *********************************************************************/ 620cf200d32Sopenharmony_ci 621cf200d32Sopenharmony_ci// Accept a command and execute it. Returns only when the user 622cf200d32Sopenharmony_ci// wants to exit (such as after a 'w' or 'q' command). 623cf200d32Sopenharmony_civoid GPTDataTextUI::MainMenu(string filename) { 624cf200d32Sopenharmony_ci int goOn = 1; 625cf200d32Sopenharmony_ci PartType typeHelper; 626cf200d32Sopenharmony_ci uint32_t temp1, temp2; 627cf200d32Sopenharmony_ci 628cf200d32Sopenharmony_ci do { 629cf200d32Sopenharmony_ci cout << "\nCommand (? for help): "; 630cf200d32Sopenharmony_ci switch (ReadString()[0]) { 631cf200d32Sopenharmony_ci case '\0': 632cf200d32Sopenharmony_ci goOn = cin.good(); 633cf200d32Sopenharmony_ci break; 634cf200d32Sopenharmony_ci case 'b': case 'B': 635cf200d32Sopenharmony_ci cout << "Enter backup filename to save: "; 636cf200d32Sopenharmony_ci SaveGPTBackup(ReadString()); 637cf200d32Sopenharmony_ci break; 638cf200d32Sopenharmony_ci case 'c': case 'C': 639cf200d32Sopenharmony_ci if (GetPartRange(&temp1, &temp2) > 0) 640cf200d32Sopenharmony_ci SetName(GetPartNum()); 641cf200d32Sopenharmony_ci else 642cf200d32Sopenharmony_ci cout << "No partitions\n"; 643cf200d32Sopenharmony_ci break; 644cf200d32Sopenharmony_ci case 'd': case 'D': 645cf200d32Sopenharmony_ci DeletePartition(); 646cf200d32Sopenharmony_ci break; 647cf200d32Sopenharmony_ci case 'i': case 'I': 648cf200d32Sopenharmony_ci ShowDetails(); 649cf200d32Sopenharmony_ci break; 650cf200d32Sopenharmony_ci case 'l': case 'L': 651cf200d32Sopenharmony_ci typeHelper.ShowAllTypes(); 652cf200d32Sopenharmony_ci break; 653cf200d32Sopenharmony_ci case 'n': case 'N': 654cf200d32Sopenharmony_ci CreatePartition(); 655cf200d32Sopenharmony_ci break; 656cf200d32Sopenharmony_ci case 'o': case 'O': 657cf200d32Sopenharmony_ci cout << "This option deletes all partitions and creates a new protective MBR.\n" 658cf200d32Sopenharmony_ci << "Proceed? "; 659cf200d32Sopenharmony_ci if (GetYN() == 'Y') { 660cf200d32Sopenharmony_ci ClearGPTData(); 661cf200d32Sopenharmony_ci MakeProtectiveMBR(); 662cf200d32Sopenharmony_ci } // if 663cf200d32Sopenharmony_ci break; 664cf200d32Sopenharmony_ci case 'p': case 'P': 665cf200d32Sopenharmony_ci DisplayGPTData(); 666cf200d32Sopenharmony_ci break; 667cf200d32Sopenharmony_ci case 'q': case 'Q': 668cf200d32Sopenharmony_ci goOn = 0; 669cf200d32Sopenharmony_ci break; 670cf200d32Sopenharmony_ci case 'r': case 'R': 671cf200d32Sopenharmony_ci RecoveryMenu(filename); 672cf200d32Sopenharmony_ci goOn = 0; 673cf200d32Sopenharmony_ci break; 674cf200d32Sopenharmony_ci case 's': case 'S': 675cf200d32Sopenharmony_ci SortGPT(); 676cf200d32Sopenharmony_ci cout << "You may need to edit /etc/fstab and/or your boot loader configuration!\n"; 677cf200d32Sopenharmony_ci break; 678cf200d32Sopenharmony_ci case 't': case 'T': 679cf200d32Sopenharmony_ci ChangePartType(); 680cf200d32Sopenharmony_ci break; 681cf200d32Sopenharmony_ci case 'v': case 'V': 682cf200d32Sopenharmony_ci Verify(); 683cf200d32Sopenharmony_ci break; 684cf200d32Sopenharmony_ci case 'w': case 'W': 685cf200d32Sopenharmony_ci if (SaveGPTData() == 1) 686cf200d32Sopenharmony_ci goOn = 0; 687cf200d32Sopenharmony_ci break; 688cf200d32Sopenharmony_ci case 'x': case 'X': 689cf200d32Sopenharmony_ci ExpertsMenu(filename); 690cf200d32Sopenharmony_ci goOn = 0; 691cf200d32Sopenharmony_ci break; 692cf200d32Sopenharmony_ci default: 693cf200d32Sopenharmony_ci ShowCommands(); 694cf200d32Sopenharmony_ci break; 695cf200d32Sopenharmony_ci } // switch 696cf200d32Sopenharmony_ci } while (goOn); 697cf200d32Sopenharmony_ci} // GPTDataTextUI::MainMenu() 698cf200d32Sopenharmony_ci 699cf200d32Sopenharmony_civoid GPTDataTextUI::ShowCommands(void) { 700cf200d32Sopenharmony_ci cout << "b\tback up GPT data to a file\n"; 701cf200d32Sopenharmony_ci cout << "c\tchange a partition's name\n"; 702cf200d32Sopenharmony_ci cout << "d\tdelete a partition\n"; 703cf200d32Sopenharmony_ci cout << "i\tshow detailed information on a partition\n"; 704cf200d32Sopenharmony_ci cout << "l\tlist known partition types\n"; 705cf200d32Sopenharmony_ci cout << "n\tadd a new partition\n"; 706cf200d32Sopenharmony_ci cout << "o\tcreate a new empty GUID partition table (GPT)\n"; 707cf200d32Sopenharmony_ci cout << "p\tprint the partition table\n"; 708cf200d32Sopenharmony_ci cout << "q\tquit without saving changes\n"; 709cf200d32Sopenharmony_ci cout << "r\trecovery and transformation options (experts only)\n"; 710cf200d32Sopenharmony_ci cout << "s\tsort partitions\n"; 711cf200d32Sopenharmony_ci cout << "t\tchange a partition's type code\n"; 712cf200d32Sopenharmony_ci cout << "v\tverify disk\n"; 713cf200d32Sopenharmony_ci cout << "w\twrite table to disk and exit\n"; 714cf200d32Sopenharmony_ci cout << "x\textra functionality (experts only)\n"; 715cf200d32Sopenharmony_ci cout << "?\tprint this menu\n"; 716cf200d32Sopenharmony_ci} // GPTDataTextUI::ShowCommands() 717cf200d32Sopenharmony_ci 718cf200d32Sopenharmony_ci// Accept a recovery & transformation menu command. Returns only when the user 719cf200d32Sopenharmony_ci// issues an exit command, such as 'w' or 'q'. 720cf200d32Sopenharmony_civoid GPTDataTextUI::RecoveryMenu(string filename) { 721cf200d32Sopenharmony_ci uint32_t numParts; 722cf200d32Sopenharmony_ci int goOn = 1, temp1; 723cf200d32Sopenharmony_ci 724cf200d32Sopenharmony_ci do { 725cf200d32Sopenharmony_ci cout << "\nRecovery/transformation command (? for help): "; 726cf200d32Sopenharmony_ci switch (ReadString()[0]) { 727cf200d32Sopenharmony_ci case '\0': 728cf200d32Sopenharmony_ci goOn = cin.good(); 729cf200d32Sopenharmony_ci break; 730cf200d32Sopenharmony_ci case 'b': case 'B': 731cf200d32Sopenharmony_ci RebuildMainHeader(); 732cf200d32Sopenharmony_ci break; 733cf200d32Sopenharmony_ci case 'c': case 'C': 734cf200d32Sopenharmony_ci cout << "Warning! This will probably do weird things if you've converted an MBR to\n" 735cf200d32Sopenharmony_ci << "GPT form and haven't yet saved the GPT! Proceed? "; 736cf200d32Sopenharmony_ci if (GetYN() == 'Y') 737cf200d32Sopenharmony_ci LoadSecondTableAsMain(); 738cf200d32Sopenharmony_ci break; 739cf200d32Sopenharmony_ci case 'd': case 'D': 740cf200d32Sopenharmony_ci RebuildSecondHeader(); 741cf200d32Sopenharmony_ci break; 742cf200d32Sopenharmony_ci case 'e': case 'E': 743cf200d32Sopenharmony_ci cout << "Warning! This will probably do weird things if you've converted an MBR to\n" 744cf200d32Sopenharmony_ci << "GPT form and haven't yet saved the GPT! Proceed? "; 745cf200d32Sopenharmony_ci if (GetYN() == 'Y') 746cf200d32Sopenharmony_ci LoadMainTable(); 747cf200d32Sopenharmony_ci break; 748cf200d32Sopenharmony_ci case 'f': case 'F': 749cf200d32Sopenharmony_ci cout << "Warning! This will destroy the currently defined partitions! Proceed? "; 750cf200d32Sopenharmony_ci if (GetYN() == 'Y') { 751cf200d32Sopenharmony_ci if (LoadMBR(filename) == 1) { // successful load 752cf200d32Sopenharmony_ci XFormPartitions(); 753cf200d32Sopenharmony_ci } else { 754cf200d32Sopenharmony_ci cout << "Problem loading MBR! GPT is untouched; regenerating protective MBR!\n"; 755cf200d32Sopenharmony_ci MakeProtectiveMBR(); 756cf200d32Sopenharmony_ci } // if/else 757cf200d32Sopenharmony_ci } // if 758cf200d32Sopenharmony_ci break; 759cf200d32Sopenharmony_ci case 'g': case 'G': 760cf200d32Sopenharmony_ci numParts = GetNumParts(); 761cf200d32Sopenharmony_ci temp1 = XFormToMBR(); 762cf200d32Sopenharmony_ci if (temp1 > 0) 763cf200d32Sopenharmony_ci cout << "\nConverted " << temp1 << " partitions. Finalize and exit? "; 764cf200d32Sopenharmony_ci if ((temp1 > 0) && (GetYN() == 'Y')) { 765cf200d32Sopenharmony_ci if ((DestroyGPT() > 0) && (SaveMBR())) { 766cf200d32Sopenharmony_ci goOn = 0; 767cf200d32Sopenharmony_ci } // if 768cf200d32Sopenharmony_ci } else { 769cf200d32Sopenharmony_ci MakeProtectiveMBR(); 770cf200d32Sopenharmony_ci SetGPTSize(numParts, 0); 771cf200d32Sopenharmony_ci cout << "Note: New protective MBR created\n\n"; 772cf200d32Sopenharmony_ci } // if/else 773cf200d32Sopenharmony_ci break; 774cf200d32Sopenharmony_ci case 'h': case 'H': 775cf200d32Sopenharmony_ci MakeHybrid(); 776cf200d32Sopenharmony_ci break; 777cf200d32Sopenharmony_ci case 'i': case 'I': 778cf200d32Sopenharmony_ci ShowDetails(); 779cf200d32Sopenharmony_ci break; 780cf200d32Sopenharmony_ci case 'l': case 'L': 781cf200d32Sopenharmony_ci cout << "Enter backup filename to load: "; 782cf200d32Sopenharmony_ci LoadGPTBackup(ReadString()); 783cf200d32Sopenharmony_ci break; 784cf200d32Sopenharmony_ci case 'm': case 'M': 785cf200d32Sopenharmony_ci MainMenu(filename); 786cf200d32Sopenharmony_ci goOn = 0; 787cf200d32Sopenharmony_ci break; 788cf200d32Sopenharmony_ci case 'o': case 'O': 789cf200d32Sopenharmony_ci DisplayMBRData(); 790cf200d32Sopenharmony_ci break; 791cf200d32Sopenharmony_ci case 'p': case 'P': 792cf200d32Sopenharmony_ci DisplayGPTData(); 793cf200d32Sopenharmony_ci break; 794cf200d32Sopenharmony_ci case 'q': case 'Q': 795cf200d32Sopenharmony_ci goOn = 0; 796cf200d32Sopenharmony_ci break; 797cf200d32Sopenharmony_ci case 't': case 'T': 798cf200d32Sopenharmony_ci XFormDisklabel(); 799cf200d32Sopenharmony_ci break; 800cf200d32Sopenharmony_ci case 'v': case 'V': 801cf200d32Sopenharmony_ci Verify(); 802cf200d32Sopenharmony_ci break; 803cf200d32Sopenharmony_ci case 'w': case 'W': 804cf200d32Sopenharmony_ci if (SaveGPTData() == 1) { 805cf200d32Sopenharmony_ci goOn = 0; 806cf200d32Sopenharmony_ci } // if 807cf200d32Sopenharmony_ci break; 808cf200d32Sopenharmony_ci case 'x': case 'X': 809cf200d32Sopenharmony_ci ExpertsMenu(filename); 810cf200d32Sopenharmony_ci goOn = 0; 811cf200d32Sopenharmony_ci break; 812cf200d32Sopenharmony_ci default: 813cf200d32Sopenharmony_ci ShowRecoveryCommands(); 814cf200d32Sopenharmony_ci break; 815cf200d32Sopenharmony_ci } // switch 816cf200d32Sopenharmony_ci } while (goOn); 817cf200d32Sopenharmony_ci} // GPTDataTextUI::RecoveryMenu() 818cf200d32Sopenharmony_ci 819cf200d32Sopenharmony_civoid GPTDataTextUI::ShowRecoveryCommands(void) { 820cf200d32Sopenharmony_ci cout << "b\tuse backup GPT header (rebuilding main)\n"; 821cf200d32Sopenharmony_ci cout << "c\tload backup partition table from disk (rebuilding main)\n"; 822cf200d32Sopenharmony_ci cout << "d\tuse main GPT header (rebuilding backup)\n"; 823cf200d32Sopenharmony_ci cout << "e\tload main partition table from disk (rebuilding backup)\n"; 824cf200d32Sopenharmony_ci cout << "f\tload MBR and build fresh GPT from it\n"; 825cf200d32Sopenharmony_ci cout << "g\tconvert GPT into MBR and exit\n"; 826cf200d32Sopenharmony_ci cout << "h\tmake hybrid MBR\n"; 827cf200d32Sopenharmony_ci cout << "i\tshow detailed information on a partition\n"; 828cf200d32Sopenharmony_ci cout << "l\tload partition data from a backup file\n"; 829cf200d32Sopenharmony_ci cout << "m\treturn to main menu\n"; 830cf200d32Sopenharmony_ci cout << "o\tprint protective MBR data\n"; 831cf200d32Sopenharmony_ci cout << "p\tprint the partition table\n"; 832cf200d32Sopenharmony_ci cout << "q\tquit without saving changes\n"; 833cf200d32Sopenharmony_ci cout << "t\ttransform BSD disklabel partition\n"; 834cf200d32Sopenharmony_ci cout << "v\tverify disk\n"; 835cf200d32Sopenharmony_ci cout << "w\twrite table to disk and exit\n"; 836cf200d32Sopenharmony_ci cout << "x\textra functionality (experts only)\n"; 837cf200d32Sopenharmony_ci cout << "?\tprint this menu\n"; 838cf200d32Sopenharmony_ci} // GPTDataTextUI::ShowRecoveryCommands() 839cf200d32Sopenharmony_ci 840cf200d32Sopenharmony_ci// Accept an experts' menu command. Returns only after the user 841cf200d32Sopenharmony_ci// selects an exit command, such as 'w' or 'q'. 842cf200d32Sopenharmony_civoid GPTDataTextUI::ExpertsMenu(string filename) { 843cf200d32Sopenharmony_ci GPTData secondDevice; 844cf200d32Sopenharmony_ci uint32_t temp1, temp2; 845cf200d32Sopenharmony_ci int goOn = 1; 846cf200d32Sopenharmony_ci string guidStr, device; 847cf200d32Sopenharmony_ci GUIDData aGUID; 848cf200d32Sopenharmony_ci ostringstream prompt; 849cf200d32Sopenharmony_ci 850cf200d32Sopenharmony_ci do { 851cf200d32Sopenharmony_ci cout << "\nExpert command (? for help): "; 852cf200d32Sopenharmony_ci switch (ReadString()[0]) { 853cf200d32Sopenharmony_ci case '\0': 854cf200d32Sopenharmony_ci goOn = cin.good(); 855cf200d32Sopenharmony_ci break; 856cf200d32Sopenharmony_ci case 'a': case 'A': 857cf200d32Sopenharmony_ci if (GetPartRange(&temp1, &temp2) > 0) 858cf200d32Sopenharmony_ci SetAttributes(GetPartNum()); 859cf200d32Sopenharmony_ci else 860cf200d32Sopenharmony_ci cout << "No partitions\n"; 861cf200d32Sopenharmony_ci break; 862cf200d32Sopenharmony_ci case 'b': case 'B': 863cf200d32Sopenharmony_ci ReverseName(GetPartNum()); 864cf200d32Sopenharmony_ci break; 865cf200d32Sopenharmony_ci case 'c': case 'C': 866cf200d32Sopenharmony_ci ChangeUniqueGuid(); 867cf200d32Sopenharmony_ci break; 868cf200d32Sopenharmony_ci case 'd': case 'D': 869cf200d32Sopenharmony_ci cout << "Partitions will begin on " << GetAlignment() 870cf200d32Sopenharmony_ci << "-sector boundaries.\n"; 871cf200d32Sopenharmony_ci break; 872cf200d32Sopenharmony_ci case 'e': case 'E': 873cf200d32Sopenharmony_ci cout << "Relocating backup data structures to the end of the disk\n"; 874cf200d32Sopenharmony_ci MoveSecondHeaderToEnd(); 875cf200d32Sopenharmony_ci break; 876cf200d32Sopenharmony_ci case 'f': case 'F': 877cf200d32Sopenharmony_ci RandomizeGUIDs(); 878cf200d32Sopenharmony_ci break; 879cf200d32Sopenharmony_ci case 'g': case 'G': 880cf200d32Sopenharmony_ci cout << "Enter the disk's unique GUID ('R' to randomize): "; 881cf200d32Sopenharmony_ci guidStr = ReadString(); 882cf200d32Sopenharmony_ci if ((guidStr.length() >= 32) || (guidStr[0] == 'R') || (guidStr[0] == 'r')) { 883cf200d32Sopenharmony_ci SetDiskGUID((GUIDData) guidStr); 884cf200d32Sopenharmony_ci cout << "The new disk GUID is " << GetDiskGUID() << "\n"; 885cf200d32Sopenharmony_ci } else { 886cf200d32Sopenharmony_ci cout << "GUID is too short!\n"; 887cf200d32Sopenharmony_ci } // if/else 888cf200d32Sopenharmony_ci break; 889cf200d32Sopenharmony_ci case 'h': case 'H': 890cf200d32Sopenharmony_ci RecomputeCHS(); 891cf200d32Sopenharmony_ci break; 892cf200d32Sopenharmony_ci case 'i': case 'I': 893cf200d32Sopenharmony_ci ShowDetails(); 894cf200d32Sopenharmony_ci break; 895cf200d32Sopenharmony_ci case 'j': case 'J': 896cf200d32Sopenharmony_ci MoveMainTable(); 897cf200d32Sopenharmony_ci break; 898cf200d32Sopenharmony_ci case 'k': case 'K': 899cf200d32Sopenharmony_ci MoveSecondTable(); 900cf200d32Sopenharmony_ci break; 901cf200d32Sopenharmony_ci case 'l': case 'L': 902cf200d32Sopenharmony_ci prompt.seekp(0); 903cf200d32Sopenharmony_ci prompt << "Enter the sector alignment value (1-" << MAX_ALIGNMENT << ", default = " 904cf200d32Sopenharmony_ci << DEFAULT_ALIGNMENT << "): "; 905cf200d32Sopenharmony_ci temp1 = GetNumber(1, MAX_ALIGNMENT, DEFAULT_ALIGNMENT, prompt.str()); 906cf200d32Sopenharmony_ci SetAlignment(temp1); 907cf200d32Sopenharmony_ci break; 908cf200d32Sopenharmony_ci case 'm': case 'M': 909cf200d32Sopenharmony_ci MainMenu(filename); 910cf200d32Sopenharmony_ci goOn = 0; 911cf200d32Sopenharmony_ci break; 912cf200d32Sopenharmony_ci case 'n': case 'N': 913cf200d32Sopenharmony_ci MakeProtectiveMBR(); 914cf200d32Sopenharmony_ci break; 915cf200d32Sopenharmony_ci case 'o': case 'O': 916cf200d32Sopenharmony_ci DisplayMBRData(); 917cf200d32Sopenharmony_ci break; 918cf200d32Sopenharmony_ci case 'p': case 'P': 919cf200d32Sopenharmony_ci DisplayGPTData(); 920cf200d32Sopenharmony_ci break; 921cf200d32Sopenharmony_ci case 'q': case 'Q': 922cf200d32Sopenharmony_ci goOn = 0; 923cf200d32Sopenharmony_ci break; 924cf200d32Sopenharmony_ci case 'r': case 'R': 925cf200d32Sopenharmony_ci RecoveryMenu(filename); 926cf200d32Sopenharmony_ci goOn = 0; 927cf200d32Sopenharmony_ci break; 928cf200d32Sopenharmony_ci case 's': case 'S': 929cf200d32Sopenharmony_ci ResizePartitionTable(); 930cf200d32Sopenharmony_ci break; 931cf200d32Sopenharmony_ci case 't': case 'T': 932cf200d32Sopenharmony_ci SwapPartitions(); 933cf200d32Sopenharmony_ci break; 934cf200d32Sopenharmony_ci case 'u': case 'U': 935cf200d32Sopenharmony_ci cout << "Type device filename, or press <Enter> to exit: "; 936cf200d32Sopenharmony_ci device = ReadString(); 937cf200d32Sopenharmony_ci if (device.length() > 0) { 938cf200d32Sopenharmony_ci secondDevice = *this; 939cf200d32Sopenharmony_ci secondDevice.SetDisk(device); 940cf200d32Sopenharmony_ci secondDevice.SaveGPTData(0); 941cf200d32Sopenharmony_ci } // if 942cf200d32Sopenharmony_ci break; 943cf200d32Sopenharmony_ci case 'v': case 'V': 944cf200d32Sopenharmony_ci Verify(); 945cf200d32Sopenharmony_ci break; 946cf200d32Sopenharmony_ci case 'w': case 'W': 947cf200d32Sopenharmony_ci if (SaveGPTData() == 1) { 948cf200d32Sopenharmony_ci goOn = 0; 949cf200d32Sopenharmony_ci } // if 950cf200d32Sopenharmony_ci break; 951cf200d32Sopenharmony_ci case 'z': case 'Z': 952cf200d32Sopenharmony_ci if (DestroyGPTwPrompt() == 1) { 953cf200d32Sopenharmony_ci goOn = 0; 954cf200d32Sopenharmony_ci } 955cf200d32Sopenharmony_ci break; 956cf200d32Sopenharmony_ci default: 957cf200d32Sopenharmony_ci ShowExpertCommands(); 958cf200d32Sopenharmony_ci break; 959cf200d32Sopenharmony_ci } // switch 960cf200d32Sopenharmony_ci } while (goOn); 961cf200d32Sopenharmony_ci} // GPTDataTextUI::ExpertsMenu() 962cf200d32Sopenharmony_ci 963cf200d32Sopenharmony_civoid GPTDataTextUI::ShowExpertCommands(void) { 964cf200d32Sopenharmony_ci cout << "a\tset attributes\n"; 965cf200d32Sopenharmony_ci cout << "b\tbyte-swap a partition's name\n"; 966cf200d32Sopenharmony_ci cout << "c\tchange partition GUID\n"; 967cf200d32Sopenharmony_ci cout << "d\tdisplay the sector alignment value\n"; 968cf200d32Sopenharmony_ci cout << "e\trelocate backup data structures to the end of the disk\n"; 969cf200d32Sopenharmony_ci cout << "f\trandomize disk and partition unique GUIDs\n"; 970cf200d32Sopenharmony_ci cout << "g\tchange disk GUID\n"; 971cf200d32Sopenharmony_ci cout << "h\trecompute CHS values in protective/hybrid MBR\n"; 972cf200d32Sopenharmony_ci cout << "i\tshow detailed information on a partition\n"; 973cf200d32Sopenharmony_ci cout << "j\tmove the main partition table\n"; 974cf200d32Sopenharmony_ci cout << "k\tmove the backup partition table\n"; 975cf200d32Sopenharmony_ci cout << "l\tset the sector alignment value\n"; 976cf200d32Sopenharmony_ci cout << "m\treturn to main menu\n"; 977cf200d32Sopenharmony_ci cout << "n\tcreate a new protective MBR\n"; 978cf200d32Sopenharmony_ci cout << "o\tprint protective MBR data\n"; 979cf200d32Sopenharmony_ci cout << "p\tprint the partition table\n"; 980cf200d32Sopenharmony_ci cout << "q\tquit without saving changes\n"; 981cf200d32Sopenharmony_ci cout << "r\trecovery and transformation options (experts only)\n"; 982cf200d32Sopenharmony_ci cout << "s\tresize partition table\n"; 983cf200d32Sopenharmony_ci cout << "t\ttranspose two partition table entries\n"; 984cf200d32Sopenharmony_ci cout << "u\treplicate partition table on new device\n"; 985cf200d32Sopenharmony_ci cout << "v\tverify disk\n"; 986cf200d32Sopenharmony_ci cout << "w\twrite table to disk and exit\n"; 987cf200d32Sopenharmony_ci cout << "z\tzap (destroy) GPT data structures and exit\n"; 988cf200d32Sopenharmony_ci cout << "?\tprint this menu\n"; 989cf200d32Sopenharmony_ci} // GPTDataTextUI::ShowExpertCommands() 990cf200d32Sopenharmony_ci 991cf200d32Sopenharmony_ci 992cf200d32Sopenharmony_ci 993cf200d32Sopenharmony_ci/******************************** 994cf200d32Sopenharmony_ci * * 995cf200d32Sopenharmony_ci * Non-class support functions. * 996cf200d32Sopenharmony_ci * * 997cf200d32Sopenharmony_ci ********************************/ 998cf200d32Sopenharmony_ci 999cf200d32Sopenharmony_ci// GetMBRTypeCode() doesn't really belong in the class, since it's MBR- 1000cf200d32Sopenharmony_ci// specific, but it's also user I/O-related, so I want to keep it in 1001cf200d32Sopenharmony_ci// this file.... 1002cf200d32Sopenharmony_ci 1003cf200d32Sopenharmony_ci// Get an MBR type code from the user and return it 1004cf200d32Sopenharmony_ciint GetMBRTypeCode(int defType) { 1005cf200d32Sopenharmony_ci string line; 1006cf200d32Sopenharmony_ci int typeCode; 1007cf200d32Sopenharmony_ci 1008cf200d32Sopenharmony_ci cout.setf(ios::uppercase); 1009cf200d32Sopenharmony_ci cout.fill('0'); 1010cf200d32Sopenharmony_ci do { 1011cf200d32Sopenharmony_ci cout << "Enter an MBR hex code (default " << hex; 1012cf200d32Sopenharmony_ci cout.width(2); 1013cf200d32Sopenharmony_ci cout << defType << "): " << dec; 1014cf200d32Sopenharmony_ci line = ReadString(); 1015cf200d32Sopenharmony_ci if (line[0] == '\0') 1016cf200d32Sopenharmony_ci typeCode = defType; 1017cf200d32Sopenharmony_ci else 1018cf200d32Sopenharmony_ci typeCode = StrToHex(line, 0); 1019cf200d32Sopenharmony_ci } while ((typeCode <= 0) || (typeCode > 255)); 1020cf200d32Sopenharmony_ci cout.fill(' '); 1021cf200d32Sopenharmony_ci return typeCode; 1022cf200d32Sopenharmony_ci} // GetMBRTypeCode 1023cf200d32Sopenharmony_ci 1024cf200d32Sopenharmony_ci#ifdef USE_UTF16 1025cf200d32Sopenharmony_ci// Note: ReadUString() is here rather than in support.cc so that the ICU 1026cf200d32Sopenharmony_ci// libraries need not be linked to fixparts. 1027cf200d32Sopenharmony_ci 1028cf200d32Sopenharmony_ci// Reads a Unicode string from stdin, returning it as an ICU-style string. 1029cf200d32Sopenharmony_ci// Note that the returned string will NOT include the carriage return 1030cf200d32Sopenharmony_ci// entered by the user. Relies on the ICU constructor from a string 1031cf200d32Sopenharmony_ci// encoded in the current codepage to work. 1032cf200d32Sopenharmony_ciUnicodeString ReadUString(void) { 1033cf200d32Sopenharmony_ci return ReadString().c_str(); 1034cf200d32Sopenharmony_ci} // ReadUString() 1035cf200d32Sopenharmony_ci#endif 1036cf200d32Sopenharmony_ci 1037