1 /* SPDX-License-Identifier: GPL-2.0 */
2 
3 #ifndef _DRIVERS_FIRMWARE_EFI_EFISTUB_H
4 #define _DRIVERS_FIRMWARE_EFI_EFISTUB_H
5 
6 #include <linux/compiler.h>
7 #include <linux/efi.h>
8 #include <linux/kernel.h>
9 #include <linux/kern_levels.h>
10 #include <linux/types.h>
11 #include <asm/efi.h>
12 
13 /*
14  * __init annotations should not be used in the EFI stub, since the code is
15  * either included in the decompressor (x86, ARM) where they have no effect,
16  * or the whole stub is __init annotated at the section level (arm64), by
17  * renaming the sections, in which case the __init annotation will be
18  * redundant, and will result in section names like .init.init.text, and our
19  * linker script does not expect that.
20  */
21 #undef __init
22 
23 /*
24  * Allow the platform to override the allocation granularity: this allows
25  * systems that have the capability to run with a larger page size to deal
26  * with the allocations for initrd and fdt more efficiently.
27  */
28 #ifndef EFI_ALLOC_ALIGN
29 #define EFI_ALLOC_ALIGN		EFI_PAGE_SIZE
30 #endif
31 
32 #ifndef EFI_ALLOC_LIMIT
33 #define EFI_ALLOC_LIMIT		ULONG_MAX
34 #endif
35 
36 extern bool efi_nochunk;
37 extern bool efi_nokaslr;
38 extern int efi_loglevel;
39 extern bool efi_novamap;
40 
41 extern const efi_system_table_t *efi_system_table;
42 
43 typedef union efi_dxe_services_table efi_dxe_services_table_t;
44 extern const efi_dxe_services_table_t *efi_dxe_table;
45 
46 efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,
47 				   efi_system_table_t *sys_table_arg);
48 
49 #ifndef ARCH_HAS_EFISTUB_WRAPPERS
50 
51 #define efi_is_native()			(true)
52 #define efi_table_attr(inst, attr)	(inst)->attr
53 #define efi_fn_call(inst, func, ...)	(inst)->func(__VA_ARGS__)
54 
55 #endif
56 
57 #define efi_call_proto(inst, func, ...) ({			\
58 	__typeof__(inst) __inst = (inst);			\
59 	efi_fn_call(__inst, func, __inst, ##__VA_ARGS__);	\
60 })
61 #define efi_bs_call(func, ...) \
62 	efi_fn_call(efi_table_attr(efi_system_table, boottime), func, ##__VA_ARGS__)
63 #define efi_rt_call(func, ...) \
64 	efi_fn_call(efi_table_attr(efi_system_table, runtime), func, ##__VA_ARGS__)
65 #define efi_dxe_call(func, ...) \
66 	efi_fn_call(efi_dxe_table, func, ##__VA_ARGS__)
67 
68 #define efi_info(fmt, ...) \
69 	efi_printk(KERN_INFO fmt, ##__VA_ARGS__)
70 #define efi_warn(fmt, ...) \
71 	efi_printk(KERN_WARNING "WARNING: " fmt, ##__VA_ARGS__)
72 #define efi_err(fmt, ...) \
73 	efi_printk(KERN_ERR "ERROR: " fmt, ##__VA_ARGS__)
74 #define efi_debug(fmt, ...) \
75 	efi_printk(KERN_DEBUG "DEBUG: " fmt, ##__VA_ARGS__)
76 
77 #define efi_printk_once(fmt, ...) 		\
78 ({						\
79 	static bool __print_once;		\
80 	bool __ret_print_once = !__print_once;	\
81 						\
82 	if (!__print_once) {			\
83 		__print_once = true;		\
84 		efi_printk(fmt, ##__VA_ARGS__);	\
85 	}					\
86 	__ret_print_once;			\
87 })
88 
89 #define efi_info_once(fmt, ...) \
90 	efi_printk_once(KERN_INFO fmt, ##__VA_ARGS__)
91 #define efi_warn_once(fmt, ...) \
92 	efi_printk_once(KERN_WARNING "WARNING: " fmt, ##__VA_ARGS__)
93 #define efi_err_once(fmt, ...) \
94 	efi_printk_once(KERN_ERR "ERROR: " fmt, ##__VA_ARGS__)
95 #define efi_debug_once(fmt, ...) \
96 	efi_printk_once(KERN_DEBUG "DEBUG: " fmt, ##__VA_ARGS__)
97 
98 /* Helper macros for the usual case of using simple C variables: */
99 #ifndef fdt_setprop_inplace_var
100 #define fdt_setprop_inplace_var(fdt, node_offset, name, var) \
101 	fdt_setprop_inplace((fdt), (node_offset), (name), &(var), sizeof(var))
102 #endif
103 
104 #ifndef fdt_setprop_var
105 #define fdt_setprop_var(fdt, node_offset, name, var) \
106 	fdt_setprop((fdt), (node_offset), (name), &(var), sizeof(var))
107 #endif
108 
109 #define get_efi_var(name, vendor, ...)				\
110 	efi_rt_call(get_variable, (efi_char16_t *)(name),	\
111 		    (efi_guid_t *)(vendor), __VA_ARGS__)
112 
113 #define set_efi_var(name, vendor, ...)				\
114 	efi_rt_call(set_variable, (efi_char16_t *)(name),	\
115 		    (efi_guid_t *)(vendor), __VA_ARGS__)
116 
117 #define efi_get_handle_at(array, idx)					\
118 	(efi_is_native() ? (array)[idx] 				\
119 		: (efi_handle_t)(unsigned long)((u32 *)(array))[idx])
120 
121 #define efi_get_handle_num(size)					\
122 	((size) / (efi_is_native() ? sizeof(efi_handle_t) : sizeof(u32)))
123 
124 #define for_each_efi_handle(handle, array, size, i)			\
125 	for (i = 0;							\
126 	     i < efi_get_handle_num(size) &&				\
127 		((handle = efi_get_handle_at((array), i)) || true);	\
128 	     i++)
129 
130 static inline
131 void efi_set_u64_split(u64 data, u32 *lo, u32 *hi)
132 {
133 	*lo = lower_32_bits(data);
134 	*hi = upper_32_bits(data);
135 }
136 
137 /*
138  * Allocation types for calls to boottime->allocate_pages.
139  */
140 #define EFI_ALLOCATE_ANY_PAGES		0
141 #define EFI_ALLOCATE_MAX_ADDRESS	1
142 #define EFI_ALLOCATE_ADDRESS		2
143 #define EFI_MAX_ALLOCATE_TYPE		3
144 
145 /*
146  * The type of search to perform when calling boottime->locate_handle
147  */
148 #define EFI_LOCATE_ALL_HANDLES			0
149 #define EFI_LOCATE_BY_REGISTER_NOTIFY		1
150 #define EFI_LOCATE_BY_PROTOCOL			2
151 
152 /*
153  * boottime->stall takes the time period in microseconds
154  */
155 #define EFI_USEC_PER_SEC		1000000
156 
157 /*
158  * boottime->set_timer takes the time in 100ns units
159  */
160 #define EFI_100NSEC_PER_USEC	((u64)10)
161 
162 /*
163  * An efi_boot_memmap is used by efi_get_memory_map() to return the
164  * EFI memory map in a dynamically allocated buffer.
165  *
166  * The buffer allocated for the EFI memory map includes extra room for
167  * a minimum of EFI_MMAP_NR_SLACK_SLOTS additional EFI memory descriptors.
168  * This facilitates the reuse of the EFI memory map buffer when a second
169  * call to ExitBootServices() is needed because of intervening changes to
170  * the EFI memory map. Other related structures, e.g. x86 e820ext, need
171  * to factor in this headroom requirement as well.
172  */
173 #define EFI_MMAP_NR_SLACK_SLOTS	8
174 
175 typedef struct efi_generic_dev_path efi_device_path_protocol_t;
176 
177 union efi_device_path_to_text_protocol {
178 	struct {
179 		efi_char16_t *(__efiapi *convert_device_node_to_text)(
180 					const efi_device_path_protocol_t *,
181 					bool, bool);
182 		efi_char16_t *(__efiapi *convert_device_path_to_text)(
183 					const efi_device_path_protocol_t *,
184 					bool, bool);
185 	};
186 	struct {
187 		u32 convert_device_node_to_text;
188 		u32 convert_device_path_to_text;
189 	} mixed_mode;
190 };
191 
192 typedef union efi_device_path_to_text_protocol efi_device_path_to_text_protocol_t;
193 
194 union efi_device_path_from_text_protocol {
195 	struct {
196 		efi_device_path_protocol_t *
197 			(__efiapi *convert_text_to_device_node)(const efi_char16_t *);
198 		efi_device_path_protocol_t *
199 			(__efiapi *convert_text_to_device_path)(const efi_char16_t *);
200 	};
201 	struct {
202 		u32 convert_text_to_device_node;
203 		u32 convert_text_to_device_path;
204 	} mixed_mode;
205 };
206 
207 typedef union efi_device_path_from_text_protocol efi_device_path_from_text_protocol_t;
208 
209 typedef void *efi_event_t;
210 /* Note that notifications won't work in mixed mode */
211 typedef void (__efiapi *efi_event_notify_t)(efi_event_t, void *);
212 
213 #define EFI_EVT_TIMER		0x80000000U
214 #define EFI_EVT_RUNTIME		0x40000000U
215 #define EFI_EVT_NOTIFY_WAIT	0x00000100U
216 #define EFI_EVT_NOTIFY_SIGNAL	0x00000200U
217 
218 /**
219  * efi_set_event_at() - add event to events array
220  *
221  * @events:	array of UEFI events
222  * @ids:	index where to put the event in the array
223  * @event:	event to add to the aray
224  *
225  * boottime->wait_for_event() takes an array of events as input.
226  * Provide a helper to set it up correctly for mixed mode.
227  */
228 static inline
229 void efi_set_event_at(efi_event_t *events, size_t idx, efi_event_t event)
230 {
231 	if (efi_is_native())
232 		events[idx] = event;
233 	else
234 		((u32 *)events)[idx] = (u32)(unsigned long)event;
235 }
236 
237 #define EFI_TPL_APPLICATION	4
238 #define EFI_TPL_CALLBACK	8
239 #define EFI_TPL_NOTIFY		16
240 #define EFI_TPL_HIGH_LEVEL	31
241 
242 typedef enum {
243 	EfiTimerCancel,
244 	EfiTimerPeriodic,
245 	EfiTimerRelative
246 } EFI_TIMER_DELAY;
247 
248 /*
249  * EFI Boot Services table
250  */
251 union efi_boot_services {
252 	struct {
253 		efi_table_hdr_t hdr;
254 		void *raise_tpl;
255 		void *restore_tpl;
256 		efi_status_t (__efiapi *allocate_pages)(int, int, unsigned long,
257 							efi_physical_addr_t *);
258 		efi_status_t (__efiapi *free_pages)(efi_physical_addr_t,
259 						    unsigned long);
260 		efi_status_t (__efiapi *get_memory_map)(unsigned long *, void *,
261 							unsigned long *,
262 							unsigned long *, u32 *);
263 		efi_status_t (__efiapi *allocate_pool)(int, unsigned long,
264 						       void **);
265 		efi_status_t (__efiapi *free_pool)(void *);
266 		efi_status_t (__efiapi *create_event)(u32, unsigned long,
267 						      efi_event_notify_t, void *,
268 						      efi_event_t *);
269 		efi_status_t (__efiapi *set_timer)(efi_event_t,
270 						  EFI_TIMER_DELAY, u64);
271 		efi_status_t (__efiapi *wait_for_event)(unsigned long,
272 							efi_event_t *,
273 							unsigned long *);
274 		void *signal_event;
275 		efi_status_t (__efiapi *close_event)(efi_event_t);
276 		void *check_event;
277 		void *install_protocol_interface;
278 		void *reinstall_protocol_interface;
279 		void *uninstall_protocol_interface;
280 		efi_status_t (__efiapi *handle_protocol)(efi_handle_t,
281 							 efi_guid_t *, void **);
282 		void *__reserved;
283 		void *register_protocol_notify;
284 		efi_status_t (__efiapi *locate_handle)(int, efi_guid_t *,
285 						       void *, unsigned long *,
286 						       efi_handle_t *);
287 		efi_status_t (__efiapi *locate_device_path)(efi_guid_t *,
288 							    efi_device_path_protocol_t **,
289 							    efi_handle_t *);
290 		efi_status_t (__efiapi *install_configuration_table)(efi_guid_t *,
291 								     void *);
292 		efi_status_t (__efiapi *load_image)(bool, efi_handle_t,
293 						    efi_device_path_protocol_t *,
294 						    void *, unsigned long,
295 						    efi_handle_t *);
296 		efi_status_t (__efiapi *start_image)(efi_handle_t, unsigned long *,
297 						     efi_char16_t **);
298 		efi_status_t __noreturn (__efiapi *exit)(efi_handle_t,
299 							 efi_status_t,
300 							 unsigned long,
301 							 efi_char16_t *);
302 		efi_status_t (__efiapi *unload_image)(efi_handle_t);
303 		efi_status_t (__efiapi *exit_boot_services)(efi_handle_t,
304 							    unsigned long);
305 		void *get_next_monotonic_count;
306 		efi_status_t (__efiapi *stall)(unsigned long);
307 		void *set_watchdog_timer;
308 		void *connect_controller;
309 		efi_status_t (__efiapi *disconnect_controller)(efi_handle_t,
310 							       efi_handle_t,
311 							       efi_handle_t);
312 		void *open_protocol;
313 		void *close_protocol;
314 		void *open_protocol_information;
315 		void *protocols_per_handle;
316 		void *locate_handle_buffer;
317 		efi_status_t (__efiapi *locate_protocol)(efi_guid_t *, void *,
318 							 void **);
319 		efi_status_t (__efiapi *install_multiple_protocol_interfaces)(efi_handle_t *, ...);
320 		efi_status_t (__efiapi *uninstall_multiple_protocol_interfaces)(efi_handle_t, ...);
321 		void *calculate_crc32;
322 		void (__efiapi *copy_mem)(void *, const void *, unsigned long);
323 		void (__efiapi *set_mem)(void *, unsigned long, unsigned char);
324 		void *create_event_ex;
325 	};
326 	struct {
327 		efi_table_hdr_t hdr;
328 		u32 raise_tpl;
329 		u32 restore_tpl;
330 		u32 allocate_pages;
331 		u32 free_pages;
332 		u32 get_memory_map;
333 		u32 allocate_pool;
334 		u32 free_pool;
335 		u32 create_event;
336 		u32 set_timer;
337 		u32 wait_for_event;
338 		u32 signal_event;
339 		u32 close_event;
340 		u32 check_event;
341 		u32 install_protocol_interface;
342 		u32 reinstall_protocol_interface;
343 		u32 uninstall_protocol_interface;
344 		u32 handle_protocol;
345 		u32 __reserved;
346 		u32 register_protocol_notify;
347 		u32 locate_handle;
348 		u32 locate_device_path;
349 		u32 install_configuration_table;
350 		u32 load_image;
351 		u32 start_image;
352 		u32 exit;
353 		u32 unload_image;
354 		u32 exit_boot_services;
355 		u32 get_next_monotonic_count;
356 		u32 stall;
357 		u32 set_watchdog_timer;
358 		u32 connect_controller;
359 		u32 disconnect_controller;
360 		u32 open_protocol;
361 		u32 close_protocol;
362 		u32 open_protocol_information;
363 		u32 protocols_per_handle;
364 		u32 locate_handle_buffer;
365 		u32 locate_protocol;
366 		u32 install_multiple_protocol_interfaces;
367 		u32 uninstall_multiple_protocol_interfaces;
368 		u32 calculate_crc32;
369 		u32 copy_mem;
370 		u32 set_mem;
371 		u32 create_event_ex;
372 	} mixed_mode;
373 };
374 
375 typedef enum {
376 	EfiGcdMemoryTypeNonExistent,
377 	EfiGcdMemoryTypeReserved,
378 	EfiGcdMemoryTypeSystemMemory,
379 	EfiGcdMemoryTypeMemoryMappedIo,
380 	EfiGcdMemoryTypePersistent,
381 	EfiGcdMemoryTypeMoreReliable,
382 	EfiGcdMemoryTypeMaximum
383 } efi_gcd_memory_type_t;
384 
385 typedef struct {
386 	efi_physical_addr_t base_address;
387 	u64 length;
388 	u64 capabilities;
389 	u64 attributes;
390 	efi_gcd_memory_type_t gcd_memory_type;
391 	void *image_handle;
392 	void *device_handle;
393 } efi_gcd_memory_space_desc_t;
394 
395 /*
396  * EFI DXE Services table
397  */
398 union efi_dxe_services_table {
399 	struct {
400 		efi_table_hdr_t hdr;
401 		void *add_memory_space;
402 		void *allocate_memory_space;
403 		void *free_memory_space;
404 		void *remove_memory_space;
405 		efi_status_t (__efiapi *get_memory_space_descriptor)(efi_physical_addr_t,
406 								     efi_gcd_memory_space_desc_t *);
407 		efi_status_t (__efiapi *set_memory_space_attributes)(efi_physical_addr_t,
408 								     u64, u64);
409 		void *get_memory_space_map;
410 		void *add_io_space;
411 		void *allocate_io_space;
412 		void *free_io_space;
413 		void *remove_io_space;
414 		void *get_io_space_descriptor;
415 		void *get_io_space_map;
416 		void *dispatch;
417 		void *schedule;
418 		void *trust;
419 		void *process_firmware_volume;
420 		void *set_memory_space_capabilities;
421 	};
422 	struct {
423 		efi_table_hdr_t hdr;
424 		u32 add_memory_space;
425 		u32 allocate_memory_space;
426 		u32 free_memory_space;
427 		u32 remove_memory_space;
428 		u32 get_memory_space_descriptor;
429 		u32 set_memory_space_attributes;
430 		u32 get_memory_space_map;
431 		u32 add_io_space;
432 		u32 allocate_io_space;
433 		u32 free_io_space;
434 		u32 remove_io_space;
435 		u32 get_io_space_descriptor;
436 		u32 get_io_space_map;
437 		u32 dispatch;
438 		u32 schedule;
439 		u32 trust;
440 		u32 process_firmware_volume;
441 		u32 set_memory_space_capabilities;
442 	} mixed_mode;
443 };
444 
445 typedef union efi_memory_attribute_protocol efi_memory_attribute_protocol_t;
446 
447 union efi_memory_attribute_protocol {
448 	struct {
449 		efi_status_t (__efiapi *get_memory_attributes)(
450 			efi_memory_attribute_protocol_t *, efi_physical_addr_t, u64, u64 *);
451 
452 		efi_status_t (__efiapi *set_memory_attributes)(
453 			efi_memory_attribute_protocol_t *, efi_physical_addr_t, u64, u64);
454 
455 		efi_status_t (__efiapi *clear_memory_attributes)(
456 			efi_memory_attribute_protocol_t *, efi_physical_addr_t, u64, u64);
457 	};
458 	struct {
459 		u32 get_memory_attributes;
460 		u32 set_memory_attributes;
461 		u32 clear_memory_attributes;
462 	} mixed_mode;
463 };
464 
465 typedef union efi_uga_draw_protocol efi_uga_draw_protocol_t;
466 
467 union efi_uga_draw_protocol {
468 	struct {
469 		efi_status_t (__efiapi *get_mode)(efi_uga_draw_protocol_t *,
470 						  u32*, u32*, u32*, u32*);
471 		void *set_mode;
472 		void *blt;
473 	};
474 	struct {
475 		u32 get_mode;
476 		u32 set_mode;
477 		u32 blt;
478 	} mixed_mode;
479 };
480 
481 typedef struct {
482 	u16 scan_code;
483 	efi_char16_t unicode_char;
484 } efi_input_key_t;
485 
486 union efi_simple_text_input_protocol {
487 	struct {
488 		void *reset;
489 		efi_status_t (__efiapi *read_keystroke)(efi_simple_text_input_protocol_t *,
490 							efi_input_key_t *);
491 		efi_event_t wait_for_key;
492 	};
493 	struct {
494 		u32 reset;
495 		u32 read_keystroke;
496 		u32 wait_for_key;
497 	} mixed_mode;
498 };
499 
500 efi_status_t efi_wait_for_key(unsigned long usec, efi_input_key_t *key);
501 
502 union efi_simple_text_output_protocol {
503 	struct {
504 		void *reset;
505 		efi_status_t (__efiapi *output_string)(efi_simple_text_output_protocol_t *,
506 						       efi_char16_t *);
507 		void *test_string;
508 	};
509 	struct {
510 		u32 reset;
511 		u32 output_string;
512 		u32 test_string;
513 	} mixed_mode;
514 };
515 
516 #define PIXEL_RGB_RESERVED_8BIT_PER_COLOR		0
517 #define PIXEL_BGR_RESERVED_8BIT_PER_COLOR		1
518 #define PIXEL_BIT_MASK					2
519 #define PIXEL_BLT_ONLY					3
520 #define PIXEL_FORMAT_MAX				4
521 
522 typedef struct {
523 	u32 red_mask;
524 	u32 green_mask;
525 	u32 blue_mask;
526 	u32 reserved_mask;
527 } efi_pixel_bitmask_t;
528 
529 typedef struct {
530 	u32 version;
531 	u32 horizontal_resolution;
532 	u32 vertical_resolution;
533 	int pixel_format;
534 	efi_pixel_bitmask_t pixel_information;
535 	u32 pixels_per_scan_line;
536 } efi_graphics_output_mode_info_t;
537 
538 typedef union efi_graphics_output_protocol_mode efi_graphics_output_protocol_mode_t;
539 
540 union efi_graphics_output_protocol_mode {
541 	struct {
542 		u32 max_mode;
543 		u32 mode;
544 		efi_graphics_output_mode_info_t *info;
545 		unsigned long size_of_info;
546 		efi_physical_addr_t frame_buffer_base;
547 		unsigned long frame_buffer_size;
548 	};
549 	struct {
550 		u32 max_mode;
551 		u32 mode;
552 		u32 info;
553 		u32 size_of_info;
554 		u64 frame_buffer_base;
555 		u32 frame_buffer_size;
556 	} mixed_mode;
557 };
558 
559 typedef union efi_graphics_output_protocol efi_graphics_output_protocol_t;
560 
561 union efi_graphics_output_protocol {
562 	struct {
563 		efi_status_t (__efiapi *query_mode)(efi_graphics_output_protocol_t *,
564 						    u32, unsigned long *,
565 						    efi_graphics_output_mode_info_t **);
566 		efi_status_t (__efiapi *set_mode)  (efi_graphics_output_protocol_t *, u32);
567 		void *blt;
568 		efi_graphics_output_protocol_mode_t *mode;
569 	};
570 	struct {
571 		u32 query_mode;
572 		u32 set_mode;
573 		u32 blt;
574 		u32 mode;
575 	} mixed_mode;
576 };
577 
578 typedef union {
579 	struct {
580 		u32			revision;
581 		efi_handle_t		parent_handle;
582 		efi_system_table_t	*system_table;
583 		efi_handle_t		device_handle;
584 		void			*file_path;
585 		void			*reserved;
586 		u32			load_options_size;
587 		void			*load_options;
588 		void			*image_base;
589 		__aligned_u64		image_size;
590 		unsigned int		image_code_type;
591 		unsigned int		image_data_type;
592 		efi_status_t		(__efiapi *unload)(efi_handle_t image_handle);
593 	};
594 	struct {
595 		u32		revision;
596 		u32		parent_handle;
597 		u32		system_table;
598 		u32		device_handle;
599 		u32		file_path;
600 		u32		reserved;
601 		u32		load_options_size;
602 		u32		load_options;
603 		u32		image_base;
604 		__aligned_u64	image_size;
605 		u32		image_code_type;
606 		u32		image_data_type;
607 		u32		unload;
608 	} mixed_mode;
609 } efi_loaded_image_t;
610 
611 typedef struct {
612 	u64			size;
613 	u64			file_size;
614 	u64			phys_size;
615 	efi_time_t		create_time;
616 	efi_time_t		last_access_time;
617 	efi_time_t		modification_time;
618 	__aligned_u64		attribute;
619 	efi_char16_t		filename[];
620 } efi_file_info_t;
621 
622 typedef union efi_file_protocol efi_file_protocol_t;
623 
624 union efi_file_protocol {
625 	struct {
626 		u64		revision;
627 		efi_status_t	(__efiapi *open)	(efi_file_protocol_t *,
628 							 efi_file_protocol_t **,
629 							 efi_char16_t *, u64,
630 							 u64);
631 		efi_status_t	(__efiapi *close)	(efi_file_protocol_t *);
632 		efi_status_t	(__efiapi *delete)	(efi_file_protocol_t *);
633 		efi_status_t	(__efiapi *read)	(efi_file_protocol_t *,
634 							 unsigned long *,
635 							 void *);
636 		efi_status_t	(__efiapi *write)	(efi_file_protocol_t *,
637 							 unsigned long, void *);
638 		efi_status_t	(__efiapi *get_position)(efi_file_protocol_t *,
639 							 u64 *);
640 		efi_status_t	(__efiapi *set_position)(efi_file_protocol_t *,
641 							 u64);
642 		efi_status_t	(__efiapi *get_info)	(efi_file_protocol_t *,
643 							 efi_guid_t *,
644 							 unsigned long *,
645 							 void *);
646 		efi_status_t	(__efiapi *set_info)	(efi_file_protocol_t *,
647 							 efi_guid_t *,
648 							 unsigned long,
649 							 void *);
650 		efi_status_t	(__efiapi *flush)	(efi_file_protocol_t *);
651 	};
652 	struct {
653 		u64 revision;
654 		u32 open;
655 		u32 close;
656 		u32 delete;
657 		u32 read;
658 		u32 write;
659 		u32 get_position;
660 		u32 set_position;
661 		u32 get_info;
662 		u32 set_info;
663 		u32 flush;
664 	} mixed_mode;
665 };
666 
667 typedef union efi_simple_file_system_protocol efi_simple_file_system_protocol_t;
668 
669 union efi_simple_file_system_protocol {
670 	struct {
671 		u64		revision;
672 		efi_status_t	(__efiapi *open_volume)(efi_simple_file_system_protocol_t *,
673 							efi_file_protocol_t **);
674 	};
675 	struct {
676 		u64 revision;
677 		u32 open_volume;
678 	} mixed_mode;
679 };
680 
681 #define EFI_FILE_MODE_READ	0x0000000000000001
682 #define EFI_FILE_MODE_WRITE	0x0000000000000002
683 #define EFI_FILE_MODE_CREATE	0x8000000000000000
684 
685 typedef enum {
686 	EfiPciIoWidthUint8,
687 	EfiPciIoWidthUint16,
688 	EfiPciIoWidthUint32,
689 	EfiPciIoWidthUint64,
690 	EfiPciIoWidthFifoUint8,
691 	EfiPciIoWidthFifoUint16,
692 	EfiPciIoWidthFifoUint32,
693 	EfiPciIoWidthFifoUint64,
694 	EfiPciIoWidthFillUint8,
695 	EfiPciIoWidthFillUint16,
696 	EfiPciIoWidthFillUint32,
697 	EfiPciIoWidthFillUint64,
698 	EfiPciIoWidthMaximum
699 } EFI_PCI_IO_PROTOCOL_WIDTH;
700 
701 typedef enum {
702 	EfiPciIoAttributeOperationGet,
703 	EfiPciIoAttributeOperationSet,
704 	EfiPciIoAttributeOperationEnable,
705 	EfiPciIoAttributeOperationDisable,
706 	EfiPciIoAttributeOperationSupported,
707     EfiPciIoAttributeOperationMaximum
708 } EFI_PCI_IO_PROTOCOL_ATTRIBUTE_OPERATION;
709 
710 typedef struct {
711 	u32 read;
712 	u32 write;
713 } efi_pci_io_protocol_access_32_t;
714 
715 typedef union efi_pci_io_protocol efi_pci_io_protocol_t;
716 
717 typedef
718 efi_status_t (__efiapi *efi_pci_io_protocol_cfg_t)(efi_pci_io_protocol_t *,
719 						   EFI_PCI_IO_PROTOCOL_WIDTH,
720 						   u32 offset,
721 						   unsigned long count,
722 						   void *buffer);
723 
724 typedef struct {
725 	void *read;
726 	void *write;
727 } efi_pci_io_protocol_access_t;
728 
729 typedef struct {
730 	efi_pci_io_protocol_cfg_t read;
731 	efi_pci_io_protocol_cfg_t write;
732 } efi_pci_io_protocol_config_access_t;
733 
734 union efi_pci_io_protocol {
735 	struct {
736 		void *poll_mem;
737 		void *poll_io;
738 		efi_pci_io_protocol_access_t mem;
739 		efi_pci_io_protocol_access_t io;
740 		efi_pci_io_protocol_config_access_t pci;
741 		void *copy_mem;
742 		void *map;
743 		void *unmap;
744 		void *allocate_buffer;
745 		void *free_buffer;
746 		void *flush;
747 		efi_status_t (__efiapi *get_location)(efi_pci_io_protocol_t *,
748 						      unsigned long *segment_nr,
749 						      unsigned long *bus_nr,
750 						      unsigned long *device_nr,
751 						      unsigned long *func_nr);
752 		void *attributes;
753 		void *get_bar_attributes;
754 		void *set_bar_attributes;
755 		uint64_t romsize;
756 		void *romimage;
757 	};
758 	struct {
759 		u32 poll_mem;
760 		u32 poll_io;
761 		efi_pci_io_protocol_access_32_t mem;
762 		efi_pci_io_protocol_access_32_t io;
763 		efi_pci_io_protocol_access_32_t pci;
764 		u32 copy_mem;
765 		u32 map;
766 		u32 unmap;
767 		u32 allocate_buffer;
768 		u32 free_buffer;
769 		u32 flush;
770 		u32 get_location;
771 		u32 attributes;
772 		u32 get_bar_attributes;
773 		u32 set_bar_attributes;
774 		u64 romsize;
775 		u32 romimage;
776 	} mixed_mode;
777 };
778 
779 #define EFI_PCI_IO_ATTRIBUTE_ISA_MOTHERBOARD_IO 0x0001
780 #define EFI_PCI_IO_ATTRIBUTE_ISA_IO 0x0002
781 #define EFI_PCI_IO_ATTRIBUTE_VGA_PALETTE_IO 0x0004
782 #define EFI_PCI_IO_ATTRIBUTE_VGA_MEMORY 0x0008
783 #define EFI_PCI_IO_ATTRIBUTE_VGA_IO 0x0010
784 #define EFI_PCI_IO_ATTRIBUTE_IDE_PRIMARY_IO 0x0020
785 #define EFI_PCI_IO_ATTRIBUTE_IDE_SECONDARY_IO 0x0040
786 #define EFI_PCI_IO_ATTRIBUTE_MEMORY_WRITE_COMBINE 0x0080
787 #define EFI_PCI_IO_ATTRIBUTE_IO 0x0100
788 #define EFI_PCI_IO_ATTRIBUTE_MEMORY 0x0200
789 #define EFI_PCI_IO_ATTRIBUTE_BUS_MASTER 0x0400
790 #define EFI_PCI_IO_ATTRIBUTE_MEMORY_CACHED 0x0800
791 #define EFI_PCI_IO_ATTRIBUTE_MEMORY_DISABLE 0x1000
792 #define EFI_PCI_IO_ATTRIBUTE_EMBEDDED_DEVICE 0x2000
793 #define EFI_PCI_IO_ATTRIBUTE_EMBEDDED_ROM 0x4000
794 #define EFI_PCI_IO_ATTRIBUTE_DUAL_ADDRESS_CYCLE 0x8000
795 #define EFI_PCI_IO_ATTRIBUTE_ISA_IO_16 0x10000
796 #define EFI_PCI_IO_ATTRIBUTE_VGA_PALETTE_IO_16 0x20000
797 #define EFI_PCI_IO_ATTRIBUTE_VGA_IO_16 0x40000
798 
799 struct efi_dev_path;
800 
801 typedef union apple_properties_protocol apple_properties_protocol_t;
802 
803 union apple_properties_protocol {
804 	struct {
805 		unsigned long version;
806 		efi_status_t (__efiapi *get)(apple_properties_protocol_t *,
807 					     struct efi_dev_path *,
808 					     efi_char16_t *, void *, u32 *);
809 		efi_status_t (__efiapi *set)(apple_properties_protocol_t *,
810 					     struct efi_dev_path *,
811 					     efi_char16_t *, void *, u32);
812 		efi_status_t (__efiapi *del)(apple_properties_protocol_t *,
813 					     struct efi_dev_path *,
814 					     efi_char16_t *);
815 		efi_status_t (__efiapi *get_all)(apple_properties_protocol_t *,
816 						 void *buffer, u32 *);
817 	};
818 	struct {
819 		u32 version;
820 		u32 get;
821 		u32 set;
822 		u32 del;
823 		u32 get_all;
824 	} mixed_mode;
825 };
826 
827 typedef u32 efi_tcg2_event_log_format;
828 
829 #define INITRD_EVENT_TAG_ID 0x8F3B22ECU
830 #define LOAD_OPTIONS_EVENT_TAG_ID 0x8F3B22EDU
831 #define EV_EVENT_TAG 0x00000006U
832 #define EFI_TCG2_EVENT_HEADER_VERSION	0x1
833 
834 struct efi_tcg2_event {
835 	u32		event_size;
836 	struct {
837 		u32	header_size;
838 		u16	header_version;
839 		u32	pcr_index;
840 		u32	event_type;
841 	} __packed event_header;
842 	/* u8[] event follows here */
843 } __packed;
844 
845 struct efi_tcg2_tagged_event {
846 	u32 tagged_event_id;
847 	u32 tagged_event_data_size;
848 	/* u8  tagged event data follows here */
849 } __packed;
850 
851 typedef struct efi_tcg2_event efi_tcg2_event_t;
852 typedef struct efi_tcg2_tagged_event efi_tcg2_tagged_event_t;
853 typedef union efi_tcg2_protocol efi_tcg2_protocol_t;
854 
855 union efi_tcg2_protocol {
856 	struct {
857 		void *get_capability;
858 		efi_status_t (__efiapi *get_event_log)(efi_tcg2_protocol_t *,
859 						       efi_tcg2_event_log_format,
860 						       efi_physical_addr_t *,
861 						       efi_physical_addr_t *,
862 						       efi_bool_t *);
863 		efi_status_t (__efiapi *hash_log_extend_event)(efi_tcg2_protocol_t *,
864 							       u64,
865 							       efi_physical_addr_t,
866 							       u64,
867 							       const efi_tcg2_event_t *);
868 		void *submit_command;
869 		void *get_active_pcr_banks;
870 		void *set_active_pcr_banks;
871 		void *get_result_of_set_active_pcr_banks;
872 	};
873 	struct {
874 		u32 get_capability;
875 		u32 get_event_log;
876 		u32 hash_log_extend_event;
877 		u32 submit_command;
878 		u32 get_active_pcr_banks;
879 		u32 set_active_pcr_banks;
880 		u32 get_result_of_set_active_pcr_banks;
881 	} mixed_mode;
882 };
883 
884 struct riscv_efi_boot_protocol {
885 	u64 revision;
886 
887 	efi_status_t (__efiapi *get_boot_hartid)(struct riscv_efi_boot_protocol *,
888 						 unsigned long *boot_hartid);
889 };
890 
891 typedef union efi_load_file_protocol efi_load_file_protocol_t;
892 typedef union efi_load_file_protocol efi_load_file2_protocol_t;
893 
894 union efi_load_file_protocol {
895 	struct {
896 		efi_status_t (__efiapi *load_file)(efi_load_file_protocol_t *,
897 						   efi_device_path_protocol_t *,
898 						   bool, unsigned long *, void *);
899 	};
900 	struct {
901 		u32 load_file;
902 	} mixed_mode;
903 };
904 
905 typedef struct {
906 	u32 attributes;
907 	u16 file_path_list_length;
908 	u8 variable_data[];
909 	// efi_char16_t description[];
910 	// efi_device_path_protocol_t file_path_list[];
911 	// u8 optional_data[];
912 } __packed efi_load_option_t;
913 
914 #define EFI_LOAD_OPTION_ACTIVE		0x0001U
915 #define EFI_LOAD_OPTION_FORCE_RECONNECT	0x0002U
916 #define EFI_LOAD_OPTION_HIDDEN		0x0008U
917 #define EFI_LOAD_OPTION_CATEGORY	0x1f00U
918 #define   EFI_LOAD_OPTION_CATEGORY_BOOT	0x0000U
919 #define   EFI_LOAD_OPTION_CATEGORY_APP	0x0100U
920 
921 #define EFI_LOAD_OPTION_BOOT_MASK \
922 	(EFI_LOAD_OPTION_ACTIVE|EFI_LOAD_OPTION_HIDDEN|EFI_LOAD_OPTION_CATEGORY)
923 #define EFI_LOAD_OPTION_MASK (EFI_LOAD_OPTION_FORCE_RECONNECT|EFI_LOAD_OPTION_BOOT_MASK)
924 
925 typedef struct {
926 	u32 attributes;
927 	u16 file_path_list_length;
928 	const efi_char16_t *description;
929 	const efi_device_path_protocol_t *file_path_list;
930 	u32 optional_data_size;
931 	const void *optional_data;
932 } efi_load_option_unpacked_t;
933 
934 void efi_pci_disable_bridge_busmaster(void);
935 
936 typedef efi_status_t (*efi_exit_boot_map_processing)(
937 	struct efi_boot_memmap *map,
938 	void *priv);
939 
940 efi_status_t efi_exit_boot_services(void *handle, void *priv,
941 				    efi_exit_boot_map_processing priv_func);
942 
943 efi_status_t efi_boot_kernel(void *handle, efi_loaded_image_t *image,
944 			     unsigned long kernel_addr, char *cmdline_ptr);
945 
946 void *get_fdt(unsigned long *fdt_size);
947 
948 efi_status_t efi_alloc_virtmap(efi_memory_desc_t **virtmap,
949 			       unsigned long *desc_size, u32 *desc_ver);
950 void efi_get_virtmap(efi_memory_desc_t *memory_map, unsigned long map_size,
951 		     unsigned long desc_size, efi_memory_desc_t *runtime_map,
952 		     int *count);
953 
954 efi_status_t efi_get_random_bytes(unsigned long size, u8 *out);
955 
956 efi_status_t efi_random_alloc(unsigned long size, unsigned long align,
957 			      unsigned long *addr, unsigned long random_seed,
958 			      int memory_type);
959 
960 efi_status_t efi_random_get_seed(void);
961 
962 efi_status_t check_platform_features(void);
963 
964 void *get_efi_config_table(efi_guid_t guid);
965 
966 /* NOTE: These functions do not print a trailing newline after the string */
967 void efi_char16_puts(efi_char16_t *);
968 void efi_puts(const char *str);
969 
970 __printf(1, 2) int efi_printk(char const *fmt, ...);
971 
972 void efi_free(unsigned long size, unsigned long addr);
973 
974 void efi_apply_loadoptions_quirk(const void **load_options, u32 *load_options_size);
975 
976 char *efi_convert_cmdline(efi_loaded_image_t *image, int *cmd_line_len);
977 
978 efi_status_t efi_get_memory_map(struct efi_boot_memmap **map,
979 				bool install_cfg_tbl);
980 
981 efi_status_t efi_allocate_pages(unsigned long size, unsigned long *addr,
982 				unsigned long max);
983 
984 efi_status_t efi_allocate_pages_aligned(unsigned long size, unsigned long *addr,
985 					unsigned long max, unsigned long align,
986 					int memory_type);
987 
988 efi_status_t efi_low_alloc_above(unsigned long size, unsigned long align,
989 				 unsigned long *addr, unsigned long min);
990 
991 efi_status_t efi_relocate_kernel(unsigned long *image_addr,
992 				 unsigned long image_size,
993 				 unsigned long alloc_size,
994 				 unsigned long preferred_addr,
995 				 unsigned long alignment,
996 				 unsigned long min_addr);
997 
998 efi_status_t efi_parse_options(char const *cmdline);
999 
1000 void efi_parse_option_graphics(char *option);
1001 
1002 efi_status_t efi_setup_gop(struct screen_info *si, efi_guid_t *proto,
1003 			   unsigned long size);
1004 
1005 efi_status_t handle_cmdline_files(efi_loaded_image_t *image,
1006 				  const efi_char16_t *optstr,
1007 				  int optstr_size,
1008 				  unsigned long soft_limit,
1009 				  unsigned long hard_limit,
1010 				  unsigned long *load_addr,
1011 				  unsigned long *load_size);
1012 
1013 
1014 static inline efi_status_t efi_load_dtb(efi_loaded_image_t *image,
1015 					unsigned long *load_addr,
1016 					unsigned long *load_size)
1017 {
1018 	return handle_cmdline_files(image, L"dtb=", sizeof(L"dtb=") - 2,
1019 				    ULONG_MAX, ULONG_MAX, load_addr, load_size);
1020 }
1021 
1022 efi_status_t efi_load_initrd(efi_loaded_image_t *image,
1023 			     unsigned long soft_limit,
1024 			     unsigned long hard_limit,
1025 			     const struct linux_efi_initrd **out);
1026 /*
1027  * This function handles the architcture specific differences between arm and
1028  * arm64 regarding where the kernel image must be loaded and any memory that
1029  * must be reserved. On failure it is required to free all
1030  * all allocations it has made.
1031  */
1032 efi_status_t handle_kernel_image(unsigned long *image_addr,
1033 				 unsigned long *image_size,
1034 				 unsigned long *reserve_addr,
1035 				 unsigned long *reserve_size,
1036 				 efi_loaded_image_t *image,
1037 				 efi_handle_t image_handle);
1038 
1039 /* shared entrypoint between the normal stub and the zboot stub */
1040 efi_status_t efi_stub_common(efi_handle_t handle,
1041 			     efi_loaded_image_t *image,
1042 			     unsigned long image_addr,
1043 			     char *cmdline_ptr);
1044 
1045 efi_status_t efi_handle_cmdline(efi_loaded_image_t *image, char **cmdline_ptr);
1046 
1047 asmlinkage void __noreturn efi_enter_kernel(unsigned long entrypoint,
1048 					    unsigned long fdt_addr,
1049 					    unsigned long fdt_size);
1050 
1051 void efi_handle_post_ebs_state(void);
1052 
1053 enum efi_secureboot_mode efi_get_secureboot(void);
1054 
1055 #ifdef CONFIG_RESET_ATTACK_MITIGATION
1056 void efi_enable_reset_attack_mitigation(void);
1057 #else
1058 static inline void
1059 efi_enable_reset_attack_mitigation(void) { }
1060 #endif
1061 
1062 void efi_retrieve_tpm2_eventlog(void);
1063 
1064 struct screen_info *alloc_screen_info(void);
1065 void free_screen_info(struct screen_info *si);
1066 
1067 void efi_cache_sync_image(unsigned long image_base,
1068 			  unsigned long alloc_size,
1069 			  unsigned long code_size);
1070 
1071 struct efi_smbios_record {
1072 	u8	type;
1073 	u8	length;
1074 	u16	handle;
1075 };
1076 
1077 struct efi_smbios_type1_record {
1078 	struct efi_smbios_record	header;
1079 
1080 	u8				manufacturer;
1081 	u8				product_name;
1082 	u8				version;
1083 	u8				serial_number;
1084 	efi_guid_t			uuid;
1085 	u8				wakeup_type;
1086 	u8				sku_number;
1087 	u8				family;
1088 };
1089 
1090 #define efi_get_smbios_string(__type, __name) ({			\
1091 	int size = sizeof(struct efi_smbios_type ## __type ## _record);	\
1092 	int off = offsetof(struct efi_smbios_type ## __type ## _record,	\
1093 			   __name);					\
1094 	__efi_get_smbios_string(__type, off, size);			\
1095 })
1096 
1097 const u8 *__efi_get_smbios_string(u8 type, int offset, int recsize);
1098 
1099 #endif
1100