• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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);
113    int SetDisk(const DiskIO & disk);
GetDisk(void)114    DiskIO* GetDisk(void) {return &myDisk;}
LoadMBR(const std::string & f)115    int LoadMBR(const std::string & f) {return protectiveMBR.ReadMBRData(f);}
WriteProtectiveMBR(void)116    int WriteProtectiveMBR(void) {return protectiveMBR.WriteMBRData(&myDisk);}
117    void PartitionScan(void);
118    int LoadPartitions(const std::string & deviceFilename);
119    int ForceLoadGPTData(void);
120    int LoadMainTable(void);
121    int LoadSecondTableAsMain(void);
122    int SaveGPTData(int quiet = 0);
123    int SaveGPTBackup(const std::string & filename);
124    int LoadGPTBackup(const std::string & filename);
125    int SaveMBR(void);
126    int DestroyGPT(void);
127    int DestroyMBR(void);
128 
129    // Display data....
130    void ShowAPMState(void);
131    void ShowGPTState(void);
132    void DisplayGPTData(void);
DisplayMBRData(void)133    void DisplayMBRData(void) {protectiveMBR.DisplayMBRData();}
134    void ShowPartDetails(uint32_t partNum);
135 
136    // Convert between GPT and other formats
137    virtual WhichToUse UseWhichPartitions(void);
138    void XFormPartitions(void);
139    int XFormDisklabel(uint32_t partNum);
140    int XFormDisklabel(BSDData* disklabel);
141    int OnePartToMBR(uint32_t gptPart, int mbrPart); // add one partition to MBR. Returns 1 if successful
142 
143    // Adjust GPT structures WITHOUT user interaction...
144    int SetGPTSize(uint32_t numEntries, int fillGPTSectors = 1);
145    int MoveMainTable(uint64_t pteSector);
146    void BlankPartitions(void);
147    int DeletePartition(uint32_t partNum);
148    uint32_t CreatePartition(uint32_t partNum, uint64_t startSector, uint64_t endSector);
149    void SortGPT(void);
150    int SwapPartitions(uint32_t partNum1, uint32_t partNum2);
151    int ClearGPTData(void);
152    void MoveSecondHeaderToEnd();
153    int SetName(uint32_t partNum, const UnicodeString & theName);
154    void SetDiskGUID(GUIDData newGUID);
155    int SetPartitionGUID(uint32_t pn, GUIDData theGUID);
156    void RandomizeGUIDs(void);
157    int ChangePartType(uint32_t pn, PartType theGUID);
MakeProtectiveMBR(void)158    void MakeProtectiveMBR(void) {protectiveMBR.MakeProtectiveMBR();}
159    void RecomputeCHS(void);
160    int Align(uint64_t* sector);
SetProtectiveMBR(BasicMBRData & newMBR)161    void SetProtectiveMBR(BasicMBRData & newMBR) {protectiveMBR = newMBR;}
162 
163    // Return data about the GPT structures....
GetState(void)164    WhichToUse GetState(void) {return whichWasUsed;}
165    int GetPartRange(uint32_t* low, uint32_t* high);
166    int FindFirstFreePart(void);
GetNumParts(void)167    uint32_t GetNumParts(void) {return mainHeader.numParts;}
GetTableSizeInSectors(void)168    uint64_t GetTableSizeInSectors(void) {return (((numParts * GPT_SIZE) / blockSize) +
169                                                  (((numParts * GPT_SIZE) % blockSize) != 0)); }
GetMainHeaderLBA(void)170    uint64_t GetMainHeaderLBA(void) {return mainHeader.currentLBA;}
GetSecondHeaderLBA(void)171    uint64_t GetSecondHeaderLBA(void) {return secondHeader.currentLBA;}
GetMainPartsLBA(void)172    uint64_t GetMainPartsLBA(void) {return mainHeader.partitionEntriesLBA;}
GetSecondPartsLBA(void)173    uint64_t GetSecondPartsLBA(void) {return secondHeader.partitionEntriesLBA;}
GetFirstUsableLBA(void)174    uint64_t GetFirstUsableLBA(void) {return mainHeader.firstUsableLBA;}
GetLastUsableLBA(void)175    uint64_t GetLastUsableLBA(void) {return mainHeader.lastUsableLBA;}
176    uint32_t CountParts(void);
177    bool ValidPartNum (const uint32_t partNum);
178    const GPTPart & operator[](uint32_t partNum) const;
179    const GUIDData & GetDiskGUID(void) const;
GetBlockSize(void)180    uint32_t GetBlockSize(void) {return blockSize;}
181 
182    // Find information about free space
183    uint64_t FindFirstAvailable(uint64_t start = 0);
184    uint64_t FindFirstUsedLBA(void);
185    uint64_t FindFirstInLargest(void);
186    uint64_t FindLastAvailable();
187    uint64_t FindLastInFree(uint64_t start, bool align = false);
188    uint64_t FindFreeBlocks(uint32_t *numSegments, uint64_t *largestSegment);
189    int IsFree(uint64_t sector, uint32_t *partNum = NULL);
190    int IsFreePartNum(uint32_t partNum);
191    int IsUsedPartNum(uint32_t partNum);
192 
193    // Change how functions work, or return information on same
194    void SetAlignment(uint32_t n);
195    uint32_t ComputeAlignment(void); // Set alignment based on current partitions
GetAlignment(void)196    uint32_t GetAlignment(void) {return sectorAlignment;}
197    void JustLooking(int i = 1) {justLooking = i;}
198    void BeQuiet(int i = 1) {beQuiet = i;}
WhichWasUsed(void)199    WhichToUse WhichWasUsed(void) {return whichWasUsed;}
200 
201    // Endianness functions
202    void ReverseHeaderBytes(struct GPTHeader* header);
203    void ReversePartitionBytes(); // for endianness
204 
205    // Attributes functions
206    int ManageAttributes(int partNum, const std::string & command, const std::string & bits);
207    void ShowAttributes(const uint32_t partNum);
208    void GetAttribute(const uint32_t partNum, const std::string& attributeBits);
209 
210 }; // class GPTData
211 
212 // Function prototypes....
213 int SizesOK(void);
214 
215 #endif
216