2 * Functions to help device tree manipulation using libfdt.
3 * It also provides functions to read entries from device tree proc
6 * Copyright 2008 IBM Corporation.
7 * Authors: Jerone Young <jyoung5@us.ibm.com>
8 * Hollis Blanchard <hollisb@us.ibm.com>
10 * This work is licensed under the GNU GPL license version 2 or later.
14 #include "qemu/osdep.h"
20 #include "qapi/error.h"
21 #include "qemu/error-report.h"
22 #include "qemu/option.h"
23 #include "qemu/bswap.h"
24 #include "qemu/cutils.h"
25 #include "sysemu/device_tree.h"
26 #include "hw/loader.h"
27 #include "hw/boards.h"
28 #include "qemu/config-file.h"
32 #define FDT_MAX_SIZE 0x100000
34 void *create_device_tree(int *sizep
)
39 *sizep
= FDT_MAX_SIZE
;
40 fdt
= g_malloc0(FDT_MAX_SIZE
);
41 ret
= fdt_create(fdt
, FDT_MAX_SIZE
);
45 ret
= fdt_finish_reservemap(fdt
);
49 ret
= fdt_begin_node(fdt
, "");
53 ret
= fdt_end_node(fdt
);
57 ret
= fdt_finish(fdt
);
61 ret
= fdt_open_into(fdt
, fdt
, *sizep
);
63 error_report("%s: Unable to copy device tree into memory: %s",
64 __func__
, fdt_strerror(ret
));
70 error_report("%s Couldn't create dt: %s", __func__
, fdt_strerror(ret
));
74 void *load_device_tree(const char *filename_path
, int *sizep
)
77 int dt_file_load_size
;
82 dt_size
= get_image_size(filename_path
);
84 error_report("Unable to get size of device tree file '%s'",
88 if (dt_size
> INT_MAX
/ 2 - 10000) {
89 error_report("Device tree file '%s' is too large", filename_path
);
93 /* Expand to 2x size to give enough room for manipulation. */
96 /* First allocate space in qemu for device tree */
97 fdt
= g_malloc0(dt_size
);
99 dt_file_load_size
= load_image_size(filename_path
, fdt
, dt_size
);
100 if (dt_file_load_size
< 0) {
101 error_report("Unable to open device tree file '%s'",
106 ret
= fdt_open_into(fdt
, fdt
, dt_size
);
108 error_report("%s: Unable to copy device tree into memory: %s",
109 __func__
, fdt_strerror(ret
));
113 /* Check sanity of device tree */
114 if (fdt_check_header(fdt
)) {
115 error_report("Device tree file loaded into memory is invalid: %s",
129 #define SYSFS_DT_BASEDIR "/proc/device-tree"
132 * read_fstree: this function is inspired from dtc read_fstree
133 * @fdt: preallocated fdt blob buffer, to be populated
134 * @dirname: directory to scan under SYSFS_DT_BASEDIR
135 * the search is recursive and the tree is searched down to the
136 * leaves (property files).
138 * the function asserts in case of error
140 static void read_fstree(void *fdt
, const char *dirname
)
145 const char *root_dir
= SYSFS_DT_BASEDIR
;
146 const char *parent_node
;
148 if (strstr(dirname
, root_dir
) != dirname
) {
149 error_report("%s: %s must be searched within %s",
150 __func__
, dirname
, root_dir
);
153 parent_node
= &dirname
[strlen(SYSFS_DT_BASEDIR
)];
155 d
= opendir(dirname
);
157 error_report("%s cannot open %s", __func__
, dirname
);
161 while ((de
= readdir(d
)) != NULL
) {
164 if (!g_strcmp0(de
->d_name
, ".")
165 || !g_strcmp0(de
->d_name
, "..")) {
169 tmpnam
= g_strdup_printf("%s/%s", dirname
, de
->d_name
);
171 if (lstat(tmpnam
, &st
) < 0) {
172 error_report("%s cannot lstat %s", __func__
, tmpnam
);
176 if (S_ISREG(st
.st_mode
)) {
180 if (!g_file_get_contents(tmpnam
, &val
, &len
, NULL
)) {
181 error_report("%s not able to extract info from %s",
186 if (strlen(parent_node
) > 0) {
187 qemu_fdt_setprop(fdt
, parent_node
,
188 de
->d_name
, val
, len
);
190 qemu_fdt_setprop(fdt
, "/", de
->d_name
, val
, len
);
193 } else if (S_ISDIR(st
.st_mode
)) {
196 node_name
= g_strdup_printf("%s/%s",
197 parent_node
, de
->d_name
);
198 qemu_fdt_add_subnode(fdt
, node_name
);
200 read_fstree(fdt
, tmpnam
);
209 /* load_device_tree_from_sysfs: extract the dt blob from host sysfs */
210 void *load_device_tree_from_sysfs(void)
215 host_fdt
= create_device_tree(&host_fdt_size
);
216 read_fstree(host_fdt
, SYSFS_DT_BASEDIR
);
217 if (fdt_check_header(host_fdt
)) {
218 error_report("%s host device tree extracted into memory is invalid",
225 #endif /* CONFIG_LINUX */
227 static int findnode_nofail(void *fdt
, const char *node_path
)
231 offset
= fdt_path_offset(fdt
, node_path
);
233 error_report("%s Couldn't find node %s: %s", __func__
, node_path
,
234 fdt_strerror(offset
));
241 char **qemu_fdt_node_unit_path(void *fdt
, const char *name
, Error
**errp
)
243 char *prefix
= g_strdup_printf("%s@", name
);
244 unsigned int path_len
= 16, n
= 0;
245 GSList
*path_list
= NULL
, *iter
;
246 const char *iter_name
;
247 int offset
, len
, ret
;
250 offset
= fdt_next_node(fdt
, -1, NULL
);
252 while (offset
>= 0) {
253 iter_name
= fdt_get_name(fdt
, offset
, &len
);
258 if (!strcmp(iter_name
, name
) || g_str_has_prefix(iter_name
, prefix
)) {
261 path
= g_malloc(path_len
);
262 while ((ret
= fdt_get_path(fdt
, offset
, path
, path_len
))
263 == -FDT_ERR_NOSPACE
) {
265 path
= g_realloc(path
, path_len
);
267 path_list
= g_slist_prepend(path_list
, path
);
270 offset
= fdt_next_node(fdt
, offset
, NULL
);
274 if (offset
< 0 && offset
!= -FDT_ERR_NOTFOUND
) {
275 error_setg(errp
, "%s: abort parsing dt for %s node units: %s",
276 __func__
, name
, fdt_strerror(offset
));
277 for (iter
= path_list
; iter
; iter
= iter
->next
) {
280 g_slist_free(path_list
);
284 path_array
= g_new(char *, n
+ 1);
285 path_array
[n
--] = NULL
;
287 for (iter
= path_list
; iter
; iter
= iter
->next
) {
288 path_array
[n
--] = iter
->data
;
291 g_slist_free(path_list
);
296 char **qemu_fdt_node_path(void *fdt
, const char *name
, const char *compat
,
299 int offset
, len
, ret
;
300 const char *iter_name
;
301 unsigned int path_len
= 16, n
= 0;
302 GSList
*path_list
= NULL
, *iter
;
305 offset
= fdt_node_offset_by_compatible(fdt
, -1, compat
);
307 while (offset
>= 0) {
308 iter_name
= fdt_get_name(fdt
, offset
, &len
);
313 if (!name
|| !strcmp(iter_name
, name
)) {
316 path
= g_malloc(path_len
);
317 while ((ret
= fdt_get_path(fdt
, offset
, path
, path_len
))
318 == -FDT_ERR_NOSPACE
) {
320 path
= g_realloc(path
, path_len
);
322 path_list
= g_slist_prepend(path_list
, path
);
325 offset
= fdt_node_offset_by_compatible(fdt
, offset
, compat
);
328 if (offset
< 0 && offset
!= -FDT_ERR_NOTFOUND
) {
329 error_setg(errp
, "%s: abort parsing dt for %s/%s: %s",
330 __func__
, name
, compat
, fdt_strerror(offset
));
331 for (iter
= path_list
; iter
; iter
= iter
->next
) {
334 g_slist_free(path_list
);
338 path_array
= g_new(char *, n
+ 1);
339 path_array
[n
--] = NULL
;
341 for (iter
= path_list
; iter
; iter
= iter
->next
) {
342 path_array
[n
--] = iter
->data
;
345 g_slist_free(path_list
);
350 int qemu_fdt_setprop(void *fdt
, const char *node_path
,
351 const char *property
, const void *val
, int size
)
355 r
= fdt_setprop(fdt
, findnode_nofail(fdt
, node_path
), property
, val
, size
);
357 error_report("%s: Couldn't set %s/%s: %s", __func__
, node_path
,
358 property
, fdt_strerror(r
));
365 int qemu_fdt_setprop_cell(void *fdt
, const char *node_path
,
366 const char *property
, uint32_t val
)
370 r
= fdt_setprop_cell(fdt
, findnode_nofail(fdt
, node_path
), property
, val
);
372 error_report("%s: Couldn't set %s/%s = %#08x: %s", __func__
,
373 node_path
, property
, val
, fdt_strerror(r
));
380 int qemu_fdt_setprop_u64(void *fdt
, const char *node_path
,
381 const char *property
, uint64_t val
)
383 val
= cpu_to_be64(val
);
384 return qemu_fdt_setprop(fdt
, node_path
, property
, &val
, sizeof(val
));
387 int qemu_fdt_setprop_string(void *fdt
, const char *node_path
,
388 const char *property
, const char *string
)
392 r
= fdt_setprop_string(fdt
, findnode_nofail(fdt
, node_path
), property
, string
);
394 error_report("%s: Couldn't set %s/%s = %s: %s", __func__
,
395 node_path
, property
, string
, fdt_strerror(r
));
403 * libfdt doesn't allow us to add string arrays directly but they are
404 * test a series of null terminated strings with a length. We build
405 * the string up here so we can calculate the final length.
407 int qemu_fdt_setprop_string_array(void *fdt
, const char *node_path
,
408 const char *prop
, char **array
, int len
)
410 int ret
, i
, total_len
= 0;
412 for (i
= 0; i
< len
; i
++) {
413 total_len
+= strlen(array
[i
]) + 1;
415 p
= str
= g_malloc0(total_len
);
416 for (i
= 0; i
< len
; i
++) {
417 int len
= strlen(array
[i
]) + 1;
418 pstrcpy(p
, len
, array
[i
]);
422 ret
= qemu_fdt_setprop(fdt
, node_path
, prop
, str
, total_len
);
427 const void *qemu_fdt_getprop(void *fdt
, const char *node_path
,
428 const char *property
, int *lenp
, Error
**errp
)
436 r
= fdt_getprop(fdt
, findnode_nofail(fdt
, node_path
), property
, lenp
);
438 error_setg(errp
, "%s: Couldn't get %s/%s: %s", __func__
,
439 node_path
, property
, fdt_strerror(*lenp
));
444 uint32_t qemu_fdt_getprop_cell(void *fdt
, const char *node_path
,
445 const char *property
, int *lenp
, Error
**errp
)
453 p
= qemu_fdt_getprop(fdt
, node_path
, property
, lenp
, errp
);
456 } else if (*lenp
!= 4) {
457 error_setg(errp
, "%s: %s/%s not 4 bytes long (not a cell?)",
458 __func__
, node_path
, property
);
462 return be32_to_cpu(*p
);
465 uint32_t qemu_fdt_get_phandle(void *fdt
, const char *path
)
469 r
= fdt_get_phandle(fdt
, findnode_nofail(fdt
, path
));
471 error_report("%s: Couldn't get phandle for %s: %s", __func__
,
472 path
, fdt_strerror(r
));
479 int qemu_fdt_setprop_phandle(void *fdt
, const char *node_path
,
480 const char *property
,
481 const char *target_node_path
)
483 uint32_t phandle
= qemu_fdt_get_phandle(fdt
, target_node_path
);
484 return qemu_fdt_setprop_cell(fdt
, node_path
, property
, phandle
);
487 uint32_t qemu_fdt_alloc_phandle(void *fdt
)
489 static int phandle
= 0x0;
492 * We need to find out if the user gave us special instruction at
493 * which phandle id to start allocating phandles.
496 phandle
= machine_phandle_start(current_machine
);
501 * None or invalid phandle given on the command line, so fall back to
502 * default starting point.
510 int qemu_fdt_nop_node(void *fdt
, const char *node_path
)
514 r
= fdt_nop_node(fdt
, findnode_nofail(fdt
, node_path
));
516 error_report("%s: Couldn't nop node %s: %s", __func__
, node_path
,
524 int qemu_fdt_add_subnode(void *fdt
, const char *name
)
526 char *dupname
= g_strdup(name
);
527 char *basename
= strrchr(dupname
, '/');
540 parent
= findnode_nofail(fdt
, dupname
);
543 retval
= fdt_add_subnode(fdt
, parent
, basename
);
545 error_report("%s: Failed to create subnode %s: %s",
546 __func__
, name
, fdt_strerror(retval
));
555 * qemu_fdt_add_path: Like qemu_fdt_add_subnode(), but will add
556 * all missing subnodes from the given path.
558 int qemu_fdt_add_path(void *fdt
, const char *path
)
564 if (path
[0] != '/') {
570 path
= strchr(name
, '/');
571 namelen
= path
!= NULL
? path
- name
: strlen(name
);
573 retval
= fdt_subnode_offset_namelen(fdt
, parent
, name
, namelen
);
574 if (retval
< 0 && retval
!= -FDT_ERR_NOTFOUND
) {
575 error_report("%s: Unexpected error in finding subnode %.*s: %s",
576 __func__
, namelen
, name
, fdt_strerror(retval
));
578 } else if (retval
== -FDT_ERR_NOTFOUND
) {
579 retval
= fdt_add_subnode_namelen(fdt
, parent
, name
, namelen
);
581 error_report("%s: Failed to create subnode %.*s: %s",
582 __func__
, namelen
, name
, fdt_strerror(retval
));
593 void qemu_fdt_dumpdtb(void *fdt
, int size
)
595 const char *dumpdtb
= current_machine
->dumpdtb
;
598 /* Dump the dtb to a file and quit */
599 if (g_file_set_contents(dumpdtb
, fdt
, size
, NULL
)) {
600 info_report("dtb dumped to %s. Exiting.", dumpdtb
);
603 error_report("%s: Failed dumping dtb to %s", __func__
, dumpdtb
);
608 int qemu_fdt_setprop_sized_cells_from_array(void *fdt
,
609 const char *node_path
,
610 const char *property
,
616 int cellnum
, vnum
, ncells
;
620 propcells
= g_new0(uint32_t, numvalues
* 2);
623 for (vnum
= 0; vnum
< numvalues
; vnum
++) {
624 ncells
= values
[vnum
* 2];
625 if (ncells
!= 1 && ncells
!= 2) {
629 value
= values
[vnum
* 2 + 1];
630 hival
= cpu_to_be32(value
>> 32);
632 propcells
[cellnum
++] = hival
;
633 } else if (hival
!= 0) {
637 propcells
[cellnum
++] = cpu_to_be32(value
);
640 ret
= qemu_fdt_setprop(fdt
, node_path
, property
, propcells
,
641 cellnum
* sizeof(uint32_t));