xref: /rkdeveloptool/gpt.h (revision c30d921c26f1105311971e7cc3731cac267aae66)
1 #ifndef _GPT_H
2 #define _GPT_H
3 
4 #define MSDOS_MBR_SIGNATURE 0xAA55
5 #define EFI_PMBR_OSTYPE_EFI 0xEF
6 #define EFI_PMBR_OSTYPE_EFI_GPT 0xEE
7 
8 #define GPT_HEADER_SIGNATURE 0x5452415020494645ULL
9 #define GPT_HEADER_REVISION_V1 0x00010000
10 #define GPT_PRIMARY_PARTITION_TABLE_LBA 1ULL
11 #define GPT_ENTRY_NAME "gpt"
12 #define GPT_ENTRY_NUMBERS		128
13 #define GPT_ENTRY_SIZE			128
14 #define PART_PROPERTY_BOOTABLE (1 << 2)
15 
16 #define EFI_GUID(a,b,c,d0,d1,d2,d3,d4,d5,d6,d7) \
17 	((efi_guid_t) \
18 	{{ (a) & 0xff, ((a) >> 8) & 0xff, ((a) >> 16) & 0xff, ((a) >> 24) & 0xff, \
19 		(b) & 0xff, ((b) >> 8) & 0xff, \
20 		(c) & 0xff, ((c) >> 8) & 0xff, \
21 		(d0), (d1), (d2), (d3), (d4), (d5), (d6), (d7) }})
22 
23 #define PARTITION_IDBLOCK_GUID \
24 	EFI_GUID(0xDA2BB095, 0x390E, 0x48ca, \
25 		0x90, 0x47, 0x05, 0xE8, 0x18, 0xB2, 0x97, 0xCE)
26 
27 typedef unsigned char u8;
28 typedef unsigned short u16;
29 typedef unsigned int  u32;
30 typedef unsigned long long u64;
31 
32 #define uswap_16(x) \
33 	((((x) & 0xff00) >> 8) | \
34 	 (((x) & 0x00ff) << 8))
35 #define uswap_32(x) \
36 	((((x) & 0xff000000) >> 24) | \
37 	 (((x) & 0x00ff0000) >>  8) | \
38 	 (((x) & 0x0000ff00) <<  8) | \
39 	 (((x) & 0x000000ff) << 24))
40 #define _uswap_64(x, sfx) \
41 	((((x) & 0xff00000000000000##sfx) >> 56) | \
42 	 (((x) & 0x00ff000000000000##sfx) >> 40) | \
43 	 (((x) & 0x0000ff0000000000##sfx) >> 24) | \
44 	 (((x) & 0x000000ff00000000##sfx) >>  8) | \
45 	 (((x) & 0x00000000ff000000##sfx) <<  8) | \
46 	 (((x) & 0x0000000000ff0000##sfx) << 24) | \
47 	 (((x) & 0x000000000000ff00##sfx) << 40) | \
48 	 (((x) & 0x00000000000000ff##sfx) << 56))
49 #if defined(__GNUC__)
50 # define uswap_64(x) _uswap_64(x, ull)
51 #else
52 # define uswap_64(x) _uswap_64(x, )
53 #endif
54 #define __LITTLE_ENDIAN__ 1
55 #ifdef __LITTLE_ENDIAN__
56 # define cpu_to_le16(x)		(x)
57 # define cpu_to_le32(x)		(x)
58 # define cpu_to_le64(x)		(x)
59 # define le16_to_cpu(x)		(x)
60 # define le32_to_cpu(x)		(x)
61 # define le64_to_cpu(x)		(x)
62 # define cpu_to_be16(x)		uswap_16(x)
63 # define cpu_to_be32(x)		uswap_32(x)
64 # define cpu_to_be64(x)		uswap_64(x)
65 # define be16_to_cpu(x)		uswap_16(x)
66 # define be32_to_cpu(x)		uswap_32(x)
67 # define be64_to_cpu(x)		uswap_64(x)
68 #else
69 # define cpu_to_le16(x)		uswap_16(x)
70 # define cpu_to_le32(x)		uswap_32(x)
71 # define cpu_to_le64(x)		uswap_64(x)
72 # define le16_to_cpu(x)		uswap_16(x)
73 # define le32_to_cpu(x)		uswap_32(x)
74 # define le64_to_cpu(x)		uswap_64(x)
75 # define cpu_to_be16(x)		(x)
76 # define cpu_to_be32(x)		(x)
77 # define cpu_to_be64(x)		(x)
78 # define be16_to_cpu(x)		(x)
79 # define be32_to_cpu(x)		(x)
80 # define be64_to_cpu(x)		(x)
81 #endif
82 
83 
84 typedef union {
85 	struct {
86 	unsigned int time_low;
87 	unsigned short time_mid;
88 	unsigned short time_hi_and_version;
89 	unsigned char clock_seq_hi_and_reserved;
90 	unsigned char clock_seq_low;
91 	unsigned char node[6];
92 	} uuid;
93 	u8 raw[16];
94 } efi_guid_t;
95 #pragma pack(1)
96 typedef struct {
97 	u16 usTag;/*0xEEEE*/
98 	u16 usBackupGpt;/*0:no backup,1:has backup*/
99 	u16 usEntryCount;
100 	u64 entryDataSize[32];
101 } gpt_compact_info;
102 /* based on linux/include/genhd.h */
103 typedef struct {
104 	u8 boot_ind;		/* 0x80 - active */
105 	u8 head;		/* starting head */
106 	u8 sector;		/* starting sector */
107 	u8 cyl;			/* starting cylinder */
108 	u8 sys_ind;		/* What partition type */
109 	u8 end_head;		/* end head */
110 	u8 end_sector;		/* end sector */
111 	u8 end_cyl;		/* end cylinder */
112 	u32 start_sect;	/* starting sector counting from 0 */
113 	u32 nr_sects;	/* nr of sectors in partition */
114 } mbr_partition ;
115 
116 /* based on linux/fs/partitions/efi.h */
117 typedef struct _gpt_header {
118 	u64 signature;
119 	u32 revision;
120 	u32 header_size;
121 	u32 header_crc32;
122 	u32 reserved1;
123 	u64 my_lba;
124 	u64 alternate_lba;
125 	u64 first_usable_lba;
126 	u64 last_usable_lba;
127 	efi_guid_t disk_guid;
128 	u64 partition_entry_lba;
129 	u32 num_partition_entries;
130 	u32 sizeof_partition_entry;
131 	u32 partition_entry_array_crc32;
132 } gpt_header;
133 
134 typedef union _gpt_entry_attributes {
135 	struct {
136 		u64 required_to_function:1;
137 		u64 no_block_io_protocol:1;
138 		u64 legacy_bios_bootable:1;
139 		u64 reserved:45;
140 		u64 type_guid_specific:16;
141 	} fields;
142 	unsigned long long raw;
143 } gpt_entry_attributes;
144 
145 #define PARTNAME_SZ	72
146 typedef struct _gpt_entry {
147 	efi_guid_t partition_type_guid;
148 	efi_guid_t unique_partition_guid;
149 	u64 starting_lba;
150 	u64 ending_lba;
151 	gpt_entry_attributes attributes;
152 	u16 partition_name[PARTNAME_SZ / sizeof(u16)];
153 } gpt_entry;
154 
155 typedef struct _legacy_mbr {
156 	u8 boot_code[440];
157 	u32 unique_mbr_signature;
158 	u16 unknown;
159 	mbr_partition partition_record[4];
160 	u16 signature;
161 } legacy_mbr;
162 #pragma pack()
163 #endif	/* _GPT_H */
164