blob: 004e821dcdbc4e75c1454b51f324e8313ac5c3ab [file] [log] [blame]
srs569496312232011-03-12 01:22:42 -05001/*
2 MBRPart class, part of GPT fdisk program family.
3 Copyright (C) 2011 Roderick W. Smith
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18*/
19
20#ifndef MBRPART_H
21#define MBRPART_H
22
23#include <stdint.h>
24
25#define MAX_HEADS 255 /* numbered 0 - 254 */
26#define MAX_SECSPERTRACK 63 /* numbered 1 - 63 */
27#define MAX_CYLINDERS 1024 /* numbered 0 - 1023 */
28
29#define NONE 0 /* don't include partition when writing */
30#define PRIMARY 1 /* write partition as primary */
31#define LOGICAL 2 /* write partition as logical */
32#define EBR 4 /* sector is used as an EBR or MBR */
33#define INVALID 8 /* sector number is too large for disk */
34
35using namespace std;
36
37// Data for a single MBR partition record
38// Note that firstSector and lastSector are in CHS addressing, which
39// splits the bits up in a weird way.
40// On read or write of MBR entries, firstLBA is an absolute disk sector.
41// On read of logical entries, it's relative to the EBR record for that
42// partition. When writing EBR records, it's relative to the extended
43// partition's start.
Tom Marshall7b5b7112019-07-23 15:18:15 -070044#pragma pack(push)
srs569496312232011-03-12 01:22:42 -050045#pragma pack(1)
46struct MBRRecord {
47 uint8_t status;
48 uint8_t firstSector[3];
49 uint8_t partitionType;
50 uint8_t lastSector[3];
51 uint32_t firstLBA; // see above
52 uint32_t lengthLBA;
53}; // struct MBRRecord
John Paul Adrian Glaubitz2fb79512018-05-18 23:44:40 +020054#pragma pack ()
srs569496312232011-03-12 01:22:42 -050055
56class MBRPart {
57protected:
58 uint8_t status;
59 uint8_t firstSector[3];
60 uint8_t partitionType;
61 uint8_t lastSector[3];
62 uint32_t firstLBA; // see above
63 uint32_t lengthLBA;
64 int includeAs; // PRIMARY, LOGICAL, or NONE
65 int canBeLogical;
66 int canBePrimary;
67 static uint32_t numHeads;
68 static uint32_t numSecspTrack;
69 static uint64_t diskSize;
70 static uint32_t blockSize;
71 static int numInstances;
72
73public:
74 MBRPart();
75 MBRPart(const MBRPart& other);
76 virtual ~MBRPart();
77 virtual MBRPart& operator=(const MBRPart& orig);
78 virtual MBRPart& operator=(const struct MBRRecord& orig);
srs5694c2f6e0c2011-03-16 02:42:33 -040079 bool operator<(const MBRPart &other) const;
srs569496312232011-03-12 01:22:42 -050080
81 // Set information on partitions or disks...
srs5694a17fe692011-09-10 20:30:20 -040082 void SetGeometry(uint32_t heads, uint32_t sectors, uint64_t ds, uint32_t bs);
srs569496312232011-03-12 01:22:42 -050083 void Empty(void);
84 void SetStartLBA(uint64_t s);
85 void SetLengthLBA(uint64_t l);
86 void SetLocation(uint64_t start, uint64_t length);
87 int SetType(uint8_t typeCode, int isExtended = 0);
88 void SetStatus(uint8_t s) {status = s;}
89 void SetInclusion(int status = PRIMARY) {includeAs = status;}
90 void SetCanBeLogical(int c) {canBeLogical = c;}
91 void SetCanBePrimary(int c) {canBePrimary = c;}
92 void StoreInStruct(struct MBRRecord *theStruct);
93
94 // Get information on partitions or disk....
95 uint8_t GetType(void) {return partitionType;}
96 uint8_t GetStatus(void) {return status;}
97 uint64_t GetStartLBA(void) {return firstLBA;}
98 uint64_t GetLengthLBA(void) {return lengthLBA;}
99 uint64_t GetLastLBA(void) const;
100 uint8_t GetInclusion(void) {return includeAs;}
101 int CanBeLogical(void) {return canBeLogical;}
102 int CanBePrimary(void) {return canBePrimary;}
103 int DoTheyOverlap (const MBRPart& other);
104
105 // Adjust information on partitions or disks...
srs5694a17fe692011-09-10 20:30:20 -0400106 int RecomputeCHS(void);
srs569496312232011-03-12 01:22:42 -0500107 int LBAtoCHS(uint32_t lba, uint8_t * chs);
108 void ReverseByteOrder(void);
109
110 // User I/O...
111 void ShowData(int isGpt);
112}; // MBRPart
113
Tom Marshall7b5b7112019-07-23 15:18:15 -0700114#pragma pack(pop)
115
srs569496312232011-03-12 01:22:42 -0500116#endif // MBRPART_H