1 /* basicmbr.h -- MBR data structure definitions, types, and functions */
2 
3 /* This program is copyright (c) 2009-2013 by Roderick W. Smith. It is distributed
4   under the terms of the GNU GPL version 2, as detailed in the COPYING file. */
5 
6 #include <stdint.h>
7 #include <sys/types.h>
8 #include "diskio.h"
9 #include "mbrpart.h"
10 
11 #ifndef __BASICMBRSTRUCTS
12 #define __BASICMBRSTRUCTS
13 
14 #define MBR_SIGNATURE UINT16_C(0xAA55)
15 
16 // Maximum number of MBR partitions
17 #define MAX_MBR_PARTS 128
18 
19 using namespace std;
20 
21 /****************************************
22  *                                      *
23  * MBRData class and related structures *
24  *                                      *
25  ****************************************/
26 
27 // A 512-byte data structure into which the MBR can be loaded in one
28 // go. Also used when loading logical partitions.
29 #pragma pack(1)
30 struct TempMBR {
31    uint8_t code[440];
32    uint32_t diskSignature;
33    uint16_t nulls;
34    struct MBRRecord partitions[4];
35    uint16_t MBRSignature;
36 }; // struct TempMBR
37 #pragma pack ()
38 
39 // Possible states of the MBR
40 enum MBRValidity {invalid, gpt, hybrid, mbr};
41 
42 // Full data in tweaked MBR format
43 class BasicMBRData {
44 protected:
45    uint8_t code[440];
46    uint32_t diskSignature;
47    uint16_t nulls;
48    // MAX_MBR_PARTS defaults to 128. This array holds both the primary and
49    // the logical partitions, to simplify data retrieval for GPT conversions.
50    MBRPart partitions[MAX_MBR_PARTS];
51    uint16_t MBRSignature;
52 
53    // Above are basic MBR data; now add more stuff....
54    uint32_t blockSize; // block size (usually 512)
55    uint64_t diskSize; // size in blocks
56    uint32_t numHeads; // number of heads, in CHS scheme
57    uint32_t numSecspTrack; // number of sectors per track, in CHS scheme
58    DiskIO* myDisk;
59    int canDeleteMyDisk;
60    string device;
61    MBRValidity state;
62    MBRPart* GetPartition(int i); // Return primary or logical partition
63 public:
64    BasicMBRData(void);
65    BasicMBRData(string deviceFilename);
66    BasicMBRData(const BasicMBRData &);
67    ~BasicMBRData(void);
68    BasicMBRData & operator=(const BasicMBRData & orig);
69 
70    // File I/O functions...
71    int ReadMBRData(const string & deviceFilename);
72    int ReadMBRData(DiskIO * theDisk, int checkBlockSize = 1);
73    int ReadLogicalParts(uint64_t extendedStart, int partNum);
74    int WriteMBRData(void);
75    int WriteMBRData(DiskIO *theDisk);
76    int WriteMBRData(const string & deviceFilename);
77    int WriteMBRData(struct TempMBR & mbr, DiskIO *theDisk, uint64_t sector);
78    void DiskSync(void) {myDisk->DiskSync();}
79    void SetDisk(DiskIO *theDisk);
80 
81    // Display data for user...
82    void DisplayMBRData(void);
83    void ShowState(void);
84 
85    // GPT checks and fixes...
86    int CheckForGPT(void);
87    int BlankGPTData(void);
88 
89    // Functions that set or get disk metadata (size, CHS geometry, etc.)
90    void SetDiskSize(uint64_t ds) {diskSize = ds;}
91    void SetBlockSize(uint32_t bs) {blockSize = bs;}
92    MBRValidity GetValidity(void) {return state;}
93    void SetHybrid(void) {state = hybrid;} // Set hybrid flag
94    void ReadCHSGeom(void);
95    int GetPartRange(uint32_t* low, uint32_t* high);
96    int LBAtoCHS(uint64_t lba, uint8_t * chs); // Convert LBA to CHS
97    int FindOverlaps(void);
98    int NumPrimaries(void);
99    int NumLogicals(void);
100    int CountParts(void);
101    void UpdateCanBeLogical(void);
102    uint64_t FirstLogicalLBA(void);
103    uint64_t LastLogicalLBA(void);
104    int AreLogicalsContiguous(void);
105    int DoTheyFit(void);
106    int SpaceBeforeAllLogicals(void);
107    int IsLegal(void);
108    int IsEEActive(void);
109    int FindNextInUse(int start);
110 
111    // Functions to create, delete, or change partitions
112    // Pass EmptyMBR 1 to clear the boot loader code, 0 to leave it intact
113    void EmptyMBR(int clearBootloader = 1);
114    void EmptyBootloader(void);
115    void AddPart(int num, const MBRPart& newPart);
116    void MakePart(int num, uint64_t startLBA, uint64_t lengthLBA, int type = 0x07,
117                  int bootable = 0);
118    int SetPartType(int num, int type);
119    int SetPartBootable(int num, int bootable = 1);
120    int MakeBiggestPart(int i, int type); // Make partition filling most space
121    void DeletePartition(int i);
122    int SetInclusionwChecks(int num, int inclStatus);
123    void RecomputeCHS(int partNum);
124    void SortMBR(int start = 0);
125    int DeleteOversizedParts();
126    int DeleteExtendedParts();
127    void OmitOverlaps(void);
128    void MaximizeLogicals();
129    void MaximizePrimaries();
130    void TrimPrimaries();
131    void MakeLogicalsContiguous(void);
132    void MakeItLegal(void);
133    int RemoveLogicalsFromFirstFour(void);
134    int MovePrimariesToFirstFour(void);
135    int CreateExtended(void);
136 
137    // Functions to find information on free space....
138    uint64_t FindFirstAvailable(uint64_t start = 1);
139    uint64_t FindLastInFree(uint64_t start);
140    uint64_t FindFirstInFree(uint64_t start);
141    int SectorUsedAs(uint64_t sector, int topPartNum = MAX_MBR_PARTS);
142 
143    // Functions to extract data on specific partitions....
144    uint8_t GetStatus(int i);
145    uint8_t GetType(int i);
146    uint64_t GetFirstSector(int i);
147    uint64_t GetLength(int i);
148 
149    // User interaction functions....
150    int DoMenu(const string& prompt = "\nMBR command (? for help): ");
151    void ShowCommands(void);
152 }; // class BasicMBRData
153 
154 #endif
155