blob: 55fbdbb21edb5425f202e68ad57f452fe7c4df5c [file] [log] [blame]
srs5694fad06422010-02-19 17:19:17 -05001/*
srs5694699941e2011-03-21 21:33:57 -04002 Copyright (C) 2010-2011 <Roderick W. Smith>
srs5694fad06422010-02-19 17:19:17 -05003
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>
srs569455d92612010-03-07 22:16:07 -050025#include <stdint.h>
srs569461768bc2010-07-04 01:54:00 -040026#include <limits.h>
srs5694fad06422010-02-19 17:19:17 -050027#include <iostream>
28#include <sstream>
srs56941c6f8b02010-02-21 11:09:20 -050029#include <cstdio>
srs5694fad06422010-02-19 17:19:17 -050030#include "attributes.h"
31#include "gpttext.h"
srs569455d92612010-03-07 22:16:07 -050032#include "support.h"
srs5694fad06422010-02-19 17:19:17 -050033
34using namespace std;
35
36/*******************************************
37* *
38* GPTDataText class and related structures *
39* *
40********************************************/
41
42GPTDataTextUI::GPTDataTextUI(void) : GPTData() {
43} // default constructor
44
45GPTDataTextUI::GPTDataTextUI(string filename) : GPTData(filename) {
46} // constructor passing filename
47
48GPTDataTextUI::~GPTDataTextUI(void) {
49} // default destructor
50
51/*********************************************************************
52 * *
53 * The following functions are extended (interactive) versions of *
54 * simpler functions in the base class.... *
55 * *
56 *********************************************************************/
57
58// Overridden function; calls base-class function and then makes
59// additional queries of the user, if the base-class function can't
60// decide what to do.
61WhichToUse GPTDataTextUI::UseWhichPartitions(void) {
62 WhichToUse which;
63 MBRValidity mbrState;
64 int answer;
65
66 which = GPTData::UseWhichPartitions();
67 if ((which != use_abort) || beQuiet)
68 return which;
69
70 // If we get past here, it means that the non-interactive tests were
71 // inconclusive, so we must ask the user which table to use....
72 mbrState = protectiveMBR.GetValidity();
73
74 if ((state == gpt_valid) && (mbrState == mbr)) {
75 cout << "Found valid MBR and GPT. Which do you want to use?\n";
76 answer = GetNumber(1, 3, 2, " 1 - MBR\n 2 - GPT\n 3 - Create blank GPT\n\nYour answer: ");
77 if (answer == 1) {
78 which = use_mbr;
79 } else if (answer == 2) {
80 which = use_gpt;
81 cout << "Using GPT and creating fresh protective MBR.\n";
82 } else which = use_new;
83 } // if
84
85 // Nasty decisions here -- GPT is present, but corrupt (bad CRCs or other
86 // problems)
87 if (state == gpt_corrupt) {
88 if ((mbrState == mbr) || (mbrState == hybrid)) {
89 cout << "Found valid MBR and corrupt GPT. Which do you want to use? (Using the\n"
90 << "GPT MAY permit recovery of GPT data.)\n";
91 answer = GetNumber(1, 3, 2, " 1 - MBR\n 2 - GPT\n 3 - Create blank GPT\n\nYour answer: ");
92 if (answer == 1) {
93 which = use_mbr;
94 } else if (answer == 2) {
95 which = use_gpt;
96 } else which = use_new;
97 } else if (mbrState == invalid) {
98 cout << "Found invalid MBR and corrupt GPT. What do you want to do? (Using the\n"
99 << "GPT MAY permit recovery of GPT data.)\n";
srs56940283dae2010-04-28 16:44:34 -0400100 answer = GetNumber(1, 2, 1, " 1 - Use current GPT\n 2 - Create blank GPT\n\nYour answer: ");
srs5694fad06422010-02-19 17:19:17 -0500101 if (answer == 1) {
102 which = use_gpt;
103 } else which = use_new;
104 } // if/else/else
105 } // if (corrupt GPT)
106
107 return which;
108} // UseWhichPartitions()
109
110// Ask the user for a partition number; and prompt for verification
111// if the requested partition isn't of a known BSD type.
112// Lets the base-class function do the work, and returns its value (the
113// number of converted partitions).
114int GPTDataTextUI::XFormDisklabel(void) {
115 uint32_t partNum;
116 uint16_t hexCode;
117 int goOn = 1, numDone = 0;
118 BSDData disklabel;
119
120 partNum = GetPartNum();
121
122 // Now see if the specified partition has a BSD type code....
123 hexCode = partitions[partNum].GetHexType();
124 if ((hexCode != 0xa500) && (hexCode != 0xa900)) {
125 cout << "Specified partition doesn't have a disklabel partition type "
126 << "code.\nContinue anyway? ";
127 goOn = (GetYN() == 'Y');
128 } // if
129
130 if (goOn)
131 numDone = GPTData::XFormDisklabel(partNum);
132
133 return numDone;
134} // GPTData::XFormDisklabel(int i)
135
srs569455d92612010-03-07 22:16:07 -0500136
srs5694fad06422010-02-19 17:19:17 -0500137/*********************************************************************
138 * *
139 * Begin functions that obtain information from the users, and often *
140 * do something with that information (call other functions) *
141 * *
142 *********************************************************************/
143
144// Prompts user for partition number and returns the result.
145uint32_t GPTDataTextUI::GetPartNum(void) {
146 uint32_t partNum;
147 uint32_t low, high;
148 ostringstream prompt;
149
150 if (GetPartRange(&low, &high) > 0) {
151 prompt << "Partition number (" << low + 1 << "-" << high + 1 << "): ";
152 partNum = GetNumber(low + 1, high + 1, low, prompt.str());
153 } else partNum = 1;
154 return (partNum - 1);
155} // GPTDataTextUI::GetPartNum()
156
157// What it says: Resize the partition table. (Default is 128 entries.)
158void GPTDataTextUI::ResizePartitionTable(void) {
159 int newSize;
160 ostringstream prompt;
161 uint32_t curLow, curHigh;
162
srs56940283dae2010-04-28 16:44:34 -0400163 cout << "Current partition table size is " << numParts << ".\n";
srs5694fad06422010-02-19 17:19:17 -0500164 GetPartRange(&curLow, &curHigh);
165 curHigh++; // since GetPartRange() returns numbers starting from 0...
166 // There's no point in having fewer than four partitions....
167 if (curHigh < (blockSize / GPT_SIZE))
168 curHigh = blockSize / GPT_SIZE;
169 prompt << "Enter new size (" << curHigh << " up, default " << NUM_GPT_ENTRIES << "): ";
170 newSize = GetNumber(4, 65535, 128, prompt.str());
171 if (newSize < 128) {
172 cout << "Caution: The partition table size should officially be 16KB or larger,\n"
173 << "which works out to 128 entries. In practice, smaller tables seem to\n"
174 << "work with most OSes, but this practice is risky. I'm proceeding with\n"
175 << "the resize, but you may want to reconsider this action and undo it.\n\n";
176 } // if
177 SetGPTSize(newSize);
178} // GPTDataTextUI::ResizePartitionTable()
179
180// Interactively create a partition
181void GPTDataTextUI::CreatePartition(void) {
srs56945a081752010-09-24 20:39:41 -0400182 uint64_t firstBlock, firstInLargest, lastBlock, sector, origSector;
srs5694fad06422010-02-19 17:19:17 -0500183 uint32_t firstFreePart = 0;
184 ostringstream prompt1, prompt2, prompt3;
185 int partNum;
186
187 // Find first free partition...
188 while (partitions[firstFreePart].GetFirstLBA() != 0) {
189 firstFreePart++;
190 } // while
191
192 if (((firstBlock = FindFirstAvailable()) != 0) &&
srs56940283dae2010-04-28 16:44:34 -0400193 (firstFreePart < numParts)) {
srs5694fad06422010-02-19 17:19:17 -0500194 lastBlock = FindLastAvailable();
srs569455d92612010-03-07 22:16:07 -0500195 firstInLargest = FindFirstInLargest();
196
197 // Get partition number....
198 do {
srs56940283dae2010-04-28 16:44:34 -0400199 prompt1 << "Partition number (" << firstFreePart + 1 << "-" << numParts
srs569455d92612010-03-07 22:16:07 -0500200 << ", default " << firstFreePart + 1 << "): ";
srs56940283dae2010-04-28 16:44:34 -0400201 partNum = GetNumber(firstFreePart + 1, numParts,
srs569455d92612010-03-07 22:16:07 -0500202 firstFreePart + 1, prompt1.str()) - 1;
203 if (partitions[partNum].GetFirstLBA() != 0)
204 cout << "partition " << partNum + 1 << " is in use.\n";
205 } while (partitions[partNum].GetFirstLBA() != 0);
206
207 // Get first block for new partition...
208 prompt2 << "First sector (" << firstBlock << "-" << lastBlock << ", default = "
srs56940873e9d2010-10-07 13:00:45 -0400209 << firstInLargest << ") or {+-}size{KMGTP}: ";
srs569455d92612010-03-07 22:16:07 -0500210 do {
srs56940873e9d2010-10-07 13:00:45 -0400211 sector = GetSectorNum(firstBlock, lastBlock, firstInLargest, blockSize, prompt2.str());
srs569455d92612010-03-07 22:16:07 -0500212 } while (IsFree(sector) == 0);
srs56945a081752010-09-24 20:39:41 -0400213 origSector = sector;
214 if (Align(&sector)) {
215 cout << "Information: Moved requested sector from " << origSector << " to "
216 << sector << " in\norder to align on " << sectorAlignment
217 << "-sector boundaries.\n";
218 if (!beQuiet)
219 cout << "Use 'l' on the experts' menu to adjust alignment\n";
220 } // if
221 // Align(&sector); // Align sector to correct multiple
srs569455d92612010-03-07 22:16:07 -0500222 firstBlock = sector;
223
224 // Get last block for new partitions...
225 lastBlock = FindLastInFree(firstBlock);
226 prompt3 << "Last sector (" << firstBlock << "-" << lastBlock << ", default = "
srs56940873e9d2010-10-07 13:00:45 -0400227 << lastBlock << ") or {+-}size{KMGTP}: ";
srs569455d92612010-03-07 22:16:07 -0500228 do {
srs56940873e9d2010-10-07 13:00:45 -0400229 sector = GetSectorNum(firstBlock, lastBlock, lastBlock, blockSize, prompt3.str());
srs569455d92612010-03-07 22:16:07 -0500230 } while (IsFree(sector) == 0);
231 lastBlock = sector;
232
233 firstFreePart = GPTData::CreatePartition(partNum, firstBlock, lastBlock);
234 partitions[partNum].ChangeType();
235 partitions[partNum].SetDefaultDescription();
srs5694fad06422010-02-19 17:19:17 -0500236 } else {
srs5694bf8950c2011-03-12 01:23:12 -0500237 if (firstFreePart >= numParts)
238 cout << "No table partition entries left\n";
239 else
240 cout << "No free sectors available\n";
srs5694fad06422010-02-19 17:19:17 -0500241 } // if/else
242} // GPTDataTextUI::CreatePartition()
243
244// Interactively delete a partition (duh!)
245void GPTDataTextUI::DeletePartition(void) {
246 int partNum;
247 uint32_t low, high;
248 ostringstream prompt;
249
250 if (GetPartRange(&low, &high) > 0) {
251 prompt << "Partition number (" << low + 1 << "-" << high + 1 << "): ";
252 partNum = GetNumber(low + 1, high + 1, low, prompt.str());
253 GPTData::DeletePartition(partNum - 1);
254 } else {
255 cout << "No partitions\n";
256 } // if/else
257} // GPTDataTextUI::DeletePartition()
258
259// Prompt user for a partition number, then change its type code
260// using ChangeGPTType(struct GPTPartition*) function.
261void GPTDataTextUI::ChangePartType(void) {
262 int partNum;
263 uint32_t low, high;
srs569455d92612010-03-07 22:16:07 -0500264
srs5694fad06422010-02-19 17:19:17 -0500265 if (GetPartRange(&low, &high) > 0) {
266 partNum = GetPartNum();
267 partitions[partNum].ChangeType();
268 } else {
269 cout << "No partitions\n";
270 } // if/else
271} // GPTDataTextUI::ChangePartType()
272
srs5694815fb652011-03-18 12:35:56 -0400273// Prompt user for a partition number, then change its unique
274// GUID.
275void GPTDataTextUI::ChangeUniqueGuid(void) {
276 int partNum;
277 uint32_t low, high;
278 string guidStr;
279
280 if (GetPartRange(&low, &high) > 0) {
281 partNum = GetPartNum();
282 cout << "Enter the partition's new unique GUID ('R' to randomize): ";
283 guidStr = ReadString();
284 if ((guidStr.length() >= 32) || (guidStr[0] == 'R') || (guidStr[0] == 'r')) {
285 SetPartitionGUID(partNum, (GUIDData) guidStr);
286 cout << "New GUID is " << partitions[partNum].GetUniqueGUID() << "\n";
287 } else {
288 cout << "GUID is too short!\n";
289 } // if/else
290 } else
291 cout << "No partitions\n";
292} // GPTDataTextUI::ChangeUniqueGuid()
293
srs5694fad06422010-02-19 17:19:17 -0500294// Partition attributes seem to be rarely used, but I want a way to
295// adjust them for completeness....
296void GPTDataTextUI::SetAttributes(uint32_t partNum) {
srs56940873e9d2010-10-07 13:00:45 -0400297 partitions[partNum].SetAttributes();
srs5694fad06422010-02-19 17:19:17 -0500298} // GPTDataTextUI::SetAttributes()
299
srs5694699941e2011-03-21 21:33:57 -0400300// Prompts the user for a partition name and sets the partition's
301// name. Returns 1 on success, 0 on failure (invalid partition
302// number). (Note that the function skips prompting when an
303// invalid partition number is detected.)
304int GPTDataTextUI::SetName(uint32_t partNum) {
305 UnicodeString theName = "";
306 int retval = 1;
307
308 if (IsUsedPartNum(partNum)) {
309 cout << "Enter name: ";
310 theName = ReadUString();
311 partitions[partNum].SetName(theName);
312 } else {
313 cerr << "Invalid partition number (" << partNum << ")\n";
314 retval = 0;
315 } // if/else
316
317 return retval;
318} // GPTDataTextUI::SetName()
319
srs5694fad06422010-02-19 17:19:17 -0500320// Ask user for two partition numbers and swap them in the table. Note that
321// this just reorders table entries; it doesn't adjust partition layout on
322// the disk.
323// Returns 1 if successful, 0 if not. (If user enters identical numbers, it
324// counts as successful.)
325int GPTDataTextUI::SwapPartitions(void) {
326 int partNum1, partNum2, didIt = 0;
327 uint32_t low, high;
328 ostringstream prompt;
329 GPTPart temp;
330
331 if (GetPartRange(&low, &high) > 0) {
332 partNum1 = GetPartNum();
srs56940283dae2010-04-28 16:44:34 -0400333 if (high >= numParts - 1)
srs5694fad06422010-02-19 17:19:17 -0500334 high = 0;
srs56940283dae2010-04-28 16:44:34 -0400335 prompt << "New partition number (1-" << numParts
srs569455d92612010-03-07 22:16:07 -0500336 << ", default " << high + 2 << "): ";
srs56940283dae2010-04-28 16:44:34 -0400337 partNum2 = GetNumber(1, numParts, high + 2, prompt.str()) - 1;
srs5694fad06422010-02-19 17:19:17 -0500338 didIt = GPTData::SwapPartitions(partNum1, partNum2);
339 } else {
340 cout << "No partitions\n";
341 } // if/else
342 return didIt;
343} // GPTDataTextUI::SwapPartitionNumbers()
344
345// This function destroys the on-disk GPT structures. Returns 1 if the user
346// confirms destruction, 0 if the user aborts or if there's a disk error.
347int GPTDataTextUI::DestroyGPTwPrompt(void) {
348 int allOK = 1;
349
350 if ((apmFound) || (bsdFound)) {
351 cout << "WARNING: APM or BSD disklabel structures detected! This operation could\n"
352 << "damage any APM or BSD partitions on this disk!\n";
353 } // if APM or BSD
354 cout << "\a\aAbout to wipe out GPT on " << device << ". Proceed? ";
355 if (GetYN() == 'Y') {
356 if (DestroyGPT()) {
357 // Note on below: Touch the MBR only if the user wants it completely
358 // blanked out. Version 0.4.2 deleted the 0xEE partition and re-wrote
359 // the MBR, but this could wipe out a valid MBR that the program
360 // had subsequently discarded (say, if it conflicted with older GPT
361 // structures).
362 cout << "Blank out MBR? ";
363 if (GetYN() == 'Y') {
364 DestroyMBR();
365 } else {
366 cout << "MBR is unchanged. You may need to delete an EFI GPT (0xEE) partition\n"
367 << "with fdisk or another tool.\n";
368 } // if/else
369 } else allOK = 0; // if GPT structures destroyed
370 } else allOK = 0; // if user confirms destruction
371 return (allOK);
372} // GPTDataTextUI::DestroyGPTwPrompt()
373
374// Get partition number from user and then call ShowPartDetails(partNum)
375// to show its detailed information
376void GPTDataTextUI::ShowDetails(void) {
377 int partNum;
378 uint32_t low, high;
srs569455d92612010-03-07 22:16:07 -0500379
srs5694fad06422010-02-19 17:19:17 -0500380 if (GetPartRange(&low, &high) > 0) {
381 partNum = GetPartNum();
382 ShowPartDetails(partNum);
383 } else {
384 cout << "No partitions\n";
385 } // if/else
386} // GPTDataTextUI::ShowDetails()
387
388// Create a hybrid MBR -- an ugly, funky thing that helps GPT work with
389// OSes that don't understand GPT.
390void GPTDataTextUI::MakeHybrid(void) {
391 uint32_t partNums[3];
srs56945a608532011-03-17 13:53:01 -0400392 string line;
srs5694bf8950c2011-03-12 01:23:12 -0500393 int numPartsToCvt, i, j, mbrNum = 0;
srs569455d92612010-03-07 22:16:07 -0500394 unsigned int hexCode = 0;
srs5694bf8950c2011-03-12 01:23:12 -0500395 MBRPart hybridPart;
396 MBRData hybridMBR;
srs5694fad06422010-02-19 17:19:17 -0500397 char eeFirst = 'Y'; // Whether EFI GPT (0xEE) partition comes first in table
398
srs5694058d4a52010-10-12 12:42:47 -0400399 cout << "\nWARNING! Hybrid MBRs are flaky and dangerous! If you decide not to use one,\n"
400 << "just hit the Enter key at the below prompt and your MBR partition table will\n"
401 << "be untouched.\n\n\a";
srs5694fad06422010-02-19 17:19:17 -0500402
srs5694bf8950c2011-03-12 01:23:12 -0500403 hybridMBR.SetDisk(&myDisk);
srs5694fad06422010-02-19 17:19:17 -0500404 // Now get the numbers of up to three partitions to add to the
405 // hybrid MBR....
406 cout << "Type from one to three GPT partition numbers, separated by spaces, to be\n"
407 << "added to the hybrid MBR, in sequence: ";
srs56945a608532011-03-17 13:53:01 -0400408 line = ReadString();
409 numPartsToCvt = sscanf(line.c_str(), "%d %d %d", &partNums[0], &partNums[1], &partNums[2]);
srs5694fad06422010-02-19 17:19:17 -0500410
srs56940283dae2010-04-28 16:44:34 -0400411 if (numPartsToCvt > 0) {
srs5694fad06422010-02-19 17:19:17 -0500412 cout << "Place EFI GPT (0xEE) partition first in MBR (good for GRUB)? ";
413 eeFirst = GetYN();
414 } // if
415
srs56940283dae2010-04-28 16:44:34 -0400416 for (i = 0; i < numPartsToCvt; i++) {
srs5694bf8950c2011-03-12 01:23:12 -0500417 j = partNums[i] - 1;
srs5694058d4a52010-10-12 12:42:47 -0400418 if (partitions[j].IsUsed()) {
419 mbrNum = i + (eeFirst == 'Y');
420 cout << "\nCreating entry for GPT partition #" << j + 1
421 << " (MBR partition #" << mbrNum + 1 << ")\n";
srs5694bf8950c2011-03-12 01:23:12 -0500422 hybridPart.SetType(GetMBRTypeCode(partitions[j].GetHexType() / 256));
423 hybridPart.SetLocation(partitions[j].GetFirstLBA(), partitions[j].GetLengthLBA());
424 hybridPart.SetInclusion(PRIMARY);
srs5694058d4a52010-10-12 12:42:47 -0400425 cout << "Set the bootable flag? ";
426 if (GetYN() == 'Y')
srs5694bf8950c2011-03-12 01:23:12 -0500427 hybridPart.SetStatus(1);
srs5694058d4a52010-10-12 12:42:47 -0400428 else
srs5694bf8950c2011-03-12 01:23:12 -0500429 hybridPart.SetStatus(0);
430 hybridPart.SetInclusion(PRIMARY);
srs5694058d4a52010-10-12 12:42:47 -0400431 } else {
srs5694058d4a52010-10-12 12:42:47 -0400432 cerr << "\nGPT partition #" << j + 1 << " does not exist; skipping.\n";
433 } // if/else
srs5694bf8950c2011-03-12 01:23:12 -0500434 hybridMBR.AddPart(mbrNum, hybridPart);
srs5694fad06422010-02-19 17:19:17 -0500435 } // for
436
srs56940283dae2010-04-28 16:44:34 -0400437 if (numPartsToCvt > 0) { // User opted to create a hybrid MBR....
srs5694fad06422010-02-19 17:19:17 -0500438 // Create EFI protective partition that covers the start of the disk.
439 // If this location (covering the main GPT data structures) is omitted,
440 // Linux won't find any partitions on the disk.
srs5694bf8950c2011-03-12 01:23:12 -0500441 hybridPart.SetLocation(1, hybridMBR.FindLastInFree(1));
442 hybridPart.SetStatus(0);
443 hybridPart.SetType(0xEE);
444 hybridPart.SetInclusion(PRIMARY);
srs569455d92612010-03-07 22:16:07 -0500445 // newNote firstLBA and lastLBA are computed later...
446 if (eeFirst == 'Y') {
srs5694bf8950c2011-03-12 01:23:12 -0500447 hybridMBR.AddPart(0, hybridPart);
srs569455d92612010-03-07 22:16:07 -0500448 } else {
srs5694bf8950c2011-03-12 01:23:12 -0500449 hybridMBR.AddPart(3, hybridPart);
srs569455d92612010-03-07 22:16:07 -0500450 } // else
srs5694bf8950c2011-03-12 01:23:12 -0500451 hybridMBR.SetHybrid();
srs5694fad06422010-02-19 17:19:17 -0500452
453 // ... and for good measure, if there are any partition spaces left,
454 // optionally create another protective EFI partition to cover as much
455 // space as possible....
srs5694bf8950c2011-03-12 01:23:12 -0500456 if (hybridMBR.CountParts() < 4) { // unused entry....
srs569455d92612010-03-07 22:16:07 -0500457 cout << "\nUnused partition space(s) found. Use one to protect more partitions? ";
458 if (GetYN() == 'Y') {
srs56945a608532011-03-17 13:53:01 -0400459 cout << "Note: Default is 0xEE, but this may confuse Mac OS X.\n";
460 // Comment on above: Mac OS treats disks with more than one
461 // 0xEE MBR partition as MBR disks, not as GPT disks.
462 hexCode = GetMBRTypeCode(0xEE);
463 hybridMBR.MakeBiggestPart(3, hexCode);
srs569455d92612010-03-07 22:16:07 -0500464 } // if (GetYN() == 'Y')
465 } // if unused entry
srs5694bf8950c2011-03-12 01:23:12 -0500466 protectiveMBR = hybridMBR;
srs56940283dae2010-04-28 16:44:34 -0400467 } // if (numPartsToCvt > 0)
srs5694fad06422010-02-19 17:19:17 -0500468} // GPTDataTextUI::MakeHybrid()
469
srs569455d92612010-03-07 22:16:07 -0500470// Convert the GPT to MBR form, storing partitions in the protectiveMBR
471// variable. This function is necessarily limited; it may not be able to
472// convert all partitions, depending on the disk size and available space
473// before each partition (one free sector is required to create a logical
474// partition, which are necessary to convert more than four partitions).
475// Returns the number of converted partitions; if this value
srs5694fad06422010-02-19 17:19:17 -0500476// is over 0, the calling function should call DestroyGPT() to destroy
srs569455d92612010-03-07 22:16:07 -0500477// the GPT data, call SaveMBR() to save the MBR, and then exit.
srs5694fad06422010-02-19 17:19:17 -0500478int GPTDataTextUI::XFormToMBR(void) {
srs569455d92612010-03-07 22:16:07 -0500479 uint32_t i;
srs5694fad06422010-02-19 17:19:17 -0500480
srs5694bf8950c2011-03-12 01:23:12 -0500481 protectiveMBR.EmptyMBR();
482 for (i = 0; i < numParts; i++) {
483 if (partitions[i].IsUsed()) {
484 protectiveMBR.MakePart(i, partitions[i].GetFirstLBA(),
485 partitions[i].GetLengthLBA(),
486 partitions[i].GetHexType() / 0x0100, 0);
487 } // if
488 } // for
489 protectiveMBR.MakeItLegal();
490 return protectiveMBR.DoMenu();
srs5694fad06422010-02-19 17:19:17 -0500491} // GPTDataTextUI::XFormToMBR()
492
srs5694699941e2011-03-21 21:33:57 -0400493/********************************
494 * *
495 * Non-class support functions. *
496 * *
497 ********************************/
498
499// GetMBRTypeCode() doesn't really belong in the class, since it's MBR-
500// specific, but it's also user I/O-related, so I want to keep it in
501// this file....
srs5694fad06422010-02-19 17:19:17 -0500502
503// Get an MBR type code from the user and return it
504int GetMBRTypeCode(int defType) {
srs56945a608532011-03-17 13:53:01 -0400505 string line;
srs5694fad06422010-02-19 17:19:17 -0500506 int typeCode;
507
508 cout.setf(ios::uppercase);
509 cout.fill('0');
510 do {
511 cout << "Enter an MBR hex code (default " << hex;
512 cout.width(2);
513 cout << defType << "): " << dec;
srs56945a608532011-03-17 13:53:01 -0400514 line = ReadString();
515 if (line[0] == '\0')
srs5694fad06422010-02-19 17:19:17 -0500516 typeCode = defType;
517 else
srs56945a608532011-03-17 13:53:01 -0400518 typeCode = StrToHex(line, 0);
srs5694fad06422010-02-19 17:19:17 -0500519 } while ((typeCode <= 0) || (typeCode > 255));
520 cout.fill(' ');
521 return typeCode;
srs56941c6f8b02010-02-21 11:09:20 -0500522} // GetMBRTypeCode
srs5694699941e2011-03-21 21:33:57 -0400523
524// Note: ReadUString() is here rather than in support.cc so that the ICU
525// libraries need not be linked to fixparts.
526
527// Reads a Unicode string from stdin, returning it as an ICU-style string.
528// Note that the returned string will NOT include the carriage return
529// entered by the user. Relies on the ICU constructor from a string
530// encoded in the current codepage to work.
531UnicodeString ReadUString(void) {
532 return ReadString().c_str();
533} // ReadUString()
534