2 MBRPart class, part of GPT fdisk program family.
3 Copyright (C) 2011 Roderick W. Smith
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.
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.
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.
25 #define MAX_HEADS 255 /* numbered 0 - 254 */
26 #define MAX_SECSPERTRACK 63 /* numbered 1 - 63 */
27 #define MAX_CYLINDERS 1024 /* numbered 0 - 1023 */
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 */
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
47 uint8_t firstSector[3];
48 uint8_t partitionType;
49 uint8_t lastSector[3];
50 uint32_t firstLBA; // see above
52 }; // struct MBRRecord
57 uint8_t firstSector[3];
58 uint8_t partitionType;
59 uint8_t lastSector[3];
60 uint32_t firstLBA; // see above
62 int includeAs; // PRIMARY, LOGICAL, or NONE
65 static uint32_t numHeads;
66 static uint32_t numSecspTrack;
67 static uint64_t diskSize;
68 static uint32_t blockSize;
69 static int numInstances;
73 MBRPart(const MBRPart& other);
75 virtual MBRPart& operator=(const MBRPart& orig);
76 virtual MBRPart& operator=(const struct MBRRecord& orig);
77 bool operator<(const MBRPart &other) const;
79 // Set information on partitions or disks...
80 void SetGeometry(uint32_t heads, uint32_t sectors, uint64_t ds, uint32_t bs);
82 void SetStartLBA(uint64_t s);
83 void SetLengthLBA(uint64_t l);
84 void SetLocation(uint64_t start, uint64_t length);
85 int SetType(uint8_t typeCode, int isExtended = 0);
86 void SetStatus(uint8_t s) {status = s;}
87 void SetInclusion(int status = PRIMARY) {includeAs = status;}
88 void SetCanBeLogical(int c) {canBeLogical = c;}
89 void SetCanBePrimary(int c) {canBePrimary = c;}
90 void StoreInStruct(struct MBRRecord *theStruct);
92 // Get information on partitions or disk....
93 uint8_t GetType(void) {return partitionType;}
94 uint8_t GetStatus(void) {return status;}
95 uint64_t GetStartLBA(void) {return firstLBA;}
96 uint64_t GetLengthLBA(void) {return lengthLBA;}
97 uint64_t GetLastLBA(void) const;
98 uint8_t GetInclusion(void) {return includeAs;}
99 int CanBeLogical(void) {return canBeLogical;}
100 int CanBePrimary(void) {return canBePrimary;}
101 int DoTheyOverlap (const MBRPart& other);
103 // Adjust information on partitions or disks...
104 int RecomputeCHS(void);
105 int LBAtoCHS(uint32_t lba, uint8_t * chs);
106 void ReverseByteOrder(void);
109 void ShowData(int isGpt);