xref: /kvm-unit-tests/lib/linux/efi.h (revision 85c3c524ebe0a64c15acca8f6cff9d91b2c935f5)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Relevant definitions from linux/efi.h. */
3 
4 #ifndef __LINUX_UEFI_H
5 #define __LINUX_UEFI_H
6 
7 #include "libcflat.h"
8 
9 #ifndef __packed
10 # define __packed		__attribute__((__packed__))
11 #endif
12 
13 #define BITS_PER_LONG 64
14 
15 #define EFI_SUCCESS		0
16 #define EFI_LOAD_ERROR		( 1 | (1UL << (BITS_PER_LONG-1)))
17 #define EFI_INVALID_PARAMETER	( 2 | (1UL << (BITS_PER_LONG-1)))
18 #define EFI_UNSUPPORTED		( 3 | (1UL << (BITS_PER_LONG-1)))
19 #define EFI_BAD_BUFFER_SIZE	( 4 | (1UL << (BITS_PER_LONG-1)))
20 #define EFI_BUFFER_TOO_SMALL	( 5 | (1UL << (BITS_PER_LONG-1)))
21 #define EFI_NOT_READY		( 6 | (1UL << (BITS_PER_LONG-1)))
22 #define EFI_DEVICE_ERROR	( 7 | (1UL << (BITS_PER_LONG-1)))
23 #define EFI_WRITE_PROTECTED	( 8 | (1UL << (BITS_PER_LONG-1)))
24 #define EFI_OUT_OF_RESOURCES	( 9 | (1UL << (BITS_PER_LONG-1)))
25 #define EFI_NOT_FOUND		(14 | (1UL << (BITS_PER_LONG-1)))
26 #define EFI_TIMEOUT		(18 | (1UL << (BITS_PER_LONG-1)))
27 #define EFI_ABORTED		(21 | (1UL << (BITS_PER_LONG-1)))
28 #define EFI_SECURITY_VIOLATION	(26 | (1UL << (BITS_PER_LONG-1)))
29 
30 typedef unsigned long efi_status_t;
31 typedef u8 efi_bool_t;
32 typedef u16 efi_char16_t;		/* UNICODE character */
33 typedef u64 efi_physical_addr_t;
34 typedef void *efi_handle_t;
35 
36 #define __efiapi __attribute__((ms_abi))
37 
38 /*
39  * The UEFI spec and EDK2 reference implementation both define EFI_GUID as
40  * struct { u32 a; u16; b; u16 c; u8 d[8]; }; and so the implied alignment
41  * is 32 bits not 8 bits like our guid_t. In some cases (i.e., on 32-bit ARM),
42  * this means that firmware services invoked by the kernel may assume that
43  * efi_guid_t* arguments are 32-bit aligned, and use memory accessors that
44  * do not tolerate misalignment. So let's set the minimum alignment to 32 bits.
45  *
46  * Note that the UEFI spec as well as some comments in the EDK2 code base
47  * suggest that EFI_GUID should be 64-bit aligned, but this appears to be
48  * a mistake, given that no code seems to exist that actually enforces that
49  * or relies on it.
50  */
51 typedef struct {
52 	u8 b[16];
53 } guid_t __attribute__((aligned(__alignof__(u32))));
54 typedef guid_t efi_guid_t;
55 
56 #define EFI_GUID(a, b, c, d...) (efi_guid_t){ {					\
57 	(a) & 0xff, ((a) >> 8) & 0xff, ((a) >> 16) & 0xff, ((a) >> 24) & 0xff,	\
58 	(b) & 0xff, ((b) >> 8) & 0xff,						\
59 	(c) & 0xff, ((c) >> 8) & 0xff, d } }
60 
61 #define ACPI_TABLE_GUID EFI_GUID(0xeb9d2d30, 0x2d88, 0x11d3, 0x9a, 0x16, 0x00, 0x90, 0x27, 0x3f, 0xc1, 0x4d)
62 
63 #define LOADED_IMAGE_PROTOCOL_GUID EFI_GUID(0x5b1b31a1, 0x9562, 0x11d2,  0x8e, 0x3f, 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b)
64 
65 typedef struct {
66 	efi_guid_t guid;
67 	void *table;
68 } efi_config_table_t;
69 
70 /*
71  * Generic EFI table header
72  */
73 typedef	struct {
74 	u64 signature;
75 	u32 revision;
76 	u32 headersize;
77 	u32 crc32;
78 	u32 reserved;
79 } efi_table_hdr_t;
80 
81 /*
82  * Memory map descriptor:
83  */
84 
85 /* Memory types: */
86 #define EFI_RESERVED_TYPE		 0
87 #define EFI_LOADER_CODE			 1
88 #define EFI_LOADER_DATA			 2
89 #define EFI_BOOT_SERVICES_CODE		 3
90 #define EFI_BOOT_SERVICES_DATA		 4
91 #define EFI_RUNTIME_SERVICES_CODE	 5
92 #define EFI_RUNTIME_SERVICES_DATA	 6
93 #define EFI_CONVENTIONAL_MEMORY		 7
94 #define EFI_UNUSABLE_MEMORY		 8
95 #define EFI_ACPI_RECLAIM_MEMORY		 9
96 #define EFI_ACPI_MEMORY_NVS		10
97 #define EFI_MEMORY_MAPPED_IO		11
98 #define EFI_MEMORY_MAPPED_IO_PORT_SPACE	12
99 #define EFI_PAL_CODE			13
100 #define EFI_PERSISTENT_MEMORY		14
101 #define EFI_MAX_MEMORY_TYPE		15
102 
103 /* Attribute values: */
104 #define EFI_MEMORY_UC		((u64)0x0000000000000001ULL)	/* uncached */
105 #define EFI_MEMORY_WC		((u64)0x0000000000000002ULL)	/* write-coalescing */
106 #define EFI_MEMORY_WT		((u64)0x0000000000000004ULL)	/* write-through */
107 #define EFI_MEMORY_WB		((u64)0x0000000000000008ULL)	/* write-back */
108 #define EFI_MEMORY_UCE		((u64)0x0000000000000010ULL)	/* uncached, exported */
109 #define EFI_MEMORY_WP		((u64)0x0000000000001000ULL)	/* write-protect */
110 #define EFI_MEMORY_RP		((u64)0x0000000000002000ULL)	/* read-protect */
111 #define EFI_MEMORY_XP		((u64)0x0000000000004000ULL)	/* execute-protect */
112 #define EFI_MEMORY_NV		((u64)0x0000000000008000ULL)	/* non-volatile */
113 #define EFI_MEMORY_MORE_RELIABLE \
114 				((u64)0x0000000000010000ULL)	/* higher reliability */
115 #define EFI_MEMORY_RO		((u64)0x0000000000020000ULL)	/* read-only */
116 #define EFI_MEMORY_SP		((u64)0x0000000000040000ULL)	/* soft reserved */
117 #define EFI_MEMORY_CPU_CRYPTO	((u64)0x0000000000080000ULL)	/* supports encryption */
118 #define EFI_MEMORY_RUNTIME	((u64)0x8000000000000000ULL)	/* range requires runtime mapping */
119 #define EFI_MEMORY_DESCRIPTOR_VERSION	1
120 
121 #define EFI_PAGE_SHIFT		12
122 #define EFI_PAGE_SIZE		(1UL << EFI_PAGE_SHIFT)
123 #define EFI_PAGES_MAX		(U64_MAX >> EFI_PAGE_SHIFT)
124 
125 typedef struct {
126 	u32 type;
127 	u32 pad;
128 	u64 phys_addr;
129 	u64 virt_addr;
130 	u64 num_pages;
131 	u64 attribute;
132 } efi_memory_desc_t;
133 
134 typedef struct {
135 	efi_guid_t guid;
136 	u32 headersize;
137 	u32 flags;
138 	u32 imagesize;
139 } efi_capsule_header_t;
140 
141 /*
142  * EFI capsule flags
143  */
144 #define EFI_CAPSULE_PERSIST_ACROSS_RESET	0x00010000
145 #define EFI_CAPSULE_POPULATE_SYSTEM_TABLE	0x00020000
146 #define EFI_CAPSULE_INITIATE_RESET		0x00040000
147 
148 struct capsule_info {
149 	efi_capsule_header_t	header;
150 	efi_capsule_header_t	*capsule;
151 	int			reset_type;
152 	long			index;
153 	size_t			count;
154 	size_t			total_size;
155 	struct page		**pages;
156 	phys_addr_t		*phys;
157 	size_t			page_bytes_remain;
158 };
159 
160 int __efi_capsule_setup_info(struct capsule_info *cap_info);
161 
162 /*
163  * Types and defines for Time Services
164  */
165 #define EFI_TIME_ADJUST_DAYLIGHT 0x1
166 #define EFI_TIME_IN_DAYLIGHT     0x2
167 #define EFI_UNSPECIFIED_TIMEZONE 0x07ff
168 
169 typedef struct {
170 	u16 year;
171 	u8 month;
172 	u8 day;
173 	u8 hour;
174 	u8 minute;
175 	u8 second;
176 	u8 pad1;
177 	u32 nanosecond;
178 	s16 timezone;
179 	u8 daylight;
180 	u8 pad2;
181 } efi_time_t;
182 
183 typedef struct {
184 	u32 resolution;
185 	u32 accuracy;
186 	u8 sets_to_zero;
187 } efi_time_cap_t;
188 
189 typedef void *efi_event_t;
190 /* Note that notifications won't work in mixed mode */
191 typedef void (__efiapi *efi_event_notify_t)(efi_event_t, void *);
192 
193 typedef enum {
194 	EfiTimerCancel,
195 	EfiTimerPeriodic,
196 	EfiTimerRelative
197 } EFI_TIMER_DELAY;
198 
199 /*
200  * EFI Device Path information
201  */
202 #define EFI_DEV_HW			0x01
203 #define  EFI_DEV_PCI				 1
204 #define  EFI_DEV_PCCARD				 2
205 #define  EFI_DEV_MEM_MAPPED			 3
206 #define  EFI_DEV_VENDOR				 4
207 #define  EFI_DEV_CONTROLLER			 5
208 #define EFI_DEV_ACPI			0x02
209 #define   EFI_DEV_BASIC_ACPI			 1
210 #define   EFI_DEV_EXPANDED_ACPI			 2
211 #define EFI_DEV_MSG			0x03
212 #define   EFI_DEV_MSG_ATAPI			 1
213 #define   EFI_DEV_MSG_SCSI			 2
214 #define   EFI_DEV_MSG_FC			 3
215 #define   EFI_DEV_MSG_1394			 4
216 #define   EFI_DEV_MSG_USB			 5
217 #define   EFI_DEV_MSG_USB_CLASS			15
218 #define   EFI_DEV_MSG_I20			 6
219 #define   EFI_DEV_MSG_MAC			11
220 #define   EFI_DEV_MSG_IPV4			12
221 #define   EFI_DEV_MSG_IPV6			13
222 #define   EFI_DEV_MSG_INFINIBAND		 9
223 #define   EFI_DEV_MSG_UART			14
224 #define   EFI_DEV_MSG_VENDOR			10
225 #define EFI_DEV_MEDIA			0x04
226 #define   EFI_DEV_MEDIA_HARD_DRIVE		 1
227 #define   EFI_DEV_MEDIA_CDROM			 2
228 #define   EFI_DEV_MEDIA_VENDOR			 3
229 #define   EFI_DEV_MEDIA_FILE			 4
230 #define   EFI_DEV_MEDIA_PROTOCOL		 5
231 #define EFI_DEV_BIOS_BOOT		0x05
232 #define EFI_DEV_END_PATH		0x7F
233 #define EFI_DEV_END_PATH2		0xFF
234 #define   EFI_DEV_END_INSTANCE			0x01
235 #define   EFI_DEV_END_ENTIRE			0xFF
236 
237 struct efi_generic_dev_path {
238 	u8				type;
239 	u8				sub_type;
240 	u16				length;
241 } __packed;
242 
243 typedef struct efi_generic_dev_path efi_device_path_protocol_t;
244 
245 /*
246  * EFI Boot Services table
247  */
248 typedef struct {
249 	efi_table_hdr_t hdr;
250 	void *raise_tpl;
251 	void *restore_tpl;
252 	efi_status_t(__efiapi *allocate_pages)(int, int, unsigned long,
253 					       efi_physical_addr_t *);
254 	efi_status_t(__efiapi *free_pages)(efi_physical_addr_t,
255 					   unsigned long);
256 	efi_status_t(__efiapi *get_memory_map)(unsigned long *, void *,
257 					       unsigned long *,
258 					       unsigned long *, u32 *);
259 	efi_status_t(__efiapi *allocate_pool)(int, unsigned long,
260 					      void **);
261 	efi_status_t(__efiapi *free_pool)(void *);
262 	efi_status_t(__efiapi *create_event)(u32, unsigned long,
263 					     efi_event_notify_t, void *,
264 					     efi_event_t *);
265 	efi_status_t(__efiapi *set_timer)(efi_event_t,
266 					  EFI_TIMER_DELAY, u64);
267 	efi_status_t(__efiapi *wait_for_event)(unsigned long,
268 					       efi_event_t *,
269 					       unsigned long *);
270 	void *signal_event;
271 	efi_status_t(__efiapi *close_event)(efi_event_t);
272 	void *check_event;
273 	void *install_protocol_interface;
274 	void *reinstall_protocol_interface;
275 	void *uninstall_protocol_interface;
276 	efi_status_t(__efiapi *handle_protocol)(efi_handle_t,
277 						efi_guid_t *, void **);
278 	void *__reserved;
279 	void *register_protocol_notify;
280 	efi_status_t(__efiapi *locate_handle)(int, efi_guid_t *,
281 					      void *, unsigned long *,
282 					      efi_handle_t *);
283 	efi_status_t(__efiapi *locate_device_path)(efi_guid_t *,
284 						   efi_device_path_protocol_t **,
285 						   efi_handle_t *);
286 	efi_status_t(__efiapi *install_configuration_table)(efi_guid_t *,
287 							    void *);
288 	void *load_image;
289 	void *start_image;
290 	efi_status_t(__efiapi *exit)(efi_handle_t,
291 				     efi_status_t,
292 				     unsigned long,
293 				     efi_char16_t *);
294 	void *unload_image;
295 	efi_status_t(__efiapi *exit_boot_services)(efi_handle_t,
296 						   unsigned long);
297 	void *get_next_monotonic_count;
298 	efi_status_t(__efiapi *stall)(unsigned long);
299 	void *set_watchdog_timer;
300 	void *connect_controller;
301 	efi_status_t(__efiapi *disconnect_controller)(efi_handle_t,
302 						      efi_handle_t,
303 						      efi_handle_t);
304 	void *open_protocol;
305 	void *close_protocol;
306 	void *open_protocol_information;
307 	void *protocols_per_handle;
308 	void *locate_handle_buffer;
309 	efi_status_t(__efiapi *locate_protocol)(efi_guid_t *, void *,
310 						void **);
311 	void *install_multiple_protocol_interfaces;
312 	void *uninstall_multiple_protocol_interfaces;
313 	void *calculate_crc32;
314 	void *copy_mem;
315 	void *set_mem;
316 	void *create_event_ex;
317 } efi_boot_services_t;
318 
319 /*
320  * Types and defines for EFI ResetSystem
321  */
322 #define EFI_RESET_COLD 0
323 #define EFI_RESET_WARM 1
324 #define EFI_RESET_SHUTDOWN 2
325 
326 /*
327  * EFI Runtime Services table
328  */
329 #define EFI_RUNTIME_SERVICES_SIGNATURE ((u64)0x5652453544e5552ULL)
330 #define EFI_RUNTIME_SERVICES_REVISION  0x00010000
331 
332 typedef efi_status_t efi_get_time_t (efi_time_t *tm, efi_time_cap_t *tc);
333 typedef efi_status_t efi_set_time_t (efi_time_t *tm);
334 typedef efi_status_t efi_get_wakeup_time_t (efi_bool_t *enabled, efi_bool_t *pending,
335 					    efi_time_t *tm);
336 typedef efi_status_t efi_set_wakeup_time_t (efi_bool_t enabled, efi_time_t *tm);
337 typedef efi_status_t efi_get_variable_t (efi_char16_t *name, efi_guid_t *vendor, u32 *attr,
338 					 unsigned long *data_size, void *data);
339 typedef efi_status_t efi_get_next_variable_t (unsigned long *name_size, efi_char16_t *name,
340 					      efi_guid_t *vendor);
341 typedef efi_status_t efi_set_variable_t (efi_char16_t *name, efi_guid_t *vendor,
342 					 u32 attr, unsigned long data_size,
343 					 void *data);
344 typedef efi_status_t efi_get_next_high_mono_count_t (u32 *count);
345 typedef void efi_reset_system_t (int reset_type, efi_status_t status,
346 				 unsigned long data_size, efi_char16_t *data);
347 typedef efi_status_t efi_set_virtual_address_map_t (unsigned long memory_map_size,
348 						unsigned long descriptor_size,
349 						u32 descriptor_version,
350 						efi_memory_desc_t *virtual_map);
351 typedef efi_status_t efi_query_variable_info_t(u32 attr,
352 					       u64 *storage_space,
353 					       u64 *remaining_space,
354 					       u64 *max_variable_size);
355 typedef efi_status_t efi_update_capsule_t(efi_capsule_header_t **capsules,
356 					  unsigned long count,
357 					  unsigned long sg_list);
358 typedef efi_status_t efi_query_capsule_caps_t(efi_capsule_header_t **capsules,
359 					      unsigned long count,
360 					      u64 *max_size,
361 					      int *reset_type);
362 typedef efi_status_t efi_query_variable_store_t(u32 attributes,
363 						unsigned long size,
364 						bool nonblocking);
365 
366 typedef struct {
367 	efi_table_hdr_t				hdr;
368 	efi_get_time_t __efiapi			*get_time;
369 	efi_set_time_t __efiapi			*set_time;
370 	efi_get_wakeup_time_t __efiapi		*get_wakeup_time;
371 	efi_set_wakeup_time_t __efiapi		*set_wakeup_time;
372 	efi_set_virtual_address_map_t __efiapi	*set_virtual_address_map;
373 	void					*convert_pointer;
374 	efi_get_variable_t __efiapi		*get_variable;
375 	efi_get_next_variable_t __efiapi	*get_next_variable;
376 	efi_set_variable_t __efiapi		*set_variable;
377 	efi_get_next_high_mono_count_t __efiapi	*get_next_high_mono_count;
378 	efi_reset_system_t __efiapi		*reset_system;
379 	efi_update_capsule_t __efiapi		*update_capsule;
380 	efi_query_capsule_caps_t __efiapi	*query_capsule_caps;
381 	efi_query_variable_info_t __efiapi	*query_variable_info;
382 } efi_runtime_services_t;
383 
384 #define EFI_SYSTEM_TABLE_SIGNATURE ((u64)0x5453595320494249ULL)
385 
386 #define EFI_2_30_SYSTEM_TABLE_REVISION  ((2 << 16) | (30))
387 #define EFI_2_20_SYSTEM_TABLE_REVISION  ((2 << 16) | (20))
388 #define EFI_2_10_SYSTEM_TABLE_REVISION  ((2 << 16) | (10))
389 #define EFI_2_00_SYSTEM_TABLE_REVISION  ((2 << 16) | (00))
390 #define EFI_1_10_SYSTEM_TABLE_REVISION  ((1 << 16) | (10))
391 #define EFI_1_02_SYSTEM_TABLE_REVISION  ((1 << 16) | (02))
392 
393 typedef union efi_simple_text_input_protocol efi_simple_text_input_protocol_t;
394 typedef union efi_simple_text_output_protocol efi_simple_text_output_protocol_t;
395 
396 typedef struct {
397 	efi_table_hdr_t hdr;
398 	unsigned long fw_vendor;	/* physical addr of CHAR16 vendor string */
399 	u32 fw_revision;
400 	unsigned long con_in_handle;
401 	efi_simple_text_input_protocol_t *con_in;
402 	unsigned long con_out_handle;
403 	efi_simple_text_output_protocol_t *con_out;
404 	unsigned long stderr_handle;
405 	unsigned long stderr;
406 	efi_runtime_services_t *runtime;
407 	efi_boot_services_t *boottime;
408 	unsigned long nr_tables;
409 	unsigned long tables;
410 } efi_system_table_t;
411 
412 struct efi_boot_memmap {
413 	efi_memory_desc_t       **map;
414 	unsigned long           *map_size;
415 	unsigned long           *desc_size;
416 	u32                     *desc_ver;
417 	unsigned long           *key_ptr;
418 	unsigned long           *buff_size;
419 };
420 
421 #define __aligned_u64 u64 __attribute__((aligned(8)))
422 
423 struct efi_loaded_image_64 {
424 	u32			revision;
425 	efi_handle_t		parent_handle;
426 	efi_system_table_t	*system_table;
427 	efi_handle_t		device_handle;
428 	void			*file_path;
429 	void			*reserved;
430 	u32			load_options_size;
431 	void			*load_options;
432 	void			*image_base;
433 	__aligned_u64		image_size;
434 	unsigned int		image_code_type;
435 	unsigned int		image_data_type;
436 	efi_status_t		(__efiapi * unload)(efi_handle_t image_handle);
437 };
438 
439 #define efi_bs_call(func, ...) efi_system_table->boottime->func(__VA_ARGS__)
440 #define efi_rs_call(func, ...) efi_system_table->runtime->func(__VA_ARGS__)
441 
442 #endif /* __LINUX_UEFI_H */
443