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