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