Tom Rini | f739fcd | 2018-05-07 17:02:21 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Alexander Graf | b993933 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 2 | /* |
| 3 | * EFI application loader |
| 4 | * |
| 5 | * Copyright (c) 2016 Alexander Graf |
Alexander Graf | b993933 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 6 | */ |
| 7 | |
Heinrich Schuchardt | d78e40d | 2017-10-18 18:13:13 +0200 | [diff] [blame] | 8 | #include <charset.h> |
Alexander Graf | b993933 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 9 | #include <common.h> |
| 10 | #include <command.h> |
Simon Glass | 9d92245 | 2017-05-17 17:18:03 -0600 | [diff] [blame] | 11 | #include <dm.h> |
Alexander Graf | b993933 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 12 | #include <efi_loader.h> |
Heinrich Schuchardt | d78e40d | 2017-10-18 18:13:13 +0200 | [diff] [blame] | 13 | #include <efi_selftest.h> |
Alexander Graf | b993933 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 14 | #include <errno.h> |
Masahiro Yamada | b08c8c4 | 2018-03-05 01:20:11 +0900 | [diff] [blame] | 15 | #include <linux/libfdt.h> |
| 16 | #include <linux/libfdt_env.h> |
Alexander Graf | 354264b | 2018-06-18 17:22:58 +0200 | [diff] [blame] | 17 | #include <mapmem.h> |
Alexander Graf | ad0c1a3 | 2016-04-11 23:51:01 +0200 | [diff] [blame] | 18 | #include <memalign.h> |
Alexander Graf | 0d9d501 | 2016-04-11 16:55:26 +0200 | [diff] [blame] | 19 | #include <asm/global_data.h> |
Simon Glass | e275458 | 2016-09-25 15:27:32 -0600 | [diff] [blame] | 20 | #include <asm-generic/sections.h> |
Heinrich Schuchardt | c3b11de | 2018-04-03 21:59:32 +0200 | [diff] [blame] | 21 | #include <asm-generic/unaligned.h> |
Simon Glass | e275458 | 2016-09-25 15:27:32 -0600 | [diff] [blame] | 22 | #include <linux/linkage.h> |
Alexander Graf | 0d9d501 | 2016-04-11 16:55:26 +0200 | [diff] [blame] | 23 | |
Mark Kettenis | dc500c3 | 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 | 0d9d501 | 2016-04-11 16:55:26 +0200 | [diff] [blame] | 29 | DECLARE_GLOBAL_DATA_PTR; |
Alexander Graf | b993933 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 30 | |
Heinrich Schuchardt | fc225e6 | 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 | 7cbc124 | 2017-07-19 19:37:22 +0200 | [diff] [blame] | 34 | |
Rob Clark | 95c5553 | 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 | b993933 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 37 | |
Heinrich Schuchardt | 7cbc124 | 2017-07-19 19:37:22 +0200 | [diff] [blame] | 38 | /* Initialize and populate EFI object list */ |
Heinrich Schuchardt | fc225e6 | 2018-03-03 15:29:02 +0100 | [diff] [blame] | 39 | efi_status_t efi_init_obj_list(void) |
Heinrich Schuchardt | 7cbc124 | 2017-07-19 19:37:22 +0200 | [diff] [blame] | 40 | { |
Heinrich Schuchardt | fc225e6 | 2018-03-03 15:29:02 +0100 | [diff] [blame] | 41 | efi_status_t ret = EFI_SUCCESS; |
| 42 | |
Heinrich Schuchardt | 1e1e1c2 | 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 | 098a6cd | 2018-03-03 15:28:58 +0100 | [diff] [blame] | 50 | /* Initialize once only */ |
Heinrich Schuchardt | fc225e6 | 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 | 7cbc124 | 2017-07-19 19:37:22 +0200 | [diff] [blame] | 53 | |
Heinrich Schuchardt | 640adad | 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 | 4e6b5d6 | 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 | 05ef48a | 2018-01-21 19:29:30 +0100 | [diff] [blame] | 64 | /* Initialize EFI driver uclass */ |
Heinrich Schuchardt | fc225e6 | 2018-03-03 15:29:02 +0100 | [diff] [blame] | 65 | ret = efi_driver_init(); |
| 66 | if (ret != EFI_SUCCESS) |
| 67 | goto out; |
Heinrich Schuchardt | 05ef48a | 2018-01-21 19:29:30 +0100 | [diff] [blame] | 68 | |
Heinrich Schuchardt | fc225e6 | 2018-03-03 15:29:02 +0100 | [diff] [blame] | 69 | ret = efi_console_register(); |
| 70 | if (ret != EFI_SUCCESS) |
| 71 | goto out; |
Heinrich Schuchardt | 7cbc124 | 2017-07-19 19:37:22 +0200 | [diff] [blame] | 72 | #ifdef CONFIG_PARTITIONS |
Heinrich Schuchardt | fc225e6 | 2018-03-03 15:29:02 +0100 | [diff] [blame] | 73 | ret = efi_disk_register(); |
| 74 | if (ret != EFI_SUCCESS) |
| 75 | goto out; |
Heinrich Schuchardt | 7cbc124 | 2017-07-19 19:37:22 +0200 | [diff] [blame] | 76 | #endif |
| 77 | #if defined(CONFIG_LCD) || defined(CONFIG_DM_VIDEO) |
Heinrich Schuchardt | fc225e6 | 2018-03-03 15:29:02 +0100 | [diff] [blame] | 78 | ret = efi_gop_register(); |
| 79 | if (ret != EFI_SUCCESS) |
| 80 | goto out; |
Heinrich Schuchardt | 7cbc124 | 2017-07-19 19:37:22 +0200 | [diff] [blame] | 81 | #endif |
Joe Hershberger | 092f2f3 | 2018-04-13 15:26:39 -0500 | [diff] [blame] | 82 | #ifdef CONFIG_NET |
Heinrich Schuchardt | fc225e6 | 2018-03-03 15:29:02 +0100 | [diff] [blame] | 83 | ret = efi_net_register(); |
| 84 | if (ret != EFI_SUCCESS) |
| 85 | goto out; |
Heinrich Schuchardt | 7cbc124 | 2017-07-19 19:37:22 +0200 | [diff] [blame] | 86 | #endif |
Bin Meng | 86df34d | 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 | 7cbc124 | 2017-07-19 19:37:22 +0200 | [diff] [blame] | 92 | #ifdef CONFIG_GENERATE_SMBIOS_TABLE |
Heinrich Schuchardt | fc225e6 | 2018-03-03 15:29:02 +0100 | [diff] [blame] | 93 | ret = efi_smbios_register(); |
| 94 | if (ret != EFI_SUCCESS) |
| 95 | goto out; |
Heinrich Schuchardt | 7cbc124 | 2017-07-19 19:37:22 +0200 | [diff] [blame] | 96 | #endif |
Heinrich Schuchardt | fc225e6 | 2018-03-03 15:29:02 +0100 | [diff] [blame] | 97 | ret = efi_watchdog_register(); |
| 98 | if (ret != EFI_SUCCESS) |
| 99 | goto out; |
Heinrich Schuchardt | 7cbc124 | 2017-07-19 19:37:22 +0200 | [diff] [blame] | 100 | |
| 101 | /* Initialize EFI runtime services */ |
Heinrich Schuchardt | fc225e6 | 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 | fc225e6 | 2018-03-03 15:29:02 +0100 | [diff] [blame] | 105 | |
| 106 | out: |
| 107 | efi_obj_list_initialized = ret; |
| 108 | return ret; |
Heinrich Schuchardt | 7cbc124 | 2017-07-19 19:37:22 +0200 | [diff] [blame] | 109 | } |
| 110 | |
Heinrich Schuchardt | d78e40d | 2017-10-18 18:13:13 +0200 | [diff] [blame] | 111 | /* |
Heinrich Schuchardt | c3b11de | 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 | d78e40d | 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 | 7086a71 | 2018-08-31 21:31:33 +0200 | [diff] [blame] | 131 | u16 *pos; |
Heinrich Schuchardt | d78e40d | 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 | 7086a71 | 2018-08-31 21:31:33 +0200 | [diff] [blame] | 137 | size = utf8_utf16_strlen(env) + 1; |
Heinrich Schuchardt | d78e40d | 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 | 7086a71 | 2018-08-31 21:31:33 +0200 | [diff] [blame] | 143 | pos = loaded_image_info->load_options; |
| 144 | utf8_utf16_strcpy(&pos, env); |
Heinrich Schuchardt | d78e40d | 2017-10-18 18:13:13 +0200 | [diff] [blame] | 145 | loaded_image_info->load_options_size = size * 2; |
| 146 | } |
| 147 | |
Simon Glass | 9dff490 | 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 | * |
Heinrich Schuchardt | 4574d1b | 2018-11-12 18:55:22 +0100 | [diff] [blame] | 155 | * @fdt_addr: On entry, address of start of FDT. On exit, address of relocated |
| 156 | * FDT start |
| 157 | * Return: status code |
Simon Glass | 9dff490 | 2018-08-08 03:54:30 -0600 | [diff] [blame] | 158 | */ |
Heinrich Schuchardt | 4574d1b | 2018-11-12 18:55:22 +0100 | [diff] [blame] | 159 | static efi_status_t copy_fdt(ulong *fdt_addrp) |
Alexander Graf | 0d9d501 | 2016-04-11 16:55:26 +0200 | [diff] [blame] | 160 | { |
Alexander Graf | ad0c1a3 | 2016-04-11 23:51:01 +0200 | [diff] [blame] | 161 | unsigned long fdt_ram_start = -1L, fdt_pages; |
Simon Glass | 9dff490 | 2018-08-08 03:54:30 -0600 | [diff] [blame] | 162 | efi_status_t ret = 0; |
| 163 | void *fdt, *new_fdt; |
Alexander Graf | ad0c1a3 | 2016-04-11 23:51:01 +0200 | [diff] [blame] | 164 | u64 new_fdt_addr; |
Simon Glass | 9dff490 | 2018-08-08 03:54:30 -0600 | [diff] [blame] | 165 | uint fdt_size; |
Alexander Graf | ad0c1a3 | 2016-04-11 23:51:01 +0200 | [diff] [blame] | 166 | int i; |
Alexander Graf | 0d9d501 | 2016-04-11 16:55:26 +0200 | [diff] [blame] | 167 | |
Simon Glass | 9dff490 | 2018-08-08 03:54:30 -0600 | [diff] [blame] | 168 | for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) { |
| 169 | u64 ram_start = gd->bd->bi_dram[i].start; |
| 170 | u64 ram_size = gd->bd->bi_dram[i].size; |
Alexander Graf | 0d9d501 | 2016-04-11 16:55:26 +0200 | [diff] [blame] | 171 | |
Alexander Graf | ad0c1a3 | 2016-04-11 23:51:01 +0200 | [diff] [blame] | 172 | if (!ram_size) |
| 173 | continue; |
| 174 | |
| 175 | if (ram_start < fdt_ram_start) |
| 176 | fdt_ram_start = ram_start; |
| 177 | } |
| 178 | |
Simon Glass | bc9a638 | 2018-06-18 08:08:25 -0600 | [diff] [blame] | 179 | /* |
| 180 | * Give us at least 4KB of breathing room in case the device tree needs |
| 181 | * to be expanded later. Round up to the nearest EFI page boundary. |
| 182 | */ |
Simon Glass | 9dff490 | 2018-08-08 03:54:30 -0600 | [diff] [blame] | 183 | fdt = map_sysmem(*fdt_addrp, 0); |
| 184 | fdt_size = fdt_totalsize(fdt); |
| 185 | fdt_size += 4096 * 3; |
Simon Glass | bc9a638 | 2018-06-18 08:08:25 -0600 | [diff] [blame] | 186 | fdt_size = ALIGN(fdt_size + EFI_PAGE_SIZE - 1, EFI_PAGE_SIZE); |
Alexander Graf | ad0c1a3 | 2016-04-11 23:51:01 +0200 | [diff] [blame] | 187 | fdt_pages = fdt_size >> EFI_PAGE_SHIFT; |
| 188 | |
Simon Glass | baf70c0 | 2018-08-08 03:54:29 -0600 | [diff] [blame] | 189 | /* Safe fdt location is at 127MB */ |
| 190 | new_fdt_addr = fdt_ram_start + (127 * 1024 * 1024) + fdt_size; |
Simon Glass | 9dff490 | 2018-08-08 03:54:30 -0600 | [diff] [blame] | 191 | ret = efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS, |
| 192 | EFI_RUNTIME_SERVICES_DATA, fdt_pages, |
| 193 | &new_fdt_addr); |
| 194 | if (ret != EFI_SUCCESS) { |
Alexander Graf | ad0c1a3 | 2016-04-11 23:51:01 +0200 | [diff] [blame] | 195 | /* If we can't put it there, put it somewhere */ |
xypron.glpk@gmx.de | a44bffc | 2017-08-11 21:19:25 +0200 | [diff] [blame] | 196 | new_fdt_addr = (ulong)memalign(EFI_PAGE_SIZE, fdt_size); |
Simon Glass | 9dff490 | 2018-08-08 03:54:30 -0600 | [diff] [blame] | 197 | ret = efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS, |
| 198 | EFI_RUNTIME_SERVICES_DATA, fdt_pages, |
| 199 | &new_fdt_addr); |
| 200 | if (ret != EFI_SUCCESS) { |
Alexander Graf | 85a6e9b | 2017-07-03 13:32:35 +0200 | [diff] [blame] | 201 | printf("ERROR: Failed to reserve space for FDT\n"); |
Simon Glass | 9dff490 | 2018-08-08 03:54:30 -0600 | [diff] [blame] | 202 | goto done; |
Alexander Graf | 85a6e9b | 2017-07-03 13:32:35 +0200 | [diff] [blame] | 203 | } |
Alexander Graf | ad0c1a3 | 2016-04-11 23:51:01 +0200 | [diff] [blame] | 204 | } |
Alexander Graf | 85a6e9b | 2017-07-03 13:32:35 +0200 | [diff] [blame] | 205 | |
Simon Glass | 9dff490 | 2018-08-08 03:54:30 -0600 | [diff] [blame] | 206 | new_fdt = map_sysmem(new_fdt_addr, fdt_size); |
Alexander Graf | 0d9d501 | 2016-04-11 16:55:26 +0200 | [diff] [blame] | 207 | memcpy(new_fdt, fdt, fdt_totalsize(fdt)); |
| 208 | fdt_set_totalsize(new_fdt, fdt_size); |
| 209 | |
Simon Glass | 9dff490 | 2018-08-08 03:54:30 -0600 | [diff] [blame] | 210 | *fdt_addrp = new_fdt_addr; |
Simon Glass | 9dff490 | 2018-08-08 03:54:30 -0600 | [diff] [blame] | 211 | done: |
| 212 | return ret; |
Alexander Graf | 0d9d501 | 2016-04-11 16:55:26 +0200 | [diff] [blame] | 213 | } |
| 214 | |
Heinrich Schuchardt | 3eb0841 | 2017-10-18 18:13:08 +0200 | [diff] [blame] | 215 | static efi_status_t efi_do_enter( |
Heinrich Schuchardt | 2074f70 | 2018-01-11 08:16:09 +0100 | [diff] [blame] | 216 | efi_handle_t image_handle, struct efi_system_table *st, |
Alexander Graf | c6fa5df | 2018-01-24 00:18:08 +0100 | [diff] [blame] | 217 | EFIAPI efi_status_t (*entry)( |
| 218 | efi_handle_t image_handle, |
| 219 | struct efi_system_table *st)) |
xypron.glpk@gmx.de | b06d8ac | 2017-07-04 23:15:21 +0200 | [diff] [blame] | 220 | { |
| 221 | efi_status_t ret = EFI_LOAD_ERROR; |
| 222 | |
| 223 | if (entry) |
| 224 | ret = entry(image_handle, st); |
| 225 | st->boottime->exit(image_handle, ret, 0, NULL); |
| 226 | return ret; |
| 227 | } |
| 228 | |
Alison Wang | ec6617c | 2016-11-10 10:49:03 +0800 | [diff] [blame] | 229 | #ifdef CONFIG_ARM64 |
Alexander Graf | c6fa5df | 2018-01-24 00:18:08 +0100 | [diff] [blame] | 230 | static efi_status_t efi_run_in_el2(EFIAPI efi_status_t (*entry)( |
Heinrich Schuchardt | 2074f70 | 2018-01-11 08:16:09 +0100 | [diff] [blame] | 231 | efi_handle_t image_handle, struct efi_system_table *st), |
| 232 | efi_handle_t image_handle, struct efi_system_table *st) |
Alison Wang | ec6617c | 2016-11-10 10:49:03 +0800 | [diff] [blame] | 233 | { |
| 234 | /* Enable caches again */ |
| 235 | dcache_enable(); |
| 236 | |
xypron.glpk@gmx.de | b06d8ac | 2017-07-04 23:15:21 +0200 | [diff] [blame] | 237 | return efi_do_enter(image_handle, st, entry); |
Alison Wang | ec6617c | 2016-11-10 10:49:03 +0800 | [diff] [blame] | 238 | } |
| 239 | #endif |
| 240 | |
Mark Kettenis | dc500c3 | 2018-06-15 23:47:12 +0200 | [diff] [blame] | 241 | #ifdef CONFIG_ARMV7_NONSEC |
Mark Kettenis | f17f200 | 2018-06-15 23:47:13 +0200 | [diff] [blame] | 242 | static bool is_nonsec; |
| 243 | |
Mark Kettenis | dc500c3 | 2018-06-15 23:47:12 +0200 | [diff] [blame] | 244 | static efi_status_t efi_run_in_hyp(EFIAPI efi_status_t (*entry)( |
| 245 | efi_handle_t image_handle, struct efi_system_table *st), |
| 246 | efi_handle_t image_handle, struct efi_system_table *st) |
| 247 | { |
| 248 | /* Enable caches again */ |
| 249 | dcache_enable(); |
| 250 | |
Mark Kettenis | f17f200 | 2018-06-15 23:47:13 +0200 | [diff] [blame] | 251 | is_nonsec = true; |
| 252 | |
Mark Kettenis | dc500c3 | 2018-06-15 23:47:12 +0200 | [diff] [blame] | 253 | return efi_do_enter(image_handle, st, entry); |
| 254 | } |
| 255 | #endif |
| 256 | |
Simon Glass | 416e07e | 2018-06-18 08:08:28 -0600 | [diff] [blame] | 257 | /* |
| 258 | * efi_carve_out_dt_rsv() - Carve out DT reserved memory ranges |
| 259 | * |
| 260 | * The mem_rsv entries of the FDT are added to the memory map. Any failures are |
| 261 | * ignored because this is not critical and we would rather continue to try to |
| 262 | * boot. |
| 263 | * |
| 264 | * @fdt: Pointer to device tree |
| 265 | */ |
| 266 | static void efi_carve_out_dt_rsv(void *fdt) |
Alexander Graf | 806d2fa | 2018-04-06 09:40:51 +0200 | [diff] [blame] | 267 | { |
| 268 | int nr_rsv, i; |
| 269 | uint64_t addr, size, pages; |
| 270 | |
| 271 | nr_rsv = fdt_num_mem_rsv(fdt); |
| 272 | |
| 273 | /* Look for an existing entry and add it to the efi mem map. */ |
| 274 | for (i = 0; i < nr_rsv; i++) { |
| 275 | if (fdt_get_mem_rsv(fdt, i, &addr, &size) != 0) |
| 276 | continue; |
| 277 | |
Heinrich Schuchardt | 23fd84b | 2018-11-12 18:55:23 +0100 | [diff] [blame] | 278 | /* |
| 279 | * Do not carve out the device tree. It is already marked as |
| 280 | * EFI_RUNTIME_SERVICES_DATA |
| 281 | */ |
| 282 | if (addr == (uintptr_t)fdt) |
| 283 | continue; |
| 284 | |
| 285 | pages = ALIGN(size + (addr & EFI_PAGE_MASK), EFI_PAGE_SIZE) >> |
| 286 | EFI_PAGE_SHIFT; |
| 287 | addr &= ~EFI_PAGE_MASK; |
Simon Glass | 416e07e | 2018-06-18 08:08:28 -0600 | [diff] [blame] | 288 | if (!efi_add_memory_map(addr, pages, EFI_RESERVED_MEMORY_TYPE, |
| 289 | false)) |
| 290 | printf("FDT memrsv map %d: Failed to add to map\n", i); |
Alexander Graf | 806d2fa | 2018-04-06 09:40:51 +0200 | [diff] [blame] | 291 | } |
Alexander Graf | 806d2fa | 2018-04-06 09:40:51 +0200 | [diff] [blame] | 292 | } |
| 293 | |
Simon Glass | 9dff490 | 2018-08-08 03:54:30 -0600 | [diff] [blame] | 294 | static efi_status_t efi_install_fdt(ulong fdt_addr) |
Heinrich Schuchardt | bc4f913 | 2018-03-03 15:29:03 +0100 | [diff] [blame] | 295 | { |
| 296 | bootm_headers_t img = { 0 }; |
Heinrich Schuchardt | bc4f913 | 2018-03-03 15:29:03 +0100 | [diff] [blame] | 297 | efi_status_t ret; |
Simon Glass | 9dff490 | 2018-08-08 03:54:30 -0600 | [diff] [blame] | 298 | void *fdt; |
Heinrich Schuchardt | bc4f913 | 2018-03-03 15:29:03 +0100 | [diff] [blame] | 299 | |
Simon Glass | 9dff490 | 2018-08-08 03:54:30 -0600 | [diff] [blame] | 300 | fdt = map_sysmem(fdt_addr, 0); |
Heinrich Schuchardt | bc4f913 | 2018-03-03 15:29:03 +0100 | [diff] [blame] | 301 | if (fdt_check_header(fdt)) { |
| 302 | printf("ERROR: invalid device tree\n"); |
| 303 | return EFI_INVALID_PARAMETER; |
| 304 | } |
| 305 | |
| 306 | /* Prepare fdt for payload */ |
Heinrich Schuchardt | 4574d1b | 2018-11-12 18:55:22 +0100 | [diff] [blame] | 307 | ret = copy_fdt(&fdt_addr); |
Simon Glass | 9dff490 | 2018-08-08 03:54:30 -0600 | [diff] [blame] | 308 | if (ret) |
| 309 | return ret; |
Heinrich Schuchardt | bc4f913 | 2018-03-03 15:29:03 +0100 | [diff] [blame] | 310 | |
Simon Glass | 9dff490 | 2018-08-08 03:54:30 -0600 | [diff] [blame] | 311 | unmap_sysmem(fdt); |
| 312 | fdt = map_sysmem(fdt_addr, 0); |
Heinrich Schuchardt | bc4f913 | 2018-03-03 15:29:03 +0100 | [diff] [blame] | 313 | if (image_setup_libfdt(&img, fdt, 0, NULL)) { |
| 314 | printf("ERROR: failed to process device tree\n"); |
| 315 | return EFI_LOAD_ERROR; |
| 316 | } |
| 317 | |
Simon Glass | 416e07e | 2018-06-18 08:08:28 -0600 | [diff] [blame] | 318 | efi_carve_out_dt_rsv(fdt); |
Alexander Graf | 806d2fa | 2018-04-06 09:40:51 +0200 | [diff] [blame] | 319 | |
Heinrich Schuchardt | bc4f913 | 2018-03-03 15:29:03 +0100 | [diff] [blame] | 320 | /* Link to it in the efi tables */ |
| 321 | ret = efi_install_configuration_table(&efi_guid_fdt, fdt); |
| 322 | if (ret != EFI_SUCCESS) |
| 323 | return EFI_OUT_OF_RESOURCES; |
| 324 | |
Heinrich Schuchardt | bc4f913 | 2018-03-03 15:29:03 +0100 | [diff] [blame] | 325 | return ret; |
| 326 | } |
| 327 | |
Heinrich Schuchardt | c982874 | 2018-09-23 17:21:51 +0200 | [diff] [blame] | 328 | /** |
| 329 | * do_bootefi_exec() - execute EFI binary |
| 330 | * |
| 331 | * @efi: address of the binary |
| 332 | * @device_path: path of the device from which the binary was loaded |
| 333 | * @image_path: device path of the binary |
| 334 | * Return: status code |
| 335 | * |
| 336 | * Load the EFI binary into a newly assigned memory unwinding the relocation |
| 337 | * information, install the loaded image protocol, and call the binary. |
Alexander Graf | b993933 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 338 | */ |
Heinrich Schuchardt | bc4f913 | 2018-03-03 15:29:03 +0100 | [diff] [blame] | 339 | static efi_status_t do_bootefi_exec(void *efi, |
Heinrich Schuchardt | 3eb0841 | 2017-10-18 18:13:08 +0200 | [diff] [blame] | 340 | struct efi_device_path *device_path, |
| 341 | struct efi_device_path *image_path) |
Alexander Graf | b993933 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 342 | { |
Heinrich Schuchardt | 8887acc | 2018-09-16 07:19:48 +0200 | [diff] [blame] | 343 | efi_handle_t mem_handle = NULL; |
Rob Clark | bf19273 | 2017-10-10 08:23:06 -0400 | [diff] [blame] | 344 | struct efi_device_path *memdp = NULL; |
Heinrich Schuchardt | 45204b1 | 2018-03-03 15:29:01 +0100 | [diff] [blame] | 345 | efi_status_t ret; |
Heinrich Schuchardt | faea104 | 2018-09-26 05:27:54 +0200 | [diff] [blame] | 346 | struct efi_loaded_image_obj *image_obj = NULL; |
Heinrich Schuchardt | c982874 | 2018-09-23 17:21:51 +0200 | [diff] [blame] | 347 | struct efi_loaded_image *loaded_image_info = NULL; |
Rob Clark | 95c5553 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 348 | |
Alexander Graf | c6fa5df | 2018-01-24 00:18:08 +0100 | [diff] [blame] | 349 | EFIAPI efi_status_t (*entry)(efi_handle_t image_handle, |
| 350 | struct efi_system_table *st); |
Alexander Graf | b993933 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 351 | |
Rob Clark | bf19273 | 2017-10-10 08:23:06 -0400 | [diff] [blame] | 352 | /* |
| 353 | * Special case for efi payload not loaded from disk, such as |
| 354 | * 'bootefi hello' or for example payload loaded directly into |
Heinrich Schuchardt | e1fec15 | 2018-10-18 21:51:38 +0200 | [diff] [blame] | 355 | * memory via JTAG, etc: |
Rob Clark | bf19273 | 2017-10-10 08:23:06 -0400 | [diff] [blame] | 356 | */ |
| 357 | if (!device_path && !image_path) { |
| 358 | printf("WARNING: using memory device/image path, this may confuse some payloads!\n"); |
| 359 | /* actual addresses filled in after efi_load_pe() */ |
| 360 | memdp = efi_dp_from_mem(0, 0, 0); |
| 361 | device_path = image_path = memdp; |
Heinrich Schuchardt | 8887acc | 2018-09-16 07:19:48 +0200 | [diff] [blame] | 362 | /* |
| 363 | * Grub expects that the device path of the loaded image is |
| 364 | * installed on a handle. |
| 365 | */ |
| 366 | ret = efi_create_handle(&mem_handle); |
| 367 | if (ret != EFI_SUCCESS) |
| 368 | goto exit; |
| 369 | ret = efi_add_protocol(mem_handle, &efi_guid_device_path, |
Alexander Graf | 58bc69d | 2018-06-12 10:21:16 +0200 | [diff] [blame] | 370 | device_path); |
| 371 | if (ret != EFI_SUCCESS) |
| 372 | goto exit; |
Rob Clark | bf19273 | 2017-10-10 08:23:06 -0400 | [diff] [blame] | 373 | } else { |
| 374 | assert(device_path && image_path); |
| 375 | } |
| 376 | |
Heinrich Schuchardt | faea104 | 2018-09-26 05:27:54 +0200 | [diff] [blame] | 377 | ret = efi_setup_loaded_image(device_path, image_path, &image_obj, |
Heinrich Schuchardt | c982874 | 2018-09-23 17:21:51 +0200 | [diff] [blame] | 378 | &loaded_image_info); |
| 379 | if (ret != EFI_SUCCESS) |
| 380 | goto exit; |
Rob Clark | 95c5553 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 381 | |
Heinrich Schuchardt | b57f48a | 2017-10-18 18:13:15 +0200 | [diff] [blame] | 382 | /* Transfer environment variable bootargs as load options */ |
Heinrich Schuchardt | c982874 | 2018-09-23 17:21:51 +0200 | [diff] [blame] | 383 | set_load_options(loaded_image_info, "bootargs"); |
Alexander Graf | b993933 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 384 | /* Load the EFI payload */ |
Heinrich Schuchardt | faea104 | 2018-09-26 05:27:54 +0200 | [diff] [blame] | 385 | entry = efi_load_pe(image_obj, efi, loaded_image_info); |
Rob Clark | 95c5553 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 386 | if (!entry) { |
Heinrich Schuchardt | 45204b1 | 2018-03-03 15:29:01 +0100 | [diff] [blame] | 387 | ret = EFI_LOAD_ERROR; |
Rob Clark | 95c5553 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 388 | goto exit; |
| 389 | } |
Alexander Graf | 80a4800 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 390 | |
Rob Clark | bf19273 | 2017-10-10 08:23:06 -0400 | [diff] [blame] | 391 | if (memdp) { |
| 392 | struct efi_device_path_memory *mdp = (void *)memdp; |
Heinrich Schuchardt | c982874 | 2018-09-23 17:21:51 +0200 | [diff] [blame] | 393 | mdp->memory_type = loaded_image_info->image_code_type; |
| 394 | mdp->start_address = (uintptr_t)loaded_image_info->image_base; |
Rob Clark | bf19273 | 2017-10-10 08:23:06 -0400 | [diff] [blame] | 395 | mdp->end_address = mdp->start_address + |
Heinrich Schuchardt | c982874 | 2018-09-23 17:21:51 +0200 | [diff] [blame] | 396 | loaded_image_info->image_size; |
Rob Clark | bf19273 | 2017-10-10 08:23:06 -0400 | [diff] [blame] | 397 | } |
| 398 | |
Rob Clark | ad644e7 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 399 | /* we don't support much: */ |
| 400 | env_set("efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_OsIndicationsSupported", |
| 401 | "{ro,boot}(blob)0000000000000000"); |
| 402 | |
Alexander Graf | b993933 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 403 | /* Call our payload! */ |
Alexander Graf | edcef3b | 2016-06-02 11:38:27 +0200 | [diff] [blame] | 404 | debug("%s:%d Jumping to 0x%lx\n", __func__, __LINE__, (long)entry); |
Alexander Graf | a86aeaf | 2016-05-20 23:28:23 +0200 | [diff] [blame] | 405 | |
Heinrich Schuchardt | faea104 | 2018-09-26 05:27:54 +0200 | [diff] [blame] | 406 | if (setjmp(&image_obj->exit_jmp)) { |
| 407 | ret = image_obj->exit_status; |
Rob Clark | 95c5553 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 408 | goto exit; |
Alexander Graf | a86aeaf | 2016-05-20 23:28:23 +0200 | [diff] [blame] | 409 | } |
| 410 | |
Alexander Graf | 69bd459 | 2016-11-17 01:02:58 +0100 | [diff] [blame] | 411 | #ifdef CONFIG_ARM64 |
| 412 | /* On AArch64 we need to make sure we call our payload in < EL3 */ |
| 413 | if (current_el() == 3) { |
| 414 | smp_kick_all_cpus(); |
| 415 | dcache_disable(); /* flush cache before switch to EL2 */ |
Alison Wang | ec6617c | 2016-11-10 10:49:03 +0800 | [diff] [blame] | 416 | |
| 417 | /* Move into EL2 and keep running there */ |
Heinrich Schuchardt | ea54ad5 | 2017-11-26 14:05:22 +0100 | [diff] [blame] | 418 | armv8_switch_to_el2((ulong)entry, |
Heinrich Schuchardt | d39646a | 2018-09-26 05:27:56 +0200 | [diff] [blame] | 419 | (ulong)&image_obj->header, |
Alison Wang | 7c5e1fe | 2017-01-17 09:39:17 +0800 | [diff] [blame] | 420 | (ulong)&systab, 0, (ulong)efi_run_in_el2, |
Alison Wang | ec6617c | 2016-11-10 10:49:03 +0800 | [diff] [blame] | 421 | ES_TO_AARCH64); |
| 422 | |
| 423 | /* Should never reach here, efi exits with longjmp */ |
| 424 | while (1) { } |
Alexander Graf | 69bd459 | 2016-11-17 01:02:58 +0100 | [diff] [blame] | 425 | } |
| 426 | #endif |
| 427 | |
Mark Kettenis | dc500c3 | 2018-06-15 23:47:12 +0200 | [diff] [blame] | 428 | #ifdef CONFIG_ARMV7_NONSEC |
Mark Kettenis | f17f200 | 2018-06-15 23:47:13 +0200 | [diff] [blame] | 429 | if (armv7_boot_nonsec() && !is_nonsec) { |
Mark Kettenis | dc500c3 | 2018-06-15 23:47:12 +0200 | [diff] [blame] | 430 | dcache_disable(); /* flush cache before switch to HYP */ |
| 431 | |
| 432 | armv7_init_nonsec(); |
| 433 | secure_ram_addr(_do_nonsec_entry)( |
| 434 | efi_run_in_hyp, |
| 435 | (uintptr_t)entry, |
Heinrich Schuchardt | d39646a | 2018-09-26 05:27:56 +0200 | [diff] [blame] | 436 | (uintptr_t)&image_obj->header, |
Mark Kettenis | dc500c3 | 2018-06-15 23:47:12 +0200 | [diff] [blame] | 437 | (uintptr_t)&systab); |
| 438 | |
| 439 | /* Should never reach here, efi exits with longjmp */ |
| 440 | while (1) { } |
| 441 | } |
| 442 | #endif |
| 443 | |
Heinrich Schuchardt | d39646a | 2018-09-26 05:27:56 +0200 | [diff] [blame] | 444 | ret = efi_do_enter(&image_obj->header, &systab, entry); |
Rob Clark | 95c5553 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 445 | |
| 446 | exit: |
| 447 | /* image has returned, loaded-image obj goes *poof*: */ |
Heinrich Schuchardt | faea104 | 2018-09-26 05:27:54 +0200 | [diff] [blame] | 448 | if (image_obj) |
Heinrich Schuchardt | d39646a | 2018-09-26 05:27:56 +0200 | [diff] [blame] | 449 | efi_delete_handle(&image_obj->header); |
Heinrich Schuchardt | 8887acc | 2018-09-16 07:19:48 +0200 | [diff] [blame] | 450 | if (mem_handle) |
| 451 | efi_delete_handle(mem_handle); |
Rob Clark | 95c5553 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 452 | |
| 453 | return ret; |
Alexander Graf | b993933 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 454 | } |
| 455 | |
Heinrich Schuchardt | bc4f913 | 2018-03-03 15:29:03 +0100 | [diff] [blame] | 456 | static int do_bootefi_bootmgr_exec(void) |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 457 | { |
| 458 | struct efi_device_path *device_path, *file_path; |
| 459 | void *addr; |
| 460 | efi_status_t r; |
| 461 | |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 462 | addr = efi_bootmgr_load(&device_path, &file_path); |
| 463 | if (!addr) |
| 464 | return 1; |
| 465 | |
| 466 | printf("## Starting EFI application at %p ...\n", addr); |
Heinrich Schuchardt | bc4f913 | 2018-03-03 15:29:03 +0100 | [diff] [blame] | 467 | r = do_bootefi_exec(addr, device_path, file_path); |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 468 | printf("## Application terminated, r = %lu\n", |
| 469 | r & ~EFI_ERROR_MASK); |
| 470 | |
| 471 | if (r != EFI_SUCCESS) |
| 472 | return 1; |
| 473 | |
| 474 | return 0; |
| 475 | } |
| 476 | |
Alexander Graf | b993933 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 477 | /* Interpreter command to boot an arbitrary EFI image from memory */ |
| 478 | static int do_bootefi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 479 | { |
Heinrich Schuchardt | bc4f913 | 2018-03-03 15:29:03 +0100 | [diff] [blame] | 480 | unsigned long addr; |
| 481 | char *saddr; |
Heinrich Schuchardt | 3eb0841 | 2017-10-18 18:13:08 +0200 | [diff] [blame] | 482 | efi_status_t r; |
Alexander Graf | 354264b | 2018-06-18 17:22:58 +0200 | [diff] [blame] | 483 | unsigned long fdt_addr; |
Alexander Graf | b993933 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 484 | |
Heinrich Schuchardt | c3b11de | 2018-04-03 21:59:32 +0200 | [diff] [blame] | 485 | /* Allow unaligned memory access */ |
| 486 | allow_unaligned(); |
| 487 | |
Heinrich Schuchardt | fc225e6 | 2018-03-03 15:29:02 +0100 | [diff] [blame] | 488 | /* Initialize EFI drivers */ |
| 489 | r = efi_init_obj_list(); |
| 490 | if (r != EFI_SUCCESS) { |
| 491 | printf("Error: Cannot set up EFI drivers, r = %lu\n", |
| 492 | r & ~EFI_ERROR_MASK); |
| 493 | return CMD_RET_FAILURE; |
| 494 | } |
| 495 | |
Alexander Graf | b993933 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 496 | if (argc < 2) |
Bin Meng | 3c1dcef | 2016-08-13 04:02:06 -0700 | [diff] [blame] | 497 | return CMD_RET_USAGE; |
Heinrich Schuchardt | bc4f913 | 2018-03-03 15:29:03 +0100 | [diff] [blame] | 498 | |
| 499 | if (argc > 2) { |
Alexander Graf | 354264b | 2018-06-18 17:22:58 +0200 | [diff] [blame] | 500 | fdt_addr = simple_strtoul(argv[2], NULL, 16); |
Heinrich Schuchardt | bc4f913 | 2018-03-03 15:29:03 +0100 | [diff] [blame] | 501 | if (!fdt_addr && *argv[2] != '0') |
| 502 | return CMD_RET_USAGE; |
| 503 | /* Install device tree */ |
Simon Glass | 9dff490 | 2018-08-08 03:54:30 -0600 | [diff] [blame] | 504 | r = efi_install_fdt(fdt_addr); |
Heinrich Schuchardt | bc4f913 | 2018-03-03 15:29:03 +0100 | [diff] [blame] | 505 | if (r != EFI_SUCCESS) { |
| 506 | printf("ERROR: failed to install device tree\n"); |
| 507 | return CMD_RET_FAILURE; |
| 508 | } |
| 509 | } else { |
| 510 | /* Remove device tree. EFI_NOT_FOUND can be ignored here */ |
| 511 | efi_install_configuration_table(&efi_guid_fdt, NULL); |
| 512 | printf("WARNING: booting without device tree\n"); |
| 513 | } |
Simon Glass | c7ae3df | 2016-11-07 08:47:08 -0700 | [diff] [blame] | 514 | #ifdef CONFIG_CMD_BOOTEFI_HELLO |
| 515 | if (!strcmp(argv[1], "hello")) { |
Heinrich Schuchardt | 5e44489 | 2017-09-05 03:19:37 +0200 | [diff] [blame] | 516 | ulong size = __efi_helloworld_end - __efi_helloworld_begin; |
Alexander Graf | b993933 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 517 | |
Heinrich Schuchardt | 51c533f | 2017-08-28 18:54:30 +0200 | [diff] [blame] | 518 | saddr = env_get("loadaddr"); |
| 519 | if (saddr) |
| 520 | addr = simple_strtoul(saddr, NULL, 16); |
| 521 | else |
| 522 | addr = CONFIG_SYS_LOAD_ADDR; |
Alexander Graf | 354264b | 2018-06-18 17:22:58 +0200 | [diff] [blame] | 523 | memcpy(map_sysmem(addr, size), __efi_helloworld_begin, size); |
Simon Glass | c7ae3df | 2016-11-07 08:47:08 -0700 | [diff] [blame] | 524 | } else |
| 525 | #endif |
Heinrich Schuchardt | 623b3a5 | 2017-09-15 10:06:11 +0200 | [diff] [blame] | 526 | #ifdef CONFIG_CMD_BOOTEFI_SELFTEST |
| 527 | if (!strcmp(argv[1], "selftest")) { |
Heinrich Schuchardt | faea104 | 2018-09-26 05:27:54 +0200 | [diff] [blame] | 528 | struct efi_loaded_image_obj *image_obj; |
Heinrich Schuchardt | c982874 | 2018-09-23 17:21:51 +0200 | [diff] [blame] | 529 | struct efi_loaded_image *loaded_image_info; |
Heinrich Schuchardt | 7aca68c | 2017-09-20 22:54:58 +0200 | [diff] [blame] | 530 | |
Heinrich Schuchardt | f972dc1 | 2017-10-18 18:13:09 +0200 | [diff] [blame] | 531 | /* Construct a dummy device path. */ |
| 532 | bootefi_device_path = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE, |
| 533 | (uintptr_t)&efi_selftest, |
| 534 | (uintptr_t)&efi_selftest); |
Simon Glass | 2ab7ef7 | 2018-11-25 20:14:36 -0700 | [diff] [blame^] | 535 | if (!bootefi_device_path) |
| 536 | return CMD_RET_FAILURE; |
| 537 | |
Heinrich Schuchardt | f972dc1 | 2017-10-18 18:13:09 +0200 | [diff] [blame] | 538 | bootefi_image_path = efi_dp_from_file(NULL, 0, "\\selftest"); |
Simon Glass | 2ab7ef7 | 2018-11-25 20:14:36 -0700 | [diff] [blame^] | 539 | if (!bootefi_image_path) |
| 540 | return CMD_RET_FAILURE; |
Heinrich Schuchardt | f972dc1 | 2017-10-18 18:13:09 +0200 | [diff] [blame] | 541 | |
Heinrich Schuchardt | c982874 | 2018-09-23 17:21:51 +0200 | [diff] [blame] | 542 | r = efi_setup_loaded_image(bootefi_device_path, |
Heinrich Schuchardt | faea104 | 2018-09-26 05:27:54 +0200 | [diff] [blame] | 543 | bootefi_image_path, &image_obj, |
Heinrich Schuchardt | c982874 | 2018-09-23 17:21:51 +0200 | [diff] [blame] | 544 | &loaded_image_info); |
| 545 | if (r != EFI_SUCCESS) |
| 546 | return CMD_RET_FAILURE; |
| 547 | |
Heinrich Schuchardt | 623b3a5 | 2017-09-15 10:06:11 +0200 | [diff] [blame] | 548 | efi_save_gd(); |
Heinrich Schuchardt | d78e40d | 2017-10-18 18:13:13 +0200 | [diff] [blame] | 549 | /* Transfer environment variable efi_selftest as load options */ |
Heinrich Schuchardt | c982874 | 2018-09-23 17:21:51 +0200 | [diff] [blame] | 550 | set_load_options(loaded_image_info, "efi_selftest"); |
Heinrich Schuchardt | d78e40d | 2017-10-18 18:13:13 +0200 | [diff] [blame] | 551 | /* Execute the test */ |
Heinrich Schuchardt | d39646a | 2018-09-26 05:27:56 +0200 | [diff] [blame] | 552 | r = efi_selftest(&image_obj->header, &systab); |
Heinrich Schuchardt | c2b5390 | 2017-10-18 18:13:14 +0200 | [diff] [blame] | 553 | efi_restore_gd(); |
Heinrich Schuchardt | c982874 | 2018-09-23 17:21:51 +0200 | [diff] [blame] | 554 | free(loaded_image_info->load_options); |
Heinrich Schuchardt | d39646a | 2018-09-26 05:27:56 +0200 | [diff] [blame] | 555 | efi_delete_handle(&image_obj->header); |
Heinrich Schuchardt | c2b5390 | 2017-10-18 18:13:14 +0200 | [diff] [blame] | 556 | return r != EFI_SUCCESS; |
Heinrich Schuchardt | 623b3a5 | 2017-09-15 10:06:11 +0200 | [diff] [blame] | 557 | } else |
| 558 | #endif |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 559 | if (!strcmp(argv[1], "bootmgr")) { |
Heinrich Schuchardt | bc4f913 | 2018-03-03 15:29:03 +0100 | [diff] [blame] | 560 | return do_bootefi_bootmgr_exec(); |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 561 | } else { |
Simon Glass | c7ae3df | 2016-11-07 08:47:08 -0700 | [diff] [blame] | 562 | saddr = argv[1]; |
Alexander Graf | b993933 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 563 | |
Simon Glass | c7ae3df | 2016-11-07 08:47:08 -0700 | [diff] [blame] | 564 | addr = simple_strtoul(saddr, NULL, 16); |
Heinrich Schuchardt | 49db1cb | 2018-01-24 20:33:54 +0100 | [diff] [blame] | 565 | /* Check that a numeric value was passed */ |
| 566 | if (!addr && *saddr != '0') |
| 567 | return CMD_RET_USAGE; |
Simon Glass | c7ae3df | 2016-11-07 08:47:08 -0700 | [diff] [blame] | 568 | |
Alexander Graf | 1c39809 | 2016-04-14 16:07:53 +0200 | [diff] [blame] | 569 | } |
| 570 | |
Simon Glass | 5ee31ba | 2016-11-07 08:47:05 -0700 | [diff] [blame] | 571 | printf("## Starting EFI application at %08lx ...\n", addr); |
Alexander Graf | 354264b | 2018-06-18 17:22:58 +0200 | [diff] [blame] | 572 | r = do_bootefi_exec(map_sysmem(addr, 0), bootefi_device_path, |
Heinrich Schuchardt | bc4f913 | 2018-03-03 15:29:03 +0100 | [diff] [blame] | 573 | bootefi_image_path); |
xypron.glpk@gmx.de | 1da1bac | 2017-07-04 23:15:23 +0200 | [diff] [blame] | 574 | printf("## Application terminated, r = %lu\n", |
| 575 | r & ~EFI_ERROR_MASK); |
Alexander Graf | b993933 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 576 | |
xypron.glpk@gmx.de | 1da1bac | 2017-07-04 23:15:23 +0200 | [diff] [blame] | 577 | if (r != EFI_SUCCESS) |
| 578 | return 1; |
| 579 | else |
| 580 | return 0; |
Alexander Graf | b993933 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | #ifdef CONFIG_SYS_LONGHELP |
| 584 | static char bootefi_help_text[] = |
Alexander Graf | 1c39809 | 2016-04-14 16:07:53 +0200 | [diff] [blame] | 585 | "<image address> [fdt address]\n" |
| 586 | " - boot EFI payload stored at address <image address>.\n" |
| 587 | " If specified, the device tree located at <fdt address> gets\n" |
Simon Glass | c7ae3df | 2016-11-07 08:47:08 -0700 | [diff] [blame] | 588 | " exposed as EFI configuration table.\n" |
| 589 | #ifdef CONFIG_CMD_BOOTEFI_HELLO |
Heinrich Schuchardt | 623b3a5 | 2017-09-15 10:06:11 +0200 | [diff] [blame] | 590 | "bootefi hello\n" |
| 591 | " - boot a sample Hello World application stored within U-Boot\n" |
| 592 | #endif |
| 593 | #ifdef CONFIG_CMD_BOOTEFI_SELFTEST |
Heinrich Schuchardt | bc4f913 | 2018-03-03 15:29:03 +0100 | [diff] [blame] | 594 | "bootefi selftest [fdt address]\n" |
Heinrich Schuchardt | 623b3a5 | 2017-09-15 10:06:11 +0200 | [diff] [blame] | 595 | " - boot an EFI selftest application stored within U-Boot\n" |
Heinrich Schuchardt | d78e40d | 2017-10-18 18:13:13 +0200 | [diff] [blame] | 596 | " Use environment variable efi_selftest to select a single test.\n" |
| 597 | " Use 'setenv efi_selftest list' to enumerate all tests.\n" |
Simon Glass | c7ae3df | 2016-11-07 08:47:08 -0700 | [diff] [blame] | 598 | #endif |
Heinrich Schuchardt | f623e07 | 2018-01-30 19:47:43 +0100 | [diff] [blame] | 599 | "bootefi bootmgr [fdt addr]\n" |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 600 | " - load and boot EFI payload based on BootOrder/BootXXXX variables.\n" |
| 601 | "\n" |
| 602 | " If specified, the device tree located at <fdt address> gets\n" |
| 603 | " exposed as EFI configuration table.\n"; |
Alexander Graf | b993933 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 604 | #endif |
| 605 | |
| 606 | U_BOOT_CMD( |
Alexander Graf | 1c39809 | 2016-04-14 16:07:53 +0200 | [diff] [blame] | 607 | bootefi, 3, 0, do_bootefi, |
Sergey Kubushyn | 92dfd92 | 2016-06-07 11:14:31 -0700 | [diff] [blame] | 608 | "Boots an EFI payload from memory", |
Alexander Graf | b993933 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 609 | bootefi_help_text |
| 610 | ); |
Alexander Graf | 0f4060e | 2016-03-04 01:10:14 +0100 | [diff] [blame] | 611 | |
Alexander Graf | c07ad7c | 2016-04-11 16:16:19 +0200 | [diff] [blame] | 612 | void efi_set_bootdev(const char *dev, const char *devnr, const char *path) |
Alexander Graf | 0f4060e | 2016-03-04 01:10:14 +0100 | [diff] [blame] | 613 | { |
AKASHI Takahiro | f1589ff | 2018-10-17 16:32:03 +0900 | [diff] [blame] | 614 | struct efi_device_path *device, *image; |
| 615 | efi_status_t ret; |
Alexander Graf | 0f4060e | 2016-03-04 01:10:14 +0100 | [diff] [blame] | 616 | |
Heinrich Schuchardt | 79276eb | 2018-09-16 07:20:21 +0200 | [diff] [blame] | 617 | /* efi_set_bootdev is typically called repeatedly, recover memory */ |
| 618 | efi_free_pool(bootefi_device_path); |
| 619 | efi_free_pool(bootefi_image_path); |
Heinrich Schuchardt | 79276eb | 2018-09-16 07:20:21 +0200 | [diff] [blame] | 620 | |
AKASHI Takahiro | f1589ff | 2018-10-17 16:32:03 +0900 | [diff] [blame] | 621 | ret = efi_dp_from_name(dev, devnr, path, &device, &image); |
| 622 | if (ret == EFI_SUCCESS) { |
| 623 | bootefi_device_path = device; |
| 624 | bootefi_image_path = image; |
Rob Clark | 95c5553 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 625 | } else { |
AKASHI Takahiro | f1589ff | 2018-10-17 16:32:03 +0900 | [diff] [blame] | 626 | bootefi_device_path = NULL; |
| 627 | bootefi_image_path = NULL; |
Alexander Graf | f9d334b | 2016-08-05 14:49:53 +0200 | [diff] [blame] | 628 | } |
Alexander Graf | 0f4060e | 2016-03-04 01:10:14 +0100 | [diff] [blame] | 629 | } |