srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame^] | 1 | /* mbr.h -- MBR data structure definitions, types, and functions */ |
| 2 | |
| 3 | #include <stdint.h> |
| 4 | #include <sys/types.h> |
| 5 | #include <sys/ioctl.h> |
| 6 | |
| 7 | #ifndef __MBRSTRUCTS |
| 8 | #define __MBRSTRUCTS |
| 9 | |
| 10 | #define MBR_SIGNATURE UINT16_C(0xAA55) |
| 11 | |
| 12 | // Maximum number of logical partitions supported |
| 13 | #define NUM_LOGICALS 124 |
| 14 | |
| 15 | using namespace std; |
| 16 | |
| 17 | /**************************************** |
| 18 | * * |
| 19 | * MBRData class and related structures * |
| 20 | * * |
| 21 | ****************************************/ |
| 22 | |
| 23 | // Data for a single MBR partition record |
| 24 | // Note that firstSector and lastSector are in CHS addressing, which |
| 25 | // splits the bits up in a weird way. |
| 26 | struct MBRRecord { |
| 27 | uint8_t status; |
| 28 | uint8_t firstSector[3]; |
| 29 | uint8_t partitionType; |
| 30 | uint8_t lastSector[3]; |
| 31 | uint32_t firstLBA; |
| 32 | uint32_t lengthLBA; |
| 33 | }; // struct MBRRecord |
| 34 | |
| 35 | // Extended Boot Record (EBR) data, used to hold one logical partition's |
| 36 | // data within an extended partition. Includes pointer to next record for |
| 37 | // in-memory linked-list access. This is similar to MBRData, but with a |
| 38 | // few tweaks.... |
| 39 | struct EBRRecord { |
| 40 | uint8_t code[446]; // generally 0s (and we don't care if they aren't) |
| 41 | // First partition entry defines partition; second points to next |
| 42 | // entry in on-disk linked list; remaining two are unused. Note that |
| 43 | // addresses are relative to the extended partition, not to the disk |
| 44 | // as a whole. |
| 45 | struct MBRRecord partitions[4]; |
| 46 | uint16_t MBRSignature; |
| 47 | }; // struct EBRRecord |
| 48 | |
| 49 | // Possible states of the MBR |
| 50 | enum MBRValidity {invalid, gpt, hybrid, mbr}; |
| 51 | |
| 52 | // Full data in tweaked MBR format |
| 53 | class MBRData { |
| 54 | protected: |
| 55 | uint8_t code[440]; |
| 56 | uint32_t diskSignature; |
| 57 | uint16_t nulls; |
| 58 | struct MBRRecord partitions[4]; |
| 59 | uint16_t MBRSignature; |
| 60 | |
| 61 | // Above are basic MBR data; now add more stuff.... |
| 62 | uint32_t blockSize; // block size (usually 512) |
| 63 | uint64_t diskSize; // size in blocks |
| 64 | char device[256]; |
| 65 | // Now an array of partitions for the logicals, in array form (easier |
| 66 | // than a linked list, and good enough for the GPT itself, so....) |
| 67 | struct MBRRecord logicals[NUM_LOGICALS]; |
| 68 | MBRValidity state; |
| 69 | struct MBRRecord* GetPartition(int i); // Return primary or logical partition |
| 70 | public: |
| 71 | MBRData(void); |
| 72 | MBRData(char* deviceFilename); |
| 73 | ~MBRData(void); |
| 74 | void EmptyMBR(void); |
| 75 | int ReadMBRData(char* deviceFilename); |
| 76 | void ReadMBRData(int fd); |
| 77 | int WriteMBRData(void); |
| 78 | void WriteMBRData(int fd); |
| 79 | int ReadLogicalPart(int fd, uint32_t extendedStart, uint32_t diskOffset, |
| 80 | int partNum); |
| 81 | void DisplayMBRData(void); |
| 82 | void MakeProtectiveMBR(void); |
| 83 | MBRValidity GetValidity(void) {return state;} |
| 84 | void ShowState(void); |
| 85 | void MakePart(int num, uint32_t startLBA, uint32_t lengthLBA, int type = 0x07, |
| 86 | int bootable = 0); |
| 87 | |
| 88 | // Functions to extract data on specific partitions.... |
| 89 | uint8_t GetStatus(int i); |
| 90 | uint8_t GetType(int i); |
| 91 | uint32_t GetFirstSector(int i); |
| 92 | uint32_t GetLength(int i); |
| 93 | }; // struct MBRData |
| 94 | |
| 95 | #endif |