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 | |
| 21 | class PartNotes; |
| 22 | |
| 23 | /**************************************** |
| 24 | * * |
| 25 | * MBRData class and related structures * |
| 26 | * * |
| 27 | ****************************************/ |
| 28 | |
srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 29 | // A 512-byte data structure into which the MBR can be loaded in one |
| 30 | // go. Also used when loading logical partitions. |
| 31 | #pragma pack(1) |
| 32 | struct TempMBR { |
| 33 | uint8_t code[440]; |
| 34 | uint32_t diskSignature; |
| 35 | uint16_t nulls; |
| 36 | struct MBRRecord partitions[4]; |
| 37 | uint16_t MBRSignature; |
| 38 | }; // struct TempMBR |
| 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); |
| 67 | ~BasicMBRData(void); |
| 68 | BasicMBRData & operator=(const BasicMBRData & orig); |
| 69 | |
| 70 | // File I/O functions... |
| 71 | int ReadMBRData(const string & deviceFilename); |
| 72 | int ReadMBRData(DiskIO * theDisk, int checkBlockSize = 1); |
| 73 | // ReadLogicalPart() returns last partition # read to logicals[] array, |
| 74 | // or -1 if there was a problem.... |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame^] | 75 | int ReadLogicalPart(uint64_t extendedStart, uint64_t diskOffset, |
srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 76 | int partNum); |
| 77 | int WriteMBRData(void); |
| 78 | int WriteMBRData(DiskIO *theDisk); |
| 79 | int WriteMBRData(const string & deviceFilename); |
| 80 | int WriteMBRData(struct TempMBR & mbr, DiskIO *theDisk, uint64_t sector); |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame^] | 81 | void DiskSync(void) {myDisk->DiskSync();} |
| 82 | void SetDisk(DiskIO *theDisk); |
srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 83 | |
| 84 | // Display data for user... |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame^] | 85 | void DisplayMBRData(void); |
srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 86 | void ShowState(void); |
| 87 | |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame^] | 88 | // GPT checks and fixes... |
| 89 | int CheckForGPT(void); |
| 90 | int BlankGPTData(void); |
| 91 | |
srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 92 | // Functions that set or get disk metadata (size, CHS geometry, etc.) |
| 93 | void SetDiskSize(uint64_t ds) {diskSize = ds;} |
| 94 | void SetBlockSize(uint32_t bs) {blockSize = bs;} |
| 95 | MBRValidity GetValidity(void) {return state;} |
| 96 | void SetHybrid(void) {state = hybrid;} // Set hybrid flag |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame^] | 97 | void ReadCHSGeom(void); |
| 98 | int GetPartRange(uint32_t* low, uint32_t* high); |
srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 99 | int LBAtoCHS(uint64_t lba, uint8_t * chs); // Convert LBA to CHS |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame^] | 100 | int FindOverlaps(void); |
| 101 | int NumPrimaries(void); |
| 102 | int NumLogicals(void); |
| 103 | int CountParts(void); |
| 104 | void UpdateCanBeLogical(void); |
| 105 | uint64_t FirstLogicalLBA(void); |
| 106 | uint64_t LastLogicalLBA(void); |
| 107 | int AreLogicalsContiguous(void); |
| 108 | int DoTheyFit(void); |
| 109 | int SpaceBeforeAllLogicals(void); |
| 110 | int IsLegal(void); |
| 111 | int FindNextInUse(int start); |
srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 112 | |
| 113 | // Functions to create, delete, or change partitions |
| 114 | // Pass EmptyMBR 1 to clear the boot loader code, 0 to leave it intact |
| 115 | void EmptyMBR(int clearBootloader = 1); |
| 116 | void EmptyBootloader(void); |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame^] | 117 | void AddPart(int num, const MBRPart& newPart); |
| 118 | void MakePart(int num, uint64_t startLBA, uint64_t lengthLBA, int type = 0x07, |
srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 119 | int bootable = 0); |
| 120 | int SetPartType(int num, int type); |
| 121 | int SetPartBootable(int num, int bootable = 1); |
| 122 | int MakeBiggestPart(int i, int type); // Make partition filling most space |
| 123 | void DeletePartition(int i); |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame^] | 124 | int SetInclusionwChecks(int num, int inclStatus); |
srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 125 | void RecomputeCHS(int partNum); |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame^] | 126 | int SwapPartitions(uint32_t partNum1, uint32_t partNum2); |
| 127 | void SortMBR(int start = 0); |
| 128 | void QuickSortMBR(int start, int finish); |
| 129 | int DeleteOversizedParts(); |
| 130 | int DeleteExtendedParts(); |
| 131 | void OmitOverlaps(void); |
| 132 | // void OmitAll(void); |
| 133 | void MaximizeLogicals(); |
| 134 | void MaximizePrimaries(); |
| 135 | void TrimPrimaries(); |
| 136 | void MakeLogicalsContiguous(void); |
| 137 | void MakeItLegal(void); |
| 138 | int RemoveLogicalsFromFirstFour(void); |
| 139 | int MovePrimariesToFirstFour(void); |
| 140 | int CreateExtended(void); |
srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 141 | |
| 142 | // Functions to find information on free space.... |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame^] | 143 | uint64_t FindFirstAvailable(uint64_t start = 1); |
| 144 | uint64_t FindLastInFree(uint64_t start); |
| 145 | uint64_t FindFirstInFree(uint64_t start); |
| 146 | // int IsFree(uint64_t sector, int topPartNum = MAX_MBR_PARTS); |
| 147 | int SectorUsedAs(uint64_t sector, int topPartNum = MAX_MBR_PARTS); |
srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 148 | |
| 149 | // Functions to extract data on specific partitions.... |
| 150 | uint8_t GetStatus(int i); |
| 151 | uint8_t GetType(int i); |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame^] | 152 | uint64_t GetFirstSector(int i); |
| 153 | uint64_t GetLength(int i); |
| 154 | |
| 155 | // User interaction functions.... |
| 156 | int DoMenu(const string& prompt = "\nMBR command (? for help): "); |
| 157 | void ShowCommands(void); |
| 158 | |
srs5694 | f2efa7d | 2011-03-01 22:03:26 -0500 | [diff] [blame] | 159 | }; // struct BasicMBRData |
| 160 | |
| 161 | #endif |