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.
15 #include <sys/types.h>
22 #include "qemu-common.h"
23 #include "sysemu/device_tree.h"
24 #include "sysemu/sysemu.h"
25 #include "hw/loader.h"
26 #include "qemu/option.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_begin_node(fdt, "");
48 ret = fdt_end_node(fdt);
52 ret = fdt_finish(fdt);
56 ret = fdt_open_into(fdt, fdt, *sizep);
58 fprintf(stderr, "Unable to copy device tree in memory\n");
64 fprintf(stderr, "%s Couldn't create dt: %s\n", __func__, fdt_strerror(ret));
68 void *load_device_tree(const char *filename_path, int *sizep)
71 int dt_file_load_size;
76 dt_size = get_image_size(filename_path);
78 printf("Unable to get size of device tree file '%s'\n",
83 /* Expand to 2x size to give enough room for manipulation. */
86 /* First allocate space in qemu for device tree */
87 fdt = g_malloc0(dt_size);
89 dt_file_load_size = load_image(filename_path, fdt);
90 if (dt_file_load_size < 0) {
91 printf("Unable to open device tree file '%s'\n",
96 ret = fdt_open_into(fdt, fdt, dt_size);
98 printf("Unable to copy device tree in memory\n");
102 /* Check sanity of device tree */
103 if (fdt_check_header(fdt)) {
104 printf ("Device tree file loaded into memory is invalid: %s\n",
116 static int findnode_nofail(void *fdt, const char *node_path)
120 offset = fdt_path_offset(fdt, node_path);
122 fprintf(stderr, "%s Couldn't find node %s: %s\n", __func__, node_path,
123 fdt_strerror(offset));
130 int qemu_devtree_setprop(void *fdt, const char *node_path,
131 const char *property, const void *val_array, int size)
135 r = fdt_setprop(fdt, findnode_nofail(fdt, node_path), property, val_array, size);
137 fprintf(stderr, "%s: Couldn't set %s/%s: %s\n", __func__, node_path,
138 property, fdt_strerror(r));
145 int qemu_devtree_setprop_cell(void *fdt, const char *node_path,
146 const char *property, uint32_t val)
150 r = fdt_setprop_cell(fdt, findnode_nofail(fdt, node_path), property, val);
152 fprintf(stderr, "%s: Couldn't set %s/%s = %#08x: %s\n", __func__,
153 node_path, property, val, fdt_strerror(r));
160 int qemu_devtree_setprop_u64(void *fdt, const char *node_path,
161 const char *property, uint64_t val)
163 val = cpu_to_be64(val);
164 return qemu_devtree_setprop(fdt, node_path, property, &val, sizeof(val));
167 int qemu_devtree_setprop_string(void *fdt, const char *node_path,
168 const char *property, const char *string)
172 r = fdt_setprop_string(fdt, findnode_nofail(fdt, node_path), property, string);
174 fprintf(stderr, "%s: Couldn't set %s/%s = %s: %s\n", __func__,
175 node_path, property, string, fdt_strerror(r));
182 const void *qemu_devtree_getprop(void *fdt, const char *node_path,
183 const char *property, int *lenp)
190 r = fdt_getprop(fdt, findnode_nofail(fdt, node_path), property, lenp);
192 fprintf(stderr, "%s: Couldn't get %s/%s: %s\n", __func__,
193 node_path, property, fdt_strerror(*lenp));
199 uint32_t qemu_devtree_getprop_cell(void *fdt, const char *node_path,
200 const char *property)
203 const uint32_t *p = qemu_devtree_getprop(fdt, node_path, property, &len);
205 fprintf(stderr, "%s: %s/%s not 4 bytes long (not a cell?)\n",
206 __func__, node_path, property);
209 return be32_to_cpu(*p);
212 uint32_t qemu_devtree_get_phandle(void *fdt, const char *path)
216 r = fdt_get_phandle(fdt, findnode_nofail(fdt, path));
218 fprintf(stderr, "%s: Couldn't get phandle for %s: %s\n", __func__,
219 path, fdt_strerror(r));
226 int qemu_devtree_setprop_phandle(void *fdt, const char *node_path,
227 const char *property,
228 const char *target_node_path)
230 uint32_t phandle = qemu_devtree_get_phandle(fdt, target_node_path);
231 return qemu_devtree_setprop_cell(fdt, node_path, property, phandle);
234 uint32_t qemu_devtree_alloc_phandle(void *fdt)
236 static int phandle = 0x0;
239 * We need to find out if the user gave us special instruction at
240 * which phandle id to start allocting phandles.
243 const char *phandle_start = qemu_opt_get(qemu_get_machine_opts(),
246 phandle = strtoul(phandle_start, NULL, 0);
252 * None or invalid phandle given on the command line, so fall back to
253 * default starting point.
261 int qemu_devtree_nop_node(void *fdt, const char *node_path)
265 r = fdt_nop_node(fdt, findnode_nofail(fdt, node_path));
267 fprintf(stderr, "%s: Couldn't nop node %s: %s\n", __func__, node_path,
275 int qemu_devtree_add_subnode(void *fdt, const char *name)
277 char *dupname = g_strdup(name);
278 char *basename = strrchr(dupname, '/');
291 parent = findnode_nofail(fdt, dupname);
294 retval = fdt_add_subnode(fdt, parent, basename);
296 fprintf(stderr, "FDT: Failed to create subnode %s: %s\n", name,
297 fdt_strerror(retval));
305 void qemu_devtree_dumpdtb(void *fdt, int size)
307 const char *dumpdtb = qemu_opt_get(qemu_get_machine_opts(), "dumpdtb");
310 /* Dump the dtb to a file and quit */
311 exit(g_file_set_contents(dumpdtb, fdt, size, NULL) ? 0 : 1);