blob: 1c0eaafe8e7c6d76a7e015ed0c6299bbaf025bfd [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
srs5694ba00fed2010-01-12 18:18:36 -050042#pragma pack(1)
srs5694e7b4ff92009-08-18 13:16:10 -040043struct 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;
srs56946699b012010-02-04 00:55:30 -050053 GUIDData diskGUID;
srs5694e7b4ff92009-08-18 13:16:10 -040054 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
srs5694e7b4ff92009-08-18 13:16:10 -040061// Data in GPT format
62class GPTData {
63protected:
64 struct GPTHeader mainHeader;
srs5694ba00fed2010-01-12 18:18:36 -050065 GPTPart *partitions;
srs5694815fb652011-03-18 12:35:56 -040066 uint32_t numParts; // # of partitions the table can hold
srs5694e7b4ff92009-08-18 13:16:10 -040067 struct GPTHeader secondHeader;
68 MBRData protectiveMBR;
srs5694fed16d02010-01-27 23:03:40 -050069 string device; // device filename
srs5694546a9c72010-01-26 16:00:26 -050070 DiskIO myDisk;
srs5694e7b4ff92009-08-18 13:16:10 -040071 uint32_t blockSize; // device block size
72 uint64_t diskSize; // size of device, in blocks
73 GPTValidity state; // is GPT valid?
srs56945d58fe02010-01-03 20:57:08 -050074 int justLooking; // Set to 1 if program launched with "-l" or if read-only
Greg Hartman2c2deeb2016-04-21 18:20:25 -070075 bool syncing; // Set to true if we should sync and reload the partition table
srs5694e7b4ff92009-08-18 13:16:10 -040076 int mainCrcOk;
77 int secondCrcOk;
78 int mainPartsCrcOk;
79 int secondPartsCrcOk;
srs5694221e0872009-08-29 15:00:31 -040080 int apmFound; // set to 1 if APM detected
81 int bsdFound; // set to 1 if BSD disklabel detected in MBR
srs56940873e9d2010-10-07 13:00:45 -040082 uint32_t sectorAlignment; // Start partitions at multiples of sectorAlignment
srs5694ba00fed2010-01-12 18:18:36 -050083 int beQuiet;
84 WhichToUse whichWasUsed;
srs5694cb76c672010-02-11 22:22:22 -050085
86 int LoadHeader(struct GPTHeader *header, DiskIO & disk, uint64_t sector, int *crcOk);
87 int LoadPartitionTable(const struct GPTHeader & header, DiskIO & disk, uint64_t sector = 0);
88 int CheckTable(struct GPTHeader *header);
89 int SaveHeader(struct GPTHeader *header, DiskIO & disk, uint64_t sector);
90 int SavePartitionTable(DiskIO & disk, uint64_t sector);
srs5694e7b4ff92009-08-18 13:16:10 -040091public:
srs5694e4ac11e2009-08-31 10:13:04 -040092 // Basic necessary functions....
srs5694e7b4ff92009-08-18 13:16:10 -040093 GPTData(void);
srs5694fed16d02010-01-27 23:03:40 -050094 GPTData(string deviceFilename);
srs569408bb0da2010-02-19 17:19:55 -050095 virtual ~GPTData(void);
srs569464cbd172011-03-01 22:03:54 -050096 GPTData & operator=(const GPTData & orig);
srs5694e4ac11e2009-08-31 10:13:04 -040097
98 // Verify (or update) data integrity
99 int Verify(void);
srs5694e7b4ff92009-08-18 13:16:10 -0400100 int CheckGPTSize(void);
srs5694e4ac11e2009-08-31 10:13:04 -0400101 int CheckHeaderValidity(void);
srs5694d1b11e82011-09-18 21:12:28 -0400102 int CheckHeaderCRC(struct GPTHeader* header, int warn = 0);
srs5694e4ac11e2009-08-31 10:13:04 -0400103 void RecomputeCRCs(void);
104 void RebuildMainHeader(void);
105 void RebuildSecondHeader(void);
srs5694bf8950c2011-03-12 01:23:12 -0500106 int VerifyMBR(void) {return protectiveMBR.FindOverlaps();}
srs5694e4ac11e2009-08-31 10:13:04 -0400107 int FindHybridMismatches(void);
108 int FindOverlaps(void);
srs569455d92612010-03-07 22:16:07 -0500109 int FindInsanePartitions(void);
srs5694e4ac11e2009-08-31 10:13:04 -0400110
111 // Load or save data from/to disk
srs5694bf8950c2011-03-12 01:23:12 -0500112 int SetDisk(const string & deviceFilename);
113 DiskIO* GetDisk(void) {return &myDisk;}
srs56940a697312010-01-28 21:10:52 -0500114 int LoadMBR(const string & f) {return protectiveMBR.ReadMBRData(f);}
srs569408bb0da2010-02-19 17:19:55 -0500115 int WriteProtectiveMBR(void) {return protectiveMBR.WriteMBRData(&myDisk);}
srs5694546a9c72010-01-26 16:00:26 -0500116 void PartitionScan(void);
srs56940a697312010-01-28 21:10:52 -0500117 int LoadPartitions(const string & deviceFilename);
srs5694546a9c72010-01-26 16:00:26 -0500118 int ForceLoadGPTData(void);
srs5694e7b4ff92009-08-18 13:16:10 -0400119 int LoadMainTable(void);
srs5694546a9c72010-01-26 16:00:26 -0500120 int LoadSecondTableAsMain(void);
srs569464cbd172011-03-01 22:03:54 -0500121 int SaveGPTData(int quiet = 0);
srs56940a697312010-01-28 21:10:52 -0500122 int SaveGPTBackup(const string & filename);
123 int LoadGPTBackup(const string & filename);
srs569408bb0da2010-02-19 17:19:55 -0500124 int SaveMBR(void);
125 int DestroyGPT(void);
126 int DestroyMBR(void);
srs5694e4ac11e2009-08-31 10:13:04 -0400127
128 // Display data....
129 void ShowAPMState(void);
130 void ShowGPTState(void);
srs5694e7b4ff92009-08-18 13:16:10 -0400131 void DisplayGPTData(void);
132 void DisplayMBRData(void) {protectiveMBR.DisplayMBRData();}
Jeff Sharkeyd761ff52015-02-28 19:18:39 -0800133 void ShowPartDetails(uint32_t partNum);
srs5694e4ac11e2009-08-31 10:13:04 -0400134
srs569408bb0da2010-02-19 17:19:55 -0500135 // Convert between GPT and other formats
136 virtual WhichToUse UseWhichPartitions(void);
137 void XFormPartitions(void);
Roderick W. Smith1eea9b02013-07-06 22:52:58 -0400138 int XFormDisklabel(uint32_t partNum);
srs569408bb0da2010-02-19 17:19:55 -0500139 int XFormDisklabel(BSDData* disklabel);
srs5694978041c2009-09-21 20:51:47 -0400140 int OnePartToMBR(uint32_t gptPart, int mbrPart); // add one partition to MBR. Returns 1 if successful
srs5694e4ac11e2009-08-31 10:13:04 -0400141
142 // Adjust GPT structures WITHOUT user interaction...
srs5694706e5122012-01-21 13:47:24 -0500143 int SetGPTSize(uint32_t numEntries, int fillGPTSectors = 1);
srs5694e4ac11e2009-08-31 10:13:04 -0400144 void BlankPartitions(void);
srs5694ba00fed2010-01-12 18:18:36 -0500145 int DeletePartition(uint32_t partNum);
srs5694e321d442010-01-29 17:44:04 -0500146 uint32_t CreatePartition(uint32_t partNum, uint64_t startSector, uint64_t endSector);
srs5694e7b4ff92009-08-18 13:16:10 -0400147 void SortGPT(void);
srs569408bb0da2010-02-19 17:19:55 -0500148 int SwapPartitions(uint32_t partNum1, uint32_t partNum2);
srs5694e7b4ff92009-08-18 13:16:10 -0400149 int ClearGPTData(void);
srs5694247657a2009-11-26 18:36:12 -0500150 void MoveSecondHeaderToEnd();
srs5694699941e2011-03-21 21:33:57 -0400151 int SetName(uint32_t partNum, const UnicodeString & theName);
srs5694e7b4ff92009-08-18 13:16:10 -0400152 void SetDiskGUID(GUIDData newGUID);
153 int SetPartitionGUID(uint32_t pn, GUIDData theGUID);
srs56949ba54212010-05-18 23:24:02 -0400154 void RandomizeGUIDs(void);
srs5694327129e2010-09-22 01:07:31 -0400155 int ChangePartType(uint32_t pn, PartType theGUID);
srs5694e4ac11e2009-08-31 10:13:04 -0400156 void MakeProtectiveMBR(void) {protectiveMBR.MakeProtectiveMBR();}
srs56949ba54212010-05-18 23:24:02 -0400157 void RecomputeCHS(void);
srs56941d1448a2009-12-31 21:20:19 -0500158 int Align(uint64_t* sector);
srs5694bf8950c2011-03-12 01:23:12 -0500159 void SetProtectiveMBR(BasicMBRData & newMBR) {protectiveMBR = newMBR;}
srs569400b6d7a2011-06-26 22:40:06 -0400160
161 // Return data about the GPT structures....
162 WhichToUse GetState(void) {return whichWasUsed;}
srs5694e4ac11e2009-08-31 10:13:04 -0400163 int GetPartRange(uint32_t* low, uint32_t* high);
srs569408bb0da2010-02-19 17:19:55 -0500164 int FindFirstFreePart(void);
srs5694e7b4ff92009-08-18 13:16:10 -0400165 uint32_t GetNumParts(void) {return mainHeader.numParts;}
166 uint64_t GetMainHeaderLBA(void) {return mainHeader.currentLBA;}
167 uint64_t GetSecondHeaderLBA(void) {return secondHeader.currentLBA;}
168 uint64_t GetMainPartsLBA(void) {return mainHeader.partitionEntriesLBA;}
169 uint64_t GetSecondPartsLBA(void) {return secondHeader.partitionEntriesLBA;}
srs569455d92612010-03-07 22:16:07 -0500170 uint64_t GetFirstUsableLBA(void) {return mainHeader.firstUsableLBA;}
171 uint64_t GetLastUsableLBA(void) {return mainHeader.lastUsableLBA;}
srs5694978041c2009-09-21 20:51:47 -0400172 uint32_t CountParts(void);
srs56949ddc14b2010-08-22 22:44:42 -0400173 bool ValidPartNum (const uint32_t partNum);
srs56945a081752010-09-24 20:39:41 -0400174 const GPTPart & operator[](uint32_t partNum) const;
175 const GUIDData & GetDiskGUID(void) const;
srs5694df9d3632011-01-08 18:33:24 -0500176 uint32_t GetBlockSize(void) {return blockSize;}
srs5694e4ac11e2009-08-31 10:13:04 -0400177
178 // Find information about free space
179 uint64_t FindFirstAvailable(uint64_t start = 0);
180 uint64_t FindFirstInLargest(void);
srs5694cb76c672010-02-11 22:22:22 -0500181 uint64_t FindLastAvailable();
srs5694e4ac11e2009-08-31 10:13:04 -0400182 uint64_t FindLastInFree(uint64_t start);
srs5694e321d442010-01-29 17:44:04 -0500183 uint64_t FindFreeBlocks(uint32_t *numSegments, uint64_t *largestSegment);
srs569455d92612010-03-07 22:16:07 -0500184 int IsFree(uint64_t sector, uint32_t *partNum = NULL);
srs5694ba00fed2010-01-12 18:18:36 -0500185 int IsFreePartNum(uint32_t partNum);
srs5694815fb652011-03-18 12:35:56 -0400186 int IsUsedPartNum(uint32_t partNum);
srs5694d8eed462012-12-15 01:55:21 -0500187
srs5694ba00fed2010-01-12 18:18:36 -0500188 // Change how functions work, or return information on same
srs5694a8582cf2010-03-19 14:21:59 -0400189 void SetAlignment(uint32_t n);
190 uint32_t ComputeAlignment(void); // Set alignment based on current partitions
191 uint32_t GetAlignment(void) {return sectorAlignment;}
srs5694ba00fed2010-01-12 18:18:36 -0500192 void JustLooking(int i = 1) {justLooking = i;}
Greg Hartman2c2deeb2016-04-21 18:20:25 -0700193 void TurnOffSyncing() {syncing = 0;}
srs5694ba00fed2010-01-12 18:18:36 -0500194 void BeQuiet(int i = 1) {beQuiet = i;}
195 WhichToUse WhichWasUsed(void) {return whichWasUsed;}
srs5694e4ac11e2009-08-31 10:13:04 -0400196
197 // Endianness functions
srs56940a697312010-01-28 21:10:52 -0500198 void ReverseHeaderBytes(struct GPTHeader* header);
srs5694e4ac11e2009-08-31 10:13:04 -0400199 void ReversePartitionBytes(); // for endianness
srs56949ddc14b2010-08-22 22:44:42 -0400200
201 // Attributes functions
202 int ManageAttributes(int partNum, const string & command, const string & bits);
203 void ShowAttributes(const uint32_t partNum);
204 void GetAttribute(const uint32_t partNum, const string& attributeBits);
205
srs5694e7b4ff92009-08-18 13:16:10 -0400206}; // class GPTData
207
208// Function prototypes....
srs5694e7b4ff92009-08-18 13:16:10 -0400209int SizesOK(void);
210
211#endif