blob: 5087d3940df2207309c11d2283d0508041e6057d [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>
Patrick Rudolph666c1722018-04-03 09:57:33 +02005#include <console/console.h>
Joel Kitching393c71c2019-06-16 16:09:42 +08006#include <ctype.h>
Patrick Rudolph666c1722018-04-03 09:57:33 +02007#include <device_tree.h>
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02008#include <endian.h>
Maximilian Brune33079b82024-03-04 15:34:41 +01009#include <stdbool.h>
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020010#include <stdint.h>
Elyes Haouasbdd03c22024-05-27 11:20:07 +020011#include <stdio.h>
Patrick Rudolph666c1722018-04-03 09:57:33 +020012#include <string.h>
13#include <stddef.h>
14#include <stdlib.h>
Alper Nebi Yasak377157c2024-02-05 17:31:20 +030015#include <limits.h>
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020016
Maximilian Brune33079b82024-03-04 15:34:41 +010017#define FDT_PATH_MAX_DEPTH 10 // should be a good enough upper bound
18#define FDT_PATH_MAX_LEN 128 // should be a good enough upper bound
Alper Nebi Yasak377157c2024-02-05 17:31:20 +030019#define FDT_MAX_MEMORY_NODES 4 // should be a good enough upper bound
20#define FDT_MAX_MEMORY_REGIONS 16 // should be a good enough upper bound
Maximilian Brune33079b82024-03-04 15:34:41 +010021
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020022/*
23 * Functions for picking apart flattened trees.
24 */
25
Maximilian Brune33079b82024-03-04 15:34:41 +010026static int fdt_skip_nops(const void *blob, uint32_t offset)
27{
28 uint32_t *ptr = (uint32_t *)(((uint8_t *)blob) + offset);
29
30 int index = 0;
31 while (be32toh(ptr[index]) == FDT_TOKEN_NOP)
32 index++;
33
34 return index * sizeof(uint32_t);
35}
36
Patrick Rudolph0a7d6902018-08-22 09:55:15 +020037int fdt_next_property(const void *blob, uint32_t offset,
38 struct fdt_property *prop)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020039{
Patrick Rudolph666c1722018-04-03 09:57:33 +020040 struct fdt_header *header = (struct fdt_header *)blob;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020041 uint32_t *ptr = (uint32_t *)(((uint8_t *)blob) + offset);
42
Maximilian Brune33079b82024-03-04 15:34:41 +010043 // skip NOP tokens
44 offset += fdt_skip_nops(blob, offset);
45
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020046 int index = 0;
Patrick Rudolph666c1722018-04-03 09:57:33 +020047 if (be32toh(ptr[index++]) != FDT_TOKEN_PROPERTY)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020048 return 0;
49
Patrick Rudolph666c1722018-04-03 09:57:33 +020050 uint32_t size = be32toh(ptr[index++]);
51 uint32_t name_offset = be32toh(ptr[index++]);
52 name_offset += be32toh(header->strings_offset);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020053
54 if (prop) {
55 prop->name = (char *)((uint8_t *)blob + name_offset);
56 prop->data = &ptr[index];
57 prop->size = size;
58 }
59
Patrick Rudolph666c1722018-04-03 09:57:33 +020060 index += DIV_ROUND_UP(size, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020061
Patrick Rudolph666c1722018-04-03 09:57:33 +020062 return index * sizeof(uint32_t);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020063}
64
Maximilian Brune33079b82024-03-04 15:34:41 +010065/*
66 * fdt_next_node_name reads a node name
67 *
68 * @params blob address of FDT
69 * @params offset offset to the node to read the name from
70 * @params name parameter to hold the name that has been read or NULL
71 *
72 * @returns Either 0 on error or offset to the properties that come after the node name
73 */
74int fdt_next_node_name(const void *blob, uint32_t offset, const char **name)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020075{
Maximilian Brune33079b82024-03-04 15:34:41 +010076 // skip NOP tokens
77 offset += fdt_skip_nops(blob, offset);
78
79 char *ptr = ((char *)blob) + offset;
Julius Wernera5ea3a22019-05-07 17:38:12 -070080 if (be32dec(ptr) != FDT_TOKEN_BEGIN_NODE)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020081 return 0;
82
83 ptr += 4;
84 if (name)
Maximilian Brune33079b82024-03-04 15:34:41 +010085 *name = ptr;
86
87 return ALIGN_UP(strlen(ptr) + 1, 4) + 4;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +020088}
89
Maximilian Brune33079b82024-03-04 15:34:41 +010090/*
91 * A utility function to skip past nodes in flattened trees.
92 */
93int fdt_skip_node(const void *blob, uint32_t start_offset)
Julius Werner6702b682019-05-03 18:13:53 -070094{
Maximilian Brune33079b82024-03-04 15:34:41 +010095 uint32_t offset = start_offset;
96
97 const char *name;
98 int size = fdt_next_node_name(blob, offset, &name);
99 if (!size)
100 return 0;
101 offset += size;
102
103 while ((size = fdt_next_property(blob, offset, NULL)))
104 offset += size;
105
106 while ((size = fdt_skip_node(blob, offset)))
107 offset += size;
108
109 // skip NOP tokens
110 offset += fdt_skip_nops(blob, offset);
111
112 return offset - start_offset + sizeof(uint32_t);
Julius Werner6702b682019-05-03 18:13:53 -0700113}
114
Maximilian Brune33079b82024-03-04 15:34:41 +0100115/*
116 * fdt_read_prop reads a property inside a node
117 *
118 * @params blob address of FDT
119 * @params node_offset offset to the node to read the property from
120 * @params prop_name name of the property to read
121 * @params fdt_prop property is saved inside this parameter
122 *
123 * @returns Either 0 if no property has been found or an offset that points to the location
124 * of the property
125 */
126u32 fdt_read_prop(const void *blob, u32 node_offset, const char *prop_name,
127 struct fdt_property *fdt_prop)
128{
129 u32 offset = node_offset;
130
131 offset += fdt_next_node_name(blob, offset, NULL); // skip node name
132
133 size_t size;
134 while ((size = fdt_next_property(blob, offset, fdt_prop))) {
135 if (strcmp(fdt_prop->name, prop_name) == 0)
136 return offset;
137 offset += size;
138 }
139 return 0; // property not found
140}
141
142/*
143 * fdt_read_reg_prop reads the reg property inside a node
144 *
145 * @params blob address of FDT
146 * @params node_offset offset to the node to read the reg property from
147 * @params addr_cells number of cells used for one address
148 * @params size_cells number of cells used for one size
149 * @params regions all regions that are read inside the reg property are saved inside
150 * this array
151 * @params regions_count maximum number of entries that can be saved inside the regions array.
152 *
153 * Returns: Either 0 on error or returns the number of regions put into the regions array.
154 */
155u32 fdt_read_reg_prop(const void *blob, u32 node_offset, u32 addr_cells, u32 size_cells,
156 struct device_tree_region regions[], size_t regions_count)
157{
158 struct fdt_property prop;
159 u32 offset = fdt_read_prop(blob, node_offset, "reg", &prop);
160
161 if (!offset) {
162 printk(BIOS_DEBUG, "no reg property found in node_offset: %x\n", node_offset);
163 return 0;
164 }
165
166 // we found the reg property, now need to parse all regions in 'reg'
167 size_t count = prop.size / (4 * addr_cells + 4 * size_cells);
168 if (count > regions_count) {
169 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);
170 count = regions_count;
171 }
172 if (addr_cells > 2 || size_cells > 2) {
173 printk(BIOS_ERR, "addr_cells (%d) or size_cells (%d) bigger than 2\n",
174 addr_cells, size_cells);
175 return 0;
176 }
177 uint32_t *ptr = prop.data;
178 for (int i = 0; i < count; i++) {
179 if (addr_cells == 1)
180 regions[i].addr = be32dec(ptr);
181 else if (addr_cells == 2)
182 regions[i].addr = be64dec(ptr);
183 ptr += addr_cells;
184 if (size_cells == 1)
185 regions[i].size = be32dec(ptr);
186 else if (size_cells == 2)
187 regions[i].size = be64dec(ptr);
188 ptr += size_cells;
189 }
190
191 return count; // return the number of regions found in the reg property
192}
193
194static u32 fdt_read_cell_props(const void *blob, u32 node_offset, u32 *addrcp, u32 *sizecp)
195{
196 struct fdt_property prop;
197 u32 offset = node_offset;
198 size_t size;
199 while ((size = fdt_next_property(blob, offset, &prop))) {
200 if (addrcp && !strcmp(prop.name, "#address-cells"))
201 *addrcp = be32dec(prop.data);
202 if (sizecp && !strcmp(prop.name, "#size-cells"))
203 *sizecp = be32dec(prop.data);
204 offset += size;
205 }
206 return offset;
207}
208
209/*
210 * fdt_find_node searches for a node relative to another node
211 *
212 * @params blob address of FDT
213 *
214 * @params parent_node_offset offset to node from which to traverse the tree
215 *
216 * @params path null terminated array of node names specifying a
217 * relative path (e.g: { "cpus", "cpu0", NULL })
218 *
219 * @params addrcp/sizecp If any address-cells and size-cells properties are found that are
220 * part of the parent node of the node we are looking, addrcp and sizecp
221 * are set to these respectively.
222 *
223 * @returns: Either 0 if no node has been found or the offset to the node found
224 */
225static u32 fdt_find_node(const void *blob, u32 parent_node_offset, char **path,
226 u32 *addrcp, u32 *sizecp)
227{
228 if (*path == NULL)
229 return parent_node_offset; // node found
230
231 size_t size = fdt_next_node_name(blob, parent_node_offset, NULL); // skip node name
232
233 /*
234 * get address-cells and size-cells properties while skipping the others.
235 * According to spec address-cells and size-cells are not inherited, but we
236 * intentionally follow the Linux implementation here and treat them as inheritable.
237 */
238 u32 node_offset = fdt_read_cell_props(blob, parent_node_offset + size, addrcp, sizecp);
239
240 const char *node_name;
241 // walk all children nodes
242 while ((size = fdt_next_node_name(blob, node_offset, &node_name))) {
243 if (!strcmp(*path, node_name)) {
244 // traverse one level deeper into the path
245 return fdt_find_node(blob, node_offset, path + 1, addrcp, sizecp);
246 }
247 // node is not the correct one. skip current node
248 node_offset += fdt_skip_node(blob, node_offset);
249 }
250
251 // we have searched everything and could not find a fitting node
252 return 0;
253}
254
255/*
256 * fdt_find_node_by_path finds a node behind a given node path
257 *
258 * @params blob address of FDT
259 * @params path absolute path to the node that should be searched for
260 *
261 * @params addrcp/sizecp Pointer that will be updated with any #address-cells and #size-cells
262 * value found in the node of the node specified by node_offset. Either
263 * may be NULL to ignore. If no #address-cells and #size-cells is found
264 * default values of #address-cells=2 and #size-cells=1 are returned.
265 *
266 * @returns Either 0 on error or the offset to the node found behind the path
267 */
268u32 fdt_find_node_by_path(const void *blob, const char *path, u32 *addrcp, u32 *sizecp)
269{
270 // sanity check
271 if (path[0] != '/') {
272 printk(BIOS_ERR, "devicetree path must start with a /\n");
273 return 0;
274 }
275 if (!blob) {
276 printk(BIOS_ERR, "devicetree blob is NULL\n");
277 return 0;
278 }
279
280 if (addrcp)
281 *addrcp = 2;
282 if (sizecp)
283 *sizecp = 1;
284
285 struct fdt_header *fdt_hdr = (struct fdt_header *)blob;
286
287 /*
288 * split path into separate nodes
289 * e.g: "/cpus/cpu0" -> { "cpus", "cpu0" }
290 */
291 char *path_array[FDT_PATH_MAX_DEPTH];
292 size_t path_size = strlen(path);
293 assert(path_size < FDT_PATH_MAX_LEN);
294 char path_copy[FDT_PATH_MAX_LEN];
295 memcpy(path_copy, path, path_size + 1);
296 char *cur = path_copy;
297 int i;
298 for (i = 0; i < FDT_PATH_MAX_DEPTH; i++) {
299 path_array[i] = strtok_r(NULL, "/", &cur);
300 if (!path_array[i])
301 break;
302 }
303 assert(i < FDT_PATH_MAX_DEPTH);
304
305 return fdt_find_node(blob, be32toh(fdt_hdr->structure_offset), path_array, addrcp, sizecp);
306}
307
308/*
309 * fdt_find_subnodes_by_prefix finds a node with a given prefix relative to a parent node
310 *
311 * @params blob The FDT to search.
312 *
313 * @params node_offset offset to the node of which the children should be searched
314 *
315 * @params prefix A string to search for a node with a given prefix. This can for example
316 * be 'cpu' to look for all nodes matching this prefix. Only children of
317 * node_offset are searched. Therefore in order to search all nodes matching
318 * the 'cpu' prefix, node_offset should probably point to the 'cpus' node.
319 * An empty prefix ("") searches for all children nodes of node_offset.
320 *
321 * @params addrcp/sizecp Pointer that will be updated with any #address-cells and #size-cells
322 * value found in the node of the node specified by node_offset. Either
323 * may be NULL to ignore. If no #address-cells and #size-cells is found
324 * addrcp and sizecp are left untouched.
325 *
326 * @params results Array of offsets pointing to each node matching the given prefix.
327 * @params results_len Number of entries allocated for the 'results' array
328 *
329 * @returns offset to last node found behind path or 0 if no node has been found
330 */
331size_t fdt_find_subnodes_by_prefix(const void *blob, u32 node_offset, const char *prefix,
332 u32 *addrcp, u32 *sizecp, u32 *results, size_t results_len)
333{
334 // sanity checks
335 if (!blob || !results || !prefix) {
336 printk(BIOS_ERR, "%s: input parameter cannot be null/\n", __func__);
337 return 0;
338 }
339
340 u32 offset = node_offset;
341
342 // we don't care about the name of the current node
343 u32 size = fdt_next_node_name(blob, offset, NULL);
344 if (!size) {
345 printk(BIOS_ERR, "%s: node_offset: %x does not point to a node\n",
346 __func__, node_offset);
347 return 0;
348 }
349 offset += size;
350
351 /*
352 * update addrcp and sizecp if the node contains an address-cells and size-cells
353 * property. Otherwise use addrcp and sizecp provided by caller.
354 */
355 offset = fdt_read_cell_props(blob, offset, addrcp, sizecp);
356
357 size_t count_results = 0;
358 int prefix_len = strlen(prefix);
359 const char *node_name;
360 // walk all children nodes of offset
361 while ((size = fdt_next_node_name(blob, offset, &node_name))) {
362
363 if (count_results >= results_len) {
364 printk(BIOS_WARNING,
365 "%s: results_len (%zd) smaller than count_results (%zd)\n",
366 __func__, results_len, count_results);
367 break;
368 }
369
370 if (!strncmp(prefix, node_name, prefix_len)) {
371 // we found a node that matches the prefix
372 results[count_results++] = offset;
373 }
374
375 // node does not match the prefix. skip current node
376 offset += fdt_skip_node(blob, offset);
377 }
378
379 // return last occurrence
380 return count_results;
381}
382
383static const char *fdt_read_alias_prop(const void *blob, const char *alias_name)
384{
385 u32 node_offset = fdt_find_node_by_path(blob, "/aliases", NULL, NULL);
386 if (!node_offset) {
387 printk(BIOS_DEBUG, "no /aliases node found\n");
388 return NULL;
389 }
390 struct fdt_property alias_prop;
391 if (!fdt_read_prop(blob, node_offset, alias_name, &alias_prop)) {
392 printk(BIOS_DEBUG, "property %s in /aliases node not found\n", alias_name);
393 return NULL;
394 }
395 return (const char *)alias_prop.data;
396}
397
398/*
399 * Find a node in the tree from a string device tree path.
400 *
401 * @params blob Address to the FDT
402 * @params alias_name node name alias that should be searched for.
403 * @params addrcp/sizecp Pointer that will be updated with any #address-cells and #size-cells
404 * value found in the node of the node specified by node_offset. Either
405 * may be NULL to ignore. If no #address-cells and #size-cells is found
406 * default values of #address-cells=2 and #size-cells=1 are returned.
407 *
408 * @returns offset to last node found behind path or 0 if no node has been found
409 */
410u32 fdt_find_node_by_alias(const void *blob, const char *alias_name, u32 *addrcp, u32 *sizecp)
411{
412 const char *node_name = fdt_read_alias_prop(blob, alias_name);
413 if (!node_name) {
414 printk(BIOS_DEBUG, "alias %s not found\n", alias_name);
415 return 0;
416 }
417
418 u32 node_offset = fdt_find_node_by_path(blob, node_name, addrcp, sizecp);
419 if (!node_offset) {
420 // This should not happen (invalid devicetree)
421 printk(BIOS_WARNING,
422 "Could not find node '%s', which alias was referring to '%s'\n",
423 node_name, alias_name);
424 return 0;
425 }
426 return node_offset;
427}
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200428
429
430/*
431 * Functions for printing flattened trees.
432 */
433
434static void print_indent(int depth)
435{
Julius Werner0d746532019-05-06 19:35:56 -0700436 printk(BIOS_DEBUG, "%*s", depth * 8, "");
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200437}
438
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200439static void print_property(const struct fdt_property *prop, int depth)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200440{
Julius Werner0d746532019-05-06 19:35:56 -0700441 int is_string = prop->size > 0 &&
442 ((char *)prop->data)[prop->size - 1] == '\0';
443
Maximilian Brune77eaec62023-09-20 05:12:04 +0200444 if (is_string) {
445 for (int i = 0; i < prop->size - 1; i++) {
446 if (!isprint(((char *)prop->data)[i])) {
Julius Werner0d746532019-05-06 19:35:56 -0700447 is_string = 0;
Maximilian Brune77eaec62023-09-20 05:12:04 +0200448 break;
449 }
450 }
451 }
Julius Werner0d746532019-05-06 19:35:56 -0700452
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200453 print_indent(depth);
Julius Werner0d746532019-05-06 19:35:56 -0700454 if (is_string) {
455 printk(BIOS_DEBUG, "%s = \"%s\";\n",
456 prop->name, (const char *)prop->data);
457 } else {
458 printk(BIOS_DEBUG, "%s = < ", prop->name);
459 for (int i = 0; i < MIN(128, prop->size); i += 4) {
460 uint32_t val = 0;
461 for (int j = 0; j < MIN(4, prop->size - i); j++)
462 val |= ((uint8_t *)prop->data)[i + j] <<
463 (24 - j * 8);
464 printk(BIOS_DEBUG, "%#.2x ", val);
465 }
466 if (prop->size > 128)
467 printk(BIOS_DEBUG, "...");
468 printk(BIOS_DEBUG, ">;\n");
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200469 }
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200470}
471
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200472static int print_flat_node(const void *blob, uint32_t start_offset, int depth)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200473{
474 int offset = start_offset;
475 const char *name;
476 int size;
477
Maximilian Brune33079b82024-03-04 15:34:41 +0100478 size = fdt_next_node_name(blob, offset, &name);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200479 if (!size)
480 return 0;
481 offset += size;
482
483 print_indent(depth);
Julius Werner0d746532019-05-06 19:35:56 -0700484 printk(BIOS_DEBUG, "%s {\n", name);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200485
Patrick Rudolph666c1722018-04-03 09:57:33 +0200486 struct fdt_property prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200487 while ((size = fdt_next_property(blob, offset, &prop))) {
488 print_property(&prop, depth + 1);
489
490 offset += size;
491 }
492
Julius Werner23df4772019-05-17 22:50:18 -0700493 printk(BIOS_DEBUG, "\n"); /* empty line between props and nodes */
Julius Werner0d746532019-05-06 19:35:56 -0700494
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200495 while ((size = print_flat_node(blob, offset, depth + 1)))
496 offset += size;
497
Julius Werner0d746532019-05-06 19:35:56 -0700498 print_indent(depth);
499 printk(BIOS_DEBUG, "}\n");
500
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200501 return offset - start_offset + sizeof(uint32_t);
502}
503
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200504void fdt_print_node(const void *blob, uint32_t offset)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200505{
506 print_flat_node(blob, offset, 0);
507}
508
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200509/*
Alper Nebi Yasak377157c2024-02-05 17:31:20 +0300510 * fdt_read_memory_regions finds memory ranges from a flat device-tree
511 *
512 * @params blob address of FDT
513 * @params regions all regions that are read inside the reg property of
514 * memory nodes are saved inside this array
515 * @params regions_count maximum number of entries that can be saved inside
516 * the regions array.
517 *
518 * Returns: Either 0 on error or returns the number of regions put into the regions array.
519 */
520size_t fdt_read_memory_regions(const void *blob,
521 struct device_tree_region regions[],
522 size_t regions_count)
523{
524 u32 node, root, addrcp, sizecp;
525 u32 nodes[FDT_MAX_MEMORY_NODES] = {0};
526 size_t region_idx = 0;
527 size_t node_count = 0;
528
529 if (!fdt_is_valid(blob))
530 return 0;
531
532 node = fdt_find_node_by_path(blob, "/memory", &addrcp, &sizecp);
533 if (node) {
534 region_idx += fdt_read_reg_prop(blob, node, addrcp, sizecp,
535 regions, regions_count);
536 if (region_idx >= regions_count) {
537 printk(BIOS_WARNING, "FDT: Too many memory regions\n");
538 goto out;
539 }
540 }
541
542 root = fdt_find_node_by_path(blob, "/", &addrcp, &sizecp);
543 node_count = fdt_find_subnodes_by_prefix(blob, root, "memory@",
544 &addrcp, &sizecp, nodes,
545 FDT_MAX_MEMORY_NODES);
546 if (node_count >= FDT_MAX_MEMORY_NODES) {
547 printk(BIOS_WARNING, "FDT: Too many memory nodes\n");
548 /* Can still reading the regions for those we got */
549 }
550
551 for (size_t i = 0; i < MIN(node_count, FDT_MAX_MEMORY_NODES); i++) {
552 region_idx += fdt_read_reg_prop(blob, nodes[i], addrcp, sizecp,
553 &regions[region_idx],
554 regions_count - region_idx);
555 if (region_idx >= regions_count) {
556 printk(BIOS_WARNING, "FDT: Too many memory regions\n");
557 goto out;
558 }
559 }
560
561out:
562 for (size_t i = 0; i < MIN(region_idx, regions_count); i++) {
563 printk(BIOS_DEBUG, "FDT: Memory region [%#llx - %#llx]\n",
564 regions[i].addr, regions[i].addr + regions[i].size);
565 }
566
567 return region_idx;
568}
569
570/*
571 * fdt_get_memory_top finds top of memory from a flat device-tree
572 *
573 * @params blob address of FDT
574 *
575 * Returns: Either 0 on error or returns the maximum memory address
576 */
577uint64_t fdt_get_memory_top(const void *blob)
578{
579 struct device_tree_region regions[FDT_MAX_MEMORY_REGIONS] = {0};
580 uint64_t top = 0;
581 uint64_t total = 0;
582 size_t count;
583
584 if (!fdt_is_valid(blob))
585 return 0;
586
587 count = fdt_read_memory_regions(blob, regions, FDT_MAX_MEMORY_REGIONS);
588 for (size_t i = 0; i < MIN(count, FDT_MAX_MEMORY_REGIONS); i++) {
589 top = MAX(top, regions[i].addr + regions[i].size);
590 total += regions[i].size;
591 }
592
593 printk(BIOS_DEBUG, "FDT: Found %u MiB of RAM\n",
594 (uint32_t)(total / MiB));
595
596 return top;
597}
598
599/*
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200600 * Functions to turn a flattened tree into an unflattened one.
601 */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200602
Maximilian Brune33079b82024-03-04 15:34:41 +0100603static int dt_prop_is_phandle(struct device_tree_property *prop)
604{
605 return !(strcmp("phandle", prop->prop.name) &&
606 strcmp("linux,phandle", prop->prop.name));
607}
608
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200609static int fdt_unflatten_node(const void *blob, uint32_t start_offset,
Julius Werner6702b682019-05-03 18:13:53 -0700610 struct device_tree *tree,
Patrick Rudolph666c1722018-04-03 09:57:33 +0200611 struct device_tree_node **new_node)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200612{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200613 struct list_node *last;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200614 int offset = start_offset;
615 const char *name;
616 int size;
617
Maximilian Brune33079b82024-03-04 15:34:41 +0100618 size = fdt_next_node_name(blob, offset, &name);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200619 if (!size)
620 return 0;
621 offset += size;
622
Julius Werner9636a102019-05-03 17:36:43 -0700623 struct device_tree_node *node = xzalloc(sizeof(*node));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200624 *new_node = node;
625 node->name = name;
626
Patrick Rudolph666c1722018-04-03 09:57:33 +0200627 struct fdt_property fprop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200628 last = &node->properties;
629 while ((size = fdt_next_property(blob, offset, &fprop))) {
Julius Werner9636a102019-05-03 17:36:43 -0700630 struct device_tree_property *prop = xzalloc(sizeof(*prop));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200631 prop->prop = fprop;
632
Julius Werner6702b682019-05-03 18:13:53 -0700633 if (dt_prop_is_phandle(prop)) {
634 node->phandle = be32dec(prop->prop.data);
635 if (node->phandle > tree->max_phandle)
636 tree->max_phandle = node->phandle;
637 }
638
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200639 list_insert_after(&prop->list_node, last);
640 last = &prop->list_node;
641
642 offset += size;
643 }
644
Patrick Rudolph666c1722018-04-03 09:57:33 +0200645 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200646 last = &node->children;
Julius Werner6702b682019-05-03 18:13:53 -0700647 while ((size = fdt_unflatten_node(blob, offset, tree, &child))) {
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200648 list_insert_after(&child->list_node, last);
649 last = &child->list_node;
650
651 offset += size;
652 }
653
654 return offset - start_offset + sizeof(uint32_t);
655}
656
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200657static int fdt_unflatten_map_entry(const void *blob, uint32_t offset,
Patrick Rudolph666c1722018-04-03 09:57:33 +0200658 struct device_tree_reserve_map_entry **new)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200659{
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200660 const uint64_t *ptr = (const uint64_t *)(((uint8_t *)blob) + offset);
661 const uint64_t start = be64toh(ptr[0]);
662 const uint64_t size = be64toh(ptr[1]);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200663
664 if (!size)
665 return 0;
666
Julius Werner9636a102019-05-03 17:36:43 -0700667 struct device_tree_reserve_map_entry *entry = xzalloc(sizeof(*entry));
Patrick Rudolph666c1722018-04-03 09:57:33 +0200668 *new = entry;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200669 entry->start = start;
670 entry->size = size;
671
672 return sizeof(uint64_t) * 2;
673}
674
Maximilian Brune33079b82024-03-04 15:34:41 +0100675bool fdt_is_valid(const void *blob)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200676{
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200677 const struct fdt_header *header = (const struct fdt_header *)blob;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200678
Julius Werner73eaec82019-05-03 17:58:07 -0700679 uint32_t magic = be32toh(header->magic);
680 uint32_t version = be32toh(header->version);
681 uint32_t last_comp_version = be32toh(header->last_comp_version);
682
683 if (magic != FDT_HEADER_MAGIC) {
Maximilian Brune33079b82024-03-04 15:34:41 +0100684 printk(BIOS_ERR, "Invalid device tree magic %#.8x!\n", magic);
685 return false;
Julius Werner73eaec82019-05-03 17:58:07 -0700686 }
687 if (last_comp_version > FDT_SUPPORTED_VERSION) {
Maximilian Brune33079b82024-03-04 15:34:41 +0100688 printk(BIOS_ERR, "Unsupported device tree version %u(>=%u)\n",
Julius Werner73eaec82019-05-03 17:58:07 -0700689 version, last_comp_version);
Maximilian Brune33079b82024-03-04 15:34:41 +0100690 return false;
Julius Werner73eaec82019-05-03 17:58:07 -0700691 }
692 if (version > FDT_SUPPORTED_VERSION)
Elyes Haouasd7326282022-12-21 08:16:03 +0100693 printk(BIOS_NOTICE, "FDT version %u too new, should add support!\n",
Julius Werner73eaec82019-05-03 17:58:07 -0700694 version);
Maximilian Brune33079b82024-03-04 15:34:41 +0100695 return true;
696}
697
698struct device_tree *fdt_unflatten(const void *blob)
699{
700 struct device_tree *tree = xzalloc(sizeof(*tree));
701 const struct fdt_header *header = (const struct fdt_header *)blob;
702 tree->header = header;
703
Maximilian Brune64663542024-06-03 05:24:32 +0200704 if (!fdt_is_valid(blob))
Maximilian Brune33079b82024-03-04 15:34:41 +0100705 return NULL;
Julius Werner73eaec82019-05-03 17:58:07 -0700706
Patrick Rudolph666c1722018-04-03 09:57:33 +0200707 uint32_t struct_offset = be32toh(header->structure_offset);
708 uint32_t strings_offset = be32toh(header->strings_offset);
709 uint32_t reserve_offset = be32toh(header->reserve_map_offset);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200710 uint32_t min_offset = 0;
711 min_offset = MIN(struct_offset, strings_offset);
712 min_offset = MIN(min_offset, reserve_offset);
Julius Werner23df4772019-05-17 22:50:18 -0700713 /* Assume everything up to the first non-header component is part of
714 the header and needs to be preserved. This will protect us against
715 new elements being added in the future. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200716 tree->header_size = min_offset;
717
Patrick Rudolph666c1722018-04-03 09:57:33 +0200718 struct device_tree_reserve_map_entry *entry;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200719 uint32_t offset = reserve_offset;
720 int size;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200721 struct list_node *last = &tree->reserve_map;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200722 while ((size = fdt_unflatten_map_entry(blob, offset, &entry))) {
723 list_insert_after(&entry->list_node, last);
724 last = &entry->list_node;
725
726 offset += size;
727 }
728
Julius Werner6702b682019-05-03 18:13:53 -0700729 fdt_unflatten_node(blob, struct_offset, tree, &tree->root);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200730
731 return tree;
732}
733
734
735
736/*
Patrick Rudolph666c1722018-04-03 09:57:33 +0200737 * Functions to find the size of the device tree if it was flattened.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200738 */
739
Patrick Rudolph666c1722018-04-03 09:57:33 +0200740static void dt_flat_prop_size(struct device_tree_property *prop,
741 uint32_t *struct_size, uint32_t *strings_size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200742{
Julius Werner23df4772019-05-17 22:50:18 -0700743 /* Starting token. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200744 *struct_size += sizeof(uint32_t);
Julius Werner23df4772019-05-17 22:50:18 -0700745 /* Size. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200746 *struct_size += sizeof(uint32_t);
Julius Werner23df4772019-05-17 22:50:18 -0700747 /* Name offset. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200748 *struct_size += sizeof(uint32_t);
Julius Werner23df4772019-05-17 22:50:18 -0700749 /* Property value. */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200750 *struct_size += ALIGN_UP(prop->prop.size, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200751
Julius Werner23df4772019-05-17 22:50:18 -0700752 /* Property name. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200753 *strings_size += strlen(prop->prop.name) + 1;
754}
755
Patrick Rudolph666c1722018-04-03 09:57:33 +0200756static void dt_flat_node_size(struct device_tree_node *node,
757 uint32_t *struct_size, uint32_t *strings_size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200758{
Julius Werner23df4772019-05-17 22:50:18 -0700759 /* Starting token. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200760 *struct_size += sizeof(uint32_t);
Julius Werner23df4772019-05-17 22:50:18 -0700761 /* Node name. */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200762 *struct_size += ALIGN_UP(strlen(node->name) + 1, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200763
Patrick Rudolph666c1722018-04-03 09:57:33 +0200764 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200765 list_for_each(prop, node->properties, list_node)
766 dt_flat_prop_size(prop, struct_size, strings_size);
767
Patrick Rudolph666c1722018-04-03 09:57:33 +0200768 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200769 list_for_each(child, node->children, list_node)
770 dt_flat_node_size(child, struct_size, strings_size);
771
Julius Werner23df4772019-05-17 22:50:18 -0700772 /* End token. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200773 *struct_size += sizeof(uint32_t);
774}
775
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200776uint32_t dt_flat_size(const struct device_tree *tree)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200777{
778 uint32_t size = tree->header_size;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200779 struct device_tree_reserve_map_entry *entry;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200780 list_for_each(entry, tree->reserve_map, list_node)
781 size += sizeof(uint64_t) * 2;
782 size += sizeof(uint64_t) * 2;
783
784 uint32_t struct_size = 0;
785 uint32_t strings_size = 0;
786 dt_flat_node_size(tree->root, &struct_size, &strings_size);
787
788 size += struct_size;
Julius Werner23df4772019-05-17 22:50:18 -0700789 /* End token. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200790 size += sizeof(uint32_t);
791
792 size += strings_size;
793
794 return size;
795}
796
797
798
799/*
800 * Functions to flatten a device tree.
801 */
802
Patrick Rudolph666c1722018-04-03 09:57:33 +0200803static void dt_flatten_map_entry(struct device_tree_reserve_map_entry *entry,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200804 void **map_start)
805{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200806 ((uint64_t *)*map_start)[0] = htobe64(entry->start);
807 ((uint64_t *)*map_start)[1] = htobe64(entry->size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200808 *map_start = ((uint8_t *)*map_start) + sizeof(uint64_t) * 2;
809}
810
Patrick Rudolph666c1722018-04-03 09:57:33 +0200811static void dt_flatten_prop(struct device_tree_property *prop,
812 void **struct_start, void *strings_base,
813 void **strings_start)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200814{
815 uint8_t *dstruct = (uint8_t *)*struct_start;
816 uint8_t *dstrings = (uint8_t *)*strings_start;
817
Julius Wernera5ea3a22019-05-07 17:38:12 -0700818 be32enc(dstruct, FDT_TOKEN_PROPERTY);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200819 dstruct += sizeof(uint32_t);
820
Julius Wernera5ea3a22019-05-07 17:38:12 -0700821 be32enc(dstruct, prop->prop.size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200822 dstruct += sizeof(uint32_t);
823
824 uint32_t name_offset = (uintptr_t)dstrings - (uintptr_t)strings_base;
Julius Wernera5ea3a22019-05-07 17:38:12 -0700825 be32enc(dstruct, name_offset);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200826 dstruct += sizeof(uint32_t);
827
828 strcpy((char *)dstrings, prop->prop.name);
829 dstrings += strlen(prop->prop.name) + 1;
830
831 memcpy(dstruct, prop->prop.data, prop->prop.size);
Patrick Rudolph666c1722018-04-03 09:57:33 +0200832 dstruct += ALIGN_UP(prop->prop.size, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200833
834 *struct_start = dstruct;
835 *strings_start = dstrings;
836}
837
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200838static void dt_flatten_node(const struct device_tree_node *node,
839 void **struct_start, void *strings_base,
840 void **strings_start)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200841{
842 uint8_t *dstruct = (uint8_t *)*struct_start;
843 uint8_t *dstrings = (uint8_t *)*strings_start;
844
Julius Wernera5ea3a22019-05-07 17:38:12 -0700845 be32enc(dstruct, FDT_TOKEN_BEGIN_NODE);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200846 dstruct += sizeof(uint32_t);
847
848 strcpy((char *)dstruct, node->name);
Patrick Rudolph666c1722018-04-03 09:57:33 +0200849 dstruct += ALIGN_UP(strlen(node->name) + 1, sizeof(uint32_t));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200850
Patrick Rudolph666c1722018-04-03 09:57:33 +0200851 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200852 list_for_each(prop, node->properties, list_node)
853 dt_flatten_prop(prop, (void **)&dstruct, strings_base,
854 (void **)&dstrings);
855
Patrick Rudolph666c1722018-04-03 09:57:33 +0200856 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200857 list_for_each(child, node->children, list_node)
858 dt_flatten_node(child, (void **)&dstruct, strings_base,
859 (void **)&dstrings);
860
Julius Wernera5ea3a22019-05-07 17:38:12 -0700861 be32enc(dstruct, FDT_TOKEN_END_NODE);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200862 dstruct += sizeof(uint32_t);
863
864 *struct_start = dstruct;
865 *strings_start = dstrings;
866}
867
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200868void dt_flatten(const struct device_tree *tree, void *start_dest)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200869{
870 uint8_t *dest = (uint8_t *)start_dest;
871
872 memcpy(dest, tree->header, tree->header_size);
Patrick Rudolph666c1722018-04-03 09:57:33 +0200873 struct fdt_header *header = (struct fdt_header *)dest;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200874 dest += tree->header_size;
875
Patrick Rudolph666c1722018-04-03 09:57:33 +0200876 struct device_tree_reserve_map_entry *entry;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200877 list_for_each(entry, tree->reserve_map, list_node)
878 dt_flatten_map_entry(entry, (void **)&dest);
879 ((uint64_t *)dest)[0] = ((uint64_t *)dest)[1] = 0;
880 dest += sizeof(uint64_t) * 2;
881
882 uint32_t struct_size = 0;
883 uint32_t strings_size = 0;
884 dt_flat_node_size(tree->root, &struct_size, &strings_size);
885
886 uint8_t *struct_start = dest;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200887 header->structure_offset = htobe32(dest - (uint8_t *)start_dest);
888 header->structure_size = htobe32(struct_size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200889 dest += struct_size;
890
Patrick Rudolph666c1722018-04-03 09:57:33 +0200891 *((uint32_t *)dest) = htobe32(FDT_TOKEN_END);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200892 dest += sizeof(uint32_t);
893
894 uint8_t *strings_start = dest;
Patrick Rudolph666c1722018-04-03 09:57:33 +0200895 header->strings_offset = htobe32(dest - (uint8_t *)start_dest);
896 header->strings_size = htobe32(strings_size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200897 dest += strings_size;
898
899 dt_flatten_node(tree->root, (void **)&struct_start, strings_start,
900 (void **)&strings_start);
901
Patrick Rudolph666c1722018-04-03 09:57:33 +0200902 header->totalsize = htobe32(dest - (uint8_t *)start_dest);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200903}
904
905
906
907/*
908 * Functions for printing a non-flattened device tree.
909 */
910
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200911static void print_node(const struct device_tree_node *node, int depth)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200912{
913 print_indent(depth);
Julius Werner23df4772019-05-17 22:50:18 -0700914 if (depth == 0) /* root node has no name, print a starting slash */
Julius Werner0d746532019-05-06 19:35:56 -0700915 printk(BIOS_DEBUG, "/");
916 printk(BIOS_DEBUG, "%s {\n", node->name);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200917
Patrick Rudolph666c1722018-04-03 09:57:33 +0200918 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200919 list_for_each(prop, node->properties, list_node)
920 print_property(&prop->prop, depth + 1);
921
Julius Werner23df4772019-05-17 22:50:18 -0700922 printk(BIOS_DEBUG, "\n"); /* empty line between props and nodes */
Julius Werner0d746532019-05-06 19:35:56 -0700923
Patrick Rudolph666c1722018-04-03 09:57:33 +0200924 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200925 list_for_each(child, node->children, list_node)
926 print_node(child, depth + 1);
Julius Werner0d746532019-05-06 19:35:56 -0700927
928 print_indent(depth);
929 printk(BIOS_DEBUG, "};\n");
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200930}
931
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200932void dt_print_node(const struct device_tree_node *node)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200933{
934 print_node(node, 0);
935}
936
937
938
939/*
940 * Functions for reading and manipulating an unflattened device tree.
941 */
942
943/*
944 * Read #address-cells and #size-cells properties from a node.
945 *
946 * @param node The device tree node to read from.
947 * @param addrcp Pointer to store #address-cells in, skipped if NULL.
948 * @param sizecp Pointer to store #size-cells in, skipped if NULL.
949 */
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200950void dt_read_cell_props(const struct device_tree_node *node, u32 *addrcp,
951 u32 *sizecp)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200952{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200953 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200954 list_for_each(prop, node->properties, list_node) {
955 if (addrcp && !strcmp("#address-cells", prop->prop.name))
Julius Wernera5ea3a22019-05-07 17:38:12 -0700956 *addrcp = be32dec(prop->prop.data);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200957 if (sizecp && !strcmp("#size-cells", prop->prop.name))
Julius Wernera5ea3a22019-05-07 17:38:12 -0700958 *sizecp = be32dec(prop->prop.data);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200959 }
960}
961
962/*
963 * Find a node from a device tree path, relative to a parent node.
964 *
965 * @param parent The node from which to start the relative path lookup.
966 * @param path An array of path component strings that will be looked
Elyes HAOUASe3e3f4f2018-06-29 21:41:41 +0200967 * up in order to find the node. Must be terminated with
968 * a NULL pointer. Example: {'firmware', 'coreboot', NULL}
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200969 * @param addrcp Pointer that will be updated with any #address-cells
Elyes HAOUASe3e3f4f2018-06-29 21:41:41 +0200970 * value found in the path. May be NULL to ignore.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200971 * @param sizecp Pointer that will be updated with any #size-cells
Elyes HAOUASe3e3f4f2018-06-29 21:41:41 +0200972 * value found in the path. May be NULL to ignore.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200973 * @param create 1: Create node(s) if not found. 0: Return NULL instead.
974 * @return The found/created node, or NULL.
975 */
Patrick Rudolph666c1722018-04-03 09:57:33 +0200976struct device_tree_node *dt_find_node(struct device_tree_node *parent,
977 const char **path, u32 *addrcp,
978 u32 *sizecp, int create)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200979{
Patrick Rudolph666c1722018-04-03 09:57:33 +0200980 struct device_tree_node *node, *found = NULL;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200981
Julius Werner23df4772019-05-17 22:50:18 -0700982 /* Update #address-cells and #size-cells for this level. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200983 dt_read_cell_props(parent, addrcp, sizecp);
984
985 if (!*path)
986 return parent;
987
Julius Werner23df4772019-05-17 22:50:18 -0700988 /* Find the next node in the path, if it exists. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200989 list_for_each(node, parent->children, list_node) {
990 if (!strcmp(node->name, *path)) {
991 found = node;
992 break;
993 }
994 }
995
Julius Werner23df4772019-05-17 22:50:18 -0700996 /* Otherwise create it or return NULL. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +0200997 if (!found) {
998 if (!create)
999 return NULL;
1000
Sergii Dmytruk206328d2022-03-13 18:23:17 +02001001 found = calloc(1, sizeof(*found));
Patrick Rudolph666c1722018-04-03 09:57:33 +02001002 if (!found)
1003 return NULL;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001004 found->name = strdup(*path);
1005 if (!found->name)
1006 return NULL;
1007
1008 list_insert_after(&found->list_node, &parent->children);
1009 }
1010
1011 return dt_find_node(found, path + 1, addrcp, sizecp, create);
1012}
1013
1014/*
Julius Wernerf36d53c2019-05-03 18:23:34 -07001015 * Find a node in the tree from a string device tree path.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001016 *
Julius Wernerf36d53c2019-05-03 18:23:34 -07001017 * @param tree The device tree to search.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001018 * @param path A string representing a path in the device tree, with
Julius Wernerfbec63d2019-05-03 18:29:28 -07001019 * nodes separated by '/'. Example: "/firmware/coreboot"
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001020 * @param addrcp Pointer that will be updated with any #address-cells
1021 * value found in the path. May be NULL to ignore.
1022 * @param sizecp Pointer that will be updated with any #size-cells
1023 * value found in the path. May be NULL to ignore.
1024 * @param create 1: Create node(s) if not found. 0: Return NULL instead.
1025 * @return The found/created node, or NULL.
1026 *
Julius Werner6d5695f2019-05-06 19:23:28 -07001027 * It is the caller responsibility to provide a path string that doesn't end
1028 * with a '/' and doesn't contain any "//". If the path does not start with a
1029 * '/', the first segment is interpreted as an alias. */
Julius Wernerf36d53c2019-05-03 18:23:34 -07001030struct device_tree_node *dt_find_node_by_path(struct device_tree *tree,
Patrick Rudolph666c1722018-04-03 09:57:33 +02001031 const char *path, u32 *addrcp,
1032 u32 *sizecp, int create)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001033{
Julius Werner6d5695f2019-05-06 19:23:28 -07001034 char *sub_path;
1035 char *duped_str;
1036 struct device_tree_node *parent;
1037 char *next_slash;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001038 /* Hopefully enough depth for any node. */
1039 const char *path_array[15];
1040 int i;
Patrick Rudolph666c1722018-04-03 09:57:33 +02001041 struct device_tree_node *node = NULL;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001042
Julius Werner23df4772019-05-17 22:50:18 -07001043 if (path[0] == '/') { /* regular path */
1044 if (path[1] == '\0') { /* special case: "/" is root node */
Julius Werner6d5695f2019-05-06 19:23:28 -07001045 dt_read_cell_props(tree->root, addrcp, sizecp);
1046 return tree->root;
1047 }
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001048
Julius Werner6d5695f2019-05-06 19:23:28 -07001049 sub_path = duped_str = strdup(&path[1]);
1050 if (!sub_path)
1051 return NULL;
1052
1053 parent = tree->root;
Julius Werner23df4772019-05-17 22:50:18 -07001054 } else { /* alias */
Julius Werner6d5695f2019-05-06 19:23:28 -07001055 char *alias;
1056
1057 alias = duped_str = strdup(path);
1058 if (!alias)
1059 return NULL;
1060
1061 sub_path = strchr(alias, '/');
1062 if (sub_path)
1063 *sub_path = '\0';
1064
1065 parent = dt_find_node_by_alias(tree, alias);
1066 if (!parent) {
1067 printk(BIOS_DEBUG,
1068 "Could not find node '%s', alias '%s' does not exist\n",
1069 path, alias);
1070 free(duped_str);
1071 return NULL;
1072 }
1073
1074 if (!sub_path) {
Julius Werner23df4772019-05-17 22:50:18 -07001075 /* it's just the alias, no sub-path */
Julius Werner6d5695f2019-05-06 19:23:28 -07001076 free(duped_str);
1077 return parent;
1078 }
1079
1080 sub_path++;
1081 }
1082
1083 next_slash = sub_path;
1084 path_array[0] = sub_path;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001085 for (i = 1; i < (ARRAY_SIZE(path_array) - 1); i++) {
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001086 next_slash = strchr(next_slash, '/');
1087 if (!next_slash)
1088 break;
1089
1090 *next_slash++ = '\0';
1091 path_array[i] = next_slash;
1092 }
1093
1094 if (!next_slash) {
1095 path_array[i] = NULL;
Julius Werner6d5695f2019-05-06 19:23:28 -07001096 node = dt_find_node(parent, path_array,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001097 addrcp, sizecp, create);
1098 }
1099
Julius Werner6d5695f2019-05-06 19:23:28 -07001100 free(duped_str);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001101 return node;
1102}
1103
Julius Werner6d5695f2019-05-06 19:23:28 -07001104/*
1105 * Find a node from an alias
1106 *
1107 * @param tree The device tree.
1108 * @param alias The alias name.
1109 * @return The found node, or NULL.
1110 */
1111struct device_tree_node *dt_find_node_by_alias(struct device_tree *tree,
1112 const char *alias)
1113{
1114 struct device_tree_node *node;
1115 const char *alias_path;
1116
1117 node = dt_find_node_by_path(tree, "/aliases", NULL, NULL, 0);
1118 if (!node)
1119 return NULL;
1120
1121 alias_path = dt_find_string_prop(node, alias);
1122 if (!alias_path)
1123 return NULL;
1124
1125 return dt_find_node_by_path(tree, alias_path, NULL, NULL, 0);
1126}
1127
Julius Werner6702b682019-05-03 18:13:53 -07001128struct device_tree_node *dt_find_node_by_phandle(struct device_tree_node *root,
1129 uint32_t phandle)
1130{
1131 if (!root)
1132 return NULL;
1133
1134 if (root->phandle == phandle)
1135 return root;
1136
1137 struct device_tree_node *node;
1138 struct device_tree_node *result;
1139 list_for_each(node, root->children, list_node) {
1140 result = dt_find_node_by_phandle(node, phandle);
1141 if (result)
1142 return result;
1143 }
1144
1145 return NULL;
1146}
1147
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001148/*
1149 * Check if given node is compatible.
1150 *
1151 * @param node The node which is to be checked for compatible property.
1152 * @param compat The compatible string to match.
1153 * @return 1 = compatible, 0 = not compatible.
1154 */
Patrick Rudolph666c1722018-04-03 09:57:33 +02001155static int dt_check_compat_match(struct device_tree_node *node,
1156 const char *compat)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001157{
Patrick Rudolph666c1722018-04-03 09:57:33 +02001158 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001159
1160 list_for_each(prop, node->properties, list_node) {
1161 if (!strcmp("compatible", prop->prop.name)) {
1162 size_t bytes = prop->prop.size;
1163 const char *str = prop->prop.data;
1164 while (bytes > 0) {
1165 if (!strncmp(compat, str, bytes))
1166 return 1;
1167 size_t len = strnlen(str, bytes) + 1;
1168 if (bytes <= len)
1169 break;
1170 str += len;
1171 bytes -= len;
1172 }
1173 break;
1174 }
1175 }
1176
1177 return 0;
1178}
1179
1180/*
1181 * Find a node from a compatible string, in the subtree of a parent node.
1182 *
1183 * @param parent The parent node under which to look.
1184 * @param compat The compatible string to find.
1185 * @return The found node, or NULL.
1186 */
Patrick Rudolph666c1722018-04-03 09:57:33 +02001187struct device_tree_node *dt_find_compat(struct device_tree_node *parent,
1188 const char *compat)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001189{
Julius Werner23df4772019-05-17 22:50:18 -07001190 /* Check if the parent node itself is compatible. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001191 if (dt_check_compat_match(parent, compat))
1192 return parent;
1193
Patrick Rudolph666c1722018-04-03 09:57:33 +02001194 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001195 list_for_each(child, parent->children, list_node) {
Patrick Rudolph666c1722018-04-03 09:57:33 +02001196 struct device_tree_node *found = dt_find_compat(child, compat);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001197 if (found)
1198 return found;
1199 }
1200
1201 return NULL;
1202}
1203
1204/*
Martin Roth0949e732021-10-01 14:28:22 -06001205 * Find the next compatible child of a given parent. All children up to the
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001206 * child passed in by caller are ignored. If child is NULL, it considers all the
1207 * children to find the first child which is compatible.
1208 *
1209 * @param parent The parent node under which to look.
1210 * @param child The child node to start search from (exclusive). If NULL
1211 * consider all children.
1212 * @param compat The compatible string to find.
1213 * @return The found node, or NULL.
1214 */
Patrick Rudolph666c1722018-04-03 09:57:33 +02001215struct device_tree_node *
1216dt_find_next_compat_child(struct device_tree_node *parent,
1217 struct device_tree_node *child,
1218 const char *compat)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001219{
Patrick Rudolph666c1722018-04-03 09:57:33 +02001220 struct device_tree_node *next;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001221 int ignore = 0;
1222
1223 if (child)
1224 ignore = 1;
1225
1226 list_for_each(next, parent->children, list_node) {
1227 if (ignore) {
1228 if (child == next)
1229 ignore = 0;
1230 continue;
1231 }
1232
1233 if (dt_check_compat_match(next, compat))
1234 return next;
1235 }
1236
1237 return NULL;
1238}
1239
1240/*
1241 * Find a node with matching property value, in the subtree of a parent node.
1242 *
1243 * @param parent The parent node under which to look.
1244 * @param name The property name to look for.
1245 * @param data The property value to look for.
1246 * @param size The property size.
1247 */
Patrick Rudolph666c1722018-04-03 09:57:33 +02001248struct device_tree_node *dt_find_prop_value(struct device_tree_node *parent,
1249 const char *name, void *data,
1250 size_t size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001251{
Patrick Rudolph666c1722018-04-03 09:57:33 +02001252 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001253
1254 /* Check if parent itself has the required property value. */
1255 list_for_each(prop, parent->properties, list_node) {
1256 if (!strcmp(name, prop->prop.name)) {
1257 size_t bytes = prop->prop.size;
Patrick Rudolph0a7d6902018-08-22 09:55:15 +02001258 const void *prop_data = prop->prop.data;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001259 if (size != bytes)
1260 break;
1261 if (!memcmp(data, prop_data, size))
1262 return parent;
1263 break;
1264 }
1265 }
1266
Patrick Rudolph666c1722018-04-03 09:57:33 +02001267 struct device_tree_node *child;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001268 list_for_each(child, parent->children, list_node) {
Patrick Rudolph666c1722018-04-03 09:57:33 +02001269 struct device_tree_node *found = dt_find_prop_value(child, name,
1270 data, size);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001271 if (found)
1272 return found;
1273 }
1274 return NULL;
1275}
1276
1277/*
1278 * Write an arbitrary sized big-endian integer into a pointer.
1279 *
1280 * @param dest Pointer to the DT property data buffer to write.
Elyes HAOUAS1ec76442018-08-07 12:20:04 +02001281 * @param src The integer to write (in CPU endianness).
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001282 * @param length the length of the destination integer in bytes.
1283 */
1284void dt_write_int(u8 *dest, u64 src, size_t length)
1285{
1286 while (length--) {
1287 dest[length] = (u8)src;
1288 src >>= 8;
1289 }
1290}
1291
1292/*
Patrick Rudolph5ccc7312018-05-30 15:05:28 +02001293 * Delete a property by name in a given node if it exists.
1294 *
1295 * @param node The device tree node to operate on.
1296 * @param name The name of the property to delete.
1297 */
1298void dt_delete_prop(struct device_tree_node *node, const char *name)
1299{
1300 struct device_tree_property *prop;
1301
1302 list_for_each(prop, node->properties, list_node) {
1303 if (!strcmp(prop->prop.name, name)) {
1304 list_remove(&prop->list_node);
1305 return;
1306 }
1307 }
1308}
1309
1310/*
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001311 * Add an arbitrary property to a node, or update it if it already exists.
1312 *
1313 * @param node The device tree node to add to.
1314 * @param name The name of the new property.
1315 * @param data The raw data blob to be stored in the property.
1316 * @param size The size of data in bytes.
1317 */
Patrick Rudolph666c1722018-04-03 09:57:33 +02001318void dt_add_bin_prop(struct device_tree_node *node, const char *name,
Julius Werner0e9116f2019-05-13 17:30:31 -07001319 void *data, size_t size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001320{
Patrick Rudolph666c1722018-04-03 09:57:33 +02001321 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001322
1323 list_for_each(prop, node->properties, list_node) {
1324 if (!strcmp(prop->prop.name, name)) {
1325 prop->prop.data = data;
1326 prop->prop.size = size;
1327 return;
1328 }
1329 }
1330
Julius Werner9636a102019-05-03 17:36:43 -07001331 prop = xzalloc(sizeof(*prop));
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001332 list_insert_after(&prop->list_node, &node->properties);
1333 prop->prop.name = name;
1334 prop->prop.data = data;
1335 prop->prop.size = size;
1336}
1337
1338/*
1339 * Find given string property in a node and return its content.
1340 *
1341 * @param node The device tree node to search.
1342 * @param name The name of the property.
1343 * @return The found string, or NULL.
1344 */
Patrick Rudolph0a7d6902018-08-22 09:55:15 +02001345const char *dt_find_string_prop(const struct device_tree_node *node,
1346 const char *name)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001347{
Patrick Rudolph0a7d6902018-08-22 09:55:15 +02001348 const void *content;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001349 size_t size;
1350
1351 dt_find_bin_prop(node, name, &content, &size);
1352
1353 return content;
1354}
1355
1356/*
1357 * Find given property in a node.
1358 *
1359 * @param node The device tree node to search.
1360 * @param name The name of the property.
1361 * @param data Pointer to return raw data blob in the property.
1362 * @param size Pointer to return the size of data in bytes.
1363 */
Patrick Rudolph0a7d6902018-08-22 09:55:15 +02001364void dt_find_bin_prop(const struct device_tree_node *node, const char *name,
1365 const void **data, size_t *size)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001366{
Patrick Rudolph666c1722018-04-03 09:57:33 +02001367 struct device_tree_property *prop;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001368
1369 *data = NULL;
1370 *size = 0;
1371
1372 list_for_each(prop, node->properties, list_node) {
1373 if (!strcmp(prop->prop.name, name)) {
1374 *data = prop->prop.data;
1375 *size = prop->prop.size;
1376 return;
1377 }
1378 }
1379}
1380
1381/*
1382 * Add a string property to a node, or update it if it already exists.
1383 *
1384 * @param node The device tree node to add to.
1385 * @param name The name of the new property.
1386 * @param str The zero-terminated string to be stored in the property.
1387 */
Patrick Rudolph666c1722018-04-03 09:57:33 +02001388void dt_add_string_prop(struct device_tree_node *node, const char *name,
Patrick Rudolph0a7d6902018-08-22 09:55:15 +02001389 const char *str)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001390{
Julius Werner0e9116f2019-05-13 17:30:31 -07001391 dt_add_bin_prop(node, name, (char *)str, strlen(str) + 1);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001392}
1393
1394/*
1395 * Add a 32-bit integer property to a node, or update it if it already exists.
1396 *
1397 * @param node The device tree node to add to.
1398 * @param name The name of the new property.
1399 * @param val The integer to be stored in the property.
1400 */
Patrick Rudolph666c1722018-04-03 09:57:33 +02001401void dt_add_u32_prop(struct device_tree_node *node, const char *name, u32 val)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001402{
Julius Werner9636a102019-05-03 17:36:43 -07001403 u32 *val_ptr = xmalloc(sizeof(val));
Patrick Rudolph666c1722018-04-03 09:57:33 +02001404 *val_ptr = htobe32(val);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001405 dt_add_bin_prop(node, name, val_ptr, sizeof(*val_ptr));
1406}
1407
1408/*
Patrick Rudolph3fca4ed2018-08-10 10:12:35 +02001409 * Add a 64-bit integer property to a node, or update it if it already exists.
1410 *
1411 * @param node The device tree node to add to.
1412 * @param name The name of the new property.
1413 * @param val The integer to be stored in the property.
1414 */
1415void dt_add_u64_prop(struct device_tree_node *node, const char *name, u64 val)
1416{
Julius Werner9636a102019-05-03 17:36:43 -07001417 u64 *val_ptr = xmalloc(sizeof(val));
Patrick Rudolph3fca4ed2018-08-10 10:12:35 +02001418 *val_ptr = htobe64(val);
1419 dt_add_bin_prop(node, name, val_ptr, sizeof(*val_ptr));
1420}
1421
1422/*
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001423 * Add a 'reg' address list property to a node, or update it if it exists.
1424 *
1425 * @param node The device tree node to add to.
Maximilian Brune33079b82024-03-04 15:34:41 +01001426 * @param regions Array of address values to be stored in the property.
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001427 * @param sizes Array of corresponding size values to 'addrs'.
1428 * @param count Number of values in 'addrs' and 'sizes' (must be equal).
1429 * @param addr_cells Value of #address-cells property valid for this node.
1430 * @param size_cells Value of #size-cells property valid for this node.
1431 */
Patrick Rudolph666c1722018-04-03 09:57:33 +02001432void dt_add_reg_prop(struct device_tree_node *node, u64 *addrs, u64 *sizes,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001433 int count, u32 addr_cells, u32 size_cells)
1434{
1435 int i;
1436 size_t length = (addr_cells + size_cells) * sizeof(u32) * count;
Julius Werner9636a102019-05-03 17:36:43 -07001437 u8 *data = xmalloc(length);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001438 u8 *cur = data;
1439
1440 for (i = 0; i < count; i++) {
1441 dt_write_int(cur, addrs[i], addr_cells * sizeof(u32));
1442 cur += addr_cells * sizeof(u32);
1443 dt_write_int(cur, sizes[i], size_cells * sizeof(u32));
1444 cur += size_cells * sizeof(u32);
1445 }
1446
1447 dt_add_bin_prop(node, "reg", data, length);
1448}
1449
1450/*
1451 * Fixups to apply to a kernel's device tree before booting it.
1452 */
1453
Patrick Rudolph666c1722018-04-03 09:57:33 +02001454struct list_node device_tree_fixups;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001455
Patrick Rudolph666c1722018-04-03 09:57:33 +02001456int dt_apply_fixups(struct device_tree *tree)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001457{
Patrick Rudolph666c1722018-04-03 09:57:33 +02001458 struct device_tree_fixup *fixup;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001459 list_for_each(fixup, device_tree_fixups, list_node) {
1460 assert(fixup->fixup);
1461 if (fixup->fixup(fixup, tree))
1462 return 1;
1463 }
1464 return 0;
1465}
1466
Patrick Rudolph666c1722018-04-03 09:57:33 +02001467int dt_set_bin_prop_by_path(struct device_tree *tree, const char *path,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001468 void *data, size_t data_size, int create)
1469{
1470 char *path_copy, *prop_name;
Patrick Rudolph666c1722018-04-03 09:57:33 +02001471 struct device_tree_node *dt_node;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001472
1473 path_copy = strdup(path);
1474
1475 if (!path_copy) {
Patrick Rudolph666c1722018-04-03 09:57:33 +02001476 printk(BIOS_ERR, "Failed to allocate a copy of path %s\n",
1477 path);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001478 return 1;
1479 }
1480
1481 prop_name = strrchr(path_copy, '/');
1482 if (!prop_name) {
Patrick Rudolph679d6242018-07-11 13:53:04 +02001483 free(path_copy);
Patrick Rudolph666c1722018-04-03 09:57:33 +02001484 printk(BIOS_ERR, "Path %s does not include '/'\n", path);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001485 return 1;
1486 }
1487
1488 *prop_name++ = '\0'; /* Separate path from the property name. */
1489
Julius Wernerf36d53c2019-05-03 18:23:34 -07001490 dt_node = dt_find_node_by_path(tree, path_copy, NULL,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001491 NULL, create);
1492
1493 if (!dt_node) {
Patrick Rudolph666c1722018-04-03 09:57:33 +02001494 printk(BIOS_ERR, "Failed to %s %s in the device tree\n",
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001495 create ? "create" : "find", path_copy);
Patrick Rudolph679d6242018-07-11 13:53:04 +02001496 free(path_copy);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001497 return 1;
1498 }
1499
1500 dt_add_bin_prop(dt_node, prop_name, data, data_size);
Patrick Rudolph679d6242018-07-11 13:53:04 +02001501 free(path_copy);
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001502
1503 return 0;
1504}
1505
1506/*
1507 * Prepare the /reserved-memory/ node.
1508 *
1509 * Technically, this can be called more than one time, to init and/or retrieve
1510 * the node. But dt_add_u32_prop() may leak a bit of memory if you do.
1511 *
1512 * @tree: Device tree to add/retrieve from.
1513 * @return: The /reserved-memory/ node (or NULL, if error).
1514 */
Patrick Rudolph666c1722018-04-03 09:57:33 +02001515struct device_tree_node *dt_init_reserved_memory_node(struct device_tree *tree)
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001516{
Patrick Rudolph666c1722018-04-03 09:57:33 +02001517 struct device_tree_node *reserved;
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001518 u32 addr = 0, size = 0;
1519
Julius Wernerfbec63d2019-05-03 18:29:28 -07001520 reserved = dt_find_node_by_path(tree, "/reserved-memory", &addr,
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001521 &size, 1);
1522 if (!reserved)
1523 return NULL;
1524
Julius Werner23df4772019-05-17 22:50:18 -07001525 /* Binding doc says this should have the same #{address,size}-cells as
1526 the root. */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001527 dt_add_u32_prop(reserved, "#address-cells", addr);
1528 dt_add_u32_prop(reserved, "#size-cells", size);
Julius Werner23df4772019-05-17 22:50:18 -07001529 /* Binding doc says this should be empty (1:1 mapping from root). */
Patrick Rudolph67aca3e2018-04-12 11:44:43 +02001530 dt_add_bin_prop(reserved, "ranges", NULL, 0);
1531
1532 return reserved;
1533}
Julius Werner735ddc92019-05-07 17:05:28 -07001534
1535/*
1536 * Increment a single phandle in prop at a given offset by a given adjustment.
1537 *
1538 * @param prop Property whose phandle should be adjusted.
1539 * @param adjustment Value that should be added to the existing phandle.
1540 * @param offset Byte offset of the phandle in the property data.
1541 *
1542 * @return New phandle value, or 0 on error.
1543 */
1544static uint32_t dt_adjust_phandle(struct device_tree_property *prop,
1545 uint32_t adjustment, uint32_t offset)
1546{
1547 if (offset + 4 > prop->prop.size)
1548 return 0;
1549
1550 uint32_t phandle = be32dec(prop->prop.data + offset);
1551 if (phandle == 0 ||
1552 phandle == FDT_PHANDLE_ILLEGAL ||
1553 phandle == 0xffffffff)
1554 return 0;
1555
1556 phandle += adjustment;
1557 if (phandle >= FDT_PHANDLE_ILLEGAL)
1558 return 0;
1559
1560 be32enc(prop->prop.data + offset, phandle);
1561 return phandle;
1562}
1563
1564/*
1565 * Adjust all phandles in subtree by adding a new base offset.
1566 *
1567 * @param node Root node of the subtree to work on.
1568 * @param base New phandle base to be added to all phandles.
1569 *
1570 * @return New highest phandle in the subtree, or 0 on error.
1571 */
1572static uint32_t dt_adjust_all_phandles(struct device_tree_node *node,
1573 uint32_t base)
1574{
Julius Werner23df4772019-05-17 22:50:18 -07001575 uint32_t new_max = MAX(base, 1); /* make sure we don't return 0 */
Julius Werner735ddc92019-05-07 17:05:28 -07001576 struct device_tree_property *prop;
1577 struct device_tree_node *child;
1578
1579 if (!node)
1580 return new_max;
1581
1582 list_for_each(prop, node->properties, list_node)
1583 if (dt_prop_is_phandle(prop)) {
1584 node->phandle = dt_adjust_phandle(prop, base, 0);
1585 if (!node->phandle)
1586 return 0;
1587 new_max = MAX(new_max, node->phandle);
Julius Werner23df4772019-05-17 22:50:18 -07001588 } /* no break -- can have more than one phandle prop */
Julius Werner735ddc92019-05-07 17:05:28 -07001589
1590 list_for_each(child, node->children, list_node)
1591 new_max = MAX(new_max, dt_adjust_all_phandles(child, base));
1592
1593 return new_max;
1594}
1595
1596/*
1597 * Apply a /__local_fixup__ subtree to the corresponding overlay subtree.
1598 *
1599 * @param node Root node of the overlay subtree to fix up.
1600 * @param node Root node of the /__local_fixup__ subtree.
1601 * @param base Adjustment that was added to phandles in the overlay.
1602 *
1603 * @return 0 on success, -1 on error.
1604 */
1605static int dt_fixup_locals(struct device_tree_node *node,
1606 struct device_tree_node *fixup, uint32_t base)
1607{
1608 struct device_tree_property *prop;
1609 struct device_tree_property *fixup_prop;
1610 struct device_tree_node *child;
1611 struct device_tree_node *fixup_child;
1612 int i;
1613
Julius Werner23df4772019-05-17 22:50:18 -07001614 /*
1615 * For local fixups the /__local_fixup__ subtree contains the same node
1616 * hierarchy as the main tree we're fixing up. Each property contains
1617 * the fixup offsets for the respective property in the main tree. For
1618 * each property in the fixup node, find the corresponding property in
1619 * the base node and apply fixups to all offsets it specifies.
1620 */
Julius Werner735ddc92019-05-07 17:05:28 -07001621 list_for_each(fixup_prop, fixup->properties, list_node) {
1622 struct device_tree_property *base_prop = NULL;
1623 list_for_each(prop, node->properties, list_node)
1624 if (!strcmp(prop->prop.name, fixup_prop->prop.name)) {
1625 base_prop = prop;
1626 break;
1627 }
1628
Julius Werner23df4772019-05-17 22:50:18 -07001629 /* We should always find a corresponding base prop for a fixup,
1630 and fixup props contain a list of 32-bit fixup offsets. */
Julius Werner735ddc92019-05-07 17:05:28 -07001631 if (!base_prop || fixup_prop->prop.size % sizeof(uint32_t))
1632 return -1;
1633
1634 for (i = 0; i < fixup_prop->prop.size; i += sizeof(uint32_t))
1635 if (!dt_adjust_phandle(base_prop, base, be32dec(
1636 fixup_prop->prop.data + i)))
1637 return -1;
1638 }
1639
Julius Werner23df4772019-05-17 22:50:18 -07001640 /* Now recursively descend both the base tree and the /__local_fixups__
1641 subtree in sync to apply all fixups. */
Julius Werner735ddc92019-05-07 17:05:28 -07001642 list_for_each(fixup_child, fixup->children, list_node) {
1643 struct device_tree_node *base_child = NULL;
1644 list_for_each(child, node->children, list_node)
1645 if (!strcmp(child->name, fixup_child->name)) {
1646 base_child = child;
1647 break;
1648 }
1649
Julius Werner23df4772019-05-17 22:50:18 -07001650 /* All fixup nodes should have a corresponding base node. */
Julius Werner735ddc92019-05-07 17:05:28 -07001651 if (!base_child)
1652 return -1;
1653
1654 if (dt_fixup_locals(base_child, fixup_child, base) < 0)
1655 return -1;
1656 }
1657
1658 return 0;
1659}
1660
1661/*
1662 * Update all /__symbols__ properties in an overlay that start with
1663 * "/fragment@X/__overlay__" with corresponding path prefix in the base tree.
1664 *
1665 * @param symbols /__symbols__ done to update.
1666 * @param fragment /fragment@X node that references to should be updated.
1667 * @param base_path Path of base tree node that the fragment overlaid.
1668 */
1669static void dt_fix_symbols(struct device_tree_node *symbols,
1670 struct device_tree_node *fragment,
1671 const char *base_path)
1672{
1673 struct device_tree_property *prop;
Julius Werner23df4772019-05-17 22:50:18 -07001674 char buf[512]; /* Should be enough for maximum DT path length? */
1675 char node_path[64]; /* easily enough for /fragment@XXXX/__overlay__ */
Julius Werner735ddc92019-05-07 17:05:28 -07001676
Julius Werner23df4772019-05-17 22:50:18 -07001677 if (!symbols) /* If the overlay has no /__symbols__ node, we're done! */
Julius Werner735ddc92019-05-07 17:05:28 -07001678 return;
1679
1680 int len = snprintf(node_path, sizeof(node_path), "/%s/__overlay__",
1681 fragment->name);
1682
1683 list_for_each(prop, symbols->properties, list_node)
1684 if (!strncmp(prop->prop.data, node_path, len)) {
1685 prop->prop.size = snprintf(buf, sizeof(buf), "%s%s",
1686 base_path, (char *)prop->prop.data + len) + 1;
1687 free(prop->prop.data);
1688 prop->prop.data = strdup(buf);
1689 }
1690}
1691
1692/*
1693 * Fix up overlay according to a property in /__fixup__. If the fixed property
1694 * is a /fragment@X:target, also update /__symbols__ references to fragment.
1695 *
1696 * @params overlay Overlay to fix up.
1697 * @params fixup /__fixup__ property.
1698 * @params phandle phandle value to insert where the fixup points to.
1699 * @params base_path Path to the base DT node that the fixup points to.
1700 * @params overlay_symbols /__symbols__ node of the overlay.
1701 *
1702 * @return 0 on success, -1 on error.
1703 */
1704static int dt_fixup_external(struct device_tree *overlay,
1705 struct device_tree_property *fixup,
1706 uint32_t phandle, const char *base_path,
1707 struct device_tree_node *overlay_symbols)
1708{
1709 struct device_tree_property *prop;
1710
Julius Werner23df4772019-05-17 22:50:18 -07001711 /* External fixup properties are encoded as "<path>:<prop>:<offset>". */
Julius Werner735ddc92019-05-07 17:05:28 -07001712 char *entry = fixup->prop.data;
1713 while ((void *)entry < fixup->prop.data + fixup->prop.size) {
Julius Werner23df4772019-05-17 22:50:18 -07001714 /* okay to destroy fixup property value, won't need it again */
Julius Werner735ddc92019-05-07 17:05:28 -07001715 char *node_path = entry;
1716 entry = strchr(node_path, ':');
1717 if (!entry)
1718 return -1;
1719 *entry++ = '\0';
1720
1721 char *prop_name = entry;
1722 entry = strchr(prop_name, ':');
1723 if (!entry)
1724 return -1;
1725 *entry++ = '\0';
1726
1727 struct device_tree_node *ovl_node = dt_find_node_by_path(
1728 overlay, node_path, NULL, NULL, 0);
1729 if (!ovl_node || !isdigit(*entry))
1730 return -1;
1731
1732 struct device_tree_property *ovl_prop = NULL;
1733 list_for_each(prop, ovl_node->properties, list_node)
1734 if (!strcmp(prop->prop.name, prop_name)) {
1735 ovl_prop = prop;
1736 break;
1737 }
1738
Julius Werner23df4772019-05-17 22:50:18 -07001739 /* Move entry to first char after number, must be a '\0'. */
Julius Werner735ddc92019-05-07 17:05:28 -07001740 uint32_t offset = skip_atoi(&entry);
1741 if (!ovl_prop || offset + 4 > ovl_prop->prop.size || entry[0])
1742 return -1;
Julius Werner23df4772019-05-17 22:50:18 -07001743 entry++; /* jump over '\0' to potential next fixup */
Julius Werner735ddc92019-05-07 17:05:28 -07001744
1745 be32enc(ovl_prop->prop.data + offset, phandle);
1746
Julius Werner23df4772019-05-17 22:50:18 -07001747 /* If this is a /fragment@X:target property, update references
1748 to this fragment in the overlay __symbols__ now. */
Julius Werner735ddc92019-05-07 17:05:28 -07001749 if (offset == 0 && !strcmp(prop_name, "target") &&
Julius Werner23df4772019-05-17 22:50:18 -07001750 !strchr(node_path + 1, '/')) /* only toplevel nodes */
Julius Werner735ddc92019-05-07 17:05:28 -07001751 dt_fix_symbols(overlay_symbols, ovl_node, base_path);
1752 }
1753
1754 return 0;
1755}
1756
1757/*
1758 * Apply all /__fixup__ properties in the overlay. This will destroy the
1759 * property data in /__fixup__ and it should not be accessed again.
1760 *
1761 * @params tree Base device tree that the overlay updates.
1762 * @params symbols /__symbols__ node of the base device tree.
1763 * @params overlay Overlay to fix up.
1764 * @params fixups /__fixup__ node in the overlay.
1765 * @params overlay_symbols /__symbols__ node of the overlay.
1766 *
1767 * @return 0 on success, -1 on error.
1768 */
1769static int dt_fixup_all_externals(struct device_tree *tree,
1770 struct device_tree_node *symbols,
1771 struct device_tree *overlay,
1772 struct device_tree_node *fixups,
1773 struct device_tree_node *overlay_symbols)
1774{
1775 struct device_tree_property *fix;
1776
Julius Werner23df4772019-05-17 22:50:18 -07001777 /* If we have any external fixups, base tree must have /__symbols__. */
Julius Werner735ddc92019-05-07 17:05:28 -07001778 if (!symbols)
1779 return -1;
1780
Julius Werner23df4772019-05-17 22:50:18 -07001781 /*
1782 * Unlike /__local_fixups__, /__fixups__ is not a whole subtree that
1783 * mirrors the node hierarchy. It's just a directory of fixup properties
1784 * that each directly contain all information necessary to apply them.
1785 */
Julius Werner735ddc92019-05-07 17:05:28 -07001786 list_for_each(fix, fixups->properties, list_node) {
Julius Werner23df4772019-05-17 22:50:18 -07001787 /* The name of a fixup property is the label of the node we want
1788 a property to phandle-reference. Look up in /__symbols__. */
Julius Werner735ddc92019-05-07 17:05:28 -07001789 const char *path = dt_find_string_prop(symbols, fix->prop.name);
1790 if (!path)
1791 return -1;
1792
Elyes HAOUAS0afaff22021-01-16 15:02:31 +01001793 /* Find node the label pointed to figure out its phandle. */
Julius Werner735ddc92019-05-07 17:05:28 -07001794 struct device_tree_node *node = dt_find_node_by_path(tree, path,
1795 NULL, NULL, 0);
1796 if (!node)
1797 return -1;
1798
Julius Werner23df4772019-05-17 22:50:18 -07001799 /* Write into the overlay property(s) pointing to that node. */
Julius Werner735ddc92019-05-07 17:05:28 -07001800 if (dt_fixup_external(overlay, fix, node->phandle,
1801 path, overlay_symbols) < 0)
1802 return -1;
1803 }
1804
1805 return 0;
1806}
1807
1808/*
1809 * Copy all nodes and properties from one DT subtree into another. This is a
1810 * shallow copy so both trees will point to the same property data afterwards.
1811 *
1812 * @params dst Destination subtree to copy into.
1813 * @params src Source subtree to copy from.
1814 * @params upd 1 to overwrite same-name properties, 0 to discard them.
1815 */
1816static void dt_copy_subtree(struct device_tree_node *dst,
1817 struct device_tree_node *src, int upd)
1818{
1819 struct device_tree_property *prop;
1820 struct device_tree_property *src_prop;
1821 list_for_each(src_prop, src->properties, list_node) {
1822 if (dt_prop_is_phandle(src_prop) ||
1823 !strcmp(src_prop->prop.name, "name")) {
1824 printk(BIOS_DEBUG,
1825 "WARNING: ignoring illegal overlay prop '%s'\n",
1826 src_prop->prop.name);
1827 continue;
1828 }
1829
1830 struct device_tree_property *dst_prop = NULL;
1831 list_for_each(prop, dst->properties, list_node)
1832 if (!strcmp(prop->prop.name, src_prop->prop.name)) {
1833 dst_prop = prop;
1834 break;
1835 }
1836
1837 if (dst_prop) {
1838 if (!upd) {
1839 printk(BIOS_DEBUG,
1840 "WARNING: ignoring prop update '%s'\n",
1841 src_prop->prop.name);
1842 continue;
1843 }
1844 } else {
1845 dst_prop = xzalloc(sizeof(*dst_prop));
1846 list_insert_after(&dst_prop->list_node,
1847 &dst->properties);
1848 }
1849
1850 dst_prop->prop = src_prop->prop;
1851 }
1852
1853 struct device_tree_node *node;
1854 struct device_tree_node *src_node;
1855 list_for_each(src_node, src->children, list_node) {
1856 struct device_tree_node *dst_node = NULL;
1857 list_for_each(node, dst->children, list_node)
1858 if (!strcmp(node->name, src_node->name)) {
1859 dst_node = node;
1860 break;
1861 }
1862
1863 if (!dst_node) {
1864 dst_node = xzalloc(sizeof(*dst_node));
1865 *dst_node = *src_node;
1866 list_insert_after(&dst_node->list_node, &dst->children);
1867 } else {
1868 dt_copy_subtree(dst_node, src_node, upd);
1869 }
1870 }
1871}
1872
1873/*
1874 * Apply an overlay /fragment@X node to a base device tree.
1875 *
1876 * @param tree Base device tree.
1877 * @param fragment /fragment@X node.
1878 * @params overlay_symbols /__symbols__ node of the overlay.
1879 *
1880 * @return 0 on success, -1 on error.
1881 */
1882static int dt_import_fragment(struct device_tree *tree,
1883 struct device_tree_node *fragment,
1884 struct device_tree_node *overlay_symbols)
1885{
Julius Werner23df4772019-05-17 22:50:18 -07001886 /* The actual overlaid nodes/props are in an __overlay__ child node. */
Julius Werner735ddc92019-05-07 17:05:28 -07001887 static const char *overlay_path[] = { "__overlay__", NULL };
1888 struct device_tree_node *overlay = dt_find_node(fragment, overlay_path,
1889 NULL, NULL, 0);
1890
Julius Werner23df4772019-05-17 22:50:18 -07001891 /* If it doesn't have an __overlay__ child, it's not a fragment. */
Julius Werner735ddc92019-05-07 17:05:28 -07001892 if (!overlay)
1893 return 0;
1894
Julius Werner23df4772019-05-17 22:50:18 -07001895 /* Target node of the fragment can be given by path or by phandle. */
Julius Werner735ddc92019-05-07 17:05:28 -07001896 struct device_tree_property *prop;
1897 struct device_tree_property *phandle = NULL;
1898 struct device_tree_property *path = NULL;
1899 list_for_each(prop, fragment->properties, list_node) {
1900 if (!strcmp(prop->prop.name, "target")) {
1901 phandle = prop;
Julius Werner23df4772019-05-17 22:50:18 -07001902 break; /* phandle target has priority, stop looking */
Julius Werner735ddc92019-05-07 17:05:28 -07001903 }
1904 if (!strcmp(prop->prop.name, "target-path"))
1905 path = prop;
1906 }
1907
1908 struct device_tree_node *target = NULL;
1909 if (phandle) {
1910 if (phandle->prop.size != sizeof(uint32_t))
1911 return -1;
1912 target = dt_find_node_by_phandle(tree->root,
1913 be32dec(phandle->prop.data));
Julius Werner23df4772019-05-17 22:50:18 -07001914 /* Symbols already updated as part of dt_fixup_external(). */
Julius Werner735ddc92019-05-07 17:05:28 -07001915 } else if (path) {
1916 target = dt_find_node_by_path(tree, path->prop.data,
1917 NULL, NULL, 0);
1918 dt_fix_symbols(overlay_symbols, fragment, path->prop.data);
1919 }
1920 if (!target)
1921 return -1;
1922
1923 dt_copy_subtree(target, overlay, 1);
1924 return 0;
1925}
1926
1927/*
1928 * Apply a device tree overlay to a base device tree. This will
1929 * destroy/incorporate the overlay data, so it should not be freed or reused.
1930 * See dtc.git/Documentation/dt-object-internal.txt for overlay format details.
1931 *
1932 * @param tree Unflattened base device tree to add the overlay into.
1933 * @param overlay Unflattened overlay device tree to apply to the base.
1934 *
1935 * @return 0 on success, -1 on error.
1936 */
1937int dt_apply_overlay(struct device_tree *tree, struct device_tree *overlay)
1938{
Julius Werner23df4772019-05-17 22:50:18 -07001939 /*
1940 * First, we need to make sure phandles inside the overlay don't clash
1941 * with those in the base tree. We just define the highest phandle value
1942 * in the base tree as the "phandle offset" for this overlay and
1943 * increment all phandles in it by that value.
1944 */
Julius Werner735ddc92019-05-07 17:05:28 -07001945 uint32_t phandle_base = tree->max_phandle;
1946 uint32_t new_max = dt_adjust_all_phandles(overlay->root, phandle_base);
1947 if (!new_max) {
Elyes HAOUAS8b5841e2022-02-08 21:23:12 +01001948 printk(BIOS_ERR, "invalid phandles in overlay\n");
Julius Werner735ddc92019-05-07 17:05:28 -07001949 return -1;
1950 }
1951 tree->max_phandle = new_max;
1952
Julius Werner23df4772019-05-17 22:50:18 -07001953 /* Now that we changed phandles in the overlay, we need to update any
1954 nodes referring to them. Those are listed in /__local_fixups__. */
Julius Werner735ddc92019-05-07 17:05:28 -07001955 struct device_tree_node *local_fixups = dt_find_node_by_path(overlay,
1956 "/__local_fixups__", NULL, NULL, 0);
1957 if (local_fixups && dt_fixup_locals(overlay->root, local_fixups,
1958 phandle_base) < 0) {
Elyes HAOUAS8b5841e2022-02-08 21:23:12 +01001959 printk(BIOS_ERR, "invalid local fixups in overlay\n");
Julius Werner735ddc92019-05-07 17:05:28 -07001960 return -1;
1961 }
1962
Julius Werner23df4772019-05-17 22:50:18 -07001963 /*
1964 * Besides local phandle references (from nodes within the overlay to
1965 * other nodes within the overlay), the overlay may also contain phandle
1966 * references to the base tree. These are stored with invalid values and
1967 * must be updated now. /__symbols__ contains a list of all labels in
1968 * the base tree, and /__fixups__ describes all nodes in the overlay
1969 * that contain external phandle references.
1970 * We also take this opportunity to update all /fragment@X/__overlay__/
1971 * prefixes in the overlay's /__symbols__ node to the correct path that
1972 * the fragment will be placed in later, since this is the only step
1973 * where we have all necessary information for that easily available.
1974 */
Julius Werner735ddc92019-05-07 17:05:28 -07001975 struct device_tree_node *symbols = dt_find_node_by_path(tree,
1976 "/__symbols__", NULL, NULL, 0);
1977 struct device_tree_node *fixups = dt_find_node_by_path(overlay,
1978 "/__fixups__", NULL, NULL, 0);
1979 struct device_tree_node *overlay_symbols = dt_find_node_by_path(overlay,
1980 "/__symbols__", NULL, NULL, 0);
1981 if (fixups && dt_fixup_all_externals(tree, symbols, overlay,
1982 fixups, overlay_symbols) < 0) {
Elyes HAOUAS8b5841e2022-02-08 21:23:12 +01001983 printk(BIOS_ERR, "cannot match external fixups from overlay\n");
Julius Werner735ddc92019-05-07 17:05:28 -07001984 return -1;
1985 }
1986
Julius Werner23df4772019-05-17 22:50:18 -07001987 /* After all this fixing up, we can finally merge overlay into the tree
1988 (one fragment at a time, because for some reason it's split up). */
Julius Werner735ddc92019-05-07 17:05:28 -07001989 struct device_tree_node *fragment;
1990 list_for_each(fragment, overlay->root->children, list_node)
1991 if (dt_import_fragment(tree, fragment, overlay_symbols) < 0) {
Elyes HAOUAS8b5841e2022-02-08 21:23:12 +01001992 printk(BIOS_ERR, "bad DT fragment '%s'\n",
Julius Werner735ddc92019-05-07 17:05:28 -07001993 fragment->name);
1994 return -1;
1995 }
1996
Julius Werner23df4772019-05-17 22:50:18 -07001997 /*
1998 * We need to also update /__symbols__ to include labels from this
1999 * overlay, in case we want to load further overlays with external
2000 * phandle references to it. If the base tree already has a /__symbols__
2001 * we merge them together, otherwise we just insert the overlay's
2002 * /__symbols__ node into the base tree root.
2003 */
Julius Werner735ddc92019-05-07 17:05:28 -07002004 if (overlay_symbols) {
2005 if (symbols)
2006 dt_copy_subtree(symbols, overlay_symbols, 0);
2007 else
2008 list_insert_after(&overlay_symbols->list_node,
2009 &tree->root->children);
2010 }
2011
2012 return 0;
2013}