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>
9 * This work is licensed under the GNU GPL license version 2 or later.
14 #include <sys/types.h>
27 #define DT_PROC_INTERFACE_PATH "/proc/device-tree"
29 /* FUNCTIONS FOR READING FROM DEVICE TREE OF HOST IN /PROC */
31 /* This function reads device-tree property files that are of
34 uint32_t read_proc_dt_prop_cell(char *path_in_device_tree
)
41 i
= asprintf(&buf
, "%s/%s", DT_PROC_INTERFACE_PATH
,
45 printf("%s: Unable to malloc string buffer buf\n",
50 stream
= fopen(buf
, "rb");
53 printf("%s: Unable to open '%s'\n", __func__
, buf
);
57 fread(&num
, sizeof(num
), 1, stream
);
64 /* FUNCTIONS FOR LOADING & MANIPULATION OF DEVICE TREE IN GUEST */
67 /* support functions */
68 static int get_offset_of_node(void *fdt
, char *node_path
)
71 node_offset
= fdt_path_offset(fdt
, node_path
);
72 if (node_offset
< 0) {
73 printf("Unable to find node in device tree '%s'\n",
80 /* public functions */
81 void *load_device_tree(char *filename_path
, unsigned long load_addr
)
84 int dt_file_load_size
;
90 dt_file_size
= get_image_size(filename_path
);
91 if (dt_file_size
< 0) {
92 printf("Unable to get size of device tree file '%s'\n",
97 /* First allocate space in qemu for device tree */
98 dt_file
= qemu_malloc(dt_file_size
);
99 if (dt_file
== NULL
) {
100 printf("Unable to allocate memory in qemu for device tree\n");
103 memset(dt_file
, 0, dt_file_size
);
105 dt_file_load_size
= load_image(filename_path
, dt_file
);
108 /* XXX Second we place new copy of 2x size in guest memory
109 * This give us enough room for manipulation.
111 new_dt_size
= dt_file_size
* 2;
113 fdt
= (void *)load_addr
;
115 ret
= fdt_open_into(dt_file
, fdt
, new_dt_size
);
117 printf("Unable to copy device tree in memory\n");
121 /* Check sanity of device tree */
122 if (fdt_check_header(fdt
)) {
123 printf ("Device tree file loaded into memory is invalid: %s\n",
127 /* free qemu memory with old device tree */
137 void dump_device_tree_to_file(void *fdt
, char *filename
)
140 fd
= open(filename
, O_RDWR
|O_CREAT
, O_RDWR
);
142 printf("Failed to open file %s\n Cannot dum device-tree\n",
147 write(fd
, fdt
, fdt_totalsize(fdt
));
151 void dt_cell(void *fdt
, char *node_path
, char *property
,
156 offset
= get_offset_of_node(fdt
, node_path
);
157 ret
= fdt_setprop_cell(fdt
, offset
, property
, val
);
159 printf("Unable to set device tree property '%s'\n",
165 /* This function is to manipulate a cell with multiple values */
166 void dt_cell_multi(void *fdt
, char *node_path
, char *property
,
167 uint32_t *val_array
, int size
)
171 offset
= get_offset_of_node(fdt
, node_path
);
172 ret
= fdt_setprop(fdt
, offset
, property
, val_array
, size
);
174 printf("Unable to set device tree property '%s'\n",
180 void dt_string(void *fdt
, char *node_path
, char *property
,
185 offset
= get_offset_of_node(fdt
, node_path
);
186 ret
= fdt_setprop_string(fdt
, offset
, property
, string
);
188 printf("Unable to set device tree property '%s'\n",