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 
61 // Data in GPT format
62 class GPTData {
63 protected:
64    struct GPTHeader mainHeader;
65    GPTPart *partitions;
66    uint32_t numParts; // # of partitions the table can hold
67    struct GPTHeader secondHeader;
68    MBRData protectiveMBR;
69    string device; // device filename
70    DiskIO myDisk;
71    uint32_t blockSize; // device block size
72    uint64_t diskSize; // size of device, in 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(string deviceFilename);
94    virtual ~GPTData(void);
95    GPTData & operator=(const GPTData & orig);
96 
97    // Verify (or update) data integrity
98    int Verify(void);
99    int CheckGPTSize(void);
100    int CheckHeaderValidity(void);
101    int CheckHeaderCRC(struct GPTHeader* header, int warn = 0);
102    void RecomputeCRCs(void);
103    void RebuildMainHeader(void);
104    void RebuildSecondHeader(void);
105    int VerifyMBR(void) {return protectiveMBR.FindOverlaps();}
106    int FindHybridMismatches(void);
107    int FindOverlaps(void);
108    int FindInsanePartitions(void);
109 
110    // Load or save data from/to disk
111    int SetDisk(const string & deviceFilename);
112    DiskIO* GetDisk(void) {return &myDisk;}
113    int LoadMBR(const string & f) {return protectiveMBR.ReadMBRData(f);}
114    int WriteProtectiveMBR(void) {return protectiveMBR.WriteMBRData(&myDisk);}
115    void PartitionScan(void);
116    int LoadPartitions(const string & deviceFilename);
117    int ForceLoadGPTData(void);
118    int LoadMainTable(void);
119    int LoadSecondTableAsMain(void);
120    int SaveGPTData(int quiet = 0);
121    int SaveGPTBackup(const string & filename);
122    int LoadGPTBackup(const string & filename);
123    int SaveMBR(void);
124    int DestroyGPT(void);
125    int DestroyMBR(void);
126 
127    // Display data....
128    void ShowAPMState(void);
129    void ShowGPTState(void);
130    void DisplayGPTData(void);
131    void DisplayMBRData(void) {protectiveMBR.DisplayMBRData();}
132    void ShowPartDetails(uint32_t partNum);
133 
134    // Convert between GPT and other formats
135    virtual WhichToUse UseWhichPartitions(void);
136    void XFormPartitions(void);
137    int XFormDisklabel(uint32_t partNum);
138    int XFormDisklabel(BSDData* disklabel);
139    int OnePartToMBR(uint32_t gptPart, int mbrPart); // add one partition to MBR. Returns 1 if successful
140 
141    // Adjust GPT structures WITHOUT user interaction...
142    int SetGPTSize(uint32_t numEntries, int fillGPTSectors = 1);
143    void BlankPartitions(void);
144    int DeletePartition(uint32_t partNum);
145    uint32_t CreatePartition(uint32_t partNum, uint64_t startSector, uint64_t endSector);
146    void SortGPT(void);
147    int SwapPartitions(uint32_t partNum1, uint32_t partNum2);
148    int ClearGPTData(void);
149    void MoveSecondHeaderToEnd();
150    int SetName(uint32_t partNum, const UnicodeString & theName);
151    void SetDiskGUID(GUIDData newGUID);
152    int SetPartitionGUID(uint32_t pn, GUIDData theGUID);
153    void RandomizeGUIDs(void);
154    int ChangePartType(uint32_t pn, PartType theGUID);
155    void MakeProtectiveMBR(void) {protectiveMBR.MakeProtectiveMBR();}
156    void RecomputeCHS(void);
157    int Align(uint64_t* sector);
158    void SetProtectiveMBR(BasicMBRData & newMBR) {protectiveMBR = newMBR;}
159 
160    // Return data about the GPT structures....
161    WhichToUse GetState(void) {return whichWasUsed;}
162    int GetPartRange(uint32_t* low, uint32_t* high);
163    int FindFirstFreePart(void);
164    uint32_t GetNumParts(void) {return mainHeader.numParts;}
165    uint64_t GetMainHeaderLBA(void) {return mainHeader.currentLBA;}
166    uint64_t GetSecondHeaderLBA(void) {return secondHeader.currentLBA;}
167    uint64_t GetMainPartsLBA(void) {return mainHeader.partitionEntriesLBA;}
168    uint64_t GetSecondPartsLBA(void) {return secondHeader.partitionEntriesLBA;}
169    uint64_t GetFirstUsableLBA(void) {return mainHeader.firstUsableLBA;}
170    uint64_t GetLastUsableLBA(void) {return mainHeader.lastUsableLBA;}
171    uint32_t CountParts(void);
172    bool ValidPartNum (const uint32_t partNum);
173    const GPTPart & operator[](uint32_t partNum) const;
174    const GUIDData & GetDiskGUID(void) const;
175    uint32_t GetBlockSize(void) {return blockSize;}
176 
177    // Find information about free space
178    uint64_t FindFirstAvailable(uint64_t start = 0);
179    uint64_t FindFirstInLargest(void);
180    uint64_t FindLastAvailable();
181    uint64_t FindLastInFree(uint64_t start);
182    uint64_t FindFreeBlocks(uint32_t *numSegments, uint64_t *largestSegment);
183    int IsFree(uint64_t sector, uint32_t *partNum = NULL);
184    int IsFreePartNum(uint32_t partNum);
185    int IsUsedPartNum(uint32_t partNum);
186 
187    // Change how functions work, or return information on same
188    void SetAlignment(uint32_t n);
189    uint32_t ComputeAlignment(void); // Set alignment based on current partitions
190    uint32_t GetAlignment(void) {return sectorAlignment;}
191    void JustLooking(int i = 1) {justLooking = i;}
192    void BeQuiet(int i = 1) {beQuiet = i;}
193    WhichToUse WhichWasUsed(void) {return whichWasUsed;}
194 
195    // Endianness functions
196    void ReverseHeaderBytes(struct GPTHeader* header);
197    void ReversePartitionBytes(); // for endianness
198 
199    // Attributes functions
200    int ManageAttributes(int partNum, const string & command, const string & bits);
201    void ShowAttributes(const uint32_t partNum);
202    void GetAttribute(const uint32_t partNum, const string& attributeBits);
203 
204 }; // class GPTData
205 
206 // Function prototypes....
207 int SizesOK(void);
208 
209 #endif
210