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"
16 #include "qemu-common.h"
17 #include "qemu/error-report.h"
18 #include "sysemu/device_tree.h"
19 #include "sysemu/sysemu.h"
20 #include "hw/loader.h"
21 #include "hw/boards.h"
22 #include "qemu/config-file.h"
26 #define FDT_MAX_SIZE 0x10000
28 void *create_device_tree(int *sizep
)
33 *sizep
= FDT_MAX_SIZE
;
34 fdt
= g_malloc0(FDT_MAX_SIZE
);
35 ret
= fdt_create(fdt
, FDT_MAX_SIZE
);
39 ret
= fdt_finish_reservemap(fdt
);
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 error_report("Unable to copy device tree in memory");
63 error_report("%s Couldn't create dt: %s", __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 error_report("Unable to get size of device tree file '%s'",
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 error_report("Unable to open device tree file '%s'",
95 ret
= fdt_open_into(fdt
, fdt
, dt_size
);
97 error_report("Unable to copy device tree in memory");
101 /* Check sanity of device tree */
102 if (fdt_check_header(fdt
)) {
103 error_report("Device tree file loaded into memory is invalid: %s",
115 static int findnode_nofail(void *fdt
, const char *node_path
)
119 offset
= fdt_path_offset(fdt
, node_path
);
121 error_report("%s Couldn't find node %s: %s", __func__
, node_path
,
122 fdt_strerror(offset
));
129 int qemu_fdt_setprop(void *fdt
, const char *node_path
,
130 const char *property
, const void *val
, int size
)
134 r
= fdt_setprop(fdt
, findnode_nofail(fdt
, node_path
), property
, val
, size
);
136 error_report("%s: Couldn't set %s/%s: %s", __func__
, node_path
,
137 property
, fdt_strerror(r
));
144 int qemu_fdt_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 error_report("%s: Couldn't set %s/%s = %#08x: %s", __func__
,
152 node_path
, property
, val
, fdt_strerror(r
));
159 int qemu_fdt_setprop_u64(void *fdt
, const char *node_path
,
160 const char *property
, uint64_t val
)
162 val
= cpu_to_be64(val
);
163 return qemu_fdt_setprop(fdt
, node_path
, property
, &val
, sizeof(val
));
166 int qemu_fdt_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 error_report("%s: Couldn't set %s/%s = %s: %s", __func__
,
174 node_path
, property
, string
, fdt_strerror(r
));
181 const void *qemu_fdt_getprop(void *fdt
, const char *node_path
,
182 const char *property
, int *lenp
)
189 r
= fdt_getprop(fdt
, findnode_nofail(fdt
, node_path
), property
, lenp
);
191 error_report("%s: Couldn't get %s/%s: %s", __func__
,
192 node_path
, property
, fdt_strerror(*lenp
));
198 uint32_t qemu_fdt_getprop_cell(void *fdt
, const char *node_path
,
199 const char *property
)
202 const uint32_t *p
= qemu_fdt_getprop(fdt
, node_path
, property
, &len
);
204 error_report("%s: %s/%s not 4 bytes long (not a cell?)",
205 __func__
, node_path
, property
);
208 return be32_to_cpu(*p
);
211 uint32_t qemu_fdt_get_phandle(void *fdt
, const char *path
)
215 r
= fdt_get_phandle(fdt
, findnode_nofail(fdt
, path
));
217 error_report("%s: Couldn't get phandle for %s: %s", __func__
,
218 path
, fdt_strerror(r
));
225 int qemu_fdt_setprop_phandle(void *fdt
, const char *node_path
,
226 const char *property
,
227 const char *target_node_path
)
229 uint32_t phandle
= qemu_fdt_get_phandle(fdt
, target_node_path
);
230 return qemu_fdt_setprop_cell(fdt
, node_path
, property
, phandle
);
233 uint32_t qemu_fdt_alloc_phandle(void *fdt
)
235 static int phandle
= 0x0;
238 * We need to find out if the user gave us special instruction at
239 * which phandle id to start allocating phandles.
242 phandle
= machine_phandle_start(current_machine
);
247 * None or invalid phandle given on the command line, so fall back to
248 * default starting point.
256 int qemu_fdt_nop_node(void *fdt
, const char *node_path
)
260 r
= fdt_nop_node(fdt
, findnode_nofail(fdt
, node_path
));
262 error_report("%s: Couldn't nop node %s: %s", __func__
, node_path
,
270 int qemu_fdt_add_subnode(void *fdt
, const char *name
)
272 char *dupname
= g_strdup(name
);
273 char *basename
= strrchr(dupname
, '/');
286 parent
= findnode_nofail(fdt
, dupname
);
289 retval
= fdt_add_subnode(fdt
, parent
, basename
);
291 error_report("FDT: Failed to create subnode %s: %s", name
,
292 fdt_strerror(retval
));
300 void qemu_fdt_dumpdtb(void *fdt
, int size
)
302 const char *dumpdtb
= qemu_opt_get(qemu_get_machine_opts(), "dumpdtb");
305 /* Dump the dtb to a file and quit */
306 exit(g_file_set_contents(dumpdtb
, fdt
, size
, NULL
) ? 0 : 1);
310 int qemu_fdt_setprop_sized_cells_from_array(void *fdt
,
311 const char *node_path
,
312 const char *property
,
318 int cellnum
, vnum
, ncells
;
322 propcells
= g_new0(uint32_t, numvalues
* 2);
325 for (vnum
= 0; vnum
< numvalues
; vnum
++) {
326 ncells
= values
[vnum
* 2];
327 if (ncells
!= 1 && ncells
!= 2) {
331 value
= values
[vnum
* 2 + 1];
332 hival
= cpu_to_be32(value
>> 32);
334 propcells
[cellnum
++] = hival
;
335 } else if (hival
!= 0) {
339 propcells
[cellnum
++] = cpu_to_be32(value
);
342 ret
= qemu_fdt_setprop(fdt
, node_path
, property
, propcells
,
343 cellnum
* sizeof(uint32_t));