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