diff options
Diffstat (limited to 'lib/efi')
-rw-r--r-- | lib/efi/Kconfig | 54 | ||||
-rw-r--r-- | lib/efi/Makefile | 17 | ||||
-rw-r--r-- | lib/efi/efi.c | 101 | ||||
-rw-r--r-- | lib/efi/efi_app.c | 139 | ||||
-rw-r--r-- | lib/efi/efi_info.c | 47 | ||||
-rw-r--r-- | lib/efi/efi_stub.c | 370 |
6 files changed, 728 insertions, 0 deletions
diff --git a/lib/efi/Kconfig b/lib/efi/Kconfig new file mode 100644 index 0000000000..919e314a0c --- /dev/null +++ b/lib/efi/Kconfig @@ -0,0 +1,54 @@ +config EFI + bool "Support running U-Boot from EFI" + depends on X86 + help + U-Boot can be started from EFI on certain platforms. This allows + EFI to perform most of the system init and then jump to U-Boot for + final system boot. Another option is to run U-Boot as an EFI + application, with U-Boot using EFI's drivers instead of its own. + +choice + prompt "Select EFI mode to use" + depends on X86 && EFI + +config EFI_APP + bool "Support running as an EFI application" + help + Build U-Boot as an application which can be started from EFI. This + is useful for examining a platform in the early stages of porting + U-Boot to it. It allows only very basic functionality, such as a + command prompt 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 EFI_APP + default 0x2000000 + help + Set the amount of EFI RAM which is claimed by U-Boot for its own + use. U-Boot allocates this from EFI on start-up (along with a few + other smaller amounts) and it can never be increased after that. + It is used as the RAM size in with U-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 new file mode 100644 index 0000000000..e32dc14f3d --- /dev/null +++ b/lib/efi/Makefile @@ -0,0 +1,17 @@ +# +# (C) Copyright 2015 Google, Inc +# +# SPDX-License-Identifier: GPL-2.0+ +# + +obj-$(CONFIG_EFI_APP) += efi_app.o efi.o +obj-$(CONFIG_EFI_STUB) += efi_info.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.c b/lib/efi/efi.c new file mode 100644 index 0000000000..81af27caab --- /dev/null +++ b/lib/efi/efi.c @@ -0,0 +1,101 @@ +/* + * 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 + * + * Common EFI functions + */ + +#include <common.h> +#include <debug_uart.h> +#include <errno.h> +#include <linux/err.h> +#include <linux/types.h> +#include <efi.h> +#include <efi_api.h> + +DECLARE_GLOBAL_DATA_PTR; + +/* + * Unfortunately we cannot access any code outside what is built especially + * for the stub. lib/string.c is already being built for the U-Boot payload + * so it uses the wrong compiler flags. Add our own memset() here. + */ +static void efi_memset(void *ptr, int ch, int size) +{ + char *dest = ptr; + + while (size-- > 0) + *dest++ = ch; +} + +/* + * Since the EFI stub cannot access most of the U-Boot code, add our own + * simple console output functions here. The EFI app will not use these since + * it can use the normal console. + */ +void efi_putc(struct efi_priv *priv, const char ch) +{ + struct efi_simple_text_output_protocol *con = priv->sys_table->con_out; + uint16_t ucode[2]; + + ucode[0] = ch; + ucode[1] = '\0'; + con->output_string(con, ucode); +} + +void efi_puts(struct efi_priv *priv, const char *str) +{ + while (*str) + efi_putc(priv, *str++); +} + +int efi_init(struct efi_priv *priv, const char *banner, efi_handle_t image, + struct efi_system_table *sys_table) +{ + efi_guid_t loaded_image_guid = LOADED_IMAGE_PROTOCOL_GUID; + struct efi_boot_services *boot = sys_table->boottime; + struct efi_loaded_image *loaded_image; + int ret; + + efi_memset(priv, '\0', sizeof(*priv)); + priv->sys_table = sys_table; + priv->boot = sys_table->boottime; + priv->parent_image = image; + priv->run = sys_table->runtime; + + efi_puts(priv, "U-Boot EFI "); + efi_puts(priv, banner); + efi_putc(priv, ' '); + + ret = boot->open_protocol(priv->parent_image, &loaded_image_guid, + (void **)&loaded_image, &priv->parent_image, + NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL); + if (ret) { + efi_puts(priv, "Failed to get loaded image protocol\n"); + return ret; + } + priv->image_data_type = loaded_image->image_data_type; + + return 0; +} + +void *efi_malloc(struct efi_priv *priv, int size, efi_status_t *retp) +{ + struct efi_boot_services *boot = priv->boot; + void *buf = NULL; + + *retp = boot->allocate_pool(priv->image_data_type, size, &buf); + + return buf; +} + +void efi_free(struct efi_priv *priv, void *ptr) +{ + struct efi_boot_services *boot = priv->boot; + + boot->free_pool(ptr); +} diff --git a/lib/efi/efi_app.c b/lib/efi/efi_app.c new file mode 100644 index 0000000000..452ab5d6ce --- /dev/null +++ b/lib/efi/efi_app.c @@ -0,0 +1,139 @@ +/* + * 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 + * + * This file implements U-Boot running as an EFI application. + */ + +#include <common.h> +#include <debug_uart.h> +#include <errno.h> +#include <linux/err.h> +#include <linux/types.h> +#include <efi.h> +#include <efi_api.h> + +DECLARE_GLOBAL_DATA_PTR; + +static struct efi_priv *global_priv; + +struct efi_system_table *efi_get_sys_table(void) +{ + return global_priv->sys_table; +} + +unsigned long efi_get_ram_base(void) +{ + return global_priv->ram_base; +} + +static efi_status_t setup_memory(struct efi_priv *priv) +{ + struct efi_boot_services *boot = priv->boot; + efi_physical_addr_t addr; + efi_status_t ret; + int pages; + + /* + * Use global_data_ptr instead of gd since it is an assignment. There + * are very few assignments to global_data in U-Boot and this makes + * it easier to find them. + */ + global_data_ptr = efi_malloc(priv, sizeof(struct global_data), &ret); + if (!global_data_ptr) + return ret; + memset(gd, '\0', sizeof(*gd)); + + gd->malloc_base = (ulong)efi_malloc(priv, CONFIG_SYS_MALLOC_F_LEN, + &ret); + if (!gd->malloc_base) + return ret; + pages = CONFIG_EFI_RAM_SIZE >> 12; + + /* + * Don't allocate any memory above 4GB. U-Boot is a 32-bit application + * so we want it to load below 4GB. + */ + addr = 1ULL << 32; + ret = boot->allocate_pages(EFI_ALLOCATE_MAX_ADDRESS, + priv->image_data_type, pages, &addr); + if (ret) { + printf("(using pool %lx) ", ret); + priv->ram_base = (ulong)efi_malloc(priv, CONFIG_EFI_RAM_SIZE, + &ret); + if (!priv->ram_base) + return ret; + priv->use_pool_for_malloc = true; + } else { + priv->ram_base = addr; + } + gd->ram_size = pages << 12; + + return 0; +} + +static void free_memory(struct efi_priv *priv) +{ + struct efi_boot_services *boot = priv->boot; + + if (priv->use_pool_for_malloc) + efi_free(priv, (void *)priv->ram_base); + else + boot->free_pages(priv->ram_base, gd->ram_size >> 12); + + efi_free(priv, (void *)gd->malloc_base); + efi_free(priv, gd); + global_data_ptr = NULL; +} + +/** + * 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; + efi_status_t ret; + + /* Set up access to EFI data structures */ + efi_init(priv, "App", image, sys_table); + + global_priv = priv; + + /* + * Set up the EFI debug UART so that printf() works. This is + * implemented in the EFI serial driver, serial_efi.c. The application + * can use printf() freely. + */ + debug_uart_init(); + + ret = setup_memory(priv); + if (ret) { + printf("Failed to set up memory: ret=%lx\n", ret); + return ret; + } + + printf("starting\n"); + + board_init_f(GD_FLG_SKIP_RELOC); + board_init_r(NULL, 0); + free_memory(priv); + + return EFI_SUCCESS; +} + +void reset_cpu(ulong addr) +{ + struct efi_priv *priv = global_priv; + + free_memory(priv); + printf("U-Boot EFI exiting\n"); + priv->boot->exit(priv->parent_image, EFI_SUCCESS, 0, NULL); +} diff --git a/lib/efi/efi_info.c b/lib/efi/efi_info.c new file mode 100644 index 0000000000..0cd9a7e9c7 --- /dev/null +++ b/lib/efi/efi_info.c @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2015 Google, Inc + * + * SPDX-License-Identifier: GPL-2.0+ + * + * Access to the EFI information table + */ + +#include <common.h> +#include <efi.h> +#include <errno.h> +#include <mapmem.h> + +int efi_info_get(enum efi_entry_t type, void **datap, int *sizep) +{ + struct efi_entry_hdr *entry; + struct efi_info_hdr *info; + int ret; + + if (!gd->arch.table) + return -ENODATA; + + info = map_sysmem(gd->arch.table, 0); + if (info->version != EFI_TABLE_VERSION) { + ret = -EPROTONOSUPPORT; + goto err; + } + + entry = (struct efi_entry_hdr *)((ulong)info + info->hdr_size); + while (entry->type != EFIET_END) { + if (entry->type == type) { + if (entry->addr) + *datap = map_sysmem(entry->addr, entry->size); + else + *datap = entry + 1; + *sizep = entry->size; + return 0; + } + entry = (struct efi_entry_hdr *)((ulong)entry + entry->link); + } + + ret = -ENOENT; +err: + unmap_sysmem(info); + + return ret; +} diff --git a/lib/efi/efi_stub.c b/lib/efi/efi_stub.c new file mode 100644 index 0000000000..d4d3e49689 --- /dev/null +++ b/lib/efi/efi_stub.c @@ -0,0 +1,370 @@ +/* + * 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 an + * EFI application. It can be built either in 32-bit or 64-bit mode. + */ + +#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) +{ +#ifdef CONFIG_EFI_STUB_32BIT + /* + * U-Boot requires these parameters in registers, not on the stack. + * See _x86boot_start() for this code. + */ + typedef void (*func_t)(int bist, int unused, ulong info) + __attribute__((regparm(3))); + + ((func_t)addr)(0, 0, info); +#else + cpu_call32(cs32, CONFIG_SYS_TEXT_BASE, info); +#endif +} + +#ifdef CONFIG_EFI_STUB_64BIT +static void get_gdt(struct desctab_info *info) +{ + asm volatile ("sgdt %0" : : "m"(*info) : "memory"); +} +#endif + +static inline unsigned long read_cr3(void) +{ + unsigned long val; + + asm volatile("mov %%cr3,%0" : "=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; + +#ifdef CONFIG_EFI_STUB_64BIT + struct desctab_info gdt; + uint64_t *ptr; + int i; + + get_gdt(&gdt); + for (ptr = (uint64_t *)(unsigned long)gdt.addr, i = 0; i < gdt.limit; + i += 8, ptr++) { + uint64_t desc = *ptr; + uint64_t base, limit; + + /* + * Check that the target U-Boot jump address is within the + * selector and that the selector is of the right type. + */ + base = ((desc >> GDT_BASE_LOW_SHIFT) & GDT_BASE_LOW_MASK) | + ((desc >> GDT_BASE_HIGH_SHIFT) & GDT_BASE_HIGH_MASK) + << 16; + limit = ((desc >> GDT_LIMIT_LOW_SHIFT) & GDT_LIMIT_LOW_MASK) | + ((desc >> GDT_LIMIT_HIGH_SHIFT) & GDT_LIMIT_HIGH_MASK) + << 16; + base <<= 12; /* 4KB granularity */ + limit <<= 12; + if ((desc & GDT_PRESENT) && (desc && GDT_NOTSYS) && + !(desc & GDT_LONG) && (desc & GDT_4KB) && + (desc & GDT_32BIT) && (desc & GDT_CODE) && + CONFIG_SYS_TEXT_BASE > base && + CONFIG_SYS_TEXT_BASE + CONFIG_SYS_MONITOR_LEN < limit + ) { + cs32 = i; + break; + } + } + +#ifdef DEBUG + puts("\ngdt: "); + printhex8(gdt.limit); + puts(", addr: "); + printhex8(gdt.addr >> 32); + printhex8(gdt.addr); + for (i = 0; i < gdt.limit; i += 8) { + uint32_t *ptr = (uint32_t *)((unsigned long)gdt.addr + i); + + puts("\n"); + printhex2(i); + puts(": "); + printhex8(ptr[1]); + puts(" "); + printhex8(ptr[0]); + } + puts("\n "); + puts("32-bit code segment: "); + printhex2(cs32); + puts("\n "); + + puts("page_table: "); + printhex8(read_cr3()); + puts("\n "); +#endif + if (!cs32) { + puts("Can't find 32-bit code segment\n"); + return -ENOENT; + } +#endif + + 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. + */ +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) { + /* + * Unfortunately it happens that we cannot exit boot services + * the first time. But the second time it work. I don't know + * why but this seems to be a repeatable problem. To get + * around it, just try again. + */ + 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, _binary_u_boot_dtb_bin_start, + (ulong)_binary_u_boot_dtb_bin_end - + (ulong)_binary_u_boot_dtb_bin_start); + +#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; +} |