1 /* gpt.h -- GPT and data structure definitions, types, and 2 functions */ 3 4 /* This program is copyright (c) 2009-2022 by Roderick W. Smith. It is distributed 5 under the terms of the GNU GPL version 2, as detailed in the COPYING file. */ 6 7 #ifndef __GPTSTRUCTS 8 #define __GPTSTRUCTS 9 10 #include <stdint.h> 11 #include <sys/types.h> 12 #include "gptpart.h" 13 #include "support.h" 14 #include "mbr.h" 15 #include "bsd.h" 16 #include "gptpart.h" 17 18 // Default values for sector alignment 19 #define DEFAULT_ALIGNMENT 2048 20 #define MAX_ALIGNMENT 65536 21 #define MIN_AF_ALIGNMENT 8 22 23 // Below constant corresponds to a ~279GiB (300GB) disk, since the 24 // smallest Advanced Format drive I know of is 320GB in size 25 #define SMALLEST_ADVANCED_FORMAT UINT64_C(585937500) 26 27 /**************************************** 28 * * 29 * GPTData class and related structures * 30 * * 31 ****************************************/ 32 33 // Validity state of GPT data 34 enum GPTValidity {gpt_valid, gpt_corrupt, gpt_invalid}; 35 36 // Which set of partition data to use 37 enum WhichToUse {use_gpt, use_mbr, use_bsd, use_new, use_abort}; 38 39 // Header (first 512 bytes) of GPT table 40 #pragma pack(1) 41 struct GPTHeader { 42 uint64_t signature; 43 uint32_t revision; 44 uint32_t headerSize; 45 uint32_t headerCRC; 46 uint32_t reserved; 47 uint64_t currentLBA; 48 uint64_t backupLBA; 49 uint64_t firstUsableLBA; 50 uint64_t lastUsableLBA; 51 GUIDData diskGUID; 52 uint64_t partitionEntriesLBA; 53 uint32_t numParts; 54 uint32_t sizeOfPartitionEntries; 55 uint32_t partitionEntriesCRC; 56 unsigned char reserved2[GPT_RESERVED]; 57 }; // struct GPTHeader 58 #pragma pack () 59 60 // Data in GPT format 61 class GPTData { 62 protected: 63 struct GPTHeader mainHeader; 64 GPTPart *partitions; 65 uint32_t numParts; // # of partitions the table can hold 66 struct GPTHeader secondHeader; 67 MBRData protectiveMBR; 68 std::string device; // device filename 69 DiskIO myDisk; 70 uint32_t blockSize; // device logical block size 71 uint32_t physBlockSize; // device physical block size (or 0 if it can't be determined) 72 uint64_t diskSize; // size of device, in logical blocks 73 GPTValidity state; // is GPT valid? 74 int justLooking; // Set to 1 if program launched with "-l" or if read-only 75 int mainCrcOk; 76 int secondCrcOk; 77 int mainPartsCrcOk; 78 int secondPartsCrcOk; 79 int apmFound; // set to 1 if APM detected 80 int bsdFound; // set to 1 if BSD disklabel detected in MBR 81 uint32_t sectorAlignment; // Start partitions at multiples of sectorAlignment 82 int beQuiet; 83 WhichToUse whichWasUsed; 84 85 int LoadHeader(struct GPTHeader *header, DiskIO & disk, uint64_t sector, int *crcOk); 86 int LoadPartitionTable(const struct GPTHeader & header, DiskIO & disk, uint64_t sector = 0); 87 int CheckTable(struct GPTHeader *header); 88 int SaveHeader(struct GPTHeader *header, DiskIO & disk, uint64_t sector); 89 int SavePartitionTable(DiskIO & disk, uint64_t sector); 90 public: 91 // Basic necessary functions.... 92 GPTData(void); 93 GPTData(const GPTData &); 94 GPTData(std::string deviceFilename); 95 virtual ~GPTData(void); 96 GPTData & operator=(const GPTData & orig); 97 98 // Verify (or update) data integrity 99 int Verify(void); 100 int CheckGPTSize(void); 101 int CheckHeaderValidity(void); 102 int CheckHeaderCRC(struct GPTHeader* header, int warn = 0); 103 void RecomputeCRCs(void); 104 void RebuildMainHeader(void); 105 void RebuildSecondHeader(void); VerifyMBR(void)106 int VerifyMBR(void) {return protectiveMBR.FindOverlaps();} 107 int FindHybridMismatches(void); 108 int FindOverlaps(void); 109 int FindInsanePartitions(void); 110 111 // Load or save data from/to disk 112 int SetDisk(const std::string & deviceFilename); GetDisk(void)113 DiskIO* GetDisk(void) {return &myDisk;} LoadMBR(const std::string & f)114 int LoadMBR(const std::string & f) {return protectiveMBR.ReadMBRData(f);} WriteProtectiveMBR(void)115 int WriteProtectiveMBR(void) {return protectiveMBR.WriteMBRData(&myDisk);} 116 void PartitionScan(void); 117 int LoadPartitions(const std::string & deviceFilename); 118 int ForceLoadGPTData(void); 119 int LoadMainTable(void); 120 int LoadSecondTableAsMain(void); 121 int SaveGPTData(int quiet = 0); 122 int SaveGPTBackup(const std::string & filename); 123 int LoadGPTBackup(const std::string & filename); 124 int SaveMBR(void); 125 int DestroyGPT(void); 126 int DestroyMBR(void); 127 128 // Display data.... 129 void ShowAPMState(void); 130 void ShowGPTState(void); 131 void DisplayGPTData(void); DisplayMBRData(void)132 void DisplayMBRData(void) {protectiveMBR.DisplayMBRData();} 133 void ShowPartDetails(uint32_t partNum); 134 135 // Convert between GPT and other formats 136 virtual WhichToUse UseWhichPartitions(void); 137 void XFormPartitions(void); 138 int XFormDisklabel(uint32_t partNum); 139 int XFormDisklabel(BSDData* disklabel); 140 int OnePartToMBR(uint32_t gptPart, int mbrPart); // add one partition to MBR. Returns 1 if successful 141 142 // Adjust GPT structures WITHOUT user interaction... 143 int SetGPTSize(uint32_t numEntries, int fillGPTSectors = 1); 144 int MoveMainTable(uint64_t pteSector); 145 void BlankPartitions(void); 146 int DeletePartition(uint32_t partNum); 147 uint32_t CreatePartition(uint32_t partNum, uint64_t startSector, uint64_t endSector); 148 void SortGPT(void); 149 int SwapPartitions(uint32_t partNum1, uint32_t partNum2); 150 int ClearGPTData(void); 151 void MoveSecondHeaderToEnd(); 152 int SetName(uint32_t partNum, const UnicodeString & theName); 153 void SetDiskGUID(GUIDData newGUID); 154 int SetPartitionGUID(uint32_t pn, GUIDData theGUID); 155 void RandomizeGUIDs(void); 156 int ChangePartType(uint32_t pn, PartType theGUID); MakeProtectiveMBR(void)157 void MakeProtectiveMBR(void) {protectiveMBR.MakeProtectiveMBR();} 158 void RecomputeCHS(void); 159 int Align(uint64_t* sector); SetProtectiveMBR(BasicMBRData & newMBR)160 void SetProtectiveMBR(BasicMBRData & newMBR) {protectiveMBR = newMBR;} 161 162 // Return data about the GPT structures.... GetState(void)163 WhichToUse GetState(void) {return whichWasUsed;} 164 int GetPartRange(uint32_t* low, uint32_t* high); 165 int FindFirstFreePart(void); GetNumParts(void)166 uint32_t GetNumParts(void) {return mainHeader.numParts;} GetTableSizeInSectors(void)167 uint64_t GetTableSizeInSectors(void) {return (((numParts * GPT_SIZE) / blockSize) + 168 (((numParts * GPT_SIZE) % blockSize) != 0)); } GetMainHeaderLBA(void)169 uint64_t GetMainHeaderLBA(void) {return mainHeader.currentLBA;} GetSecondHeaderLBA(void)170 uint64_t GetSecondHeaderLBA(void) {return secondHeader.currentLBA;} GetMainPartsLBA(void)171 uint64_t GetMainPartsLBA(void) {return mainHeader.partitionEntriesLBA;} GetSecondPartsLBA(void)172 uint64_t GetSecondPartsLBA(void) {return secondHeader.partitionEntriesLBA;} GetFirstUsableLBA(void)173 uint64_t GetFirstUsableLBA(void) {return mainHeader.firstUsableLBA;} GetLastUsableLBA(void)174 uint64_t GetLastUsableLBA(void) {return mainHeader.lastUsableLBA;} 175 uint32_t CountParts(void); 176 bool ValidPartNum (const uint32_t partNum); 177 const GPTPart & operator[](uint32_t partNum) const; 178 const GUIDData & GetDiskGUID(void) const; GetBlockSize(void)179 uint32_t GetBlockSize(void) {return blockSize;} 180 181 // Find information about free space 182 uint64_t FindFirstAvailable(uint64_t start = 0); 183 uint64_t FindFirstUsedLBA(void); 184 uint64_t FindFirstInLargest(void); 185 uint64_t FindLastAvailable(); 186 uint64_t FindLastInFree(uint64_t start, bool align = false); 187 uint64_t FindFreeBlocks(uint32_t *numSegments, uint64_t *largestSegment); 188 int IsFree(uint64_t sector, uint32_t *partNum = NULL); 189 int IsFreePartNum(uint32_t partNum); 190 int IsUsedPartNum(uint32_t partNum); 191 192 // Change how functions work, or return information on same 193 void SetAlignment(uint32_t n); 194 uint32_t ComputeAlignment(void); // Set alignment based on current partitions GetAlignment(void)195 uint32_t GetAlignment(void) {return sectorAlignment;} 196 void JustLooking(int i = 1) {justLooking = i;} 197 void BeQuiet(int i = 1) {beQuiet = i;} WhichWasUsed(void)198 WhichToUse WhichWasUsed(void) {return whichWasUsed;} 199 200 // Endianness functions 201 void ReverseHeaderBytes(struct GPTHeader* header); 202 void ReversePartitionBytes(); // for endianness 203 204 // Attributes functions 205 int ManageAttributes(int partNum, const std::string & command, const std::string & bits); 206 void ShowAttributes(const uint32_t partNum); 207 void GetAttribute(const uint32_t partNum, const std::string& attributeBits); 208 209 }; // class GPTData 210 211 // Function prototypes.... 212 int SizesOK(void); 213 214 #endif 215