xref: /OK3568_Linux_fs/external/recovery/update_engine/gpt.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * Copyright (C) 2023 Rockchip Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *       http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef _GPT_H
18 #define _GPT_H
19 
20 #define MSDOS_MBR_SIGNATURE 0xAA55
21 #define EFI_PMBR_OSTYPE_EFI 0xEF
22 #define EFI_PMBR_OSTYPE_EFI_GPT 0xEE
23 
24 #define GPT_HEADER_SIGNATURE 0x5452415020494645ULL
25 #define GPT_HEADER_REVISION_V1 0x00010000
26 #define GPT_PRIMARY_PARTITION_TABLE_LBA 1ULL
27 #define GPT_ENTRY_NAME "gpt"
28 #define GPT_ENTRY_NUMBERS        128
29 #define GPT_ENTRY_SIZE            128
30 #define PART_PROPERTY_BOOTABLE (1 << 2)
31 
32 #define EFI_GUID(a,b,c,d0,d1,d2,d3,d4,d5,d6,d7) \
33     ((efi_guid_t) \
34     {{ (a) & 0xff, ((a) >> 8) & 0xff, ((a) >> 16) & 0xff, ((a) >> 24) & 0xff, \
35         (b) & 0xff, ((b) >> 8) & 0xff, \
36         (c) & 0xff, ((c) >> 8) & 0xff, \
37         (d0), (d1), (d2), (d3), (d4), (d5), (d6), (d7) }})
38 
39 #define PARTITION_IDBLOCK_GUID \
40     EFI_GUID(0xDA2BB095, 0x390E, 0x48ca, \
41         0x90, 0x47, 0x05, 0xE8, 0x18, 0xB2, 0x97, 0xCE)
42 
43 typedef unsigned char u8;
44 typedef unsigned short u16;
45 typedef unsigned int  u32;
46 typedef unsigned long long u64;
47 
48 #define uswap_16(x) \
49     ((((x) & 0xff00) >> 8) | \
50      (((x) & 0x00ff) << 8))
51 #define uswap_32(x) \
52     ((((x) & 0xff000000) >> 24) | \
53      (((x) & 0x00ff0000) >>  8) | \
54      (((x) & 0x0000ff00) <<  8) | \
55      (((x) & 0x000000ff) << 24))
56 #define _uswap_64(x, sfx) \
57     ((((x) & 0xff00000000000000##sfx) >> 56) | \
58      (((x) & 0x00ff000000000000##sfx) >> 40) | \
59      (((x) & 0x0000ff0000000000##sfx) >> 24) | \
60      (((x) & 0x000000ff00000000##sfx) >>  8) | \
61      (((x) & 0x00000000ff000000##sfx) <<  8) | \
62      (((x) & 0x0000000000ff0000##sfx) << 24) | \
63      (((x) & 0x000000000000ff00##sfx) << 40) | \
64      (((x) & 0x00000000000000ff##sfx) << 56))
65 #if defined(__GNUC__)
66 # define uswap_64(x) _uswap_64(x, ull)
67 #else
68 # define uswap_64(x) _uswap_64(x, )
69 #endif
70 #define __LITTLE_ENDIAN__ 1
71 #ifdef __LITTLE_ENDIAN__
72 # define cpu_to_le16(x)        (x)
73 # define cpu_to_le32(x)        (x)
74 # define cpu_to_le64(x)        (x)
75 # define le16_to_cpu(x)        (x)
76 # define le32_to_cpu(x)        (x)
77 # define le64_to_cpu(x)        (x)
78 # define cpu_to_be16(x)        uswap_16(x)
79 # define cpu_to_be32(x)        uswap_32(x)
80 # define cpu_to_be64(x)        uswap_64(x)
81 # define be16_to_cpu(x)        uswap_16(x)
82 # define be32_to_cpu(x)        uswap_32(x)
83 # define be64_to_cpu(x)        uswap_64(x)
84 #else
85 # define cpu_to_le16(x)        uswap_16(x)
86 # define cpu_to_le32(x)        uswap_32(x)
87 # define cpu_to_le64(x)        uswap_64(x)
88 # define le16_to_cpu(x)        uswap_16(x)
89 # define le32_to_cpu(x)        uswap_32(x)
90 # define le64_to_cpu(x)        uswap_64(x)
91 # define cpu_to_be16(x)        (x)
92 # define cpu_to_be32(x)        (x)
93 # define cpu_to_be64(x)        (x)
94 # define be16_to_cpu(x)        (x)
95 # define be32_to_cpu(x)        (x)
96 # define be64_to_cpu(x)        (x)
97 #endif
98 
99 
100 typedef union {
101     struct {
102         unsigned int time_low;
103         unsigned short time_mid;
104         unsigned short time_hi_and_version;
105         unsigned char clock_seq_hi_and_reserved;
106         unsigned char clock_seq_low;
107         unsigned char node[6];
108     } uuid;
109     u8 raw[16];
110 } efi_guid_t;
111 #pragma pack(1)
112 typedef struct {
113     u16 usTag;/*0xEEEE*/
114     u16 usBackupGpt;/*0:no backup,1:has backup*/
115     u16 usEntryCount;
116     u64 entryDataSize[32];
117 } gpt_compact_info;
118 /* based on linux/include/genhd.h */
119 typedef struct {
120     u8 boot_ind;        /* 0x80 - active */
121     u8 head;        /* starting head */
122     u8 sector;        /* starting sector */
123     u8 cyl;            /* starting cylinder */
124     u8 sys_ind;        /* What partition type */
125     u8 end_head;        /* end head */
126     u8 end_sector;        /* end sector */
127     u8 end_cyl;        /* end cylinder */
128     u32 start_sect;    /* starting sector counting from 0 */
129     u32 nr_sects;    /* nr of sectors in partition */
130 } mbr_partition ;
131 
132 /* based on linux/fs/partitions/efi.h */
133 typedef struct _gpt_header {
134     u64 signature;                          //固定字符串“EFI PART”
135     u32 revision;                           //GPT版本号
136     u32 header_size;                        //主GPT头大小
137     u32 header_crc32;                       //主GPT头的CRC32校验数据,注意不是累加和校验
138     u32 reserved1;                          //保留位
139     u64 my_lba;                             //主GPT头起始扇区号,LBA1
140     u64 alternate_lba;                      //主GPT头备份位置扇区号 LBA-1
141     u64 first_usable_lba;                   //GPT分区起始扇区号,LBA34
142     u64 last_usable_lba;                    //GPT分区结束扇区号,LBA-34
143     efi_guid_t disk_guid;                   //磁盘的GUID,唯一标识,这也意味着全硬盘复制是行不通的.
144     u64 partition_entry_lba;                //分区表起始扇区号,LBA2
145     u32 num_partition_entries;              //分区表项数目,MS强行给128
146     u32 sizeof_partition_entry;             //每个分区表占用字节数,128
147     u32 partition_entry_array_crc32;        //分区表CRC校验数据
148 } gpt_header;
149 
150 typedef union _gpt_entry_attributes {
151     struct {
152         u64 required_to_function: 1;
153         u64 no_block_io_protocol: 1;
154         u64 legacy_bios_bootable: 1;
155         u64 reserved: 45;
156         u64 type_guid_specific: 16;
157     } fields;
158     unsigned long long raw;
159 } gpt_entry_attributes;
160 
161 #define PARTNAME_SZ    72
162 typedef struct _gpt_entry {
163     efi_guid_t partition_type_guid;
164     efi_guid_t unique_partition_guid;
165     u64 starting_lba;
166     u64 ending_lba;
167     gpt_entry_attributes attributes;
168     u16 partition_name[PARTNAME_SZ / sizeof(u16)];
169 } gpt_entry;
170 
171 typedef struct _legacy_mbr {
172     u8 boot_code[440];
173     u32 unique_mbr_signature;
174     u16 unknown;
175     mbr_partition partition_record[4];
176     u16 signature;
177 } legacy_mbr;
178 #pragma pack()
179 #endif    /* _GPT_H */
180