forked from kini/gptfdisk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mbrpart.cc
359 lines (319 loc) · 11.2 KB
/
mbrpart.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
MBRPart class, part of GPT fdisk program family.
Copyright (C) 2011 Roderick W. Smith
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#define __STDC_LIMIT_MACROS
#define __STDC_CONSTANT_MACROS
#include <stddef.h>
#include <stdint.h>
#include <iostream>
#include "support.h"
#include "mbrpart.h"
using namespace std;
uint32_t MBRPart::numHeads = MAX_HEADS;
uint32_t MBRPart::numSecspTrack = MAX_SECSPERTRACK;
uint64_t MBRPart::diskSize = 0;
uint32_t MBRPart::blockSize = 512;
int MBRPart::numInstances = 0;
MBRPart::MBRPart() {
int i;
status = 0;
for (i = 0; i < 3; i++) {
firstSector[i] = 0;
lastSector[i] = 0;
} // for
partitionType = 0x00;
firstLBA = 0;
lengthLBA = 0;
includeAs = NONE;
canBePrimary = 0;
canBeLogical = 0;
if (numInstances == 0) {
numHeads = MAX_HEADS;
numSecspTrack = MAX_SECSPERTRACK;
diskSize = 0;
blockSize = 512;
} // if
numInstances++;
}
MBRPart::MBRPart(const MBRPart& orig) {
numInstances++;
operator=(orig);
}
MBRPart::~MBRPart() {
numInstances--;
}
MBRPart& MBRPart::operator=(const MBRPart& orig) {
int i;
status = orig.status;
for (i = 0; i < 3; i++) {
firstSector[i] = orig.firstSector[i];
lastSector[i] = orig.lastSector[i];
} // for
partitionType = orig.partitionType;
firstLBA = orig.firstLBA;
lengthLBA = orig.lengthLBA;
includeAs = orig.includeAs;
canBePrimary = orig.canBePrimary;
canBeLogical = orig.canBeLogical;
return *this;
} // MBRPart::operator=(const MBRPart& orig)
// Set partition data from packed MBRRecord structure.
MBRPart& MBRPart::operator=(const struct MBRRecord& orig) {
int i;
status = orig.status;
for (i = 0; i < 3; i++) {
firstSector[i] = orig.firstSector[i];
lastSector[i] = orig.lastSector[i];
} // for
partitionType = orig.partitionType;
firstLBA = orig.firstLBA;
lengthLBA = orig.lengthLBA;
if (lengthLBA > 0)
includeAs = PRIMARY;
else
includeAs = NONE;
return *this;
} // MBRPart::operator=(const struct MBRRecord& orig)
// Compare the values, and return a bool result.
// Because this is intended for sorting and a lengthLBA value of 0 denotes
// a partition that's not in use and so that should be sorted upwards,
// we return the opposite of the usual arithmetic result when either
// lengthLBA value is 0.
bool MBRPart::operator<(const MBRPart &other) const {
if (lengthLBA && other.lengthLBA)
return (firstLBA < other.firstLBA);
else
return (other.firstLBA < firstLBA);
} // operator<()
/**************************************************
* *
* Set information on partitions or disks without *
* interacting with the user.... *
* *
**************************************************/
void MBRPart::SetGeometry(uint32_t heads, uint32_t sectors, uint64_t ds, uint32_t bs) {
numHeads = heads;
numSecspTrack = sectors;
diskSize = ds;
blockSize = bs;
} // MBRPart::SetGeometry
// Empty the partition (zero out all values).
void MBRPart::Empty(void) {
status = UINT8_C(0);
firstSector[0] = UINT8_C(0);
firstSector[1] = UINT8_C(0);
firstSector[2] = UINT8_C(0);
partitionType = UINT8_C(0);
lastSector[0] = UINT8_C(0);
lastSector[1] = UINT8_C(0);
lastSector[2] = UINT8_C(0);
firstLBA = UINT32_C(0);
lengthLBA = UINT32_C(0);
includeAs = NONE;
} // MBRPart::Empty()
// Sets the type code, but silently refuses to change it to an extended type
// code.
// Returns 1 on success, 0 on failure (extended type code)
int MBRPart::SetType(uint8_t typeCode, int isExtended) {
int allOK = 0;
if ((isExtended == 1) || ((typeCode != 0x05) && (typeCode != 0x0f) && (typeCode != 0x85))) {
partitionType = typeCode;
allOK = 1;
} // if
return allOK;
} // MBRPart::SetType()
void MBRPart::SetStartLBA(uint64_t start) {
if (start > UINT32_MAX)
cerr << "Partition start out of range! Continuing, but problems now likely!\n";
firstLBA = (uint32_t) start;
RecomputeCHS();
} // MBRPart::SetStartLBA()
void MBRPart::SetLengthLBA(uint64_t length) {
if (length > UINT32_MAX)
cerr << "Partition length out of range! Continuing, but problems now likely!\n";
lengthLBA = (uint32_t) length;
RecomputeCHS();
} // MBRPart::SetLengthLBA()
// Set the start point and length of the partition. This function takes LBA
// values, sets them directly, and sets the CHS values based on the LBA
// values and the current geometry settings.
void MBRPart::SetLocation(uint64_t start, uint64_t length) {
int validCHS;
if ((start > UINT32_MAX) || (length > UINT32_MAX)) {
cerr << "Partition values out of range in MBRPart::SetLocation()!\n"
<< "Continuing, but strange problems are now likely!\n";
} // if
firstLBA = (uint32_t) start;
lengthLBA = (uint32_t) length;
validCHS = RecomputeCHS();
// If this is a complete 0xEE protective MBR partition, max out its
// CHS last sector value, as per the GPT spec. (Set to 0xffffff,
// although the maximum legal MBR value is 0xfeffff, which is
// actually what GNU Parted and Apple's Disk Utility use, in
// violation of the GPT spec.)
if ((partitionType == 0xEE) && (!validCHS) && (firstLBA == 1) &&
((lengthLBA == diskSize - 1) || (lengthLBA == UINT32_MAX))) {
lastSector[0] = lastSector[1] = lastSector[2] = 0xFF;
} // if
} // MBRPart::SetLocation()
// Store the MBR data in the packed structure used for disk I/O...
void MBRPart::StoreInStruct(MBRRecord* theStruct) {
int i;
theStruct->firstLBA = firstLBA;
theStruct->lengthLBA = lengthLBA;
theStruct->partitionType = partitionType;
theStruct->status = status;
for (i = 0; i < 3; i++) {
theStruct->firstSector[i] = firstSector[i];
theStruct->lastSector[i] = lastSector[i];
} // for
} // MBRPart::StoreInStruct()
/**********************************************
* *
* Get information on partitions or disks.... *
* *
**********************************************/
// Returns the last LBA value. Note that this can theoretically be a 33-bit
// value, so we return a 64-bit value. If lengthLBA == 0, returns 0, even if
// firstLBA is non-0.
uint64_t MBRPart::GetLastLBA(void) const {
if (lengthLBA > 0)
return (uint64_t) firstLBA + (uint64_t) lengthLBA - UINT64_C(1);
else
return 0;
} // MBRPart::GetLastLBA()
// Returns 1 if other overlaps with the current partition, 0 if they don't
// overlap
int MBRPart::DoTheyOverlap (const MBRPart& other) {
return lengthLBA && other.lengthLBA &&
(firstLBA <= other.GetLastLBA()) != (GetLastLBA() < other.firstLBA);
} // MBRPart::DoTheyOverlap()
/*************************************************
* *
* Adjust information on partitions or disks.... *
* *
*************************************************/
// Recompute the CHS values for the start and end points.
// Returns 1 if both computed values are within the range
// that can be expressed by that CHS, 0 otherwise.
int MBRPart::RecomputeCHS(void) {
int retval = 1;
if (lengthLBA > 0) {
retval = LBAtoCHS(firstLBA, firstSector);
retval *= LBAtoCHS(firstLBA + lengthLBA - 1, lastSector);
} // if
return retval;
} // MBRPart::RecomputeCHS()
// Converts 32-bit LBA value to MBR-style CHS value. Returns 1 if conversion
// was within the range that can be expressed by CHS (including 0, for an
// empty partition), 0 if the value is outside that range, and -1 if chs is
// invalid.
int MBRPart::LBAtoCHS(uint32_t lba, uint8_t * chs) {
uint64_t cylinder, head, sector; // all numbered from 0
uint64_t remainder;
int retval = 1;
int done = 0;
if (chs != NULL) {
// Special case: In case of 0 LBA value, zero out CHS values....
if (lba == 0) {
chs[0] = chs[1] = chs[2] = UINT8_C(0);
done = 1;
} // if
// If LBA value is too large for CHS, max out CHS values....
if ((!done) && (lba >= (numHeads * numSecspTrack * MAX_CYLINDERS))) {
chs[0] = 254;
chs[1] = chs[2] = 255;
done = 1;
retval = 0;
} // if
// If neither of the above applies, compute CHS values....
if (!done) {
cylinder = lba / (numHeads * numSecspTrack);
remainder = lba - (cylinder * numHeads * numSecspTrack);
head = remainder / numSecspTrack;
remainder -= head * numSecspTrack;
sector = remainder;
if (head < numHeads)
chs[0] = (uint8_t) head;
else
retval = 0;
if (sector < numSecspTrack) {
chs[1] = (uint8_t) ((sector + 1) + (cylinder >> 8) * 64);
chs[2] = (uint8_t) (cylinder & UINT32_C(0xFF));
} else {
retval = 0;
} // if/else
} // if value is expressible and non-0
} else { // Invalid (NULL) chs pointer
retval = -1;
} // if CHS pointer valid
return (retval);
} // MBRPart::LBAtoCHS()
// Reverses the byte order, but only if we're on a big-endian platform.
// Note that most data come in 8-bit structures, so don't need reversing;
// only the LBA data needs to be reversed....
void MBRPart::ReverseByteOrder(void) {
if (IsLittleEndian() == 0) {
ReverseBytes(&firstLBA, 4);
ReverseBytes(&lengthLBA, 4);
} // if
} // MBRPart::ReverseByteOrder()
/**************************
* *
* User I/O functions.... *
* *
**************************/
// Show MBR data. Should update canBeLogical flags before calling.
// If isGpt == 1, omits the "can be logical" and "can be primary" columns.
void MBRPart::ShowData(int isGpt) {
char bootCode = ' ';
if (status && 0x80) // it's bootable
bootCode = '*';
cout.fill(' ');
cout << bootCode << " ";
cout.width(13);
cout << firstLBA;
cout.width(13);
cout << GetLastLBA() << " ";
switch (includeAs) {
case PRIMARY:
cout << "primary";
break;
case LOGICAL:
cout << "logical";
break;
case NONE:
cout << "omitted";
break;
default:
cout << "error ";
break;
} // switch
cout.width(7);
if (!isGpt) {
if (canBeLogical)
cout << " Y ";
else
cout << " ";
if (canBePrimary)
cout << " Y ";
else
cout << " ";
} // if
cout << "0x";
cout.width(2);
cout.fill('0');
cout << hex << (int) partitionType << dec << "\n";
} // MBRPart::ShowData()