blob: 12ec92bc8d0265751f8562fd47d348a7f4ce534b [file] [log] [blame]
srs5694f2efa7d2011-03-01 22:03:26 -05001/* basicmbr.cc -- Functions for loading, saving, and manipulating legacy MBR partition
2 data. */
3
4/* Initial coding by Rod Smith, January to February, 2009 */
5
srs5694bf8950c2011-03-12 01:23:12 -05006/* This program is copyright (c) 2009-2011 by Roderick W. Smith. It is distributed
srs5694f2efa7d2011-03-01 22:03:26 -05007 under the terms of the GNU GPL version 2, as detailed in the COPYING file. */
8
9#define __STDC_LIMIT_MACROS
10#define __STDC_CONSTANT_MACROS
11
12#include <stdio.h>
13#include <stdlib.h>
14#include <stdint.h>
15#include <fcntl.h>
16#include <string.h>
17#include <time.h>
18#include <sys/stat.h>
19#include <errno.h>
20#include <iostream>
srs5694c2f6e0c2011-03-16 02:42:33 -040021#include <algorithm>
srs5694f2efa7d2011-03-01 22:03:26 -050022#include "mbr.h"
srs5694f2efa7d2011-03-01 22:03:26 -050023#include "support.h"
24
25using namespace std;
26
27/****************************************
28 * *
29 * MBRData class and related structures *
30 * *
31 ****************************************/
32
33BasicMBRData::BasicMBRData(void) {
34 blockSize = SECTOR_SIZE;
35 diskSize = 0;
36 device = "";
37 state = invalid;
srs5694f2efa7d2011-03-01 22:03:26 -050038 numHeads = MAX_HEADS;
39 numSecspTrack = MAX_SECSPERTRACK;
40 myDisk = NULL;
41 canDeleteMyDisk = 0;
42 EmptyMBR();
43} // BasicMBRData default constructor
44
45BasicMBRData::BasicMBRData(string filename) {
46 blockSize = SECTOR_SIZE;
47 diskSize = 0;
48 device = filename;
49 state = invalid;
50 numHeads = MAX_HEADS;
51 numSecspTrack = MAX_SECSPERTRACK;
52 myDisk = NULL;
53 canDeleteMyDisk = 0;
54
srs5694f2efa7d2011-03-01 22:03:26 -050055 // Try to read the specified partition table, but if it fails....
56 if (!ReadMBRData(filename)) {
57 EmptyMBR();
58 device = "";
59 } // if
60} // BasicMBRData(string filename) constructor
61
62// Free space used by myDisk only if that's OK -- sometimes it will be
63// copied from an outside source, in which case that source should handle
64// it!
65BasicMBRData::~BasicMBRData(void) {
66 if (canDeleteMyDisk)
67 delete myDisk;
68} // BasicMBRData destructor
69
70// Assignment operator -- copy entire set of MBR data.
71BasicMBRData & BasicMBRData::operator=(const BasicMBRData & orig) {
72 int i;
73
srs5694bf8950c2011-03-12 01:23:12 -050074 memcpy(code, orig.code, 440);
srs5694f2efa7d2011-03-01 22:03:26 -050075 diskSignature = orig.diskSignature;
76 nulls = orig.nulls;
77 MBRSignature = orig.MBRSignature;
78 blockSize = orig.blockSize;
79 diskSize = orig.diskSize;
80 numHeads = orig.numHeads;
81 numSecspTrack = orig.numSecspTrack;
82 canDeleteMyDisk = orig.canDeleteMyDisk;
83 device = orig.device;
84 state = orig.state;
85
86 myDisk = new DiskIO;
srs5694bf8950c2011-03-12 01:23:12 -050087 if (orig.myDisk != NULL)
88 myDisk->OpenForRead(orig.myDisk->GetName());
srs5694f2efa7d2011-03-01 22:03:26 -050089
90 for (i = 0; i < MAX_MBR_PARTS; i++) {
91 partitions[i] = orig.partitions[i];
92 } // for
93 return *this;
94} // BasicMBRData::operator=()
95
96/**********************
97 * *
98 * Disk I/O functions *
99 * *
100 **********************/
101
102// Read data from MBR. Returns 1 if read was successful (even if the
103// data isn't a valid MBR), 0 if the read failed.
104int BasicMBRData::ReadMBRData(const string & deviceFilename) {
105 int allOK = 1;
106
107 if (myDisk == NULL) {
108 myDisk = new DiskIO;
109 canDeleteMyDisk = 1;
110 } // if
111 if (myDisk->OpenForRead(deviceFilename)) {
112 allOK = ReadMBRData(myDisk);
113 } else {
114 allOK = 0;
115 } // if
116
117 if (allOK)
118 device = deviceFilename;
119
120 return allOK;
121} // BasicMBRData::ReadMBRData(const string & deviceFilename)
122
123// Read data from MBR. If checkBlockSize == 1 (the default), the block
124// size is checked; otherwise it's set to the default (512 bytes).
125// Note that any extended partition(s) present will be explicitly stored
126// in the partitions[] array, along with their contained partitions; the
127// extended container partition(s) should be ignored by other functions.
128int BasicMBRData::ReadMBRData(DiskIO * theDisk, int checkBlockSize) {
srs5694bf8950c2011-03-12 01:23:12 -0500129 int allOK = 1, i, logicalNum = 0;
srs5694f2efa7d2011-03-01 22:03:26 -0500130 int err = 1;
131 TempMBR tempMBR;
132
srs5694bf8950c2011-03-12 01:23:12 -0500133 if ((myDisk != NULL) && (myDisk != theDisk) && (canDeleteMyDisk)) {
srs5694f2efa7d2011-03-01 22:03:26 -0500134 delete myDisk;
135 canDeleteMyDisk = 0;
136 } // if
137
138 myDisk = theDisk;
139
140 // Empty existing MBR data, including the logical partitions...
141 EmptyMBR(0);
142
143 if (myDisk->Seek(0))
144 if (myDisk->Read(&tempMBR, 512))
145 err = 0;
146 if (err) {
147 cerr << "Problem reading disk in BasicMBRData::ReadMBRData()!\n";
148 } else {
149 for (i = 0; i < 440; i++)
150 code[i] = tempMBR.code[i];
151 diskSignature = tempMBR.diskSignature;
152 nulls = tempMBR.nulls;
153 for (i = 0; i < 4; i++) {
srs5694bf8950c2011-03-12 01:23:12 -0500154 partitions[i] = tempMBR.partitions[i];
155 if (partitions[i].GetLengthLBA() > 0)
156 partitions[i].SetInclusion(PRIMARY);
srs5694f2efa7d2011-03-01 22:03:26 -0500157 } // for i... (reading all four partitions)
158 MBRSignature = tempMBR.MBRSignature;
srs5694bf8950c2011-03-12 01:23:12 -0500159 ReadCHSGeom();
srs5694f2efa7d2011-03-01 22:03:26 -0500160
161 // Reverse the byte order, if necessary
162 if (IsLittleEndian() == 0) {
163 ReverseBytes(&diskSignature, 4);
164 ReverseBytes(&nulls, 2);
165 ReverseBytes(&MBRSignature, 2);
166 for (i = 0; i < 4; i++) {
srs5694bf8950c2011-03-12 01:23:12 -0500167 partitions[i].ReverseByteOrder();
srs5694f2efa7d2011-03-01 22:03:26 -0500168 } // for
169 } // if
170
171 if (MBRSignature != MBR_SIGNATURE) {
172 allOK = 0;
173 state = invalid;
174 } // if
175
176 // Find disk size
177 diskSize = myDisk->DiskSize(&err);
178
179 // Find block size
180 if (checkBlockSize) {
181 blockSize = myDisk->GetBlockSize();
182 } // if (checkBlockSize)
183
184 // Load logical partition data, if any is found....
185 if (allOK) {
186 for (i = 0; i < 4; i++) {
srs5694bf8950c2011-03-12 01:23:12 -0500187 if ((partitions[i].GetType() == 0x05) || (partitions[i].GetType() == 0x0f)
188 || (partitions[i].GetType() == 0x85)) {
srs5694f2efa7d2011-03-01 22:03:26 -0500189 // Found it, so call a recursive algorithm to load everything from them....
srs5694bf8950c2011-03-12 01:23:12 -0500190 logicalNum = ReadLogicalPart(partitions[i].GetStartLBA(), UINT32_C(0), 4);
srs5694f2efa7d2011-03-01 22:03:26 -0500191 if ((logicalNum < 0) || (logicalNum >= MAX_MBR_PARTS)) {
192 allOK = 0;
193 cerr << "Error reading logical partitions! List may be truncated!\n";
194 } // if maxLogicals valid
srs5694bf8950c2011-03-12 01:23:12 -0500195 DeletePartition(i);
srs5694f2efa7d2011-03-01 22:03:26 -0500196 } // if primary partition is extended
197 } // for primary partition loop
198 if (allOK) { // Loaded logicals OK
199 state = mbr;
200 } else {
201 state = invalid;
202 } // if
203 } // if
204
205 // Check to see if it's in GPT format....
206 if (allOK) {
207 for (i = 0; i < 4; i++) {
srs5694bf8950c2011-03-12 01:23:12 -0500208 if (partitions[i].GetType() == UINT8_C(0xEE)) {
srs5694f2efa7d2011-03-01 22:03:26 -0500209 state = gpt;
210 } // if
211 } // for
212 } // if
213
214 // If there's an EFI GPT partition, look for other partition types,
215 // to flag as hybrid
216 if (state == gpt) {
217 for (i = 0 ; i < 4; i++) {
srs5694bf8950c2011-03-12 01:23:12 -0500218 if ((partitions[i].GetType() != UINT8_C(0xEE)) &&
219 (partitions[i].GetType() != UINT8_C(0x00)))
srs5694f2efa7d2011-03-01 22:03:26 -0500220 state = hybrid;
srs5694bf8950c2011-03-12 01:23:12 -0500221 if (logicalNum > 0)
222 cerr << "Warning! MBR Logical partitions found on a hybrid MBR disk! This is an\n"
223 << "EXTREMELY dangerous configuration!\n\a";
srs5694f2efa7d2011-03-01 22:03:26 -0500224 } // for
225 } // if (hybrid detection code)
226 } // no initial error
227 return allOK;
228} // BasicMBRData::ReadMBRData(DiskIO * theDisk, int checkBlockSize)
229
230// This is a recursive function to read all the logical partitions, following the
231// logical partition linked list from the disk and storing the basic data in the
232// partitions[] array. Returns last index to partitions[] used, or -1 if there was
233// a problem.
234// Parameters:
235// extendedStart = LBA of the start of the extended partition
236// diskOffset = LBA offset WITHIN the extended partition of the one to be read
237// partNum = location in partitions[] array to store retrieved data
srs5694bf8950c2011-03-12 01:23:12 -0500238int BasicMBRData::ReadLogicalPart(uint64_t extendedStart, uint64_t diskOffset, int partNum) {
srs5694f2efa7d2011-03-01 22:03:26 -0500239 struct TempMBR ebr;
srs5694bf8950c2011-03-12 01:23:12 -0500240 uint8_t ebrType;
srs5694f2efa7d2011-03-01 22:03:26 -0500241 uint64_t offset;
242
243 // Check for a valid partition number. Note that partitions MAY be read into
244 // the area normally used by primary partitions, although the only calling
srs5694bf8950c2011-03-12 01:23:12 -0500245 // functions as of GPT fdisk version 0.7.0 don't do so.
srs5694f2efa7d2011-03-01 22:03:26 -0500246 if ((partNum < MAX_MBR_PARTS) && (partNum >= 0)) {
247 offset = (uint64_t) (extendedStart + diskOffset);
248 if (myDisk->Seek(offset) == 0) { // seek to EBR record
249 cerr << "Unable to seek to " << offset << "! Aborting!\n";
250 partNum = -1;
251 }
252 if (myDisk->Read(&ebr, 512) != 512) { // Load the data....
253 cerr << "Error seeking to or reading logical partition data from " << offset
254 << "!\nAborting!\n";
255 partNum = -1;
256 } else if (IsLittleEndian() != 1) { // Reverse byte ordering of some data....
257 ReverseBytes(&ebr.MBRSignature, 2);
258 ReverseBytes(&ebr.partitions[0].firstLBA, 4);
259 ReverseBytes(&ebr.partitions[0].lengthLBA, 4);
260 ReverseBytes(&ebr.partitions[1].firstLBA, 4);
261 ReverseBytes(&ebr.partitions[1].lengthLBA, 4);
262 } // if/else/if
263
264 if (ebr.MBRSignature != MBR_SIGNATURE) {
265 partNum = -1;
266 cerr << "MBR signature in logical partition invalid; read 0x";
267 cerr.fill('0');
268 cerr.width(4);
269 cerr.setf(ios::uppercase);
270 cerr << hex << ebr.MBRSignature << ", but should be 0x";
271 cerr.width(4);
272 cerr << MBR_SIGNATURE << dec << "\n";
273 cerr.fill(' ');
274 } // if
275
srs5694bf8950c2011-03-12 01:23:12 -0500276 // Sometimes an EBR points directly to another EBR, rather than defining
277 // a logical partition and then pointing to another EBR. Thus, we recurse
278 // directly if this is detected, else extract partition data and then
279 // recurse on the second entry in the EBR...
280 ebrType = ebr.partitions[0].partitionType;
281 if ((ebrType == 0x05) || (ebrType == 0x0f) || (ebrType == 0x85)) {
282 partNum = ReadLogicalPart(extendedStart, ebr.partitions[0].firstLBA, partNum);
srs5694f2efa7d2011-03-01 22:03:26 -0500283 } else {
srs5694bf8950c2011-03-12 01:23:12 -0500284 // Copy over the basic data....
285 partitions[partNum] = ebr.partitions[0];
286 // Adjust the start LBA, since it's encoded strangely....
287 partitions[partNum].SetStartLBA(ebr.partitions[0].firstLBA + diskOffset + extendedStart);
288 partitions[partNum].SetInclusion(LOGICAL);
289
290 // Find the next partition (if there is one) and recurse....
291 if ((ebr.partitions[1].firstLBA != UINT32_C(0)) && (partNum >= 4) &&
292 (partNum < (MAX_MBR_PARTS - 1))) {
293 partNum = ReadLogicalPart(extendedStart, ebr.partitions[1].firstLBA,
294 partNum + 1);
295 } else {
296 partNum++;
297 } // if another partition
298 } // if/else
srs5694f2efa7d2011-03-01 22:03:26 -0500299 } // Not enough space for all the logicals (or previous error encountered)
300 return (partNum);
301} // BasicMBRData::ReadLogicalPart()
302
303// Write the MBR data to the default defined device. This writes both the
304// MBR itself and any defined logical partitions, provided there's an
305// MBR extended partition.
306int BasicMBRData::WriteMBRData(void) {
307 int allOK = 1;
308
309 if (myDisk != NULL) {
310 if (myDisk->OpenForWrite() != 0) {
311 allOK = WriteMBRData(myDisk);
srs5694bf8950c2011-03-12 01:23:12 -0500312 cout << "Done writing data!\n";
srs5694f2efa7d2011-03-01 22:03:26 -0500313 } else {
314 allOK = 0;
315 } // if/else
316 myDisk->Close();
317 } else allOK = 0;
318 return allOK;
319} // BasicMBRData::WriteMBRData(void)
320
321// Save the MBR data to a file. This writes both the
srs5694bf8950c2011-03-12 01:23:12 -0500322// MBR itself and any defined logical partitions.
srs5694f2efa7d2011-03-01 22:03:26 -0500323int BasicMBRData::WriteMBRData(DiskIO *theDisk) {
srs5694bf8950c2011-03-12 01:23:12 -0500324 int i, j, partNum, next, allOK = 1, moreLogicals = 0;
325 uint64_t extFirstLBA = 0;
srs5694f2efa7d2011-03-01 22:03:26 -0500326 uint64_t writeEbrTo; // 64-bit because we support extended in 2-4TiB range
327 TempMBR tempMBR;
328
srs5694bf8950c2011-03-12 01:23:12 -0500329 allOK = CreateExtended();
330 if (allOK) {
331 // First write the main MBR data structure....
332 memcpy(tempMBR.code, code, 440);
333 tempMBR.diskSignature = diskSignature;
334 tempMBR.nulls = nulls;
335 tempMBR.MBRSignature = MBRSignature;
336 for (i = 0; i < 4; i++) {
337 partitions[i].StoreInStruct(&tempMBR.partitions[i]);
338 if (partitions[i].GetType() == 0x0f) {
339 extFirstLBA = partitions[i].GetStartLBA();
340 moreLogicals = 1;
341 } // if
342 } // for i...
343 } // if
344 allOK = allOK && WriteMBRData(tempMBR, theDisk, 0);
srs5694f2efa7d2011-03-01 22:03:26 -0500345
346 // Set up tempMBR with some constant data for logical partitions...
347 tempMBR.diskSignature = 0;
348 for (i = 2; i < 4; i++) {
349 tempMBR.partitions[i].firstLBA = tempMBR.partitions[i].lengthLBA = 0;
350 tempMBR.partitions[i].partitionType = 0x00;
351 for (j = 0; j < 3; j++) {
352 tempMBR.partitions[i].firstSector[j] = 0;
353 tempMBR.partitions[i].lastSector[j] = 0;
354 } // for j
355 } // for i
356
srs5694bf8950c2011-03-12 01:23:12 -0500357 partNum = FindNextInUse(4);
srs5694f2efa7d2011-03-01 22:03:26 -0500358 writeEbrTo = (uint64_t) extFirstLBA;
srs5694bf8950c2011-03-12 01:23:12 -0500359 // Write logicals...
srs5694f2efa7d2011-03-01 22:03:26 -0500360 while (allOK && moreLogicals && (partNum < MAX_MBR_PARTS)) {
srs5694bf8950c2011-03-12 01:23:12 -0500361 partitions[partNum].StoreInStruct(&tempMBR.partitions[0]);
362 tempMBR.partitions[0].firstLBA = 1;
srs5694f2efa7d2011-03-01 22:03:26 -0500363 // tempMBR.partitions[1] points to next EBR or terminates EBR linked list...
srs5694bf8950c2011-03-12 01:23:12 -0500364 next = FindNextInUse(partNum + 1);
365 if ((next < MAX_MBR_PARTS) && (next > 0) && (partitions[next].GetStartLBA() > 0)) {
srs5694f2efa7d2011-03-01 22:03:26 -0500366 tempMBR.partitions[1].partitionType = 0x0f;
srs5694bf8950c2011-03-12 01:23:12 -0500367 tempMBR.partitions[1].firstLBA = (uint32_t) (partitions[next].GetStartLBA() - extFirstLBA - 1);
368 tempMBR.partitions[1].lengthLBA = (uint32_t) (partitions[next].GetLengthLBA() + 1);
srs5694f2efa7d2011-03-01 22:03:26 -0500369 LBAtoCHS((uint64_t) tempMBR.partitions[1].firstLBA,
370 (uint8_t *) &tempMBR.partitions[1].firstSector);
371 LBAtoCHS(tempMBR.partitions[1].lengthLBA - extFirstLBA,
372 (uint8_t *) &tempMBR.partitions[1].lastSector);
373 } else {
374 tempMBR.partitions[1].partitionType = 0x00;
375 tempMBR.partitions[1].firstLBA = 0;
376 tempMBR.partitions[1].lengthLBA = 0;
377 moreLogicals = 0;
378 } // if/else
379 allOK = WriteMBRData(tempMBR, theDisk, writeEbrTo);
srs5694f2efa7d2011-03-01 22:03:26 -0500380 writeEbrTo = (uint64_t) tempMBR.partitions[1].firstLBA + (uint64_t) extFirstLBA;
srs5694bf8950c2011-03-12 01:23:12 -0500381 partNum = next;
srs5694f2efa7d2011-03-01 22:03:26 -0500382 } // while
srs5694bf8950c2011-03-12 01:23:12 -0500383 DeleteExtendedParts();
srs5694f2efa7d2011-03-01 22:03:26 -0500384 return allOK;
385} // BasicMBRData::WriteMBRData(DiskIO *theDisk)
386
387int BasicMBRData::WriteMBRData(const string & deviceFilename) {
388 device = deviceFilename;
389 return WriteMBRData();
390} // BasicMBRData::WriteMBRData(const string & deviceFilename)
391
392// Write a single MBR record to the specified sector. Used by the like-named
393// function to write both the MBR and multiple EBR (for logical partition)
394// records.
395// Returns 1 on success, 0 on failure
396int BasicMBRData::WriteMBRData(struct TempMBR & mbr, DiskIO *theDisk, uint64_t sector) {
397 int i, allOK;
398
399 // Reverse the byte order, if necessary
400 if (IsLittleEndian() == 0) {
401 ReverseBytes(&mbr.diskSignature, 4);
402 ReverseBytes(&mbr.nulls, 2);
403 ReverseBytes(&mbr.MBRSignature, 2);
404 for (i = 0; i < 4; i++) {
405 ReverseBytes(&mbr.partitions[i].firstLBA, 4);
406 ReverseBytes(&mbr.partitions[i].lengthLBA, 4);
407 } // for
408 } // if
409
410 // Now write the data structure...
411 allOK = theDisk->OpenForWrite();
412 if (allOK && theDisk->Seek(sector)) {
413 if (theDisk->Write(&mbr, 512) != 512) {
414 allOK = 0;
415 cerr << "Error " << errno << " when saving MBR!\n";
416 } // if
417 } else {
418 allOK = 0;
419 cerr << "Error " << errno << " when seeking to MBR to write it!\n";
420 } // if/else
421 theDisk->Close();
422
423 // Reverse the byte order back, if necessary
424 if (IsLittleEndian() == 0) {
425 ReverseBytes(&mbr.diskSignature, 4);
426 ReverseBytes(&mbr.nulls, 2);
427 ReverseBytes(&mbr.MBRSignature, 2);
428 for (i = 0; i < 4; i++) {
429 ReverseBytes(&mbr.partitions[i].firstLBA, 4);
430 ReverseBytes(&mbr.partitions[i].lengthLBA, 4);
431 } // for
432 }// if
433 return allOK;
434} // BasicMBRData::WriteMBRData(uint64_t sector)
435
srs5694bf8950c2011-03-12 01:23:12 -0500436// Set a new disk device; used in copying one disk's partition
437// table to another disk.
438void BasicMBRData::SetDisk(DiskIO *theDisk) {
439 int err;
440
441 myDisk = theDisk;
442 diskSize = theDisk->DiskSize(&err);
443 canDeleteMyDisk = 0;
444 ReadCHSGeom();
445} // BasicMBRData::SetDisk()
446
srs5694f2efa7d2011-03-01 22:03:26 -0500447/********************************************
448 * *
449 * Functions that display data for the user *
450 * *
451 ********************************************/
452
453// Show the MBR data to the user, up to the specified maximum number
454// of partitions....
srs5694bf8950c2011-03-12 01:23:12 -0500455void BasicMBRData::DisplayMBRData(void) {
srs5694f2efa7d2011-03-01 22:03:26 -0500456 int i;
srs5694f2efa7d2011-03-01 22:03:26 -0500457
srs5694bf8950c2011-03-12 01:23:12 -0500458 cout << "\nDisk size is " << diskSize << " sectors ("
srs569401f7f082011-03-15 23:53:31 -0400459 << BytesToIeee(diskSize, blockSize) << ")\n";
srs5694f2efa7d2011-03-01 22:03:26 -0500460 cout << "MBR disk identifier: 0x";
461 cout.width(8);
462 cout.fill('0');
463 cout.setf(ios::uppercase);
464 cout << hex << diskSignature << dec << "\n";
srs5694bf8950c2011-03-12 01:23:12 -0500465 cout << "MBR partitions:\n\n";
466 if ((state == gpt) || (state == hybrid)) {
467 cout << "Number Boot Start Sector End Sector Status Code\n";
468 } else {
469 cout << " Can Be Can Be\n";
470 cout << "Number Boot Start Sector End Sector Status Logical Primary Code\n";
471 UpdateCanBeLogical();
472 } //
473 for (i = 0; i < MAX_MBR_PARTS; i++) {
474 if (partitions[i].GetLengthLBA() != 0) {
srs5694f2efa7d2011-03-01 22:03:26 -0500475 cout.fill(' ');
476 cout.width(4);
srs5694bf8950c2011-03-12 01:23:12 -0500477 cout << i + 1 << " ";
478 partitions[i].ShowData((state == gpt) || (state == hybrid));
srs5694f2efa7d2011-03-01 22:03:26 -0500479 } // if
480 cout.fill(' ');
481 } // for
srs5694f2efa7d2011-03-01 22:03:26 -0500482} // BasicMBRData::DisplayMBRData()
483
484// Displays the state, as a word, on stdout. Used for debugging & to
485// tell the user about the MBR state when the program launches....
486void BasicMBRData::ShowState(void) {
487 switch (state) {
488 case invalid:
489 cout << " MBR: not present\n";
490 break;
491 case gpt:
492 cout << " MBR: protective\n";
493 break;
494 case hybrid:
495 cout << " MBR: hybrid\n";
496 break;
497 case mbr:
498 cout << " MBR: MBR only\n";
499 break;
500 default:
501 cout << "\a MBR: unknown -- bug!\n";
502 break;
503 } // switch
504} // BasicMBRData::ShowState()
505
srs5694bf8950c2011-03-12 01:23:12 -0500506/************************
507 * *
508 * GPT Checks and fixes *
509 * *
510 ************************/
511
512// Perform a very rudimentary check for GPT data on the disk; searches for
513// the GPT signature in the main and backup metadata areas.
514// Returns 0 if GPT data not found, 1 if main data only is found, 2 if
515// backup only is found, 3 if both main and backup data are found, and
516// -1 if a disk error occurred.
517int BasicMBRData::CheckForGPT(void) {
518 int retval = 0, err;
519 char signature1[9], signature2[9];
520
521 if (myDisk != NULL) {
522 if (myDisk->OpenForRead() != 0) {
523 if (myDisk->Seek(1)) {
524 myDisk->Read(signature1, 8);
525 signature1[8] = '\0';
526 } else retval = -1;
527 if (myDisk->Seek(myDisk->DiskSize(&err) - 1)) {
528 myDisk->Read(signature2, 8);
529 signature2[8] = '\0';
530 } else retval = -1;
531 if ((retval >= 0) && (strcmp(signature1, "EFI PART") == 0))
532 retval += 1;
533 if ((retval >= 0) && (strcmp(signature2, "EFI PART") == 0))
534 retval += 2;
535 } else {
536 retval = -1;
537 } // if/else
538 myDisk->Close();
539 } else retval = -1;
540 return retval;
541} // BasicMBRData::CheckForGPT()
542
543// Blanks the 2nd (sector #1, numbered from 0) and last sectors of the disk,
544// but only if GPT data are verified on the disk, and only for the sector(s)
545// with GPT signatures.
546// Returns 1 if operation completes successfully, 0 if not (returns 1 if
547// no GPT data are found on the disk).
548int BasicMBRData::BlankGPTData(void) {
549 int allOK = 1, err;
550 uint8_t blank[512];
551
552 memset(blank, 0, 512);
553 switch (CheckForGPT()) {
554 case -1:
555 allOK = 0;
556 break;
557 case 0:
558 break;
559 case 1:
560 if ((myDisk != NULL) && (myDisk->OpenForWrite())) {
561 if (!((myDisk->Seek(1)) && (myDisk->Write(blank, 512) == 512)))
562 allOK = 0;
563 myDisk->Close();
564 } else allOK = 0;
565 break;
566 case 2:
567 if ((myDisk != NULL) && (myDisk->OpenForWrite())) {
568 if (!((myDisk->Seek(myDisk->DiskSize(&err) - 1)) &&
569 (myDisk->Write(blank, 512) == 512)))
570 allOK = 0;
571 myDisk->Close();
572 } else allOK = 0;
573 break;
574 case 3:
575 if ((myDisk != NULL) && (myDisk->OpenForWrite())) {
576 if (!((myDisk->Seek(1)) && (myDisk->Write(blank, 512) == 512)))
577 allOK = 0;
578 if (!((myDisk->Seek(myDisk->DiskSize(&err) - 1)) &&
579 (myDisk->Write(blank, 512) == 512)))
580 allOK = 0;
581 myDisk->Close();
582 } else allOK = 0;
583 break;
584 default:
585 break;
586 } // switch()
587 return allOK;
588} // BasicMBRData::BlankGPTData
589
srs5694f2efa7d2011-03-01 22:03:26 -0500590/*********************************************************************
591 * *
592 * Functions that set or get disk metadata (CHS geometry, disk size, *
593 * etc.) *
594 * *
595 *********************************************************************/
596
srs5694bf8950c2011-03-12 01:23:12 -0500597// Read the CHS geometry using OS calls, or if that fails, set to
598// the most common value for big disks (255 heads, 63 sectors per
599// track, & however many cylinders that computes to).
600void BasicMBRData::ReadCHSGeom(void) {
601 numHeads = myDisk->GetNumHeads();
602 numSecspTrack = myDisk->GetNumSecsPerTrack();
603 partitions[0].SetGeometry(numHeads, numSecspTrack, diskSize, blockSize);
604} // BasicMBRData::ReadCHSGeom()
605
606// Find the low and high used partition numbers (numbered from 0).
607// Return value is the number of partitions found. Note that the
608// *low and *high values are both set to 0 when no partitions
609// are found, as well as when a single partition in the first
610// position exists. Thus, the return value is the only way to
611// tell when no partitions exist.
612int BasicMBRData::GetPartRange(uint32_t *low, uint32_t *high) {
613 uint32_t i;
614 int numFound = 0;
615
616 *low = MAX_MBR_PARTS + 1; // code for "not found"
617 *high = 0;
618 for (i = 0; i < MAX_MBR_PARTS; i++) {
619 if (partitions[i].GetStartLBA() != UINT32_C(0)) { // it exists
620 *high = i; // since we're counting up, set the high value
621 // Set the low value only if it's not yet found...
622 if (*low == (MAX_MBR_PARTS + 1))
623 *low = i;
624 numFound++;
625 } // if
626 } // for
627
628 // Above will leave *low pointing to its "not found" value if no partitions
629 // are defined, so reset to 0 if this is the case....
630 if (*low == (MAX_MBR_PARTS + 1))
631 *low = 0;
632 return numFound;
633} // GPTData::GetPartRange()
srs5694f2efa7d2011-03-01 22:03:26 -0500634
635// Converts 64-bit LBA value to MBR-style CHS value. Returns 1 if conversion
636// was within the range that can be expressed by CHS (including 0, for an
637// empty partition), 0 if the value is outside that range, and -1 if chs is
638// invalid.
639int BasicMBRData::LBAtoCHS(uint64_t lba, uint8_t * chs) {
640 uint64_t cylinder, head, sector; // all numbered from 0
641 uint64_t remainder;
642 int retval = 1;
643 int done = 0;
644
645 if (chs != NULL) {
646 // Special case: In case of 0 LBA value, zero out CHS values....
647 if (lba == 0) {
648 chs[0] = chs[1] = chs[2] = UINT8_C(0);
649 done = 1;
650 } // if
651 // If LBA value is too large for CHS, max out CHS values....
652 if ((!done) && (lba >= (numHeads * numSecspTrack * MAX_CYLINDERS))) {
653 chs[0] = 254;
654 chs[1] = chs[2] = 255;
655 done = 1;
656 retval = 0;
657 } // if
658 // If neither of the above applies, compute CHS values....
659 if (!done) {
660 cylinder = lba / (uint64_t) (numHeads * numSecspTrack);
661 remainder = lba - (cylinder * numHeads * numSecspTrack);
662 head = remainder / numSecspTrack;
663 remainder -= head * numSecspTrack;
664 sector = remainder;
665 if (head < numHeads)
666 chs[0] = (uint8_t) head;
667 else
668 retval = 0;
669 if (sector < numSecspTrack) {
670 chs[1] = (uint8_t) ((sector + 1) + (cylinder >> 8) * 64);
671 chs[2] = (uint8_t) (cylinder & UINT64_C(0xFF));
672 } else {
673 retval = 0;
674 } // if/else
675 } // if value is expressible and non-0
676 } else { // Invalid (NULL) chs pointer
677 retval = -1;
678 } // if CHS pointer valid
679 return (retval);
680} // BasicMBRData::LBAtoCHS()
681
srs5694bf8950c2011-03-12 01:23:12 -0500682// Look for overlapping partitions.
683// Returns the number of problems found
684int BasicMBRData::FindOverlaps(void) {
685 int i, j, numProbs = 0, numEE = 0;
srs5694f2efa7d2011-03-01 22:03:26 -0500686
687 for (i = 0; i < MAX_MBR_PARTS; i++) {
688 for (j = i + 1; j < MAX_MBR_PARTS; j++) {
srs5694bf8950c2011-03-12 01:23:12 -0500689 if ((partitions[i].GetInclusion() != NONE) &&
690 (partitions[i].DoTheyOverlap(partitions[j]))) {
srs5694f2efa7d2011-03-01 22:03:26 -0500691 numProbs++;
692 cout << "\nProblem: MBR partitions " << i + 1 << " and " << j + 1
693 << " overlap!\n";
694 } // if
695 } // for (j...)
srs5694bf8950c2011-03-12 01:23:12 -0500696 if (partitions[i].GetType() == 0xEE) {
srs5694f2efa7d2011-03-01 22:03:26 -0500697 numEE++;
srs5694bf8950c2011-03-12 01:23:12 -0500698 if (partitions[i].GetStartLBA() != 1)
699 cout << "\nWarning: 0xEE partition doesn't start on sector 1. This can cause "
700 << "problems\nin some OSes.\n";
srs5694f2efa7d2011-03-01 22:03:26 -0500701 } // if
702 } // for (i...)
703 if (numEE > 1)
704 cout << "\nCaution: More than one 0xEE MBR partition found. This can cause problems\n"
705 << "in some OSes.\n";
706
707 return numProbs;
srs5694bf8950c2011-03-12 01:23:12 -0500708} // BasicMBRData::FindOverlaps()
709
710// Returns the number of primary partitions, including the extended partition
711// required to hold any logical partitions found.
712int BasicMBRData::NumPrimaries(void) {
713 int i, numPrimaries = 0, logicalsFound = 0;
714
715 for (i = 0; i < MAX_MBR_PARTS; i++) {
716 if (partitions[i].GetLengthLBA() > 0) {
717 if (partitions[i].GetInclusion() == PRIMARY)
718 numPrimaries++;
719 if (partitions[i].GetInclusion() == LOGICAL)
720 logicalsFound = 1;
721 } // if
722 } // for
723 return (numPrimaries + logicalsFound);
724} // BasicMBRData::NumPrimaries()
725
726// Returns the number of logical partitions.
727int BasicMBRData::NumLogicals(void) {
728 int i, numLogicals = 0;
729
730 for (i = 0; i < MAX_MBR_PARTS; i++) {
731 if (partitions[i].GetInclusion() == LOGICAL)
732 numLogicals++;
733 } // for
734 return numLogicals;
735} // BasicMBRData::NumLogicals()
736
737// Returns the number of partitions (primaries plus logicals), NOT including
738// the extended partition required to house the logicals.
739int BasicMBRData::CountParts(void) {
740 int i, num = 0;
741
742 for (i = 0; i < MAX_MBR_PARTS; i++) {
743 if ((partitions[i].GetInclusion() == LOGICAL) ||
744 (partitions[i].GetInclusion() == PRIMARY))
745 num++;
746 } // for
747 return num;
748} // BasicMBRData::CountParts()
749
750// Updates the canBeLogical and canBePrimary flags for all the partitions.
751void BasicMBRData::UpdateCanBeLogical(void) {
752 int i, j, sectorBefore, numPrimaries, numLogicals, usedAsEBR;
753 uint64_t firstLogical, lastLogical, lStart, pStart;
754
755 numPrimaries = NumPrimaries();
756 numLogicals = NumLogicals();
757 firstLogical = FirstLogicalLBA() - 1;
758 lastLogical = LastLogicalLBA();
759 for (i = 0; i < MAX_MBR_PARTS; i++) {
760 usedAsEBR = (SectorUsedAs(partitions[i].GetLastLBA()) == EBR);
761 if (usedAsEBR) {
762 partitions[i].SetCanBeLogical(0);
763 partitions[i].SetCanBePrimary(0);
764 } else if (partitions[i].GetLengthLBA() > 0) {
765 // First determine if it can be logical....
766 sectorBefore = SectorUsedAs(partitions[i].GetStartLBA() - 1);
767 lStart = partitions[i].GetStartLBA(); // start of potential logical part.
768 if ((lastLogical > 0) &&
769 ((sectorBefore == EBR) || (sectorBefore == NONE))) {
770 // Assume it can be logical, then search for primaries that make it
771 // not work and, if found, flag appropriately.
772 partitions[i].SetCanBeLogical(1);
773 for (j = 0; j < MAX_MBR_PARTS; j++) {
774 if ((i != j) && (partitions[j].GetInclusion() == PRIMARY)) {
775 pStart = partitions[j].GetStartLBA();
776 if (((pStart < lStart) && (firstLogical < pStart)) ||
777 ((pStart > lStart) && (firstLogical > pStart))) {
778 partitions[i].SetCanBeLogical(0);
779 } // if/else
780 } // if
781 } // for
782 } else {
783 if ((sectorBefore != EBR) && (sectorBefore != NONE))
784 partitions[i].SetCanBeLogical(0);
785 else
786 partitions[i].SetCanBeLogical(lastLogical == 0); // can be logical only if no logicals already
787 } // if/else
788 // Now determine if it can be primary. Start by assuming it can be...
789 partitions[i].SetCanBePrimary(1);
790 if ((numPrimaries >= 4) && (partitions[i].GetInclusion() != PRIMARY)) {
791 partitions[i].SetCanBePrimary(0);
792 if ((partitions[i].GetInclusion() == LOGICAL) && (numLogicals == 1) &&
793 (numPrimaries == 4))
794 partitions[i].SetCanBePrimary(1);
795 } // if
796 if ((partitions[i].GetStartLBA() > (firstLogical + 1)) &&
797 (partitions[i].GetLastLBA() < lastLogical))
798 partitions[i].SetCanBePrimary(0);
799 } // else if
800 } // for
801} // BasicMBRData::UpdateCanBeLogical()
802
803// Returns the first sector occupied by any logical partition. Note that
804// this does NOT include the logical partition's EBR! Returns UINT32_MAX
805// if there are no logical partitions defined.
806uint64_t BasicMBRData::FirstLogicalLBA(void) {
807 int i;
808 uint64_t firstFound = UINT32_MAX;
809
810 for (i = 0; i < MAX_MBR_PARTS; i++) {
811 if ((partitions[i].GetInclusion() == LOGICAL) &&
812 (partitions[i].GetStartLBA() < firstFound)) {
813 firstFound = partitions[i].GetStartLBA();
814 } // if
815 } // for
816 return firstFound;
817} // BasicMBRData::FirstLogicalLBA()
818
819// Returns the last sector occupied by any logical partition, or 0 if
820// there are no logical partitions defined.
821uint64_t BasicMBRData::LastLogicalLBA(void) {
822 int i;
823 uint64_t lastFound = 0;
824
825 for (i = 0; i < MAX_MBR_PARTS; i++) {
826 if ((partitions[i].GetInclusion() == LOGICAL) &&
827 (partitions[i].GetLastLBA() > lastFound))
828 lastFound = partitions[i].GetLastLBA();
829 } // for
830 return lastFound;
831} // BasicMBRData::LastLogicalLBA()
832
833// Returns 1 if logical partitions are contiguous (have no primaries
834// in their midst), or 0 if one or more primaries exist between
835// logicals.
836int BasicMBRData::AreLogicalsContiguous(void) {
837 int allOK = 1, i = 0;
838 uint64_t firstLogical, lastLogical;
839
840 firstLogical = FirstLogicalLBA() - 1; // subtract 1 for EBR
841 lastLogical = LastLogicalLBA();
842 if (lastLogical > 0) {
843 do {
844 if ((partitions[i].GetInclusion() == PRIMARY) &&
845 (partitions[i].GetStartLBA() >= firstLogical) &&
846 (partitions[i].GetStartLBA() <= lastLogical)) {
847 allOK = 0;
848 } // if
849 i++;
850 } while ((i < MAX_MBR_PARTS) && allOK);
851 } // if
852 return allOK;
853} // BasicMBRData::AreLogicalsContiguous()
854
855// Returns 1 if all partitions fit on the disk, given its size; 0 if any
856// partition is too big.
857int BasicMBRData::DoTheyFit(void) {
858 int i, allOK = 1;
859
860 for (i = 0; i < MAX_MBR_PARTS; i++) {
861 if ((partitions[i].GetStartLBA() > diskSize) || (partitions[i].GetLastLBA() > diskSize)) {
862 allOK = 0;
863 } // if
864 } // for
865 return allOK;
866} // BasicMBRData::DoTheyFit(void)
867
868// Returns 1 if there's at least one free sector immediately preceding
869// all partitions flagged as logical; 0 if any logical partition lacks
870// this space.
871int BasicMBRData::SpaceBeforeAllLogicals(void) {
872 int i = 0, allOK = 1;
873
874 do {
875 if ((partitions[i].GetStartLBA() > 0) && (partitions[i].GetInclusion() == LOGICAL)) {
876 allOK = allOK && (SectorUsedAs(partitions[i].GetStartLBA() - 1) == EBR);
srs5694bf8950c2011-03-12 01:23:12 -0500877 } // if
878 i++;
879 } while (allOK && (i < MAX_MBR_PARTS));
880 return allOK;
881} // BasicMBRData::SpaceBeforeAllLogicals()
882
883// Returns 1 if the partitions describe a legal layout -- all logicals
884// are contiguous and have at least one preceding empty partitions,
885// the number of primaries is under 4 (or under 3 if there are any
886// logicals), there are no overlapping partitions, etc.
887// Does NOT assume that primaries are numbered 1-4; uses the
888// IsItPrimary() function of the MBRPart class to determine
889// primary status. Also does NOT consider partition order; there
890// can be gaps and it will still be considered legal.
891int BasicMBRData::IsLegal(void) {
892 int allOK = 1;
893
894 allOK = (FindOverlaps() == 0);
895 allOK = (allOK && (NumPrimaries() <= 4));
896 allOK = (allOK && AreLogicalsContiguous());
897 allOK = (allOK && DoTheyFit());
898 allOK = (allOK && SpaceBeforeAllLogicals());
899 return allOK;
900} // BasicMBRData::IsLegal()
901
902// Finds the next in-use partition, starting with start (will return start
903// if it's in use). Returns -1 if no subsequent partition is in use.
904int BasicMBRData::FindNextInUse(int start) {
905 if (start >= MAX_MBR_PARTS)
906 start = -1;
907 while ((start < MAX_MBR_PARTS) && (start >= 0) && (partitions[start].GetInclusion() == NONE))
908 start++;
909 if ((start < 0) || (start >= MAX_MBR_PARTS))
910 start = -1;
911 return start;
912} // BasicMBRData::FindFirstLogical();
srs5694f2efa7d2011-03-01 22:03:26 -0500913
914/*****************************************************
915 * *
916 * Functions to create, delete, or change partitions *
917 * *
918 *****************************************************/
919
920// Empty all data. Meant mainly for calling by constructors, but it's also
921// used by the hybrid MBR functions in the GPTData class.
922void BasicMBRData::EmptyMBR(int clearBootloader) {
923 int i;
924
925 // Zero out the boot loader section, the disk signature, and the
926 // 2-byte nulls area only if requested to do so. (This is the
927 // default.)
928 if (clearBootloader == 1) {
929 EmptyBootloader();
930 } // if
931
932 // Blank out the partitions
933 for (i = 0; i < MAX_MBR_PARTS; i++) {
srs5694bf8950c2011-03-12 01:23:12 -0500934 partitions[i].Empty();
srs5694f2efa7d2011-03-01 22:03:26 -0500935 } // for
936 MBRSignature = MBR_SIGNATURE;
srs5694bf8950c2011-03-12 01:23:12 -0500937 state = mbr;
srs5694f2efa7d2011-03-01 22:03:26 -0500938} // BasicMBRData::EmptyMBR()
939
940// Blank out the boot loader area. Done with the initial MBR-to-GPT
941// conversion, since MBR boot loaders don't understand GPT, and so
942// need to be replaced....
943void BasicMBRData::EmptyBootloader(void) {
944 int i;
945
946 for (i = 0; i < 440; i++)
947 code[i] = 0;
948 nulls = 0;
949} // BasicMBRData::EmptyBootloader
950
srs5694bf8950c2011-03-12 01:23:12 -0500951// Create a partition of the specified number based on the passed
952// partition. This function does *NO* error checking, so it's possible
srs5694f2efa7d2011-03-01 22:03:26 -0500953// to seriously screw up a partition table using this function!
954// Note: This function should NOT be used to create the 0xEE partition
955// in a conventional GPT configuration, since that partition has
956// specific size requirements that this function won't handle. It may
957// be used for creating the 0xEE partition(s) in a hybrid MBR, though,
958// since those toss the rulebook away anyhow....
srs5694bf8950c2011-03-12 01:23:12 -0500959void BasicMBRData::AddPart(int num, const MBRPart& newPart) {
960 partitions[num] = newPart;
961} // BasicMBRData::AddPart()
962
963// Create a partition of the specified number, starting LBA, and
964// length. This function does almost no error checking, so it's possible
965// to seriously screw up a partition table using this function!
966// Note: This function should NOT be used to create the 0xEE partition
967// in a conventional GPT configuration, since that partition has
968// specific size requirements that this function won't handle. It may
969// be used for creating the 0xEE partition(s) in a hybrid MBR, though,
970// since those toss the rulebook away anyhow....
971void BasicMBRData::MakePart(int num, uint64_t start, uint64_t length, int type, int bootable) {
972 if ((num >= 0) && (num < MAX_MBR_PARTS) && (start <= UINT32_MAX) && (length <= UINT32_MAX)) {
973 partitions[num].Empty();
974 partitions[num].SetType(type);
975 partitions[num].SetLocation(start, length);
976 if (num < 4)
977 partitions[num].SetInclusion(PRIMARY);
978 else
979 partitions[num].SetInclusion(LOGICAL);
srs5694f2efa7d2011-03-01 22:03:26 -0500980 SetPartBootable(num, bootable);
srs5694bf8950c2011-03-12 01:23:12 -0500981 } // if valid partition number & size
srs5694f2efa7d2011-03-01 22:03:26 -0500982} // BasicMBRData::MakePart()
983
984// Set the partition's type code.
985// Returns 1 if successful, 0 if not (invalid partition number)
986int BasicMBRData::SetPartType(int num, int type) {
987 int allOK = 1;
988
989 if ((num >= 0) && (num < MAX_MBR_PARTS)) {
srs5694bf8950c2011-03-12 01:23:12 -0500990 if (partitions[num].GetLengthLBA() != UINT32_C(0)) {
991 allOK = partitions[num].SetType(type);
srs5694f2efa7d2011-03-01 22:03:26 -0500992 } else allOK = 0;
993 } else allOK = 0;
994 return allOK;
995} // BasicMBRData::SetPartType()
996
997// Set (or remove) the partition's bootable flag. Setting it is the
998// default; pass 0 as bootable to remove the flag.
999// Returns 1 if successful, 0 if not (invalid partition number)
1000int BasicMBRData::SetPartBootable(int num, int bootable) {
1001 int allOK = 1;
1002
1003 if ((num >= 0) && (num < MAX_MBR_PARTS)) {
srs5694bf8950c2011-03-12 01:23:12 -05001004 if (partitions[num].GetLengthLBA() != UINT32_C(0)) {
srs5694f2efa7d2011-03-01 22:03:26 -05001005 if (bootable == 0)
srs5694bf8950c2011-03-12 01:23:12 -05001006 partitions[num].SetStatus(UINT8_C(0x00));
srs5694f2efa7d2011-03-01 22:03:26 -05001007 else
srs5694bf8950c2011-03-12 01:23:12 -05001008 partitions[num].SetStatus(UINT8_C(0x80));
srs5694f2efa7d2011-03-01 22:03:26 -05001009 } else allOK = 0;
1010 } else allOK = 0;
1011 return allOK;
1012} // BasicMBRData::SetPartBootable()
1013
1014// Create a partition that fills the most available space. Returns
1015// 1 if partition was created, 0 otherwise. Intended for use in
1016// creating hybrid MBRs.
1017int BasicMBRData::MakeBiggestPart(int i, int type) {
srs5694bf8950c2011-03-12 01:23:12 -05001018 uint64_t start = UINT64_C(1); // starting point for each search
1019 uint64_t firstBlock; // first block in a segment
1020 uint64_t lastBlock; // last block in a segment
1021 uint64_t segmentSize; // size of segment in blocks
1022 uint64_t selectedSegment = UINT64_C(0); // location of largest segment
1023 uint64_t selectedSize = UINT64_C(0); // size of largest segment in blocks
srs5694f2efa7d2011-03-01 22:03:26 -05001024 int found = 0;
1025
1026 do {
1027 firstBlock = FindFirstAvailable(start);
srs5694bf8950c2011-03-12 01:23:12 -05001028 if (firstBlock > UINT64_C(0)) { // something's free...
srs5694f2efa7d2011-03-01 22:03:26 -05001029 lastBlock = FindLastInFree(firstBlock);
srs5694bf8950c2011-03-12 01:23:12 -05001030 segmentSize = lastBlock - firstBlock + UINT64_C(1);
srs5694f2efa7d2011-03-01 22:03:26 -05001031 if (segmentSize > selectedSize) {
1032 selectedSize = segmentSize;
1033 selectedSegment = firstBlock;
1034 } // if
1035 start = lastBlock + 1;
1036 } // if
1037 } while (firstBlock != 0);
srs5694bf8950c2011-03-12 01:23:12 -05001038 if ((selectedSize > UINT64_C(0)) && (selectedSize < diskSize)) {
srs5694f2efa7d2011-03-01 22:03:26 -05001039 found = 1;
1040 MakePart(i, selectedSegment, selectedSize, type, 0);
1041 } else {
1042 found = 0;
1043 } // if/else
1044 return found;
1045} // BasicMBRData::MakeBiggestPart(int i)
1046
1047// Delete partition #i
1048void BasicMBRData::DeletePartition(int i) {
srs5694bf8950c2011-03-12 01:23:12 -05001049 partitions[i].Empty();
srs5694f2efa7d2011-03-01 22:03:26 -05001050} // BasicMBRData::DeletePartition()
1051
srs5694bf8950c2011-03-12 01:23:12 -05001052// Set the inclusion status (PRIMARY, LOGICAL, or NONE) with some sanity
1053// checks to ensure the table remains legal.
1054// Returns 1 on success, 0 on failure.
1055int BasicMBRData::SetInclusionwChecks(int num, int inclStatus) {
1056 int allOK = 1, origValue;
1057
1058 if (IsLegal()) {
1059 if ((inclStatus == PRIMARY) || (inclStatus == LOGICAL) || (inclStatus == NONE)) {
1060 origValue = partitions[num].GetInclusion();
1061 partitions[num].SetInclusion(inclStatus);
1062 if (!IsLegal()) {
1063 partitions[num].SetInclusion(origValue);
1064 cerr << "Specified change is not legal! Aborting change!\n";
1065 } // if
1066 } else {
1067 cerr << "Invalid partition inclusion code in BasicMBRData::SetInclusionwChecks()!\n";
1068 } // if/else
1069 } else {
1070 cerr << "Partition table is not currently in a valid state. Aborting change!\n";
1071 allOK = 0;
1072 } // if/else
1073 return allOK;
1074} // BasicMBRData::SetInclusionwChecks()
1075
srs5694f2efa7d2011-03-01 22:03:26 -05001076// Recomputes the CHS values for the specified partition and adjusts the value.
1077// Note that this will create a technically incorrect CHS value for EFI GPT (0xEE)
1078// protective partitions, but this is required by some buggy BIOSes, so I'm
1079// providing a function to do this deliberately at the user's command.
1080// This function does nothing if the partition's length is 0.
1081void BasicMBRData::RecomputeCHS(int partNum) {
srs5694bf8950c2011-03-12 01:23:12 -05001082 partitions[partNum].RecomputeCHS();
srs5694f2efa7d2011-03-01 22:03:26 -05001083} // BasicMBRData::RecomputeCHS()
1084
srs5694699941e2011-03-21 21:33:57 -04001085// Sorts the partitions starting with partition #start. This function
1086// does NOT pay attention to primary/logical assignment, which is
1087// critical when writing the partitions.
srs5694bf8950c2011-03-12 01:23:12 -05001088void BasicMBRData::SortMBR(int start) {
srs5694c2f6e0c2011-03-16 02:42:33 -04001089 if ((start < MAX_MBR_PARTS) && (start >= 0))
1090 sort(partitions + start, partitions + MAX_MBR_PARTS);
1091} // BasicMBRData::SortMBR()
srs5694bf8950c2011-03-12 01:23:12 -05001092
1093// Delete any partitions that are too big to fit on the disk
1094// or that are too big for MBR (32-bit limits).
1095// This really deletes the partitions by setting values to 0.
1096// Returns the number of partitions deleted in this way.
1097int BasicMBRData::DeleteOversizedParts() {
1098 int num = 0, i;
1099
1100 for (i = 0; i < MAX_MBR_PARTS; i++) {
1101 if ((partitions[i].GetStartLBA() > diskSize) || (partitions[i].GetLastLBA() > diskSize) ||
1102 (partitions[i].GetStartLBA() > UINT32_MAX) || (partitions[i].GetLengthLBA() > UINT32_MAX)) {
1103 partitions[i].Empty();
1104 num++;
1105 } // if
1106 } // for
1107 return num;
1108} // BasicMBRData::DeleteOversizedParts()
1109
1110// Search for and delete extended partitions.
1111// Returns the number of partitions deleted.
1112int BasicMBRData::DeleteExtendedParts() {
1113 int i, numDeleted = 0;
1114 uint8_t type;
1115
1116 for (i = 0; i < MAX_MBR_PARTS; i++) {
1117 type = partitions[i].GetType();
1118 if (((type == 0x05) || (type == 0x0f) || (type == (0x85))) &&
1119 (partitions[i].GetLengthLBA() > 0)) {
1120 partitions[i].Empty();
1121 numDeleted++;
1122 } // if
1123 } // for
1124 return numDeleted;
1125} // BasicMBRData::DeleteExtendedParts()
1126
1127// Finds any overlapping partitions and omits the smaller of the two.
1128void BasicMBRData::OmitOverlaps() {
1129 int i, j;
1130
1131 for (i = 0; i < MAX_MBR_PARTS; i++) {
1132 for (j = i + 1; j < MAX_MBR_PARTS; j++) {
1133 if ((partitions[i].GetInclusion() != NONE) &&
1134 partitions[i].DoTheyOverlap(partitions[j])) {
1135 if (partitions[i].GetLengthLBA() < partitions[j].GetLengthLBA())
1136 partitions[i].SetInclusion(NONE);
1137 else
1138 partitions[j].SetInclusion(NONE);
srs5694f2efa7d2011-03-01 22:03:26 -05001139 } // if
srs5694bf8950c2011-03-12 01:23:12 -05001140 } // for (j...)
1141 } // for (i...)
1142} // BasicMBRData::OmitOverlaps()
1143
srs5694bf8950c2011-03-12 01:23:12 -05001144// Convert as many partitions into logicals as possible, except for
1145// the first partition, if possible.
1146void BasicMBRData::MaximizeLogicals() {
1147 int earliestPart = 0, earliestPartWas = NONE, i;
1148
1149 for (i = MAX_MBR_PARTS - 1; i >= 0; i--) {
1150 UpdateCanBeLogical();
1151 earliestPart = i;
1152 if (partitions[i].CanBeLogical()) {
1153 partitions[i].SetInclusion(LOGICAL);
1154 } else if (partitions[i].CanBePrimary()) {
1155 partitions[i].SetInclusion(PRIMARY);
1156 } else {
1157 partitions[i].SetInclusion(NONE);
1158 } // if/elseif/else
1159 } // for
1160 // If we have spare primaries, convert back the earliest partition to
1161 // its original state....
1162 if ((NumPrimaries() < 4) && (partitions[earliestPart].GetInclusion() == LOGICAL))
1163 partitions[earliestPart].SetInclusion(earliestPartWas);
1164} // BasicMBRData::MaximizeLogicals()
1165
1166// Add primaries up to the maximum allowed, from the omitted category.
1167void BasicMBRData::MaximizePrimaries() {
1168 int num, i = 0;
1169
1170 num = NumPrimaries();
1171 while ((num < 4) && (i < MAX_MBR_PARTS)) {
1172 if ((partitions[i].GetInclusion() == NONE) && (partitions[i].CanBePrimary())) {
1173 partitions[i].SetInclusion(PRIMARY);
srs5694699941e2011-03-21 21:33:57 -04001174 num++;
1175 UpdateCanBeLogical();
srs5694bf8950c2011-03-12 01:23:12 -05001176 } // if
1177 i++;
1178 } // while
1179} // BasicMBRData::MaximizePrimaries()
1180
1181// Remove primary partitions in excess of 4, starting with the later ones,
1182// in terms of the array location....
1183void BasicMBRData::TrimPrimaries(void) {
1184 int numToDelete, i = MAX_MBR_PARTS;
1185
1186 numToDelete = NumPrimaries() - 4;
1187 while ((numToDelete > 0) && (i >= 0)) {
1188 if (partitions[i].GetInclusion() == PRIMARY) {
1189 partitions[i].SetInclusion(NONE);
1190 numToDelete--;
1191 } // if
1192 i--;
1193 } // while (numToDelete > 0)
1194} // BasicMBRData::TrimPrimaries()
1195
1196// Locates primary partitions located between logical partitions and
1197// either converts the primaries into logicals (if possible) or omits
1198// them.
1199void BasicMBRData::MakeLogicalsContiguous(void) {
1200 uint64_t firstLogicalLBA, lastLogicalLBA;
1201 int i;
1202
1203 firstLogicalLBA = FirstLogicalLBA();
1204 lastLogicalLBA = LastLogicalLBA();
1205 for (i = 0; i < MAX_MBR_PARTS; i++) {
1206 if ((partitions[i].GetInclusion() == PRIMARY) &&
1207 (partitions[i].GetStartLBA() >= firstLogicalLBA) &&
1208 (partitions[i].GetLastLBA() <= lastLogicalLBA)) {
1209 if (SectorUsedAs(partitions[i].GetStartLBA() - 1) == NONE)
1210 partitions[i].SetInclusion(LOGICAL);
1211 else
1212 partitions[i].SetInclusion(NONE);
1213 } // if
1214 } // for
1215} // BasicMBRData::MakeLogicalsContiguous()
1216
1217// If MBR data aren't legal, adjust primary/logical assignments and,
1218// if necessary, drop partitions, to make the data legal.
1219void BasicMBRData::MakeItLegal(void) {
1220 if (!IsLegal()) {
1221 DeleteOversizedParts();
srs5694bf8950c2011-03-12 01:23:12 -05001222 MaximizeLogicals();
1223 MaximizePrimaries();
1224 if (!AreLogicalsContiguous())
1225 MakeLogicalsContiguous();
1226 if (NumPrimaries() > 4)
1227 TrimPrimaries();
1228 OmitOverlaps();
1229 } // if
1230} // BasicMBRData::MakeItLegal()
1231
1232// Removes logical partitions and deactivated partitions from first four
1233// entries (primary space).
1234// Returns the number of partitions moved.
1235int BasicMBRData::RemoveLogicalsFromFirstFour(void) {
1236 int i, j = 4, numMoved = 0, swapped = 0;
1237 MBRPart temp;
1238
1239 for (i = 0; i < 4; i++) {
1240 if ((partitions[i].GetInclusion() != PRIMARY) && (partitions[i].GetLengthLBA() > 0)) {
1241 j = 4;
1242 swapped = 0;
1243 do {
1244 if ((partitions[j].GetInclusion() == NONE) && (partitions[j].GetLengthLBA() == 0)) {
1245 temp = partitions[j];
1246 partitions[j] = partitions[i];
1247 partitions[i] = temp;
1248 swapped = 1;
1249 numMoved++;
1250 } // if
1251 j++;
1252 } while ((j < MAX_MBR_PARTS) && !swapped);
1253 if (j >= MAX_MBR_PARTS)
1254 cerr << "Warning! Too many partitions in BasicMBRData::RemoveLogicalsFromFirstFour()!\n";
1255 } // if
1256 } // for i...
1257 return numMoved;
1258} // BasicMBRData::RemoveLogicalsFromFirstFour()
1259
1260// Move all primaries into the first four partition spaces
1261// Returns the number of partitions moved.
1262int BasicMBRData::MovePrimariesToFirstFour(void) {
1263 int i, j = 0, numMoved = 0, swapped = 0;
1264 MBRPart temp;
1265
1266 for (i = 4; i < MAX_MBR_PARTS; i++) {
1267 if (partitions[i].GetInclusion() == PRIMARY) {
1268 j = 0;
1269 swapped = 0;
1270 do {
1271 if (partitions[j].GetInclusion() != PRIMARY) {
1272 temp = partitions[j];
1273 partitions[j] = partitions[i];
1274 partitions[i] = temp;
1275 swapped = 1;
1276 numMoved++;
1277 } // if
1278 j++;
1279 } while ((j < 4) && !swapped);
1280 } // if
1281 } // for
1282 return numMoved;
1283} // BasicMBRData::MovePrimariesToFirstFour()
1284
1285// Create an extended partition, if necessary, to hold the logical partitions.
1286// This function also sorts the primaries into the first four positions of
1287// the table.
1288// Returns 1 on success, 0 on failure.
1289int BasicMBRData::CreateExtended(void) {
1290 int allOK = 1, i = 0, swapped = 0;
1291 MBRPart temp;
1292
1293 if (IsLegal()) {
1294 // Move logicals out of primary space...
1295 RemoveLogicalsFromFirstFour();
1296 // Move primaries out of logical space...
1297 MovePrimariesToFirstFour();
1298
1299 // Create the extended partition
1300 if (NumLogicals() > 0) {
1301 SortMBR(4); // sort starting from 4 -- that is, logicals only
1302 temp.Empty();
1303 temp.SetStartLBA(FirstLogicalLBA() - 1);
1304 temp.SetLengthLBA(LastLogicalLBA() - FirstLogicalLBA() + 2);
1305 temp.SetType(0x0f, 1);
1306 temp.SetInclusion(PRIMARY);
1307 do {
1308 if ((partitions[i].GetInclusion() == NONE) || (partitions[i].GetLengthLBA() == 0)) {
1309 partitions[i] = temp;
1310 swapped = 1;
1311 } // if
1312 i++;
1313 } while ((i < 4) && !swapped);
1314 if (!swapped) {
1315 cerr << "Could not create extended partition; no room in primary table!\n";
1316 allOK = 0;
1317 } // if
1318 } // if (NumLogicals() > 0)
1319 } else allOK = 0;
1320 // Do a final check for EFI GPT (0xEE) partitions & flag as a problem if found
1321 // along with an extended partition
1322 for (i = 0; i < MAX_MBR_PARTS; i++)
1323 if (swapped && partitions[i].GetType() == 0xEE)
1324 allOK = 0;
1325 return allOK;
1326} // BasicMBRData::CreateExtended()
srs5694f2efa7d2011-03-01 22:03:26 -05001327
1328/****************************************
1329 * *
1330 * Functions to find data on free space *
1331 * *
1332 ****************************************/
1333
1334// Finds the first free space on the disk from start onward; returns 0
1335// if none available....
srs5694bf8950c2011-03-12 01:23:12 -05001336uint64_t BasicMBRData::FindFirstAvailable(uint64_t start) {
1337 uint64_t first;
1338 uint64_t i;
srs5694f2efa7d2011-03-01 22:03:26 -05001339 int firstMoved;
1340
1341 first = start;
1342
1343 // ...now search through all partitions; if first is within an
1344 // existing partition, move it to the next sector after that
1345 // partition and repeat. If first was moved, set firstMoved
1346 // flag; repeat until firstMoved is not set, so as to catch
1347 // cases where partitions are out of sequential order....
1348 do {
1349 firstMoved = 0;
1350 for (i = 0; i < 4; i++) {
1351 // Check if it's in the existing partition
srs5694bf8950c2011-03-12 01:23:12 -05001352 if ((first >= partitions[i].GetStartLBA()) &&
1353 (first < (partitions[i].GetStartLBA() + partitions[i].GetLengthLBA()))) {
1354 first = partitions[i].GetStartLBA() + partitions[i].GetLengthLBA();
srs5694f2efa7d2011-03-01 22:03:26 -05001355 firstMoved = 1;
1356 } // if
1357 } // for
1358 } while (firstMoved == 1);
srs5694bf8950c2011-03-12 01:23:12 -05001359 if ((first >= diskSize) || (first > UINT32_MAX))
srs5694f2efa7d2011-03-01 22:03:26 -05001360 first = 0;
1361 return (first);
1362} // BasicMBRData::FindFirstAvailable()
1363
1364// Finds the last free sector on the disk from start forward.
srs5694bf8950c2011-03-12 01:23:12 -05001365uint64_t BasicMBRData::FindLastInFree(uint64_t start) {
1366 uint64_t nearestStart;
1367 uint64_t i;
srs5694f2efa7d2011-03-01 22:03:26 -05001368
1369 if ((diskSize <= UINT32_MAX) && (diskSize > 0))
srs5694bf8950c2011-03-12 01:23:12 -05001370 nearestStart = diskSize - 1;
srs5694f2efa7d2011-03-01 22:03:26 -05001371 else
1372 nearestStart = UINT32_MAX - 1;
srs5694699941e2011-03-21 21:33:57 -04001373
srs5694f2efa7d2011-03-01 22:03:26 -05001374 for (i = 0; i < 4; i++) {
srs5694bf8950c2011-03-12 01:23:12 -05001375 if ((nearestStart > partitions[i].GetStartLBA()) &&
1376 (partitions[i].GetStartLBA() > start)) {
1377 nearestStart = partitions[i].GetStartLBA() - 1;
srs5694f2efa7d2011-03-01 22:03:26 -05001378 } // if
1379 } // for
1380 return (nearestStart);
1381} // BasicMBRData::FindLastInFree()
1382
1383// Finds the first free sector on the disk from start backward.
srs5694bf8950c2011-03-12 01:23:12 -05001384uint64_t BasicMBRData::FindFirstInFree(uint64_t start) {
1385 uint64_t bestLastLBA, thisLastLBA;
srs5694f2efa7d2011-03-01 22:03:26 -05001386 int i;
1387
1388 bestLastLBA = 1;
1389 for (i = 0; i < 4; i++) {
srs5694bf8950c2011-03-12 01:23:12 -05001390 thisLastLBA = partitions[i].GetLastLBA() + 1;
srs5694f2efa7d2011-03-01 22:03:26 -05001391 if (thisLastLBA > 0)
1392 thisLastLBA--;
1393 if ((thisLastLBA > bestLastLBA) && (thisLastLBA < start))
1394 bestLastLBA = thisLastLBA + 1;
1395 } // for
1396 return (bestLastLBA);
1397} // BasicMBRData::FindFirstInFree()
1398
srs5694bf8950c2011-03-12 01:23:12 -05001399// Returns NONE (unused), PRIMARY, LOGICAL, EBR (for EBR or MBR), or INVALID
1400int BasicMBRData::SectorUsedAs(uint64_t sector, int topPartNum) {
1401 int i = 0, usedAs = NONE;
srs5694f2efa7d2011-03-01 22:03:26 -05001402
srs5694bf8950c2011-03-12 01:23:12 -05001403 do {
1404 if ((partitions[i].GetStartLBA() <= sector) && (partitions[i].GetLastLBA() >= sector))
1405 usedAs = partitions[i].GetInclusion();
1406 if ((partitions[i].GetStartLBA() == (sector + 1)) && (partitions[i].GetInclusion() == LOGICAL))
1407 usedAs = EBR;
1408 if (sector == 0)
1409 usedAs = EBR;
1410 if (sector >= diskSize)
1411 usedAs = INVALID;
1412 i++;
1413 } while ((i < topPartNum) && (usedAs == NONE));
1414 return usedAs;
1415} // BasicMBRData::SectorUsedAs()
1416
srs5694f2efa7d2011-03-01 22:03:26 -05001417/******************************************************
1418 * *
1419 * Functions that extract data on specific partitions *
1420 * *
1421 ******************************************************/
1422
1423uint8_t BasicMBRData::GetStatus(int i) {
srs5694bf8950c2011-03-12 01:23:12 -05001424 MBRPart* thePart;
srs5694f2efa7d2011-03-01 22:03:26 -05001425 uint8_t retval;
1426
1427 thePart = GetPartition(i);
1428 if (thePart != NULL)
srs5694bf8950c2011-03-12 01:23:12 -05001429 retval = thePart->GetStatus();
srs5694f2efa7d2011-03-01 22:03:26 -05001430 else
1431 retval = UINT8_C(0);
1432 return retval;
1433} // BasicMBRData::GetStatus()
1434
1435uint8_t BasicMBRData::GetType(int i) {
srs5694bf8950c2011-03-12 01:23:12 -05001436 MBRPart* thePart;
srs5694f2efa7d2011-03-01 22:03:26 -05001437 uint8_t retval;
1438
1439 thePart = GetPartition(i);
1440 if (thePart != NULL)
srs5694bf8950c2011-03-12 01:23:12 -05001441 retval = thePart->GetType();
srs5694f2efa7d2011-03-01 22:03:26 -05001442 else
1443 retval = UINT8_C(0);
1444 return retval;
1445} // BasicMBRData::GetType()
1446
srs5694bf8950c2011-03-12 01:23:12 -05001447uint64_t BasicMBRData::GetFirstSector(int i) {
1448 MBRPart* thePart;
1449 uint64_t retval;
srs5694f2efa7d2011-03-01 22:03:26 -05001450
1451 thePart = GetPartition(i);
1452 if (thePart != NULL) {
srs5694bf8950c2011-03-12 01:23:12 -05001453 retval = thePart->GetStartLBA();
srs5694f2efa7d2011-03-01 22:03:26 -05001454 } else
1455 retval = UINT32_C(0);
1456 return retval;
1457} // BasicMBRData::GetFirstSector()
1458
srs5694bf8950c2011-03-12 01:23:12 -05001459uint64_t BasicMBRData::GetLength(int i) {
1460 MBRPart* thePart;
1461 uint64_t retval;
srs5694f2efa7d2011-03-01 22:03:26 -05001462
1463 thePart = GetPartition(i);
1464 if (thePart != NULL) {
srs5694bf8950c2011-03-12 01:23:12 -05001465 retval = thePart->GetLengthLBA();
srs5694f2efa7d2011-03-01 22:03:26 -05001466 } else
srs5694bf8950c2011-03-12 01:23:12 -05001467 retval = UINT64_C(0);
srs5694f2efa7d2011-03-01 22:03:26 -05001468 return retval;
1469} // BasicMBRData::GetLength()
1470
1471/***********************
1472 * *
1473 * Protected functions *
1474 * *
1475 ***********************/
1476
1477// Return a pointer to a primary or logical partition, or NULL if
1478// the partition is out of range....
srs5694bf8950c2011-03-12 01:23:12 -05001479MBRPart* BasicMBRData::GetPartition(int i) {
1480 MBRPart* thePart = NULL;
srs5694f2efa7d2011-03-01 22:03:26 -05001481
1482 if ((i >= 0) && (i < MAX_MBR_PARTS))
1483 thePart = &partitions[i];
1484 return thePart;
1485} // GetPartition()
srs5694bf8950c2011-03-12 01:23:12 -05001486
1487/*******************************************
1488 * *
1489 * Functions that involve user interaction *
1490 * *
1491 *******************************************/
1492
1493// Present the MBR operations menu. Note that the 'w' option does not
1494// immediately write data; that's handled by the calling function.
1495// Returns the number of partitions defined on exit, or -1 if the
1496// user selected the 'q' option. (Thus, the caller should save data
1497// if the return value is >0, or possibly >=0 depending on intentions.)
1498int BasicMBRData::DoMenu(const string& prompt) {
srs5694bf8950c2011-03-12 01:23:12 -05001499 int goOn = 1, quitting = 0, retval, num, haveShownInfo = 0;
1500 unsigned int hexCode = 0x00;
1501
1502 do {
1503 cout << prompt;
srs56945a608532011-03-17 13:53:01 -04001504 switch (ReadString()[0]) {
1505 case '\0':
srs5694bf8950c2011-03-12 01:23:12 -05001506 break;
1507 case 'a': case 'A':
1508 num = GetNumber(1, MAX_MBR_PARTS, 1, "Toggle active flag for partition: ") - 1;
1509 if (partitions[num].GetInclusion() != NONE)
1510 partitions[num].SetStatus(partitions[num].GetStatus() ^ 0x80);
1511 break;
1512 case 'c': case 'C':
1513 for (num = 0; num < MAX_MBR_PARTS; num++)
1514 RecomputeCHS(num);
1515 break;
1516 case 'l': case 'L':
1517 num = GetNumber(1, MAX_MBR_PARTS, 1, "Partition to set as logical: ") - 1;
1518 SetInclusionwChecks(num, LOGICAL);
1519 break;
1520 case 'o': case 'O':
1521 num = GetNumber(1, MAX_MBR_PARTS, 1, "Partition to omit: ") - 1;
1522 SetInclusionwChecks(num, NONE);
1523 break;
1524 case 'p': case 'P':
1525 if (!haveShownInfo) {
1526 cout << "\n** NOTE: Partition numbers do NOT indicate final primary/logical "
1527 << "status,\n** unlike in most MBR partitioning tools!\n\a";
1528 cout << "\n** Extended partitions are not displayed, but will be generated "
1529 << "as required.\n";
1530 haveShownInfo = 1;
1531 } // if
1532 DisplayMBRData();
1533 break;
1534 case 'q': case 'Q':
1535 cout << "This will abandon your changes. Are you sure? ";
1536 if (GetYN() == 'Y') {
1537 goOn = 0;
1538 quitting = 1;
1539 } // if
1540 break;
1541 case 'r': case 'R':
1542 num = GetNumber(1, MAX_MBR_PARTS, 1, "Partition to set as primary: ") - 1;
1543 SetInclusionwChecks(num, PRIMARY);
1544 break;
1545 case 's': case 'S':
1546 SortMBR();
1547 break;
1548 case 't': case 'T':
1549 num = GetNumber(1, MAX_MBR_PARTS, 1, "Partition to change type code: ") - 1;
1550 if (partitions[num].GetLengthLBA() > 0) {
srs5694bf8950c2011-03-12 01:23:12 -05001551 while ((hexCode <= 0) || (hexCode > 255)) {
1552 cout << "Enter an MBR hex code: ";
srs56945a608532011-03-17 13:53:01 -04001553 hexCode = StrToHex(ReadString(), 0);
srs5694bf8950c2011-03-12 01:23:12 -05001554 } // while
1555 partitions[num].SetType(hexCode);
1556 } // if
1557 break;
1558 case 'w': case 'W':
1559 goOn = 0;
1560 break;
1561 default:
1562 ShowCommands();
1563 break;
1564 } // switch
1565 } while (goOn);
1566 if (quitting)
1567 retval = -1;
1568 else
1569 retval = CountParts();
1570 return (retval);
1571} // BasicMBRData::DoMenu()
1572
1573void BasicMBRData::ShowCommands(void) {
1574 cout << "a\ttoggle the active/boot flag\n";
1575 cout << "c\trecompute all CHS values\n";
1576 cout << "l\tset partition as logical\n";
1577 cout << "o\tomit partition\n";
1578 cout << "p\tprint the MBR partition table\n";
1579 cout << "q\tquit without saving changes\n";
1580 cout << "r\tset partition as primary\n";
1581 cout << "s\tsort MBR partitions\n";
1582 cout << "t\tchange partition type code\n";
1583 cout << "w\twrite the MBR partition table to disk and exit\n";
1584} // BasicMBRData::ShowCommands()