1 /*
2 * Copyright (c) 2006-2023, RT-Thread Development Team
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Change Logs:
7 * Date Author Notes
8 * 2022-05-05 linzhenxing first version
9 * 2023-02-25 GuEe-GUI make blk interface
10 */
11
12 #ifndef __PARTITIONS_EFI_H__
13 #define __PARTITIONS_EFI_H__
14
15 #include "../blk_partition.h"
16 #include <drivers/misc.h>
17 #include <drivers/byteorder.h>
18
19 #define MSDOS_MBR_SIGNATURE 0xaa55
20 #define EFI_PMBR_OSTYPE_EFI 0xef
21 #define EFI_PMBR_OSTYPE_EFI_GPT 0xee
22
23 #define GPT_MBR_PROTECTIVE 1
24 #define GPT_MBR_HYBRID 2
25
26 #define GPT_HEADER_SIGNATURE 0x5452415020494645ULL
27 #define GPT_HEADER_REVISION_V1 0x00010000
28 #define GPT_PRIMARY_PARTITION_TABLE_LBA 1
29
30 #ifndef __UUID_H__
31 #define UUID_SIZE 16
32
33 rt_packed(struct _guid_t
34 {
35 rt_uint8_t b[UUID_SIZE];
36 });
37 typedef struct _guid_t guid_t;
38 #endif /* __UUID_H__ */
39
40 #ifndef __EFI_H__
41 typedef guid_t efi_guid_t rt_align(4);
42
43 #define EFI_GUID(a, b, c, d...) (efi_guid_t) \
44 {{ \
45 (a) & 0xff, ((a) >> 8) & 0xff, ((a) >> 16) & 0xff, ((a) >> 24) & 0xff, \
46 (b) & 0xff, ((b) >> 8) & 0xff, \
47 (c) & 0xff, ((c) >> 8) & 0xff, \
48 d \
49 }}
50
51 #define NULL_GUID \
52 EFI_GUID(0x00000000, 0x0000, 0x0000, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)
53
efi_guidcmp(efi_guid_t left,efi_guid_t right)54 rt_inline int efi_guidcmp(efi_guid_t left, efi_guid_t right)
55 {
56 return rt_memcmp(&left, &right, sizeof (efi_guid_t));
57 }
58 #endif /* __EFI_H__ */
59
60 #define PARTITION_SYSTEM_GUID \
61 EFI_GUID(0xc12a7328, 0xf81f, 0x11d2, 0xba, 0x4b, 0x00, 0xa0, 0xc9, 0x3e, 0xc9, 0x3b)
62
63 #define LEGACY_MBR_PARTITION_GUID \
64 EFI_GUID(0x024dee41, 0x33e7, 0x11d3, 0x9d, 0x69, 0x00, 0x08, 0xc7, 0x81, 0xf3, 0x9f)
65
66 #define PARTITION_MSFT_RESERVED_GUID \
67 EFI_GUID(0xe3c9e316, 0x0b5c, 0x4db8, 0x81, 0x7d, 0xf9, 0x2d, 0xf0, 0x02, 0x15, 0xae)
68
69 #define PARTITION_BASIC_DATA_GUID \
70 EFI_GUID(0xebd0a0a2, 0xb9e5, 0x4433, 0x87, 0xc0, 0x68, 0xb6, 0xb7, 0x26, 0x99, 0xc7)
71
72 rt_packed(struct _gpt_header
73 {
74 rt_le64_t signature;
75 rt_le32_t revision;
76 rt_le32_t header_size;
77 rt_le32_t header_crc32;
78 rt_le32_t reserved1;
79 rt_le64_t start_lba;
80 rt_le64_t alternate_lba;
81 rt_le64_t first_usable_lba;
82 rt_le64_t last_usable_lba;
83 efi_guid_t disk_guid;
84 rt_le64_t partition_entry_lba;
85 rt_le32_t num_partition_entries;
86 rt_le32_t sizeof_partition_entry;
87 rt_le32_t partition_entry_array_crc32;
88
89 /*
90 * The rest of the logical block is reserved by UEFI and must be zero.
91 * EFI standard handles this by:
92 *
93 * uint8_t reserved2[BlockSize - 92];
94 */
95 });
96 typedef struct _gpt_header gpt_header;
97
98 rt_packed(struct _gpt_entry_attributes
99 {
100 rt_uint64_t required_to_function:1;
101 rt_uint64_t reserved:47;
102 rt_uint64_t type_guid_specific:16;
103 });
104 typedef struct _gpt_entry_attributes gpt_entry_attributes;
105
106 rt_packed(struct _gpt_entry
107 {
108 efi_guid_t partition_type_guid;
109 efi_guid_t unique_partition_guid;
110 rt_le64_t starting_lba;
111 rt_le64_t ending_lba;
112 gpt_entry_attributes attributes;
113 rt_le16_t partition_name[72/sizeof(rt_le16_t)];
114 });
115 typedef struct _gpt_entry gpt_entry;
116
117 rt_packed(struct _gpt_mbr_record
118 {
119 rt_uint8_t boot_indicator; /* unused by EFI, set to 0x80 for bootable */
120 rt_uint8_t start_head; /* unused by EFI, pt start in CHS */
121 rt_uint8_t start_sector; /* unused by EFI, pt start in CHS */
122 rt_uint8_t start_track;
123 rt_uint8_t os_type; /* EFI and legacy non-EFI OS types */
124 rt_uint8_t end_head; /* unused by EFI, pt end in CHS */
125 rt_uint8_t end_sector; /* unused by EFI, pt end in CHS */
126 rt_uint8_t end_track; /* unused by EFI, pt end in CHS */
127 rt_le32_t starting_lba; /* used by EFI - start addr of the on disk pt */
128 rt_le32_t size_in_lba; /* used by EFI - size of pt in LBA */
129 });
130 typedef struct _gpt_mbr_record gpt_mbr_record;
131
132 rt_packed(struct _legacy_mbr
133 {
134 rt_uint8_t boot_code[440];
135 rt_le32_t unique_mbr_signature;
136 rt_le16_t unknown;
137 gpt_mbr_record partition_record[4];
138 rt_le16_t signature;
139 });
140 typedef struct _legacy_mbr legacy_mbr;
141
142 #endif /* __PARTITIONS_EFI_H__ */
143