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