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.
10 * This work is licensed under the GNU GPL license version 2 or later.
14 #include "qemu/osdep.h"
20 #include "qemu-common.h"
21 #include "qemu/error-report.h"
22 #include "sysemu/device_tree.h"
23 #include "sysemu/sysemu.h"
24 #include "hw/loader.h"
25 #include "hw/boards.h"
26 #include "qemu/config-file.h"
30 #define FDT_MAX_SIZE 0x10000
32 void *create_device_tree(int *sizep)
37 *sizep = FDT_MAX_SIZE;
38 fdt = g_malloc0(FDT_MAX_SIZE);
39 ret = fdt_create(fdt, FDT_MAX_SIZE);
43 ret = fdt_finish_reservemap(fdt);
47 ret = fdt_begin_node(fdt, "");
51 ret = fdt_end_node(fdt);
55 ret = fdt_finish(fdt);
59 ret = fdt_open_into(fdt, fdt, *sizep);
61 error_report("Unable to copy device tree in memory");
67 error_report("%s Couldn't create dt: %s", __func__, fdt_strerror(ret));
71 void *load_device_tree(const char *filename_path, int *sizep)
74 int dt_file_load_size;
79 dt_size = get_image_size(filename_path);
81 error_report("Unable to get size of device tree file '%s'",
86 /* Expand to 2x size to give enough room for manipulation. */
89 /* First allocate space in qemu for device tree */
90 fdt = g_malloc0(dt_size);
92 dt_file_load_size = load_image(filename_path, fdt);
93 if (dt_file_load_size < 0) {
94 error_report("Unable to open device tree file '%s'",
99 ret = fdt_open_into(fdt, fdt, dt_size);
101 error_report("Unable to copy device tree in memory");
105 /* Check sanity of device tree */
106 if (fdt_check_header(fdt)) {
107 error_report("Device tree file loaded into memory is invalid: %s",
121 #define SYSFS_DT_BASEDIR "/proc/device-tree"
124 * read_fstree: this function is inspired from dtc read_fstree
125 * @fdt: preallocated fdt blob buffer, to be populated
126 * @dirname: directory to scan under SYSFS_DT_BASEDIR
127 * the search is recursive and the tree is searched down to the
128 * leaves (property files).
130 * the function asserts in case of error
132 static void read_fstree(void *fdt, const char *dirname)
137 const char *root_dir = SYSFS_DT_BASEDIR;
138 const char *parent_node;
140 if (strstr(dirname, root_dir) != dirname) {
141 error_setg(&error_fatal, "%s: %s must be searched within %s",
142 __func__, dirname, root_dir);
144 parent_node = &dirname[strlen(SYSFS_DT_BASEDIR)];
146 d = opendir(dirname);
148 error_setg(&error_fatal, "%s cannot open %s", __func__, dirname);
151 while ((de = readdir(d)) != NULL) {
154 if (!g_strcmp0(de->d_name, ".")
155 || !g_strcmp0(de->d_name, "..")) {
159 tmpnam = g_strdup_printf("%s/%s", dirname, de->d_name);
161 if (lstat(tmpnam, &st) < 0) {
162 error_setg(&error_fatal, "%s cannot lstat %s", __func__, tmpnam);
165 if (S_ISREG(st.st_mode)) {
169 if (!g_file_get_contents(tmpnam, &val, &len, NULL)) {
170 error_setg(&error_fatal, "%s not able to extract info from %s",
174 if (strlen(parent_node) > 0) {
175 qemu_fdt_setprop(fdt, parent_node,
176 de->d_name, val, len);
178 qemu_fdt_setprop(fdt, "/", de->d_name, val, len);
181 } else if (S_ISDIR(st.st_mode)) {
184 node_name = g_strdup_printf("%s/%s",
185 parent_node, de->d_name);
186 qemu_fdt_add_subnode(fdt, node_name);
188 read_fstree(fdt, tmpnam);
197 /* load_device_tree_from_sysfs: extract the dt blob from host sysfs */
198 void *load_device_tree_from_sysfs(void)
203 host_fdt = create_device_tree(&host_fdt_size);
204 read_fstree(host_fdt, SYSFS_DT_BASEDIR);
205 if (fdt_check_header(host_fdt)) {
206 error_setg(&error_fatal,
207 "%s host device tree extracted into memory is invalid",
213 #endif /* CONFIG_LINUX */
215 static int findnode_nofail(void *fdt, const char *node_path)
219 offset = fdt_path_offset(fdt, node_path);
221 error_report("%s Couldn't find node %s: %s", __func__, node_path,
222 fdt_strerror(offset));
229 char **qemu_fdt_node_path(void *fdt, const char *name, char *compat,
232 int offset, len, ret;
233 const char *iter_name;
234 unsigned int path_len = 16, n = 0;
235 GSList *path_list = NULL, *iter;
238 offset = fdt_node_offset_by_compatible(fdt, -1, compat);
240 while (offset >= 0) {
241 iter_name = fdt_get_name(fdt, offset, &len);
246 if (!strcmp(iter_name, name)) {
249 path = g_malloc(path_len);
250 while ((ret = fdt_get_path(fdt, offset, path, path_len))
251 == -FDT_ERR_NOSPACE) {
253 path = g_realloc(path, path_len);
255 path_list = g_slist_prepend(path_list, path);
258 offset = fdt_node_offset_by_compatible(fdt, offset, compat);
261 if (offset < 0 && offset != -FDT_ERR_NOTFOUND) {
262 error_setg(errp, "%s: abort parsing dt for %s/%s: %s",
263 __func__, name, compat, fdt_strerror(offset));
264 for (iter = path_list; iter; iter = iter->next) {
267 g_slist_free(path_list);
271 path_array = g_new(char *, n + 1);
272 path_array[n--] = NULL;
274 for (iter = path_list; iter; iter = iter->next) {
275 path_array[n--] = iter->data;
278 g_slist_free(path_list);
283 int qemu_fdt_setprop(void *fdt, const char *node_path,
284 const char *property, const void *val, int size)
288 r = fdt_setprop(fdt, findnode_nofail(fdt, node_path), property, val, size);
290 error_report("%s: Couldn't set %s/%s: %s", __func__, node_path,
291 property, fdt_strerror(r));
298 int qemu_fdt_setprop_cell(void *fdt, const char *node_path,
299 const char *property, uint32_t val)
303 r = fdt_setprop_cell(fdt, findnode_nofail(fdt, node_path), property, val);
305 error_report("%s: Couldn't set %s/%s = %#08x: %s", __func__,
306 node_path, property, val, fdt_strerror(r));
313 int qemu_fdt_setprop_u64(void *fdt, const char *node_path,
314 const char *property, uint64_t val)
316 val = cpu_to_be64(val);
317 return qemu_fdt_setprop(fdt, node_path, property, &val, sizeof(val));
320 int qemu_fdt_setprop_string(void *fdt, const char *node_path,
321 const char *property, const char *string)
325 r = fdt_setprop_string(fdt, findnode_nofail(fdt, node_path), property, string);
327 error_report("%s: Couldn't set %s/%s = %s: %s", __func__,
328 node_path, property, string, fdt_strerror(r));
335 const void *qemu_fdt_getprop(void *fdt, const char *node_path,
336 const char *property, int *lenp, Error **errp)
344 r = fdt_getprop(fdt, findnode_nofail(fdt, node_path), property, lenp);
346 error_setg(errp, "%s: Couldn't get %s/%s: %s", __func__,
347 node_path, property, fdt_strerror(*lenp));
352 uint32_t qemu_fdt_getprop_cell(void *fdt, const char *node_path,
353 const char *property, int *lenp, Error **errp)
361 p = qemu_fdt_getprop(fdt, node_path, property, lenp, errp);
364 } else if (*lenp != 4) {
365 error_setg(errp, "%s: %s/%s not 4 bytes long (not a cell?)",
366 __func__, node_path, property);
370 return be32_to_cpu(*p);
373 uint32_t qemu_fdt_get_phandle(void *fdt, const char *path)
377 r = fdt_get_phandle(fdt, findnode_nofail(fdt, path));
379 error_report("%s: Couldn't get phandle for %s: %s", __func__,
380 path, fdt_strerror(r));
387 int qemu_fdt_setprop_phandle(void *fdt, const char *node_path,
388 const char *property,
389 const char *target_node_path)
391 uint32_t phandle = qemu_fdt_get_phandle(fdt, target_node_path);
392 return qemu_fdt_setprop_cell(fdt, node_path, property, phandle);
395 uint32_t qemu_fdt_alloc_phandle(void *fdt)
397 static int phandle = 0x0;
400 * We need to find out if the user gave us special instruction at
401 * which phandle id to start allocating phandles.
404 phandle = machine_phandle_start(current_machine);
409 * None or invalid phandle given on the command line, so fall back to
410 * default starting point.
418 int qemu_fdt_nop_node(void *fdt, const char *node_path)
422 r = fdt_nop_node(fdt, findnode_nofail(fdt, node_path));
424 error_report("%s: Couldn't nop node %s: %s", __func__, node_path,
432 int qemu_fdt_add_subnode(void *fdt, const char *name)
434 char *dupname = g_strdup(name);
435 char *basename = strrchr(dupname, '/');
448 parent = findnode_nofail(fdt, dupname);
451 retval = fdt_add_subnode(fdt, parent, basename);
453 error_report("FDT: Failed to create subnode %s: %s", name,
454 fdt_strerror(retval));
462 void qemu_fdt_dumpdtb(void *fdt, int size)
464 const char *dumpdtb = qemu_opt_get(qemu_get_machine_opts(), "dumpdtb");
467 /* Dump the dtb to a file and quit */
468 exit(g_file_set_contents(dumpdtb, fdt, size, NULL) ? 0 : 1);
472 int qemu_fdt_setprop_sized_cells_from_array(void *fdt,
473 const char *node_path,
474 const char *property,
480 int cellnum, vnum, ncells;
484 propcells = g_new0(uint32_t, numvalues * 2);
487 for (vnum = 0; vnum < numvalues; vnum++) {
488 ncells = values[vnum * 2];
489 if (ncells != 1 && ncells != 2) {
493 value = values[vnum * 2 + 1];
494 hival = cpu_to_be32(value >> 32);
496 propcells[cellnum++] = hival;
497 } else if (hival != 0) {
501 propcells[cellnum++] = cpu_to_be32(value);
504 ret = qemu_fdt_setprop(fdt, node_path, property, propcells,
505 cellnum * sizeof(uint32_t));