xref: /OK3568_Linux_fs/external/rkupdate/gpt.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
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 {
86     struct
87     {
88         unsigned int time_low;
89         unsigned short time_mid;
90         unsigned short time_hi_and_version;
91         unsigned char clock_seq_hi_and_reserved;
92         unsigned char clock_seq_low;
93         unsigned char node[6];
94     } uuid;
95     u8 raw[16];
96 } efi_guid_t;
97 #pragma pack(1)
98 typedef struct
99 {
100     u16 usTag;/*0xEEEE*/
101     u16 usBackupGpt;/*0:no backup,1:has backup*/
102     u16 usEntryCount;
103     u64 entryDataSize[32];
104 } gpt_compact_info;
105 /* based on linux/include/genhd.h */
106 typedef struct
107 {
108     u8 boot_ind;        /* 0x80 - active */
109     u8 head;        /* starting head */
110     u8 sector;      /* starting sector */
111     u8 cyl;         /* starting cylinder */
112     u8 sys_ind;     /* What partition type */
113     u8 end_head;        /* end head */
114     u8 end_sector;      /* end sector */
115     u8 end_cyl;     /* end cylinder */
116     u32 start_sect; /* starting sector counting from 0 */
117     u32 nr_sects;   /* nr of sectors in partition */
118 } mbr_partition ;
119 
120 /* based on linux/fs/partitions/efi.h */
121 typedef struct _gpt_header
122 {
123     u64 signature;
124     u32 revision;
125     u32 header_size;
126     u32 header_crc32;
127     u32 reserved1;
128     u64 my_lba;
129     u64 alternate_lba;
130     u64 first_usable_lba;
131     u64 last_usable_lba;
132     efi_guid_t disk_guid;
133     u64 partition_entry_lba;
134     u32 num_partition_entries;
135     u32 sizeof_partition_entry;
136     u32 partition_entry_array_crc32;
137 } gpt_header;
138 
139 typedef union _gpt_entry_attributes
140 {
141     struct
142     {
143         u64 required_to_function: 1;
144         u64 no_block_io_protocol: 1;
145         u64 legacy_bios_bootable: 1;
146         u64 reserved: 45;
147         u64 type_guid_specific: 16;
148     } fields;
149     unsigned long long raw;
150 } gpt_entry_attributes;
151 
152 #define PARTNAME_SZ 72
153 typedef struct _gpt_entry
154 {
155     efi_guid_t partition_type_guid;
156     efi_guid_t unique_partition_guid;
157     u64 starting_lba;
158     u64 ending_lba;
159     gpt_entry_attributes attributes;
160     u16 partition_name[PARTNAME_SZ / sizeof(u16)];
161 } gpt_entry;
162 
163 typedef struct _legacy_mbr
164 {
165     u8 boot_code[440];
166     u32 unique_mbr_signature;
167     u16 unknown;
168     mbr_partition partition_record[4];
169     u16 signature;
170 } legacy_mbr;
171 #pragma pack()
172 #endif  /* _GPT_H */
173