blob: 2f405d5ec113c2c7adadebca6249c6831b9145dc [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?
64 int mainCrcOk;
65 int secondCrcOk;
66 int mainPartsCrcOk;
67 int secondPartsCrcOk;
srs5694221e0872009-08-29 15:00:31 -040068 int apmFound; // set to 1 if APM detected
69 int bsdFound; // set to 1 if BSD disklabel detected in MBR
srs5694e7b4ff92009-08-18 13:16:10 -040070// uint32_t units; // display units, in multiples of sectors
71 PartTypes typeHelper;
72public:
73 GPTData(void);
74 GPTData(char* deviceFilename);
75 ~GPTData(void);
76 int SetGPTSize(uint32_t numEntries);
77 int CheckGPTSize(void);
srs5694221e0872009-08-29 15:00:31 -040078 void ShowAPMState(void);
79 void ShowGPTState(void);
80 void PartitionScan(int fd);
srs5694e7b4ff92009-08-18 13:16:10 -040081 int LoadPartitions(char* deviceFilename);
82 int ForceLoadGPTData(int fd);
83 int LoadMainTable(void);
84 WhichToUse UseWhichPartitions(void);
85 void ResizePartitionTable(void);
86 int GetPartRange(uint32_t* low, uint32_t* high);
87 void DisplayGPTData(void);
88 void DisplayMBRData(void) {protectiveMBR.DisplayMBRData();}
89 void ShowDetails(void);
90 void ShowPartDetails(uint32_t partNum);
91 void CreatePartition(void);
92 void DeletePartition(void);
93 void BlankPartitions(void);
94 uint64_t FindFirstAvailable(uint64_t start = 0);
95 uint64_t FindLastAvailable(uint64_t start);
96 uint64_t FindLastInFree(uint64_t start);
97 int IsFree(uint64_t sector);
srs5694221e0872009-08-29 15:00:31 -040098 int XFormPartitions(void);
99 int XFormDisklabel(int OnGptPart = -1);
100 int XFormDisklabel(BSDData* disklabel, int startPart);
srs5694e7b4ff92009-08-18 13:16:10 -0400101 void SortGPT(void);
102 int ClearGPTData(void);
103 void ChangePartType(void);
104 uint32_t GetPartNum(void);
105 void SetAttributes(uint32_t partNum);
106 void SetName(uint32_t partNum, char* theName = NULL);
107 void SetDiskGUID(GUIDData newGUID);
108 int SetPartitionGUID(uint32_t pn, GUIDData theGUID);
109 int CheckHeaderValidity(void);
110 int CheckHeaderCRC(struct GPTHeader* header);
111 void RecomputeCRCs(void);
112 int Verify(void);
113 void RebuildMainHeader(void);
114 void RebuildSecondHeader(void);
115 void LoadSecondTableAsMain(void);
116 uint64_t FindFreeBlocks(int *numSegments, uint64_t *largestSegment);
srs5694c0ca8f82009-08-20 21:35:25 -0400117 void MakeHybrid(void);
118 void MakeProtectiveMBR(void);
srs5694e7b4ff92009-08-18 13:16:10 -0400119 int SaveGPTData(void);
120 int SaveGPTBackup(char* filename);
121 int LoadGPTBackup(char* filename);
srs5694c0ca8f82009-08-20 21:35:25 -0400122 int DestroyGPT(void); // Returns 1 if user proceeds
srs5694221e0872009-08-29 15:00:31 -0400123
124 // Endianness functions
srs56942a9f5da2009-08-26 00:48:01 -0400125 void ReverseHeaderBytes(struct GPTHeader* header); // for endianness
126 void ReversePartitionBytes(); // for endianness
srs5694e7b4ff92009-08-18 13:16:10 -0400127
128 // Return data about the GPT structures....
129 uint32_t GetNumParts(void) {return mainHeader.numParts;}
130 uint64_t GetMainHeaderLBA(void) {return mainHeader.currentLBA;}
131 uint64_t GetSecondHeaderLBA(void) {return secondHeader.currentLBA;}
132 uint64_t GetMainPartsLBA(void) {return mainHeader.partitionEntriesLBA;}
133 uint64_t GetSecondPartsLBA(void) {return secondHeader.partitionEntriesLBA;}
134 uint64_t GetBlocksInPartTable(void) {return (mainHeader.numParts *
135 mainHeader.sizeOfPartitionEntries) / blockSize;}
136}; // class GPTData
137
138// Function prototypes....
139void BlankPartition(struct GPTPartition* partition);
srs5694e7b4ff92009-08-18 13:16:10 -0400140int TheyOverlap(struct GPTPartition* first, struct GPTPartition* second);
141void ChangeGPTType(struct GPTPartition* part);
142int SizesOK(void);
143
144#endif