srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 1 | /* basicmbr.h -- MBR data structure definitions, types, and functions */ |
| 2 | |
Roderick W. Smith | e3ee733 | 2013-09-24 12:56:11 -0400 | [diff] [blame] | 3 | /* This program is copyright (c) 2009-2013 by Roderick W. Smith. It is distributed |
srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 4 | under the terms of the GNU GPL version 2, as detailed in the COPYING file. */ |
| 5 | |
Rod Smith | 273fff5 | 2021-01-13 14:33:54 -0500 | [diff] [blame] | 6 | #ifndef __BASICMBRSTRUCTS |
| 7 | #define __BASICMBRSTRUCTS |
| 8 | |
srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 9 | #include <stdint.h> |
| 10 | #include <sys/types.h> |
srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 11 | #include "diskio.h" |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 12 | #include "mbrpart.h" |
srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 13 | |
srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 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 | |
srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 19 | /**************************************** |
| 20 | * * |
| 21 | * MBRData class and related structures * |
| 22 | * * |
| 23 | ****************************************/ |
| 24 | |
srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 25 | // A 512-byte data structure into which the MBR can be loaded in one |
| 26 | // go. Also used when loading logical partitions. |
Tom Marshall | bb7250f | 2019-07-23 15:18:15 -0700 | [diff] [blame] | 27 | #pragma pack(push) |
srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 28 | #pragma pack(1) |
| 29 | struct TempMBR { |
| 30 | uint8_t code[440]; |
| 31 | uint32_t diskSignature; |
| 32 | uint16_t nulls; |
| 33 | struct MBRRecord partitions[4]; |
| 34 | uint16_t MBRSignature; |
| 35 | }; // struct TempMBR |
John Paul Adrian Glaubitz | 2fb7951 | 2018-05-18 23:44:40 +0200 | [diff] [blame] | 36 | #pragma pack () |
srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 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; |
Rod Smith | edc67b6 | 2022-04-10 09:28:08 -0400 | [diff] [blame] | 59 | std::string device; |
srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 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); |
Rod Smith | edc67b6 | 2022-04-10 09:28:08 -0400 | [diff] [blame] | 64 | BasicMBRData(std::string deviceFilename); |
Rod Smith | 9ae6019 | 2018-07-05 16:50:13 -0400 | [diff] [blame] | 65 | BasicMBRData(const BasicMBRData &); |
srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 66 | ~BasicMBRData(void); |
| 67 | BasicMBRData & operator=(const BasicMBRData & orig); |
| 68 | |
| 69 | // File I/O functions... |
Rod Smith | edc67b6 | 2022-04-10 09:28:08 -0400 | [diff] [blame] | 70 | int ReadMBRData(const std::string & deviceFilename); |
srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 71 | int ReadMBRData(DiskIO * theDisk, int checkBlockSize = 1); |
srs5694 | 23d8d54 | 2011-10-01 18:40:10 -0400 | [diff] [blame] | 72 | int ReadLogicalParts(uint64_t extendedStart, int partNum); |
srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 73 | int WriteMBRData(void); |
| 74 | int WriteMBRData(DiskIO *theDisk); |
Rod Smith | edc67b6 | 2022-04-10 09:28:08 -0400 | [diff] [blame] | 75 | int WriteMBRData(const std::string & deviceFilename); |
srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 76 | int WriteMBRData(struct TempMBR & mbr, DiskIO *theDisk, uint64_t sector); |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 77 | void DiskSync(void) {myDisk->DiskSync();} |
| 78 | void SetDisk(DiskIO *theDisk); |
srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 79 | |
| 80 | // Display data for user... |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 81 | void DisplayMBRData(void); |
srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 82 | void ShowState(void); |
| 83 | |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 84 | // GPT checks and fixes... |
| 85 | int CheckForGPT(void); |
| 86 | int BlankGPTData(void); |
| 87 | |
srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 88 | // Functions that set or get disk metadata (size, CHS geometry, etc.) |
| 89 | void SetDiskSize(uint64_t ds) {diskSize = ds;} |
| 90 | void SetBlockSize(uint32_t bs) {blockSize = bs;} |
| 91 | MBRValidity GetValidity(void) {return state;} |
| 92 | void SetHybrid(void) {state = hybrid;} // Set hybrid flag |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 93 | void ReadCHSGeom(void); |
| 94 | int GetPartRange(uint32_t* low, uint32_t* high); |
srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 95 | int LBAtoCHS(uint64_t lba, uint8_t * chs); // Convert LBA to CHS |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 96 | int FindOverlaps(void); |
| 97 | int NumPrimaries(void); |
| 98 | int NumLogicals(void); |
| 99 | int CountParts(void); |
| 100 | void UpdateCanBeLogical(void); |
| 101 | uint64_t FirstLogicalLBA(void); |
| 102 | uint64_t LastLogicalLBA(void); |
| 103 | int AreLogicalsContiguous(void); |
| 104 | int DoTheyFit(void); |
| 105 | int SpaceBeforeAllLogicals(void); |
| 106 | int IsLegal(void); |
Roderick W. Smith | 042f38a | 2013-08-31 17:40:15 -0400 | [diff] [blame] | 107 | int IsEEActive(void); |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 108 | int FindNextInUse(int start); |
srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 109 | |
| 110 | // Functions to create, delete, or change partitions |
| 111 | // Pass EmptyMBR 1 to clear the boot loader code, 0 to leave it intact |
| 112 | void EmptyMBR(int clearBootloader = 1); |
| 113 | void EmptyBootloader(void); |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 114 | void AddPart(int num, const MBRPart& newPart); |
| 115 | void MakePart(int num, uint64_t startLBA, uint64_t lengthLBA, int type = 0x07, |
srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 116 | int bootable = 0); |
| 117 | int SetPartType(int num, int type); |
| 118 | int SetPartBootable(int num, int bootable = 1); |
| 119 | int MakeBiggestPart(int i, int type); // Make partition filling most space |
| 120 | void DeletePartition(int i); |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 121 | int SetInclusionwChecks(int num, int inclStatus); |
srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 122 | void RecomputeCHS(int partNum); |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 123 | void SortMBR(int start = 0); |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 124 | int DeleteOversizedParts(); |
| 125 | int DeleteExtendedParts(); |
| 126 | void OmitOverlaps(void); |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 127 | void MaximizeLogicals(); |
| 128 | void MaximizePrimaries(); |
| 129 | void TrimPrimaries(); |
| 130 | void MakeLogicalsContiguous(void); |
| 131 | void MakeItLegal(void); |
| 132 | int RemoveLogicalsFromFirstFour(void); |
| 133 | int MovePrimariesToFirstFour(void); |
| 134 | int CreateExtended(void); |
srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 135 | |
| 136 | // Functions to find information on free space.... |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 137 | uint64_t FindFirstAvailable(uint64_t start = 1); |
| 138 | uint64_t FindLastInFree(uint64_t start); |
| 139 | uint64_t FindFirstInFree(uint64_t start); |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 140 | int SectorUsedAs(uint64_t sector, int topPartNum = MAX_MBR_PARTS); |
srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 141 | |
| 142 | // Functions to extract data on specific partitions.... |
| 143 | uint8_t GetStatus(int i); |
| 144 | uint8_t GetType(int i); |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 145 | uint64_t GetFirstSector(int i); |
| 146 | uint64_t GetLength(int i); |
| 147 | |
| 148 | // User interaction functions.... |
Rod Smith | edc67b6 | 2022-04-10 09:28:08 -0400 | [diff] [blame] | 149 | int DoMenu(const std::string& prompt = "\nMBR command (? for help): "); |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 150 | void ShowCommands(void); |
srs5694 | 699941e | 2011-03-21 21:33:57 -0400 | [diff] [blame] | 151 | }; // class BasicMBRData |
srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 152 | |
Tom Marshall | bb7250f | 2019-07-23 15:18:15 -0700 | [diff] [blame] | 153 | #pragma pack(pop) |
| 154 | |
srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 155 | #endif |