blob: c2f2daef3376582252da4175c9aa41f64055f8c9 [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>
11#include <mapmem.h>
12
Heinrich Schuchardt2c1e2242020-12-13 10:30:24 +010013const efi_guid_t efi_guid_dt_fixup_protocol = EFI_DT_FIXUP_PROTOCOL_GUID;
14
15/**
16 * efi_reserve_memory() - add reserved memory to memory map
17 *
18 * @addr: start address of the reserved memory range
19 * @size: size of the reserved memory range
20 * @nomap: indicates that the memory range shall not be accessed by the
21 * UEFI payload
22 */
23static void efi_reserve_memory(u64 addr, u64 size, bool nomap)
24{
25 int type;
26 efi_uintn_t ret;
27
28 /* Convert from sandbox address space. */
29 addr = (uintptr_t)map_sysmem(addr, 0);
30
31 if (nomap)
32 type = EFI_RESERVED_MEMORY_TYPE;
33 else
34 type = EFI_BOOT_SERVICES_DATA;
35
36 ret = efi_add_memory_map(addr, size, type);
37 if (ret != EFI_SUCCESS)
38 log_err("Reserved memory mapping failed addr %llx size %llx\n",
39 addr, size);
40}
41
42/**
43 * efi_carve_out_dt_rsv() - Carve out DT reserved memory ranges
44 *
45 * The mem_rsv entries of the FDT are added to the memory map. Any failures are
46 * ignored because this is not critical and we would rather continue to try to
47 * boot.
48 *
49 * @fdt: Pointer to device tree
50 */
51void efi_carve_out_dt_rsv(void *fdt)
52{
53 int nr_rsv, i;
54 u64 addr, size;
55 int nodeoffset, subnode;
56
57 nr_rsv = fdt_num_mem_rsv(fdt);
58
59 /* Look for an existing entry and add it to the efi mem map. */
60 for (i = 0; i < nr_rsv; i++) {
61 if (fdt_get_mem_rsv(fdt, i, &addr, &size) != 0)
62 continue;
63 efi_reserve_memory(addr, size, false);
64 }
65
66 /* process reserved-memory */
67 nodeoffset = fdt_subnode_offset(fdt, 0, "reserved-memory");
68 if (nodeoffset >= 0) {
69 subnode = fdt_first_subnode(fdt, nodeoffset);
70 while (subnode >= 0) {
71 fdt_addr_t fdt_addr;
72 fdt_size_t fdt_size;
73
74 /* check if this subnode has a reg property */
75 fdt_addr = fdtdec_get_addr_size_auto_parent(
76 fdt, nodeoffset, subnode,
77 "reg", 0, &fdt_size, false);
78 /*
79 * The /reserved-memory node may have children with
80 * a size instead of a reg property.
81 */
82 if (fdt_addr != FDT_ADDR_T_NONE &&
83 fdtdec_get_is_enabled(fdt, subnode)) {
84 bool nomap;
85
86 nomap = !!fdt_getprop(fdt, subnode, "no-map",
87 NULL);
88 efi_reserve_memory(fdt_addr, fdt_size, nomap);
89 }
90 subnode = fdt_next_subnode(fdt, subnode);
91 }
92 }
93}
94
Heinrich Schuchardt20d48db2021-01-16 08:50:10 +010095/**
96 * efi_dt_fixup() - fix up device tree
97 *
98 * This function implements the Fixup() service of the
99 * EFI Device Tree Fixup Protocol.
100 *
101 * @this: instance of the protocol
102 * @dtb: device tree provided by caller
103 * @buffer_size: size of buffer for the device tree including free space
104 * @flags: bit field designating action to be performed
105 * Return: status code
106 */
Heinrich Schuchardt2c1e2242020-12-13 10:30:24 +0100107static efi_status_t EFIAPI efi_dt_fixup(struct efi_dt_fixup_protocol *this,
108 void *dtb,
109 efi_uintn_t *buffer_size,
110 u32 flags)
111{
112 efi_status_t ret;
113 size_t required_size;
114 bootm_headers_t img = { 0 };
115
116 EFI_ENTRY("%p, %p, %p, %d", this, dtb, buffer_size, flags);
117
118 if (this != &efi_dt_fixup_prot || !dtb || !buffer_size ||
119 !flags || (flags & ~EFI_DT_ALL)) {
120 ret = EFI_INVALID_PARAMETER;
121 goto out;
122 }
123 if (fdt_check_header(dtb)) {
124 ret = EFI_INVALID_PARAMETER;
125 goto out;
126 }
127 if (flags & EFI_DT_APPLY_FIXUPS) {
128 required_size = fdt_off_dt_strings(dtb) +
129 fdt_size_dt_strings(dtb) +
130 0x3000;
131 } else {
132 required_size = fdt_totalsize(dtb);
133 }
134 if (required_size > *buffer_size) {
135 *buffer_size = required_size;
136 ret = EFI_BUFFER_TOO_SMALL;
137 goto out;
138 }
139 fdt_set_totalsize(dtb, *buffer_size);
140
141 if (flags & EFI_DT_APPLY_FIXUPS) {
142 if (image_setup_libfdt(&img, dtb, 0, NULL)) {
143 log_err("failed to process device tree\n");
144 ret = EFI_INVALID_PARAMETER;
145 goto out;
146 }
147 }
148 if (flags & EFI_DT_RESERVE_MEMORY)
149 efi_carve_out_dt_rsv(dtb);
150
151 if (EFI_DT_INSTALL_TABLE) {
152 ret = efi_install_configuration_table(&efi_guid_fdt, dtb);
153 if (ret != EFI_SUCCESS) {
154 log_err("ERROR: failed to install device tree\n");
155 goto out;
156 }
157 }
158
159 ret = EFI_SUCCESS;
160out:
161 return EFI_EXIT(ret);
162}
Heinrich Schuchardt20d48db2021-01-16 08:50:10 +0100163
164struct efi_dt_fixup_protocol efi_dt_fixup_prot = {
165 .revision = EFI_DT_FIXUP_PROTOCOL_REVISION,
166 .fixup = efi_dt_fixup
167};