1 /* SPDX-License-Identifier: GPL-2.0 */
3 #ifndef _DRIVERS_FIRMWARE_EFI_EFISTUB_H
4 #define _DRIVERS_FIRMWARE_EFI_EFISTUB_H
6 /* error code which can't be mistaken for valid address */
7 #define EFI_ERROR (~0UL)
10 * __init annotations should not be used in the EFI stub, since the code is
11 * either included in the decompressor (x86, ARM) where they have no effect,
12 * or the whole stub is __init annotated at the section level (arm64), by
13 * renaming the sections, in which case the __init annotation will be
14 * redundant, and will result in section names like .init.init.text, and our
15 * linker script does not expect that.
20 * Allow the platform to override the allocation granularity: this allows
21 * systems that have the capability to run with a larger page size to deal
22 * with the allocations for initrd and fdt more efficiently.
24 #ifndef EFI_ALLOC_ALIGN
25 #define EFI_ALLOC_ALIGN EFI_PAGE_SIZE
28 #if defined(CONFIG_X86)
29 #define __efistub_global __section(.data)
31 #define __efistub_global
34 extern bool __pure nochunk(void);
35 extern bool __pure nokaslr(void);
36 extern bool __pure noinitrd(void);
37 extern bool __pure is_quiet(void);
38 extern bool __pure novamap(void);
40 extern __pure efi_system_table_t *efi_system_table(void);
43 #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__)
49 #define efi_is_native() (true)
51 #ifndef efi_table_attr
52 #define efi_table_attr(inst, attr) (inst->attr)
54 #ifndef efi_call_proto
55 #define efi_call_proto(inst, func, ...) inst->func(inst, ##__VA_ARGS__)
58 #define pr_efi(msg) do { \
59 if (!is_quiet()) efi_printk("EFI stub: "msg); \
62 #define pr_efi_err(msg) efi_printk("EFI stub: ERROR: "msg)
64 /* Helper macros for the usual case of using simple C variables: */
65 #ifndef fdt_setprop_inplace_var
66 #define fdt_setprop_inplace_var(fdt, node_offset, name, var) \
67 fdt_setprop_inplace((fdt), (node_offset), (name), &(var), sizeof(var))
70 #ifndef fdt_setprop_var
71 #define fdt_setprop_var(fdt, node_offset, name, var) \
72 fdt_setprop((fdt), (node_offset), (name), &(var), sizeof(var))
75 #define get_efi_var(name, vendor, ...) \
76 efi_rt_call(get_variable, (efi_char16_t *)(name), \
77 (efi_guid_t *)(vendor), __VA_ARGS__)
79 #define set_efi_var(name, vendor, ...) \
80 efi_rt_call(set_variable, (efi_char16_t *)(name), \
81 (efi_guid_t *)(vendor), __VA_ARGS__)
83 #define efi_get_handle_at(array, idx) \
84 (efi_is_native() ? (array)[idx] \
85 : (efi_handle_t)(unsigned long)((u32 *)(array))[idx])
87 #define efi_get_handle_num(size) \
88 ((size) / (efi_is_native() ? sizeof(efi_handle_t) : sizeof(u32)))
90 #define for_each_efi_handle(handle, array, size, i) \
92 i < efi_get_handle_num(size) && \
93 ((handle = efi_get_handle_at((array), i)) || true); \
97 * Allocation types for calls to boottime->allocate_pages.
99 #define EFI_ALLOCATE_ANY_PAGES 0
100 #define EFI_ALLOCATE_MAX_ADDRESS 1
101 #define EFI_ALLOCATE_ADDRESS 2
102 #define EFI_MAX_ALLOCATE_TYPE 3
105 * The type of search to perform when calling boottime->locate_handle
107 #define EFI_LOCATE_ALL_HANDLES 0
108 #define EFI_LOCATE_BY_REGISTER_NOTIFY 1
109 #define EFI_LOCATE_BY_PROTOCOL 2
111 struct efi_boot_memmap {
112 efi_memory_desc_t **map;
113 unsigned long *map_size;
114 unsigned long *desc_size;
116 unsigned long *key_ptr;
117 unsigned long *buff_size;
120 typedef struct efi_generic_dev_path efi_device_path_protocol_t;
123 * EFI Boot Services table
125 union efi_boot_services {
130 efi_status_t (__efiapi *allocate_pages)(int, int, unsigned long,
131 efi_physical_addr_t *);
132 efi_status_t (__efiapi *free_pages)(efi_physical_addr_t,
134 efi_status_t (__efiapi *get_memory_map)(unsigned long *, void *,
136 unsigned long *, u32 *);
137 efi_status_t (__efiapi *allocate_pool)(int, unsigned long,
139 efi_status_t (__efiapi *free_pool)(void *);
142 void *wait_for_event;
146 void *install_protocol_interface;
147 void *reinstall_protocol_interface;
148 void *uninstall_protocol_interface;
149 efi_status_t (__efiapi *handle_protocol)(efi_handle_t,
150 efi_guid_t *, void **);
152 void *register_protocol_notify;
153 efi_status_t (__efiapi *locate_handle)(int, efi_guid_t *,
154 void *, unsigned long *,
156 efi_status_t (__efiapi *locate_device_path)(efi_guid_t *,
157 efi_device_path_protocol_t **,
159 efi_status_t (__efiapi *install_configuration_table)(efi_guid_t *,
163 efi_status_t __noreturn (__efiapi *exit)(efi_handle_t,
168 efi_status_t (__efiapi *exit_boot_services)(efi_handle_t,
170 void *get_next_monotonic_count;
172 void *set_watchdog_timer;
173 void *connect_controller;
174 efi_status_t (__efiapi *disconnect_controller)(efi_handle_t,
178 void *close_protocol;
179 void *open_protocol_information;
180 void *protocols_per_handle;
181 void *locate_handle_buffer;
182 efi_status_t (__efiapi *locate_protocol)(efi_guid_t *, void *,
184 void *install_multiple_protocol_interfaces;
185 void *uninstall_multiple_protocol_interfaces;
186 void *calculate_crc32;
189 void *create_event_ex;
206 u32 install_protocol_interface;
207 u32 reinstall_protocol_interface;
208 u32 uninstall_protocol_interface;
211 u32 register_protocol_notify;
213 u32 locate_device_path;
214 u32 install_configuration_table;
219 u32 exit_boot_services;
220 u32 get_next_monotonic_count;
222 u32 set_watchdog_timer;
223 u32 connect_controller;
224 u32 disconnect_controller;
227 u32 open_protocol_information;
228 u32 protocols_per_handle;
229 u32 locate_handle_buffer;
231 u32 install_multiple_protocol_interfaces;
232 u32 uninstall_multiple_protocol_interfaces;
240 typedef union efi_uga_draw_protocol efi_uga_draw_protocol_t;
242 union efi_uga_draw_protocol {
244 efi_status_t (__efiapi *get_mode)(efi_uga_draw_protocol_t *,
245 u32*, u32*, u32*, u32*);
256 union efi_simple_text_output_protocol {
259 efi_status_t (__efiapi *output_string)(efi_simple_text_output_protocol_t *,
270 #define PIXEL_RGB_RESERVED_8BIT_PER_COLOR 0
271 #define PIXEL_BGR_RESERVED_8BIT_PER_COLOR 1
272 #define PIXEL_BIT_MASK 2
273 #define PIXEL_BLT_ONLY 3
274 #define PIXEL_FORMAT_MAX 4
281 } efi_pixel_bitmask_t;
285 u32 horizontal_resolution;
286 u32 vertical_resolution;
288 efi_pixel_bitmask_t pixel_information;
289 u32 pixels_per_scan_line;
290 } efi_graphics_output_mode_info_t;
292 typedef union efi_graphics_output_protocol_mode efi_graphics_output_protocol_mode_t;
294 union efi_graphics_output_protocol_mode {
298 efi_graphics_output_mode_info_t *info;
299 unsigned long size_of_info;
300 efi_physical_addr_t frame_buffer_base;
301 unsigned long frame_buffer_size;
308 u64 frame_buffer_base;
309 u32 frame_buffer_size;
313 typedef union efi_graphics_output_protocol efi_graphics_output_protocol_t;
315 union efi_graphics_output_protocol {
317 efi_status_t (__efiapi *query_mode)(efi_graphics_output_protocol_t *,
318 u32, unsigned long *,
319 efi_graphics_output_mode_info_t **);
320 efi_status_t (__efiapi *set_mode) (efi_graphics_output_protocol_t *, u32);
322 efi_graphics_output_protocol_mode_t *mode;
335 efi_handle_t parent_handle;
336 efi_system_table_t *system_table;
337 efi_handle_t device_handle;
340 u32 load_options_size;
343 __aligned_u64 image_size;
344 unsigned int image_code_type;
345 unsigned int image_data_type;
346 efi_status_t (__efiapi *unload)(efi_handle_t image_handle);
355 u32 load_options_size;
358 __aligned_u64 image_size;
363 } efi_loaded_image_t;
369 efi_time_t create_time;
370 efi_time_t last_access_time;
371 efi_time_t modification_time;
372 __aligned_u64 attribute;
373 efi_char16_t filename[];
376 typedef struct efi_file_protocol efi_file_protocol_t;
378 struct efi_file_protocol {
380 efi_status_t (__efiapi *open) (efi_file_protocol_t *,
381 efi_file_protocol_t **,
382 efi_char16_t *, u64, u64);
383 efi_status_t (__efiapi *close) (efi_file_protocol_t *);
384 efi_status_t (__efiapi *delete) (efi_file_protocol_t *);
385 efi_status_t (__efiapi *read) (efi_file_protocol_t *,
386 unsigned long *, void *);
387 efi_status_t (__efiapi *write) (efi_file_protocol_t *,
388 unsigned long, void *);
389 efi_status_t (__efiapi *get_position)(efi_file_protocol_t *, u64 *);
390 efi_status_t (__efiapi *set_position)(efi_file_protocol_t *, u64);
391 efi_status_t (__efiapi *get_info) (efi_file_protocol_t *,
392 efi_guid_t *, unsigned long *,
394 efi_status_t (__efiapi *set_info) (efi_file_protocol_t *,
395 efi_guid_t *, unsigned long,
397 efi_status_t (__efiapi *flush) (efi_file_protocol_t *);
400 typedef struct efi_simple_file_system_protocol efi_simple_file_system_protocol_t;
402 struct efi_simple_file_system_protocol {
404 int (__efiapi *open_volume)(efi_simple_file_system_protocol_t *,
405 efi_file_protocol_t **);
408 #define EFI_FILE_MODE_READ 0x0000000000000001
409 #define EFI_FILE_MODE_WRITE 0x0000000000000002
410 #define EFI_FILE_MODE_CREATE 0x8000000000000000
417 EfiPciIoWidthFifoUint8,
418 EfiPciIoWidthFifoUint16,
419 EfiPciIoWidthFifoUint32,
420 EfiPciIoWidthFifoUint64,
421 EfiPciIoWidthFillUint8,
422 EfiPciIoWidthFillUint16,
423 EfiPciIoWidthFillUint32,
424 EfiPciIoWidthFillUint64,
426 } EFI_PCI_IO_PROTOCOL_WIDTH;
429 EfiPciIoAttributeOperationGet,
430 EfiPciIoAttributeOperationSet,
431 EfiPciIoAttributeOperationEnable,
432 EfiPciIoAttributeOperationDisable,
433 EfiPciIoAttributeOperationSupported,
434 EfiPciIoAttributeOperationMaximum
435 } EFI_PCI_IO_PROTOCOL_ATTRIBUTE_OPERATION;
440 } efi_pci_io_protocol_access_32_t;
442 typedef union efi_pci_io_protocol efi_pci_io_protocol_t;
445 efi_status_t (__efiapi *efi_pci_io_protocol_cfg_t)(efi_pci_io_protocol_t *,
446 EFI_PCI_IO_PROTOCOL_WIDTH,
454 } efi_pci_io_protocol_access_t;
457 efi_pci_io_protocol_cfg_t read;
458 efi_pci_io_protocol_cfg_t write;
459 } efi_pci_io_protocol_config_access_t;
461 union efi_pci_io_protocol {
465 efi_pci_io_protocol_access_t mem;
466 efi_pci_io_protocol_access_t io;
467 efi_pci_io_protocol_config_access_t pci;
471 void *allocate_buffer;
474 efi_status_t (__efiapi *get_location)(efi_pci_io_protocol_t *,
475 unsigned long *segment_nr,
476 unsigned long *bus_nr,
477 unsigned long *device_nr,
478 unsigned long *func_nr);
480 void *get_bar_attributes;
481 void *set_bar_attributes;
488 efi_pci_io_protocol_access_32_t mem;
489 efi_pci_io_protocol_access_32_t io;
490 efi_pci_io_protocol_access_32_t pci;
499 u32 get_bar_attributes;
500 u32 set_bar_attributes;
506 #define EFI_PCI_IO_ATTRIBUTE_ISA_MOTHERBOARD_IO 0x0001
507 #define EFI_PCI_IO_ATTRIBUTE_ISA_IO 0x0002
508 #define EFI_PCI_IO_ATTRIBUTE_VGA_PALETTE_IO 0x0004
509 #define EFI_PCI_IO_ATTRIBUTE_VGA_MEMORY 0x0008
510 #define EFI_PCI_IO_ATTRIBUTE_VGA_IO 0x0010
511 #define EFI_PCI_IO_ATTRIBUTE_IDE_PRIMARY_IO 0x0020
512 #define EFI_PCI_IO_ATTRIBUTE_IDE_SECONDARY_IO 0x0040
513 #define EFI_PCI_IO_ATTRIBUTE_MEMORY_WRITE_COMBINE 0x0080
514 #define EFI_PCI_IO_ATTRIBUTE_IO 0x0100
515 #define EFI_PCI_IO_ATTRIBUTE_MEMORY 0x0200
516 #define EFI_PCI_IO_ATTRIBUTE_BUS_MASTER 0x0400
517 #define EFI_PCI_IO_ATTRIBUTE_MEMORY_CACHED 0x0800
518 #define EFI_PCI_IO_ATTRIBUTE_MEMORY_DISABLE 0x1000
519 #define EFI_PCI_IO_ATTRIBUTE_EMBEDDED_DEVICE 0x2000
520 #define EFI_PCI_IO_ATTRIBUTE_EMBEDDED_ROM 0x4000
521 #define EFI_PCI_IO_ATTRIBUTE_DUAL_ADDRESS_CYCLE 0x8000
522 #define EFI_PCI_IO_ATTRIBUTE_ISA_IO_16 0x10000
523 #define EFI_PCI_IO_ATTRIBUTE_VGA_PALETTE_IO_16 0x20000
524 #define EFI_PCI_IO_ATTRIBUTE_VGA_IO_16 0x40000
528 typedef union apple_properties_protocol apple_properties_protocol_t;
530 union apple_properties_protocol {
532 unsigned long version;
533 efi_status_t (__efiapi *get)(apple_properties_protocol_t *,
534 struct efi_dev_path *,
535 efi_char16_t *, void *, u32 *);
536 efi_status_t (__efiapi *set)(apple_properties_protocol_t *,
537 struct efi_dev_path *,
538 efi_char16_t *, void *, u32);
539 efi_status_t (__efiapi *del)(apple_properties_protocol_t *,
540 struct efi_dev_path *,
542 efi_status_t (__efiapi *get_all)(apple_properties_protocol_t *,
543 void *buffer, u32 *);
554 typedef u32 efi_tcg2_event_log_format;
556 typedef union efi_tcg2_protocol efi_tcg2_protocol_t;
558 union efi_tcg2_protocol {
560 void *get_capability;
561 efi_status_t (__efiapi *get_event_log)(efi_handle_t,
562 efi_tcg2_event_log_format,
563 efi_physical_addr_t *,
564 efi_physical_addr_t *,
566 void *hash_log_extend_event;
567 void *submit_command;
568 void *get_active_pcr_banks;
569 void *set_active_pcr_banks;
570 void *get_result_of_set_active_pcr_banks;
575 u32 hash_log_extend_event;
577 u32 get_active_pcr_banks;
578 u32 set_active_pcr_banks;
579 u32 get_result_of_set_active_pcr_banks;
583 typedef union efi_load_file_protocol efi_load_file_protocol_t;
584 typedef union efi_load_file_protocol efi_load_file2_protocol_t;
586 union efi_load_file_protocol {
588 efi_status_t (__efiapi *load_file)(efi_load_file_protocol_t *,
589 efi_device_path_protocol_t *,
590 bool, unsigned long *, void *);
597 void efi_pci_disable_bridge_busmaster(void);
599 typedef efi_status_t (*efi_exit_boot_map_processing)(
600 struct efi_boot_memmap *map,
603 efi_status_t efi_exit_boot_services(void *handle,
604 struct efi_boot_memmap *map,
606 efi_exit_boot_map_processing priv_func);
608 void efi_char16_printk(efi_char16_t *);
610 efi_status_t allocate_new_fdt_and_exit_boot(void *handle,
611 unsigned long *new_fdt_addr,
612 unsigned long max_addr,
613 u64 initrd_addr, u64 initrd_size,
615 unsigned long fdt_addr,
616 unsigned long fdt_size);
618 void *get_fdt(unsigned long *fdt_size);
620 void efi_get_virtmap(efi_memory_desc_t *memory_map, unsigned long map_size,
621 unsigned long desc_size, efi_memory_desc_t *runtime_map,
624 efi_status_t efi_get_random_bytes(unsigned long size, u8 *out);
626 efi_status_t efi_random_alloc(unsigned long size, unsigned long align,
627 unsigned long *addr, unsigned long random_seed);
629 efi_status_t check_platform_features(void);
631 void *get_efi_config_table(efi_guid_t guid);
633 void efi_printk(char *str);
635 void efi_free(unsigned long size, unsigned long addr);
637 char *efi_convert_cmdline(efi_loaded_image_t *image, int *cmd_line_len,
638 unsigned long max_addr);
640 efi_status_t efi_get_memory_map(struct efi_boot_memmap *map);
642 efi_status_t efi_allocate_pages(unsigned long size, unsigned long *addr,
645 efi_status_t efi_allocate_pages_aligned(unsigned long size, unsigned long *addr,
646 unsigned long max, unsigned long align);
648 efi_status_t efi_relocate_kernel(unsigned long *image_addr,
649 unsigned long image_size,
650 unsigned long alloc_size,
651 unsigned long preferred_addr,
652 unsigned long alignment,
653 unsigned long min_addr);
655 efi_status_t efi_parse_options(char const *cmdline);
657 void efi_parse_option_graphics(char *option);
659 efi_status_t efi_setup_gop(struct screen_info *si, efi_guid_t *proto,
662 efi_status_t handle_cmdline_files(efi_loaded_image_t *image,
663 const efi_char16_t *optstr,
665 unsigned long soft_limit,
666 unsigned long hard_limit,
667 unsigned long *load_addr,
668 unsigned long *load_size);
671 static inline efi_status_t efi_load_dtb(efi_loaded_image_t *image,
672 unsigned long *load_addr,
673 unsigned long *load_size)
675 return handle_cmdline_files(image, L"dtb=", sizeof(L"dtb=") - 2,
676 ULONG_MAX, ULONG_MAX, load_addr, load_size);
679 static inline efi_status_t efi_load_initrd(efi_loaded_image_t *image,
680 unsigned long *load_addr,
681 unsigned long *load_size,
682 unsigned long soft_limit,
683 unsigned long hard_limit)
685 if (!IS_ENABLED(CONFIG_EFI_GENERIC_STUB_INITRD_CMDLINE_LOADER))
688 return handle_cmdline_files(image, L"initrd=", sizeof(L"initrd=") - 2,
689 soft_limit, hard_limit, load_addr, load_size);
692 efi_status_t efi_load_initrd_dev_path(unsigned long *load_addr,
693 unsigned long *load_size,