target/s390x: simplify gs_allowed()
[qemu.git] / device_tree.c
bloba24ddff02bdd857cbbf50477879c07df19d89054
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 "qapi/error.h"
21 #include "qemu-common.h"
22 #include "qemu/error-report.h"
23 #include "qemu/bswap.h"
24 #include "sysemu/device_tree.h"
25 #include "sysemu/sysemu.h"
26 #include "hw/loader.h"
27 #include "hw/boards.h"
28 #include "qemu/config-file.h"
30 #include <libfdt.h>
32 #define FDT_MAX_SIZE 0x10000
34 void *create_device_tree(int *sizep)
36 void *fdt;
37 int ret;
39 *sizep = FDT_MAX_SIZE;
40 fdt = g_malloc0(FDT_MAX_SIZE);
41 ret = fdt_create(fdt, FDT_MAX_SIZE);
42 if (ret < 0) {
43 goto fail;
45 ret = fdt_finish_reservemap(fdt);
46 if (ret < 0) {
47 goto fail;
49 ret = fdt_begin_node(fdt, "");
50 if (ret < 0) {
51 goto fail;
53 ret = fdt_end_node(fdt);
54 if (ret < 0) {
55 goto fail;
57 ret = fdt_finish(fdt);
58 if (ret < 0) {
59 goto fail;
61 ret = fdt_open_into(fdt, fdt, *sizep);
62 if (ret) {
63 error_report("Unable to copy device tree in memory");
64 exit(1);
67 return fdt;
68 fail:
69 error_report("%s Couldn't create dt: %s", __func__, fdt_strerror(ret));
70 exit(1);
73 void *load_device_tree(const char *filename_path, int *sizep)
75 int dt_size;
76 int dt_file_load_size;
77 int ret;
78 void *fdt = NULL;
80 *sizep = 0;
81 dt_size = get_image_size(filename_path);
82 if (dt_size < 0) {
83 error_report("Unable to get size of device tree file '%s'",
84 filename_path);
85 goto fail;
88 /* Expand to 2x size to give enough room for manipulation. */
89 dt_size += 10000;
90 dt_size *= 2;
91 /* First allocate space in qemu for device tree */
92 fdt = g_malloc0(dt_size);
94 dt_file_load_size = load_image(filename_path, fdt);
95 if (dt_file_load_size < 0) {
96 error_report("Unable to open device tree file '%s'",
97 filename_path);
98 goto fail;
101 ret = fdt_open_into(fdt, fdt, dt_size);
102 if (ret) {
103 error_report("Unable to copy device tree in memory");
104 goto fail;
107 /* Check sanity of device tree */
108 if (fdt_check_header(fdt)) {
109 error_report("Device tree file loaded into memory is invalid: %s",
110 filename_path);
111 goto fail;
113 *sizep = dt_size;
114 return fdt;
116 fail:
117 g_free(fdt);
118 return NULL;
121 #ifdef CONFIG_LINUX
123 #define SYSFS_DT_BASEDIR "/proc/device-tree"
126 * read_fstree: this function is inspired from dtc read_fstree
127 * @fdt: preallocated fdt blob buffer, to be populated
128 * @dirname: directory to scan under SYSFS_DT_BASEDIR
129 * the search is recursive and the tree is searched down to the
130 * leaves (property files).
132 * the function asserts in case of error
134 static void read_fstree(void *fdt, const char *dirname)
136 DIR *d;
137 struct dirent *de;
138 struct stat st;
139 const char *root_dir = SYSFS_DT_BASEDIR;
140 const char *parent_node;
142 if (strstr(dirname, root_dir) != dirname) {
143 error_setg(&error_fatal, "%s: %s must be searched within %s",
144 __func__, dirname, root_dir);
146 parent_node = &dirname[strlen(SYSFS_DT_BASEDIR)];
148 d = opendir(dirname);
149 if (!d) {
150 error_setg(&error_fatal, "%s cannot open %s", __func__, dirname);
151 return;
154 while ((de = readdir(d)) != NULL) {
155 char *tmpnam;
157 if (!g_strcmp0(de->d_name, ".")
158 || !g_strcmp0(de->d_name, "..")) {
159 continue;
162 tmpnam = g_strdup_printf("%s/%s", dirname, de->d_name);
164 if (lstat(tmpnam, &st) < 0) {
165 error_setg(&error_fatal, "%s cannot lstat %s", __func__, tmpnam);
168 if (S_ISREG(st.st_mode)) {
169 gchar *val;
170 gsize len;
172 if (!g_file_get_contents(tmpnam, &val, &len, NULL)) {
173 error_setg(&error_fatal, "%s not able to extract info from %s",
174 __func__, tmpnam);
177 if (strlen(parent_node) > 0) {
178 qemu_fdt_setprop(fdt, parent_node,
179 de->d_name, val, len);
180 } else {
181 qemu_fdt_setprop(fdt, "/", de->d_name, val, len);
183 g_free(val);
184 } else if (S_ISDIR(st.st_mode)) {
185 char *node_name;
187 node_name = g_strdup_printf("%s/%s",
188 parent_node, de->d_name);
189 qemu_fdt_add_subnode(fdt, node_name);
190 g_free(node_name);
191 read_fstree(fdt, tmpnam);
194 g_free(tmpnam);
197 closedir(d);
200 /* load_device_tree_from_sysfs: extract the dt blob from host sysfs */
201 void *load_device_tree_from_sysfs(void)
203 void *host_fdt;
204 int host_fdt_size;
206 host_fdt = create_device_tree(&host_fdt_size);
207 read_fstree(host_fdt, SYSFS_DT_BASEDIR);
208 if (fdt_check_header(host_fdt)) {
209 error_setg(&error_fatal,
210 "%s host device tree extracted into memory is invalid",
211 __func__);
213 return host_fdt;
216 #endif /* CONFIG_LINUX */
218 static int findnode_nofail(void *fdt, const char *node_path)
220 int offset;
222 offset = fdt_path_offset(fdt, node_path);
223 if (offset < 0) {
224 error_report("%s Couldn't find node %s: %s", __func__, node_path,
225 fdt_strerror(offset));
226 exit(1);
229 return offset;
232 char **qemu_fdt_node_path(void *fdt, const char *name, char *compat,
233 Error **errp)
235 int offset, len, ret;
236 const char *iter_name;
237 unsigned int path_len = 16, n = 0;
238 GSList *path_list = NULL, *iter;
239 char **path_array;
241 offset = fdt_node_offset_by_compatible(fdt, -1, compat);
243 while (offset >= 0) {
244 iter_name = fdt_get_name(fdt, offset, &len);
245 if (!iter_name) {
246 offset = len;
247 break;
249 if (!strcmp(iter_name, name)) {
250 char *path;
252 path = g_malloc(path_len);
253 while ((ret = fdt_get_path(fdt, offset, path, path_len))
254 == -FDT_ERR_NOSPACE) {
255 path_len += 16;
256 path = g_realloc(path, path_len);
258 path_list = g_slist_prepend(path_list, path);
259 n++;
261 offset = fdt_node_offset_by_compatible(fdt, offset, compat);
264 if (offset < 0 && offset != -FDT_ERR_NOTFOUND) {
265 error_setg(errp, "%s: abort parsing dt for %s/%s: %s",
266 __func__, name, compat, fdt_strerror(offset));
267 for (iter = path_list; iter; iter = iter->next) {
268 g_free(iter->data);
270 g_slist_free(path_list);
271 return NULL;
274 path_array = g_new(char *, n + 1);
275 path_array[n--] = NULL;
277 for (iter = path_list; iter; iter = iter->next) {
278 path_array[n--] = iter->data;
281 g_slist_free(path_list);
283 return path_array;
286 int qemu_fdt_setprop(void *fdt, const char *node_path,
287 const char *property, const void *val, int size)
289 int r;
291 r = fdt_setprop(fdt, findnode_nofail(fdt, node_path), property, val, size);
292 if (r < 0) {
293 error_report("%s: Couldn't set %s/%s: %s", __func__, node_path,
294 property, fdt_strerror(r));
295 exit(1);
298 return r;
301 int qemu_fdt_setprop_cell(void *fdt, const char *node_path,
302 const char *property, uint32_t val)
304 int r;
306 r = fdt_setprop_cell(fdt, findnode_nofail(fdt, node_path), property, val);
307 if (r < 0) {
308 error_report("%s: Couldn't set %s/%s = %#08x: %s", __func__,
309 node_path, property, val, fdt_strerror(r));
310 exit(1);
313 return r;
316 int qemu_fdt_setprop_u64(void *fdt, const char *node_path,
317 const char *property, uint64_t val)
319 val = cpu_to_be64(val);
320 return qemu_fdt_setprop(fdt, node_path, property, &val, sizeof(val));
323 int qemu_fdt_setprop_string(void *fdt, const char *node_path,
324 const char *property, const char *string)
326 int r;
328 r = fdt_setprop_string(fdt, findnode_nofail(fdt, node_path), property, string);
329 if (r < 0) {
330 error_report("%s: Couldn't set %s/%s = %s: %s", __func__,
331 node_path, property, string, fdt_strerror(r));
332 exit(1);
335 return r;
338 const void *qemu_fdt_getprop(void *fdt, const char *node_path,
339 const char *property, int *lenp, Error **errp)
341 int len;
342 const void *r;
344 if (!lenp) {
345 lenp = &len;
347 r = fdt_getprop(fdt, findnode_nofail(fdt, node_path), property, lenp);
348 if (!r) {
349 error_setg(errp, "%s: Couldn't get %s/%s: %s", __func__,
350 node_path, property, fdt_strerror(*lenp));
352 return r;
355 uint32_t qemu_fdt_getprop_cell(void *fdt, const char *node_path,
356 const char *property, int *lenp, Error **errp)
358 int len;
359 const uint32_t *p;
361 if (!lenp) {
362 lenp = &len;
364 p = qemu_fdt_getprop(fdt, node_path, property, lenp, errp);
365 if (!p) {
366 return 0;
367 } else if (*lenp != 4) {
368 error_setg(errp, "%s: %s/%s not 4 bytes long (not a cell?)",
369 __func__, node_path, property);
370 *lenp = -EINVAL;
371 return 0;
373 return be32_to_cpu(*p);
376 uint32_t qemu_fdt_get_phandle(void *fdt, const char *path)
378 uint32_t r;
380 r = fdt_get_phandle(fdt, findnode_nofail(fdt, path));
381 if (r == 0) {
382 error_report("%s: Couldn't get phandle for %s: %s", __func__,
383 path, fdt_strerror(r));
384 exit(1);
387 return r;
390 int qemu_fdt_setprop_phandle(void *fdt, const char *node_path,
391 const char *property,
392 const char *target_node_path)
394 uint32_t phandle = qemu_fdt_get_phandle(fdt, target_node_path);
395 return qemu_fdt_setprop_cell(fdt, node_path, property, phandle);
398 uint32_t qemu_fdt_alloc_phandle(void *fdt)
400 static int phandle = 0x0;
403 * We need to find out if the user gave us special instruction at
404 * which phandle id to start allocating phandles.
406 if (!phandle) {
407 phandle = machine_phandle_start(current_machine);
410 if (!phandle) {
412 * None or invalid phandle given on the command line, so fall back to
413 * default starting point.
415 phandle = 0x8000;
418 return phandle++;
421 int qemu_fdt_nop_node(void *fdt, const char *node_path)
423 int r;
425 r = fdt_nop_node(fdt, findnode_nofail(fdt, node_path));
426 if (r < 0) {
427 error_report("%s: Couldn't nop node %s: %s", __func__, node_path,
428 fdt_strerror(r));
429 exit(1);
432 return r;
435 int qemu_fdt_add_subnode(void *fdt, const char *name)
437 char *dupname = g_strdup(name);
438 char *basename = strrchr(dupname, '/');
439 int retval;
440 int parent = 0;
442 if (!basename) {
443 g_free(dupname);
444 return -1;
447 basename[0] = '\0';
448 basename++;
450 if (dupname[0]) {
451 parent = findnode_nofail(fdt, dupname);
454 retval = fdt_add_subnode(fdt, parent, basename);
455 if (retval < 0) {
456 error_report("FDT: Failed to create subnode %s: %s", name,
457 fdt_strerror(retval));
458 exit(1);
461 g_free(dupname);
462 return retval;
465 void qemu_fdt_dumpdtb(void *fdt, int size)
467 const char *dumpdtb = qemu_opt_get(qemu_get_machine_opts(), "dumpdtb");
469 if (dumpdtb) {
470 /* Dump the dtb to a file and quit */
471 exit(g_file_set_contents(dumpdtb, fdt, size, NULL) ? 0 : 1);
475 int qemu_fdt_setprop_sized_cells_from_array(void *fdt,
476 const char *node_path,
477 const char *property,
478 int numvalues,
479 uint64_t *values)
481 uint32_t *propcells;
482 uint64_t value;
483 int cellnum, vnum, ncells;
484 uint32_t hival;
485 int ret;
487 propcells = g_new0(uint32_t, numvalues * 2);
489 cellnum = 0;
490 for (vnum = 0; vnum < numvalues; vnum++) {
491 ncells = values[vnum * 2];
492 if (ncells != 1 && ncells != 2) {
493 ret = -1;
494 goto out;
496 value = values[vnum * 2 + 1];
497 hival = cpu_to_be32(value >> 32);
498 if (ncells > 1) {
499 propcells[cellnum++] = hival;
500 } else if (hival != 0) {
501 ret = -1;
502 goto out;
504 propcells[cellnum++] = cpu_to_be32(value);
507 ret = qemu_fdt_setprop(fdt, node_path, property, propcells,
508 cellnum * sizeof(uint32_t));
509 out:
510 g_free(propcells);
511 return ret;