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