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_finish_reservemap(fdt
);
48 ret
= fdt_begin_node(fdt
, "");
52 ret
= fdt_end_node(fdt
);
56 ret
= fdt_finish(fdt
);
60 ret
= fdt_open_into(fdt
, fdt
, *sizep
);
62 fprintf(stderr
, "Unable to copy device tree in memory\n");
68 fprintf(stderr
, "%s Couldn't create dt: %s\n", __func__
, fdt_strerror(ret
));
72 void *load_device_tree(const char *filename_path
, int *sizep
)
75 int dt_file_load_size
;
80 dt_size
= get_image_size(filename_path
);
82 printf("Unable to get size of device tree file '%s'\n",
87 /* Expand to 2x size to give enough room for manipulation. */
90 /* First allocate space in qemu for device tree */
91 fdt
= g_malloc0(dt_size
);
93 dt_file_load_size
= load_image(filename_path
, fdt
);
94 if (dt_file_load_size
< 0) {
95 printf("Unable to open device tree file '%s'\n",
100 ret
= fdt_open_into(fdt
, fdt
, dt_size
);
102 printf("Unable to copy device tree in memory\n");
106 /* Check sanity of device tree */
107 if (fdt_check_header(fdt
)) {
108 printf ("Device tree file loaded into memory is invalid: %s\n",
120 static int findnode_nofail(void *fdt
, const char *node_path
)
124 offset
= fdt_path_offset(fdt
, node_path
);
126 fprintf(stderr
, "%s Couldn't find node %s: %s\n", __func__
, node_path
,
127 fdt_strerror(offset
));
134 int qemu_fdt_setprop(void *fdt
, const char *node_path
,
135 const char *property
, const void *val
, int size
)
139 r
= fdt_setprop(fdt
, findnode_nofail(fdt
, node_path
), property
, val
, size
);
141 fprintf(stderr
, "%s: Couldn't set %s/%s: %s\n", __func__
, node_path
,
142 property
, fdt_strerror(r
));
149 int qemu_fdt_setprop_cell(void *fdt
, const char *node_path
,
150 const char *property
, uint32_t val
)
154 r
= fdt_setprop_cell(fdt
, findnode_nofail(fdt
, node_path
), property
, val
);
156 fprintf(stderr
, "%s: Couldn't set %s/%s = %#08x: %s\n", __func__
,
157 node_path
, property
, val
, fdt_strerror(r
));
164 int qemu_fdt_setprop_u64(void *fdt
, const char *node_path
,
165 const char *property
, uint64_t val
)
167 val
= cpu_to_be64(val
);
168 return qemu_fdt_setprop(fdt
, node_path
, property
, &val
, sizeof(val
));
171 int qemu_fdt_setprop_string(void *fdt
, const char *node_path
,
172 const char *property
, const char *string
)
176 r
= fdt_setprop_string(fdt
, findnode_nofail(fdt
, node_path
), property
, string
);
178 fprintf(stderr
, "%s: Couldn't set %s/%s = %s: %s\n", __func__
,
179 node_path
, property
, string
, fdt_strerror(r
));
186 const void *qemu_fdt_getprop(void *fdt
, const char *node_path
,
187 const char *property
, int *lenp
)
194 r
= fdt_getprop(fdt
, findnode_nofail(fdt
, node_path
), property
, lenp
);
196 fprintf(stderr
, "%s: Couldn't get %s/%s: %s\n", __func__
,
197 node_path
, property
, fdt_strerror(*lenp
));
203 uint32_t qemu_fdt_getprop_cell(void *fdt
, const char *node_path
,
204 const char *property
)
207 const uint32_t *p
= qemu_fdt_getprop(fdt
, node_path
, property
, &len
);
209 fprintf(stderr
, "%s: %s/%s not 4 bytes long (not a cell?)\n",
210 __func__
, node_path
, property
);
213 return be32_to_cpu(*p
);
216 uint32_t qemu_fdt_get_phandle(void *fdt
, const char *path
)
220 r
= fdt_get_phandle(fdt
, findnode_nofail(fdt
, path
));
222 fprintf(stderr
, "%s: Couldn't get phandle for %s: %s\n", __func__
,
223 path
, fdt_strerror(r
));
230 int qemu_fdt_setprop_phandle(void *fdt
, const char *node_path
,
231 const char *property
,
232 const char *target_node_path
)
234 uint32_t phandle
= qemu_fdt_get_phandle(fdt
, target_node_path
);
235 return qemu_fdt_setprop_cell(fdt
, node_path
, property
, phandle
);
238 uint32_t qemu_fdt_alloc_phandle(void *fdt
)
240 static int phandle
= 0x0;
243 * We need to find out if the user gave us special instruction at
244 * which phandle id to start allocting phandles.
247 phandle
= qemu_opt_get_number(qemu_get_machine_opts(),
253 * None or invalid phandle given on the command line, so fall back to
254 * default starting point.
262 int qemu_fdt_nop_node(void *fdt
, const char *node_path
)
266 r
= fdt_nop_node(fdt
, findnode_nofail(fdt
, node_path
));
268 fprintf(stderr
, "%s: Couldn't nop node %s: %s\n", __func__
, node_path
,
276 int qemu_fdt_add_subnode(void *fdt
, const char *name
)
278 char *dupname
= g_strdup(name
);
279 char *basename
= strrchr(dupname
, '/');
292 parent
= findnode_nofail(fdt
, dupname
);
295 retval
= fdt_add_subnode(fdt
, parent
, basename
);
297 fprintf(stderr
, "FDT: Failed to create subnode %s: %s\n", name
,
298 fdt_strerror(retval
));
306 void qemu_fdt_dumpdtb(void *fdt
, int size
)
308 const char *dumpdtb
= qemu_opt_get(qemu_get_machine_opts(), "dumpdtb");
311 /* Dump the dtb to a file and quit */
312 exit(g_file_set_contents(dumpdtb
, fdt
, size
, NULL
) ? 0 : 1);
316 int qemu_fdt_setprop_sized_cells_from_array(void *fdt
,
317 const char *node_path
,
318 const char *property
,
324 int cellnum
, vnum
, ncells
;
327 propcells
= g_new0(uint32_t, numvalues
* 2);
330 for (vnum
= 0; vnum
< numvalues
; vnum
++) {
331 ncells
= values
[vnum
* 2];
332 if (ncells
!= 1 && ncells
!= 2) {
335 value
= values
[vnum
* 2 + 1];
336 hival
= cpu_to_be32(value
>> 32);
338 propcells
[cellnum
++] = hival
;
339 } else if (hival
!= 0) {
342 propcells
[cellnum
++] = cpu_to_be32(value
);
345 return qemu_fdt_setprop(fdt
, node_path
, property
, propcells
,
346 cellnum
* sizeof(uint32_t));