Ilias Apalodimas | aa0f755 | 2021-03-17 21:54:59 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (c) 2020, Linaro Limited |
| 4 | */ |
| 5 | |
| 6 | #define LOG_CATEGORY LOGC_EFI |
AKASHI Takahiro | 9b08b9a | 2024-01-17 13:39:41 +0900 | [diff] [blame] | 7 | #include <bootm.h> |
Ilias Apalodimas | aa0f755 | 2021-03-17 21:54:59 +0200 | [diff] [blame] | 8 | #include <env.h> |
AKASHI Takahiro | 9b08b9a | 2024-01-17 13:39:41 +0900 | [diff] [blame] | 9 | #include <image.h> |
| 10 | #include <log.h> |
Ilias Apalodimas | aa0f755 | 2021-03-17 21:54:59 +0200 | [diff] [blame] | 11 | #include <malloc.h> |
AKASHI Takahiro | 9b08b9a | 2024-01-17 13:39:41 +0900 | [diff] [blame] | 12 | #include <mapmem.h> |
Ilias Apalodimas | aa0f755 | 2021-03-17 21:54:59 +0200 | [diff] [blame] | 13 | #include <dm.h> |
| 14 | #include <fs.h> |
AKASHI Takahiro | 9b08b9a | 2024-01-17 13:39:41 +0900 | [diff] [blame] | 15 | #include <efi_api.h> |
Ilias Apalodimas | aa0f755 | 2021-03-17 21:54:59 +0200 | [diff] [blame] | 16 | #include <efi_load_initrd.h> |
| 17 | #include <efi_loader.h> |
| 18 | #include <efi_variable.h> |
AKASHI Takahiro | 9b08b9a | 2024-01-17 13:39:41 +0900 | [diff] [blame] | 19 | #include <linux/libfdt.h> |
| 20 | #include <linux/list.h> |
Ilias Apalodimas | aa0f755 | 2021-03-17 21:54:59 +0200 | [diff] [blame] | 21 | |
Heinrich Schuchardt | 6c405cb | 2021-10-15 02:33:33 +0200 | [diff] [blame] | 22 | #if defined(CONFIG_CMD_EFIDEBUG) || defined(CONFIG_EFI_LOAD_FILE2_INITRD) |
| 23 | /* GUID used by Linux to identify the LoadFile2 protocol with the initrd */ |
| 24 | const efi_guid_t efi_lf2_initrd_guid = EFI_INITRD_MEDIA_GUID; |
| 25 | #endif |
| 26 | |
Ilias Apalodimas | aa0f755 | 2021-03-17 21:54:59 +0200 | [diff] [blame] | 27 | /** |
| 28 | * efi_create_current_boot_var() - Return Boot#### name were #### is replaced by |
| 29 | * the value of BootCurrent |
| 30 | * |
| 31 | * @var_name: variable name |
| 32 | * @var_name_size: size of var_name |
| 33 | * |
| 34 | * Return: Status code |
| 35 | */ |
| 36 | static efi_status_t efi_create_current_boot_var(u16 var_name[], |
| 37 | size_t var_name_size) |
| 38 | { |
| 39 | efi_uintn_t boot_current_size; |
| 40 | efi_status_t ret; |
| 41 | u16 boot_current; |
| 42 | u16 *pos; |
| 43 | |
| 44 | boot_current_size = sizeof(boot_current); |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 45 | ret = efi_get_variable_int(u"BootCurrent", |
Ilias Apalodimas | aa0f755 | 2021-03-17 21:54:59 +0200 | [diff] [blame] | 46 | &efi_global_variable_guid, NULL, |
| 47 | &boot_current_size, &boot_current, NULL); |
| 48 | if (ret != EFI_SUCCESS) |
| 49 | goto out; |
| 50 | |
| 51 | pos = efi_create_indexed_name(var_name, var_name_size, "Boot", |
| 52 | boot_current); |
| 53 | if (!pos) { |
| 54 | ret = EFI_OUT_OF_RESOURCES; |
| 55 | goto out; |
| 56 | } |
| 57 | |
| 58 | out: |
| 59 | return ret; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * efi_get_dp_from_boot() - Retrieve and return a device path from an EFI |
| 64 | * Boot### variable. |
| 65 | * A boot option may contain an array of device paths. |
| 66 | * We use a VenMedia() with a specific GUID to identify |
| 67 | * the usage of the array members. This function is |
| 68 | * used to extract a specific device path |
| 69 | * |
| 70 | * @guid: vendor GUID of the VenMedia() device path node identifying the |
| 71 | * device path |
| 72 | * |
| 73 | * Return: device path or NULL. Caller must free the returned value |
| 74 | */ |
Heinrich Schuchardt | efd90d7 | 2024-04-26 16:13:08 +0200 | [diff] [blame] | 75 | struct efi_device_path *efi_get_dp_from_boot(const efi_guid_t *guid) |
Ilias Apalodimas | aa0f755 | 2021-03-17 21:54:59 +0200 | [diff] [blame] | 76 | { |
Ilias Apalodimas | aa0f755 | 2021-03-17 21:54:59 +0200 | [diff] [blame] | 77 | struct efi_load_option lo; |
Heinrich Schuchardt | 35dd322 | 2021-10-15 02:59:15 +0200 | [diff] [blame] | 78 | void *var_value; |
Ilias Apalodimas | aa0f755 | 2021-03-17 21:54:59 +0200 | [diff] [blame] | 79 | efi_uintn_t size; |
| 80 | efi_status_t ret; |
| 81 | u16 var_name[16]; |
| 82 | |
| 83 | ret = efi_create_current_boot_var(var_name, sizeof(var_name)); |
| 84 | if (ret != EFI_SUCCESS) |
| 85 | return NULL; |
| 86 | |
| 87 | var_value = efi_get_var(var_name, &efi_global_variable_guid, &size); |
| 88 | if (!var_value) |
| 89 | return NULL; |
| 90 | |
| 91 | ret = efi_deserialize_load_option(&lo, var_value, &size); |
| 92 | if (ret != EFI_SUCCESS) |
Heinrich Schuchardt | 35dd322 | 2021-10-15 02:59:15 +0200 | [diff] [blame] | 93 | goto err; |
Ilias Apalodimas | aa0f755 | 2021-03-17 21:54:59 +0200 | [diff] [blame] | 94 | |
Heinrich Schuchardt | efd90d7 | 2024-04-26 16:13:08 +0200 | [diff] [blame] | 95 | return efi_dp_from_lo(&lo, guid); |
Ilias Apalodimas | aa0f755 | 2021-03-17 21:54:59 +0200 | [diff] [blame] | 96 | |
Heinrich Schuchardt | 35dd322 | 2021-10-15 02:59:15 +0200 | [diff] [blame] | 97 | err: |
Ilias Apalodimas | aa0f755 | 2021-03-17 21:54:59 +0200 | [diff] [blame] | 98 | free(var_value); |
Heinrich Schuchardt | 35dd322 | 2021-10-15 02:59:15 +0200 | [diff] [blame] | 99 | return NULL; |
Ilias Apalodimas | aa0f755 | 2021-03-17 21:54:59 +0200 | [diff] [blame] | 100 | } |
Ilias Apalodimas | 34db9b1 | 2022-05-06 15:36:00 +0300 | [diff] [blame] | 101 | |
Heinrich Schuchardt | f7529f7 | 2024-04-26 16:13:11 +0200 | [diff] [blame] | 102 | /** |
| 103 | * efi_load_option_dp_join() - join device-paths for load option |
| 104 | * |
| 105 | * @dp: in: binary device-path, out: joined device-path |
| 106 | * @dp_size: size of joined device-path |
| 107 | * @initrd_dp: initrd device-path or NULL |
| 108 | * @fdt_dp: device-tree device-path or NULL |
| 109 | * Return: status_code |
| 110 | */ |
| 111 | efi_status_t efi_load_option_dp_join(struct efi_device_path **dp, |
| 112 | size_t *dp_size, |
| 113 | struct efi_device_path *initrd_dp, |
| 114 | struct efi_device_path *fdt_dp) |
| 115 | { |
| 116 | if (!dp) |
| 117 | return EFI_INVALID_PARAMETER; |
| 118 | |
| 119 | *dp_size = efi_dp_size(*dp); |
| 120 | |
| 121 | if (initrd_dp) { |
| 122 | struct efi_device_path *tmp_dp = *dp; |
| 123 | |
| 124 | *dp = efi_dp_concat(tmp_dp, initrd_dp, *dp_size); |
| 125 | efi_free_pool(tmp_dp); |
| 126 | if (!*dp) |
| 127 | return EFI_OUT_OF_RESOURCES; |
| 128 | *dp_size += efi_dp_size(initrd_dp) + sizeof(END); |
| 129 | } |
| 130 | |
| 131 | if (fdt_dp) { |
| 132 | struct efi_device_path *tmp_dp = *dp; |
| 133 | |
| 134 | *dp = efi_dp_concat(tmp_dp, fdt_dp, *dp_size); |
| 135 | efi_free_pool(tmp_dp); |
| 136 | if (!dp) |
| 137 | return EFI_OUT_OF_RESOURCES; |
| 138 | *dp_size += efi_dp_size(fdt_dp) + sizeof(END); |
| 139 | } |
| 140 | |
| 141 | *dp_size += sizeof(END); |
| 142 | |
| 143 | return EFI_SUCCESS; |
| 144 | } |
| 145 | |
Ilias Apalodimas | 34db9b1 | 2022-05-06 15:36:00 +0300 | [diff] [blame] | 146 | const struct guid_to_hash_map { |
| 147 | efi_guid_t guid; |
| 148 | const char algo[32]; |
| 149 | u32 bits; |
| 150 | } guid_to_hash[] = { |
| 151 | { |
| 152 | EFI_CERT_X509_SHA256_GUID, |
| 153 | "sha256", |
| 154 | SHA256_SUM_LEN * 8, |
| 155 | }, |
| 156 | { |
| 157 | EFI_CERT_SHA256_GUID, |
| 158 | "sha256", |
| 159 | SHA256_SUM_LEN * 8, |
| 160 | }, |
| 161 | { |
| 162 | EFI_CERT_X509_SHA384_GUID, |
| 163 | "sha384", |
| 164 | SHA384_SUM_LEN * 8, |
| 165 | }, |
| 166 | { |
| 167 | EFI_CERT_X509_SHA512_GUID, |
| 168 | "sha512", |
| 169 | SHA512_SUM_LEN * 8, |
| 170 | }, |
| 171 | }; |
| 172 | |
| 173 | #define MAX_GUID_TO_HASH_COUNT ARRAY_SIZE(guid_to_hash) |
| 174 | |
| 175 | /** guid_to_sha_str - return the sha string e.g "sha256" for a given guid |
| 176 | * used on EFI security databases |
| 177 | * |
| 178 | * @guid: guid to check |
| 179 | * |
| 180 | * Return: len or 0 if no match is found |
| 181 | */ |
| 182 | const char *guid_to_sha_str(const efi_guid_t *guid) |
| 183 | { |
| 184 | size_t i; |
| 185 | |
| 186 | for (i = 0; i < MAX_GUID_TO_HASH_COUNT; i++) { |
| 187 | if (!guidcmp(guid, &guid_to_hash[i].guid)) |
| 188 | return guid_to_hash[i].algo; |
| 189 | } |
| 190 | |
| 191 | return NULL; |
| 192 | } |
| 193 | |
| 194 | /** algo_to_len - return the sha size in bytes for a given string |
| 195 | * |
| 196 | * @algo: string indicating hashing algorithm to check |
| 197 | * |
| 198 | * Return: length of hash in bytes or 0 if no match is found |
| 199 | */ |
| 200 | int algo_to_len(const char *algo) |
| 201 | { |
| 202 | size_t i; |
| 203 | |
| 204 | for (i = 0; i < MAX_GUID_TO_HASH_COUNT; i++) { |
| 205 | if (!strcmp(algo, guid_to_hash[i].algo)) |
| 206 | return guid_to_hash[i].bits / 8; |
| 207 | } |
| 208 | |
| 209 | return 0; |
| 210 | } |
Masahisa Kojima | c961108 | 2022-07-22 11:39:10 +0900 | [diff] [blame] | 211 | |
| 212 | /** efi_link_dev - link the efi_handle_t and udevice |
| 213 | * |
| 214 | * @handle: efi handle to associate with udevice |
| 215 | * @dev: udevice to associate with efi handle |
| 216 | * |
| 217 | * Return: 0 on success, negative on failure |
| 218 | */ |
| 219 | int efi_link_dev(efi_handle_t handle, struct udevice *dev) |
| 220 | { |
| 221 | handle->dev = dev; |
| 222 | return dev_tag_set_ptr(dev, DM_TAG_EFI, handle); |
| 223 | } |
Heinrich Schuchardt | 34f3462 | 2022-10-03 09:47:51 +0200 | [diff] [blame] | 224 | |
| 225 | /** |
| 226 | * efi_unlink_dev() - unlink udevice and handle |
| 227 | * |
| 228 | * @handle: EFI handle to unlink |
| 229 | * |
| 230 | * Return: 0 on success, negative on failure |
| 231 | */ |
| 232 | int efi_unlink_dev(efi_handle_t handle) |
| 233 | { |
| 234 | int ret; |
| 235 | |
| 236 | ret = dev_tag_del(handle->dev, DM_TAG_EFI); |
| 237 | if (ret) |
| 238 | return ret; |
| 239 | handle->dev = NULL; |
| 240 | |
| 241 | return 0; |
| 242 | } |
Masahisa Kojima | 2f407f0 | 2022-12-02 13:59:35 +0900 | [diff] [blame] | 243 | |
| 244 | static int u16_tohex(u16 c) |
| 245 | { |
| 246 | if (c >= '0' && c <= '9') |
| 247 | return c - '0'; |
| 248 | if (c >= 'A' && c <= 'F') |
| 249 | return c - 'A' + 10; |
| 250 | |
| 251 | /* not hexadecimal */ |
| 252 | return -1; |
| 253 | } |
| 254 | |
| 255 | bool efi_varname_is_load_option(u16 *var_name16, int *index) |
| 256 | { |
| 257 | int id, i, digit; |
| 258 | |
| 259 | if (memcmp(var_name16, u"Boot", 8)) |
| 260 | return false; |
| 261 | |
| 262 | for (id = 0, i = 0; i < 4; i++) { |
| 263 | digit = u16_tohex(var_name16[4 + i]); |
| 264 | if (digit < 0) |
| 265 | break; |
| 266 | id = (id << 4) + digit; |
| 267 | } |
| 268 | if (i == 4 && !var_name16[8]) { |
| 269 | if (index) |
| 270 | *index = id; |
| 271 | return true; |
| 272 | } |
| 273 | |
| 274 | return false; |
| 275 | } |
Masahisa Kojima | 7ec3c6f | 2022-12-19 11:33:12 +0900 | [diff] [blame] | 276 | |
| 277 | /** |
| 278 | * efi_next_variable_name() - get next variable name |
| 279 | * |
| 280 | * This function is a wrapper of efi_get_next_variable_name_int(). |
| 281 | * If efi_get_next_variable_name_int() returns EFI_BUFFER_TOO_SMALL, |
| 282 | * @size and @buf are updated by new buffer size and realloced buffer. |
| 283 | * |
| 284 | * @size: pointer to the buffer size |
| 285 | * @buf: pointer to the buffer |
| 286 | * @guid: pointer to the guid |
| 287 | * Return: status code |
| 288 | */ |
| 289 | efi_status_t efi_next_variable_name(efi_uintn_t *size, u16 **buf, efi_guid_t *guid) |
| 290 | { |
| 291 | u16 *p; |
| 292 | efi_status_t ret; |
| 293 | efi_uintn_t buf_size = *size; |
| 294 | |
| 295 | ret = efi_get_next_variable_name_int(&buf_size, *buf, guid); |
| 296 | if (ret == EFI_NOT_FOUND) |
| 297 | return ret; |
| 298 | if (ret == EFI_BUFFER_TOO_SMALL) { |
| 299 | p = realloc(*buf, buf_size); |
| 300 | if (!p) |
| 301 | return EFI_OUT_OF_RESOURCES; |
| 302 | |
| 303 | *buf = p; |
| 304 | *size = buf_size; |
| 305 | ret = efi_get_next_variable_name_int(&buf_size, *buf, guid); |
| 306 | } |
| 307 | |
| 308 | return ret; |
| 309 | } |
Raymond Mao | 70a76c5 | 2023-06-19 14:22:58 -0700 | [diff] [blame] | 310 | |
| 311 | /** |
| 312 | * efi_search_bootorder() - search the boot option index in BootOrder |
| 313 | * |
| 314 | * @bootorder: pointer to the BootOrder variable |
| 315 | * @num: number of BootOrder entry |
| 316 | * @target: target boot option index to search |
| 317 | * @index: pointer to store the index of BootOrder variable |
| 318 | * Return: true if exists, false otherwise |
| 319 | */ |
| 320 | bool efi_search_bootorder(u16 *bootorder, efi_uintn_t num, u32 target, u32 *index) |
| 321 | { |
| 322 | u32 i; |
| 323 | |
| 324 | for (i = 0; i < num; i++) { |
| 325 | if (target == bootorder[i]) { |
| 326 | if (index) |
| 327 | *index = i; |
| 328 | |
| 329 | return true; |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | return false; |
| 334 | } |
AKASHI Takahiro | 9b08b9a | 2024-01-17 13:39:41 +0900 | [diff] [blame] | 335 | |
| 336 | /** |
| 337 | * efi_env_set_load_options() - set load options from environment variable |
| 338 | * |
| 339 | * @handle: the image handle |
| 340 | * @env_var: name of the environment variable |
| 341 | * @load_options: pointer to load options (output) |
| 342 | * Return: status code |
| 343 | */ |
| 344 | efi_status_t efi_env_set_load_options(efi_handle_t handle, |
| 345 | const char *env_var, |
| 346 | u16 **load_options) |
| 347 | { |
| 348 | const char *env = env_get(env_var); |
| 349 | size_t size; |
| 350 | u16 *pos; |
| 351 | efi_status_t ret; |
| 352 | |
| 353 | *load_options = NULL; |
| 354 | if (!env) |
| 355 | return EFI_SUCCESS; |
| 356 | size = sizeof(u16) * (utf8_utf16_strlen(env) + 1); |
| 357 | pos = calloc(size, 1); |
| 358 | if (!pos) |
| 359 | return EFI_OUT_OF_RESOURCES; |
| 360 | *load_options = pos; |
| 361 | utf8_utf16_strcpy(&pos, env); |
| 362 | ret = efi_set_load_options(handle, size, *load_options); |
| 363 | if (ret != EFI_SUCCESS) { |
| 364 | free(*load_options); |
| 365 | *load_options = NULL; |
| 366 | } |
| 367 | return ret; |
| 368 | } |
| 369 | |
| 370 | /** |
| 371 | * copy_fdt() - Copy the device tree to a new location available to EFI |
| 372 | * |
| 373 | * The FDT is copied to a suitable location within the EFI memory map. |
| 374 | * Additional 12 KiB are added to the space in case the device tree needs to be |
| 375 | * expanded later with fdt_open_into(). |
| 376 | * |
| 377 | * @fdtp: On entry a pointer to the flattened device tree. |
| 378 | * On exit a pointer to the copy of the flattened device tree. |
| 379 | * FDT start |
| 380 | * Return: status code |
| 381 | */ |
| 382 | static efi_status_t copy_fdt(void **fdtp) |
| 383 | { |
| 384 | unsigned long fdt_ram_start = -1L, fdt_pages; |
| 385 | efi_status_t ret = 0; |
| 386 | void *fdt, *new_fdt; |
| 387 | u64 new_fdt_addr; |
| 388 | uint fdt_size; |
| 389 | int i; |
| 390 | |
| 391 | for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) { |
| 392 | u64 ram_start = gd->bd->bi_dram[i].start; |
| 393 | u64 ram_size = gd->bd->bi_dram[i].size; |
| 394 | |
| 395 | if (!ram_size) |
| 396 | continue; |
| 397 | |
| 398 | if (ram_start < fdt_ram_start) |
| 399 | fdt_ram_start = ram_start; |
| 400 | } |
| 401 | |
| 402 | /* |
| 403 | * Give us at least 12 KiB of breathing room in case the device tree |
| 404 | * needs to be expanded later. |
| 405 | */ |
| 406 | fdt = *fdtp; |
| 407 | fdt_pages = efi_size_in_pages(fdt_totalsize(fdt) + 0x3000); |
| 408 | fdt_size = fdt_pages << EFI_PAGE_SHIFT; |
| 409 | |
| 410 | ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, |
| 411 | EFI_ACPI_RECLAIM_MEMORY, fdt_pages, |
| 412 | &new_fdt_addr); |
| 413 | if (ret != EFI_SUCCESS) { |
| 414 | log_err("ERROR: Failed to reserve space for FDT\n"); |
| 415 | goto done; |
| 416 | } |
| 417 | new_fdt = (void *)(uintptr_t)new_fdt_addr; |
| 418 | memcpy(new_fdt, fdt, fdt_totalsize(fdt)); |
| 419 | fdt_set_totalsize(new_fdt, fdt_size); |
| 420 | |
| 421 | *fdtp = (void *)(uintptr_t)new_fdt_addr; |
| 422 | done: |
| 423 | return ret; |
| 424 | } |
| 425 | |
| 426 | /** |
Heinrich Schuchardt | 0a4a2f3 | 2024-01-26 08:54:30 +0100 | [diff] [blame] | 427 | * efi_get_configuration_table() - get configuration table |
AKASHI Takahiro | 9b08b9a | 2024-01-17 13:39:41 +0900 | [diff] [blame] | 428 | * |
| 429 | * @guid: GUID of the configuration table |
| 430 | * Return: pointer to configuration table or NULL |
| 431 | */ |
Heinrich Schuchardt | 0a4a2f3 | 2024-01-26 08:54:30 +0100 | [diff] [blame] | 432 | void *efi_get_configuration_table(const efi_guid_t *guid) |
AKASHI Takahiro | 9b08b9a | 2024-01-17 13:39:41 +0900 | [diff] [blame] | 433 | { |
| 434 | size_t i; |
| 435 | |
| 436 | for (i = 0; i < systab.nr_tables; i++) { |
| 437 | if (!guidcmp(guid, &systab.tables[i].guid)) |
| 438 | return systab.tables[i].table; |
| 439 | } |
| 440 | return NULL; |
| 441 | } |
| 442 | |
| 443 | /** |
| 444 | * efi_install_fdt() - install device tree |
| 445 | * |
| 446 | * If fdt is not EFI_FDT_USE_INTERNAL, the device tree located at that memory |
| 447 | * address will be installed as configuration table, otherwise the device |
| 448 | * tree located at the address indicated by environment variable fdt_addr or as |
| 449 | * fallback fdtcontroladdr will be used. |
| 450 | * |
| 451 | * On architectures using ACPI tables device trees shall not be installed as |
| 452 | * configuration table. |
| 453 | * |
| 454 | * @fdt: address of device tree or EFI_FDT_USE_INTERNAL to use |
| 455 | * the hardware device tree as indicated by environment variable |
| 456 | * fdt_addr or as fallback the internal device tree as indicated by |
| 457 | * the environment variable fdtcontroladdr |
| 458 | * Return: status code |
| 459 | */ |
| 460 | efi_status_t efi_install_fdt(void *fdt) |
| 461 | { |
| 462 | struct bootm_headers img = { 0 }; |
| 463 | efi_status_t ret; |
| 464 | |
| 465 | /* |
| 466 | * The EBBR spec requires that we have either an FDT or an ACPI table |
| 467 | * but not both. |
| 468 | */ |
| 469 | if (CONFIG_IS_ENABLED(GENERATE_ACPI_TABLE) && fdt) |
| 470 | log_warning("WARNING: Can't have ACPI table and device tree - ignoring DT.\n"); |
| 471 | |
| 472 | if (fdt == EFI_FDT_USE_INTERNAL) { |
| 473 | const char *fdt_opt; |
| 474 | uintptr_t fdt_addr; |
| 475 | |
| 476 | /* Look for device tree that is already installed */ |
Heinrich Schuchardt | 0a4a2f3 | 2024-01-26 08:54:30 +0100 | [diff] [blame] | 477 | if (efi_get_configuration_table(&efi_guid_fdt)) |
AKASHI Takahiro | 9b08b9a | 2024-01-17 13:39:41 +0900 | [diff] [blame] | 478 | return EFI_SUCCESS; |
| 479 | /* Check if there is a hardware device tree */ |
| 480 | fdt_opt = env_get("fdt_addr"); |
| 481 | /* Use our own device tree as fallback */ |
| 482 | if (!fdt_opt) { |
| 483 | fdt_opt = env_get("fdtcontroladdr"); |
| 484 | if (!fdt_opt) { |
| 485 | log_err("ERROR: need device tree\n"); |
| 486 | return EFI_NOT_FOUND; |
| 487 | } |
| 488 | } |
| 489 | fdt_addr = hextoul(fdt_opt, NULL); |
| 490 | if (!fdt_addr) { |
| 491 | log_err("ERROR: invalid $fdt_addr or $fdtcontroladdr\n"); |
| 492 | return EFI_LOAD_ERROR; |
| 493 | } |
| 494 | fdt = map_sysmem(fdt_addr, 0); |
| 495 | } |
| 496 | |
| 497 | /* Install device tree */ |
| 498 | if (fdt_check_header(fdt)) { |
| 499 | log_err("ERROR: invalid device tree\n"); |
| 500 | return EFI_LOAD_ERROR; |
| 501 | } |
| 502 | |
Mark Kettenis | 98c598c | 2024-02-16 00:25:34 +0100 | [diff] [blame] | 503 | if (CONFIG_IS_ENABLED(GENERATE_ACPI_TABLE)) { |
| 504 | /* Create memory reservations as indicated by the device tree */ |
| 505 | efi_carve_out_dt_rsv(fdt); |
AKASHI Takahiro | 9b08b9a | 2024-01-17 13:39:41 +0900 | [diff] [blame] | 506 | return EFI_SUCCESS; |
Mark Kettenis | 98c598c | 2024-02-16 00:25:34 +0100 | [diff] [blame] | 507 | } |
AKASHI Takahiro | 9b08b9a | 2024-01-17 13:39:41 +0900 | [diff] [blame] | 508 | |
| 509 | /* Prepare device tree for payload */ |
| 510 | ret = copy_fdt(&fdt); |
| 511 | if (ret) { |
| 512 | log_err("ERROR: out of memory\n"); |
| 513 | return EFI_OUT_OF_RESOURCES; |
| 514 | } |
| 515 | |
| 516 | if (image_setup_libfdt(&img, fdt, NULL)) { |
| 517 | log_err("ERROR: failed to process device tree\n"); |
| 518 | return EFI_LOAD_ERROR; |
| 519 | } |
| 520 | |
Mark Kettenis | 98c598c | 2024-02-16 00:25:34 +0100 | [diff] [blame] | 521 | /* Create memory reservations as indicated by the device tree */ |
| 522 | efi_carve_out_dt_rsv(fdt); |
| 523 | |
AKASHI Takahiro | 9b08b9a | 2024-01-17 13:39:41 +0900 | [diff] [blame] | 524 | efi_try_purge_kaslr_seed(fdt); |
| 525 | |
| 526 | if (CONFIG_IS_ENABLED(EFI_TCG2_PROTOCOL_MEASURE_DTB)) { |
| 527 | ret = efi_tcg2_measure_dtb(fdt); |
| 528 | if (ret == EFI_SECURITY_VIOLATION) { |
| 529 | log_err("ERROR: failed to measure DTB\n"); |
| 530 | return ret; |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | /* Install device tree as UEFI table */ |
| 535 | ret = efi_install_configuration_table(&efi_guid_fdt, fdt); |
| 536 | if (ret != EFI_SUCCESS) { |
| 537 | log_err("ERROR: failed to install device tree\n"); |
| 538 | return ret; |
| 539 | } |
| 540 | |
| 541 | return EFI_SUCCESS; |
| 542 | } |
| 543 | |
| 544 | /** |
| 545 | * do_bootefi_exec() - execute EFI binary |
| 546 | * |
| 547 | * The image indicated by @handle is started. When it returns the allocated |
| 548 | * memory for the @load_options is freed. |
| 549 | * |
| 550 | * @handle: handle of loaded image |
| 551 | * @load_options: load options |
| 552 | * Return: status code |
| 553 | * |
| 554 | * Load the EFI binary into a newly assigned memory unwinding the relocation |
| 555 | * information, install the loaded image protocol, and call the binary. |
| 556 | */ |
| 557 | efi_status_t do_bootefi_exec(efi_handle_t handle, void *load_options) |
| 558 | { |
| 559 | efi_status_t ret; |
| 560 | efi_uintn_t exit_data_size = 0; |
| 561 | u16 *exit_data = NULL; |
| 562 | struct efi_event *evt; |
| 563 | |
| 564 | /* On ARM switch from EL3 or secure mode to EL2 or non-secure mode */ |
| 565 | switch_to_non_secure_mode(); |
| 566 | |
| 567 | /* |
| 568 | * The UEFI standard requires that the watchdog timer is set to five |
| 569 | * minutes when invoking an EFI boot option. |
| 570 | * |
| 571 | * Unified Extensible Firmware Interface (UEFI), version 2.7 Errata A |
| 572 | * 7.5. Miscellaneous Boot Services - EFI_BOOT_SERVICES.SetWatchdogTimer |
| 573 | */ |
| 574 | ret = efi_set_watchdog(300); |
| 575 | if (ret != EFI_SUCCESS) { |
| 576 | log_err("ERROR: Failed to set watchdog timer\n"); |
| 577 | goto out; |
| 578 | } |
| 579 | |
| 580 | /* Call our payload! */ |
| 581 | ret = EFI_CALL(efi_start_image(handle, &exit_data_size, &exit_data)); |
| 582 | if (ret != EFI_SUCCESS) { |
| 583 | log_err("## Application failed, r = %lu\n", |
| 584 | ret & ~EFI_ERROR_MASK); |
| 585 | if (exit_data) { |
| 586 | log_err("## %ls\n", exit_data); |
| 587 | efi_free_pool(exit_data); |
| 588 | } |
| 589 | } |
| 590 | |
AKASHI Takahiro | 9b08b9a | 2024-01-17 13:39:41 +0900 | [diff] [blame] | 591 | out: |
| 592 | free(load_options); |
| 593 | |
AKASHI Takahiro | 9b08b9a | 2024-01-17 13:39:41 +0900 | [diff] [blame] | 594 | /* Notify EFI_EVENT_GROUP_RETURN_TO_EFIBOOTMGR event group. */ |
| 595 | list_for_each_entry(evt, &efi_events, link) { |
| 596 | if (evt->group && |
| 597 | !guidcmp(evt->group, |
| 598 | &efi_guid_event_group_return_to_efibootmgr)) { |
| 599 | efi_signal_event(evt); |
| 600 | EFI_CALL(systab.boottime->close_event(evt)); |
| 601 | break; |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | /* Control is returned to U-Boot, disable EFI watchdog */ |
| 606 | efi_set_watchdog(0); |
| 607 | |
| 608 | return ret; |
| 609 | } |