blob: f70aaf7115fd9b2fdd642bc08018013215d9ea0e [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>
Maximilian Bruneda336cd2023-09-16 20:08:41 +02005#include <commonlib/device_tree.h>
Joel Kitching393c71c2019-06-16 16:09:42 +08006#include <ctype.h>
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02007#include <endian.h>
Maximilian Brune33079b82024-03-04 15:34:41 +01008#include <stdbool.h>
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02009#include <stdint.h>
Maximilian Bruneda336cd2023-09-16 20:08:41 +020010#ifdef __COREBOOT__
11#include <console/console.h>
12#else
13#include <stdio.h>
14#define printk(level, ...) printf(__VA_ARGS__)
15#endif
Elyes Haouasbdd03c22024-05-27 11:20:07 +020016#include <stdio.h>
Patrick Rudolph666c1722018-04-03 09:57:33 +020017#include <string.h>
18#include <stddef.h>
19#include <stdlib.h>
Alper Nebi Yasak377157c2024-02-05 17:31:20 +030020#include <limits.h>
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020021
Maximilian Brune33079b82024-03-04 15:34:41 +010022#define FDT_PATH_MAX_DEPTH 10 // should be a good enough upper bound
23#define FDT_PATH_MAX_LEN 128 // should be a good enough upper bound
Alper Nebi Yasak377157c2024-02-05 17:31:20 +030024#define FDT_MAX_MEMORY_NODES 4 // should be a good enough upper bound
25#define FDT_MAX_MEMORY_REGIONS 16 // should be a good enough upper bound
Maximilian Brune33079b82024-03-04 15:34:41 +010026
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020027/*
28 * Functions for picking apart flattened trees.
29 */
30
Maximilian Brune33079b82024-03-04 15:34:41 +010031static int fdt_skip_nops(const void *blob, uint32_t offset)
32{
33 uint32_t *ptr = (uint32_t *)(((uint8_t *)blob) + offset);
34
35 int index = 0;
36 while (be32toh(ptr[index]) == FDT_TOKEN_NOP)
37 index++;
38
39 return index * sizeof(uint32_t);
40}
41
Patrick Rudolph0a7d6902018-08-22 09:55:15 +020042int fdt_next_property(const void *blob, uint32_t offset,
43 struct fdt_property *prop)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020044{
Patrick Rudolph666c1722018-04-03 09:57:33 +020045 struct fdt_header *header = (struct fdt_header *)blob;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020046 uint32_t *ptr = (uint32_t *)(((uint8_t *)blob) + offset);
47
Maximilian Brune33079b82024-03-04 15:34:41 +010048 // skip NOP tokens
49 offset += fdt_skip_nops(blob, offset);
50
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020051 int index = 0;
Patrick Rudolph666c1722018-04-03 09:57:33 +020052 if (be32toh(ptr[index++]) != FDT_TOKEN_PROPERTY)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020053 return 0;
54
Patrick Rudolph666c1722018-04-03 09:57:33 +020055 uint32_t size = be32toh(ptr[index++]);
56 uint32_t name_offset = be32toh(ptr[index++]);
57 name_offset += be32toh(header->strings_offset);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020058
59 if (prop) {
60 prop->name = (char *)((uint8_t *)blob + name_offset);
61 prop->data = &ptr[index];
62 prop->size = size;
63 }
64
Patrick Rudolph666c1722018-04-03 09:57:33 +020065 index += DIV_ROUND_UP(size, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020066
Patrick Rudolph666c1722018-04-03 09:57:33 +020067 return index * sizeof(uint32_t);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020068}
69
Maximilian Brune33079b82024-03-04 15:34:41 +010070/*
71 * fdt_next_node_name reads a node name
72 *
73 * @params blob address of FDT
74 * @params offset offset to the node to read the name from
75 * @params name parameter to hold the name that has been read or NULL
76 *
77 * @returns Either 0 on error or offset to the properties that come after the node name
78 */
79int fdt_next_node_name(const void *blob, uint32_t offset, const char **name)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020080{
Maximilian Brune33079b82024-03-04 15:34:41 +010081 // skip NOP tokens
82 offset += fdt_skip_nops(blob, offset);
83
84 char *ptr = ((char *)blob) + offset;
Julius Wernera5ea3a22019-05-07 17:38:12 -070085 if (be32dec(ptr) != FDT_TOKEN_BEGIN_NODE)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020086 return 0;
87
88 ptr += 4;
89 if (name)
Maximilian Brune33079b82024-03-04 15:34:41 +010090 *name = ptr;
91
92 return ALIGN_UP(strlen(ptr) + 1, 4) + 4;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020093}
94
Maximilian Brune33079b82024-03-04 15:34:41 +010095/*
96 * A utility function to skip past nodes in flattened trees.
97 */
98int fdt_skip_node(const void *blob, uint32_t start_offset)
Julius Werner6702b682019-05-03 18:13:53 -070099{
Maximilian Brune33079b82024-03-04 15:34:41 +0100100 uint32_t offset = start_offset;
101
102 const char *name;
103 int size = fdt_next_node_name(blob, offset, &name);
104 if (!size)
105 return 0;
106 offset += size;
107
108 while ((size = fdt_next_property(blob, offset, NULL)))
109 offset += size;
110
111 while ((size = fdt_skip_node(blob, offset)))
112 offset += size;
113
114 // skip NOP tokens
115 offset += fdt_skip_nops(blob, offset);
116
117 return offset - start_offset + sizeof(uint32_t);
Julius Werner6702b682019-05-03 18:13:53 -0700118}
119
Maximilian Brune33079b82024-03-04 15:34:41 +0100120/*
121 * fdt_read_prop reads a property inside a node
122 *
123 * @params blob address of FDT
124 * @params node_offset offset to the node to read the property from
125 * @params prop_name name of the property to read
126 * @params fdt_prop property is saved inside this parameter
127 *
128 * @returns Either 0 if no property has been found or an offset that points to the location
129 * of the property
130 */
131u32 fdt_read_prop(const void *blob, u32 node_offset, const char *prop_name,
132 struct fdt_property *fdt_prop)
133{
134 u32 offset = node_offset;
135
136 offset += fdt_next_node_name(blob, offset, NULL); // skip node name
137
138 size_t size;
139 while ((size = fdt_next_property(blob, offset, fdt_prop))) {
140 if (strcmp(fdt_prop->name, prop_name) == 0)
141 return offset;
142 offset += size;
143 }
144 return 0; // property not found
145}
146
147/*
148 * fdt_read_reg_prop reads the reg property inside a node
149 *
150 * @params blob address of FDT
151 * @params node_offset offset to the node to read the reg property from
152 * @params addr_cells number of cells used for one address
153 * @params size_cells number of cells used for one size
154 * @params regions all regions that are read inside the reg property are saved inside
155 * this array
156 * @params regions_count maximum number of entries that can be saved inside the regions array.
157 *
158 * Returns: Either 0 on error or returns the number of regions put into the regions array.
159 */
160u32 fdt_read_reg_prop(const void *blob, u32 node_offset, u32 addr_cells, u32 size_cells,
161 struct device_tree_region regions[], size_t regions_count)
162{
163 struct fdt_property prop;
164 u32 offset = fdt_read_prop(blob, node_offset, "reg", &prop);
165
166 if (!offset) {
167 printk(BIOS_DEBUG, "no reg property found in node_offset: %x\n", node_offset);
168 return 0;
169 }
170
171 // we found the reg property, now need to parse all regions in 'reg'
172 size_t count = prop.size / (4 * addr_cells + 4 * size_cells);
173 if (count > regions_count) {
174 printk(BIOS_ERR, "reg property at node_offset: %x has more entries (%zd) than regions array can hold (%zd)\n", node_offset, count, regions_count);
175 count = regions_count;
176 }
177 if (addr_cells > 2 || size_cells > 2) {
178 printk(BIOS_ERR, "addr_cells (%d) or size_cells (%d) bigger than 2\n",
179 addr_cells, size_cells);
180 return 0;
181 }
182 uint32_t *ptr = prop.data;
183 for (int i = 0; i < count; i++) {
184 if (addr_cells == 1)
185 regions[i].addr = be32dec(ptr);
186 else if (addr_cells == 2)
187 regions[i].addr = be64dec(ptr);
188 ptr += addr_cells;
189 if (size_cells == 1)
190 regions[i].size = be32dec(ptr);
191 else if (size_cells == 2)
192 regions[i].size = be64dec(ptr);
193 ptr += size_cells;
194 }
195
196 return count; // return the number of regions found in the reg property
197}
198
199static u32 fdt_read_cell_props(const void *blob, u32 node_offset, u32 *addrcp, u32 *sizecp)
200{
201 struct fdt_property prop;
202 u32 offset = node_offset;
203 size_t size;
204 while ((size = fdt_next_property(blob, offset, &prop))) {
205 if (addrcp && !strcmp(prop.name, "#address-cells"))
206 *addrcp = be32dec(prop.data);
207 if (sizecp && !strcmp(prop.name, "#size-cells"))
208 *sizecp = be32dec(prop.data);
209 offset += size;
210 }
211 return offset;
212}
213
214/*
215 * fdt_find_node searches for a node relative to another node
216 *
217 * @params blob address of FDT
218 *
219 * @params parent_node_offset offset to node from which to traverse the tree
220 *
221 * @params path null terminated array of node names specifying a
222 * relative path (e.g: { "cpus", "cpu0", NULL })
223 *
224 * @params addrcp/sizecp If any address-cells and size-cells properties are found that are
225 * part of the parent node of the node we are looking, addrcp and sizecp
226 * are set to these respectively.
227 *
228 * @returns: Either 0 if no node has been found or the offset to the node found
229 */
230static u32 fdt_find_node(const void *blob, u32 parent_node_offset, char **path,
231 u32 *addrcp, u32 *sizecp)
232{
233 if (*path == NULL)
234 return parent_node_offset; // node found
235
236 size_t size = fdt_next_node_name(blob, parent_node_offset, NULL); // skip node name
237
238 /*
239 * get address-cells and size-cells properties while skipping the others.
240 * According to spec address-cells and size-cells are not inherited, but we
241 * intentionally follow the Linux implementation here and treat them as inheritable.
242 */
243 u32 node_offset = fdt_read_cell_props(blob, parent_node_offset + size, addrcp, sizecp);
244
245 const char *node_name;
246 // walk all children nodes
247 while ((size = fdt_next_node_name(blob, node_offset, &node_name))) {
248 if (!strcmp(*path, node_name)) {
249 // traverse one level deeper into the path
250 return fdt_find_node(blob, node_offset, path + 1, addrcp, sizecp);
251 }
252 // node is not the correct one. skip current node
253 node_offset += fdt_skip_node(blob, node_offset);
254 }
255
256 // we have searched everything and could not find a fitting node
257 return 0;
258}
259
260/*
261 * fdt_find_node_by_path finds a node behind a given node path
262 *
263 * @params blob address of FDT
264 * @params path absolute path to the node that should be searched for
265 *
266 * @params addrcp/sizecp Pointer that will be updated with any #address-cells and #size-cells
267 * value found in the node of the node specified by node_offset. Either
268 * may be NULL to ignore. If no #address-cells and #size-cells is found
269 * default values of #address-cells=2 and #size-cells=1 are returned.
270 *
271 * @returns Either 0 on error or the offset to the node found behind the path
272 */
273u32 fdt_find_node_by_path(const void *blob, const char *path, u32 *addrcp, u32 *sizecp)
274{
275 // sanity check
276 if (path[0] != '/') {
277 printk(BIOS_ERR, "devicetree path must start with a /\n");
278 return 0;
279 }
280 if (!blob) {
281 printk(BIOS_ERR, "devicetree blob is NULL\n");
282 return 0;
283 }
284
285 if (addrcp)
286 *addrcp = 2;
287 if (sizecp)
288 *sizecp = 1;
289
290 struct fdt_header *fdt_hdr = (struct fdt_header *)blob;
291
292 /*
293 * split path into separate nodes
294 * e.g: "/cpus/cpu0" -> { "cpus", "cpu0" }
295 */
296 char *path_array[FDT_PATH_MAX_DEPTH];
297 size_t path_size = strlen(path);
298 assert(path_size < FDT_PATH_MAX_LEN);
299 char path_copy[FDT_PATH_MAX_LEN];
300 memcpy(path_copy, path, path_size + 1);
301 char *cur = path_copy;
302 int i;
303 for (i = 0; i < FDT_PATH_MAX_DEPTH; i++) {
304 path_array[i] = strtok_r(NULL, "/", &cur);
305 if (!path_array[i])
306 break;
307 }
308 assert(i < FDT_PATH_MAX_DEPTH);
309
310 return fdt_find_node(blob, be32toh(fdt_hdr->structure_offset), path_array, addrcp, sizecp);
311}
312
313/*
314 * fdt_find_subnodes_by_prefix finds a node with a given prefix relative to a parent node
315 *
316 * @params blob The FDT to search.
317 *
318 * @params node_offset offset to the node of which the children should be searched
319 *
320 * @params prefix A string to search for a node with a given prefix. This can for example
321 * be 'cpu' to look for all nodes matching this prefix. Only children of
322 * node_offset are searched. Therefore in order to search all nodes matching
323 * the 'cpu' prefix, node_offset should probably point to the 'cpus' node.
324 * An empty prefix ("") searches for all children nodes of node_offset.
325 *
326 * @params addrcp/sizecp Pointer that will be updated with any #address-cells and #size-cells
327 * value found in the node of the node specified by node_offset. Either
328 * may be NULL to ignore. If no #address-cells and #size-cells is found
329 * addrcp and sizecp are left untouched.
330 *
331 * @params results Array of offsets pointing to each node matching the given prefix.
332 * @params results_len Number of entries allocated for the 'results' array
333 *
334 * @returns offset to last node found behind path or 0 if no node has been found
335 */
336size_t fdt_find_subnodes_by_prefix(const void *blob, u32 node_offset, const char *prefix,
337 u32 *addrcp, u32 *sizecp, u32 *results, size_t results_len)
338{
339 // sanity checks
340 if (!blob || !results || !prefix) {
341 printk(BIOS_ERR, "%s: input parameter cannot be null/\n", __func__);
342 return 0;
343 }
344
345 u32 offset = node_offset;
346
347 // we don't care about the name of the current node
348 u32 size = fdt_next_node_name(blob, offset, NULL);
349 if (!size) {
350 printk(BIOS_ERR, "%s: node_offset: %x does not point to a node\n",
351 __func__, node_offset);
352 return 0;
353 }
354 offset += size;
355
356 /*
357 * update addrcp and sizecp if the node contains an address-cells and size-cells
358 * property. Otherwise use addrcp and sizecp provided by caller.
359 */
360 offset = fdt_read_cell_props(blob, offset, addrcp, sizecp);
361
362 size_t count_results = 0;
363 int prefix_len = strlen(prefix);
364 const char *node_name;
365 // walk all children nodes of offset
366 while ((size = fdt_next_node_name(blob, offset, &node_name))) {
367
368 if (count_results >= results_len) {
369 printk(BIOS_WARNING,
370 "%s: results_len (%zd) smaller than count_results (%zd)\n",
371 __func__, results_len, count_results);
372 break;
373 }
374
375 if (!strncmp(prefix, node_name, prefix_len)) {
376 // we found a node that matches the prefix
377 results[count_results++] = offset;
378 }
379
380 // node does not match the prefix. skip current node
381 offset += fdt_skip_node(blob, offset);
382 }
383
384 // return last occurrence
385 return count_results;
386}
387
388static const char *fdt_read_alias_prop(const void *blob, const char *alias_name)
389{
390 u32 node_offset = fdt_find_node_by_path(blob, "/aliases", NULL, NULL);
391 if (!node_offset) {
392 printk(BIOS_DEBUG, "no /aliases node found\n");
393 return NULL;
394 }
395 struct fdt_property alias_prop;
396 if (!fdt_read_prop(blob, node_offset, alias_name, &alias_prop)) {
397 printk(BIOS_DEBUG, "property %s in /aliases node not found\n", alias_name);
398 return NULL;
399 }
400 return (const char *)alias_prop.data;
401}
402
403/*
404 * Find a node in the tree from a string device tree path.
405 *
406 * @params blob Address to the FDT
407 * @params alias_name node name alias that should be searched for.
408 * @params addrcp/sizecp Pointer that will be updated with any #address-cells and #size-cells
409 * value found in the node of the node specified by node_offset. Either
410 * may be NULL to ignore. If no #address-cells and #size-cells is found
411 * default values of #address-cells=2 and #size-cells=1 are returned.
412 *
413 * @returns offset to last node found behind path or 0 if no node has been found
414 */
415u32 fdt_find_node_by_alias(const void *blob, const char *alias_name, u32 *addrcp, u32 *sizecp)
416{
417 const char *node_name = fdt_read_alias_prop(blob, alias_name);
418 if (!node_name) {
419 printk(BIOS_DEBUG, "alias %s not found\n", alias_name);
420 return 0;
421 }
422
423 u32 node_offset = fdt_find_node_by_path(blob, node_name, addrcp, sizecp);
424 if (!node_offset) {
425 // This should not happen (invalid devicetree)
426 printk(BIOS_WARNING,
427 "Could not find node '%s', which alias was referring to '%s'\n",
428 node_name, alias_name);
429 return 0;
430 }
431 return node_offset;
432}
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200433
434
435/*
436 * Functions for printing flattened trees.
437 */
438
439static void print_indent(int depth)
440{
Julius Werner0d746532019-05-06 19:35:56 -0700441 printk(BIOS_DEBUG, "%*s", depth * 8, "");
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200442}
443
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200444static void print_property(const struct fdt_property *prop, int depth)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200445{
Julius Werner0d746532019-05-06 19:35:56 -0700446 int is_string = prop->size > 0 &&
447 ((char *)prop->data)[prop->size - 1] == '\0';
448
Maximilian Brune77eaec62023-09-20 05:12:04 +0200449 if (is_string) {
450 for (int i = 0; i < prop->size - 1; i++) {
451 if (!isprint(((char *)prop->data)[i])) {
Julius Werner0d746532019-05-06 19:35:56 -0700452 is_string = 0;
Maximilian Brune77eaec62023-09-20 05:12:04 +0200453 break;
454 }
455 }
456 }
Julius Werner0d746532019-05-06 19:35:56 -0700457
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200458 print_indent(depth);
Julius Werner0d746532019-05-06 19:35:56 -0700459 if (is_string) {
460 printk(BIOS_DEBUG, "%s = \"%s\";\n",
461 prop->name, (const char *)prop->data);
462 } else {
463 printk(BIOS_DEBUG, "%s = < ", prop->name);
464 for (int i = 0; i < MIN(128, prop->size); i += 4) {
465 uint32_t val = 0;
466 for (int j = 0; j < MIN(4, prop->size - i); j++)
467 val |= ((uint8_t *)prop->data)[i + j] <<
468 (24 - j * 8);
469 printk(BIOS_DEBUG, "%#.2x ", val);
470 }
471 if (prop->size > 128)
472 printk(BIOS_DEBUG, "...");
473 printk(BIOS_DEBUG, ">;\n");
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200474 }
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200475}
476
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200477static int print_flat_node(const void *blob, uint32_t start_offset, int depth)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200478{
479 int offset = start_offset;
480 const char *name;
481 int size;
482
Maximilian Brune33079b82024-03-04 15:34:41 +0100483 size = fdt_next_node_name(blob, offset, &name);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200484 if (!size)
485 return 0;
486 offset += size;
487
488 print_indent(depth);
Julius Werner0d746532019-05-06 19:35:56 -0700489 printk(BIOS_DEBUG, "%s {\n", name);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200490
Patrick Rudolph666c1722018-04-03 09:57:33 +0200491 struct fdt_property prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200492 while ((size = fdt_next_property(blob, offset, &prop))) {
493 print_property(&prop, depth + 1);
494
495 offset += size;
496 }
497
Julius Werner23df4772019-05-17 22:50:18 -0700498 printk(BIOS_DEBUG, "\n"); /* empty line between props and nodes */
Julius Werner0d746532019-05-06 19:35:56 -0700499
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200500 while ((size = print_flat_node(blob, offset, depth + 1)))
501 offset += size;
502
Julius Werner0d746532019-05-06 19:35:56 -0700503 print_indent(depth);
504 printk(BIOS_DEBUG, "}\n");
505
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200506 return offset - start_offset + sizeof(uint32_t);
507}
508
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200509void fdt_print_node(const void *blob, uint32_t offset)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200510{
511 print_flat_node(blob, offset, 0);
512}
513
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200514/*
Alper Nebi Yasak377157c2024-02-05 17:31:20 +0300515 * fdt_read_memory_regions finds memory ranges from a flat device-tree
516 *
517 * @params blob address of FDT
518 * @params regions all regions that are read inside the reg property of
519 * memory nodes are saved inside this array
520 * @params regions_count maximum number of entries that can be saved inside
521 * the regions array.
522 *
523 * Returns: Either 0 on error or returns the number of regions put into the regions array.
524 */
525size_t fdt_read_memory_regions(const void *blob,
526 struct device_tree_region regions[],
527 size_t regions_count)
528{
529 u32 node, root, addrcp, sizecp;
530 u32 nodes[FDT_MAX_MEMORY_NODES] = {0};
531 size_t region_idx = 0;
532 size_t node_count = 0;
533
534 if (!fdt_is_valid(blob))
535 return 0;
536
537 node = fdt_find_node_by_path(blob, "/memory", &addrcp, &sizecp);
538 if (node) {
539 region_idx += fdt_read_reg_prop(blob, node, addrcp, sizecp,
540 regions, regions_count);
541 if (region_idx >= regions_count) {
542 printk(BIOS_WARNING, "FDT: Too many memory regions\n");
543 goto out;
544 }
545 }
546
547 root = fdt_find_node_by_path(blob, "/", &addrcp, &sizecp);
548 node_count = fdt_find_subnodes_by_prefix(blob, root, "memory@",
549 &addrcp, &sizecp, nodes,
550 FDT_MAX_MEMORY_NODES);
551 if (node_count >= FDT_MAX_MEMORY_NODES) {
552 printk(BIOS_WARNING, "FDT: Too many memory nodes\n");
553 /* Can still reading the regions for those we got */
554 }
555
556 for (size_t i = 0; i < MIN(node_count, FDT_MAX_MEMORY_NODES); i++) {
557 region_idx += fdt_read_reg_prop(blob, nodes[i], addrcp, sizecp,
558 &regions[region_idx],
559 regions_count - region_idx);
560 if (region_idx >= regions_count) {
561 printk(BIOS_WARNING, "FDT: Too many memory regions\n");
562 goto out;
563 }
564 }
565
566out:
567 for (size_t i = 0; i < MIN(region_idx, regions_count); i++) {
568 printk(BIOS_DEBUG, "FDT: Memory region [%#llx - %#llx]\n",
569 regions[i].addr, regions[i].addr + regions[i].size);
570 }
571
572 return region_idx;
573}
574
575/*
576 * fdt_get_memory_top finds top of memory from a flat device-tree
577 *
578 * @params blob address of FDT
579 *
580 * Returns: Either 0 on error or returns the maximum memory address
581 */
582uint64_t fdt_get_memory_top(const void *blob)
583{
584 struct device_tree_region regions[FDT_MAX_MEMORY_REGIONS] = {0};
585 uint64_t top = 0;
586 uint64_t total = 0;
587 size_t count;
588
589 if (!fdt_is_valid(blob))
590 return 0;
591
592 count = fdt_read_memory_regions(blob, regions, FDT_MAX_MEMORY_REGIONS);
593 for (size_t i = 0; i < MIN(count, FDT_MAX_MEMORY_REGIONS); i++) {
594 top = MAX(top, regions[i].addr + regions[i].size);
595 total += regions[i].size;
596 }
597
598 printk(BIOS_DEBUG, "FDT: Found %u MiB of RAM\n",
599 (uint32_t)(total / MiB));
600
601 return top;
602}
603
604/*
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200605 * Functions to turn a flattened tree into an unflattened one.
606 */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200607
Maximilian Brune33079b82024-03-04 15:34:41 +0100608static int dt_prop_is_phandle(struct device_tree_property *prop)
609{
610 return !(strcmp("phandle", prop->prop.name) &&
611 strcmp("linux,phandle", prop->prop.name));
612}
613
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200614static int fdt_unflatten_node(const void *blob, uint32_t start_offset,
Julius Werner6702b682019-05-03 18:13:53 -0700615 struct device_tree *tree,
Patrick Rudolph666c1722018-04-03 09:57:33 +0200616 struct device_tree_node **new_node)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200617{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200618 struct list_node *last;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200619 int offset = start_offset;
620 const char *name;
621 int size;
622
Maximilian Brune33079b82024-03-04 15:34:41 +0100623 size = fdt_next_node_name(blob, offset, &name);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200624 if (!size)
625 return 0;
626 offset += size;
627
Julius Werner9636a102019-05-03 17:36:43 -0700628 struct device_tree_node *node = xzalloc(sizeof(*node));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200629 *new_node = node;
630 node->name = name;
631
Patrick Rudolph666c1722018-04-03 09:57:33 +0200632 struct fdt_property fprop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200633 last = &node->properties;
634 while ((size = fdt_next_property(blob, offset, &fprop))) {
Julius Werner9636a102019-05-03 17:36:43 -0700635 struct device_tree_property *prop = xzalloc(sizeof(*prop));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200636 prop->prop = fprop;
637
Julius Werner6702b682019-05-03 18:13:53 -0700638 if (dt_prop_is_phandle(prop)) {
639 node->phandle = be32dec(prop->prop.data);
640 if (node->phandle > tree->max_phandle)
641 tree->max_phandle = node->phandle;
642 }
643
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200644 list_insert_after(&prop->list_node, last);
645 last = &prop->list_node;
646
647 offset += size;
648 }
649
Patrick Rudolph666c1722018-04-03 09:57:33 +0200650 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200651 last = &node->children;
Julius Werner6702b682019-05-03 18:13:53 -0700652 while ((size = fdt_unflatten_node(blob, offset, tree, &child))) {
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200653 list_insert_after(&child->list_node, last);
654 last = &child->list_node;
655
656 offset += size;
657 }
658
659 return offset - start_offset + sizeof(uint32_t);
660}
661
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200662static int fdt_unflatten_map_entry(const void *blob, uint32_t offset,
Patrick Rudolph666c1722018-04-03 09:57:33 +0200663 struct device_tree_reserve_map_entry **new)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200664{
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200665 const uint64_t *ptr = (const uint64_t *)(((uint8_t *)blob) + offset);
666 const uint64_t start = be64toh(ptr[0]);
667 const uint64_t size = be64toh(ptr[1]);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200668
669 if (!size)
670 return 0;
671
Julius Werner9636a102019-05-03 17:36:43 -0700672 struct device_tree_reserve_map_entry *entry = xzalloc(sizeof(*entry));
Patrick Rudolph666c1722018-04-03 09:57:33 +0200673 *new = entry;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200674 entry->start = start;
675 entry->size = size;
676
677 return sizeof(uint64_t) * 2;
678}
679
Maximilian Brune33079b82024-03-04 15:34:41 +0100680bool fdt_is_valid(const void *blob)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200681{
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200682 const struct fdt_header *header = (const struct fdt_header *)blob;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200683
Julius Werner73eaec82019-05-03 17:58:07 -0700684 uint32_t magic = be32toh(header->magic);
685 uint32_t version = be32toh(header->version);
686 uint32_t last_comp_version = be32toh(header->last_comp_version);
687
688 if (magic != FDT_HEADER_MAGIC) {
Maximilian Brune33079b82024-03-04 15:34:41 +0100689 printk(BIOS_ERR, "Invalid device tree magic %#.8x!\n", magic);
690 return false;
Julius Werner73eaec82019-05-03 17:58:07 -0700691 }
692 if (last_comp_version > FDT_SUPPORTED_VERSION) {
Maximilian Brune33079b82024-03-04 15:34:41 +0100693 printk(BIOS_ERR, "Unsupported device tree version %u(>=%u)\n",
Julius Werner73eaec82019-05-03 17:58:07 -0700694 version, last_comp_version);
Maximilian Brune33079b82024-03-04 15:34:41 +0100695 return false;
Julius Werner73eaec82019-05-03 17:58:07 -0700696 }
697 if (version > FDT_SUPPORTED_VERSION)
Elyes Haouasd7326282022-12-21 08:16:03 +0100698 printk(BIOS_NOTICE, "FDT version %u too new, should add support!\n",
Julius Werner73eaec82019-05-03 17:58:07 -0700699 version);
Maximilian Brune33079b82024-03-04 15:34:41 +0100700 return true;
701}
702
703struct device_tree *fdt_unflatten(const void *blob)
704{
705 struct device_tree *tree = xzalloc(sizeof(*tree));
706 const struct fdt_header *header = (const struct fdt_header *)blob;
707 tree->header = header;
708
Maximilian Brune64663542024-06-03 05:24:32 +0200709 if (!fdt_is_valid(blob))
Maximilian Brune33079b82024-03-04 15:34:41 +0100710 return NULL;
Julius Werner73eaec82019-05-03 17:58:07 -0700711
Patrick Rudolph666c1722018-04-03 09:57:33 +0200712 uint32_t struct_offset = be32toh(header->structure_offset);
713 uint32_t strings_offset = be32toh(header->strings_offset);
714 uint32_t reserve_offset = be32toh(header->reserve_map_offset);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200715 uint32_t min_offset = 0;
716 min_offset = MIN(struct_offset, strings_offset);
717 min_offset = MIN(min_offset, reserve_offset);
Julius Werner23df4772019-05-17 22:50:18 -0700718 /* Assume everything up to the first non-header component is part of
719 the header and needs to be preserved. This will protect us against
720 new elements being added in the future. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200721 tree->header_size = min_offset;
722
Patrick Rudolph666c1722018-04-03 09:57:33 +0200723 struct device_tree_reserve_map_entry *entry;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200724 uint32_t offset = reserve_offset;
725 int size;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200726 struct list_node *last = &tree->reserve_map;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200727 while ((size = fdt_unflatten_map_entry(blob, offset, &entry))) {
728 list_insert_after(&entry->list_node, last);
729 last = &entry->list_node;
730
731 offset += size;
732 }
733
Julius Werner6702b682019-05-03 18:13:53 -0700734 fdt_unflatten_node(blob, struct_offset, tree, &tree->root);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200735
736 return tree;
737}
738
739
740
741/*
Patrick Rudolph666c1722018-04-03 09:57:33 +0200742 * Functions to find the size of the device tree if it was flattened.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200743 */
744
Patrick Rudolph666c1722018-04-03 09:57:33 +0200745static void dt_flat_prop_size(struct device_tree_property *prop,
746 uint32_t *struct_size, uint32_t *strings_size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200747{
Julius Werner23df4772019-05-17 22:50:18 -0700748 /* Starting token. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200749 *struct_size += sizeof(uint32_t);
Julius Werner23df4772019-05-17 22:50:18 -0700750 /* Size. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200751 *struct_size += sizeof(uint32_t);
Julius Werner23df4772019-05-17 22:50:18 -0700752 /* Name offset. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200753 *struct_size += sizeof(uint32_t);
Julius Werner23df4772019-05-17 22:50:18 -0700754 /* Property value. */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200755 *struct_size += ALIGN_UP(prop->prop.size, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200756
Julius Werner23df4772019-05-17 22:50:18 -0700757 /* Property name. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200758 *strings_size += strlen(prop->prop.name) + 1;
759}
760
Patrick Rudolph666c1722018-04-03 09:57:33 +0200761static void dt_flat_node_size(struct device_tree_node *node,
762 uint32_t *struct_size, uint32_t *strings_size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200763{
Julius Werner23df4772019-05-17 22:50:18 -0700764 /* Starting token. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200765 *struct_size += sizeof(uint32_t);
Julius Werner23df4772019-05-17 22:50:18 -0700766 /* Node name. */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200767 *struct_size += ALIGN_UP(strlen(node->name) + 1, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200768
Patrick Rudolph666c1722018-04-03 09:57:33 +0200769 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200770 list_for_each(prop, node->properties, list_node)
771 dt_flat_prop_size(prop, struct_size, strings_size);
772
Patrick Rudolph666c1722018-04-03 09:57:33 +0200773 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200774 list_for_each(child, node->children, list_node)
775 dt_flat_node_size(child, struct_size, strings_size);
776
Julius Werner23df4772019-05-17 22:50:18 -0700777 /* End token. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200778 *struct_size += sizeof(uint32_t);
779}
780
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200781uint32_t dt_flat_size(const struct device_tree *tree)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200782{
783 uint32_t size = tree->header_size;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200784 struct device_tree_reserve_map_entry *entry;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200785 list_for_each(entry, tree->reserve_map, list_node)
786 size += sizeof(uint64_t) * 2;
787 size += sizeof(uint64_t) * 2;
788
789 uint32_t struct_size = 0;
790 uint32_t strings_size = 0;
791 dt_flat_node_size(tree->root, &struct_size, &strings_size);
792
793 size += struct_size;
Julius Werner23df4772019-05-17 22:50:18 -0700794 /* End token. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200795 size += sizeof(uint32_t);
796
797 size += strings_size;
798
799 return size;
800}
801
802
803
804/*
805 * Functions to flatten a device tree.
806 */
807
Patrick Rudolph666c1722018-04-03 09:57:33 +0200808static void dt_flatten_map_entry(struct device_tree_reserve_map_entry *entry,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200809 void **map_start)
810{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200811 ((uint64_t *)*map_start)[0] = htobe64(entry->start);
812 ((uint64_t *)*map_start)[1] = htobe64(entry->size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200813 *map_start = ((uint8_t *)*map_start) + sizeof(uint64_t) * 2;
814}
815
Patrick Rudolph666c1722018-04-03 09:57:33 +0200816static void dt_flatten_prop(struct device_tree_property *prop,
817 void **struct_start, void *strings_base,
818 void **strings_start)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200819{
820 uint8_t *dstruct = (uint8_t *)*struct_start;
821 uint8_t *dstrings = (uint8_t *)*strings_start;
822
Julius Wernera5ea3a22019-05-07 17:38:12 -0700823 be32enc(dstruct, FDT_TOKEN_PROPERTY);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200824 dstruct += sizeof(uint32_t);
825
Julius Wernera5ea3a22019-05-07 17:38:12 -0700826 be32enc(dstruct, prop->prop.size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200827 dstruct += sizeof(uint32_t);
828
829 uint32_t name_offset = (uintptr_t)dstrings - (uintptr_t)strings_base;
Julius Wernera5ea3a22019-05-07 17:38:12 -0700830 be32enc(dstruct, name_offset);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200831 dstruct += sizeof(uint32_t);
832
833 strcpy((char *)dstrings, prop->prop.name);
834 dstrings += strlen(prop->prop.name) + 1;
835
836 memcpy(dstruct, prop->prop.data, prop->prop.size);
Patrick Rudolph666c1722018-04-03 09:57:33 +0200837 dstruct += ALIGN_UP(prop->prop.size, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200838
839 *struct_start = dstruct;
840 *strings_start = dstrings;
841}
842
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200843static void dt_flatten_node(const struct device_tree_node *node,
844 void **struct_start, void *strings_base,
845 void **strings_start)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200846{
847 uint8_t *dstruct = (uint8_t *)*struct_start;
848 uint8_t *dstrings = (uint8_t *)*strings_start;
849
Julius Wernera5ea3a22019-05-07 17:38:12 -0700850 be32enc(dstruct, FDT_TOKEN_BEGIN_NODE);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200851 dstruct += sizeof(uint32_t);
852
853 strcpy((char *)dstruct, node->name);
Patrick Rudolph666c1722018-04-03 09:57:33 +0200854 dstruct += ALIGN_UP(strlen(node->name) + 1, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200855
Patrick Rudolph666c1722018-04-03 09:57:33 +0200856 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200857 list_for_each(prop, node->properties, list_node)
858 dt_flatten_prop(prop, (void **)&dstruct, strings_base,
859 (void **)&dstrings);
860
Patrick Rudolph666c1722018-04-03 09:57:33 +0200861 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200862 list_for_each(child, node->children, list_node)
863 dt_flatten_node(child, (void **)&dstruct, strings_base,
864 (void **)&dstrings);
865
Julius Wernera5ea3a22019-05-07 17:38:12 -0700866 be32enc(dstruct, FDT_TOKEN_END_NODE);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200867 dstruct += sizeof(uint32_t);
868
869 *struct_start = dstruct;
870 *strings_start = dstrings;
871}
872
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200873void dt_flatten(const struct device_tree *tree, void *start_dest)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200874{
875 uint8_t *dest = (uint8_t *)start_dest;
876
877 memcpy(dest, tree->header, tree->header_size);
Patrick Rudolph666c1722018-04-03 09:57:33 +0200878 struct fdt_header *header = (struct fdt_header *)dest;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200879 dest += tree->header_size;
880
Patrick Rudolph666c1722018-04-03 09:57:33 +0200881 struct device_tree_reserve_map_entry *entry;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200882 list_for_each(entry, tree->reserve_map, list_node)
883 dt_flatten_map_entry(entry, (void **)&dest);
884 ((uint64_t *)dest)[0] = ((uint64_t *)dest)[1] = 0;
885 dest += sizeof(uint64_t) * 2;
886
887 uint32_t struct_size = 0;
888 uint32_t strings_size = 0;
889 dt_flat_node_size(tree->root, &struct_size, &strings_size);
890
891 uint8_t *struct_start = dest;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200892 header->structure_offset = htobe32(dest - (uint8_t *)start_dest);
893 header->structure_size = htobe32(struct_size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200894 dest += struct_size;
895
Patrick Rudolph666c1722018-04-03 09:57:33 +0200896 *((uint32_t *)dest) = htobe32(FDT_TOKEN_END);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200897 dest += sizeof(uint32_t);
898
899 uint8_t *strings_start = dest;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200900 header->strings_offset = htobe32(dest - (uint8_t *)start_dest);
901 header->strings_size = htobe32(strings_size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200902 dest += strings_size;
903
904 dt_flatten_node(tree->root, (void **)&struct_start, strings_start,
905 (void **)&strings_start);
906
Patrick Rudolph666c1722018-04-03 09:57:33 +0200907 header->totalsize = htobe32(dest - (uint8_t *)start_dest);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200908}
909
910
911
912/*
913 * Functions for printing a non-flattened device tree.
914 */
915
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200916static void print_node(const struct device_tree_node *node, int depth)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200917{
918 print_indent(depth);
Julius Werner23df4772019-05-17 22:50:18 -0700919 if (depth == 0) /* root node has no name, print a starting slash */
Julius Werner0d746532019-05-06 19:35:56 -0700920 printk(BIOS_DEBUG, "/");
921 printk(BIOS_DEBUG, "%s {\n", node->name);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200922
Patrick Rudolph666c1722018-04-03 09:57:33 +0200923 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200924 list_for_each(prop, node->properties, list_node)
925 print_property(&prop->prop, depth + 1);
926
Julius Werner23df4772019-05-17 22:50:18 -0700927 printk(BIOS_DEBUG, "\n"); /* empty line between props and nodes */
Julius Werner0d746532019-05-06 19:35:56 -0700928
Patrick Rudolph666c1722018-04-03 09:57:33 +0200929 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200930 list_for_each(child, node->children, list_node)
931 print_node(child, depth + 1);
Julius Werner0d746532019-05-06 19:35:56 -0700932
933 print_indent(depth);
934 printk(BIOS_DEBUG, "};\n");
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200935}
936
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200937void dt_print_node(const struct device_tree_node *node)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200938{
939 print_node(node, 0);
940}
941
942
943
944/*
945 * Functions for reading and manipulating an unflattened device tree.
946 */
947
948/*
949 * Read #address-cells and #size-cells properties from a node.
950 *
951 * @param node The device tree node to read from.
952 * @param addrcp Pointer to store #address-cells in, skipped if NULL.
953 * @param sizecp Pointer to store #size-cells in, skipped if NULL.
954 */
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200955void dt_read_cell_props(const struct device_tree_node *node, u32 *addrcp,
956 u32 *sizecp)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200957{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200958 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200959 list_for_each(prop, node->properties, list_node) {
960 if (addrcp && !strcmp("#address-cells", prop->prop.name))
Julius Wernera5ea3a22019-05-07 17:38:12 -0700961 *addrcp = be32dec(prop->prop.data);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200962 if (sizecp && !strcmp("#size-cells", prop->prop.name))
Julius Wernera5ea3a22019-05-07 17:38:12 -0700963 *sizecp = be32dec(prop->prop.data);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200964 }
965}
966
967/*
968 * Find a node from a device tree path, relative to a parent node.
969 *
970 * @param parent The node from which to start the relative path lookup.
971 * @param path An array of path component strings that will be looked
Elyes HAOUASe3e3f4f2018-06-29 21:41:41 +0200972 * up in order to find the node. Must be terminated with
973 * a NULL pointer. Example: {'firmware', 'coreboot', NULL}
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200974 * @param addrcp Pointer that will be updated with any #address-cells
Elyes HAOUASe3e3f4f2018-06-29 21:41:41 +0200975 * value found in the path. May be NULL to ignore.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200976 * @param sizecp Pointer that will be updated with any #size-cells
Elyes HAOUASe3e3f4f2018-06-29 21:41:41 +0200977 * value found in the path. May be NULL to ignore.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200978 * @param create 1: Create node(s) if not found. 0: Return NULL instead.
979 * @return The found/created node, or NULL.
980 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200981struct device_tree_node *dt_find_node(struct device_tree_node *parent,
982 const char **path, u32 *addrcp,
983 u32 *sizecp, int create)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200984{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200985 struct device_tree_node *node, *found = NULL;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200986
Julius Werner23df4772019-05-17 22:50:18 -0700987 /* Update #address-cells and #size-cells for this level. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200988 dt_read_cell_props(parent, addrcp, sizecp);
989
990 if (!*path)
991 return parent;
992
Julius Werner23df4772019-05-17 22:50:18 -0700993 /* Find the next node in the path, if it exists. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200994 list_for_each(node, parent->children, list_node) {
995 if (!strcmp(node->name, *path)) {
996 found = node;
997 break;
998 }
999 }
1000
Julius Werner23df4772019-05-17 22:50:18 -07001001 /* Otherwise create it or return NULL. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001002 if (!found) {
1003 if (!create)
1004 return NULL;
1005
Sergii Dmytruk206328d2022-03-13 18:23:17 +02001006 found = calloc(1, sizeof(*found));
Patrick Rudolph666c1722018-04-03 09:57:33 +02001007 if (!found)
1008 return NULL;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001009 found->name = strdup(*path);
1010 if (!found->name)
1011 return NULL;
1012
1013 list_insert_after(&found->list_node, &parent->children);
1014 }
1015
1016 return dt_find_node(found, path + 1, addrcp, sizecp, create);
1017}
1018
1019/*
Julius Wernerf36d53c2019-05-03 18:23:34 -07001020 * Find a node in the tree from a string device tree path.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001021 *
Julius Wernerf36d53c2019-05-03 18:23:34 -07001022 * @param tree The device tree to search.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001023 * @param path A string representing a path in the device tree, with
Julius Wernerfbec63d2019-05-03 18:29:28 -07001024 * nodes separated by '/'. Example: "/firmware/coreboot"
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001025 * @param addrcp Pointer that will be updated with any #address-cells
1026 * value found in the path. May be NULL to ignore.
1027 * @param sizecp Pointer that will be updated with any #size-cells
1028 * value found in the path. May be NULL to ignore.
1029 * @param create 1: Create node(s) if not found. 0: Return NULL instead.
1030 * @return The found/created node, or NULL.
1031 *
Julius Werner6d5695f2019-05-06 19:23:28 -07001032 * It is the caller responsibility to provide a path string that doesn't end
1033 * with a '/' and doesn't contain any "//". If the path does not start with a
1034 * '/', the first segment is interpreted as an alias. */
Julius Wernerf36d53c2019-05-03 18:23:34 -07001035struct device_tree_node *dt_find_node_by_path(struct device_tree *tree,
Patrick Rudolph666c1722018-04-03 09:57:33 +02001036 const char *path, u32 *addrcp,
1037 u32 *sizecp, int create)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001038{
Julius Werner6d5695f2019-05-06 19:23:28 -07001039 char *sub_path;
1040 char *duped_str;
1041 struct device_tree_node *parent;
1042 char *next_slash;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001043 /* Hopefully enough depth for any node. */
1044 const char *path_array[15];
1045 int i;
Patrick Rudolph666c1722018-04-03 09:57:33 +02001046 struct device_tree_node *node = NULL;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001047
Julius Werner23df4772019-05-17 22:50:18 -07001048 if (path[0] == '/') { /* regular path */
1049 if (path[1] == '\0') { /* special case: "/" is root node */
Julius Werner6d5695f2019-05-06 19:23:28 -07001050 dt_read_cell_props(tree->root, addrcp, sizecp);
1051 return tree->root;
1052 }
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001053
Julius Werner6d5695f2019-05-06 19:23:28 -07001054 sub_path = duped_str = strdup(&path[1]);
1055 if (!sub_path)
1056 return NULL;
1057
1058 parent = tree->root;
Julius Werner23df4772019-05-17 22:50:18 -07001059 } else { /* alias */
Julius Werner6d5695f2019-05-06 19:23:28 -07001060 char *alias;
1061
1062 alias = duped_str = strdup(path);
1063 if (!alias)
1064 return NULL;
1065
1066 sub_path = strchr(alias, '/');
1067 if (sub_path)
1068 *sub_path = '\0';
1069
1070 parent = dt_find_node_by_alias(tree, alias);
1071 if (!parent) {
1072 printk(BIOS_DEBUG,
1073 "Could not find node '%s', alias '%s' does not exist\n",
1074 path, alias);
1075 free(duped_str);
1076 return NULL;
1077 }
1078
1079 if (!sub_path) {
Julius Werner23df4772019-05-17 22:50:18 -07001080 /* it's just the alias, no sub-path */
Julius Werner6d5695f2019-05-06 19:23:28 -07001081 free(duped_str);
1082 return parent;
1083 }
1084
1085 sub_path++;
1086 }
1087
1088 next_slash = sub_path;
1089 path_array[0] = sub_path;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001090 for (i = 1; i < (ARRAY_SIZE(path_array) - 1); i++) {
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001091 next_slash = strchr(next_slash, '/');
1092 if (!next_slash)
1093 break;
1094
1095 *next_slash++ = '\0';
1096 path_array[i] = next_slash;
1097 }
1098
1099 if (!next_slash) {
1100 path_array[i] = NULL;
Julius Werner6d5695f2019-05-06 19:23:28 -07001101 node = dt_find_node(parent, path_array,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001102 addrcp, sizecp, create);
1103 }
1104
Julius Werner6d5695f2019-05-06 19:23:28 -07001105 free(duped_str);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001106 return node;
1107}
1108
Julius Werner6d5695f2019-05-06 19:23:28 -07001109/*
1110 * Find a node from an alias
1111 *
1112 * @param tree The device tree.
1113 * @param alias The alias name.
1114 * @return The found node, or NULL.
1115 */
1116struct device_tree_node *dt_find_node_by_alias(struct device_tree *tree,
1117 const char *alias)
1118{
1119 struct device_tree_node *node;
1120 const char *alias_path;
1121
1122 node = dt_find_node_by_path(tree, "/aliases", NULL, NULL, 0);
1123 if (!node)
1124 return NULL;
1125
1126 alias_path = dt_find_string_prop(node, alias);
1127 if (!alias_path)
1128 return NULL;
1129
1130 return dt_find_node_by_path(tree, alias_path, NULL, NULL, 0);
1131}
1132
Julius Werner6702b682019-05-03 18:13:53 -07001133struct device_tree_node *dt_find_node_by_phandle(struct device_tree_node *root,
1134 uint32_t phandle)
1135{
1136 if (!root)
1137 return NULL;
1138
1139 if (root->phandle == phandle)
1140 return root;
1141
1142 struct device_tree_node *node;
1143 struct device_tree_node *result;
1144 list_for_each(node, root->children, list_node) {
1145 result = dt_find_node_by_phandle(node, phandle);
1146 if (result)
1147 return result;
1148 }
1149
1150 return NULL;
1151}
1152
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001153/*
1154 * Check if given node is compatible.
1155 *
1156 * @param node The node which is to be checked for compatible property.
1157 * @param compat The compatible string to match.
1158 * @return 1 = compatible, 0 = not compatible.
1159 */
Patrick Rudolph666c1722018-04-03 09:57:33 +02001160static int dt_check_compat_match(struct device_tree_node *node,
1161 const char *compat)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001162{
Patrick Rudolph666c1722018-04-03 09:57:33 +02001163 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001164
1165 list_for_each(prop, node->properties, list_node) {
1166 if (!strcmp("compatible", prop->prop.name)) {
1167 size_t bytes = prop->prop.size;
1168 const char *str = prop->prop.data;
1169 while (bytes > 0) {
1170 if (!strncmp(compat, str, bytes))
1171 return 1;
1172 size_t len = strnlen(str, bytes) + 1;
1173 if (bytes <= len)
1174 break;
1175 str += len;
1176 bytes -= len;
1177 }
1178 break;
1179 }
1180 }
1181
1182 return 0;
1183}
1184
1185/*
1186 * Find a node from a compatible string, in the subtree of a parent node.
1187 *
1188 * @param parent The parent node under which to look.
1189 * @param compat The compatible string to find.
1190 * @return The found node, or NULL.
1191 */
Patrick Rudolph666c1722018-04-03 09:57:33 +02001192struct device_tree_node *dt_find_compat(struct device_tree_node *parent,
1193 const char *compat)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001194{
Julius Werner23df4772019-05-17 22:50:18 -07001195 /* Check if the parent node itself is compatible. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001196 if (dt_check_compat_match(parent, compat))
1197 return parent;
1198
Patrick Rudolph666c1722018-04-03 09:57:33 +02001199 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001200 list_for_each(child, parent->children, list_node) {
Patrick Rudolph666c1722018-04-03 09:57:33 +02001201 struct device_tree_node *found = dt_find_compat(child, compat);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001202 if (found)
1203 return found;
1204 }
1205
1206 return NULL;
1207}
1208
1209/*
Martin Roth0949e732021-10-01 14:28:22 -06001210 * Find the next compatible child of a given parent. All children up to the
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001211 * child passed in by caller are ignored. If child is NULL, it considers all the
1212 * children to find the first child which is compatible.
1213 *
1214 * @param parent The parent node under which to look.
1215 * @param child The child node to start search from (exclusive). If NULL
1216 * consider all children.
1217 * @param compat The compatible string to find.
1218 * @return The found node, or NULL.
1219 */
Patrick Rudolph666c1722018-04-03 09:57:33 +02001220struct device_tree_node *
1221dt_find_next_compat_child(struct device_tree_node *parent,
1222 struct device_tree_node *child,
1223 const char *compat)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001224{
Patrick Rudolph666c1722018-04-03 09:57:33 +02001225 struct device_tree_node *next;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001226 int ignore = 0;
1227
1228 if (child)
1229 ignore = 1;
1230
1231 list_for_each(next, parent->children, list_node) {
1232 if (ignore) {
1233 if (child == next)
1234 ignore = 0;
1235 continue;
1236 }
1237
1238 if (dt_check_compat_match(next, compat))
1239 return next;
1240 }
1241
1242 return NULL;
1243}
1244
1245/*
1246 * Find a node with matching property value, in the subtree of a parent node.
1247 *
1248 * @param parent The parent node under which to look.
1249 * @param name The property name to look for.
1250 * @param data The property value to look for.
1251 * @param size The property size.
1252 */
Patrick Rudolph666c1722018-04-03 09:57:33 +02001253struct device_tree_node *dt_find_prop_value(struct device_tree_node *parent,
1254 const char *name, void *data,
1255 size_t size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001256{
Patrick Rudolph666c1722018-04-03 09:57:33 +02001257 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001258
1259 /* Check if parent itself has the required property value. */
1260 list_for_each(prop, parent->properties, list_node) {
1261 if (!strcmp(name, prop->prop.name)) {
1262 size_t bytes = prop->prop.size;
Patrick Rudolph0a7d6902018-08-22 09:55:15 +02001263 const void *prop_data = prop->prop.data;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001264 if (size != bytes)
1265 break;
1266 if (!memcmp(data, prop_data, size))
1267 return parent;
1268 break;
1269 }
1270 }
1271
Patrick Rudolph666c1722018-04-03 09:57:33 +02001272 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001273 list_for_each(child, parent->children, list_node) {
Patrick Rudolph666c1722018-04-03 09:57:33 +02001274 struct device_tree_node *found = dt_find_prop_value(child, name,
1275 data, size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001276 if (found)
1277 return found;
1278 }
1279 return NULL;
1280}
1281
1282/*
1283 * Write an arbitrary sized big-endian integer into a pointer.
1284 *
1285 * @param dest Pointer to the DT property data buffer to write.
Elyes HAOUAS1ec76442018-08-07 12:20:04 +02001286 * @param src The integer to write (in CPU endianness).
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001287 * @param length the length of the destination integer in bytes.
1288 */
1289void dt_write_int(u8 *dest, u64 src, size_t length)
1290{
1291 while (length--) {
1292 dest[length] = (u8)src;
1293 src >>= 8;
1294 }
1295}
1296
1297/*
Patrick Rudolph5ccc7312018-05-30 15:05:28 +02001298 * Delete a property by name in a given node if it exists.
1299 *
1300 * @param node The device tree node to operate on.
1301 * @param name The name of the property to delete.
1302 */
1303void dt_delete_prop(struct device_tree_node *node, const char *name)
1304{
1305 struct device_tree_property *prop;
1306
1307 list_for_each(prop, node->properties, list_node) {
1308 if (!strcmp(prop->prop.name, name)) {
1309 list_remove(&prop->list_node);
1310 return;
1311 }
1312 }
1313}
1314
1315/*
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001316 * Add an arbitrary property to a node, or update it if it already exists.
1317 *
1318 * @param node The device tree node to add to.
1319 * @param name The name of the new property.
1320 * @param data The raw data blob to be stored in the property.
1321 * @param size The size of data in bytes.
1322 */
Patrick Rudolph666c1722018-04-03 09:57:33 +02001323void dt_add_bin_prop(struct device_tree_node *node, const char *name,
Julius Werner0e9116f2019-05-13 17:30:31 -07001324 void *data, size_t size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001325{
Patrick Rudolph666c1722018-04-03 09:57:33 +02001326 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001327
1328 list_for_each(prop, node->properties, list_node) {
1329 if (!strcmp(prop->prop.name, name)) {
1330 prop->prop.data = data;
1331 prop->prop.size = size;
1332 return;
1333 }
1334 }
1335
Julius Werner9636a102019-05-03 17:36:43 -07001336 prop = xzalloc(sizeof(*prop));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001337 list_insert_after(&prop->list_node, &node->properties);
1338 prop->prop.name = name;
1339 prop->prop.data = data;
1340 prop->prop.size = size;
1341}
1342
1343/*
1344 * Find given string property in a node and return its content.
1345 *
1346 * @param node The device tree node to search.
1347 * @param name The name of the property.
1348 * @return The found string, or NULL.
1349 */
Patrick Rudolph0a7d6902018-08-22 09:55:15 +02001350const char *dt_find_string_prop(const struct device_tree_node *node,
1351 const char *name)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001352{
Patrick Rudolph0a7d6902018-08-22 09:55:15 +02001353 const void *content;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001354 size_t size;
1355
1356 dt_find_bin_prop(node, name, &content, &size);
1357
1358 return content;
1359}
1360
1361/*
1362 * Find given property in a node.
1363 *
1364 * @param node The device tree node to search.
1365 * @param name The name of the property.
1366 * @param data Pointer to return raw data blob in the property.
1367 * @param size Pointer to return the size of data in bytes.
1368 */
Patrick Rudolph0a7d6902018-08-22 09:55:15 +02001369void dt_find_bin_prop(const struct device_tree_node *node, const char *name,
1370 const void **data, size_t *size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001371{
Patrick Rudolph666c1722018-04-03 09:57:33 +02001372 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001373
1374 *data = NULL;
1375 *size = 0;
1376
1377 list_for_each(prop, node->properties, list_node) {
1378 if (!strcmp(prop->prop.name, name)) {
1379 *data = prop->prop.data;
1380 *size = prop->prop.size;
1381 return;
1382 }
1383 }
1384}
1385
1386/*
1387 * Add a string property to a node, or update it if it already exists.
1388 *
1389 * @param node The device tree node to add to.
1390 * @param name The name of the new property.
1391 * @param str The zero-terminated string to be stored in the property.
1392 */
Patrick Rudolph666c1722018-04-03 09:57:33 +02001393void dt_add_string_prop(struct device_tree_node *node, const char *name,
Patrick Rudolph0a7d6902018-08-22 09:55:15 +02001394 const char *str)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001395{
Julius Werner0e9116f2019-05-13 17:30:31 -07001396 dt_add_bin_prop(node, name, (char *)str, strlen(str) + 1);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001397}
1398
1399/*
1400 * Add a 32-bit integer property to a node, or update it if it already exists.
1401 *
1402 * @param node The device tree node to add to.
1403 * @param name The name of the new property.
1404 * @param val The integer to be stored in the property.
1405 */
Patrick Rudolph666c1722018-04-03 09:57:33 +02001406void dt_add_u32_prop(struct device_tree_node *node, const char *name, u32 val)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001407{
Julius Werner9636a102019-05-03 17:36:43 -07001408 u32 *val_ptr = xmalloc(sizeof(val));
Patrick Rudolph666c1722018-04-03 09:57:33 +02001409 *val_ptr = htobe32(val);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001410 dt_add_bin_prop(node, name, val_ptr, sizeof(*val_ptr));
1411}
1412
1413/*
Patrick Rudolph3fca4ed2018-08-10 10:12:35 +02001414 * Add a 64-bit integer property to a node, or update it if it already exists.
1415 *
1416 * @param node The device tree node to add to.
1417 * @param name The name of the new property.
1418 * @param val The integer to be stored in the property.
1419 */
1420void dt_add_u64_prop(struct device_tree_node *node, const char *name, u64 val)
1421{
Julius Werner9636a102019-05-03 17:36:43 -07001422 u64 *val_ptr = xmalloc(sizeof(val));
Patrick Rudolph3fca4ed2018-08-10 10:12:35 +02001423 *val_ptr = htobe64(val);
1424 dt_add_bin_prop(node, name, val_ptr, sizeof(*val_ptr));
1425}
1426
1427/*
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001428 * Add a 'reg' address list property to a node, or update it if it exists.
1429 *
1430 * @param node The device tree node to add to.
Maximilian Brune33079b82024-03-04 15:34:41 +01001431 * @param regions Array of address values to be stored in the property.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001432 * @param sizes Array of corresponding size values to 'addrs'.
1433 * @param count Number of values in 'addrs' and 'sizes' (must be equal).
1434 * @param addr_cells Value of #address-cells property valid for this node.
1435 * @param size_cells Value of #size-cells property valid for this node.
1436 */
Patrick Rudolph666c1722018-04-03 09:57:33 +02001437void dt_add_reg_prop(struct device_tree_node *node, u64 *addrs, u64 *sizes,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001438 int count, u32 addr_cells, u32 size_cells)
1439{
1440 int i;
1441 size_t length = (addr_cells + size_cells) * sizeof(u32) * count;
Julius Werner9636a102019-05-03 17:36:43 -07001442 u8 *data = xmalloc(length);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001443 u8 *cur = data;
1444
1445 for (i = 0; i < count; i++) {
1446 dt_write_int(cur, addrs[i], addr_cells * sizeof(u32));
1447 cur += addr_cells * sizeof(u32);
1448 dt_write_int(cur, sizes[i], size_cells * sizeof(u32));
1449 cur += size_cells * sizeof(u32);
1450 }
1451
1452 dt_add_bin_prop(node, "reg", data, length);
1453}
1454
1455/*
1456 * Fixups to apply to a kernel's device tree before booting it.
1457 */
1458
Patrick Rudolph666c1722018-04-03 09:57:33 +02001459struct list_node device_tree_fixups;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001460
Patrick Rudolph666c1722018-04-03 09:57:33 +02001461int dt_apply_fixups(struct device_tree *tree)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001462{
Patrick Rudolph666c1722018-04-03 09:57:33 +02001463 struct device_tree_fixup *fixup;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001464 list_for_each(fixup, device_tree_fixups, list_node) {
1465 assert(fixup->fixup);
1466 if (fixup->fixup(fixup, tree))
1467 return 1;
1468 }
1469 return 0;
1470}
1471
Patrick Rudolph666c1722018-04-03 09:57:33 +02001472int dt_set_bin_prop_by_path(struct device_tree *tree, const char *path,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001473 void *data, size_t data_size, int create)
1474{
1475 char *path_copy, *prop_name;
Patrick Rudolph666c1722018-04-03 09:57:33 +02001476 struct device_tree_node *dt_node;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001477
1478 path_copy = strdup(path);
1479
1480 if (!path_copy) {
Patrick Rudolph666c1722018-04-03 09:57:33 +02001481 printk(BIOS_ERR, "Failed to allocate a copy of path %s\n",
1482 path);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001483 return 1;
1484 }
1485
1486 prop_name = strrchr(path_copy, '/');
1487 if (!prop_name) {
Patrick Rudolph679d6242018-07-11 13:53:04 +02001488 free(path_copy);
Patrick Rudolph666c1722018-04-03 09:57:33 +02001489 printk(BIOS_ERR, "Path %s does not include '/'\n", path);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001490 return 1;
1491 }
1492
1493 *prop_name++ = '\0'; /* Separate path from the property name. */
1494
Julius Wernerf36d53c2019-05-03 18:23:34 -07001495 dt_node = dt_find_node_by_path(tree, path_copy, NULL,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001496 NULL, create);
1497
1498 if (!dt_node) {
Patrick Rudolph666c1722018-04-03 09:57:33 +02001499 printk(BIOS_ERR, "Failed to %s %s in the device tree\n",
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001500 create ? "create" : "find", path_copy);
Patrick Rudolph679d6242018-07-11 13:53:04 +02001501 free(path_copy);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001502 return 1;
1503 }
1504
1505 dt_add_bin_prop(dt_node, prop_name, data, data_size);
Patrick Rudolph679d6242018-07-11 13:53:04 +02001506 free(path_copy);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001507
1508 return 0;
1509}
1510
1511/*
1512 * Prepare the /reserved-memory/ node.
1513 *
1514 * Technically, this can be called more than one time, to init and/or retrieve
1515 * the node. But dt_add_u32_prop() may leak a bit of memory if you do.
1516 *
1517 * @tree: Device tree to add/retrieve from.
1518 * @return: The /reserved-memory/ node (or NULL, if error).
1519 */
Patrick Rudolph666c1722018-04-03 09:57:33 +02001520struct device_tree_node *dt_init_reserved_memory_node(struct device_tree *tree)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001521{
Patrick Rudolph666c1722018-04-03 09:57:33 +02001522 struct device_tree_node *reserved;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001523 u32 addr = 0, size = 0;
1524
Julius Wernerfbec63d2019-05-03 18:29:28 -07001525 reserved = dt_find_node_by_path(tree, "/reserved-memory", &addr,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001526 &size, 1);
1527 if (!reserved)
1528 return NULL;
1529
Julius Werner23df4772019-05-17 22:50:18 -07001530 /* Binding doc says this should have the same #{address,size}-cells as
1531 the root. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001532 dt_add_u32_prop(reserved, "#address-cells", addr);
1533 dt_add_u32_prop(reserved, "#size-cells", size);
Julius Werner23df4772019-05-17 22:50:18 -07001534 /* Binding doc says this should be empty (1:1 mapping from root). */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001535 dt_add_bin_prop(reserved, "ranges", NULL, 0);
1536
1537 return reserved;
1538}
Julius Werner735ddc92019-05-07 17:05:28 -07001539
1540/*
1541 * Increment a single phandle in prop at a given offset by a given adjustment.
1542 *
1543 * @param prop Property whose phandle should be adjusted.
1544 * @param adjustment Value that should be added to the existing phandle.
1545 * @param offset Byte offset of the phandle in the property data.
1546 *
1547 * @return New phandle value, or 0 on error.
1548 */
1549static uint32_t dt_adjust_phandle(struct device_tree_property *prop,
1550 uint32_t adjustment, uint32_t offset)
1551{
1552 if (offset + 4 > prop->prop.size)
1553 return 0;
1554
1555 uint32_t phandle = be32dec(prop->prop.data + offset);
1556 if (phandle == 0 ||
1557 phandle == FDT_PHANDLE_ILLEGAL ||
1558 phandle == 0xffffffff)
1559 return 0;
1560
1561 phandle += adjustment;
1562 if (phandle >= FDT_PHANDLE_ILLEGAL)
1563 return 0;
1564
1565 be32enc(prop->prop.data + offset, phandle);
1566 return phandle;
1567}
1568
1569/*
1570 * Adjust all phandles in subtree by adding a new base offset.
1571 *
1572 * @param node Root node of the subtree to work on.
1573 * @param base New phandle base to be added to all phandles.
1574 *
1575 * @return New highest phandle in the subtree, or 0 on error.
1576 */
1577static uint32_t dt_adjust_all_phandles(struct device_tree_node *node,
1578 uint32_t base)
1579{
Julius Werner23df4772019-05-17 22:50:18 -07001580 uint32_t new_max = MAX(base, 1); /* make sure we don't return 0 */
Julius Werner735ddc92019-05-07 17:05:28 -07001581 struct device_tree_property *prop;
1582 struct device_tree_node *child;
1583
1584 if (!node)
1585 return new_max;
1586
1587 list_for_each(prop, node->properties, list_node)
1588 if (dt_prop_is_phandle(prop)) {
1589 node->phandle = dt_adjust_phandle(prop, base, 0);
1590 if (!node->phandle)
1591 return 0;
1592 new_max = MAX(new_max, node->phandle);
Julius Werner23df4772019-05-17 22:50:18 -07001593 } /* no break -- can have more than one phandle prop */
Julius Werner735ddc92019-05-07 17:05:28 -07001594
1595 list_for_each(child, node->children, list_node)
1596 new_max = MAX(new_max, dt_adjust_all_phandles(child, base));
1597
1598 return new_max;
1599}
1600
1601/*
1602 * Apply a /__local_fixup__ subtree to the corresponding overlay subtree.
1603 *
1604 * @param node Root node of the overlay subtree to fix up.
1605 * @param node Root node of the /__local_fixup__ subtree.
1606 * @param base Adjustment that was added to phandles in the overlay.
1607 *
1608 * @return 0 on success, -1 on error.
1609 */
1610static int dt_fixup_locals(struct device_tree_node *node,
1611 struct device_tree_node *fixup, uint32_t base)
1612{
1613 struct device_tree_property *prop;
1614 struct device_tree_property *fixup_prop;
1615 struct device_tree_node *child;
1616 struct device_tree_node *fixup_child;
1617 int i;
1618
Julius Werner23df4772019-05-17 22:50:18 -07001619 /*
1620 * For local fixups the /__local_fixup__ subtree contains the same node
1621 * hierarchy as the main tree we're fixing up. Each property contains
1622 * the fixup offsets for the respective property in the main tree. For
1623 * each property in the fixup node, find the corresponding property in
1624 * the base node and apply fixups to all offsets it specifies.
1625 */
Julius Werner735ddc92019-05-07 17:05:28 -07001626 list_for_each(fixup_prop, fixup->properties, list_node) {
1627 struct device_tree_property *base_prop = NULL;
1628 list_for_each(prop, node->properties, list_node)
1629 if (!strcmp(prop->prop.name, fixup_prop->prop.name)) {
1630 base_prop = prop;
1631 break;
1632 }
1633
Julius Werner23df4772019-05-17 22:50:18 -07001634 /* We should always find a corresponding base prop for a fixup,
1635 and fixup props contain a list of 32-bit fixup offsets. */
Julius Werner735ddc92019-05-07 17:05:28 -07001636 if (!base_prop || fixup_prop->prop.size % sizeof(uint32_t))
1637 return -1;
1638
1639 for (i = 0; i < fixup_prop->prop.size; i += sizeof(uint32_t))
1640 if (!dt_adjust_phandle(base_prop, base, be32dec(
1641 fixup_prop->prop.data + i)))
1642 return -1;
1643 }
1644
Julius Werner23df4772019-05-17 22:50:18 -07001645 /* Now recursively descend both the base tree and the /__local_fixups__
1646 subtree in sync to apply all fixups. */
Julius Werner735ddc92019-05-07 17:05:28 -07001647 list_for_each(fixup_child, fixup->children, list_node) {
1648 struct device_tree_node *base_child = NULL;
1649 list_for_each(child, node->children, list_node)
1650 if (!strcmp(child->name, fixup_child->name)) {
1651 base_child = child;
1652 break;
1653 }
1654
Julius Werner23df4772019-05-17 22:50:18 -07001655 /* All fixup nodes should have a corresponding base node. */
Julius Werner735ddc92019-05-07 17:05:28 -07001656 if (!base_child)
1657 return -1;
1658
1659 if (dt_fixup_locals(base_child, fixup_child, base) < 0)
1660 return -1;
1661 }
1662
1663 return 0;
1664}
1665
1666/*
1667 * Update all /__symbols__ properties in an overlay that start with
1668 * "/fragment@X/__overlay__" with corresponding path prefix in the base tree.
1669 *
1670 * @param symbols /__symbols__ done to update.
1671 * @param fragment /fragment@X node that references to should be updated.
1672 * @param base_path Path of base tree node that the fragment overlaid.
1673 */
1674static void dt_fix_symbols(struct device_tree_node *symbols,
1675 struct device_tree_node *fragment,
1676 const char *base_path)
1677{
1678 struct device_tree_property *prop;
Julius Werner23df4772019-05-17 22:50:18 -07001679 char buf[512]; /* Should be enough for maximum DT path length? */
1680 char node_path[64]; /* easily enough for /fragment@XXXX/__overlay__ */
Julius Werner735ddc92019-05-07 17:05:28 -07001681
Julius Werner23df4772019-05-17 22:50:18 -07001682 if (!symbols) /* If the overlay has no /__symbols__ node, we're done! */
Julius Werner735ddc92019-05-07 17:05:28 -07001683 return;
1684
1685 int len = snprintf(node_path, sizeof(node_path), "/%s/__overlay__",
1686 fragment->name);
1687
1688 list_for_each(prop, symbols->properties, list_node)
1689 if (!strncmp(prop->prop.data, node_path, len)) {
1690 prop->prop.size = snprintf(buf, sizeof(buf), "%s%s",
1691 base_path, (char *)prop->prop.data + len) + 1;
1692 free(prop->prop.data);
1693 prop->prop.data = strdup(buf);
1694 }
1695}
1696
1697/*
1698 * Fix up overlay according to a property in /__fixup__. If the fixed property
1699 * is a /fragment@X:target, also update /__symbols__ references to fragment.
1700 *
1701 * @params overlay Overlay to fix up.
1702 * @params fixup /__fixup__ property.
1703 * @params phandle phandle value to insert where the fixup points to.
1704 * @params base_path Path to the base DT node that the fixup points to.
1705 * @params overlay_symbols /__symbols__ node of the overlay.
1706 *
1707 * @return 0 on success, -1 on error.
1708 */
1709static int dt_fixup_external(struct device_tree *overlay,
1710 struct device_tree_property *fixup,
1711 uint32_t phandle, const char *base_path,
1712 struct device_tree_node *overlay_symbols)
1713{
1714 struct device_tree_property *prop;
1715
Julius Werner23df4772019-05-17 22:50:18 -07001716 /* External fixup properties are encoded as "<path>:<prop>:<offset>". */
Julius Werner735ddc92019-05-07 17:05:28 -07001717 char *entry = fixup->prop.data;
1718 while ((void *)entry < fixup->prop.data + fixup->prop.size) {
Julius Werner23df4772019-05-17 22:50:18 -07001719 /* okay to destroy fixup property value, won't need it again */
Julius Werner735ddc92019-05-07 17:05:28 -07001720 char *node_path = entry;
1721 entry = strchr(node_path, ':');
1722 if (!entry)
1723 return -1;
1724 *entry++ = '\0';
1725
1726 char *prop_name = entry;
1727 entry = strchr(prop_name, ':');
1728 if (!entry)
1729 return -1;
1730 *entry++ = '\0';
1731
1732 struct device_tree_node *ovl_node = dt_find_node_by_path(
1733 overlay, node_path, NULL, NULL, 0);
1734 if (!ovl_node || !isdigit(*entry))
1735 return -1;
1736
1737 struct device_tree_property *ovl_prop = NULL;
1738 list_for_each(prop, ovl_node->properties, list_node)
1739 if (!strcmp(prop->prop.name, prop_name)) {
1740 ovl_prop = prop;
1741 break;
1742 }
1743
Julius Werner23df4772019-05-17 22:50:18 -07001744 /* Move entry to first char after number, must be a '\0'. */
Julius Werner735ddc92019-05-07 17:05:28 -07001745 uint32_t offset = skip_atoi(&entry);
1746 if (!ovl_prop || offset + 4 > ovl_prop->prop.size || entry[0])
1747 return -1;
Julius Werner23df4772019-05-17 22:50:18 -07001748 entry++; /* jump over '\0' to potential next fixup */
Julius Werner735ddc92019-05-07 17:05:28 -07001749
1750 be32enc(ovl_prop->prop.data + offset, phandle);
1751
Julius Werner23df4772019-05-17 22:50:18 -07001752 /* If this is a /fragment@X:target property, update references
1753 to this fragment in the overlay __symbols__ now. */
Julius Werner735ddc92019-05-07 17:05:28 -07001754 if (offset == 0 && !strcmp(prop_name, "target") &&
Julius Werner23df4772019-05-17 22:50:18 -07001755 !strchr(node_path + 1, '/')) /* only toplevel nodes */
Julius Werner735ddc92019-05-07 17:05:28 -07001756 dt_fix_symbols(overlay_symbols, ovl_node, base_path);
1757 }
1758
1759 return 0;
1760}
1761
1762/*
1763 * Apply all /__fixup__ properties in the overlay. This will destroy the
1764 * property data in /__fixup__ and it should not be accessed again.
1765 *
1766 * @params tree Base device tree that the overlay updates.
1767 * @params symbols /__symbols__ node of the base device tree.
1768 * @params overlay Overlay to fix up.
1769 * @params fixups /__fixup__ node in the overlay.
1770 * @params overlay_symbols /__symbols__ node of the overlay.
1771 *
1772 * @return 0 on success, -1 on error.
1773 */
1774static int dt_fixup_all_externals(struct device_tree *tree,
1775 struct device_tree_node *symbols,
1776 struct device_tree *overlay,
1777 struct device_tree_node *fixups,
1778 struct device_tree_node *overlay_symbols)
1779{
1780 struct device_tree_property *fix;
1781
Julius Werner23df4772019-05-17 22:50:18 -07001782 /* If we have any external fixups, base tree must have /__symbols__. */
Julius Werner735ddc92019-05-07 17:05:28 -07001783 if (!symbols)
1784 return -1;
1785
Julius Werner23df4772019-05-17 22:50:18 -07001786 /*
1787 * Unlike /__local_fixups__, /__fixups__ is not a whole subtree that
1788 * mirrors the node hierarchy. It's just a directory of fixup properties
1789 * that each directly contain all information necessary to apply them.
1790 */
Julius Werner735ddc92019-05-07 17:05:28 -07001791 list_for_each(fix, fixups->properties, list_node) {
Julius Werner23df4772019-05-17 22:50:18 -07001792 /* The name of a fixup property is the label of the node we want
1793 a property to phandle-reference. Look up in /__symbols__. */
Julius Werner735ddc92019-05-07 17:05:28 -07001794 const char *path = dt_find_string_prop(symbols, fix->prop.name);
1795 if (!path)
1796 return -1;
1797
Elyes HAOUAS0afaff22021-01-16 15:02:31 +01001798 /* Find node the label pointed to figure out its phandle. */
Julius Werner735ddc92019-05-07 17:05:28 -07001799 struct device_tree_node *node = dt_find_node_by_path(tree, path,
1800 NULL, NULL, 0);
1801 if (!node)
1802 return -1;
1803
Julius Werner23df4772019-05-17 22:50:18 -07001804 /* Write into the overlay property(s) pointing to that node. */
Julius Werner735ddc92019-05-07 17:05:28 -07001805 if (dt_fixup_external(overlay, fix, node->phandle,
1806 path, overlay_symbols) < 0)
1807 return -1;
1808 }
1809
1810 return 0;
1811}
1812
1813/*
1814 * Copy all nodes and properties from one DT subtree into another. This is a
1815 * shallow copy so both trees will point to the same property data afterwards.
1816 *
1817 * @params dst Destination subtree to copy into.
1818 * @params src Source subtree to copy from.
1819 * @params upd 1 to overwrite same-name properties, 0 to discard them.
1820 */
1821static void dt_copy_subtree(struct device_tree_node *dst,
1822 struct device_tree_node *src, int upd)
1823{
1824 struct device_tree_property *prop;
1825 struct device_tree_property *src_prop;
1826 list_for_each(src_prop, src->properties, list_node) {
1827 if (dt_prop_is_phandle(src_prop) ||
1828 !strcmp(src_prop->prop.name, "name")) {
1829 printk(BIOS_DEBUG,
1830 "WARNING: ignoring illegal overlay prop '%s'\n",
1831 src_prop->prop.name);
1832 continue;
1833 }
1834
1835 struct device_tree_property *dst_prop = NULL;
1836 list_for_each(prop, dst->properties, list_node)
1837 if (!strcmp(prop->prop.name, src_prop->prop.name)) {
1838 dst_prop = prop;
1839 break;
1840 }
1841
1842 if (dst_prop) {
1843 if (!upd) {
1844 printk(BIOS_DEBUG,
1845 "WARNING: ignoring prop update '%s'\n",
1846 src_prop->prop.name);
1847 continue;
1848 }
1849 } else {
1850 dst_prop = xzalloc(sizeof(*dst_prop));
1851 list_insert_after(&dst_prop->list_node,
1852 &dst->properties);
1853 }
1854
1855 dst_prop->prop = src_prop->prop;
1856 }
1857
1858 struct device_tree_node *node;
1859 struct device_tree_node *src_node;
1860 list_for_each(src_node, src->children, list_node) {
1861 struct device_tree_node *dst_node = NULL;
1862 list_for_each(node, dst->children, list_node)
1863 if (!strcmp(node->name, src_node->name)) {
1864 dst_node = node;
1865 break;
1866 }
1867
1868 if (!dst_node) {
1869 dst_node = xzalloc(sizeof(*dst_node));
1870 *dst_node = *src_node;
1871 list_insert_after(&dst_node->list_node, &dst->children);
1872 } else {
1873 dt_copy_subtree(dst_node, src_node, upd);
1874 }
1875 }
1876}
1877
1878/*
1879 * Apply an overlay /fragment@X node to a base device tree.
1880 *
1881 * @param tree Base device tree.
1882 * @param fragment /fragment@X node.
1883 * @params overlay_symbols /__symbols__ node of the overlay.
1884 *
1885 * @return 0 on success, -1 on error.
1886 */
1887static int dt_import_fragment(struct device_tree *tree,
1888 struct device_tree_node *fragment,
1889 struct device_tree_node *overlay_symbols)
1890{
Julius Werner23df4772019-05-17 22:50:18 -07001891 /* The actual overlaid nodes/props are in an __overlay__ child node. */
Julius Werner735ddc92019-05-07 17:05:28 -07001892 static const char *overlay_path[] = { "__overlay__", NULL };
1893 struct device_tree_node *overlay = dt_find_node(fragment, overlay_path,
1894 NULL, NULL, 0);
1895
Julius Werner23df4772019-05-17 22:50:18 -07001896 /* If it doesn't have an __overlay__ child, it's not a fragment. */
Julius Werner735ddc92019-05-07 17:05:28 -07001897 if (!overlay)
1898 return 0;
1899
Julius Werner23df4772019-05-17 22:50:18 -07001900 /* Target node of the fragment can be given by path or by phandle. */
Julius Werner735ddc92019-05-07 17:05:28 -07001901 struct device_tree_property *prop;
1902 struct device_tree_property *phandle = NULL;
1903 struct device_tree_property *path = NULL;
1904 list_for_each(prop, fragment->properties, list_node) {
1905 if (!strcmp(prop->prop.name, "target")) {
1906 phandle = prop;
Julius Werner23df4772019-05-17 22:50:18 -07001907 break; /* phandle target has priority, stop looking */
Julius Werner735ddc92019-05-07 17:05:28 -07001908 }
1909 if (!strcmp(prop->prop.name, "target-path"))
1910 path = prop;
1911 }
1912
1913 struct device_tree_node *target = NULL;
1914 if (phandle) {
1915 if (phandle->prop.size != sizeof(uint32_t))
1916 return -1;
1917 target = dt_find_node_by_phandle(tree->root,
1918 be32dec(phandle->prop.data));
Julius Werner23df4772019-05-17 22:50:18 -07001919 /* Symbols already updated as part of dt_fixup_external(). */
Julius Werner735ddc92019-05-07 17:05:28 -07001920 } else if (path) {
1921 target = dt_find_node_by_path(tree, path->prop.data,
1922 NULL, NULL, 0);
1923 dt_fix_symbols(overlay_symbols, fragment, path->prop.data);
1924 }
1925 if (!target)
1926 return -1;
1927
1928 dt_copy_subtree(target, overlay, 1);
1929 return 0;
1930}
1931
1932/*
1933 * Apply a device tree overlay to a base device tree. This will
1934 * destroy/incorporate the overlay data, so it should not be freed or reused.
1935 * See dtc.git/Documentation/dt-object-internal.txt for overlay format details.
1936 *
1937 * @param tree Unflattened base device tree to add the overlay into.
1938 * @param overlay Unflattened overlay device tree to apply to the base.
1939 *
1940 * @return 0 on success, -1 on error.
1941 */
1942int dt_apply_overlay(struct device_tree *tree, struct device_tree *overlay)
1943{
Julius Werner23df4772019-05-17 22:50:18 -07001944 /*
1945 * First, we need to make sure phandles inside the overlay don't clash
1946 * with those in the base tree. We just define the highest phandle value
1947 * in the base tree as the "phandle offset" for this overlay and
1948 * increment all phandles in it by that value.
1949 */
Julius Werner735ddc92019-05-07 17:05:28 -07001950 uint32_t phandle_base = tree->max_phandle;
1951 uint32_t new_max = dt_adjust_all_phandles(overlay->root, phandle_base);
1952 if (!new_max) {
Elyes HAOUAS8b5841e2022-02-08 21:23:12 +01001953 printk(BIOS_ERR, "invalid phandles in overlay\n");
Julius Werner735ddc92019-05-07 17:05:28 -07001954 return -1;
1955 }
1956 tree->max_phandle = new_max;
1957
Julius Werner23df4772019-05-17 22:50:18 -07001958 /* Now that we changed phandles in the overlay, we need to update any
1959 nodes referring to them. Those are listed in /__local_fixups__. */
Julius Werner735ddc92019-05-07 17:05:28 -07001960 struct device_tree_node *local_fixups = dt_find_node_by_path(overlay,
1961 "/__local_fixups__", NULL, NULL, 0);
1962 if (local_fixups && dt_fixup_locals(overlay->root, local_fixups,
1963 phandle_base) < 0) {
Elyes HAOUAS8b5841e2022-02-08 21:23:12 +01001964 printk(BIOS_ERR, "invalid local fixups in overlay\n");
Julius Werner735ddc92019-05-07 17:05:28 -07001965 return -1;
1966 }
1967
Julius Werner23df4772019-05-17 22:50:18 -07001968 /*
1969 * Besides local phandle references (from nodes within the overlay to
1970 * other nodes within the overlay), the overlay may also contain phandle
1971 * references to the base tree. These are stored with invalid values and
1972 * must be updated now. /__symbols__ contains a list of all labels in
1973 * the base tree, and /__fixups__ describes all nodes in the overlay
1974 * that contain external phandle references.
1975 * We also take this opportunity to update all /fragment@X/__overlay__/
1976 * prefixes in the overlay's /__symbols__ node to the correct path that
1977 * the fragment will be placed in later, since this is the only step
1978 * where we have all necessary information for that easily available.
1979 */
Julius Werner735ddc92019-05-07 17:05:28 -07001980 struct device_tree_node *symbols = dt_find_node_by_path(tree,
1981 "/__symbols__", NULL, NULL, 0);
1982 struct device_tree_node *fixups = dt_find_node_by_path(overlay,
1983 "/__fixups__", NULL, NULL, 0);
1984 struct device_tree_node *overlay_symbols = dt_find_node_by_path(overlay,
1985 "/__symbols__", NULL, NULL, 0);
1986 if (fixups && dt_fixup_all_externals(tree, symbols, overlay,
1987 fixups, overlay_symbols) < 0) {
Elyes HAOUAS8b5841e2022-02-08 21:23:12 +01001988 printk(BIOS_ERR, "cannot match external fixups from overlay\n");
Julius Werner735ddc92019-05-07 17:05:28 -07001989 return -1;
1990 }
1991
Julius Werner23df4772019-05-17 22:50:18 -07001992 /* After all this fixing up, we can finally merge overlay into the tree
1993 (one fragment at a time, because for some reason it's split up). */
Julius Werner735ddc92019-05-07 17:05:28 -07001994 struct device_tree_node *fragment;
1995 list_for_each(fragment, overlay->root->children, list_node)
1996 if (dt_import_fragment(tree, fragment, overlay_symbols) < 0) {
Elyes HAOUAS8b5841e2022-02-08 21:23:12 +01001997 printk(BIOS_ERR, "bad DT fragment '%s'\n",
Julius Werner735ddc92019-05-07 17:05:28 -07001998 fragment->name);
1999 return -1;
2000 }
2001
Julius Werner23df4772019-05-17 22:50:18 -07002002 /*
2003 * We need to also update /__symbols__ to include labels from this
2004 * overlay, in case we want to load further overlays with external
2005 * phandle references to it. If the base tree already has a /__symbols__
2006 * we merge them together, otherwise we just insert the overlay's
2007 * /__symbols__ node into the base tree root.
2008 */
Julius Werner735ddc92019-05-07 17:05:28 -07002009 if (overlay_symbols) {
2010 if (symbols)
2011 dt_copy_subtree(symbols, overlay_symbols, 0);
2012 else
2013 list_insert_after(&overlay_symbols->list_node,
2014 &tree->root->children);
2015 }
2016
2017 return 0;
2018}