xref: /kvm-unit-tests/lib/linux/efi.h (revision 2c96b77ec9d3b1fcec7525174e23a6240ee05949)
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 typedef struct {
64 	efi_guid_t guid;
65 	void *table;
66 } efi_config_table_t;
67 
68 /*
69  * Generic EFI table header
70  */
71 typedef	struct {
72 	u64 signature;
73 	u32 revision;
74 	u32 headersize;
75 	u32 crc32;
76 	u32 reserved;
77 } efi_table_hdr_t;
78 
79 /*
80  * Memory map descriptor:
81  */
82 
83 /* Memory types: */
84 #define EFI_RESERVED_TYPE		 0
85 #define EFI_LOADER_CODE			 1
86 #define EFI_LOADER_DATA			 2
87 #define EFI_BOOT_SERVICES_CODE		 3
88 #define EFI_BOOT_SERVICES_DATA		 4
89 #define EFI_RUNTIME_SERVICES_CODE	 5
90 #define EFI_RUNTIME_SERVICES_DATA	 6
91 #define EFI_CONVENTIONAL_MEMORY		 7
92 #define EFI_UNUSABLE_MEMORY		 8
93 #define EFI_ACPI_RECLAIM_MEMORY		 9
94 #define EFI_ACPI_MEMORY_NVS		10
95 #define EFI_MEMORY_MAPPED_IO		11
96 #define EFI_MEMORY_MAPPED_IO_PORT_SPACE	12
97 #define EFI_PAL_CODE			13
98 #define EFI_PERSISTENT_MEMORY		14
99 #define EFI_MAX_MEMORY_TYPE		15
100 
101 /* Attribute values: */
102 #define EFI_MEMORY_UC		((u64)0x0000000000000001ULL)	/* uncached */
103 #define EFI_MEMORY_WC		((u64)0x0000000000000002ULL)	/* write-coalescing */
104 #define EFI_MEMORY_WT		((u64)0x0000000000000004ULL)	/* write-through */
105 #define EFI_MEMORY_WB		((u64)0x0000000000000008ULL)	/* write-back */
106 #define EFI_MEMORY_UCE		((u64)0x0000000000000010ULL)	/* uncached, exported */
107 #define EFI_MEMORY_WP		((u64)0x0000000000001000ULL)	/* write-protect */
108 #define EFI_MEMORY_RP		((u64)0x0000000000002000ULL)	/* read-protect */
109 #define EFI_MEMORY_XP		((u64)0x0000000000004000ULL)	/* execute-protect */
110 #define EFI_MEMORY_NV		((u64)0x0000000000008000ULL)	/* non-volatile */
111 #define EFI_MEMORY_MORE_RELIABLE \
112 				((u64)0x0000000000010000ULL)	/* higher reliability */
113 #define EFI_MEMORY_RO		((u64)0x0000000000020000ULL)	/* read-only */
114 #define EFI_MEMORY_SP		((u64)0x0000000000040000ULL)	/* soft reserved */
115 #define EFI_MEMORY_CPU_CRYPTO	((u64)0x0000000000080000ULL)	/* supports encryption */
116 #define EFI_MEMORY_RUNTIME	((u64)0x8000000000000000ULL)	/* range requires runtime mapping */
117 #define EFI_MEMORY_DESCRIPTOR_VERSION	1
118 
119 #define EFI_PAGE_SHIFT		12
120 #define EFI_PAGE_SIZE		(1UL << EFI_PAGE_SHIFT)
121 #define EFI_PAGES_MAX		(U64_MAX >> EFI_PAGE_SHIFT)
122 
123 typedef struct {
124 	u32 type;
125 	u32 pad;
126 	u64 phys_addr;
127 	u64 virt_addr;
128 	u64 num_pages;
129 	u64 attribute;
130 } efi_memory_desc_t;
131 
132 typedef struct {
133 	efi_guid_t guid;
134 	u32 headersize;
135 	u32 flags;
136 	u32 imagesize;
137 } efi_capsule_header_t;
138 
139 /*
140  * EFI capsule flags
141  */
142 #define EFI_CAPSULE_PERSIST_ACROSS_RESET	0x00010000
143 #define EFI_CAPSULE_POPULATE_SYSTEM_TABLE	0x00020000
144 #define EFI_CAPSULE_INITIATE_RESET		0x00040000
145 
146 struct capsule_info {
147 	efi_capsule_header_t	header;
148 	efi_capsule_header_t	*capsule;
149 	int			reset_type;
150 	long			index;
151 	size_t			count;
152 	size_t			total_size;
153 	struct page		**pages;
154 	phys_addr_t		*phys;
155 	size_t			page_bytes_remain;
156 };
157 
158 int __efi_capsule_setup_info(struct capsule_info *cap_info);
159 
160 /*
161  * Types and defines for Time Services
162  */
163 #define EFI_TIME_ADJUST_DAYLIGHT 0x1
164 #define EFI_TIME_IN_DAYLIGHT     0x2
165 #define EFI_UNSPECIFIED_TIMEZONE 0x07ff
166 
167 typedef struct {
168 	u16 year;
169 	u8 month;
170 	u8 day;
171 	u8 hour;
172 	u8 minute;
173 	u8 second;
174 	u8 pad1;
175 	u32 nanosecond;
176 	s16 timezone;
177 	u8 daylight;
178 	u8 pad2;
179 } efi_time_t;
180 
181 typedef struct {
182 	u32 resolution;
183 	u32 accuracy;
184 	u8 sets_to_zero;
185 } efi_time_cap_t;
186 
187 typedef void *efi_event_t;
188 /* Note that notifications won't work in mixed mode */
189 typedef void (__efiapi *efi_event_notify_t)(efi_event_t, void *);
190 
191 typedef enum {
192 	EfiTimerCancel,
193 	EfiTimerPeriodic,
194 	EfiTimerRelative
195 } EFI_TIMER_DELAY;
196 
197 /*
198  * EFI Device Path information
199  */
200 #define EFI_DEV_HW			0x01
201 #define  EFI_DEV_PCI				 1
202 #define  EFI_DEV_PCCARD				 2
203 #define  EFI_DEV_MEM_MAPPED			 3
204 #define  EFI_DEV_VENDOR				 4
205 #define  EFI_DEV_CONTROLLER			 5
206 #define EFI_DEV_ACPI			0x02
207 #define   EFI_DEV_BASIC_ACPI			 1
208 #define   EFI_DEV_EXPANDED_ACPI			 2
209 #define EFI_DEV_MSG			0x03
210 #define   EFI_DEV_MSG_ATAPI			 1
211 #define   EFI_DEV_MSG_SCSI			 2
212 #define   EFI_DEV_MSG_FC			 3
213 #define   EFI_DEV_MSG_1394			 4
214 #define   EFI_DEV_MSG_USB			 5
215 #define   EFI_DEV_MSG_USB_CLASS			15
216 #define   EFI_DEV_MSG_I20			 6
217 #define   EFI_DEV_MSG_MAC			11
218 #define   EFI_DEV_MSG_IPV4			12
219 #define   EFI_DEV_MSG_IPV6			13
220 #define   EFI_DEV_MSG_INFINIBAND		 9
221 #define   EFI_DEV_MSG_UART			14
222 #define   EFI_DEV_MSG_VENDOR			10
223 #define EFI_DEV_MEDIA			0x04
224 #define   EFI_DEV_MEDIA_HARD_DRIVE		 1
225 #define   EFI_DEV_MEDIA_CDROM			 2
226 #define   EFI_DEV_MEDIA_VENDOR			 3
227 #define   EFI_DEV_MEDIA_FILE			 4
228 #define   EFI_DEV_MEDIA_PROTOCOL		 5
229 #define EFI_DEV_BIOS_BOOT		0x05
230 #define EFI_DEV_END_PATH		0x7F
231 #define EFI_DEV_END_PATH2		0xFF
232 #define   EFI_DEV_END_INSTANCE			0x01
233 #define   EFI_DEV_END_ENTIRE			0xFF
234 
235 struct efi_generic_dev_path {
236 	u8				type;
237 	u8				sub_type;
238 	u16				length;
239 } __packed;
240 
241 typedef struct efi_generic_dev_path efi_device_path_protocol_t;
242 
243 /*
244  * EFI Boot Services table
245  */
246 typedef struct {
247 	efi_table_hdr_t hdr;
248 	void *raise_tpl;
249 	void *restore_tpl;
250 	efi_status_t(__efiapi *allocate_pages)(int, int, unsigned long,
251 					       efi_physical_addr_t *);
252 	efi_status_t(__efiapi *free_pages)(efi_physical_addr_t,
253 					   unsigned long);
254 	efi_status_t(__efiapi *get_memory_map)(unsigned long *, void *,
255 					       unsigned long *,
256 					       unsigned long *, u32 *);
257 	efi_status_t(__efiapi *allocate_pool)(int, unsigned long,
258 					      void **);
259 	efi_status_t(__efiapi *free_pool)(void *);
260 	efi_status_t(__efiapi *create_event)(u32, unsigned long,
261 					     efi_event_notify_t, void *,
262 					     efi_event_t *);
263 	efi_status_t(__efiapi *set_timer)(efi_event_t,
264 					  EFI_TIMER_DELAY, u64);
265 	efi_status_t(__efiapi *wait_for_event)(unsigned long,
266 					       efi_event_t *,
267 					       unsigned long *);
268 	void *signal_event;
269 	efi_status_t(__efiapi *close_event)(efi_event_t);
270 	void *check_event;
271 	void *install_protocol_interface;
272 	void *reinstall_protocol_interface;
273 	void *uninstall_protocol_interface;
274 	efi_status_t(__efiapi *handle_protocol)(efi_handle_t,
275 						efi_guid_t *, void **);
276 	void *__reserved;
277 	void *register_protocol_notify;
278 	efi_status_t(__efiapi *locate_handle)(int, efi_guid_t *,
279 					      void *, unsigned long *,
280 					      efi_handle_t *);
281 	efi_status_t(__efiapi *locate_device_path)(efi_guid_t *,
282 						   efi_device_path_protocol_t **,
283 						   efi_handle_t *);
284 	efi_status_t(__efiapi *install_configuration_table)(efi_guid_t *,
285 							    void *);
286 	void *load_image;
287 	void *start_image;
288 	efi_status_t(__efiapi *exit)(efi_handle_t,
289 				     efi_status_t,
290 				     unsigned long,
291 				     efi_char16_t *);
292 	void *unload_image;
293 	efi_status_t(__efiapi *exit_boot_services)(efi_handle_t,
294 						   unsigned long);
295 	void *get_next_monotonic_count;
296 	efi_status_t(__efiapi *stall)(unsigned long);
297 	void *set_watchdog_timer;
298 	void *connect_controller;
299 	efi_status_t(__efiapi *disconnect_controller)(efi_handle_t,
300 						      efi_handle_t,
301 						      efi_handle_t);
302 	void *open_protocol;
303 	void *close_protocol;
304 	void *open_protocol_information;
305 	void *protocols_per_handle;
306 	void *locate_handle_buffer;
307 	efi_status_t(__efiapi *locate_protocol)(efi_guid_t *, void *,
308 						void **);
309 	void *install_multiple_protocol_interfaces;
310 	void *uninstall_multiple_protocol_interfaces;
311 	void *calculate_crc32;
312 	void *copy_mem;
313 	void *set_mem;
314 	void *create_event_ex;
315 } efi_boot_services_t;
316 
317 /*
318  * Types and defines for EFI ResetSystem
319  */
320 #define EFI_RESET_COLD 0
321 #define EFI_RESET_WARM 1
322 #define EFI_RESET_SHUTDOWN 2
323 
324 /*
325  * EFI Runtime Services table
326  */
327 #define EFI_RUNTIME_SERVICES_SIGNATURE ((u64)0x5652453544e5552ULL)
328 #define EFI_RUNTIME_SERVICES_REVISION  0x00010000
329 
330 typedef efi_status_t efi_get_time_t (efi_time_t *tm, efi_time_cap_t *tc);
331 typedef efi_status_t efi_set_time_t (efi_time_t *tm);
332 typedef efi_status_t efi_get_wakeup_time_t (efi_bool_t *enabled, efi_bool_t *pending,
333 					    efi_time_t *tm);
334 typedef efi_status_t efi_set_wakeup_time_t (efi_bool_t enabled, efi_time_t *tm);
335 typedef efi_status_t efi_get_variable_t (efi_char16_t *name, efi_guid_t *vendor, u32 *attr,
336 					 unsigned long *data_size, void *data);
337 typedef efi_status_t efi_get_next_variable_t (unsigned long *name_size, efi_char16_t *name,
338 					      efi_guid_t *vendor);
339 typedef efi_status_t efi_set_variable_t (efi_char16_t *name, efi_guid_t *vendor,
340 					 u32 attr, unsigned long data_size,
341 					 void *data);
342 typedef efi_status_t efi_get_next_high_mono_count_t (u32 *count);
343 typedef void efi_reset_system_t (int reset_type, efi_status_t status,
344 				 unsigned long data_size, efi_char16_t *data);
345 typedef efi_status_t efi_set_virtual_address_map_t (unsigned long memory_map_size,
346 						unsigned long descriptor_size,
347 						u32 descriptor_version,
348 						efi_memory_desc_t *virtual_map);
349 typedef efi_status_t efi_query_variable_info_t(u32 attr,
350 					       u64 *storage_space,
351 					       u64 *remaining_space,
352 					       u64 *max_variable_size);
353 typedef efi_status_t efi_update_capsule_t(efi_capsule_header_t **capsules,
354 					  unsigned long count,
355 					  unsigned long sg_list);
356 typedef efi_status_t efi_query_capsule_caps_t(efi_capsule_header_t **capsules,
357 					      unsigned long count,
358 					      u64 *max_size,
359 					      int *reset_type);
360 typedef efi_status_t efi_query_variable_store_t(u32 attributes,
361 						unsigned long size,
362 						bool nonblocking);
363 
364 typedef struct {
365 	efi_table_hdr_t				hdr;
366 	efi_get_time_t __efiapi			*get_time;
367 	efi_set_time_t __efiapi			*set_time;
368 	efi_get_wakeup_time_t __efiapi		*get_wakeup_time;
369 	efi_set_wakeup_time_t __efiapi		*set_wakeup_time;
370 	efi_set_virtual_address_map_t __efiapi	*set_virtual_address_map;
371 	void					*convert_pointer;
372 	efi_get_variable_t __efiapi		*get_variable;
373 	efi_get_next_variable_t __efiapi	*get_next_variable;
374 	efi_set_variable_t __efiapi		*set_variable;
375 	efi_get_next_high_mono_count_t __efiapi	*get_next_high_mono_count;
376 	efi_reset_system_t __efiapi		*reset_system;
377 	efi_update_capsule_t __efiapi		*update_capsule;
378 	efi_query_capsule_caps_t __efiapi	*query_capsule_caps;
379 	efi_query_variable_info_t __efiapi	*query_variable_info;
380 } efi_runtime_services_t;
381 
382 #define EFI_SYSTEM_TABLE_SIGNATURE ((u64)0x5453595320494249ULL)
383 
384 #define EFI_2_30_SYSTEM_TABLE_REVISION  ((2 << 16) | (30))
385 #define EFI_2_20_SYSTEM_TABLE_REVISION  ((2 << 16) | (20))
386 #define EFI_2_10_SYSTEM_TABLE_REVISION  ((2 << 16) | (10))
387 #define EFI_2_00_SYSTEM_TABLE_REVISION  ((2 << 16) | (00))
388 #define EFI_1_10_SYSTEM_TABLE_REVISION  ((1 << 16) | (10))
389 #define EFI_1_02_SYSTEM_TABLE_REVISION  ((1 << 16) | (02))
390 
391 typedef union efi_simple_text_input_protocol efi_simple_text_input_protocol_t;
392 typedef union efi_simple_text_output_protocol efi_simple_text_output_protocol_t;
393 
394 typedef struct {
395 	efi_table_hdr_t hdr;
396 	unsigned long fw_vendor;	/* physical addr of CHAR16 vendor string */
397 	u32 fw_revision;
398 	unsigned long con_in_handle;
399 	efi_simple_text_input_protocol_t *con_in;
400 	unsigned long con_out_handle;
401 	efi_simple_text_output_protocol_t *con_out;
402 	unsigned long stderr_handle;
403 	unsigned long stderr;
404 	efi_runtime_services_t *runtime;
405 	efi_boot_services_t *boottime;
406 	unsigned long nr_tables;
407 	unsigned long tables;
408 } efi_system_table_t;
409 
410 struct efi_boot_memmap {
411 	efi_memory_desc_t       **map;
412 	unsigned long           *map_size;
413 	unsigned long           *desc_size;
414 	u32                     *desc_ver;
415 	unsigned long           *key_ptr;
416 	unsigned long           *buff_size;
417 };
418 
419 #define efi_bs_call(func, ...) efi_system_table->boottime->func(__VA_ARGS__)
420 #define efi_rs_call(func, ...) efi_system_table->runtime->func(__VA_ARGS__)
421 
422 #endif /* __LINUX_UEFI_H */
423