blob: 3fa1f5dda7b966f61f3fdd6a720866c22fa73af9 [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
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;
46 struct GUIDData diskGUID;
47 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;
61 char device[256]; // device filename
62 uint32_t blockSize; // device block size
63 uint64_t diskSize; // size of device, in blocks
64 GPTValidity state; // is GPT valid?
srs56945d58fe02010-01-03 20:57:08 -050065 int justLooking; // Set to 1 if program launched with "-l" or if read-only
srs5694e7b4ff92009-08-18 13:16:10 -040066 int mainCrcOk;
67 int secondCrcOk;
68 int mainPartsCrcOk;
69 int secondPartsCrcOk;
srs5694221e0872009-08-29 15:00:31 -040070 int apmFound; // set to 1 if APM detected
71 int bsdFound; // set to 1 if BSD disklabel detected in MBR
srs56941d1448a2009-12-31 21:20:19 -050072 int sectorAlignment; // Start & end partitions at multiples of sectorAlignment
srs5694e7b4ff92009-08-18 13:16:10 -040073 PartTypes typeHelper;
srs5694ba00fed2010-01-12 18:18:36 -050074 int beQuiet;
75 WhichToUse whichWasUsed;
srs5694e7b4ff92009-08-18 13:16:10 -040076public:
srs5694e4ac11e2009-08-31 10:13:04 -040077 // Basic necessary functions....
srs5694e7b4ff92009-08-18 13:16:10 -040078 GPTData(void);
79 GPTData(char* deviceFilename);
80 ~GPTData(void);
srs5694e4ac11e2009-08-31 10:13:04 -040081
82 // Verify (or update) data integrity
83 int Verify(void);
srs5694e7b4ff92009-08-18 13:16:10 -040084 int CheckGPTSize(void);
srs5694e4ac11e2009-08-31 10:13:04 -040085 int CheckHeaderValidity(void);
86 int CheckHeaderCRC(struct GPTHeader* header);
87 void RecomputeCRCs(void);
88 void RebuildMainHeader(void);
89 void RebuildSecondHeader(void);
90 int FindHybridMismatches(void);
91 int FindOverlaps(void);
92
93 // Load or save data from/to disk
srs5694978041c2009-09-21 20:51:47 -040094 int LoadMBR(char* f) {return protectiveMBR.ReadMBRData(f);}
srs5694221e0872009-08-29 15:00:31 -040095 void PartitionScan(int fd);
srs5694e7b4ff92009-08-18 13:16:10 -040096 int LoadPartitions(char* deviceFilename);
97 int ForceLoadGPTData(int fd);
98 int LoadMainTable(void);
srs5694e4ac11e2009-08-31 10:13:04 -040099 void LoadSecondTableAsMain(void);
srs5694ba00fed2010-01-12 18:18:36 -0500100 int SaveGPTData(int quiet = 0);
srs5694e4ac11e2009-08-31 10:13:04 -0400101 int SaveGPTBackup(char* filename);
102 int LoadGPTBackup(char* filename);
103
104 // Display data....
105 void ShowAPMState(void);
106 void ShowGPTState(void);
srs5694e7b4ff92009-08-18 13:16:10 -0400107 void DisplayGPTData(void);
108 void DisplayMBRData(void) {protectiveMBR.DisplayMBRData();}
109 void ShowDetails(void);
110 void ShowPartDetails(uint32_t partNum);
srs5694e4ac11e2009-08-31 10:13:04 -0400111
112 // Request information from the user (& possibly do something with it)
113 uint32_t GetPartNum(void);
114 void ResizePartitionTable(void);
srs5694e7b4ff92009-08-18 13:16:10 -0400115 void CreatePartition(void);
116 void DeletePartition(void);
srs5694e4ac11e2009-08-31 10:13:04 -0400117 void ChangePartType(void);
118 void SetAttributes(uint32_t partNum);
srs5694978041c2009-09-21 20:51:47 -0400119 int DestroyGPT(int prompt = 1); // Returns 1 if user proceeds
srs5694e4ac11e2009-08-31 10:13:04 -0400120
srs5694978041c2009-09-21 20:51:47 -0400121 // Convert between GPT and other formats (may require user interaction)
srs5694e4ac11e2009-08-31 10:13:04 -0400122 WhichToUse UseWhichPartitions(void);
srs5694221e0872009-08-29 15:00:31 -0400123 int XFormPartitions(void);
124 int XFormDisklabel(int OnGptPart = -1);
125 int XFormDisklabel(BSDData* disklabel, int startPart);
srs5694978041c2009-09-21 20:51:47 -0400126 int OnePartToMBR(uint32_t gptPart, int mbrPart); // add one partition to MBR. Returns 1 if successful
127 int XFormToMBR(void); // convert GPT to MBR, wiping GPT afterwards. Returns 1 if successful
srs5694e4ac11e2009-08-31 10:13:04 -0400128 void MakeHybrid(void);
129
130 // Adjust GPT structures WITHOUT user interaction...
131 int SetGPTSize(uint32_t numEntries);
132 void BlankPartitions(void);
srs5694ba00fed2010-01-12 18:18:36 -0500133 int DeletePartition(uint32_t partNum);
134 int CreatePartition(uint32_t partNum, uint64_t startSector, uint64_t endSector);
srs5694e7b4ff92009-08-18 13:16:10 -0400135 void SortGPT(void);
136 int ClearGPTData(void);
srs5694247657a2009-11-26 18:36:12 -0500137 void MoveSecondHeaderToEnd();
srs5694ba00fed2010-01-12 18:18:36 -0500138 int SetName(uint32_t partNum, char* theName = NULL);
srs5694e7b4ff92009-08-18 13:16:10 -0400139 void SetDiskGUID(GUIDData newGUID);
140 int SetPartitionGUID(uint32_t pn, GUIDData theGUID);
srs5694ba00fed2010-01-12 18:18:36 -0500141 int ChangePartType(uint32_t pn, uint16_t hexCode);
srs5694e4ac11e2009-08-31 10:13:04 -0400142 void MakeProtectiveMBR(void) {protectiveMBR.MakeProtectiveMBR();}
srs56941d1448a2009-12-31 21:20:19 -0500143 int Align(uint64_t* sector);
srs5694e7b4ff92009-08-18 13:16:10 -0400144
145 // Return data about the GPT structures....
srs5694e4ac11e2009-08-31 10:13:04 -0400146 int GetPartRange(uint32_t* low, uint32_t* high);
srs5694e7b4ff92009-08-18 13:16:10 -0400147 uint32_t GetNumParts(void) {return mainHeader.numParts;}
148 uint64_t GetMainHeaderLBA(void) {return mainHeader.currentLBA;}
149 uint64_t GetSecondHeaderLBA(void) {return secondHeader.currentLBA;}
150 uint64_t GetMainPartsLBA(void) {return mainHeader.partitionEntriesLBA;}
151 uint64_t GetSecondPartsLBA(void) {return secondHeader.partitionEntriesLBA;}
srs5694978041c2009-09-21 20:51:47 -0400152 uint32_t CountParts(void);
srs5694e4ac11e2009-08-31 10:13:04 -0400153
154 // Find information about free space
155 uint64_t FindFirstAvailable(uint64_t start = 0);
156 uint64_t FindFirstInLargest(void);
157 uint64_t FindLastAvailable(uint64_t start);
158 uint64_t FindLastInFree(uint64_t start);
159 uint64_t FindFreeBlocks(int *numSegments, uint64_t *largestSegment);
160 int IsFree(uint64_t sector);
srs5694ba00fed2010-01-12 18:18:36 -0500161 int IsFreePartNum(uint32_t partNum);
162
163 // Change how functions work, or return information on same
164 void SetAlignment(int n) {sectorAlignment = n;}
165 int GetAlignment(void) {return sectorAlignment;}
166 void JustLooking(int i = 1) {justLooking = i;}
167 void BeQuiet(int i = 1) {beQuiet = i;}
168 WhichToUse WhichWasUsed(void) {return whichWasUsed;}
srs5694e4ac11e2009-08-31 10:13:04 -0400169
170 // Endianness functions
171 void ReverseHeaderBytes(struct GPTHeader* header); // for endianness
172 void ReversePartitionBytes(); // for endianness
srs5694e7b4ff92009-08-18 13:16:10 -0400173}; // class GPTData
174
175// Function prototypes....
srs5694e7b4ff92009-08-18 13:16:10 -0400176int SizesOK(void);
177
178#endif