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