1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2009 Benjamin Herrenschmidt, IBM Corp
4 * benh@kernel.crashing.org
6 * Based on parts of drivers/of/fdt.c from Linux v4.9
7 * Modifications for U-Boot
8 * Copyright (c) 2017 Google, Inc
13 #include <linux/libfdt.h>
16 #include <dm/of_access.h>
17 #include <linux/err.h>
19 static void *unflatten_dt_alloc(void **mem, unsigned long size,
24 *mem = PTR_ALIGN(*mem, align);
32 * unflatten_dt_node() - Alloc and populate a device_node from the flat tree
33 * @blob: The parent device tree blob
34 * @mem: Memory chunk to use for allocating device nodes and properties
35 * @poffset: pointer to node in flat tree
36 * @dad: Parent struct device_node
37 * @nodepp: The device_node tree created by the call
38 * @fpsize: Size of the node path up at t05he current depth.
39 * @dryrun: If true, do not allocate device nodes but still calculate needed
42 static void *unflatten_dt_node(const void *blob, void *mem, int *poffset,
43 struct device_node *dad,
44 struct device_node **nodepp,
45 unsigned long fpsize, bool dryrun)
48 struct device_node *np;
49 struct property *pp, **prev_pp = NULL;
59 pathp = fdt_get_name(blob, *poffset, &l);
66 * version 0x10 has a more compact unit name here instead of the full
67 * path. we accumulate the full path size using "fpsize", we'll rebuild
68 * it later. We detect this because the first character of the name is
71 if ((*pathp) != '/') {
75 * root node: special case. fpsize accounts for path
76 * plus terminating zero. root node only has '/', so
77 * fpsize should be 2, but we want to avoid the first
78 * level nodes to have two '/' so we use fpsize 1 here
86 * account for '/' and path size minus terminal 0
94 np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + allocl,
95 __alignof__(struct device_node));
99 fn = (char *)np + sizeof(*np);
102 /* rebuild full path for new format */
103 if (dad && dad->parent) {
104 strcpy(fn, dad->full_name);
106 if ((strlen(fn) + l + 1) != allocl) {
107 debug("%s: p: %d, l: %d, a: %d\n",
108 pathp, (int)strlen(fn), l,
116 memcpy(fn, pathp, l);
118 prev_pp = &np->properties;
121 np->sibling = dad->child;
125 /* process properties */
126 for (offset = fdt_first_property_offset(blob, *poffset);
128 (offset = fdt_next_property_offset(blob, offset))) {
132 p = fdt_getprop_by_offset(blob, offset, &pname, &sz);
134 offset = -FDT_ERR_INTERNAL;
139 debug("Can't find property name in list !\n");
142 if (strcmp(pname, "name") == 0)
144 pp = unflatten_dt_alloc(&mem, sizeof(struct property),
145 __alignof__(struct property));
148 * We accept flattened tree phandles either in
149 * ePAPR-style "phandle" properties, or the
150 * legacy "linux,phandle" properties. If both
151 * appear and have different values, things
152 * will get weird. Don't do that. */
153 if ((strcmp(pname, "phandle") == 0) ||
154 (strcmp(pname, "linux,phandle") == 0)) {
155 if (np->phandle == 0)
156 np->phandle = be32_to_cpup(p);
159 * And we process the "ibm,phandle" property
160 * used in pSeries dynamic device tree
162 if (strcmp(pname, "ibm,phandle") == 0)
163 np->phandle = be32_to_cpup(p);
164 pp->name = (char *)pname;
166 pp->value = (__be32 *)p;
172 * with version 0x10 we may not have the name property, recreate
173 * it here from the unit name if absent
176 const char *p1 = pathp, *ps = pathp, *pa = NULL;
189 pp = unflatten_dt_alloc(&mem, sizeof(struct property) + sz,
190 __alignof__(struct property));
197 memcpy(pp->value, ps, sz - 1);
198 ((char *)pp->value)[sz - 1] = 0;
199 debug("fixed up name for %s -> %s\n", pathp,
205 np->name = of_get_property(np, "name", NULL);
206 np->type = of_get_property(np, "device_type", NULL);
211 np->type = "<NULL>"; }
214 *poffset = fdt_next_node(blob, *poffset, &depth);
217 while (*poffset > 0 && depth > old_depth) {
218 mem = unflatten_dt_node(blob, mem, poffset, np, NULL,
224 if (*poffset < 0 && *poffset != -FDT_ERR_NOTFOUND) {
225 debug("unflatten: error %d processing FDT\n", *poffset);
230 * Reverse the child list. Some drivers assumes node order matches .dts
233 if (!dryrun && np->child) {
234 struct device_node *child = np->child;
237 struct device_node *next = child->sibling;
239 child->sibling = np->child;
252 * unflatten_device_tree() - create tree of device_nodes from flat blob
254 * unflattens a device-tree, creating the
255 * tree of struct device_node. It also fills the "name" and "type"
256 * pointers of the nodes so the normal device-tree walking functions
258 * @blob: The blob to expand
259 * @mynodes: The device_node tree created by the call
260 * @return 0 if OK, -ve on error
262 static int unflatten_device_tree(const void *blob,
263 struct device_node **mynodes)
269 debug(" -> unflatten_device_tree()\n");
272 debug("No device tree pointer\n");
276 debug("Unflattening device tree:\n");
277 debug("magic: %08x\n", fdt_magic(blob));
278 debug("size: %08x\n", fdt_totalsize(blob));
279 debug("version: %08x\n", fdt_version(blob));
281 if (fdt_check_header(blob)) {
282 debug("Invalid device tree blob header\n");
286 /* First pass, scan for size */
288 size = (unsigned long)unflatten_dt_node(blob, NULL, &start, NULL, NULL,
292 size = ALIGN(size, 4);
294 debug(" size is %lx, allocating...\n", size);
296 /* Allocate memory for the expanded device tree */
297 mem = malloc(size + 4);
298 memset(mem, '\0', size);
300 *(__be32 *)(mem + size) = cpu_to_be32(0xdeadbeef);
302 debug(" unflattening %p...\n", mem);
304 /* Second pass, do actual unflattening */
306 unflatten_dt_node(blob, mem, &start, NULL, mynodes, 0, false);
307 if (be32_to_cpup(mem + size) != 0xdeadbeef) {
308 debug("End of tree marker overwritten: %08x\n",
309 be32_to_cpup(mem + size));
313 debug(" <- unflatten_device_tree()\n");
318 int of_live_build(const void *fdt_blob, struct device_node **rootp)
322 debug("%s: start\n", __func__);
323 ret = unflatten_device_tree(fdt_blob, rootp);
325 debug("Failed to create live tree: err=%d\n", ret);
328 ret = of_alias_scan();
330 debug("Failed to scan live tree aliases: err=%d\n", ret);
333 debug("%s: stop\n", __func__);