blob: f53331f443810db900913f2a45075cfdf667efc9 [file] [log] [blame]
srs5694e7b4ff92009-08-18 13:16:10 -04001/* gpt.h -- GPT and data structure definitions, types, and
2 functions */
3
srs569464cbd172011-03-01 22:03:54 -05004/* This program is copyright (c) 2009-2011 by Roderick W. Smith. It is distributed
srs5694221e0872009-08-29 15:00:31 -04005 under the terms of the GNU GPL version 2, as detailed in the COPYING file. */
6
srs5694e7b4ff92009-08-18 13:16:10 -04007#include <stdint.h>
8#include <sys/types.h>
srs5694546a9c72010-01-26 16:00:26 -05009#include "gptpart.h"
srs5694e7b4ff92009-08-18 13:16:10 -040010#include "support.h"
srs5694e7b4ff92009-08-18 13:16:10 -040011#include "mbr.h"
srs5694221e0872009-08-29 15:00:31 -040012#include "bsd.h"
13#include "gptpart.h"
srs5694e7b4ff92009-08-18 13:16:10 -040014
15#ifndef __GPTSTRUCTS
16#define __GPTSTRUCTS
17
srs5694a8582cf2010-03-19 14:21:59 -040018// Default values for sector alignment
19#define DEFAULT_ALIGNMENT 2048
srs56948a4ddfc2010-03-21 19:05:49 -040020#define MAX_ALIGNMENT 65536
srs56940873e9d2010-10-07 13:00:45 -040021#define MIN_AF_ALIGNMENT 8
srs5694a8582cf2010-03-19 14:21:59 -040022
srs569464cbd172011-03-01 22:03:54 -050023// 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)
srs5694a8582cf2010-03-19 14:21:59 -040026
srs5694e7b4ff92009-08-18 13:16:10 -040027using namespace std;
28
29/****************************************
30 * *
31 * GPTData class and related structures *
32 * *
33 ****************************************/
34
35// Validity state of GPT data
36enum GPTValidity {gpt_valid, gpt_corrupt, gpt_invalid};
37
38// Which set of partition data to use
srs56943c0af382010-01-15 19:19:18 -050039enum WhichToUse {use_gpt, use_mbr, use_bsd, use_new, use_abort};
srs5694e7b4ff92009-08-18 13:16:10 -040040
41// Header (first 512 bytes) of GPT table
Tom Marshall7b5b7112019-07-23 15:18:15 -070042#pragma pack(push)
srs5694ba00fed2010-01-12 18:18:36 -050043#pragma pack(1)
srs5694e7b4ff92009-08-18 13:16:10 -040044struct GPTHeader {
45 uint64_t signature;
46 uint32_t revision;
47 uint32_t headerSize;
48 uint32_t headerCRC;
49 uint32_t reserved;
50 uint64_t currentLBA;
51 uint64_t backupLBA;
52 uint64_t firstUsableLBA;
53 uint64_t lastUsableLBA;
srs56946699b012010-02-04 00:55:30 -050054 GUIDData diskGUID;
srs5694e7b4ff92009-08-18 13:16:10 -040055 uint64_t partitionEntriesLBA;
56 uint32_t numParts;
57 uint32_t sizeOfPartitionEntries;
58 uint32_t partitionEntriesCRC;
59 unsigned char reserved2[GPT_RESERVED];
60}; // struct GPTHeader
John Paul Adrian Glaubitz2fb79512018-05-18 23:44:40 +020061#pragma pack ()
srs5694e7b4ff92009-08-18 13:16:10 -040062
srs5694e7b4ff92009-08-18 13:16:10 -040063// Data in GPT format
64class GPTData {
65protected:
66 struct GPTHeader mainHeader;
srs5694ba00fed2010-01-12 18:18:36 -050067 GPTPart *partitions;
srs5694815fb652011-03-18 12:35:56 -040068 uint32_t numParts; // # of partitions the table can hold
srs5694e7b4ff92009-08-18 13:16:10 -040069 struct GPTHeader secondHeader;
70 MBRData protectiveMBR;
srs5694fed16d02010-01-27 23:03:40 -050071 string device; // device filename
srs5694546a9c72010-01-26 16:00:26 -050072 DiskIO myDisk;
Rod Smithfc0e0142017-07-25 21:33:18 -040073 uint32_t blockSize; // device logical block size
74 uint32_t physBlockSize; // device physical block size (or 0 if it can't be determined)
75 uint64_t diskSize; // size of device, in logical blocks
srs5694e7b4ff92009-08-18 13:16:10 -040076 GPTValidity state; // is GPT valid?
srs56945d58fe02010-01-03 20:57:08 -050077 int justLooking; // Set to 1 if program launched with "-l" or if read-only
srs5694e7b4ff92009-08-18 13:16:10 -040078 int mainCrcOk;
79 int secondCrcOk;
80 int mainPartsCrcOk;
81 int secondPartsCrcOk;
srs5694221e0872009-08-29 15:00:31 -040082 int apmFound; // set to 1 if APM detected
83 int bsdFound; // set to 1 if BSD disklabel detected in MBR
srs56940873e9d2010-10-07 13:00:45 -040084 uint32_t sectorAlignment; // Start partitions at multiples of sectorAlignment
srs5694ba00fed2010-01-12 18:18:36 -050085 int beQuiet;
86 WhichToUse whichWasUsed;
srs5694cb76c672010-02-11 22:22:22 -050087
88 int LoadHeader(struct GPTHeader *header, DiskIO & disk, uint64_t sector, int *crcOk);
89 int LoadPartitionTable(const struct GPTHeader & header, DiskIO & disk, uint64_t sector = 0);
90 int CheckTable(struct GPTHeader *header);
91 int SaveHeader(struct GPTHeader *header, DiskIO & disk, uint64_t sector);
92 int SavePartitionTable(DiskIO & disk, uint64_t sector);
srs5694e7b4ff92009-08-18 13:16:10 -040093public:
srs5694e4ac11e2009-08-31 10:13:04 -040094 // Basic necessary functions....
srs5694e7b4ff92009-08-18 13:16:10 -040095 GPTData(void);
Rod Smith9ae60192018-07-05 16:50:13 -040096 GPTData(const GPTData &);
srs5694fed16d02010-01-27 23:03:40 -050097 GPTData(string deviceFilename);
srs569408bb0da2010-02-19 17:19:55 -050098 virtual ~GPTData(void);
srs569464cbd172011-03-01 22:03:54 -050099 GPTData & operator=(const GPTData & orig);
srs5694e4ac11e2009-08-31 10:13:04 -0400100
101 // Verify (or update) data integrity
102 int Verify(void);
srs5694e7b4ff92009-08-18 13:16:10 -0400103 int CheckGPTSize(void);
srs5694e4ac11e2009-08-31 10:13:04 -0400104 int CheckHeaderValidity(void);
srs5694d1b11e82011-09-18 21:12:28 -0400105 int CheckHeaderCRC(struct GPTHeader* header, int warn = 0);
srs5694e4ac11e2009-08-31 10:13:04 -0400106 void RecomputeCRCs(void);
107 void RebuildMainHeader(void);
108 void RebuildSecondHeader(void);
srs5694bf8950c2011-03-12 01:23:12 -0500109 int VerifyMBR(void) {return protectiveMBR.FindOverlaps();}
srs5694e4ac11e2009-08-31 10:13:04 -0400110 int FindHybridMismatches(void);
111 int FindOverlaps(void);
srs569455d92612010-03-07 22:16:07 -0500112 int FindInsanePartitions(void);
srs5694e4ac11e2009-08-31 10:13:04 -0400113
114 // Load or save data from/to disk
srs5694bf8950c2011-03-12 01:23:12 -0500115 int SetDisk(const string & deviceFilename);
Jeff Sharkey83fdc992020-10-13 19:50:30 -0600116 int SetDisk(const DiskIO & disk);
srs5694bf8950c2011-03-12 01:23:12 -0500117 DiskIO* GetDisk(void) {return &myDisk;}
srs56940a697312010-01-28 21:10:52 -0500118 int LoadMBR(const string & f) {return protectiveMBR.ReadMBRData(f);}
srs569408bb0da2010-02-19 17:19:55 -0500119 int WriteProtectiveMBR(void) {return protectiveMBR.WriteMBRData(&myDisk);}
srs5694546a9c72010-01-26 16:00:26 -0500120 void PartitionScan(void);
srs56940a697312010-01-28 21:10:52 -0500121 int LoadPartitions(const string & deviceFilename);
srs5694546a9c72010-01-26 16:00:26 -0500122 int ForceLoadGPTData(void);
srs5694e7b4ff92009-08-18 13:16:10 -0400123 int LoadMainTable(void);
srs5694546a9c72010-01-26 16:00:26 -0500124 int LoadSecondTableAsMain(void);
srs569464cbd172011-03-01 22:03:54 -0500125 int SaveGPTData(int quiet = 0);
srs56940a697312010-01-28 21:10:52 -0500126 int SaveGPTBackup(const string & filename);
127 int LoadGPTBackup(const string & filename);
srs569408bb0da2010-02-19 17:19:55 -0500128 int SaveMBR(void);
129 int DestroyGPT(void);
130 int DestroyMBR(void);
srs5694e4ac11e2009-08-31 10:13:04 -0400131
132 // Display data....
133 void ShowAPMState(void);
134 void ShowGPTState(void);
srs5694e7b4ff92009-08-18 13:16:10 -0400135 void DisplayGPTData(void);
136 void DisplayMBRData(void) {protectiveMBR.DisplayMBRData();}
srs5694e7b4ff92009-08-18 13:16:10 -0400137 void ShowPartDetails(uint32_t partNum);
srs5694e4ac11e2009-08-31 10:13:04 -0400138
srs569408bb0da2010-02-19 17:19:55 -0500139 // Convert between GPT and other formats
140 virtual WhichToUse UseWhichPartitions(void);
141 void XFormPartitions(void);
Roderick W. Smith1eea9b02013-07-06 22:52:58 -0400142 int XFormDisklabel(uint32_t partNum);
srs569408bb0da2010-02-19 17:19:55 -0500143 int XFormDisklabel(BSDData* disklabel);
srs5694978041c2009-09-21 20:51:47 -0400144 int OnePartToMBR(uint32_t gptPart, int mbrPart); // add one partition to MBR. Returns 1 if successful
srs5694e4ac11e2009-08-31 10:13:04 -0400145
146 // Adjust GPT structures WITHOUT user interaction...
srs5694706e5122012-01-21 13:47:24 -0500147 int SetGPTSize(uint32_t numEntries, int fillGPTSectors = 1);
Rod Smith503e9ad2017-07-21 21:48:13 -0400148 int MoveMainTable(uint64_t pteSector);
srs5694e4ac11e2009-08-31 10:13:04 -0400149 void BlankPartitions(void);
srs5694ba00fed2010-01-12 18:18:36 -0500150 int DeletePartition(uint32_t partNum);
srs5694e321d442010-01-29 17:44:04 -0500151 uint32_t CreatePartition(uint32_t partNum, uint64_t startSector, uint64_t endSector);
srs5694e7b4ff92009-08-18 13:16:10 -0400152 void SortGPT(void);
srs569408bb0da2010-02-19 17:19:55 -0500153 int SwapPartitions(uint32_t partNum1, uint32_t partNum2);
srs5694e7b4ff92009-08-18 13:16:10 -0400154 int ClearGPTData(void);
srs5694247657a2009-11-26 18:36:12 -0500155 void MoveSecondHeaderToEnd();
srs5694699941e2011-03-21 21:33:57 -0400156 int SetName(uint32_t partNum, const UnicodeString & theName);
srs5694e7b4ff92009-08-18 13:16:10 -0400157 void SetDiskGUID(GUIDData newGUID);
158 int SetPartitionGUID(uint32_t pn, GUIDData theGUID);
srs56949ba54212010-05-18 23:24:02 -0400159 void RandomizeGUIDs(void);
srs5694327129e2010-09-22 01:07:31 -0400160 int ChangePartType(uint32_t pn, PartType theGUID);
srs5694e4ac11e2009-08-31 10:13:04 -0400161 void MakeProtectiveMBR(void) {protectiveMBR.MakeProtectiveMBR();}
srs56949ba54212010-05-18 23:24:02 -0400162 void RecomputeCHS(void);
srs56941d1448a2009-12-31 21:20:19 -0500163 int Align(uint64_t* sector);
srs5694bf8950c2011-03-12 01:23:12 -0500164 void SetProtectiveMBR(BasicMBRData & newMBR) {protectiveMBR = newMBR;}
srs569400b6d7a2011-06-26 22:40:06 -0400165
166 // Return data about the GPT structures....
167 WhichToUse GetState(void) {return whichWasUsed;}
srs5694e4ac11e2009-08-31 10:13:04 -0400168 int GetPartRange(uint32_t* low, uint32_t* high);
srs569408bb0da2010-02-19 17:19:55 -0500169 int FindFirstFreePart(void);
srs5694e7b4ff92009-08-18 13:16:10 -0400170 uint32_t GetNumParts(void) {return mainHeader.numParts;}
Rod Smith503e9ad2017-07-21 21:48:13 -0400171 uint64_t GetTableSizeInSectors(void) {return (((numParts * GPT_SIZE) / blockSize) +
172 (((numParts * GPT_SIZE) % blockSize) != 0)); }
srs5694e7b4ff92009-08-18 13:16:10 -0400173 uint64_t GetMainHeaderLBA(void) {return mainHeader.currentLBA;}
174 uint64_t GetSecondHeaderLBA(void) {return secondHeader.currentLBA;}
175 uint64_t GetMainPartsLBA(void) {return mainHeader.partitionEntriesLBA;}
176 uint64_t GetSecondPartsLBA(void) {return secondHeader.partitionEntriesLBA;}
srs569455d92612010-03-07 22:16:07 -0500177 uint64_t GetFirstUsableLBA(void) {return mainHeader.firstUsableLBA;}
178 uint64_t GetLastUsableLBA(void) {return mainHeader.lastUsableLBA;}
srs5694978041c2009-09-21 20:51:47 -0400179 uint32_t CountParts(void);
srs56949ddc14b2010-08-22 22:44:42 -0400180 bool ValidPartNum (const uint32_t partNum);
srs56945a081752010-09-24 20:39:41 -0400181 const GPTPart & operator[](uint32_t partNum) const;
182 const GUIDData & GetDiskGUID(void) const;
srs5694df9d3632011-01-08 18:33:24 -0500183 uint32_t GetBlockSize(void) {return blockSize;}
srs5694e4ac11e2009-08-31 10:13:04 -0400184
185 // Find information about free space
186 uint64_t FindFirstAvailable(uint64_t start = 0);
Rod Smith503e9ad2017-07-21 21:48:13 -0400187 uint64_t FindFirstUsedLBA(void);
srs5694e4ac11e2009-08-31 10:13:04 -0400188 uint64_t FindFirstInLargest(void);
srs5694cb76c672010-02-11 22:22:22 -0500189 uint64_t FindLastAvailable();
srs5694e4ac11e2009-08-31 10:13:04 -0400190 uint64_t FindLastInFree(uint64_t start);
srs5694e321d442010-01-29 17:44:04 -0500191 uint64_t FindFreeBlocks(uint32_t *numSegments, uint64_t *largestSegment);
srs569455d92612010-03-07 22:16:07 -0500192 int IsFree(uint64_t sector, uint32_t *partNum = NULL);
srs5694ba00fed2010-01-12 18:18:36 -0500193 int IsFreePartNum(uint32_t partNum);
srs5694815fb652011-03-18 12:35:56 -0400194 int IsUsedPartNum(uint32_t partNum);
srs5694d8eed462012-12-15 01:55:21 -0500195
srs5694ba00fed2010-01-12 18:18:36 -0500196 // Change how functions work, or return information on same
srs5694a8582cf2010-03-19 14:21:59 -0400197 void SetAlignment(uint32_t n);
198 uint32_t ComputeAlignment(void); // Set alignment based on current partitions
199 uint32_t GetAlignment(void) {return sectorAlignment;}
srs5694ba00fed2010-01-12 18:18:36 -0500200 void JustLooking(int i = 1) {justLooking = i;}
201 void BeQuiet(int i = 1) {beQuiet = i;}
202 WhichToUse WhichWasUsed(void) {return whichWasUsed;}
srs5694e4ac11e2009-08-31 10:13:04 -0400203
204 // Endianness functions
srs56940a697312010-01-28 21:10:52 -0500205 void ReverseHeaderBytes(struct GPTHeader* header);
srs5694e4ac11e2009-08-31 10:13:04 -0400206 void ReversePartitionBytes(); // for endianness
srs56949ddc14b2010-08-22 22:44:42 -0400207
208 // Attributes functions
209 int ManageAttributes(int partNum, const string & command, const string & bits);
210 void ShowAttributes(const uint32_t partNum);
211 void GetAttribute(const uint32_t partNum, const string& attributeBits);
212
srs5694e7b4ff92009-08-18 13:16:10 -0400213}; // class GPTData
214
215// Function prototypes....
srs5694e7b4ff92009-08-18 13:16:10 -0400216int SizesOK(void);
217
Tom Marshall7b5b7112019-07-23 15:18:15 -0700218#pragma pack(pop)
219
srs5694e7b4ff92009-08-18 13:16:10 -0400220#endif