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