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