blob: 296278e12ae01020e7c0b8564587a7fa4d2dcf16 [file] [log] [blame]
aurel32f652e6a2008-12-16 10:43:48 +00001/*
2 * Functions to help device tree manipulation using libfdt.
3 * It also provides functions to read entries from device tree proc
4 * interface.
5 *
6 * Copyright 2008 IBM Corporation.
7 * Authors: Jerone Young <jyoung5@us.ibm.com>
8 * Hollis Blanchard <hollisb@us.ibm.com>
9 *
10 * This work is licensed under the GNU GPL license version 2 or later.
11 *
12 */
13
Peter Maydelld38ea872016-01-29 17:50:05 +000014#include "qemu/osdep.h"
aurel32f652e6a2008-12-16 10:43:48 +000015
Eric Auger60e43e92016-02-19 09:42:29 -070016#ifdef CONFIG_LINUX
17#include <dirent.h>
18#endif
19
Markus Armbrusterda34e652016-03-14 09:01:28 +010020#include "qapi/error.h"
Li Liudb013f82014-08-26 14:38:02 +080021#include "qemu/error-report.h"
Markus Armbruster922a01a2018-02-01 12:18:46 +010022#include "qemu/option.h"
Paolo Bonzini58369e22016-03-15 17:22:36 +010023#include "qemu/bswap.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010024#include "sysemu/device_tree.h"
Markus Armbruster2ff3de62013-07-04 15:09:22 +020025#include "sysemu/sysemu.h"
Blue Swirl39b7f202009-09-22 17:51:36 +000026#include "hw/loader.h"
Marcel Apfelbaum6cabe7f2015-02-04 17:43:53 +020027#include "hw/boards.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010028#include "qemu/config-file.h"
aurel32f652e6a2008-12-16 10:43:48 +000029
30#include <libfdt.h>
31
Geert Uytterhoeven14ec3cb2018-04-26 11:04:38 +010032#define FDT_MAX_SIZE 0x100000
Alexander Grafce362522012-05-17 15:33:54 +020033
34void *create_device_tree(int *sizep)
35{
36 void *fdt;
37 int ret;
38
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;
44 }
Peter Maydellef6de702013-11-22 17:17:09 +000045 ret = fdt_finish_reservemap(fdt);
46 if (ret < 0) {
47 goto fail;
48 }
Alexander Grafce362522012-05-17 15:33:54 +020049 ret = fdt_begin_node(fdt, "");
50 if (ret < 0) {
51 goto fail;
52 }
53 ret = fdt_end_node(fdt);
54 if (ret < 0) {
55 goto fail;
56 }
57 ret = fdt_finish(fdt);
58 if (ret < 0) {
59 goto fail;
60 }
61 ret = fdt_open_into(fdt, fdt, *sizep);
62 if (ret) {
Li Liu508e2212014-08-26 14:38:03 +080063 error_report("Unable to copy device tree in memory");
Alexander Grafce362522012-05-17 15:33:54 +020064 exit(1);
65 }
66
67 return fdt;
68fail:
Li Liu508e2212014-08-26 14:38:03 +080069 error_report("%s Couldn't create dt: %s", __func__, fdt_strerror(ret));
Alexander Grafce362522012-05-17 15:33:54 +020070 exit(1);
71}
72
pbrook7ec632b2009-04-10 16:23:59 +000073void *load_device_tree(const char *filename_path, int *sizep)
aurel32f652e6a2008-12-16 10:43:48 +000074{
pbrook7ec632b2009-04-10 16:23:59 +000075 int dt_size;
aurel32f652e6a2008-12-16 10:43:48 +000076 int dt_file_load_size;
aurel32f652e6a2008-12-16 10:43:48 +000077 int ret;
pbrook7ec632b2009-04-10 16:23:59 +000078 void *fdt = NULL;
aurel32f652e6a2008-12-16 10:43:48 +000079
pbrook7ec632b2009-04-10 16:23:59 +000080 *sizep = 0;
81 dt_size = get_image_size(filename_path);
82 if (dt_size < 0) {
Li Liudb013f82014-08-26 14:38:02 +080083 error_report("Unable to get size of device tree file '%s'",
84 filename_path);
aurel32f652e6a2008-12-16 10:43:48 +000085 goto fail;
86 }
87
pbrook7ec632b2009-04-10 16:23:59 +000088 /* Expand to 2x size to give enough room for manipulation. */
Alexander Grafded57c52011-07-23 10:54:11 +020089 dt_size += 10000;
pbrook7ec632b2009-04-10 16:23:59 +000090 dt_size *= 2;
aurel32f652e6a2008-12-16 10:43:48 +000091 /* First allocate space in qemu for device tree */
Anthony Liguori7267c092011-08-20 22:09:37 -050092 fdt = g_malloc0(dt_size);
aurel32f652e6a2008-12-16 10:43:48 +000093
Peter Maydellda885fe2018-12-14 13:30:52 +000094 dt_file_load_size = load_image_size(filename_path, fdt, dt_size);
pbrook7ec632b2009-04-10 16:23:59 +000095 if (dt_file_load_size < 0) {
Li Liudb013f82014-08-26 14:38:02 +080096 error_report("Unable to open device tree file '%s'",
97 filename_path);
pbrook7ec632b2009-04-10 16:23:59 +000098 goto fail;
99 }
aurel32f652e6a2008-12-16 10:43:48 +0000100
pbrook7ec632b2009-04-10 16:23:59 +0000101 ret = fdt_open_into(fdt, fdt, dt_size);
aurel32f652e6a2008-12-16 10:43:48 +0000102 if (ret) {
Li Liudb013f82014-08-26 14:38:02 +0800103 error_report("Unable to copy device tree in memory");
aurel32f652e6a2008-12-16 10:43:48 +0000104 goto fail;
105 }
106
107 /* Check sanity of device tree */
108 if (fdt_check_header(fdt)) {
Li Liudb013f82014-08-26 14:38:02 +0800109 error_report("Device tree file loaded into memory is invalid: %s",
110 filename_path);
aurel32f652e6a2008-12-16 10:43:48 +0000111 goto fail;
112 }
pbrook7ec632b2009-04-10 16:23:59 +0000113 *sizep = dt_size;
aurel32f652e6a2008-12-16 10:43:48 +0000114 return fdt;
115
116fail:
Anthony Liguori7267c092011-08-20 22:09:37 -0500117 g_free(fdt);
aurel32f652e6a2008-12-16 10:43:48 +0000118 return NULL;
119}
120
Eric Auger60e43e92016-02-19 09:42:29 -0700121#ifdef CONFIG_LINUX
122
123#define SYSFS_DT_BASEDIR "/proc/device-tree"
124
125/**
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).
131 *
132 * the function asserts in case of error
133 */
134static void read_fstree(void *fdt, const char *dirname)
135{
136 DIR *d;
137 struct dirent *de;
138 struct stat st;
139 const char *root_dir = SYSFS_DT_BASEDIR;
140 const char *parent_node;
141
142 if (strstr(dirname, root_dir) != dirname) {
Philippe Mathieu-Daudé38754e42018-06-29 15:11:00 +0100143 error_report("%s: %s must be searched within %s",
144 __func__, dirname, root_dir);
145 exit(1);
Eric Auger60e43e92016-02-19 09:42:29 -0700146 }
147 parent_node = &dirname[strlen(SYSFS_DT_BASEDIR)];
148
149 d = opendir(dirname);
150 if (!d) {
Philippe Mathieu-Daudé38754e42018-06-29 15:11:00 +0100151 error_report("%s cannot open %s", __func__, dirname);
152 exit(1);
Eric Auger60e43e92016-02-19 09:42:29 -0700153 }
154
155 while ((de = readdir(d)) != NULL) {
156 char *tmpnam;
157
158 if (!g_strcmp0(de->d_name, ".")
159 || !g_strcmp0(de->d_name, "..")) {
160 continue;
161 }
162
163 tmpnam = g_strdup_printf("%s/%s", dirname, de->d_name);
164
165 if (lstat(tmpnam, &st) < 0) {
Philippe Mathieu-Daudé38754e42018-06-29 15:11:00 +0100166 error_report("%s cannot lstat %s", __func__, tmpnam);
167 exit(1);
Eric Auger60e43e92016-02-19 09:42:29 -0700168 }
169
170 if (S_ISREG(st.st_mode)) {
171 gchar *val;
172 gsize len;
173
174 if (!g_file_get_contents(tmpnam, &val, &len, NULL)) {
Philippe Mathieu-Daudé38754e42018-06-29 15:11:00 +0100175 error_report("%s not able to extract info from %s",
176 __func__, tmpnam);
177 exit(1);
Eric Auger60e43e92016-02-19 09:42:29 -0700178 }
179
180 if (strlen(parent_node) > 0) {
181 qemu_fdt_setprop(fdt, parent_node,
182 de->d_name, val, len);
183 } else {
184 qemu_fdt_setprop(fdt, "/", de->d_name, val, len);
185 }
186 g_free(val);
187 } else if (S_ISDIR(st.st_mode)) {
188 char *node_name;
189
190 node_name = g_strdup_printf("%s/%s",
191 parent_node, de->d_name);
192 qemu_fdt_add_subnode(fdt, node_name);
193 g_free(node_name);
194 read_fstree(fdt, tmpnam);
195 }
196
197 g_free(tmpnam);
198 }
199
200 closedir(d);
201}
202
203/* load_device_tree_from_sysfs: extract the dt blob from host sysfs */
204void *load_device_tree_from_sysfs(void)
205{
206 void *host_fdt;
207 int host_fdt_size;
208
209 host_fdt = create_device_tree(&host_fdt_size);
210 read_fstree(host_fdt, SYSFS_DT_BASEDIR);
211 if (fdt_check_header(host_fdt)) {
Philippe Mathieu-Daudé38754e42018-06-29 15:11:00 +0100212 error_report("%s host device tree extracted into memory is invalid",
213 __func__);
214 exit(1);
Eric Auger60e43e92016-02-19 09:42:29 -0700215 }
216 return host_fdt;
217}
218
219#endif /* CONFIG_LINUX */
220
Alexander Grafccbcfed2011-07-23 10:52:00 +0200221static int findnode_nofail(void *fdt, const char *node_path)
aurel32f652e6a2008-12-16 10:43:48 +0000222{
223 int offset;
224
225 offset = fdt_path_offset(fdt, node_path);
Alexander Grafccbcfed2011-07-23 10:52:00 +0200226 if (offset < 0) {
Li Liu508e2212014-08-26 14:38:03 +0800227 error_report("%s Couldn't find node %s: %s", __func__, node_path,
228 fdt_strerror(offset));
Alexander Grafccbcfed2011-07-23 10:52:00 +0200229 exit(1);
230 }
aurel32f652e6a2008-12-16 10:43:48 +0000231
Alexander Grafccbcfed2011-07-23 10:52:00 +0200232 return offset;
233}
234
Eric Augerf963cc22018-06-29 15:11:01 +0100235char **qemu_fdt_node_unit_path(void *fdt, const char *name, Error **errp)
236{
237 char *prefix = g_strdup_printf("%s@", name);
238 unsigned int path_len = 16, n = 0;
239 GSList *path_list = NULL, *iter;
240 const char *iter_name;
241 int offset, len, ret;
242 char **path_array;
243
244 offset = fdt_next_node(fdt, -1, NULL);
245
246 while (offset >= 0) {
247 iter_name = fdt_get_name(fdt, offset, &len);
248 if (!iter_name) {
249 offset = len;
250 break;
251 }
252 if (!strcmp(iter_name, name) || g_str_has_prefix(iter_name, prefix)) {
253 char *path;
254
255 path = g_malloc(path_len);
256 while ((ret = fdt_get_path(fdt, offset, path, path_len))
257 == -FDT_ERR_NOSPACE) {
258 path_len += 16;
259 path = g_realloc(path, path_len);
260 }
261 path_list = g_slist_prepend(path_list, path);
262 n++;
263 }
264 offset = fdt_next_node(fdt, offset, NULL);
265 }
266 g_free(prefix);
267
268 if (offset < 0 && offset != -FDT_ERR_NOTFOUND) {
269 error_setg(errp, "%s: abort parsing dt for %s node units: %s",
270 __func__, name, fdt_strerror(offset));
271 for (iter = path_list; iter; iter = iter->next) {
272 g_free(iter->data);
273 }
274 g_slist_free(path_list);
275 return NULL;
276 }
277
278 path_array = g_new(char *, n + 1);
279 path_array[n--] = NULL;
280
281 for (iter = path_list; iter; iter = iter->next) {
282 path_array[n--] = iter->data;
283 }
284
285 g_slist_free(path_list);
286
287 return path_array;
288}
289
Eric Auger6d795662016-02-19 09:42:30 -0700290char **qemu_fdt_node_path(void *fdt, const char *name, char *compat,
291 Error **errp)
292{
293 int offset, len, ret;
294 const char *iter_name;
295 unsigned int path_len = 16, n = 0;
296 GSList *path_list = NULL, *iter;
297 char **path_array;
298
299 offset = fdt_node_offset_by_compatible(fdt, -1, compat);
300
301 while (offset >= 0) {
302 iter_name = fdt_get_name(fdt, offset, &len);
303 if (!iter_name) {
304 offset = len;
305 break;
306 }
307 if (!strcmp(iter_name, name)) {
308 char *path;
309
310 path = g_malloc(path_len);
311 while ((ret = fdt_get_path(fdt, offset, path, path_len))
312 == -FDT_ERR_NOSPACE) {
313 path_len += 16;
314 path = g_realloc(path, path_len);
315 }
316 path_list = g_slist_prepend(path_list, path);
317 n++;
318 }
319 offset = fdt_node_offset_by_compatible(fdt, offset, compat);
320 }
321
322 if (offset < 0 && offset != -FDT_ERR_NOTFOUND) {
323 error_setg(errp, "%s: abort parsing dt for %s/%s: %s",
324 __func__, name, compat, fdt_strerror(offset));
325 for (iter = path_list; iter; iter = iter->next) {
326 g_free(iter->data);
327 }
328 g_slist_free(path_list);
329 return NULL;
330 }
331
332 path_array = g_new(char *, n + 1);
333 path_array[n--] = NULL;
334
335 for (iter = path_list; iter; iter = iter->next) {
336 path_array[n--] = iter->data;
337 }
338
339 g_slist_free(path_list);
340
341 return path_array;
342}
343
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000344int qemu_fdt_setprop(void *fdt, const char *node_path,
Peter Crosthwaitebe5907f2013-11-11 18:15:21 +1000345 const char *property, const void *val, int size)
Alexander Grafccbcfed2011-07-23 10:52:00 +0200346{
347 int r;
348
Peter Crosthwaitebe5907f2013-11-11 18:15:21 +1000349 r = fdt_setprop(fdt, findnode_nofail(fdt, node_path), property, val, size);
Alexander Grafccbcfed2011-07-23 10:52:00 +0200350 if (r < 0) {
Li Liu508e2212014-08-26 14:38:03 +0800351 error_report("%s: Couldn't set %s/%s: %s", __func__, node_path,
352 property, fdt_strerror(r));
Alexander Grafccbcfed2011-07-23 10:52:00 +0200353 exit(1);
354 }
355
356 return r;
aurel32f652e6a2008-12-16 10:43:48 +0000357}
358
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000359int qemu_fdt_setprop_cell(void *fdt, const char *node_path,
360 const char *property, uint32_t val)
aurel32f652e6a2008-12-16 10:43:48 +0000361{
Alexander Grafccbcfed2011-07-23 10:52:00 +0200362 int r;
aurel32f652e6a2008-12-16 10:43:48 +0000363
Alexander Grafccbcfed2011-07-23 10:52:00 +0200364 r = fdt_setprop_cell(fdt, findnode_nofail(fdt, node_path), property, val);
365 if (r < 0) {
Li Liu508e2212014-08-26 14:38:03 +0800366 error_report("%s: Couldn't set %s/%s = %#08x: %s", __func__,
367 node_path, property, val, fdt_strerror(r));
Alexander Grafccbcfed2011-07-23 10:52:00 +0200368 exit(1);
369 }
aurel32f652e6a2008-12-16 10:43:48 +0000370
Alexander Grafccbcfed2011-07-23 10:52:00 +0200371 return r;
aurel32f652e6a2008-12-16 10:43:48 +0000372}
373
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000374int qemu_fdt_setprop_u64(void *fdt, const char *node_path,
375 const char *property, uint64_t val)
Alexander Grafbb28eb32012-05-18 01:53:01 +0200376{
377 val = cpu_to_be64(val);
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000378 return qemu_fdt_setprop(fdt, node_path, property, &val, sizeof(val));
Alexander Grafbb28eb32012-05-18 01:53:01 +0200379}
380
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000381int qemu_fdt_setprop_string(void *fdt, const char *node_path,
382 const char *property, const char *string)
aurel32f652e6a2008-12-16 10:43:48 +0000383{
Alexander Grafccbcfed2011-07-23 10:52:00 +0200384 int r;
aurel32f652e6a2008-12-16 10:43:48 +0000385
Alexander Grafccbcfed2011-07-23 10:52:00 +0200386 r = fdt_setprop_string(fdt, findnode_nofail(fdt, node_path), property, string);
387 if (r < 0) {
Li Liu508e2212014-08-26 14:38:03 +0800388 error_report("%s: Couldn't set %s/%s = %s: %s", __func__,
389 node_path, property, string, fdt_strerror(r));
Alexander Grafccbcfed2011-07-23 10:52:00 +0200390 exit(1);
391 }
aurel32f652e6a2008-12-16 10:43:48 +0000392
Alexander Grafccbcfed2011-07-23 10:52:00 +0200393 return r;
aurel32f652e6a2008-12-16 10:43:48 +0000394}
Alexander Grafd69a8e62011-07-21 01:52:57 +0200395
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000396const void *qemu_fdt_getprop(void *fdt, const char *node_path,
Eric Auger78e24f22016-02-19 09:42:30 -0700397 const char *property, int *lenp, Error **errp)
Peter Maydellf0aa7132012-07-20 13:34:50 +0100398{
399 int len;
400 const void *r;
Eric Auger78e24f22016-02-19 09:42:30 -0700401
Peter Maydellf0aa7132012-07-20 13:34:50 +0100402 if (!lenp) {
403 lenp = &len;
404 }
405 r = fdt_getprop(fdt, findnode_nofail(fdt, node_path), property, lenp);
406 if (!r) {
Eric Auger78e24f22016-02-19 09:42:30 -0700407 error_setg(errp, "%s: Couldn't get %s/%s: %s", __func__,
408 node_path, property, fdt_strerror(*lenp));
Peter Maydellf0aa7132012-07-20 13:34:50 +0100409 }
410 return r;
411}
412
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000413uint32_t qemu_fdt_getprop_cell(void *fdt, const char *node_path,
Eric Auger58e71092016-02-19 09:42:30 -0700414 const char *property, int *lenp, Error **errp)
Peter Maydellf0aa7132012-07-20 13:34:50 +0100415{
416 int len;
Eric Auger58e71092016-02-19 09:42:30 -0700417 const uint32_t *p;
418
419 if (!lenp) {
420 lenp = &len;
421 }
422 p = qemu_fdt_getprop(fdt, node_path, property, lenp, errp);
423 if (!p) {
424 return 0;
425 } else if (*lenp != 4) {
426 error_setg(errp, "%s: %s/%s not 4 bytes long (not a cell?)",
427 __func__, node_path, property);
428 *lenp = -EINVAL;
429 return 0;
Peter Maydellf0aa7132012-07-20 13:34:50 +0100430 }
431 return be32_to_cpu(*p);
432}
433
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000434uint32_t qemu_fdt_get_phandle(void *fdt, const char *path)
Alexander Graf7d5fd102012-05-17 15:23:39 +0200435{
436 uint32_t r;
437
438 r = fdt_get_phandle(fdt, findnode_nofail(fdt, path));
Stefan Weil909a1962013-06-10 22:12:25 +0200439 if (r == 0) {
Li Liu508e2212014-08-26 14:38:03 +0800440 error_report("%s: Couldn't get phandle for %s: %s", __func__,
441 path, fdt_strerror(r));
Alexander Graf7d5fd102012-05-17 15:23:39 +0200442 exit(1);
443 }
444
445 return r;
446}
447
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000448int qemu_fdt_setprop_phandle(void *fdt, const char *node_path,
449 const char *property,
450 const char *target_node_path)
Alexander Graf8535ab12012-05-17 14:11:52 +0200451{
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000452 uint32_t phandle = qemu_fdt_get_phandle(fdt, target_node_path);
453 return qemu_fdt_setprop_cell(fdt, node_path, property, phandle);
Alexander Graf8535ab12012-05-17 14:11:52 +0200454}
455
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000456uint32_t qemu_fdt_alloc_phandle(void *fdt)
Alexander Graf3601b572012-05-17 16:58:55 +0200457{
Alexander Graf4b1b1c82012-06-06 01:01:23 +0200458 static int phandle = 0x0;
459
460 /*
461 * We need to find out if the user gave us special instruction at
Kamalesh Babulalcc47a162015-07-24 13:48:13 +0530462 * which phandle id to start allocating phandles.
Alexander Graf4b1b1c82012-06-06 01:01:23 +0200463 */
464 if (!phandle) {
Marcel Apfelbaum6cabe7f2015-02-04 17:43:53 +0200465 phandle = machine_phandle_start(current_machine);
Alexander Graf4b1b1c82012-06-06 01:01:23 +0200466 }
467
468 if (!phandle) {
469 /*
470 * None or invalid phandle given on the command line, so fall back to
471 * default starting point.
472 */
473 phandle = 0x8000;
474 }
Alexander Graf3601b572012-05-17 16:58:55 +0200475
476 return phandle++;
477}
478
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000479int qemu_fdt_nop_node(void *fdt, const char *node_path)
Alexander Grafd69a8e62011-07-21 01:52:57 +0200480{
Alexander Grafccbcfed2011-07-23 10:52:00 +0200481 int r;
Alexander Grafd69a8e62011-07-21 01:52:57 +0200482
Alexander Grafccbcfed2011-07-23 10:52:00 +0200483 r = fdt_nop_node(fdt, findnode_nofail(fdt, node_path));
484 if (r < 0) {
Li Liu508e2212014-08-26 14:38:03 +0800485 error_report("%s: Couldn't nop node %s: %s", __func__, node_path,
486 fdt_strerror(r));
Alexander Grafccbcfed2011-07-23 10:52:00 +0200487 exit(1);
488 }
Alexander Grafd69a8e62011-07-21 01:52:57 +0200489
Alexander Grafccbcfed2011-07-23 10:52:00 +0200490 return r;
Alexander Grafd69a8e62011-07-21 01:52:57 +0200491}
Alexander Graf80ad7812011-07-22 13:55:37 +0200492
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000493int qemu_fdt_add_subnode(void *fdt, const char *name)
Alexander Graf80ad7812011-07-22 13:55:37 +0200494{
Alexander Graf80ad7812011-07-22 13:55:37 +0200495 char *dupname = g_strdup(name);
496 char *basename = strrchr(dupname, '/');
497 int retval;
Alexander Grafc640d082012-05-17 11:40:42 +0200498 int parent = 0;
Alexander Graf80ad7812011-07-22 13:55:37 +0200499
500 if (!basename) {
Stefan Weilbff39b62011-10-17 22:12:09 +0200501 g_free(dupname);
Alexander Graf80ad7812011-07-22 13:55:37 +0200502 return -1;
503 }
504
505 basename[0] = '\0';
506 basename++;
507
Alexander Grafc640d082012-05-17 11:40:42 +0200508 if (dupname[0]) {
509 parent = findnode_nofail(fdt, dupname);
510 }
511
512 retval = fdt_add_subnode(fdt, parent, basename);
Alexander Grafccbcfed2011-07-23 10:52:00 +0200513 if (retval < 0) {
Li Liu508e2212014-08-26 14:38:03 +0800514 error_report("FDT: Failed to create subnode %s: %s", name,
515 fdt_strerror(retval));
Alexander Grafccbcfed2011-07-23 10:52:00 +0200516 exit(1);
Alexander Graf80ad7812011-07-22 13:55:37 +0200517 }
518
Alexander Graf80ad7812011-07-22 13:55:37 +0200519 g_free(dupname);
520 return retval;
521}
Alexander Graf71193432012-09-23 08:37:59 +0200522
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000523void qemu_fdt_dumpdtb(void *fdt, int size)
Alexander Graf71193432012-09-23 08:37:59 +0200524{
Markus Armbruster2ff3de62013-07-04 15:09:22 +0200525 const char *dumpdtb = qemu_opt_get(qemu_get_machine_opts(), "dumpdtb");
Alexander Graf71193432012-09-23 08:37:59 +0200526
Markus Armbruster2ff3de62013-07-04 15:09:22 +0200527 if (dumpdtb) {
528 /* Dump the dtb to a file and quit */
529 exit(g_file_set_contents(dumpdtb, fdt, size, NULL) ? 0 : 1);
Alexander Graf71193432012-09-23 08:37:59 +0200530 }
Alexander Graf71193432012-09-23 08:37:59 +0200531}
Peter Maydell97c38f82013-07-16 13:25:05 +0100532
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000533int qemu_fdt_setprop_sized_cells_from_array(void *fdt,
534 const char *node_path,
535 const char *property,
536 int numvalues,
537 uint64_t *values)
Peter Maydell97c38f82013-07-16 13:25:05 +0100538{
539 uint32_t *propcells;
540 uint64_t value;
541 int cellnum, vnum, ncells;
542 uint32_t hival;
Sergey Fedorov2bf9feb2014-12-11 18:45:05 +0300543 int ret;
Peter Maydell97c38f82013-07-16 13:25:05 +0100544
545 propcells = g_new0(uint32_t, numvalues * 2);
546
547 cellnum = 0;
548 for (vnum = 0; vnum < numvalues; vnum++) {
549 ncells = values[vnum * 2];
550 if (ncells != 1 && ncells != 2) {
Sergey Fedorov2bf9feb2014-12-11 18:45:05 +0300551 ret = -1;
552 goto out;
Peter Maydell97c38f82013-07-16 13:25:05 +0100553 }
554 value = values[vnum * 2 + 1];
555 hival = cpu_to_be32(value >> 32);
556 if (ncells > 1) {
557 propcells[cellnum++] = hival;
558 } else if (hival != 0) {
Sergey Fedorov2bf9feb2014-12-11 18:45:05 +0300559 ret = -1;
560 goto out;
Peter Maydell97c38f82013-07-16 13:25:05 +0100561 }
562 propcells[cellnum++] = cpu_to_be32(value);
563 }
564
Sergey Fedorov2bf9feb2014-12-11 18:45:05 +0300565 ret = qemu_fdt_setprop(fdt, node_path, property, propcells,
566 cellnum * sizeof(uint32_t));
567out:
568 g_free(propcells);
569 return ret;
Peter Maydell97c38f82013-07-16 13:25:05 +0100570}