blob: b6fe5d2e5a34d49ce00e6dbe7a6311749cb0dcc5 [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
8#include <common.h>
9#include <efi_dt_fixup.h>
10#include <efi_loader.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/**
44 * efi_carve_out_dt_rsv() - Carve out DT reserved memory ranges
45 *
46 * The mem_rsv entries of the FDT are added to the memory map. Any failures are
47 * ignored because this is not critical and we would rather continue to try to
48 * boot.
49 *
50 * @fdt: Pointer to device tree
51 */
52void efi_carve_out_dt_rsv(void *fdt)
53{
54 int nr_rsv, i;
55 u64 addr, size;
56 int nodeoffset, subnode;
57
58 nr_rsv = fdt_num_mem_rsv(fdt);
59
60 /* Look for an existing entry and add it to the efi mem map. */
61 for (i = 0; i < nr_rsv; i++) {
62 if (fdt_get_mem_rsv(fdt, i, &addr, &size) != 0)
63 continue;
Mark Ketteniscdcef022021-03-14 20:04:24 +010064 efi_reserve_memory(addr, size, true);
Heinrich Schuchardt2c1e2242020-12-13 10:30:24 +010065 }
66
67 /* process reserved-memory */
68 nodeoffset = fdt_subnode_offset(fdt, 0, "reserved-memory");
69 if (nodeoffset >= 0) {
70 subnode = fdt_first_subnode(fdt, nodeoffset);
71 while (subnode >= 0) {
72 fdt_addr_t fdt_addr;
73 fdt_size_t fdt_size;
74
75 /* check if this subnode has a reg property */
76 fdt_addr = fdtdec_get_addr_size_auto_parent(
77 fdt, nodeoffset, subnode,
78 "reg", 0, &fdt_size, false);
79 /*
80 * The /reserved-memory node may have children with
81 * a size instead of a reg property.
82 */
83 if (fdt_addr != FDT_ADDR_T_NONE &&
84 fdtdec_get_is_enabled(fdt, subnode)) {
85 bool nomap;
86
87 nomap = !!fdt_getprop(fdt, subnode, "no-map",
88 NULL);
89 efi_reserve_memory(fdt_addr, fdt_size, nomap);
90 }
91 subnode = fdt_next_subnode(fdt, subnode);
92 }
93 }
94}
95
Heinrich Schuchardt20d48db2021-01-16 08:50:10 +010096/**
97 * efi_dt_fixup() - fix up device tree
98 *
99 * This function implements the Fixup() service of the
100 * EFI Device Tree Fixup Protocol.
101 *
102 * @this: instance of the protocol
103 * @dtb: device tree provided by caller
104 * @buffer_size: size of buffer for the device tree including free space
105 * @flags: bit field designating action to be performed
106 * Return: status code
107 */
Heinrich Schuchardt0404b282021-01-16 09:33:24 +0100108static efi_status_t __maybe_unused EFIAPI
109efi_dt_fixup(struct efi_dt_fixup_protocol *this, void *dtb,
110 efi_uintn_t *buffer_size, u32 flags)
Heinrich Schuchardt2c1e2242020-12-13 10:30:24 +0100111{
112 efi_status_t ret;
113 size_t required_size;
Heinrich Schuchardtc57840e2021-02-02 07:27:42 +0100114 size_t total_size;
Heinrich Schuchardt2c1e2242020-12-13 10:30:24 +0100115 bootm_headers_t img = { 0 };
116
117 EFI_ENTRY("%p, %p, %p, %d", this, dtb, buffer_size, flags);
118
119 if (this != &efi_dt_fixup_prot || !dtb || !buffer_size ||
120 !flags || (flags & ~EFI_DT_ALL)) {
121 ret = EFI_INVALID_PARAMETER;
122 goto out;
123 }
124 if (fdt_check_header(dtb)) {
125 ret = EFI_INVALID_PARAMETER;
126 goto out;
127 }
128 if (flags & EFI_DT_APPLY_FIXUPS) {
Heinrich Schuchardtc57840e2021-02-02 07:27:42 +0100129 /* Check size */
Heinrich Schuchardt2c1e2242020-12-13 10:30:24 +0100130 required_size = fdt_off_dt_strings(dtb) +
131 fdt_size_dt_strings(dtb) +
132 0x3000;
Heinrich Schuchardtc57840e2021-02-02 07:27:42 +0100133 total_size = fdt_totalsize(dtb);
134 if (required_size < total_size)
135 required_size = total_size;
136 if (required_size > *buffer_size) {
137 *buffer_size = required_size;
138 ret = EFI_BUFFER_TOO_SMALL;
139 goto out;
140 }
Heinrich Schuchardt2c1e2242020-12-13 10:30:24 +0100141
Heinrich Schuchardtc57840e2021-02-02 07:27:42 +0100142 fdt_set_totalsize(dtb, *buffer_size);
Heinrich Schuchardt2c1e2242020-12-13 10:30:24 +0100143 if (image_setup_libfdt(&img, dtb, 0, NULL)) {
144 log_err("failed to process device tree\n");
145 ret = EFI_INVALID_PARAMETER;
146 goto out;
147 }
148 }
149 if (flags & EFI_DT_RESERVE_MEMORY)
150 efi_carve_out_dt_rsv(dtb);
151
Heinrich Schuchardtc57840e2021-02-02 07:27:42 +0100152 if (flags & EFI_DT_INSTALL_TABLE) {
Heinrich Schuchardt2c1e2242020-12-13 10:30:24 +0100153 ret = efi_install_configuration_table(&efi_guid_fdt, dtb);
154 if (ret != EFI_SUCCESS) {
Heinrich Schuchardtc57840e2021-02-02 07:27:42 +0100155 log_err("failed to install device tree\n");
Heinrich Schuchardt2c1e2242020-12-13 10:30:24 +0100156 goto out;
157 }
158 }
159
160 ret = EFI_SUCCESS;
161out:
162 return EFI_EXIT(ret);
163}
Heinrich Schuchardt20d48db2021-01-16 08:50:10 +0100164
165struct efi_dt_fixup_protocol efi_dt_fixup_prot = {
166 .revision = EFI_DT_FIXUP_PROTOCOL_REVISION,
167 .fixup = efi_dt_fixup
168};