srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1 | /* gpt.h -- GPT and data structure definitions, types, and |
| 2 | functions */ |
| 3 | |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame^] | 4 | /* This program is copyright (c) 2009 by Roderick W. Smith. It is distributed |
| 5 | under the terms of the GNU GPL version 2, as detailed in the COPYING file. */ |
| 6 | |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 7 | #include <stdint.h> |
| 8 | #include <sys/types.h> |
| 9 | #include <sys/ioctl.h> |
| 10 | #include "support.h" |
| 11 | #include "parttypes.h" |
| 12 | #include "mbr.h" |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame^] | 13 | #include "bsd.h" |
| 14 | #include "gptpart.h" |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 15 | |
| 16 | #ifndef __GPTSTRUCTS |
| 17 | #define __GPTSTRUCTS |
| 18 | |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 19 | |
| 20 | using namespace std; |
| 21 | |
| 22 | /**************************************** |
| 23 | * * |
| 24 | * GPTData class and related structures * |
| 25 | * * |
| 26 | ****************************************/ |
| 27 | |
| 28 | // Validity state of GPT data |
| 29 | enum GPTValidity {gpt_valid, gpt_corrupt, gpt_invalid}; |
| 30 | |
| 31 | // Which set of partition data to use |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame^] | 32 | enum WhichToUse {use_gpt, use_mbr, use_bsd, use_new}; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 33 | |
| 34 | // Header (first 512 bytes) of GPT table |
| 35 | struct GPTHeader { |
| 36 | uint64_t signature; |
| 37 | uint32_t revision; |
| 38 | uint32_t headerSize; |
| 39 | uint32_t headerCRC; |
| 40 | uint32_t reserved; |
| 41 | uint64_t currentLBA; |
| 42 | uint64_t backupLBA; |
| 43 | uint64_t firstUsableLBA; |
| 44 | uint64_t lastUsableLBA; |
| 45 | struct GUIDData diskGUID; |
| 46 | uint64_t partitionEntriesLBA; |
| 47 | uint32_t numParts; |
| 48 | uint32_t sizeOfPartitionEntries; |
| 49 | uint32_t partitionEntriesCRC; |
| 50 | unsigned char reserved2[GPT_RESERVED]; |
| 51 | }; // struct GPTHeader |
| 52 | |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 53 | // Data in GPT format |
| 54 | class GPTData { |
| 55 | protected: |
| 56 | struct GPTHeader mainHeader; |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame^] | 57 | struct GPTPart *partitions; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 58 | struct GPTHeader secondHeader; |
| 59 | MBRData protectiveMBR; |
| 60 | char device[256]; // device filename |
| 61 | uint32_t blockSize; // device block size |
| 62 | uint64_t diskSize; // size of device, in blocks |
| 63 | GPTValidity state; // is GPT valid? |
| 64 | int mainCrcOk; |
| 65 | int secondCrcOk; |
| 66 | int mainPartsCrcOk; |
| 67 | int secondPartsCrcOk; |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame^] | 68 | int apmFound; // set to 1 if APM detected |
| 69 | int bsdFound; // set to 1 if BSD disklabel detected in MBR |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 70 | // uint32_t units; // display units, in multiples of sectors |
| 71 | PartTypes typeHelper; |
| 72 | public: |
| 73 | GPTData(void); |
| 74 | GPTData(char* deviceFilename); |
| 75 | ~GPTData(void); |
| 76 | int SetGPTSize(uint32_t numEntries); |
| 77 | int CheckGPTSize(void); |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame^] | 78 | void ShowAPMState(void); |
| 79 | void ShowGPTState(void); |
| 80 | void PartitionScan(int fd); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 81 | int LoadPartitions(char* deviceFilename); |
| 82 | int ForceLoadGPTData(int fd); |
| 83 | int LoadMainTable(void); |
| 84 | WhichToUse UseWhichPartitions(void); |
| 85 | void ResizePartitionTable(void); |
| 86 | int GetPartRange(uint32_t* low, uint32_t* high); |
| 87 | void DisplayGPTData(void); |
| 88 | void DisplayMBRData(void) {protectiveMBR.DisplayMBRData();} |
| 89 | void ShowDetails(void); |
| 90 | void ShowPartDetails(uint32_t partNum); |
| 91 | void CreatePartition(void); |
| 92 | void DeletePartition(void); |
| 93 | void BlankPartitions(void); |
| 94 | uint64_t FindFirstAvailable(uint64_t start = 0); |
| 95 | uint64_t FindLastAvailable(uint64_t start); |
| 96 | uint64_t FindLastInFree(uint64_t start); |
| 97 | int IsFree(uint64_t sector); |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame^] | 98 | int XFormPartitions(void); |
| 99 | int XFormDisklabel(int OnGptPart = -1); |
| 100 | int XFormDisklabel(BSDData* disklabel, int startPart); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 101 | void SortGPT(void); |
| 102 | int ClearGPTData(void); |
| 103 | void ChangePartType(void); |
| 104 | uint32_t GetPartNum(void); |
| 105 | void SetAttributes(uint32_t partNum); |
| 106 | void SetName(uint32_t partNum, char* theName = NULL); |
| 107 | void SetDiskGUID(GUIDData newGUID); |
| 108 | int SetPartitionGUID(uint32_t pn, GUIDData theGUID); |
| 109 | int CheckHeaderValidity(void); |
| 110 | int CheckHeaderCRC(struct GPTHeader* header); |
| 111 | void RecomputeCRCs(void); |
| 112 | int Verify(void); |
| 113 | void RebuildMainHeader(void); |
| 114 | void RebuildSecondHeader(void); |
| 115 | void LoadSecondTableAsMain(void); |
| 116 | uint64_t FindFreeBlocks(int *numSegments, uint64_t *largestSegment); |
srs5694 | c0ca8f8 | 2009-08-20 21:35:25 -0400 | [diff] [blame] | 117 | void MakeHybrid(void); |
| 118 | void MakeProtectiveMBR(void); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 119 | int SaveGPTData(void); |
| 120 | int SaveGPTBackup(char* filename); |
| 121 | int LoadGPTBackup(char* filename); |
srs5694 | c0ca8f8 | 2009-08-20 21:35:25 -0400 | [diff] [blame] | 122 | int DestroyGPT(void); // Returns 1 if user proceeds |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame^] | 123 | |
| 124 | // Endianness functions |
srs5694 | 2a9f5da | 2009-08-26 00:48:01 -0400 | [diff] [blame] | 125 | void ReverseHeaderBytes(struct GPTHeader* header); // for endianness |
| 126 | void ReversePartitionBytes(); // for endianness |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 127 | |
| 128 | // Return data about the GPT structures.... |
| 129 | uint32_t GetNumParts(void) {return mainHeader.numParts;} |
| 130 | uint64_t GetMainHeaderLBA(void) {return mainHeader.currentLBA;} |
| 131 | uint64_t GetSecondHeaderLBA(void) {return secondHeader.currentLBA;} |
| 132 | uint64_t GetMainPartsLBA(void) {return mainHeader.partitionEntriesLBA;} |
| 133 | uint64_t GetSecondPartsLBA(void) {return secondHeader.partitionEntriesLBA;} |
| 134 | uint64_t GetBlocksInPartTable(void) {return (mainHeader.numParts * |
| 135 | mainHeader.sizeOfPartitionEntries) / blockSize;} |
| 136 | }; // class GPTData |
| 137 | |
| 138 | // Function prototypes.... |
| 139 | void BlankPartition(struct GPTPartition* partition); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 140 | int TheyOverlap(struct GPTPartition* first, struct GPTPartition* second); |
| 141 | void ChangeGPTType(struct GPTPartition* part); |
| 142 | int SizesOK(void); |
| 143 | |
| 144 | #endif |