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-common.h"
22 #include "qemu/error-report.h"
23 #include "sysemu/device_tree.h"
24 #include "sysemu/sysemu.h"
25 #include "hw/loader.h"
26 #include "hw/boards.h"
27 #include "qemu/config-file.h"
31 #define FDT_MAX_SIZE 0x10000
33 void *create_device_tree(int *sizep
)
38 *sizep
= FDT_MAX_SIZE
;
39 fdt
= g_malloc0(FDT_MAX_SIZE
);
40 ret
= fdt_create(fdt
, FDT_MAX_SIZE
);
44 ret
= fdt_finish_reservemap(fdt
);
48 ret
= fdt_begin_node(fdt
, "");
52 ret
= fdt_end_node(fdt
);
56 ret
= fdt_finish(fdt
);
60 ret
= fdt_open_into(fdt
, fdt
, *sizep
);
62 error_report("Unable to copy device tree in memory");
68 error_report("%s Couldn't create dt: %s", __func__
, fdt_strerror(ret
));
72 void *load_device_tree(const char *filename_path
, int *sizep
)
75 int dt_file_load_size
;
80 dt_size
= get_image_size(filename_path
);
82 error_report("Unable to get size of device tree file '%s'",
87 /* Expand to 2x size to give enough room for manipulation. */
90 /* First allocate space in qemu for device tree */
91 fdt
= g_malloc0(dt_size
);
93 dt_file_load_size
= load_image(filename_path
, fdt
);
94 if (dt_file_load_size
< 0) {
95 error_report("Unable to open device tree file '%s'",
100 ret
= fdt_open_into(fdt
, fdt
, dt_size
);
102 error_report("Unable to copy device tree in memory");
106 /* Check sanity of device tree */
107 if (fdt_check_header(fdt
)) {
108 error_report("Device tree file loaded into memory is invalid: %s",
122 #define SYSFS_DT_BASEDIR "/proc/device-tree"
125 * read_fstree: this function is inspired from dtc read_fstree
126 * @fdt: preallocated fdt blob buffer, to be populated
127 * @dirname: directory to scan under SYSFS_DT_BASEDIR
128 * the search is recursive and the tree is searched down to the
129 * leaves (property files).
131 * the function asserts in case of error
133 static void read_fstree(void *fdt
, const char *dirname
)
138 const char *root_dir
= SYSFS_DT_BASEDIR
;
139 const char *parent_node
;
141 if (strstr(dirname
, root_dir
) != dirname
) {
142 error_setg(&error_fatal
, "%s: %s must be searched within %s",
143 __func__
, dirname
, root_dir
);
145 parent_node
= &dirname
[strlen(SYSFS_DT_BASEDIR
)];
147 d
= opendir(dirname
);
149 error_setg(&error_fatal
, "%s cannot open %s", __func__
, dirname
);
152 while ((de
= readdir(d
)) != NULL
) {
155 if (!g_strcmp0(de
->d_name
, ".")
156 || !g_strcmp0(de
->d_name
, "..")) {
160 tmpnam
= g_strdup_printf("%s/%s", dirname
, de
->d_name
);
162 if (lstat(tmpnam
, &st
) < 0) {
163 error_setg(&error_fatal
, "%s cannot lstat %s", __func__
, tmpnam
);
166 if (S_ISREG(st
.st_mode
)) {
170 if (!g_file_get_contents(tmpnam
, &val
, &len
, NULL
)) {
171 error_setg(&error_fatal
, "%s not able to extract info from %s",
175 if (strlen(parent_node
) > 0) {
176 qemu_fdt_setprop(fdt
, parent_node
,
177 de
->d_name
, val
, len
);
179 qemu_fdt_setprop(fdt
, "/", de
->d_name
, val
, len
);
182 } else if (S_ISDIR(st
.st_mode
)) {
185 node_name
= g_strdup_printf("%s/%s",
186 parent_node
, de
->d_name
);
187 qemu_fdt_add_subnode(fdt
, node_name
);
189 read_fstree(fdt
, tmpnam
);
198 /* load_device_tree_from_sysfs: extract the dt blob from host sysfs */
199 void *load_device_tree_from_sysfs(void)
204 host_fdt
= create_device_tree(&host_fdt_size
);
205 read_fstree(host_fdt
, SYSFS_DT_BASEDIR
);
206 if (fdt_check_header(host_fdt
)) {
207 error_setg(&error_fatal
,
208 "%s host device tree extracted into memory is invalid",
214 #endif /* CONFIG_LINUX */
216 static int findnode_nofail(void *fdt
, const char *node_path
)
220 offset
= fdt_path_offset(fdt
, node_path
);
222 error_report("%s Couldn't find node %s: %s", __func__
, node_path
,
223 fdt_strerror(offset
));
230 char **qemu_fdt_node_path(void *fdt
, const char *name
, char *compat
,
233 int offset
, len
, ret
;
234 const char *iter_name
;
235 unsigned int path_len
= 16, n
= 0;
236 GSList
*path_list
= NULL
, *iter
;
239 offset
= fdt_node_offset_by_compatible(fdt
, -1, compat
);
241 while (offset
>= 0) {
242 iter_name
= fdt_get_name(fdt
, offset
, &len
);
247 if (!strcmp(iter_name
, name
)) {
250 path
= g_malloc(path_len
);
251 while ((ret
= fdt_get_path(fdt
, offset
, path
, path_len
))
252 == -FDT_ERR_NOSPACE
) {
254 path
= g_realloc(path
, path_len
);
256 path_list
= g_slist_prepend(path_list
, path
);
259 offset
= fdt_node_offset_by_compatible(fdt
, offset
, compat
);
262 if (offset
< 0 && offset
!= -FDT_ERR_NOTFOUND
) {
263 error_setg(errp
, "%s: abort parsing dt for %s/%s: %s",
264 __func__
, name
, compat
, fdt_strerror(offset
));
265 for (iter
= path_list
; iter
; iter
= iter
->next
) {
268 g_slist_free(path_list
);
272 path_array
= g_new(char *, n
+ 1);
273 path_array
[n
--] = NULL
;
275 for (iter
= path_list
; iter
; iter
= iter
->next
) {
276 path_array
[n
--] = iter
->data
;
279 g_slist_free(path_list
);
284 int qemu_fdt_setprop(void *fdt
, const char *node_path
,
285 const char *property
, const void *val
, int size
)
289 r
= fdt_setprop(fdt
, findnode_nofail(fdt
, node_path
), property
, val
, size
);
291 error_report("%s: Couldn't set %s/%s: %s", __func__
, node_path
,
292 property
, fdt_strerror(r
));
299 int qemu_fdt_setprop_cell(void *fdt
, const char *node_path
,
300 const char *property
, uint32_t val
)
304 r
= fdt_setprop_cell(fdt
, findnode_nofail(fdt
, node_path
), property
, val
);
306 error_report("%s: Couldn't set %s/%s = %#08x: %s", __func__
,
307 node_path
, property
, val
, fdt_strerror(r
));
314 int qemu_fdt_setprop_u64(void *fdt
, const char *node_path
,
315 const char *property
, uint64_t val
)
317 val
= cpu_to_be64(val
);
318 return qemu_fdt_setprop(fdt
, node_path
, property
, &val
, sizeof(val
));
321 int qemu_fdt_setprop_string(void *fdt
, const char *node_path
,
322 const char *property
, const char *string
)
326 r
= fdt_setprop_string(fdt
, findnode_nofail(fdt
, node_path
), property
, string
);
328 error_report("%s: Couldn't set %s/%s = %s: %s", __func__
,
329 node_path
, property
, string
, fdt_strerror(r
));
336 const void *qemu_fdt_getprop(void *fdt
, const char *node_path
,
337 const char *property
, int *lenp
, Error
**errp
)
345 r
= fdt_getprop(fdt
, findnode_nofail(fdt
, node_path
), property
, lenp
);
347 error_setg(errp
, "%s: Couldn't get %s/%s: %s", __func__
,
348 node_path
, property
, fdt_strerror(*lenp
));
353 uint32_t qemu_fdt_getprop_cell(void *fdt
, const char *node_path
,
354 const char *property
, int *lenp
, Error
**errp
)
362 p
= qemu_fdt_getprop(fdt
, node_path
, property
, lenp
, errp
);
365 } else if (*lenp
!= 4) {
366 error_setg(errp
, "%s: %s/%s not 4 bytes long (not a cell?)",
367 __func__
, node_path
, property
);
371 return be32_to_cpu(*p
);
374 uint32_t qemu_fdt_get_phandle(void *fdt
, const char *path
)
378 r
= fdt_get_phandle(fdt
, findnode_nofail(fdt
, path
));
380 error_report("%s: Couldn't get phandle for %s: %s", __func__
,
381 path
, fdt_strerror(r
));
388 int qemu_fdt_setprop_phandle(void *fdt
, const char *node_path
,
389 const char *property
,
390 const char *target_node_path
)
392 uint32_t phandle
= qemu_fdt_get_phandle(fdt
, target_node_path
);
393 return qemu_fdt_setprop_cell(fdt
, node_path
, property
, phandle
);
396 uint32_t qemu_fdt_alloc_phandle(void *fdt
)
398 static int phandle
= 0x0;
401 * We need to find out if the user gave us special instruction at
402 * which phandle id to start allocating phandles.
405 phandle
= machine_phandle_start(current_machine
);
410 * None or invalid phandle given on the command line, so fall back to
411 * default starting point.
419 int qemu_fdt_nop_node(void *fdt
, const char *node_path
)
423 r
= fdt_nop_node(fdt
, findnode_nofail(fdt
, node_path
));
425 error_report("%s: Couldn't nop node %s: %s", __func__
, node_path
,
433 int qemu_fdt_add_subnode(void *fdt
, const char *name
)
435 char *dupname
= g_strdup(name
);
436 char *basename
= strrchr(dupname
, '/');
449 parent
= findnode_nofail(fdt
, dupname
);
452 retval
= fdt_add_subnode(fdt
, parent
, basename
);
454 error_report("FDT: Failed to create subnode %s: %s", name
,
455 fdt_strerror(retval
));
463 void qemu_fdt_dumpdtb(void *fdt
, int size
)
465 const char *dumpdtb
= qemu_opt_get(qemu_get_machine_opts(), "dumpdtb");
468 /* Dump the dtb to a file and quit */
469 exit(g_file_set_contents(dumpdtb
, fdt
, size
, NULL
) ? 0 : 1);
473 int qemu_fdt_setprop_sized_cells_from_array(void *fdt
,
474 const char *node_path
,
475 const char *property
,
481 int cellnum
, vnum
, ncells
;
485 propcells
= g_new0(uint32_t, numvalues
* 2);
488 for (vnum
= 0; vnum
< numvalues
; vnum
++) {
489 ncells
= values
[vnum
* 2];
490 if (ncells
!= 1 && ncells
!= 2) {
494 value
= values
[vnum
* 2 + 1];
495 hival
= cpu_to_be32(value
>> 32);
497 propcells
[cellnum
++] = hival
;
498 } else if (hival
!= 0) {
502 propcells
[cellnum
++] = cpu_to_be32(value
);
505 ret
= qemu_fdt_setprop(fdt
, node_path
, property
, propcells
,
506 cellnum
* sizeof(uint32_t));