On 02.02.16 15:47, Leif Lindholm wrote: > On Tue, Feb 02, 2016 at 03:45:02AM +0100, Alexander Graf wrote: >> When an EFI application runs, it has access to a few descriptor and callback >> tables to instruct the EFI compliant firmware to do things for it. The bulk >> of those interfaces are "boot time services". They handle all object >> management, >> and memory allocation. >> >> This patch adds support for the boot time services and also exposes a system >> table, which is the point of entry descriptor table for EFI payloads. >> >> Signed-off-by: Alexander Graf <ag...@suse.de> >> Reviewed-by: Simon Glass <s...@chromium.org> >> >> --- >> >> v1 -> v2: >> >> - Fix typo s/does now/does not/ >> - Add #ifdefs around header to allow inclusion when efi_loader is disabled >> - Add stub efi_restore_gd() function when efi_loader is disabled >> - Disable debug >> - Mark runtime region as such >> - Fix up memory map >> - Allow efi_restore_gd to be called before first efi entry >> - Add 32bit arm cache workaround >> - Move memory map to separate patch >> - Change BTS version to 2.5 >> - Fix return values for a few callbacks to more EFI compliant ones >> - Change vendor to "Das U-Boot" >> - Add warning when truncating timer trigger >> - Move to GPLv2+ >> >> v2 -> v3: >> >> - Use external efi_memory helpers >> - Add EFIAPI to function prototypes >> - Initialize event timer to -1ULL to prevent early firing >> - Document header >> - Move obj list to lib >> - Remove implicit guid table >> - Add guid compare function >> - Fix return values >> - Implement efi_wait_for_event >> - Implement efi_install_configuration_table >> --- >> include/efi_loader.h | 84 +++++ >> lib/efi_loader/efi_boottime.c | 781 >> ++++++++++++++++++++++++++++++++++++++++++ >> 2 files changed, 865 insertions(+) >> create mode 100644 lib/efi_loader/efi_boottime.c >> >> diff --git a/include/efi_loader.h b/include/efi_loader.h >> index 5618185..a7f033e 100644 >> --- a/include/efi_loader.h >> +++ b/include/efi_loader.h >> @@ -6,15 +6,99 @@ >> * SPDX-License-Identifier: GPL-2.0+ >> */ >> >> +#include <common.h> >> #include <part_efi.h> >> #include <efi_api.h> >> + >> +#ifdef CONFIG_EFI_LOADER >> + >> #include <linux/list.h> >> >> +/* #define DEBUG_EFI */ >> + >> +#ifdef DEBUG_EFI >> +#define EFI_ENTRY(format, ...) do { \ >> + efi_restore_gd(); \ >> + printf("EFI: Entry %s(" format ")\n", __func__, ##__VA_ARGS__); \ >> + } while(0) >> +#else >> +#define EFI_ENTRY(format, ...) do { \ >> + efi_restore_gd(); \ >> + } while(0) >> +#endif >> + >> +#define EFI_EXIT(ret) efi_exit_func(ret); >> + >> +extern struct efi_system_table systab; >> + >> extern const efi_guid_t efi_guid_device_path; >> extern const efi_guid_t efi_guid_loaded_image; >> >> +/* >> + * While UEFI objects can have callbacks, you can also call functions on >> + * protocols (classes) themselves. This struct maps a protocol GUID to its >> + * interface (usually a struct with callback functions). >> + */ >> +struct efi_class_map { >> + const efi_guid_t *guid; >> + const void *interface; >> +}; >> + >> +/* >> + * When the UEFI payload wants to open a protocol on an object to get its >> + * interface (usually a struct with callback functions), this struct maps >> the >> + * protocol GUID to the respective protocol handler open function for that >> + * object protocol combination. >> + */ >> +struct efi_handler { >> + const efi_guid_t *guid; >> + efi_status_t (EFIAPI *open)(void *handle, >> + efi_guid_t *protocol, void **protocol_interface, >> + void *agent_handle, void *controller_handle, >> + uint32_t attributes); >> +}; >> + >> +/* >> + * UEFI has a poor man's OO model where one "object" can be polimorphic and >> have > > Polymorphic. > Also, somewhat long lines in this comment block?
Exactly 80 characters long :). > >> + * multiple different protocols (classes) attached to it. >> + * >> + * This struct is the parent struct for all of our actual implementation >> objects >> + * that can include it to make themselves an EFI object >> + */ >> +struct efi_object { >> + /* Every UEFI object is part of a global object list */ >> + struct list_head link; >> + /* We support up to 4 "protocols" an object can be accessed through */ >> + struct efi_handler protocols[4]; >> + /* The object spawner can either use this for data or as identifier */ >> + void *handle; >> +}; >> + >> +/* This list contains all UEFI objects we know of */ >> +extern struct list_head efi_obj_list; >> + >> +/* >> + * Stub implementation for a protocol opener that just returns the handle as >> + * interface >> + */ >> efi_status_t efi_return_handle(void *handle, >> efi_guid_t *protocol, void **protocol_interface, >> void *agent_handle, void *controller_handle, >> uint32_t attributes); >> +/* Called from places to check whether a timer expired */ >> +void efi_timer_check(void); >> +/* PE loader implementation */ >> void *efi_load_pe(void *efi, struct efi_loaded_image *loaded_image_info); >> +/* Called once to store the pristine gd pointer */ >> +void efi_save_gd(void); >> +/* Called from EFI_ENTRY on callback entry to put gd into the gd register */ >> +void efi_restore_gd(void); >> +/* Called from EFI_EXIT on callback exit to restore the gd register */ >> +efi_status_t efi_exit_func(efi_status_t ret); >> + >> +#else /* defined(EFI_LOADER) */ >> + >> +/* No loader configured, stub out EFI_ENTRY */ >> +static inline void efi_restore_gd(void) { } >> + >> +#endif >> diff --git a/lib/efi_loader/efi_boottime.c b/lib/efi_loader/efi_boottime.c >> new file mode 100644 >> index 0000000..e60fae9 >> --- /dev/null >> +++ b/lib/efi_loader/efi_boottime.c >> @@ -0,0 +1,781 @@ >> +/* >> + * EFI application boot time services >> + * >> + * Copyright (c) 2016 Alexander Graf >> + * >> + * SPDX-License-Identifier: GPL-2.0+ >> + */ >> + >> +/* #define DEBUG_EFI */ >> + >> +#include <common.h> >> +#include <efi_loader.h> >> +#include <malloc.h> >> +#include <asm/global_data.h> >> +#include <libfdt_env.h> >> +#include <u-boot/crc.h> >> +#include <bootm.h> >> +#include <inttypes.h> >> +#include <watchdog.h> >> + >> +DECLARE_GLOBAL_DATA_PTR; >> + >> +/* This list contains all the EFI objects our payload has access to */ >> +LIST_HEAD(efi_obj_list); >> + >> +/* >> + * If we're running on nasty systems (32bit ARM booting into non-EFI Linux) >> + * we need to do trickery with caches. Since we don't want to break the EFI >> + * aware boot path, only apply hacks when loading exiting directly (breaking >> + * direct Linux EFI booting along the way - oh well). >> + */ >> +static bool efi_is_direct_boot = true; > > Is this a case worth supporting? Which one? :) Without this hack we can't run current Linux kernels. Without the check we break the grub2 -> Linux case with EFI enabled Linux. Both are IMHO the two most important use cases. I want to have grub2 be able to boot today's kernels and tomorrows just as well. > > >> + >> +/* >> + * EFI can pass arbitrary additional "tables" containing vendor specific >> + * information to the payload. One such table is the FDT table which >> contains >> + * a pointer to a flattened device tree blob. >> + * >> + * In most cases we want to pass an FDT to the payload, so reserve one slot >> of >> + * config table space for it. The pointer gets populated by >> do_bootefi_exec(). >> + */ >> +static struct efi_configuration_table efi_conf_table[1]; >> + >> +/* >> + * The "gd" pointer lives in a register on ARM and AArch64 that we declare >> + * fixed when compiling U-Boot. However, the payload does not know about >> that >> + * restriction so we need to manually swap its and our view of that >> register on >> + * EFI callback entry/exit. >> + */ >> +static volatile void *efi_gd, *app_gd; >> + >> +/* Called from do_bootefi_exec() */ >> +void efi_save_gd(void) >> +{ >> + efi_gd = gd; >> +} >> + >> +/* Called on every callback entry */ >> +void efi_restore_gd(void) >> +{ >> + /* Only restore if we're already in EFI context */ >> + if (!efi_gd) >> + return; >> + >> + if (gd != efi_gd) >> + app_gd = gd; >> + gd = efi_gd; >> +} >> + >> +/* Called on every callback exit */ >> +efi_status_t efi_exit_func(efi_status_t ret) >> +{ >> + gd = app_gd; >> + return ret; >> +} >> + >> +static efi_status_t efi_unsupported(const char *funcname) >> +{ >> +#ifdef DEBUG_EFI >> + printf("EFI: App called into unimplemented function %s\n", funcname); >> +#endif >> + return EFI_EXIT(EFI_UNSUPPORTED); >> +} >> + >> +static int guidcmp(const efi_guid_t *g1, const efi_guid_t *g2) >> +{ >> + return memcmp(g1, g2, sizeof(efi_guid_t)); >> +} > > Not necessary for this set, but this could be useful as a generic > function. (at least part_efi.c could be another consumer) > >> + >> +static unsigned long EFIAPI efi_raise_tpl(unsigned long new_tpl) >> +{ >> + EFI_ENTRY("0x%lx", new_tpl); >> + return EFI_EXIT(0); >> +} >> + >> +static void EFIAPI efi_restore_tpl(unsigned long old_tpl) >> +{ >> + EFI_ENTRY("0x%lx", old_tpl); >> + EFI_EXIT(efi_unsupported(__func__)); >> +} >> + >> +efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type, >> + unsigned long pages, >> + uint64_t *memory) >> +{ >> + efi_status_t r; >> + >> + EFI_ENTRY("%d, %d, 0x%lx, %p", type, memory_type, pages, memory); >> + r = efi_allocate_pages(type, memory_type, pages, memory); >> + return EFI_EXIT(r); >> +} >> + >> +efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory, unsigned long pages) >> +{ >> + efi_status_t r; >> + >> + EFI_ENTRY("%"PRIx64", 0x%lx", memory, pages); >> + r = efi_free_pages(memory, pages); >> + return EFI_EXIT(r); >> +} >> + >> +efi_status_t EFIAPI efi_get_memory_map_ext(unsigned long *memory_map_size, >> + struct efi_mem_desc *memory_map, >> + unsigned long *map_key, >> + unsigned long *descriptor_size, >> + uint32_t *descriptor_version) >> +{ >> + efi_status_t r; >> + >> + EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map, >> + map_key, descriptor_size, descriptor_version); >> + r = efi_get_memory_map(memory_map_size, memory_map, map_key, >> + descriptor_size, descriptor_version); >> + return EFI_EXIT(r); >> +} >> + >> +static efi_status_t EFIAPI efi_allocate_pool(int pool_type, unsigned long >> size, >> + void **buffer) >> +{ >> + return efi_allocate_pages(0, pool_type, (size + 0xfff) >> 12, >> (void*)buffer); >> +} >> + >> +static efi_status_t EFIAPI efi_free_pool(void *buffer) >> +{ >> + return efi_free_pages((ulong)buffer, 0); >> +} >> + >> +/* >> + * Our event capabilities are very limited. Only support a single >> + * event to exist, so we don't need to maintain lists. >> + */ >> +static struct { >> + enum efi_event_type type; >> + u32 trigger_type; >> + u32 trigger_time; >> + u64 trigger_next; >> + unsigned long notify_tpl; >> + void (*notify_function) (void *event, void *context); >> + void *notify_context; >> +} efi_event = { >> + /* Disable timers on bootup */ >> + .trigger_next = -1ULL, >> +}; >> + >> +static efi_status_t EFIAPI efi_create_event( >> + enum efi_event_type type, ulong notify_tpl, >> + void (*notify_function) (void *event, void *context), >> + void *notify_context, void **event) >> +{ >> + EFI_ENTRY("%d, 0x%lx, %p, %p", type, notify_tpl, notify_function, >> + notify_context); >> + if (efi_event.notify_function) { >> + /* We only support one event at a time */ >> + return EFI_EXIT(EFI_OUT_OF_RESOURCES); >> + } >> + >> + efi_event.type = type; >> + efi_event.notify_tpl = notify_tpl; >> + efi_event.notify_function = notify_function; >> + efi_event.notify_context = notify_context; >> + *event = &efi_event; >> + >> + return EFI_EXIT(EFI_SUCCESS); >> +} >> + >> +/* >> + * Our timers have to work without interrupts, so we check whenever keyboard >> + * input or disk accesses happen if enough time elapsed for it to fire. >> + */ >> +void efi_timer_check(void) >> +{ >> + u64 now = timer_get_us(); >> + >> + if (now >= efi_event.trigger_next) { >> + /* Triggering! */ >> + if (efi_event.trigger_type == EFI_TIMER_PERIODIC) >> + efi_event.trigger_next += efi_event.trigger_time / 10; >> + efi_event.notify_function(&efi_event, efi_event.notify_context); >> + } >> + >> + WATCHDOG_RESET(); >> +} >> + >> +static efi_status_t EFIAPI efi_set_timer(void *event, int type, >> + uint64_t trigger_time) >> +{ >> + /* We don't have 64bit division available everywhere, so limit timer >> + * distances to 32bit bits. */ >> + u32 trigger32 = trigger_time; >> + >> + EFI_ENTRY("%p, %d, %"PRIx64, event, type, trigger_time); >> + >> + if (trigger32 < trigger_time) { >> + printf("WARNING: Truncating timer from %"PRIx64" to %x\n", >> + trigger_time, trigger32); >> + } >> + >> + if (event != &efi_event) { >> + /* We only support one event at a time */ >> + return EFI_EXIT(EFI_INVALID_PARAMETER); >> + } >> + >> + switch (type) { >> + case EFI_TIMER_STOP: >> + efi_event.trigger_next = -1ULL; >> + break; >> + case EFI_TIMER_PERIODIC: >> + case EFI_TIMER_RELATIVE: >> + efi_event.trigger_next = timer_get_us() + (trigger32 / 10); >> + break; >> + default: >> + return EFI_EXIT(EFI_INVALID_PARAMETER); >> + } >> + efi_event.trigger_type = type; >> + efi_event.trigger_time = trigger_time; >> + >> + return EFI_EXIT(EFI_SUCCESS); >> +} >> + >> +static efi_status_t EFIAPI efi_wait_for_event(unsigned long num_events, >> + void *event, unsigned long *index) >> +{ >> + u64 now; >> + >> + EFI_ENTRY("%ld, %p, %p", num_events, event, index); >> + >> + now = timer_get_us(); >> + while (now < efi_event.trigger_next) { } >> + efi_timer_check(); >> + >> + return EFI_EXIT(EFI_SUCCESS); >> +} >> + >> +static efi_status_t EFIAPI efi_signal_event(void *event) >> +{ >> + EFI_ENTRY("%p", event); >> + return EFI_EXIT(EFI_SUCCESS); >> +} >> + >> +static efi_status_t EFIAPI efi_close_event(void *event) >> +{ >> + EFI_ENTRY("%p", event); >> + efi_event.trigger_next = -1ULL; >> + return EFI_EXIT(EFI_SUCCESS); >> +} >> + >> +static efi_status_t EFIAPI efi_check_event(void *event) >> +{ >> + EFI_ENTRY("%p", event); >> + return EFI_EXIT(EFI_NOT_READY); >> +} >> + >> +static efi_status_t EFIAPI efi_install_protocol_interface(void **handle, >> + efi_guid_t *protocol, int protocol_interface_type, >> + void *protocol_interface) >> +{ >> + EFI_ENTRY("%p, %p, %d, %p", handle, protocol, protocol_interface_type, >> + protocol_interface); >> + return EFI_EXIT(EFI_OUT_OF_RESOURCES); >> +} >> +static efi_status_t EFIAPI efi_reinstall_protocol_interface(void *handle, >> + efi_guid_t *protocol, void *old_interface, >> + void *new_interface) >> +{ >> + EFI_ENTRY("%p, %p, %p, %p", handle, protocol, old_interface, >> + new_interface); >> + return EFI_EXIT(EFI_ACCESS_DENIED); >> +} >> + >> +static efi_status_t EFIAPI efi_uninstall_protocol_interface(void *handle, >> + efi_guid_t *protocol, void *protocol_interface) >> +{ >> + EFI_ENTRY("%p, %p, %p", handle, protocol, protocol_interface); >> + return EFI_EXIT(EFI_NOT_FOUND); >> +} >> + >> +static efi_status_t EFIAPI efi_register_protocol_notify(efi_guid_t >> *protocol, >> + void *event, >> + void **registration) >> +{ >> + EFI_ENTRY("%p, %p, %p", protocol, event, registration); >> + return EFI_EXIT(EFI_OUT_OF_RESOURCES); >> +} >> + >> +static int efi_search(enum efi_locate_search_type search_type, >> + efi_guid_t *protocol, void *search_key, >> + struct efi_object *efiobj) >> +{ >> + int i; >> + >> + switch (search_type) { >> + case all_handles: >> + return 0; >> + case by_register_notify: >> + return -1; >> + case by_protocol: >> + for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) { >> + const efi_guid_t *guid = efiobj->protocols[i].guid; >> + if (guid && !guidcmp(guid, protocol)) >> + return 0; >> + } >> + return -1; >> + } >> + >> + return -1; >> +} >> + >> +static efi_status_t EFIAPI efi_locate_handle( >> + enum efi_locate_search_type search_type, >> + efi_guid_t *protocol, void *search_key, >> + unsigned long *buffer_size, efi_handle_t *buffer) >> +{ >> + struct list_head *lhandle; >> + unsigned long size = 0; >> + >> + EFI_ENTRY("%d, %p, %p, %p, %p", search_type, protocol, search_key, >> + buffer_size, buffer); >> + >> + /* Count how much space we need */ >> + list_for_each(lhandle, &efi_obj_list) { >> + struct efi_object *efiobj; >> + efiobj = list_entry(lhandle, struct efi_object, link); >> + if (!efi_search(search_type, protocol, search_key, efiobj)) { >> + size += sizeof(void*); >> + } >> + } >> + >> + if (*buffer_size < size) { >> + *buffer_size = size; >> + return EFI_EXIT(EFI_BUFFER_TOO_SMALL); >> + } >> + >> + /* Then fill the array */ >> + list_for_each(lhandle, &efi_obj_list) { >> + struct efi_object *efiobj; >> + efiobj = list_entry(lhandle, struct efi_object, link); >> + if (!efi_search(search_type, protocol, search_key, efiobj)) { >> + *(buffer++) = efiobj->handle; >> + } >> + } >> + >> + *buffer_size = size; >> + return EFI_EXIT(EFI_SUCCESS); >> +} >> + >> +static efi_status_t EFIAPI efi_locate_device_path(efi_guid_t *protocol, >> + struct efi_device_path **device_path, >> + efi_handle_t *device) >> +{ >> + EFI_ENTRY("%p, %p, %p", protocol, device_path, device); >> + return EFI_EXIT(EFI_NOT_FOUND); >> +} >> + >> +static efi_status_t EFIAPI efi_install_configuration_table(efi_guid_t *guid, >> + void *table) >> +{ >> + int i; >> + >> + EFI_ENTRY("%p, %p", guid, table); >> + >> + /* Check for guid override */ >> + for (i = 0; i < systab.nr_tables; i++) { >> + if (!guidcmp(guid, &efi_conf_table[i].guid)) { >> + efi_conf_table[i].table = table; >> + return EFI_EXIT(EFI_SUCCESS); >> + } >> + } >> + >> + /* No override, check for overflow */ >> + if (i >= ARRAY_SIZE(efi_conf_table)) >> + return EFI_EXIT(EFI_OUT_OF_RESOURCES); >> + >> + /* Add a new entry */ >> + memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid)); >> + efi_conf_table[i].table = table; >> + systab.nr_tables = i; >> + >> + return EFI_EXIT(EFI_SUCCESS); >> +} >> + >> +static efi_status_t EFIAPI efi_load_image(bool boot_policy, >> + efi_handle_t parent_image, >> + struct efi_device_path *file_path, >> + void *source_buffer, >> + unsigned long source_size, >> + efi_handle_t *image_handle) >> +{ >> + static struct efi_object loaded_image_info_obj = { >> + .protocols = { >> + { >> + .guid = &efi_guid_loaded_image, >> + .open = &efi_return_handle, >> + }, >> + }, >> + }; >> + struct efi_loaded_image *info; >> + struct efi_object *obj; >> + >> + EFI_ENTRY("%d, %p, %p, %p, %ld, %p", boot_policy, parent_image, >> + file_path, source_buffer, source_size, image_handle); >> + info = malloc(sizeof(*info)); >> + obj = malloc(sizeof(loaded_image_info_obj)); >> + memset(info, 0, sizeof(*info)); >> + memcpy(obj, &loaded_image_info_obj, sizeof(loaded_image_info_obj)); >> + obj->handle = info; >> + info->file_path = file_path; >> + info->reserved = efi_load_pe(source_buffer, info); >> + if (!info->reserved) { >> + free(info); >> + free(obj); >> + return EFI_EXIT(EFI_UNSUPPORTED); >> + } >> + >> + *image_handle = info; >> + list_add_tail(&obj->link, &efi_obj_list); >> + >> + return EFI_EXIT(EFI_SUCCESS); >> +} >> + >> +static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle, >> + unsigned long *exit_data_size, >> + s16 **exit_data) >> +{ >> + ulong (*entry)(void *image_handle, struct efi_system_table *st); >> + struct efi_loaded_image *info = image_handle; >> + >> + EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data); >> + entry = info->reserved; >> + >> + efi_is_direct_boot = false; >> + >> + /* call the image! */ >> + entry(image_handle, &systab); >> + >> + /* Should usually never get here */ >> + return EFI_EXIT(EFI_SUCCESS); >> +} >> + >> +static efi_status_t EFIAPI efi_exit(void *image_handle, long exit_status, >> + unsigned long exit_data_size, >> + uint16_t *exit_data) >> +{ >> + EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status, >> + exit_data_size, exit_data); >> + return EFI_EXIT(efi_unsupported(__func__)); >> +} >> + >> +static struct efi_object *efi_search_obj(void *handle) >> +{ >> + struct list_head *lhandle; >> + >> + list_for_each(lhandle, &efi_obj_list) { >> + struct efi_object *efiobj; >> + efiobj = list_entry(lhandle, struct efi_object, link); >> + if (efiobj->handle == handle) >> + return efiobj; >> + } >> + >> + return NULL; >> +} >> + >> +static efi_status_t EFIAPI efi_unload_image(void *image_handle) >> +{ >> + struct efi_object *efiobj; >> + >> + EFI_ENTRY("%p", image_handle); >> + efiobj = efi_search_obj(image_handle); >> + if (efiobj) >> + list_del(&efiobj->link); >> + >> + return EFI_EXIT(EFI_SUCCESS); >> +} >> + >> +static void efi_exit_caches(void) >> +{ >> +#if defined(CONFIG_ARM) && !defined(CONFIG_ARM64) >> + /* >> + * Grub on 32bit ARM needs to have caches disabled before jumping into >> + * a zImage, but does not know of all cache layers. Give it a hand. >> + */ > > Should we even be doing this hack? > EFI stub support for 32-bit ARM is now (finally) upstream. See above :). We can talk about removing the hack a few years down the road, when all kernels out in the wild have an efi stub. Alex _______________________________________________ U-Boot mailing list U-Boot@lists.denx.de http://lists.denx.de/mailman/listinfo/u-boot