Heinrich Schuchardt | 1a41ca4 | 2024-04-26 16:13:18 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Bootmethod for distro boot via EFI |
| 4 | * |
| 5 | * Copyright 2021 Google LLC |
| 6 | * Written by Simon Glass <sjg@chromium.org> |
| 7 | */ |
| 8 | |
Heinrich Schuchardt | 955a321 | 2025-01-16 20:26:59 +0100 | [diff] [blame] | 9 | #define LOG_CATEGORY LOGC_EFI |
| 10 | |
Simon Glass | 37972f4 | 2025-05-24 11:28:21 -0600 | [diff] [blame^] | 11 | #include <efi_device_path.h> |
Heinrich Schuchardt | 1a41ca4 | 2024-04-26 16:13:18 +0200 | [diff] [blame] | 12 | #include <efi_loader.h> |
| 13 | #include <env.h> |
| 14 | #include <errno.h> |
| 15 | #include <log.h> |
| 16 | #include <string.h> |
| 17 | #include <vsprintf.h> |
| 18 | |
| 19 | /** |
Heinrich Schuchardt | 4e11a33 | 2024-08-07 00:11:38 +0200 | [diff] [blame] | 20 | * efi_get_distro_fdt_name() - get the filename for reading the .dtb file |
Heinrich Schuchardt | 1a41ca4 | 2024-04-26 16:13:18 +0200 | [diff] [blame] | 21 | * |
| 22 | * @fname: buffer for filename |
| 23 | * @size: buffer size |
| 24 | * @seq: sequence number, to cycle through options (0=first) |
| 25 | * |
| 26 | * Returns: |
| 27 | * 0 on success, |
| 28 | * -ENOENT if the "fdtfile" env var does not exist, |
| 29 | * -EINVAL if there are no more options, |
| 30 | * -EALREADY if the control FDT should be used |
| 31 | */ |
| 32 | int efi_get_distro_fdt_name(char *fname, int size, int seq) |
| 33 | { |
| 34 | const char *fdt_fname; |
| 35 | const char *prefix; |
| 36 | |
| 37 | /* select the prefix */ |
| 38 | switch (seq) { |
| 39 | case 0: |
| 40 | /* this is the default */ |
| 41 | prefix = "/dtb"; |
| 42 | break; |
| 43 | case 1: |
| 44 | prefix = ""; |
| 45 | break; |
| 46 | case 2: |
| 47 | prefix = "/dtb/current"; |
| 48 | break; |
Caleb Connolly | feb76da | 2024-07-22 19:55:23 +0200 | [diff] [blame] | 49 | case 3: |
| 50 | prefix = "/dtbs"; |
| 51 | break; |
Heinrich Schuchardt | 1a41ca4 | 2024-04-26 16:13:18 +0200 | [diff] [blame] | 52 | default: |
| 53 | return log_msg_ret("pref", -EINVAL); |
| 54 | } |
| 55 | |
| 56 | fdt_fname = env_get("fdtfile"); |
| 57 | if (fdt_fname) { |
| 58 | snprintf(fname, size, "%s/%s", prefix, fdt_fname); |
| 59 | log_debug("Using device tree: %s\n", fname); |
| 60 | } else if (IS_ENABLED(CONFIG_OF_HAS_PRIOR_STAGE)) { |
| 61 | strcpy(fname, "<prior>"); |
| 62 | return log_msg_ret("pref", -EALREADY); |
| 63 | /* Use this fallback only for 32-bit ARM */ |
| 64 | } else if (IS_ENABLED(CONFIG_ARM) && !IS_ENABLED(CONFIG_ARM64)) { |
| 65 | const char *soc = env_get("soc"); |
| 66 | const char *board = env_get("board"); |
| 67 | const char *boardver = env_get("boardver"); |
| 68 | |
| 69 | /* cf the code in label_boot() which seems very complex */ |
| 70 | snprintf(fname, size, "%s/%s%s%s%s.dtb", prefix, |
| 71 | soc ? soc : "", soc ? "-" : "", board ? board : "", |
| 72 | boardver ? boardver : ""); |
| 73 | log_debug("Using default device tree: %s\n", fname); |
| 74 | } else { |
| 75 | return log_msg_ret("env", -ENOENT); |
| 76 | } |
| 77 | |
| 78 | return 0; |
| 79 | } |
Heinrich Schuchardt | d9b2363 | 2024-04-26 16:13:21 +0200 | [diff] [blame] | 80 | |
| 81 | /** |
| 82 | * efi_load_distro_fdt() - load distro device-tree |
| 83 | * |
Heinrich Schuchardt | 8257f16 | 2024-07-13 13:30:29 +0200 | [diff] [blame] | 84 | * @handle: handle of loaded image |
Heinrich Schuchardt | d9b2363 | 2024-04-26 16:13:21 +0200 | [diff] [blame] | 85 | * @fdt: on return device-tree, must be freed via efi_free_pages() |
| 86 | * @fdt_size: buffer size |
| 87 | */ |
Heinrich Schuchardt | 8257f16 | 2024-07-13 13:30:29 +0200 | [diff] [blame] | 88 | void efi_load_distro_fdt(efi_handle_t handle, void **fdt, efi_uintn_t *fdt_size) |
Heinrich Schuchardt | d9b2363 | 2024-04-26 16:13:21 +0200 | [diff] [blame] | 89 | { |
Heinrich Schuchardt | 8257f16 | 2024-07-13 13:30:29 +0200 | [diff] [blame] | 90 | struct efi_device_path *dp; |
Heinrich Schuchardt | d9b2363 | 2024-04-26 16:13:21 +0200 | [diff] [blame] | 91 | efi_status_t ret; |
Heinrich Schuchardt | 8257f16 | 2024-07-13 13:30:29 +0200 | [diff] [blame] | 92 | struct efi_handler *handler; |
| 93 | struct efi_loaded_image *loaded_image; |
Heinrich Schuchardt | d9b2363 | 2024-04-26 16:13:21 +0200 | [diff] [blame] | 94 | efi_handle_t device; |
| 95 | |
| 96 | *fdt = NULL; |
| 97 | |
Heinrich Schuchardt | 8257f16 | 2024-07-13 13:30:29 +0200 | [diff] [blame] | 98 | /* Get boot device from loaded image protocol */ |
| 99 | ret = efi_search_protocol(handle, &efi_guid_loaded_image, &handler); |
| 100 | if (ret != EFI_SUCCESS) |
Heinrich Schuchardt | d9b2363 | 2024-04-26 16:13:21 +0200 | [diff] [blame] | 101 | return; |
Heinrich Schuchardt | 8257f16 | 2024-07-13 13:30:29 +0200 | [diff] [blame] | 102 | loaded_image = handler->protocol_interface; |
| 103 | device = loaded_image->device_handle; |
| 104 | |
| 105 | /* Get device path of boot device */ |
| 106 | ret = efi_search_protocol(device, &efi_guid_device_path, &handler); |
Heinrich Schuchardt | d9b2363 | 2024-04-26 16:13:21 +0200 | [diff] [blame] | 107 | if (ret != EFI_SUCCESS) |
Heinrich Schuchardt | 8257f16 | 2024-07-13 13:30:29 +0200 | [diff] [blame] | 108 | return; |
| 109 | dp = handler->protocol_interface; |
Heinrich Schuchardt | d9b2363 | 2024-04-26 16:13:21 +0200 | [diff] [blame] | 110 | |
Heinrich Schuchardt | 8257f16 | 2024-07-13 13:30:29 +0200 | [diff] [blame] | 111 | /* Try the various available names */ |
Heinrich Schuchardt | d9b2363 | 2024-04-26 16:13:21 +0200 | [diff] [blame] | 112 | for (int seq = 0; ; ++seq) { |
| 113 | struct efi_device_path *file; |
| 114 | char buf[255]; |
| 115 | |
| 116 | if (efi_get_distro_fdt_name(buf, sizeof(buf), seq)) |
| 117 | break; |
| 118 | file = efi_dp_from_file(dp, buf); |
| 119 | if (!file) |
| 120 | break; |
| 121 | ret = efi_load_image_from_path(true, file, fdt, fdt_size); |
| 122 | efi_free_pool(file); |
Heinrich Schuchardt | 8257f16 | 2024-07-13 13:30:29 +0200 | [diff] [blame] | 123 | if (ret == EFI_SUCCESS) { |
| 124 | log_debug("Fdt %pD loaded\n", file); |
Heinrich Schuchardt | d9b2363 | 2024-04-26 16:13:21 +0200 | [diff] [blame] | 125 | break; |
Heinrich Schuchardt | 8257f16 | 2024-07-13 13:30:29 +0200 | [diff] [blame] | 126 | } |
Heinrich Schuchardt | d9b2363 | 2024-04-26 16:13:21 +0200 | [diff] [blame] | 127 | } |
Heinrich Schuchardt | d9b2363 | 2024-04-26 16:13:21 +0200 | [diff] [blame] | 128 | } |