]>
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
11 #define LOG_CATEGORY LOGC_DT
15 #include <linux/libfdt.h>
18 #include <dm/of_access.h>
19 #include <linux/err.h>
20 #include <linux/sizes.h>
26 static void *unflatten_dt_alloc(void **mem
, unsigned long size
,
31 *mem
= PTR_ALIGN(*mem
, align
);
39 * unflatten_dt_node() - Alloc and populate a device_node from the flat tree
40 * @blob: The parent device tree blob
41 * @mem: Memory chunk to use for allocating device nodes and properties
42 * @poffset: pointer to node in flat tree
43 * @dad: Parent struct device_node
44 * @nodepp: The device_node tree created by the call
45 * @fpsize: Size of the node path up at t05he current depth.
46 * @dryrun: If true, do not allocate device nodes but still calculate needed
49 static void *unflatten_dt_node(const void *blob
, void *mem
, int *poffset
,
50 struct device_node
*dad
,
51 struct device_node
**nodepp
,
52 unsigned long fpsize
, bool dryrun
)
55 struct device_node
*np
;
56 struct property
*pp
, **prev_pp
= NULL
;
66 pathp
= fdt_get_name(blob
, *poffset
, &l
);
73 * version 0x10 has a more compact unit name here instead of the full
74 * path. we accumulate the full path size using "fpsize", we'll rebuild
75 * it later. We detect this because the first character of the name is
78 if ((*pathp
) != '/') {
82 * root node: special case. fpsize accounts for path
83 * plus terminating zero. root node only has '/', so
84 * fpsize should be 2, but we want to avoid the first
85 * level nodes to have two '/' so we use fpsize 1 here
93 * account for '/' and path size minus terminal 0
101 np
= unflatten_dt_alloc(&mem
, sizeof(struct device_node
) + allocl
,
102 __alignof__(struct device_node
));
106 fn
= (char *)np
+ sizeof(*np
);
113 /* rebuild full path for new format */
114 if (dad
&& dad
->parent
) {
115 strcpy(fn
, dad
->full_name
);
117 if ((strlen(fn
) + l
+ 1) != allocl
) {
118 debug("%s: p: %d, l: %d, a: %d\n",
119 pathp
, (int)strlen(fn
), l
,
127 memcpy(fn
, pathp
, l
);
129 prev_pp
= &np
->properties
;
132 np
->sibling
= dad
->child
;
136 /* process properties */
137 for (offset
= fdt_first_property_offset(blob
, *poffset
);
139 (offset
= fdt_next_property_offset(blob
, offset
))) {
143 p
= fdt_getprop_by_offset(blob
, offset
, &pname
, &sz
);
145 offset
= -FDT_ERR_INTERNAL
;
150 debug("Can't find property name in list !\n");
153 if (strcmp(pname
, "name") == 0)
155 pp
= unflatten_dt_alloc(&mem
, sizeof(struct property
),
156 __alignof__(struct property
));
159 * We accept flattened tree phandles either in
160 * ePAPR-style "phandle" properties, or the
161 * legacy "linux,phandle" properties. If both
162 * appear and have different values, things
163 * will get weird. Don't do that. */
164 if ((strcmp(pname
, "phandle") == 0) ||
165 (strcmp(pname
, "linux,phandle") == 0)) {
166 if (np
->phandle
== 0)
167 np
->phandle
= be32_to_cpup(p
);
170 * And we process the "ibm,phandle" property
171 * used in pSeries dynamic device tree
173 if (strcmp(pname
, "ibm,phandle") == 0)
174 np
->phandle
= be32_to_cpup(p
);
175 pp
->name
= (char *)pname
;
177 pp
->value
= (__be32
*)p
;
183 * with version 0x10 we may not have the name property, recreate
184 * it here from the unit name if absent
187 const char *p1
= pathp
, *ps
= pathp
, *pa
= NULL
;
200 pp
= unflatten_dt_alloc(&mem
, sizeof(struct property
) + sz
,
201 __alignof__(struct property
));
208 memcpy(pp
->value
, ps
, sz
- 1);
209 ((char *)pp
->value
)[sz
- 1] = 0;
210 debug("fixed up name for %s -> %s\n", pathp
,
217 np
->name
= of_get_property(np
, "name", NULL
);
218 np
->type
= of_get_property(np
, "device_type", NULL
);
223 np
->type
= "<NULL>"; }
226 *poffset
= fdt_next_node(blob
, *poffset
, &depth
);
229 while (*poffset
> 0 && depth
> old_depth
) {
230 mem
= unflatten_dt_node(blob
, mem
, poffset
, np
, NULL
,
236 if (*poffset
< 0 && *poffset
!= -FDT_ERR_NOTFOUND
) {
237 debug("unflatten: error %d processing FDT\n", *poffset
);
242 * Reverse the child list. Some drivers assumes node order matches .dts
245 if (!dryrun
&& np
->child
) {
246 struct device_node
*child
= np
->child
;
249 struct device_node
*next
= child
->sibling
;
251 child
->sibling
= np
->child
;
263 int unflatten_device_tree(const void *blob
, 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
= memalign(__alignof__(struct device_node
), size
+ 4);
298 memset(mem
, '\0', size
);
300 /* Set up value for dm_test_livetree_align() */
301 *(u32
*)mem
= BAD_OF_ROOT
;
303 *(__be32
*)(mem
+ size
) = cpu_to_be32(0xdeadbeef);
305 debug(" unflattening %p...\n", mem
);
307 /* Second pass, do actual unflattening */
309 unflatten_dt_node(blob
, mem
, &start
, NULL
, mynodes
, 0, false);
310 if (be32_to_cpup(mem
+ size
) != 0xdeadbeef) {
311 debug("End of tree marker overwritten: %08x\n",
312 be32_to_cpup(mem
+ size
));
316 debug(" <- unflatten_device_tree()\n");
321 int of_live_build(const void *fdt_blob
, struct device_node
**rootp
)
325 debug("%s: start\n", __func__
);
326 ret
= unflatten_device_tree(fdt_blob
, rootp
);
328 debug("Failed to create live tree: err=%d\n", ret
);
331 ret
= of_alias_scan();
333 debug("Failed to scan live tree aliases: err=%d\n", ret
);
336 debug("%s: stop\n", __func__
);
341 void of_live_free(struct device_node
*root
)
343 /* the tree is stored as a contiguous block of memory */
347 int of_live_create_empty(struct device_node
**rootp
)
349 struct device_node
*root
;
351 root
= calloc(1, sizeof(struct device_node
));
354 root
->name
= strdup("");
359 root
->type
= "<NULL>";
360 root
->full_name
= "";
366 static int check_space(int ret
, struct abuf
*buf
)
368 if (ret
== -FDT_ERR_NOSPACE
) {
369 if (!abuf_realloc_inc(buf
, BUF_STEP
))
370 return log_msg_ret("spc", -ENOMEM
);
371 ret
= fdt_resize(abuf_data(buf
), abuf_data(buf
),
374 return log_msg_ret("res", -EFAULT
);
383 * flatten_node() - Write out the node and its properties into a flat tree
385 static int flatten_node(struct abuf
*buf
, const struct device_node
*node
)
387 const struct device_node
*np
;
388 const struct property
*pp
;
391 ret
= fdt_begin_node(abuf_data(buf
), node
->name
);
392 ret
= check_space(ret
, buf
);
393 if (ret
== -EAGAIN
) {
394 ret
= fdt_begin_node(abuf_data(buf
), node
->name
);
396 log_debug("Internal error a %d\n", ret
);
401 return log_msg_ret("beg", ret
);
403 /* First write out the properties */
404 for (pp
= node
->properties
; !ret
&& pp
; pp
= pp
->next
) {
405 ret
= fdt_property(abuf_data(buf
), pp
->name
, pp
->value
,
407 ret
= check_space(ret
, buf
);
408 if (ret
== -EAGAIN
) {
409 ret
= fdt_property(abuf_data(buf
), pp
->name
, pp
->value
,
414 /* Next write out the subnodes */
415 for (np
= node
->child
; np
; np
= np
->sibling
) {
416 ret
= flatten_node(buf
, np
);
418 return log_msg_ret("sub", ret
);
421 ret
= fdt_end_node(abuf_data(buf
));
422 ret
= check_space(ret
, buf
);
423 if (ret
== -EAGAIN
) {
424 ret
= fdt_end_node(abuf_data(buf
));
426 log_debug("Internal error b %d\n", ret
);
431 return log_msg_ret("end", ret
);
436 int of_live_flatten(const struct device_node
*root
, struct abuf
*buf
)
441 if (!abuf_realloc(buf
, BUF_STEP
))
442 return log_msg_ret("ini", -ENOMEM
);
444 ret
= fdt_create(abuf_data(buf
), abuf_size(buf
));
446 ret
= fdt_finish_reservemap(abuf_data(buf
));
448 log_debug("Failed to start FDT (err=%d)\n", ret
);
449 return log_msg_ret("sta", -EINVAL
);
452 ret
= flatten_node(buf
, root
);
454 return log_msg_ret("flt", ret
);
456 ret
= fdt_finish(abuf_data(buf
));
457 ret
= check_space(ret
, buf
);
458 if (ret
== -EAGAIN
) {
459 ret
= fdt_finish(abuf_data(buf
));
461 log_debug("Internal error c %d\n", ret
);
466 return log_msg_ret("fin", ret
);
468 ret
= fdt_pack(abuf_data(buf
));
470 log_debug("Failed to pack (err=%d)\n", ret
);
471 return log_msg_ret("pac", -EFAULT
);
474 if (!abuf_realloc(buf
, fdt_totalsize(abuf_data(buf
))))
475 return log_msg_ret("abu", -EFAULT
);