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