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 | /** |
Ilias Apalodimas | e4e5660 | 2022-01-03 14:07:37 +0200 | [diff] [blame] | 44 | * efi_try_purge_kaslr_seed() - Remove unused kaslr-seed |
| 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 | */ |
| 54 | void efi_try_purge_kaslr_seed(void *fdt) |
| 55 | { |
| 56 | const efi_guid_t efi_guid_rng_protocol = EFI_RNG_PROTOCOL_GUID; |
| 57 | struct efi_handler *handler; |
| 58 | efi_status_t ret; |
| 59 | int nodeoff = 0; |
| 60 | int err = 0; |
| 61 | |
| 62 | ret = efi_search_protocol(efi_root, &efi_guid_rng_protocol, &handler); |
| 63 | if (ret != EFI_SUCCESS) |
| 64 | return; |
| 65 | |
| 66 | nodeoff = fdt_path_offset(fdt, "/chosen"); |
| 67 | if (nodeoff < 0) |
| 68 | return; |
| 69 | |
| 70 | err = fdt_delprop(fdt, nodeoff, "kaslr-seed"); |
| 71 | if (err < 0 && err != -FDT_ERR_NOTFOUND) |
| 72 | log_err("Error deleting kaslr-seed\n"); |
| 73 | } |
| 74 | |
| 75 | /** |
Heinrich Schuchardt | 2c1e224 | 2020-12-13 10:30:24 +0100 | [diff] [blame] | 76 | * efi_carve_out_dt_rsv() - Carve out DT reserved memory ranges |
| 77 | * |
| 78 | * The mem_rsv entries of the FDT are added to the memory map. Any failures are |
| 79 | * ignored because this is not critical and we would rather continue to try to |
| 80 | * boot. |
| 81 | * |
| 82 | * @fdt: Pointer to device tree |
| 83 | */ |
| 84 | void efi_carve_out_dt_rsv(void *fdt) |
| 85 | { |
| 86 | int nr_rsv, i; |
| 87 | u64 addr, size; |
| 88 | int nodeoffset, subnode; |
| 89 | |
| 90 | nr_rsv = fdt_num_mem_rsv(fdt); |
| 91 | |
| 92 | /* Look for an existing entry and add it to the efi mem map. */ |
| 93 | for (i = 0; i < nr_rsv; i++) { |
| 94 | if (fdt_get_mem_rsv(fdt, i, &addr, &size) != 0) |
| 95 | continue; |
Mark Kettenis | cdcef02 | 2021-03-14 20:04:24 +0100 | [diff] [blame] | 96 | efi_reserve_memory(addr, size, true); |
Heinrich Schuchardt | 2c1e224 | 2020-12-13 10:30:24 +0100 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | /* process reserved-memory */ |
| 100 | nodeoffset = fdt_subnode_offset(fdt, 0, "reserved-memory"); |
| 101 | if (nodeoffset >= 0) { |
| 102 | subnode = fdt_first_subnode(fdt, nodeoffset); |
| 103 | while (subnode >= 0) { |
| 104 | fdt_addr_t fdt_addr; |
| 105 | fdt_size_t fdt_size; |
| 106 | |
| 107 | /* check if this subnode has a reg property */ |
| 108 | fdt_addr = fdtdec_get_addr_size_auto_parent( |
| 109 | fdt, nodeoffset, subnode, |
| 110 | "reg", 0, &fdt_size, false); |
| 111 | /* |
| 112 | * The /reserved-memory node may have children with |
| 113 | * a size instead of a reg property. |
| 114 | */ |
| 115 | if (fdt_addr != FDT_ADDR_T_NONE && |
| 116 | fdtdec_get_is_enabled(fdt, subnode)) { |
| 117 | bool nomap; |
| 118 | |
| 119 | nomap = !!fdt_getprop(fdt, subnode, "no-map", |
| 120 | NULL); |
| 121 | efi_reserve_memory(fdt_addr, fdt_size, nomap); |
| 122 | } |
| 123 | subnode = fdt_next_subnode(fdt, subnode); |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
Heinrich Schuchardt | 20d48db | 2021-01-16 08:50:10 +0100 | [diff] [blame] | 128 | /** |
| 129 | * efi_dt_fixup() - fix up device tree |
| 130 | * |
| 131 | * This function implements the Fixup() service of the |
| 132 | * EFI Device Tree Fixup Protocol. |
| 133 | * |
| 134 | * @this: instance of the protocol |
| 135 | * @dtb: device tree provided by caller |
| 136 | * @buffer_size: size of buffer for the device tree including free space |
| 137 | * @flags: bit field designating action to be performed |
| 138 | * Return: status code |
| 139 | */ |
Heinrich Schuchardt | 0404b28 | 2021-01-16 09:33:24 +0100 | [diff] [blame] | 140 | static efi_status_t __maybe_unused EFIAPI |
| 141 | efi_dt_fixup(struct efi_dt_fixup_protocol *this, void *dtb, |
| 142 | efi_uintn_t *buffer_size, u32 flags) |
Heinrich Schuchardt | 2c1e224 | 2020-12-13 10:30:24 +0100 | [diff] [blame] | 143 | { |
| 144 | efi_status_t ret; |
| 145 | size_t required_size; |
Heinrich Schuchardt | c57840e | 2021-02-02 07:27:42 +0100 | [diff] [blame] | 146 | size_t total_size; |
Simon Glass | df00afa | 2022-09-06 20:26:50 -0600 | [diff] [blame] | 147 | struct bootm_headers img = { 0 }; |
Heinrich Schuchardt | 2c1e224 | 2020-12-13 10:30:24 +0100 | [diff] [blame] | 148 | |
| 149 | EFI_ENTRY("%p, %p, %p, %d", this, dtb, buffer_size, flags); |
| 150 | |
| 151 | if (this != &efi_dt_fixup_prot || !dtb || !buffer_size || |
| 152 | !flags || (flags & ~EFI_DT_ALL)) { |
| 153 | ret = EFI_INVALID_PARAMETER; |
| 154 | goto out; |
| 155 | } |
| 156 | if (fdt_check_header(dtb)) { |
| 157 | ret = EFI_INVALID_PARAMETER; |
| 158 | goto out; |
| 159 | } |
| 160 | if (flags & EFI_DT_APPLY_FIXUPS) { |
Heinrich Schuchardt | c57840e | 2021-02-02 07:27:42 +0100 | [diff] [blame] | 161 | /* Check size */ |
Heinrich Schuchardt | 2c1e224 | 2020-12-13 10:30:24 +0100 | [diff] [blame] | 162 | required_size = fdt_off_dt_strings(dtb) + |
| 163 | fdt_size_dt_strings(dtb) + |
| 164 | 0x3000; |
Heinrich Schuchardt | c57840e | 2021-02-02 07:27:42 +0100 | [diff] [blame] | 165 | total_size = fdt_totalsize(dtb); |
| 166 | if (required_size < total_size) |
| 167 | required_size = total_size; |
| 168 | if (required_size > *buffer_size) { |
| 169 | *buffer_size = required_size; |
| 170 | ret = EFI_BUFFER_TOO_SMALL; |
| 171 | goto out; |
| 172 | } |
Heinrich Schuchardt | 2c1e224 | 2020-12-13 10:30:24 +0100 | [diff] [blame] | 173 | |
Heinrich Schuchardt | c57840e | 2021-02-02 07:27:42 +0100 | [diff] [blame] | 174 | fdt_set_totalsize(dtb, *buffer_size); |
Simon Glass | 30762dd | 2023-11-12 08:27:44 -0700 | [diff] [blame] | 175 | if (image_setup_libfdt(&img, dtb, NULL)) { |
Heinrich Schuchardt | 2c1e224 | 2020-12-13 10:30:24 +0100 | [diff] [blame] | 176 | log_err("failed to process device tree\n"); |
| 177 | ret = EFI_INVALID_PARAMETER; |
| 178 | goto out; |
| 179 | } |
| 180 | } |
| 181 | if (flags & EFI_DT_RESERVE_MEMORY) |
| 182 | efi_carve_out_dt_rsv(dtb); |
| 183 | |
Heinrich Schuchardt | c57840e | 2021-02-02 07:27:42 +0100 | [diff] [blame] | 184 | if (flags & EFI_DT_INSTALL_TABLE) { |
Heinrich Schuchardt | 2c1e224 | 2020-12-13 10:30:24 +0100 | [diff] [blame] | 185 | ret = efi_install_configuration_table(&efi_guid_fdt, dtb); |
| 186 | if (ret != EFI_SUCCESS) { |
Heinrich Schuchardt | c57840e | 2021-02-02 07:27:42 +0100 | [diff] [blame] | 187 | log_err("failed to install device tree\n"); |
Heinrich Schuchardt | 2c1e224 | 2020-12-13 10:30:24 +0100 | [diff] [blame] | 188 | goto out; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | ret = EFI_SUCCESS; |
| 193 | out: |
| 194 | return EFI_EXIT(ret); |
| 195 | } |
Heinrich Schuchardt | 20d48db | 2021-01-16 08:50:10 +0100 | [diff] [blame] | 196 | |
| 197 | struct efi_dt_fixup_protocol efi_dt_fixup_prot = { |
| 198 | .revision = EFI_DT_FIXUP_PROTOCOL_REVISION, |
| 199 | .fixup = efi_dt_fixup |
| 200 | }; |