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