]>
git.proxmox.com Git - qemu.git/blob - device_tree.c
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 "device_tree.h"
24 #include "hw/loader.h"
28 #define FDT_MAX_SIZE 0x10000
30 void *create_device_tree(int *sizep
)
35 *sizep
= FDT_MAX_SIZE
;
36 fdt
= g_malloc0(FDT_MAX_SIZE
);
37 ret
= fdt_create(fdt
, FDT_MAX_SIZE
);
41 ret
= fdt_begin_node(fdt
, "");
45 ret
= fdt_end_node(fdt
);
49 ret
= fdt_finish(fdt
);
53 ret
= fdt_open_into(fdt
, fdt
, *sizep
);
55 fprintf(stderr
, "Unable to copy device tree in memory\n");
61 fprintf(stderr
, "%s Couldn't create dt: %s\n", __func__
, fdt_strerror(ret
));
65 void *load_device_tree(const char *filename_path
, int *sizep
)
68 int dt_file_load_size
;
73 dt_size
= get_image_size(filename_path
);
75 printf("Unable to get size of device tree file '%s'\n",
80 /* Expand to 2x size to give enough room for manipulation. */
83 /* First allocate space in qemu for device tree */
84 fdt
= g_malloc0(dt_size
);
86 dt_file_load_size
= load_image(filename_path
, fdt
);
87 if (dt_file_load_size
< 0) {
88 printf("Unable to open device tree file '%s'\n",
93 ret
= fdt_open_into(fdt
, fdt
, dt_size
);
95 printf("Unable to copy device tree in memory\n");
99 /* Check sanity of device tree */
100 if (fdt_check_header(fdt
)) {
101 printf ("Device tree file loaded into memory is invalid: %s\n",
113 static int findnode_nofail(void *fdt
, const char *node_path
)
117 offset
= fdt_path_offset(fdt
, node_path
);
119 fprintf(stderr
, "%s Couldn't find node %s: %s\n", __func__
, node_path
,
120 fdt_strerror(offset
));
127 int qemu_devtree_setprop(void *fdt
, const char *node_path
,
128 const char *property
, void *val_array
, int size
)
132 r
= fdt_setprop(fdt
, findnode_nofail(fdt
, node_path
), property
, val_array
, size
);
134 fprintf(stderr
, "%s: Couldn't set %s/%s: %s\n", __func__
, node_path
,
135 property
, fdt_strerror(r
));
142 int qemu_devtree_setprop_cell(void *fdt
, const char *node_path
,
143 const char *property
, uint32_t val
)
147 r
= fdt_setprop_cell(fdt
, findnode_nofail(fdt
, node_path
), property
, val
);
149 fprintf(stderr
, "%s: Couldn't set %s/%s = %#08x: %s\n", __func__
,
150 node_path
, property
, val
, fdt_strerror(r
));
157 int qemu_devtree_setprop_u64(void *fdt
, const char *node_path
,
158 const char *property
, uint64_t val
)
160 val
= cpu_to_be64(val
);
161 return qemu_devtree_setprop(fdt
, node_path
, property
, &val
, sizeof(val
));
164 int qemu_devtree_setprop_string(void *fdt
, const char *node_path
,
165 const char *property
, const char *string
)
169 r
= fdt_setprop_string(fdt
, findnode_nofail(fdt
, node_path
), property
, string
);
171 fprintf(stderr
, "%s: Couldn't set %s/%s = %s: %s\n", __func__
,
172 node_path
, property
, string
, fdt_strerror(r
));
179 uint32_t qemu_devtree_get_phandle(void *fdt
, const char *path
)
183 r
= fdt_get_phandle(fdt
, findnode_nofail(fdt
, path
));
185 fprintf(stderr
, "%s: Couldn't get phandle for %s: %s\n", __func__
,
186 path
, fdt_strerror(r
));
193 int qemu_devtree_setprop_phandle(void *fdt
, const char *node_path
,
194 const char *property
,
195 const char *target_node_path
)
197 uint32_t phandle
= qemu_devtree_get_phandle(fdt
, target_node_path
);
198 return qemu_devtree_setprop_cell(fdt
, node_path
, property
, phandle
);
201 uint32_t qemu_devtree_alloc_phandle(void *fdt
)
203 static int phandle
= 0x8000;
208 int qemu_devtree_nop_node(void *fdt
, const char *node_path
)
212 r
= fdt_nop_node(fdt
, findnode_nofail(fdt
, node_path
));
214 fprintf(stderr
, "%s: Couldn't nop node %s: %s\n", __func__
, node_path
,
222 int qemu_devtree_add_subnode(void *fdt
, const char *name
)
224 char *dupname
= g_strdup(name
);
225 char *basename
= strrchr(dupname
, '/');
238 parent
= findnode_nofail(fdt
, dupname
);
241 retval
= fdt_add_subnode(fdt
, parent
, basename
);
244 fprintf(stderr
, "FDT: Failed to create subnode %s: %s\n", name
,
245 fdt_strerror(retval
));