]>
git.ipfire.org Git - thirdparty/u-boot.git/blob - lib/of_live.c
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
);
106 /* rebuild full path for new format */
107 if (dad
&& dad
->parent
) {
108 strcpy(fn
, dad
->full_name
);
110 if ((strlen(fn
) + l
+ 1) != allocl
) {
111 debug("%s: p: %d, l: %d, a: %d\n",
112 pathp
, (int)strlen(fn
), l
,
120 memcpy(fn
, pathp
, l
);
122 prev_pp
= &np
->properties
;
125 np
->sibling
= dad
->child
;
129 /* process properties */
130 for (offset
= fdt_first_property_offset(blob
, *poffset
);
132 (offset
= fdt_next_property_offset(blob
, offset
))) {
136 p
= fdt_getprop_by_offset(blob
, offset
, &pname
, &sz
);
138 offset
= -FDT_ERR_INTERNAL
;
143 debug("Can't find property name in list !\n");
146 if (strcmp(pname
, "name") == 0)
148 pp
= unflatten_dt_alloc(&mem
, sizeof(struct property
),
149 __alignof__(struct property
));
152 * We accept flattened tree phandles either in
153 * ePAPR-style "phandle" properties, or the
154 * legacy "linux,phandle" properties. If both
155 * appear and have different values, things
156 * will get weird. Don't do that. */
157 if ((strcmp(pname
, "phandle") == 0) ||
158 (strcmp(pname
, "linux,phandle") == 0)) {
159 if (np
->phandle
== 0)
160 np
->phandle
= be32_to_cpup(p
);
163 * And we process the "ibm,phandle" property
164 * used in pSeries dynamic device tree
166 if (strcmp(pname
, "ibm,phandle") == 0)
167 np
->phandle
= be32_to_cpup(p
);
168 pp
->name
= (char *)pname
;
170 pp
->value
= (__be32
*)p
;
176 * with version 0x10 we may not have the name property, recreate
177 * it here from the unit name if absent
180 const char *p1
= pathp
, *ps
= pathp
, *pa
= NULL
;
193 pp
= unflatten_dt_alloc(&mem
, sizeof(struct property
) + sz
,
194 __alignof__(struct property
));
201 memcpy(pp
->value
, ps
, sz
- 1);
202 ((char *)pp
->value
)[sz
- 1] = 0;
203 debug("fixed up name for %s -> %s\n", pathp
,
210 np
->name
= of_get_property(np
, "name", NULL
);
211 np
->type
= of_get_property(np
, "device_type", NULL
);
216 np
->type
= "<NULL>"; }
219 *poffset
= fdt_next_node(blob
, *poffset
, &depth
);
222 while (*poffset
> 0 && depth
> old_depth
) {
223 mem
= unflatten_dt_node(blob
, mem
, poffset
, np
, NULL
,
229 if (*poffset
< 0 && *poffset
!= -FDT_ERR_NOTFOUND
) {
230 debug("unflatten: error %d processing FDT\n", *poffset
);
235 * Reverse the child list. Some drivers assumes node order matches .dts
238 if (!dryrun
&& np
->child
) {
239 struct device_node
*child
= np
->child
;
242 struct device_node
*next
= child
->sibling
;
244 child
->sibling
= np
->child
;
256 int unflatten_device_tree(const void *blob
, struct device_node
**mynodes
)
262 debug(" -> unflatten_device_tree()\n");
265 debug("No device tree pointer\n");
269 debug("Unflattening device tree:\n");
270 debug("magic: %08x\n", fdt_magic(blob
));
271 debug("size: %08x\n", fdt_totalsize(blob
));
272 debug("version: %08x\n", fdt_version(blob
));
274 if (fdt_check_header(blob
)) {
275 debug("Invalid device tree blob header\n");
279 /* First pass, scan for size */
281 size
= (unsigned long)unflatten_dt_node(blob
, NULL
, &start
, NULL
, NULL
,
285 size
= ALIGN(size
, 4);
287 debug(" size is %lx, allocating...\n", size
);
289 /* Allocate memory for the expanded device tree */
290 mem
= memalign(__alignof__(struct device_node
), size
+ 4);
291 memset(mem
, '\0', size
);
293 /* Set up value for dm_test_livetree_align() */
294 *(u32
*)mem
= BAD_OF_ROOT
;
296 *(__be32
*)(mem
+ size
) = cpu_to_be32(0xdeadbeef);
298 debug(" unflattening %p...\n", mem
);
300 /* Second pass, do actual unflattening */
302 unflatten_dt_node(blob
, mem
, &start
, NULL
, mynodes
, 0, false);
303 if (be32_to_cpup(mem
+ size
) != 0xdeadbeef) {
304 debug("End of tree marker overwritten: %08x\n",
305 be32_to_cpup(mem
+ size
));
309 debug(" <- unflatten_device_tree()\n");
314 int of_live_build(const void *fdt_blob
, struct device_node
**rootp
)
318 debug("%s: start\n", __func__
);
319 ret
= unflatten_device_tree(fdt_blob
, rootp
);
321 debug("Failed to create live tree: err=%d\n", ret
);
324 ret
= of_alias_scan();
326 debug("Failed to scan live tree aliases: err=%d\n", ret
);
329 debug("%s: stop\n", __func__
);
334 void of_live_free(struct device_node
*root
)
336 /* the tree is stored as a contiguous block of memory */