blob: 2dab928014b4fdb224a747fc6db9e013ad16e6b4 [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
srs56943c0af382010-01-15 19:19:18 -050019#define GPTFDISK_VERSION "0.6.0"
srs5694e7b4ff92009-08-18 13:16:10 -040020
21using namespace std;
22
23/****************************************
24 * *
25 * GPTData class and related structures *
26 * *
27 ****************************************/
28
29// Validity state of GPT data
30enum GPTValidity {gpt_valid, gpt_corrupt, gpt_invalid};
31
32// Which set of partition data to use
srs56943c0af382010-01-15 19:19:18 -050033enum WhichToUse {use_gpt, use_mbr, use_bsd, use_new, use_abort};
srs5694e7b4ff92009-08-18 13:16:10 -040034
35// Header (first 512 bytes) of GPT table
srs5694ba00fed2010-01-12 18:18:36 -050036#pragma pack(1)
srs5694e7b4ff92009-08-18 13:16:10 -040037struct GPTHeader {
38 uint64_t signature;
39 uint32_t revision;
40 uint32_t headerSize;
41 uint32_t headerCRC;
42 uint32_t reserved;
43 uint64_t currentLBA;
44 uint64_t backupLBA;
45 uint64_t firstUsableLBA;
46 uint64_t lastUsableLBA;
47 struct GUIDData diskGUID;
48 uint64_t partitionEntriesLBA;
49 uint32_t numParts;
50 uint32_t sizeOfPartitionEntries;
51 uint32_t partitionEntriesCRC;
52 unsigned char reserved2[GPT_RESERVED];
53}; // struct GPTHeader
54
srs5694e7b4ff92009-08-18 13:16:10 -040055// Data in GPT format
56class GPTData {
57protected:
58 struct GPTHeader mainHeader;
srs5694ba00fed2010-01-12 18:18:36 -050059 GPTPart *partitions;
srs5694e7b4ff92009-08-18 13:16:10 -040060 struct GPTHeader secondHeader;
61 MBRData protectiveMBR;
62 char device[256]; // device filename
63 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
srs5694e7b4ff92009-08-18 13:16:10 -040074 PartTypes typeHelper;
srs5694ba00fed2010-01-12 18:18:36 -050075 int beQuiet;
76 WhichToUse whichWasUsed;
srs5694e7b4ff92009-08-18 13:16:10 -040077public:
srs5694e4ac11e2009-08-31 10:13:04 -040078 // Basic necessary functions....
srs5694e7b4ff92009-08-18 13:16:10 -040079 GPTData(void);
80 GPTData(char* deviceFilename);
81 ~GPTData(void);
srs5694e4ac11e2009-08-31 10:13:04 -040082
83 // Verify (or update) data integrity
84 int Verify(void);
srs5694e7b4ff92009-08-18 13:16:10 -040085 int CheckGPTSize(void);
srs5694e4ac11e2009-08-31 10:13:04 -040086 int CheckHeaderValidity(void);
87 int CheckHeaderCRC(struct GPTHeader* header);
88 void RecomputeCRCs(void);
89 void RebuildMainHeader(void);
90 void RebuildSecondHeader(void);
91 int FindHybridMismatches(void);
92 int FindOverlaps(void);
93
94 // Load or save data from/to disk
srs5694978041c2009-09-21 20:51:47 -040095 int LoadMBR(char* f) {return protectiveMBR.ReadMBRData(f);}
srs5694221e0872009-08-29 15:00:31 -040096 void PartitionScan(int fd);
srs5694e7b4ff92009-08-18 13:16:10 -040097 int LoadPartitions(char* deviceFilename);
98 int ForceLoadGPTData(int fd);
99 int LoadMainTable(void);
srs5694e4ac11e2009-08-31 10:13:04 -0400100 void LoadSecondTableAsMain(void);
srs5694ba00fed2010-01-12 18:18:36 -0500101 int SaveGPTData(int quiet = 0);
srs5694e4ac11e2009-08-31 10:13:04 -0400102 int SaveGPTBackup(char* filename);
103 int LoadGPTBackup(char* filename);
104
105 // Display data....
106 void ShowAPMState(void);
107 void ShowGPTState(void);
srs5694e7b4ff92009-08-18 13:16:10 -0400108 void DisplayGPTData(void);
109 void DisplayMBRData(void) {protectiveMBR.DisplayMBRData();}
110 void ShowDetails(void);
111 void ShowPartDetails(uint32_t partNum);
srs5694e4ac11e2009-08-31 10:13:04 -0400112
113 // Request information from the user (& possibly do something with it)
114 uint32_t GetPartNum(void);
115 void ResizePartitionTable(void);
srs5694e7b4ff92009-08-18 13:16:10 -0400116 void CreatePartition(void);
117 void DeletePartition(void);
srs5694e4ac11e2009-08-31 10:13:04 -0400118 void ChangePartType(void);
119 void SetAttributes(uint32_t partNum);
srs5694978041c2009-09-21 20:51:47 -0400120 int DestroyGPT(int prompt = 1); // Returns 1 if user proceeds
srs5694e4ac11e2009-08-31 10:13:04 -0400121
srs5694978041c2009-09-21 20:51:47 -0400122 // Convert between GPT and other formats (may require user interaction)
srs5694e4ac11e2009-08-31 10:13:04 -0400123 WhichToUse UseWhichPartitions(void);
srs5694221e0872009-08-29 15:00:31 -0400124 int XFormPartitions(void);
125 int XFormDisklabel(int OnGptPart = -1);
126 int XFormDisklabel(BSDData* disklabel, int startPart);
srs5694978041c2009-09-21 20:51:47 -0400127 int OnePartToMBR(uint32_t gptPart, int mbrPart); // add one partition to MBR. Returns 1 if successful
128 int XFormToMBR(void); // convert GPT to MBR, wiping GPT afterwards. Returns 1 if successful
srs5694e4ac11e2009-08-31 10:13:04 -0400129 void MakeHybrid(void);
130
131 // Adjust GPT structures WITHOUT user interaction...
132 int SetGPTSize(uint32_t numEntries);
133 void BlankPartitions(void);
srs5694ba00fed2010-01-12 18:18:36 -0500134 int DeletePartition(uint32_t partNum);
135 int CreatePartition(uint32_t partNum, uint64_t startSector, uint64_t endSector);
srs5694e7b4ff92009-08-18 13:16:10 -0400136 void SortGPT(void);
137 int ClearGPTData(void);
srs5694247657a2009-11-26 18:36:12 -0500138 void MoveSecondHeaderToEnd();
srs5694ba00fed2010-01-12 18:18:36 -0500139 int SetName(uint32_t partNum, char* theName = NULL);
srs5694e7b4ff92009-08-18 13:16:10 -0400140 void SetDiskGUID(GUIDData newGUID);
141 int SetPartitionGUID(uint32_t pn, GUIDData theGUID);
srs5694ba00fed2010-01-12 18:18:36 -0500142 int ChangePartType(uint32_t pn, uint16_t hexCode);
srs5694e4ac11e2009-08-31 10:13:04 -0400143 void MakeProtectiveMBR(void) {protectiveMBR.MakeProtectiveMBR();}
srs56941d1448a2009-12-31 21:20:19 -0500144 int Align(uint64_t* sector);
srs5694e7b4ff92009-08-18 13:16:10 -0400145
146 // Return data about the GPT structures....
srs5694e4ac11e2009-08-31 10:13:04 -0400147 int GetPartRange(uint32_t* low, uint32_t* high);
srs5694e7b4ff92009-08-18 13:16:10 -0400148 uint32_t GetNumParts(void) {return mainHeader.numParts;}
149 uint64_t GetMainHeaderLBA(void) {return mainHeader.currentLBA;}
150 uint64_t GetSecondHeaderLBA(void) {return secondHeader.currentLBA;}
151 uint64_t GetMainPartsLBA(void) {return mainHeader.partitionEntriesLBA;}
152 uint64_t GetSecondPartsLBA(void) {return secondHeader.partitionEntriesLBA;}
srs5694978041c2009-09-21 20:51:47 -0400153 uint32_t CountParts(void);
srs5694e4ac11e2009-08-31 10:13:04 -0400154
155 // Find information about free space
156 uint64_t FindFirstAvailable(uint64_t start = 0);
157 uint64_t FindFirstInLargest(void);
158 uint64_t FindLastAvailable(uint64_t start);
159 uint64_t FindLastInFree(uint64_t start);
160 uint64_t FindFreeBlocks(int *numSegments, uint64_t *largestSegment);
161 int IsFree(uint64_t sector);
srs5694ba00fed2010-01-12 18:18:36 -0500162 int IsFreePartNum(uint32_t partNum);
163
164 // Change how functions work, or return information on same
165 void SetAlignment(int n) {sectorAlignment = n;}
166 int GetAlignment(void) {return sectorAlignment;}
167 void JustLooking(int i = 1) {justLooking = i;}
168 void BeQuiet(int i = 1) {beQuiet = i;}
169 WhichToUse WhichWasUsed(void) {return whichWasUsed;}
srs5694e4ac11e2009-08-31 10:13:04 -0400170
171 // Endianness functions
172 void ReverseHeaderBytes(struct GPTHeader* header); // for endianness
173 void ReversePartitionBytes(); // for endianness
srs5694e7b4ff92009-08-18 13:16:10 -0400174}; // class GPTData
175
176// Function prototypes....
srs5694e7b4ff92009-08-18 13:16:10 -0400177int SizesOK(void);
178
179#endif