srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1 | /* gpt.h -- GPT and data structure definitions, types, and |
| 2 | functions */ |
| 3 | |
| 4 | #include <stdint.h> |
| 5 | #include <sys/types.h> |
| 6 | #include <sys/ioctl.h> |
| 7 | #include "support.h" |
| 8 | #include "parttypes.h" |
| 9 | #include "mbr.h" |
| 10 | |
| 11 | #ifndef __GPTSTRUCTS |
| 12 | #define __GPTSTRUCTS |
| 13 | |
| 14 | #define GPT_SIGNATURE UINT64_C(0x5452415020494645) |
| 15 | |
| 16 | /* Number and size of GPT entries... */ |
| 17 | #define NUM_GPT_ENTRIES 128 |
| 18 | #define GPT_SIZE 128 |
| 19 | /* Offset, in 512-byte sectors, for GPT table and partition data. |
| 20 | Note this is above two multiplied together, divided by 512, with 2 |
| 21 | added |
| 22 | #define GPT_OFFSET (((NUM_GPT_ENTRIES * GPT_SIZE) / SECTOR_SIZE) + 2) |
| 23 | */ |
| 24 | |
| 25 | #define HEADER_SIZE 92 |
| 26 | |
| 27 | #define GPT_RESERVED 420 |
| 28 | #define NAME_SIZE 72 |
| 29 | |
| 30 | using namespace std; |
| 31 | |
| 32 | /**************************************** |
| 33 | * * |
| 34 | * GPTData class and related structures * |
| 35 | * * |
| 36 | ****************************************/ |
| 37 | |
| 38 | // Validity state of GPT data |
| 39 | enum GPTValidity {gpt_valid, gpt_corrupt, gpt_invalid}; |
| 40 | |
| 41 | // Which set of partition data to use |
| 42 | enum WhichToUse {use_gpt, use_mbr, use_new}; |
| 43 | |
| 44 | // Header (first 512 bytes) of GPT table |
| 45 | struct GPTHeader { |
| 46 | uint64_t signature; |
| 47 | uint32_t revision; |
| 48 | uint32_t headerSize; |
| 49 | uint32_t headerCRC; |
| 50 | uint32_t reserved; |
| 51 | uint64_t currentLBA; |
| 52 | uint64_t backupLBA; |
| 53 | uint64_t firstUsableLBA; |
| 54 | uint64_t lastUsableLBA; |
| 55 | struct GUIDData diskGUID; |
| 56 | uint64_t partitionEntriesLBA; |
| 57 | uint32_t numParts; |
| 58 | uint32_t sizeOfPartitionEntries; |
| 59 | uint32_t partitionEntriesCRC; |
| 60 | unsigned char reserved2[GPT_RESERVED]; |
| 61 | }; // struct GPTHeader |
| 62 | |
| 63 | struct GPTPartition { |
| 64 | struct GUIDData partitionType; |
| 65 | struct GUIDData uniqueGUID; |
| 66 | uint64_t firstLBA; |
| 67 | uint64_t lastLBA; |
| 68 | uint64_t attributes; |
| 69 | unsigned char name[NAME_SIZE]; |
| 70 | }; // struct GPTPartition |
| 71 | |
| 72 | // Data in GPT format |
| 73 | class GPTData { |
| 74 | protected: |
| 75 | struct GPTHeader mainHeader; |
| 76 | struct GPTPartition *partitions; |
| 77 | struct GPTHeader secondHeader; |
| 78 | MBRData protectiveMBR; |
| 79 | char device[256]; // device filename |
| 80 | uint32_t blockSize; // device block size |
| 81 | uint64_t diskSize; // size of device, in blocks |
| 82 | GPTValidity state; // is GPT valid? |
| 83 | int mainCrcOk; |
| 84 | int secondCrcOk; |
| 85 | int mainPartsCrcOk; |
| 86 | int secondPartsCrcOk; |
| 87 | // uint32_t units; // display units, in multiples of sectors |
| 88 | PartTypes typeHelper; |
| 89 | public: |
| 90 | GPTData(void); |
| 91 | GPTData(char* deviceFilename); |
| 92 | ~GPTData(void); |
| 93 | int SetGPTSize(uint32_t numEntries); |
| 94 | int CheckGPTSize(void); |
| 95 | int LoadPartitions(char* deviceFilename); |
| 96 | int ForceLoadGPTData(int fd); |
| 97 | int LoadMainTable(void); |
| 98 | WhichToUse UseWhichPartitions(void); |
| 99 | void ResizePartitionTable(void); |
| 100 | int GetPartRange(uint32_t* low, uint32_t* high); |
| 101 | void DisplayGPTData(void); |
| 102 | void DisplayMBRData(void) {protectiveMBR.DisplayMBRData();} |
| 103 | void ShowDetails(void); |
| 104 | void ShowPartDetails(uint32_t partNum); |
| 105 | void CreatePartition(void); |
| 106 | void DeletePartition(void); |
| 107 | void BlankPartitions(void); |
| 108 | uint64_t FindFirstAvailable(uint64_t start = 0); |
| 109 | uint64_t FindLastAvailable(uint64_t start); |
| 110 | uint64_t FindLastInFree(uint64_t start); |
| 111 | int IsFree(uint64_t sector); |
| 112 | int XFormPartitions(MBRData* origParts); |
| 113 | void SortGPT(void); |
| 114 | int ClearGPTData(void); |
| 115 | void ChangePartType(void); |
| 116 | uint32_t GetPartNum(void); |
| 117 | void SetAttributes(uint32_t partNum); |
| 118 | void SetName(uint32_t partNum, char* theName = NULL); |
| 119 | void SetDiskGUID(GUIDData newGUID); |
| 120 | int SetPartitionGUID(uint32_t pn, GUIDData theGUID); |
| 121 | int CheckHeaderValidity(void); |
| 122 | int CheckHeaderCRC(struct GPTHeader* header); |
| 123 | void RecomputeCRCs(void); |
| 124 | int Verify(void); |
| 125 | void RebuildMainHeader(void); |
| 126 | void RebuildSecondHeader(void); |
| 127 | void LoadSecondTableAsMain(void); |
| 128 | uint64_t FindFreeBlocks(int *numSegments, uint64_t *largestSegment); |
| 129 | // void MakeHybrid(void); |
| 130 | void MakeProtectiveMBR(void) {return protectiveMBR.MakeProtectiveMBR();} |
| 131 | int SaveGPTData(void); |
| 132 | int SaveGPTBackup(char* filename); |
| 133 | int LoadGPTBackup(char* filename); |
| 134 | |
| 135 | // Return data about the GPT structures.... |
| 136 | uint32_t GetNumParts(void) {return mainHeader.numParts;} |
| 137 | uint64_t GetMainHeaderLBA(void) {return mainHeader.currentLBA;} |
| 138 | uint64_t GetSecondHeaderLBA(void) {return secondHeader.currentLBA;} |
| 139 | uint64_t GetMainPartsLBA(void) {return mainHeader.partitionEntriesLBA;} |
| 140 | uint64_t GetSecondPartsLBA(void) {return secondHeader.partitionEntriesLBA;} |
| 141 | uint64_t GetBlocksInPartTable(void) {return (mainHeader.numParts * |
| 142 | mainHeader.sizeOfPartitionEntries) / blockSize;} |
| 143 | }; // class GPTData |
| 144 | |
| 145 | // Function prototypes.... |
| 146 | void BlankPartition(struct GPTPartition* partition); |
| 147 | //int XFormType(uint8_t oldType, struct GUIDData* newType, int partNum); |
| 148 | void QuickSortGPT(struct GPTPartition* partitions, int start, int finish); |
| 149 | int TheyOverlap(struct GPTPartition* first, struct GPTPartition* second); |
| 150 | void ChangeGPTType(struct GPTPartition* part); |
| 151 | int SizesOK(void); |
| 152 | |
| 153 | #endif |