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