Heinrich Schuchardt | 2c1e224 | 2020-12-13 10:30:24 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * EFI_DT_FIXUP_PROTOCOL |
| 4 | * |
| 5 | * Copyright (c) 2020 Heinrich Schuchardt |
| 6 | */ |
| 7 | |
Heinrich Schuchardt | 2c1e224 | 2020-12-13 10:30:24 +0100 | [diff] [blame] | 8 | #include <efi_dt_fixup.h> |
| 9 | #include <efi_loader.h> |
Ilias Apalodimas | e4e5660 | 2022-01-03 14:07:37 +0200 | [diff] [blame] | 10 | #include <efi_rng.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 11 | #include <fdtdec.h> |
Heinrich Schuchardt | 2c1e224 | 2020-12-13 10:30:24 +0100 | [diff] [blame] | 12 | #include <mapmem.h> |
| 13 | |
Heinrich Schuchardt | 2c1e224 | 2020-12-13 10:30:24 +0100 | [diff] [blame] | 14 | const efi_guid_t efi_guid_dt_fixup_protocol = EFI_DT_FIXUP_PROTOCOL_GUID; |
| 15 | |
| 16 | /** |
| 17 | * efi_reserve_memory() - add reserved memory to memory map |
| 18 | * |
| 19 | * @addr: start address of the reserved memory range |
| 20 | * @size: size of the reserved memory range |
| 21 | * @nomap: indicates that the memory range shall not be accessed by the |
| 22 | * UEFI payload |
| 23 | */ |
| 24 | static void efi_reserve_memory(u64 addr, u64 size, bool nomap) |
| 25 | { |
| 26 | int type; |
| 27 | efi_uintn_t ret; |
| 28 | |
| 29 | /* Convert from sandbox address space. */ |
| 30 | addr = (uintptr_t)map_sysmem(addr, 0); |
| 31 | |
| 32 | if (nomap) |
| 33 | type = EFI_RESERVED_MEMORY_TYPE; |
| 34 | else |
| 35 | type = EFI_BOOT_SERVICES_DATA; |
| 36 | |
| 37 | ret = efi_add_memory_map(addr, size, type); |
| 38 | if (ret != EFI_SUCCESS) |
| 39 | log_err("Reserved memory mapping failed addr %llx size %llx\n", |
| 40 | addr, size); |
| 41 | } |
| 42 | |
| 43 | /** |
Heinrich Schuchardt | 069079c | 2024-09-17 10:49:29 +0200 | [diff] [blame] | 44 | * efi_try_purge_rng_seed() - Remove unused kaslr-seed, rng-seed |
Ilias Apalodimas | e4e5660 | 2022-01-03 14:07:37 +0200 | [diff] [blame] | 45 | * |
| 46 | * Kernel's EFI STUB only relies on EFI_RNG_PROTOCOL for randomization |
| 47 | * and completely ignores the kaslr-seed for its own randomness needs |
| 48 | * (i.e the randomization of the physical placement of the kernel). |
| 49 | * Weed it out from the DTB we hand over, which would mess up our DTB |
| 50 | * TPM measurements as well. |
| 51 | * |
| 52 | * @fdt: Pointer to device tree |
| 53 | */ |
Heinrich Schuchardt | 069079c | 2024-09-17 10:49:29 +0200 | [diff] [blame] | 54 | void efi_try_purge_rng_seed(void *fdt) |
Ilias Apalodimas | e4e5660 | 2022-01-03 14:07:37 +0200 | [diff] [blame] | 55 | { |
Heinrich Schuchardt | 069079c | 2024-09-17 10:49:29 +0200 | [diff] [blame] | 56 | const char * const prop[] = {"kaslr-seed", "rng-seed"}; |
Ilias Apalodimas | e4e5660 | 2022-01-03 14:07:37 +0200 | [diff] [blame] | 57 | const efi_guid_t efi_guid_rng_protocol = EFI_RNG_PROTOCOL_GUID; |
| 58 | struct efi_handler *handler; |
| 59 | efi_status_t ret; |
| 60 | int nodeoff = 0; |
| 61 | int err = 0; |
| 62 | |
| 63 | ret = efi_search_protocol(efi_root, &efi_guid_rng_protocol, &handler); |
| 64 | if (ret != EFI_SUCCESS) |
| 65 | return; |
| 66 | |
| 67 | nodeoff = fdt_path_offset(fdt, "/chosen"); |
| 68 | if (nodeoff < 0) |
| 69 | return; |
| 70 | |
Heinrich Schuchardt | 069079c | 2024-09-17 10:49:29 +0200 | [diff] [blame] | 71 | for (size_t i = 0; i < ARRAY_SIZE(prop); ++i) { |
| 72 | err = fdt_delprop(fdt, nodeoff, prop[i]); |
| 73 | if (err < 0 && err != -FDT_ERR_NOTFOUND) |
| 74 | log_err("Error deleting %s\n", prop[i]); |
| 75 | else |
| 76 | log_debug("Deleted /chosen/%s\n", prop[i]); |
| 77 | } |
Ilias Apalodimas | e4e5660 | 2022-01-03 14:07:37 +0200 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | /** |
Heinrich Schuchardt | 2c1e224 | 2020-12-13 10:30:24 +0100 | [diff] [blame] | 81 | * efi_carve_out_dt_rsv() - Carve out DT reserved memory ranges |
| 82 | * |
| 83 | * The mem_rsv entries of the FDT are added to the memory map. Any failures are |
| 84 | * ignored because this is not critical and we would rather continue to try to |
| 85 | * boot. |
| 86 | * |
| 87 | * @fdt: Pointer to device tree |
| 88 | */ |
| 89 | void efi_carve_out_dt_rsv(void *fdt) |
| 90 | { |
| 91 | int nr_rsv, i; |
| 92 | u64 addr, size; |
| 93 | int nodeoffset, subnode; |
| 94 | |
| 95 | nr_rsv = fdt_num_mem_rsv(fdt); |
| 96 | |
| 97 | /* Look for an existing entry and add it to the efi mem map. */ |
| 98 | for (i = 0; i < nr_rsv; i++) { |
| 99 | if (fdt_get_mem_rsv(fdt, i, &addr, &size) != 0) |
| 100 | continue; |
Mark Kettenis | cdcef02 | 2021-03-14 20:04:24 +0100 | [diff] [blame] | 101 | efi_reserve_memory(addr, size, true); |
Heinrich Schuchardt | 2c1e224 | 2020-12-13 10:30:24 +0100 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | /* process reserved-memory */ |
| 105 | nodeoffset = fdt_subnode_offset(fdt, 0, "reserved-memory"); |
| 106 | if (nodeoffset >= 0) { |
| 107 | subnode = fdt_first_subnode(fdt, nodeoffset); |
| 108 | while (subnode >= 0) { |
| 109 | fdt_addr_t fdt_addr; |
| 110 | fdt_size_t fdt_size; |
| 111 | |
| 112 | /* check if this subnode has a reg property */ |
| 113 | fdt_addr = fdtdec_get_addr_size_auto_parent( |
| 114 | fdt, nodeoffset, subnode, |
| 115 | "reg", 0, &fdt_size, false); |
| 116 | /* |
| 117 | * The /reserved-memory node may have children with |
| 118 | * a size instead of a reg property. |
| 119 | */ |
| 120 | if (fdt_addr != FDT_ADDR_T_NONE && |
| 121 | fdtdec_get_is_enabled(fdt, subnode)) { |
| 122 | bool nomap; |
| 123 | |
| 124 | nomap = !!fdt_getprop(fdt, subnode, "no-map", |
| 125 | NULL); |
| 126 | efi_reserve_memory(fdt_addr, fdt_size, nomap); |
| 127 | } |
| 128 | subnode = fdt_next_subnode(fdt, subnode); |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
Heinrich Schuchardt | 20d48db | 2021-01-16 08:50:10 +0100 | [diff] [blame] | 133 | /** |
| 134 | * efi_dt_fixup() - fix up device tree |
| 135 | * |
| 136 | * This function implements the Fixup() service of the |
| 137 | * EFI Device Tree Fixup Protocol. |
| 138 | * |
| 139 | * @this: instance of the protocol |
| 140 | * @dtb: device tree provided by caller |
| 141 | * @buffer_size: size of buffer for the device tree including free space |
| 142 | * @flags: bit field designating action to be performed |
| 143 | * Return: status code |
| 144 | */ |
Heinrich Schuchardt | 0404b28 | 2021-01-16 09:33:24 +0100 | [diff] [blame] | 145 | static efi_status_t __maybe_unused EFIAPI |
| 146 | efi_dt_fixup(struct efi_dt_fixup_protocol *this, void *dtb, |
| 147 | efi_uintn_t *buffer_size, u32 flags) |
Heinrich Schuchardt | 2c1e224 | 2020-12-13 10:30:24 +0100 | [diff] [blame] | 148 | { |
| 149 | efi_status_t ret; |
| 150 | size_t required_size; |
Heinrich Schuchardt | c57840e | 2021-02-02 07:27:42 +0100 | [diff] [blame] | 151 | size_t total_size; |
Simon Glass | df00afa | 2022-09-06 20:26:50 -0600 | [diff] [blame] | 152 | struct bootm_headers img = { 0 }; |
Heinrich Schuchardt | 2c1e224 | 2020-12-13 10:30:24 +0100 | [diff] [blame] | 153 | |
| 154 | EFI_ENTRY("%p, %p, %p, %d", this, dtb, buffer_size, flags); |
| 155 | |
| 156 | if (this != &efi_dt_fixup_prot || !dtb || !buffer_size || |
| 157 | !flags || (flags & ~EFI_DT_ALL)) { |
| 158 | ret = EFI_INVALID_PARAMETER; |
| 159 | goto out; |
| 160 | } |
| 161 | if (fdt_check_header(dtb)) { |
| 162 | ret = EFI_INVALID_PARAMETER; |
| 163 | goto out; |
| 164 | } |
| 165 | if (flags & EFI_DT_APPLY_FIXUPS) { |
Heinrich Schuchardt | c57840e | 2021-02-02 07:27:42 +0100 | [diff] [blame] | 166 | /* Check size */ |
Heinrich Schuchardt | 2c1e224 | 2020-12-13 10:30:24 +0100 | [diff] [blame] | 167 | required_size = fdt_off_dt_strings(dtb) + |
| 168 | fdt_size_dt_strings(dtb) + |
| 169 | 0x3000; |
Heinrich Schuchardt | c57840e | 2021-02-02 07:27:42 +0100 | [diff] [blame] | 170 | total_size = fdt_totalsize(dtb); |
| 171 | if (required_size < total_size) |
| 172 | required_size = total_size; |
| 173 | if (required_size > *buffer_size) { |
| 174 | *buffer_size = required_size; |
| 175 | ret = EFI_BUFFER_TOO_SMALL; |
| 176 | goto out; |
| 177 | } |
Heinrich Schuchardt | 2c1e224 | 2020-12-13 10:30:24 +0100 | [diff] [blame] | 178 | |
Heinrich Schuchardt | c57840e | 2021-02-02 07:27:42 +0100 | [diff] [blame] | 179 | fdt_set_totalsize(dtb, *buffer_size); |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 180 | if (image_setup_libfdt(&img, dtb, false)) { |
Heinrich Schuchardt | 2c1e224 | 2020-12-13 10:30:24 +0100 | [diff] [blame] | 181 | log_err("failed to process device tree\n"); |
| 182 | ret = EFI_INVALID_PARAMETER; |
| 183 | goto out; |
| 184 | } |
| 185 | } |
| 186 | if (flags & EFI_DT_RESERVE_MEMORY) |
| 187 | efi_carve_out_dt_rsv(dtb); |
| 188 | |
Heinrich Schuchardt | c57840e | 2021-02-02 07:27:42 +0100 | [diff] [blame] | 189 | if (flags & EFI_DT_INSTALL_TABLE) { |
Heinrich Schuchardt | 2c1e224 | 2020-12-13 10:30:24 +0100 | [diff] [blame] | 190 | ret = efi_install_configuration_table(&efi_guid_fdt, dtb); |
| 191 | if (ret != EFI_SUCCESS) { |
Heinrich Schuchardt | c57840e | 2021-02-02 07:27:42 +0100 | [diff] [blame] | 192 | log_err("failed to install device tree\n"); |
Heinrich Schuchardt | 2c1e224 | 2020-12-13 10:30:24 +0100 | [diff] [blame] | 193 | goto out; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | ret = EFI_SUCCESS; |
| 198 | out: |
| 199 | return EFI_EXIT(ret); |
| 200 | } |
Heinrich Schuchardt | 20d48db | 2021-01-16 08:50:10 +0100 | [diff] [blame] | 201 | |
| 202 | struct efi_dt_fixup_protocol efi_dt_fixup_prot = { |
| 203 | .revision = EFI_DT_FIXUP_PROTOCOL_REVISION, |
| 204 | .fixup = efi_dt_fixup |
| 205 | }; |