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