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