Tom Rini | 70df9d6 | 2018-05-07 17:02:21 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 2 | /* |
| 3 | * EFI application loader |
| 4 | * |
| 5 | * Copyright (c) 2016 Alexander Graf |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 6 | */ |
| 7 | |
Heinrich Schuchardt | 02efd5d | 2017-10-18 18:13:13 +0200 | [diff] [blame] | 8 | #include <charset.h> |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 9 | #include <common.h> |
| 10 | #include <command.h> |
Simon Glass | 11c89f3 | 2017-05-17 17:18:03 -0600 | [diff] [blame] | 11 | #include <dm.h> |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 12 | #include <efi_loader.h> |
Heinrich Schuchardt | 02efd5d | 2017-10-18 18:13:13 +0200 | [diff] [blame] | 13 | #include <efi_selftest.h> |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 14 | #include <errno.h> |
Masahiro Yamada | 75f82d0 | 2018-03-05 01:20:11 +0900 | [diff] [blame] | 15 | #include <linux/libfdt.h> |
| 16 | #include <linux/libfdt_env.h> |
Alexander Graf | a241451 | 2018-06-18 17:22:58 +0200 | [diff] [blame] | 17 | #include <mapmem.h> |
Alexander Graf | fdbae01 | 2016-04-11 23:51:01 +0200 | [diff] [blame] | 18 | #include <memalign.h> |
Alexander Graf | 2ff5cc3 | 2016-04-11 16:55:26 +0200 | [diff] [blame] | 19 | #include <asm/global_data.h> |
Simon Glass | c2194bf | 2016-09-25 15:27:32 -0600 | [diff] [blame] | 20 | #include <asm-generic/sections.h> |
Heinrich Schuchardt | 954e2b9 | 2018-04-03 21:59:32 +0200 | [diff] [blame] | 21 | #include <asm-generic/unaligned.h> |
Simon Glass | c2194bf | 2016-09-25 15:27:32 -0600 | [diff] [blame] | 22 | #include <linux/linkage.h> |
Alexander Graf | 2ff5cc3 | 2016-04-11 16:55:26 +0200 | [diff] [blame] | 23 | |
Mark Kettenis | 5eb68ed | 2018-06-15 23:47:12 +0200 | [diff] [blame] | 24 | #ifdef CONFIG_ARMV7_NONSEC |
| 25 | #include <asm/armv7.h> |
| 26 | #include <asm/secure.h> |
| 27 | #endif |
| 28 | |
Alexander Graf | 2ff5cc3 | 2016-04-11 16:55:26 +0200 | [diff] [blame] | 29 | DECLARE_GLOBAL_DATA_PTR; |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 30 | |
Heinrich Schuchardt | 8c0dfc2 | 2018-03-03 15:29:02 +0100 | [diff] [blame] | 31 | #define OBJ_LIST_NOT_INITIALIZED 1 |
| 32 | |
| 33 | static efi_status_t efi_obj_list_initialized = OBJ_LIST_NOT_INITIALIZED; |
Heinrich Schuchardt | 5e9900f | 2017-07-19 19:37:22 +0200 | [diff] [blame] | 34 | |
Rob Clark | f8db922 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 35 | static struct efi_device_path *bootefi_image_path; |
| 36 | static struct efi_device_path *bootefi_device_path; |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 37 | |
Heinrich Schuchardt | 5e9900f | 2017-07-19 19:37:22 +0200 | [diff] [blame] | 38 | /* Initialize and populate EFI object list */ |
Heinrich Schuchardt | 8c0dfc2 | 2018-03-03 15:29:02 +0100 | [diff] [blame] | 39 | efi_status_t efi_init_obj_list(void) |
Heinrich Schuchardt | 5e9900f | 2017-07-19 19:37:22 +0200 | [diff] [blame] | 40 | { |
Heinrich Schuchardt | 8c0dfc2 | 2018-03-03 15:29:02 +0100 | [diff] [blame] | 41 | efi_status_t ret = EFI_SUCCESS; |
| 42 | |
Heinrich Schuchardt | 237a73e | 2018-10-03 23:55:38 +0200 | [diff] [blame] | 43 | /* |
| 44 | * On the ARM architecture gd is mapped to a fixed register (r9 or x18). |
| 45 | * As this register may be overwritten by an EFI payload we save it here |
| 46 | * and restore it on every callback entered. |
| 47 | */ |
| 48 | efi_save_gd(); |
| 49 | |
Heinrich Schuchardt | d4b38f0 | 2018-03-03 15:28:58 +0100 | [diff] [blame] | 50 | /* Initialize once only */ |
Heinrich Schuchardt | 8c0dfc2 | 2018-03-03 15:29:02 +0100 | [diff] [blame] | 51 | if (efi_obj_list_initialized != OBJ_LIST_NOT_INITIALIZED) |
| 52 | return efi_obj_list_initialized; |
Heinrich Schuchardt | 5e9900f | 2017-07-19 19:37:22 +0200 | [diff] [blame] | 53 | |
Heinrich Schuchardt | 15070db | 2018-06-28 12:45:31 +0200 | [diff] [blame] | 54 | /* Initialize system table */ |
| 55 | ret = efi_initialize_system_table(); |
| 56 | if (ret != EFI_SUCCESS) |
| 57 | goto out; |
| 58 | |
Heinrich Schuchardt | 79d7202 | 2018-09-20 21:58:23 +0200 | [diff] [blame] | 59 | /* Initialize root node */ |
| 60 | ret = efi_root_node_register(); |
| 61 | if (ret != EFI_SUCCESS) |
| 62 | goto out; |
| 63 | |
Heinrich Schuchardt | 11206f4 | 2018-01-21 19:29:30 +0100 | [diff] [blame] | 64 | /* Initialize EFI driver uclass */ |
Heinrich Schuchardt | 8c0dfc2 | 2018-03-03 15:29:02 +0100 | [diff] [blame] | 65 | ret = efi_driver_init(); |
| 66 | if (ret != EFI_SUCCESS) |
| 67 | goto out; |
Heinrich Schuchardt | 11206f4 | 2018-01-21 19:29:30 +0100 | [diff] [blame] | 68 | |
Heinrich Schuchardt | 8c0dfc2 | 2018-03-03 15:29:02 +0100 | [diff] [blame] | 69 | ret = efi_console_register(); |
| 70 | if (ret != EFI_SUCCESS) |
| 71 | goto out; |
Heinrich Schuchardt | 5e9900f | 2017-07-19 19:37:22 +0200 | [diff] [blame] | 72 | #ifdef CONFIG_PARTITIONS |
Heinrich Schuchardt | 8c0dfc2 | 2018-03-03 15:29:02 +0100 | [diff] [blame] | 73 | ret = efi_disk_register(); |
| 74 | if (ret != EFI_SUCCESS) |
| 75 | goto out; |
Heinrich Schuchardt | 5e9900f | 2017-07-19 19:37:22 +0200 | [diff] [blame] | 76 | #endif |
| 77 | #if defined(CONFIG_LCD) || defined(CONFIG_DM_VIDEO) |
Heinrich Schuchardt | 8c0dfc2 | 2018-03-03 15:29:02 +0100 | [diff] [blame] | 78 | ret = efi_gop_register(); |
| 79 | if (ret != EFI_SUCCESS) |
| 80 | goto out; |
Heinrich Schuchardt | 5e9900f | 2017-07-19 19:37:22 +0200 | [diff] [blame] | 81 | #endif |
Joe Hershberger | 5277a97 | 2018-04-13 15:26:39 -0500 | [diff] [blame] | 82 | #ifdef CONFIG_NET |
Heinrich Schuchardt | 8c0dfc2 | 2018-03-03 15:29:02 +0100 | [diff] [blame] | 83 | ret = efi_net_register(); |
| 84 | if (ret != EFI_SUCCESS) |
| 85 | goto out; |
Heinrich Schuchardt | 5e9900f | 2017-07-19 19:37:22 +0200 | [diff] [blame] | 86 | #endif |
Bin Meng | 2cfbdae | 2018-06-27 20:38:03 -0700 | [diff] [blame] | 87 | #ifdef CONFIG_GENERATE_ACPI_TABLE |
| 88 | ret = efi_acpi_register(); |
| 89 | if (ret != EFI_SUCCESS) |
| 90 | goto out; |
| 91 | #endif |
Heinrich Schuchardt | 5e9900f | 2017-07-19 19:37:22 +0200 | [diff] [blame] | 92 | #ifdef CONFIG_GENERATE_SMBIOS_TABLE |
Heinrich Schuchardt | 8c0dfc2 | 2018-03-03 15:29:02 +0100 | [diff] [blame] | 93 | ret = efi_smbios_register(); |
| 94 | if (ret != EFI_SUCCESS) |
| 95 | goto out; |
Heinrich Schuchardt | 5e9900f | 2017-07-19 19:37:22 +0200 | [diff] [blame] | 96 | #endif |
Heinrich Schuchardt | 8c0dfc2 | 2018-03-03 15:29:02 +0100 | [diff] [blame] | 97 | ret = efi_watchdog_register(); |
| 98 | if (ret != EFI_SUCCESS) |
| 99 | goto out; |
Heinrich Schuchardt | 5e9900f | 2017-07-19 19:37:22 +0200 | [diff] [blame] | 100 | |
| 101 | /* Initialize EFI runtime services */ |
Heinrich Schuchardt | 8c0dfc2 | 2018-03-03 15:29:02 +0100 | [diff] [blame] | 102 | ret = efi_reset_system_init(); |
| 103 | if (ret != EFI_SUCCESS) |
| 104 | goto out; |
Heinrich Schuchardt | 8c0dfc2 | 2018-03-03 15:29:02 +0100 | [diff] [blame] | 105 | |
| 106 | out: |
| 107 | efi_obj_list_initialized = ret; |
| 108 | return ret; |
Heinrich Schuchardt | 5e9900f | 2017-07-19 19:37:22 +0200 | [diff] [blame] | 109 | } |
| 110 | |
Heinrich Schuchardt | 02efd5d | 2017-10-18 18:13:13 +0200 | [diff] [blame] | 111 | /* |
Heinrich Schuchardt | 954e2b9 | 2018-04-03 21:59:32 +0200 | [diff] [blame] | 112 | * Allow unaligned memory access. |
| 113 | * |
| 114 | * This routine is overridden by architectures providing this feature. |
| 115 | */ |
| 116 | void __weak allow_unaligned(void) |
| 117 | { |
| 118 | } |
| 119 | |
| 120 | /* |
Heinrich Schuchardt | 02efd5d | 2017-10-18 18:13:13 +0200 | [diff] [blame] | 121 | * Set the load options of an image from an environment variable. |
| 122 | * |
| 123 | * @loaded_image_info: the image |
| 124 | * @env_var: name of the environment variable |
| 125 | */ |
| 126 | static void set_load_options(struct efi_loaded_image *loaded_image_info, |
| 127 | const char *env_var) |
| 128 | { |
| 129 | size_t size; |
| 130 | const char *env = env_get(env_var); |
Heinrich Schuchardt | de52780 | 2018-08-31 21:31:33 +0200 | [diff] [blame] | 131 | u16 *pos; |
Heinrich Schuchardt | 02efd5d | 2017-10-18 18:13:13 +0200 | [diff] [blame] | 132 | |
| 133 | loaded_image_info->load_options = NULL; |
| 134 | loaded_image_info->load_options_size = 0; |
| 135 | if (!env) |
| 136 | return; |
Heinrich Schuchardt | de52780 | 2018-08-31 21:31:33 +0200 | [diff] [blame] | 137 | size = utf8_utf16_strlen(env) + 1; |
Heinrich Schuchardt | 02efd5d | 2017-10-18 18:13:13 +0200 | [diff] [blame] | 138 | loaded_image_info->load_options = calloc(size, sizeof(u16)); |
| 139 | if (!loaded_image_info->load_options) { |
| 140 | printf("ERROR: Out of memory\n"); |
| 141 | return; |
| 142 | } |
Heinrich Schuchardt | de52780 | 2018-08-31 21:31:33 +0200 | [diff] [blame] | 143 | pos = loaded_image_info->load_options; |
| 144 | utf8_utf16_strcpy(&pos, env); |
Heinrich Schuchardt | 02efd5d | 2017-10-18 18:13:13 +0200 | [diff] [blame] | 145 | loaded_image_info->load_options_size = size * 2; |
| 146 | } |
| 147 | |
Simon Glass | 5d618df | 2018-08-08 03:54:30 -0600 | [diff] [blame] | 148 | /** |
| 149 | * copy_fdt() - Copy the device tree to a new location available to EFI |
| 150 | * |
| 151 | * The FDT is relocated into a suitable location within the EFI memory map. |
| 152 | * An additional 12KB is added to the space in case the device tree needs to be |
| 153 | * expanded later with fdt_open_into(). |
| 154 | * |
| 155 | * @fdt_addr: On entry, address of start of FDT. On exit, address of relocated |
| 156 | * FDT start |
| 157 | * @fdt_sizep: Returns new size of FDT, including |
| 158 | * @return new relocated address of FDT |
| 159 | */ |
| 160 | static efi_status_t copy_fdt(ulong *fdt_addrp, ulong *fdt_sizep) |
Alexander Graf | 2ff5cc3 | 2016-04-11 16:55:26 +0200 | [diff] [blame] | 161 | { |
Alexander Graf | fdbae01 | 2016-04-11 23:51:01 +0200 | [diff] [blame] | 162 | unsigned long fdt_ram_start = -1L, fdt_pages; |
Simon Glass | 5d618df | 2018-08-08 03:54:30 -0600 | [diff] [blame] | 163 | efi_status_t ret = 0; |
| 164 | void *fdt, *new_fdt; |
Alexander Graf | fdbae01 | 2016-04-11 23:51:01 +0200 | [diff] [blame] | 165 | u64 new_fdt_addr; |
Simon Glass | 5d618df | 2018-08-08 03:54:30 -0600 | [diff] [blame] | 166 | uint fdt_size; |
Alexander Graf | fdbae01 | 2016-04-11 23:51:01 +0200 | [diff] [blame] | 167 | int i; |
Alexander Graf | 2ff5cc3 | 2016-04-11 16:55:26 +0200 | [diff] [blame] | 168 | |
Simon Glass | 5d618df | 2018-08-08 03:54:30 -0600 | [diff] [blame] | 169 | for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) { |
| 170 | u64 ram_start = gd->bd->bi_dram[i].start; |
| 171 | u64 ram_size = gd->bd->bi_dram[i].size; |
Alexander Graf | 2ff5cc3 | 2016-04-11 16:55:26 +0200 | [diff] [blame] | 172 | |
Alexander Graf | fdbae01 | 2016-04-11 23:51:01 +0200 | [diff] [blame] | 173 | if (!ram_size) |
| 174 | continue; |
| 175 | |
| 176 | if (ram_start < fdt_ram_start) |
| 177 | fdt_ram_start = ram_start; |
| 178 | } |
| 179 | |
Simon Glass | 56bbcc1 | 2018-06-18 08:08:25 -0600 | [diff] [blame] | 180 | /* |
| 181 | * Give us at least 4KB of breathing room in case the device tree needs |
| 182 | * to be expanded later. Round up to the nearest EFI page boundary. |
| 183 | */ |
Simon Glass | 5d618df | 2018-08-08 03:54:30 -0600 | [diff] [blame] | 184 | fdt = map_sysmem(*fdt_addrp, 0); |
| 185 | fdt_size = fdt_totalsize(fdt); |
| 186 | fdt_size += 4096 * 3; |
Simon Glass | 56bbcc1 | 2018-06-18 08:08:25 -0600 | [diff] [blame] | 187 | fdt_size = ALIGN(fdt_size + EFI_PAGE_SIZE - 1, EFI_PAGE_SIZE); |
Alexander Graf | fdbae01 | 2016-04-11 23:51:01 +0200 | [diff] [blame] | 188 | fdt_pages = fdt_size >> EFI_PAGE_SHIFT; |
| 189 | |
Simon Glass | 609c392 | 2018-08-08 03:54:29 -0600 | [diff] [blame] | 190 | /* Safe fdt location is at 127MB */ |
| 191 | new_fdt_addr = fdt_ram_start + (127 * 1024 * 1024) + fdt_size; |
Simon Glass | 5d618df | 2018-08-08 03:54:30 -0600 | [diff] [blame] | 192 | ret = efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS, |
| 193 | EFI_RUNTIME_SERVICES_DATA, fdt_pages, |
| 194 | &new_fdt_addr); |
| 195 | if (ret != EFI_SUCCESS) { |
Alexander Graf | fdbae01 | 2016-04-11 23:51:01 +0200 | [diff] [blame] | 196 | /* If we can't put it there, put it somewhere */ |
xypron.glpk@gmx.de | f6b9912 | 2017-08-11 21:19:25 +0200 | [diff] [blame] | 197 | new_fdt_addr = (ulong)memalign(EFI_PAGE_SIZE, fdt_size); |
Simon Glass | 5d618df | 2018-08-08 03:54:30 -0600 | [diff] [blame] | 198 | ret = efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS, |
| 199 | EFI_RUNTIME_SERVICES_DATA, fdt_pages, |
| 200 | &new_fdt_addr); |
| 201 | if (ret != EFI_SUCCESS) { |
Alexander Graf | 9b9b716 | 2017-07-03 13:32:35 +0200 | [diff] [blame] | 202 | printf("ERROR: Failed to reserve space for FDT\n"); |
Simon Glass | 5d618df | 2018-08-08 03:54:30 -0600 | [diff] [blame] | 203 | goto done; |
Alexander Graf | 9b9b716 | 2017-07-03 13:32:35 +0200 | [diff] [blame] | 204 | } |
Alexander Graf | fdbae01 | 2016-04-11 23:51:01 +0200 | [diff] [blame] | 205 | } |
Alexander Graf | 9b9b716 | 2017-07-03 13:32:35 +0200 | [diff] [blame] | 206 | |
Simon Glass | 5d618df | 2018-08-08 03:54:30 -0600 | [diff] [blame] | 207 | new_fdt = map_sysmem(new_fdt_addr, fdt_size); |
Alexander Graf | 2ff5cc3 | 2016-04-11 16:55:26 +0200 | [diff] [blame] | 208 | memcpy(new_fdt, fdt, fdt_totalsize(fdt)); |
| 209 | fdt_set_totalsize(new_fdt, fdt_size); |
| 210 | |
Simon Glass | 5d618df | 2018-08-08 03:54:30 -0600 | [diff] [blame] | 211 | *fdt_addrp = new_fdt_addr; |
| 212 | *fdt_sizep = fdt_size; |
| 213 | done: |
| 214 | return ret; |
Alexander Graf | 2ff5cc3 | 2016-04-11 16:55:26 +0200 | [diff] [blame] | 215 | } |
| 216 | |
Heinrich Schuchardt | 4e363a9 | 2017-10-18 18:13:08 +0200 | [diff] [blame] | 217 | static efi_status_t efi_do_enter( |
Heinrich Schuchardt | b7cb8b4 | 2018-01-11 08:16:09 +0100 | [diff] [blame] | 218 | efi_handle_t image_handle, struct efi_system_table *st, |
Alexander Graf | 54d8cdf | 2018-01-24 00:18:08 +0100 | [diff] [blame] | 219 | EFIAPI efi_status_t (*entry)( |
| 220 | efi_handle_t image_handle, |
| 221 | struct efi_system_table *st)) |
xypron.glpk@gmx.de | 1e5afc6 | 2017-07-04 23:15:21 +0200 | [diff] [blame] | 222 | { |
| 223 | efi_status_t ret = EFI_LOAD_ERROR; |
| 224 | |
| 225 | if (entry) |
| 226 | ret = entry(image_handle, st); |
| 227 | st->boottime->exit(image_handle, ret, 0, NULL); |
| 228 | return ret; |
| 229 | } |
| 230 | |
Alison Wang | 73818d5 | 2016-11-10 10:49:03 +0800 | [diff] [blame] | 231 | #ifdef CONFIG_ARM64 |
Alexander Graf | 54d8cdf | 2018-01-24 00:18:08 +0100 | [diff] [blame] | 232 | static efi_status_t efi_run_in_el2(EFIAPI efi_status_t (*entry)( |
Heinrich Schuchardt | b7cb8b4 | 2018-01-11 08:16:09 +0100 | [diff] [blame] | 233 | efi_handle_t image_handle, struct efi_system_table *st), |
| 234 | efi_handle_t image_handle, struct efi_system_table *st) |
Alison Wang | 73818d5 | 2016-11-10 10:49:03 +0800 | [diff] [blame] | 235 | { |
| 236 | /* Enable caches again */ |
| 237 | dcache_enable(); |
| 238 | |
xypron.glpk@gmx.de | 1e5afc6 | 2017-07-04 23:15:21 +0200 | [diff] [blame] | 239 | return efi_do_enter(image_handle, st, entry); |
Alison Wang | 73818d5 | 2016-11-10 10:49:03 +0800 | [diff] [blame] | 240 | } |
| 241 | #endif |
| 242 | |
Mark Kettenis | 5eb68ed | 2018-06-15 23:47:12 +0200 | [diff] [blame] | 243 | #ifdef CONFIG_ARMV7_NONSEC |
Mark Kettenis | 6d140a6 | 2018-06-15 23:47:13 +0200 | [diff] [blame] | 244 | static bool is_nonsec; |
| 245 | |
Mark Kettenis | 5eb68ed | 2018-06-15 23:47:12 +0200 | [diff] [blame] | 246 | static efi_status_t efi_run_in_hyp(EFIAPI efi_status_t (*entry)( |
| 247 | efi_handle_t image_handle, struct efi_system_table *st), |
| 248 | efi_handle_t image_handle, struct efi_system_table *st) |
| 249 | { |
| 250 | /* Enable caches again */ |
| 251 | dcache_enable(); |
| 252 | |
Mark Kettenis | 6d140a6 | 2018-06-15 23:47:13 +0200 | [diff] [blame] | 253 | is_nonsec = true; |
| 254 | |
Mark Kettenis | 5eb68ed | 2018-06-15 23:47:12 +0200 | [diff] [blame] | 255 | return efi_do_enter(image_handle, st, entry); |
| 256 | } |
| 257 | #endif |
| 258 | |
Simon Glass | 54b9b6e | 2018-06-18 08:08:28 -0600 | [diff] [blame] | 259 | /* |
| 260 | * efi_carve_out_dt_rsv() - Carve out DT reserved memory ranges |
| 261 | * |
| 262 | * The mem_rsv entries of the FDT are added to the memory map. Any failures are |
| 263 | * ignored because this is not critical and we would rather continue to try to |
| 264 | * boot. |
| 265 | * |
| 266 | * @fdt: Pointer to device tree |
| 267 | */ |
| 268 | static void efi_carve_out_dt_rsv(void *fdt) |
Alexander Graf | 22c88bf | 2018-04-06 09:40:51 +0200 | [diff] [blame] | 269 | { |
| 270 | int nr_rsv, i; |
| 271 | uint64_t addr, size, pages; |
| 272 | |
| 273 | nr_rsv = fdt_num_mem_rsv(fdt); |
| 274 | |
| 275 | /* Look for an existing entry and add it to the efi mem map. */ |
| 276 | for (i = 0; i < nr_rsv; i++) { |
| 277 | if (fdt_get_mem_rsv(fdt, i, &addr, &size) != 0) |
| 278 | continue; |
| 279 | |
| 280 | pages = ALIGN(size, EFI_PAGE_SIZE) >> EFI_PAGE_SHIFT; |
Simon Glass | 54b9b6e | 2018-06-18 08:08:28 -0600 | [diff] [blame] | 281 | if (!efi_add_memory_map(addr, pages, EFI_RESERVED_MEMORY_TYPE, |
| 282 | false)) |
| 283 | printf("FDT memrsv map %d: Failed to add to map\n", i); |
Alexander Graf | 22c88bf | 2018-04-06 09:40:51 +0200 | [diff] [blame] | 284 | } |
Alexander Graf | 22c88bf | 2018-04-06 09:40:51 +0200 | [diff] [blame] | 285 | } |
| 286 | |
Simon Glass | 5d618df | 2018-08-08 03:54:30 -0600 | [diff] [blame] | 287 | static efi_status_t efi_install_fdt(ulong fdt_addr) |
Heinrich Schuchardt | 44ab21b | 2018-03-03 15:29:03 +0100 | [diff] [blame] | 288 | { |
| 289 | bootm_headers_t img = { 0 }; |
Simon Glass | 5d618df | 2018-08-08 03:54:30 -0600 | [diff] [blame] | 290 | ulong fdt_pages, fdt_size, fdt_start; |
Heinrich Schuchardt | 44ab21b | 2018-03-03 15:29:03 +0100 | [diff] [blame] | 291 | efi_status_t ret; |
Simon Glass | 5d618df | 2018-08-08 03:54:30 -0600 | [diff] [blame] | 292 | void *fdt; |
Heinrich Schuchardt | 44ab21b | 2018-03-03 15:29:03 +0100 | [diff] [blame] | 293 | |
Simon Glass | 5d618df | 2018-08-08 03:54:30 -0600 | [diff] [blame] | 294 | fdt = map_sysmem(fdt_addr, 0); |
Heinrich Schuchardt | 44ab21b | 2018-03-03 15:29:03 +0100 | [diff] [blame] | 295 | if (fdt_check_header(fdt)) { |
| 296 | printf("ERROR: invalid device tree\n"); |
| 297 | return EFI_INVALID_PARAMETER; |
| 298 | } |
| 299 | |
| 300 | /* Prepare fdt for payload */ |
Simon Glass | 5d618df | 2018-08-08 03:54:30 -0600 | [diff] [blame] | 301 | ret = copy_fdt(&fdt_addr, &fdt_size); |
| 302 | if (ret) |
| 303 | return ret; |
Heinrich Schuchardt | 44ab21b | 2018-03-03 15:29:03 +0100 | [diff] [blame] | 304 | |
Simon Glass | 5d618df | 2018-08-08 03:54:30 -0600 | [diff] [blame] | 305 | unmap_sysmem(fdt); |
| 306 | fdt = map_sysmem(fdt_addr, 0); |
| 307 | fdt_size = fdt_totalsize(fdt); |
Heinrich Schuchardt | 44ab21b | 2018-03-03 15:29:03 +0100 | [diff] [blame] | 308 | if (image_setup_libfdt(&img, fdt, 0, NULL)) { |
| 309 | printf("ERROR: failed to process device tree\n"); |
| 310 | return EFI_LOAD_ERROR; |
| 311 | } |
| 312 | |
Simon Glass | 54b9b6e | 2018-06-18 08:08:28 -0600 | [diff] [blame] | 313 | efi_carve_out_dt_rsv(fdt); |
Alexander Graf | 22c88bf | 2018-04-06 09:40:51 +0200 | [diff] [blame] | 314 | |
Heinrich Schuchardt | 44ab21b | 2018-03-03 15:29:03 +0100 | [diff] [blame] | 315 | /* Link to it in the efi tables */ |
| 316 | ret = efi_install_configuration_table(&efi_guid_fdt, fdt); |
| 317 | if (ret != EFI_SUCCESS) |
| 318 | return EFI_OUT_OF_RESOURCES; |
| 319 | |
| 320 | /* And reserve the space in the memory map */ |
Simon Glass | 5d618df | 2018-08-08 03:54:30 -0600 | [diff] [blame] | 321 | fdt_start = fdt_addr; |
Heinrich Schuchardt | 44ab21b | 2018-03-03 15:29:03 +0100 | [diff] [blame] | 322 | fdt_pages = fdt_size >> EFI_PAGE_SHIFT; |
Simon Glass | 5d618df | 2018-08-08 03:54:30 -0600 | [diff] [blame] | 323 | |
Heinrich Schuchardt | 44ab21b | 2018-03-03 15:29:03 +0100 | [diff] [blame] | 324 | ret = efi_add_memory_map(fdt_start, fdt_pages, |
| 325 | EFI_BOOT_SERVICES_DATA, true); |
Simon Glass | 5d618df | 2018-08-08 03:54:30 -0600 | [diff] [blame] | 326 | |
Heinrich Schuchardt | 44ab21b | 2018-03-03 15:29:03 +0100 | [diff] [blame] | 327 | return ret; |
| 328 | } |
| 329 | |
Heinrich Schuchardt | 3c3a735 | 2018-09-23 17:21:51 +0200 | [diff] [blame] | 330 | /** |
| 331 | * do_bootefi_exec() - execute EFI binary |
| 332 | * |
| 333 | * @efi: address of the binary |
| 334 | * @device_path: path of the device from which the binary was loaded |
| 335 | * @image_path: device path of the binary |
| 336 | * Return: status code |
| 337 | * |
| 338 | * Load the EFI binary into a newly assigned memory unwinding the relocation |
| 339 | * information, install the loaded image protocol, and call the binary. |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 340 | */ |
Heinrich Schuchardt | 44ab21b | 2018-03-03 15:29:03 +0100 | [diff] [blame] | 341 | static efi_status_t do_bootefi_exec(void *efi, |
Heinrich Schuchardt | 4e363a9 | 2017-10-18 18:13:08 +0200 | [diff] [blame] | 342 | struct efi_device_path *device_path, |
| 343 | struct efi_device_path *image_path) |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 344 | { |
Heinrich Schuchardt | 078d6e2 | 2018-09-16 07:19:48 +0200 | [diff] [blame] | 345 | efi_handle_t mem_handle = NULL; |
Rob Clark | 18ceba7 | 2017-10-10 08:23:06 -0400 | [diff] [blame] | 346 | struct efi_device_path *memdp = NULL; |
Heinrich Schuchardt | 4b055a2 | 2018-03-03 15:29:01 +0100 | [diff] [blame] | 347 | efi_status_t ret; |
Heinrich Schuchardt | 4f94ec5 | 2018-09-26 05:27:54 +0200 | [diff] [blame^] | 348 | struct efi_loaded_image_obj *image_obj = NULL; |
Heinrich Schuchardt | 3c3a735 | 2018-09-23 17:21:51 +0200 | [diff] [blame] | 349 | struct efi_loaded_image *loaded_image_info = NULL; |
Rob Clark | f8db922 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 350 | |
Alexander Graf | 54d8cdf | 2018-01-24 00:18:08 +0100 | [diff] [blame] | 351 | EFIAPI efi_status_t (*entry)(efi_handle_t image_handle, |
| 352 | struct efi_system_table *st); |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 353 | |
Rob Clark | 18ceba7 | 2017-10-10 08:23:06 -0400 | [diff] [blame] | 354 | /* |
| 355 | * Special case for efi payload not loaded from disk, such as |
| 356 | * 'bootefi hello' or for example payload loaded directly into |
Heinrich Schuchardt | 078d6e2 | 2018-09-16 07:19:48 +0200 | [diff] [blame] | 357 | * memory via jtag, etc: |
Rob Clark | 18ceba7 | 2017-10-10 08:23:06 -0400 | [diff] [blame] | 358 | */ |
| 359 | if (!device_path && !image_path) { |
| 360 | printf("WARNING: using memory device/image path, this may confuse some payloads!\n"); |
| 361 | /* actual addresses filled in after efi_load_pe() */ |
| 362 | memdp = efi_dp_from_mem(0, 0, 0); |
| 363 | device_path = image_path = memdp; |
Heinrich Schuchardt | 078d6e2 | 2018-09-16 07:19:48 +0200 | [diff] [blame] | 364 | /* |
| 365 | * Grub expects that the device path of the loaded image is |
| 366 | * installed on a handle. |
| 367 | */ |
| 368 | ret = efi_create_handle(&mem_handle); |
| 369 | if (ret != EFI_SUCCESS) |
| 370 | goto exit; |
| 371 | ret = efi_add_protocol(mem_handle, &efi_guid_device_path, |
Alexander Graf | 2d00dbe | 2018-06-12 10:21:16 +0200 | [diff] [blame] | 372 | device_path); |
| 373 | if (ret != EFI_SUCCESS) |
| 374 | goto exit; |
Rob Clark | 18ceba7 | 2017-10-10 08:23:06 -0400 | [diff] [blame] | 375 | } else { |
| 376 | assert(device_path && image_path); |
| 377 | } |
| 378 | |
Heinrich Schuchardt | 4f94ec5 | 2018-09-26 05:27:54 +0200 | [diff] [blame^] | 379 | ret = efi_setup_loaded_image(device_path, image_path, &image_obj, |
Heinrich Schuchardt | 3c3a735 | 2018-09-23 17:21:51 +0200 | [diff] [blame] | 380 | &loaded_image_info); |
| 381 | if (ret != EFI_SUCCESS) |
| 382 | goto exit; |
Rob Clark | f8db922 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 383 | |
Heinrich Schuchardt | a4589e2 | 2017-10-18 18:13:15 +0200 | [diff] [blame] | 384 | /* Transfer environment variable bootargs as load options */ |
Heinrich Schuchardt | 3c3a735 | 2018-09-23 17:21:51 +0200 | [diff] [blame] | 385 | set_load_options(loaded_image_info, "bootargs"); |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 386 | /* Load the EFI payload */ |
Heinrich Schuchardt | 4f94ec5 | 2018-09-26 05:27:54 +0200 | [diff] [blame^] | 387 | entry = efi_load_pe(image_obj, efi, loaded_image_info); |
Rob Clark | f8db922 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 388 | if (!entry) { |
Heinrich Schuchardt | 4b055a2 | 2018-03-03 15:29:01 +0100 | [diff] [blame] | 389 | ret = EFI_LOAD_ERROR; |
Rob Clark | f8db922 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 390 | goto exit; |
| 391 | } |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 392 | |
Rob Clark | 18ceba7 | 2017-10-10 08:23:06 -0400 | [diff] [blame] | 393 | if (memdp) { |
| 394 | struct efi_device_path_memory *mdp = (void *)memdp; |
Heinrich Schuchardt | 3c3a735 | 2018-09-23 17:21:51 +0200 | [diff] [blame] | 395 | mdp->memory_type = loaded_image_info->image_code_type; |
| 396 | mdp->start_address = (uintptr_t)loaded_image_info->image_base; |
Rob Clark | 18ceba7 | 2017-10-10 08:23:06 -0400 | [diff] [blame] | 397 | mdp->end_address = mdp->start_address + |
Heinrich Schuchardt | 3c3a735 | 2018-09-23 17:21:51 +0200 | [diff] [blame] | 398 | loaded_image_info->image_size; |
Rob Clark | 18ceba7 | 2017-10-10 08:23:06 -0400 | [diff] [blame] | 399 | } |
| 400 | |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 401 | /* we don't support much: */ |
| 402 | env_set("efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_OsIndicationsSupported", |
| 403 | "{ro,boot}(blob)0000000000000000"); |
| 404 | |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 405 | /* Call our payload! */ |
Alexander Graf | 62a6748 | 2016-06-02 11:38:27 +0200 | [diff] [blame] | 406 | debug("%s:%d Jumping to 0x%lx\n", __func__, __LINE__, (long)entry); |
Alexander Graf | 988c066 | 2016-05-20 23:28:23 +0200 | [diff] [blame] | 407 | |
Heinrich Schuchardt | 4f94ec5 | 2018-09-26 05:27:54 +0200 | [diff] [blame^] | 408 | if (setjmp(&image_obj->exit_jmp)) { |
| 409 | ret = image_obj->exit_status; |
Rob Clark | f8db922 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 410 | goto exit; |
Alexander Graf | 988c066 | 2016-05-20 23:28:23 +0200 | [diff] [blame] | 411 | } |
| 412 | |
Alexander Graf | bfd199c | 2016-11-17 01:02:58 +0100 | [diff] [blame] | 413 | #ifdef CONFIG_ARM64 |
| 414 | /* On AArch64 we need to make sure we call our payload in < EL3 */ |
| 415 | if (current_el() == 3) { |
| 416 | smp_kick_all_cpus(); |
| 417 | dcache_disable(); /* flush cache before switch to EL2 */ |
Alison Wang | 73818d5 | 2016-11-10 10:49:03 +0800 | [diff] [blame] | 418 | |
| 419 | /* Move into EL2 and keep running there */ |
Heinrich Schuchardt | 754d497 | 2017-11-26 14:05:22 +0100 | [diff] [blame] | 420 | armv8_switch_to_el2((ulong)entry, |
Heinrich Schuchardt | 4f94ec5 | 2018-09-26 05:27:54 +0200 | [diff] [blame^] | 421 | (ulong)&image_obj->parent, |
Alison Wang | eb2088d | 2017-01-17 09:39:17 +0800 | [diff] [blame] | 422 | (ulong)&systab, 0, (ulong)efi_run_in_el2, |
Alison Wang | 73818d5 | 2016-11-10 10:49:03 +0800 | [diff] [blame] | 423 | ES_TO_AARCH64); |
| 424 | |
| 425 | /* Should never reach here, efi exits with longjmp */ |
| 426 | while (1) { } |
Alexander Graf | bfd199c | 2016-11-17 01:02:58 +0100 | [diff] [blame] | 427 | } |
| 428 | #endif |
| 429 | |
Mark Kettenis | 5eb68ed | 2018-06-15 23:47:12 +0200 | [diff] [blame] | 430 | #ifdef CONFIG_ARMV7_NONSEC |
Mark Kettenis | 6d140a6 | 2018-06-15 23:47:13 +0200 | [diff] [blame] | 431 | if (armv7_boot_nonsec() && !is_nonsec) { |
Mark Kettenis | 5eb68ed | 2018-06-15 23:47:12 +0200 | [diff] [blame] | 432 | dcache_disable(); /* flush cache before switch to HYP */ |
| 433 | |
| 434 | armv7_init_nonsec(); |
| 435 | secure_ram_addr(_do_nonsec_entry)( |
| 436 | efi_run_in_hyp, |
| 437 | (uintptr_t)entry, |
Heinrich Schuchardt | 4f94ec5 | 2018-09-26 05:27:54 +0200 | [diff] [blame^] | 438 | (uintptr_t)&image_obj->parent, |
Mark Kettenis | 5eb68ed | 2018-06-15 23:47:12 +0200 | [diff] [blame] | 439 | (uintptr_t)&systab); |
| 440 | |
| 441 | /* Should never reach here, efi exits with longjmp */ |
| 442 | while (1) { } |
| 443 | } |
| 444 | #endif |
| 445 | |
Heinrich Schuchardt | 4f94ec5 | 2018-09-26 05:27:54 +0200 | [diff] [blame^] | 446 | ret = efi_do_enter(&image_obj->parent, &systab, entry); |
Rob Clark | f8db922 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 447 | |
| 448 | exit: |
| 449 | /* image has returned, loaded-image obj goes *poof*: */ |
Heinrich Schuchardt | 4f94ec5 | 2018-09-26 05:27:54 +0200 | [diff] [blame^] | 450 | if (image_obj) |
| 451 | efi_delete_handle(&image_obj->parent); |
Heinrich Schuchardt | 078d6e2 | 2018-09-16 07:19:48 +0200 | [diff] [blame] | 452 | if (mem_handle) |
| 453 | efi_delete_handle(mem_handle); |
Rob Clark | f8db922 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 454 | |
| 455 | return ret; |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 456 | } |
| 457 | |
Heinrich Schuchardt | 44ab21b | 2018-03-03 15:29:03 +0100 | [diff] [blame] | 458 | static int do_bootefi_bootmgr_exec(void) |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 459 | { |
| 460 | struct efi_device_path *device_path, *file_path; |
| 461 | void *addr; |
| 462 | efi_status_t r; |
| 463 | |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 464 | addr = efi_bootmgr_load(&device_path, &file_path); |
| 465 | if (!addr) |
| 466 | return 1; |
| 467 | |
| 468 | printf("## Starting EFI application at %p ...\n", addr); |
Heinrich Schuchardt | 44ab21b | 2018-03-03 15:29:03 +0100 | [diff] [blame] | 469 | r = do_bootefi_exec(addr, device_path, file_path); |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 470 | printf("## Application terminated, r = %lu\n", |
| 471 | r & ~EFI_ERROR_MASK); |
| 472 | |
| 473 | if (r != EFI_SUCCESS) |
| 474 | return 1; |
| 475 | |
| 476 | return 0; |
| 477 | } |
| 478 | |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 479 | /* Interpreter command to boot an arbitrary EFI image from memory */ |
| 480 | static int do_bootefi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 481 | { |
Heinrich Schuchardt | 44ab21b | 2018-03-03 15:29:03 +0100 | [diff] [blame] | 482 | unsigned long addr; |
| 483 | char *saddr; |
Heinrich Schuchardt | 4e363a9 | 2017-10-18 18:13:08 +0200 | [diff] [blame] | 484 | efi_status_t r; |
Alexander Graf | a241451 | 2018-06-18 17:22:58 +0200 | [diff] [blame] | 485 | unsigned long fdt_addr; |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 486 | |
Heinrich Schuchardt | 954e2b9 | 2018-04-03 21:59:32 +0200 | [diff] [blame] | 487 | /* Allow unaligned memory access */ |
| 488 | allow_unaligned(); |
| 489 | |
Heinrich Schuchardt | 8c0dfc2 | 2018-03-03 15:29:02 +0100 | [diff] [blame] | 490 | /* Initialize EFI drivers */ |
| 491 | r = efi_init_obj_list(); |
| 492 | if (r != EFI_SUCCESS) { |
| 493 | printf("Error: Cannot set up EFI drivers, r = %lu\n", |
| 494 | r & ~EFI_ERROR_MASK); |
| 495 | return CMD_RET_FAILURE; |
| 496 | } |
| 497 | |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 498 | if (argc < 2) |
Bin Meng | da800ff | 2016-08-13 04:02:06 -0700 | [diff] [blame] | 499 | return CMD_RET_USAGE; |
Heinrich Schuchardt | 44ab21b | 2018-03-03 15:29:03 +0100 | [diff] [blame] | 500 | |
| 501 | if (argc > 2) { |
Alexander Graf | a241451 | 2018-06-18 17:22:58 +0200 | [diff] [blame] | 502 | fdt_addr = simple_strtoul(argv[2], NULL, 16); |
Heinrich Schuchardt | 44ab21b | 2018-03-03 15:29:03 +0100 | [diff] [blame] | 503 | if (!fdt_addr && *argv[2] != '0') |
| 504 | return CMD_RET_USAGE; |
| 505 | /* Install device tree */ |
Simon Glass | 5d618df | 2018-08-08 03:54:30 -0600 | [diff] [blame] | 506 | r = efi_install_fdt(fdt_addr); |
Heinrich Schuchardt | 44ab21b | 2018-03-03 15:29:03 +0100 | [diff] [blame] | 507 | if (r != EFI_SUCCESS) { |
| 508 | printf("ERROR: failed to install device tree\n"); |
| 509 | return CMD_RET_FAILURE; |
| 510 | } |
| 511 | } else { |
| 512 | /* Remove device tree. EFI_NOT_FOUND can be ignored here */ |
| 513 | efi_install_configuration_table(&efi_guid_fdt, NULL); |
| 514 | printf("WARNING: booting without device tree\n"); |
| 515 | } |
Simon Glass | fac4ced | 2016-11-07 08:47:08 -0700 | [diff] [blame] | 516 | #ifdef CONFIG_CMD_BOOTEFI_HELLO |
| 517 | if (!strcmp(argv[1], "hello")) { |
Heinrich Schuchardt | 7804ae9 | 2017-09-05 03:19:37 +0200 | [diff] [blame] | 518 | ulong size = __efi_helloworld_end - __efi_helloworld_begin; |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 519 | |
Heinrich Schuchardt | e6a9ab5 | 2017-08-28 18:54:30 +0200 | [diff] [blame] | 520 | saddr = env_get("loadaddr"); |
| 521 | if (saddr) |
| 522 | addr = simple_strtoul(saddr, NULL, 16); |
| 523 | else |
| 524 | addr = CONFIG_SYS_LOAD_ADDR; |
Alexander Graf | a241451 | 2018-06-18 17:22:58 +0200 | [diff] [blame] | 525 | memcpy(map_sysmem(addr, size), __efi_helloworld_begin, size); |
Simon Glass | fac4ced | 2016-11-07 08:47:08 -0700 | [diff] [blame] | 526 | } else |
| 527 | #endif |
Heinrich Schuchardt | d33ae3e | 2017-09-15 10:06:11 +0200 | [diff] [blame] | 528 | #ifdef CONFIG_CMD_BOOTEFI_SELFTEST |
| 529 | if (!strcmp(argv[1], "selftest")) { |
Heinrich Schuchardt | 4f94ec5 | 2018-09-26 05:27:54 +0200 | [diff] [blame^] | 530 | struct efi_loaded_image_obj *image_obj; |
Heinrich Schuchardt | 3c3a735 | 2018-09-23 17:21:51 +0200 | [diff] [blame] | 531 | struct efi_loaded_image *loaded_image_info; |
Heinrich Schuchardt | 389f251 | 2017-09-20 22:54:58 +0200 | [diff] [blame] | 532 | |
Heinrich Schuchardt | a809bc2 | 2017-10-18 18:13:09 +0200 | [diff] [blame] | 533 | /* Construct a dummy device path. */ |
| 534 | bootefi_device_path = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE, |
| 535 | (uintptr_t)&efi_selftest, |
| 536 | (uintptr_t)&efi_selftest); |
| 537 | bootefi_image_path = efi_dp_from_file(NULL, 0, "\\selftest"); |
| 538 | |
Heinrich Schuchardt | 3c3a735 | 2018-09-23 17:21:51 +0200 | [diff] [blame] | 539 | r = efi_setup_loaded_image(bootefi_device_path, |
Heinrich Schuchardt | 4f94ec5 | 2018-09-26 05:27:54 +0200 | [diff] [blame^] | 540 | bootefi_image_path, &image_obj, |
Heinrich Schuchardt | 3c3a735 | 2018-09-23 17:21:51 +0200 | [diff] [blame] | 541 | &loaded_image_info); |
| 542 | if (r != EFI_SUCCESS) |
| 543 | return CMD_RET_FAILURE; |
| 544 | |
Heinrich Schuchardt | d33ae3e | 2017-09-15 10:06:11 +0200 | [diff] [blame] | 545 | efi_save_gd(); |
Heinrich Schuchardt | 02efd5d | 2017-10-18 18:13:13 +0200 | [diff] [blame] | 546 | /* Transfer environment variable efi_selftest as load options */ |
Heinrich Schuchardt | 3c3a735 | 2018-09-23 17:21:51 +0200 | [diff] [blame] | 547 | set_load_options(loaded_image_info, "efi_selftest"); |
Heinrich Schuchardt | 02efd5d | 2017-10-18 18:13:13 +0200 | [diff] [blame] | 548 | /* Execute the test */ |
Heinrich Schuchardt | 4f94ec5 | 2018-09-26 05:27:54 +0200 | [diff] [blame^] | 549 | r = efi_selftest(&image_obj->parent, &systab); |
Heinrich Schuchardt | d2d90b4 | 2017-10-18 18:13:14 +0200 | [diff] [blame] | 550 | efi_restore_gd(); |
Heinrich Schuchardt | 3c3a735 | 2018-09-23 17:21:51 +0200 | [diff] [blame] | 551 | free(loaded_image_info->load_options); |
Heinrich Schuchardt | 4f94ec5 | 2018-09-26 05:27:54 +0200 | [diff] [blame^] | 552 | efi_delete_handle(&image_obj->parent); |
Heinrich Schuchardt | d2d90b4 | 2017-10-18 18:13:14 +0200 | [diff] [blame] | 553 | return r != EFI_SUCCESS; |
Heinrich Schuchardt | d33ae3e | 2017-09-15 10:06:11 +0200 | [diff] [blame] | 554 | } else |
| 555 | #endif |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 556 | if (!strcmp(argv[1], "bootmgr")) { |
Heinrich Schuchardt | 44ab21b | 2018-03-03 15:29:03 +0100 | [diff] [blame] | 557 | return do_bootefi_bootmgr_exec(); |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 558 | } else { |
Simon Glass | fac4ced | 2016-11-07 08:47:08 -0700 | [diff] [blame] | 559 | saddr = argv[1]; |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 560 | |
Simon Glass | fac4ced | 2016-11-07 08:47:08 -0700 | [diff] [blame] | 561 | addr = simple_strtoul(saddr, NULL, 16); |
Heinrich Schuchardt | aa6fd9a | 2018-01-24 20:33:54 +0100 | [diff] [blame] | 562 | /* Check that a numeric value was passed */ |
| 563 | if (!addr && *saddr != '0') |
| 564 | return CMD_RET_USAGE; |
Simon Glass | fac4ced | 2016-11-07 08:47:08 -0700 | [diff] [blame] | 565 | |
Alexander Graf | 54c1e83 | 2016-04-14 16:07:53 +0200 | [diff] [blame] | 566 | } |
| 567 | |
Simon Glass | 1a5490e | 2016-11-07 08:47:05 -0700 | [diff] [blame] | 568 | printf("## Starting EFI application at %08lx ...\n", addr); |
Alexander Graf | a241451 | 2018-06-18 17:22:58 +0200 | [diff] [blame] | 569 | r = do_bootefi_exec(map_sysmem(addr, 0), bootefi_device_path, |
Heinrich Schuchardt | 44ab21b | 2018-03-03 15:29:03 +0100 | [diff] [blame] | 570 | bootefi_image_path); |
xypron.glpk@gmx.de | 5eadbfa | 2017-07-04 23:15:23 +0200 | [diff] [blame] | 571 | printf("## Application terminated, r = %lu\n", |
| 572 | r & ~EFI_ERROR_MASK); |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 573 | |
xypron.glpk@gmx.de | 5eadbfa | 2017-07-04 23:15:23 +0200 | [diff] [blame] | 574 | if (r != EFI_SUCCESS) |
| 575 | return 1; |
| 576 | else |
| 577 | return 0; |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | #ifdef CONFIG_SYS_LONGHELP |
| 581 | static char bootefi_help_text[] = |
Alexander Graf | 54c1e83 | 2016-04-14 16:07:53 +0200 | [diff] [blame] | 582 | "<image address> [fdt address]\n" |
| 583 | " - boot EFI payload stored at address <image address>.\n" |
| 584 | " If specified, the device tree located at <fdt address> gets\n" |
Simon Glass | fac4ced | 2016-11-07 08:47:08 -0700 | [diff] [blame] | 585 | " exposed as EFI configuration table.\n" |
| 586 | #ifdef CONFIG_CMD_BOOTEFI_HELLO |
Heinrich Schuchardt | d33ae3e | 2017-09-15 10:06:11 +0200 | [diff] [blame] | 587 | "bootefi hello\n" |
| 588 | " - boot a sample Hello World application stored within U-Boot\n" |
| 589 | #endif |
| 590 | #ifdef CONFIG_CMD_BOOTEFI_SELFTEST |
Heinrich Schuchardt | 44ab21b | 2018-03-03 15:29:03 +0100 | [diff] [blame] | 591 | "bootefi selftest [fdt address]\n" |
Heinrich Schuchardt | d33ae3e | 2017-09-15 10:06:11 +0200 | [diff] [blame] | 592 | " - boot an EFI selftest application stored within U-Boot\n" |
Heinrich Schuchardt | 02efd5d | 2017-10-18 18:13:13 +0200 | [diff] [blame] | 593 | " Use environment variable efi_selftest to select a single test.\n" |
| 594 | " Use 'setenv efi_selftest list' to enumerate all tests.\n" |
Simon Glass | fac4ced | 2016-11-07 08:47:08 -0700 | [diff] [blame] | 595 | #endif |
Heinrich Schuchardt | 1b2320f | 2018-01-30 19:47:43 +0100 | [diff] [blame] | 596 | "bootefi bootmgr [fdt addr]\n" |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 597 | " - load and boot EFI payload based on BootOrder/BootXXXX variables.\n" |
| 598 | "\n" |
| 599 | " If specified, the device tree located at <fdt address> gets\n" |
| 600 | " exposed as EFI configuration table.\n"; |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 601 | #endif |
| 602 | |
| 603 | U_BOOT_CMD( |
Alexander Graf | 54c1e83 | 2016-04-14 16:07:53 +0200 | [diff] [blame] | 604 | bootefi, 3, 0, do_bootefi, |
Sergey Kubushyn | 268f19e | 2016-06-07 11:14:31 -0700 | [diff] [blame] | 605 | "Boots an EFI payload from memory", |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 606 | bootefi_help_text |
| 607 | ); |
Alexander Graf | 8f19f26 | 2016-03-04 01:10:14 +0100 | [diff] [blame] | 608 | |
Rob Clark | f8db922 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 609 | void efi_set_bootdev(const char *dev, const char *devnr, const char *path) |
| 610 | { |
| 611 | char filename[32] = { 0 }; /* dp->str is u16[32] long */ |
| 612 | char *s; |
Alexander Graf | 34118e7 | 2016-08-05 14:49:53 +0200 | [diff] [blame] | 613 | |
Heinrich Schuchardt | 761ecc8 | 2018-09-16 07:20:21 +0200 | [diff] [blame] | 614 | /* efi_set_bootdev is typically called repeatedly, recover memory */ |
| 615 | efi_free_pool(bootefi_device_path); |
| 616 | efi_free_pool(bootefi_image_path); |
| 617 | /* If blk_get_device_part_str fails, avoid duplicate free. */ |
| 618 | bootefi_device_path = NULL; |
| 619 | bootefi_image_path = NULL; |
| 620 | |
Rob Clark | f8db922 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 621 | if (strcmp(dev, "Net")) { |
| 622 | struct blk_desc *desc; |
Heinrich Schuchardt | 7e262a7 | 2018-04-03 22:40:55 +0200 | [diff] [blame] | 623 | disk_partition_t fs_partition; |
Rob Clark | f8db922 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 624 | int part; |
Alexander Graf | eb8d82c | 2016-04-11 16:16:18 +0200 | [diff] [blame] | 625 | |
Heinrich Schuchardt | 7e262a7 | 2018-04-03 22:40:55 +0200 | [diff] [blame] | 626 | part = blk_get_device_part_str(dev, devnr, &desc, &fs_partition, |
| 627 | 1); |
| 628 | if (part < 0) |
Stefan Roese | df76976 | 2017-11-17 08:47:09 +0100 | [diff] [blame] | 629 | return; |
Alexander Graf | 8f19f26 | 2016-03-04 01:10:14 +0100 | [diff] [blame] | 630 | |
Rob Clark | f8db922 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 631 | bootefi_device_path = efi_dp_from_part(desc, part); |
| 632 | } else { |
Joe Hershberger | 5277a97 | 2018-04-13 15:26:39 -0500 | [diff] [blame] | 633 | #ifdef CONFIG_NET |
Rob Clark | f8db922 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 634 | bootefi_device_path = efi_dp_from_eth(); |
| 635 | #endif |
| 636 | } |
Alexander Graf | c49a1b8 | 2016-04-11 16:16:19 +0200 | [diff] [blame] | 637 | |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 638 | if (!path) |
| 639 | return; |
| 640 | |
Alexander Graf | 45e437d | 2016-07-21 01:44:46 +0200 | [diff] [blame] | 641 | if (strcmp(dev, "Net")) { |
| 642 | /* Add leading / to fs paths, because they're absolute */ |
Rob Clark | f8db922 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 643 | snprintf(filename, sizeof(filename), "/%s", path); |
Alexander Graf | 45e437d | 2016-07-21 01:44:46 +0200 | [diff] [blame] | 644 | } else { |
Rob Clark | f8db922 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 645 | snprintf(filename, sizeof(filename), "%s", path); |
Alexander Graf | 45e437d | 2016-07-21 01:44:46 +0200 | [diff] [blame] | 646 | } |
Rob Clark | 6533652 | 2017-07-24 07:59:09 -0400 | [diff] [blame] | 647 | /* DOS style file path: */ |
Rob Clark | f8db922 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 648 | s = filename; |
Rob Clark | 6533652 | 2017-07-24 07:59:09 -0400 | [diff] [blame] | 649 | while ((s = strchr(s, '/'))) |
| 650 | *s++ = '\\'; |
Rob Clark | f8db922 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 651 | bootefi_image_path = efi_dp_from_file(NULL, 0, filename); |
Alexander Graf | 8f19f26 | 2016-03-04 01:10:14 +0100 | [diff] [blame] | 652 | } |