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 typedef void *efi_event_t;
183 /* Note that notifications won't work in mixed mode */
184 typedef void (__efiapi *efi_event_notify_t)(efi_event_t, void *);
185 
186 #define EFI_EVT_TIMER		0x80000000U
187 #define EFI_EVT_RUNTIME		0x40000000U
188 #define EFI_EVT_NOTIFY_WAIT	0x00000100U
189 #define EFI_EVT_NOTIFY_SIGNAL	0x00000200U
190 
191 /**
192  * efi_set_event_at() - add event to events array
193  *
194  * @events:	array of UEFI events
195  * @ids:	index where to put the event in the array
196  * @event:	event to add to the aray
197  *
198  * boottime->wait_for_event() takes an array of events as input.
199  * Provide a helper to set it up correctly for mixed mode.
200  */
201 static inline
202 void efi_set_event_at(efi_event_t *events, size_t idx, efi_event_t event)
203 {
204 	if (efi_is_native())
205 		events[idx] = event;
206 	else
207 		((u32 *)events)[idx] = (u32)(unsigned long)event;
208 }
209 
210 #define EFI_TPL_APPLICATION	4
211 #define EFI_TPL_CALLBACK	8
212 #define EFI_TPL_NOTIFY		16
213 #define EFI_TPL_HIGH_LEVEL	31
214 
215 typedef enum {
216 	EfiTimerCancel,
217 	EfiTimerPeriodic,
218 	EfiTimerRelative
219 } EFI_TIMER_DELAY;
220 
221 /*
222  * EFI Boot Services table
223  */
224 union efi_boot_services {
225 	struct {
226 		efi_table_hdr_t hdr;
227 		void *raise_tpl;
228 		void *restore_tpl;
229 		efi_status_t (__efiapi *allocate_pages)(int, int, unsigned long,
230 							efi_physical_addr_t *);
231 		efi_status_t (__efiapi *free_pages)(efi_physical_addr_t,
232 						    unsigned long);
233 		efi_status_t (__efiapi *get_memory_map)(unsigned long *, void *,
234 							unsigned long *,
235 							unsigned long *, u32 *);
236 		efi_status_t (__efiapi *allocate_pool)(int, unsigned long,
237 						       void **);
238 		efi_status_t (__efiapi *free_pool)(void *);
239 		efi_status_t (__efiapi *create_event)(u32, unsigned long,
240 						      efi_event_notify_t, void *,
241 						      efi_event_t *);
242 		efi_status_t (__efiapi *set_timer)(efi_event_t,
243 						  EFI_TIMER_DELAY, u64);
244 		efi_status_t (__efiapi *wait_for_event)(unsigned long,
245 							efi_event_t *,
246 							unsigned long *);
247 		void *signal_event;
248 		efi_status_t (__efiapi *close_event)(efi_event_t);
249 		void *check_event;
250 		void *install_protocol_interface;
251 		void *reinstall_protocol_interface;
252 		void *uninstall_protocol_interface;
253 		efi_status_t (__efiapi *handle_protocol)(efi_handle_t,
254 							 efi_guid_t *, void **);
255 		void *__reserved;
256 		void *register_protocol_notify;
257 		efi_status_t (__efiapi *locate_handle)(int, efi_guid_t *,
258 						       void *, unsigned long *,
259 						       efi_handle_t *);
260 		efi_status_t (__efiapi *locate_device_path)(efi_guid_t *,
261 							    efi_device_path_protocol_t **,
262 							    efi_handle_t *);
263 		efi_status_t (__efiapi *install_configuration_table)(efi_guid_t *,
264 								     void *);
265 		efi_status_t (__efiapi *load_image)(bool, efi_handle_t,
266 						    efi_device_path_protocol_t *,
267 						    void *, unsigned long,
268 						    efi_handle_t *);
269 		efi_status_t (__efiapi *start_image)(efi_handle_t, unsigned long *,
270 						     efi_char16_t **);
271 		efi_status_t __noreturn (__efiapi *exit)(efi_handle_t,
272 							 efi_status_t,
273 							 unsigned long,
274 							 efi_char16_t *);
275 		efi_status_t (__efiapi *unload_image)(efi_handle_t);
276 		efi_status_t (__efiapi *exit_boot_services)(efi_handle_t,
277 							    unsigned long);
278 		void *get_next_monotonic_count;
279 		efi_status_t (__efiapi *stall)(unsigned long);
280 		void *set_watchdog_timer;
281 		void *connect_controller;
282 		efi_status_t (__efiapi *disconnect_controller)(efi_handle_t,
283 							       efi_handle_t,
284 							       efi_handle_t);
285 		void *open_protocol;
286 		void *close_protocol;
287 		void *open_protocol_information;
288 		void *protocols_per_handle;
289 		void *locate_handle_buffer;
290 		efi_status_t (__efiapi *locate_protocol)(efi_guid_t *, void *,
291 							 void **);
292 		efi_status_t (__efiapi *install_multiple_protocol_interfaces)(efi_handle_t *, ...);
293 		efi_status_t (__efiapi *uninstall_multiple_protocol_interfaces)(efi_handle_t, ...);
294 		void *calculate_crc32;
295 		void (__efiapi *copy_mem)(void *, const void *, unsigned long);
296 		void (__efiapi *set_mem)(void *, unsigned long, unsigned char);
297 		void *create_event_ex;
298 	};
299 	struct {
300 		efi_table_hdr_t hdr;
301 		u32 raise_tpl;
302 		u32 restore_tpl;
303 		u32 allocate_pages;
304 		u32 free_pages;
305 		u32 get_memory_map;
306 		u32 allocate_pool;
307 		u32 free_pool;
308 		u32 create_event;
309 		u32 set_timer;
310 		u32 wait_for_event;
311 		u32 signal_event;
312 		u32 close_event;
313 		u32 check_event;
314 		u32 install_protocol_interface;
315 		u32 reinstall_protocol_interface;
316 		u32 uninstall_protocol_interface;
317 		u32 handle_protocol;
318 		u32 __reserved;
319 		u32 register_protocol_notify;
320 		u32 locate_handle;
321 		u32 locate_device_path;
322 		u32 install_configuration_table;
323 		u32 load_image;
324 		u32 start_image;
325 		u32 exit;
326 		u32 unload_image;
327 		u32 exit_boot_services;
328 		u32 get_next_monotonic_count;
329 		u32 stall;
330 		u32 set_watchdog_timer;
331 		u32 connect_controller;
332 		u32 disconnect_controller;
333 		u32 open_protocol;
334 		u32 close_protocol;
335 		u32 open_protocol_information;
336 		u32 protocols_per_handle;
337 		u32 locate_handle_buffer;
338 		u32 locate_protocol;
339 		u32 install_multiple_protocol_interfaces;
340 		u32 uninstall_multiple_protocol_interfaces;
341 		u32 calculate_crc32;
342 		u32 copy_mem;
343 		u32 set_mem;
344 		u32 create_event_ex;
345 	} mixed_mode;
346 };
347 
348 typedef enum {
349 	EfiGcdMemoryTypeNonExistent,
350 	EfiGcdMemoryTypeReserved,
351 	EfiGcdMemoryTypeSystemMemory,
352 	EfiGcdMemoryTypeMemoryMappedIo,
353 	EfiGcdMemoryTypePersistent,
354 	EfiGcdMemoryTypeMoreReliable,
355 	EfiGcdMemoryTypeMaximum
356 } efi_gcd_memory_type_t;
357 
358 typedef struct {
359 	efi_physical_addr_t base_address;
360 	u64 length;
361 	u64 capabilities;
362 	u64 attributes;
363 	efi_gcd_memory_type_t gcd_memory_type;
364 	void *image_handle;
365 	void *device_handle;
366 } efi_gcd_memory_space_desc_t;
367 
368 /*
369  * EFI DXE Services table
370  */
371 union efi_dxe_services_table {
372 	struct {
373 		efi_table_hdr_t hdr;
374 		void *add_memory_space;
375 		void *allocate_memory_space;
376 		void *free_memory_space;
377 		void *remove_memory_space;
378 		efi_status_t (__efiapi *get_memory_space_descriptor)(efi_physical_addr_t,
379 								     efi_gcd_memory_space_desc_t *);
380 		efi_status_t (__efiapi *set_memory_space_attributes)(efi_physical_addr_t,
381 								     u64, u64);
382 		void *get_memory_space_map;
383 		void *add_io_space;
384 		void *allocate_io_space;
385 		void *free_io_space;
386 		void *remove_io_space;
387 		void *get_io_space_descriptor;
388 		void *get_io_space_map;
389 		void *dispatch;
390 		void *schedule;
391 		void *trust;
392 		void *process_firmware_volume;
393 		void *set_memory_space_capabilities;
394 	};
395 	struct {
396 		efi_table_hdr_t hdr;
397 		u32 add_memory_space;
398 		u32 allocate_memory_space;
399 		u32 free_memory_space;
400 		u32 remove_memory_space;
401 		u32 get_memory_space_descriptor;
402 		u32 set_memory_space_attributes;
403 		u32 get_memory_space_map;
404 		u32 add_io_space;
405 		u32 allocate_io_space;
406 		u32 free_io_space;
407 		u32 remove_io_space;
408 		u32 get_io_space_descriptor;
409 		u32 get_io_space_map;
410 		u32 dispatch;
411 		u32 schedule;
412 		u32 trust;
413 		u32 process_firmware_volume;
414 		u32 set_memory_space_capabilities;
415 	} mixed_mode;
416 };
417 
418 typedef union efi_uga_draw_protocol efi_uga_draw_protocol_t;
419 
420 union efi_uga_draw_protocol {
421 	struct {
422 		efi_status_t (__efiapi *get_mode)(efi_uga_draw_protocol_t *,
423 						  u32*, u32*, u32*, u32*);
424 		void *set_mode;
425 		void *blt;
426 	};
427 	struct {
428 		u32 get_mode;
429 		u32 set_mode;
430 		u32 blt;
431 	} mixed_mode;
432 };
433 
434 typedef struct {
435 	u16 scan_code;
436 	efi_char16_t unicode_char;
437 } efi_input_key_t;
438 
439 union efi_simple_text_input_protocol {
440 	struct {
441 		void *reset;
442 		efi_status_t (__efiapi *read_keystroke)(efi_simple_text_input_protocol_t *,
443 							efi_input_key_t *);
444 		efi_event_t wait_for_key;
445 	};
446 	struct {
447 		u32 reset;
448 		u32 read_keystroke;
449 		u32 wait_for_key;
450 	} mixed_mode;
451 };
452 
453 efi_status_t efi_wait_for_key(unsigned long usec, efi_input_key_t *key);
454 
455 union efi_simple_text_output_protocol {
456 	struct {
457 		void *reset;
458 		efi_status_t (__efiapi *output_string)(efi_simple_text_output_protocol_t *,
459 						       efi_char16_t *);
460 		void *test_string;
461 	};
462 	struct {
463 		u32 reset;
464 		u32 output_string;
465 		u32 test_string;
466 	} mixed_mode;
467 };
468 
469 #define PIXEL_RGB_RESERVED_8BIT_PER_COLOR		0
470 #define PIXEL_BGR_RESERVED_8BIT_PER_COLOR		1
471 #define PIXEL_BIT_MASK					2
472 #define PIXEL_BLT_ONLY					3
473 #define PIXEL_FORMAT_MAX				4
474 
475 typedef struct {
476 	u32 red_mask;
477 	u32 green_mask;
478 	u32 blue_mask;
479 	u32 reserved_mask;
480 } efi_pixel_bitmask_t;
481 
482 typedef struct {
483 	u32 version;
484 	u32 horizontal_resolution;
485 	u32 vertical_resolution;
486 	int pixel_format;
487 	efi_pixel_bitmask_t pixel_information;
488 	u32 pixels_per_scan_line;
489 } efi_graphics_output_mode_info_t;
490 
491 typedef union efi_graphics_output_protocol_mode efi_graphics_output_protocol_mode_t;
492 
493 union efi_graphics_output_protocol_mode {
494 	struct {
495 		u32 max_mode;
496 		u32 mode;
497 		efi_graphics_output_mode_info_t *info;
498 		unsigned long size_of_info;
499 		efi_physical_addr_t frame_buffer_base;
500 		unsigned long frame_buffer_size;
501 	};
502 	struct {
503 		u32 max_mode;
504 		u32 mode;
505 		u32 info;
506 		u32 size_of_info;
507 		u64 frame_buffer_base;
508 		u32 frame_buffer_size;
509 	} mixed_mode;
510 };
511 
512 typedef union efi_graphics_output_protocol efi_graphics_output_protocol_t;
513 
514 union efi_graphics_output_protocol {
515 	struct {
516 		efi_status_t (__efiapi *query_mode)(efi_graphics_output_protocol_t *,
517 						    u32, unsigned long *,
518 						    efi_graphics_output_mode_info_t **);
519 		efi_status_t (__efiapi *set_mode)  (efi_graphics_output_protocol_t *, u32);
520 		void *blt;
521 		efi_graphics_output_protocol_mode_t *mode;
522 	};
523 	struct {
524 		u32 query_mode;
525 		u32 set_mode;
526 		u32 blt;
527 		u32 mode;
528 	} mixed_mode;
529 };
530 
531 typedef union {
532 	struct {
533 		u32			revision;
534 		efi_handle_t		parent_handle;
535 		efi_system_table_t	*system_table;
536 		efi_handle_t		device_handle;
537 		void			*file_path;
538 		void			*reserved;
539 		u32			load_options_size;
540 		void			*load_options;
541 		void			*image_base;
542 		__aligned_u64		image_size;
543 		unsigned int		image_code_type;
544 		unsigned int		image_data_type;
545 		efi_status_t		(__efiapi *unload)(efi_handle_t image_handle);
546 	};
547 	struct {
548 		u32		revision;
549 		u32		parent_handle;
550 		u32		system_table;
551 		u32		device_handle;
552 		u32		file_path;
553 		u32		reserved;
554 		u32		load_options_size;
555 		u32		load_options;
556 		u32		image_base;
557 		__aligned_u64	image_size;
558 		u32		image_code_type;
559 		u32		image_data_type;
560 		u32		unload;
561 	} mixed_mode;
562 } efi_loaded_image_t;
563 
564 typedef struct {
565 	u64			size;
566 	u64			file_size;
567 	u64			phys_size;
568 	efi_time_t		create_time;
569 	efi_time_t		last_access_time;
570 	efi_time_t		modification_time;
571 	__aligned_u64		attribute;
572 	efi_char16_t		filename[];
573 } efi_file_info_t;
574 
575 typedef struct efi_file_protocol efi_file_protocol_t;
576 
577 struct efi_file_protocol {
578 	u64		revision;
579 	efi_status_t	(__efiapi *open)	(efi_file_protocol_t *,
580 						 efi_file_protocol_t **,
581 						 efi_char16_t *, u64, u64);
582 	efi_status_t	(__efiapi *close)	(efi_file_protocol_t *);
583 	efi_status_t	(__efiapi *delete)	(efi_file_protocol_t *);
584 	efi_status_t	(__efiapi *read)	(efi_file_protocol_t *,
585 						 unsigned long *, void *);
586 	efi_status_t	(__efiapi *write)	(efi_file_protocol_t *,
587 						 unsigned long, void *);
588 	efi_status_t	(__efiapi *get_position)(efi_file_protocol_t *, u64 *);
589 	efi_status_t	(__efiapi *set_position)(efi_file_protocol_t *, u64);
590 	efi_status_t	(__efiapi *get_info)	(efi_file_protocol_t *,
591 						 efi_guid_t *, unsigned long *,
592 						 void *);
593 	efi_status_t	(__efiapi *set_info)	(efi_file_protocol_t *,
594 						 efi_guid_t *, unsigned long,
595 						 void *);
596 	efi_status_t	(__efiapi *flush)	(efi_file_protocol_t *);
597 };
598 
599 typedef struct efi_simple_file_system_protocol efi_simple_file_system_protocol_t;
600 
601 struct efi_simple_file_system_protocol {
602 	u64	revision;
603 	int	(__efiapi *open_volume)(efi_simple_file_system_protocol_t *,
604 					efi_file_protocol_t **);
605 };
606 
607 #define EFI_FILE_MODE_READ	0x0000000000000001
608 #define EFI_FILE_MODE_WRITE	0x0000000000000002
609 #define EFI_FILE_MODE_CREATE	0x8000000000000000
610 
611 typedef enum {
612 	EfiPciIoWidthUint8,
613 	EfiPciIoWidthUint16,
614 	EfiPciIoWidthUint32,
615 	EfiPciIoWidthUint64,
616 	EfiPciIoWidthFifoUint8,
617 	EfiPciIoWidthFifoUint16,
618 	EfiPciIoWidthFifoUint32,
619 	EfiPciIoWidthFifoUint64,
620 	EfiPciIoWidthFillUint8,
621 	EfiPciIoWidthFillUint16,
622 	EfiPciIoWidthFillUint32,
623 	EfiPciIoWidthFillUint64,
624 	EfiPciIoWidthMaximum
625 } EFI_PCI_IO_PROTOCOL_WIDTH;
626 
627 typedef enum {
628 	EfiPciIoAttributeOperationGet,
629 	EfiPciIoAttributeOperationSet,
630 	EfiPciIoAttributeOperationEnable,
631 	EfiPciIoAttributeOperationDisable,
632 	EfiPciIoAttributeOperationSupported,
633     EfiPciIoAttributeOperationMaximum
634 } EFI_PCI_IO_PROTOCOL_ATTRIBUTE_OPERATION;
635 
636 typedef struct {
637 	u32 read;
638 	u32 write;
639 } efi_pci_io_protocol_access_32_t;
640 
641 typedef union efi_pci_io_protocol efi_pci_io_protocol_t;
642 
643 typedef
644 efi_status_t (__efiapi *efi_pci_io_protocol_cfg_t)(efi_pci_io_protocol_t *,
645 						   EFI_PCI_IO_PROTOCOL_WIDTH,
646 						   u32 offset,
647 						   unsigned long count,
648 						   void *buffer);
649 
650 typedef struct {
651 	void *read;
652 	void *write;
653 } efi_pci_io_protocol_access_t;
654 
655 typedef struct {
656 	efi_pci_io_protocol_cfg_t read;
657 	efi_pci_io_protocol_cfg_t write;
658 } efi_pci_io_protocol_config_access_t;
659 
660 union efi_pci_io_protocol {
661 	struct {
662 		void *poll_mem;
663 		void *poll_io;
664 		efi_pci_io_protocol_access_t mem;
665 		efi_pci_io_protocol_access_t io;
666 		efi_pci_io_protocol_config_access_t pci;
667 		void *copy_mem;
668 		void *map;
669 		void *unmap;
670 		void *allocate_buffer;
671 		void *free_buffer;
672 		void *flush;
673 		efi_status_t (__efiapi *get_location)(efi_pci_io_protocol_t *,
674 						      unsigned long *segment_nr,
675 						      unsigned long *bus_nr,
676 						      unsigned long *device_nr,
677 						      unsigned long *func_nr);
678 		void *attributes;
679 		void *get_bar_attributes;
680 		void *set_bar_attributes;
681 		uint64_t romsize;
682 		void *romimage;
683 	};
684 	struct {
685 		u32 poll_mem;
686 		u32 poll_io;
687 		efi_pci_io_protocol_access_32_t mem;
688 		efi_pci_io_protocol_access_32_t io;
689 		efi_pci_io_protocol_access_32_t pci;
690 		u32 copy_mem;
691 		u32 map;
692 		u32 unmap;
693 		u32 allocate_buffer;
694 		u32 free_buffer;
695 		u32 flush;
696 		u32 get_location;
697 		u32 attributes;
698 		u32 get_bar_attributes;
699 		u32 set_bar_attributes;
700 		u64 romsize;
701 		u32 romimage;
702 	} mixed_mode;
703 };
704 
705 #define EFI_PCI_IO_ATTRIBUTE_ISA_MOTHERBOARD_IO 0x0001
706 #define EFI_PCI_IO_ATTRIBUTE_ISA_IO 0x0002
707 #define EFI_PCI_IO_ATTRIBUTE_VGA_PALETTE_IO 0x0004
708 #define EFI_PCI_IO_ATTRIBUTE_VGA_MEMORY 0x0008
709 #define EFI_PCI_IO_ATTRIBUTE_VGA_IO 0x0010
710 #define EFI_PCI_IO_ATTRIBUTE_IDE_PRIMARY_IO 0x0020
711 #define EFI_PCI_IO_ATTRIBUTE_IDE_SECONDARY_IO 0x0040
712 #define EFI_PCI_IO_ATTRIBUTE_MEMORY_WRITE_COMBINE 0x0080
713 #define EFI_PCI_IO_ATTRIBUTE_IO 0x0100
714 #define EFI_PCI_IO_ATTRIBUTE_MEMORY 0x0200
715 #define EFI_PCI_IO_ATTRIBUTE_BUS_MASTER 0x0400
716 #define EFI_PCI_IO_ATTRIBUTE_MEMORY_CACHED 0x0800
717 #define EFI_PCI_IO_ATTRIBUTE_MEMORY_DISABLE 0x1000
718 #define EFI_PCI_IO_ATTRIBUTE_EMBEDDED_DEVICE 0x2000
719 #define EFI_PCI_IO_ATTRIBUTE_EMBEDDED_ROM 0x4000
720 #define EFI_PCI_IO_ATTRIBUTE_DUAL_ADDRESS_CYCLE 0x8000
721 #define EFI_PCI_IO_ATTRIBUTE_ISA_IO_16 0x10000
722 #define EFI_PCI_IO_ATTRIBUTE_VGA_PALETTE_IO_16 0x20000
723 #define EFI_PCI_IO_ATTRIBUTE_VGA_IO_16 0x40000
724 
725 struct efi_dev_path;
726 
727 typedef union apple_properties_protocol apple_properties_protocol_t;
728 
729 union apple_properties_protocol {
730 	struct {
731 		unsigned long version;
732 		efi_status_t (__efiapi *get)(apple_properties_protocol_t *,
733 					     struct efi_dev_path *,
734 					     efi_char16_t *, void *, u32 *);
735 		efi_status_t (__efiapi *set)(apple_properties_protocol_t *,
736 					     struct efi_dev_path *,
737 					     efi_char16_t *, void *, u32);
738 		efi_status_t (__efiapi *del)(apple_properties_protocol_t *,
739 					     struct efi_dev_path *,
740 					     efi_char16_t *);
741 		efi_status_t (__efiapi *get_all)(apple_properties_protocol_t *,
742 						 void *buffer, u32 *);
743 	};
744 	struct {
745 		u32 version;
746 		u32 get;
747 		u32 set;
748 		u32 del;
749 		u32 get_all;
750 	} mixed_mode;
751 };
752 
753 typedef u32 efi_tcg2_event_log_format;
754 
755 #define INITRD_EVENT_TAG_ID 0x8F3B22ECU
756 #define LOAD_OPTIONS_EVENT_TAG_ID 0x8F3B22EDU
757 #define EV_EVENT_TAG 0x00000006U
758 #define EFI_TCG2_EVENT_HEADER_VERSION	0x1
759 
760 struct efi_tcg2_event {
761 	u32		event_size;
762 	struct {
763 		u32	header_size;
764 		u16	header_version;
765 		u32	pcr_index;
766 		u32	event_type;
767 	} __packed event_header;
768 	/* u8[] event follows here */
769 } __packed;
770 
771 struct efi_tcg2_tagged_event {
772 	u32 tagged_event_id;
773 	u32 tagged_event_data_size;
774 	/* u8  tagged event data follows here */
775 } __packed;
776 
777 typedef struct efi_tcg2_event efi_tcg2_event_t;
778 typedef struct efi_tcg2_tagged_event efi_tcg2_tagged_event_t;
779 typedef union efi_tcg2_protocol efi_tcg2_protocol_t;
780 
781 union efi_tcg2_protocol {
782 	struct {
783 		void *get_capability;
784 		efi_status_t (__efiapi *get_event_log)(efi_tcg2_protocol_t *,
785 						       efi_tcg2_event_log_format,
786 						       efi_physical_addr_t *,
787 						       efi_physical_addr_t *,
788 						       efi_bool_t *);
789 		efi_status_t (__efiapi *hash_log_extend_event)(efi_tcg2_protocol_t *,
790 							       u64,
791 							       efi_physical_addr_t,
792 							       u64,
793 							       const efi_tcg2_event_t *);
794 		void *submit_command;
795 		void *get_active_pcr_banks;
796 		void *set_active_pcr_banks;
797 		void *get_result_of_set_active_pcr_banks;
798 	};
799 	struct {
800 		u32 get_capability;
801 		u32 get_event_log;
802 		u32 hash_log_extend_event;
803 		u32 submit_command;
804 		u32 get_active_pcr_banks;
805 		u32 set_active_pcr_banks;
806 		u32 get_result_of_set_active_pcr_banks;
807 	} mixed_mode;
808 };
809 
810 struct riscv_efi_boot_protocol {
811 	u64 revision;
812 
813 	efi_status_t (__efiapi *get_boot_hartid)(struct riscv_efi_boot_protocol *,
814 						 unsigned long *boot_hartid);
815 };
816 
817 typedef union efi_load_file_protocol efi_load_file_protocol_t;
818 typedef union efi_load_file_protocol efi_load_file2_protocol_t;
819 
820 union efi_load_file_protocol {
821 	struct {
822 		efi_status_t (__efiapi *load_file)(efi_load_file_protocol_t *,
823 						   efi_device_path_protocol_t *,
824 						   bool, unsigned long *, void *);
825 	};
826 	struct {
827 		u32 load_file;
828 	} mixed_mode;
829 };
830 
831 typedef struct {
832 	u32 attributes;
833 	u16 file_path_list_length;
834 	u8 variable_data[];
835 	// efi_char16_t description[];
836 	// efi_device_path_protocol_t file_path_list[];
837 	// u8 optional_data[];
838 } __packed efi_load_option_t;
839 
840 #define EFI_LOAD_OPTION_ACTIVE		0x0001U
841 #define EFI_LOAD_OPTION_FORCE_RECONNECT	0x0002U
842 #define EFI_LOAD_OPTION_HIDDEN		0x0008U
843 #define EFI_LOAD_OPTION_CATEGORY	0x1f00U
844 #define   EFI_LOAD_OPTION_CATEGORY_BOOT	0x0000U
845 #define   EFI_LOAD_OPTION_CATEGORY_APP	0x0100U
846 
847 #define EFI_LOAD_OPTION_BOOT_MASK \
848 	(EFI_LOAD_OPTION_ACTIVE|EFI_LOAD_OPTION_HIDDEN|EFI_LOAD_OPTION_CATEGORY)
849 #define EFI_LOAD_OPTION_MASK (EFI_LOAD_OPTION_FORCE_RECONNECT|EFI_LOAD_OPTION_BOOT_MASK)
850 
851 typedef struct {
852 	u32 attributes;
853 	u16 file_path_list_length;
854 	const efi_char16_t *description;
855 	const efi_device_path_protocol_t *file_path_list;
856 	u32 optional_data_size;
857 	const void *optional_data;
858 } efi_load_option_unpacked_t;
859 
860 void efi_pci_disable_bridge_busmaster(void);
861 
862 typedef efi_status_t (*efi_exit_boot_map_processing)(
863 	struct efi_boot_memmap *map,
864 	void *priv);
865 
866 efi_status_t efi_exit_boot_services(void *handle, void *priv,
867 				    efi_exit_boot_map_processing priv_func);
868 
869 efi_status_t efi_boot_kernel(void *handle, efi_loaded_image_t *image,
870 			     unsigned long kernel_addr, char *cmdline_ptr);
871 
872 void *get_fdt(unsigned long *fdt_size);
873 
874 efi_status_t efi_alloc_virtmap(efi_memory_desc_t **virtmap,
875 			       unsigned long *desc_size, u32 *desc_ver);
876 void efi_get_virtmap(efi_memory_desc_t *memory_map, unsigned long map_size,
877 		     unsigned long desc_size, efi_memory_desc_t *runtime_map,
878 		     int *count);
879 
880 efi_status_t efi_get_random_bytes(unsigned long size, u8 *out);
881 
882 efi_status_t efi_random_alloc(unsigned long size, unsigned long align,
883 			      unsigned long *addr, unsigned long random_seed);
884 
885 efi_status_t check_platform_features(void);
886 
887 void *get_efi_config_table(efi_guid_t guid);
888 
889 /* NOTE: These functions do not print a trailing newline after the string */
890 void efi_char16_puts(efi_char16_t *);
891 void efi_puts(const char *str);
892 
893 __printf(1, 2) int efi_printk(char const *fmt, ...);
894 
895 void efi_free(unsigned long size, unsigned long addr);
896 
897 void efi_apply_loadoptions_quirk(const void **load_options, u32 *load_options_size);
898 
899 char *efi_convert_cmdline(efi_loaded_image_t *image, int *cmd_line_len);
900 
901 efi_status_t efi_get_memory_map(struct efi_boot_memmap **map,
902 				bool install_cfg_tbl);
903 
904 efi_status_t efi_allocate_pages(unsigned long size, unsigned long *addr,
905 				unsigned long max);
906 
907 efi_status_t efi_allocate_pages_aligned(unsigned long size, unsigned long *addr,
908 					unsigned long max, unsigned long align);
909 
910 efi_status_t efi_low_alloc_above(unsigned long size, unsigned long align,
911 				 unsigned long *addr, unsigned long min);
912 
913 efi_status_t efi_relocate_kernel(unsigned long *image_addr,
914 				 unsigned long image_size,
915 				 unsigned long alloc_size,
916 				 unsigned long preferred_addr,
917 				 unsigned long alignment,
918 				 unsigned long min_addr);
919 
920 efi_status_t efi_parse_options(char const *cmdline);
921 
922 void efi_parse_option_graphics(char *option);
923 
924 efi_status_t efi_setup_gop(struct screen_info *si, efi_guid_t *proto,
925 			   unsigned long size);
926 
927 efi_status_t handle_cmdline_files(efi_loaded_image_t *image,
928 				  const efi_char16_t *optstr,
929 				  int optstr_size,
930 				  unsigned long soft_limit,
931 				  unsigned long hard_limit,
932 				  unsigned long *load_addr,
933 				  unsigned long *load_size);
934 
935 
936 static inline efi_status_t efi_load_dtb(efi_loaded_image_t *image,
937 					unsigned long *load_addr,
938 					unsigned long *load_size)
939 {
940 	return handle_cmdline_files(image, L"dtb=", sizeof(L"dtb=") - 2,
941 				    ULONG_MAX, ULONG_MAX, load_addr, load_size);
942 }
943 
944 efi_status_t efi_load_initrd(efi_loaded_image_t *image,
945 			     unsigned long soft_limit,
946 			     unsigned long hard_limit,
947 			     const struct linux_efi_initrd **out);
948 /*
949  * This function handles the architcture specific differences between arm and
950  * arm64 regarding where the kernel image must be loaded and any memory that
951  * must be reserved. On failure it is required to free all
952  * all allocations it has made.
953  */
954 efi_status_t handle_kernel_image(unsigned long *image_addr,
955 				 unsigned long *image_size,
956 				 unsigned long *reserve_addr,
957 				 unsigned long *reserve_size,
958 				 efi_loaded_image_t *image,
959 				 efi_handle_t image_handle);
960 
961 asmlinkage void __noreturn efi_enter_kernel(unsigned long entrypoint,
962 					    unsigned long fdt_addr,
963 					    unsigned long fdt_size);
964 
965 void efi_handle_post_ebs_state(void);
966 
967 enum efi_secureboot_mode efi_get_secureboot(void);
968 
969 #ifdef CONFIG_RESET_ATTACK_MITIGATION
970 void efi_enable_reset_attack_mitigation(void);
971 #else
972 static inline void
973 efi_enable_reset_attack_mitigation(void) { }
974 #endif
975 
976 void efi_retrieve_tpm2_eventlog(void);
977 
978 #endif
979