Debian Codegraph @

src/commonlib/device_tree.c

raw symbols SCIP
1 /* Taken from depthcharge: src/base/device_tree.c */
2 /* SPDX-License-Identifier: GPL-2.0-or-later */
3
4 #include <assert.h>
5 #include <commonlib/device_tree.h>
6 #include <ctype.h>
7 #include <endian.h>
8 #include <stdbool.h>
9 #include <stdint.h>
10 #ifdef __COREBOOT__
11 #include <console/console.h>
12 #else
13 #define printk(level, ...) printf(__VA_ARGS__)
14 #endif
15 #include <stdio.h>
16 #include <string.h>
17 #include <stddef.h>
18 #include <stdlib.h>
19 #include <limits.h>
20
21 #define FDT_PATH_MAX_DEPTH 10 // should be a good enough upper bound
22 #define FDT_PATH_MAX_LEN 128 // should be a good enough upper bound
23 #define FDT_MAX_MEMORY_NODES 4 // should be a good enough upper bound
24 #define FDT_MAX_MEMORY_REGIONS 16 // should be a good enough upper bound
25
26 /*
27 * libpayload's malloc() has a linear allocation complexity, which means that it
28 * degrades massively if we make a few thousand small allocations. Preventing
29 * that problem with a custom scratchpad is well-worth some increase in BSS
30 * size (64 * 2000 + 40 * 10000 = ~1/2 MB).
31 */
32
33 /* Try to give these a healthy margin above what the average kernel DT needs. */
34 #define LP_ALLOC_NODE_SCRATCH_COUNT 2000
35 #define LP_ALLOC_PROP_SCRATCH_COUNT 10000
36
37 static struct device_tree_node *alloc_node(void)
38 {
39 #ifndef __COREBOOT__
40 static struct device_tree_node scratch[LP_ALLOC_NODE_SCRATCH_COUNT];
41 static int counter = 0;
42
43 if (counter < ARRAY_SIZE(scratch))
44 return &scratch[counter++];
45 #endif
46 return xzalloc(sizeof(struct device_tree_node));
47 }
48
49 static struct device_tree_property *alloc_prop(void)
50 {
51 #ifndef __COREBOOT__
52 static struct device_tree_property scratch[LP_ALLOC_PROP_SCRATCH_COUNT];
53 static int counter = 0;
54
55 if (counter < ARRAY_SIZE(scratch))
56 return &scratch[counter++];
57 #endif
58 return xzalloc(sizeof(struct device_tree_property));
59 }
60
61
62 /*
63 * internal functions used by both unflattened and flattened device tree variants
64 */
65
66
67 static size_t read_reg_prop(struct fdt_property *prop, u32 addr_cells, u32 size_cells,
68 struct device_tree_region regions[], size_t regions_count)
69 {
70 // we found the reg property, no need to parse all regions in 'reg'
71 size_t count = prop->size / (4 * addr_cells + 4 * size_cells);
72 if (count > regions_count) {
73 printk(BIOS_ERR, "reg property has more entries (%zd) than regions array can hold (%zd)\n", count, regions_count);
74 count = regions_count;
75 }
76 if (addr_cells > 2 || size_cells > 2) {
77 printk(BIOS_ERR, "addr_cells (%d) or size_cells (%d) bigger than 2\n",
78 addr_cells, size_cells);
79 return 0;
80 }
81 uint32_t *ptr = prop->data;
82 for (int i = 0; i < count; i++) {
83 if (addr_cells == 1)
84 regions[i].addr = be32dec(ptr);
85 else if (addr_cells == 2)
86 regions[i].addr = be64dec(ptr);
87 ptr += addr_cells;
88 if (size_cells == 1)
89 regions[i].size = be32dec(ptr);
90 else if (size_cells == 2)
91 regions[i].size = be64dec(ptr);
92 ptr += size_cells;
93 }
94
95 return count; // return the number of regions found in the reg property
96 }
97
98
99 /*
100 * Functions for picking apart flattened trees.
101 */
102
103
104 static int fdt_skip_nops(const void *blob, uint32_t offset)
105 {
106 uint32_t *ptr = (uint32_t *)(((uint8_t *)blob) + offset);
107
108 int index = 0;
109 while (be32toh(ptr[index]) == FDT_TOKEN_NOP)
110 index++;
111
112 return index * sizeof(uint32_t);
113 }
114
115 int fdt_next_property(const void *blob, uint32_t offset,
116 struct fdt_property *prop)
117 {
118 struct fdt_header *header = (struct fdt_header *)blob;
119 uint32_t *ptr = (uint32_t *)(((uint8_t *)blob) + offset);
120
121 // skip NOP tokens
122 offset += fdt_skip_nops(blob, offset);
123
124 int index = 0;
125 if (be32toh(ptr[index++]) != FDT_TOKEN_PROPERTY)
126 return 0;
127
128 uint32_t size = be32toh(ptr[index++]);
129 uint32_t name_offset = be32toh(ptr[index++]);
130 name_offset += be32toh(header->strings_offset);
131
132 if (prop) {
133 prop->name = (char *)((uint8_t *)blob + name_offset);
134 prop->data = &ptr[index];
135 prop->size = size;
136 }
137
138 index += DIV_ROUND_UP(size, sizeof(uint32_t));
139
140 return index * sizeof(uint32_t);
141 }
142
143 /*
144 * fdt_next_node_name reads a node name
145 *
146 * @params blob address of FDT
147 * @params offset offset to the node to read the name from
148 * @params name parameter to hold the name that has been read or NULL
149 *
150 * @returns Either 0 on error or offset to the properties that come after the node name
151 */
152 int fdt_next_node_name(const void *blob, uint32_t offset, const char **name)
153 {
154 // skip NOP tokens
155 offset += fdt_skip_nops(blob, offset);
156
157 char *ptr = ((char *)blob) + offset;
158 if (be32dec(ptr) != FDT_TOKEN_BEGIN_NODE)
159 return 0;
160
161 ptr += 4;
162 if (name)
163 *name = ptr;
164
165 return ALIGN_UP(strlen(ptr) + 1, 4) + 4;
166 }
167
168 /*
169 * A utility function to skip past nodes in flattened trees.
170 */
171 int fdt_skip_node(const void *blob, uint32_t start_offset)
172 {
173 uint32_t offset = start_offset;
174
175 const char *name;
176 int size = fdt_next_node_name(blob, offset, &name);
177 if (!size)
178 return 0;
179 offset += size;
180
181 while ((size = fdt_next_property(blob, offset, NULL)))
182 offset += size;
183
184 while ((size = fdt_skip_node(blob, offset)))
185 offset += size;
186
187 // skip NOP tokens
188 offset += fdt_skip_nops(blob, offset);
189
190 return offset - start_offset + sizeof(uint32_t);
191 }
192
193 /*
194 * fdt_read_prop reads a property inside a node
195 *
196 * @params blob address of FDT
197 * @params node_offset offset to the node to read the property from
198 * @params prop_name name of the property to read
199 * @params fdt_prop property is saved inside this parameter
200 *
201 * @returns Either 0 if no property has been found or an offset that points to the location
202 * of the property
203 */
204 u32 fdt_read_prop(const void *blob, u32 node_offset, const char *prop_name,
205 struct fdt_property *fdt_prop)
206 {
207 u32 offset = node_offset;
208
209 offset += fdt_next_node_name(blob, offset, NULL); // skip node name
210
211 size_t size;
212 while ((size = fdt_next_property(blob, offset, fdt_prop))) {
213 if (strcmp(fdt_prop->name, prop_name) == 0)
214 return offset;
215 offset += size;
216 }
217 return 0; // property not found
218 }
219
220 /*
221 * fdt_read_reg_prop reads the reg property inside a node
222 *
223 * @params blob address of FDT
224 * @params node_offset offset to the node to read the reg property from
225 * @params addr_cells number of cells used for one address
226 * @params size_cells number of cells used for one size
227 * @params regions all regions that are read inside the reg property are saved inside
228 * this array
229 * @params regions_count maximum number of entries that can be saved inside the regions array.
230 *
231 * Returns: Either 0 on error or returns the number of regions put into the regions array.
232 */
233 u32 fdt_read_reg_prop(const void *blob, u32 node_offset, u32 addr_cells, u32 size_cells,
234 struct device_tree_region regions[], size_t regions_count)
235 {
236 struct fdt_property prop;
237 u32 offset = fdt_read_prop(blob, node_offset, "reg", &prop);
238
239 if (!offset) {
240 printk(BIOS_DEBUG, "no reg property found in node_offset: %x\n", node_offset);
241 return 0;
242 }
243
244 return read_reg_prop(&prop, addr_cells, size_cells, regions, regions_count);
245 }
246
247 static u32 fdt_read_cell_props(const void *blob, u32 node_offset, u32 *addrcp, u32 *sizecp)
248 {
249 struct fdt_property prop;
250 u32 offset = node_offset;
251 size_t size;
252 while ((size = fdt_next_property(blob, offset, &prop))) {
253 if (addrcp && !strcmp(prop.name, "#address-cells"))
254 *addrcp = be32dec(prop.data);
255 if (sizecp && !strcmp(prop.name, "#size-cells"))
256 *sizecp = be32dec(prop.data);
257 offset += size;
258 }
259 return offset;
260 }
261
262 uint64_t fdt_read_int_prop(struct fdt_property *prop, u32 cells)
263 {
264 if (cells == 0)
265 cells = prop->size / 4;
266
267 if (cells * 4 != prop->size) {
268 printk(BIOS_ERR, "FDT integer property of size %u @%p doesn't match expected cell count %u\n",
269 prop->size, prop->data, cells);
270 return 0;
271 }
272
273 if (cells == 2)
274 return be64dec(prop->data);
275 else if (cells == 1)
276 return be32dec(prop->data);
277
278 printk(BIOS_ERR, "Illegal FDT integer property size %u @%p\n", prop->size, prop);
279 return 0;
280 }
281
282 /*
283 * fdt_find_node searches for a node relative to another node
284 *
285 * @params blob address of FDT
286 *
287 * @params parent_node_offset offset to node from which to traverse the tree
288 *
289 * @params path null terminated array of node names specifying a
290 * relative path (e.g: { "cpus", "cpu0", NULL })
291 *
292 * @params addrcp/sizecp If any address-cells and size-cells properties are found that are
293 * part of the parent node of the node we are looking, addrcp and sizecp
294 * are set to these respectively.
295 *
296 * @returns: Either 0 if no node has been found or the offset to the node found
297 */
298 static u32 fdt_find_node(const void *blob, u32 parent_node_offset, char **path,
299 u32 *addrcp, u32 *sizecp)
300 {
301 if (*path == NULL)
302 return parent_node_offset; // node found
303
304 size_t size = fdt_next_node_name(blob, parent_node_offset, NULL); // skip node name
305
306 /*
307 * get address-cells and size-cells properties while skipping the others.
308 * According to spec address-cells and size-cells are not inherited, but we
309 * intentionally follow the Linux implementation here and treat them as inheritable.
310 */
311 u32 node_offset = fdt_read_cell_props(blob, parent_node_offset + size, addrcp, sizecp);
312
313 const char *node_name;
314 // walk all children nodes
315 while ((size = fdt_next_node_name(blob, node_offset, &node_name))) {
316 if (!strcmp(*path, node_name)) {
317 // traverse one level deeper into the path
318 return fdt_find_node(blob, node_offset, path + 1, addrcp, sizecp);
319 }
320 // node is not the correct one. skip current node
321 node_offset += fdt_skip_node(blob, node_offset);
322 }
323
324 // we have searched everything and could not find a fitting node
325 return 0;
326 }
327
328 /*
329 * fdt_find_node_by_path finds a node behind a given node path
330 *
331 * @params blob address of FDT
332 * @params path absolute path to the node that should be searched for
333 *
334 * @params addrcp/sizecp Pointer that will be updated with any #address-cells and #size-cells
335 * value found in the node of the node specified by node_offset. Either
336 * may be NULL to ignore. If no #address-cells and #size-cells is found
337 * default values of #address-cells=2 and #size-cells=1 are returned.
338 *
339 * @returns Either 0 on error or the offset to the node found behind the path
340 */
341 u32 fdt_find_node_by_path(const void *blob, const char *path, u32 *addrcp, u32 *sizecp)
342 {
343 // sanity check
344 if (path[0] != '/') {
345 printk(BIOS_ERR, "devicetree path must start with a /\n");
346 return 0;
347 }
348 if (!blob) {
349 printk(BIOS_ERR, "devicetree blob is NULL\n");
350 return 0;
351 }
352
353 if (addrcp)
354 *addrcp = 2;
355 if (sizecp)
356 *sizecp = 1;
357
358 struct fdt_header *fdt_hdr = (struct fdt_header *)blob;
359
360 /*
361 * split path into separate nodes
362 * e.g: "/cpus/cpu0" -> { "cpus", "cpu0" }
363 */
364 char *path_array[FDT_PATH_MAX_DEPTH];
365 size_t path_size = strlen(path);
366 assert(path_size < FDT_PATH_MAX_LEN);
367 char path_copy[FDT_PATH_MAX_LEN];
368 memcpy(path_copy, path, path_size + 1);
369 char *cur = path_copy;
370 int i;
371 for (i = 0; i < FDT_PATH_MAX_DEPTH; i++) {
372 path_array[i] = strtok_r(NULL, "/", &cur);
373 if (!path_array[i])
374 break;
375 }
376 assert(i < FDT_PATH_MAX_DEPTH);
377
378 return fdt_find_node(blob, be32toh(fdt_hdr->structure_offset), path_array, addrcp, sizecp);
379 }
380
381 /*
382 * fdt_find_subnodes_by_prefix finds a node with a given prefix relative to a parent node
383 *
384 * @params blob The FDT to search.
385 *
386 * @params node_offset offset to the node of which the children should be searched
387 *
388 * @params prefix A string to search for a node with a given prefix. This can for example
389 * be 'cpu' to look for all nodes matching this prefix. Only children of
390 * node_offset are searched. Therefore in order to search all nodes matching
391 * the 'cpu' prefix, node_offset should probably point to the 'cpus' node.
392 * An empty prefix ("") searches for all children nodes of node_offset.
393 *
394 * @params addrcp/sizecp Pointer that will be updated with any #address-cells and #size-cells
395 * value found in the node of the node specified by node_offset. Either
396 * may be NULL to ignore. If no #address-cells and #size-cells is found
397 * addrcp and sizecp are left untouched.
398 *
399 * @params results Array of offsets pointing to each node matching the given prefix.
400 * @params results_len Number of entries allocated for the 'results' array
401 *
402 * @returns offset to last node found behind path or 0 if no node has been found
403 */
404 size_t fdt_find_subnodes_by_prefix(const void *blob, u32 node_offset, const char *prefix,
405 u32 *addrcp, u32 *sizecp, u32 *results, size_t results_len)
406 {
407 // sanity checks
408 if (!blob || !results || !prefix) {
409 printk(BIOS_ERR, "%s: input parameter cannot be null/\n", __func__);
410 return 0;
411 }
412
413 u32 offset = node_offset;
414
415 // we don't care about the name of the current node
416 u32 size = fdt_next_node_name(blob, offset, NULL);
417 if (!size) {
418 printk(BIOS_ERR, "%s: node_offset: %x does not point to a node\n",
419 __func__, node_offset);
420 return 0;
421 }
422 offset += size;
423
424 /*
425 * update addrcp and sizecp if the node contains an address-cells and size-cells
426 * property. Otherwise use addrcp and sizecp provided by caller.
427 */
428 offset = fdt_read_cell_props(blob, offset, addrcp, sizecp);
429
430 size_t count_results = 0;
431 int prefix_len = strlen(prefix);
432 const char *node_name;
433 // walk all children nodes of offset
434 while ((size = fdt_next_node_name(blob, offset, &node_name))) {
435
436 // check if there is space left in the results array
437 if (count_results >= results_len)
438 break;
439
440 if (!strncmp(prefix, node_name, prefix_len)) {
441 // we found a node that matches the prefix
442 results[count_results++] = offset;
443 }
444 // node does not match the prefix. skip current node
445 offset += fdt_skip_node(blob, offset);
446 }
447
448 // return last occurrence
449 return count_results;
450 }
451
452 static const char *fdt_read_alias_prop(const void *blob, const char *alias_name)
453 {
454 u32 node_offset = fdt_find_node_by_path(blob, "/aliases", NULL, NULL);
455 if (!node_offset) {
456 printk(BIOS_DEBUG, "no /aliases node found\n");
457 return NULL;
458 }
459 struct fdt_property alias_prop;
460 if (!fdt_read_prop(blob, node_offset, alias_name, &alias_prop)) {
461 printk(BIOS_DEBUG, "property %s in /aliases node not found\n", alias_name);
462 return NULL;
463 }
464 return (const char *)alias_prop.data;
465 }
466
467 /*
468 * Find a node in the tree from a string device tree path.
469 *
470 * @params blob Address to the FDT
471 * @params alias_name node name alias that should be searched for.
472 * @params addrcp/sizecp Pointer that will be updated with any #address-cells and #size-cells
473 * value found in the node of the node specified by node_offset. Either
474 * may be NULL to ignore. If no #address-cells and #size-cells is found
475 * default values of #address-cells=2 and #size-cells=1 are returned.
476 *
477 * @returns offset to last node found behind path or 0 if no node has been found
478 */
479 u32 fdt_find_node_by_alias(const void *blob, const char *alias_name, u32 *addrcp, u32 *sizecp)
480 {
481 const char *node_name = fdt_read_alias_prop(blob, alias_name);
482 if (!node_name) {
483 printk(BIOS_DEBUG, "alias %s not found\n", alias_name);
484 return 0;
485 }
486
487 u32 node_offset = fdt_find_node_by_path(blob, node_name, addrcp, sizecp);
488 if (!node_offset) {
489 // This should not happen (invalid devicetree)
490 printk(BIOS_WARNING,
491 "Could not find node '%s', which alias was referring to '%s'\n",
492 node_name, alias_name);
493 return 0;
494 }
495 return node_offset;
496 }
497
498
499 /*
500 * Functions for printing flattened trees.
501 */
502
503 static void print_indent(int depth)
504 {
505 printk(BIOS_DEBUG, "%*s", depth * 8, "");
506 }
507
508 static void print_property(const struct fdt_property *prop, int depth)
509 {
510 int is_string = prop->size > 0 &&
511 ((char *)prop->data)[prop->size - 1] == '\0';
512
513 if (is_string) {
514 for (int i = 0; i < prop->size - 1; i++) {
515 if (!isprint(((char *)prop->data)[i])) {
516 is_string = 0;
517 break;
518 }
519 }
520 }
521
522 print_indent(depth);
523 if (is_string) {
524 printk(BIOS_DEBUG, "%s = \"%s\";\n",
525 prop->name, (const char *)prop->data);
526 } else {
527 printk(BIOS_DEBUG, "%s = < ", prop->name);
528 for (int i = 0; i < MIN(128, prop->size); i += 4) {
529 uint32_t val = 0;
530 for (int j = 0; j < MIN(4, prop->size - i); j++)
531 val |= ((uint8_t *)prop->data)[i + j] <<
532 (24 - j * 8);
533 printk(BIOS_DEBUG, "%#.2x ", val);
534 }
535 if (prop->size > 128)
536 printk(BIOS_DEBUG, "...");
537 printk(BIOS_DEBUG, ">;\n");
538 }
539 }
540
541 static int print_flat_node(const void *blob, uint32_t start_offset, int depth)
542 {
543 int offset = start_offset;
544 const char *name;
545 int size;
546
547 size = fdt_next_node_name(blob, offset, &name);
548 if (!size)
549 return 0;
550 offset += size;
551
552 print_indent(depth);
553 printk(BIOS_DEBUG, "%s {\n", name);
554
555 struct fdt_property prop;
556 while ((size = fdt_next_property(blob, offset, &prop))) {
557 print_property(&prop, depth + 1);
558
559 offset += size;
560 }
561
562 printk(BIOS_DEBUG, "\n"); /* empty line between props and nodes */
563
564 while ((size = print_flat_node(blob, offset, depth + 1)))
565 offset += size;
566
567 print_indent(depth);
568 printk(BIOS_DEBUG, "}\n");
569
570 return offset - start_offset + sizeof(uint32_t);
571 }
572
573 void fdt_print_node(const void *blob, uint32_t offset)
574 {
575 print_flat_node(blob, offset, 0);
576 }
577
578 /*
579 * fdt_read_memory_regions finds memory ranges from a flat device-tree
580 *
581 * @params blob address of FDT
582 * @params regions all regions that are read inside the reg property of
583 * memory nodes are saved inside this array
584 * @params regions_count maximum number of entries that can be saved inside
585 * the regions array.
586 *
587 * Returns: Either 0 on error or returns the number of regions put into the regions array.
588 */
589 size_t fdt_read_memory_regions(const void *blob,
590 struct device_tree_region regions[],
591 size_t regions_count)
592 {
593 u32 node, root, addrcp, sizecp;
594 u32 nodes[FDT_MAX_MEMORY_NODES] = {0};
595 size_t region_idx = 0;
596 size_t node_count = 0;
597
598 if (!fdt_is_valid(blob))
599 return 0;
600
601 node = fdt_find_node_by_path(blob, "/memory", &addrcp, &sizecp);
602 if (node) {
603 region_idx += fdt_read_reg_prop(blob, node, addrcp, sizecp,
604 regions, regions_count);
605 if (region_idx >= regions_count) {
606 printk(BIOS_WARNING, "FDT: Too many memory regions\n");
607 goto out;
608 }
609 }
610
611 root = fdt_find_node_by_path(blob, "/", &addrcp, &sizecp);
612 node_count = fdt_find_subnodes_by_prefix(blob, root, "memory@",
613 &addrcp, &sizecp, nodes,
614 FDT_MAX_MEMORY_NODES);
615 if (node_count >= FDT_MAX_MEMORY_NODES) {
616 printk(BIOS_WARNING, "FDT: Too many memory nodes\n");
617 /* Can still reading the regions for those we got */
618 }
619
620 for (size_t i = 0; i < MIN(node_count, FDT_MAX_MEMORY_NODES); i++) {
621 region_idx += fdt_read_reg_prop(blob, nodes[i], addrcp, sizecp,
622 &regions[region_idx],
623 regions_count - region_idx);
624 if (region_idx >= regions_count) {
625 printk(BIOS_WARNING, "FDT: Too many memory regions\n");
626 goto out;
627 }
628 }
629
630 out:
631 for (size_t i = 0; i < MIN(region_idx, regions_count); i++) {
632 printk(BIOS_DEBUG, "FDT: Memory region [%#llx - %#llx]\n",
633 regions[i].addr, regions[i].addr + regions[i].size);
634 }
635
636 return region_idx;
637 }
638
639 /*
640 * fdt_get_memory_top finds top of memory from a flat device-tree
641 *
642 * @params blob address of FDT
643 *
644 * Returns: Either 0 on error or returns the maximum memory address
645 */
646 uint64_t fdt_get_memory_top(const void *blob)
647 {
648 struct device_tree_region regions[FDT_MAX_MEMORY_REGIONS] = {0};
649 uint64_t top = 0;
650 uint64_t total = 0;
651 size_t count;
652
653 if (!fdt_is_valid(blob))
654 return 0;
655
656 count = fdt_read_memory_regions(blob, regions, FDT_MAX_MEMORY_REGIONS);
657 for (size_t i = 0; i < MIN(count, FDT_MAX_MEMORY_REGIONS); i++) {
658 top = MAX(top, regions[i].addr + regions[i].size);
659 total += regions[i].size;
660 }
661
662 printk(BIOS_DEBUG, "FDT: Found %u MiB of RAM\n",
663 (uint32_t)(total / MiB));
664
665 return top;
666 }
667
668 /*
669 * Functions to turn a flattened tree into an unflattened one.
670 */
671
672 static int dt_prop_is_phandle(struct device_tree_property *prop)
673 {
674 return !(strcmp("phandle", prop->prop.name) &&
675 strcmp("linux,phandle", prop->prop.name));
676 }
677
678 static int fdt_unflatten_node(const void *blob, uint32_t start_offset,
679 struct device_tree *tree,
680 struct device_tree_node **new_node)
681 {
682 struct list_node *last;
683 int offset = start_offset;
684 const char *name;
685 int size;
686
687 size = fdt_next_node_name(blob, offset, &name);
688 if (!size)
689 return 0;
690 offset += size;
691
692 struct device_tree_node *node = alloc_node();
693 *new_node = node;
694 node->name = name;
695
696 struct fdt_property fprop;
697 last = &node->properties;
698 while ((size = fdt_next_property(blob, offset, &fprop))) {
699 struct device_tree_property *prop = alloc_prop();
700 prop->prop = fprop;
701
702 if (dt_prop_is_phandle(prop)) {
703 node->phandle = be32dec(prop->prop.data);
704 if (node->phandle > tree->max_phandle)
705 tree->max_phandle = node->phandle;
706 }
707
708 list_insert_after(&prop->list_node, last);
709 last = &prop->list_node;
710
711 offset += size;
712 }
713
714 struct device_tree_node *child;
715 last = &node->children;
716 while ((size = fdt_unflatten_node(blob, offset, tree, &child))) {
717 list_insert_after(&child->list_node, last);
718 last = &child->list_node;
719
720 offset += size;
721 }
722
723 return offset - start_offset + sizeof(uint32_t);
724 }
725
726 static int fdt_unflatten_map_entry(const void *blob, uint32_t offset,
727 struct device_tree_reserve_map_entry **new)
728 {
729 const uint64_t *ptr = (const uint64_t *)(((uint8_t *)blob) + offset);
730 const uint64_t start = be64toh(ptr[0]);
731 const uint64_t size = be64toh(ptr[1]);
732
733 if (!size)
734 return 0;
735
736 struct device_tree_reserve_map_entry *entry = xzalloc(sizeof(*entry));
737 *new = entry;
738 entry->start = start;
739 entry->size = size;
740
741 return sizeof(uint64_t) * 2;
742 }
743
744 bool fdt_is_valid(const void *blob)
745 {
746 const struct fdt_header *header = (const struct fdt_header *)blob;
747
748 uint32_t magic = be32toh(header->magic);
749 uint32_t version = be32toh(header->version);
750 uint32_t last_comp_version = be32toh(header->last_comp_version);
751
752 if (magic != FDT_HEADER_MAGIC) {
753 printk(BIOS_ERR, "Invalid device tree magic %#.8x!\n", magic);
754 return false;
755 }
756 if (last_comp_version > FDT_SUPPORTED_VERSION) {
757 printk(BIOS_ERR, "Unsupported device tree version %u(>=%u)\n",
758 version, last_comp_version);
759 return false;
760 }
761 if (version > FDT_SUPPORTED_VERSION)
762 printk(BIOS_NOTICE, "FDT version %u too new, should add support!\n",
763 version);
764 return true;
765 }
766
767 struct device_tree *fdt_unflatten(const void *blob)
768 {
769 if (!fdt_is_valid(blob))
770 return NULL;
771
772 const struct fdt_header *header = blob;
773 struct device_tree *tree = xzalloc(sizeof(*tree));
774 tree->header = header;
775
776 uint32_t struct_offset = be32toh(header->structure_offset);
777 uint32_t strings_offset = be32toh(header->strings_offset);
778 uint32_t reserve_offset = be32toh(header->reserve_map_offset);
779 uint32_t min_offset = 0;
780 min_offset = MIN(struct_offset, strings_offset);
781 min_offset = MIN(min_offset, reserve_offset);
782 /* Assume everything up to the first non-header component is part of
783 the header and needs to be preserved. This will protect us against
784 new elements being added in the future. */
785 tree->header_size = min_offset;
786
787 struct device_tree_reserve_map_entry *entry;
788 uint32_t offset = reserve_offset;
789 int size;
790 struct list_node *last = &tree->reserve_map;
791 while ((size = fdt_unflatten_map_entry(blob, offset, &entry))) {
792 list_insert_after(&entry->list_node, last);
793 last = &entry->list_node;
794
795 offset += size;
796 }
797
798 fdt_unflatten_node(blob, struct_offset, tree, &tree->root);
799
800 return tree;
801 }
802
803
804
805 /*
806 * Functions to find the size of the device tree if it was flattened.
807 */
808
809 static void dt_flat_prop_size(struct device_tree_property *prop,
810 uint32_t *struct_size, uint32_t *strings_size)
811 {
812 /* Starting token. */
813 *struct_size += sizeof(uint32_t);
814 /* Size. */
815 *struct_size += sizeof(uint32_t);
816 /* Name offset. */
817 *struct_size += sizeof(uint32_t);
818 /* Property value. */
819 *struct_size += ALIGN_UP(prop->prop.size, sizeof(uint32_t));
820
821 /* Property name. */
822 *strings_size += strlen(prop->prop.name) + 1;
823 }
824
825 static void dt_flat_node_size(struct device_tree_node *node,
826 uint32_t *struct_size, uint32_t *strings_size)
827 {
828 /* Starting token. */
829 *struct_size += sizeof(uint32_t);
830 /* Node name. */
831 *struct_size += ALIGN_UP(strlen(node->name) + 1, sizeof(uint32_t));
832
833 struct device_tree_property *prop;
834 list_for_each(prop, node->properties, list_node)
835 dt_flat_prop_size(prop, struct_size, strings_size);
836
837 struct device_tree_node *child;
838 list_for_each(child, node->children, list_node)
839 dt_flat_node_size(child, struct_size, strings_size);
840
841 /* End token. */
842 *struct_size += sizeof(uint32_t);
843 }
844
845 uint32_t dt_flat_size(const struct device_tree *tree)
846 {
847 uint32_t size = tree->header_size;
848 struct device_tree_reserve_map_entry *entry;
849 list_for_each(entry, tree->reserve_map, list_node)
850 size += sizeof(uint64_t) * 2;
851 size += sizeof(uint64_t) * 2;
852
853 uint32_t struct_size = 0;
854 uint32_t strings_size = 0;
855 dt_flat_node_size(tree->root, &struct_size, &strings_size);
856
857 size += struct_size;
858 /* End token. */
859 size += sizeof(uint32_t);
860
861 size += strings_size;
862
863 return size;
864 }
865
866
867
868 /*
869 * Functions to flatten a device tree.
870 */
871
872 static void dt_flatten_map_entry(struct device_tree_reserve_map_entry *entry,
873 void **map_start)
874 {
875 ((uint64_t *)*map_start)[0] = htobe64(entry->start);
876 ((uint64_t *)*map_start)[1] = htobe64(entry->size);
877 *map_start = ((uint8_t *)*map_start) + sizeof(uint64_t) * 2;
878 }
879
880 static void dt_flatten_prop(struct device_tree_property *prop,
881 void **struct_start, void *strings_base,
882 void **strings_start)
883 {
884 uint8_t *dstruct = (uint8_t *)*struct_start;
885 uint8_t *dstrings = (uint8_t *)*strings_start;
886
887 be32enc(dstruct, FDT_TOKEN_PROPERTY);
888 dstruct += sizeof(uint32_t);
889
890 be32enc(dstruct, prop->prop.size);
891 dstruct += sizeof(uint32_t);
892
893 uint32_t name_offset = (uintptr_t)dstrings - (uintptr_t)strings_base;
894 be32enc(dstruct, name_offset);
895 dstruct += sizeof(uint32_t);
896
897 strcpy((char *)dstrings, prop->prop.name);
898 dstrings += strlen(prop->prop.name) + 1;
899
900 memcpy(dstruct, prop->prop.data, prop->prop.size);
901 dstruct += ALIGN_UP(prop->prop.size, sizeof(uint32_t));
902
903 *struct_start = dstruct;
904 *strings_start = dstrings;
905 }
906
907 static void dt_flatten_node(const struct device_tree_node *node,
908 void **struct_start, void *strings_base,
909 void **strings_start)
910 {
911 uint8_t *dstruct = (uint8_t *)*struct_start;
912 uint8_t *dstrings = (uint8_t *)*strings_start;
913
914 be32enc(dstruct, FDT_TOKEN_BEGIN_NODE);
915 dstruct += sizeof(uint32_t);
916
917 strcpy((char *)dstruct, node->name);
918 dstruct += ALIGN_UP(strlen(node->name) + 1, sizeof(uint32_t));
919
920 struct device_tree_property *prop;
921 list_for_each(prop, node->properties, list_node)
922 dt_flatten_prop(prop, (void **)&dstruct, strings_base,
923 (void **)&dstrings);
924
925 struct device_tree_node *child;
926 list_for_each(child, node->children, list_node)
927 dt_flatten_node(child, (void **)&dstruct, strings_base,
928 (void **)&dstrings);
929
930 be32enc(dstruct, FDT_TOKEN_END_NODE);
931 dstruct += sizeof(uint32_t);
932
933 *struct_start = dstruct;
934 *strings_start = dstrings;
935 }
936
937 void dt_flatten(const struct device_tree *tree, void *start_dest)
938 {
939 uint8_t *dest = (uint8_t *)start_dest;
940
941 memcpy(dest, tree->header, tree->header_size);
942 struct fdt_header *header = (struct fdt_header *)dest;
943 dest += tree->header_size;
944
945 struct device_tree_reserve_map_entry *entry;
946 list_for_each(entry, tree->reserve_map, list_node)
947 dt_flatten_map_entry(entry, (void **)&dest);
948 ((uint64_t *)dest)[0] = ((uint64_t *)dest)[1] = 0;
949 dest += sizeof(uint64_t) * 2;
950
951 uint32_t struct_size = 0;
952 uint32_t strings_size = 0;
953 dt_flat_node_size(tree->root, &struct_size, &strings_size);
954
955 uint8_t *struct_start = dest;
956 header->structure_offset = htobe32(dest - (uint8_t *)start_dest);
957 be32enc(&dest[struct_size], FDT_TOKEN_END);
958 struct_size += sizeof(uint32_t);
959 header->structure_size = htobe32(struct_size);
960 dest += struct_size;
961
962 uint8_t *strings_start = dest;
963 header->strings_offset = htobe32(dest - (uint8_t *)start_dest);
964 header->strings_size = htobe32(strings_size);
965 dest += strings_size;
966
967 dt_flatten_node(tree->root, (void **)&struct_start, strings_start,
968 (void **)&strings_start);
969
970 header->totalsize = htobe32(dest - (uint8_t *)start_dest);
971 }
972
973
974
975 /*
976 * Functions for printing a non-flattened device tree.
977 */
978
979 static void print_node(const struct device_tree_node *node, int depth)
980 {
981 print_indent(depth);
982 if (depth == 0) /* root node has no name, print a starting slash */
983 printk(BIOS_DEBUG, "/");
984 printk(BIOS_DEBUG, "%s {\n", node->name);
985
986 struct device_tree_property *prop;
987 list_for_each(prop, node->properties, list_node)
988 print_property(&prop->prop, depth + 1);
989
990 printk(BIOS_DEBUG, "\n"); /* empty line between props and nodes */
991
992 struct device_tree_node *child;
993 list_for_each(child, node->children, list_node)
994 print_node(child, depth + 1);
995
996 print_indent(depth);
997 printk(BIOS_DEBUG, "};\n");
998 }
999
1000 void dt_print_node(const struct device_tree_node *node)
1001 {
1002 print_node(node, 0);
1003 }
1004
1005
1006
1007 /*
1008 * Functions for reading and manipulating an unflattened device tree.
1009 */
1010
1011 /*
1012 * dt_read_reg_prop reads the reg property inside a node
1013 *
1014 * @params node device tree node to read reg property from
1015 * @params addr_cells number of cells used for one address
1016 * @params size_cells number of cells used for one size
1017 * @params regions all regions that are read inside the reg property are saved inside
1018 * this array
1019 * @params regions_count maximum number of entries that can be saved inside the regions array.
1020 *
1021 * Returns: Either 0 on error or returns the number of regions put into the regions array.
1022 */
1023 size_t dt_read_reg_prop(struct device_tree_node *node, u32 addr_cells, u32 size_cells,
1024 struct device_tree_region regions[], size_t regions_count)
1025 {
1026 struct device_tree_property *prop;
1027 bool found = false;
1028 list_for_each(prop, node->properties, list_node) {
1029 if (!strcmp("reg", prop->prop.name)) {
1030 found = true;
1031 break;
1032 }
1033 }
1034
1035 if (!found) {
1036 printk(BIOS_DEBUG, "no reg property found\n");
1037 return 0;
1038 }
1039 return read_reg_prop(&prop->prop, addr_cells, size_cells, regions, regions_count);
1040 }
1041
1042 /*
1043 * Read #address-cells and #size-cells properties from a node.
1044 *
1045 * @param node The device tree node to read from.
1046 * @param addrcp Pointer to store #address-cells in, skipped if NULL.
1047 * @param sizecp Pointer to store #size-cells in, skipped if NULL.
1048 */
1049 void dt_read_cell_props(const struct device_tree_node *node, u32 *addrcp,
1050 u32 *sizecp)
1051 {
1052 struct device_tree_property *prop;
1053 list_for_each(prop, node->properties, list_node) {
1054 if (addrcp && !strcmp("#address-cells", prop->prop.name))
1055 *addrcp = be32dec(prop->prop.data);
1056 if (sizecp && !strcmp("#size-cells", prop->prop.name))
1057 *sizecp = be32dec(prop->prop.data);
1058 }
1059 }
1060
1061 static struct device_tree_node *_dt_find_node(struct device_tree_node *parent,
1062 const char *const *path, u32 *addrcp,
1063 u32 *sizecp, int create)
1064 {
1065 struct device_tree_node *node, *found = NULL;
1066
1067 if (!*path)
1068 return parent;
1069
1070 /* Update #address-cells and #size-cells for the parent level (cells
1071 properties always count for the direct children of their node). */
1072 if (addrcp || sizecp)
1073 dt_read_cell_props(parent, addrcp, sizecp);
1074
1075 /* Find the next node in the path, if it exists. */
1076 list_for_each(node, parent->children, list_node) {
1077 if (!strcmp(node->name, *path)) {
1078 found = node;
1079 break;
1080 }
1081 }
1082
1083 /* Otherwise create it or return NULL. */
1084 if (!found) {
1085 if (!create)
1086 return NULL;
1087
1088 found = alloc_node();
1089 found->name = strdup(*path);
1090 if (!found->name)
1091 return NULL;
1092
1093 list_insert_after(&found->list_node, &parent->children);
1094 }
1095
1096 return _dt_find_node(found, path + 1, addrcp, sizecp, create);
1097 }
1098
1099 /*
1100 * Find a node from a device tree path, relative to a parent node.
1101 *
1102 * @param parent The node from which to start the relative path lookup.
1103 * @param path An array of path component strings that will be looked
1104 * up in order to find the node. Must be terminated with
1105 * a NULL pointer. Example: {'firmware', 'coreboot', NULL}
1106 * @param addrcp Pointer that will be updated with any #address-cells
1107 * value found in the path. May be NULL to ignore.
1108 * @param sizecp Pointer that will be updated with any #size-cells
1109 * value found in the path. May be NULL to ignore.
1110 * @param create 1: Create node(s) if not found. 0: Return NULL instead.
1111 * @return The found/created node, or NULL.
1112 */
1113 struct device_tree_node *dt_find_node(struct device_tree_node *parent,
1114 const char *const *path, u32 *addrcp,
1115 u32 *sizecp, int create)
1116 {
1117 /* Initialize cells to default values according to FDT spec. */
1118 if (addrcp)
1119 *addrcp = 2;
1120
1121 if (sizecp)
1122 *sizecp = 1;
1123
1124 return _dt_find_node(parent, path, addrcp, sizecp, create);
1125 }
1126
1127 /*
1128 * Find a node in the tree from a string device tree path.
1129 *
1130 * @param tree The device tree to search.
1131 * @param path A string representing a path in the device tree, with
1132 * nodes separated by '/'. Example: "/firmware/coreboot"
1133 * @param addrcp Pointer that will be updated with any #address-cells
1134 * value found in the path. May be NULL to ignore.
1135 * @param sizecp Pointer that will be updated with any #size-cells
1136 * value found in the path. May be NULL to ignore.
1137 * @param create 1: Create node(s) if not found. 0: Return NULL instead.
1138 * @return The found/created node, or NULL.
1139 *
1140 * It is the caller responsibility to provide a path string that doesn't end
1141 * with a '/' and doesn't contain any "//". If the path does not start with a
1142 * '/', the first segment is interpreted as an alias. */
1143 struct device_tree_node *dt_find_node_by_path(struct device_tree *tree,
1144 const char *path, u32 *addrcp,
1145 u32 *sizecp, int create)
1146 {
1147 char *sub_path;
1148 char *duped_str;
1149 struct device_tree_node *parent;
1150 char *next_slash;
1151 /* Hopefully enough depth for any node. */
1152 const char *path_array[15];
1153 int i;
1154 struct device_tree_node *node = NULL;
1155
1156 if (path[0] == '/') { /* regular path */
1157 if (path[1] == '\0') { /* special case: "/" is root node */
1158 if (addrcp || sizecp)
1159 dt_read_cell_props(tree->root, addrcp, sizecp);
1160 return tree->root;
1161 }
1162
1163 sub_path = duped_str = strdup(&path[1]);
1164 if (!sub_path)
1165 return NULL;
1166
1167 parent = tree->root;
1168 } else { /* alias */
1169 char *alias;
1170
1171 alias = duped_str = strdup(path);
1172 if (!alias)
1173 return NULL;
1174
1175 sub_path = strchr(alias, '/');
1176 if (sub_path)
1177 *sub_path = '\0';
1178
1179 parent = dt_find_node_by_alias(tree, alias);
1180 if (!parent) {
1181 printk(BIOS_DEBUG,
1182 "Could not find node '%s', alias '%s' does not exist\n",
1183 path, alias);
1184 free(duped_str);
1185 return NULL;
1186 }
1187
1188 if (!sub_path) {
1189 /* it's just the alias, no sub-path */
1190 free(duped_str);
1191 return parent;
1192 }
1193
1194 sub_path++;
1195 }
1196
1197 next_slash = sub_path;
1198 path_array[0] = sub_path;
1199 for (i = 1; i < (ARRAY_SIZE(path_array) - 1); i++) {
1200 next_slash = strchr(next_slash, '/');
1201 if (!next_slash)
1202 break;
1203
1204 *next_slash++ = '\0';
1205 path_array[i] = next_slash;
1206 }
1207
1208 if (!next_slash) {
1209 path_array[i] = NULL;
1210 node = dt_find_node(parent, path_array,
1211 addrcp, sizecp, create);
1212 }
1213
1214 free(duped_str);
1215 return node;
1216 }
1217
1218 /*
1219 * Find a node from an alias
1220 *
1221 * @param tree The device tree.
1222 * @param alias The alias name.
1223 * @return The found node, or NULL.
1224 */
1225 struct device_tree_node *dt_find_node_by_alias(struct device_tree *tree,
1226 const char *alias)
1227 {
1228 struct device_tree_node *node;
1229 const char *alias_path;
1230
1231 node = dt_find_node_by_path(tree, "/aliases", NULL, NULL, 0);
1232 if (!node)
1233 return NULL;
1234
1235 alias_path = dt_find_string_prop(node, alias);
1236 if (!alias_path)
1237 return NULL;
1238
1239 return dt_find_node_by_path(tree, alias_path, NULL, NULL, 0);
1240 }
1241
1242 struct device_tree_node *dt_find_node_by_phandle(struct device_tree_node *root,
1243 uint32_t phandle)
1244 {
1245 if (!root)
1246 return NULL;
1247
1248 if (root->phandle == phandle)
1249 return root;
1250
1251 struct device_tree_node *node;
1252 struct device_tree_node *result;
1253 list_for_each(node, root->children, list_node) {
1254 result = dt_find_node_by_phandle(node, phandle);
1255 if (result)
1256 return result;
1257 }
1258
1259 return NULL;
1260 }
1261
1262 /*
1263 * Check if given node is compatible.
1264 *
1265 * @param node The node which is to be checked for compatible property.
1266 * @param compat The compatible string to match.
1267 * @return 1 = compatible, 0 = not compatible.
1268 */
1269 static int dt_check_compat_match(struct device_tree_node *node,
1270 const char *compat)
1271 {
1272 struct device_tree_property *prop;
1273
1274 list_for_each(prop, node->properties, list_node) {
1275 if (!strcmp("compatible", prop->prop.name)) {
1276 size_t bytes = prop->prop.size;
1277 const char *str = prop->prop.data;
1278 while (bytes > 0) {
1279 if (!strncmp(compat, str, bytes))
1280 return 1;
1281 size_t len = strnlen(str, bytes) + 1;
1282 if (bytes <= len)
1283 break;
1284 str += len;
1285 bytes -= len;
1286 }
1287 break;
1288 }
1289 }
1290
1291 return 0;
1292 }
1293
1294 /*
1295 * Find a node from a compatible string, in the subtree of a parent node.
1296 *
1297 * @param parent The parent node under which to look.
1298 * @param compat The compatible string to find.
1299 * @return The found node, or NULL.
1300 */
1301 struct device_tree_node *dt_find_compat(struct device_tree_node *parent,
1302 const char *compat)
1303 {
1304 /* Check if the parent node itself is compatible. */
1305 if (dt_check_compat_match(parent, compat))
1306 return parent;
1307
1308 struct device_tree_node *child;
1309 list_for_each(child, parent->children, list_node) {
1310 struct device_tree_node *found = dt_find_compat(child, compat);
1311 if (found)
1312 return found;
1313 }
1314
1315 return NULL;
1316 }
1317
1318 /*
1319 * Find the next compatible child of a given parent. All children up to the
1320 * child passed in by caller are ignored. If child is NULL, it considers all the
1321 * children to find the first child which is compatible.
1322 *
1323 * @param parent The parent node under which to look.
1324 * @param child The child node to start search from (exclusive). If NULL
1325 * consider all children.
1326 * @param compat The compatible string to find.
1327 * @return The found node, or NULL.
1328 */
1329 struct device_tree_node *
1330 dt_find_next_compat_child(struct device_tree_node *parent,
1331 struct device_tree_node *child,
1332 const char *compat)
1333 {
1334 struct device_tree_node *next;
1335 int ignore = 0;
1336
1337 if (child)
1338 ignore = 1;
1339
1340 list_for_each(next, parent->children, list_node) {
1341 if (ignore) {
1342 if (child == next)
1343 ignore = 0;
1344 continue;
1345 }
1346
1347 if (dt_check_compat_match(next, compat))
1348 return next;
1349 }
1350
1351 return NULL;
1352 }
1353
1354 /*
1355 * Find a node with matching property value, in the subtree of a parent node.
1356 *
1357 * @param parent The parent node under which to look.
1358 * @param name The property name to look for.
1359 * @param data The property value to look for.
1360 * @param size The property size.
1361 */
1362 struct device_tree_node *dt_find_prop_value(struct device_tree_node *parent,
1363 const char *name, void *data,
1364 size_t size)
1365 {
1366 struct device_tree_property *prop;
1367
1368 /* Check if parent itself has the required property value. */
1369 list_for_each(prop, parent->properties, list_node) {
1370 if (!strcmp(name, prop->prop.name)) {
1371 size_t bytes = prop->prop.size;
1372 const void *prop_data = prop->prop.data;
1373 if (size != bytes)
1374 break;
1375 if (!memcmp(data, prop_data, size))
1376 return parent;
1377 break;
1378 }
1379 }
1380
1381 struct device_tree_node *child;
1382 list_for_each(child, parent->children, list_node) {
1383 struct device_tree_node *found = dt_find_prop_value(child, name,
1384 data, size);
1385 if (found)
1386 return found;
1387 }
1388 return NULL;
1389 }
1390
1391 /*
1392 * Write an arbitrary sized big-endian integer into a pointer.
1393 *
1394 * @param dest Pointer to the DT property data buffer to write.
1395 * @param src The integer to write (in CPU endianness).
1396 * @param length the length of the destination integer in bytes.
1397 */
1398 void dt_write_int(u8 *dest, u64 src, size_t length)
1399 {
1400 while (length--) {
1401 dest[length] = (u8)src;
1402 src >>= 8;
1403 }
1404 }
1405
1406 /*
1407 * Delete a property by name in a given node if it exists.
1408 *
1409 * @param node The device tree node to operate on.
1410 * @param name The name of the property to delete.
1411 */
1412 void dt_delete_prop(struct device_tree_node *node, const char *name)
1413 {
1414 struct device_tree_property *prop;
1415
1416 list_for_each(prop, node->properties, list_node) {
1417 if (!strcmp(prop->prop.name, name)) {
1418 list_remove(&prop->list_node);
1419 return;
1420 }
1421 }
1422 }
1423
1424 /*
1425 * Add an arbitrary property to a node, or update it if it already exists.
1426 *
1427 * @param node The device tree node to add to.
1428 * @param name The name of the new property.
1429 * @param data The raw data blob to be stored in the property.
1430 * @param size The size of data in bytes.
1431 */
1432 void dt_add_bin_prop(struct device_tree_node *node, const char *name,
1433 void *data, size_t size)
1434 {
1435 struct device_tree_property *prop;
1436
1437 list_for_each(prop, node->properties, list_node) {
1438 if (!strcmp(prop->prop.name, name)) {
1439 prop->prop.data = data;
1440 prop->prop.size = size;
1441 return;
1442 }
1443 }
1444
1445 prop = alloc_prop();
1446 list_insert_after(&prop->list_node, &node->properties);
1447 prop->prop.name = name;
1448 prop->prop.data = data;
1449 prop->prop.size = size;
1450 }
1451
1452 /*
1453 * Find given string property in a node and return its content.
1454 *
1455 * @param node The device tree node to search.
1456 * @param name The name of the property.
1457 * @return The found string, or NULL.
1458 */
1459 const char *dt_find_string_prop(const struct device_tree_node *node,
1460 const char *name)
1461 {
1462 const void *content;
1463 size_t size;
1464
1465 dt_find_bin_prop(node, name, &content, &size);
1466
1467 return content;
1468 }
1469
1470 /*
1471 * Find given property in a node.
1472 *
1473 * @param node The device tree node to search.
1474 * @param name The name of the property.
1475 * @param data Pointer to return raw data blob in the property.
1476 * @param size Pointer to return the size of data in bytes.
1477 */
1478 void dt_find_bin_prop(const struct device_tree_node *node, const char *name,
1479 const void **data, size_t *size)
1480 {
1481 struct device_tree_property *prop;
1482
1483 *data = NULL;
1484 *size = 0;
1485
1486 list_for_each(prop, node->properties, list_node) {
1487 if (!strcmp(prop->prop.name, name)) {
1488 *data = prop->prop.data;
1489 *size = prop->prop.size;
1490 return;
1491 }
1492 }
1493 }
1494
1495 /*
1496 * Add a string property to a node, or update it if it already exists.
1497 *
1498 * @param node The device tree node to add to.
1499 * @param name The name of the new property.
1500 * @param str The zero-terminated string to be stored in the property.
1501 */
1502 void dt_add_string_prop(struct device_tree_node *node, const char *name,
1503 const char *str)
1504 {
1505 dt_add_bin_prop(node, name, (char *)str, strlen(str) + 1);
1506 }
1507
1508 /*
1509 * Add a 32-bit integer property to a node, or update it if it already exists.
1510 *
1511 * @param node The device tree node to add to.
1512 * @param name The name of the new property.
1513 * @param val The integer to be stored in the property.
1514 */
1515 void dt_add_u32_prop(struct device_tree_node *node, const char *name, u32 val)
1516 {
1517 u32 *val_ptr = xmalloc(sizeof(val));
1518 *val_ptr = htobe32(val);
1519 dt_add_bin_prop(node, name, val_ptr, sizeof(*val_ptr));
1520 }
1521
1522 /*
1523 * Add a 64-bit integer property to a node, or update it if it already exists.
1524 *
1525 * @param node The device tree node to add to.
1526 * @param name The name of the new property.
1527 * @param val The integer to be stored in the property.
1528 */
1529 void dt_add_u64_prop(struct device_tree_node *node, const char *name, u64 val)
1530 {
1531 u64 *val_ptr = xmalloc(sizeof(val));
1532 *val_ptr = htobe64(val);
1533 dt_add_bin_prop(node, name, val_ptr, sizeof(*val_ptr));
1534 }
1535
1536 static void dt_add_addr_and_size_prop(struct device_tree_node *node, const char *prop_name,
1537 const u64 *addrs, const u64 *sizes, int count,
1538 u32 addr_cells, u32 size_cells)
1539 {
1540 int i;
1541 size_t length = (size_t)(addr_cells + size_cells) * sizeof(u32) * count;
1542 u8 *data = xmalloc(length);
1543 u8 *cur = data;
1544
1545 for (i = 0; i < count; i++) {
1546 dt_write_int(cur, addrs[i], addr_cells * sizeof(u32));
1547 cur += addr_cells * sizeof(u32);
1548 dt_write_int(cur, sizes[i], size_cells * sizeof(u32));
1549 cur += size_cells * sizeof(u32);
1550 }
1551
1552 dt_add_bin_prop(node, prop_name, data, length);
1553 }
1554
1555 /*
1556 * Add a 'reg' address list property to a node, or update it if it exists.
1557 *
1558 * @param node The device tree node to add to.
1559 * @param regions Array of address values to be stored in the property.
1560 * @param sizes Array of corresponding size values to 'addrs'.
1561 * @param count Number of values in 'addrs' and 'sizes' (must be equal).
1562 * @param addr_cells Value of #address-cells property valid for this node.
1563 * @param size_cells Value of #size-cells property valid for this node.
1564 */
1565 void dt_add_reg_prop(struct device_tree_node *node, const u64 *addrs, const u64 *sizes,
1566 int count, u32 addr_cells, u32 size_cells)
1567 {
1568 dt_add_addr_and_size_prop(node, "reg", addrs, sizes, count, addr_cells, size_cells);
1569 }
1570
1571 /*
1572 * Add a 'iommu-addresses' address list property to a node, or update it if it exists.
1573 *
1574 * @param node The device tree node to add to.
1575 * @param regions Array of address values to be stored in the property.
1576 * @param sizes Array of corresponding size values to 'addrs'.
1577 * @param count Number of values in 'addrs' and 'sizes' (must be equal).
1578 * @param addr_cells Value of #address-cells property valid for this node.
1579 * @param size_cells Value of #size-cells property valid for this node.
1580 */
1581 void dt_add_iommu_addr_prop(struct device_tree_node *node, const u64 *addrs, const u64 *sizes,
1582 int count, u32 addr_cells, u32 size_cells)
1583 {
1584 dt_add_addr_and_size_prop(node, "iommu-addresses", addrs, sizes, count, addr_cells,
1585 size_cells);
1586 }
1587
1588 /*
1589 * Fixups to apply to a kernel's device tree before booting it.
1590 */
1591
1592 struct list_node device_tree_fixups;
1593
1594 int dt_apply_fixups(struct device_tree *tree)
1595 {
1596 struct device_tree_fixup *fixup;
1597 list_for_each(fixup, device_tree_fixups, list_node) {
1598 assert(fixup->fixup);
1599 if (fixup->fixup(fixup, tree))
1600 return 1;
1601 }
1602 return 0;
1603 }
1604
1605 int dt_set_bin_prop_by_path(struct device_tree *tree, const char *path,
1606 void *data, size_t data_size, int create)
1607 {
1608 char *path_copy, *prop_name;
1609 struct device_tree_node *dt_node;
1610
1611 path_copy = strdup(path);
1612
1613 if (!path_copy) {
1614 printk(BIOS_ERR, "Failed to allocate a copy of path %s\n",
1615 path);
1616 return 1;
1617 }
1618
1619 prop_name = strrchr(path_copy, '/');
1620 if (!prop_name) {
1621 free(path_copy);
1622 printk(BIOS_ERR, "Path %s does not include '/'\n", path);
1623 return 1;
1624 }
1625
1626 *prop_name++ = '\0'; /* Separate path from the property name. */
1627
1628 dt_node = dt_find_node_by_path(tree, path_copy, NULL,
1629 NULL, create);
1630
1631 if (!dt_node) {
1632 printk(BIOS_ERR, "Failed to %s %s in the device tree\n",
1633 create ? "create" : "find", path_copy);
1634 free(path_copy);
1635 return 1;
1636 }
1637
1638 dt_add_bin_prop(dt_node, prop_name, data, data_size);
1639 free(path_copy);
1640
1641 return 0;
1642 }
1643
1644 /*
1645 * Prepare the /reserved-memory/ node.
1646 *
1647 * Technically, this can be called more than one time, to init and/or retrieve
1648 * the node. But dt_add_u32_prop() may leak a bit of memory if you do.
1649 *
1650 * @tree: Device tree to add/retrieve from.
1651 * @return: The /reserved-memory/ node (or NULL, if error).
1652 */
1653 struct device_tree_node *dt_init_reserved_memory_node(struct device_tree *tree)
1654 {
1655 struct device_tree_node *reserved;
1656 u32 addr = 0, size = 0;
1657
1658 reserved = dt_find_node_by_path(tree, "/reserved-memory", &addr,
1659 &size, 1);
1660 if (!reserved)
1661 return NULL;
1662
1663 /* Binding doc says this should have the same #{address,size}-cells as
1664 the root. */
1665 dt_add_u32_prop(reserved, "#address-cells", addr);
1666 dt_add_u32_prop(reserved, "#size-cells", size);
1667 /* Binding doc says this should be empty (1:1 mapping from root). */
1668 dt_add_bin_prop(reserved, "ranges", NULL, 0);
1669
1670 return reserved;
1671 }
1672
1673 /*
1674 * Increment a single phandle in prop at a given offset by a given adjustment.
1675 *
1676 * @param prop Property whose phandle should be adjusted.
1677 * @param adjustment Value that should be added to the existing phandle.
1678 * @param offset Byte offset of the phandle in the property data.
1679 *
1680 * @return New phandle value, or 0 on error.
1681 */
1682 static uint32_t dt_adjust_phandle(struct device_tree_property *prop,
1683 uint32_t adjustment, uint32_t offset)
1684 {
1685 if (offset + 4 > prop->prop.size)
1686 return 0;
1687
1688 uint32_t phandle = be32dec(prop->prop.data + offset);
1689 if (phandle == 0 ||
1690 phandle == FDT_PHANDLE_ILLEGAL ||
1691 phandle == 0xffffffff)
1692 return 0;
1693
1694 phandle += adjustment;
1695 if (phandle >= FDT_PHANDLE_ILLEGAL)
1696 return 0;
1697
1698 be32enc(prop->prop.data + offset, phandle);
1699 return phandle;
1700 }
1701
1702 /*
1703 * Adjust all phandles in subtree by adding a new base offset.
1704 *
1705 * @param node Root node of the subtree to work on.
1706 * @param base New phandle base to be added to all phandles.
1707 *
1708 * @return New highest phandle in the subtree, or 0 on error.
1709 */
1710 static uint32_t dt_adjust_all_phandles(struct device_tree_node *node,
1711 uint32_t base)
1712 {
1713 uint32_t new_max = MAX(base, 1); /* make sure we don't return 0 */
1714 struct device_tree_property *prop;
1715 struct device_tree_node *child;
1716
1717 if (!node)
1718 return new_max;
1719
1720 list_for_each(prop, node->properties, list_node)
1721 if (dt_prop_is_phandle(prop)) {
1722 node->phandle = dt_adjust_phandle(prop, base, 0);
1723 if (!node->phandle)
1724 return 0;
1725 new_max = MAX(new_max, node->phandle);
1726 } /* no break -- can have more than one phandle prop */
1727
1728 list_for_each(child, node->children, list_node)
1729 new_max = MAX(new_max, dt_adjust_all_phandles(child, base));
1730
1731 return new_max;
1732 }
1733
1734 /*
1735 * Apply a /__local_fixup__ subtree to the corresponding overlay subtree.
1736 *
1737 * @param node Root node of the overlay subtree to fix up.
1738 * @param node Root node of the /__local_fixup__ subtree.
1739 * @param base Adjustment that was added to phandles in the overlay.
1740 *
1741 * @return 0 on success, -1 on error.
1742 */
1743 static int dt_fixup_locals(struct device_tree_node *node,
1744 struct device_tree_node *fixup, uint32_t base)
1745 {
1746 struct device_tree_property *prop;
1747 struct device_tree_property *fixup_prop;
1748 struct device_tree_node *child;
1749 struct device_tree_node *fixup_child;
1750 int i;
1751
1752 /*
1753 * For local fixups the /__local_fixup__ subtree contains the same node
1754 * hierarchy as the main tree we're fixing up. Each property contains
1755 * the fixup offsets for the respective property in the main tree. For
1756 * each property in the fixup node, find the corresponding property in
1757 * the base node and apply fixups to all offsets it specifies.
1758 */
1759 list_for_each(fixup_prop, fixup->properties, list_node) {
1760 struct device_tree_property *base_prop = NULL;
1761 list_for_each(prop, node->properties, list_node)
1762 if (!strcmp(prop->prop.name, fixup_prop->prop.name)) {
1763 base_prop = prop;
1764 break;
1765 }
1766
1767 /* We should always find a corresponding base prop for a fixup,
1768 and fixup props contain a list of 32-bit fixup offsets. */
1769 if (!base_prop || fixup_prop->prop.size % sizeof(uint32_t))
1770 return -1;
1771
1772 for (i = 0; i < fixup_prop->prop.size; i += sizeof(uint32_t))
1773 if (!dt_adjust_phandle(base_prop, base, be32dec(
1774 fixup_prop->prop.data + i)))
1775 return -1;
1776 }
1777
1778 /* Now recursively descend both the base tree and the /__local_fixups__
1779 subtree in sync to apply all fixups. */
1780 list_for_each(fixup_child, fixup->children, list_node) {
1781 struct device_tree_node *base_child = NULL;
1782 list_for_each(child, node->children, list_node)
1783 if (!strcmp(child->name, fixup_child->name)) {
1784 base_child = child;
1785 break;
1786 }
1787
1788 /* All fixup nodes should have a corresponding base node. */
1789 if (!base_child)
1790 return -1;
1791
1792 if (dt_fixup_locals(base_child, fixup_child, base) < 0)
1793 return -1;
1794 }
1795
1796 return 0;
1797 }
1798
1799 /*
1800 * Update all /__symbols__ properties in an overlay that start with
1801 * "/fragment@X/__overlay__" with corresponding path prefix in the base tree.
1802 *
1803 * @param symbols /__symbols__ done to update.
1804 * @param fragment /fragment@X node that references to should be updated.
1805 * @param base_path Path of base tree node that the fragment overlaid.
1806 */
1807 static void dt_fix_symbols(struct device_tree_node *symbols,
1808 struct device_tree_node *fragment,
1809 const char *base_path)
1810 {
1811 struct device_tree_property *prop;
1812 char buf[512]; /* Should be enough for maximum DT path length? */
1813 char node_path[64]; /* easily enough for /fragment@XXXX/__overlay__ */
1814
1815 if (!symbols) /* If the overlay has no /__symbols__ node, we're done! */
1816 return;
1817
1818 int len = snprintf(node_path, sizeof(node_path), "/%s/__overlay__",
1819 fragment->name);
1820
1821 list_for_each(prop, symbols->properties, list_node)
1822 if (!strncmp(prop->prop.data, node_path, len)) {
1823 prop->prop.size = snprintf(buf, sizeof(buf), "%s%s",
1824 base_path, (char *)prop->prop.data + len) + 1;
1825 free(prop->prop.data);
1826 prop->prop.data = strdup(buf);
1827 }
1828 }
1829
1830 /*
1831 * Fix up overlay according to a property in /__fixup__. If the fixed property
1832 * is a /fragment@X:target, also update /__symbols__ references to fragment.
1833 *
1834 * @params overlay Overlay to fix up.
1835 * @params fixup /__fixup__ property.
1836 * @params phandle phandle value to insert where the fixup points to.
1837 * @params base_path Path to the base DT node that the fixup points to.
1838 * @params overlay_symbols /__symbols__ node of the overlay.
1839 *
1840 * @return 0 on success, -1 on error.
1841 */
1842 static int dt_fixup_external(struct device_tree *overlay,
1843 struct device_tree_property *fixup,
1844 uint32_t phandle, const char *base_path,
1845 struct device_tree_node *overlay_symbols)
1846 {
1847 struct device_tree_property *prop;
1848
1849 /* External fixup properties are encoded as "<path>:<prop>:<offset>". */
1850 char *entry = fixup->prop.data;
1851 while ((void *)entry < fixup->prop.data + fixup->prop.size) {
1852 /* okay to destroy fixup property value, won't need it again */
1853 char *node_path = entry;
1854 entry = strchr(node_path, ':');
1855 if (!entry)
1856 return -1;
1857 *entry++ = '\0';
1858
1859 char *prop_name = entry;
1860 entry = strchr(prop_name, ':');
1861 if (!entry)
1862 return -1;
1863 *entry++ = '\0';
1864
1865 struct device_tree_node *ovl_node = dt_find_node_by_path(
1866 overlay, node_path, NULL, NULL, 0);
1867 if (!ovl_node || !isdigit(*entry))
1868 return -1;
1869
1870 struct device_tree_property *ovl_prop = NULL;
1871 list_for_each(prop, ovl_node->properties, list_node)
1872 if (!strcmp(prop->prop.name, prop_name)) {
1873 ovl_prop = prop;
1874 break;
1875 }
1876
1877 /* Move entry to first char after number, must be a '\0'. */
1878 uint32_t offset = skip_atoi(&entry);
1879 if (!ovl_prop || offset + 4 > ovl_prop->prop.size || entry[0])
1880 return -1;
1881 entry++; /* jump over '\0' to potential next fixup */
1882
1883 be32enc(ovl_prop->prop.data + offset, phandle);
1884
1885 /* If this is a /fragment@X:target property, update references
1886 to this fragment in the overlay __symbols__ now. */
1887 if (offset == 0 && !strcmp(prop_name, "target") &&
1888 !strchr(node_path + 1, '/')) /* only toplevel nodes */
1889 dt_fix_symbols(overlay_symbols, ovl_node, base_path);
1890 }
1891
1892 return 0;
1893 }
1894
1895 /*
1896 * Apply all /__fixup__ properties in the overlay. This will destroy the
1897 * property data in /__fixup__ and it should not be accessed again.
1898 *
1899 * @params tree Base device tree that the overlay updates.
1900 * @params symbols /__symbols__ node of the base device tree.
1901 * @params overlay Overlay to fix up.
1902 * @params fixups /__fixup__ node in the overlay.
1903 * @params overlay_symbols /__symbols__ node of the overlay.
1904 *
1905 * @return 0 on success, -1 on error.
1906 */
1907 static int dt_fixup_all_externals(struct device_tree *tree,
1908 struct device_tree_node *symbols,
1909 struct device_tree *overlay,
1910 struct device_tree_node *fixups,
1911 struct device_tree_node *overlay_symbols)
1912 {
1913 struct device_tree_property *fix;
1914
1915 /* If we have any external fixups, base tree must have /__symbols__. */
1916 if (!symbols)
1917 return -1;
1918
1919 /*
1920 * Unlike /__local_fixups__, /__fixups__ is not a whole subtree that
1921 * mirrors the node hierarchy. It's just a directory of fixup properties
1922 * that each directly contain all information necessary to apply them.
1923 */
1924 list_for_each(fix, fixups->properties, list_node) {
1925 /* The name of a fixup property is the label of the node we want
1926 a property to phandle-reference. Look up in /__symbols__. */
1927 const char *path = dt_find_string_prop(symbols, fix->prop.name);
1928 if (!path)
1929 return -1;
1930
1931 /* Find node the label pointed to figure out its phandle. */
1932 struct device_tree_node *node = dt_find_node_by_path(tree, path,
1933 NULL, NULL, 0);
1934 if (!node)
1935 return -1;
1936
1937 /* Write into the overlay property(s) pointing to that node. */
1938 if (dt_fixup_external(overlay, fix, node->phandle,
1939 path, overlay_symbols) < 0)
1940 return -1;
1941 }
1942
1943 return 0;
1944 }
1945
1946 /*
1947 * Copy all nodes and properties from one DT subtree into another. This is a
1948 * shallow copy so both trees will point to the same property data afterwards.
1949 *
1950 * @params dst Destination subtree to copy into.
1951 * @params src Source subtree to copy from.
1952 * @params upd 1 to overwrite same-name properties, 0 to discard them.
1953 */
1954 static void dt_copy_subtree(struct device_tree_node *dst,
1955 struct device_tree_node *src, int upd)
1956 {
1957 struct device_tree_property *prop;
1958 struct device_tree_property *src_prop;
1959 list_for_each(src_prop, src->properties, list_node) {
1960 if (dt_prop_is_phandle(src_prop) ||
1961 !strcmp(src_prop->prop.name, "name")) {
1962 printk(BIOS_DEBUG,
1963 "WARNING: ignoring illegal overlay prop '%s'\n",
1964 src_prop->prop.name);
1965 continue;
1966 }
1967
1968 struct device_tree_property *dst_prop = NULL;
1969 list_for_each(prop, dst->properties, list_node)
1970 if (!strcmp(prop->prop.name, src_prop->prop.name)) {
1971 dst_prop = prop;
1972 break;
1973 }
1974
1975 if (dst_prop) {
1976 if (!upd) {
1977 printk(BIOS_DEBUG,
1978 "WARNING: ignoring prop update '%s'\n",
1979 src_prop->prop.name);
1980 continue;
1981 }
1982 } else {
1983 dst_prop = alloc_prop();
1984 list_insert_after(&dst_prop->list_node,
1985 &dst->properties);
1986 }
1987
1988 dst_prop->prop = src_prop->prop;
1989 }
1990
1991 struct device_tree_node *node;
1992 struct device_tree_node *src_node;
1993 list_for_each(src_node, src->children, list_node) {
1994 struct device_tree_node *dst_node = NULL;
1995 list_for_each(node, dst->children, list_node)
1996 if (!strcmp(node->name, src_node->name)) {
1997 dst_node = node;
1998 break;
1999 }
2000
2001 if (!dst_node) {
2002 dst_node = alloc_node();
2003 *dst_node = *src_node;
2004 list_insert_after(&dst_node->list_node, &dst->children);
2005 } else {
2006 dt_copy_subtree(dst_node, src_node, upd);
2007 }
2008 }
2009 }
2010
2011 /*
2012 * Apply an overlay /fragment@X node to a base device tree.
2013 *
2014 * @param tree Base device tree.
2015 * @param fragment /fragment@X node.
2016 * @params overlay_symbols /__symbols__ node of the overlay.
2017 *
2018 * @return 0 on success, -1 on error.
2019 */
2020 static int dt_import_fragment(struct device_tree *tree,
2021 struct device_tree_node *fragment,
2022 struct device_tree_node *overlay_symbols)
2023 {
2024 /* The actual overlaid nodes/props are in an __overlay__ child node. */
2025 static const char *overlay_path[] = { "__overlay__", NULL };
2026 struct device_tree_node *overlay = dt_find_node(fragment, overlay_path,
2027 NULL, NULL, 0);
2028
2029 /* If it doesn't have an __overlay__ child, it's not a fragment. */
2030 if (!overlay)
2031 return 0;
2032
2033 /* Target node of the fragment can be given by path or by phandle. */
2034 struct device_tree_property *prop;
2035 struct device_tree_property *phandle = NULL;
2036 struct device_tree_property *path = NULL;
2037 list_for_each(prop, fragment->properties, list_node) {
2038 if (!strcmp(prop->prop.name, "target")) {
2039 phandle = prop;
2040 break; /* phandle target has priority, stop looking */
2041 }
2042 if (!strcmp(prop->prop.name, "target-path"))
2043 path = prop;
2044 }
2045
2046 struct device_tree_node *target = NULL;
2047 if (phandle) {
2048 if (phandle->prop.size != sizeof(uint32_t))
2049 return -1;
2050 target = dt_find_node_by_phandle(tree->root,
2051 be32dec(phandle->prop.data));
2052 /* Symbols already updated as part of dt_fixup_external(). */
2053 } else if (path) {
2054 target = dt_find_node_by_path(tree, path->prop.data,
2055 NULL, NULL, 0);
2056 dt_fix_symbols(overlay_symbols, fragment, path->prop.data);
2057 }
2058 if (!target)
2059 return -1;
2060
2061 dt_copy_subtree(target, overlay, 1);
2062 return 0;
2063 }
2064
2065 /*
2066 * Apply a device tree overlay to a base device tree. This will
2067 * destroy/incorporate the overlay data, so it should not be freed or reused.
2068 * See dtc.git/Documentation/dt-object-internal.txt for overlay format details.
2069 *
2070 * @param tree Unflattened base device tree to add the overlay into.
2071 * @param overlay Unflattened overlay device tree to apply to the base.
2072 *
2073 * @return 0 on success, -1 on error.
2074 */
2075 int dt_apply_overlay(struct device_tree *tree, struct device_tree *overlay)
2076 {
2077 /*
2078 * First, we need to make sure phandles inside the overlay don't clash
2079 * with those in the base tree. We just define the highest phandle value
2080 * in the base tree as the "phandle offset" for this overlay and
2081 * increment all phandles in it by that value.
2082 */
2083 uint32_t phandle_base = tree->max_phandle;
2084 uint32_t new_max = dt_adjust_all_phandles(overlay->root, phandle_base);
2085 if (!new_max) {
2086 printk(BIOS_ERR, "invalid phandles in overlay\n");
2087 return -1;
2088 }
2089 tree->max_phandle = new_max;
2090
2091 /* Now that we changed phandles in the overlay, we need to update any
2092 nodes referring to them. Those are listed in /__local_fixups__. */
2093 struct device_tree_node *local_fixups = dt_find_node_by_path(overlay,
2094 "/__local_fixups__", NULL, NULL, 0);
2095 if (local_fixups && dt_fixup_locals(overlay->root, local_fixups,
2096 phandle_base) < 0) {
2097 printk(BIOS_ERR, "invalid local fixups in overlay\n");
2098 return -1;
2099 }
2100
2101 /*
2102 * Besides local phandle references (from nodes within the overlay to
2103 * other nodes within the overlay), the overlay may also contain phandle
2104 * references to the base tree. These are stored with invalid values and
2105 * must be updated now. /__symbols__ contains a list of all labels in
2106 * the base tree, and /__fixups__ describes all nodes in the overlay
2107 * that contain external phandle references.
2108 * We also take this opportunity to update all /fragment@X/__overlay__/
2109 * prefixes in the overlay's /__symbols__ node to the correct path that
2110 * the fragment will be placed in later, since this is the only step
2111 * where we have all necessary information for that easily available.
2112 */
2113 struct device_tree_node *symbols = dt_find_node_by_path(tree,
2114 "/__symbols__", NULL, NULL, 0);
2115 struct device_tree_node *fixups = dt_find_node_by_path(overlay,
2116 "/__fixups__", NULL, NULL, 0);
2117 struct device_tree_node *overlay_symbols = dt_find_node_by_path(overlay,
2118 "/__symbols__", NULL, NULL, 0);
2119 if (fixups && dt_fixup_all_externals(tree, symbols, overlay,
2120 fixups, overlay_symbols) < 0) {
2121 printk(BIOS_ERR, "cannot match external fixups from overlay\n");
2122 return -1;
2123 }
2124
2125 /* After all this fixing up, we can finally merge overlay into the tree
2126 (one fragment at a time, because for some reason it's split up). */
2127 struct device_tree_node *fragment;
2128 list_for_each(fragment, overlay->root->children, list_node)
2129 if (dt_import_fragment(tree, fragment, overlay_symbols) < 0) {
2130 printk(BIOS_ERR, "bad DT fragment '%s'\n",
2131 fragment->name);
2132 return -1;
2133 }
2134
2135 /*
2136 * We need to also update /__symbols__ to include labels from this
2137 * overlay, in case we want to load further overlays with external
2138 * phandle references to it. If the base tree already has a /__symbols__
2139 * we merge them together, otherwise we just insert the overlay's
2140 * /__symbols__ node into the base tree root.
2141 */
2142 if (overlay_symbols) {
2143 if (symbols)
2144 dt_copy_subtree(symbols, overlay_symbols, 0);
2145 else
2146 list_insert_after(&overlay_symbols->list_node,
2147 &tree->root->children);
2148 }
2149
2150 return 0;
2151 }