| 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> |
| srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 9 | #include "gptpart.h" |
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 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 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame^] | 19 | #define GPTFDISK_VERSION "0.6.2-pre2" |
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 20 | |
| 21 | using namespace std; |
| 22 | |
| 23 | /**************************************** |
| 24 | * * |
| 25 | * GPTData class and related structures * |
| 26 | * * |
| 27 | ****************************************/ |
| 28 | |
| 29 | // Validity state of GPT data |
| 30 | enum GPTValidity {gpt_valid, gpt_corrupt, gpt_invalid}; |
| 31 | |
| 32 | // Which set of partition data to use |
| srs5694 | 3c0af38 | 2010-01-15 19:19:18 -0500 | [diff] [blame] | 33 | enum WhichToUse {use_gpt, use_mbr, use_bsd, use_new, use_abort}; |
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 34 | |
| 35 | // Header (first 512 bytes) of GPT table |
| srs5694 | ba00fed | 2010-01-12 18:18:36 -0500 | [diff] [blame] | 36 | #pragma pack(1) |
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 37 | struct GPTHeader { |
| 38 | uint64_t signature; |
| 39 | uint32_t revision; |
| 40 | uint32_t headerSize; |
| 41 | uint32_t headerCRC; |
| 42 | uint32_t reserved; |
| 43 | uint64_t currentLBA; |
| 44 | uint64_t backupLBA; |
| 45 | uint64_t firstUsableLBA; |
| 46 | uint64_t lastUsableLBA; |
| 47 | struct GUIDData diskGUID; |
| 48 | uint64_t partitionEntriesLBA; |
| 49 | uint32_t numParts; |
| 50 | uint32_t sizeOfPartitionEntries; |
| 51 | uint32_t partitionEntriesCRC; |
| 52 | unsigned char reserved2[GPT_RESERVED]; |
| 53 | }; // struct GPTHeader |
| 54 | |
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 55 | // Data in GPT format |
| 56 | class GPTData { |
| 57 | protected: |
| 58 | struct GPTHeader mainHeader; |
| srs5694 | ba00fed | 2010-01-12 18:18:36 -0500 | [diff] [blame] | 59 | GPTPart *partitions; |
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 60 | struct GPTHeader secondHeader; |
| 61 | MBRData protectiveMBR; |
| srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame^] | 62 | string device; // device filename |
| srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 63 | DiskIO myDisk; |
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 64 | uint32_t blockSize; // device block size |
| 65 | uint64_t diskSize; // size of device, in blocks |
| 66 | GPTValidity state; // is GPT valid? |
| srs5694 | 5d58fe0 | 2010-01-03 20:57:08 -0500 | [diff] [blame] | 67 | int justLooking; // Set to 1 if program launched with "-l" or if read-only |
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 68 | int mainCrcOk; |
| 69 | int secondCrcOk; |
| 70 | int mainPartsCrcOk; |
| 71 | int secondPartsCrcOk; |
| srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 72 | int apmFound; // set to 1 if APM detected |
| 73 | int bsdFound; // set to 1 if BSD disklabel detected in MBR |
| srs5694 | 1d1448a | 2009-12-31 21:20:19 -0500 | [diff] [blame] | 74 | int sectorAlignment; // Start & end partitions at multiples of sectorAlignment |
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 75 | PartTypes typeHelper; |
| srs5694 | ba00fed | 2010-01-12 18:18:36 -0500 | [diff] [blame] | 76 | int beQuiet; |
| 77 | WhichToUse whichWasUsed; |
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 78 | public: |
| srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 79 | // Basic necessary functions.... |
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 80 | GPTData(void); |
| srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame^] | 81 | GPTData(string deviceFilename); |
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 82 | ~GPTData(void); |
| srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 83 | |
| 84 | // Verify (or update) data integrity |
| 85 | int Verify(void); |
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 86 | int CheckGPTSize(void); |
| srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 87 | int CheckHeaderValidity(void); |
| 88 | int CheckHeaderCRC(struct GPTHeader* header); |
| 89 | void RecomputeCRCs(void); |
| 90 | void RebuildMainHeader(void); |
| 91 | void RebuildSecondHeader(void); |
| 92 | int FindHybridMismatches(void); |
| 93 | int FindOverlaps(void); |
| 94 | |
| 95 | // Load or save data from/to disk |
| srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame^] | 96 | int LoadMBR(string f) {return protectiveMBR.ReadMBRData(f);} |
| srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 97 | void PartitionScan(void); |
| srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame^] | 98 | int LoadPartitions(string deviceFilename); |
| srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 99 | int ForceLoadGPTData(void); |
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 100 | int LoadMainTable(void); |
| srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 101 | int LoadSecondTableAsMain(void); |
| srs5694 | ba00fed | 2010-01-12 18:18:36 -0500 | [diff] [blame] | 102 | int SaveGPTData(int quiet = 0); |
| srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame^] | 103 | int SaveGPTBackup(string filename); |
| 104 | int LoadGPTBackup(string filename); |
| srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 105 | |
| 106 | // Display data.... |
| 107 | void ShowAPMState(void); |
| 108 | void ShowGPTState(void); |
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 109 | void DisplayGPTData(void); |
| 110 | void DisplayMBRData(void) {protectiveMBR.DisplayMBRData();} |
| 111 | void ShowDetails(void); |
| 112 | void ShowPartDetails(uint32_t partNum); |
| srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 113 | |
| 114 | // Request information from the user (& possibly do something with it) |
| 115 | uint32_t GetPartNum(void); |
| 116 | void ResizePartitionTable(void); |
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 117 | void CreatePartition(void); |
| 118 | void DeletePartition(void); |
| srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 119 | void ChangePartType(void); |
| 120 | void SetAttributes(uint32_t partNum); |
| srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 121 | int DestroyGPT(int prompt = 1); // Returns 1 if user proceeds |
| srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 122 | |
| srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 123 | // Convert between GPT and other formats (may require user interaction) |
| srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 124 | WhichToUse UseWhichPartitions(void); |
| srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 125 | int XFormPartitions(void); |
| 126 | int XFormDisklabel(int OnGptPart = -1); |
| 127 | int XFormDisklabel(BSDData* disklabel, int startPart); |
| srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 128 | int OnePartToMBR(uint32_t gptPart, int mbrPart); // add one partition to MBR. Returns 1 if successful |
| 129 | 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] | 130 | void MakeHybrid(void); |
| 131 | |
| 132 | // Adjust GPT structures WITHOUT user interaction... |
| 133 | int SetGPTSize(uint32_t numEntries); |
| 134 | void BlankPartitions(void); |
| srs5694 | ba00fed | 2010-01-12 18:18:36 -0500 | [diff] [blame] | 135 | int DeletePartition(uint32_t partNum); |
| 136 | int CreatePartition(uint32_t partNum, uint64_t startSector, uint64_t endSector); |
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 137 | void SortGPT(void); |
| 138 | int ClearGPTData(void); |
| srs5694 | 247657a | 2009-11-26 18:36:12 -0500 | [diff] [blame] | 139 | void MoveSecondHeaderToEnd(); |
| srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame^] | 140 | int SetName(uint32_t partNum, string theName = ""); |
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 141 | void SetDiskGUID(GUIDData newGUID); |
| 142 | int SetPartitionGUID(uint32_t pn, GUIDData theGUID); |
| srs5694 | ba00fed | 2010-01-12 18:18:36 -0500 | [diff] [blame] | 143 | int ChangePartType(uint32_t pn, uint16_t hexCode); |
| srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 144 | void MakeProtectiveMBR(void) {protectiveMBR.MakeProtectiveMBR();} |
| srs5694 | 1d1448a | 2009-12-31 21:20:19 -0500 | [diff] [blame] | 145 | int Align(uint64_t* sector); |
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 146 | |
| 147 | // Return data about the GPT structures.... |
| srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 148 | int GetPartRange(uint32_t* low, uint32_t* high); |
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 149 | uint32_t GetNumParts(void) {return mainHeader.numParts;} |
| 150 | uint64_t GetMainHeaderLBA(void) {return mainHeader.currentLBA;} |
| 151 | uint64_t GetSecondHeaderLBA(void) {return secondHeader.currentLBA;} |
| 152 | uint64_t GetMainPartsLBA(void) {return mainHeader.partitionEntriesLBA;} |
| 153 | uint64_t GetSecondPartsLBA(void) {return secondHeader.partitionEntriesLBA;} |
| srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 154 | uint32_t CountParts(void); |
| srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 155 | |
| 156 | // Find information about free space |
| 157 | uint64_t FindFirstAvailable(uint64_t start = 0); |
| 158 | uint64_t FindFirstInLargest(void); |
| 159 | uint64_t FindLastAvailable(uint64_t start); |
| 160 | uint64_t FindLastInFree(uint64_t start); |
| 161 | uint64_t FindFreeBlocks(int *numSegments, uint64_t *largestSegment); |
| 162 | int IsFree(uint64_t sector); |
| srs5694 | ba00fed | 2010-01-12 18:18:36 -0500 | [diff] [blame] | 163 | int IsFreePartNum(uint32_t partNum); |
| 164 | |
| 165 | // Change how functions work, or return information on same |
| 166 | void SetAlignment(int n) {sectorAlignment = n;} |
| 167 | int GetAlignment(void) {return sectorAlignment;} |
| 168 | void JustLooking(int i = 1) {justLooking = i;} |
| 169 | void BeQuiet(int i = 1) {beQuiet = i;} |
| 170 | WhichToUse WhichWasUsed(void) {return whichWasUsed;} |
| srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 171 | |
| 172 | // Endianness functions |
| 173 | void ReverseHeaderBytes(struct GPTHeader* header); // for endianness |
| 174 | void ReversePartitionBytes(); // for endianness |
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 175 | }; // class GPTData |
| 176 | |
| 177 | // Function prototypes.... |
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 178 | int SizesOK(void); |
| 179 | |
| 180 | #endif |