-
Notifications
You must be signed in to change notification settings - Fork 5
/
mbrpart.h
112 lines (96 loc) · 3.85 KB
/
mbrpart.h
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
/*
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.
*/
#ifndef MBRPART_H
#define MBRPART_H
#include <stdint.h>
#define MAX_HEADS 255 /* numbered 0 - 254 */
#define MAX_SECSPERTRACK 63 /* numbered 1 - 63 */
#define MAX_CYLINDERS 1024 /* numbered 0 - 1023 */
#define NONE 0 /* don't include partition when writing */
#define PRIMARY 1 /* write partition as primary */
#define LOGICAL 2 /* write partition as logical */
#define EBR 4 /* sector is used as an EBR or MBR */
#define INVALID 8 /* sector number is too large for disk */
using namespace std;
// Data for a single MBR partition record
// Note that firstSector and lastSector are in CHS addressing, which
// splits the bits up in a weird way.
// On read or write of MBR entries, firstLBA is an absolute disk sector.
// On read of logical entries, it's relative to the EBR record for that
// partition. When writing EBR records, it's relative to the extended
// partition's start.
#pragma pack(1)
struct MBRRecord {
uint8_t status;
uint8_t firstSector[3];
uint8_t partitionType;
uint8_t lastSector[3];
uint32_t firstLBA; // see above
uint32_t lengthLBA;
}; // struct MBRRecord
class MBRPart {
protected:
uint8_t status;
uint8_t firstSector[3];
uint8_t partitionType;
uint8_t lastSector[3];
uint32_t firstLBA; // see above
uint32_t lengthLBA;
int includeAs; // PRIMARY, LOGICAL, or NONE
int canBeLogical;
int canBePrimary;
static uint32_t numHeads;
static uint32_t numSecspTrack;
static uint64_t diskSize;
static uint32_t blockSize;
static int numInstances;
public:
MBRPart();
MBRPart(const MBRPart& other);
virtual ~MBRPart();
virtual MBRPart& operator=(const MBRPart& orig);
virtual MBRPart& operator=(const struct MBRRecord& orig);
bool operator<(const MBRPart &other) const;
// Set information on partitions or disks...
void SetGeometry(uint32_t heads, uint32_t sectors, uint64_t ds, uint32_t bs);
void Empty(void);
void SetStartLBA(uint64_t s);
void SetLengthLBA(uint64_t l);
void SetLocation(uint64_t start, uint64_t length);
int SetType(uint8_t typeCode, int isExtended = 0);
void SetStatus(uint8_t s) {status = s;}
void SetInclusion(int status = PRIMARY) {includeAs = status;}
void SetCanBeLogical(int c) {canBeLogical = c;}
void SetCanBePrimary(int c) {canBePrimary = c;}
void StoreInStruct(struct MBRRecord *theStruct);
// Get information on partitions or disk....
uint8_t GetType(void) {return partitionType;}
uint8_t GetStatus(void) {return status;}
uint64_t GetStartLBA(void) {return firstLBA;}
uint64_t GetLengthLBA(void) {return lengthLBA;}
uint64_t GetLastLBA(void) const;
uint8_t GetInclusion(void) {return includeAs;}
int CanBeLogical(void) {return canBeLogical;}
int CanBePrimary(void) {return canBePrimary;}
int DoTheyOverlap (const MBRPart& other);
// Adjust information on partitions or disks...
int RecomputeCHS(void);
int LBAtoCHS(uint32_t lba, uint8_t * chs);
void ReverseByteOrder(void);
// User I/O...
void ShowData(int isGpt);
}; // MBRPart
#endif // MBRPART_H