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 | 61768bc | 2010-07-04 01:54:00 -0400 | [diff] [blame] | 4 | /* This program is copyright (c) 2009, 2010 by Roderick W. Smith. It is distributed |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 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" |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 11 | #include "mbr.h" |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 12 | #include "bsd.h" |
| 13 | #include "gptpart.h" |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 14 | #include "partnotes.h" |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 15 | |
| 16 | #ifndef __GPTSTRUCTS |
| 17 | #define __GPTSTRUCTS |
| 18 | |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame^] | 19 | #define GPTFDISK_VERSION "0.6.12" |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 20 | |
| 21 | // Constants used by GPTData::PartsToMBR(). MBR_EMPTY must be the lowest- |
| 22 | // numbered value to refer to partition numbers. (Most will be 0 or positive, |
| 23 | // of course.) |
| 24 | #define MBR_EFI_GPT -1 |
| 25 | #define MBR_EMPTY -2 |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 26 | |
srs5694 | a8582cf | 2010-03-19 14:21:59 -0400 | [diff] [blame] | 27 | // Default values for sector alignment |
| 28 | #define DEFAULT_ALIGNMENT 2048 |
srs5694 | 8a4ddfc | 2010-03-21 19:05:49 -0400 | [diff] [blame] | 29 | #define MAX_ALIGNMENT 65536 |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame^] | 30 | #define MIN_AF_ALIGNMENT 8 |
srs5694 | a8582cf | 2010-03-19 14:21:59 -0400 | [diff] [blame] | 31 | |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame^] | 32 | // Below constant corresponds to a ~596GiB (640MB) disk, since WD has |
| 33 | // introduced a smaller Advanced Format drive |
srs5694 | a8582cf | 2010-03-19 14:21:59 -0400 | [diff] [blame] | 34 | #define SMALLEST_ADVANCED_FORMAT UINT64_C(1250263728) |
| 35 | |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 36 | using namespace std; |
| 37 | |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 38 | class PartNotes; |
| 39 | |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 40 | /**************************************** |
| 41 | * * |
| 42 | * GPTData class and related structures * |
| 43 | * * |
| 44 | ****************************************/ |
| 45 | |
| 46 | // Validity state of GPT data |
| 47 | enum GPTValidity {gpt_valid, gpt_corrupt, gpt_invalid}; |
| 48 | |
| 49 | // Which set of partition data to use |
srs5694 | 3c0af38 | 2010-01-15 19:19:18 -0500 | [diff] [blame] | 50 | enum WhichToUse {use_gpt, use_mbr, use_bsd, use_new, use_abort}; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 51 | |
| 52 | // Header (first 512 bytes) of GPT table |
srs5694 | ba00fed | 2010-01-12 18:18:36 -0500 | [diff] [blame] | 53 | #pragma pack(1) |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 54 | struct GPTHeader { |
| 55 | uint64_t signature; |
| 56 | uint32_t revision; |
| 57 | uint32_t headerSize; |
| 58 | uint32_t headerCRC; |
| 59 | uint32_t reserved; |
| 60 | uint64_t currentLBA; |
| 61 | uint64_t backupLBA; |
| 62 | uint64_t firstUsableLBA; |
| 63 | uint64_t lastUsableLBA; |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 64 | GUIDData diskGUID; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 65 | uint64_t partitionEntriesLBA; |
| 66 | uint32_t numParts; |
| 67 | uint32_t sizeOfPartitionEntries; |
| 68 | uint32_t partitionEntriesCRC; |
| 69 | unsigned char reserved2[GPT_RESERVED]; |
| 70 | }; // struct GPTHeader |
| 71 | |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 72 | // Data in GPT format |
| 73 | class GPTData { |
| 74 | protected: |
| 75 | struct GPTHeader mainHeader; |
srs5694 | ba00fed | 2010-01-12 18:18:36 -0500 | [diff] [blame] | 76 | GPTPart *partitions; |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 77 | uint32_t numParts; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 78 | struct GPTHeader secondHeader; |
| 79 | MBRData protectiveMBR; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 80 | string device; // device filename |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 81 | DiskIO myDisk; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 82 | uint32_t blockSize; // device block size |
| 83 | uint64_t diskSize; // size of device, in blocks |
| 84 | GPTValidity state; // is GPT valid? |
srs5694 | 5d58fe0 | 2010-01-03 20:57:08 -0500 | [diff] [blame] | 85 | 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] | 86 | int mainCrcOk; |
| 87 | int secondCrcOk; |
| 88 | int mainPartsCrcOk; |
| 89 | int secondPartsCrcOk; |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 90 | int apmFound; // set to 1 if APM detected |
| 91 | int bsdFound; // set to 1 if BSD disklabel detected in MBR |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame^] | 92 | uint32_t sectorAlignment; // Start partitions at multiples of sectorAlignment |
srs5694 | ba00fed | 2010-01-12 18:18:36 -0500 | [diff] [blame] | 93 | int beQuiet; |
| 94 | WhichToUse whichWasUsed; |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 95 | |
| 96 | int LoadHeader(struct GPTHeader *header, DiskIO & disk, uint64_t sector, int *crcOk); |
| 97 | int LoadPartitionTable(const struct GPTHeader & header, DiskIO & disk, uint64_t sector = 0); |
| 98 | int CheckTable(struct GPTHeader *header); |
| 99 | int SaveHeader(struct GPTHeader *header, DiskIO & disk, uint64_t sector); |
| 100 | int SavePartitionTable(DiskIO & disk, uint64_t sector); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 101 | public: |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 102 | // Basic necessary functions.... |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 103 | GPTData(void); |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 104 | GPTData(string deviceFilename); |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 105 | virtual ~GPTData(void); |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 106 | |
| 107 | // Verify (or update) data integrity |
| 108 | int Verify(void); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 109 | int CheckGPTSize(void); |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 110 | int CheckHeaderValidity(void); |
| 111 | int CheckHeaderCRC(struct GPTHeader* header); |
| 112 | void RecomputeCRCs(void); |
| 113 | void RebuildMainHeader(void); |
| 114 | void RebuildSecondHeader(void); |
srs5694 | 327129e | 2010-09-22 01:07:31 -0400 | [diff] [blame] | 115 | int VerifyMBR(void) {return protectiveMBR.Verify();} |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 116 | int FindHybridMismatches(void); |
| 117 | int FindOverlaps(void); |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 118 | int FindInsanePartitions(void); |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 119 | |
| 120 | // Load or save data from/to disk |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 121 | int LoadMBR(const string & f) {return protectiveMBR.ReadMBRData(f);} |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 122 | int WriteProtectiveMBR(void) {return protectiveMBR.WriteMBRData(&myDisk);} |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 123 | void PartitionScan(void); |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 124 | int LoadPartitions(const string & deviceFilename); |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 125 | int ForceLoadGPTData(void); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 126 | int LoadMainTable(void); |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 127 | int LoadSecondTableAsMain(void); |
srs5694 | f9312b0 | 2010-07-06 15:39:51 -0400 | [diff] [blame] | 128 | int SaveGPTData(int quiet = 0, string filename = ""); |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 129 | int SaveGPTBackup(const string & filename); |
| 130 | int LoadGPTBackup(const string & filename); |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 131 | int SaveMBR(void); |
| 132 | int DestroyGPT(void); |
| 133 | int DestroyMBR(void); |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 134 | |
| 135 | // Display data.... |
| 136 | void ShowAPMState(void); |
| 137 | void ShowGPTState(void); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 138 | void DisplayGPTData(void); |
| 139 | void DisplayMBRData(void) {protectiveMBR.DisplayMBRData();} |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 140 | void ShowPartDetails(uint32_t partNum); |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 141 | |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 142 | // Convert between GPT and other formats |
| 143 | virtual WhichToUse UseWhichPartitions(void); |
| 144 | void XFormPartitions(void); |
| 145 | virtual int XFormDisklabel(uint32_t partNum); |
| 146 | int XFormDisklabel(BSDData* disklabel); |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 147 | int OnePartToMBR(uint32_t gptPart, int mbrPart); // add one partition to MBR. Returns 1 if successful |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 148 | int PartsToMBR(PartNotes & notes); |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 149 | |
| 150 | // Adjust GPT structures WITHOUT user interaction... |
| 151 | int SetGPTSize(uint32_t numEntries); |
| 152 | void BlankPartitions(void); |
srs5694 | ba00fed | 2010-01-12 18:18:36 -0500 | [diff] [blame] | 153 | int DeletePartition(uint32_t partNum); |
srs5694 | e321d44 | 2010-01-29 17:44:04 -0500 | [diff] [blame] | 154 | uint32_t CreatePartition(uint32_t partNum, uint64_t startSector, uint64_t endSector); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 155 | void SortGPT(void); |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 156 | void QuickSortGPT(int start, int finish); |
| 157 | int SwapPartitions(uint32_t partNum1, uint32_t partNum2); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 158 | int ClearGPTData(void); |
srs5694 | 247657a | 2009-11-26 18:36:12 -0500 | [diff] [blame] | 159 | void MoveSecondHeaderToEnd(); |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 160 | int SetName(uint32_t partNum, const string & theName = ""); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 161 | void SetDiskGUID(GUIDData newGUID); |
| 162 | int SetPartitionGUID(uint32_t pn, GUIDData theGUID); |
srs5694 | 9ba5421 | 2010-05-18 23:24:02 -0400 | [diff] [blame] | 163 | void RandomizeGUIDs(void); |
srs5694 | 327129e | 2010-09-22 01:07:31 -0400 | [diff] [blame] | 164 | int ChangePartType(uint32_t pn, PartType theGUID); |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 165 | void MakeProtectiveMBR(void) {protectiveMBR.MakeProtectiveMBR();} |
srs5694 | 9ba5421 | 2010-05-18 23:24:02 -0400 | [diff] [blame] | 166 | void RecomputeCHS(void); |
srs5694 | 1d1448a | 2009-12-31 21:20:19 -0500 | [diff] [blame] | 167 | int Align(uint64_t* sector); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 168 | |
| 169 | // Return data about the GPT structures.... |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 170 | int GetPartRange(uint32_t* low, uint32_t* high); |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 171 | int FindFirstFreePart(void); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 172 | uint32_t GetNumParts(void) {return mainHeader.numParts;} |
| 173 | uint64_t GetMainHeaderLBA(void) {return mainHeader.currentLBA;} |
| 174 | uint64_t GetSecondHeaderLBA(void) {return secondHeader.currentLBA;} |
| 175 | uint64_t GetMainPartsLBA(void) {return mainHeader.partitionEntriesLBA;} |
| 176 | uint64_t GetSecondPartsLBA(void) {return secondHeader.partitionEntriesLBA;} |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 177 | uint64_t GetFirstUsableLBA(void) {return mainHeader.firstUsableLBA;} |
| 178 | uint64_t GetLastUsableLBA(void) {return mainHeader.lastUsableLBA;} |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 179 | uint32_t CountParts(void); |
srs5694 | 9ddc14b | 2010-08-22 22:44:42 -0400 | [diff] [blame] | 180 | bool ValidPartNum (const uint32_t partNum); |
srs5694 | 5a08175 | 2010-09-24 20:39:41 -0400 | [diff] [blame] | 181 | const GPTPart & operator[](uint32_t partNum) const; |
| 182 | const GUIDData & GetDiskGUID(void) const; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 183 | |
| 184 | // Find information about free space |
| 185 | uint64_t FindFirstAvailable(uint64_t start = 0); |
| 186 | uint64_t FindFirstInLargest(void); |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 187 | uint64_t FindLastAvailable(); |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 188 | uint64_t FindLastInFree(uint64_t start); |
srs5694 | e321d44 | 2010-01-29 17:44:04 -0500 | [diff] [blame] | 189 | uint64_t FindFreeBlocks(uint32_t *numSegments, uint64_t *largestSegment); |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 190 | int IsFree(uint64_t sector, uint32_t *partNum = NULL); |
srs5694 | ba00fed | 2010-01-12 18:18:36 -0500 | [diff] [blame] | 191 | int IsFreePartNum(uint32_t partNum); |
| 192 | |
| 193 | // Change how functions work, or return information on same |
srs5694 | a8582cf | 2010-03-19 14:21:59 -0400 | [diff] [blame] | 194 | void SetAlignment(uint32_t n); |
| 195 | uint32_t ComputeAlignment(void); // Set alignment based on current partitions |
| 196 | uint32_t GetAlignment(void) {return sectorAlignment;} |
srs5694 | ba00fed | 2010-01-12 18:18:36 -0500 | [diff] [blame] | 197 | void JustLooking(int i = 1) {justLooking = i;} |
| 198 | void BeQuiet(int i = 1) {beQuiet = i;} |
| 199 | WhichToUse WhichWasUsed(void) {return whichWasUsed;} |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 200 | |
| 201 | // Endianness functions |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 202 | void ReverseHeaderBytes(struct GPTHeader* header); |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 203 | void ReversePartitionBytes(); // for endianness |
srs5694 | 9ddc14b | 2010-08-22 22:44:42 -0400 | [diff] [blame] | 204 | |
| 205 | // Attributes functions |
| 206 | int ManageAttributes(int partNum, const string & command, const string & bits); |
| 207 | void ShowAttributes(const uint32_t partNum); |
| 208 | void GetAttribute(const uint32_t partNum, const string& attributeBits); |
| 209 | |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 210 | }; // class GPTData |
| 211 | |
| 212 | // Function prototypes.... |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 213 | int SizesOK(void); |
| 214 | |
| 215 | #endif |