srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1 | /* mbr.h -- MBR data structure definitions, types, and functions */ |
| 2 | |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 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 | |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 6 | #include <stdint.h> |
| 7 | #include <sys/types.h> |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 8 | #include "gptpart.h" |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame^] | 9 | #include "partnotes.h" |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 10 | #include "diskio.h" |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 11 | |
| 12 | #ifndef __MBRSTRUCTS |
| 13 | #define __MBRSTRUCTS |
| 14 | |
| 15 | #define MBR_SIGNATURE UINT16_C(0xAA55) |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 16 | #define MAX_HEADS 255 /* numbered 0 - 254 */ |
| 17 | #define MAX_SECSPERTRACK 63 /* numbered 1 - 63 */ |
| 18 | #define MAX_CYLINDERS 1024 /* numbered 0 - 1023 */ |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 19 | |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 20 | // Maximum number of MBR partitions |
| 21 | #define MAX_MBR_PARTS 128 |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 22 | |
| 23 | using namespace std; |
| 24 | |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame^] | 25 | class PartNotes; |
| 26 | |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 27 | /**************************************** |
| 28 | * * |
| 29 | * MBRData class and related structures * |
| 30 | * * |
| 31 | ****************************************/ |
| 32 | |
| 33 | // Data for a single MBR partition record |
| 34 | // Note that firstSector and lastSector are in CHS addressing, which |
| 35 | // splits the bits up in a weird way. |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame^] | 36 | // On read or write of MBR entries, firstLBA is an absolute disk sector. |
| 37 | // On read of logical entries, it's relative to the EBR record for that |
| 38 | // partition. When writing EBR records, it's relative to the extended |
| 39 | // partition's start. |
srs5694 | ba00fed | 2010-01-12 18:18:36 -0500 | [diff] [blame] | 40 | #pragma pack(1) |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 41 | struct MBRRecord { |
| 42 | uint8_t status; |
| 43 | uint8_t firstSector[3]; |
| 44 | uint8_t partitionType; |
| 45 | uint8_t lastSector[3]; |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame^] | 46 | uint32_t firstLBA; // see above |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 47 | uint32_t lengthLBA; |
| 48 | }; // struct MBRRecord |
| 49 | |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 50 | // A 512-byte data structure into which the MBR can be loaded in one |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 51 | // go. Also used when loading logical partitions. |
srs5694 | ba00fed | 2010-01-12 18:18:36 -0500 | [diff] [blame] | 52 | #pragma pack(1) |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 53 | struct TempMBR { |
| 54 | uint8_t code[440]; |
| 55 | uint32_t diskSignature; |
| 56 | uint16_t nulls; |
| 57 | struct MBRRecord partitions[4]; |
| 58 | uint16_t MBRSignature; |
| 59 | }; // struct TempMBR |
| 60 | |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 61 | // Possible states of the MBR |
| 62 | enum MBRValidity {invalid, gpt, hybrid, mbr}; |
| 63 | |
| 64 | // Full data in tweaked MBR format |
| 65 | class MBRData { |
| 66 | protected: |
| 67 | uint8_t code[440]; |
| 68 | uint32_t diskSignature; |
| 69 | uint16_t nulls; |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 70 | // MAX_MBR_PARTS defaults to 128. This array holds both the primary and |
| 71 | // the logical partitions, to simplify data retrieval for GPT conversions. |
| 72 | struct MBRRecord partitions[MAX_MBR_PARTS]; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 73 | uint16_t MBRSignature; |
| 74 | |
| 75 | // Above are basic MBR data; now add more stuff.... |
| 76 | uint32_t blockSize; // block size (usually 512) |
| 77 | uint64_t diskSize; // size in blocks |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 78 | uint64_t numHeads; // number of heads, in CHS scheme |
| 79 | uint64_t numSecspTrack; // number of sectors per track, in CHS scheme |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 80 | DiskIO* myDisk; |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 81 | int canDeleteMyDisk; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 82 | string device; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 83 | MBRValidity state; |
| 84 | struct MBRRecord* GetPartition(int i); // Return primary or logical partition |
| 85 | public: |
| 86 | MBRData(void); |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 87 | MBRData(string deviceFilename); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 88 | ~MBRData(void); |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 89 | |
| 90 | // File I/O functions... |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 91 | int ReadMBRData(const string & deviceFilename); |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 92 | void ReadMBRData(DiskIO * theDisk, int checkBlockSize = 1); |
srs5694 | ba00fed | 2010-01-12 18:18:36 -0500 | [diff] [blame] | 93 | // ReadLogicalPart() returns last partition # read to logicals[] array, |
| 94 | // or -1 if there was a problem.... |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 95 | int ReadLogicalPart(uint32_t extendedStart, uint32_t diskOffset, |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 96 | int partNum); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 97 | int WriteMBRData(void); |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 98 | int WriteMBRData(DiskIO *theDisk); |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 99 | int WriteMBRData(const string & deviceFilename); |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame^] | 100 | int WriteMBRData(struct TempMBR & mbr, DiskIO *theDisk, uint64_t sector); |
| 101 | void SetDisk(DiskIO *theDisk) {myDisk = theDisk; canDeleteMyDisk = 0;} |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 102 | |
| 103 | // Display data for user... |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame^] | 104 | void DisplayMBRData(int maxParts = 4); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 105 | void ShowState(void); |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 106 | |
| 107 | // Functions that set or get disk metadata (size, CHS geometry, etc.) |
| 108 | void SetDiskSize(uint64_t ds) {diskSize = ds;} |
| 109 | MBRValidity GetValidity(void) {return state;} |
| 110 | void SetHybrid(void) {state = hybrid;} // Set hybrid flag |
| 111 | void SetCHSGeom(uint32_t h, uint32_t s); |
| 112 | int LBAtoCHS(uint64_t lba, uint8_t * chs); // Convert LBA to CHS |
| 113 | |
| 114 | // Functions to create, delete, or change partitions |
| 115 | // Pass EmptyMBR 1 to clear the boot loader code, 0 to leave it intact |
| 116 | void EmptyMBR(int clearBootloader = 1); |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 117 | void EmptyBootloader(void); |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 118 | void MakeProtectiveMBR(int clearBoot = 0); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 119 | void MakePart(int num, uint32_t startLBA, uint32_t lengthLBA, int type = 0x07, |
| 120 | int bootable = 0); |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 121 | int SetPartType(int num, int type); |
| 122 | int SetPartBootable(int num, int bootable = 1); |
srs5694 | c0ca8f8 | 2009-08-20 21:35:25 -0400 | [diff] [blame] | 123 | int MakeBiggestPart(int i, int type); // Make partition filling most space |
srs5694 | e35eb1b | 2009-09-14 00:29:34 -0400 | [diff] [blame] | 124 | void DeletePartition(int i); |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 125 | int DeleteByLocation(uint64_t start64, uint64_t length64); |
| 126 | void OptimizeEESize(void); |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame^] | 127 | int CreateLogicals(PartNotes& notes); |
srs5694 | c0ca8f8 | 2009-08-20 21:35:25 -0400 | [diff] [blame] | 128 | |
| 129 | // Functions to find information on free space.... |
| 130 | uint32_t FindFirstAvailable(uint32_t start = 1); |
| 131 | uint32_t FindLastInFree(uint32_t start); |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 132 | uint32_t FindFirstInFree(uint32_t start); |
| 133 | int IsFree(uint32_t sector); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 134 | |
| 135 | // Functions to extract data on specific partitions.... |
| 136 | uint8_t GetStatus(int i); |
| 137 | uint8_t GetType(int i); |
| 138 | uint32_t GetFirstSector(int i); |
| 139 | uint32_t GetLength(int i); |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 140 | GPTPart AsGPT(int i); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 141 | }; // struct MBRData |
| 142 | |
| 143 | #endif |