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