Tom Rini | 70df9d6 | 2018-05-07 17:02:21 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 2 | /* |
| 3 | * EFI application loader |
| 4 | * |
| 5 | * Copyright (c) 2016 Alexander Graf |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 6 | */ |
| 7 | |
Heinrich Schuchardt | bb38a1f | 2020-07-17 20:21:00 +0200 | [diff] [blame] | 8 | #define LOG_CATEGORY LOGC_EFI |
| 9 | |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 10 | #include <common.h> |
Heinrich Schuchardt | af64c3e | 2021-01-24 14:34:12 +0000 | [diff] [blame] | 11 | #include <bootm.h> |
Heinrich Schuchardt | aa0b11b | 2019-01-08 18:13:06 +0100 | [diff] [blame] | 12 | #include <charset.h> |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 13 | #include <command.h> |
Simon Glass | 11c89f3 | 2017-05-17 17:18:03 -0600 | [diff] [blame] | 14 | #include <dm.h> |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 15 | #include <efi_loader.h> |
Heinrich Schuchardt | 02efd5d | 2017-10-18 18:13:13 +0200 | [diff] [blame] | 16 | #include <efi_selftest.h> |
Simon Glass | 0af6e2d | 2019-08-01 09:46:52 -0600 | [diff] [blame] | 17 | #include <env.h> |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 18 | #include <errno.h> |
Simon Glass | 2dc9c34 | 2020-05-10 11:40:01 -0600 | [diff] [blame] | 19 | #include <image.h> |
Heinrich Schuchardt | bb38a1f | 2020-07-17 20:21:00 +0200 | [diff] [blame] | 20 | #include <log.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 21 | #include <malloc.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 22 | #include <asm/global_data.h> |
Masahiro Yamada | 75f82d0 | 2018-03-05 01:20:11 +0900 | [diff] [blame] | 23 | #include <linux/libfdt.h> |
| 24 | #include <linux/libfdt_env.h> |
Alexander Graf | a241451 | 2018-06-18 17:22:58 +0200 | [diff] [blame] | 25 | #include <mapmem.h> |
Alexander Graf | fdbae01 | 2016-04-11 23:51:01 +0200 | [diff] [blame] | 26 | #include <memalign.h> |
Simon Glass | c2194bf | 2016-09-25 15:27:32 -0600 | [diff] [blame] | 27 | #include <asm-generic/sections.h> |
| 28 | #include <linux/linkage.h> |
Alexander Graf | 2ff5cc3 | 2016-04-11 16:55:26 +0200 | [diff] [blame] | 29 | |
| 30 | DECLARE_GLOBAL_DATA_PTR; |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 31 | |
AKASHI Takahiro | 203d196 | 2023-11-21 10:29:43 +0900 | [diff] [blame^] | 32 | static struct efi_device_path *test_image_path; |
| 33 | static struct efi_device_path *test_device_path; |
Rob Clark | f8db922 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 34 | static struct efi_device_path *bootefi_image_path; |
| 35 | static struct efi_device_path *bootefi_device_path; |
Heinrich Schuchardt | 6b82159 | 2021-01-12 12:46:24 +0100 | [diff] [blame] | 36 | static void *image_addr; |
| 37 | static size_t image_size; |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 38 | |
Heinrich Schuchardt | 031ea8f | 2019-07-14 13:00:44 +0200 | [diff] [blame] | 39 | /** |
Rui Miguel Silva | 433f15a | 2022-05-11 10:55:40 +0100 | [diff] [blame] | 40 | * efi_get_image_parameters() - return image parameters |
| 41 | * |
| 42 | * @img_addr: address of loaded image in memory |
| 43 | * @img_size: size of loaded image |
| 44 | */ |
| 45 | void efi_get_image_parameters(void **img_addr, size_t *img_size) |
| 46 | { |
| 47 | *img_addr = image_addr; |
| 48 | *img_size = image_size; |
| 49 | } |
| 50 | |
| 51 | /** |
Heinrich Schuchardt | 6b82159 | 2021-01-12 12:46:24 +0100 | [diff] [blame] | 52 | * efi_clear_bootdev() - clear boot device |
| 53 | */ |
| 54 | static void efi_clear_bootdev(void) |
| 55 | { |
| 56 | efi_free_pool(bootefi_device_path); |
| 57 | efi_free_pool(bootefi_image_path); |
| 58 | bootefi_device_path = NULL; |
| 59 | bootefi_image_path = NULL; |
| 60 | image_addr = NULL; |
| 61 | image_size = 0; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * efi_set_bootdev() - set boot device |
| 66 | * |
| 67 | * This function is called when a file is loaded, e.g. via the 'load' command. |
| 68 | * We use the path to this file to inform the UEFI binary about the boot device. |
| 69 | * |
| 70 | * @dev: device, e.g. "MMC" |
| 71 | * @devnr: number of the device, e.g. "1:2" |
| 72 | * @path: path to file loaded |
| 73 | * @buffer: buffer with file loaded |
| 74 | * @buffer_size: size of file loaded |
| 75 | */ |
| 76 | void efi_set_bootdev(const char *dev, const char *devnr, const char *path, |
| 77 | void *buffer, size_t buffer_size) |
| 78 | { |
| 79 | struct efi_device_path *device, *image; |
| 80 | efi_status_t ret; |
| 81 | |
Simon Glass | 95c1d25 | 2022-01-29 14:58:37 -0700 | [diff] [blame] | 82 | log_debug("dev=%s, devnr=%s, path=%s, buffer=%p, size=%zx\n", dev, |
| 83 | devnr, path, buffer, buffer_size); |
| 84 | |
Heinrich Schuchardt | 6b82159 | 2021-01-12 12:46:24 +0100 | [diff] [blame] | 85 | /* Forget overwritten image */ |
| 86 | if (buffer + buffer_size >= image_addr && |
| 87 | image_addr + image_size >= buffer) |
| 88 | efi_clear_bootdev(); |
| 89 | |
| 90 | /* Remember only PE-COFF and FIT images */ |
| 91 | if (efi_check_pe(buffer, buffer_size, NULL) != EFI_SUCCESS) { |
Simon Glass | 95c1d25 | 2022-01-29 14:58:37 -0700 | [diff] [blame] | 92 | if (IS_ENABLED(CONFIG_FIT) && |
| 93 | !fit_check_format(buffer, IMAGE_SIZE_INVAL)) { |
| 94 | /* |
| 95 | * FIT images of type EFI_OS are started via command |
| 96 | * bootm. We should not use their boot device with the |
| 97 | * bootefi command. |
| 98 | */ |
| 99 | buffer = 0; |
| 100 | buffer_size = 0; |
| 101 | } else { |
| 102 | log_debug("- not remembering image\n"); |
Heinrich Schuchardt | 6b82159 | 2021-01-12 12:46:24 +0100 | [diff] [blame] | 103 | return; |
Simon Glass | 95c1d25 | 2022-01-29 14:58:37 -0700 | [diff] [blame] | 104 | } |
Heinrich Schuchardt | 6b82159 | 2021-01-12 12:46:24 +0100 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /* efi_set_bootdev() is typically called repeatedly, recover memory */ |
| 108 | efi_clear_bootdev(); |
| 109 | |
| 110 | image_addr = buffer; |
| 111 | image_size = buffer_size; |
| 112 | |
| 113 | ret = efi_dp_from_name(dev, devnr, path, &device, &image); |
| 114 | if (ret == EFI_SUCCESS) { |
| 115 | bootefi_device_path = device; |
| 116 | if (image) { |
| 117 | /* FIXME: image should not contain device */ |
| 118 | struct efi_device_path *image_tmp = image; |
| 119 | |
| 120 | efi_dp_split_file_path(image, &device, &image); |
| 121 | efi_free_pool(image_tmp); |
| 122 | } |
| 123 | bootefi_image_path = image; |
Heinrich Schuchardt | b274e77 | 2022-07-10 15:46:57 +0200 | [diff] [blame] | 124 | log_debug("- boot device %pD\n", device); |
Simon Glass | 95c1d25 | 2022-01-29 14:58:37 -0700 | [diff] [blame] | 125 | if (image) |
Heinrich Schuchardt | b274e77 | 2022-07-10 15:46:57 +0200 | [diff] [blame] | 126 | log_debug("- image %pD\n", image); |
Heinrich Schuchardt | 6b82159 | 2021-01-12 12:46:24 +0100 | [diff] [blame] | 127 | } else { |
Simon Glass | 95c1d25 | 2022-01-29 14:58:37 -0700 | [diff] [blame] | 128 | log_debug("- efi_dp_from_name() failed, err=%lx\n", ret); |
Heinrich Schuchardt | 6b82159 | 2021-01-12 12:46:24 +0100 | [diff] [blame] | 129 | efi_clear_bootdev(); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | /** |
Heinrich Schuchardt | 25c6be5 | 2020-08-07 17:47:13 +0200 | [diff] [blame] | 134 | * efi_env_set_load_options() - set load options from environment variable |
Heinrich Schuchardt | 02efd5d | 2017-10-18 18:13:13 +0200 | [diff] [blame] | 135 | * |
Heinrich Schuchardt | e626395 | 2020-01-03 22:53:42 +0100 | [diff] [blame] | 136 | * @handle: the image handle |
| 137 | * @env_var: name of the environment variable |
| 138 | * @load_options: pointer to load options (output) |
| 139 | * Return: status code |
Heinrich Schuchardt | 02efd5d | 2017-10-18 18:13:13 +0200 | [diff] [blame] | 140 | */ |
Heinrich Schuchardt | 25c6be5 | 2020-08-07 17:47:13 +0200 | [diff] [blame] | 141 | static efi_status_t efi_env_set_load_options(efi_handle_t handle, |
| 142 | const char *env_var, |
| 143 | u16 **load_options) |
Heinrich Schuchardt | 02efd5d | 2017-10-18 18:13:13 +0200 | [diff] [blame] | 144 | { |
Heinrich Schuchardt | 02efd5d | 2017-10-18 18:13:13 +0200 | [diff] [blame] | 145 | const char *env = env_get(env_var); |
Heinrich Schuchardt | 25c6be5 | 2020-08-07 17:47:13 +0200 | [diff] [blame] | 146 | size_t size; |
Heinrich Schuchardt | de52780 | 2018-08-31 21:31:33 +0200 | [diff] [blame] | 147 | u16 *pos; |
AKASHI Takahiro | e25a7b8 | 2019-04-19 12:22:28 +0900 | [diff] [blame] | 148 | efi_status_t ret; |
| 149 | |
Heinrich Schuchardt | e626395 | 2020-01-03 22:53:42 +0100 | [diff] [blame] | 150 | *load_options = NULL; |
Heinrich Schuchardt | 02efd5d | 2017-10-18 18:13:13 +0200 | [diff] [blame] | 151 | if (!env) |
Heinrich Schuchardt | 25c6be5 | 2020-08-07 17:47:13 +0200 | [diff] [blame] | 152 | return EFI_SUCCESS; |
| 153 | size = sizeof(u16) * (utf8_utf16_strlen(env) + 1); |
| 154 | pos = calloc(size, 1); |
| 155 | if (!pos) |
AKASHI Takahiro | e25a7b8 | 2019-04-19 12:22:28 +0900 | [diff] [blame] | 156 | return EFI_OUT_OF_RESOURCES; |
Heinrich Schuchardt | e626395 | 2020-01-03 22:53:42 +0100 | [diff] [blame] | 157 | *load_options = pos; |
Heinrich Schuchardt | de52780 | 2018-08-31 21:31:33 +0200 | [diff] [blame] | 158 | utf8_utf16_strcpy(&pos, env); |
Heinrich Schuchardt | 25c6be5 | 2020-08-07 17:47:13 +0200 | [diff] [blame] | 159 | ret = efi_set_load_options(handle, size, *load_options); |
| 160 | if (ret != EFI_SUCCESS) { |
| 161 | free(*load_options); |
| 162 | *load_options = NULL; |
| 163 | } |
| 164 | return ret; |
Heinrich Schuchardt | 02efd5d | 2017-10-18 18:13:13 +0200 | [diff] [blame] | 165 | } |
| 166 | |
Heinrich Schuchardt | 4420a33 | 2019-04-20 13:33:55 +0200 | [diff] [blame] | 167 | #if !CONFIG_IS_ENABLED(GENERATE_ACPI_TABLE) |
| 168 | |
Simon Glass | 5d618df | 2018-08-08 03:54:30 -0600 | [diff] [blame] | 169 | /** |
| 170 | * copy_fdt() - Copy the device tree to a new location available to EFI |
| 171 | * |
Heinrich Schuchardt | b23f9b7 | 2018-11-18 17:58:52 +0100 | [diff] [blame] | 172 | * The FDT is copied to a suitable location within the EFI memory map. |
Heinrich Schuchardt | dcdca29 | 2018-11-18 17:58:49 +0100 | [diff] [blame] | 173 | * Additional 12 KiB are added to the space in case the device tree needs to be |
Simon Glass | 5d618df | 2018-08-08 03:54:30 -0600 | [diff] [blame] | 174 | * expanded later with fdt_open_into(). |
| 175 | * |
Heinrich Schuchardt | b23f9b7 | 2018-11-18 17:58:52 +0100 | [diff] [blame] | 176 | * @fdtp: On entry a pointer to the flattened device tree. |
| 177 | * On exit a pointer to the copy of the flattened device tree. |
Heinrich Schuchardt | 96af56e | 2018-11-12 18:55:22 +0100 | [diff] [blame] | 178 | * FDT start |
| 179 | * Return: status code |
Simon Glass | 5d618df | 2018-08-08 03:54:30 -0600 | [diff] [blame] | 180 | */ |
Heinrich Schuchardt | b23f9b7 | 2018-11-18 17:58:52 +0100 | [diff] [blame] | 181 | static efi_status_t copy_fdt(void **fdtp) |
Alexander Graf | 2ff5cc3 | 2016-04-11 16:55:26 +0200 | [diff] [blame] | 182 | { |
Alexander Graf | fdbae01 | 2016-04-11 23:51:01 +0200 | [diff] [blame] | 183 | unsigned long fdt_ram_start = -1L, fdt_pages; |
Simon Glass | 5d618df | 2018-08-08 03:54:30 -0600 | [diff] [blame] | 184 | efi_status_t ret = 0; |
| 185 | void *fdt, *new_fdt; |
Alexander Graf | fdbae01 | 2016-04-11 23:51:01 +0200 | [diff] [blame] | 186 | u64 new_fdt_addr; |
Simon Glass | 5d618df | 2018-08-08 03:54:30 -0600 | [diff] [blame] | 187 | uint fdt_size; |
Alexander Graf | fdbae01 | 2016-04-11 23:51:01 +0200 | [diff] [blame] | 188 | int i; |
Alexander Graf | 2ff5cc3 | 2016-04-11 16:55:26 +0200 | [diff] [blame] | 189 | |
Simon Glass | 5d618df | 2018-08-08 03:54:30 -0600 | [diff] [blame] | 190 | for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) { |
| 191 | u64 ram_start = gd->bd->bi_dram[i].start; |
| 192 | u64 ram_size = gd->bd->bi_dram[i].size; |
Alexander Graf | 2ff5cc3 | 2016-04-11 16:55:26 +0200 | [diff] [blame] | 193 | |
Alexander Graf | fdbae01 | 2016-04-11 23:51:01 +0200 | [diff] [blame] | 194 | if (!ram_size) |
| 195 | continue; |
| 196 | |
| 197 | if (ram_start < fdt_ram_start) |
| 198 | fdt_ram_start = ram_start; |
| 199 | } |
| 200 | |
Simon Glass | 56bbcc1 | 2018-06-18 08:08:25 -0600 | [diff] [blame] | 201 | /* |
Heinrich Schuchardt | dcdca29 | 2018-11-18 17:58:49 +0100 | [diff] [blame] | 202 | * Give us at least 12 KiB of breathing room in case the device tree |
| 203 | * needs to be expanded later. |
Simon Glass | 56bbcc1 | 2018-06-18 08:08:25 -0600 | [diff] [blame] | 204 | */ |
Heinrich Schuchardt | b23f9b7 | 2018-11-18 17:58:52 +0100 | [diff] [blame] | 205 | fdt = *fdtp; |
Heinrich Schuchardt | dcdca29 | 2018-11-18 17:58:49 +0100 | [diff] [blame] | 206 | fdt_pages = efi_size_in_pages(fdt_totalsize(fdt) + 0x3000); |
| 207 | fdt_size = fdt_pages << EFI_PAGE_SHIFT; |
Alexander Graf | fdbae01 | 2016-04-11 23:51:01 +0200 | [diff] [blame] | 208 | |
Heinrich Schuchardt | 45a755d | 2023-02-23 20:27:38 +0100 | [diff] [blame] | 209 | ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, |
Heinrich Schuchardt | e00764b | 2020-05-06 20:32:31 +0200 | [diff] [blame] | 210 | EFI_ACPI_RECLAIM_MEMORY, fdt_pages, |
Simon Glass | 5d618df | 2018-08-08 03:54:30 -0600 | [diff] [blame] | 211 | &new_fdt_addr); |
| 212 | if (ret != EFI_SUCCESS) { |
Heinrich Schuchardt | 45a755d | 2023-02-23 20:27:38 +0100 | [diff] [blame] | 213 | log_err("ERROR: Failed to reserve space for FDT\n"); |
| 214 | goto done; |
Alexander Graf | fdbae01 | 2016-04-11 23:51:01 +0200 | [diff] [blame] | 215 | } |
Heinrich Schuchardt | b23f9b7 | 2018-11-18 17:58:52 +0100 | [diff] [blame] | 216 | new_fdt = (void *)(uintptr_t)new_fdt_addr; |
Alexander Graf | 2ff5cc3 | 2016-04-11 16:55:26 +0200 | [diff] [blame] | 217 | memcpy(new_fdt, fdt, fdt_totalsize(fdt)); |
| 218 | fdt_set_totalsize(new_fdt, fdt_size); |
| 219 | |
Heinrich Schuchardt | b23f9b7 | 2018-11-18 17:58:52 +0100 | [diff] [blame] | 220 | *fdtp = (void *)(uintptr_t)new_fdt_addr; |
Simon Glass | 5d618df | 2018-08-08 03:54:30 -0600 | [diff] [blame] | 221 | done: |
| 222 | return ret; |
Alexander Graf | 2ff5cc3 | 2016-04-11 16:55:26 +0200 | [diff] [blame] | 223 | } |
| 224 | |
Heinrich Schuchardt | 928de7d | 2020-08-27 12:52:20 +0200 | [diff] [blame] | 225 | /** |
Heinrich Schuchardt | 4420a33 | 2019-04-20 13:33:55 +0200 | [diff] [blame] | 226 | * get_config_table() - get configuration table |
| 227 | * |
| 228 | * @guid: GUID of the configuration table |
| 229 | * Return: pointer to configuration table or NULL |
| 230 | */ |
| 231 | static void *get_config_table(const efi_guid_t *guid) |
| 232 | { |
| 233 | size_t i; |
| 234 | |
| 235 | for (i = 0; i < systab.nr_tables; i++) { |
| 236 | if (!guidcmp(guid, &systab.tables[i].guid)) |
| 237 | return systab.tables[i].table; |
| 238 | } |
| 239 | return NULL; |
Alexander Graf | 22c88bf | 2018-04-06 09:40:51 +0200 | [diff] [blame] | 240 | } |
| 241 | |
Heinrich Schuchardt | 4420a33 | 2019-04-20 13:33:55 +0200 | [diff] [blame] | 242 | #endif /* !CONFIG_IS_ENABLED(GENERATE_ACPI_TABLE) */ |
| 243 | |
AKASHI Takahiro | cc02f17 | 2019-04-19 12:22:29 +0900 | [diff] [blame] | 244 | /** |
Heinrich Schuchardt | 6fbb8ac | 2019-11-28 06:46:09 +0100 | [diff] [blame] | 245 | * efi_install_fdt() - install device tree |
Heinrich Schuchardt | a3086cd | 2019-05-12 20:16:25 +0200 | [diff] [blame] | 246 | * |
Heinrich Schuchardt | b112ac2 | 2020-02-07 22:10:49 +0100 | [diff] [blame] | 247 | * If fdt is not EFI_FDT_USE_INTERNAL, the device tree located at that memory |
| 248 | * address will will be installed as configuration table, otherwise the device |
| 249 | * tree located at the address indicated by environment variable fdt_addr or as |
| 250 | * fallback fdtcontroladdr will be used. |
Heinrich Schuchardt | a3086cd | 2019-05-12 20:16:25 +0200 | [diff] [blame] | 251 | * |
Heinrich Schuchardt | 6fbb8ac | 2019-11-28 06:46:09 +0100 | [diff] [blame] | 252 | * On architectures using ACPI tables device trees shall not be installed as |
| 253 | * configuration table. |
Heinrich Schuchardt | a3086cd | 2019-05-12 20:16:25 +0200 | [diff] [blame] | 254 | * |
Heinrich Schuchardt | b112ac2 | 2020-02-07 22:10:49 +0100 | [diff] [blame] | 255 | * @fdt: address of device tree or EFI_FDT_USE_INTERNAL to use the |
Heinrich Schuchardt | e478ad5 | 2019-12-04 12:31:12 +0100 | [diff] [blame] | 256 | * the hardware device tree as indicated by environment variable |
| 257 | * fdt_addr or as fallback the internal device tree as indicated by |
| 258 | * the environment variable fdtcontroladdr |
AKASHI Takahiro | cc02f17 | 2019-04-19 12:22:29 +0900 | [diff] [blame] | 259 | * Return: status code |
AKASHI Takahiro | cc02f17 | 2019-04-19 12:22:29 +0900 | [diff] [blame] | 260 | */ |
Heinrich Schuchardt | 6246741 | 2019-12-08 01:07:01 +0100 | [diff] [blame] | 261 | efi_status_t efi_install_fdt(void *fdt) |
AKASHI Takahiro | cc02f17 | 2019-04-19 12:22:29 +0900 | [diff] [blame] | 262 | { |
Heinrich Schuchardt | 4420a33 | 2019-04-20 13:33:55 +0200 | [diff] [blame] | 263 | /* |
| 264 | * The EBBR spec requires that we have either an FDT or an ACPI table |
| 265 | * but not both. |
| 266 | */ |
| 267 | #if CONFIG_IS_ENABLED(GENERATE_ACPI_TABLE) |
Heinrich Schuchardt | 6246741 | 2019-12-08 01:07:01 +0100 | [diff] [blame] | 268 | if (fdt) { |
Alexander Graf | 0899497 | 2022-02-27 13:18:56 +0100 | [diff] [blame] | 269 | log_warning("WARNING: Can't have ACPI table and device tree - ignoring DT.\n"); |
| 270 | return EFI_SUCCESS; |
Heinrich Schuchardt | 4420a33 | 2019-04-20 13:33:55 +0200 | [diff] [blame] | 271 | } |
| 272 | #else |
Simon Glass | df00afa | 2022-09-06 20:26:50 -0600 | [diff] [blame] | 273 | struct bootm_headers img = { 0 }; |
AKASHI Takahiro | cc02f17 | 2019-04-19 12:22:29 +0900 | [diff] [blame] | 274 | efi_status_t ret; |
| 275 | |
Heinrich Schuchardt | 6246741 | 2019-12-08 01:07:01 +0100 | [diff] [blame] | 276 | if (fdt == EFI_FDT_USE_INTERNAL) { |
Heinrich Schuchardt | 6fbb8ac | 2019-11-28 06:46:09 +0100 | [diff] [blame] | 277 | const char *fdt_opt; |
Heinrich Schuchardt | 6246741 | 2019-12-08 01:07:01 +0100 | [diff] [blame] | 278 | uintptr_t fdt_addr; |
Heinrich Schuchardt | 6fbb8ac | 2019-11-28 06:46:09 +0100 | [diff] [blame] | 279 | |
Heinrich Schuchardt | 4420a33 | 2019-04-20 13:33:55 +0200 | [diff] [blame] | 280 | /* Look for device tree that is already installed */ |
| 281 | if (get_config_table(&efi_guid_fdt)) |
| 282 | return EFI_SUCCESS; |
Heinrich Schuchardt | e478ad5 | 2019-12-04 12:31:12 +0100 | [diff] [blame] | 283 | /* Check if there is a hardware device tree */ |
| 284 | fdt_opt = env_get("fdt_addr"); |
| 285 | /* Use our own device tree as fallback */ |
Heinrich Schuchardt | 4420a33 | 2019-04-20 13:33:55 +0200 | [diff] [blame] | 286 | if (!fdt_opt) { |
Heinrich Schuchardt | e478ad5 | 2019-12-04 12:31:12 +0100 | [diff] [blame] | 287 | fdt_opt = env_get("fdtcontroladdr"); |
| 288 | if (!fdt_opt) { |
Heinrich Schuchardt | bb38a1f | 2020-07-17 20:21:00 +0200 | [diff] [blame] | 289 | log_err("ERROR: need device tree\n"); |
Heinrich Schuchardt | e478ad5 | 2019-12-04 12:31:12 +0100 | [diff] [blame] | 290 | return EFI_NOT_FOUND; |
| 291 | } |
Heinrich Schuchardt | 4420a33 | 2019-04-20 13:33:55 +0200 | [diff] [blame] | 292 | } |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 293 | fdt_addr = hextoul(fdt_opt, NULL); |
Heinrich Schuchardt | 4420a33 | 2019-04-20 13:33:55 +0200 | [diff] [blame] | 294 | if (!fdt_addr) { |
Heinrich Schuchardt | bb38a1f | 2020-07-17 20:21:00 +0200 | [diff] [blame] | 295 | log_err("ERROR: invalid $fdt_addr or $fdtcontroladdr\n"); |
Heinrich Schuchardt | 4420a33 | 2019-04-20 13:33:55 +0200 | [diff] [blame] | 296 | return EFI_LOAD_ERROR; |
AKASHI Takahiro | 451e991 | 2019-04-19 12:22:30 +0900 | [diff] [blame] | 297 | } |
Heinrich Schuchardt | 6246741 | 2019-12-08 01:07:01 +0100 | [diff] [blame] | 298 | fdt = map_sysmem(fdt_addr, 0); |
Heinrich Schuchardt | 4420a33 | 2019-04-20 13:33:55 +0200 | [diff] [blame] | 299 | } |
AKASHI Takahiro | 451e991 | 2019-04-19 12:22:30 +0900 | [diff] [blame] | 300 | |
Heinrich Schuchardt | 4420a33 | 2019-04-20 13:33:55 +0200 | [diff] [blame] | 301 | /* Install device tree */ |
Heinrich Schuchardt | 4420a33 | 2019-04-20 13:33:55 +0200 | [diff] [blame] | 302 | if (fdt_check_header(fdt)) { |
Heinrich Schuchardt | bb38a1f | 2020-07-17 20:21:00 +0200 | [diff] [blame] | 303 | log_err("ERROR: invalid device tree\n"); |
Heinrich Schuchardt | 4420a33 | 2019-04-20 13:33:55 +0200 | [diff] [blame] | 304 | return EFI_LOAD_ERROR; |
| 305 | } |
AKASHI Takahiro | 451e991 | 2019-04-19 12:22:30 +0900 | [diff] [blame] | 306 | |
Heinrich Schuchardt | 4420a33 | 2019-04-20 13:33:55 +0200 | [diff] [blame] | 307 | /* Prepare device tree for payload */ |
| 308 | ret = copy_fdt(&fdt); |
| 309 | if (ret) { |
Heinrich Schuchardt | bb38a1f | 2020-07-17 20:21:00 +0200 | [diff] [blame] | 310 | log_err("ERROR: out of memory\n"); |
Heinrich Schuchardt | 4420a33 | 2019-04-20 13:33:55 +0200 | [diff] [blame] | 311 | return EFI_OUT_OF_RESOURCES; |
| 312 | } |
AKASHI Takahiro | 451e991 | 2019-04-19 12:22:30 +0900 | [diff] [blame] | 313 | |
Simon Glass | 30762dd | 2023-11-12 08:27:44 -0700 | [diff] [blame] | 314 | if (image_setup_libfdt(&img, fdt, NULL)) { |
Heinrich Schuchardt | bb38a1f | 2020-07-17 20:21:00 +0200 | [diff] [blame] | 315 | log_err("ERROR: failed to process device tree\n"); |
Heinrich Schuchardt | 4420a33 | 2019-04-20 13:33:55 +0200 | [diff] [blame] | 316 | return EFI_LOAD_ERROR; |
| 317 | } |
| 318 | |
Heinrich Schuchardt | 74811c8 | 2020-03-14 10:59:34 +0100 | [diff] [blame] | 319 | /* Create memory reservations as indicated by the device tree */ |
| 320 | efi_carve_out_dt_rsv(fdt); |
| 321 | |
Ilias Apalodimas | e4e5660 | 2022-01-03 14:07:37 +0200 | [diff] [blame] | 322 | efi_try_purge_kaslr_seed(fdt); |
| 323 | |
Etienne Carriere | b906435 | 2023-02-16 17:29:48 +0100 | [diff] [blame] | 324 | if (CONFIG_IS_ENABLED(EFI_TCG2_PROTOCOL_MEASURE_DTB)) { |
| 325 | ret = efi_tcg2_measure_dtb(fdt); |
| 326 | if (ret == EFI_SECURITY_VIOLATION) { |
| 327 | log_err("ERROR: failed to measure DTB\n"); |
| 328 | return ret; |
| 329 | } |
| 330 | } |
| 331 | |
Heinrich Schuchardt | 4420a33 | 2019-04-20 13:33:55 +0200 | [diff] [blame] | 332 | /* Install device tree as UEFI table */ |
| 333 | ret = efi_install_configuration_table(&efi_guid_fdt, fdt); |
| 334 | if (ret != EFI_SUCCESS) { |
Heinrich Schuchardt | bb38a1f | 2020-07-17 20:21:00 +0200 | [diff] [blame] | 335 | log_err("ERROR: failed to install device tree\n"); |
Heinrich Schuchardt | 4420a33 | 2019-04-20 13:33:55 +0200 | [diff] [blame] | 336 | return ret; |
AKASHI Takahiro | cc02f17 | 2019-04-19 12:22:29 +0900 | [diff] [blame] | 337 | } |
Heinrich Schuchardt | 4420a33 | 2019-04-20 13:33:55 +0200 | [diff] [blame] | 338 | #endif /* GENERATE_ACPI_TABLE */ |
AKASHI Takahiro | cc02f17 | 2019-04-19 12:22:29 +0900 | [diff] [blame] | 339 | |
| 340 | return EFI_SUCCESS; |
| 341 | } |
| 342 | |
Simon Glass | 4d77ee6 | 2018-11-25 20:14:39 -0700 | [diff] [blame] | 343 | /** |
Heinrich Schuchardt | 3c3a735 | 2018-09-23 17:21:51 +0200 | [diff] [blame] | 344 | * do_bootefi_exec() - execute EFI binary |
| 345 | * |
Heinrich Schuchardt | aa4a66c | 2020-08-15 23:10:22 +0200 | [diff] [blame] | 346 | * The image indicated by @handle is started. When it returns the allocated |
| 347 | * memory for the @load_options is freed. |
| 348 | * |
AKASHI Takahiro | 14ff23b | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 349 | * @handle: handle of loaded image |
Heinrich Schuchardt | aa4a66c | 2020-08-15 23:10:22 +0200 | [diff] [blame] | 350 | * @load_options: load options |
Heinrich Schuchardt | 3c3a735 | 2018-09-23 17:21:51 +0200 | [diff] [blame] | 351 | * Return: status code |
| 352 | * |
| 353 | * Load the EFI binary into a newly assigned memory unwinding the relocation |
| 354 | * information, install the loaded image protocol, and call the binary. |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 355 | */ |
Heinrich Schuchardt | a7647a7 | 2020-08-07 17:49:39 +0200 | [diff] [blame] | 356 | static efi_status_t do_bootefi_exec(efi_handle_t handle, void *load_options) |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 357 | { |
Heinrich Schuchardt | 4b055a2 | 2018-03-03 15:29:01 +0100 | [diff] [blame] | 358 | efi_status_t ret; |
Heinrich Schuchardt | 3d44512 | 2019-04-30 17:57:30 +0200 | [diff] [blame] | 359 | efi_uintn_t exit_data_size = 0; |
| 360 | u16 *exit_data = NULL; |
Masahisa Kojima | 1923f36 | 2023-11-10 13:25:39 +0900 | [diff] [blame] | 361 | struct efi_event *evt; |
Rob Clark | 18ceba7 | 2017-10-10 08:23:06 -0400 | [diff] [blame] | 362 | |
Heinrich Schuchardt | af64c3e | 2021-01-24 14:34:12 +0000 | [diff] [blame] | 363 | /* On ARM switch from EL3 or secure mode to EL2 or non-secure mode */ |
| 364 | switch_to_non_secure_mode(); |
| 365 | |
Masahisa Kojima | d250a8f | 2022-02-22 09:58:30 +0900 | [diff] [blame] | 366 | /* |
| 367 | * The UEFI standard requires that the watchdog timer is set to five |
| 368 | * minutes when invoking an EFI boot option. |
| 369 | * |
| 370 | * Unified Extensible Firmware Interface (UEFI), version 2.7 Errata A |
| 371 | * 7.5. Miscellaneous Boot Services - EFI_BOOT_SERVICES.SetWatchdogTimer |
| 372 | */ |
| 373 | ret = efi_set_watchdog(300); |
| 374 | if (ret != EFI_SUCCESS) { |
| 375 | log_err("ERROR: Failed to set watchdog timer\n"); |
| 376 | goto out; |
| 377 | } |
| 378 | |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 379 | /* Call our payload! */ |
Heinrich Schuchardt | 3d44512 | 2019-04-30 17:57:30 +0200 | [diff] [blame] | 380 | ret = EFI_CALL(efi_start_image(handle, &exit_data_size, &exit_data)); |
Heinrich Schuchardt | bb38a1f | 2020-07-17 20:21:00 +0200 | [diff] [blame] | 381 | if (ret != EFI_SUCCESS) { |
| 382 | log_err("## Application failed, r = %lu\n", |
| 383 | ret & ~EFI_ERROR_MASK); |
| 384 | if (exit_data) { |
| 385 | log_err("## %ls\n", exit_data); |
| 386 | efi_free_pool(exit_data); |
| 387 | } |
Heinrich Schuchardt | 3d44512 | 2019-04-30 17:57:30 +0200 | [diff] [blame] | 388 | } |
Rob Clark | f8db922 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 389 | |
AKASHI Takahiro | 09a81c7 | 2019-04-19 12:22:31 +0900 | [diff] [blame] | 390 | efi_restore_gd(); |
Simon Glass | 4d77ee6 | 2018-11-25 20:14:39 -0700 | [diff] [blame] | 391 | |
Masahisa Kojima | d250a8f | 2022-02-22 09:58:30 +0900 | [diff] [blame] | 392 | out: |
Heinrich Schuchardt | e626395 | 2020-01-03 22:53:42 +0100 | [diff] [blame] | 393 | free(load_options); |
Rob Clark | f8db922 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 394 | |
Ilias Apalodimas | 7e05279 | 2022-10-16 11:36:32 +0300 | [diff] [blame] | 395 | if (IS_ENABLED(CONFIG_EFI_LOAD_FILE2_INITRD)) { |
| 396 | if (efi_initrd_deregister() != EFI_SUCCESS) |
| 397 | log_err("Failed to remove loadfile2 for initrd\n"); |
| 398 | } |
Ilias Apalodimas | b307e3d | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 399 | |
Masahisa Kojima | 1923f36 | 2023-11-10 13:25:39 +0900 | [diff] [blame] | 400 | /* Notify EFI_EVENT_GROUP_RETURN_TO_EFIBOOTMGR event group. */ |
| 401 | list_for_each_entry(evt, &efi_events, link) { |
| 402 | if (evt->group && |
| 403 | !guidcmp(evt->group, |
| 404 | &efi_guid_event_group_return_to_efibootmgr)) { |
| 405 | efi_signal_event(evt); |
| 406 | EFI_CALL(systab.boottime->close_event(evt)); |
| 407 | break; |
| 408 | } |
| 409 | } |
| 410 | |
Masahisa Kojima | d250a8f | 2022-02-22 09:58:30 +0900 | [diff] [blame] | 411 | /* Control is returned to U-Boot, disable EFI watchdog */ |
| 412 | efi_set_watchdog(0); |
| 413 | |
Rob Clark | f8db922 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 414 | return ret; |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 415 | } |
| 416 | |
AKASHI Takahiro | cf81983 | 2019-04-19 12:22:33 +0900 | [diff] [blame] | 417 | /** |
AKASHI Takahiro | 4ab6c5b | 2023-11-21 10:29:41 +0900 | [diff] [blame] | 418 | * efi_bootmgr_run() - execute EFI boot manager |
| 419 | * fdt: Flat device tree |
| 420 | * |
| 421 | * Invoke EFI boot manager and execute a binary depending on |
| 422 | * boot options. If @fdt is not NULL, it will be passed to |
| 423 | * the executed binary. |
AKASHI Takahiro | cf81983 | 2019-04-19 12:22:33 +0900 | [diff] [blame] | 424 | * |
AKASHI Takahiro | cf81983 | 2019-04-19 12:22:33 +0900 | [diff] [blame] | 425 | * Return: status code |
AKASHI Takahiro | cf81983 | 2019-04-19 12:22:33 +0900 | [diff] [blame] | 426 | */ |
AKASHI Takahiro | 4ab6c5b | 2023-11-21 10:29:41 +0900 | [diff] [blame] | 427 | static efi_status_t efi_bootmgr_run(void *fdt) |
AKASHI Takahiro | 758733d | 2019-04-19 12:22:32 +0900 | [diff] [blame] | 428 | { |
AKASHI Takahiro | 14ff23b | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 429 | efi_handle_t handle; |
Heinrich Schuchardt | a7647a7 | 2020-08-07 17:49:39 +0200 | [diff] [blame] | 430 | void *load_options; |
AKASHI Takahiro | 4ab6c5b | 2023-11-21 10:29:41 +0900 | [diff] [blame] | 431 | efi_status_t ret; |
AKASHI Takahiro | cf81983 | 2019-04-19 12:22:33 +0900 | [diff] [blame] | 432 | |
AKASHI Takahiro | 4ab6c5b | 2023-11-21 10:29:41 +0900 | [diff] [blame] | 433 | /* Initialize EFI drivers */ |
| 434 | ret = efi_init_obj_list(); |
AKASHI Takahiro | 14ff23b | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 435 | if (ret != EFI_SUCCESS) { |
AKASHI Takahiro | 4ab6c5b | 2023-11-21 10:29:41 +0900 | [diff] [blame] | 436 | log_err("Error: Cannot initialize UEFI sub-system, r = %lu\n", |
| 437 | ret & ~EFI_ERROR_MASK); |
AKASHI Takahiro | 14ff23b | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 438 | return CMD_RET_FAILURE; |
| 439 | } |
AKASHI Takahiro | 758733d | 2019-04-19 12:22:32 +0900 | [diff] [blame] | 440 | |
AKASHI Takahiro | 4ab6c5b | 2023-11-21 10:29:41 +0900 | [diff] [blame] | 441 | ret = efi_install_fdt(fdt); |
AKASHI Takahiro | cf81983 | 2019-04-19 12:22:33 +0900 | [diff] [blame] | 442 | if (ret != EFI_SUCCESS) |
AKASHI Takahiro | 4ab6c5b | 2023-11-21 10:29:41 +0900 | [diff] [blame] | 443 | return ret; |
| 444 | |
| 445 | ret = efi_bootmgr_load(&handle, &load_options); |
| 446 | if (ret != EFI_SUCCESS) { |
| 447 | log_notice("EFI boot manager: Cannot load any image\n"); |
| 448 | return ret; |
| 449 | } |
AKASHI Takahiro | 758733d | 2019-04-19 12:22:32 +0900 | [diff] [blame] | 450 | |
AKASHI Takahiro | 4ab6c5b | 2023-11-21 10:29:41 +0900 | [diff] [blame] | 451 | return do_bootefi_exec(handle, load_options); |
AKASHI Takahiro | 758733d | 2019-04-19 12:22:32 +0900 | [diff] [blame] | 452 | } |
| 453 | |
Heinrich Schuchardt | 031ea8f | 2019-07-14 13:00:44 +0200 | [diff] [blame] | 454 | /** |
Heinrich Schuchardt | ffb9059 | 2019-12-07 20:51:06 +0100 | [diff] [blame] | 455 | * efi_run_image() - run loaded UEFI image |
| 456 | * |
| 457 | * @source_buffer: memory address of the UEFI image |
| 458 | * @source_size: size of the UEFI image |
| 459 | * Return: status code |
| 460 | */ |
| 461 | efi_status_t efi_run_image(void *source_buffer, efi_uintn_t source_size) |
| 462 | { |
| 463 | efi_handle_t mem_handle = NULL, handle; |
| 464 | struct efi_device_path *file_path = NULL; |
Heinrich Schuchardt | c3a9c9c | 2020-08-25 17:54:05 +0000 | [diff] [blame] | 465 | struct efi_device_path *msg_path; |
Ilias Apalodimas | 3e2c20f | 2022-10-06 16:08:44 +0300 | [diff] [blame] | 466 | efi_status_t ret, ret2; |
Heinrich Schuchardt | c3a9c9c | 2020-08-25 17:54:05 +0000 | [diff] [blame] | 467 | u16 *load_options; |
Heinrich Schuchardt | ffb9059 | 2019-12-07 20:51:06 +0100 | [diff] [blame] | 468 | |
| 469 | if (!bootefi_device_path || !bootefi_image_path) { |
Simon Glass | 95c1d25 | 2022-01-29 14:58:37 -0700 | [diff] [blame] | 470 | log_debug("Not loaded from disk\n"); |
AKASHI Takahiro | 14ff23b | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 471 | /* |
| 472 | * Special case for efi payload not loaded from disk, |
| 473 | * such as 'bootefi hello' or for example payload |
| 474 | * loaded directly into memory via JTAG, etc: |
| 475 | */ |
| 476 | file_path = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE, |
Heinrich Schuchardt | ffb9059 | 2019-12-07 20:51:06 +0100 | [diff] [blame] | 477 | (uintptr_t)source_buffer, |
| 478 | source_size); |
AKASHI Takahiro | 14ff23b | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 479 | /* |
| 480 | * Make sure that device for device_path exist |
| 481 | * in load_image(). Otherwise, shell and grub will fail. |
| 482 | */ |
Ilias Apalodimas | 3e2c20f | 2022-10-06 16:08:44 +0300 | [diff] [blame] | 483 | ret = efi_install_multiple_protocol_interfaces(&mem_handle, |
| 484 | &efi_guid_device_path, |
| 485 | file_path, NULL); |
AKASHI Takahiro | 14ff23b | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 486 | if (ret != EFI_SUCCESS) |
| 487 | goto out; |
Heinrich Schuchardt | c3a9c9c | 2020-08-25 17:54:05 +0000 | [diff] [blame] | 488 | msg_path = file_path; |
AKASHI Takahiro | 14ff23b | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 489 | } else { |
Heinrich Schuchardt | ffb9059 | 2019-12-07 20:51:06 +0100 | [diff] [blame] | 490 | file_path = efi_dp_append(bootefi_device_path, |
| 491 | bootefi_image_path); |
Heinrich Schuchardt | c3a9c9c | 2020-08-25 17:54:05 +0000 | [diff] [blame] | 492 | msg_path = bootefi_image_path; |
Simon Glass | 95c1d25 | 2022-01-29 14:58:37 -0700 | [diff] [blame] | 493 | log_debug("Loaded from disk\n"); |
AKASHI Takahiro | fbd3ba5 | 2019-04-19 12:22:34 +0900 | [diff] [blame] | 494 | } |
| 495 | |
Heinrich Schuchardt | c3a9c9c | 2020-08-25 17:54:05 +0000 | [diff] [blame] | 496 | log_info("Booting %pD\n", msg_path); |
| 497 | |
Heinrich Schuchardt | ffb9059 | 2019-12-07 20:51:06 +0100 | [diff] [blame] | 498 | ret = EFI_CALL(efi_load_image(false, efi_root, file_path, source_buffer, |
| 499 | source_size, &handle)); |
Heinrich Schuchardt | c3a9c9c | 2020-08-25 17:54:05 +0000 | [diff] [blame] | 500 | if (ret != EFI_SUCCESS) { |
| 501 | log_err("Loading image failed\n"); |
AKASHI Takahiro | 14ff23b | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 502 | goto out; |
Heinrich Schuchardt | c3a9c9c | 2020-08-25 17:54:05 +0000 | [diff] [blame] | 503 | } |
Heinrich Schuchardt | a7647a7 | 2020-08-07 17:49:39 +0200 | [diff] [blame] | 504 | |
| 505 | /* Transfer environment variable as load options */ |
| 506 | ret = efi_env_set_load_options(handle, "bootargs", &load_options); |
| 507 | if (ret != EFI_SUCCESS) |
| 508 | goto out; |
| 509 | |
| 510 | ret = do_bootefi_exec(handle, load_options); |
AKASHI Takahiro | 14ff23b | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 511 | |
| 512 | out: |
Ilias Apalodimas | 3e2c20f | 2022-10-06 16:08:44 +0300 | [diff] [blame] | 513 | ret2 = efi_uninstall_multiple_protocol_interfaces(mem_handle, |
| 514 | &efi_guid_device_path, |
| 515 | file_path, NULL); |
Heinrich Schuchardt | 6e35d3b | 2020-04-20 12:44:56 +0200 | [diff] [blame] | 516 | efi_free_pool(file_path); |
Ilias Apalodimas | 3e2c20f | 2022-10-06 16:08:44 +0300 | [diff] [blame] | 517 | return (ret != EFI_SUCCESS) ? ret : ret2; |
AKASHI Takahiro | fbd3ba5 | 2019-04-19 12:22:34 +0900 | [diff] [blame] | 518 | } |
| 519 | |
AKASHI Takahiro | d52dacb | 2023-11-21 10:29:42 +0900 | [diff] [blame] | 520 | /** |
| 521 | * efi_binary_run() - run loaded UEFI image |
| 522 | * |
| 523 | * @image: memory address of the UEFI image |
| 524 | * @size: size of the UEFI image |
| 525 | * |
| 526 | * Execute an EFI binary image loaded at @image. |
| 527 | * @size may be zero if the binary is loaded with U-Boot load command. |
| 528 | * |
| 529 | * Return: status code |
| 530 | */ |
| 531 | static efi_status_t efi_binary_run(void *image, size_t size, void *fdt) |
| 532 | { |
| 533 | efi_status_t ret; |
| 534 | |
| 535 | /* Initialize EFI drivers */ |
| 536 | ret = efi_init_obj_list(); |
| 537 | if (ret != EFI_SUCCESS) { |
| 538 | log_err("Error: Cannot initialize UEFI sub-system, r = %lu\n", |
| 539 | ret & ~EFI_ERROR_MASK); |
| 540 | return ret; |
| 541 | } |
| 542 | |
| 543 | ret = efi_install_fdt(fdt); |
| 544 | if (ret != EFI_SUCCESS) |
| 545 | return ret; |
| 546 | |
| 547 | return efi_run_image(image, size); |
| 548 | } |
| 549 | |
AKASHI Takahiro | 09a81c7 | 2019-04-19 12:22:31 +0900 | [diff] [blame] | 550 | static efi_status_t bootefi_run_prepare(const char *load_options_path, |
| 551 | struct efi_device_path *device_path, |
| 552 | struct efi_device_path *image_path, |
| 553 | struct efi_loaded_image_obj **image_objp, |
| 554 | struct efi_loaded_image **loaded_image_infop) |
| 555 | { |
| 556 | efi_status_t ret; |
Heinrich Schuchardt | e626395 | 2020-01-03 22:53:42 +0100 | [diff] [blame] | 557 | u16 *load_options; |
AKASHI Takahiro | 09a81c7 | 2019-04-19 12:22:31 +0900 | [diff] [blame] | 558 | |
| 559 | ret = efi_setup_loaded_image(device_path, image_path, image_objp, |
| 560 | loaded_image_infop); |
| 561 | if (ret != EFI_SUCCESS) |
| 562 | return ret; |
| 563 | |
| 564 | /* Transfer environment variable as load options */ |
Heinrich Schuchardt | 25c6be5 | 2020-08-07 17:47:13 +0200 | [diff] [blame] | 565 | return efi_env_set_load_options((efi_handle_t)*image_objp, |
| 566 | load_options_path, |
| 567 | &load_options); |
AKASHI Takahiro | 09a81c7 | 2019-04-19 12:22:31 +0900 | [diff] [blame] | 568 | } |
| 569 | |
Simon Glass | 93f2559 | 2018-11-25 20:14:37 -0700 | [diff] [blame] | 570 | /** |
| 571 | * bootefi_test_prepare() - prepare to run an EFI test |
| 572 | * |
Heinrich Schuchardt | fd9cbe3 | 2019-01-12 14:42:40 +0100 | [diff] [blame] | 573 | * Prepare to run a test as if it were provided by a loaded image. |
Simon Glass | 93f2559 | 2018-11-25 20:14:37 -0700 | [diff] [blame] | 574 | * |
Heinrich Schuchardt | fd9cbe3 | 2019-01-12 14:42:40 +0100 | [diff] [blame] | 575 | * @image_objp: pointer to be set to the loaded image handle |
| 576 | * @loaded_image_infop: pointer to be set to the loaded image protocol |
| 577 | * @path: dummy file path used to construct the device path |
| 578 | * set in the loaded image protocol |
| 579 | * @load_options_path: name of a U-Boot environment variable. Its value is |
| 580 | * set as load options in the loaded image protocol. |
| 581 | * Return: status code |
Simon Glass | 93f2559 | 2018-11-25 20:14:37 -0700 | [diff] [blame] | 582 | */ |
| 583 | static efi_status_t bootefi_test_prepare |
| 584 | (struct efi_loaded_image_obj **image_objp, |
Heinrich Schuchardt | fd9cbe3 | 2019-01-12 14:42:40 +0100 | [diff] [blame] | 585 | struct efi_loaded_image **loaded_image_infop, const char *path, |
| 586 | const char *load_options_path) |
Simon Glass | 93f2559 | 2018-11-25 20:14:37 -0700 | [diff] [blame] | 587 | { |
Heinrich Schuchardt | fd9cbe3 | 2019-01-12 14:42:40 +0100 | [diff] [blame] | 588 | efi_status_t ret; |
| 589 | |
Simon Glass | 93f2559 | 2018-11-25 20:14:37 -0700 | [diff] [blame] | 590 | /* Construct a dummy device path */ |
AKASHI Takahiro | 203d196 | 2023-11-21 10:29:43 +0900 | [diff] [blame^] | 591 | test_device_path = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE, 0, 0); |
| 592 | if (!test_device_path) |
Simon Glass | 93f2559 | 2018-11-25 20:14:37 -0700 | [diff] [blame] | 593 | return EFI_OUT_OF_RESOURCES; |
Heinrich Schuchardt | fd9cbe3 | 2019-01-12 14:42:40 +0100 | [diff] [blame] | 594 | |
AKASHI Takahiro | 203d196 | 2023-11-21 10:29:43 +0900 | [diff] [blame^] | 595 | test_image_path = efi_dp_from_file(NULL, path); |
| 596 | if (!test_image_path) { |
Heinrich Schuchardt | fd9cbe3 | 2019-01-12 14:42:40 +0100 | [diff] [blame] | 597 | ret = EFI_OUT_OF_RESOURCES; |
| 598 | goto failure; |
| 599 | } |
| 600 | |
AKASHI Takahiro | 203d196 | 2023-11-21 10:29:43 +0900 | [diff] [blame^] | 601 | ret = bootefi_run_prepare(load_options_path, test_device_path, |
| 602 | test_image_path, image_objp, |
Heinrich Schuchardt | fd9cbe3 | 2019-01-12 14:42:40 +0100 | [diff] [blame] | 603 | loaded_image_infop); |
| 604 | if (ret == EFI_SUCCESS) |
| 605 | return ret; |
Simon Glass | 93f2559 | 2018-11-25 20:14:37 -0700 | [diff] [blame] | 606 | |
Heinrich Schuchardt | fd9cbe3 | 2019-01-12 14:42:40 +0100 | [diff] [blame] | 607 | failure: |
AKASHI Takahiro | 203d196 | 2023-11-21 10:29:43 +0900 | [diff] [blame^] | 608 | efi_free_pool(test_device_path); |
| 609 | efi_free_pool(test_image_path); |
| 610 | /* TODO: not sure calling clear function is necessary */ |
Heinrich Schuchardt | 6b82159 | 2021-01-12 12:46:24 +0100 | [diff] [blame] | 611 | efi_clear_bootdev(); |
Heinrich Schuchardt | fd9cbe3 | 2019-01-12 14:42:40 +0100 | [diff] [blame] | 612 | return ret; |
Simon Glass | 93f2559 | 2018-11-25 20:14:37 -0700 | [diff] [blame] | 613 | } |
| 614 | |
AKASHI Takahiro | 09a81c7 | 2019-04-19 12:22:31 +0900 | [diff] [blame] | 615 | /** |
Heinrich Schuchardt | 8fc56c6 | 2019-05-12 20:16:25 +0200 | [diff] [blame] | 616 | * do_efi_selftest() - execute EFI selftest |
AKASHI Takahiro | 09a81c7 | 2019-04-19 12:22:31 +0900 | [diff] [blame] | 617 | * |
AKASHI Takahiro | 09a81c7 | 2019-04-19 12:22:31 +0900 | [diff] [blame] | 618 | * Return: status code |
AKASHI Takahiro | 09a81c7 | 2019-04-19 12:22:31 +0900 | [diff] [blame] | 619 | */ |
Heinrich Schuchardt | 8fc56c6 | 2019-05-12 20:16:25 +0200 | [diff] [blame] | 620 | static int do_efi_selftest(void) |
AKASHI Takahiro | 09a81c7 | 2019-04-19 12:22:31 +0900 | [diff] [blame] | 621 | { |
| 622 | struct efi_loaded_image_obj *image_obj; |
| 623 | struct efi_loaded_image *loaded_image_info; |
| 624 | efi_status_t ret; |
| 625 | |
AKASHI Takahiro | 09a81c7 | 2019-04-19 12:22:31 +0900 | [diff] [blame] | 626 | ret = bootefi_test_prepare(&image_obj, &loaded_image_info, |
| 627 | "\\selftest", "efi_selftest"); |
| 628 | if (ret != EFI_SUCCESS) |
| 629 | return CMD_RET_FAILURE; |
| 630 | |
| 631 | /* Execute the test */ |
| 632 | ret = EFI_CALL(efi_selftest(&image_obj->header, &systab)); |
Ilias Apalodimas | b09ecf1 | 2023-07-24 13:17:36 +0300 | [diff] [blame] | 633 | efi_restore_gd(); |
| 634 | free(loaded_image_info->load_options); |
AKASHI Takahiro | 203d196 | 2023-11-21 10:29:43 +0900 | [diff] [blame^] | 635 | efi_free_pool(test_device_path); |
| 636 | efi_free_pool(test_image_path); |
Ilias Apalodimas | b09ecf1 | 2023-07-24 13:17:36 +0300 | [diff] [blame] | 637 | if (ret != EFI_SUCCESS) |
| 638 | efi_delete_handle(&image_obj->header); |
| 639 | else |
| 640 | ret = efi_delete_handle(&image_obj->header); |
AKASHI Takahiro | 09a81c7 | 2019-04-19 12:22:31 +0900 | [diff] [blame] | 641 | |
| 642 | return ret != EFI_SUCCESS; |
| 643 | } |
Simon Glass | 93f2559 | 2018-11-25 20:14:37 -0700 | [diff] [blame] | 644 | |
Heinrich Schuchardt | 8fc56c6 | 2019-05-12 20:16:25 +0200 | [diff] [blame] | 645 | /** |
| 646 | * do_bootefi() - execute `bootefi` command |
| 647 | * |
| 648 | * @cmdtp: table entry describing command |
| 649 | * @flag: bitmap indicating how the command was invoked |
| 650 | * @argc: number of arguments |
| 651 | * @argv: command line arguments |
| 652 | * Return: status code |
| 653 | */ |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 654 | static int do_bootefi(struct cmd_tbl *cmdtp, int flag, int argc, |
| 655 | char *const argv[]) |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 656 | { |
Heinrich Schuchardt | 8fc56c6 | 2019-05-12 20:16:25 +0200 | [diff] [blame] | 657 | efi_status_t ret; |
AKASHI Takahiro | 28cb4a8 | 2023-11-21 10:29:39 +0900 | [diff] [blame] | 658 | char *p; |
| 659 | void *fdt, *image_buf; |
| 660 | unsigned long addr, size; |
Heinrich Schuchardt | 8fc56c6 | 2019-05-12 20:16:25 +0200 | [diff] [blame] | 661 | |
AKASHI Takahiro | 09a81c7 | 2019-04-19 12:22:31 +0900 | [diff] [blame] | 662 | if (argc < 2) |
| 663 | return CMD_RET_USAGE; |
AKASHI Takahiro | cf81983 | 2019-04-19 12:22:33 +0900 | [diff] [blame] | 664 | |
Heinrich Schuchardt | 3ea20e7 | 2022-05-19 08:00:56 +0200 | [diff] [blame] | 665 | if (argc > 2) { |
Heinrich Schuchardt | 6246741 | 2019-12-08 01:07:01 +0100 | [diff] [blame] | 666 | uintptr_t fdt_addr; |
| 667 | |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 668 | fdt_addr = hextoul(argv[2], NULL); |
Heinrich Schuchardt | 6246741 | 2019-12-08 01:07:01 +0100 | [diff] [blame] | 669 | fdt = map_sysmem(fdt_addr, 0); |
| 670 | } else { |
| 671 | fdt = EFI_FDT_USE_INTERNAL; |
| 672 | } |
AKASHI Takahiro | 6a6858e | 2023-11-21 10:29:40 +0900 | [diff] [blame] | 673 | |
| 674 | if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR) && |
| 675 | !strcmp(argv[1], "bootmgr")) { |
AKASHI Takahiro | 4ab6c5b | 2023-11-21 10:29:41 +0900 | [diff] [blame] | 676 | ret = efi_bootmgr_run(fdt); |
Heinrich Schuchardt | 8fc56c6 | 2019-05-12 20:16:25 +0200 | [diff] [blame] | 677 | |
AKASHI Takahiro | 6a6858e | 2023-11-21 10:29:40 +0900 | [diff] [blame] | 678 | if (ret == EFI_INVALID_PARAMETER) |
| 679 | return CMD_RET_USAGE; |
AKASHI Takahiro | 4ab6c5b | 2023-11-21 10:29:41 +0900 | [diff] [blame] | 680 | else if (ret) |
AKASHI Takahiro | 6a6858e | 2023-11-21 10:29:40 +0900 | [diff] [blame] | 681 | return CMD_RET_FAILURE; |
| 682 | |
AKASHI Takahiro | 4ab6c5b | 2023-11-21 10:29:41 +0900 | [diff] [blame] | 683 | return CMD_RET_SUCCESS; |
Heinrich Schuchardt | b2625e8 | 2021-01-15 19:02:50 +0100 | [diff] [blame] | 684 | } |
AKASHI Takahiro | 6a6858e | 2023-11-21 10:29:40 +0900 | [diff] [blame] | 685 | |
| 686 | if (IS_ENABLED(CONFIG_CMD_BOOTEFI_SELFTEST) && |
| 687 | !strcmp(argv[1], "selftest")) { |
| 688 | /* Initialize EFI drivers */ |
| 689 | ret = efi_init_obj_list(); |
| 690 | if (ret != EFI_SUCCESS) { |
| 691 | log_err("Error: Cannot initialize UEFI sub-system, r = %lu\n", |
| 692 | ret & ~EFI_ERROR_MASK); |
| 693 | return CMD_RET_FAILURE; |
| 694 | } |
| 695 | |
| 696 | ret = efi_install_fdt(fdt); |
| 697 | if (ret == EFI_INVALID_PARAMETER) |
| 698 | return CMD_RET_USAGE; |
| 699 | else if (ret != EFI_SUCCESS) |
| 700 | return CMD_RET_FAILURE; |
| 701 | |
Heinrich Schuchardt | 8fc56c6 | 2019-05-12 20:16:25 +0200 | [diff] [blame] | 702 | return do_efi_selftest(); |
AKASHI Takahiro | 6a6858e | 2023-11-21 10:29:40 +0900 | [diff] [blame] | 703 | } |
AKASHI Takahiro | 28cb4a8 | 2023-11-21 10:29:39 +0900 | [diff] [blame] | 704 | |
AKASHI Takahiro | 6a6858e | 2023-11-21 10:29:40 +0900 | [diff] [blame] | 705 | if (!IS_ENABLED(CONFIG_CMD_BOOTEFI_BINARY)) |
| 706 | return CMD_RET_SUCCESS; |
| 707 | |
| 708 | if (IS_ENABLED(CONFIG_CMD_BOOTEFI_HELLO) && |
| 709 | !strcmp(argv[1], "hello")) { |
AKASHI Takahiro | 28cb4a8 | 2023-11-21 10:29:39 +0900 | [diff] [blame] | 710 | image_buf = __efi_helloworld_begin; |
| 711 | size = __efi_helloworld_end - __efi_helloworld_begin; |
| 712 | efi_clear_bootdev(); |
AKASHI Takahiro | 6a6858e | 2023-11-21 10:29:40 +0900 | [diff] [blame] | 713 | } else { |
AKASHI Takahiro | 28cb4a8 | 2023-11-21 10:29:39 +0900 | [diff] [blame] | 714 | addr = strtoul(argv[1], NULL, 16); |
| 715 | /* Check that a numeric value was passed */ |
| 716 | if (!addr) |
| 717 | return CMD_RET_USAGE; |
| 718 | image_buf = map_sysmem(addr, 0); |
| 719 | |
| 720 | p = strchr(argv[1], ':'); |
| 721 | if (p) { |
| 722 | size = strtoul(++p, NULL, 16); |
| 723 | if (!size) |
| 724 | return CMD_RET_USAGE; |
| 725 | efi_clear_bootdev(); |
| 726 | } else { |
| 727 | if (image_buf != image_addr) { |
| 728 | log_err("No UEFI binary known at %s\n", |
| 729 | argv[1]); |
| 730 | return CMD_RET_FAILURE; |
| 731 | } |
| 732 | size = image_size; |
| 733 | } |
Heinrich Schuchardt | 3ea20e7 | 2022-05-19 08:00:56 +0200 | [diff] [blame] | 734 | } |
AKASHI Takahiro | 6a6858e | 2023-11-21 10:29:40 +0900 | [diff] [blame] | 735 | |
AKASHI Takahiro | d52dacb | 2023-11-21 10:29:42 +0900 | [diff] [blame] | 736 | ret = efi_binary_run(image_buf, size, fdt); |
AKASHI Takahiro | 6a6858e | 2023-11-21 10:29:40 +0900 | [diff] [blame] | 737 | |
AKASHI Takahiro | 6a6858e | 2023-11-21 10:29:40 +0900 | [diff] [blame] | 738 | if (ret == EFI_INVALID_PARAMETER) |
| 739 | return CMD_RET_USAGE; |
AKASHI Takahiro | d52dacb | 2023-11-21 10:29:42 +0900 | [diff] [blame] | 740 | else if (ret) |
AKASHI Takahiro | 28cb4a8 | 2023-11-21 10:29:39 +0900 | [diff] [blame] | 741 | return CMD_RET_FAILURE; |
| 742 | |
| 743 | return CMD_RET_SUCCESS; |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 744 | } |
| 745 | |
Tom Rini | 03f146c | 2023-10-07 15:13:08 -0400 | [diff] [blame] | 746 | U_BOOT_LONGHELP(bootefi, |
Heinrich Schuchardt | 3ea20e7 | 2022-05-19 08:00:56 +0200 | [diff] [blame] | 747 | "<image address>[:<image size>] [<fdt address>]\n" |
| 748 | " - boot EFI payload\n" |
Simon Glass | fac4ced | 2016-11-07 08:47:08 -0700 | [diff] [blame] | 749 | #ifdef CONFIG_CMD_BOOTEFI_HELLO |
Heinrich Schuchardt | d33ae3e | 2017-09-15 10:06:11 +0200 | [diff] [blame] | 750 | "bootefi hello\n" |
| 751 | " - boot a sample Hello World application stored within U-Boot\n" |
| 752 | #endif |
| 753 | #ifdef CONFIG_CMD_BOOTEFI_SELFTEST |
Heinrich Schuchardt | 44ab21b | 2018-03-03 15:29:03 +0100 | [diff] [blame] | 754 | "bootefi selftest [fdt address]\n" |
Heinrich Schuchardt | d33ae3e | 2017-09-15 10:06:11 +0200 | [diff] [blame] | 755 | " - boot an EFI selftest application stored within U-Boot\n" |
Heinrich Schuchardt | 02efd5d | 2017-10-18 18:13:13 +0200 | [diff] [blame] | 756 | " Use environment variable efi_selftest to select a single test.\n" |
| 757 | " Use 'setenv efi_selftest list' to enumerate all tests.\n" |
Simon Glass | fac4ced | 2016-11-07 08:47:08 -0700 | [diff] [blame] | 758 | #endif |
Heinrich Schuchardt | b2625e8 | 2021-01-15 19:02:50 +0100 | [diff] [blame] | 759 | #ifdef CONFIG_CMD_BOOTEFI_BOOTMGR |
AKASHI Takahiro | 14ff23b | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 760 | "bootefi bootmgr [fdt address]\n" |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 761 | " - load and boot EFI payload based on BootOrder/BootXXXX variables.\n" |
| 762 | "\n" |
| 763 | " If specified, the device tree located at <fdt address> gets\n" |
Heinrich Schuchardt | b2625e8 | 2021-01-15 19:02:50 +0100 | [diff] [blame] | 764 | " exposed as EFI configuration table.\n" |
| 765 | #endif |
Tom Rini | 03f146c | 2023-10-07 15:13:08 -0400 | [diff] [blame] | 766 | ); |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 767 | |
| 768 | U_BOOT_CMD( |
Kyle Evans | 2c3bf4e | 2022-04-10 16:05:55 -0500 | [diff] [blame] | 769 | bootefi, 4, 0, do_bootefi, |
Sergey Kubushyn | 268f19e | 2016-06-07 11:14:31 -0700 | [diff] [blame] | 770 | "Boots an EFI payload from memory", |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 771 | bootefi_help_text |
| 772 | ); |