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 | |
| 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. |
Tom Marshall | 7b5b711 | 2019-07-23 15:18:15 -0700 | [diff] [blame] | 29 | #pragma pack(push) |
srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 30 | #pragma pack(1) |
| 31 | struct TempMBR { |
| 32 | uint8_t code[440]; |
| 33 | uint32_t diskSignature; |
| 34 | uint16_t nulls; |
| 35 | struct MBRRecord partitions[4]; |
| 36 | uint16_t MBRSignature; |
| 37 | }; // struct TempMBR |
John Paul Adrian Glaubitz | 2fb7951 | 2018-05-18 23:44:40 +0200 | [diff] [blame] | 38 | #pragma pack () |
srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 39 | |
| 40 | // Possible states of the MBR |
| 41 | enum MBRValidity {invalid, gpt, hybrid, mbr}; |
| 42 | |
| 43 | // Full data in tweaked MBR format |
| 44 | class BasicMBRData { |
| 45 | protected: |
| 46 | uint8_t code[440]; |
| 47 | uint32_t diskSignature; |
| 48 | uint16_t nulls; |
| 49 | // MAX_MBR_PARTS defaults to 128. This array holds both the primary and |
| 50 | // the logical partitions, to simplify data retrieval for GPT conversions. |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 51 | MBRPart partitions[MAX_MBR_PARTS]; |
srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 52 | uint16_t MBRSignature; |
| 53 | |
| 54 | // Above are basic MBR data; now add more stuff.... |
| 55 | uint32_t blockSize; // block size (usually 512) |
| 56 | uint64_t diskSize; // size in blocks |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 57 | uint32_t numHeads; // number of heads, in CHS scheme |
| 58 | uint32_t numSecspTrack; // number of sectors per track, in CHS scheme |
srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 59 | DiskIO* myDisk; |
| 60 | int canDeleteMyDisk; |
| 61 | string device; |
| 62 | MBRValidity state; |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 63 | MBRPart* GetPartition(int i); // Return primary or logical partition |
srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 64 | public: |
| 65 | BasicMBRData(void); |
| 66 | BasicMBRData(string deviceFilename); |
Rod Smith | 9ae6019 | 2018-07-05 16:50:13 -0400 | [diff] [blame] | 67 | BasicMBRData(const BasicMBRData &); |
srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 68 | ~BasicMBRData(void); |
| 69 | BasicMBRData & operator=(const BasicMBRData & orig); |
| 70 | |
| 71 | // File I/O functions... |
| 72 | int ReadMBRData(const string & deviceFilename); |
| 73 | int ReadMBRData(DiskIO * theDisk, int checkBlockSize = 1); |
srs5694 | 23d8d54 | 2011-10-01 18:40:10 -0400 | [diff] [blame] | 74 | int ReadLogicalParts(uint64_t extendedStart, int partNum); |
srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 75 | int WriteMBRData(void); |
| 76 | int WriteMBRData(DiskIO *theDisk); |
| 77 | int WriteMBRData(const string & deviceFilename); |
| 78 | int WriteMBRData(struct TempMBR & mbr, DiskIO *theDisk, uint64_t sector); |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 79 | void DiskSync(void) {myDisk->DiskSync();} |
| 80 | void SetDisk(DiskIO *theDisk); |
srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 81 | |
| 82 | // Display data for user... |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 83 | void DisplayMBRData(void); |
srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 84 | void ShowState(void); |
| 85 | |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 86 | // GPT checks and fixes... |
| 87 | int CheckForGPT(void); |
| 88 | int BlankGPTData(void); |
| 89 | |
srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 90 | // Functions that set or get disk metadata (size, CHS geometry, etc.) |
| 91 | void SetDiskSize(uint64_t ds) {diskSize = ds;} |
| 92 | void SetBlockSize(uint32_t bs) {blockSize = bs;} |
| 93 | MBRValidity GetValidity(void) {return state;} |
| 94 | void SetHybrid(void) {state = hybrid;} // Set hybrid flag |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 95 | void ReadCHSGeom(void); |
| 96 | int GetPartRange(uint32_t* low, uint32_t* high); |
srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 97 | int LBAtoCHS(uint64_t lba, uint8_t * chs); // Convert LBA to CHS |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 98 | int FindOverlaps(void); |
| 99 | int NumPrimaries(void); |
| 100 | int NumLogicals(void); |
| 101 | int CountParts(void); |
| 102 | void UpdateCanBeLogical(void); |
| 103 | uint64_t FirstLogicalLBA(void); |
| 104 | uint64_t LastLogicalLBA(void); |
| 105 | int AreLogicalsContiguous(void); |
| 106 | int DoTheyFit(void); |
| 107 | int SpaceBeforeAllLogicals(void); |
| 108 | int IsLegal(void); |
Roderick W. Smith | 042f38a | 2013-08-31 17:40:15 -0400 | [diff] [blame] | 109 | int IsEEActive(void); |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 110 | int FindNextInUse(int start); |
srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 111 | |
| 112 | // Functions to create, delete, or change partitions |
| 113 | // Pass EmptyMBR 1 to clear the boot loader code, 0 to leave it intact |
| 114 | void EmptyMBR(int clearBootloader = 1); |
| 115 | void EmptyBootloader(void); |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 116 | void AddPart(int num, const MBRPart& newPart); |
| 117 | void MakePart(int num, uint64_t startLBA, uint64_t lengthLBA, int type = 0x07, |
srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 118 | int bootable = 0); |
| 119 | int SetPartType(int num, int type); |
| 120 | int SetPartBootable(int num, int bootable = 1); |
| 121 | int MakeBiggestPart(int i, int type); // Make partition filling most space |
| 122 | void DeletePartition(int i); |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 123 | int SetInclusionwChecks(int num, int inclStatus); |
srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 124 | void RecomputeCHS(int partNum); |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 125 | void SortMBR(int start = 0); |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 126 | int DeleteOversizedParts(); |
| 127 | int DeleteExtendedParts(); |
| 128 | void OmitOverlaps(void); |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 129 | void MaximizeLogicals(); |
| 130 | void MaximizePrimaries(); |
| 131 | void TrimPrimaries(); |
| 132 | void MakeLogicalsContiguous(void); |
| 133 | void MakeItLegal(void); |
| 134 | int RemoveLogicalsFromFirstFour(void); |
| 135 | int MovePrimariesToFirstFour(void); |
| 136 | int CreateExtended(void); |
srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 137 | |
| 138 | // Functions to find information on free space.... |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 139 | uint64_t FindFirstAvailable(uint64_t start = 1); |
| 140 | uint64_t FindLastInFree(uint64_t start); |
| 141 | uint64_t FindFirstInFree(uint64_t start); |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 142 | int SectorUsedAs(uint64_t sector, int topPartNum = MAX_MBR_PARTS); |
srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 143 | |
| 144 | // Functions to extract data on specific partitions.... |
| 145 | uint8_t GetStatus(int i); |
| 146 | uint8_t GetType(int i); |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 147 | uint64_t GetFirstSector(int i); |
| 148 | uint64_t GetLength(int i); |
| 149 | |
| 150 | // User interaction functions.... |
| 151 | int DoMenu(const string& prompt = "\nMBR command (? for help): "); |
| 152 | void ShowCommands(void); |
srs5694 | 699941e | 2011-03-21 21:33:57 -0400 | [diff] [blame] | 153 | }; // class BasicMBRData |
srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 154 | |
Tom Marshall | 7b5b711 | 2019-07-23 15:18:15 -0700 | [diff] [blame] | 155 | #pragma pack(pop) |
| 156 | |
srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 157 | #endif |