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