1 /* SPDX-License-Identifier: GPL-2.0 */
3 #ifndef _DRIVERS_FIRMWARE_EFI_EFISTUB_H
4 #define _DRIVERS_FIRMWARE_EFI_EFISTUB_H
6 #include <linux/compiler.h>
8 #include <linux/kernel.h>
9 #include <linux/kern_levels.h>
10 #include <linux/types.h>
13 /* error code which can't be mistaken for valid address */
14 #define EFI_ERROR (~0UL)
17 * __init annotations should not be used in the EFI stub, since the code is
18 * either included in the decompressor (x86, ARM) where they have no effect,
19 * or the whole stub is __init annotated at the section level (arm64), by
20 * renaming the sections, in which case the __init annotation will be
21 * redundant, and will result in section names like .init.init.text, and our
22 * linker script does not expect that.
27 * Allow the platform to override the allocation granularity: this allows
28 * systems that have the capability to run with a larger page size to deal
29 * with the allocations for initrd and fdt more efficiently.
31 #ifndef EFI_ALLOC_ALIGN
32 #define EFI_ALLOC_ALIGN EFI_PAGE_SIZE
35 extern bool efi_nochunk;
36 extern bool efi_nokaslr;
37 extern bool efi_noinitrd;
38 extern int efi_loglevel;
39 extern bool efi_novamap;
41 extern const efi_system_table_t *efi_system_table;
43 efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,
44 efi_system_table_t *sys_table_arg);
46 #ifndef ARCH_HAS_EFISTUB_WRAPPERS
48 #define efi_is_native() (true)
49 #define efi_bs_call(func, ...) efi_system_table->boottime->func(__VA_ARGS__)
50 #define efi_rt_call(func, ...) efi_system_table->runtime->func(__VA_ARGS__)
51 #define efi_table_attr(inst, attr) (inst->attr)
52 #define efi_call_proto(inst, func, ...) inst->func(inst, ##__VA_ARGS__)
56 #define efi_info(fmt, ...) \
57 efi_printk(KERN_INFO fmt, ##__VA_ARGS__)
58 #define efi_err(fmt, ...) \
59 efi_printk(KERN_ERR "ERROR: " fmt, ##__VA_ARGS__)
60 #define efi_debug(fmt, ...) \
61 efi_printk(KERN_DEBUG "DEBUG: " fmt, ##__VA_ARGS__)
63 /* Helper macros for the usual case of using simple C variables: */
64 #ifndef fdt_setprop_inplace_var
65 #define fdt_setprop_inplace_var(fdt, node_offset, name, var) \
66 fdt_setprop_inplace((fdt), (node_offset), (name), &(var), sizeof(var))
69 #ifndef fdt_setprop_var
70 #define fdt_setprop_var(fdt, node_offset, name, var) \
71 fdt_setprop((fdt), (node_offset), (name), &(var), sizeof(var))
74 #define get_efi_var(name, vendor, ...) \
75 efi_rt_call(get_variable, (efi_char16_t *)(name), \
76 (efi_guid_t *)(vendor), __VA_ARGS__)
78 #define set_efi_var(name, vendor, ...) \
79 efi_rt_call(set_variable, (efi_char16_t *)(name), \
80 (efi_guid_t *)(vendor), __VA_ARGS__)
82 #define efi_get_handle_at(array, idx) \
83 (efi_is_native() ? (array)[idx] \
84 : (efi_handle_t)(unsigned long)((u32 *)(array))[idx])
86 #define efi_get_handle_num(size) \
87 ((size) / (efi_is_native() ? sizeof(efi_handle_t) : sizeof(u32)))
89 #define for_each_efi_handle(handle, array, size, i) \
91 i < efi_get_handle_num(size) && \
92 ((handle = efi_get_handle_at((array), i)) || true); \
96 void efi_set_u64_split(u64 data, u32 *lo, u32 *hi)
98 *lo = lower_32_bits(data);
99 *hi = upper_32_bits(data);
103 * Allocation types for calls to boottime->allocate_pages.
105 #define EFI_ALLOCATE_ANY_PAGES 0
106 #define EFI_ALLOCATE_MAX_ADDRESS 1
107 #define EFI_ALLOCATE_ADDRESS 2
108 #define EFI_MAX_ALLOCATE_TYPE 3
111 * The type of search to perform when calling boottime->locate_handle
113 #define EFI_LOCATE_ALL_HANDLES 0
114 #define EFI_LOCATE_BY_REGISTER_NOTIFY 1
115 #define EFI_LOCATE_BY_PROTOCOL 2
118 * boottime->stall takes the time period in microseconds
120 #define EFI_USEC_PER_SEC 1000000
123 * boottime->set_timer takes the time in 100ns units
125 #define EFI_100NSEC_PER_USEC ((u64)10)
128 * An efi_boot_memmap is used by efi_get_memory_map() to return the
129 * EFI memory map in a dynamically allocated buffer.
131 * The buffer allocated for the EFI memory map includes extra room for
132 * a minimum of EFI_MMAP_NR_SLACK_SLOTS additional EFI memory descriptors.
133 * This facilitates the reuse of the EFI memory map buffer when a second
134 * call to ExitBootServices() is needed because of intervening changes to
135 * the EFI memory map. Other related structures, e.g. x86 e820ext, need
136 * to factor in this headroom requirement as well.
138 #define EFI_MMAP_NR_SLACK_SLOTS 8
140 struct efi_boot_memmap {
141 efi_memory_desc_t **map;
142 unsigned long *map_size;
143 unsigned long *desc_size;
145 unsigned long *key_ptr;
146 unsigned long *buff_size;
149 typedef struct efi_generic_dev_path efi_device_path_protocol_t;
151 typedef void *efi_event_t;
152 /* Note that notifications won't work in mixed mode */
153 typedef void (__efiapi *efi_event_notify_t)(efi_event_t, void *);
155 #define EFI_EVT_TIMER 0x80000000U
156 #define EFI_EVT_RUNTIME 0x40000000U
157 #define EFI_EVT_NOTIFY_WAIT 0x00000100U
158 #define EFI_EVT_NOTIFY_SIGNAL 0x00000200U
161 * efi_set_event_at() - add event to events array
163 * @events: array of UEFI events
164 * @ids: index where to put the event in the array
165 * @event: event to add to the aray
167 * boottime->wait_for_event() takes an array of events as input.
168 * Provide a helper to set it up correctly for mixed mode.
171 void efi_set_event_at(efi_event_t *events, size_t idx, efi_event_t event)
176 ((u32 *)events)[idx] = (u32)(unsigned long)event;
179 #define EFI_TPL_APPLICATION 4
180 #define EFI_TPL_CALLBACK 8
181 #define EFI_TPL_NOTIFY 16
182 #define EFI_TPL_HIGH_LEVEL 31
191 * EFI Boot Services table
193 union efi_boot_services {
198 efi_status_t (__efiapi *allocate_pages)(int, int, unsigned long,
199 efi_physical_addr_t *);
200 efi_status_t (__efiapi *free_pages)(efi_physical_addr_t,
202 efi_status_t (__efiapi *get_memory_map)(unsigned long *, void *,
204 unsigned long *, u32 *);
205 efi_status_t (__efiapi *allocate_pool)(int, unsigned long,
207 efi_status_t (__efiapi *free_pool)(void *);
208 efi_status_t (__efiapi *create_event)(u32, unsigned long,
209 efi_event_notify_t, void *,
211 efi_status_t (__efiapi *set_timer)(efi_event_t,
212 EFI_TIMER_DELAY, u64);
213 efi_status_t (__efiapi *wait_for_event)(unsigned long,
217 efi_status_t (__efiapi *close_event)(efi_event_t);
219 void *install_protocol_interface;
220 void *reinstall_protocol_interface;
221 void *uninstall_protocol_interface;
222 efi_status_t (__efiapi *handle_protocol)(efi_handle_t,
223 efi_guid_t *, void **);
225 void *register_protocol_notify;
226 efi_status_t (__efiapi *locate_handle)(int, efi_guid_t *,
227 void *, unsigned long *,
229 efi_status_t (__efiapi *locate_device_path)(efi_guid_t *,
230 efi_device_path_protocol_t **,
232 efi_status_t (__efiapi *install_configuration_table)(efi_guid_t *,
236 efi_status_t __noreturn (__efiapi *exit)(efi_handle_t,
241 efi_status_t (__efiapi *exit_boot_services)(efi_handle_t,
243 void *get_next_monotonic_count;
244 efi_status_t (__efiapi *stall)(unsigned long);
245 void *set_watchdog_timer;
246 void *connect_controller;
247 efi_status_t (__efiapi *disconnect_controller)(efi_handle_t,
251 void *close_protocol;
252 void *open_protocol_information;
253 void *protocols_per_handle;
254 void *locate_handle_buffer;
255 efi_status_t (__efiapi *locate_protocol)(efi_guid_t *, void *,
257 void *install_multiple_protocol_interfaces;
258 void *uninstall_multiple_protocol_interfaces;
259 void *calculate_crc32;
262 void *create_event_ex;
279 u32 install_protocol_interface;
280 u32 reinstall_protocol_interface;
281 u32 uninstall_protocol_interface;
284 u32 register_protocol_notify;
286 u32 locate_device_path;
287 u32 install_configuration_table;
292 u32 exit_boot_services;
293 u32 get_next_monotonic_count;
295 u32 set_watchdog_timer;
296 u32 connect_controller;
297 u32 disconnect_controller;
300 u32 open_protocol_information;
301 u32 protocols_per_handle;
302 u32 locate_handle_buffer;
304 u32 install_multiple_protocol_interfaces;
305 u32 uninstall_multiple_protocol_interfaces;
313 typedef union efi_uga_draw_protocol efi_uga_draw_protocol_t;
315 union efi_uga_draw_protocol {
317 efi_status_t (__efiapi *get_mode)(efi_uga_draw_protocol_t *,
318 u32*, u32*, u32*, u32*);
331 efi_char16_t unicode_char;
334 union efi_simple_text_input_protocol {
337 efi_status_t (__efiapi *read_keystroke)(efi_simple_text_input_protocol_t *,
339 efi_event_t wait_for_key;
348 efi_status_t efi_wait_for_key(unsigned long usec, efi_input_key_t *key);
350 union efi_simple_text_output_protocol {
353 efi_status_t (__efiapi *output_string)(efi_simple_text_output_protocol_t *,
364 #define PIXEL_RGB_RESERVED_8BIT_PER_COLOR 0
365 #define PIXEL_BGR_RESERVED_8BIT_PER_COLOR 1
366 #define PIXEL_BIT_MASK 2
367 #define PIXEL_BLT_ONLY 3
368 #define PIXEL_FORMAT_MAX 4
375 } efi_pixel_bitmask_t;
379 u32 horizontal_resolution;
380 u32 vertical_resolution;
382 efi_pixel_bitmask_t pixel_information;
383 u32 pixels_per_scan_line;
384 } efi_graphics_output_mode_info_t;
386 typedef union efi_graphics_output_protocol_mode efi_graphics_output_protocol_mode_t;
388 union efi_graphics_output_protocol_mode {
392 efi_graphics_output_mode_info_t *info;
393 unsigned long size_of_info;
394 efi_physical_addr_t frame_buffer_base;
395 unsigned long frame_buffer_size;
402 u64 frame_buffer_base;
403 u32 frame_buffer_size;
407 typedef union efi_graphics_output_protocol efi_graphics_output_protocol_t;
409 union efi_graphics_output_protocol {
411 efi_status_t (__efiapi *query_mode)(efi_graphics_output_protocol_t *,
412 u32, unsigned long *,
413 efi_graphics_output_mode_info_t **);
414 efi_status_t (__efiapi *set_mode) (efi_graphics_output_protocol_t *, u32);
416 efi_graphics_output_protocol_mode_t *mode;
429 efi_handle_t parent_handle;
430 efi_system_table_t *system_table;
431 efi_handle_t device_handle;
434 u32 load_options_size;
437 __aligned_u64 image_size;
438 unsigned int image_code_type;
439 unsigned int image_data_type;
440 efi_status_t (__efiapi *unload)(efi_handle_t image_handle);
449 u32 load_options_size;
452 __aligned_u64 image_size;
457 } efi_loaded_image_t;
463 efi_time_t create_time;
464 efi_time_t last_access_time;
465 efi_time_t modification_time;
466 __aligned_u64 attribute;
467 efi_char16_t filename[];
470 typedef struct efi_file_protocol efi_file_protocol_t;
472 struct efi_file_protocol {
474 efi_status_t (__efiapi *open) (efi_file_protocol_t *,
475 efi_file_protocol_t **,
476 efi_char16_t *, u64, u64);
477 efi_status_t (__efiapi *close) (efi_file_protocol_t *);
478 efi_status_t (__efiapi *delete) (efi_file_protocol_t *);
479 efi_status_t (__efiapi *read) (efi_file_protocol_t *,
480 unsigned long *, void *);
481 efi_status_t (__efiapi *write) (efi_file_protocol_t *,
482 unsigned long, void *);
483 efi_status_t (__efiapi *get_position)(efi_file_protocol_t *, u64 *);
484 efi_status_t (__efiapi *set_position)(efi_file_protocol_t *, u64);
485 efi_status_t (__efiapi *get_info) (efi_file_protocol_t *,
486 efi_guid_t *, unsigned long *,
488 efi_status_t (__efiapi *set_info) (efi_file_protocol_t *,
489 efi_guid_t *, unsigned long,
491 efi_status_t (__efiapi *flush) (efi_file_protocol_t *);
494 typedef struct efi_simple_file_system_protocol efi_simple_file_system_protocol_t;
496 struct efi_simple_file_system_protocol {
498 int (__efiapi *open_volume)(efi_simple_file_system_protocol_t *,
499 efi_file_protocol_t **);
502 #define EFI_FILE_MODE_READ 0x0000000000000001
503 #define EFI_FILE_MODE_WRITE 0x0000000000000002
504 #define EFI_FILE_MODE_CREATE 0x8000000000000000
511 EfiPciIoWidthFifoUint8,
512 EfiPciIoWidthFifoUint16,
513 EfiPciIoWidthFifoUint32,
514 EfiPciIoWidthFifoUint64,
515 EfiPciIoWidthFillUint8,
516 EfiPciIoWidthFillUint16,
517 EfiPciIoWidthFillUint32,
518 EfiPciIoWidthFillUint64,
520 } EFI_PCI_IO_PROTOCOL_WIDTH;
523 EfiPciIoAttributeOperationGet,
524 EfiPciIoAttributeOperationSet,
525 EfiPciIoAttributeOperationEnable,
526 EfiPciIoAttributeOperationDisable,
527 EfiPciIoAttributeOperationSupported,
528 EfiPciIoAttributeOperationMaximum
529 } EFI_PCI_IO_PROTOCOL_ATTRIBUTE_OPERATION;
534 } efi_pci_io_protocol_access_32_t;
536 typedef union efi_pci_io_protocol efi_pci_io_protocol_t;
539 efi_status_t (__efiapi *efi_pci_io_protocol_cfg_t)(efi_pci_io_protocol_t *,
540 EFI_PCI_IO_PROTOCOL_WIDTH,
548 } efi_pci_io_protocol_access_t;
551 efi_pci_io_protocol_cfg_t read;
552 efi_pci_io_protocol_cfg_t write;
553 } efi_pci_io_protocol_config_access_t;
555 union efi_pci_io_protocol {
559 efi_pci_io_protocol_access_t mem;
560 efi_pci_io_protocol_access_t io;
561 efi_pci_io_protocol_config_access_t pci;
565 void *allocate_buffer;
568 efi_status_t (__efiapi *get_location)(efi_pci_io_protocol_t *,
569 unsigned long *segment_nr,
570 unsigned long *bus_nr,
571 unsigned long *device_nr,
572 unsigned long *func_nr);
574 void *get_bar_attributes;
575 void *set_bar_attributes;
582 efi_pci_io_protocol_access_32_t mem;
583 efi_pci_io_protocol_access_32_t io;
584 efi_pci_io_protocol_access_32_t pci;
593 u32 get_bar_attributes;
594 u32 set_bar_attributes;
600 #define EFI_PCI_IO_ATTRIBUTE_ISA_MOTHERBOARD_IO 0x0001
601 #define EFI_PCI_IO_ATTRIBUTE_ISA_IO 0x0002
602 #define EFI_PCI_IO_ATTRIBUTE_VGA_PALETTE_IO 0x0004
603 #define EFI_PCI_IO_ATTRIBUTE_VGA_MEMORY 0x0008
604 #define EFI_PCI_IO_ATTRIBUTE_VGA_IO 0x0010
605 #define EFI_PCI_IO_ATTRIBUTE_IDE_PRIMARY_IO 0x0020
606 #define EFI_PCI_IO_ATTRIBUTE_IDE_SECONDARY_IO 0x0040
607 #define EFI_PCI_IO_ATTRIBUTE_MEMORY_WRITE_COMBINE 0x0080
608 #define EFI_PCI_IO_ATTRIBUTE_IO 0x0100
609 #define EFI_PCI_IO_ATTRIBUTE_MEMORY 0x0200
610 #define EFI_PCI_IO_ATTRIBUTE_BUS_MASTER 0x0400
611 #define EFI_PCI_IO_ATTRIBUTE_MEMORY_CACHED 0x0800
612 #define EFI_PCI_IO_ATTRIBUTE_MEMORY_DISABLE 0x1000
613 #define EFI_PCI_IO_ATTRIBUTE_EMBEDDED_DEVICE 0x2000
614 #define EFI_PCI_IO_ATTRIBUTE_EMBEDDED_ROM 0x4000
615 #define EFI_PCI_IO_ATTRIBUTE_DUAL_ADDRESS_CYCLE 0x8000
616 #define EFI_PCI_IO_ATTRIBUTE_ISA_IO_16 0x10000
617 #define EFI_PCI_IO_ATTRIBUTE_VGA_PALETTE_IO_16 0x20000
618 #define EFI_PCI_IO_ATTRIBUTE_VGA_IO_16 0x40000
622 typedef union apple_properties_protocol apple_properties_protocol_t;
624 union apple_properties_protocol {
626 unsigned long version;
627 efi_status_t (__efiapi *get)(apple_properties_protocol_t *,
628 struct efi_dev_path *,
629 efi_char16_t *, void *, u32 *);
630 efi_status_t (__efiapi *set)(apple_properties_protocol_t *,
631 struct efi_dev_path *,
632 efi_char16_t *, void *, u32);
633 efi_status_t (__efiapi *del)(apple_properties_protocol_t *,
634 struct efi_dev_path *,
636 efi_status_t (__efiapi *get_all)(apple_properties_protocol_t *,
637 void *buffer, u32 *);
648 typedef u32 efi_tcg2_event_log_format;
650 typedef union efi_tcg2_protocol efi_tcg2_protocol_t;
652 union efi_tcg2_protocol {
654 void *get_capability;
655 efi_status_t (__efiapi *get_event_log)(efi_handle_t,
656 efi_tcg2_event_log_format,
657 efi_physical_addr_t *,
658 efi_physical_addr_t *,
660 void *hash_log_extend_event;
661 void *submit_command;
662 void *get_active_pcr_banks;
663 void *set_active_pcr_banks;
664 void *get_result_of_set_active_pcr_banks;
669 u32 hash_log_extend_event;
671 u32 get_active_pcr_banks;
672 u32 set_active_pcr_banks;
673 u32 get_result_of_set_active_pcr_banks;
677 typedef union efi_load_file_protocol efi_load_file_protocol_t;
678 typedef union efi_load_file_protocol efi_load_file2_protocol_t;
680 union efi_load_file_protocol {
682 efi_status_t (__efiapi *load_file)(efi_load_file_protocol_t *,
683 efi_device_path_protocol_t *,
684 bool, unsigned long *, void *);
691 void efi_pci_disable_bridge_busmaster(void);
693 typedef efi_status_t (*efi_exit_boot_map_processing)(
694 struct efi_boot_memmap *map,
697 efi_status_t efi_exit_boot_services(void *handle,
698 struct efi_boot_memmap *map,
700 efi_exit_boot_map_processing priv_func);
702 efi_status_t allocate_new_fdt_and_exit_boot(void *handle,
703 unsigned long *new_fdt_addr,
704 unsigned long max_addr,
705 u64 initrd_addr, u64 initrd_size,
707 unsigned long fdt_addr,
708 unsigned long fdt_size);
710 void *get_fdt(unsigned long *fdt_size);
712 void efi_get_virtmap(efi_memory_desc_t *memory_map, unsigned long map_size,
713 unsigned long desc_size, efi_memory_desc_t *runtime_map,
716 efi_status_t efi_get_random_bytes(unsigned long size, u8 *out);
718 efi_status_t efi_random_alloc(unsigned long size, unsigned long align,
719 unsigned long *addr, unsigned long random_seed);
721 efi_status_t check_platform_features(void);
723 void *get_efi_config_table(efi_guid_t guid);
725 /* NOTE: These functions do not print a trailing newline after the string */
726 void efi_char16_puts(efi_char16_t *);
727 void efi_puts(const char *str);
729 __printf(1, 2) int efi_printk(char const *fmt, ...);
731 void efi_free(unsigned long size, unsigned long addr);
733 char *efi_convert_cmdline(efi_loaded_image_t *image, int *cmd_line_len);
735 efi_status_t efi_get_memory_map(struct efi_boot_memmap *map);
737 efi_status_t efi_allocate_pages(unsigned long size, unsigned long *addr,
740 efi_status_t efi_allocate_pages_aligned(unsigned long size, unsigned long *addr,
741 unsigned long max, unsigned long align);
743 efi_status_t efi_relocate_kernel(unsigned long *image_addr,
744 unsigned long image_size,
745 unsigned long alloc_size,
746 unsigned long preferred_addr,
747 unsigned long alignment,
748 unsigned long min_addr);
750 efi_status_t efi_parse_options(char const *cmdline);
752 void efi_parse_option_graphics(char *option);
754 efi_status_t efi_setup_gop(struct screen_info *si, efi_guid_t *proto,
757 efi_status_t handle_cmdline_files(efi_loaded_image_t *image,
758 const efi_char16_t *optstr,
760 unsigned long soft_limit,
761 unsigned long hard_limit,
762 unsigned long *load_addr,
763 unsigned long *load_size);
766 static inline efi_status_t efi_load_dtb(efi_loaded_image_t *image,
767 unsigned long *load_addr,
768 unsigned long *load_size)
770 return handle_cmdline_files(image, L"dtb=", sizeof(L"dtb=") - 2,
771 ULONG_MAX, ULONG_MAX, load_addr, load_size);
774 efi_status_t efi_load_initrd(efi_loaded_image_t *image,
775 unsigned long *load_addr,
776 unsigned long *load_size,
777 unsigned long soft_limit,
778 unsigned long hard_limit);
780 * This function handles the architcture specific differences between arm and
781 * arm64 regarding where the kernel image must be loaded and any memory that
782 * must be reserved. On failure it is required to free all
783 * all allocations it has made.
785 efi_status_t handle_kernel_image(unsigned long *image_addr,
786 unsigned long *image_size,
787 unsigned long *reserve_addr,
788 unsigned long *reserve_size,
789 unsigned long dram_base,
790 efi_loaded_image_t *image);
792 asmlinkage void __noreturn efi_enter_kernel(unsigned long entrypoint,
793 unsigned long fdt_addr,
794 unsigned long fdt_size);
796 void efi_handle_post_ebs_state(void);