blob: 24786488c4ca980356c0da23eb352e9d2be1241a [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
srs5694cb76c672010-02-11 22:22:22 -050018#define GPTFDISK_VERSION "0.6.4-pre1"
srs5694e7b4ff92009-08-18 13:16:10 -040019
20using namespace std;
21
22/****************************************
23 * *
24 * GPTData class and related structures *
25 * *
26 ****************************************/
27
28// Validity state of GPT data
29enum GPTValidity {gpt_valid, gpt_corrupt, gpt_invalid};
30
31// Which set of partition data to use
srs56943c0af382010-01-15 19:19:18 -050032enum WhichToUse {use_gpt, use_mbr, use_bsd, use_new, use_abort};
srs5694e7b4ff92009-08-18 13:16:10 -040033
34// Header (first 512 bytes) of GPT table
srs5694ba00fed2010-01-12 18:18:36 -050035#pragma pack(1)
srs5694e7b4ff92009-08-18 13:16:10 -040036struct GPTHeader {
37 uint64_t signature;
38 uint32_t revision;
39 uint32_t headerSize;
40 uint32_t headerCRC;
41 uint32_t reserved;
42 uint64_t currentLBA;
43 uint64_t backupLBA;
44 uint64_t firstUsableLBA;
45 uint64_t lastUsableLBA;
srs56946699b012010-02-04 00:55:30 -050046 GUIDData diskGUID;
srs5694e7b4ff92009-08-18 13:16:10 -040047 uint64_t partitionEntriesLBA;
48 uint32_t numParts;
49 uint32_t sizeOfPartitionEntries;
50 uint32_t partitionEntriesCRC;
51 unsigned char reserved2[GPT_RESERVED];
52}; // struct GPTHeader
53
srs5694e7b4ff92009-08-18 13:16:10 -040054// Data in GPT format
55class GPTData {
56protected:
57 struct GPTHeader mainHeader;
srs5694ba00fed2010-01-12 18:18:36 -050058 GPTPart *partitions;
srs5694e7b4ff92009-08-18 13:16:10 -040059 struct GPTHeader secondHeader;
60 MBRData protectiveMBR;
srs5694fed16d02010-01-27 23:03:40 -050061 string device; // device filename
srs5694546a9c72010-01-26 16:00:26 -050062 DiskIO myDisk;
srs5694e7b4ff92009-08-18 13:16:10 -040063 uint32_t blockSize; // device block size
64 uint64_t diskSize; // size of device, in blocks
65 GPTValidity state; // is GPT valid?
srs56945d58fe02010-01-03 20:57:08 -050066 int justLooking; // Set to 1 if program launched with "-l" or if read-only
srs5694e7b4ff92009-08-18 13:16:10 -040067 int mainCrcOk;
68 int secondCrcOk;
69 int mainPartsCrcOk;
70 int secondPartsCrcOk;
srs5694221e0872009-08-29 15:00:31 -040071 int apmFound; // set to 1 if APM detected
72 int bsdFound; // set to 1 if BSD disklabel detected in MBR
srs56941d1448a2009-12-31 21:20:19 -050073 int sectorAlignment; // Start & end partitions at multiples of sectorAlignment
srs5694ba00fed2010-01-12 18:18:36 -050074 int beQuiet;
75 WhichToUse whichWasUsed;
srs5694cb76c672010-02-11 22:22:22 -050076
77 int LoadHeader(struct GPTHeader *header, DiskIO & disk, uint64_t sector, int *crcOk);
78 int LoadPartitionTable(const struct GPTHeader & header, DiskIO & disk, uint64_t sector = 0);
79 int CheckTable(struct GPTHeader *header);
80 int SaveHeader(struct GPTHeader *header, DiskIO & disk, uint64_t sector);
81 int SavePartitionTable(DiskIO & disk, uint64_t sector);
srs5694e7b4ff92009-08-18 13:16:10 -040082public:
srs5694e4ac11e2009-08-31 10:13:04 -040083 // Basic necessary functions....
srs5694e7b4ff92009-08-18 13:16:10 -040084 GPTData(void);
srs5694fed16d02010-01-27 23:03:40 -050085 GPTData(string deviceFilename);
srs5694e7b4ff92009-08-18 13:16:10 -040086 ~GPTData(void);
srs5694e4ac11e2009-08-31 10:13:04 -040087
88 // Verify (or update) data integrity
89 int Verify(void);
srs5694e7b4ff92009-08-18 13:16:10 -040090 int CheckGPTSize(void);
srs5694e4ac11e2009-08-31 10:13:04 -040091 int CheckHeaderValidity(void);
92 int CheckHeaderCRC(struct GPTHeader* header);
93 void RecomputeCRCs(void);
94 void RebuildMainHeader(void);
95 void RebuildSecondHeader(void);
96 int FindHybridMismatches(void);
97 int FindOverlaps(void);
98
99 // Load or save data from/to disk
srs56940a697312010-01-28 21:10:52 -0500100 int LoadMBR(const string & f) {return protectiveMBR.ReadMBRData(f);}
srs5694546a9c72010-01-26 16:00:26 -0500101 void PartitionScan(void);
srs56940a697312010-01-28 21:10:52 -0500102 int LoadPartitions(const string & deviceFilename);
srs5694546a9c72010-01-26 16:00:26 -0500103 int ForceLoadGPTData(void);
srs5694e7b4ff92009-08-18 13:16:10 -0400104 int LoadMainTable(void);
srs5694546a9c72010-01-26 16:00:26 -0500105 int LoadSecondTableAsMain(void);
srs5694ba00fed2010-01-12 18:18:36 -0500106 int SaveGPTData(int quiet = 0);
srs56940a697312010-01-28 21:10:52 -0500107 int SaveGPTBackup(const string & filename);
108 int LoadGPTBackup(const string & filename);
srs5694e4ac11e2009-08-31 10:13:04 -0400109
110 // Display data....
111 void ShowAPMState(void);
112 void ShowGPTState(void);
srs5694e7b4ff92009-08-18 13:16:10 -0400113 void DisplayGPTData(void);
114 void DisplayMBRData(void) {protectiveMBR.DisplayMBRData();}
115 void ShowDetails(void);
116 void ShowPartDetails(uint32_t partNum);
srs5694e4ac11e2009-08-31 10:13:04 -0400117
118 // Request information from the user (& possibly do something with it)
119 uint32_t GetPartNum(void);
120 void ResizePartitionTable(void);
srs5694e7b4ff92009-08-18 13:16:10 -0400121 void CreatePartition(void);
122 void DeletePartition(void);
srs5694e4ac11e2009-08-31 10:13:04 -0400123 void ChangePartType(void);
124 void SetAttributes(uint32_t partNum);
srs5694978041c2009-09-21 20:51:47 -0400125 int DestroyGPT(int prompt = 1); // Returns 1 if user proceeds
srs5694e4ac11e2009-08-31 10:13:04 -0400126
srs5694978041c2009-09-21 20:51:47 -0400127 // Convert between GPT and other formats (may require user interaction)
srs5694e4ac11e2009-08-31 10:13:04 -0400128 WhichToUse UseWhichPartitions(void);
srs5694221e0872009-08-29 15:00:31 -0400129 int XFormPartitions(void);
130 int XFormDisklabel(int OnGptPart = -1);
srs5694e321d442010-01-29 17:44:04 -0500131 int XFormDisklabel(BSDData* disklabel, uint32_t startPart);
srs5694978041c2009-09-21 20:51:47 -0400132 int OnePartToMBR(uint32_t gptPart, int mbrPart); // add one partition to MBR. Returns 1 if successful
133 int XFormToMBR(void); // convert GPT to MBR, wiping GPT afterwards. Returns 1 if successful
srs5694e4ac11e2009-08-31 10:13:04 -0400134 void MakeHybrid(void);
135
136 // Adjust GPT structures WITHOUT user interaction...
137 int SetGPTSize(uint32_t numEntries);
138 void BlankPartitions(void);
srs5694ba00fed2010-01-12 18:18:36 -0500139 int DeletePartition(uint32_t partNum);
srs5694e321d442010-01-29 17:44:04 -0500140 uint32_t CreatePartition(uint32_t partNum, uint64_t startSector, uint64_t endSector);
srs5694e7b4ff92009-08-18 13:16:10 -0400141 void SortGPT(void);
142 int ClearGPTData(void);
srs5694247657a2009-11-26 18:36:12 -0500143 void MoveSecondHeaderToEnd();
srs56940a697312010-01-28 21:10:52 -0500144 int SetName(uint32_t partNum, const string & theName = "");
srs5694e7b4ff92009-08-18 13:16:10 -0400145 void SetDiskGUID(GUIDData newGUID);
146 int SetPartitionGUID(uint32_t pn, GUIDData theGUID);
srs5694ba00fed2010-01-12 18:18:36 -0500147 int ChangePartType(uint32_t pn, uint16_t hexCode);
srs5694e4ac11e2009-08-31 10:13:04 -0400148 void MakeProtectiveMBR(void) {protectiveMBR.MakeProtectiveMBR();}
srs56941d1448a2009-12-31 21:20:19 -0500149 int Align(uint64_t* sector);
srs5694e7b4ff92009-08-18 13:16:10 -0400150
151 // Return data about the GPT structures....
srs5694e4ac11e2009-08-31 10:13:04 -0400152 int GetPartRange(uint32_t* low, uint32_t* high);
srs5694e7b4ff92009-08-18 13:16:10 -0400153 uint32_t GetNumParts(void) {return mainHeader.numParts;}
154 uint64_t GetMainHeaderLBA(void) {return mainHeader.currentLBA;}
155 uint64_t GetSecondHeaderLBA(void) {return secondHeader.currentLBA;}
156 uint64_t GetMainPartsLBA(void) {return mainHeader.partitionEntriesLBA;}
157 uint64_t GetSecondPartsLBA(void) {return secondHeader.partitionEntriesLBA;}
srs5694978041c2009-09-21 20:51:47 -0400158 uint32_t CountParts(void);
srs5694e4ac11e2009-08-31 10:13:04 -0400159
160 // Find information about free space
161 uint64_t FindFirstAvailable(uint64_t start = 0);
162 uint64_t FindFirstInLargest(void);
srs5694cb76c672010-02-11 22:22:22 -0500163 uint64_t FindLastAvailable();
srs5694e4ac11e2009-08-31 10:13:04 -0400164 uint64_t FindLastInFree(uint64_t start);
srs5694e321d442010-01-29 17:44:04 -0500165 uint64_t FindFreeBlocks(uint32_t *numSegments, uint64_t *largestSegment);
srs5694e4ac11e2009-08-31 10:13:04 -0400166 int IsFree(uint64_t sector);
srs5694ba00fed2010-01-12 18:18:36 -0500167 int IsFreePartNum(uint32_t partNum);
168
169 // Change how functions work, or return information on same
170 void SetAlignment(int n) {sectorAlignment = n;}
171 int GetAlignment(void) {return sectorAlignment;}
172 void JustLooking(int i = 1) {justLooking = i;}
173 void BeQuiet(int i = 1) {beQuiet = i;}
174 WhichToUse WhichWasUsed(void) {return whichWasUsed;}
srs5694e4ac11e2009-08-31 10:13:04 -0400175
176 // Endianness functions
srs56940a697312010-01-28 21:10:52 -0500177 void ReverseHeaderBytes(struct GPTHeader* header);
srs5694e4ac11e2009-08-31 10:13:04 -0400178 void ReversePartitionBytes(); // for endianness
srs5694e7b4ff92009-08-18 13:16:10 -0400179}; // class GPTData
180
181// Function prototypes....
srs5694e7b4ff92009-08-18 13:16:10 -0400182int SizesOK(void);
183
184#endif