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("Unable to copy device tree in memory");
69 error_report("%s Couldn't create dt: %s", __func__
, fdt_strerror(ret
));
73 void *load_device_tree(const char *filename_path
, int *sizep
)
76 int dt_file_load_size
;
81 dt_size
= get_image_size(filename_path
);
83 error_report("Unable to get size of device tree file '%s'",
87 if (dt_size
> INT_MAX
/ 2 - 10000) {
88 error_report("Device tree file '%s' is too large", filename_path
);
92 /* Expand to 2x size to give enough room for manipulation. */
95 /* First allocate space in qemu for device tree */
96 fdt
= g_malloc0(dt_size
);
98 dt_file_load_size
= load_image_size(filename_path
, fdt
, dt_size
);
99 if (dt_file_load_size
< 0) {
100 error_report("Unable to open device tree file '%s'",
105 ret
= fdt_open_into(fdt
, fdt
, dt_size
);
107 error_report("Unable to copy device tree in memory");
111 /* Check sanity of device tree */
112 if (fdt_check_header(fdt
)) {
113 error_report("Device tree file loaded into memory is invalid: %s",
127 #define SYSFS_DT_BASEDIR "/proc/device-tree"
130 * read_fstree: this function is inspired from dtc read_fstree
131 * @fdt: preallocated fdt blob buffer, to be populated
132 * @dirname: directory to scan under SYSFS_DT_BASEDIR
133 * the search is recursive and the tree is searched down to the
134 * leaves (property files).
136 * the function asserts in case of error
138 static void read_fstree(void *fdt
, const char *dirname
)
143 const char *root_dir
= SYSFS_DT_BASEDIR
;
144 const char *parent_node
;
146 if (strstr(dirname
, root_dir
) != dirname
) {
147 error_report("%s: %s must be searched within %s",
148 __func__
, dirname
, root_dir
);
151 parent_node
= &dirname
[strlen(SYSFS_DT_BASEDIR
)];
153 d
= opendir(dirname
);
155 error_report("%s cannot open %s", __func__
, dirname
);
159 while ((de
= readdir(d
)) != NULL
) {
162 if (!g_strcmp0(de
->d_name
, ".")
163 || !g_strcmp0(de
->d_name
, "..")) {
167 tmpnam
= g_strdup_printf("%s/%s", dirname
, de
->d_name
);
169 if (lstat(tmpnam
, &st
) < 0) {
170 error_report("%s cannot lstat %s", __func__
, tmpnam
);
174 if (S_ISREG(st
.st_mode
)) {
178 if (!g_file_get_contents(tmpnam
, &val
, &len
, NULL
)) {
179 error_report("%s not able to extract info from %s",
184 if (strlen(parent_node
) > 0) {
185 qemu_fdt_setprop(fdt
, parent_node
,
186 de
->d_name
, val
, len
);
188 qemu_fdt_setprop(fdt
, "/", de
->d_name
, val
, len
);
191 } else if (S_ISDIR(st
.st_mode
)) {
194 node_name
= g_strdup_printf("%s/%s",
195 parent_node
, de
->d_name
);
196 qemu_fdt_add_subnode(fdt
, node_name
);
198 read_fstree(fdt
, tmpnam
);
207 /* load_device_tree_from_sysfs: extract the dt blob from host sysfs */
208 void *load_device_tree_from_sysfs(void)
213 host_fdt
= create_device_tree(&host_fdt_size
);
214 read_fstree(host_fdt
, SYSFS_DT_BASEDIR
);
215 if (fdt_check_header(host_fdt
)) {
216 error_report("%s host device tree extracted into memory is invalid",
223 #endif /* CONFIG_LINUX */
225 static int findnode_nofail(void *fdt
, const char *node_path
)
229 offset
= fdt_path_offset(fdt
, node_path
);
231 error_report("%s Couldn't find node %s: %s", __func__
, node_path
,
232 fdt_strerror(offset
));
239 char **qemu_fdt_node_unit_path(void *fdt
, const char *name
, Error
**errp
)
241 char *prefix
= g_strdup_printf("%s@", name
);
242 unsigned int path_len
= 16, n
= 0;
243 GSList
*path_list
= NULL
, *iter
;
244 const char *iter_name
;
245 int offset
, len
, ret
;
248 offset
= fdt_next_node(fdt
, -1, NULL
);
250 while (offset
>= 0) {
251 iter_name
= fdt_get_name(fdt
, offset
, &len
);
256 if (!strcmp(iter_name
, name
) || g_str_has_prefix(iter_name
, prefix
)) {
259 path
= g_malloc(path_len
);
260 while ((ret
= fdt_get_path(fdt
, offset
, path
, path_len
))
261 == -FDT_ERR_NOSPACE
) {
263 path
= g_realloc(path
, path_len
);
265 path_list
= g_slist_prepend(path_list
, path
);
268 offset
= fdt_next_node(fdt
, offset
, NULL
);
272 if (offset
< 0 && offset
!= -FDT_ERR_NOTFOUND
) {
273 error_setg(errp
, "%s: abort parsing dt for %s node units: %s",
274 __func__
, name
, fdt_strerror(offset
));
275 for (iter
= path_list
; iter
; iter
= iter
->next
) {
278 g_slist_free(path_list
);
282 path_array
= g_new(char *, n
+ 1);
283 path_array
[n
--] = NULL
;
285 for (iter
= path_list
; iter
; iter
= iter
->next
) {
286 path_array
[n
--] = iter
->data
;
289 g_slist_free(path_list
);
294 char **qemu_fdt_node_path(void *fdt
, const char *name
, const char *compat
,
297 int offset
, len
, ret
;
298 const char *iter_name
;
299 unsigned int path_len
= 16, n
= 0;
300 GSList
*path_list
= NULL
, *iter
;
303 offset
= fdt_node_offset_by_compatible(fdt
, -1, compat
);
305 while (offset
>= 0) {
306 iter_name
= fdt_get_name(fdt
, offset
, &len
);
311 if (!name
|| !strcmp(iter_name
, name
)) {
314 path
= g_malloc(path_len
);
315 while ((ret
= fdt_get_path(fdt
, offset
, path
, path_len
))
316 == -FDT_ERR_NOSPACE
) {
318 path
= g_realloc(path
, path_len
);
320 path_list
= g_slist_prepend(path_list
, path
);
323 offset
= fdt_node_offset_by_compatible(fdt
, offset
, compat
);
326 if (offset
< 0 && offset
!= -FDT_ERR_NOTFOUND
) {
327 error_setg(errp
, "%s: abort parsing dt for %s/%s: %s",
328 __func__
, name
, compat
, fdt_strerror(offset
));
329 for (iter
= path_list
; iter
; iter
= iter
->next
) {
332 g_slist_free(path_list
);
336 path_array
= g_new(char *, n
+ 1);
337 path_array
[n
--] = NULL
;
339 for (iter
= path_list
; iter
; iter
= iter
->next
) {
340 path_array
[n
--] = iter
->data
;
343 g_slist_free(path_list
);
348 int qemu_fdt_setprop(void *fdt
, const char *node_path
,
349 const char *property
, const void *val
, int size
)
353 r
= fdt_setprop(fdt
, findnode_nofail(fdt
, node_path
), property
, val
, size
);
355 error_report("%s: Couldn't set %s/%s: %s", __func__
, node_path
,
356 property
, fdt_strerror(r
));
363 int qemu_fdt_setprop_cell(void *fdt
, const char *node_path
,
364 const char *property
, uint32_t val
)
368 r
= fdt_setprop_cell(fdt
, findnode_nofail(fdt
, node_path
), property
, val
);
370 error_report("%s: Couldn't set %s/%s = %#08x: %s", __func__
,
371 node_path
, property
, val
, fdt_strerror(r
));
378 int qemu_fdt_setprop_u64(void *fdt
, const char *node_path
,
379 const char *property
, uint64_t val
)
381 val
= cpu_to_be64(val
);
382 return qemu_fdt_setprop(fdt
, node_path
, property
, &val
, sizeof(val
));
385 int qemu_fdt_setprop_string(void *fdt
, const char *node_path
,
386 const char *property
, const char *string
)
390 r
= fdt_setprop_string(fdt
, findnode_nofail(fdt
, node_path
), property
, string
);
392 error_report("%s: Couldn't set %s/%s = %s: %s", __func__
,
393 node_path
, property
, string
, fdt_strerror(r
));
401 * libfdt doesn't allow us to add string arrays directly but they are
402 * test a series of null terminated strings with a length. We build
403 * the string up here so we can calculate the final length.
405 int qemu_fdt_setprop_string_array(void *fdt
, const char *node_path
,
406 const char *prop
, char **array
, int len
)
408 int ret
, i
, total_len
= 0;
410 for (i
= 0; i
< len
; i
++) {
411 total_len
+= strlen(array
[i
]) + 1;
413 p
= str
= g_malloc0(total_len
);
414 for (i
= 0; i
< len
; i
++) {
415 int len
= strlen(array
[i
]) + 1;
416 pstrcpy(p
, len
, array
[i
]);
420 ret
= qemu_fdt_setprop(fdt
, node_path
, prop
, str
, total_len
);
425 const void *qemu_fdt_getprop(void *fdt
, const char *node_path
,
426 const char *property
, int *lenp
, Error
**errp
)
434 r
= fdt_getprop(fdt
, findnode_nofail(fdt
, node_path
), property
, lenp
);
436 error_setg(errp
, "%s: Couldn't get %s/%s: %s", __func__
,
437 node_path
, property
, fdt_strerror(*lenp
));
442 uint32_t qemu_fdt_getprop_cell(void *fdt
, const char *node_path
,
443 const char *property
, int *lenp
, Error
**errp
)
451 p
= qemu_fdt_getprop(fdt
, node_path
, property
, lenp
, errp
);
454 } else if (*lenp
!= 4) {
455 error_setg(errp
, "%s: %s/%s not 4 bytes long (not a cell?)",
456 __func__
, node_path
, property
);
460 return be32_to_cpu(*p
);
463 uint32_t qemu_fdt_get_phandle(void *fdt
, const char *path
)
467 r
= fdt_get_phandle(fdt
, findnode_nofail(fdt
, path
));
469 error_report("%s: Couldn't get phandle for %s: %s", __func__
,
470 path
, fdt_strerror(r
));
477 int qemu_fdt_setprop_phandle(void *fdt
, const char *node_path
,
478 const char *property
,
479 const char *target_node_path
)
481 uint32_t phandle
= qemu_fdt_get_phandle(fdt
, target_node_path
);
482 return qemu_fdt_setprop_cell(fdt
, node_path
, property
, phandle
);
485 uint32_t qemu_fdt_alloc_phandle(void *fdt
)
487 static int phandle
= 0x0;
490 * We need to find out if the user gave us special instruction at
491 * which phandle id to start allocating phandles.
494 phandle
= machine_phandle_start(current_machine
);
499 * None or invalid phandle given on the command line, so fall back to
500 * default starting point.
508 int qemu_fdt_nop_node(void *fdt
, const char *node_path
)
512 r
= fdt_nop_node(fdt
, findnode_nofail(fdt
, node_path
));
514 error_report("%s: Couldn't nop node %s: %s", __func__
, node_path
,
522 int qemu_fdt_add_subnode(void *fdt
, const char *name
)
524 char *dupname
= g_strdup(name
);
525 char *basename
= strrchr(dupname
, '/');
538 parent
= findnode_nofail(fdt
, dupname
);
541 retval
= fdt_add_subnode(fdt
, parent
, basename
);
543 error_report("%s: Failed to create subnode %s: %s",
544 __func__
, name
, fdt_strerror(retval
));
553 * qemu_fdt_add_path: Like qemu_fdt_add_subnode(), but will add
554 * all missing subnodes from the given path.
556 int qemu_fdt_add_path(void *fdt
, const char *path
)
559 const char *p
= path
;
563 if (path
[0] != '/') {
569 p
= strchr(name
, '/');
570 namelen
= p
!= NULL
? p
- name
: strlen(name
);
572 retval
= fdt_subnode_offset_namelen(fdt
, parent
, name
, namelen
);
573 if (retval
< 0 && retval
!= -FDT_ERR_NOTFOUND
) {
574 error_report("%s: Unexpected error in finding subnode %.*s: %s",
575 __func__
, namelen
, name
, fdt_strerror(retval
));
577 } else if (retval
== -FDT_ERR_NOTFOUND
) {
578 retval
= fdt_add_subnode_namelen(fdt
, parent
, name
, namelen
);
580 error_report("%s: Failed to create subnode %.*s: %s",
581 __func__
, namelen
, name
, fdt_strerror(retval
));
592 void qemu_fdt_dumpdtb(void *fdt
, int size
)
594 const char *dumpdtb
= current_machine
->dumpdtb
;
597 /* Dump the dtb to a file and quit */
598 if (g_file_set_contents(dumpdtb
, fdt
, size
, NULL
)) {
599 info_report("dtb dumped to %s. Exiting.", dumpdtb
);
602 error_report("%s: Failed dumping dtb to %s", __func__
, dumpdtb
);
607 int qemu_fdt_setprop_sized_cells_from_array(void *fdt
,
608 const char *node_path
,
609 const char *property
,
615 int cellnum
, vnum
, ncells
;
619 propcells
= g_new0(uint32_t, numvalues
* 2);
622 for (vnum
= 0; vnum
< numvalues
; vnum
++) {
623 ncells
= values
[vnum
* 2];
624 if (ncells
!= 1 && ncells
!= 2) {
628 value
= values
[vnum
* 2 + 1];
629 hival
= cpu_to_be32(value
>> 32);
631 propcells
[cellnum
++] = hival
;
632 } else if (hival
!= 0) {
636 propcells
[cellnum
++] = cpu_to_be32(value
);
639 ret
= qemu_fdt_setprop(fdt
, node_path
, property
, propcells
,
640 cellnum
* sizeof(uint32_t));