blob: 0dac94b0c6c0f59fc69931177861e1b5170823e9 [file] [log] [blame]
Heinrich Schuchardt2c1e2242020-12-13 10:30:24 +01001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * EFI_DT_FIXUP_PROTOCOL
4 *
5 * Copyright (c) 2020 Heinrich Schuchardt
6 */
7
Heinrich Schuchardt2c1e2242020-12-13 10:30:24 +01008#include <efi_dt_fixup.h>
9#include <efi_loader.h>
Ilias Apalodimase4e56602022-01-03 14:07:37 +020010#include <efi_rng.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060011#include <fdtdec.h>
Heinrich Schuchardt2c1e2242020-12-13 10:30:24 +010012#include <mapmem.h>
13
Heinrich Schuchardt2c1e2242020-12-13 10:30:24 +010014const 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 */
24static 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 Schuchardt069079c2024-09-17 10:49:29 +020044 * efi_try_purge_rng_seed() - Remove unused kaslr-seed, rng-seed
Ilias Apalodimase4e56602022-01-03 14:07:37 +020045 *
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 Schuchardt069079c2024-09-17 10:49:29 +020054void efi_try_purge_rng_seed(void *fdt)
Ilias Apalodimase4e56602022-01-03 14:07:37 +020055{
Heinrich Schuchardt069079c2024-09-17 10:49:29 +020056 const char * const prop[] = {"kaslr-seed", "rng-seed"};
Ilias Apalodimase4e56602022-01-03 14:07:37 +020057 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 Schuchardt069079c2024-09-17 10:49:29 +020071 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 Apalodimase4e56602022-01-03 14:07:37 +020078}
79
80/**
Heinrich Schuchardt2c1e2242020-12-13 10:30:24 +010081 * 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 */
89void 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 Ketteniscdcef022021-03-14 20:04:24 +0100101 efi_reserve_memory(addr, size, true);
Heinrich Schuchardt2c1e2242020-12-13 10:30:24 +0100102 }
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 Schuchardt20d48db2021-01-16 08:50:10 +0100133/**
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 Schuchardt0404b282021-01-16 09:33:24 +0100145static efi_status_t __maybe_unused EFIAPI
146efi_dt_fixup(struct efi_dt_fixup_protocol *this, void *dtb,
147 efi_uintn_t *buffer_size, u32 flags)
Heinrich Schuchardt2c1e2242020-12-13 10:30:24 +0100148{
149 efi_status_t ret;
150 size_t required_size;
Heinrich Schuchardtc57840e2021-02-02 07:27:42 +0100151 size_t total_size;
Simon Glassdf00afa2022-09-06 20:26:50 -0600152 struct bootm_headers img = { 0 };
Heinrich Schuchardt2c1e2242020-12-13 10:30:24 +0100153
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 Schuchardtc57840e2021-02-02 07:27:42 +0100166 /* Check size */
Heinrich Schuchardt2c1e2242020-12-13 10:30:24 +0100167 required_size = fdt_off_dt_strings(dtb) +
168 fdt_size_dt_strings(dtb) +
169 0x3000;
Heinrich Schuchardtc57840e2021-02-02 07:27:42 +0100170 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 Schuchardt2c1e2242020-12-13 10:30:24 +0100178
Heinrich Schuchardtc57840e2021-02-02 07:27:42 +0100179 fdt_set_totalsize(dtb, *buffer_size);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530180 if (image_setup_libfdt(&img, dtb, false)) {
Heinrich Schuchardt2c1e2242020-12-13 10:30:24 +0100181 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 Schuchardtc57840e2021-02-02 07:27:42 +0100189 if (flags & EFI_DT_INSTALL_TABLE) {
Heinrich Schuchardt2c1e2242020-12-13 10:30:24 +0100190 ret = efi_install_configuration_table(&efi_guid_fdt, dtb);
191 if (ret != EFI_SUCCESS) {
Heinrich Schuchardtc57840e2021-02-02 07:27:42 +0100192 log_err("failed to install device tree\n");
Heinrich Schuchardt2c1e2242020-12-13 10:30:24 +0100193 goto out;
194 }
195 }
196
197 ret = EFI_SUCCESS;
198out:
199 return EFI_EXIT(ret);
200}
Heinrich Schuchardt20d48db2021-01-16 08:50:10 +0100201
202struct efi_dt_fixup_protocol efi_dt_fixup_prot = {
203 .revision = EFI_DT_FIXUP_PROTOCOL_REVISION,
204 .fixup = efi_dt_fixup
205};