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"
28 void *load_device_tree(const char *filename_path, int *sizep)
31 int dt_file_load_size;
36 dt_size = get_image_size(filename_path);
38 printf("Unable to get size of device tree file '%s'\n",
43 /* Expand to 2x size to give enough room for manipulation. */
46 /* First allocate space in qemu for device tree */
47 fdt = g_malloc0(dt_size);
49 dt_file_load_size = load_image(filename_path, fdt);
50 if (dt_file_load_size < 0) {
51 printf("Unable to open device tree file '%s'\n",
56 ret = fdt_open_into(fdt, fdt, dt_size);
58 printf("Unable to copy device tree in memory\n");
62 /* Check sanity of device tree */
63 if (fdt_check_header(fdt)) {
64 printf ("Device tree file loaded into memory is invalid: %s\n",
76 static int findnode_nofail(void *fdt, const char *node_path)
80 offset = fdt_path_offset(fdt, node_path);
82 fprintf(stderr, "%s Couldn't find node %s: %s\n", __func__, node_path,
83 fdt_strerror(offset));
90 int qemu_devtree_setprop(void *fdt, const char *node_path,
91 const char *property, void *val_array, int size)
95 r = fdt_setprop(fdt, findnode_nofail(fdt, node_path), property, val_array, size);
97 fprintf(stderr, "%s: Couldn't set %s/%s: %s\n", __func__, node_path,
98 property, fdt_strerror(r));
105 int qemu_devtree_setprop_cell(void *fdt, const char *node_path,
106 const char *property, uint32_t val)
110 r = fdt_setprop_cell(fdt, findnode_nofail(fdt, node_path), property, val);
112 fprintf(stderr, "%s: Couldn't set %s/%s = %#08x: %s\n", __func__,
113 node_path, property, val, fdt_strerror(r));
120 int qemu_devtree_setprop_string(void *fdt, const char *node_path,
121 const char *property, const char *string)
125 r = fdt_setprop_string(fdt, findnode_nofail(fdt, node_path), property, string);
127 fprintf(stderr, "%s: Couldn't set %s/%s = %s: %s\n", __func__,
128 node_path, property, string, fdt_strerror(r));
135 int qemu_devtree_nop_node(void *fdt, const char *node_path)
139 r = fdt_nop_node(fdt, findnode_nofail(fdt, node_path));
141 fprintf(stderr, "%s: Couldn't nop node %s: %s\n", __func__, node_path,
149 int qemu_devtree_add_subnode(void *fdt, const char *name)
151 char *dupname = g_strdup(name);
152 char *basename = strrchr(dupname, '/');
162 retval = fdt_add_subnode(fdt, findnode_nofail(fdt, dupname), basename);
164 fprintf(stderr, "FDT: Failed to create subnode %s: %s\n", name,
165 fdt_strerror(retval));