blob: 9820cae3fd5ee2e14e0b1e25457f30bd1e1391ee [file] [log] [blame]
srs5694e7b4ff92009-08-18 13:16:10 -04001/* gpt.h -- GPT and data structure definitions, types, and
2 functions */
3
srs569461768bc2010-07-04 01:54:00 -04004/* This program is copyright (c) 2009, 2010 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"
srs569455d92612010-03-07 22:16:07 -050014#include "partnotes.h"
srs5694e7b4ff92009-08-18 13:16:10 -040015
16#ifndef __GPTSTRUCTS
17#define __GPTSTRUCTS
18
srs56940873e9d2010-10-07 13:00:45 -040019#define GPTFDISK_VERSION "0.6.12"
srs569408bb0da2010-02-19 17:19:55 -050020
21// Constants used by GPTData::PartsToMBR(). MBR_EMPTY must be the lowest-
22// numbered value to refer to partition numbers. (Most will be 0 or positive,
23// of course.)
24#define MBR_EFI_GPT -1
25#define MBR_EMPTY -2
srs5694e7b4ff92009-08-18 13:16:10 -040026
srs5694a8582cf2010-03-19 14:21:59 -040027// Default values for sector alignment
28#define DEFAULT_ALIGNMENT 2048
srs56948a4ddfc2010-03-21 19:05:49 -040029#define MAX_ALIGNMENT 65536
srs56940873e9d2010-10-07 13:00:45 -040030#define MIN_AF_ALIGNMENT 8
srs5694a8582cf2010-03-19 14:21:59 -040031
srs56940873e9d2010-10-07 13:00:45 -040032// Below constant corresponds to a ~596GiB (640MB) disk, since WD has
33// introduced a smaller Advanced Format drive
srs5694a8582cf2010-03-19 14:21:59 -040034#define SMALLEST_ADVANCED_FORMAT UINT64_C(1250263728)
35
srs5694e7b4ff92009-08-18 13:16:10 -040036using namespace std;
37
srs569455d92612010-03-07 22:16:07 -050038class PartNotes;
39
srs5694e7b4ff92009-08-18 13:16:10 -040040/****************************************
41 * *
42 * GPTData class and related structures *
43 * *
44 ****************************************/
45
46// Validity state of GPT data
47enum GPTValidity {gpt_valid, gpt_corrupt, gpt_invalid};
48
49// Which set of partition data to use
srs56943c0af382010-01-15 19:19:18 -050050enum WhichToUse {use_gpt, use_mbr, use_bsd, use_new, use_abort};
srs5694e7b4ff92009-08-18 13:16:10 -040051
52// Header (first 512 bytes) of GPT table
srs5694ba00fed2010-01-12 18:18:36 -050053#pragma pack(1)
srs5694e7b4ff92009-08-18 13:16:10 -040054struct GPTHeader {
55 uint64_t signature;
56 uint32_t revision;
57 uint32_t headerSize;
58 uint32_t headerCRC;
59 uint32_t reserved;
60 uint64_t currentLBA;
61 uint64_t backupLBA;
62 uint64_t firstUsableLBA;
63 uint64_t lastUsableLBA;
srs56946699b012010-02-04 00:55:30 -050064 GUIDData diskGUID;
srs5694e7b4ff92009-08-18 13:16:10 -040065 uint64_t partitionEntriesLBA;
66 uint32_t numParts;
67 uint32_t sizeOfPartitionEntries;
68 uint32_t partitionEntriesCRC;
69 unsigned char reserved2[GPT_RESERVED];
70}; // struct GPTHeader
71
srs5694e7b4ff92009-08-18 13:16:10 -040072// Data in GPT format
73class GPTData {
74protected:
75 struct GPTHeader mainHeader;
srs5694ba00fed2010-01-12 18:18:36 -050076 GPTPart *partitions;
srs56940283dae2010-04-28 16:44:34 -040077 uint32_t numParts;
srs5694e7b4ff92009-08-18 13:16:10 -040078 struct GPTHeader secondHeader;
79 MBRData protectiveMBR;
srs5694fed16d02010-01-27 23:03:40 -050080 string device; // device filename
srs5694546a9c72010-01-26 16:00:26 -050081 DiskIO myDisk;
srs5694e7b4ff92009-08-18 13:16:10 -040082 uint32_t blockSize; // device block size
83 uint64_t diskSize; // size of device, in blocks
84 GPTValidity state; // is GPT valid?
srs56945d58fe02010-01-03 20:57:08 -050085 int justLooking; // Set to 1 if program launched with "-l" or if read-only
srs5694e7b4ff92009-08-18 13:16:10 -040086 int mainCrcOk;
87 int secondCrcOk;
88 int mainPartsCrcOk;
89 int secondPartsCrcOk;
srs5694221e0872009-08-29 15:00:31 -040090 int apmFound; // set to 1 if APM detected
91 int bsdFound; // set to 1 if BSD disklabel detected in MBR
srs56940873e9d2010-10-07 13:00:45 -040092 uint32_t sectorAlignment; // Start partitions at multiples of sectorAlignment
srs5694ba00fed2010-01-12 18:18:36 -050093 int beQuiet;
94 WhichToUse whichWasUsed;
srs5694cb76c672010-02-11 22:22:22 -050095
96 int LoadHeader(struct GPTHeader *header, DiskIO & disk, uint64_t sector, int *crcOk);
97 int LoadPartitionTable(const struct GPTHeader & header, DiskIO & disk, uint64_t sector = 0);
98 int CheckTable(struct GPTHeader *header);
99 int SaveHeader(struct GPTHeader *header, DiskIO & disk, uint64_t sector);
100 int SavePartitionTable(DiskIO & disk, uint64_t sector);
srs5694e7b4ff92009-08-18 13:16:10 -0400101public:
srs5694e4ac11e2009-08-31 10:13:04 -0400102 // Basic necessary functions....
srs5694e7b4ff92009-08-18 13:16:10 -0400103 GPTData(void);
srs5694fed16d02010-01-27 23:03:40 -0500104 GPTData(string deviceFilename);
srs569408bb0da2010-02-19 17:19:55 -0500105 virtual ~GPTData(void);
srs5694e4ac11e2009-08-31 10:13:04 -0400106
107 // Verify (or update) data integrity
108 int Verify(void);
srs5694e7b4ff92009-08-18 13:16:10 -0400109 int CheckGPTSize(void);
srs5694e4ac11e2009-08-31 10:13:04 -0400110 int CheckHeaderValidity(void);
111 int CheckHeaderCRC(struct GPTHeader* header);
112 void RecomputeCRCs(void);
113 void RebuildMainHeader(void);
114 void RebuildSecondHeader(void);
srs5694327129e2010-09-22 01:07:31 -0400115 int VerifyMBR(void) {return protectiveMBR.Verify();}
srs5694e4ac11e2009-08-31 10:13:04 -0400116 int FindHybridMismatches(void);
117 int FindOverlaps(void);
srs569455d92612010-03-07 22:16:07 -0500118 int FindInsanePartitions(void);
srs5694e4ac11e2009-08-31 10:13:04 -0400119
120 // Load or save data from/to disk
srs56940a697312010-01-28 21:10:52 -0500121 int LoadMBR(const string & f) {return protectiveMBR.ReadMBRData(f);}
srs569408bb0da2010-02-19 17:19:55 -0500122 int WriteProtectiveMBR(void) {return protectiveMBR.WriteMBRData(&myDisk);}
srs5694546a9c72010-01-26 16:00:26 -0500123 void PartitionScan(void);
srs56940a697312010-01-28 21:10:52 -0500124 int LoadPartitions(const string & deviceFilename);
srs5694546a9c72010-01-26 16:00:26 -0500125 int ForceLoadGPTData(void);
srs5694e7b4ff92009-08-18 13:16:10 -0400126 int LoadMainTable(void);
srs5694546a9c72010-01-26 16:00:26 -0500127 int LoadSecondTableAsMain(void);
srs5694f9312b02010-07-06 15:39:51 -0400128 int SaveGPTData(int quiet = 0, string filename = "");
srs56940a697312010-01-28 21:10:52 -0500129 int SaveGPTBackup(const string & filename);
130 int LoadGPTBackup(const string & filename);
srs569408bb0da2010-02-19 17:19:55 -0500131 int SaveMBR(void);
132 int DestroyGPT(void);
133 int DestroyMBR(void);
srs5694e4ac11e2009-08-31 10:13:04 -0400134
135 // Display data....
136 void ShowAPMState(void);
137 void ShowGPTState(void);
srs5694e7b4ff92009-08-18 13:16:10 -0400138 void DisplayGPTData(void);
139 void DisplayMBRData(void) {protectiveMBR.DisplayMBRData();}
srs5694e7b4ff92009-08-18 13:16:10 -0400140 void ShowPartDetails(uint32_t partNum);
srs5694e4ac11e2009-08-31 10:13:04 -0400141
srs569408bb0da2010-02-19 17:19:55 -0500142 // Convert between GPT and other formats
143 virtual WhichToUse UseWhichPartitions(void);
144 void XFormPartitions(void);
145 virtual int XFormDisklabel(uint32_t partNum);
146 int XFormDisklabel(BSDData* disklabel);
srs5694978041c2009-09-21 20:51:47 -0400147 int OnePartToMBR(uint32_t gptPart, int mbrPart); // add one partition to MBR. Returns 1 if successful
srs569455d92612010-03-07 22:16:07 -0500148 int PartsToMBR(PartNotes & notes);
srs5694e4ac11e2009-08-31 10:13:04 -0400149
150 // Adjust GPT structures WITHOUT user interaction...
151 int SetGPTSize(uint32_t numEntries);
152 void BlankPartitions(void);
srs5694ba00fed2010-01-12 18:18:36 -0500153 int DeletePartition(uint32_t partNum);
srs5694e321d442010-01-29 17:44:04 -0500154 uint32_t CreatePartition(uint32_t partNum, uint64_t startSector, uint64_t endSector);
srs5694e7b4ff92009-08-18 13:16:10 -0400155 void SortGPT(void);
srs569408bb0da2010-02-19 17:19:55 -0500156 void QuickSortGPT(int start, int finish);
157 int SwapPartitions(uint32_t partNum1, uint32_t partNum2);
srs5694e7b4ff92009-08-18 13:16:10 -0400158 int ClearGPTData(void);
srs5694247657a2009-11-26 18:36:12 -0500159 void MoveSecondHeaderToEnd();
srs56940a697312010-01-28 21:10:52 -0500160 int SetName(uint32_t partNum, const string & theName = "");
srs5694e7b4ff92009-08-18 13:16:10 -0400161 void SetDiskGUID(GUIDData newGUID);
162 int SetPartitionGUID(uint32_t pn, GUIDData theGUID);
srs56949ba54212010-05-18 23:24:02 -0400163 void RandomizeGUIDs(void);
srs5694327129e2010-09-22 01:07:31 -0400164 int ChangePartType(uint32_t pn, PartType theGUID);
srs5694e4ac11e2009-08-31 10:13:04 -0400165 void MakeProtectiveMBR(void) {protectiveMBR.MakeProtectiveMBR();}
srs56949ba54212010-05-18 23:24:02 -0400166 void RecomputeCHS(void);
srs56941d1448a2009-12-31 21:20:19 -0500167 int Align(uint64_t* sector);
srs5694e7b4ff92009-08-18 13:16:10 -0400168
169 // Return data about the GPT structures....
srs5694e4ac11e2009-08-31 10:13:04 -0400170 int GetPartRange(uint32_t* low, uint32_t* high);
srs569408bb0da2010-02-19 17:19:55 -0500171 int FindFirstFreePart(void);
srs5694e7b4ff92009-08-18 13:16:10 -0400172 uint32_t GetNumParts(void) {return mainHeader.numParts;}
173 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;
srs5694e4ac11e2009-08-31 10:13:04 -0400183
184 // Find information about free space
185 uint64_t FindFirstAvailable(uint64_t start = 0);
186 uint64_t FindFirstInLargest(void);
srs5694cb76c672010-02-11 22:22:22 -0500187 uint64_t FindLastAvailable();
srs5694e4ac11e2009-08-31 10:13:04 -0400188 uint64_t FindLastInFree(uint64_t start);
srs5694e321d442010-01-29 17:44:04 -0500189 uint64_t FindFreeBlocks(uint32_t *numSegments, uint64_t *largestSegment);
srs569455d92612010-03-07 22:16:07 -0500190 int IsFree(uint64_t sector, uint32_t *partNum = NULL);
srs5694ba00fed2010-01-12 18:18:36 -0500191 int IsFreePartNum(uint32_t partNum);
192
193 // Change how functions work, or return information on same
srs5694a8582cf2010-03-19 14:21:59 -0400194 void SetAlignment(uint32_t n);
195 uint32_t ComputeAlignment(void); // Set alignment based on current partitions
196 uint32_t GetAlignment(void) {return sectorAlignment;}
srs5694ba00fed2010-01-12 18:18:36 -0500197 void JustLooking(int i = 1) {justLooking = i;}
198 void BeQuiet(int i = 1) {beQuiet = i;}
199 WhichToUse WhichWasUsed(void) {return whichWasUsed;}
srs5694e4ac11e2009-08-31 10:13:04 -0400200
201 // Endianness functions
srs56940a697312010-01-28 21:10:52 -0500202 void ReverseHeaderBytes(struct GPTHeader* header);
srs5694e4ac11e2009-08-31 10:13:04 -0400203 void ReversePartitionBytes(); // for endianness
srs56949ddc14b2010-08-22 22:44:42 -0400204
205 // Attributes functions
206 int ManageAttributes(int partNum, const string & command, const string & bits);
207 void ShowAttributes(const uint32_t partNum);
208 void GetAttribute(const uint32_t partNum, const string& attributeBits);
209
srs5694e7b4ff92009-08-18 13:16:10 -0400210}; // class GPTData
211
212// Function prototypes....
srs5694e7b4ff92009-08-18 13:16:10 -0400213int SizesOK(void);
214
215#endif