blob: 26928cfc454cdba1f325f3aa23de19bb1563f768 [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 Schuchardt955a3212025-01-16 20:26:59 +01008#define LOG_CATEGORY LOGC_EFI
9
Heinrich Schuchardt2c1e2242020-12-13 10:30:24 +010010#include <efi_dt_fixup.h>
11#include <efi_loader.h>
Ilias Apalodimase4e56602022-01-03 14:07:37 +020012#include <efi_rng.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060013#include <fdtdec.h>
Heinrich Schuchardt2c1e2242020-12-13 10:30:24 +010014#include <mapmem.h>
15
Heinrich Schuchardt2c1e2242020-12-13 10:30:24 +010016const efi_guid_t efi_guid_dt_fixup_protocol = EFI_DT_FIXUP_PROTOCOL_GUID;
17
18/**
19 * efi_reserve_memory() - add reserved memory to memory map
20 *
21 * @addr: start address of the reserved memory range
22 * @size: size of the reserved memory range
23 * @nomap: indicates that the memory range shall not be accessed by the
24 * UEFI payload
25 */
26static void efi_reserve_memory(u64 addr, u64 size, bool nomap)
27{
28 int type;
29 efi_uintn_t ret;
30
31 /* Convert from sandbox address space. */
32 addr = (uintptr_t)map_sysmem(addr, 0);
33
34 if (nomap)
35 type = EFI_RESERVED_MEMORY_TYPE;
36 else
37 type = EFI_BOOT_SERVICES_DATA;
38
39 ret = efi_add_memory_map(addr, size, type);
40 if (ret != EFI_SUCCESS)
41 log_err("Reserved memory mapping failed addr %llx size %llx\n",
42 addr, size);
43}
44
45/**
Heinrich Schuchardt069079c2024-09-17 10:49:29 +020046 * efi_try_purge_rng_seed() - Remove unused kaslr-seed, rng-seed
Ilias Apalodimase4e56602022-01-03 14:07:37 +020047 *
48 * Kernel's EFI STUB only relies on EFI_RNG_PROTOCOL for randomization
49 * and completely ignores the kaslr-seed for its own randomness needs
50 * (i.e the randomization of the physical placement of the kernel).
51 * Weed it out from the DTB we hand over, which would mess up our DTB
52 * TPM measurements as well.
53 *
54 * @fdt: Pointer to device tree
55 */
Heinrich Schuchardt069079c2024-09-17 10:49:29 +020056void efi_try_purge_rng_seed(void *fdt)
Ilias Apalodimase4e56602022-01-03 14:07:37 +020057{
Heinrich Schuchardt069079c2024-09-17 10:49:29 +020058 const char * const prop[] = {"kaslr-seed", "rng-seed"};
Ilias Apalodimase4e56602022-01-03 14:07:37 +020059 const efi_guid_t efi_guid_rng_protocol = EFI_RNG_PROTOCOL_GUID;
60 struct efi_handler *handler;
61 efi_status_t ret;
62 int nodeoff = 0;
63 int err = 0;
64
65 ret = efi_search_protocol(efi_root, &efi_guid_rng_protocol, &handler);
66 if (ret != EFI_SUCCESS)
67 return;
68
69 nodeoff = fdt_path_offset(fdt, "/chosen");
70 if (nodeoff < 0)
71 return;
72
Heinrich Schuchardt069079c2024-09-17 10:49:29 +020073 for (size_t i = 0; i < ARRAY_SIZE(prop); ++i) {
74 err = fdt_delprop(fdt, nodeoff, prop[i]);
75 if (err < 0 && err != -FDT_ERR_NOTFOUND)
76 log_err("Error deleting %s\n", prop[i]);
77 else
78 log_debug("Deleted /chosen/%s\n", prop[i]);
79 }
Ilias Apalodimase4e56602022-01-03 14:07:37 +020080}
81
82/**
Heinrich Schuchardt2c1e2242020-12-13 10:30:24 +010083 * efi_carve_out_dt_rsv() - Carve out DT reserved memory ranges
84 *
85 * The mem_rsv entries of the FDT are added to the memory map. Any failures are
86 * ignored because this is not critical and we would rather continue to try to
87 * boot.
88 *
89 * @fdt: Pointer to device tree
90 */
91void efi_carve_out_dt_rsv(void *fdt)
92{
93 int nr_rsv, i;
94 u64 addr, size;
95 int nodeoffset, subnode;
96
97 nr_rsv = fdt_num_mem_rsv(fdt);
98
99 /* Look for an existing entry and add it to the efi mem map. */
100 for (i = 0; i < nr_rsv; i++) {
101 if (fdt_get_mem_rsv(fdt, i, &addr, &size) != 0)
102 continue;
Mark Ketteniscdcef022021-03-14 20:04:24 +0100103 efi_reserve_memory(addr, size, true);
Heinrich Schuchardt2c1e2242020-12-13 10:30:24 +0100104 }
105
106 /* process reserved-memory */
107 nodeoffset = fdt_subnode_offset(fdt, 0, "reserved-memory");
108 if (nodeoffset >= 0) {
109 subnode = fdt_first_subnode(fdt, nodeoffset);
110 while (subnode >= 0) {
111 fdt_addr_t fdt_addr;
112 fdt_size_t fdt_size;
113
114 /* check if this subnode has a reg property */
115 fdt_addr = fdtdec_get_addr_size_auto_parent(
116 fdt, nodeoffset, subnode,
117 "reg", 0, &fdt_size, false);
118 /*
119 * The /reserved-memory node may have children with
120 * a size instead of a reg property.
121 */
122 if (fdt_addr != FDT_ADDR_T_NONE &&
123 fdtdec_get_is_enabled(fdt, subnode)) {
124 bool nomap;
125
126 nomap = !!fdt_getprop(fdt, subnode, "no-map",
127 NULL);
128 efi_reserve_memory(fdt_addr, fdt_size, nomap);
129 }
130 subnode = fdt_next_subnode(fdt, subnode);
131 }
132 }
133}
134
Heinrich Schuchardt20d48db2021-01-16 08:50:10 +0100135/**
136 * efi_dt_fixup() - fix up device tree
137 *
138 * This function implements the Fixup() service of the
139 * EFI Device Tree Fixup Protocol.
140 *
141 * @this: instance of the protocol
142 * @dtb: device tree provided by caller
143 * @buffer_size: size of buffer for the device tree including free space
144 * @flags: bit field designating action to be performed
145 * Return: status code
146 */
Heinrich Schuchardt0404b282021-01-16 09:33:24 +0100147static efi_status_t __maybe_unused EFIAPI
148efi_dt_fixup(struct efi_dt_fixup_protocol *this, void *dtb,
149 efi_uintn_t *buffer_size, u32 flags)
Heinrich Schuchardt2c1e2242020-12-13 10:30:24 +0100150{
151 efi_status_t ret;
152 size_t required_size;
Heinrich Schuchardtc57840e2021-02-02 07:27:42 +0100153 size_t total_size;
Simon Glassdf00afa2022-09-06 20:26:50 -0600154 struct bootm_headers img = { 0 };
Heinrich Schuchardt2c1e2242020-12-13 10:30:24 +0100155
156 EFI_ENTRY("%p, %p, %p, %d", this, dtb, buffer_size, flags);
157
158 if (this != &efi_dt_fixup_prot || !dtb || !buffer_size ||
159 !flags || (flags & ~EFI_DT_ALL)) {
160 ret = EFI_INVALID_PARAMETER;
161 goto out;
162 }
163 if (fdt_check_header(dtb)) {
164 ret = EFI_INVALID_PARAMETER;
165 goto out;
166 }
167 if (flags & EFI_DT_APPLY_FIXUPS) {
Heinrich Schuchardtc57840e2021-02-02 07:27:42 +0100168 /* Check size */
Heinrich Schuchardt2c1e2242020-12-13 10:30:24 +0100169 required_size = fdt_off_dt_strings(dtb) +
170 fdt_size_dt_strings(dtb) +
171 0x3000;
Heinrich Schuchardtc57840e2021-02-02 07:27:42 +0100172 total_size = fdt_totalsize(dtb);
173 if (required_size < total_size)
174 required_size = total_size;
175 if (required_size > *buffer_size) {
176 *buffer_size = required_size;
177 ret = EFI_BUFFER_TOO_SMALL;
178 goto out;
179 }
Heinrich Schuchardt2c1e2242020-12-13 10:30:24 +0100180
Heinrich Schuchardtc57840e2021-02-02 07:27:42 +0100181 fdt_set_totalsize(dtb, *buffer_size);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530182 if (image_setup_libfdt(&img, dtb, false)) {
Heinrich Schuchardt2c1e2242020-12-13 10:30:24 +0100183 log_err("failed to process device tree\n");
184 ret = EFI_INVALID_PARAMETER;
185 goto out;
186 }
187 }
188 if (flags & EFI_DT_RESERVE_MEMORY)
189 efi_carve_out_dt_rsv(dtb);
190
Heinrich Schuchardtc57840e2021-02-02 07:27:42 +0100191 if (flags & EFI_DT_INSTALL_TABLE) {
Heinrich Schuchardt2c1e2242020-12-13 10:30:24 +0100192 ret = efi_install_configuration_table(&efi_guid_fdt, dtb);
193 if (ret != EFI_SUCCESS) {
Heinrich Schuchardtc57840e2021-02-02 07:27:42 +0100194 log_err("failed to install device tree\n");
Heinrich Schuchardt2c1e2242020-12-13 10:30:24 +0100195 goto out;
196 }
197 }
198
199 ret = EFI_SUCCESS;
200out:
201 return EFI_EXIT(ret);
202}
Heinrich Schuchardt20d48db2021-01-16 08:50:10 +0100203
204struct efi_dt_fixup_protocol efi_dt_fixup_prot = {
205 .revision = EFI_DT_FIXUP_PROTOCOL_REVISION,
206 .fixup = efi_dt_fixup
207};