srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 1 | /* basicmbr.h -- MBR data structure definitions, types, and functions */ |
| 2 | |
| 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. */ |
| 5 | |
| 6 | #include <stdint.h> |
| 7 | #include <sys/types.h> |
srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 8 | #include "diskio.h" |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 9 | #include "mbrpart.h" |
srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 10 | |
| 11 | #ifndef __BASICMBRSTRUCTS |
| 12 | #define __BASICMBRSTRUCTS |
| 13 | |
| 14 | #define MBR_SIGNATURE UINT16_C(0xAA55) |
srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 15 | |
| 16 | // Maximum number of MBR partitions |
| 17 | #define MAX_MBR_PARTS 128 |
| 18 | |
| 19 | using namespace std; |
| 20 | |
srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 21 | /**************************************** |
| 22 | * * |
| 23 | * MBRData class and related structures * |
| 24 | * * |
| 25 | ****************************************/ |
| 26 | |
srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 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 | |
| 38 | // Possible states of the MBR |
| 39 | enum MBRValidity {invalid, gpt, hybrid, mbr}; |
| 40 | |
| 41 | // Full data in tweaked MBR format |
| 42 | class BasicMBRData { |
| 43 | protected: |
| 44 | uint8_t code[440]; |
| 45 | uint32_t diskSignature; |
| 46 | uint16_t nulls; |
| 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. |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 49 | MBRPart partitions[MAX_MBR_PARTS]; |
srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 50 | uint16_t MBRSignature; |
| 51 | |
| 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 |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 55 | uint32_t numHeads; // number of heads, in CHS scheme |
| 56 | uint32_t numSecspTrack; // number of sectors per track, in CHS scheme |
srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 57 | DiskIO* myDisk; |
| 58 | int canDeleteMyDisk; |
| 59 | string device; |
| 60 | MBRValidity state; |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 61 | MBRPart* GetPartition(int i); // Return primary or logical partition |
srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 62 | public: |
| 63 | BasicMBRData(void); |
| 64 | BasicMBRData(string deviceFilename); |
| 65 | ~BasicMBRData(void); |
| 66 | BasicMBRData & operator=(const BasicMBRData & orig); |
| 67 | |
| 68 | // File I/O functions... |
| 69 | int ReadMBRData(const string & deviceFilename); |
| 70 | int ReadMBRData(DiskIO * theDisk, int checkBlockSize = 1); |
srs5694 | 23d8d54 | 2011-10-01 18:40:10 -0400 | [diff] [blame] | 71 | int ReadLogicalParts(uint64_t extendedStart, int partNum); |
srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 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); |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 76 | void DiskSync(void) {myDisk->DiskSync();} |
| 77 | void SetDisk(DiskIO *theDisk); |
srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 78 | |
| 79 | // Display data for user... |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 80 | void DisplayMBRData(void); |
srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 81 | void ShowState(void); |
| 82 | |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 83 | // GPT checks and fixes... |
| 84 | int CheckForGPT(void); |
| 85 | int BlankGPTData(void); |
| 86 | |
srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 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 |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 92 | void ReadCHSGeom(void); |
| 93 | int GetPartRange(uint32_t* low, uint32_t* high); |
srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 94 | int LBAtoCHS(uint64_t lba, uint8_t * chs); // Convert LBA to CHS |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 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); |
Roderick W. Smith | 042f38a | 2013-08-31 17:40:15 -0400 | [diff] [blame] | 106 | int IsEEActive(void); |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 107 | int FindNextInUse(int start); |
srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 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); |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 113 | void AddPart(int num, const MBRPart& newPart); |
| 114 | void MakePart(int num, uint64_t startLBA, uint64_t lengthLBA, int type = 0x07, |
srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 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); |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 120 | int SetInclusionwChecks(int num, int inclStatus); |
srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 121 | void RecomputeCHS(int partNum); |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 122 | void SortMBR(int start = 0); |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 123 | int DeleteOversizedParts(); |
| 124 | int DeleteExtendedParts(); |
| 125 | void OmitOverlaps(void); |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 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); |
srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 134 | |
| 135 | // Functions to find information on free space.... |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 136 | uint64_t FindFirstAvailable(uint64_t start = 1); |
| 137 | uint64_t FindLastInFree(uint64_t start); |
| 138 | uint64_t FindFirstInFree(uint64_t start); |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 139 | int SectorUsedAs(uint64_t sector, int topPartNum = MAX_MBR_PARTS); |
srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 140 | |
| 141 | // Functions to extract data on specific partitions.... |
| 142 | uint8_t GetStatus(int i); |
| 143 | uint8_t GetType(int i); |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 144 | uint64_t GetFirstSector(int i); |
| 145 | uint64_t GetLength(int i); |
| 146 | |
| 147 | // User interaction functions.... |
| 148 | int DoMenu(const string& prompt = "\nMBR command (? for help): "); |
| 149 | void ShowCommands(void); |
srs5694 | 699941e | 2011-03-21 21:33:57 -0400 | [diff] [blame] | 150 | }; // class BasicMBRData |
srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 151 | |
| 152 | #endif |