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