• 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 // Data for a single MBR partition record
36 // Note that firstSector and lastSector are in CHS addressing, which
37 // splits the bits up in a weird way.
38 // On read or write of MBR entries, firstLBA is an absolute disk sector.
39 // On read of logical entries, it's relative to the EBR record for that
40 // partition. When writing EBR records, it's relative to the extended
41 // partition's start.
42 #pragma pack(1)
43 struct MBRRecord {
44    uint8_t status;
45    uint8_t firstSector[3];
46    uint8_t partitionType;
47    uint8_t lastSector[3];
48    uint32_t firstLBA; // see above
49    uint32_t lengthLBA;
50 }; // struct MBRRecord
51 #pragma pack ()
52 
53 class MBRPart {
54 protected:
55    uint8_t status;
56    uint8_t firstSector[3];
57    uint8_t partitionType;
58    uint8_t lastSector[3];
59    uint32_t firstLBA; // see above
60    uint32_t lengthLBA;
61    int includeAs; // PRIMARY, LOGICAL, or NONE
62    int canBeLogical;
63    int canBePrimary;
64    static uint32_t numHeads;
65    static uint32_t numSecspTrack;
66    static uint64_t diskSize;
67    static uint32_t blockSize;
68    static int numInstances;
69 
70 public:
71     MBRPart();
72     MBRPart(const MBRPart& other);
73     virtual ~MBRPart();
74     virtual MBRPart& operator=(const MBRPart& orig);
75     virtual MBRPart& operator=(const struct MBRRecord& orig);
76     bool operator<(const MBRPart &other) const;
77 
78     // Set information on partitions or disks...
79     void SetGeometry(uint32_t heads, uint32_t sectors, uint64_t ds, uint32_t bs);
80     void Empty(void);
81     void SetStartLBA(uint64_t s);
82     void SetLengthLBA(uint64_t l);
83     void SetLocation(uint64_t start, uint64_t length);
84     int SetType(uint8_t typeCode, int isExtended = 0);
SetStatus(uint8_t s)85     void SetStatus(uint8_t s) {status = s;}
86     void SetInclusion(int status = PRIMARY) {includeAs = status;}
SetCanBeLogical(int c)87     void SetCanBeLogical(int c) {canBeLogical = c;}
SetCanBePrimary(int c)88     void SetCanBePrimary(int c) {canBePrimary = c;}
89     void StoreInStruct(struct MBRRecord *theStruct);
90 
91     // Get information on partitions or disk....
GetType(void)92     uint8_t GetType(void) {return partitionType;}
GetStatus(void)93     uint8_t GetStatus(void) {return status;}
GetStartLBA(void)94     uint64_t GetStartLBA(void) {return firstLBA;}
GetLengthLBA(void)95     uint64_t GetLengthLBA(void) {return lengthLBA;}
96     uint64_t GetLastLBA(void) const;
GetInclusion(void)97     uint8_t GetInclusion(void) {return includeAs;}
CanBeLogical(void)98     int CanBeLogical(void) {return canBeLogical;}
CanBePrimary(void)99     int CanBePrimary(void) {return canBePrimary;}
100     int DoTheyOverlap (const MBRPart& other);
101 
102     // Adjust information on partitions or disks...
103     int RecomputeCHS(void);
104     int LBAtoCHS(uint32_t lba, uint8_t * chs);
105     void ReverseByteOrder(void);
106 
107     // User I/O...
108     void ShowData(int isGpt);
109 }; // MBRPart
110 
111 #endif // MBRPART_H
112