blob: b2657e76ac2d0b6be248a6fadec957dd0403d90c [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
srs569455d92612010-03-07 22:16:07 -050019#define GPTFDISK_VERSION "0.6.5"
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
27using namespace std;
28
srs569455d92612010-03-07 22:16:07 -050029class PartNotes;
30
srs5694e7b4ff92009-08-18 13:16:10 -040031/****************************************
32 * *
33 * GPTData class and related structures *
34 * *
35 ****************************************/
36
37// Validity state of GPT data
38enum GPTValidity {gpt_valid, gpt_corrupt, gpt_invalid};
39
40// Which set of partition data to use
srs56943c0af382010-01-15 19:19:18 -050041enum WhichToUse {use_gpt, use_mbr, use_bsd, use_new, use_abort};
srs5694e7b4ff92009-08-18 13:16:10 -040042
43// Header (first 512 bytes) of GPT table
srs5694ba00fed2010-01-12 18:18:36 -050044#pragma pack(1)
srs5694e7b4ff92009-08-18 13:16:10 -040045struct GPTHeader {
46 uint64_t signature;
47 uint32_t revision;
48 uint32_t headerSize;
49 uint32_t headerCRC;
50 uint32_t reserved;
51 uint64_t currentLBA;
52 uint64_t backupLBA;
53 uint64_t firstUsableLBA;
54 uint64_t lastUsableLBA;
srs56946699b012010-02-04 00:55:30 -050055 GUIDData diskGUID;
srs5694e7b4ff92009-08-18 13:16:10 -040056 uint64_t partitionEntriesLBA;
57 uint32_t numParts;
58 uint32_t sizeOfPartitionEntries;
59 uint32_t partitionEntriesCRC;
60 unsigned char reserved2[GPT_RESERVED];
61}; // struct GPTHeader
62
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;
srs5694e7b4ff92009-08-18 13:16:10 -040068 struct GPTHeader secondHeader;
69 MBRData protectiveMBR;
srs5694fed16d02010-01-27 23:03:40 -050070 string device; // device filename
srs5694546a9c72010-01-26 16:00:26 -050071 DiskIO myDisk;
srs5694e7b4ff92009-08-18 13:16:10 -040072 uint32_t blockSize; // device block size
73 uint64_t diskSize; // size of device, in blocks
74 GPTValidity state; // is GPT valid?
srs56945d58fe02010-01-03 20:57:08 -050075 int justLooking; // Set to 1 if program launched with "-l" or if read-only
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
srs56941d1448a2009-12-31 21:20:19 -050082 int sectorAlignment; // Start & end 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);
srs5694e4ac11e2009-08-31 10:13:04 -040096
97 // Verify (or update) data integrity
98 int Verify(void);
srs5694e7b4ff92009-08-18 13:16:10 -040099 int CheckGPTSize(void);
srs5694e4ac11e2009-08-31 10:13:04 -0400100 int CheckHeaderValidity(void);
101 int CheckHeaderCRC(struct GPTHeader* header);
102 void RecomputeCRCs(void);
103 void RebuildMainHeader(void);
104 void RebuildSecondHeader(void);
105 int FindHybridMismatches(void);
106 int FindOverlaps(void);
srs569455d92612010-03-07 22:16:07 -0500107 int FindInsanePartitions(void);
srs5694e4ac11e2009-08-31 10:13:04 -0400108
109 // Load or save data from/to disk
srs56940a697312010-01-28 21:10:52 -0500110 int LoadMBR(const string & f) {return protectiveMBR.ReadMBRData(f);}
srs569408bb0da2010-02-19 17:19:55 -0500111 int WriteProtectiveMBR(void) {return protectiveMBR.WriteMBRData(&myDisk);}
srs5694546a9c72010-01-26 16:00:26 -0500112 void PartitionScan(void);
srs56940a697312010-01-28 21:10:52 -0500113 int LoadPartitions(const string & deviceFilename);
srs5694546a9c72010-01-26 16:00:26 -0500114 int ForceLoadGPTData(void);
srs5694e7b4ff92009-08-18 13:16:10 -0400115 int LoadMainTable(void);
srs5694546a9c72010-01-26 16:00:26 -0500116 int LoadSecondTableAsMain(void);
srs5694ba00fed2010-01-12 18:18:36 -0500117 int SaveGPTData(int quiet = 0);
srs56940a697312010-01-28 21:10:52 -0500118 int SaveGPTBackup(const string & filename);
119 int LoadGPTBackup(const string & filename);
srs569408bb0da2010-02-19 17:19:55 -0500120 int SaveMBR(void);
121 int DestroyGPT(void);
122 int DestroyMBR(void);
srs5694e4ac11e2009-08-31 10:13:04 -0400123
124 // Display data....
125 void ShowAPMState(void);
126 void ShowGPTState(void);
srs5694e7b4ff92009-08-18 13:16:10 -0400127 void DisplayGPTData(void);
128 void DisplayMBRData(void) {protectiveMBR.DisplayMBRData();}
srs5694e7b4ff92009-08-18 13:16:10 -0400129 void ShowPartDetails(uint32_t partNum);
srs5694e4ac11e2009-08-31 10:13:04 -0400130
srs569408bb0da2010-02-19 17:19:55 -0500131 // Convert between GPT and other formats
132 virtual WhichToUse UseWhichPartitions(void);
133 void XFormPartitions(void);
134 virtual int XFormDisklabel(uint32_t partNum);
135 int XFormDisklabel(BSDData* disklabel);
srs5694978041c2009-09-21 20:51:47 -0400136 int OnePartToMBR(uint32_t gptPart, int mbrPart); // add one partition to MBR. Returns 1 if successful
srs569455d92612010-03-07 22:16:07 -0500137 int PartsToMBR(PartNotes & notes);
srs5694e4ac11e2009-08-31 10:13:04 -0400138
139 // Adjust GPT structures WITHOUT user interaction...
140 int SetGPTSize(uint32_t numEntries);
141 void BlankPartitions(void);
srs5694ba00fed2010-01-12 18:18:36 -0500142 int DeletePartition(uint32_t partNum);
srs5694e321d442010-01-29 17:44:04 -0500143 uint32_t CreatePartition(uint32_t partNum, uint64_t startSector, uint64_t endSector);
srs5694e7b4ff92009-08-18 13:16:10 -0400144 void SortGPT(void);
srs569408bb0da2010-02-19 17:19:55 -0500145 void QuickSortGPT(int start, int finish);
146 int SwapPartitions(uint32_t partNum1, uint32_t partNum2);
srs5694e7b4ff92009-08-18 13:16:10 -0400147 int ClearGPTData(void);
srs5694247657a2009-11-26 18:36:12 -0500148 void MoveSecondHeaderToEnd();
srs56940a697312010-01-28 21:10:52 -0500149 int SetName(uint32_t partNum, const string & theName = "");
srs5694e7b4ff92009-08-18 13:16:10 -0400150 void SetDiskGUID(GUIDData newGUID);
151 int SetPartitionGUID(uint32_t pn, GUIDData theGUID);
srs5694ba00fed2010-01-12 18:18:36 -0500152 int ChangePartType(uint32_t pn, uint16_t hexCode);
srs5694e4ac11e2009-08-31 10:13:04 -0400153 void MakeProtectiveMBR(void) {protectiveMBR.MakeProtectiveMBR();}
srs56941d1448a2009-12-31 21:20:19 -0500154 int Align(uint64_t* sector);
srs5694e7b4ff92009-08-18 13:16:10 -0400155
156 // Return data about the GPT structures....
srs5694e4ac11e2009-08-31 10:13:04 -0400157 int GetPartRange(uint32_t* low, uint32_t* high);
srs569408bb0da2010-02-19 17:19:55 -0500158 int FindFirstFreePart(void);
srs5694e7b4ff92009-08-18 13:16:10 -0400159 uint32_t GetNumParts(void) {return mainHeader.numParts;}
160 uint64_t GetMainHeaderLBA(void) {return mainHeader.currentLBA;}
161 uint64_t GetSecondHeaderLBA(void) {return secondHeader.currentLBA;}
162 uint64_t GetMainPartsLBA(void) {return mainHeader.partitionEntriesLBA;}
163 uint64_t GetSecondPartsLBA(void) {return secondHeader.partitionEntriesLBA;}
srs569455d92612010-03-07 22:16:07 -0500164 uint64_t GetFirstUsableLBA(void) {return mainHeader.firstUsableLBA;}
165 uint64_t GetLastUsableLBA(void) {return mainHeader.lastUsableLBA;}
166 uint64_t GetPartFirstLBA(uint32_t i) {return partitions[i].GetFirstLBA();}
167 uint64_t GetPartLastLBA(uint32_t i) {return partitions[i].GetLastLBA();}
srs5694978041c2009-09-21 20:51:47 -0400168 uint32_t CountParts(void);
srs5694e4ac11e2009-08-31 10:13:04 -0400169
170 // Find information about free space
171 uint64_t FindFirstAvailable(uint64_t start = 0);
172 uint64_t FindFirstInLargest(void);
srs5694cb76c672010-02-11 22:22:22 -0500173 uint64_t FindLastAvailable();
srs5694e4ac11e2009-08-31 10:13:04 -0400174 uint64_t FindLastInFree(uint64_t start);
srs5694e321d442010-01-29 17:44:04 -0500175 uint64_t FindFreeBlocks(uint32_t *numSegments, uint64_t *largestSegment);
srs569455d92612010-03-07 22:16:07 -0500176 int IsFree(uint64_t sector, uint32_t *partNum = NULL);
srs5694ba00fed2010-01-12 18:18:36 -0500177 int IsFreePartNum(uint32_t partNum);
178
179 // Change how functions work, or return information on same
180 void SetAlignment(int n) {sectorAlignment = n;}
181 int GetAlignment(void) {return sectorAlignment;}
182 void JustLooking(int i = 1) {justLooking = i;}
183 void BeQuiet(int i = 1) {beQuiet = i;}
184 WhichToUse WhichWasUsed(void) {return whichWasUsed;}
srs5694e4ac11e2009-08-31 10:13:04 -0400185
186 // Endianness functions
srs56940a697312010-01-28 21:10:52 -0500187 void ReverseHeaderBytes(struct GPTHeader* header);
srs5694e4ac11e2009-08-31 10:13:04 -0400188 void ReversePartitionBytes(); // for endianness
srs5694e7b4ff92009-08-18 13:16:10 -0400189}; // class GPTData
190
191// Function prototypes....
srs5694e7b4ff92009-08-18 13:16:10 -0400192int SizesOK(void);
193
194#endif