• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
35 using 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.
44 #pragma pack(1)
45 struct MBRRecord {
46    uint8_t status;
47    uint8_t firstSector[3];
48    uint8_t partitionType;
49    uint8_t lastSector[3];
50    uint32_t firstLBA; // see above
51    uint32_t lengthLBA;
52 }; // struct MBRRecord
53 
54 class MBRPart {
55 protected:
56    uint8_t status;
57    uint8_t firstSector[3];
58    uint8_t partitionType;
59    uint8_t lastSector[3];
60    uint32_t firstLBA; // see above
61    uint32_t lengthLBA;
62    int includeAs; // PRIMARY, LOGICAL, or NONE
63    int canBeLogical;
64    int canBePrimary;
65    static uint32_t numHeads;
66    static uint32_t numSecspTrack;
67    static uint64_t diskSize;
68    static uint32_t blockSize;
69    static int numInstances;
70 
71 public:
72     MBRPart();
73     MBRPart(const MBRPart& other);
74     virtual ~MBRPart();
75     virtual MBRPart& operator=(const MBRPart& orig);
76     virtual MBRPart& operator=(const struct MBRRecord& orig);
77     bool operator<(const MBRPart &other) const;
78 
79     // Set information on partitions or disks...
80     void SetGeometry(uint32_t heads, uint32_t sectors, uint64_t ds, uint32_t bs);
81     void Empty(void);
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);
SetStatus(uint8_t s)86     void SetStatus(uint8_t s) {status = s;}
87     void SetInclusion(int status = PRIMARY) {includeAs = status;}
SetCanBeLogical(int c)88     void SetCanBeLogical(int c) {canBeLogical = c;}
SetCanBePrimary(int c)89     void SetCanBePrimary(int c) {canBePrimary = c;}
90     void StoreInStruct(struct MBRRecord *theStruct);
91 
92     // Get information on partitions or disk....
GetType(void)93     uint8_t GetType(void) {return partitionType;}
GetStatus(void)94     uint8_t GetStatus(void) {return status;}
GetStartLBA(void)95     uint64_t GetStartLBA(void) {return firstLBA;}
GetLengthLBA(void)96     uint64_t GetLengthLBA(void) {return lengthLBA;}
97     uint64_t GetLastLBA(void) const;
GetInclusion(void)98     uint8_t GetInclusion(void) {return includeAs;}
CanBeLogical(void)99     int CanBeLogical(void) {return canBeLogical;}
CanBePrimary(void)100     int CanBePrimary(void) {return canBePrimary;}
101     int DoTheyOverlap (const MBRPart& other);
102 
103     // Adjust information on partitions or disks...
104     int RecomputeCHS(void);
105     int LBAtoCHS(uint32_t lba, uint8_t * chs);
106     void ReverseByteOrder(void);
107 
108     // User I/O...
109     void ShowData(int isGpt);
110 }; // MBRPart
111 
112 #endif // MBRPART_H
113