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 | |
| 8 | #include <common.h> |
Heinrich Schuchardt | aa0b11b | 2019-01-08 18:13:06 +0100 | [diff] [blame] | 9 | #include <bootm.h> |
| 10 | #include <charset.h> |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 11 | #include <command.h> |
Simon Glass | 11c89f3 | 2017-05-17 17:18:03 -0600 | [diff] [blame] | 12 | #include <dm.h> |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 13 | #include <efi_loader.h> |
Heinrich Schuchardt | 02efd5d | 2017-10-18 18:13:13 +0200 | [diff] [blame] | 14 | #include <efi_selftest.h> |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 15 | #include <errno.h> |
Masahiro Yamada | 75f82d0 | 2018-03-05 01:20:11 +0900 | [diff] [blame] | 16 | #include <linux/libfdt.h> |
| 17 | #include <linux/libfdt_env.h> |
Alexander Graf | a241451 | 2018-06-18 17:22:58 +0200 | [diff] [blame] | 18 | #include <mapmem.h> |
Alexander Graf | fdbae01 | 2016-04-11 23:51:01 +0200 | [diff] [blame] | 19 | #include <memalign.h> |
Alexander Graf | 2ff5cc3 | 2016-04-11 16:55:26 +0200 | [diff] [blame] | 20 | #include <asm/global_data.h> |
Simon Glass | c2194bf | 2016-09-25 15:27:32 -0600 | [diff] [blame] | 21 | #include <asm-generic/sections.h> |
Heinrich Schuchardt | 954e2b9 | 2018-04-03 21:59:32 +0200 | [diff] [blame] | 22 | #include <asm-generic/unaligned.h> |
Simon Glass | c2194bf | 2016-09-25 15:27:32 -0600 | [diff] [blame] | 23 | #include <linux/linkage.h> |
Alexander Graf | 2ff5cc3 | 2016-04-11 16:55:26 +0200 | [diff] [blame] | 24 | |
| 25 | DECLARE_GLOBAL_DATA_PTR; |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 26 | |
Rob Clark | f8db922 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 27 | static struct efi_device_path *bootefi_image_path; |
| 28 | static struct efi_device_path *bootefi_device_path; |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 29 | |
Heinrich Schuchardt | 02efd5d | 2017-10-18 18:13:13 +0200 | [diff] [blame] | 30 | /* |
Heinrich Schuchardt | 954e2b9 | 2018-04-03 21:59:32 +0200 | [diff] [blame] | 31 | * Allow unaligned memory access. |
| 32 | * |
| 33 | * This routine is overridden by architectures providing this feature. |
| 34 | */ |
| 35 | void __weak allow_unaligned(void) |
| 36 | { |
| 37 | } |
| 38 | |
| 39 | /* |
Heinrich Schuchardt | 02efd5d | 2017-10-18 18:13:13 +0200 | [diff] [blame] | 40 | * Set the load options of an image from an environment variable. |
| 41 | * |
| 42 | * @loaded_image_info: the image |
| 43 | * @env_var: name of the environment variable |
| 44 | */ |
| 45 | static void set_load_options(struct efi_loaded_image *loaded_image_info, |
| 46 | const char *env_var) |
| 47 | { |
| 48 | size_t size; |
| 49 | const char *env = env_get(env_var); |
Heinrich Schuchardt | de52780 | 2018-08-31 21:31:33 +0200 | [diff] [blame] | 50 | u16 *pos; |
Heinrich Schuchardt | 02efd5d | 2017-10-18 18:13:13 +0200 | [diff] [blame] | 51 | |
| 52 | loaded_image_info->load_options = NULL; |
| 53 | loaded_image_info->load_options_size = 0; |
| 54 | if (!env) |
| 55 | return; |
Heinrich Schuchardt | de52780 | 2018-08-31 21:31:33 +0200 | [diff] [blame] | 56 | size = utf8_utf16_strlen(env) + 1; |
Heinrich Schuchardt | 02efd5d | 2017-10-18 18:13:13 +0200 | [diff] [blame] | 57 | loaded_image_info->load_options = calloc(size, sizeof(u16)); |
| 58 | if (!loaded_image_info->load_options) { |
| 59 | printf("ERROR: Out of memory\n"); |
| 60 | return; |
| 61 | } |
Heinrich Schuchardt | de52780 | 2018-08-31 21:31:33 +0200 | [diff] [blame] | 62 | pos = loaded_image_info->load_options; |
| 63 | utf8_utf16_strcpy(&pos, env); |
Heinrich Schuchardt | 02efd5d | 2017-10-18 18:13:13 +0200 | [diff] [blame] | 64 | loaded_image_info->load_options_size = size * 2; |
| 65 | } |
| 66 | |
Simon Glass | 5d618df | 2018-08-08 03:54:30 -0600 | [diff] [blame] | 67 | /** |
| 68 | * copy_fdt() - Copy the device tree to a new location available to EFI |
| 69 | * |
Heinrich Schuchardt | b23f9b7 | 2018-11-18 17:58:52 +0100 | [diff] [blame] | 70 | * The FDT is copied to a suitable location within the EFI memory map. |
Heinrich Schuchardt | dcdca29 | 2018-11-18 17:58:49 +0100 | [diff] [blame] | 71 | * Additional 12 KiB are added to the space in case the device tree needs to be |
Simon Glass | 5d618df | 2018-08-08 03:54:30 -0600 | [diff] [blame] | 72 | * expanded later with fdt_open_into(). |
| 73 | * |
Heinrich Schuchardt | b23f9b7 | 2018-11-18 17:58:52 +0100 | [diff] [blame] | 74 | * @fdtp: On entry a pointer to the flattened device tree. |
| 75 | * On exit a pointer to the copy of the flattened device tree. |
Heinrich Schuchardt | 96af56e | 2018-11-12 18:55:22 +0100 | [diff] [blame] | 76 | * FDT start |
| 77 | * Return: status code |
Simon Glass | 5d618df | 2018-08-08 03:54:30 -0600 | [diff] [blame] | 78 | */ |
Heinrich Schuchardt | b23f9b7 | 2018-11-18 17:58:52 +0100 | [diff] [blame] | 79 | static efi_status_t copy_fdt(void **fdtp) |
Alexander Graf | 2ff5cc3 | 2016-04-11 16:55:26 +0200 | [diff] [blame] | 80 | { |
Alexander Graf | fdbae01 | 2016-04-11 23:51:01 +0200 | [diff] [blame] | 81 | unsigned long fdt_ram_start = -1L, fdt_pages; |
Simon Glass | 5d618df | 2018-08-08 03:54:30 -0600 | [diff] [blame] | 82 | efi_status_t ret = 0; |
| 83 | void *fdt, *new_fdt; |
Alexander Graf | fdbae01 | 2016-04-11 23:51:01 +0200 | [diff] [blame] | 84 | u64 new_fdt_addr; |
Simon Glass | 5d618df | 2018-08-08 03:54:30 -0600 | [diff] [blame] | 85 | uint fdt_size; |
Alexander Graf | fdbae01 | 2016-04-11 23:51:01 +0200 | [diff] [blame] | 86 | int i; |
Alexander Graf | 2ff5cc3 | 2016-04-11 16:55:26 +0200 | [diff] [blame] | 87 | |
Simon Glass | 5d618df | 2018-08-08 03:54:30 -0600 | [diff] [blame] | 88 | for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) { |
| 89 | u64 ram_start = gd->bd->bi_dram[i].start; |
| 90 | u64 ram_size = gd->bd->bi_dram[i].size; |
Alexander Graf | 2ff5cc3 | 2016-04-11 16:55:26 +0200 | [diff] [blame] | 91 | |
Alexander Graf | fdbae01 | 2016-04-11 23:51:01 +0200 | [diff] [blame] | 92 | if (!ram_size) |
| 93 | continue; |
| 94 | |
| 95 | if (ram_start < fdt_ram_start) |
| 96 | fdt_ram_start = ram_start; |
| 97 | } |
| 98 | |
Simon Glass | 56bbcc1 | 2018-06-18 08:08:25 -0600 | [diff] [blame] | 99 | /* |
Heinrich Schuchardt | dcdca29 | 2018-11-18 17:58:49 +0100 | [diff] [blame] | 100 | * Give us at least 12 KiB of breathing room in case the device tree |
| 101 | * needs to be expanded later. |
Simon Glass | 56bbcc1 | 2018-06-18 08:08:25 -0600 | [diff] [blame] | 102 | */ |
Heinrich Schuchardt | b23f9b7 | 2018-11-18 17:58:52 +0100 | [diff] [blame] | 103 | fdt = *fdtp; |
Heinrich Schuchardt | dcdca29 | 2018-11-18 17:58:49 +0100 | [diff] [blame] | 104 | fdt_pages = efi_size_in_pages(fdt_totalsize(fdt) + 0x3000); |
| 105 | fdt_size = fdt_pages << EFI_PAGE_SHIFT; |
Alexander Graf | fdbae01 | 2016-04-11 23:51:01 +0200 | [diff] [blame] | 106 | |
Heinrich Schuchardt | b23f9b7 | 2018-11-18 17:58:52 +0100 | [diff] [blame] | 107 | /* |
| 108 | * Safe fdt location is at 127 MiB. |
| 109 | * On the sandbox convert from the sandbox address space. |
| 110 | */ |
| 111 | new_fdt_addr = (uintptr_t)map_sysmem(fdt_ram_start + 0x7f00000 + |
| 112 | fdt_size, 0); |
Simon Glass | 5d618df | 2018-08-08 03:54:30 -0600 | [diff] [blame] | 113 | ret = efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS, |
| 114 | EFI_RUNTIME_SERVICES_DATA, fdt_pages, |
| 115 | &new_fdt_addr); |
| 116 | if (ret != EFI_SUCCESS) { |
Alexander Graf | fdbae01 | 2016-04-11 23:51:01 +0200 | [diff] [blame] | 117 | /* If we can't put it there, put it somewhere */ |
xypron.glpk@gmx.de | f6b9912 | 2017-08-11 21:19:25 +0200 | [diff] [blame] | 118 | new_fdt_addr = (ulong)memalign(EFI_PAGE_SIZE, fdt_size); |
Simon Glass | 5d618df | 2018-08-08 03:54:30 -0600 | [diff] [blame] | 119 | ret = efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS, |
| 120 | EFI_RUNTIME_SERVICES_DATA, fdt_pages, |
| 121 | &new_fdt_addr); |
| 122 | if (ret != EFI_SUCCESS) { |
Alexander Graf | 9b9b716 | 2017-07-03 13:32:35 +0200 | [diff] [blame] | 123 | printf("ERROR: Failed to reserve space for FDT\n"); |
Simon Glass | 5d618df | 2018-08-08 03:54:30 -0600 | [diff] [blame] | 124 | goto done; |
Alexander Graf | 9b9b716 | 2017-07-03 13:32:35 +0200 | [diff] [blame] | 125 | } |
Alexander Graf | fdbae01 | 2016-04-11 23:51:01 +0200 | [diff] [blame] | 126 | } |
Heinrich Schuchardt | b23f9b7 | 2018-11-18 17:58:52 +0100 | [diff] [blame] | 127 | new_fdt = (void *)(uintptr_t)new_fdt_addr; |
Alexander Graf | 2ff5cc3 | 2016-04-11 16:55:26 +0200 | [diff] [blame] | 128 | memcpy(new_fdt, fdt, fdt_totalsize(fdt)); |
| 129 | fdt_set_totalsize(new_fdt, fdt_size); |
| 130 | |
Heinrich Schuchardt | b23f9b7 | 2018-11-18 17:58:52 +0100 | [diff] [blame] | 131 | *fdtp = (void *)(uintptr_t)new_fdt_addr; |
Simon Glass | 5d618df | 2018-08-08 03:54:30 -0600 | [diff] [blame] | 132 | done: |
| 133 | return ret; |
Alexander Graf | 2ff5cc3 | 2016-04-11 16:55:26 +0200 | [diff] [blame] | 134 | } |
| 135 | |
Simon Glass | 54b9b6e | 2018-06-18 08:08:28 -0600 | [diff] [blame] | 136 | /* |
| 137 | * efi_carve_out_dt_rsv() - Carve out DT reserved memory ranges |
| 138 | * |
| 139 | * The mem_rsv entries of the FDT are added to the memory map. Any failures are |
| 140 | * ignored because this is not critical and we would rather continue to try to |
| 141 | * boot. |
| 142 | * |
| 143 | * @fdt: Pointer to device tree |
| 144 | */ |
| 145 | static void efi_carve_out_dt_rsv(void *fdt) |
Alexander Graf | 22c88bf | 2018-04-06 09:40:51 +0200 | [diff] [blame] | 146 | { |
| 147 | int nr_rsv, i; |
| 148 | uint64_t addr, size, pages; |
| 149 | |
| 150 | nr_rsv = fdt_num_mem_rsv(fdt); |
| 151 | |
| 152 | /* Look for an existing entry and add it to the efi mem map. */ |
| 153 | for (i = 0; i < nr_rsv; i++) { |
| 154 | if (fdt_get_mem_rsv(fdt, i, &addr, &size) != 0) |
| 155 | continue; |
| 156 | |
Heinrich Schuchardt | b23f9b7 | 2018-11-18 17:58:52 +0100 | [diff] [blame] | 157 | /* Convert from sandbox address space. */ |
| 158 | addr = (uintptr_t)map_sysmem(addr, 0); |
| 159 | |
Heinrich Schuchardt | dcdca29 | 2018-11-18 17:58:49 +0100 | [diff] [blame] | 160 | pages = efi_size_in_pages(size + (addr & EFI_PAGE_MASK)); |
Heinrich Schuchardt | 3d92fc8 | 2018-11-12 18:55:23 +0100 | [diff] [blame] | 161 | addr &= ~EFI_PAGE_MASK; |
Simon Glass | 54b9b6e | 2018-06-18 08:08:28 -0600 | [diff] [blame] | 162 | if (!efi_add_memory_map(addr, pages, EFI_RESERVED_MEMORY_TYPE, |
| 163 | false)) |
| 164 | printf("FDT memrsv map %d: Failed to add to map\n", i); |
Alexander Graf | 22c88bf | 2018-04-06 09:40:51 +0200 | [diff] [blame] | 165 | } |
Alexander Graf | 22c88bf | 2018-04-06 09:40:51 +0200 | [diff] [blame] | 166 | } |
| 167 | |
Simon Glass | 5d618df | 2018-08-08 03:54:30 -0600 | [diff] [blame] | 168 | static efi_status_t efi_install_fdt(ulong fdt_addr) |
Heinrich Schuchardt | 44ab21b | 2018-03-03 15:29:03 +0100 | [diff] [blame] | 169 | { |
| 170 | bootm_headers_t img = { 0 }; |
Heinrich Schuchardt | 44ab21b | 2018-03-03 15:29:03 +0100 | [diff] [blame] | 171 | efi_status_t ret; |
Simon Glass | 5d618df | 2018-08-08 03:54:30 -0600 | [diff] [blame] | 172 | void *fdt; |
Heinrich Schuchardt | 44ab21b | 2018-03-03 15:29:03 +0100 | [diff] [blame] | 173 | |
Simon Glass | 5d618df | 2018-08-08 03:54:30 -0600 | [diff] [blame] | 174 | fdt = map_sysmem(fdt_addr, 0); |
Heinrich Schuchardt | 44ab21b | 2018-03-03 15:29:03 +0100 | [diff] [blame] | 175 | if (fdt_check_header(fdt)) { |
| 176 | printf("ERROR: invalid device tree\n"); |
| 177 | return EFI_INVALID_PARAMETER; |
| 178 | } |
| 179 | |
Heinrich Schuchardt | 68a0d5b | 2018-11-18 17:58:53 +0100 | [diff] [blame] | 180 | /* Create memory reservation as indicated by the device tree */ |
| 181 | efi_carve_out_dt_rsv(fdt); |
| 182 | |
Heinrich Schuchardt | 44ab21b | 2018-03-03 15:29:03 +0100 | [diff] [blame] | 183 | /* Prepare fdt for payload */ |
Heinrich Schuchardt | b23f9b7 | 2018-11-18 17:58:52 +0100 | [diff] [blame] | 184 | ret = copy_fdt(&fdt); |
Simon Glass | 5d618df | 2018-08-08 03:54:30 -0600 | [diff] [blame] | 185 | if (ret) |
| 186 | return ret; |
Heinrich Schuchardt | 44ab21b | 2018-03-03 15:29:03 +0100 | [diff] [blame] | 187 | |
| 188 | if (image_setup_libfdt(&img, fdt, 0, NULL)) { |
| 189 | printf("ERROR: failed to process device tree\n"); |
| 190 | return EFI_LOAD_ERROR; |
| 191 | } |
| 192 | |
| 193 | /* Link to it in the efi tables */ |
| 194 | ret = efi_install_configuration_table(&efi_guid_fdt, fdt); |
| 195 | if (ret != EFI_SUCCESS) |
| 196 | return EFI_OUT_OF_RESOURCES; |
| 197 | |
Heinrich Schuchardt | 44ab21b | 2018-03-03 15:29:03 +0100 | [diff] [blame] | 198 | return ret; |
| 199 | } |
| 200 | |
Simon Glass | 7816c7a | 2018-11-25 20:14:38 -0700 | [diff] [blame] | 201 | static efi_status_t bootefi_run_prepare(const char *load_options_path, |
| 202 | struct efi_device_path *device_path, |
| 203 | struct efi_device_path *image_path, |
| 204 | struct efi_loaded_image_obj **image_objp, |
| 205 | struct efi_loaded_image **loaded_image_infop) |
| 206 | { |
| 207 | efi_status_t ret; |
| 208 | |
| 209 | ret = efi_setup_loaded_image(device_path, image_path, image_objp, |
| 210 | loaded_image_infop); |
| 211 | if (ret != EFI_SUCCESS) |
| 212 | return ret; |
| 213 | |
| 214 | /* Transfer environment variable as load options */ |
| 215 | set_load_options(*loaded_image_infop, load_options_path); |
| 216 | |
| 217 | return 0; |
| 218 | } |
| 219 | |
Heinrich Schuchardt | 3c3a735 | 2018-09-23 17:21:51 +0200 | [diff] [blame] | 220 | /** |
Simon Glass | 4d77ee6 | 2018-11-25 20:14:39 -0700 | [diff] [blame] | 221 | * bootefi_run_finish() - finish up after running an EFI test |
| 222 | * |
| 223 | * @loaded_image_info: Pointer to a struct which holds the loaded image info |
| 224 | * @image_objj: Pointer to a struct which holds the loaded image object |
| 225 | */ |
| 226 | static void bootefi_run_finish(struct efi_loaded_image_obj *image_obj, |
| 227 | struct efi_loaded_image *loaded_image_info) |
| 228 | { |
| 229 | efi_restore_gd(); |
| 230 | free(loaded_image_info->load_options); |
| 231 | efi_delete_handle(&image_obj->header); |
| 232 | } |
| 233 | |
| 234 | /** |
Heinrich Schuchardt | 3c3a735 | 2018-09-23 17:21:51 +0200 | [diff] [blame] | 235 | * do_bootefi_exec() - execute EFI binary |
| 236 | * |
| 237 | * @efi: address of the binary |
| 238 | * @device_path: path of the device from which the binary was loaded |
| 239 | * @image_path: device path of the binary |
| 240 | * Return: status code |
| 241 | * |
| 242 | * Load the EFI binary into a newly assigned memory unwinding the relocation |
| 243 | * information, install the loaded image protocol, and call the binary. |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 244 | */ |
Heinrich Schuchardt | 44ab21b | 2018-03-03 15:29:03 +0100 | [diff] [blame] | 245 | static efi_status_t do_bootefi_exec(void *efi, |
Heinrich Schuchardt | 4e363a9 | 2017-10-18 18:13:08 +0200 | [diff] [blame] | 246 | struct efi_device_path *device_path, |
| 247 | struct efi_device_path *image_path) |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 248 | { |
Heinrich Schuchardt | 078d6e2 | 2018-09-16 07:19:48 +0200 | [diff] [blame] | 249 | efi_handle_t mem_handle = NULL; |
Rob Clark | 18ceba7 | 2017-10-10 08:23:06 -0400 | [diff] [blame] | 250 | struct efi_device_path *memdp = NULL; |
Heinrich Schuchardt | 4b055a2 | 2018-03-03 15:29:01 +0100 | [diff] [blame] | 251 | efi_status_t ret; |
Heinrich Schuchardt | 4f94ec5 | 2018-09-26 05:27:54 +0200 | [diff] [blame] | 252 | struct efi_loaded_image_obj *image_obj = NULL; |
Heinrich Schuchardt | 3c3a735 | 2018-09-23 17:21:51 +0200 | [diff] [blame] | 253 | struct efi_loaded_image *loaded_image_info = NULL; |
Rob Clark | f8db922 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 254 | |
Rob Clark | 18ceba7 | 2017-10-10 08:23:06 -0400 | [diff] [blame] | 255 | /* |
| 256 | * Special case for efi payload not loaded from disk, such as |
| 257 | * 'bootefi hello' or for example payload loaded directly into |
Heinrich Schuchardt | 5e96f42 | 2018-10-18 21:51:38 +0200 | [diff] [blame] | 258 | * memory via JTAG, etc: |
Rob Clark | 18ceba7 | 2017-10-10 08:23:06 -0400 | [diff] [blame] | 259 | */ |
| 260 | if (!device_path && !image_path) { |
| 261 | printf("WARNING: using memory device/image path, this may confuse some payloads!\n"); |
| 262 | /* actual addresses filled in after efi_load_pe() */ |
Heinrich Schuchardt | 4c419bb | 2018-12-26 22:23:00 +0100 | [diff] [blame] | 263 | memdp = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE, 0, 0); |
Rob Clark | 18ceba7 | 2017-10-10 08:23:06 -0400 | [diff] [blame] | 264 | device_path = image_path = memdp; |
Heinrich Schuchardt | 078d6e2 | 2018-09-16 07:19:48 +0200 | [diff] [blame] | 265 | /* |
| 266 | * Grub expects that the device path of the loaded image is |
| 267 | * installed on a handle. |
| 268 | */ |
| 269 | ret = efi_create_handle(&mem_handle); |
| 270 | if (ret != EFI_SUCCESS) |
Simon Glass | 4d77ee6 | 2018-11-25 20:14:39 -0700 | [diff] [blame] | 271 | return ret; /* TODO: leaks device_path */ |
Heinrich Schuchardt | 078d6e2 | 2018-09-16 07:19:48 +0200 | [diff] [blame] | 272 | ret = efi_add_protocol(mem_handle, &efi_guid_device_path, |
Alexander Graf | 2d00dbe | 2018-06-12 10:21:16 +0200 | [diff] [blame] | 273 | device_path); |
| 274 | if (ret != EFI_SUCCESS) |
Simon Glass | 4d77ee6 | 2018-11-25 20:14:39 -0700 | [diff] [blame] | 275 | goto err_add_protocol; |
Rob Clark | 18ceba7 | 2017-10-10 08:23:06 -0400 | [diff] [blame] | 276 | } else { |
| 277 | assert(device_path && image_path); |
| 278 | } |
| 279 | |
Simon Glass | 7816c7a | 2018-11-25 20:14:38 -0700 | [diff] [blame] | 280 | ret = bootefi_run_prepare("bootargs", device_path, image_path, |
| 281 | &image_obj, &loaded_image_info); |
| 282 | if (ret) |
Simon Glass | 4d77ee6 | 2018-11-25 20:14:39 -0700 | [diff] [blame] | 283 | goto err_prepare; |
Rob Clark | f8db922 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 284 | |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 285 | /* Load the EFI payload */ |
Heinrich Schuchardt | 408fbcc | 2018-12-26 12:49:09 +0100 | [diff] [blame] | 286 | ret = efi_load_pe(image_obj, efi, loaded_image_info); |
| 287 | if (ret != EFI_SUCCESS) |
Simon Glass | 4d77ee6 | 2018-11-25 20:14:39 -0700 | [diff] [blame] | 288 | goto err_prepare; |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 289 | |
Rob Clark | 18ceba7 | 2017-10-10 08:23:06 -0400 | [diff] [blame] | 290 | if (memdp) { |
| 291 | struct efi_device_path_memory *mdp = (void *)memdp; |
Heinrich Schuchardt | 3c3a735 | 2018-09-23 17:21:51 +0200 | [diff] [blame] | 292 | mdp->memory_type = loaded_image_info->image_code_type; |
| 293 | mdp->start_address = (uintptr_t)loaded_image_info->image_base; |
Rob Clark | 18ceba7 | 2017-10-10 08:23:06 -0400 | [diff] [blame] | 294 | mdp->end_address = mdp->start_address + |
Heinrich Schuchardt | 3c3a735 | 2018-09-23 17:21:51 +0200 | [diff] [blame] | 295 | loaded_image_info->image_size; |
Rob Clark | 18ceba7 | 2017-10-10 08:23:06 -0400 | [diff] [blame] | 296 | } |
| 297 | |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 298 | /* we don't support much: */ |
| 299 | env_set("efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_OsIndicationsSupported", |
| 300 | "{ro,boot}(blob)0000000000000000"); |
| 301 | |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 302 | /* Call our payload! */ |
Heinrich Schuchardt | 408fbcc | 2018-12-26 12:49:09 +0100 | [diff] [blame] | 303 | debug("%s: Jumping to 0x%p\n", __func__, image_obj->entry); |
Heinrich Schuchardt | 2f8eb08 | 2018-12-26 13:28:09 +0100 | [diff] [blame^] | 304 | ret = EFI_CALL(efi_start_image(&image_obj->header, NULL, NULL)); |
Rob Clark | f8db922 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 305 | |
Simon Glass | 4d77ee6 | 2018-11-25 20:14:39 -0700 | [diff] [blame] | 306 | err_prepare: |
Rob Clark | f8db922 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 307 | /* image has returned, loaded-image obj goes *poof*: */ |
Simon Glass | 4d77ee6 | 2018-11-25 20:14:39 -0700 | [diff] [blame] | 308 | bootefi_run_finish(image_obj, loaded_image_info); |
| 309 | |
| 310 | err_add_protocol: |
Heinrich Schuchardt | 078d6e2 | 2018-09-16 07:19:48 +0200 | [diff] [blame] | 311 | if (mem_handle) |
| 312 | efi_delete_handle(mem_handle); |
Rob Clark | f8db922 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 313 | |
| 314 | return ret; |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 315 | } |
| 316 | |
Simon Glass | 93f2559 | 2018-11-25 20:14:37 -0700 | [diff] [blame] | 317 | #ifdef CONFIG_CMD_BOOTEFI_SELFTEST |
| 318 | /** |
| 319 | * bootefi_test_prepare() - prepare to run an EFI test |
| 320 | * |
| 321 | * This sets things up so we can call EFI functions. This involves preparing |
| 322 | * the 'gd' pointer and setting up the load ed image data structures. |
| 323 | * |
| 324 | * @image_objp: loaded_image_infop: Pointer to a struct which will hold the |
| 325 | * loaded image object. This struct will be inited by this function before |
| 326 | * use. |
| 327 | * @loaded_image_infop: Pointer to a struct which will hold the loaded image |
| 328 | * info. This struct will be inited by this function before use. |
| 329 | * @path: File path to the test being run (often just the test name with a |
| 330 | * backslash before it |
| 331 | * @test_func: Address of the test function that is being run |
Simon Glass | 7816c7a | 2018-11-25 20:14:38 -0700 | [diff] [blame] | 332 | * @load_options_path: U-Boot environment variable to use as load options |
Simon Glass | 93f2559 | 2018-11-25 20:14:37 -0700 | [diff] [blame] | 333 | * @return 0 if OK, -ve on error |
| 334 | */ |
| 335 | static efi_status_t bootefi_test_prepare |
| 336 | (struct efi_loaded_image_obj **image_objp, |
Simon Glass | 7816c7a | 2018-11-25 20:14:38 -0700 | [diff] [blame] | 337 | struct efi_loaded_image **loaded_image_infop, const char *path, |
| 338 | ulong test_func, const char *load_options_path) |
Simon Glass | 93f2559 | 2018-11-25 20:14:37 -0700 | [diff] [blame] | 339 | { |
Simon Glass | 93f2559 | 2018-11-25 20:14:37 -0700 | [diff] [blame] | 340 | /* Construct a dummy device path */ |
| 341 | bootefi_device_path = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE, |
| 342 | (uintptr_t)test_func, |
| 343 | (uintptr_t)test_func); |
| 344 | if (!bootefi_device_path) |
| 345 | return EFI_OUT_OF_RESOURCES; |
| 346 | bootefi_image_path = efi_dp_from_file(NULL, 0, path); |
| 347 | if (!bootefi_image_path) |
| 348 | return EFI_OUT_OF_RESOURCES; |
Simon Glass | 93f2559 | 2018-11-25 20:14:37 -0700 | [diff] [blame] | 349 | |
Simon Glass | 7816c7a | 2018-11-25 20:14:38 -0700 | [diff] [blame] | 350 | return bootefi_run_prepare(load_options_path, bootefi_device_path, |
| 351 | bootefi_image_path, image_objp, |
| 352 | loaded_image_infop); |
Simon Glass | 93f2559 | 2018-11-25 20:14:37 -0700 | [diff] [blame] | 353 | } |
| 354 | |
Simon Glass | 93f2559 | 2018-11-25 20:14:37 -0700 | [diff] [blame] | 355 | #endif /* CONFIG_CMD_BOOTEFI_SELFTEST */ |
| 356 | |
Heinrich Schuchardt | 44ab21b | 2018-03-03 15:29:03 +0100 | [diff] [blame] | 357 | static int do_bootefi_bootmgr_exec(void) |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 358 | { |
| 359 | struct efi_device_path *device_path, *file_path; |
| 360 | void *addr; |
| 361 | efi_status_t r; |
| 362 | |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 363 | addr = efi_bootmgr_load(&device_path, &file_path); |
| 364 | if (!addr) |
| 365 | return 1; |
| 366 | |
| 367 | printf("## Starting EFI application at %p ...\n", addr); |
Heinrich Schuchardt | 44ab21b | 2018-03-03 15:29:03 +0100 | [diff] [blame] | 368 | r = do_bootefi_exec(addr, device_path, file_path); |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 369 | printf("## Application terminated, r = %lu\n", |
| 370 | r & ~EFI_ERROR_MASK); |
| 371 | |
| 372 | if (r != EFI_SUCCESS) |
| 373 | return 1; |
| 374 | |
| 375 | return 0; |
| 376 | } |
| 377 | |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 378 | /* Interpreter command to boot an arbitrary EFI image from memory */ |
| 379 | static int do_bootefi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 380 | { |
Heinrich Schuchardt | 44ab21b | 2018-03-03 15:29:03 +0100 | [diff] [blame] | 381 | unsigned long addr; |
| 382 | char *saddr; |
Heinrich Schuchardt | 4e363a9 | 2017-10-18 18:13:08 +0200 | [diff] [blame] | 383 | efi_status_t r; |
Alexander Graf | a241451 | 2018-06-18 17:22:58 +0200 | [diff] [blame] | 384 | unsigned long fdt_addr; |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 385 | |
Heinrich Schuchardt | 954e2b9 | 2018-04-03 21:59:32 +0200 | [diff] [blame] | 386 | /* Allow unaligned memory access */ |
| 387 | allow_unaligned(); |
| 388 | |
Heinrich Schuchardt | aa0b11b | 2019-01-08 18:13:06 +0100 | [diff] [blame] | 389 | switch_to_non_secure_mode(); |
| 390 | |
Heinrich Schuchardt | 8c0dfc2 | 2018-03-03 15:29:02 +0100 | [diff] [blame] | 391 | /* Initialize EFI drivers */ |
| 392 | r = efi_init_obj_list(); |
| 393 | if (r != EFI_SUCCESS) { |
| 394 | printf("Error: Cannot set up EFI drivers, r = %lu\n", |
| 395 | r & ~EFI_ERROR_MASK); |
| 396 | return CMD_RET_FAILURE; |
| 397 | } |
| 398 | |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 399 | if (argc < 2) |
Bin Meng | da800ff | 2016-08-13 04:02:06 -0700 | [diff] [blame] | 400 | return CMD_RET_USAGE; |
Heinrich Schuchardt | 44ab21b | 2018-03-03 15:29:03 +0100 | [diff] [blame] | 401 | |
| 402 | if (argc > 2) { |
Alexander Graf | a241451 | 2018-06-18 17:22:58 +0200 | [diff] [blame] | 403 | fdt_addr = simple_strtoul(argv[2], NULL, 16); |
Heinrich Schuchardt | 44ab21b | 2018-03-03 15:29:03 +0100 | [diff] [blame] | 404 | if (!fdt_addr && *argv[2] != '0') |
| 405 | return CMD_RET_USAGE; |
| 406 | /* Install device tree */ |
Simon Glass | 5d618df | 2018-08-08 03:54:30 -0600 | [diff] [blame] | 407 | r = efi_install_fdt(fdt_addr); |
Heinrich Schuchardt | 44ab21b | 2018-03-03 15:29:03 +0100 | [diff] [blame] | 408 | if (r != EFI_SUCCESS) { |
| 409 | printf("ERROR: failed to install device tree\n"); |
| 410 | return CMD_RET_FAILURE; |
| 411 | } |
| 412 | } else { |
| 413 | /* Remove device tree. EFI_NOT_FOUND can be ignored here */ |
| 414 | efi_install_configuration_table(&efi_guid_fdt, NULL); |
| 415 | printf("WARNING: booting without device tree\n"); |
| 416 | } |
Simon Glass | fac4ced | 2016-11-07 08:47:08 -0700 | [diff] [blame] | 417 | #ifdef CONFIG_CMD_BOOTEFI_HELLO |
| 418 | if (!strcmp(argv[1], "hello")) { |
Heinrich Schuchardt | 7804ae9 | 2017-09-05 03:19:37 +0200 | [diff] [blame] | 419 | ulong size = __efi_helloworld_end - __efi_helloworld_begin; |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 420 | |
Heinrich Schuchardt | e6a9ab5 | 2017-08-28 18:54:30 +0200 | [diff] [blame] | 421 | saddr = env_get("loadaddr"); |
| 422 | if (saddr) |
| 423 | addr = simple_strtoul(saddr, NULL, 16); |
| 424 | else |
| 425 | addr = CONFIG_SYS_LOAD_ADDR; |
Alexander Graf | a241451 | 2018-06-18 17:22:58 +0200 | [diff] [blame] | 426 | memcpy(map_sysmem(addr, size), __efi_helloworld_begin, size); |
Simon Glass | fac4ced | 2016-11-07 08:47:08 -0700 | [diff] [blame] | 427 | } else |
| 428 | #endif |
Heinrich Schuchardt | d33ae3e | 2017-09-15 10:06:11 +0200 | [diff] [blame] | 429 | #ifdef CONFIG_CMD_BOOTEFI_SELFTEST |
| 430 | if (!strcmp(argv[1], "selftest")) { |
Heinrich Schuchardt | 4f94ec5 | 2018-09-26 05:27:54 +0200 | [diff] [blame] | 431 | struct efi_loaded_image_obj *image_obj; |
Heinrich Schuchardt | 3c3a735 | 2018-09-23 17:21:51 +0200 | [diff] [blame] | 432 | struct efi_loaded_image *loaded_image_info; |
Heinrich Schuchardt | 389f251 | 2017-09-20 22:54:58 +0200 | [diff] [blame] | 433 | |
Simon Glass | 93f2559 | 2018-11-25 20:14:37 -0700 | [diff] [blame] | 434 | if (bootefi_test_prepare(&image_obj, &loaded_image_info, |
Simon Glass | 7816c7a | 2018-11-25 20:14:38 -0700 | [diff] [blame] | 435 | "\\selftest", (uintptr_t)&efi_selftest, |
| 436 | "efi_selftest")) |
Simon Glass | 758cfa1 | 2018-11-25 20:14:36 -0700 | [diff] [blame] | 437 | return CMD_RET_FAILURE; |
| 438 | |
Heinrich Schuchardt | 02efd5d | 2017-10-18 18:13:13 +0200 | [diff] [blame] | 439 | /* Execute the test */ |
Heinrich Schuchardt | 7292872 | 2018-09-26 05:27:56 +0200 | [diff] [blame] | 440 | r = efi_selftest(&image_obj->header, &systab); |
Simon Glass | 4d77ee6 | 2018-11-25 20:14:39 -0700 | [diff] [blame] | 441 | bootefi_run_finish(image_obj, loaded_image_info); |
Heinrich Schuchardt | d2d90b4 | 2017-10-18 18:13:14 +0200 | [diff] [blame] | 442 | return r != EFI_SUCCESS; |
Heinrich Schuchardt | d33ae3e | 2017-09-15 10:06:11 +0200 | [diff] [blame] | 443 | } else |
| 444 | #endif |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 445 | if (!strcmp(argv[1], "bootmgr")) { |
Heinrich Schuchardt | 44ab21b | 2018-03-03 15:29:03 +0100 | [diff] [blame] | 446 | return do_bootefi_bootmgr_exec(); |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 447 | } else { |
Simon Glass | fac4ced | 2016-11-07 08:47:08 -0700 | [diff] [blame] | 448 | saddr = argv[1]; |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 449 | |
Simon Glass | fac4ced | 2016-11-07 08:47:08 -0700 | [diff] [blame] | 450 | addr = simple_strtoul(saddr, NULL, 16); |
Heinrich Schuchardt | aa6fd9a | 2018-01-24 20:33:54 +0100 | [diff] [blame] | 451 | /* Check that a numeric value was passed */ |
| 452 | if (!addr && *saddr != '0') |
| 453 | return CMD_RET_USAGE; |
Simon Glass | fac4ced | 2016-11-07 08:47:08 -0700 | [diff] [blame] | 454 | |
Alexander Graf | 54c1e83 | 2016-04-14 16:07:53 +0200 | [diff] [blame] | 455 | } |
| 456 | |
Simon Glass | 1a5490e | 2016-11-07 08:47:05 -0700 | [diff] [blame] | 457 | printf("## Starting EFI application at %08lx ...\n", addr); |
Alexander Graf | a241451 | 2018-06-18 17:22:58 +0200 | [diff] [blame] | 458 | r = do_bootefi_exec(map_sysmem(addr, 0), bootefi_device_path, |
Heinrich Schuchardt | 44ab21b | 2018-03-03 15:29:03 +0100 | [diff] [blame] | 459 | bootefi_image_path); |
xypron.glpk@gmx.de | 5eadbfa | 2017-07-04 23:15:23 +0200 | [diff] [blame] | 460 | printf("## Application terminated, r = %lu\n", |
| 461 | r & ~EFI_ERROR_MASK); |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 462 | |
xypron.glpk@gmx.de | 5eadbfa | 2017-07-04 23:15:23 +0200 | [diff] [blame] | 463 | if (r != EFI_SUCCESS) |
| 464 | return 1; |
| 465 | else |
| 466 | return 0; |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | #ifdef CONFIG_SYS_LONGHELP |
| 470 | static char bootefi_help_text[] = |
Alexander Graf | 54c1e83 | 2016-04-14 16:07:53 +0200 | [diff] [blame] | 471 | "<image address> [fdt address]\n" |
| 472 | " - boot EFI payload stored at address <image address>.\n" |
| 473 | " If specified, the device tree located at <fdt address> gets\n" |
Simon Glass | fac4ced | 2016-11-07 08:47:08 -0700 | [diff] [blame] | 474 | " exposed as EFI configuration table.\n" |
| 475 | #ifdef CONFIG_CMD_BOOTEFI_HELLO |
Heinrich Schuchardt | d33ae3e | 2017-09-15 10:06:11 +0200 | [diff] [blame] | 476 | "bootefi hello\n" |
| 477 | " - boot a sample Hello World application stored within U-Boot\n" |
| 478 | #endif |
| 479 | #ifdef CONFIG_CMD_BOOTEFI_SELFTEST |
Heinrich Schuchardt | 44ab21b | 2018-03-03 15:29:03 +0100 | [diff] [blame] | 480 | "bootefi selftest [fdt address]\n" |
Heinrich Schuchardt | d33ae3e | 2017-09-15 10:06:11 +0200 | [diff] [blame] | 481 | " - boot an EFI selftest application stored within U-Boot\n" |
Heinrich Schuchardt | 02efd5d | 2017-10-18 18:13:13 +0200 | [diff] [blame] | 482 | " Use environment variable efi_selftest to select a single test.\n" |
| 483 | " Use 'setenv efi_selftest list' to enumerate all tests.\n" |
Simon Glass | fac4ced | 2016-11-07 08:47:08 -0700 | [diff] [blame] | 484 | #endif |
Heinrich Schuchardt | 1b2320f | 2018-01-30 19:47:43 +0100 | [diff] [blame] | 485 | "bootefi bootmgr [fdt addr]\n" |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 486 | " - load and boot EFI payload based on BootOrder/BootXXXX variables.\n" |
| 487 | "\n" |
| 488 | " If specified, the device tree located at <fdt address> gets\n" |
| 489 | " exposed as EFI configuration table.\n"; |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 490 | #endif |
| 491 | |
| 492 | U_BOOT_CMD( |
Alexander Graf | 54c1e83 | 2016-04-14 16:07:53 +0200 | [diff] [blame] | 493 | bootefi, 3, 0, do_bootefi, |
Sergey Kubushyn | 268f19e | 2016-06-07 11:14:31 -0700 | [diff] [blame] | 494 | "Boots an EFI payload from memory", |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 495 | bootefi_help_text |
| 496 | ); |
Alexander Graf | 8f19f26 | 2016-03-04 01:10:14 +0100 | [diff] [blame] | 497 | |
Rob Clark | f8db922 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 498 | void efi_set_bootdev(const char *dev, const char *devnr, const char *path) |
| 499 | { |
AKASHI Takahiro | 035fb01 | 2018-10-17 16:32:03 +0900 | [diff] [blame] | 500 | struct efi_device_path *device, *image; |
| 501 | efi_status_t ret; |
Alexander Graf | 34118e7 | 2016-08-05 14:49:53 +0200 | [diff] [blame] | 502 | |
Heinrich Schuchardt | 761ecc8 | 2018-09-16 07:20:21 +0200 | [diff] [blame] | 503 | /* efi_set_bootdev is typically called repeatedly, recover memory */ |
| 504 | efi_free_pool(bootefi_device_path); |
| 505 | efi_free_pool(bootefi_image_path); |
Heinrich Schuchardt | 761ecc8 | 2018-09-16 07:20:21 +0200 | [diff] [blame] | 506 | |
AKASHI Takahiro | 035fb01 | 2018-10-17 16:32:03 +0900 | [diff] [blame] | 507 | ret = efi_dp_from_name(dev, devnr, path, &device, &image); |
| 508 | if (ret == EFI_SUCCESS) { |
| 509 | bootefi_device_path = device; |
| 510 | bootefi_image_path = image; |
Alexander Graf | 45e437d | 2016-07-21 01:44:46 +0200 | [diff] [blame] | 511 | } else { |
AKASHI Takahiro | 035fb01 | 2018-10-17 16:32:03 +0900 | [diff] [blame] | 512 | bootefi_device_path = NULL; |
| 513 | bootefi_image_path = NULL; |
Alexander Graf | 45e437d | 2016-07-21 01:44:46 +0200 | [diff] [blame] | 514 | } |
Alexander Graf | 8f19f26 | 2016-03-04 01:10:14 +0100 | [diff] [blame] | 515 | } |