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