blob: cae03397c148c071f54a616784d976730783e904 [file] [log] [blame]
srs5694e7b4ff92009-08-18 13:16:10 -04001/* gpt.h -- GPT and data structure definitions, types, and
2 functions */
3
srs5694221e0872009-08-29 15:00:31 -04004/* This program is copyright (c) 2009 by Roderick W. Smith. It is distributed
5 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
srs56948a4ddfc2010-03-21 19:05:49 -040019#define GPTFDISK_VERSION "0.6.6"
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
srs5694a8582cf2010-03-19 14:21:59 -040030
31// Below constant corresponds to an 800GB disk -- a somewhat arbitrary
32// cutoff
33//#define SMALLEST_ADVANCED_FORMAT UINT64_C(1677721600)
34// Now ~596GiB (640MB), since WD has introduced a smaller Advanced Format drive
35#define SMALLEST_ADVANCED_FORMAT UINT64_C(1250263728)
36
srs5694e7b4ff92009-08-18 13:16:10 -040037using namespace std;
38
srs569455d92612010-03-07 22:16:07 -050039class PartNotes;
40
srs5694e7b4ff92009-08-18 13:16:10 -040041/****************************************
42 * *
43 * GPTData class and related structures *
44 * *
45 ****************************************/
46
47// Validity state of GPT data
48enum GPTValidity {gpt_valid, gpt_corrupt, gpt_invalid};
49
50// Which set of partition data to use
srs56943c0af382010-01-15 19:19:18 -050051enum WhichToUse {use_gpt, use_mbr, use_bsd, use_new, use_abort};
srs5694e7b4ff92009-08-18 13:16:10 -040052
53// Header (first 512 bytes) of GPT table
srs5694ba00fed2010-01-12 18:18:36 -050054#pragma pack(1)
srs5694e7b4ff92009-08-18 13:16:10 -040055struct GPTHeader {
56 uint64_t signature;
57 uint32_t revision;
58 uint32_t headerSize;
59 uint32_t headerCRC;
60 uint32_t reserved;
61 uint64_t currentLBA;
62 uint64_t backupLBA;
63 uint64_t firstUsableLBA;
64 uint64_t lastUsableLBA;
srs56946699b012010-02-04 00:55:30 -050065 GUIDData diskGUID;
srs5694e7b4ff92009-08-18 13:16:10 -040066 uint64_t partitionEntriesLBA;
67 uint32_t numParts;
68 uint32_t sizeOfPartitionEntries;
69 uint32_t partitionEntriesCRC;
70 unsigned char reserved2[GPT_RESERVED];
71}; // struct GPTHeader
72
srs5694e7b4ff92009-08-18 13:16:10 -040073// Data in GPT format
74class GPTData {
75protected:
76 struct GPTHeader mainHeader;
srs5694ba00fed2010-01-12 18:18:36 -050077 GPTPart *partitions;
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
srs5694a8582cf2010-03-19 14:21:59 -040092 uint32_t sectorAlignment; // Start & end 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);
115 int FindHybridMismatches(void);
116 int FindOverlaps(void);
srs569455d92612010-03-07 22:16:07 -0500117 int FindInsanePartitions(void);
srs5694e4ac11e2009-08-31 10:13:04 -0400118
119 // Load or save data from/to disk
srs56940a697312010-01-28 21:10:52 -0500120 int LoadMBR(const string & f) {return protectiveMBR.ReadMBRData(f);}
srs569408bb0da2010-02-19 17:19:55 -0500121 int WriteProtectiveMBR(void) {return protectiveMBR.WriteMBRData(&myDisk);}
srs5694546a9c72010-01-26 16:00:26 -0500122 void PartitionScan(void);
srs56940a697312010-01-28 21:10:52 -0500123 int LoadPartitions(const string & deviceFilename);
srs5694546a9c72010-01-26 16:00:26 -0500124 int ForceLoadGPTData(void);
srs5694e7b4ff92009-08-18 13:16:10 -0400125 int LoadMainTable(void);
srs5694546a9c72010-01-26 16:00:26 -0500126 int LoadSecondTableAsMain(void);
srs5694ba00fed2010-01-12 18:18:36 -0500127 int SaveGPTData(int quiet = 0);
srs56940a697312010-01-28 21:10:52 -0500128 int SaveGPTBackup(const string & filename);
129 int LoadGPTBackup(const string & filename);
srs569408bb0da2010-02-19 17:19:55 -0500130 int SaveMBR(void);
131 int DestroyGPT(void);
132 int DestroyMBR(void);
srs5694e4ac11e2009-08-31 10:13:04 -0400133
134 // Display data....
135 void ShowAPMState(void);
136 void ShowGPTState(void);
srs5694e7b4ff92009-08-18 13:16:10 -0400137 void DisplayGPTData(void);
138 void DisplayMBRData(void) {protectiveMBR.DisplayMBRData();}
srs5694e7b4ff92009-08-18 13:16:10 -0400139 void ShowPartDetails(uint32_t partNum);
srs5694e4ac11e2009-08-31 10:13:04 -0400140
srs569408bb0da2010-02-19 17:19:55 -0500141 // Convert between GPT and other formats
142 virtual WhichToUse UseWhichPartitions(void);
143 void XFormPartitions(void);
144 virtual int XFormDisklabel(uint32_t partNum);
145 int XFormDisklabel(BSDData* disklabel);
srs5694978041c2009-09-21 20:51:47 -0400146 int OnePartToMBR(uint32_t gptPart, int mbrPart); // add one partition to MBR. Returns 1 if successful
srs569455d92612010-03-07 22:16:07 -0500147 int PartsToMBR(PartNotes & notes);
srs5694e4ac11e2009-08-31 10:13:04 -0400148
149 // Adjust GPT structures WITHOUT user interaction...
150 int SetGPTSize(uint32_t numEntries);
151 void BlankPartitions(void);
srs5694ba00fed2010-01-12 18:18:36 -0500152 int DeletePartition(uint32_t partNum);
srs5694e321d442010-01-29 17:44:04 -0500153 uint32_t CreatePartition(uint32_t partNum, uint64_t startSector, uint64_t endSector);
srs5694e7b4ff92009-08-18 13:16:10 -0400154 void SortGPT(void);
srs569408bb0da2010-02-19 17:19:55 -0500155 void QuickSortGPT(int start, int finish);
156 int SwapPartitions(uint32_t partNum1, uint32_t partNum2);
srs5694e7b4ff92009-08-18 13:16:10 -0400157 int ClearGPTData(void);
srs5694247657a2009-11-26 18:36:12 -0500158 void MoveSecondHeaderToEnd();
srs56940a697312010-01-28 21:10:52 -0500159 int SetName(uint32_t partNum, const string & theName = "");
srs5694e7b4ff92009-08-18 13:16:10 -0400160 void SetDiskGUID(GUIDData newGUID);
161 int SetPartitionGUID(uint32_t pn, GUIDData theGUID);
srs5694ba00fed2010-01-12 18:18:36 -0500162 int ChangePartType(uint32_t pn, uint16_t hexCode);
srs5694e4ac11e2009-08-31 10:13:04 -0400163 void MakeProtectiveMBR(void) {protectiveMBR.MakeProtectiveMBR();}
srs56941d1448a2009-12-31 21:20:19 -0500164 int Align(uint64_t* sector);
srs5694e7b4ff92009-08-18 13:16:10 -0400165
166 // Return data about the GPT structures....
srs5694e4ac11e2009-08-31 10:13:04 -0400167 int GetPartRange(uint32_t* low, uint32_t* high);
srs569408bb0da2010-02-19 17:19:55 -0500168 int FindFirstFreePart(void);
srs5694e7b4ff92009-08-18 13:16:10 -0400169 uint32_t GetNumParts(void) {return mainHeader.numParts;}
170 uint64_t GetMainHeaderLBA(void) {return mainHeader.currentLBA;}
171 uint64_t GetSecondHeaderLBA(void) {return secondHeader.currentLBA;}
172 uint64_t GetMainPartsLBA(void) {return mainHeader.partitionEntriesLBA;}
173 uint64_t GetSecondPartsLBA(void) {return secondHeader.partitionEntriesLBA;}
srs569455d92612010-03-07 22:16:07 -0500174 uint64_t GetFirstUsableLBA(void) {return mainHeader.firstUsableLBA;}
175 uint64_t GetLastUsableLBA(void) {return mainHeader.lastUsableLBA;}
176 uint64_t GetPartFirstLBA(uint32_t i) {return partitions[i].GetFirstLBA();}
177 uint64_t GetPartLastLBA(uint32_t i) {return partitions[i].GetLastLBA();}
srs5694978041c2009-09-21 20:51:47 -0400178 uint32_t CountParts(void);
srs5694e4ac11e2009-08-31 10:13:04 -0400179
180 // Find information about free space
181 uint64_t FindFirstAvailable(uint64_t start = 0);
182 uint64_t FindFirstInLargest(void);
srs5694cb76c672010-02-11 22:22:22 -0500183 uint64_t FindLastAvailable();
srs5694e4ac11e2009-08-31 10:13:04 -0400184 uint64_t FindLastInFree(uint64_t start);
srs5694e321d442010-01-29 17:44:04 -0500185 uint64_t FindFreeBlocks(uint32_t *numSegments, uint64_t *largestSegment);
srs569455d92612010-03-07 22:16:07 -0500186 int IsFree(uint64_t sector, uint32_t *partNum = NULL);
srs5694ba00fed2010-01-12 18:18:36 -0500187 int IsFreePartNum(uint32_t partNum);
188
189 // Change how functions work, or return information on same
srs5694a8582cf2010-03-19 14:21:59 -0400190 void SetAlignment(uint32_t n);
191 uint32_t ComputeAlignment(void); // Set alignment based on current partitions
192 uint32_t GetAlignment(void) {return sectorAlignment;}
srs5694ba00fed2010-01-12 18:18:36 -0500193 void JustLooking(int i = 1) {justLooking = i;}
194 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
srs5694e7b4ff92009-08-18 13:16:10 -0400200}; // class GPTData
201
202// Function prototypes....
srs5694e7b4ff92009-08-18 13:16:10 -0400203int SizesOK(void);
204
205#endif