blob: 4c2904eea3eb1106096fce3813305240ab3f3a63 [file] [log] [blame]
srs5694e7b4ff92009-08-18 13:16:10 -04001/* gpt.h -- GPT and data structure definitions, types, and
2 functions */
3
4#include <stdint.h>
5#include <sys/types.h>
6#include <sys/ioctl.h>
7#include "support.h"
8#include "parttypes.h"
9#include "mbr.h"
10
11#ifndef __GPTSTRUCTS
12#define __GPTSTRUCTS
13
14#define GPT_SIGNATURE UINT64_C(0x5452415020494645)
15
16/* Number and size of GPT entries... */
17#define NUM_GPT_ENTRIES 128
18#define GPT_SIZE 128
19/* Offset, in 512-byte sectors, for GPT table and partition data.
20 Note this is above two multiplied together, divided by 512, with 2
21 added
22#define GPT_OFFSET (((NUM_GPT_ENTRIES * GPT_SIZE) / SECTOR_SIZE) + 2)
23*/
24
25#define HEADER_SIZE 92
26
27#define GPT_RESERVED 420
28#define NAME_SIZE 72
29
30using namespace std;
31
32/****************************************
33 * *
34 * GPTData class and related structures *
35 * *
36 ****************************************/
37
38// Validity state of GPT data
39enum GPTValidity {gpt_valid, gpt_corrupt, gpt_invalid};
40
41// Which set of partition data to use
42enum WhichToUse {use_gpt, use_mbr, use_new};
43
44// Header (first 512 bytes) of GPT table
45struct GPTHeader {
46 uint64_t signature;
47 uint32_t revision;
48 uint32_t headerSize;
49 uint32_t headerCRC;
50 uint32_t reserved;
51 uint64_t currentLBA;
52 uint64_t backupLBA;
53 uint64_t firstUsableLBA;
54 uint64_t lastUsableLBA;
55 struct GUIDData diskGUID;
56 uint64_t partitionEntriesLBA;
57 uint32_t numParts;
58 uint32_t sizeOfPartitionEntries;
59 uint32_t partitionEntriesCRC;
60 unsigned char reserved2[GPT_RESERVED];
61}; // struct GPTHeader
62
63struct GPTPartition {
64 struct GUIDData partitionType;
65 struct GUIDData uniqueGUID;
66 uint64_t firstLBA;
67 uint64_t lastLBA;
68 uint64_t attributes;
69 unsigned char name[NAME_SIZE];
70}; // struct GPTPartition
71
72// Data in GPT format
73class GPTData {
74protected:
75 struct GPTHeader mainHeader;
76 struct GPTPartition *partitions;
77 struct GPTHeader secondHeader;
78 MBRData protectiveMBR;
79 char device[256]; // device filename
80 uint32_t blockSize; // device block size
81 uint64_t diskSize; // size of device, in blocks
82 GPTValidity state; // is GPT valid?
83 int mainCrcOk;
84 int secondCrcOk;
85 int mainPartsCrcOk;
86 int secondPartsCrcOk;
87// uint32_t units; // display units, in multiples of sectors
88 PartTypes typeHelper;
89public:
90 GPTData(void);
91 GPTData(char* deviceFilename);
92 ~GPTData(void);
93 int SetGPTSize(uint32_t numEntries);
94 int CheckGPTSize(void);
95 int LoadPartitions(char* deviceFilename);
96 int ForceLoadGPTData(int fd);
97 int LoadMainTable(void);
98 WhichToUse UseWhichPartitions(void);
99 void ResizePartitionTable(void);
100 int GetPartRange(uint32_t* low, uint32_t* high);
101 void DisplayGPTData(void);
102 void DisplayMBRData(void) {protectiveMBR.DisplayMBRData();}
103 void ShowDetails(void);
104 void ShowPartDetails(uint32_t partNum);
105 void CreatePartition(void);
106 void DeletePartition(void);
107 void BlankPartitions(void);
108 uint64_t FindFirstAvailable(uint64_t start = 0);
109 uint64_t FindLastAvailable(uint64_t start);
110 uint64_t FindLastInFree(uint64_t start);
111 int IsFree(uint64_t sector);
112 int XFormPartitions(MBRData* origParts);
113 void SortGPT(void);
114 int ClearGPTData(void);
115 void ChangePartType(void);
116 uint32_t GetPartNum(void);
117 void SetAttributes(uint32_t partNum);
118 void SetName(uint32_t partNum, char* theName = NULL);
119 void SetDiskGUID(GUIDData newGUID);
120 int SetPartitionGUID(uint32_t pn, GUIDData theGUID);
121 int CheckHeaderValidity(void);
122 int CheckHeaderCRC(struct GPTHeader* header);
123 void RecomputeCRCs(void);
124 int Verify(void);
125 void RebuildMainHeader(void);
126 void RebuildSecondHeader(void);
127 void LoadSecondTableAsMain(void);
128 uint64_t FindFreeBlocks(int *numSegments, uint64_t *largestSegment);
srs5694c0ca8f82009-08-20 21:35:25 -0400129 void MakeHybrid(void);
130 void MakeProtectiveMBR(void);
srs5694e7b4ff92009-08-18 13:16:10 -0400131 int SaveGPTData(void);
132 int SaveGPTBackup(char* filename);
133 int LoadGPTBackup(char* filename);
srs5694c0ca8f82009-08-20 21:35:25 -0400134 int DestroyGPT(void); // Returns 1 if user proceeds
srs5694e7b4ff92009-08-18 13:16:10 -0400135
136 // Return data about the GPT structures....
137 uint32_t GetNumParts(void) {return mainHeader.numParts;}
138 uint64_t GetMainHeaderLBA(void) {return mainHeader.currentLBA;}
139 uint64_t GetSecondHeaderLBA(void) {return secondHeader.currentLBA;}
140 uint64_t GetMainPartsLBA(void) {return mainHeader.partitionEntriesLBA;}
141 uint64_t GetSecondPartsLBA(void) {return secondHeader.partitionEntriesLBA;}
142 uint64_t GetBlocksInPartTable(void) {return (mainHeader.numParts *
143 mainHeader.sizeOfPartitionEntries) / blockSize;}
144}; // class GPTData
145
146// Function prototypes....
147void BlankPartition(struct GPTPartition* partition);
148//int XFormType(uint8_t oldType, struct GUIDData* newType, int partNum);
149void QuickSortGPT(struct GPTPartition* partitions, int start, int finish);
150int TheyOverlap(struct GPTPartition* first, struct GPTPartition* second);
151void ChangeGPTType(struct GPTPartition* part);
152int SizesOK(void);
153
154#endif