| 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 | 1d1448a | 2009-12-31 21:20:19 -0500 | [diff] [blame^] | 70 | int sectorAlignment; // Start & end partitions at multiples of sectorAlignment |
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 71 | PartTypes typeHelper; |
| 72 | public: |
| srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 73 | // Basic necessary functions.... |
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 74 | GPTData(void); |
| 75 | GPTData(char* deviceFilename); |
| 76 | ~GPTData(void); |
| srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 77 | |
| 78 | // Verify (or update) data integrity |
| 79 | int Verify(void); |
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 80 | int CheckGPTSize(void); |
| srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 81 | int CheckHeaderValidity(void); |
| 82 | int CheckHeaderCRC(struct GPTHeader* header); |
| 83 | void RecomputeCRCs(void); |
| 84 | void RebuildMainHeader(void); |
| 85 | void RebuildSecondHeader(void); |
| 86 | int FindHybridMismatches(void); |
| 87 | int FindOverlaps(void); |
| 88 | |
| 89 | // Load or save data from/to disk |
| srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 90 | int LoadMBR(char* f) {return protectiveMBR.ReadMBRData(f);} |
| srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 91 | void PartitionScan(int fd); |
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 92 | int LoadPartitions(char* deviceFilename); |
| 93 | int ForceLoadGPTData(int fd); |
| 94 | int LoadMainTable(void); |
| srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 95 | void LoadSecondTableAsMain(void); |
| 96 | int SaveGPTData(void); |
| 97 | int SaveGPTBackup(char* filename); |
| 98 | int LoadGPTBackup(char* filename); |
| 99 | |
| 100 | // Display data.... |
| 101 | void ShowAPMState(void); |
| 102 | void ShowGPTState(void); |
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 103 | void DisplayGPTData(void); |
| 104 | void DisplayMBRData(void) {protectiveMBR.DisplayMBRData();} |
| 105 | void ShowDetails(void); |
| 106 | void ShowPartDetails(uint32_t partNum); |
| srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 107 | |
| 108 | // Request information from the user (& possibly do something with it) |
| 109 | uint32_t GetPartNum(void); |
| 110 | void ResizePartitionTable(void); |
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 111 | void CreatePartition(void); |
| 112 | void DeletePartition(void); |
| srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 113 | void ChangePartType(void); |
| 114 | void SetAttributes(uint32_t partNum); |
| srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 115 | int DestroyGPT(int prompt = 1); // Returns 1 if user proceeds |
| srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 116 | |
| srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 117 | // Convert between GPT and other formats (may require user interaction) |
| srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 118 | WhichToUse UseWhichPartitions(void); |
| srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 119 | int XFormPartitions(void); |
| 120 | int XFormDisklabel(int OnGptPart = -1); |
| 121 | int XFormDisklabel(BSDData* disklabel, int startPart); |
| srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 122 | int OnePartToMBR(uint32_t gptPart, int mbrPart); // add one partition to MBR. Returns 1 if successful |
| 123 | int XFormToMBR(void); // convert GPT to MBR, wiping GPT afterwards. Returns 1 if successful |
| srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 124 | void MakeHybrid(void); |
| 125 | |
| 126 | // Adjust GPT structures WITHOUT user interaction... |
| 127 | int SetGPTSize(uint32_t numEntries); |
| 128 | void BlankPartitions(void); |
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 129 | void SortGPT(void); |
| 130 | int ClearGPTData(void); |
| srs5694 | 247657a | 2009-11-26 18:36:12 -0500 | [diff] [blame] | 131 | void MoveSecondHeaderToEnd(); |
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 132 | void SetName(uint32_t partNum, char* theName = NULL); |
| 133 | void SetDiskGUID(GUIDData newGUID); |
| 134 | int SetPartitionGUID(uint32_t pn, GUIDData theGUID); |
| srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 135 | void MakeProtectiveMBR(void) {protectiveMBR.MakeProtectiveMBR();} |
| srs5694 | 1d1448a | 2009-12-31 21:20:19 -0500 | [diff] [blame^] | 136 | int Align(uint64_t* sector); |
| 137 | void SetAlignment(int n) {sectorAlignment = n;} |
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 138 | |
| 139 | // Return data about the GPT structures.... |
| srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 140 | int GetPartRange(uint32_t* low, uint32_t* high); |
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 141 | uint32_t GetNumParts(void) {return mainHeader.numParts;} |
| 142 | uint64_t GetMainHeaderLBA(void) {return mainHeader.currentLBA;} |
| 143 | uint64_t GetSecondHeaderLBA(void) {return secondHeader.currentLBA;} |
| 144 | uint64_t GetMainPartsLBA(void) {return mainHeader.partitionEntriesLBA;} |
| 145 | uint64_t GetSecondPartsLBA(void) {return secondHeader.partitionEntriesLBA;} |
| 146 | uint64_t GetBlocksInPartTable(void) {return (mainHeader.numParts * |
| 147 | mainHeader.sizeOfPartitionEntries) / blockSize;} |
| srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 148 | uint32_t CountParts(void); |
| srs5694 | 1d1448a | 2009-12-31 21:20:19 -0500 | [diff] [blame^] | 149 | int GetAlignment(void) {return sectorAlignment;} |
| srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 150 | |
| 151 | // Find information about free space |
| 152 | uint64_t FindFirstAvailable(uint64_t start = 0); |
| 153 | uint64_t FindFirstInLargest(void); |
| 154 | uint64_t FindLastAvailable(uint64_t start); |
| 155 | uint64_t FindLastInFree(uint64_t start); |
| 156 | uint64_t FindFreeBlocks(int *numSegments, uint64_t *largestSegment); |
| 157 | int IsFree(uint64_t sector); |
| 158 | |
| 159 | // Endianness functions |
| 160 | void ReverseHeaderBytes(struct GPTHeader* header); // for endianness |
| 161 | void ReversePartitionBytes(); // for endianness |
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 162 | }; // class GPTData |
| 163 | |
| 164 | // Function prototypes.... |
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 165 | int SizesOK(void); |
| 166 | |
| 167 | #endif |