blob: 352f2545dbb446bf88b7ee8fc008835d11037722 [file] [log] [blame]
Patrick Georgiafd4c872020-05-05 23:43:18 +02001/* Taken from depthcharge: src/base/device_tree.c */
Patrick Georgiac959032020-05-05 22:49:26 +02002/* SPDX-License-Identifier: GPL-2.0-or-later */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02003
4#include <assert.h>
Julius Werner9636a102019-05-03 17:36:43 -07005#include <commonlib/stdlib.h>
Patrick Rudolph666c1722018-04-03 09:57:33 +02006#include <console/console.h>
Joel Kitching393c71c2019-06-16 16:09:42 +08007#include <ctype.h>
Patrick Rudolph666c1722018-04-03 09:57:33 +02008#include <device_tree.h>
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02009#include <endian.h>
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020010#include <stdint.h>
Patrick Rudolph666c1722018-04-03 09:57:33 +020011#include <string.h>
12#include <stddef.h>
13#include <stdlib.h>
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020014
15/*
16 * Functions for picking apart flattened trees.
17 */
18
Patrick Rudolph0a7d6902018-08-22 09:55:15 +020019int fdt_next_property(const void *blob, uint32_t offset,
20 struct fdt_property *prop)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020021{
Patrick Rudolph666c1722018-04-03 09:57:33 +020022 struct fdt_header *header = (struct fdt_header *)blob;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020023 uint32_t *ptr = (uint32_t *)(((uint8_t *)blob) + offset);
24
25 int index = 0;
Patrick Rudolph666c1722018-04-03 09:57:33 +020026 if (be32toh(ptr[index++]) != FDT_TOKEN_PROPERTY)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020027 return 0;
28
Patrick Rudolph666c1722018-04-03 09:57:33 +020029 uint32_t size = be32toh(ptr[index++]);
30 uint32_t name_offset = be32toh(ptr[index++]);
31 name_offset += be32toh(header->strings_offset);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020032
33 if (prop) {
34 prop->name = (char *)((uint8_t *)blob + name_offset);
35 prop->data = &ptr[index];
36 prop->size = size;
37 }
38
Patrick Rudolph666c1722018-04-03 09:57:33 +020039 index += DIV_ROUND_UP(size, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020040
Patrick Rudolph666c1722018-04-03 09:57:33 +020041 return index * sizeof(uint32_t);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020042}
43
Patrick Rudolph0a7d6902018-08-22 09:55:15 +020044int fdt_node_name(const void *blob, uint32_t offset, const char **name)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020045{
46 uint8_t *ptr = ((uint8_t *)blob) + offset;
Julius Wernera5ea3a22019-05-07 17:38:12 -070047 if (be32dec(ptr) != FDT_TOKEN_BEGIN_NODE)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020048 return 0;
49
50 ptr += 4;
51 if (name)
52 *name = (char *)ptr;
Patrick Rudolph666c1722018-04-03 09:57:33 +020053 return ALIGN_UP(strlen((char *)ptr) + 1, sizeof(uint32_t)) + 4;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020054}
55
Julius Werner6702b682019-05-03 18:13:53 -070056static int dt_prop_is_phandle(struct device_tree_property *prop)
57{
58 return !(strcmp("phandle", prop->prop.name) &&
59 strcmp("linux,phandle", prop->prop.name));
60}
61
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020062
63
64/*
65 * Functions for printing flattened trees.
66 */
67
68static void print_indent(int depth)
69{
Julius Werner0d746532019-05-06 19:35:56 -070070 printk(BIOS_DEBUG, "%*s", depth * 8, "");
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020071}
72
Patrick Rudolph0a7d6902018-08-22 09:55:15 +020073static void print_property(const struct fdt_property *prop, int depth)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020074{
Julius Werner0d746532019-05-06 19:35:56 -070075 int is_string = prop->size > 0 &&
76 ((char *)prop->data)[prop->size - 1] == '\0';
77
78 if (is_string)
79 for (const char *c = prop->data; *c != '\0'; c++)
80 if (!isprint(*c))
81 is_string = 0;
82
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020083 print_indent(depth);
Julius Werner0d746532019-05-06 19:35:56 -070084 if (is_string) {
85 printk(BIOS_DEBUG, "%s = \"%s\";\n",
86 prop->name, (const char *)prop->data);
87 } else {
88 printk(BIOS_DEBUG, "%s = < ", prop->name);
89 for (int i = 0; i < MIN(128, prop->size); i += 4) {
90 uint32_t val = 0;
91 for (int j = 0; j < MIN(4, prop->size - i); j++)
92 val |= ((uint8_t *)prop->data)[i + j] <<
93 (24 - j * 8);
94 printk(BIOS_DEBUG, "%#.2x ", val);
95 }
96 if (prop->size > 128)
97 printk(BIOS_DEBUG, "...");
98 printk(BIOS_DEBUG, ">;\n");
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020099 }
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200100}
101
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200102static int print_flat_node(const void *blob, uint32_t start_offset, int depth)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200103{
104 int offset = start_offset;
105 const char *name;
106 int size;
107
108 size = fdt_node_name(blob, offset, &name);
109 if (!size)
110 return 0;
111 offset += size;
112
113 print_indent(depth);
Julius Werner0d746532019-05-06 19:35:56 -0700114 printk(BIOS_DEBUG, "%s {\n", name);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200115
Patrick Rudolph666c1722018-04-03 09:57:33 +0200116 struct fdt_property prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200117 while ((size = fdt_next_property(blob, offset, &prop))) {
118 print_property(&prop, depth + 1);
119
120 offset += size;
121 }
122
Julius Werner23df4772019-05-17 22:50:18 -0700123 printk(BIOS_DEBUG, "\n"); /* empty line between props and nodes */
Julius Werner0d746532019-05-06 19:35:56 -0700124
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200125 while ((size = print_flat_node(blob, offset, depth + 1)))
126 offset += size;
127
Julius Werner0d746532019-05-06 19:35:56 -0700128 print_indent(depth);
129 printk(BIOS_DEBUG, "}\n");
130
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200131 return offset - start_offset + sizeof(uint32_t);
132}
133
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200134void fdt_print_node(const void *blob, uint32_t offset)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200135{
136 print_flat_node(blob, offset, 0);
137}
138
139
140
141/*
142 * A utility function to skip past nodes in flattened trees.
143 */
144
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200145int fdt_skip_node(const void *blob, uint32_t start_offset)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200146{
147 int offset = start_offset;
148 int size;
149
150 const char *name;
151 size = fdt_node_name(blob, offset, &name);
152 if (!size)
153 return 0;
154 offset += size;
155
156 while ((size = fdt_next_property(blob, offset, NULL)))
157 offset += size;
158
159 while ((size = fdt_skip_node(blob, offset)))
160 offset += size;
161
162 return offset - start_offset + sizeof(uint32_t);
163}
164
165
166
167/*
168 * Functions to turn a flattened tree into an unflattened one.
169 */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200170
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200171static int fdt_unflatten_node(const void *blob, uint32_t start_offset,
Julius Werner6702b682019-05-03 18:13:53 -0700172 struct device_tree *tree,
Patrick Rudolph666c1722018-04-03 09:57:33 +0200173 struct device_tree_node **new_node)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200174{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200175 struct list_node *last;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200176 int offset = start_offset;
177 const char *name;
178 int size;
179
180 size = fdt_node_name(blob, offset, &name);
181 if (!size)
182 return 0;
183 offset += size;
184
Julius Werner9636a102019-05-03 17:36:43 -0700185 struct device_tree_node *node = xzalloc(sizeof(*node));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200186 *new_node = node;
187 node->name = name;
188
Patrick Rudolph666c1722018-04-03 09:57:33 +0200189 struct fdt_property fprop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200190 last = &node->properties;
191 while ((size = fdt_next_property(blob, offset, &fprop))) {
Julius Werner9636a102019-05-03 17:36:43 -0700192 struct device_tree_property *prop = xzalloc(sizeof(*prop));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200193 prop->prop = fprop;
194
Julius Werner6702b682019-05-03 18:13:53 -0700195 if (dt_prop_is_phandle(prop)) {
196 node->phandle = be32dec(prop->prop.data);
197 if (node->phandle > tree->max_phandle)
198 tree->max_phandle = node->phandle;
199 }
200
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200201 list_insert_after(&prop->list_node, last);
202 last = &prop->list_node;
203
204 offset += size;
205 }
206
Patrick Rudolph666c1722018-04-03 09:57:33 +0200207 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200208 last = &node->children;
Julius Werner6702b682019-05-03 18:13:53 -0700209 while ((size = fdt_unflatten_node(blob, offset, tree, &child))) {
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200210 list_insert_after(&child->list_node, last);
211 last = &child->list_node;
212
213 offset += size;
214 }
215
216 return offset - start_offset + sizeof(uint32_t);
217}
218
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200219static int fdt_unflatten_map_entry(const void *blob, uint32_t offset,
Patrick Rudolph666c1722018-04-03 09:57:33 +0200220 struct device_tree_reserve_map_entry **new)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200221{
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200222 const uint64_t *ptr = (const uint64_t *)(((uint8_t *)blob) + offset);
223 const uint64_t start = be64toh(ptr[0]);
224 const uint64_t size = be64toh(ptr[1]);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200225
226 if (!size)
227 return 0;
228
Julius Werner9636a102019-05-03 17:36:43 -0700229 struct device_tree_reserve_map_entry *entry = xzalloc(sizeof(*entry));
Patrick Rudolph666c1722018-04-03 09:57:33 +0200230 *new = entry;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200231 entry->start = start;
232 entry->size = size;
233
234 return sizeof(uint64_t) * 2;
235}
236
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200237struct device_tree *fdt_unflatten(const void *blob)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200238{
Julius Werner9636a102019-05-03 17:36:43 -0700239 struct device_tree *tree = xzalloc(sizeof(*tree));
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200240 const struct fdt_header *header = (const struct fdt_header *)blob;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200241 tree->header = header;
242
Julius Werner73eaec82019-05-03 17:58:07 -0700243 uint32_t magic = be32toh(header->magic);
244 uint32_t version = be32toh(header->version);
245 uint32_t last_comp_version = be32toh(header->last_comp_version);
246
247 if (magic != FDT_HEADER_MAGIC) {
248 printk(BIOS_DEBUG, "Invalid device tree magic %#.8x!\n", magic);
Jacob Garber698d83a2019-06-07 10:28:54 -0600249 free(tree);
Julius Werner73eaec82019-05-03 17:58:07 -0700250 return NULL;
251 }
252 if (last_comp_version > FDT_SUPPORTED_VERSION) {
253 printk(BIOS_DEBUG, "Unsupported device tree version %u(>=%u)\n",
254 version, last_comp_version);
Jacob Garber698d83a2019-06-07 10:28:54 -0600255 free(tree);
Julius Werner73eaec82019-05-03 17:58:07 -0700256 return NULL;
257 }
258 if (version > FDT_SUPPORTED_VERSION)
Elyes Haouasd7326282022-12-21 08:16:03 +0100259 printk(BIOS_NOTICE, "FDT version %u too new, should add support!\n",
Julius Werner73eaec82019-05-03 17:58:07 -0700260 version);
261
Patrick Rudolph666c1722018-04-03 09:57:33 +0200262 uint32_t struct_offset = be32toh(header->structure_offset);
263 uint32_t strings_offset = be32toh(header->strings_offset);
264 uint32_t reserve_offset = be32toh(header->reserve_map_offset);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200265 uint32_t min_offset = 0;
266 min_offset = MIN(struct_offset, strings_offset);
267 min_offset = MIN(min_offset, reserve_offset);
Julius Werner23df4772019-05-17 22:50:18 -0700268 /* Assume everything up to the first non-header component is part of
269 the header and needs to be preserved. This will protect us against
270 new elements being added in the future. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200271 tree->header_size = min_offset;
272
Patrick Rudolph666c1722018-04-03 09:57:33 +0200273 struct device_tree_reserve_map_entry *entry;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200274 uint32_t offset = reserve_offset;
275 int size;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200276 struct list_node *last = &tree->reserve_map;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200277 while ((size = fdt_unflatten_map_entry(blob, offset, &entry))) {
278 list_insert_after(&entry->list_node, last);
279 last = &entry->list_node;
280
281 offset += size;
282 }
283
Julius Werner6702b682019-05-03 18:13:53 -0700284 fdt_unflatten_node(blob, struct_offset, tree, &tree->root);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200285
286 return tree;
287}
288
289
290
291/*
Patrick Rudolph666c1722018-04-03 09:57:33 +0200292 * Functions to find the size of the device tree if it was flattened.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200293 */
294
Patrick Rudolph666c1722018-04-03 09:57:33 +0200295static void dt_flat_prop_size(struct device_tree_property *prop,
296 uint32_t *struct_size, uint32_t *strings_size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200297{
Julius Werner23df4772019-05-17 22:50:18 -0700298 /* Starting token. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200299 *struct_size += sizeof(uint32_t);
Julius Werner23df4772019-05-17 22:50:18 -0700300 /* Size. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200301 *struct_size += sizeof(uint32_t);
Julius Werner23df4772019-05-17 22:50:18 -0700302 /* Name offset. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200303 *struct_size += sizeof(uint32_t);
Julius Werner23df4772019-05-17 22:50:18 -0700304 /* Property value. */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200305 *struct_size += ALIGN_UP(prop->prop.size, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200306
Julius Werner23df4772019-05-17 22:50:18 -0700307 /* Property name. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200308 *strings_size += strlen(prop->prop.name) + 1;
309}
310
Patrick Rudolph666c1722018-04-03 09:57:33 +0200311static void dt_flat_node_size(struct device_tree_node *node,
312 uint32_t *struct_size, uint32_t *strings_size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200313{
Julius Werner23df4772019-05-17 22:50:18 -0700314 /* Starting token. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200315 *struct_size += sizeof(uint32_t);
Julius Werner23df4772019-05-17 22:50:18 -0700316 /* Node name. */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200317 *struct_size += ALIGN_UP(strlen(node->name) + 1, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200318
Patrick Rudolph666c1722018-04-03 09:57:33 +0200319 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200320 list_for_each(prop, node->properties, list_node)
321 dt_flat_prop_size(prop, struct_size, strings_size);
322
Patrick Rudolph666c1722018-04-03 09:57:33 +0200323 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200324 list_for_each(child, node->children, list_node)
325 dt_flat_node_size(child, struct_size, strings_size);
326
Julius Werner23df4772019-05-17 22:50:18 -0700327 /* End token. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200328 *struct_size += sizeof(uint32_t);
329}
330
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200331uint32_t dt_flat_size(const struct device_tree *tree)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200332{
333 uint32_t size = tree->header_size;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200334 struct device_tree_reserve_map_entry *entry;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200335 list_for_each(entry, tree->reserve_map, list_node)
336 size += sizeof(uint64_t) * 2;
337 size += sizeof(uint64_t) * 2;
338
339 uint32_t struct_size = 0;
340 uint32_t strings_size = 0;
341 dt_flat_node_size(tree->root, &struct_size, &strings_size);
342
343 size += struct_size;
Julius Werner23df4772019-05-17 22:50:18 -0700344 /* End token. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200345 size += sizeof(uint32_t);
346
347 size += strings_size;
348
349 return size;
350}
351
352
353
354/*
355 * Functions to flatten a device tree.
356 */
357
Patrick Rudolph666c1722018-04-03 09:57:33 +0200358static void dt_flatten_map_entry(struct device_tree_reserve_map_entry *entry,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200359 void **map_start)
360{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200361 ((uint64_t *)*map_start)[0] = htobe64(entry->start);
362 ((uint64_t *)*map_start)[1] = htobe64(entry->size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200363 *map_start = ((uint8_t *)*map_start) + sizeof(uint64_t) * 2;
364}
365
Patrick Rudolph666c1722018-04-03 09:57:33 +0200366static void dt_flatten_prop(struct device_tree_property *prop,
367 void **struct_start, void *strings_base,
368 void **strings_start)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200369{
370 uint8_t *dstruct = (uint8_t *)*struct_start;
371 uint8_t *dstrings = (uint8_t *)*strings_start;
372
Julius Wernera5ea3a22019-05-07 17:38:12 -0700373 be32enc(dstruct, FDT_TOKEN_PROPERTY);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200374 dstruct += sizeof(uint32_t);
375
Julius Wernera5ea3a22019-05-07 17:38:12 -0700376 be32enc(dstruct, prop->prop.size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200377 dstruct += sizeof(uint32_t);
378
379 uint32_t name_offset = (uintptr_t)dstrings - (uintptr_t)strings_base;
Julius Wernera5ea3a22019-05-07 17:38:12 -0700380 be32enc(dstruct, name_offset);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200381 dstruct += sizeof(uint32_t);
382
383 strcpy((char *)dstrings, prop->prop.name);
384 dstrings += strlen(prop->prop.name) + 1;
385
386 memcpy(dstruct, prop->prop.data, prop->prop.size);
Patrick Rudolph666c1722018-04-03 09:57:33 +0200387 dstruct += ALIGN_UP(prop->prop.size, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200388
389 *struct_start = dstruct;
390 *strings_start = dstrings;
391}
392
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200393static void dt_flatten_node(const struct device_tree_node *node,
394 void **struct_start, void *strings_base,
395 void **strings_start)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200396{
397 uint8_t *dstruct = (uint8_t *)*struct_start;
398 uint8_t *dstrings = (uint8_t *)*strings_start;
399
Julius Wernera5ea3a22019-05-07 17:38:12 -0700400 be32enc(dstruct, FDT_TOKEN_BEGIN_NODE);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200401 dstruct += sizeof(uint32_t);
402
403 strcpy((char *)dstruct, node->name);
Patrick Rudolph666c1722018-04-03 09:57:33 +0200404 dstruct += ALIGN_UP(strlen(node->name) + 1, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200405
Patrick Rudolph666c1722018-04-03 09:57:33 +0200406 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200407 list_for_each(prop, node->properties, list_node)
408 dt_flatten_prop(prop, (void **)&dstruct, strings_base,
409 (void **)&dstrings);
410
Patrick Rudolph666c1722018-04-03 09:57:33 +0200411 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200412 list_for_each(child, node->children, list_node)
413 dt_flatten_node(child, (void **)&dstruct, strings_base,
414 (void **)&dstrings);
415
Julius Wernera5ea3a22019-05-07 17:38:12 -0700416 be32enc(dstruct, FDT_TOKEN_END_NODE);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200417 dstruct += sizeof(uint32_t);
418
419 *struct_start = dstruct;
420 *strings_start = dstrings;
421}
422
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200423void dt_flatten(const struct device_tree *tree, void *start_dest)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200424{
425 uint8_t *dest = (uint8_t *)start_dest;
426
427 memcpy(dest, tree->header, tree->header_size);
Patrick Rudolph666c1722018-04-03 09:57:33 +0200428 struct fdt_header *header = (struct fdt_header *)dest;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200429 dest += tree->header_size;
430
Patrick Rudolph666c1722018-04-03 09:57:33 +0200431 struct device_tree_reserve_map_entry *entry;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200432 list_for_each(entry, tree->reserve_map, list_node)
433 dt_flatten_map_entry(entry, (void **)&dest);
434 ((uint64_t *)dest)[0] = ((uint64_t *)dest)[1] = 0;
435 dest += sizeof(uint64_t) * 2;
436
437 uint32_t struct_size = 0;
438 uint32_t strings_size = 0;
439 dt_flat_node_size(tree->root, &struct_size, &strings_size);
440
441 uint8_t *struct_start = dest;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200442 header->structure_offset = htobe32(dest - (uint8_t *)start_dest);
443 header->structure_size = htobe32(struct_size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200444 dest += struct_size;
445
Patrick Rudolph666c1722018-04-03 09:57:33 +0200446 *((uint32_t *)dest) = htobe32(FDT_TOKEN_END);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200447 dest += sizeof(uint32_t);
448
449 uint8_t *strings_start = dest;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200450 header->strings_offset = htobe32(dest - (uint8_t *)start_dest);
451 header->strings_size = htobe32(strings_size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200452 dest += strings_size;
453
454 dt_flatten_node(tree->root, (void **)&struct_start, strings_start,
455 (void **)&strings_start);
456
Patrick Rudolph666c1722018-04-03 09:57:33 +0200457 header->totalsize = htobe32(dest - (uint8_t *)start_dest);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200458}
459
460
461
462/*
463 * Functions for printing a non-flattened device tree.
464 */
465
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200466static void print_node(const struct device_tree_node *node, int depth)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200467{
468 print_indent(depth);
Julius Werner23df4772019-05-17 22:50:18 -0700469 if (depth == 0) /* root node has no name, print a starting slash */
Julius Werner0d746532019-05-06 19:35:56 -0700470 printk(BIOS_DEBUG, "/");
471 printk(BIOS_DEBUG, "%s {\n", node->name);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200472
Patrick Rudolph666c1722018-04-03 09:57:33 +0200473 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200474 list_for_each(prop, node->properties, list_node)
475 print_property(&prop->prop, depth + 1);
476
Julius Werner23df4772019-05-17 22:50:18 -0700477 printk(BIOS_DEBUG, "\n"); /* empty line between props and nodes */
Julius Werner0d746532019-05-06 19:35:56 -0700478
Patrick Rudolph666c1722018-04-03 09:57:33 +0200479 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200480 list_for_each(child, node->children, list_node)
481 print_node(child, depth + 1);
Julius Werner0d746532019-05-06 19:35:56 -0700482
483 print_indent(depth);
484 printk(BIOS_DEBUG, "};\n");
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200485}
486
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200487void dt_print_node(const struct device_tree_node *node)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200488{
489 print_node(node, 0);
490}
491
492
493
494/*
495 * Functions for reading and manipulating an unflattened device tree.
496 */
497
498/*
499 * Read #address-cells and #size-cells properties from a node.
500 *
501 * @param node The device tree node to read from.
502 * @param addrcp Pointer to store #address-cells in, skipped if NULL.
503 * @param sizecp Pointer to store #size-cells in, skipped if NULL.
504 */
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200505void dt_read_cell_props(const struct device_tree_node *node, u32 *addrcp,
506 u32 *sizecp)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200507{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200508 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200509 list_for_each(prop, node->properties, list_node) {
510 if (addrcp && !strcmp("#address-cells", prop->prop.name))
Julius Wernera5ea3a22019-05-07 17:38:12 -0700511 *addrcp = be32dec(prop->prop.data);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200512 if (sizecp && !strcmp("#size-cells", prop->prop.name))
Julius Wernera5ea3a22019-05-07 17:38:12 -0700513 *sizecp = be32dec(prop->prop.data);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200514 }
515}
516
517/*
518 * Find a node from a device tree path, relative to a parent node.
519 *
520 * @param parent The node from which to start the relative path lookup.
521 * @param path An array of path component strings that will be looked
Elyes HAOUASe3e3f4f2018-06-29 21:41:41 +0200522 * up in order to find the node. Must be terminated with
523 * a NULL pointer. Example: {'firmware', 'coreboot', NULL}
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200524 * @param addrcp Pointer that will be updated with any #address-cells
Elyes HAOUASe3e3f4f2018-06-29 21:41:41 +0200525 * value found in the path. May be NULL to ignore.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200526 * @param sizecp Pointer that will be updated with any #size-cells
Elyes HAOUASe3e3f4f2018-06-29 21:41:41 +0200527 * value found in the path. May be NULL to ignore.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200528 * @param create 1: Create node(s) if not found. 0: Return NULL instead.
529 * @return The found/created node, or NULL.
530 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200531struct device_tree_node *dt_find_node(struct device_tree_node *parent,
532 const char **path, u32 *addrcp,
533 u32 *sizecp, int create)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200534{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200535 struct device_tree_node *node, *found = NULL;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200536
Julius Werner23df4772019-05-17 22:50:18 -0700537 /* Update #address-cells and #size-cells for this level. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200538 dt_read_cell_props(parent, addrcp, sizecp);
539
540 if (!*path)
541 return parent;
542
Julius Werner23df4772019-05-17 22:50:18 -0700543 /* Find the next node in the path, if it exists. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200544 list_for_each(node, parent->children, list_node) {
545 if (!strcmp(node->name, *path)) {
546 found = node;
547 break;
548 }
549 }
550
Julius Werner23df4772019-05-17 22:50:18 -0700551 /* Otherwise create it or return NULL. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200552 if (!found) {
553 if (!create)
554 return NULL;
555
Sergii Dmytruk206328d2022-03-13 18:23:17 +0200556 found = calloc(1, sizeof(*found));
Patrick Rudolph666c1722018-04-03 09:57:33 +0200557 if (!found)
558 return NULL;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200559 found->name = strdup(*path);
560 if (!found->name)
561 return NULL;
562
563 list_insert_after(&found->list_node, &parent->children);
564 }
565
566 return dt_find_node(found, path + 1, addrcp, sizecp, create);
567}
568
569/*
Julius Wernerf36d53c2019-05-03 18:23:34 -0700570 * Find a node in the tree from a string device tree path.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200571 *
Julius Wernerf36d53c2019-05-03 18:23:34 -0700572 * @param tree The device tree to search.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200573 * @param path A string representing a path in the device tree, with
Julius Wernerfbec63d2019-05-03 18:29:28 -0700574 * nodes separated by '/'. Example: "/firmware/coreboot"
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200575 * @param addrcp Pointer that will be updated with any #address-cells
576 * value found in the path. May be NULL to ignore.
577 * @param sizecp Pointer that will be updated with any #size-cells
578 * value found in the path. May be NULL to ignore.
579 * @param create 1: Create node(s) if not found. 0: Return NULL instead.
580 * @return The found/created node, or NULL.
581 *
Julius Werner6d5695f2019-05-06 19:23:28 -0700582 * It is the caller responsibility to provide a path string that doesn't end
583 * with a '/' and doesn't contain any "//". If the path does not start with a
584 * '/', the first segment is interpreted as an alias. */
Julius Wernerf36d53c2019-05-03 18:23:34 -0700585struct device_tree_node *dt_find_node_by_path(struct device_tree *tree,
Patrick Rudolph666c1722018-04-03 09:57:33 +0200586 const char *path, u32 *addrcp,
587 u32 *sizecp, int create)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200588{
Julius Werner6d5695f2019-05-06 19:23:28 -0700589 char *sub_path;
590 char *duped_str;
591 struct device_tree_node *parent;
592 char *next_slash;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200593 /* Hopefully enough depth for any node. */
594 const char *path_array[15];
595 int i;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200596 struct device_tree_node *node = NULL;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200597
Julius Werner23df4772019-05-17 22:50:18 -0700598 if (path[0] == '/') { /* regular path */
599 if (path[1] == '\0') { /* special case: "/" is root node */
Julius Werner6d5695f2019-05-06 19:23:28 -0700600 dt_read_cell_props(tree->root, addrcp, sizecp);
601 return tree->root;
602 }
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200603
Julius Werner6d5695f2019-05-06 19:23:28 -0700604 sub_path = duped_str = strdup(&path[1]);
605 if (!sub_path)
606 return NULL;
607
608 parent = tree->root;
Julius Werner23df4772019-05-17 22:50:18 -0700609 } else { /* alias */
Julius Werner6d5695f2019-05-06 19:23:28 -0700610 char *alias;
611
612 alias = duped_str = strdup(path);
613 if (!alias)
614 return NULL;
615
616 sub_path = strchr(alias, '/');
617 if (sub_path)
618 *sub_path = '\0';
619
620 parent = dt_find_node_by_alias(tree, alias);
621 if (!parent) {
622 printk(BIOS_DEBUG,
623 "Could not find node '%s', alias '%s' does not exist\n",
624 path, alias);
625 free(duped_str);
626 return NULL;
627 }
628
629 if (!sub_path) {
Julius Werner23df4772019-05-17 22:50:18 -0700630 /* it's just the alias, no sub-path */
Julius Werner6d5695f2019-05-06 19:23:28 -0700631 free(duped_str);
632 return parent;
633 }
634
635 sub_path++;
636 }
637
638 next_slash = sub_path;
639 path_array[0] = sub_path;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200640 for (i = 1; i < (ARRAY_SIZE(path_array) - 1); i++) {
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200641 next_slash = strchr(next_slash, '/');
642 if (!next_slash)
643 break;
644
645 *next_slash++ = '\0';
646 path_array[i] = next_slash;
647 }
648
649 if (!next_slash) {
650 path_array[i] = NULL;
Julius Werner6d5695f2019-05-06 19:23:28 -0700651 node = dt_find_node(parent, path_array,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200652 addrcp, sizecp, create);
653 }
654
Julius Werner6d5695f2019-05-06 19:23:28 -0700655 free(duped_str);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200656 return node;
657}
658
Julius Werner6d5695f2019-05-06 19:23:28 -0700659/*
660 * Find a node from an alias
661 *
662 * @param tree The device tree.
663 * @param alias The alias name.
664 * @return The found node, or NULL.
665 */
666struct device_tree_node *dt_find_node_by_alias(struct device_tree *tree,
667 const char *alias)
668{
669 struct device_tree_node *node;
670 const char *alias_path;
671
672 node = dt_find_node_by_path(tree, "/aliases", NULL, NULL, 0);
673 if (!node)
674 return NULL;
675
676 alias_path = dt_find_string_prop(node, alias);
677 if (!alias_path)
678 return NULL;
679
680 return dt_find_node_by_path(tree, alias_path, NULL, NULL, 0);
681}
682
Julius Werner6702b682019-05-03 18:13:53 -0700683struct device_tree_node *dt_find_node_by_phandle(struct device_tree_node *root,
684 uint32_t phandle)
685{
686 if (!root)
687 return NULL;
688
689 if (root->phandle == phandle)
690 return root;
691
692 struct device_tree_node *node;
693 struct device_tree_node *result;
694 list_for_each(node, root->children, list_node) {
695 result = dt_find_node_by_phandle(node, phandle);
696 if (result)
697 return result;
698 }
699
700 return NULL;
701}
702
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200703/*
704 * Check if given node is compatible.
705 *
706 * @param node The node which is to be checked for compatible property.
707 * @param compat The compatible string to match.
708 * @return 1 = compatible, 0 = not compatible.
709 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200710static int dt_check_compat_match(struct device_tree_node *node,
711 const char *compat)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200712{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200713 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200714
715 list_for_each(prop, node->properties, list_node) {
716 if (!strcmp("compatible", prop->prop.name)) {
717 size_t bytes = prop->prop.size;
718 const char *str = prop->prop.data;
719 while (bytes > 0) {
720 if (!strncmp(compat, str, bytes))
721 return 1;
722 size_t len = strnlen(str, bytes) + 1;
723 if (bytes <= len)
724 break;
725 str += len;
726 bytes -= len;
727 }
728 break;
729 }
730 }
731
732 return 0;
733}
734
735/*
736 * Find a node from a compatible string, in the subtree of a parent node.
737 *
738 * @param parent The parent node under which to look.
739 * @param compat The compatible string to find.
740 * @return The found node, or NULL.
741 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200742struct device_tree_node *dt_find_compat(struct device_tree_node *parent,
743 const char *compat)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200744{
Julius Werner23df4772019-05-17 22:50:18 -0700745 /* Check if the parent node itself is compatible. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200746 if (dt_check_compat_match(parent, compat))
747 return parent;
748
Patrick Rudolph666c1722018-04-03 09:57:33 +0200749 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200750 list_for_each(child, parent->children, list_node) {
Patrick Rudolph666c1722018-04-03 09:57:33 +0200751 struct device_tree_node *found = dt_find_compat(child, compat);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200752 if (found)
753 return found;
754 }
755
756 return NULL;
757}
758
759/*
Martin Roth0949e732021-10-01 14:28:22 -0600760 * Find the next compatible child of a given parent. All children up to the
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200761 * child passed in by caller are ignored. If child is NULL, it considers all the
762 * children to find the first child which is compatible.
763 *
764 * @param parent The parent node under which to look.
765 * @param child The child node to start search from (exclusive). If NULL
766 * consider all children.
767 * @param compat The compatible string to find.
768 * @return The found node, or NULL.
769 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200770struct device_tree_node *
771dt_find_next_compat_child(struct device_tree_node *parent,
772 struct device_tree_node *child,
773 const char *compat)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200774{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200775 struct device_tree_node *next;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200776 int ignore = 0;
777
778 if (child)
779 ignore = 1;
780
781 list_for_each(next, parent->children, list_node) {
782 if (ignore) {
783 if (child == next)
784 ignore = 0;
785 continue;
786 }
787
788 if (dt_check_compat_match(next, compat))
789 return next;
790 }
791
792 return NULL;
793}
794
795/*
796 * Find a node with matching property value, in the subtree of a parent node.
797 *
798 * @param parent The parent node under which to look.
799 * @param name The property name to look for.
800 * @param data The property value to look for.
801 * @param size The property size.
802 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200803struct device_tree_node *dt_find_prop_value(struct device_tree_node *parent,
804 const char *name, void *data,
805 size_t size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200806{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200807 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200808
809 /* Check if parent itself has the required property value. */
810 list_for_each(prop, parent->properties, list_node) {
811 if (!strcmp(name, prop->prop.name)) {
812 size_t bytes = prop->prop.size;
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200813 const void *prop_data = prop->prop.data;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200814 if (size != bytes)
815 break;
816 if (!memcmp(data, prop_data, size))
817 return parent;
818 break;
819 }
820 }
821
Patrick Rudolph666c1722018-04-03 09:57:33 +0200822 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200823 list_for_each(child, parent->children, list_node) {
Patrick Rudolph666c1722018-04-03 09:57:33 +0200824 struct device_tree_node *found = dt_find_prop_value(child, name,
825 data, size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200826 if (found)
827 return found;
828 }
829 return NULL;
830}
831
832/*
833 * Write an arbitrary sized big-endian integer into a pointer.
834 *
835 * @param dest Pointer to the DT property data buffer to write.
Elyes HAOUAS1ec76442018-08-07 12:20:04 +0200836 * @param src The integer to write (in CPU endianness).
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200837 * @param length the length of the destination integer in bytes.
838 */
839void dt_write_int(u8 *dest, u64 src, size_t length)
840{
841 while (length--) {
842 dest[length] = (u8)src;
843 src >>= 8;
844 }
845}
846
847/*
Patrick Rudolph5ccc7312018-05-30 15:05:28 +0200848 * Delete a property by name in a given node if it exists.
849 *
850 * @param node The device tree node to operate on.
851 * @param name The name of the property to delete.
852 */
853void dt_delete_prop(struct device_tree_node *node, const char *name)
854{
855 struct device_tree_property *prop;
856
857 list_for_each(prop, node->properties, list_node) {
858 if (!strcmp(prop->prop.name, name)) {
859 list_remove(&prop->list_node);
860 return;
861 }
862 }
863}
864
865/*
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200866 * Add an arbitrary property to a node, or update it if it already exists.
867 *
868 * @param node The device tree node to add to.
869 * @param name The name of the new property.
870 * @param data The raw data blob to be stored in the property.
871 * @param size The size of data in bytes.
872 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200873void dt_add_bin_prop(struct device_tree_node *node, const char *name,
Julius Werner0e9116f2019-05-13 17:30:31 -0700874 void *data, size_t size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200875{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200876 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200877
878 list_for_each(prop, node->properties, list_node) {
879 if (!strcmp(prop->prop.name, name)) {
880 prop->prop.data = data;
881 prop->prop.size = size;
882 return;
883 }
884 }
885
Julius Werner9636a102019-05-03 17:36:43 -0700886 prop = xzalloc(sizeof(*prop));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200887 list_insert_after(&prop->list_node, &node->properties);
888 prop->prop.name = name;
889 prop->prop.data = data;
890 prop->prop.size = size;
891}
892
893/*
894 * Find given string property in a node and return its content.
895 *
896 * @param node The device tree node to search.
897 * @param name The name of the property.
898 * @return The found string, or NULL.
899 */
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200900const char *dt_find_string_prop(const struct device_tree_node *node,
901 const char *name)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200902{
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200903 const void *content;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200904 size_t size;
905
906 dt_find_bin_prop(node, name, &content, &size);
907
908 return content;
909}
910
911/*
912 * Find given property in a node.
913 *
914 * @param node The device tree node to search.
915 * @param name The name of the property.
916 * @param data Pointer to return raw data blob in the property.
917 * @param size Pointer to return the size of data in bytes.
918 */
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200919void dt_find_bin_prop(const struct device_tree_node *node, const char *name,
920 const void **data, size_t *size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200921{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200922 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200923
924 *data = NULL;
925 *size = 0;
926
927 list_for_each(prop, node->properties, list_node) {
928 if (!strcmp(prop->prop.name, name)) {
929 *data = prop->prop.data;
930 *size = prop->prop.size;
931 return;
932 }
933 }
934}
935
936/*
937 * Add a string property to a node, or update it if it already exists.
938 *
939 * @param node The device tree node to add to.
940 * @param name The name of the new property.
941 * @param str The zero-terminated string to be stored in the property.
942 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200943void dt_add_string_prop(struct device_tree_node *node, const char *name,
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200944 const char *str)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200945{
Julius Werner0e9116f2019-05-13 17:30:31 -0700946 dt_add_bin_prop(node, name, (char *)str, strlen(str) + 1);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200947}
948
949/*
950 * Add a 32-bit integer property to a node, or update it if it already exists.
951 *
952 * @param node The device tree node to add to.
953 * @param name The name of the new property.
954 * @param val The integer to be stored in the property.
955 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200956void dt_add_u32_prop(struct device_tree_node *node, const char *name, u32 val)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200957{
Julius Werner9636a102019-05-03 17:36:43 -0700958 u32 *val_ptr = xmalloc(sizeof(val));
Patrick Rudolph666c1722018-04-03 09:57:33 +0200959 *val_ptr = htobe32(val);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200960 dt_add_bin_prop(node, name, val_ptr, sizeof(*val_ptr));
961}
962
963/*
Patrick Rudolph3fca4ed2018-08-10 10:12:35 +0200964 * Add a 64-bit integer property to a node, or update it if it already exists.
965 *
966 * @param node The device tree node to add to.
967 * @param name The name of the new property.
968 * @param val The integer to be stored in the property.
969 */
970void dt_add_u64_prop(struct device_tree_node *node, const char *name, u64 val)
971{
Julius Werner9636a102019-05-03 17:36:43 -0700972 u64 *val_ptr = xmalloc(sizeof(val));
Patrick Rudolph3fca4ed2018-08-10 10:12:35 +0200973 *val_ptr = htobe64(val);
974 dt_add_bin_prop(node, name, val_ptr, sizeof(*val_ptr));
975}
976
977/*
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200978 * Add a 'reg' address list property to a node, or update it if it exists.
979 *
980 * @param node The device tree node to add to.
981 * @param addrs Array of address values to be stored in the property.
982 * @param sizes Array of corresponding size values to 'addrs'.
983 * @param count Number of values in 'addrs' and 'sizes' (must be equal).
984 * @param addr_cells Value of #address-cells property valid for this node.
985 * @param size_cells Value of #size-cells property valid for this node.
986 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200987void dt_add_reg_prop(struct device_tree_node *node, u64 *addrs, u64 *sizes,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200988 int count, u32 addr_cells, u32 size_cells)
989{
990 int i;
991 size_t length = (addr_cells + size_cells) * sizeof(u32) * count;
Julius Werner9636a102019-05-03 17:36:43 -0700992 u8 *data = xmalloc(length);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200993 u8 *cur = data;
994
995 for (i = 0; i < count; i++) {
996 dt_write_int(cur, addrs[i], addr_cells * sizeof(u32));
997 cur += addr_cells * sizeof(u32);
998 dt_write_int(cur, sizes[i], size_cells * sizeof(u32));
999 cur += size_cells * sizeof(u32);
1000 }
1001
1002 dt_add_bin_prop(node, "reg", data, length);
1003}
1004
1005/*
1006 * Fixups to apply to a kernel's device tree before booting it.
1007 */
1008
Patrick Rudolph666c1722018-04-03 09:57:33 +02001009struct list_node device_tree_fixups;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001010
Patrick Rudolph666c1722018-04-03 09:57:33 +02001011int dt_apply_fixups(struct device_tree *tree)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001012{
Patrick Rudolph666c1722018-04-03 09:57:33 +02001013 struct device_tree_fixup *fixup;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001014 list_for_each(fixup, device_tree_fixups, list_node) {
1015 assert(fixup->fixup);
1016 if (fixup->fixup(fixup, tree))
1017 return 1;
1018 }
1019 return 0;
1020}
1021
Patrick Rudolph666c1722018-04-03 09:57:33 +02001022int dt_set_bin_prop_by_path(struct device_tree *tree, const char *path,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001023 void *data, size_t data_size, int create)
1024{
1025 char *path_copy, *prop_name;
Patrick Rudolph666c1722018-04-03 09:57:33 +02001026 struct device_tree_node *dt_node;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001027
1028 path_copy = strdup(path);
1029
1030 if (!path_copy) {
Patrick Rudolph666c1722018-04-03 09:57:33 +02001031 printk(BIOS_ERR, "Failed to allocate a copy of path %s\n",
1032 path);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001033 return 1;
1034 }
1035
1036 prop_name = strrchr(path_copy, '/');
1037 if (!prop_name) {
Patrick Rudolph679d6242018-07-11 13:53:04 +02001038 free(path_copy);
Patrick Rudolph666c1722018-04-03 09:57:33 +02001039 printk(BIOS_ERR, "Path %s does not include '/'\n", path);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001040 return 1;
1041 }
1042
1043 *prop_name++ = '\0'; /* Separate path from the property name. */
1044
Julius Wernerf36d53c2019-05-03 18:23:34 -07001045 dt_node = dt_find_node_by_path(tree, path_copy, NULL,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001046 NULL, create);
1047
1048 if (!dt_node) {
Patrick Rudolph666c1722018-04-03 09:57:33 +02001049 printk(BIOS_ERR, "Failed to %s %s in the device tree\n",
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001050 create ? "create" : "find", path_copy);
Patrick Rudolph679d6242018-07-11 13:53:04 +02001051 free(path_copy);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001052 return 1;
1053 }
1054
1055 dt_add_bin_prop(dt_node, prop_name, data, data_size);
Patrick Rudolph679d6242018-07-11 13:53:04 +02001056 free(path_copy);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001057
1058 return 0;
1059}
1060
1061/*
1062 * Prepare the /reserved-memory/ node.
1063 *
1064 * Technically, this can be called more than one time, to init and/or retrieve
1065 * the node. But dt_add_u32_prop() may leak a bit of memory if you do.
1066 *
1067 * @tree: Device tree to add/retrieve from.
1068 * @return: The /reserved-memory/ node (or NULL, if error).
1069 */
Patrick Rudolph666c1722018-04-03 09:57:33 +02001070struct device_tree_node *dt_init_reserved_memory_node(struct device_tree *tree)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001071{
Patrick Rudolph666c1722018-04-03 09:57:33 +02001072 struct device_tree_node *reserved;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001073 u32 addr = 0, size = 0;
1074
Julius Wernerfbec63d2019-05-03 18:29:28 -07001075 reserved = dt_find_node_by_path(tree, "/reserved-memory", &addr,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001076 &size, 1);
1077 if (!reserved)
1078 return NULL;
1079
Julius Werner23df4772019-05-17 22:50:18 -07001080 /* Binding doc says this should have the same #{address,size}-cells as
1081 the root. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001082 dt_add_u32_prop(reserved, "#address-cells", addr);
1083 dt_add_u32_prop(reserved, "#size-cells", size);
Julius Werner23df4772019-05-17 22:50:18 -07001084 /* Binding doc says this should be empty (1:1 mapping from root). */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001085 dt_add_bin_prop(reserved, "ranges", NULL, 0);
1086
1087 return reserved;
1088}
Julius Werner735ddc92019-05-07 17:05:28 -07001089
1090/*
1091 * Increment a single phandle in prop at a given offset by a given adjustment.
1092 *
1093 * @param prop Property whose phandle should be adjusted.
1094 * @param adjustment Value that should be added to the existing phandle.
1095 * @param offset Byte offset of the phandle in the property data.
1096 *
1097 * @return New phandle value, or 0 on error.
1098 */
1099static uint32_t dt_adjust_phandle(struct device_tree_property *prop,
1100 uint32_t adjustment, uint32_t offset)
1101{
1102 if (offset + 4 > prop->prop.size)
1103 return 0;
1104
1105 uint32_t phandle = be32dec(prop->prop.data + offset);
1106 if (phandle == 0 ||
1107 phandle == FDT_PHANDLE_ILLEGAL ||
1108 phandle == 0xffffffff)
1109 return 0;
1110
1111 phandle += adjustment;
1112 if (phandle >= FDT_PHANDLE_ILLEGAL)
1113 return 0;
1114
1115 be32enc(prop->prop.data + offset, phandle);
1116 return phandle;
1117}
1118
1119/*
1120 * Adjust all phandles in subtree by adding a new base offset.
1121 *
1122 * @param node Root node of the subtree to work on.
1123 * @param base New phandle base to be added to all phandles.
1124 *
1125 * @return New highest phandle in the subtree, or 0 on error.
1126 */
1127static uint32_t dt_adjust_all_phandles(struct device_tree_node *node,
1128 uint32_t base)
1129{
Julius Werner23df4772019-05-17 22:50:18 -07001130 uint32_t new_max = MAX(base, 1); /* make sure we don't return 0 */
Julius Werner735ddc92019-05-07 17:05:28 -07001131 struct device_tree_property *prop;
1132 struct device_tree_node *child;
1133
1134 if (!node)
1135 return new_max;
1136
1137 list_for_each(prop, node->properties, list_node)
1138 if (dt_prop_is_phandle(prop)) {
1139 node->phandle = dt_adjust_phandle(prop, base, 0);
1140 if (!node->phandle)
1141 return 0;
1142 new_max = MAX(new_max, node->phandle);
Julius Werner23df4772019-05-17 22:50:18 -07001143 } /* no break -- can have more than one phandle prop */
Julius Werner735ddc92019-05-07 17:05:28 -07001144
1145 list_for_each(child, node->children, list_node)
1146 new_max = MAX(new_max, dt_adjust_all_phandles(child, base));
1147
1148 return new_max;
1149}
1150
1151/*
1152 * Apply a /__local_fixup__ subtree to the corresponding overlay subtree.
1153 *
1154 * @param node Root node of the overlay subtree to fix up.
1155 * @param node Root node of the /__local_fixup__ subtree.
1156 * @param base Adjustment that was added to phandles in the overlay.
1157 *
1158 * @return 0 on success, -1 on error.
1159 */
1160static int dt_fixup_locals(struct device_tree_node *node,
1161 struct device_tree_node *fixup, uint32_t base)
1162{
1163 struct device_tree_property *prop;
1164 struct device_tree_property *fixup_prop;
1165 struct device_tree_node *child;
1166 struct device_tree_node *fixup_child;
1167 int i;
1168
Julius Werner23df4772019-05-17 22:50:18 -07001169 /*
1170 * For local fixups the /__local_fixup__ subtree contains the same node
1171 * hierarchy as the main tree we're fixing up. Each property contains
1172 * the fixup offsets for the respective property in the main tree. For
1173 * each property in the fixup node, find the corresponding property in
1174 * the base node and apply fixups to all offsets it specifies.
1175 */
Julius Werner735ddc92019-05-07 17:05:28 -07001176 list_for_each(fixup_prop, fixup->properties, list_node) {
1177 struct device_tree_property *base_prop = NULL;
1178 list_for_each(prop, node->properties, list_node)
1179 if (!strcmp(prop->prop.name, fixup_prop->prop.name)) {
1180 base_prop = prop;
1181 break;
1182 }
1183
Julius Werner23df4772019-05-17 22:50:18 -07001184 /* We should always find a corresponding base prop for a fixup,
1185 and fixup props contain a list of 32-bit fixup offsets. */
Julius Werner735ddc92019-05-07 17:05:28 -07001186 if (!base_prop || fixup_prop->prop.size % sizeof(uint32_t))
1187 return -1;
1188
1189 for (i = 0; i < fixup_prop->prop.size; i += sizeof(uint32_t))
1190 if (!dt_adjust_phandle(base_prop, base, be32dec(
1191 fixup_prop->prop.data + i)))
1192 return -1;
1193 }
1194
Julius Werner23df4772019-05-17 22:50:18 -07001195 /* Now recursively descend both the base tree and the /__local_fixups__
1196 subtree in sync to apply all fixups. */
Julius Werner735ddc92019-05-07 17:05:28 -07001197 list_for_each(fixup_child, fixup->children, list_node) {
1198 struct device_tree_node *base_child = NULL;
1199 list_for_each(child, node->children, list_node)
1200 if (!strcmp(child->name, fixup_child->name)) {
1201 base_child = child;
1202 break;
1203 }
1204
Julius Werner23df4772019-05-17 22:50:18 -07001205 /* All fixup nodes should have a corresponding base node. */
Julius Werner735ddc92019-05-07 17:05:28 -07001206 if (!base_child)
1207 return -1;
1208
1209 if (dt_fixup_locals(base_child, fixup_child, base) < 0)
1210 return -1;
1211 }
1212
1213 return 0;
1214}
1215
1216/*
1217 * Update all /__symbols__ properties in an overlay that start with
1218 * "/fragment@X/__overlay__" with corresponding path prefix in the base tree.
1219 *
1220 * @param symbols /__symbols__ done to update.
1221 * @param fragment /fragment@X node that references to should be updated.
1222 * @param base_path Path of base tree node that the fragment overlaid.
1223 */
1224static void dt_fix_symbols(struct device_tree_node *symbols,
1225 struct device_tree_node *fragment,
1226 const char *base_path)
1227{
1228 struct device_tree_property *prop;
Julius Werner23df4772019-05-17 22:50:18 -07001229 char buf[512]; /* Should be enough for maximum DT path length? */
1230 char node_path[64]; /* easily enough for /fragment@XXXX/__overlay__ */
Julius Werner735ddc92019-05-07 17:05:28 -07001231
Julius Werner23df4772019-05-17 22:50:18 -07001232 if (!symbols) /* If the overlay has no /__symbols__ node, we're done! */
Julius Werner735ddc92019-05-07 17:05:28 -07001233 return;
1234
1235 int len = snprintf(node_path, sizeof(node_path), "/%s/__overlay__",
1236 fragment->name);
1237
1238 list_for_each(prop, symbols->properties, list_node)
1239 if (!strncmp(prop->prop.data, node_path, len)) {
1240 prop->prop.size = snprintf(buf, sizeof(buf), "%s%s",
1241 base_path, (char *)prop->prop.data + len) + 1;
1242 free(prop->prop.data);
1243 prop->prop.data = strdup(buf);
1244 }
1245}
1246
1247/*
1248 * Fix up overlay according to a property in /__fixup__. If the fixed property
1249 * is a /fragment@X:target, also update /__symbols__ references to fragment.
1250 *
1251 * @params overlay Overlay to fix up.
1252 * @params fixup /__fixup__ property.
1253 * @params phandle phandle value to insert where the fixup points to.
1254 * @params base_path Path to the base DT node that the fixup points to.
1255 * @params overlay_symbols /__symbols__ node of the overlay.
1256 *
1257 * @return 0 on success, -1 on error.
1258 */
1259static int dt_fixup_external(struct device_tree *overlay,
1260 struct device_tree_property *fixup,
1261 uint32_t phandle, const char *base_path,
1262 struct device_tree_node *overlay_symbols)
1263{
1264 struct device_tree_property *prop;
1265
Julius Werner23df4772019-05-17 22:50:18 -07001266 /* External fixup properties are encoded as "<path>:<prop>:<offset>". */
Julius Werner735ddc92019-05-07 17:05:28 -07001267 char *entry = fixup->prop.data;
1268 while ((void *)entry < fixup->prop.data + fixup->prop.size) {
Julius Werner23df4772019-05-17 22:50:18 -07001269 /* okay to destroy fixup property value, won't need it again */
Julius Werner735ddc92019-05-07 17:05:28 -07001270 char *node_path = entry;
1271 entry = strchr(node_path, ':');
1272 if (!entry)
1273 return -1;
1274 *entry++ = '\0';
1275
1276 char *prop_name = entry;
1277 entry = strchr(prop_name, ':');
1278 if (!entry)
1279 return -1;
1280 *entry++ = '\0';
1281
1282 struct device_tree_node *ovl_node = dt_find_node_by_path(
1283 overlay, node_path, NULL, NULL, 0);
1284 if (!ovl_node || !isdigit(*entry))
1285 return -1;
1286
1287 struct device_tree_property *ovl_prop = NULL;
1288 list_for_each(prop, ovl_node->properties, list_node)
1289 if (!strcmp(prop->prop.name, prop_name)) {
1290 ovl_prop = prop;
1291 break;
1292 }
1293
Julius Werner23df4772019-05-17 22:50:18 -07001294 /* Move entry to first char after number, must be a '\0'. */
Julius Werner735ddc92019-05-07 17:05:28 -07001295 uint32_t offset = skip_atoi(&entry);
1296 if (!ovl_prop || offset + 4 > ovl_prop->prop.size || entry[0])
1297 return -1;
Julius Werner23df4772019-05-17 22:50:18 -07001298 entry++; /* jump over '\0' to potential next fixup */
Julius Werner735ddc92019-05-07 17:05:28 -07001299
1300 be32enc(ovl_prop->prop.data + offset, phandle);
1301
Julius Werner23df4772019-05-17 22:50:18 -07001302 /* If this is a /fragment@X:target property, update references
1303 to this fragment in the overlay __symbols__ now. */
Julius Werner735ddc92019-05-07 17:05:28 -07001304 if (offset == 0 && !strcmp(prop_name, "target") &&
Julius Werner23df4772019-05-17 22:50:18 -07001305 !strchr(node_path + 1, '/')) /* only toplevel nodes */
Julius Werner735ddc92019-05-07 17:05:28 -07001306 dt_fix_symbols(overlay_symbols, ovl_node, base_path);
1307 }
1308
1309 return 0;
1310}
1311
1312/*
1313 * Apply all /__fixup__ properties in the overlay. This will destroy the
1314 * property data in /__fixup__ and it should not be accessed again.
1315 *
1316 * @params tree Base device tree that the overlay updates.
1317 * @params symbols /__symbols__ node of the base device tree.
1318 * @params overlay Overlay to fix up.
1319 * @params fixups /__fixup__ node in the overlay.
1320 * @params overlay_symbols /__symbols__ node of the overlay.
1321 *
1322 * @return 0 on success, -1 on error.
1323 */
1324static int dt_fixup_all_externals(struct device_tree *tree,
1325 struct device_tree_node *symbols,
1326 struct device_tree *overlay,
1327 struct device_tree_node *fixups,
1328 struct device_tree_node *overlay_symbols)
1329{
1330 struct device_tree_property *fix;
1331
Julius Werner23df4772019-05-17 22:50:18 -07001332 /* If we have any external fixups, base tree must have /__symbols__. */
Julius Werner735ddc92019-05-07 17:05:28 -07001333 if (!symbols)
1334 return -1;
1335
Julius Werner23df4772019-05-17 22:50:18 -07001336 /*
1337 * Unlike /__local_fixups__, /__fixups__ is not a whole subtree that
1338 * mirrors the node hierarchy. It's just a directory of fixup properties
1339 * that each directly contain all information necessary to apply them.
1340 */
Julius Werner735ddc92019-05-07 17:05:28 -07001341 list_for_each(fix, fixups->properties, list_node) {
Julius Werner23df4772019-05-17 22:50:18 -07001342 /* The name of a fixup property is the label of the node we want
1343 a property to phandle-reference. Look up in /__symbols__. */
Julius Werner735ddc92019-05-07 17:05:28 -07001344 const char *path = dt_find_string_prop(symbols, fix->prop.name);
1345 if (!path)
1346 return -1;
1347
Elyes HAOUAS0afaff22021-01-16 15:02:31 +01001348 /* Find node the label pointed to figure out its phandle. */
Julius Werner735ddc92019-05-07 17:05:28 -07001349 struct device_tree_node *node = dt_find_node_by_path(tree, path,
1350 NULL, NULL, 0);
1351 if (!node)
1352 return -1;
1353
Julius Werner23df4772019-05-17 22:50:18 -07001354 /* Write into the overlay property(s) pointing to that node. */
Julius Werner735ddc92019-05-07 17:05:28 -07001355 if (dt_fixup_external(overlay, fix, node->phandle,
1356 path, overlay_symbols) < 0)
1357 return -1;
1358 }
1359
1360 return 0;
1361}
1362
1363/*
1364 * Copy all nodes and properties from one DT subtree into another. This is a
1365 * shallow copy so both trees will point to the same property data afterwards.
1366 *
1367 * @params dst Destination subtree to copy into.
1368 * @params src Source subtree to copy from.
1369 * @params upd 1 to overwrite same-name properties, 0 to discard them.
1370 */
1371static void dt_copy_subtree(struct device_tree_node *dst,
1372 struct device_tree_node *src, int upd)
1373{
1374 struct device_tree_property *prop;
1375 struct device_tree_property *src_prop;
1376 list_for_each(src_prop, src->properties, list_node) {
1377 if (dt_prop_is_phandle(src_prop) ||
1378 !strcmp(src_prop->prop.name, "name")) {
1379 printk(BIOS_DEBUG,
1380 "WARNING: ignoring illegal overlay prop '%s'\n",
1381 src_prop->prop.name);
1382 continue;
1383 }
1384
1385 struct device_tree_property *dst_prop = NULL;
1386 list_for_each(prop, dst->properties, list_node)
1387 if (!strcmp(prop->prop.name, src_prop->prop.name)) {
1388 dst_prop = prop;
1389 break;
1390 }
1391
1392 if (dst_prop) {
1393 if (!upd) {
1394 printk(BIOS_DEBUG,
1395 "WARNING: ignoring prop update '%s'\n",
1396 src_prop->prop.name);
1397 continue;
1398 }
1399 } else {
1400 dst_prop = xzalloc(sizeof(*dst_prop));
1401 list_insert_after(&dst_prop->list_node,
1402 &dst->properties);
1403 }
1404
1405 dst_prop->prop = src_prop->prop;
1406 }
1407
1408 struct device_tree_node *node;
1409 struct device_tree_node *src_node;
1410 list_for_each(src_node, src->children, list_node) {
1411 struct device_tree_node *dst_node = NULL;
1412 list_for_each(node, dst->children, list_node)
1413 if (!strcmp(node->name, src_node->name)) {
1414 dst_node = node;
1415 break;
1416 }
1417
1418 if (!dst_node) {
1419 dst_node = xzalloc(sizeof(*dst_node));
1420 *dst_node = *src_node;
1421 list_insert_after(&dst_node->list_node, &dst->children);
1422 } else {
1423 dt_copy_subtree(dst_node, src_node, upd);
1424 }
1425 }
1426}
1427
1428/*
1429 * Apply an overlay /fragment@X node to a base device tree.
1430 *
1431 * @param tree Base device tree.
1432 * @param fragment /fragment@X node.
1433 * @params overlay_symbols /__symbols__ node of the overlay.
1434 *
1435 * @return 0 on success, -1 on error.
1436 */
1437static int dt_import_fragment(struct device_tree *tree,
1438 struct device_tree_node *fragment,
1439 struct device_tree_node *overlay_symbols)
1440{
Julius Werner23df4772019-05-17 22:50:18 -07001441 /* The actual overlaid nodes/props are in an __overlay__ child node. */
Julius Werner735ddc92019-05-07 17:05:28 -07001442 static const char *overlay_path[] = { "__overlay__", NULL };
1443 struct device_tree_node *overlay = dt_find_node(fragment, overlay_path,
1444 NULL, NULL, 0);
1445
Julius Werner23df4772019-05-17 22:50:18 -07001446 /* If it doesn't have an __overlay__ child, it's not a fragment. */
Julius Werner735ddc92019-05-07 17:05:28 -07001447 if (!overlay)
1448 return 0;
1449
Julius Werner23df4772019-05-17 22:50:18 -07001450 /* Target node of the fragment can be given by path or by phandle. */
Julius Werner735ddc92019-05-07 17:05:28 -07001451 struct device_tree_property *prop;
1452 struct device_tree_property *phandle = NULL;
1453 struct device_tree_property *path = NULL;
1454 list_for_each(prop, fragment->properties, list_node) {
1455 if (!strcmp(prop->prop.name, "target")) {
1456 phandle = prop;
Julius Werner23df4772019-05-17 22:50:18 -07001457 break; /* phandle target has priority, stop looking */
Julius Werner735ddc92019-05-07 17:05:28 -07001458 }
1459 if (!strcmp(prop->prop.name, "target-path"))
1460 path = prop;
1461 }
1462
1463 struct device_tree_node *target = NULL;
1464 if (phandle) {
1465 if (phandle->prop.size != sizeof(uint32_t))
1466 return -1;
1467 target = dt_find_node_by_phandle(tree->root,
1468 be32dec(phandle->prop.data));
Julius Werner23df4772019-05-17 22:50:18 -07001469 /* Symbols already updated as part of dt_fixup_external(). */
Julius Werner735ddc92019-05-07 17:05:28 -07001470 } else if (path) {
1471 target = dt_find_node_by_path(tree, path->prop.data,
1472 NULL, NULL, 0);
1473 dt_fix_symbols(overlay_symbols, fragment, path->prop.data);
1474 }
1475 if (!target)
1476 return -1;
1477
1478 dt_copy_subtree(target, overlay, 1);
1479 return 0;
1480}
1481
1482/*
1483 * Apply a device tree overlay to a base device tree. This will
1484 * destroy/incorporate the overlay data, so it should not be freed or reused.
1485 * See dtc.git/Documentation/dt-object-internal.txt for overlay format details.
1486 *
1487 * @param tree Unflattened base device tree to add the overlay into.
1488 * @param overlay Unflattened overlay device tree to apply to the base.
1489 *
1490 * @return 0 on success, -1 on error.
1491 */
1492int dt_apply_overlay(struct device_tree *tree, struct device_tree *overlay)
1493{
Julius Werner23df4772019-05-17 22:50:18 -07001494 /*
1495 * First, we need to make sure phandles inside the overlay don't clash
1496 * with those in the base tree. We just define the highest phandle value
1497 * in the base tree as the "phandle offset" for this overlay and
1498 * increment all phandles in it by that value.
1499 */
Julius Werner735ddc92019-05-07 17:05:28 -07001500 uint32_t phandle_base = tree->max_phandle;
1501 uint32_t new_max = dt_adjust_all_phandles(overlay->root, phandle_base);
1502 if (!new_max) {
Elyes HAOUAS8b5841e2022-02-08 21:23:12 +01001503 printk(BIOS_ERR, "invalid phandles in overlay\n");
Julius Werner735ddc92019-05-07 17:05:28 -07001504 return -1;
1505 }
1506 tree->max_phandle = new_max;
1507
Julius Werner23df4772019-05-17 22:50:18 -07001508 /* Now that we changed phandles in the overlay, we need to update any
1509 nodes referring to them. Those are listed in /__local_fixups__. */
Julius Werner735ddc92019-05-07 17:05:28 -07001510 struct device_tree_node *local_fixups = dt_find_node_by_path(overlay,
1511 "/__local_fixups__", NULL, NULL, 0);
1512 if (local_fixups && dt_fixup_locals(overlay->root, local_fixups,
1513 phandle_base) < 0) {
Elyes HAOUAS8b5841e2022-02-08 21:23:12 +01001514 printk(BIOS_ERR, "invalid local fixups in overlay\n");
Julius Werner735ddc92019-05-07 17:05:28 -07001515 return -1;
1516 }
1517
Julius Werner23df4772019-05-17 22:50:18 -07001518 /*
1519 * Besides local phandle references (from nodes within the overlay to
1520 * other nodes within the overlay), the overlay may also contain phandle
1521 * references to the base tree. These are stored with invalid values and
1522 * must be updated now. /__symbols__ contains a list of all labels in
1523 * the base tree, and /__fixups__ describes all nodes in the overlay
1524 * that contain external phandle references.
1525 * We also take this opportunity to update all /fragment@X/__overlay__/
1526 * prefixes in the overlay's /__symbols__ node to the correct path that
1527 * the fragment will be placed in later, since this is the only step
1528 * where we have all necessary information for that easily available.
1529 */
Julius Werner735ddc92019-05-07 17:05:28 -07001530 struct device_tree_node *symbols = dt_find_node_by_path(tree,
1531 "/__symbols__", NULL, NULL, 0);
1532 struct device_tree_node *fixups = dt_find_node_by_path(overlay,
1533 "/__fixups__", NULL, NULL, 0);
1534 struct device_tree_node *overlay_symbols = dt_find_node_by_path(overlay,
1535 "/__symbols__", NULL, NULL, 0);
1536 if (fixups && dt_fixup_all_externals(tree, symbols, overlay,
1537 fixups, overlay_symbols) < 0) {
Elyes HAOUAS8b5841e2022-02-08 21:23:12 +01001538 printk(BIOS_ERR, "cannot match external fixups from overlay\n");
Julius Werner735ddc92019-05-07 17:05:28 -07001539 return -1;
1540 }
1541
Julius Werner23df4772019-05-17 22:50:18 -07001542 /* After all this fixing up, we can finally merge overlay into the tree
1543 (one fragment at a time, because for some reason it's split up). */
Julius Werner735ddc92019-05-07 17:05:28 -07001544 struct device_tree_node *fragment;
1545 list_for_each(fragment, overlay->root->children, list_node)
1546 if (dt_import_fragment(tree, fragment, overlay_symbols) < 0) {
Elyes HAOUAS8b5841e2022-02-08 21:23:12 +01001547 printk(BIOS_ERR, "bad DT fragment '%s'\n",
Julius Werner735ddc92019-05-07 17:05:28 -07001548 fragment->name);
1549 return -1;
1550 }
1551
Julius Werner23df4772019-05-17 22:50:18 -07001552 /*
1553 * We need to also update /__symbols__ to include labels from this
1554 * overlay, in case we want to load further overlays with external
1555 * phandle references to it. If the base tree already has a /__symbols__
1556 * we merge them together, otherwise we just insert the overlay's
1557 * /__symbols__ node into the base tree root.
1558 */
Julius Werner735ddc92019-05-07 17:05:28 -07001559 if (overlay_symbols) {
1560 if (symbols)
1561 dt_copy_subtree(symbols, overlay_symbols, 0);
1562 else
1563 list_insert_after(&overlay_symbols->list_node,
1564 &tree->root->children);
1565 }
1566
1567 return 0;
1568}