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