Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 1 | /* |
| 2 | * EFI application loader |
| 3 | * |
| 4 | * Copyright (c) 2016 Alexander Graf |
| 5 | * |
| 6 | * SPDX-License-Identifier: GPL-2.0+ |
| 7 | */ |
| 8 | |
| 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> |
| 13 | #include <errno.h> |
| 14 | #include <libfdt.h> |
| 15 | #include <libfdt_env.h> |
Alexander Graf | fdbae01 | 2016-04-11 23:51:01 +0200 | [diff] [blame] | 16 | #include <memalign.h> |
Alexander Graf | 2ff5cc3 | 2016-04-11 16:55:26 +0200 | [diff] [blame] | 17 | #include <asm/global_data.h> |
Simon Glass | c2194bf | 2016-09-25 15:27:32 -0600 | [diff] [blame] | 18 | #include <asm-generic/sections.h> |
| 19 | #include <linux/linkage.h> |
Alexander Graf | 2ff5cc3 | 2016-04-11 16:55:26 +0200 | [diff] [blame] | 20 | |
| 21 | DECLARE_GLOBAL_DATA_PTR; |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 22 | |
Heinrich Schuchardt | 5e9900f | 2017-07-19 19:37:22 +0200 | [diff] [blame] | 23 | static uint8_t efi_obj_list_initalized; |
| 24 | |
Rob Clark | f8db922 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 25 | static struct efi_device_path *bootefi_image_path; |
| 26 | static struct efi_device_path *bootefi_device_path; |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 27 | |
Heinrich Schuchardt | 5e9900f | 2017-07-19 19:37:22 +0200 | [diff] [blame] | 28 | /* Initialize and populate EFI object list */ |
| 29 | static void efi_init_obj_list(void) |
| 30 | { |
| 31 | efi_obj_list_initalized = 1; |
| 32 | |
Heinrich Schuchardt | 5e9900f | 2017-07-19 19:37:22 +0200 | [diff] [blame] | 33 | efi_console_register(); |
| 34 | #ifdef CONFIG_PARTITIONS |
| 35 | efi_disk_register(); |
| 36 | #endif |
| 37 | #if defined(CONFIG_LCD) || defined(CONFIG_DM_VIDEO) |
| 38 | efi_gop_register(); |
| 39 | #endif |
| 40 | #ifdef CONFIG_NET |
Rob Clark | f8db922 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 41 | efi_net_register(); |
Heinrich Schuchardt | 5e9900f | 2017-07-19 19:37:22 +0200 | [diff] [blame] | 42 | #endif |
| 43 | #ifdef CONFIG_GENERATE_SMBIOS_TABLE |
| 44 | efi_smbios_register(); |
| 45 | #endif |
Heinrich Schuchardt | 18081d4 | 2017-10-18 18:13:04 +0200 | [diff] [blame^] | 46 | efi_watchdog_register(); |
Heinrich Schuchardt | 5e9900f | 2017-07-19 19:37:22 +0200 | [diff] [blame] | 47 | |
| 48 | /* Initialize EFI runtime services */ |
| 49 | efi_reset_system_init(); |
| 50 | efi_get_time_init(); |
| 51 | } |
| 52 | |
Alexander Graf | 2ff5cc3 | 2016-04-11 16:55:26 +0200 | [diff] [blame] | 53 | static void *copy_fdt(void *fdt) |
| 54 | { |
| 55 | u64 fdt_size = fdt_totalsize(fdt); |
Alexander Graf | fdbae01 | 2016-04-11 23:51:01 +0200 | [diff] [blame] | 56 | unsigned long fdt_ram_start = -1L, fdt_pages; |
| 57 | u64 new_fdt_addr; |
Alexander Graf | 2ff5cc3 | 2016-04-11 16:55:26 +0200 | [diff] [blame] | 58 | void *new_fdt; |
Alexander Graf | fdbae01 | 2016-04-11 23:51:01 +0200 | [diff] [blame] | 59 | int i; |
Alexander Graf | 2ff5cc3 | 2016-04-11 16:55:26 +0200 | [diff] [blame] | 60 | |
Alexander Graf | fdbae01 | 2016-04-11 23:51:01 +0200 | [diff] [blame] | 61 | for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) { |
| 62 | u64 ram_start = gd->bd->bi_dram[i].start; |
| 63 | u64 ram_size = gd->bd->bi_dram[i].size; |
Alexander Graf | 2ff5cc3 | 2016-04-11 16:55:26 +0200 | [diff] [blame] | 64 | |
Alexander Graf | fdbae01 | 2016-04-11 23:51:01 +0200 | [diff] [blame] | 65 | if (!ram_size) |
| 66 | continue; |
| 67 | |
| 68 | if (ram_start < fdt_ram_start) |
| 69 | fdt_ram_start = ram_start; |
| 70 | } |
| 71 | |
| 72 | /* Give us at least 4kb breathing room */ |
xypron.glpk@gmx.de | f6b9912 | 2017-08-11 21:19:25 +0200 | [diff] [blame] | 73 | fdt_size = ALIGN(fdt_size + 4096, EFI_PAGE_SIZE); |
Alexander Graf | fdbae01 | 2016-04-11 23:51:01 +0200 | [diff] [blame] | 74 | fdt_pages = fdt_size >> EFI_PAGE_SHIFT; |
| 75 | |
| 76 | /* Safe fdt location is at 128MB */ |
| 77 | new_fdt_addr = fdt_ram_start + (128 * 1024 * 1024) + fdt_size; |
| 78 | if (efi_allocate_pages(1, EFI_BOOT_SERVICES_DATA, fdt_pages, |
| 79 | &new_fdt_addr) != EFI_SUCCESS) { |
| 80 | /* If we can't put it there, put it somewhere */ |
xypron.glpk@gmx.de | f6b9912 | 2017-08-11 21:19:25 +0200 | [diff] [blame] | 81 | new_fdt_addr = (ulong)memalign(EFI_PAGE_SIZE, fdt_size); |
Alexander Graf | 9b9b716 | 2017-07-03 13:32:35 +0200 | [diff] [blame] | 82 | if (efi_allocate_pages(1, EFI_BOOT_SERVICES_DATA, fdt_pages, |
| 83 | &new_fdt_addr) != EFI_SUCCESS) { |
| 84 | printf("ERROR: Failed to reserve space for FDT\n"); |
| 85 | return NULL; |
| 86 | } |
Alexander Graf | fdbae01 | 2016-04-11 23:51:01 +0200 | [diff] [blame] | 87 | } |
Alexander Graf | 9b9b716 | 2017-07-03 13:32:35 +0200 | [diff] [blame] | 88 | |
Alexander Graf | fdbae01 | 2016-04-11 23:51:01 +0200 | [diff] [blame] | 89 | new_fdt = (void*)(ulong)new_fdt_addr; |
Alexander Graf | 2ff5cc3 | 2016-04-11 16:55:26 +0200 | [diff] [blame] | 90 | memcpy(new_fdt, fdt, fdt_totalsize(fdt)); |
| 91 | fdt_set_totalsize(new_fdt, fdt_size); |
| 92 | |
| 93 | return new_fdt; |
| 94 | } |
| 95 | |
xypron.glpk@gmx.de | 1e5afc6 | 2017-07-04 23:15:21 +0200 | [diff] [blame] | 96 | static ulong efi_do_enter(void *image_handle, |
| 97 | struct efi_system_table *st, |
| 98 | asmlinkage ulong (*entry)(void *image_handle, |
| 99 | struct efi_system_table *st)) |
| 100 | { |
| 101 | efi_status_t ret = EFI_LOAD_ERROR; |
| 102 | |
| 103 | if (entry) |
| 104 | ret = entry(image_handle, st); |
| 105 | st->boottime->exit(image_handle, ret, 0, NULL); |
| 106 | return ret; |
| 107 | } |
| 108 | |
Alison Wang | 73818d5 | 2016-11-10 10:49:03 +0800 | [diff] [blame] | 109 | #ifdef CONFIG_ARM64 |
xypron.glpk@gmx.de | 1e5afc6 | 2017-07-04 23:15:21 +0200 | [diff] [blame] | 110 | static unsigned long efi_run_in_el2(asmlinkage ulong (*entry)( |
| 111 | void *image_handle, struct efi_system_table *st), |
| 112 | void *image_handle, struct efi_system_table *st) |
Alison Wang | 73818d5 | 2016-11-10 10:49:03 +0800 | [diff] [blame] | 113 | { |
| 114 | /* Enable caches again */ |
| 115 | dcache_enable(); |
| 116 | |
xypron.glpk@gmx.de | 1e5afc6 | 2017-07-04 23:15:21 +0200 | [diff] [blame] | 117 | return efi_do_enter(image_handle, st, entry); |
Alison Wang | 73818d5 | 2016-11-10 10:49:03 +0800 | [diff] [blame] | 118 | } |
| 119 | #endif |
| 120 | |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 121 | /* |
| 122 | * Load an EFI payload into a newly allocated piece of memory, register all |
| 123 | * EFI objects it would want to access and jump to it. |
| 124 | */ |
Rob Clark | f8db922 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 125 | static unsigned long do_bootefi_exec(void *efi, void *fdt, |
| 126 | struct efi_device_path *device_path, |
| 127 | struct efi_device_path *image_path) |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 128 | { |
Rob Clark | f8db922 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 129 | struct efi_loaded_image loaded_image_info = {}; |
| 130 | struct efi_object loaded_image_info_obj = {}; |
Rob Clark | 18ceba7 | 2017-10-10 08:23:06 -0400 | [diff] [blame] | 131 | struct efi_device_path *memdp = NULL; |
Rob Clark | f8db922 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 132 | ulong ret; |
| 133 | |
Simon Glass | c2194bf | 2016-09-25 15:27:32 -0600 | [diff] [blame] | 134 | ulong (*entry)(void *image_handle, struct efi_system_table *st) |
| 135 | asmlinkage; |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 136 | ulong fdt_pages, fdt_size, fdt_start, fdt_end; |
Alexander Graf | 5fb802a | 2017-07-26 13:41:05 +0200 | [diff] [blame] | 137 | const efi_guid_t fdt_guid = EFI_FDT_GUID; |
Alexander Graf | c81b347 | 2016-03-04 01:10:13 +0100 | [diff] [blame] | 138 | bootm_headers_t img = { 0 }; |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 139 | |
Rob Clark | 18ceba7 | 2017-10-10 08:23:06 -0400 | [diff] [blame] | 140 | /* |
| 141 | * Special case for efi payload not loaded from disk, such as |
| 142 | * 'bootefi hello' or for example payload loaded directly into |
| 143 | * memory via jtag/etc: |
| 144 | */ |
| 145 | if (!device_path && !image_path) { |
| 146 | printf("WARNING: using memory device/image path, this may confuse some payloads!\n"); |
| 147 | /* actual addresses filled in after efi_load_pe() */ |
| 148 | memdp = efi_dp_from_mem(0, 0, 0); |
| 149 | device_path = image_path = memdp; |
| 150 | } else { |
| 151 | assert(device_path && image_path); |
| 152 | } |
| 153 | |
Rob Clark | f8db922 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 154 | /* Initialize and populate EFI object list */ |
| 155 | if (!efi_obj_list_initalized) |
| 156 | efi_init_obj_list(); |
| 157 | |
| 158 | efi_setup_loaded_image(&loaded_image_info, &loaded_image_info_obj, |
| 159 | device_path, image_path); |
| 160 | |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 161 | /* |
| 162 | * gd lives in a fixed register which may get clobbered while we execute |
| 163 | * the payload. So save it here and restore it on every callback entry |
| 164 | */ |
| 165 | efi_save_gd(); |
| 166 | |
Alexander Graf | 54c1e83 | 2016-04-14 16:07:53 +0200 | [diff] [blame] | 167 | if (fdt && !fdt_check_header(fdt)) { |
Alexander Graf | c81b347 | 2016-03-04 01:10:13 +0100 | [diff] [blame] | 168 | /* Prepare fdt for payload */ |
Alexander Graf | 2ff5cc3 | 2016-04-11 16:55:26 +0200 | [diff] [blame] | 169 | fdt = copy_fdt(fdt); |
| 170 | |
| 171 | if (image_setup_libfdt(&img, fdt, 0, NULL)) { |
Alexander Graf | c81b347 | 2016-03-04 01:10:13 +0100 | [diff] [blame] | 172 | printf("ERROR: Failed to process device tree\n"); |
| 173 | return -EINVAL; |
| 174 | } |
| 175 | |
| 176 | /* Link to it in the efi tables */ |
Alexander Graf | 5fb802a | 2017-07-26 13:41:05 +0200 | [diff] [blame] | 177 | efi_install_configuration_table(&fdt_guid, fdt); |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 178 | |
| 179 | /* And reserve the space in the memory map */ |
Alexander Graf | 2ff5cc3 | 2016-04-11 16:55:26 +0200 | [diff] [blame] | 180 | fdt_start = ((ulong)fdt) & ~EFI_PAGE_MASK; |
| 181 | fdt_end = ((ulong)fdt) + fdt_totalsize(fdt); |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 182 | fdt_size = (fdt_end - fdt_start) + EFI_PAGE_MASK; |
| 183 | fdt_pages = fdt_size >> EFI_PAGE_SHIFT; |
| 184 | /* Give a bootloader the chance to modify the device tree */ |
| 185 | fdt_pages += 2; |
| 186 | efi_add_memory_map(fdt_start, fdt_pages, |
| 187 | EFI_BOOT_SERVICES_DATA, true); |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 188 | } else { |
Alexander Graf | 54c1e83 | 2016-04-14 16:07:53 +0200 | [diff] [blame] | 189 | printf("WARNING: Invalid device tree, expect boot to fail\n"); |
Alexander Graf | 5fb802a | 2017-07-26 13:41:05 +0200 | [diff] [blame] | 190 | efi_install_configuration_table(&fdt_guid, NULL); |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | /* Load the EFI payload */ |
| 194 | entry = efi_load_pe(efi, &loaded_image_info); |
Rob Clark | f8db922 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 195 | if (!entry) { |
| 196 | ret = -ENOENT; |
| 197 | goto exit; |
| 198 | } |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 199 | |
Rob Clark | 18ceba7 | 2017-10-10 08:23:06 -0400 | [diff] [blame] | 200 | if (memdp) { |
| 201 | struct efi_device_path_memory *mdp = (void *)memdp; |
| 202 | mdp->memory_type = loaded_image_info.image_code_type; |
| 203 | mdp->start_address = (uintptr_t)loaded_image_info.image_base; |
| 204 | mdp->end_address = mdp->start_address + |
| 205 | loaded_image_info.image_size; |
| 206 | } |
| 207 | |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 208 | /* we don't support much: */ |
| 209 | env_set("efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_OsIndicationsSupported", |
| 210 | "{ro,boot}(blob)0000000000000000"); |
| 211 | |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 212 | /* Call our payload! */ |
Alexander Graf | 62a6748 | 2016-06-02 11:38:27 +0200 | [diff] [blame] | 213 | debug("%s:%d Jumping to 0x%lx\n", __func__, __LINE__, (long)entry); |
Alexander Graf | 988c066 | 2016-05-20 23:28:23 +0200 | [diff] [blame] | 214 | |
| 215 | if (setjmp(&loaded_image_info.exit_jmp)) { |
Rob Clark | f8db922 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 216 | ret = loaded_image_info.exit_status; |
Rob Clark | f8db922 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 217 | goto exit; |
Alexander Graf | 988c066 | 2016-05-20 23:28:23 +0200 | [diff] [blame] | 218 | } |
| 219 | |
Alexander Graf | bfd199c | 2016-11-17 01:02:58 +0100 | [diff] [blame] | 220 | #ifdef CONFIG_ARM64 |
| 221 | /* On AArch64 we need to make sure we call our payload in < EL3 */ |
| 222 | if (current_el() == 3) { |
| 223 | smp_kick_all_cpus(); |
| 224 | dcache_disable(); /* flush cache before switch to EL2 */ |
Alison Wang | 73818d5 | 2016-11-10 10:49:03 +0800 | [diff] [blame] | 225 | |
| 226 | /* Move into EL2 and keep running there */ |
| 227 | armv8_switch_to_el2((ulong)entry, (ulong)&loaded_image_info, |
Alison Wang | eb2088d | 2017-01-17 09:39:17 +0800 | [diff] [blame] | 228 | (ulong)&systab, 0, (ulong)efi_run_in_el2, |
Alison Wang | 73818d5 | 2016-11-10 10:49:03 +0800 | [diff] [blame] | 229 | ES_TO_AARCH64); |
| 230 | |
| 231 | /* Should never reach here, efi exits with longjmp */ |
| 232 | while (1) { } |
Alexander Graf | bfd199c | 2016-11-17 01:02:58 +0100 | [diff] [blame] | 233 | } |
| 234 | #endif |
| 235 | |
Rob Clark | f8db922 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 236 | ret = efi_do_enter(&loaded_image_info, &systab, entry); |
| 237 | |
| 238 | exit: |
| 239 | /* image has returned, loaded-image obj goes *poof*: */ |
| 240 | list_del(&loaded_image_info_obj.link); |
| 241 | |
| 242 | return ret; |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 243 | } |
| 244 | |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 245 | static int do_bootefi_bootmgr_exec(unsigned long fdt_addr) |
| 246 | { |
| 247 | struct efi_device_path *device_path, *file_path; |
| 248 | void *addr; |
| 249 | efi_status_t r; |
| 250 | |
| 251 | /* Initialize and populate EFI object list */ |
| 252 | if (!efi_obj_list_initalized) |
| 253 | efi_init_obj_list(); |
| 254 | |
| 255 | /* |
| 256 | * gd lives in a fixed register which may get clobbered while we execute |
| 257 | * the payload. So save it here and restore it on every callback entry |
| 258 | */ |
| 259 | efi_save_gd(); |
| 260 | |
| 261 | addr = efi_bootmgr_load(&device_path, &file_path); |
| 262 | if (!addr) |
| 263 | return 1; |
| 264 | |
| 265 | printf("## Starting EFI application at %p ...\n", addr); |
| 266 | r = do_bootefi_exec(addr, (void *)fdt_addr, device_path, file_path); |
| 267 | printf("## Application terminated, r = %lu\n", |
| 268 | r & ~EFI_ERROR_MASK); |
| 269 | |
| 270 | if (r != EFI_SUCCESS) |
| 271 | return 1; |
| 272 | |
| 273 | return 0; |
| 274 | } |
| 275 | |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 276 | /* Interpreter command to boot an arbitrary EFI image from memory */ |
| 277 | static int do_bootefi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 278 | { |
Alexander Graf | 54c1e83 | 2016-04-14 16:07:53 +0200 | [diff] [blame] | 279 | char *saddr, *sfdt; |
| 280 | unsigned long addr, fdt_addr = 0; |
xypron.glpk@gmx.de | 5eadbfa | 2017-07-04 23:15:23 +0200 | [diff] [blame] | 281 | unsigned long r; |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 282 | |
| 283 | if (argc < 2) |
Bin Meng | da800ff | 2016-08-13 04:02:06 -0700 | [diff] [blame] | 284 | return CMD_RET_USAGE; |
Simon Glass | fac4ced | 2016-11-07 08:47:08 -0700 | [diff] [blame] | 285 | #ifdef CONFIG_CMD_BOOTEFI_HELLO |
| 286 | if (!strcmp(argv[1], "hello")) { |
Heinrich Schuchardt | 7804ae9 | 2017-09-05 03:19:37 +0200 | [diff] [blame] | 287 | ulong size = __efi_helloworld_end - __efi_helloworld_begin; |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 288 | |
Heinrich Schuchardt | e6a9ab5 | 2017-08-28 18:54:30 +0200 | [diff] [blame] | 289 | saddr = env_get("loadaddr"); |
| 290 | if (saddr) |
| 291 | addr = simple_strtoul(saddr, NULL, 16); |
| 292 | else |
| 293 | addr = CONFIG_SYS_LOAD_ADDR; |
Heinrich Schuchardt | 7804ae9 | 2017-09-05 03:19:37 +0200 | [diff] [blame] | 294 | memcpy((char *)addr, __efi_helloworld_begin, size); |
Simon Glass | fac4ced | 2016-11-07 08:47:08 -0700 | [diff] [blame] | 295 | } else |
| 296 | #endif |
Heinrich Schuchardt | d33ae3e | 2017-09-15 10:06:11 +0200 | [diff] [blame] | 297 | #ifdef CONFIG_CMD_BOOTEFI_SELFTEST |
| 298 | if (!strcmp(argv[1], "selftest")) { |
Heinrich Schuchardt | 389f251 | 2017-09-20 22:54:58 +0200 | [diff] [blame] | 299 | struct efi_loaded_image loaded_image_info = {}; |
| 300 | struct efi_object loaded_image_info_obj = {}; |
| 301 | |
| 302 | efi_setup_loaded_image(&loaded_image_info, |
| 303 | &loaded_image_info_obj, |
| 304 | bootefi_device_path, bootefi_image_path); |
Heinrich Schuchardt | d33ae3e | 2017-09-15 10:06:11 +0200 | [diff] [blame] | 305 | /* |
| 306 | * gd lives in a fixed register which may get clobbered while we |
| 307 | * execute the payload. So save it here and restore it on every |
| 308 | * callback entry |
| 309 | */ |
| 310 | efi_save_gd(); |
| 311 | /* Initialize and populate EFI object list */ |
| 312 | if (!efi_obj_list_initalized) |
| 313 | efi_init_obj_list(); |
Heinrich Schuchardt | d33ae3e | 2017-09-15 10:06:11 +0200 | [diff] [blame] | 314 | return efi_selftest(&loaded_image_info, &systab); |
| 315 | } else |
| 316 | #endif |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 317 | if (!strcmp(argv[1], "bootmgr")) { |
| 318 | unsigned long fdt_addr = 0; |
| 319 | |
| 320 | if (argc > 2) |
| 321 | fdt_addr = simple_strtoul(argv[2], NULL, 16); |
| 322 | |
| 323 | return do_bootefi_bootmgr_exec(fdt_addr); |
| 324 | } else { |
Simon Glass | fac4ced | 2016-11-07 08:47:08 -0700 | [diff] [blame] | 325 | saddr = argv[1]; |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 326 | |
Simon Glass | fac4ced | 2016-11-07 08:47:08 -0700 | [diff] [blame] | 327 | addr = simple_strtoul(saddr, NULL, 16); |
| 328 | |
| 329 | if (argc > 2) { |
| 330 | sfdt = argv[2]; |
| 331 | fdt_addr = simple_strtoul(sfdt, NULL, 16); |
| 332 | } |
Alexander Graf | 54c1e83 | 2016-04-14 16:07:53 +0200 | [diff] [blame] | 333 | } |
| 334 | |
Simon Glass | 1a5490e | 2016-11-07 08:47:05 -0700 | [diff] [blame] | 335 | printf("## Starting EFI application at %08lx ...\n", addr); |
Rob Clark | f8db922 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 336 | r = do_bootefi_exec((void *)addr, (void *)fdt_addr, |
| 337 | bootefi_device_path, bootefi_image_path); |
xypron.glpk@gmx.de | 5eadbfa | 2017-07-04 23:15:23 +0200 | [diff] [blame] | 338 | printf("## Application terminated, r = %lu\n", |
| 339 | r & ~EFI_ERROR_MASK); |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 340 | |
xypron.glpk@gmx.de | 5eadbfa | 2017-07-04 23:15:23 +0200 | [diff] [blame] | 341 | if (r != EFI_SUCCESS) |
| 342 | return 1; |
| 343 | else |
| 344 | return 0; |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | #ifdef CONFIG_SYS_LONGHELP |
| 348 | static char bootefi_help_text[] = |
Alexander Graf | 54c1e83 | 2016-04-14 16:07:53 +0200 | [diff] [blame] | 349 | "<image address> [fdt address]\n" |
| 350 | " - boot EFI payload stored at address <image address>.\n" |
| 351 | " If specified, the device tree located at <fdt address> gets\n" |
Simon Glass | fac4ced | 2016-11-07 08:47:08 -0700 | [diff] [blame] | 352 | " exposed as EFI configuration table.\n" |
| 353 | #ifdef CONFIG_CMD_BOOTEFI_HELLO |
Heinrich Schuchardt | d33ae3e | 2017-09-15 10:06:11 +0200 | [diff] [blame] | 354 | "bootefi hello\n" |
| 355 | " - boot a sample Hello World application stored within U-Boot\n" |
| 356 | #endif |
| 357 | #ifdef CONFIG_CMD_BOOTEFI_SELFTEST |
| 358 | "bootefi selftest\n" |
| 359 | " - boot an EFI selftest application stored within U-Boot\n" |
Simon Glass | fac4ced | 2016-11-07 08:47:08 -0700 | [diff] [blame] | 360 | #endif |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 361 | "bootmgr [fdt addr]\n" |
| 362 | " - load and boot EFI payload based on BootOrder/BootXXXX variables.\n" |
| 363 | "\n" |
| 364 | " If specified, the device tree located at <fdt address> gets\n" |
| 365 | " exposed as EFI configuration table.\n"; |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 366 | #endif |
| 367 | |
| 368 | U_BOOT_CMD( |
Alexander Graf | 54c1e83 | 2016-04-14 16:07:53 +0200 | [diff] [blame] | 369 | bootefi, 3, 0, do_bootefi, |
Sergey Kubushyn | 268f19e | 2016-06-07 11:14:31 -0700 | [diff] [blame] | 370 | "Boots an EFI payload from memory", |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 371 | bootefi_help_text |
| 372 | ); |
Alexander Graf | 8f19f26 | 2016-03-04 01:10:14 +0100 | [diff] [blame] | 373 | |
Rob Clark | f8db922 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 374 | static int parse_partnum(const char *devnr) |
Alexander Graf | 8f19f26 | 2016-03-04 01:10:14 +0100 | [diff] [blame] | 375 | { |
Rob Clark | f8db922 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 376 | const char *str = strchr(devnr, ':'); |
| 377 | if (str) { |
| 378 | str++; |
| 379 | return simple_strtoul(str, NULL, 16); |
Alexander Graf | 34118e7 | 2016-08-05 14:49:53 +0200 | [diff] [blame] | 380 | } |
Rob Clark | f8db922 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 381 | return 0; |
| 382 | } |
Alexander Graf | eb8d82c | 2016-04-11 16:16:18 +0200 | [diff] [blame] | 383 | |
Rob Clark | f8db922 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 384 | void efi_set_bootdev(const char *dev, const char *devnr, const char *path) |
| 385 | { |
| 386 | char filename[32] = { 0 }; /* dp->str is u16[32] long */ |
| 387 | char *s; |
Alexander Graf | 34118e7 | 2016-08-05 14:49:53 +0200 | [diff] [blame] | 388 | |
Rob Clark | f8db922 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 389 | if (strcmp(dev, "Net")) { |
| 390 | struct blk_desc *desc; |
| 391 | int part; |
Alexander Graf | eb8d82c | 2016-04-11 16:16:18 +0200 | [diff] [blame] | 392 | |
Rob Clark | f8db922 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 393 | desc = blk_get_dev(dev, simple_strtol(devnr, NULL, 10)); |
| 394 | part = parse_partnum(devnr); |
Alexander Graf | 8f19f26 | 2016-03-04 01:10:14 +0100 | [diff] [blame] | 395 | |
Rob Clark | f8db922 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 396 | bootefi_device_path = efi_dp_from_part(desc, part); |
| 397 | } else { |
| 398 | #ifdef CONFIG_NET |
| 399 | bootefi_device_path = efi_dp_from_eth(); |
| 400 | #endif |
| 401 | } |
Alexander Graf | c49a1b8 | 2016-04-11 16:16:19 +0200 | [diff] [blame] | 402 | |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 403 | if (!path) |
| 404 | return; |
| 405 | |
Alexander Graf | 45e437d | 2016-07-21 01:44:46 +0200 | [diff] [blame] | 406 | if (strcmp(dev, "Net")) { |
| 407 | /* Add leading / to fs paths, because they're absolute */ |
Rob Clark | f8db922 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 408 | snprintf(filename, sizeof(filename), "/%s", path); |
Alexander Graf | 45e437d | 2016-07-21 01:44:46 +0200 | [diff] [blame] | 409 | } else { |
Rob Clark | f8db922 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 410 | snprintf(filename, sizeof(filename), "%s", path); |
Alexander Graf | 45e437d | 2016-07-21 01:44:46 +0200 | [diff] [blame] | 411 | } |
Rob Clark | 6533652 | 2017-07-24 07:59:09 -0400 | [diff] [blame] | 412 | /* DOS style file path: */ |
Rob Clark | f8db922 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 413 | s = filename; |
Rob Clark | 6533652 | 2017-07-24 07:59:09 -0400 | [diff] [blame] | 414 | while ((s = strchr(s, '/'))) |
| 415 | *s++ = '\\'; |
Rob Clark | f8db922 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 416 | bootefi_image_path = efi_dp_from_file(NULL, 0, filename); |
Alexander Graf | 8f19f26 | 2016-03-04 01:10:14 +0100 | [diff] [blame] | 417 | } |