[U-Boot] [PATCH 34/48] efi: Add support for loading U-Boot through an EFI stub
Simon Glass
sjg at chromium.org
Wed Jul 22 17:49:26 CEST 2015
It is useful to be able to load U-Boot onto a board even it is it already
running EFI. This can allow access to the U-Boot command interface, flexible
booting options and easier development.
The easiest way to do this is to build U-Boot as a binary blob and have an
EFI stub copy it into RAM. Add support for this feature, targeting 32-bit
initially.
Also add a way to detect when U-Boot has been loaded via a stub. This goes
in common.h since it needs to be widely available so that we avoid redoing
initialisation that should be skipped.
Signed-off-by: Simon Glass <sjg at chromium.org>
---
Makefile | 32 ++++++
include/common.h | 7 ++
include/efi.h | 4 +
lib/efi/Kconfig | 21 ++++
lib/efi/Makefile | 9 ++
lib/efi/efi_stub.c | 293 +++++++++++++++++++++++++++++++++++++++++++++++++++++
6 files changed, 366 insertions(+)
create mode 100644 lib/efi/efi_stub.c
diff --git a/Makefile b/Makefile
index 91ebc2e..9f863e1 100644
--- a/Makefile
+++ b/Makefile
@@ -753,6 +753,7 @@ ALL-$(CONFIG_SPL) += $(CONFIG_SPL_TARGET:"%"=%)
endif
ALL-$(CONFIG_REMAKE_ELF) += u-boot.elf
ALL-$(CONFIG_ARCH_EFI) += u-boot.efi
+ALL-$(CONFIG_EFI_STUB) += u-boot-payload.efi
ifneq ($(BUILD_ROM),)
ALL-$(CONFIG_X86_RESET_VECTOR) += u-boot.rom
@@ -1085,6 +1086,37 @@ OBJCOPYFLAGS_u-boot.efi := $(OBJCOPYFLAGS_EFI)
u-boot.efi: u-boot FORCE
$(call if_changed,zobjcopy)
+# Generate an assembly file to wrap a binary file
+quiet_cmd_bin_S = BIN $@
+cmd_bin_S = \
+( \
+ echo '.section .u_boot_bin.init.rodata,"a"'; \
+ echo '.balign 16'; \
+ echo '.global __u_boot_bin_begin'; \
+ echo '__u_boot_bin_begin:'; \
+ echo '.incbin "$<" '; \
+ echo '__u_boot_bin_end:'; \
+ echo '.global __u_boot_bin_end'; \
+ echo '.balign 16'; \
+) > $@
+
+u-boot-dtb.bin.S: u-boot-dtb.bin FORCE
+ $(call if_changed,bin_S)
+
+u-boot-payload.lds: $(LDSCRIPT_EFI) FORCE
+ $(call if_changed_dep,cpp_lds)
+
+u-boot-payload: u-boot-dtb.bin.o u-boot-payload.lds \
+ FORCE
+ $(LD) $(LDFLAGS_EFI) -o $@ \
+ -T u-boot-payload.lds \
+ lib/efi/efi.o lib/efi/efi_stub.o u-boot-dtb.bin.o \
+ $(addprefix arch/$(ARCH)/lib/,$(EFISTUB))
+
+OBJCOPYFLAGS_u-boot-payload.efi := $(OBJCOPYFLAGS_EFI)
+u-boot-payload.efi: u-boot-payload FORCE
+ $(call if_changed,zobjcopy)
+
u-boot-img.bin: spl/u-boot-spl.bin u-boot.img FORCE
$(call if_changed,cat)
diff --git a/include/common.h b/include/common.h
index 8f4b2ec..406fb33 100644
--- a/include/common.h
+++ b/include/common.h
@@ -1010,6 +1010,13 @@ int cpu_release(int nr, int argc, char * const argv[]);
#define DEFINE_CACHE_ALIGN_BUFFER(type, name, size) \
DEFINE_ALIGN_BUFFER(type, name, size, ARCH_DMA_MINALIGN)
+/* Avoid using CONFIG_EFI_STUB directly as we may boot from other loaders */
+#ifdef CONFIG_EFI_STUB
+#define ll_boot_init() false
+#else
+#define ll_boot_init() true
+#endif
+
/* Pull in stuff for the build system */
#ifdef DO_DEPS_ONLY
# include <environment.h>
diff --git a/include/efi.h b/include/efi.h
index 66ef6c3..edc8cd9 100644
--- a/include/efi.h
+++ b/include/efi.h
@@ -268,11 +268,15 @@ struct efi_priv {
/* Base address of the EFI image */
extern char ImageBase[];
+/* Start and end of U-Boot image (for payload) */
+extern char __u_boot_bin_begin[], __u_boot_bin_end[];
+
/**
* efi_get_sys_table() - Get access to the main EFI system table
*
* @return pointer to EFI system table
*/
+
struct efi_system_table *efi_get_sys_table(void);
/**
diff --git a/lib/efi/Kconfig b/lib/efi/Kconfig
index 2b3dbd4..f1a8873 100644
--- a/lib/efi/Kconfig
+++ b/lib/efi/Kconfig
@@ -20,6 +20,11 @@ config ARCH_EFI
command problem and memory and I/O functions. Use 'reset' to return
to EFI.
+config EFI_STUB
+ bool "Support running as an EFI payload"
+
+endchoice
+
config EFI_RAM_SIZE
hex "Amount of EFI RAM for U-Boot"
depends on ARCH_EFI
@@ -30,4 +35,20 @@ config EFI_RAM_SIZE
other smaller amounts) and it can never be increased after that.
It is used as the RAM size in withU-Boot.
+choice
+ prompt "EFI 32/64-bit selection"
+ depends on EFI_STUB
+ help
+ EFI does not support mixing 32-bit and 64-bit modes. This is a
+ significant problem because it means that you must build a stub with
+ the correct type for EFI to load it correctly. If you are using
+ 32-bit EFI, select 32-bit here, else select 64-bit. Failure to do
+ this may produce no error message - it just won't start!
+
+config EFI_STUB_32BIT
+ bool "Produce a stub for running with 32-bit EFI"
+
+config EFI_STUB_64BIT
+ bool "Produce a stub for running with 64-bit EFI"
+
endchoice
diff --git a/lib/efi/Makefile b/lib/efi/Makefile
index 137d2f9..ba2824e 100644
--- a/lib/efi/Makefile
+++ b/lib/efi/Makefile
@@ -5,3 +5,12 @@
#
obj-$(CONFIG_ARCH_EFI) += efi_app.o efi.o
+
+CFLAGS_REMOVE_efi_stub.o := -mregparm=3 \
+ $(if $(CONFIG_EFI_STUB_64BIT),-march=i386 -m32)
+CFLAGS_efi_stub.o := -fpic -fshort-wchar
+CFLAGS_REMOVE_efi.o := -mregparm=3 \
+ $(if $(CONFIG_EFI_STUB_64BIT),-march=i386 -m32)
+CFLAGS_efi.o := -fpic -fshort-wchar
+
+extra-$(CONFIG_EFI_STUB) += efi_stub.o efi.o
diff --git a/lib/efi/efi_stub.c b/lib/efi/efi_stub.c
new file mode 100644
index 0000000..e3b7cdb
--- /dev/null
+++ b/lib/efi/efi_stub.c
@@ -0,0 +1,293 @@
+/*
+ * Copyright (c) 2015 Google, Inc
+ *
+ * SPDX-License-Identifier: GPL-2.0+
+ *
+ * EFI information obtained here:
+ * http://wiki.phoenix.com/wiki/index.php/EFI_BOOT_SERVICES
+ *
+ * Loads a payload (U-Boot) within the EFI environment. This is built as a
+ * 32-bit EFI application.
+ */
+
+#include <common.h>
+#include <debug_uart.h>
+#include <efi.h>
+#include <efi_api.h>
+#include <errno.h>
+#include <ns16550.h>
+#include <asm/cpu.h>
+#include <asm/io.h>
+#include <linux/err.h>
+#include <linux/types.h>
+
+DECLARE_GLOBAL_DATA_PTR;
+
+#ifndef CONFIG_X86
+/*
+ * Problem areas:
+ * - putc() uses the ns16550 address directly and assumed I/O access. Many
+ * platforms will use memory access
+ * get_codeseg32() is only meaningful on x86
+ */
+#error "This file needs to be ported for use on architectures"
+#endif
+
+static struct efi_priv *global_priv;
+static bool use_uart;
+
+struct __packed desctab_info {
+ uint16_t limit;
+ uint64_t addr;
+ uint16_t pad;
+};
+
+/*
+ * EFI uses Unicode and we don't. The easiest way to get a sensible output
+ * function is to use the U-Boot debug UART. We use EFI's console output
+ * function where available, and assume the built-in UART after that. We rely
+ * on EFI to set up the UART for us and just bring in the functions here.
+ * This last bit is a bit icky, but it's only for debugging anyway. We could
+ * build in ns16550.c with some effort, but this is a payload loader after
+ * all.
+ *
+ * Note: We avoid using printf() so we don't need to bring in lib/vsprintf.c.
+ * That would require some refactoring since we already build this for U-Boot.
+ * Building an EFI shared library version would have to be a separate stem.
+ * That might push us to using the SPL framework to build this stub. However
+ * that would involve a round of EFI-specific changes in SPL. Worth
+ * considering if we start needing more U-Boot functionality. Note that we
+ * could then move get_codeseg32() to arch/x86/cpu/cpu.c.
+ */
+void debug_uart_init(void)
+{
+}
+
+void putc(const char ch)
+{
+ if (use_uart) {
+ NS16550_t com_port = (NS16550_t)0x3f8;
+
+ while ((inb((ulong)&com_port->lsr) & UART_LSR_THRE) == 0)
+ ;
+ outb(ch, (ulong)&com_port->thr);
+ } else {
+ efi_putc(global_priv, ch);
+ }
+ if (ch == '\n')
+ putc('\r');
+}
+
+void puts(const char *str)
+{
+ while (*str)
+ putc(*str++);
+}
+
+static void _debug_uart_putc(int ch)
+{
+ putc(ch);
+}
+
+DEBUG_UART_FUNCS
+
+void *memcpy(void *dest, const void *src, size_t size)
+{
+ unsigned char *dptr = dest;
+ const unsigned char *ptr = src;
+ const unsigned char *end = src + size;
+
+ while (ptr < end)
+ *dptr++ = *ptr++;
+
+ return dest;
+}
+
+void *memset(void *inptr, int ch, size_t size)
+{
+ char *ptr = inptr;
+ char *end = ptr + size;
+
+ while (ptr < end)
+ *ptr++ = ch;
+
+ return ptr;
+}
+
+static void jump_to_uboot(ulong cs32, ulong addr, ulong info)
+{
+ typedef void (*func_t)(int bist, int unused, ulong info);
+
+#ifdef CONFIG_EFI_STUB_32BIT
+ ((func_t)addr)(bist, 0, info);
+#else
+ /* TODO: Implement this */
+#endif
+}
+
+static void get_gdt(struct desctab_info *info)
+{
+ asm volatile ("sgdt %0" : : "m"(*info) : "memory");
+}
+
+static inline unsigned long read_cr3(void)
+{
+ unsigned long val;
+
+ asm volatile("mov %%cr3,%0\n\t" : "=r" (val) : : "memory");
+ return val;
+}
+
+/**
+ * get_codeseg32() - Find the code segment to use for 32-bit code
+ *
+ * U-Boot only works in 32-bit mode at present, so when booting from 64-bit
+ * EFI we must first change to 32-bit mode. To do this we need to find the
+ * correct code segment to use (an entry in the Global Descriptor Table).
+ *
+ * @return code segment GDT offset, or 0 for 32-bit EFI, -ENOENT if not found
+ */
+static int get_codeseg32(void)
+{
+ int cs32 = 0;
+
+ /* TODO(sjg): Implement this for 64-bit mode */
+ return cs32;
+}
+
+static int setup_info_table(struct efi_priv *priv, int size)
+{
+ struct efi_info_hdr *info;
+ efi_status_t ret;
+
+ /* Get some memory for our info table */
+ priv->info_size = size;
+ info = efi_malloc(priv, priv->info_size, &ret);
+ if (ret) {
+ printhex2(ret);
+ puts(" No memory for info table: ");
+ return ret;
+ }
+
+ memset(info, '\0', sizeof(*info));
+ info->version = EFI_TABLE_VERSION;
+ info->hdr_size = sizeof(*info);
+ priv->info = info;
+ priv->next_hdr = (char *)info + info->hdr_size;
+
+ return 0;
+}
+
+static void add_entry_addr(struct efi_priv *priv, enum efi_entry_t type,
+ void *ptr1, int size1, void *ptr2, int size2)
+{
+ struct efi_entry_hdr *hdr = priv->next_hdr;
+
+ hdr->type = type;
+ hdr->size = size1 + size2;
+ hdr->addr = 0;
+ hdr->link = ALIGN(sizeof(*hdr) + hdr->size, 16);
+ priv->next_hdr += hdr->link;
+ memcpy(hdr + 1, ptr1, size1);
+ memcpy((void *)(hdr + 1) + size1, ptr2, size2);
+ priv->info->total_size = (ulong)priv->next_hdr - (ulong)priv->info;
+}
+
+/**
+ * efi_main() - Start an EFI image
+ *
+ * This function is called by our EFI start-up code. It handles running
+ * U-Boot. If it returns, EFI will continue. Another way to get back to EFI
+ * is via reset_cpu().
+ */
+efi_status_t efi_main(efi_handle_t image, struct efi_system_table *sys_table)
+{
+ struct efi_priv local_priv, *priv = &local_priv;
+ struct efi_boot_services *boot = sys_table->boottime;
+ struct efi_mem_desc *desc;
+ struct efi_entry_memmap map;
+ ulong key, desc_size, size;
+ efi_status_t ret;
+ u32 version;
+ int cs32;
+
+ ret = efi_init(priv, "Payload", image, sys_table);
+ if (ret) {
+ printhex2(ret); puts(" efi_init() failed\n");
+ return ret;
+ }
+ global_priv = priv;
+
+ cs32 = get_codeseg32();
+ if (cs32 < 0)
+ return EFI_UNSUPPORTED;
+
+ /* Get the memory map so we can switch off EFI */
+ size = 0;
+ ret = boot->get_memory_map(&size, NULL, &key, &desc_size, &version);
+ if (ret != EFI_BUFFER_TOO_SMALL) {
+ printhex2(BITS_PER_LONG);
+ printhex2(ret);
+ puts(" No memory map\n");
+ return ret;
+ }
+ size += 1024; /* Since doing a malloc() may change the memory map! */
+ desc = efi_malloc(priv, size, &ret);
+ if (!desc) {
+ printhex2(ret);
+ puts(" No memory for memory descriptor: ");
+ return ret;
+ }
+ ret = setup_info_table(priv, size + 128);
+ if (ret)
+ return ret;
+
+ ret = boot->get_memory_map(&size, desc, &key, &desc_size, &version);
+ if (ret) {
+ printhex2(ret);
+ puts(" Can't get memory map\n");
+ return ret;
+ }
+
+ ret = boot->exit_boot_services(image, key);
+ if (ret) {
+ printhex2(ret);
+ puts(" Can't exit boot services\n");
+ size = sizeof(desc);
+ ret = boot->get_memory_map(&size, desc, &key, &desc_size,
+ &version);
+ if (ret) {
+ printhex2(ret);
+ puts(" Can't get memory map\n");
+ return ret;
+ }
+ ret = boot->exit_boot_services(image, key);
+ if (ret) {
+ printhex2(ret);
+ puts(" Can't exit boot services 2\n");
+ return ret;
+ }
+ }
+
+ map.version = version;
+ map.desc_size = desc_size;
+ add_entry_addr(priv, EFIET_MEMORY_MAP, &map, sizeof(map), desc, size);
+ add_entry_addr(priv, EFIET_END, NULL, 0, 0, 0);
+
+ /* The EFI UART won't work now, switch to a debug one */
+ use_uart = true;
+
+ memcpy((void *)CONFIG_SYS_TEXT_BASE, __u_boot_bin_begin,
+ (ulong)__u_boot_bin_end - (ulong)__u_boot_bin_begin);
+
+#ifdef DEBUG
+ puts("EFI table at ");
+ printhex8((ulong)priv->info);
+ puts(" size ");
+ printhex8(priv->info->total_size);
+#endif
+ putc('\n');
+ jump_to_uboot(cs32, CONFIG_SYS_TEXT_BASE, (ulong)priv->info);
+
+ return EFI_LOAD_ERROR;
+}
--
2.4.3.573.g4eafbef
More information about the U-Boot
mailing list