/* SPDX-License-Identifier: GPL-2.0 */
#ifndef _DRIVERS_FIRMWARE_EFI_EFISTUB_H
#define _DRIVERS_FIRMWARE_EFI_EFISTUB_H
#include <linux/compiler.h>
#include <linux/efi.h>
#include <linux/kernel.h>
#include <linux/kern_levels.h>
#include <linux/types.h>
#include <asm/efi.h>
/*
* __init annotations should not be used in the EFI stub, since the code is
* either included in the decompressor (x86, ARM) where they have no effect,
* or the whole stub is __init annotated at the section level (arm64), by
* renaming the sections, in which case the __init annotation will be
* redundant, and will result in section names like .init.init.text, and our
* linker script does not expect that.
*/
#undef __init
/*
* Allow the platform to override the allocation granularity: this allows
* systems that have the capability to run with a larger page size to deal
* with the allocations for initrd and fdt more efficiently.
*/
#ifndef EFI_ALLOC_ALIGN
#define EFI_ALLOC_ALIGN EFI_PAGE_SIZE
#endif
#ifndef EFI_ALLOC_LIMIT
#define EFI_ALLOC_LIMIT ULONG_MAX
#endif
extern bool efi_no5lvl;
extern bool efi_nochunk;
extern bool efi_nokaslr;
extern int efi_loglevel;
extern int efi_mem_encrypt;
extern bool efi_novamap;
extern const efi_system_table_t *efi_system_table;
typedef union efi_dxe_services_table efi_dxe_services_table_t;
extern const efi_dxe_services_table_t *efi_dxe_table;
efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,
efi_system_table_t *sys_table_arg);
#ifndef ARCH_HAS_EFISTUB_WRAPPERS
#define efi_is_native() (true)
#define efi_table_attr(inst, attr) (inst)->attr
#define efi_fn_call(inst, func, ...) (inst)->func(__VA_ARGS__)
#endif
#define efi_call_proto(inst, func, ...) ({ \
__typeof__(inst) __inst = (inst); \
efi_fn_call(__inst, func, __inst, ##__VA_ARGS__); \
})
#define efi_bs_call(func, ...) \
efi_fn_call(efi_table_attr(efi_system_table, boottime), func, ##__VA_ARGS__)
#define efi_rt_call(func, ...) \
efi_fn_call(efi_table_attr(efi_system_table, runtime), func, ##__VA_ARGS__)
#define efi_dxe_call(func, ...) \
efi_fn_call(efi_dxe_table, func, ##__VA_ARGS__)
#define efi_info(fmt, ...) \
efi_printk(KERN_INFO fmt, ##__VA_ARGS__)
#define efi_warn(fmt, ...) \
efi_printk(KERN_WARNING "WARNING: " fmt, ##__VA_ARGS__)
#define efi_err(fmt, ...) \
efi_printk(KERN_ERR "ERROR: " fmt, ##__VA_ARGS__)
#define efi_debug(fmt, ...) \
efi_printk(KERN_DEBUG "DEBUG: " fmt, ##__VA_ARGS__)
#define efi_printk_once(fmt, ...) \
({ \
static bool __print_once; \
bool __ret_print_once = !__print_once; \
\
if (!__print_once) { \
__print_once = true; \
efi_printk(fmt, ##__VA_ARGS__); \
} \
__ret_print_once; \
})
#define efi_info_once(fmt, ...) \
efi_printk_once(KERN_INFO fmt, ##__VA_ARGS__)
#define efi_warn_once(fmt, ...) \
efi_printk_once(KERN_WARNING "WARNING: " fmt, ##__VA_ARGS__)
#define efi_err_once(fmt, ...) \
efi_printk_once(KERN_ERR "ERROR: " fmt, ##__VA_ARGS__)
#define efi_debug_once(fmt, ...) \
efi_printk_once(KERN_DEBUG "DEBUG: " fmt, ##__VA_ARGS__)
/* Helper macros for the usual case of using simple C variables: */
#ifndef fdt_setprop_inplace_var
#define fdt_setprop_inplace_var(fdt, node_offset, name, var) \
fdt_setprop_inplace((fdt), (node_offset), (name), &(var), sizeof(var))
#endif
#ifndef fdt_setprop_var
#define fdt_setprop_var(fdt, node_offset, name, var) \
fdt_setprop((fdt), (node_offset), (name), &(var), sizeof(var))
#endif
#define get_efi_var(name, vendor, ...) \
efi_rt_call(get_variable, (efi_char16_t *)(name), \
(efi_guid_t *)(vendor), __VA_ARGS__)
#define set_efi_var(name, vendor, ...) \
efi_rt_call(set_variable, (efi_char16_t *)(name), \
(efi_guid_t *)(vendor), __VA_ARGS__)
#define efi_get_handle_at(array, idx) \
(efi_is_native() ? (array)[idx] \
: (efi_handle_t)(unsigned long)((u32 *)(array))[idx])
#define efi_get_handle_num(size) \
((size) / (efi_is_native() ? sizeof(efi_handle_t) : sizeof(u32)))
#define for_each_efi_handle(handle, array, size, i) \
for (i = 0; \
i < efi_get_handle_num(size) && \
((handle = efi_get_handle_at((array), i)) || true); \
i++)
static inline
void efi_set_u64_split(u64 data, u32 *lo, u32 *hi)
{
*lo = lower_32_bits(data);
*hi = upper_32_bits(data);
}
/*
* Allocation types for calls to boottime->allocate_pages.
*/
#define EFI_ALLOCATE_ANY_PAGES 0
#define EFI_ALLOCATE_MAX_ADDRESS 1
#define EFI_ALLOCATE_ADDRESS 2
#define EFI_MAX_ALLOCATE_TYPE 3
/*
* The type of search to perform when calling boottime->locate_handle
*/
#define EFI_LOCATE_ALL_HANDLES 0
#define EFI_LOCATE_BY_REGISTER_NOTIFY 1
#define EFI_LOCATE_BY_PROTOCOL 2
/*
* boottime->stall takes the time period in microseconds
*/
#define EFI_USEC_PER_SEC 1000000
/*
* boottime->set_timer takes the time in 100ns units
*/
#define EFI_100NSEC_PER_USEC ((u64)10)
/*
* An efi_boot_memmap is used by efi_get_memory_map() to return the
* EFI memory map in a dynamically allocated buffer.
*
* The buffer allocated for the EFI memory map includes extra room for
* a minimum of EFI_MMAP_NR_SLACK_SLOTS additional EFI memory descriptors.
* This facilitates the reuse of the EFI memory map buffer when a second
* call to ExitBootServices() is needed because of intervening changes to
* the EFI memory map. Other related structures, e.g. x86 e820ext, need
* to factor in this headroom requirement as well.
*/
#define EFI_MMAP_NR_SLACK_SLOTS 8
typedef struct efi_generic_dev_path efi_device_path_protocol_t;
union efi_device_path_to_text_protocol {
struct {
efi_char16_t *(__efiapi *convert_device_node_to_text)(
const efi_device_path_protocol_t *,
bool, bool);
efi_char16_t *(__efiapi *convert_device_path_to_text)(
const efi_device_path_protocol_t *,
bool, bool);
};
struct {
u32 convert_device_node_to_text;
u32 convert_device_path_to_text;
} mixed_mode;
};
typedef union efi_device_path_to_text_protocol efi_device_path_to_text_protocol_t;
union efi_device_path_from_text_protocol {
struct {
efi_device_path_protocol_t *
(__efiapi *convert_text_to_device_node)(const efi_char16_t *);
efi_device_path_protocol_t *
(__efiapi *convert_text_to_device_path)(const efi_char16_t *);
};
struct {
u32 convert_text_to_device_node;
u32 convert_text_to_device_path;
} mixed_mode;
};
typedef union efi_device_path_from_text_protocol efi_device_path_f