blob: 68baf1e65f3b409ca088616eaaaaf2859b59f1db [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>
9#include <sys/ioctl.h>
10#include "support.h"
11#include "parttypes.h"
12#include "mbr.h"
srs5694221e0872009-08-29 15:00:31 -040013#include "bsd.h"
14#include "gptpart.h"
srs5694e7b4ff92009-08-18 13:16:10 -040015
16#ifndef __GPTSTRUCTS
17#define __GPTSTRUCTS
18
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
srs5694221e0872009-08-29 15:00:31 -040032enum WhichToUse {use_gpt, use_mbr, use_bsd, use_new};
srs5694e7b4ff92009-08-18 13:16:10 -040033
34// Header (first 512 bytes) of GPT table
35struct GPTHeader {
36 uint64_t signature;
37 uint32_t revision;
38 uint32_t headerSize;
39 uint32_t headerCRC;
40 uint32_t reserved;
41 uint64_t currentLBA;
42 uint64_t backupLBA;
43 uint64_t firstUsableLBA;
44 uint64_t lastUsableLBA;
45 struct GUIDData diskGUID;
46 uint64_t partitionEntriesLBA;
47 uint32_t numParts;
48 uint32_t sizeOfPartitionEntries;
49 uint32_t partitionEntriesCRC;
50 unsigned char reserved2[GPT_RESERVED];
51}; // struct GPTHeader
52
srs5694e7b4ff92009-08-18 13:16:10 -040053// Data in GPT format
54class GPTData {
55protected:
56 struct GPTHeader mainHeader;
srs5694221e0872009-08-29 15:00:31 -040057 struct GPTPart *partitions;
srs5694e7b4ff92009-08-18 13:16:10 -040058 struct GPTHeader secondHeader;
59 MBRData protectiveMBR;
60 char device[256]; // device filename
61 uint32_t blockSize; // device block size
62 uint64_t diskSize; // size of device, in blocks
63 GPTValidity state; // is GPT valid?
srs56945d58fe02010-01-03 20:57:08 -050064 int justLooking; // Set to 1 if program launched with "-l" or if read-only
srs5694e7b4ff92009-08-18 13:16:10 -040065 int mainCrcOk;
66 int secondCrcOk;
67 int mainPartsCrcOk;
68 int secondPartsCrcOk;
srs5694221e0872009-08-29 15:00:31 -040069 int apmFound; // set to 1 if APM detected
70 int bsdFound; // set to 1 if BSD disklabel detected in MBR
srs56941d1448a2009-12-31 21:20:19 -050071 int sectorAlignment; // Start & end partitions at multiples of sectorAlignment
srs5694e7b4ff92009-08-18 13:16:10 -040072 PartTypes typeHelper;
73public:
srs5694e4ac11e2009-08-31 10:13:04 -040074 // Basic necessary functions....
srs5694e7b4ff92009-08-18 13:16:10 -040075 GPTData(void);
76 GPTData(char* deviceFilename);
77 ~GPTData(void);
srs5694e4ac11e2009-08-31 10:13:04 -040078
79 // Verify (or update) data integrity
80 int Verify(void);
srs5694e7b4ff92009-08-18 13:16:10 -040081 int CheckGPTSize(void);
srs5694e4ac11e2009-08-31 10:13:04 -040082 int CheckHeaderValidity(void);
83 int CheckHeaderCRC(struct GPTHeader* header);
84 void RecomputeCRCs(void);
85 void RebuildMainHeader(void);
86 void RebuildSecondHeader(void);
87 int FindHybridMismatches(void);
88 int FindOverlaps(void);
89
90 // Load or save data from/to disk
srs5694978041c2009-09-21 20:51:47 -040091 int LoadMBR(char* f) {return protectiveMBR.ReadMBRData(f);}
srs5694221e0872009-08-29 15:00:31 -040092 void PartitionScan(int fd);
srs5694e7b4ff92009-08-18 13:16:10 -040093 int LoadPartitions(char* deviceFilename);
94 int ForceLoadGPTData(int fd);
95 int LoadMainTable(void);
srs5694e4ac11e2009-08-31 10:13:04 -040096 void LoadSecondTableAsMain(void);
97 int SaveGPTData(void);
98 int SaveGPTBackup(char* filename);
99 int LoadGPTBackup(char* filename);
100
101 // Display data....
102 void ShowAPMState(void);
103 void ShowGPTState(void);
srs5694e7b4ff92009-08-18 13:16:10 -0400104 void DisplayGPTData(void);
105 void DisplayMBRData(void) {protectiveMBR.DisplayMBRData();}
106 void ShowDetails(void);
107 void ShowPartDetails(uint32_t partNum);
srs5694e4ac11e2009-08-31 10:13:04 -0400108
109 // Request information from the user (& possibly do something with it)
110 uint32_t GetPartNum(void);
111 void ResizePartitionTable(void);
srs5694e7b4ff92009-08-18 13:16:10 -0400112 void CreatePartition(void);
113 void DeletePartition(void);
srs5694e4ac11e2009-08-31 10:13:04 -0400114 void ChangePartType(void);
115 void SetAttributes(uint32_t partNum);
srs5694978041c2009-09-21 20:51:47 -0400116 int DestroyGPT(int prompt = 1); // Returns 1 if user proceeds
srs5694e4ac11e2009-08-31 10:13:04 -0400117
srs5694978041c2009-09-21 20:51:47 -0400118 // Convert between GPT and other formats (may require user interaction)
srs5694e4ac11e2009-08-31 10:13:04 -0400119 WhichToUse UseWhichPartitions(void);
srs5694221e0872009-08-29 15:00:31 -0400120 int XFormPartitions(void);
121 int XFormDisklabel(int OnGptPart = -1);
122 int XFormDisklabel(BSDData* disklabel, int startPart);
srs5694978041c2009-09-21 20:51:47 -0400123 int OnePartToMBR(uint32_t gptPart, int mbrPart); // add one partition to MBR. Returns 1 if successful
124 int XFormToMBR(void); // convert GPT to MBR, wiping GPT afterwards. Returns 1 if successful
srs5694e4ac11e2009-08-31 10:13:04 -0400125 void MakeHybrid(void);
126
127 // Adjust GPT structures WITHOUT user interaction...
128 int SetGPTSize(uint32_t numEntries);
129 void BlankPartitions(void);
srs5694e7b4ff92009-08-18 13:16:10 -0400130 void SortGPT(void);
131 int ClearGPTData(void);
srs5694247657a2009-11-26 18:36:12 -0500132 void MoveSecondHeaderToEnd();
srs5694e7b4ff92009-08-18 13:16:10 -0400133 void SetName(uint32_t partNum, char* theName = NULL);
134 void SetDiskGUID(GUIDData newGUID);
135 int SetPartitionGUID(uint32_t pn, GUIDData theGUID);
srs5694e4ac11e2009-08-31 10:13:04 -0400136 void MakeProtectiveMBR(void) {protectiveMBR.MakeProtectiveMBR();}
srs56941d1448a2009-12-31 21:20:19 -0500137 int Align(uint64_t* sector);
138 void SetAlignment(int n) {sectorAlignment = n;}
srs56945d58fe02010-01-03 20:57:08 -0500139 void JustLooking(int i = 1) {justLooking = i;}
srs5694e7b4ff92009-08-18 13:16:10 -0400140
141 // Return data about the GPT structures....
srs5694e4ac11e2009-08-31 10:13:04 -0400142 int GetPartRange(uint32_t* low, uint32_t* high);
srs5694e7b4ff92009-08-18 13:16:10 -0400143 uint32_t GetNumParts(void) {return mainHeader.numParts;}
144 uint64_t GetMainHeaderLBA(void) {return mainHeader.currentLBA;}
145 uint64_t GetSecondHeaderLBA(void) {return secondHeader.currentLBA;}
146 uint64_t GetMainPartsLBA(void) {return mainHeader.partitionEntriesLBA;}
147 uint64_t GetSecondPartsLBA(void) {return secondHeader.partitionEntriesLBA;}
srs5694978041c2009-09-21 20:51:47 -0400148 uint32_t CountParts(void);
srs56941d1448a2009-12-31 21:20:19 -0500149 int GetAlignment(void) {return sectorAlignment;}
srs5694e4ac11e2009-08-31 10:13:04 -0400150
151 // Find information about free space
152 uint64_t FindFirstAvailable(uint64_t start = 0);
153 uint64_t FindFirstInLargest(void);
154 uint64_t FindLastAvailable(uint64_t start);
155 uint64_t FindLastInFree(uint64_t start);
156 uint64_t FindFreeBlocks(int *numSegments, uint64_t *largestSegment);
157 int IsFree(uint64_t sector);
158
159 // Endianness functions
160 void ReverseHeaderBytes(struct GPTHeader* header); // for endianness
161 void ReversePartitionBytes(); // for endianness
srs5694e7b4ff92009-08-18 13:16:10 -0400162}; // class GPTData
163
164// Function prototypes....
srs5694e7b4ff92009-08-18 13:16:10 -0400165int SizesOK(void);
166
167#endif