device_tree: introduce load_device_tree_from_sysfs
[qemu/ar7.git] / device_tree.c
blob9e77c6990ce652a77b81c38c93795a28cfdae186
1 /*
2 * Functions to help device tree manipulation using libfdt.
3 * It also provides functions to read entries from device tree proc
4 * interface.
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 #ifdef CONFIG_LINUX
17 #include <dirent.h>
18 #endif
20 #include "qemu-common.h"
21 #include "qemu/error-report.h"
22 #include "sysemu/device_tree.h"
23 #include "sysemu/sysemu.h"
24 #include "hw/loader.h"
25 #include "hw/boards.h"
26 #include "qemu/config-file.h"
28 #include <libfdt.h>
30 #define FDT_MAX_SIZE 0x10000
32 void *create_device_tree(int *sizep)
34 void *fdt;
35 int ret;
37 *sizep = FDT_MAX_SIZE;
38 fdt = g_malloc0(FDT_MAX_SIZE);
39 ret = fdt_create(fdt, FDT_MAX_SIZE);
40 if (ret < 0) {
41 goto fail;
43 ret = fdt_finish_reservemap(fdt);
44 if (ret < 0) {
45 goto fail;
47 ret = fdt_begin_node(fdt, "");
48 if (ret < 0) {
49 goto fail;
51 ret = fdt_end_node(fdt);
52 if (ret < 0) {
53 goto fail;
55 ret = fdt_finish(fdt);
56 if (ret < 0) {
57 goto fail;
59 ret = fdt_open_into(fdt, fdt, *sizep);
60 if (ret) {
61 error_report("Unable to copy device tree in memory");
62 exit(1);
65 return fdt;
66 fail:
67 error_report("%s Couldn't create dt: %s", __func__, fdt_strerror(ret));
68 exit(1);
71 void *load_device_tree(const char *filename_path, int *sizep)
73 int dt_size;
74 int dt_file_load_size;
75 int ret;
76 void *fdt = NULL;
78 *sizep = 0;
79 dt_size = get_image_size(filename_path);
80 if (dt_size < 0) {
81 error_report("Unable to get size of device tree file '%s'",
82 filename_path);
83 goto fail;
86 /* Expand to 2x size to give enough room for manipulation. */
87 dt_size += 10000;
88 dt_size *= 2;
89 /* First allocate space in qemu for device tree */
90 fdt = g_malloc0(dt_size);
92 dt_file_load_size = load_image(filename_path, fdt);
93 if (dt_file_load_size < 0) {
94 error_report("Unable to open device tree file '%s'",
95 filename_path);
96 goto fail;
99 ret = fdt_open_into(fdt, fdt, dt_size);
100 if (ret) {
101 error_report("Unable to copy device tree in memory");
102 goto fail;
105 /* Check sanity of device tree */
106 if (fdt_check_header(fdt)) {
107 error_report("Device tree file loaded into memory is invalid: %s",
108 filename_path);
109 goto fail;
111 *sizep = dt_size;
112 return fdt;
114 fail:
115 g_free(fdt);
116 return NULL;
119 #ifdef CONFIG_LINUX
121 #define SYSFS_DT_BASEDIR "/proc/device-tree"
124 * read_fstree: this function is inspired from dtc read_fstree
125 * @fdt: preallocated fdt blob buffer, to be populated
126 * @dirname: directory to scan under SYSFS_DT_BASEDIR
127 * the search is recursive and the tree is searched down to the
128 * leaves (property files).
130 * the function asserts in case of error
132 static void read_fstree(void *fdt, const char *dirname)
134 DIR *d;
135 struct dirent *de;
136 struct stat st;
137 const char *root_dir = SYSFS_DT_BASEDIR;
138 const char *parent_node;
140 if (strstr(dirname, root_dir) != dirname) {
141 error_setg(&error_fatal, "%s: %s must be searched within %s",
142 __func__, dirname, root_dir);
144 parent_node = &dirname[strlen(SYSFS_DT_BASEDIR)];
146 d = opendir(dirname);
147 if (!d) {
148 error_setg(&error_fatal, "%s cannot open %s", __func__, dirname);
151 while ((de = readdir(d)) != NULL) {
152 char *tmpnam;
154 if (!g_strcmp0(de->d_name, ".")
155 || !g_strcmp0(de->d_name, "..")) {
156 continue;
159 tmpnam = g_strdup_printf("%s/%s", dirname, de->d_name);
161 if (lstat(tmpnam, &st) < 0) {
162 error_setg(&error_fatal, "%s cannot lstat %s", __func__, tmpnam);
165 if (S_ISREG(st.st_mode)) {
166 gchar *val;
167 gsize len;
169 if (!g_file_get_contents(tmpnam, &val, &len, NULL)) {
170 error_setg(&error_fatal, "%s not able to extract info from %s",
171 __func__, tmpnam);
174 if (strlen(parent_node) > 0) {
175 qemu_fdt_setprop(fdt, parent_node,
176 de->d_name, val, len);
177 } else {
178 qemu_fdt_setprop(fdt, "/", de->d_name, val, len);
180 g_free(val);
181 } else if (S_ISDIR(st.st_mode)) {
182 char *node_name;
184 node_name = g_strdup_printf("%s/%s",
185 parent_node, de->d_name);
186 qemu_fdt_add_subnode(fdt, node_name);
187 g_free(node_name);
188 read_fstree(fdt, tmpnam);
191 g_free(tmpnam);
194 closedir(d);
197 /* load_device_tree_from_sysfs: extract the dt blob from host sysfs */
198 void *load_device_tree_from_sysfs(void)
200 void *host_fdt;
201 int host_fdt_size;
203 host_fdt = create_device_tree(&host_fdt_size);
204 read_fstree(host_fdt, SYSFS_DT_BASEDIR);
205 if (fdt_check_header(host_fdt)) {
206 error_setg(&error_fatal,
207 "%s host device tree extracted into memory is invalid",
208 __func__);
210 return host_fdt;
213 #endif /* CONFIG_LINUX */
215 static int findnode_nofail(void *fdt, const char *node_path)
217 int offset;
219 offset = fdt_path_offset(fdt, node_path);
220 if (offset < 0) {
221 error_report("%s Couldn't find node %s: %s", __func__, node_path,
222 fdt_strerror(offset));
223 exit(1);
226 return offset;
229 int qemu_fdt_setprop(void *fdt, const char *node_path,
230 const char *property, const void *val, int size)
232 int r;
234 r = fdt_setprop(fdt, findnode_nofail(fdt, node_path), property, val, size);
235 if (r < 0) {
236 error_report("%s: Couldn't set %s/%s: %s", __func__, node_path,
237 property, fdt_strerror(r));
238 exit(1);
241 return r;
244 int qemu_fdt_setprop_cell(void *fdt, const char *node_path,
245 const char *property, uint32_t val)
247 int r;
249 r = fdt_setprop_cell(fdt, findnode_nofail(fdt, node_path), property, val);
250 if (r < 0) {
251 error_report("%s: Couldn't set %s/%s = %#08x: %s", __func__,
252 node_path, property, val, fdt_strerror(r));
253 exit(1);
256 return r;
259 int qemu_fdt_setprop_u64(void *fdt, const char *node_path,
260 const char *property, uint64_t val)
262 val = cpu_to_be64(val);
263 return qemu_fdt_setprop(fdt, node_path, property, &val, sizeof(val));
266 int qemu_fdt_setprop_string(void *fdt, const char *node_path,
267 const char *property, const char *string)
269 int r;
271 r = fdt_setprop_string(fdt, findnode_nofail(fdt, node_path), property, string);
272 if (r < 0) {
273 error_report("%s: Couldn't set %s/%s = %s: %s", __func__,
274 node_path, property, string, fdt_strerror(r));
275 exit(1);
278 return r;
281 const void *qemu_fdt_getprop(void *fdt, const char *node_path,
282 const char *property, int *lenp)
284 int len;
285 const void *r;
286 if (!lenp) {
287 lenp = &len;
289 r = fdt_getprop(fdt, findnode_nofail(fdt, node_path), property, lenp);
290 if (!r) {
291 error_report("%s: Couldn't get %s/%s: %s", __func__,
292 node_path, property, fdt_strerror(*lenp));
293 exit(1);
295 return r;
298 uint32_t qemu_fdt_getprop_cell(void *fdt, const char *node_path,
299 const char *property)
301 int len;
302 const uint32_t *p = qemu_fdt_getprop(fdt, node_path, property, &len);
303 if (len != 4) {
304 error_report("%s: %s/%s not 4 bytes long (not a cell?)",
305 __func__, node_path, property);
306 exit(1);
308 return be32_to_cpu(*p);
311 uint32_t qemu_fdt_get_phandle(void *fdt, const char *path)
313 uint32_t r;
315 r = fdt_get_phandle(fdt, findnode_nofail(fdt, path));
316 if (r == 0) {
317 error_report("%s: Couldn't get phandle for %s: %s", __func__,
318 path, fdt_strerror(r));
319 exit(1);
322 return r;
325 int qemu_fdt_setprop_phandle(void *fdt, const char *node_path,
326 const char *property,
327 const char *target_node_path)
329 uint32_t phandle = qemu_fdt_get_phandle(fdt, target_node_path);
330 return qemu_fdt_setprop_cell(fdt, node_path, property, phandle);
333 uint32_t qemu_fdt_alloc_phandle(void *fdt)
335 static int phandle = 0x0;
338 * We need to find out if the user gave us special instruction at
339 * which phandle id to start allocating phandles.
341 if (!phandle) {
342 phandle = machine_phandle_start(current_machine);
345 if (!phandle) {
347 * None or invalid phandle given on the command line, so fall back to
348 * default starting point.
350 phandle = 0x8000;
353 return phandle++;
356 int qemu_fdt_nop_node(void *fdt, const char *node_path)
358 int r;
360 r = fdt_nop_node(fdt, findnode_nofail(fdt, node_path));
361 if (r < 0) {
362 error_report("%s: Couldn't nop node %s: %s", __func__, node_path,
363 fdt_strerror(r));
364 exit(1);
367 return r;
370 int qemu_fdt_add_subnode(void *fdt, const char *name)
372 char *dupname = g_strdup(name);
373 char *basename = strrchr(dupname, '/');
374 int retval;
375 int parent = 0;
377 if (!basename) {
378 g_free(dupname);
379 return -1;
382 basename[0] = '\0';
383 basename++;
385 if (dupname[0]) {
386 parent = findnode_nofail(fdt, dupname);
389 retval = fdt_add_subnode(fdt, parent, basename);
390 if (retval < 0) {
391 error_report("FDT: Failed to create subnode %s: %s", name,
392 fdt_strerror(retval));
393 exit(1);
396 g_free(dupname);
397 return retval;
400 void qemu_fdt_dumpdtb(void *fdt, int size)
402 const char *dumpdtb = qemu_opt_get(qemu_get_machine_opts(), "dumpdtb");
404 if (dumpdtb) {
405 /* Dump the dtb to a file and quit */
406 exit(g_file_set_contents(dumpdtb, fdt, size, NULL) ? 0 : 1);
410 int qemu_fdt_setprop_sized_cells_from_array(void *fdt,
411 const char *node_path,
412 const char *property,
413 int numvalues,
414 uint64_t *values)
416 uint32_t *propcells;
417 uint64_t value;
418 int cellnum, vnum, ncells;
419 uint32_t hival;
420 int ret;
422 propcells = g_new0(uint32_t, numvalues * 2);
424 cellnum = 0;
425 for (vnum = 0; vnum < numvalues; vnum++) {
426 ncells = values[vnum * 2];
427 if (ncells != 1 && ncells != 2) {
428 ret = -1;
429 goto out;
431 value = values[vnum * 2 + 1];
432 hival = cpu_to_be32(value >> 32);
433 if (ncells > 1) {
434 propcells[cellnum++] = hival;
435 } else if (hival != 0) {
436 ret = -1;
437 goto out;
439 propcells[cellnum++] = cpu_to_be32(value);
442 ret = qemu_fdt_setprop(fdt, node_path, property, propcells,
443 cellnum * sizeof(uint32_t));
444 out:
445 g_free(propcells);
446 return ret;