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 "qemu/guest-random.h"
26 #include "sysemu/device_tree.h"
27 #include "hw/loader.h"
28 #include "hw/boards.h"
29 #include "qemu/config-file.h"
30 #include "qapi/qapi-commands-machine.h"
31 #include "qapi/qmp/qdict.h"
32 #include "monitor/hmp.h"
36 #define FDT_MAX_SIZE 0x100000
38 void *create_device_tree(int *sizep)
43 *sizep = FDT_MAX_SIZE;
44 fdt = g_malloc0(FDT_MAX_SIZE);
45 ret = fdt_create(fdt, FDT_MAX_SIZE);
49 ret = fdt_finish_reservemap(fdt);
53 ret = fdt_begin_node(fdt, "");
57 ret = fdt_end_node(fdt);
61 ret = fdt_finish(fdt);
65 ret = fdt_open_into(fdt, fdt, *sizep);
67 error_report("%s: Unable to copy device tree into memory: %s",
68 __func__, fdt_strerror(ret));
74 error_report("%s Couldn't create dt: %s", __func__, fdt_strerror(ret));
78 void *load_device_tree(const char *filename_path, int *sizep)
81 int dt_file_load_size;
86 dt_size = get_image_size(filename_path);
88 error_report("Unable to get size of device tree file '%s'",
92 if (dt_size > INT_MAX / 2 - 10000) {
93 error_report("Device tree file '%s' is too large", filename_path);
97 /* Expand to 2x size to give enough room for manipulation. */
100 /* First allocate space in qemu for device tree */
101 fdt = g_malloc0(dt_size);
103 dt_file_load_size = load_image_size(filename_path, fdt, dt_size);
104 if (dt_file_load_size < 0) {
105 error_report("Unable to open device tree file '%s'",
110 ret = fdt_open_into(fdt, fdt, dt_size);
112 error_report("%s: Unable to copy device tree into memory: %s",
113 __func__, fdt_strerror(ret));
117 /* Check sanity of device tree */
118 if (fdt_check_header(fdt)) {
119 error_report("Device tree file loaded into memory is invalid: %s",
133 #define SYSFS_DT_BASEDIR "/proc/device-tree"
136 * read_fstree: this function is inspired from dtc read_fstree
137 * @fdt: preallocated fdt blob buffer, to be populated
138 * @dirname: directory to scan under SYSFS_DT_BASEDIR
139 * the search is recursive and the tree is searched down to the
140 * leaves (property files).
142 * the function asserts in case of error
144 static void read_fstree(void *fdt, const char *dirname)
149 const char *root_dir = SYSFS_DT_BASEDIR;
150 const char *parent_node;
152 if (strstr(dirname, root_dir) != dirname) {
153 error_report("%s: %s must be searched within %s",
154 __func__, dirname, root_dir);
157 parent_node = &dirname[strlen(SYSFS_DT_BASEDIR)];
159 d = opendir(dirname);
161 error_report("%s cannot open %s", __func__, dirname);
165 while ((de = readdir(d)) != NULL) {
168 if (!g_strcmp0(de->d_name, ".")
169 || !g_strcmp0(de->d_name, "..")) {
173 tmpnam = g_strdup_printf("%s/%s", dirname, de->d_name);
175 if (lstat(tmpnam, &st) < 0) {
176 error_report("%s cannot lstat %s", __func__, tmpnam);
180 if (S_ISREG(st.st_mode)) {
184 if (!g_file_get_contents(tmpnam, &val, &len, NULL)) {
185 error_report("%s not able to extract info from %s",
190 if (strlen(parent_node) > 0) {
191 qemu_fdt_setprop(fdt, parent_node,
192 de->d_name, val, len);
194 qemu_fdt_setprop(fdt, "/", de->d_name, val, len);
197 } else if (S_ISDIR(st.st_mode)) {
200 node_name = g_strdup_printf("%s/%s",
201 parent_node, de->d_name);
202 qemu_fdt_add_subnode(fdt, node_name);
204 read_fstree(fdt, tmpnam);
213 /* load_device_tree_from_sysfs: extract the dt blob from host sysfs */
214 void *load_device_tree_from_sysfs(void)
219 host_fdt = create_device_tree(&host_fdt_size);
220 read_fstree(host_fdt, SYSFS_DT_BASEDIR);
221 if (fdt_check_header(host_fdt)) {
222 error_report("%s host device tree extracted into memory is invalid",
229 #endif /* CONFIG_LINUX */
231 static int findnode_nofail(void *fdt, const char *node_path)
235 offset = fdt_path_offset(fdt, node_path);
237 error_report("%s Couldn't find node %s: %s", __func__, node_path,
238 fdt_strerror(offset));
245 char **qemu_fdt_node_unit_path(void *fdt, const char *name, Error **errp)
247 char *prefix = g_strdup_printf("%s@", name);
248 unsigned int path_len = 16, n = 0;
249 GSList *path_list = NULL, *iter;
250 const char *iter_name;
251 int offset, len, ret;
254 offset = fdt_next_node(fdt, -1, NULL);
256 while (offset >= 0) {
257 iter_name = fdt_get_name(fdt, offset, &len);
262 if (!strcmp(iter_name, name) || g_str_has_prefix(iter_name, prefix)) {
265 path = g_malloc(path_len);
266 while ((ret = fdt_get_path(fdt, offset, path, path_len))
267 == -FDT_ERR_NOSPACE) {
269 path = g_realloc(path, path_len);
271 path_list = g_slist_prepend(path_list, path);
274 offset = fdt_next_node(fdt, offset, NULL);
278 if (offset < 0 && offset != -FDT_ERR_NOTFOUND) {
279 error_setg(errp, "%s: abort parsing dt for %s node units: %s",
280 __func__, name, fdt_strerror(offset));
281 for (iter = path_list; iter; iter = iter->next) {
284 g_slist_free(path_list);
288 path_array = g_new(char *, n + 1);
289 path_array[n--] = NULL;
291 for (iter = path_list; iter; iter = iter->next) {
292 path_array[n--] = iter->data;
295 g_slist_free(path_list);
300 char **qemu_fdt_node_path(void *fdt, const char *name, const char *compat,
303 int offset, len, ret;
304 const char *iter_name;
305 unsigned int path_len = 16, n = 0;
306 GSList *path_list = NULL, *iter;
309 offset = fdt_node_offset_by_compatible(fdt, -1, compat);
311 while (offset >= 0) {
312 iter_name = fdt_get_name(fdt, offset, &len);
317 if (!name || !strcmp(iter_name, name)) {
320 path = g_malloc(path_len);
321 while ((ret = fdt_get_path(fdt, offset, path, path_len))
322 == -FDT_ERR_NOSPACE) {
324 path = g_realloc(path, path_len);
326 path_list = g_slist_prepend(path_list, path);
329 offset = fdt_node_offset_by_compatible(fdt, offset, compat);
332 if (offset < 0 && offset != -FDT_ERR_NOTFOUND) {
333 error_setg(errp, "%s: abort parsing dt for %s/%s: %s",
334 __func__, name, compat, fdt_strerror(offset));
335 for (iter = path_list; iter; iter = iter->next) {
338 g_slist_free(path_list);
342 path_array = g_new(char *, n + 1);
343 path_array[n--] = NULL;
345 for (iter = path_list; iter; iter = iter->next) {
346 path_array[n--] = iter->data;
349 g_slist_free(path_list);
354 int qemu_fdt_setprop(void *fdt, const char *node_path,
355 const char *property, const void *val, int size)
359 r = fdt_setprop(fdt, findnode_nofail(fdt, node_path), property, val, size);
361 error_report("%s: Couldn't set %s/%s: %s", __func__, node_path,
362 property, fdt_strerror(r));
369 int qemu_fdt_setprop_cell(void *fdt, const char *node_path,
370 const char *property, uint32_t val)
374 r = fdt_setprop_cell(fdt, findnode_nofail(fdt, node_path), property, val);
376 error_report("%s: Couldn't set %s/%s = %#08x: %s", __func__,
377 node_path, property, val, fdt_strerror(r));
384 int qemu_fdt_setprop_u64(void *fdt, const char *node_path,
385 const char *property, uint64_t val)
387 val = cpu_to_be64(val);
388 return qemu_fdt_setprop(fdt, node_path, property, &val, sizeof(val));
391 int qemu_fdt_setprop_string(void *fdt, const char *node_path,
392 const char *property, const char *string)
396 r = fdt_setprop_string(fdt, findnode_nofail(fdt, node_path), property, string);
398 error_report("%s: Couldn't set %s/%s = %s: %s", __func__,
399 node_path, property, string, fdt_strerror(r));
407 * libfdt doesn't allow us to add string arrays directly but they are
408 * test a series of null terminated strings with a length. We build
409 * the string up here so we can calculate the final length.
411 int qemu_fdt_setprop_string_array(void *fdt, const char *node_path,
412 const char *prop, char **array, int len)
414 int ret, i, total_len = 0;
416 for (i = 0; i < len; i++) {
417 total_len += strlen(array[i]) + 1;
419 p = str = g_malloc0(total_len);
420 for (i = 0; i < len; i++) {
421 int offset = strlen(array[i]) + 1;
422 pstrcpy(p, offset, array[i]);
426 ret = qemu_fdt_setprop(fdt, node_path, prop, str, total_len);
431 const void *qemu_fdt_getprop(void *fdt, const char *node_path,
432 const char *property, int *lenp, Error **errp)
440 r = fdt_getprop(fdt, findnode_nofail(fdt, node_path), property, lenp);
442 error_setg(errp, "%s: Couldn't get %s/%s: %s", __func__,
443 node_path, property, fdt_strerror(*lenp));
448 uint32_t qemu_fdt_getprop_cell(void *fdt, const char *node_path,
449 const char *property, int *lenp, Error **errp)
457 p = qemu_fdt_getprop(fdt, node_path, property, lenp, errp);
460 } else if (*lenp != 4) {
461 error_setg(errp, "%s: %s/%s not 4 bytes long (not a cell?)",
462 __func__, node_path, property);
466 return be32_to_cpu(*p);
469 uint32_t qemu_fdt_get_phandle(void *fdt, const char *path)
473 r = fdt_get_phandle(fdt, findnode_nofail(fdt, path));
475 error_report("%s: Couldn't get phandle for %s: %s", __func__,
476 path, fdt_strerror(r));
483 int qemu_fdt_setprop_phandle(void *fdt, const char *node_path,
484 const char *property,
485 const char *target_node_path)
487 uint32_t phandle = qemu_fdt_get_phandle(fdt, target_node_path);
488 return qemu_fdt_setprop_cell(fdt, node_path, property, phandle);
491 uint32_t qemu_fdt_alloc_phandle(void *fdt)
493 static int phandle = 0x0;
496 * We need to find out if the user gave us special instruction at
497 * which phandle id to start allocating phandles.
500 phandle = machine_phandle_start(current_machine);
505 * None or invalid phandle given on the command line, so fall back to
506 * default starting point.
514 int qemu_fdt_nop_node(void *fdt, const char *node_path)
518 r = fdt_nop_node(fdt, findnode_nofail(fdt, node_path));
520 error_report("%s: Couldn't nop node %s: %s", __func__, node_path,
528 int qemu_fdt_add_subnode(void *fdt, const char *name)
530 char *dupname = g_strdup(name);
531 char *basename = strrchr(dupname, '/');
544 parent = findnode_nofail(fdt, dupname);
547 retval = fdt_add_subnode(fdt, parent, basename);
549 error_report("%s: Failed to create subnode %s: %s",
550 __func__, name, fdt_strerror(retval));
559 * qemu_fdt_add_path: Like qemu_fdt_add_subnode(), but will add
560 * all missing subnodes from the given path.
562 int qemu_fdt_add_path(void *fdt, const char *path)
568 if (path[0] != '/') {
574 path = strchr(name, '/');
575 namelen = path != NULL ? path - name : strlen(name);
577 retval = fdt_subnode_offset_namelen(fdt, parent, name, namelen);
578 if (retval < 0 && retval != -FDT_ERR_NOTFOUND) {
579 error_report("%s: Unexpected error in finding subnode %.*s: %s",
580 __func__, namelen, name, fdt_strerror(retval));
582 } else if (retval == -FDT_ERR_NOTFOUND) {
583 retval = fdt_add_subnode_namelen(fdt, parent, name, namelen);
585 error_report("%s: Failed to create subnode %.*s: %s",
586 __func__, namelen, name, fdt_strerror(retval));
597 void qemu_fdt_dumpdtb(void *fdt, int size)
599 const char *dumpdtb = current_machine->dumpdtb;
602 /* Dump the dtb to a file and quit */
603 if (g_file_set_contents(dumpdtb, fdt, size, NULL)) {
604 info_report("dtb dumped to %s. Exiting.", dumpdtb);
607 error_report("%s: Failed dumping dtb to %s", __func__, dumpdtb);
612 int qemu_fdt_setprop_sized_cells_from_array(void *fdt,
613 const char *node_path,
614 const char *property,
620 int cellnum, vnum, ncells;
624 propcells = g_new0(uint32_t, numvalues * 2);
627 for (vnum = 0; vnum < numvalues; vnum++) {
628 ncells = values[vnum * 2];
629 if (ncells != 1 && ncells != 2) {
633 value = values[vnum * 2 + 1];
634 hival = cpu_to_be32(value >> 32);
636 propcells[cellnum++] = hival;
637 } else if (hival != 0) {
641 propcells[cellnum++] = cpu_to_be32(value);
644 ret = qemu_fdt_setprop(fdt, node_path, property, propcells,
645 cellnum * sizeof(uint32_t));
651 void qmp_dumpdtb(const char *filename, Error **errp)
653 g_autoptr(GError) err = NULL;
656 if (!current_machine->fdt) {
657 error_setg(errp, "This machine doesn't have a FDT");
661 size = fdt_totalsize(current_machine->fdt);
665 if (!g_file_set_contents(filename, current_machine->fdt, size, &err)) {
666 error_setg(errp, "Error saving FDT to file %s: %s",
667 filename, err->message);
671 void hmp_dumpdtb(Monitor *mon, const QDict *qdict)
673 const char *filename = qdict_get_str(qdict, "filename");
674 Error *local_err = NULL;
676 qmp_dumpdtb(filename, &local_err);
678 if (hmp_handle_error(mon, local_err)) {
682 info_report("dtb dumped to %s", filename);
685 void qemu_fdt_randomize_seeds(void *fdt)
687 int noffset, poffset, len;
691 for (noffset = fdt_next_node(fdt, 0, NULL);
693 noffset = fdt_next_node(fdt, noffset, NULL)) {
694 for (poffset = fdt_first_property_offset(fdt, noffset);
696 poffset = fdt_next_property_offset(fdt, poffset)) {
697 data = (uint8_t *)fdt_getprop_by_offset(fdt, poffset, &name, &len);
698 if (!data || strcmp(name, "rng-seed"))
700 qemu_guest_getrandom_nofail(data, len);