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