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.
15 #include <sys/types.h>
22 #include "qemu-common.h"
23 #include "device_tree.h"
24 #include "hw/loader.h"
25 #include "qemu-option.h"
26 #include "qemu-config.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_begin_node(fdt, "");
47 ret = fdt_end_node(fdt);
51 ret = fdt_finish(fdt);
55 ret = fdt_open_into(fdt, fdt, *sizep);
57 fprintf(stderr, "Unable to copy device tree in memory\n");
63 fprintf(stderr, "%s Couldn't create dt: %s\n", __func__, fdt_strerror(ret));
67 void *load_device_tree(const char *filename_path, int *sizep)
70 int dt_file_load_size;
75 dt_size = get_image_size(filename_path);
77 printf("Unable to get size of device tree file '%s'\n",
82 /* Expand to 2x size to give enough room for manipulation. */
85 /* First allocate space in qemu for device tree */
86 fdt = g_malloc0(dt_size);
88 dt_file_load_size = load_image(filename_path, fdt);
89 if (dt_file_load_size < 0) {
90 printf("Unable to open device tree file '%s'\n",
95 ret = fdt_open_into(fdt, fdt, dt_size);
97 printf("Unable to copy device tree in memory\n");
101 /* Check sanity of device tree */
102 if (fdt_check_header(fdt)) {
103 printf ("Device tree file loaded into memory is invalid: %s\n",
115 static int findnode_nofail(void *fdt, const char *node_path)
119 offset = fdt_path_offset(fdt, node_path);
121 fprintf(stderr, "%s Couldn't find node %s: %s\n", __func__, node_path,
122 fdt_strerror(offset));
129 int qemu_devtree_setprop(void *fdt, const char *node_path,
130 const char *property, const void *val_array, int size)
134 r = fdt_setprop(fdt, findnode_nofail(fdt, node_path), property, val_array, size);
136 fprintf(stderr, "%s: Couldn't set %s/%s: %s\n", __func__, node_path,
137 property, fdt_strerror(r));
144 int qemu_devtree_setprop_cell(void *fdt, const char *node_path,
145 const char *property, uint32_t val)
149 r = fdt_setprop_cell(fdt, findnode_nofail(fdt, node_path), property, val);
151 fprintf(stderr, "%s: Couldn't set %s/%s = %#08x: %s\n", __func__,
152 node_path, property, val, fdt_strerror(r));
159 int qemu_devtree_setprop_u64(void *fdt, const char *node_path,
160 const char *property, uint64_t val)
162 val = cpu_to_be64(val);
163 return qemu_devtree_setprop(fdt, node_path, property, &val, sizeof(val));
166 int qemu_devtree_setprop_string(void *fdt, const char *node_path,
167 const char *property, const char *string)
171 r = fdt_setprop_string(fdt, findnode_nofail(fdt, node_path), property, string);
173 fprintf(stderr, "%s: Couldn't set %s/%s = %s: %s\n", __func__,
174 node_path, property, string, fdt_strerror(r));
181 uint32_t qemu_devtree_get_phandle(void *fdt, const char *path)
185 r = fdt_get_phandle(fdt, findnode_nofail(fdt, path));
187 fprintf(stderr, "%s: Couldn't get phandle for %s: %s\n", __func__,
188 path, fdt_strerror(r));
195 int qemu_devtree_setprop_phandle(void *fdt, const char *node_path,
196 const char *property,
197 const char *target_node_path)
199 uint32_t phandle = qemu_devtree_get_phandle(fdt, target_node_path);
200 return qemu_devtree_setprop_cell(fdt, node_path, property, phandle);
203 uint32_t qemu_devtree_alloc_phandle(void *fdt)
205 static int phandle = 0x0;
208 * We need to find out if the user gave us special instruction at
209 * which phandle id to start allocting phandles.
212 QemuOpts *machine_opts;
213 machine_opts = qemu_opts_find(qemu_find_opts("machine"), 0);
215 const char *phandle_start;
216 phandle_start = qemu_opt_get(machine_opts, "phandle_start");
218 phandle = strtoul(phandle_start, NULL, 0);
225 * None or invalid phandle given on the command line, so fall back to
226 * default starting point.
234 int qemu_devtree_nop_node(void *fdt, const char *node_path)
238 r = fdt_nop_node(fdt, findnode_nofail(fdt, node_path));
240 fprintf(stderr, "%s: Couldn't nop node %s: %s\n", __func__, node_path,
248 int qemu_devtree_add_subnode(void *fdt, const char *name)
250 char *dupname = g_strdup(name);
251 char *basename = strrchr(dupname, '/');
264 parent = findnode_nofail(fdt, dupname);
267 retval = fdt_add_subnode(fdt, parent, basename);
269 fprintf(stderr, "FDT: Failed to create subnode %s: %s\n", name,
270 fdt_strerror(retval));