xref: /DragonStub/inc/dragonstub/linux-efi.h (revision 3e6106c4d60a23aae3c0740979c5e6fb728b63c3)
1 #pragma once
2 #include <efidef.h>
3 #include "compiler_attributes.h"
4 #include "types.h"
5 #include "linux/pfn.h"
6 #if defined(CONFIG_riscv64)
7 #include "riscv64.h"
8 #endif
9 
10 #define efi_table_hdr_t EFI_TABLE_HEADER
11 #define efi_guid_t EFI_GUID
12 #define efi_runtime_services_t EFI_RUNTIME_SERVICES
13 #define efi_boot_services_t EFI_BOOT_SERVICES
14 
15 #define MAKE_EFI_GUID(a, b, c, d...)       \
16 	(efi_guid_t)                       \
17 	{                                  \
18 		a, b & 0xffff, c & 0xffff, \
19 		{                          \
20 			d                  \
21 		}                          \
22 	}
23 
24 /*
25  * EFI Configuration Table and GUID definitions
26  *
27  * These are all defined in a single line to make them easier to
28  * grep for and to see them at a glance - while still having a
29  * similar structure to the definitions in the spec.
30  *
31  * Here's how they are structured:
32  *
33  * GUID: 12345678-1234-1234-1234-123456789012
34  * Spec:
35  *      #define EFI_SOME_PROTOCOL_GUID \
36  *        {0x12345678,0x1234,0x1234,\
37  *          {0x12,0x34,0x12,0x34,0x56,0x78,0x90,0x12}}
38  * Here:
39  *	#define SOME_PROTOCOL_GUID		EFI_GUID(0x12345678, 0x1234, 0x1234,  0x12, 0x34, 0x12, 0x34, 0x56, 0x78, 0x90, 0x12)
40  *					^ tabs					    ^extra space
41  *
42  * Note that the 'extra space' separates the values at the same place
43  * where the UEFI SPEC breaks the line.
44  */
45 #define NULL_GUID                                                         \
46 	MAKE_EFI_GUID(0x00000000, 0x0000, 0x0000, 0x00, 0x00, 0x00, 0x00, \
47 		      0x00, 0x00, 0x00, 0x00)
48 #ifndef MPS_TABLE_GUID
49 #define MPS_TABLE_GUID                                                    \
50 	MAKE_EFI_GUID(0xeb9d2d2f, 0x2d88, 0x11d3, 0x9a, 0x16, 0x00, 0x90, \
51 		      0x27, 0x3f, 0xc1, 0x4d)
52 #endif
53 #ifndef ACPI_TABLE_GUID
54 #define ACPI_TABLE_GUID                                                   \
55 	MAKE_EFI_GUID(0xeb9d2d30, 0x2d88, 0x11d3, 0x9a, 0x16, 0x00, 0x90, \
56 		      0x27, 0x3f, 0xc1, 0x4d)
57 #endif
58 #ifndef ACPI_20_TABLE_GUID
59 #define ACPI_20_TABLE_GUID                                                \
60 	MAKE_EFI_GUID(0x8868e871, 0xe4f1, 0x11d3, 0xbc, 0x22, 0x00, 0x80, \
61 		      0xc7, 0x3c, 0x88, 0x81)
62 #endif
63 #ifndef SMBIOS_TABLE_GUID
64 #define SMBIOS_TABLE_GUID                                                 \
65 	MAKE_EFI_GUID(0xeb9d2d31, 0x2d88, 0x11d3, 0x9a, 0x16, 0x00, 0x90, \
66 		      0x27, 0x3f, 0xc1, 0x4d)
67 #endif
68 #ifndef SMBIOS3_TABLE_GUID
69 #define SMBIOS3_TABLE_GUID                                                \
70 	MAKE_EFI_GUID(0xf2fd1544, 0x9794, 0x4a2c, 0x99, 0x2e, 0xe5, 0xbb, \
71 		      0xcf, 0x20, 0xe3, 0x94)
72 #endif
73 #ifndef SAL_SYSTEM_TABLE_GUID
74 #define SAL_SYSTEM_TABLE_GUID                                             \
75 	MAKE_EFI_GUID(0xeb9d2d32, 0x2d88, 0x11d3, 0x9a, 0x16, 0x00, 0x90, \
76 		      0x27, 0x3f, 0xc1, 0x4d)
77 #endif
78 #define HCDP_TABLE_GUID                                                   \
79 	MAKE_EFI_GUID(0xf951938d, 0x620b, 0x42ef, 0x82, 0x79, 0xa8, 0x4b, \
80 		      0x79, 0x61, 0x78, 0x98)
81 #define UGA_IO_PROTOCOL_GUID                                              \
82 	MAKE_EFI_GUID(0x61a4d49e, 0x6f68, 0x4f1b, 0xb9, 0x22, 0xa8, 0x6e, \
83 		      0xed, 0x0b, 0x07, 0xa2)
84 #define EFI_GLOBAL_VARIABLE_GUID                                          \
85 	MAKE_EFI_GUID(0x8be4df61, 0x93ca, 0x11d2, 0xaa, 0x0d, 0x00, 0xe0, \
86 		      0x98, 0x03, 0x2b, 0x8c)
87 #define UV_SYSTEM_TABLE_GUID                                              \
88 	MAKE_EFI_GUID(0x3b13a7d4, 0x633e, 0x11dd, 0x93, 0xec, 0xda, 0x25, \
89 		      0x56, 0xd8, 0x95, 0x93)
90 #define LINUX_EFI_CRASH_GUID                                              \
91 	MAKE_EFI_GUID(0xcfc8fc79, 0xbe2e, 0x4ddc, 0x97, 0xf0, 0x9f, 0x98, \
92 		      0xbf, 0xe2, 0x98, 0xa0)
93 #define LOADED_IMAGE_PROTOCOL_GUID                                        \
94 	MAKE_EFI_GUID(0x5b1b31a1, 0x9562, 0x11d2, 0x8e, 0x3f, 0x00, 0xa0, \
95 		      0xc9, 0x69, 0x72, 0x3b)
96 #define LOADED_IMAGE_DEVICE_PATH_PROTOCOL_GUID                            \
97 	MAKE_EFI_GUID(0xbc62157e, 0x3e33, 0x4fec, 0x99, 0x20, 0x2d, 0x3b, \
98 		      0x36, 0xd7, 0x50, 0xdf)
99 #ifndef EFI_DEVICE_PATH_PROTOCOL_GUID
100 #define EFI_DEVICE_PATH_PROTOCOL_GUID                                     \
101 	MAKE_EFI_GUID(0x09576e91, 0x6d3f, 0x11d2, 0x8e, 0x39, 0x00, 0xa0, \
102 		      0xc9, 0x69, 0x72, 0x3b)
103 #endif
104 #ifndef EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID
105 #define EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID                             \
106 	MAKE_EFI_GUID(0x8b843e20, 0x8132, 0x4852, 0x90, 0xcc, 0x55, 0x1a, \
107 		      0x4e, 0x4a, 0x7f, 0x1c)
108 #endif
109 #ifndef EFI_DEVICE_PATH_FROM_TEXT_PROTOCOL_GUID
110 #define EFI_DEVICE_PATH_FROM_TEXT_PROTOCOL_GUID                           \
111 	MAKE_EFI_GUID(0x05c99a21, 0xc70f, 0x4ad2, 0x8a, 0x5f, 0x35, 0xdf, \
112 		      0x33, 0x43, 0xf5, 0x1e)
113 #endif
114 #ifndef EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID
115 #define EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID                                 \
116 	MAKE_EFI_GUID(0x9042a9de, 0x23dc, 0x4a38, 0x96, 0xfb, 0x7a, 0xde, \
117 		      0xd0, 0x80, 0x51, 0x6a)
118 #endif
119 #define EFI_UGA_PROTOCOL_GUID                                             \
120 	MAKE_EFI_GUID(0x982c298b, 0xf4fa, 0x41cb, 0xb8, 0x38, 0x77, 0xaa, \
121 		      0x68, 0x8f, 0xb8, 0x39)
122 #ifndef EFI_PCI_IO_PROTOCOL_GUID
123 #define EFI_PCI_IO_PROTOCOL_GUID                                          \
124 	MAKE_EFI_GUID(0x4cf5b200, 0x68b8, 0x4ca5, 0x9e, 0xec, 0xb2, 0x3e, \
125 		      0x3f, 0x50, 0x02, 0x9a)
126 #endif
127 #ifndef EFI_FILE_INFO_ID
128 #define EFI_FILE_INFO_ID                                                  \
129 	MAKE_EFI_GUID(0x09576e92, 0x6d3f, 0x11d2, 0x8e, 0x39, 0x00, 0xa0, \
130 		      0xc9, 0x69, 0x72, 0x3b)
131 #endif
132 #define EFI_SYSTEM_RESOURCE_TABLE_GUID                                    \
133 	MAKE_EFI_GUID(0xb122a263, 0x3661, 0x4f68, 0x99, 0x29, 0x78, 0xf8, \
134 		      0xb0, 0xd6, 0x21, 0x80)
135 #define EFI_FILE_SYSTEM_GUID                                              \
136 	MAKE_EFI_GUID(0x964e5b22, 0x6459, 0x11d2, 0x8e, 0x39, 0x00, 0xa0, \
137 		      0xc9, 0x69, 0x72, 0x3b)
138 #define DEVICE_TREE_GUID                                                  \
139 	MAKE_EFI_GUID(0xb1b621d5, 0xf19c, 0x41a5, 0x83, 0x0b, 0xd9, 0x15, \
140 		      0x2c, 0x69, 0xaa, 0xe0)
141 #define EFI_PROPERTIES_TABLE_GUID                                         \
142 	MAKE_EFI_GUID(0x880aaca3, 0x4adc, 0x4a04, 0x90, 0x79, 0xb7, 0x47, \
143 		      0x34, 0x08, 0x25, 0xe5)
144 #ifndef EFI_RNG_PROTOCOL_GUID
145 #define EFI_RNG_PROTOCOL_GUID                                             \
146 	MAKE_EFI_GUID(0x3152bca5, 0xeade, 0x433d, 0x86, 0x2e, 0xc0, 0x1c, \
147 		      0xdc, 0x29, 0x1f, 0x44)
148 #endif
149 #ifndef EFI_RNG_ALGORITHM_RAW
150 #define EFI_RNG_ALGORITHM_RAW                                             \
151 	MAKE_EFI_GUID(0xe43176d7, 0xb6e8, 0x4827, 0xb7, 0x84, 0x7f, 0xfd, \
152 		      0xc4, 0xb6, 0x85, 0x61)
153 #endif
154 #define EFI_MEMORY_ATTRIBUTES_TABLE_GUID                                  \
155 	MAKE_EFI_GUID(0xdcfa911d, 0x26eb, 0x469f, 0xa2, 0x20, 0x38, 0xb7, \
156 		      0xdc, 0x46, 0x12, 0x20)
157 #define EFI_CONSOLE_OUT_DEVICE_GUID                                       \
158 	MAKE_EFI_GUID(0xd3b36f2c, 0xd551, 0x11d4, 0x9a, 0x46, 0x00, 0x90, \
159 		      0x27, 0x3f, 0xc1, 0x4d)
160 #define APPLE_PROPERTIES_PROTOCOL_GUID                                    \
161 	MAKE_EFI_GUID(0x91bd12fe, 0xf6c3, 0x44fb, 0xa5, 0xb7, 0x51, 0x22, \
162 		      0xab, 0x30, 0x3a, 0xe0)
163 #define EFI_TCG2_PROTOCOL_GUID                                            \
164 	MAKE_EFI_GUID(0x607f766c, 0x7455, 0x42be, 0x93, 0x0b, 0xe4, 0xd7, \
165 		      0x6d, 0xb2, 0x72, 0x0f)
166 #ifndef EFI_LOAD_FILE_PROTOCOL_GUID
167 #define EFI_LOAD_FILE_PROTOCOL_GUID                                       \
168 	MAKE_EFI_GUID(0x56ec3091, 0x954c, 0x11d2, 0x8e, 0x3f, 0x00, 0xa0, \
169 		      0xc9, 0x69, 0x72, 0x3b)
170 #endif
171 #define EFI_LOAD_FILE2_PROTOCOL_GUID                                      \
172 	MAKE_EFI_GUID(0x4006c0c1, 0xfcb3, 0x403e, 0x99, 0x6d, 0x4a, 0x6c, \
173 		      0x87, 0x24, 0xe0, 0x6d)
174 #define EFI_RT_PROPERTIES_TABLE_GUID                                      \
175 	MAKE_EFI_GUID(0xeb66918a, 0x7eef, 0x402a, 0x84, 0x2e, 0x93, 0x1d, \
176 		      0x21, 0xc3, 0x8a, 0xe9)
177 #define EFI_DXE_SERVICES_TABLE_GUID                                       \
178 	MAKE_EFI_GUID(0x05ad34ba, 0x6f02, 0x4214, 0x95, 0x2e, 0x4d, 0xa0, \
179 		      0x39, 0x8e, 0x2b, 0xb9)
180 #define EFI_SMBIOS_PROTOCOL_GUID                                          \
181 	MAKE_EFI_GUID(0x03583ff6, 0xcb36, 0x4940, 0x94, 0x7e, 0xb9, 0xb3, \
182 		      0x9f, 0x4a, 0xfa, 0xf7)
183 #define EFI_MEMORY_ATTRIBUTE_PROTOCOL_GUID                                \
184 	MAKE_EFI_GUID(0xf4560cf6, 0x40ec, 0x4b4a, 0xa1, 0x92, 0xbf, 0x1d, \
185 		      0x57, 0xd0, 0xb1, 0x89)
186 
187 #define EFI_IMAGE_SECURITY_DATABASE_GUID                                  \
188 	MAKE_EFI_GUID(0xd719b2cb, 0x3d3a, 0x4596, 0xa3, 0xbc, 0xda, 0xd0, \
189 		      0x0e, 0x67, 0x65, 0x6f)
190 #define EFI_SHIM_LOCK_GUID                                                \
191 	MAKE_EFI_GUID(0x605dab50, 0xe046, 0x4300, 0xab, 0xb6, 0x3d, 0xd8, \
192 		      0x10, 0xdd, 0x8b, 0x23)
193 
194 #define EFI_CERT_SHA256_GUID                                              \
195 	MAKE_EFI_GUID(0xc1c41626, 0x504c, 0x4092, 0xac, 0xa9, 0x41, 0xf9, \
196 		      0x36, 0x93, 0x43, 0x28)
197 #define EFI_CERT_X509_GUID                                                \
198 	MAKE_EFI_GUID(0xa5c059a1, 0x94e4, 0x4aa7, 0x87, 0xb5, 0xab, 0x15, \
199 		      0x5c, 0x2b, 0xf0, 0x72)
200 #define EFI_CERT_X509_SHA256_GUID                                         \
201 	MAKE_EFI_GUID(0x3bd2a492, 0x96c0, 0x4079, 0xb4, 0x20, 0xfc, 0xf9, \
202 		      0x8e, 0xf1, 0x03, 0xed)
203 #define EFI_CC_BLOB_GUID                                                  \
204 	MAKE_EFI_GUID(0x067b1f5f, 0xcf26, 0x44c5, 0x85, 0x54, 0x93, 0xd7, \
205 		      0x77, 0x91, 0x2d, 0x42)
206 
207 #define RISCV_EFI_BOOT_PROTOCOL_GUID                                      \
208 	MAKE_EFI_GUID(0xccd15fec, 0x6f73, 0x4eec, 0x83, 0x95, 0x3e, 0x69, \
209 		      0xe4, 0xb9, 0x40, 0xbf)
210 
211 #if defined(CONFIG_X86_64)
212 #define __efiapi __attribute__((ms_abi))
213 #elif defined(CONFIG_X86_32)
214 #define __efiapi __attribute__((regparm(0)))
215 #elif defined(CONFIG_riscv64)
216 #define __efiapi
217 #elif defined(CONFIG_aarch64)
218 #define __efiapi
219 #else
220 #error "Unsupported architecture"
221 #endif
222 
223 /*
224  * EFI Device Path information
225  */
226 #define EFI_DEV_HW 0x01
227 #define EFI_DEV_PCI 1
228 #define EFI_DEV_PCCARD 2
229 #define EFI_DEV_MEM_MAPPED 3
230 #define EFI_DEV_VENDOR 4
231 #define EFI_DEV_CONTROLLER 5
232 #define EFI_DEV_ACPI 0x02
233 #define EFI_DEV_BASIC_ACPI 1
234 #define EFI_DEV_EXPANDED_ACPI 2
235 #define EFI_DEV_MSG 0x03
236 #define EFI_DEV_MSG_ATAPI 1
237 #define EFI_DEV_MSG_SCSI 2
238 #define EFI_DEV_MSG_FC 3
239 #define EFI_DEV_MSG_1394 4
240 #define EFI_DEV_MSG_USB 5
241 #define EFI_DEV_MSG_USB_CLASS 15
242 #define EFI_DEV_MSG_I20 6
243 #define EFI_DEV_MSG_MAC 11
244 #define EFI_DEV_MSG_IPV4 12
245 #define EFI_DEV_MSG_IPV6 13
246 #define EFI_DEV_MSG_INFINIBAND 9
247 #define EFI_DEV_MSG_UART 14
248 #define EFI_DEV_MSG_VENDOR 10
249 #define EFI_DEV_MEDIA 0x04
250 #define EFI_DEV_MEDIA_HARD_DRIVE 1
251 #define EFI_DEV_MEDIA_CDROM 2
252 #define EFI_DEV_MEDIA_VENDOR 3
253 #define EFI_DEV_MEDIA_FILE 4
254 #define EFI_DEV_MEDIA_PROTOCOL 5
255 #define EFI_DEV_MEDIA_REL_OFFSET 8
256 #define EFI_DEV_BIOS_BOOT 0x05
257 #define EFI_DEV_END_PATH 0x7F
258 #define EFI_DEV_END_PATH2 0xFF
259 #define EFI_DEV_END_INSTANCE 0x01
260 #define EFI_DEV_END_ENTIRE 0xFF
261 
262 struct efi_generic_dev_path {
263 	u8 type;
264 	u8 sub_type;
265 	u16 length;
266 } __packed;
267 
268 struct efi_acpi_dev_path {
269 	struct efi_generic_dev_path header;
270 	u32 hid;
271 	u32 uid;
272 } __packed;
273 
274 struct efi_pci_dev_path {
275 	struct efi_generic_dev_path header;
276 	u8 fn;
277 	u8 dev;
278 } __packed;
279 
280 struct efi_vendor_dev_path {
281 	struct efi_generic_dev_path header;
282 	efi_guid_t vendorguid;
283 	u8 vendordata[];
284 } __packed;
285 
286 struct efi_rel_offset_dev_path {
287 	struct efi_generic_dev_path header;
288 	u32 reserved;
289 	u64 starting_offset;
290 	u64 ending_offset;
291 } __packed;
292 
293 struct efi_mem_mapped_dev_path {
294 	struct efi_generic_dev_path header;
295 	u32 memory_type;
296 	u64 starting_addr;
297 	u64 ending_addr;
298 } __packed;
299 
300 struct efi_file_path_dev_path {
301 	struct efi_generic_dev_path header;
302 	efi_char16_t filename[];
303 } __packed;
304 
305 struct efi_dev_path {
306 	union {
307 		struct efi_generic_dev_path header;
308 		struct efi_acpi_dev_path acpi;
309 		struct efi_pci_dev_path pci;
310 		struct efi_vendor_dev_path vendor;
311 		struct efi_rel_offset_dev_path rel_offset;
312 	};
313 } __packed;
314 
315 // struct device *efi_get_device_by_path(const struct efi_dev_path **node,
316 // 				      size_t *len);
317 
318 static inline void memrange_efi_to_native(u64 *addr, u64 *npages)
319 {
320 	*npages = PFN_UP(*addr + (*npages << EFI_PAGE_SHIFT)) - PFN_DOWN(*addr);
321 	*addr &= PAGE_MASK;
322 }
323