2 * Copyright 2009 Benjamin Herrenschmidt, IBM Corp
3 * benh@kernel.crashing.org
5 * Based on parts of drivers/of/fdt.c from Linux v4.9
6 * Modifications for U-Boot
7 * Copyright (c) 2017 Google, Inc
9 * SPDX-License-Identifier: GPL-2.0+
16 #include <dm/of_access.h>
17 #include <linux/err.h>
19 DECLARE_GLOBAL_DATA_PTR;
21 static void *unflatten_dt_alloc(void **mem, unsigned long size,
26 *mem = PTR_ALIGN(*mem, align);
34 * unflatten_dt_node() - Alloc and populate a device_node from the flat tree
35 * @blob: The parent device tree blob
36 * @mem: Memory chunk to use for allocating device nodes and properties
37 * @poffset: pointer to node in flat tree
38 * @dad: Parent struct device_node
39 * @nodepp: The device_node tree created by the call
40 * @fpsize: Size of the node path up at t05he current depth.
41 * @dryrun: If true, do not allocate device nodes but still calculate needed
44 static void *unflatten_dt_node(const void *blob, void *mem, int *poffset,
45 struct device_node *dad,
46 struct device_node **nodepp,
47 unsigned long fpsize, bool dryrun)
50 struct device_node *np;
51 struct property *pp, **prev_pp = NULL;
61 pathp = fdt_get_name(blob, *poffset, &l);
68 * version 0x10 has a more compact unit name here instead of the full
69 * path. we accumulate the full path size using "fpsize", we'll rebuild
70 * it later. We detect this because the first character of the name is
73 if ((*pathp) != '/') {
77 * root node: special case. fpsize accounts for path
78 * plus terminating zero. root node only has '/', so
79 * fpsize should be 2, but we want to avoid the first
80 * level nodes to have two '/' so we use fpsize 1 here
88 * account for '/' and path size minus terminal 0
96 np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + allocl,
97 __alignof__(struct device_node));
101 fn = (char *)np + sizeof(*np);
104 /* rebuild full path for new format */
105 if (dad && dad->parent) {
106 strcpy(fn, dad->full_name);
108 if ((strlen(fn) + l + 1) != allocl) {
109 debug("%s: p: %d, l: %d, a: %d\n",
110 pathp, (int)strlen(fn), l,
118 memcpy(fn, pathp, l);
120 prev_pp = &np->properties;
123 np->sibling = dad->child;
127 /* process properties */
128 for (offset = fdt_first_property_offset(blob, *poffset);
130 (offset = fdt_next_property_offset(blob, offset))) {
134 p = fdt_getprop_by_offset(blob, offset, &pname, &sz);
136 offset = -FDT_ERR_INTERNAL;
141 debug("Can't find property name in list !\n");
144 if (strcmp(pname, "name") == 0)
146 pp = unflatten_dt_alloc(&mem, sizeof(struct property),
147 __alignof__(struct property));
150 * We accept flattened tree phandles either in
151 * ePAPR-style "phandle" properties, or the
152 * legacy "linux,phandle" properties. If both
153 * appear and have different values, things
154 * will get weird. Don't do that. */
155 if ((strcmp(pname, "phandle") == 0) ||
156 (strcmp(pname, "linux,phandle") == 0)) {
157 if (np->phandle == 0)
158 np->phandle = be32_to_cpup(p);
161 * And we process the "ibm,phandle" property
162 * used in pSeries dynamic device tree
164 if (strcmp(pname, "ibm,phandle") == 0)
165 np->phandle = be32_to_cpup(p);
166 pp->name = (char *)pname;
168 pp->value = (__be32 *)p;
174 * with version 0x10 we may not have the name property, recreate
175 * it here from the unit name if absent
178 const char *p1 = pathp, *ps = pathp, *pa = NULL;
191 pp = unflatten_dt_alloc(&mem, sizeof(struct property) + sz,
192 __alignof__(struct property));
199 memcpy(pp->value, ps, sz - 1);
200 ((char *)pp->value)[sz - 1] = 0;
201 debug("fixed up name for %s -> %s\n", pathp,
207 np->name = of_get_property(np, "name", NULL);
208 np->type = of_get_property(np, "device_type", NULL);
213 np->type = "<NULL>"; }
216 *poffset = fdt_next_node(blob, *poffset, &depth);
219 while (*poffset > 0 && depth > old_depth) {
220 mem = unflatten_dt_node(blob, mem, poffset, np, NULL,
226 if (*poffset < 0 && *poffset != -FDT_ERR_NOTFOUND) {
227 debug("unflatten: error %d processing FDT\n", *poffset);
232 * Reverse the child list. Some drivers assumes node order matches .dts
235 if (!dryrun && np->child) {
236 struct device_node *child = np->child;
239 struct device_node *next = child->sibling;
241 child->sibling = np->child;
254 * unflatten_device_tree() - create tree of device_nodes from flat blob
256 * unflattens a device-tree, creating the
257 * tree of struct device_node. It also fills the "name" and "type"
258 * pointers of the nodes so the normal device-tree walking functions
260 * @blob: The blob to expand
261 * @mynodes: The device_node tree created by the call
262 * @return 0 if OK, -ve on error
264 static int unflatten_device_tree(const void *blob,
265 struct device_node **mynodes)
271 debug(" -> unflatten_device_tree()\n");
274 debug("No device tree pointer\n");
278 debug("Unflattening device tree:\n");
279 debug("magic: %08x\n", fdt_magic(blob));
280 debug("size: %08x\n", fdt_totalsize(blob));
281 debug("version: %08x\n", fdt_version(blob));
283 if (fdt_check_header(blob)) {
284 debug("Invalid device tree blob header\n");
288 /* First pass, scan for size */
290 size = (unsigned long)unflatten_dt_node(blob, NULL, &start, NULL, NULL,
294 size = ALIGN(size, 4);
296 debug(" size is %lx, allocating...\n", size);
298 /* Allocate memory for the expanded device tree */
299 mem = malloc(size + 4);
300 memset(mem, '\0', size);
302 *(__be32 *)(mem + size) = cpu_to_be32(0xdeadbeef);
304 debug(" unflattening %p...\n", mem);
306 /* Second pass, do actual unflattening */
308 unflatten_dt_node(blob, mem, &start, NULL, mynodes, 0, false);
309 if (be32_to_cpup(mem + size) != 0xdeadbeef) {
310 debug("End of tree marker overwritten: %08x\n",
311 be32_to_cpup(mem + size));
315 debug(" <- unflatten_device_tree()\n");
320 int of_live_build(const void *fdt_blob, struct device_node **rootp)
324 debug("%s: start\n", __func__);
325 ret = unflatten_device_tree(fdt_blob, rootp);
327 debug("Failed to create live tree: err=%d\n", ret);
330 ret = of_alias_scan();
332 debug("Failed to scan live tree aliases: err=%d\n", ret);
335 debug("%s: stop\n", __func__);