blob: 876ecde912d799e0d914ec843246018d5ba1e37f [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"
srs5694e7b4ff92009-08-18 13:16:10 -040014
15#ifndef __GPTSTRUCTS
16#define __GPTSTRUCTS
17
srs5694825c2642010-02-21 11:10:13 -050018#define GPTFDISK_VERSION "0.6.5-pre1"
srs569408bb0da2010-02-19 17:19:55 -050019
20// Constants used by GPTData::PartsToMBR(). MBR_EMPTY must be the lowest-
21// numbered value to refer to partition numbers. (Most will be 0 or positive,
22// of course.)
23#define MBR_EFI_GPT -1
24#define MBR_EMPTY -2
srs5694e7b4ff92009-08-18 13:16:10 -040025
26using namespace std;
27
28/****************************************
29 * *
30 * GPTData class and related structures *
31 * *
32 ****************************************/
33
34// Validity state of GPT data
35enum GPTValidity {gpt_valid, gpt_corrupt, gpt_invalid};
36
37// Which set of partition data to use
srs56943c0af382010-01-15 19:19:18 -050038enum WhichToUse {use_gpt, use_mbr, use_bsd, use_new, use_abort};
srs5694e7b4ff92009-08-18 13:16:10 -040039
40// Header (first 512 bytes) of GPT table
srs5694ba00fed2010-01-12 18:18:36 -050041#pragma pack(1)
srs5694e7b4ff92009-08-18 13:16:10 -040042struct GPTHeader {
43 uint64_t signature;
44 uint32_t revision;
45 uint32_t headerSize;
46 uint32_t headerCRC;
47 uint32_t reserved;
48 uint64_t currentLBA;
49 uint64_t backupLBA;
50 uint64_t firstUsableLBA;
51 uint64_t lastUsableLBA;
srs56946699b012010-02-04 00:55:30 -050052 GUIDData diskGUID;
srs5694e7b4ff92009-08-18 13:16:10 -040053 uint64_t partitionEntriesLBA;
54 uint32_t numParts;
55 uint32_t sizeOfPartitionEntries;
56 uint32_t partitionEntriesCRC;
57 unsigned char reserved2[GPT_RESERVED];
58}; // struct GPTHeader
59
srs5694e7b4ff92009-08-18 13:16:10 -040060// Data in GPT format
61class GPTData {
62protected:
63 struct GPTHeader mainHeader;
srs5694ba00fed2010-01-12 18:18:36 -050064 GPTPart *partitions;
srs5694e7b4ff92009-08-18 13:16:10 -040065 struct GPTHeader secondHeader;
66 MBRData protectiveMBR;
srs5694fed16d02010-01-27 23:03:40 -050067 string device; // device filename
srs5694546a9c72010-01-26 16:00:26 -050068 DiskIO myDisk;
srs5694e7b4ff92009-08-18 13:16:10 -040069 uint32_t blockSize; // device block size
70 uint64_t diskSize; // size of device, in blocks
71 GPTValidity state; // is GPT valid?
srs56945d58fe02010-01-03 20:57:08 -050072 int justLooking; // Set to 1 if program launched with "-l" or if read-only
srs5694e7b4ff92009-08-18 13:16:10 -040073 int mainCrcOk;
74 int secondCrcOk;
75 int mainPartsCrcOk;
76 int secondPartsCrcOk;
srs5694221e0872009-08-29 15:00:31 -040077 int apmFound; // set to 1 if APM detected
78 int bsdFound; // set to 1 if BSD disklabel detected in MBR
srs56941d1448a2009-12-31 21:20:19 -050079 int sectorAlignment; // Start & end partitions at multiples of sectorAlignment
srs5694ba00fed2010-01-12 18:18:36 -050080 int beQuiet;
81 WhichToUse whichWasUsed;
srs5694cb76c672010-02-11 22:22:22 -050082
83 int LoadHeader(struct GPTHeader *header, DiskIO & disk, uint64_t sector, int *crcOk);
84 int LoadPartitionTable(const struct GPTHeader & header, DiskIO & disk, uint64_t sector = 0);
85 int CheckTable(struct GPTHeader *header);
86 int SaveHeader(struct GPTHeader *header, DiskIO & disk, uint64_t sector);
87 int SavePartitionTable(DiskIO & disk, uint64_t sector);
srs5694e7b4ff92009-08-18 13:16:10 -040088public:
srs5694e4ac11e2009-08-31 10:13:04 -040089 // Basic necessary functions....
srs5694e7b4ff92009-08-18 13:16:10 -040090 GPTData(void);
srs5694fed16d02010-01-27 23:03:40 -050091 GPTData(string deviceFilename);
srs569408bb0da2010-02-19 17:19:55 -050092 virtual ~GPTData(void);
srs5694e4ac11e2009-08-31 10:13:04 -040093
94 // Verify (or update) data integrity
95 int Verify(void);
srs5694e7b4ff92009-08-18 13:16:10 -040096 int CheckGPTSize(void);
srs5694e4ac11e2009-08-31 10:13:04 -040097 int CheckHeaderValidity(void);
98 int CheckHeaderCRC(struct GPTHeader* header);
99 void RecomputeCRCs(void);
100 void RebuildMainHeader(void);
101 void RebuildSecondHeader(void);
102 int FindHybridMismatches(void);
103 int FindOverlaps(void);
104
105 // Load or save data from/to disk
srs56940a697312010-01-28 21:10:52 -0500106 int LoadMBR(const string & f) {return protectiveMBR.ReadMBRData(f);}
srs569408bb0da2010-02-19 17:19:55 -0500107 int WriteProtectiveMBR(void) {return protectiveMBR.WriteMBRData(&myDisk);}
srs5694546a9c72010-01-26 16:00:26 -0500108 void PartitionScan(void);
srs56940a697312010-01-28 21:10:52 -0500109 int LoadPartitions(const string & deviceFilename);
srs5694546a9c72010-01-26 16:00:26 -0500110 int ForceLoadGPTData(void);
srs5694e7b4ff92009-08-18 13:16:10 -0400111 int LoadMainTable(void);
srs5694546a9c72010-01-26 16:00:26 -0500112 int LoadSecondTableAsMain(void);
srs5694ba00fed2010-01-12 18:18:36 -0500113 int SaveGPTData(int quiet = 0);
srs56940a697312010-01-28 21:10:52 -0500114 int SaveGPTBackup(const string & filename);
115 int LoadGPTBackup(const string & filename);
srs569408bb0da2010-02-19 17:19:55 -0500116 int SaveMBR(void);
117 int DestroyGPT(void);
118 int DestroyMBR(void);
srs5694e4ac11e2009-08-31 10:13:04 -0400119
120 // Display data....
121 void ShowAPMState(void);
122 void ShowGPTState(void);
srs5694e7b4ff92009-08-18 13:16:10 -0400123 void DisplayGPTData(void);
124 void DisplayMBRData(void) {protectiveMBR.DisplayMBRData();}
srs5694e7b4ff92009-08-18 13:16:10 -0400125 void ShowPartDetails(uint32_t partNum);
srs5694e4ac11e2009-08-31 10:13:04 -0400126
srs569408bb0da2010-02-19 17:19:55 -0500127 // Convert between GPT and other formats
128 virtual WhichToUse UseWhichPartitions(void);
129 void XFormPartitions(void);
130 virtual int XFormDisklabel(uint32_t partNum);
131 int XFormDisklabel(BSDData* disklabel);
srs5694978041c2009-09-21 20:51:47 -0400132 int OnePartToMBR(uint32_t gptPart, int mbrPart); // add one partition to MBR. Returns 1 if successful
srs569408bb0da2010-02-19 17:19:55 -0500133 int PartsToMBR(const int *gptParts, const int *mbrTypes);
srs5694e4ac11e2009-08-31 10:13:04 -0400134
135 // Adjust GPT structures WITHOUT user interaction...
136 int SetGPTSize(uint32_t numEntries);
137 void BlankPartitions(void);
srs5694ba00fed2010-01-12 18:18:36 -0500138 int DeletePartition(uint32_t partNum);
srs5694e321d442010-01-29 17:44:04 -0500139 uint32_t CreatePartition(uint32_t partNum, uint64_t startSector, uint64_t endSector);
srs5694e7b4ff92009-08-18 13:16:10 -0400140 void SortGPT(void);
srs569408bb0da2010-02-19 17:19:55 -0500141 void QuickSortGPT(int start, int finish);
142 int SwapPartitions(uint32_t partNum1, uint32_t partNum2);
srs5694e7b4ff92009-08-18 13:16:10 -0400143 int ClearGPTData(void);
srs5694247657a2009-11-26 18:36:12 -0500144 void MoveSecondHeaderToEnd();
srs56940a697312010-01-28 21:10:52 -0500145 int SetName(uint32_t partNum, const string & theName = "");
srs5694e7b4ff92009-08-18 13:16:10 -0400146 void SetDiskGUID(GUIDData newGUID);
147 int SetPartitionGUID(uint32_t pn, GUIDData theGUID);
srs5694ba00fed2010-01-12 18:18:36 -0500148 int ChangePartType(uint32_t pn, uint16_t hexCode);
srs5694e4ac11e2009-08-31 10:13:04 -0400149 void MakeProtectiveMBR(void) {protectiveMBR.MakeProtectiveMBR();}
srs56941d1448a2009-12-31 21:20:19 -0500150 int Align(uint64_t* sector);
srs5694e7b4ff92009-08-18 13:16:10 -0400151
152 // Return data about the GPT structures....
srs5694e4ac11e2009-08-31 10:13:04 -0400153 int GetPartRange(uint32_t* low, uint32_t* high);
srs569408bb0da2010-02-19 17:19:55 -0500154 int FindFirstFreePart(void);
srs5694e7b4ff92009-08-18 13:16:10 -0400155 uint32_t GetNumParts(void) {return mainHeader.numParts;}
156 uint64_t GetMainHeaderLBA(void) {return mainHeader.currentLBA;}
157 uint64_t GetSecondHeaderLBA(void) {return secondHeader.currentLBA;}
158 uint64_t GetMainPartsLBA(void) {return mainHeader.partitionEntriesLBA;}
159 uint64_t GetSecondPartsLBA(void) {return secondHeader.partitionEntriesLBA;}
srs5694978041c2009-09-21 20:51:47 -0400160 uint32_t CountParts(void);
srs5694e4ac11e2009-08-31 10:13:04 -0400161
162 // Find information about free space
163 uint64_t FindFirstAvailable(uint64_t start = 0);
164 uint64_t FindFirstInLargest(void);
srs5694cb76c672010-02-11 22:22:22 -0500165 uint64_t FindLastAvailable();
srs5694e4ac11e2009-08-31 10:13:04 -0400166 uint64_t FindLastInFree(uint64_t start);
srs5694e321d442010-01-29 17:44:04 -0500167 uint64_t FindFreeBlocks(uint32_t *numSegments, uint64_t *largestSegment);
srs5694e4ac11e2009-08-31 10:13:04 -0400168 int IsFree(uint64_t sector);
srs5694ba00fed2010-01-12 18:18:36 -0500169 int IsFreePartNum(uint32_t partNum);
170
171 // Change how functions work, or return information on same
172 void SetAlignment(int n) {sectorAlignment = n;}
173 int GetAlignment(void) {return sectorAlignment;}
174 void JustLooking(int i = 1) {justLooking = i;}
175 void BeQuiet(int i = 1) {beQuiet = i;}
176 WhichToUse WhichWasUsed(void) {return whichWasUsed;}
srs5694e4ac11e2009-08-31 10:13:04 -0400177
178 // Endianness functions
srs56940a697312010-01-28 21:10:52 -0500179 void ReverseHeaderBytes(struct GPTHeader* header);
srs5694e4ac11e2009-08-31 10:13:04 -0400180 void ReversePartitionBytes(); // for endianness
srs5694e7b4ff92009-08-18 13:16:10 -0400181}; // class GPTData
182
183// Function prototypes....
srs5694e7b4ff92009-08-18 13:16:10 -0400184int SizesOK(void);
185
186#endif