EFI payload / application support
Subsystems (2)
v4 cover EFI payload / application support
This is my (now very late) Christmas present for my openSUSE friends :).
U-Boot is a great project for embedded devices. However, convincing everyone involved that only for "a few oddball ARM devices" we need to support different configuration formats from grub2 when all other platforms (PPC, System Z, x86) are standardized on a single format is a nightmare.
So we started to explore alternatives. At first, people tried to get grub2 running using the u-boot api interface. However, FWIW that one doesn't support relocations, so you need to know where to link grub2 to at compile time. It also seems to be broken more often than not. And on top of it all, it's a one-off interface, so yet another thing to maintain.
That led to a nifty idea. What if we can just implement the EFI application protocol on top of U-Boot? Then we could compile a single grub2 binary for uEFI based systems and U-Boot based systems and as soon as that one's loaded, everything looks and feels (almost) the same.
This patch set is the result of pursuing this endeavor.
- I am successfully able to run grub2 and Linux EFI binaries with this code.
- When enabled, the resulting U-Boot binary only grows by ~10kb, so it's very light weight.
- It works on 32bit ARM and AArch64.
- All storage devices are directly accessible
- No EFI variables
- Removable media booting (search for /efi/boot/boota{a64,arm}.efi)
- Everything in place for runtime service support
Of course, there are still a few things one could do on top:
- Improve disk media detection (don't scan, use what information we have)
- Add EFI variable support using NVRAM
- Add GFX support
- Make EFI Shell work ;)
- Network device support
- Support for payload exit
But so far, I'm very happy with the state of the patches. They completely eliminate potential arguments against U-Boot internally and give users the chance to run with the same level of comfort on all firmware types.
Version 4 was successfully tested to boot grub2 and Linux from there on a HiKey. Please apply the arm64 mmu patch set if you want to run this on an AArch64 system.
If you read this far and want to try out the patches, feel free to grab the source from git at:
https://github.com/agraf/u-boot efi-v4
v1 -> v2:
- move memory allocation to separate patch
- limit 32/64 to hosts that support it
- check 32bit optional nt header magic
- switch to GPLv2+
- 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
- Add runtime detach
- Enable runtime relocations
- Add get_time
- Fix relocation
- Fix 32bit
- Add am335x support
- Move section definition to header
- Add systab to runtime section
- Add self-relocation hook table
- Relocate efi_runtime section early during bootup
- Fix return values for a number of callbacks to be more UEFI compliant
- Move to block_drvr array
- Fix header order
- Document efi block object struct
- Use calloc rather than malloc & memset
- Default to y
- New patches:
- disk/part.c: Expose list of available block drivers
- arm64: Allow exceptions to return
- arm64: Allow EFI payload code to take exceptions
- efi_loader: Add DCACHE_OFF support for arm64
- efi_loader: Add distro boot script for removable media
v2 -> v3:
- Add EFIAPI to notify_function
- Add access denied code
- use efi_alloc
- add EFIAPI to function prototypes
- remove unused macros
- reorder header inclusion
- split relocation code into function
- flush cache after loading
- 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
- Add EFIAPI to function prototypes
- Patch reset to NULL
- Add EFIAPI to function prototypes
- Document header
- Add dm.h include
- Remove non-dm rtc support
- Return DEVICE_ERROR in rtc path
- Adapt to newer u-boot block API
- Add EFIAPI to function prototypes
- Document header
- Check for DEV_TYPE_UNKNOWN
- Document 16byte limit for dp string
- Move to new cmd directory
- Add kconfig option
- Fix comment style
- Add help text
- s/-1/-ENOENT
- Move obj list to lib
- Rewrite memory allocation and map
- Document header
- Add memory file
- New files:
- README patch
- MAINTAINERS patch
v3 -> v4:
- Add EFI_PAGE_MASK define
- Use EFI_PAGE_SHIFT define
- Flush icache after image load
- Fix white space in pe header
- s/polimorphic/polymorphic
- return EFI_SUCCESS in efi_cout_enable_cursor()
- Use EFI_PAGE_.* defines
- Return EFI_OUT_OF_RESOURCES in efi_allocate_pages
- Add readme section about config options
- s/10kb/10KB/
Alex
Alexander Graf (15):
disk/part.c: Expose list of available block drivers include/efi_api.h: Add more detailed API definitions
efi_loader: Add PE image loader
efi_loader: Add boot time services
efi_loader: Add console interface
efi_loader: Add runtime services
efi_loader: Add disk interfaces
efi_loader: Add "bootefi" command
efi_loader: Implement memory allocation and map
arm64: Allow exceptions to return
arm64: Allow EFI payload code to take exceptions
efi_loader: hook up in build environment
efi_loader: Add distro boot script for removable media
efi_loader: Add README section in README.efi
efi_loader: Add MAINTAINERS entry
MAINTAINERS | 7 + arch/arm/config.mk | 4 + arch/arm/cpu/armv8/exceptions.S | 34 ++ arch/arm/cpu/armv8/u-boot.lds | 16 + arch/arm/cpu/u-boot.lds | 30 ++ arch/arm/lib/interrupts_64.c | 9 + arch/arm/lib/sections.c | 4 + board/ti/am335x/u-boot.lds | 30 ++ cmd/Kconfig | 7 + cmd/Makefile | 1 + cmd/bootefi.c | 167 ++++++++ common/board_r.c | 7 + disk/part.c | 7 +- doc/README.efi | 83 +++- include/config_distro_bootcmd.h | 47 ++- include/efi.h | 2 + include/efi_api.h | 198 ++++++++-- include/efi_loader.h | 147 +++++++ include/part.h | 8 + include/pe.h | 263 +++++++++++++ lib/Kconfig | 1 + lib/Makefile | 1 + lib/efi_loader/Kconfig | 9 + lib/efi_loader/Makefile | 12 + lib/efi_loader/efi_boottime.c | 781 ++++++++++++++++++++++++++++++++++++++ lib/efi_loader/efi_console.c | 360 ++++++++++++++++++ lib/efi_loader/efi_disk.c | 218 +++++++++++ lib/efi_loader/efi_image_loader.c | 183 +++++++++ lib/efi_loader/efi_memory.c | 319 ++++++++++++++++ lib/efi_loader/efi_runtime.c | 290 ++++++++++++++ 30 files changed, 3200 insertions(+), 45 deletions(-) create mode 100644 cmd/bootefi.c create mode 100644 include/efi_loader.h create mode 100644 include/pe.h create mode 100644 lib/efi_loader/Kconfig create mode 100644 lib/efi_loader/Makefile create mode 100644 lib/efi_loader/efi_boottime.c create mode 100644 lib/efi_loader/efi_console.c create mode 100644 lib/efi_loader/efi_disk.c create mode 100644 lib/efi_loader/efi_image_loader.c create mode 100644 lib/efi_loader/efi_memory.c create mode 100644 lib/efi_loader/efi_runtime.c
Signature · 1 line
1.8.5.6
PATCH v4 1/15 disk/part.c: Expose list of available block drivers
We have a pretty nice and generic interface to ask for a specific block device. However, that one is still based around the magic notion that we know the driver name.
In order to be able to write fully generic disk access code, expose the currently internal list to other source files so that they can scan through all available block drivers.
--- disk/part.c | 7 +------ include/part.h | 8 ++++++++ 2 files changed, 9 insertions(+), 6 deletions(-)
disk/part.c +1 -6
@@ -20,13 +20,8 @@ #define PRINTF(fmt,args...)#endif -struct block_drvr { -char *name; -block_dev_desc_t * (*get_dev)(int dev); -int (*select_hwpart)(int dev_num,int hwpart); -}; -static const struct block_drvr block_drvr[] = { +const struct block_drvr block_drvr[] = {#if defined(CONFIG_CMD_IDE ) { .name = "ide", .get_dev = ide_get_dev, }, #endif
include/part.h +8 -0
@@ -42,6 +42,12 @@ struct block_dev_desc {void *priv;/* driver private struct pointer */ }; +struct block_drvr { +char *name; +block_dev_desc_t * (*get_dev)(int dev); +int (*select_hwpart)(int dev_num,int hwpart); +}; +#define BLOCK_CNT (size, block_dev_desc) (PAD_COUNT(size, block_dev_desc->blksz))#define PAD_TO_BLOCKSIZE (size, block_dev_desc) \ (PAD_SIZE(size, block_dev_desc->blksz))
@@ -123,6 +129,8 @@ int get_device(const char *ifname, const char *dev_str,int get_device_and_partition (const char *ifname,const char *dev_part_str,block_dev_desc_t **dev_desc,disk_partition_t *info,int allow_whole_dev); + +extern const struct block_drvr block_drvr[];#else static inline block_dev_desc_t *get_dev (const char *ifname,int dev) {return NULL ; }
Signature · 1 line
1.8.5.6
PATCH v4 2/15 include/efi_api.h: Add more detailed API definitions
In reply to Alexander GrafThe EFI API header is great, but missing a good chunk of function prototype, GUID defines and enum declarations.
This patch extends it to cover more of the EFI API. It's still not 100% complete, but sufficient enough for our EFI payload interface.
---
v2 -> v3:
- Add EFIAPI to notify_function
- Add access denied code
v3 -> v4:
- Add EFI_PAGE_MASK define
--- include/efi.h | 2 + include/efi_api.h | 198 ++++++++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 163 insertions(+), 37 deletions(-)
include/efi.h +2 -0
@@ -38,6 +38,7 @@ struct efi_device_path;#define EFI_WRITE_PROTECTED (8 | (1UL << (BITS_PER_LONG - 1)))#define EFI_OUT_OF_RESOURCES (9 | (1UL << (BITS_PER_LONG - 1)))#define EFI_NOT_FOUND (14 | (1UL << (BITS_PER_LONG - 1))) +#define EFI_ACCESS_DENIED (15 | (1UL << (BITS_PER_LONG - 1)))#define EFI_SECURITY_VIOLATION (26 | (1UL << (BITS_PER_LONG - 1)))typedef unsigned long efi_status_t ;
@@ -139,6 +140,7 @@ enum {#define EFI_PAGE_SHIFT 12#define EFI_PAGE_SIZE (1UL << EFI_PAGE_SHIFT) +#define EFI_PAGE_MASK (EFI_PAGE_SIZE - 1)struct efi_mem_desc {u32 type;
include/efi_api.h +161 -37
305 lines not on this page · raw message
Signature · 1 line
1.8.5.6
PATCH v4 3/15 efi_loader: Add PE image loader
In reply to Alexander GrafEFI uses the PE binary format for its application images. Add support to EFI PE binaries as well as all necessary bits for the "EFI image loader" interfaces.
---
v1 -> v2:
- move memory allocation to separate patch
- limit 32/64 to hosts that support it
- check 32bit optional nt header magic
- switch to GPL2+
v2 -> v3:
- use efi_alloc
- add EFIAPI to function prototypes
- remove unused macros
- reorder header inclusion
- split relocation code into function
- flush cache after loading
v3 -> v4:
- Use EFI_PAGE_SHIFT define
- Flush icache after image load
- Fix white space in pe header
--- include/efi_loader.h | 20 +++ include/pe.h | 263 ++++++++++++++++++++++++++++++++++++++ lib/efi_loader/efi_image_loader.c | 183 ++++++++++++++++++++++++++ 3 files changed, 466 insertions(+) create mode 100644 include/efi_loader.h create mode 100644 include/pe.h create mode 100644 lib/efi_loader/efi_image_loader.c
include/efi_loader.h new file +20 -0
@@ -0,0 +1,20 @@ +/* +* EFI application loader +* +* Copyright (c) 2016 Alexander Graf +* +* SPDX-License-Identifier: GPL-2.0+ +*/ + +#include <part_efi.h> +#include <efi_api.h> +#include <linux/list.h> + +extern const efi_guid_t efi_guid_device_path; +extern const efi_guid_t efi_guid_loaded_image; + +efi_status_t efi_return_handle (void *handle, +efi_guid_t *protocol,void **protocol_interface, +void *agent_handle,void *controller_handle, +uint32_t attributes); +void *efi_load_pe (void *efi,struct efi_loaded_image *loaded_image_info);
include/pe.h new file +263 -0
263 lines not on this page · raw message
lib/efi_loader/efi_image_loader.c new file +183 -0
183 lines not on this page · raw message
Signature · 1 line
1.8.5.6
PATCH v4 4/15 efi_loader: Add boot time services
In reply to Alexander GrafWhen 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.
---
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
v3 -> v4:
- s/polimorphic/polymorphic
--- 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
include/efi_loader.h +84 -0
@@ -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 polymorphic and have +* 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
lib/efi_loader/efi_boottime.c new file +781 -0
781 lines not on this page · raw message
Signature · 1 line
1.8.5.6
PATCH v4 5/15 efi_loader: Add console interface
In reply to Alexander GrafOne of the basic EFI interfaces is the console interface. Using it an EFI application can interface with the user. This patch implements an EFI console interface using getc() and putc().
Today, we only implement text based consoles. We also convert the EFI Unicode characters to UTF-8 on the fly, hoping that everyone managed to jump on the train by now.
---
v1 -> v2:
- Move to GPLv2+
v2 -> v3:
- Add EFIAPI to function prototypes
v3 -> v4:
- return EFI_SUCCESS in efi_cout_enable_cursor()
--- include/efi_loader.h | 5 + lib/efi_loader/efi_console.c | 360 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 365 insertions(+) create mode 100644 lib/efi_loader/efi_console.c
include/efi_loader.h +5 -0
@@ -31,6 +31,11 @@extern struct efi_system_table systab; +extern const struct efi_simple_text_output_protocol efi_con_out; +extern const struct efi_simple_input_interface efi_con_in; +extern const struct efi_console_control_protocol efi_console_control; + +extern const efi_guid_t efi_guid_console_control;extern const efi_guid_t efi_guid_device_path;extern const efi_guid_t efi_guid_loaded_image;
lib/efi_loader/efi_console.c new file +360 -0
360 lines not on this page · raw message
Signature · 1 line
1.8.5.6
PATCH v4 6/15 efi_loader: Add runtime services
In reply to Alexander GrafAfter booting has finished, EFI allows firmware to still interact with the OS using the "runtime services". These callbacks live in a separate address space, since they are available long after U-Boot has been overwritten by the OS.
This patch adds enough framework for arbitrary code inside of U-Boot to become a runtime service with the right section attributes set. For now, we don't make use of it yet though.
We could maybe in the future map U-boot environment variables to EFI variables here.
---
v1 -> v2:
- Fix runtime service sections
- Add runtime detach
- Enable runtime relocations
- Add get_time
- Fix relocation
- Fix 32bit
- Add am335x support
- Move section definition to header
- Add systab to runtime section
- Add self-relocation hook table
- Fix self-relocation
- Relocate efi_runtime section early during bootup
- Fix return values for a number of callbacks to be more UEFI compliant
- Move to GPLv2+
v2 -> v3:
- Patch reset to NULL
- Add EFIAPI to function prototypes
- Document header
- Add dm.h include
- Remove non-dm rtc support
- Return DEVICE_ERROR in rtc path
--- arch/arm/config.mk | 4 + arch/arm/cpu/armv8/u-boot.lds | 16 +++ arch/arm/cpu/u-boot.lds | 30 +++++ arch/arm/lib/sections.c | 4 + board/ti/am335x/u-boot.lds | 30 +++++ common/board_r.c | 4 + include/efi_loader.h | 17 +++ lib/efi_loader/efi_boottime.c | 6 +- lib/efi_loader/efi_runtime.c | 290 ++++++++++++++++++++++++++++++++++++++++++ 9 files changed, 398 insertions(+), 3 deletions(-) create mode 100644 lib/efi_loader/efi_runtime.c
arch/arm/config.mk +4 -0
@@ -122,6 +122,10 @@ ifdef CONFIG_OF_EMBED OBJCOPYFLAGS += -j .dtb.init.rodata endif +ifdef CONFIG_EFI_LOADER +OBJCOPYFLAGS += -j .efi_runtime -j .efi_runtime_rel +endif + ifneq ($(CONFIG_IMX_CONFIG),) ifdef CONFIG_SPL ifndef CONFIG_SPL_BUILD
arch/arm/cpu/armv8/u-boot.lds +16 -0
@@ -42,6 +42,22 @@ SECTIONS . = ALIGN(8); + .efi_runtime : { + __efi_runtime_start = .; + *(efi_runtime_text) + *(efi_runtime_data) + __efi_runtime_stop = .; + } + + .efi_runtime_rel : { + __efi_runtime_rel_start = .; + *(.relaefi_runtime_text) + *(.relaefi_runtime_data) + __efi_runtime_rel_stop = .; + } + + . = ALIGN(8); + .image_copy_end : { *(.__image_copy_end)
arch/arm/cpu/u-boot.lds +30 -0
@@ -90,6 +90,36 @@ SECTIONS . = ALIGN(4); + .__efi_runtime_start : { + *(.__efi_runtime_start) + } + + .efi_runtime : { + *(efi_runtime_text) + *(efi_runtime_data) + } + + .__efi_runtime_stop : { + *(.__efi_runtime_stop) + } + + .efi_runtime_rel_start : + { + *(.__efi_runtime_rel_start) + } + + .efi_runtime_rel : { + *(.relefi_runtime_text) + *(.relefi_runtime_data) + } + + .efi_runtime_rel_stop : + { + *(.__efi_runtime_rel_stop) + } + + . = ALIGN(4); + .image_copy_end : { *(.__image_copy_end)
arch/arm/lib/sections.c +4 -0
@@ -27,4 +27,8 @@ char __rel_dyn_start[0] __attribute__((section(".__rel_dyn_start"))); char __rel_dyn_end[0] __attribute__((section(".__rel_dyn_end"))); char __secure_start[0] __attribute__((section(".__secure_start"))); char __secure_end[0] __attribute__((section(".__secure_end"))); +char __efi_runtime_start[0] __attribute__((section(".__efi_runtime_start"))); +char __efi_runtime_stop[0] __attribute__((section(".__efi_runtime_stop"))); +char __efi_runtime_rel_start[0] __attribute__((section(".__efi_runtime_rel_start"))); +char __efi_runtime_rel_stop[0] __attribute__((section(".__efi_runtime_rel_stop"))); char _end[0] __attribute__((section(".__end")));
board/ti/am335x/u-boot.lds +30 -0
@@ -59,6 +59,36 @@ SECTIONS . = ALIGN(4); + .__efi_runtime_start : { + *(.__efi_runtime_start) + } + + .efi_runtime : { + *(efi_runtime_text) + *(efi_runtime_data) + } + + .__efi_runtime_stop : { + *(.__efi_runtime_stop) + } + + .efi_runtime_rel_start : + { + *(.__efi_runtime_rel_start) + } + + .efi_runtime_rel : { + *(.relefi_runtime_text) + *(.relefi_runtime_data) + } + + .efi_runtime_rel_stop : + { + *(.__efi_runtime_rel_stop) + } + + . = ALIGN(4); + .image_copy_end : { *(.__image_copy_end)
common/board_r.c +4 -0
@@ -64,6 +64,7 @@#ifdef CONFIG_AVR32 #include <asm/arch/mmu.h> #endif +#include <efi_loader.h> DECLARE_GLOBAL_DATA_PTR ;
@@ -176,6 +177,9 @@ static int initr_reloc_global_data(void)*/ gd->fdt_blob += gd->reloc_off;#endif +#ifdef CONFIG_EFI_LOADER +efi_runtime_relocate (gd->relocaddr,NULL ); +#endif return 0 ; }
include/efi_loader.h +17 -0
@@ -29,6 +29,7 @@#define EFI_EXIT (ret) efi_exit_func(ret); +extern struct efi_runtime_services efi_runtime_services;extern struct efi_system_table systab;extern const struct efi_simple_text_output_protocol efi_con_out;
@@ -39,6 +40,9 @@ extern const efi_guid_t efi_guid_console_control;extern const efi_guid_t efi_guid_device_path;extern const efi_guid_t efi_guid_loaded_image; +extern unsigned int __efi_runtime_start, __efi_runtime_stop; +extern unsigned int __efi_runtime_rel_start, __efi_runtime_rel_stop; +/* * While UEFI objects can have callbacks, you can also call functions on * protocols (classes) themselves. This struct maps a protocol GUID to its
@@ -100,9 +104,22 @@ void efi_save_gd(void);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); +/* Call this to relocate the runtime section to an address space */ +void efi_runtime_relocate (ulong offset,struct efi_mem_desc *map); + +/* +* Use these to indicate that your code / data should go into the EFI runtime +* section and thus still be available when the OS is running +*/ +#define EFI_RUNTIME_DATA __attribute__ ((section ("efi_runtime_data"))) +#define EFI_RUNTIME_TEXT __attribute__ ((section ("efi_runtime_text")))#else /* defined(EFI_LOADER) */ +/* Without CONFIG_EFI_LOADER we don't have a runtime section, stub it out */ +#define EFI_RUNTIME_DATA +#define EFI_RUNTIME_TEXT +/* No loader configured, stub out EFI_ENTRY */ static inline void efi_restore_gd (void ) { }
lib/efi_loader/efi_boottime.c +3 -3
@@ -39,7 +39,7 @@ static bool efi_is_direct_boot = true;* 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 ]; +static struct efi_configuration_table EFI_RUNTIME_DATA efi_conf_table[1];/* * The "gd" pointer lives in a register on ARM and AArch64 that we declare
@@ -761,10 +761,10 @@ static const struct efi_boot_services efi_boot_services = { }; -static uint16_t firmware_vendor[] = +static uint16_t EFI_RUNTIME_DATA firmware_vendor[] = {'D' ,'a' ,'s' ,' ' ,'U' ,'-' ,'b' ,'o' ,'o' ,'t' ,0 }; -struct efi_system_table systab = { +struct efi_system_table EFI_RUNTIME_DATA systab = { .hdr = { .signature =EFI_SYSTEM_TABLE_SIGNATURE , .revision =0x20005 ,/* 2.5 */
lib/efi_loader/efi_runtime.c new file +290 -0
290 lines not on this page · raw message
Signature · 1 line
1.8.5.6
PATCH v4 7/15 efi_loader: Add disk interfaces
In reply to Alexander GrafA EFI applications usually want to access storage devices to load data from.
This patch adds support for EFI disk interfaces. It loops through all block storage interfaces known to U-Boot and creates an EFI object for each existing one. EFI applications can then through these objects call U-Boot's read and write functions.
---
v1 -> v2:
- Move to block_drvr array
- Move to GPLv2+
- Fix header order
- Document efi block object struct
- Use calloc rather than malloc & memset
v2 -> v3:
- Adapt to newer u-boot block API
- Add EFIAPI to function prototypes
- Document header
- Check for DEV_TYPE_UNKNOWN
- Document 16byte limit for dp string
--- include/efi_loader.h | 2 + lib/efi_loader/efi_disk.c | 218 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 220 insertions(+) create mode 100644 lib/efi_loader/efi_disk.c
include/efi_loader.h +2 -0
@@ -86,6 +86,8 @@ struct efi_object {/* This list contains all UEFI objects we know of */ extern struct list_head efi_obj_list; +/* Called by bootefi to make all disk storage accessible as EFI objects */ +int efi_disk_register (void );/* * Stub implementation for a protocol opener that just returns the handle as * interface
lib/efi_loader/efi_disk.c new file +218 -0
218 lines not on this page · raw message
Signature · 1 line
1.8.5.6
PATCH v4 8/15 efi_loader: Add "bootefi" command
In reply to Alexander GrafIn order to execute an EFI application, we need to bridge the gap between U-Boot's notion of executing images and EFI's notion of doing the same.
The best path forward IMHO here is to stick completely to the way U-Boot deals with payloads. You manually load them using whatever method to RAM and then have a simple boot command to execute them. So in our case, you would do
# load mmc 0:1 $loadaddr grub.efi # bootefi $loadaddr
which then gets you into a grub shell. Fdt information known to U-boot via the fdt addr command is also passed to the EFI payload.
---
v1 -> v2:
- Move to GPLv2+
v2 -> v3:
- Move to new cmd directory
- Add kconfig option
- Fix comment style
- Add help text
- s/-1/-ENOENT
- Move obj list to lib
XXX bootefi: make dtb conf table more explicit
--- cmd/Kconfig | 7 +++ cmd/Makefile | 1 + cmd/bootefi.c | 167 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 175 insertions(+) create mode 100644 cmd/bootefi.c
cmd/Kconfig +7 -0
@@ -148,6 +148,13 @@ config CMD_BOOTM help Boot an application image from the memory. +config CMD_BOOTEFI + bool "bootefi" + depends on EFI_LOADER + default y + help + Boot an EFI image from memory. + config CMD_ELF bool "bootelf, bootvx" default y
cmd/Makefile +1 -0
@@ -21,6 +21,7 @@ obj-$(CONFIG_CMD_SOURCE) += source.o obj-$(CONFIG_CMD_BDI) += bdinfo.o obj-$(CONFIG_CMD_BEDBUG) += bedbug.o obj-$(CONFIG_CMD_BMP) += bmp.o +obj-$(CONFIG_CMD_BOOTEFI) += bootefi.o obj-$(CONFIG_CMD_BOOTMENU) += bootmenu.o obj-$(CONFIG_CMD_BOOTLDR) += bootldr.o obj-$(CONFIG_CMD_BOOTSTAGE) += bootstage.o
cmd/bootefi.c new file +167 -0
167 lines not on this page · raw message
Signature · 1 line
1.8.5.6
PATCH v4 9/15 efi_loader: Implement memory allocation and map
In reply to Alexander GrafThe EFI loader needs to maintain views of memory - general system memory windows as well as used locations inside those and potential runtime service MMIO windows.
To manage all of these, add a few helpers that maintain an internal representation of the map the similar to how the EFI API later on reports it to the application.
For allocations, the scheme is very simple. We basically allow allocations to replace chunks of previously done maps, so that a new LOADER_DATA allocation for example can remove a piece of the RAM map. When no specific address is given, we just take the highest possible address in the lowest RAM map that fits the allocation size.
---
v2 -> v3:
- Rewrite memory allocation and map
- Document header
v3 -> v4:
- Use EFI_PAGE_.* defines
- Return EFI_OUT_OF_RESOURCES in efi_allocate_pages
--- common/board_r.c | 3 + include/efi_loader.h | 19 +++ lib/efi_loader/efi_memory.c | 319 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 341 insertions(+) create mode 100644 lib/efi_loader/efi_memory.c
common/board_r.c +3 -0
@@ -785,6 +785,9 @@ init_fnc_t init_sequence_r[] = {#ifdef CONFIG_CLOCKS set_cpu_clk_info, /* Setup clock information */#endif +#ifdef CONFIG_EFI_LOADER + efi_memory_init, +#endif stdio_init_tables, initr_serial, initr_announce,
include/efi_loader.h +19 -0
@@ -109,6 +109,25 @@ efi_status_t efi_exit_func(efi_status_t ret);/* Call this to relocate the runtime section to an address space */ void efi_runtime_relocate (ulong offset,struct efi_mem_desc *map); +/* Generic EFI memory allocator, call this to get memory */ +void *efi_alloc (uint64_t len,int memory_type); +/* More specific EFI memory allocator, called by EFI payloads */ +efi_status_t efi_allocate_pages (int type,int memory_type,unsigned long pages, +uint64_t *memory); +/* EFI memory free function. Not implemented today */ +efi_status_t efi_free_pages (uint64_t memory,unsigned long pages); +/* Returns the EFI memory map */ +efi_status_t efi_get_memory_map (unsigned long *memory_map_size, +struct efi_mem_desc *memory_map, +unsigned long *map_key, +unsigned long *descriptor_size, +uint32_t *descriptor_version); +/* Adds a range into the EFI memory map */ +uint64_t efi_add_memory_map (uint64_t start,uint64_t pages,int memory_type, +bool overlap_only_ram); +/* Called by board init to initialize the EFI memory map */ +int efi_memory_init (void ); +/* * Use these to indicate that your code / data should go into the EFI runtime * section and thus still be available when the OS is running
lib/efi_loader/efi_memory.c new file +319 -0
319 lines not on this page · raw message
Signature · 1 line
1.8.5.6
PATCH v4 10/15 arm64: Allow exceptions to return
In reply to Alexander GrafOur current arm64 exception handlers all panic and never return to the exception triggering code.
But if any handler wanted to continue execution after fixups, it would need help from the exception handling code to restore all registers.
This patch implements that help. With this code, exception handlers on aarch64 can successfully return to the place the exception happened (or somewhere else if they modify elr).
--- arch/arm/cpu/armv8/exceptions.S | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+)
arch/arm/cpu/armv8/exceptions.S +34 -0
@@ -82,31 +82,65 @@ vectors: _do_bad_sync: exception_entry bl do_bad_sync + b exception_exit _do_bad_irq: exception_entry bl do_bad_irq + b exception_exit _do_bad_fiq: exception_entry bl do_bad_fiq + b exception_exit _do_bad_error: exception_entry bl do_bad_error + b exception_exit _do_sync: exception_entry bl do_sync + b exception_exit _do_irq: exception_entry bl do_irq + b exception_exit _do_fiq: exception_entry bl do_fiq + b exception_exit _do_error: exception_entry bl do_error + b exception_exit + +exception_exit: + ldp x2, x0, [sp],#16 + switch_el x11, 3f, 2f, 1f +3: msr elr_el3, x2 + b 0f +2: msr elr_el2, x2 + b 0f +1: msr elr_el1, x2 +0: + ldp x1, x2, [sp],#16 + ldp x3, x4, [sp],#16 + ldp x5, x6, [sp],#16 + ldp x7, x8, [sp],#16 + ldp x9, x10, [sp],#16 + ldp x11, x12, [sp],#16 + ldp x13, x14, [sp],#16 + ldp x15, x16, [sp],#16 + ldp x17, x18, [sp],#16 + ldp x19, x20, [sp],#16 + ldp x21, x22, [sp],#16 + ldp x23, x24, [sp],#16 + ldp x25, x26, [sp],#16 + ldp x27, x28, [sp],#16 + ldp x29, x30, [sp],#16 + eret
Signature · 1 line
1.8.5.6
PATCH v4 11/15 arm64: Allow EFI payload code to take exceptions
In reply to Alexander GrafThere are 2 ways an EFI payload could return into u-boot:
- Callback function
- Exception
While in EFI payload mode, x18 is owned by the payload and may not contain a valid pointer to gd, so we need to fix it up. We do that properly for the payload to callback path already.
This patch also adds gd pointer restoral for the exception path.
--- arch/arm/lib/interrupts_64.c | 9 +++++++++ 1 file changed, 9 insertions(+)
arch/arm/lib/interrupts_64.c +9 -0
@@ -7,6 +7,7 @@#include <common.h> #include <linux/compiler.h> +#include <efi_loader.h> int interrupt_init (void )
@@ -41,6 +42,7 @@ void show_regs(struct pt_regs *regs)*/ void do_bad_sync (struct pt_regs *pt_regs,unsigned int esr) { +efi_restore_gd ();printf ("Bad mode in \"Synchronous Abort\" handler, esr 0x%08x\n" , esr);show_regs (pt_regs);panic ("Resetting CPU ...\n" );
@@ -51,6 +53,7 @@ void do_bad_sync(struct pt_regs *pt_regs, unsigned int esr)*/ void do_bad_irq (struct pt_regs *pt_regs,unsigned int esr) { +efi_restore_gd ();printf ("Bad mode in \"Irq\" handler, esr 0x%08x\n" , esr);show_regs (pt_regs);panic ("Resetting CPU ...\n" );
@@ -61,6 +64,7 @@ void do_bad_irq(struct pt_regs *pt_regs, unsigned int esr)*/ void do_bad_fiq (struct pt_regs *pt_regs,unsigned int esr) { +efi_restore_gd ();printf ("Bad mode in \"Fiq\" handler, esr 0x%08x\n" , esr);show_regs (pt_regs);panic ("Resetting CPU ...\n" );
@@ -71,6 +75,7 @@ void do_bad_fiq(struct pt_regs *pt_regs, unsigned int esr)*/ void do_bad_error (struct pt_regs *pt_regs,unsigned int esr) { +efi_restore_gd ();printf ("Bad mode in \"Error\" handler, esr 0x%08x\n" , esr);show_regs (pt_regs);panic ("Resetting CPU ...\n" );
@@ -81,6 +86,7 @@ void do_bad_error(struct pt_regs *pt_regs, unsigned int esr)*/ void do_sync (struct pt_regs *pt_regs,unsigned int esr) { +efi_restore_gd ();printf ("\"Synchronous Abort\" handler, esr 0x%08x\n" , esr);show_regs (pt_regs);panic ("Resetting CPU ...\n" );
@@ -91,6 +97,7 @@ void do_sync(struct pt_regs *pt_regs, unsigned int esr)*/ void do_irq (struct pt_regs *pt_regs,unsigned int esr) { +efi_restore_gd ();printf ("\"Irq\" handler, esr 0x%08x\n" , esr);show_regs (pt_regs);panic ("Resetting CPU ...\n" );
@@ -101,6 +108,7 @@ void do_irq(struct pt_regs *pt_regs, unsigned int esr)*/ void do_fiq (struct pt_regs *pt_regs,unsigned int esr) { +efi_restore_gd ();printf ("\"Fiq\" handler, esr 0x%08x\n" , esr);show_regs (pt_regs);panic ("Resetting CPU ...\n" );
@@ -114,6 +122,7 @@ void do_fiq(struct pt_regs *pt_regs, unsigned int esr)*/ void __weak do_error (struct pt_regs *pt_regs,unsigned int esr) { +efi_restore_gd ();printf ("\"Error\" handler, esr 0x%08x\n" , esr);show_regs (pt_regs);panic ("Resetting CPU ...\n" );
Signature · 1 line
1.8.5.6
PATCH v4 12/15 efi_loader: hook up in build environment
In reply to Alexander GrafNow that we have all the bits and pieces ready for EFI payload loading support, hook them up in Makefiles and KConfigs so that we can build.
---
v1 -> v2:
- Move to GPLv2+
- Default to y
v2 -> v3:
- Add memory file
--- lib/Kconfig | 1 + lib/Makefile | 1 + lib/efi_loader/Kconfig | 9 +++++++++ lib/efi_loader/Makefile | 12 ++++++++++++ 4 files changed, 23 insertions(+) create mode 100644 lib/efi_loader/Kconfig create mode 100644 lib/efi_loader/Makefile
lib/Kconfig +1 -0
@@ -129,5 +129,6 @@ config ERRNO_STR - if errno is negative - a pointer to errno related message source lib/efi/Kconfig +source lib/efi_loader/Kconfig endmenu
lib/Makefile +1 -0
@@ -8,6 +8,7 @@ ifndef CONFIG_SPL_BUILD obj-$(CONFIG_EFI) += efi/ +obj-$(CONFIG_EFI_LOADER) += efi_loader/ obj-$(CONFIG_RSA) += rsa/ obj-$(CONFIG_LZMA) += lzma/ obj-$(CONFIG_LZO) += lzo/
lib/efi_loader/Kconfig new file +9 -0
@@ -0,0 +1,9 @@ +config EFI_LOADER + bool "Support running EFI Applications in U-Boot" + depends on ARM64 ||?ARM + default y + help + Select this option if you want to run EFI applications (like grub2) + on top of U-Boot. If this option is enabled, U-Boot will expose EFI + interfaces to a loaded EFI application, enabling it to reuse U-Boot's + device drivers.
lib/efi_loader/Makefile new file +12 -0
@@ -0,0 +1,12 @@ +# +# (C) Copyright 2016 Alexander Graf +# +# SPDX-License-Identifier: GPL-2.0+ +# + +# This file only gets included with CONFIG_EFI_LOADER set, so all +# object inclusion implicitly depends on it + +obj-y += efi_image_loader.o efi_boottime.o efi_runtime.o efi_console.o +obj-y += efi_memory.o +obj-$(CONFIG_PARTITIONS) += efi_disk.o
Signature · 1 line
1.8.5.6
PATCH v4 13/15 efi_loader: Add distro boot script for removable media
In reply to Alexander GrafUEFI defines a simple boot protocol for removable media. There we should look at the EFI (first GPT FAT) partition and search for /efi/boot/bootXXX.efi with XXX being different between different platforms (x86, x64, arm, aa64, ...).
This patch implements a simple version of that protocol for the default distro boot script. With this we can automatically boot from valid UEFI enabled removable media.
Because from all I could see U-Boot by default doesn't deliver device tree blobs with its firmware, we also need to load the dtb from somewhere. Traverse the same EFI partition for an fdt file that fits our current board so that an OS receives a valid device tree when booted automatically.
--- include/config_distro_bootcmd.h | 47 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-)
include/config_distro_bootcmd.h +46 -1
@@ -90,6 +90,48 @@ BOOT_TARGET_DEVICES_references_UBIFS_without_CONFIG_CMD_UBIFS#endif +#ifdef CONFIG_EFI_LOADER +#if defined(CONFIG_ARM64 ) +#define BOOTEFI_NAME "bootaa64.efi" +#elif defined(CONFIG_ARM ) +#define BOOTEFI_NAME "bootarm.efi" +#endif +#endif + +#ifdef BOOTEFI_NAME +#define BOOTENV_SHARED_EFI \ + "boot_efi_binary=" \ + "load ${devtype} ${devnum}:${distro_bootpart} " \ + "${kernel_addr_r} efi/boot/"BOOTEFI_NAME"; " \ + "bootefi ${kernel_addr_r}\0" \ + \ + "load_efi_dtb=" \ + "load ${devtype} ${devnum}:${distro_bootpart} " \ + "${fdt_addr_r} ${prefix}${fdt_name}; " \ + "fdt addr ${fdt_addr_r}\0" \ + \ + "efi_dtb_prefixes=/ /dtb/ /dtb/current/\0" \ + "scan_dev_for_efi=" \ + "for prefix in ${efi_dtb_prefixes}; do " \ + "if test -e ${devtype} " \ + "${devnum}:${distro_bootpart} " \ + "${prefix}${fdt_name}; then " \ + "run load_efi_dtb; " \ + "fi;" \ + "done;" \ + "if test -e ${devtype} ${devnum}:${distro_bootpart} " \ + "efi/boot/"BOOTEFI_NAME"; then " \ + "echo Found EFI removable media binary " \ + "efi/boot/"BOOTEFI_NAME"; " \ + "run boot_efi_binary; " \ + "echo EFI LOAD FAILED: continuing...; " \ + "fi; " +#define SCAN_DEV_FOR_EFI "run scan_dev_for_efi;" +#else +#define BOOTENV_SHARED_EFI +#define SCAN_DEV_FOR_EFI +#endif +#ifdef CONFIG_CMD_SATA #define BOOTENV_SHARED_SATA BOOTENV_SHARED_BLKDEV(sata)#define BOOTENV_DEV_SATA BOOTENV_DEV_BLKDEV
@@ -217,6 +259,7 @@BOOTENV_SHARED_SCSI \ BOOTENV_SHARED_IDE \BOOTENV_SHARED_UBIFS \ + BOOTENV_SHARED_EFI \"boot_prefixes=/ /boot/\0" \"boot_scripts=boot.scr.uimg boot.scr\0" \"boot_script_dhcp=boot.scr.uimg\0" \
@@ -258,7 +301,9 @@"for prefix in ${boot_prefixes}; do " \"run scan_dev_for_extlinux; " \"run scan_dev_for_scripts; " \ -"done\0" \ +"done;" \ +SCAN_DEV_FOR_EFI \ +"\0" \ \"scan_dev_for_boot_part=" \"part list ${devtype} ${devnum} -bootable devplist; " \
Signature · 1 line
1.8.5.6
PATCH v4 14/15 efi_loader: Add README section in README.efi
In reply to Alexander GrafTo preserve all cover letter knowledge of the status on UEFI payload support, let's add some sections to README.efi.
v3 -> v4:
- Add section about config options
- s/10kb/10KB/
--- doc/README.efi | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 82 insertions(+), 1 deletion(-)
doc/README.efi +82 -1
@@ -4,6 +4,28 @@ # SPDX-License-Identifier: GPL-2.0+ # +=========== Table of Contents =========== + + 1 U-Boot on EFI + 1.1 In God's Name, Why? + 1.2 Status + 1.3 Build Instructions + 1.4 Trying it out + 1.5 Inner workings + 1.6 EFI Application + 1.7 EFI Payload + 1.8 Tables + 1.9 Interrupts + 1.10 32/64-bit + 1.11 Future work + 1.12 Where is the code? + + 2 EFI on U-Boot + 2.1 In God's Name, Why? + 2.2 How do I get it? + 2.3 Status + 2.4 Future work + U-Boot on EFI ============= This document provides information about U-Boot running on top of EFI, either
@@ -234,7 +256,6 @@ board/efi/efi-x86/efi.c common/cmd_efi.c the 'efi' command - -- Ben Stoltz, Simon Glass Google, Inc
@@ -242,3 +263,63 @@ July 2015 [1] http://www.qemu.org [2] http://www.tianocore.org/ovmf/ + +------------------------------------------------------------------------------- + +EFI on U-Boot +============= + +In addition to support for running U-Boot as a UEFI application, U-Boot itself +can also expose the UEFI interfaces and thus allow UEFI payloads to run under +it. + +In God's Name, Why? +------------------- + +With this support in place, you can run any UEFI payload (such as the Linux +kernel, grub2 or gummiboot) on U-Boot. This dramatically simplifies boot loader +configuration, as U-Boot based systems now look and feel (almost) the same way +as TianoCore based systems. + +How do I get it? +---------------- + +EFI support for 32bit ARM and AArch64 is already included in U-Boot. All you +need to do is enable + + CONFIG_CMD_BOOTEFI=y + CONFIG_EFI_LOADER=y + +in your .config file and you will automatically get a bootefi command to run +an efi application as well as snippet in the default distro boot script that +scans for removable media efi binaries as fallback. + +Status +------ + +I am successfully able to run grub2 and Linux EFI binaries with this code on +ARMv7 as well as AArch64 systems. + +When enabled, the resulting U-Boot binary only grows by ~10KB, so it's very +light weight. + +All storage devices are directly accessible from the uEFI payload + +Removable media booting (search for /efi/boot/boota{a64,arm}.efi) is supported. + +Simple use cases like "Plug this SD card into my ARM device and it just +boots into grub which boots into Linux", work very well. + +Future work +----------- + +Of course, there are still a few things one could do on top: + + - Improve disk media detection (don't scan, use what information we +have) + - Add EFI variable support using NVRAM + - Add GFX support + - Make EFI Shell work + - Network device support + - Support for payload exit + - Payload Watchdog support
Signature · 1 line
1.8.5.6
PATCH v4 15/15 efi_loader: Add MAINTAINERS entry
In reply to Alexander GrafNow that everything's in place, let's add myself as the maintainer for the efi payload support.
--- MAINTAINERS | 7 +++++++ 1 file changed, 7 insertions(+)
MAINTAINERS +7 -0
@@ -230,6 +230,13 @@ F: drivers/core/ F: include/dm/ F: test/dm/ +EFI PAYLOAD +M: Alexander Graf <[email protected]> +S: Maintained +F: include/efi_loader.h +F: lib/efi_loader/ +F: cmd/bootefi.c + FLATTENED DEVICE TREE M: Simon Glass <[email protected]> S: Maintained
Signature · 1 line
1.8.5.6