blob: 3965c834ca629f04266c7389b26e57ff6a60e345 [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"
Alex Bennée78da6a12021-03-03 17:36:38 +000024#include "qemu/cutils.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010025#include "sysemu/device_tree.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 }
Markus Armbruster065e6292019-04-09 19:40:18 +020087 if (dt_size > INT_MAX / 2 - 10000) {
88 error_report("Device tree file '%s' is too large", filename_path);
89 goto fail;
90 }
aurel32f652e6a2008-12-16 10:43:48 +000091
pbrook7ec632b2009-04-10 16:23:59 +000092 /* Expand to 2x size to give enough room for manipulation. */
Alexander Grafded57c52011-07-23 10:54:11 +020093 dt_size += 10000;
pbrook7ec632b2009-04-10 16:23:59 +000094 dt_size *= 2;
aurel32f652e6a2008-12-16 10:43:48 +000095 /* First allocate space in qemu for device tree */
Anthony Liguori7267c092011-08-20 22:09:37 -050096 fdt = g_malloc0(dt_size);
aurel32f652e6a2008-12-16 10:43:48 +000097
Peter Maydellda885fe2018-12-14 13:30:52 +000098 dt_file_load_size = load_image_size(filename_path, fdt, dt_size);
pbrook7ec632b2009-04-10 16:23:59 +000099 if (dt_file_load_size < 0) {
Li Liudb013f82014-08-26 14:38:02 +0800100 error_report("Unable to open device tree file '%s'",
101 filename_path);
pbrook7ec632b2009-04-10 16:23:59 +0000102 goto fail;
103 }
aurel32f652e6a2008-12-16 10:43:48 +0000104
pbrook7ec632b2009-04-10 16:23:59 +0000105 ret = fdt_open_into(fdt, fdt, dt_size);
aurel32f652e6a2008-12-16 10:43:48 +0000106 if (ret) {
Li Liudb013f82014-08-26 14:38:02 +0800107 error_report("Unable to copy device tree in memory");
aurel32f652e6a2008-12-16 10:43:48 +0000108 goto fail;
109 }
110
111 /* Check sanity of device tree */
112 if (fdt_check_header(fdt)) {
Li Liudb013f82014-08-26 14:38:02 +0800113 error_report("Device tree file loaded into memory is invalid: %s",
114 filename_path);
aurel32f652e6a2008-12-16 10:43:48 +0000115 goto fail;
116 }
pbrook7ec632b2009-04-10 16:23:59 +0000117 *sizep = dt_size;
aurel32f652e6a2008-12-16 10:43:48 +0000118 return fdt;
119
120fail:
Anthony Liguori7267c092011-08-20 22:09:37 -0500121 g_free(fdt);
aurel32f652e6a2008-12-16 10:43:48 +0000122 return NULL;
123}
124
Eric Auger60e43e92016-02-19 09:42:29 -0700125#ifdef CONFIG_LINUX
126
127#define SYSFS_DT_BASEDIR "/proc/device-tree"
128
129/**
130 * read_fstree: this function is inspired from dtc read_fstree
131 * @fdt: preallocated fdt blob buffer, to be populated
132 * @dirname: directory to scan under SYSFS_DT_BASEDIR
133 * the search is recursive and the tree is searched down to the
134 * leaves (property files).
135 *
136 * the function asserts in case of error
137 */
138static void read_fstree(void *fdt, const char *dirname)
139{
140 DIR *d;
141 struct dirent *de;
142 struct stat st;
143 const char *root_dir = SYSFS_DT_BASEDIR;
144 const char *parent_node;
145
146 if (strstr(dirname, root_dir) != dirname) {
Philippe Mathieu-Daudé38754e42018-06-29 15:11:00 +0100147 error_report("%s: %s must be searched within %s",
148 __func__, dirname, root_dir);
149 exit(1);
Eric Auger60e43e92016-02-19 09:42:29 -0700150 }
151 parent_node = &dirname[strlen(SYSFS_DT_BASEDIR)];
152
153 d = opendir(dirname);
154 if (!d) {
Philippe Mathieu-Daudé38754e42018-06-29 15:11:00 +0100155 error_report("%s cannot open %s", __func__, dirname);
156 exit(1);
Eric Auger60e43e92016-02-19 09:42:29 -0700157 }
158
159 while ((de = readdir(d)) != NULL) {
160 char *tmpnam;
161
162 if (!g_strcmp0(de->d_name, ".")
163 || !g_strcmp0(de->d_name, "..")) {
164 continue;
165 }
166
167 tmpnam = g_strdup_printf("%s/%s", dirname, de->d_name);
168
169 if (lstat(tmpnam, &st) < 0) {
Philippe Mathieu-Daudé38754e42018-06-29 15:11:00 +0100170 error_report("%s cannot lstat %s", __func__, tmpnam);
171 exit(1);
Eric Auger60e43e92016-02-19 09:42:29 -0700172 }
173
174 if (S_ISREG(st.st_mode)) {
175 gchar *val;
176 gsize len;
177
178 if (!g_file_get_contents(tmpnam, &val, &len, NULL)) {
Philippe Mathieu-Daudé38754e42018-06-29 15:11:00 +0100179 error_report("%s not able to extract info from %s",
180 __func__, tmpnam);
181 exit(1);
Eric Auger60e43e92016-02-19 09:42:29 -0700182 }
183
184 if (strlen(parent_node) > 0) {
185 qemu_fdt_setprop(fdt, parent_node,
186 de->d_name, val, len);
187 } else {
188 qemu_fdt_setprop(fdt, "/", de->d_name, val, len);
189 }
190 g_free(val);
191 } else if (S_ISDIR(st.st_mode)) {
192 char *node_name;
193
194 node_name = g_strdup_printf("%s/%s",
195 parent_node, de->d_name);
196 qemu_fdt_add_subnode(fdt, node_name);
197 g_free(node_name);
198 read_fstree(fdt, tmpnam);
199 }
200
201 g_free(tmpnam);
202 }
203
204 closedir(d);
205}
206
207/* load_device_tree_from_sysfs: extract the dt blob from host sysfs */
208void *load_device_tree_from_sysfs(void)
209{
210 void *host_fdt;
211 int host_fdt_size;
212
213 host_fdt = create_device_tree(&host_fdt_size);
214 read_fstree(host_fdt, SYSFS_DT_BASEDIR);
215 if (fdt_check_header(host_fdt)) {
Philippe Mathieu-Daudé38754e42018-06-29 15:11:00 +0100216 error_report("%s host device tree extracted into memory is invalid",
217 __func__);
218 exit(1);
Eric Auger60e43e92016-02-19 09:42:29 -0700219 }
220 return host_fdt;
221}
222
223#endif /* CONFIG_LINUX */
224
Alexander Grafccbcfed2011-07-23 10:52:00 +0200225static int findnode_nofail(void *fdt, const char *node_path)
aurel32f652e6a2008-12-16 10:43:48 +0000226{
227 int offset;
228
229 offset = fdt_path_offset(fdt, node_path);
Alexander Grafccbcfed2011-07-23 10:52:00 +0200230 if (offset < 0) {
Li Liu508e2212014-08-26 14:38:03 +0800231 error_report("%s Couldn't find node %s: %s", __func__, node_path,
232 fdt_strerror(offset));
Alexander Grafccbcfed2011-07-23 10:52:00 +0200233 exit(1);
234 }
aurel32f652e6a2008-12-16 10:43:48 +0000235
Alexander Grafccbcfed2011-07-23 10:52:00 +0200236 return offset;
237}
238
Eric Augerf963cc22018-06-29 15:11:01 +0100239char **qemu_fdt_node_unit_path(void *fdt, const char *name, Error **errp)
240{
241 char *prefix = g_strdup_printf("%s@", name);
242 unsigned int path_len = 16, n = 0;
243 GSList *path_list = NULL, *iter;
244 const char *iter_name;
245 int offset, len, ret;
246 char **path_array;
247
248 offset = fdt_next_node(fdt, -1, NULL);
249
250 while (offset >= 0) {
251 iter_name = fdt_get_name(fdt, offset, &len);
252 if (!iter_name) {
253 offset = len;
254 break;
255 }
256 if (!strcmp(iter_name, name) || g_str_has_prefix(iter_name, prefix)) {
257 char *path;
258
259 path = g_malloc(path_len);
260 while ((ret = fdt_get_path(fdt, offset, path, path_len))
261 == -FDT_ERR_NOSPACE) {
262 path_len += 16;
263 path = g_realloc(path, path_len);
264 }
265 path_list = g_slist_prepend(path_list, path);
266 n++;
267 }
268 offset = fdt_next_node(fdt, offset, NULL);
269 }
270 g_free(prefix);
271
272 if (offset < 0 && offset != -FDT_ERR_NOTFOUND) {
273 error_setg(errp, "%s: abort parsing dt for %s node units: %s",
274 __func__, name, fdt_strerror(offset));
275 for (iter = path_list; iter; iter = iter->next) {
276 g_free(iter->data);
277 }
278 g_slist_free(path_list);
279 return NULL;
280 }
281
282 path_array = g_new(char *, n + 1);
283 path_array[n--] = NULL;
284
285 for (iter = path_list; iter; iter = iter->next) {
286 path_array[n--] = iter->data;
287 }
288
289 g_slist_free(path_list);
290
291 return path_array;
292}
293
Edgar E. Iglesias958bae12020-04-23 14:11:12 +0200294char **qemu_fdt_node_path(void *fdt, const char *name, const char *compat,
Eric Auger6d795662016-02-19 09:42:30 -0700295 Error **errp)
296{
297 int offset, len, ret;
298 const char *iter_name;
299 unsigned int path_len = 16, n = 0;
300 GSList *path_list = NULL, *iter;
301 char **path_array;
302
303 offset = fdt_node_offset_by_compatible(fdt, -1, compat);
304
305 while (offset >= 0) {
306 iter_name = fdt_get_name(fdt, offset, &len);
307 if (!iter_name) {
308 offset = len;
309 break;
310 }
Edgar E. Iglesias80972d32020-04-23 14:11:11 +0200311 if (!name || !strcmp(iter_name, name)) {
Eric Auger6d795662016-02-19 09:42:30 -0700312 char *path;
313
314 path = g_malloc(path_len);
315 while ((ret = fdt_get_path(fdt, offset, path, path_len))
316 == -FDT_ERR_NOSPACE) {
317 path_len += 16;
318 path = g_realloc(path, path_len);
319 }
320 path_list = g_slist_prepend(path_list, path);
321 n++;
322 }
323 offset = fdt_node_offset_by_compatible(fdt, offset, compat);
324 }
325
326 if (offset < 0 && offset != -FDT_ERR_NOTFOUND) {
327 error_setg(errp, "%s: abort parsing dt for %s/%s: %s",
328 __func__, name, compat, fdt_strerror(offset));
329 for (iter = path_list; iter; iter = iter->next) {
330 g_free(iter->data);
331 }
332 g_slist_free(path_list);
333 return NULL;
334 }
335
336 path_array = g_new(char *, n + 1);
337 path_array[n--] = NULL;
338
339 for (iter = path_list; iter; iter = iter->next) {
340 path_array[n--] = iter->data;
341 }
342
343 g_slist_free(path_list);
344
345 return path_array;
346}
347
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000348int qemu_fdt_setprop(void *fdt, const char *node_path,
Peter Crosthwaitebe5907f2013-11-11 18:15:21 +1000349 const char *property, const void *val, int size)
Alexander Grafccbcfed2011-07-23 10:52:00 +0200350{
351 int r;
352
Peter Crosthwaitebe5907f2013-11-11 18:15:21 +1000353 r = fdt_setprop(fdt, findnode_nofail(fdt, node_path), property, val, size);
Alexander Grafccbcfed2011-07-23 10:52:00 +0200354 if (r < 0) {
Li Liu508e2212014-08-26 14:38:03 +0800355 error_report("%s: Couldn't set %s/%s: %s", __func__, node_path,
356 property, fdt_strerror(r));
Alexander Grafccbcfed2011-07-23 10:52:00 +0200357 exit(1);
358 }
359
360 return r;
aurel32f652e6a2008-12-16 10:43:48 +0000361}
362
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000363int qemu_fdt_setprop_cell(void *fdt, const char *node_path,
364 const char *property, uint32_t val)
aurel32f652e6a2008-12-16 10:43:48 +0000365{
Alexander Grafccbcfed2011-07-23 10:52:00 +0200366 int r;
aurel32f652e6a2008-12-16 10:43:48 +0000367
Alexander Grafccbcfed2011-07-23 10:52:00 +0200368 r = fdt_setprop_cell(fdt, findnode_nofail(fdt, node_path), property, val);
369 if (r < 0) {
Li Liu508e2212014-08-26 14:38:03 +0800370 error_report("%s: Couldn't set %s/%s = %#08x: %s", __func__,
371 node_path, property, val, fdt_strerror(r));
Alexander Grafccbcfed2011-07-23 10:52:00 +0200372 exit(1);
373 }
aurel32f652e6a2008-12-16 10:43:48 +0000374
Alexander Grafccbcfed2011-07-23 10:52:00 +0200375 return r;
aurel32f652e6a2008-12-16 10:43:48 +0000376}
377
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000378int qemu_fdt_setprop_u64(void *fdt, const char *node_path,
379 const char *property, uint64_t val)
Alexander Grafbb28eb32012-05-18 01:53:01 +0200380{
381 val = cpu_to_be64(val);
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000382 return qemu_fdt_setprop(fdt, node_path, property, &val, sizeof(val));
Alexander Grafbb28eb32012-05-18 01:53:01 +0200383}
384
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000385int qemu_fdt_setprop_string(void *fdt, const char *node_path,
386 const char *property, const char *string)
aurel32f652e6a2008-12-16 10:43:48 +0000387{
Alexander Grafccbcfed2011-07-23 10:52:00 +0200388 int r;
aurel32f652e6a2008-12-16 10:43:48 +0000389
Alexander Grafccbcfed2011-07-23 10:52:00 +0200390 r = fdt_setprop_string(fdt, findnode_nofail(fdt, node_path), property, string);
391 if (r < 0) {
Li Liu508e2212014-08-26 14:38:03 +0800392 error_report("%s: Couldn't set %s/%s = %s: %s", __func__,
393 node_path, property, string, fdt_strerror(r));
Alexander Grafccbcfed2011-07-23 10:52:00 +0200394 exit(1);
395 }
aurel32f652e6a2008-12-16 10:43:48 +0000396
Alexander Grafccbcfed2011-07-23 10:52:00 +0200397 return r;
aurel32f652e6a2008-12-16 10:43:48 +0000398}
Alexander Grafd69a8e62011-07-21 01:52:57 +0200399
Alex Bennée78da6a12021-03-03 17:36:38 +0000400/*
401 * libfdt doesn't allow us to add string arrays directly but they are
402 * test a series of null terminated strings with a length. We build
403 * the string up here so we can calculate the final length.
404 */
405int qemu_fdt_setprop_string_array(void *fdt, const char *node_path,
406 const char *prop, char **array, int len)
407{
408 int ret, i, total_len = 0;
409 char *str, *p;
410 for (i = 0; i < len; i++) {
411 total_len += strlen(array[i]) + 1;
412 }
413 p = str = g_malloc0(total_len);
414 for (i = 0; i < len; i++) {
415 int len = strlen(array[i]) + 1;
416 pstrcpy(p, len, array[i]);
417 p += len;
418 }
419
420 ret = qemu_fdt_setprop(fdt, node_path, prop, str, total_len);
421 g_free(str);
422 return ret;
423}
424
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000425const void *qemu_fdt_getprop(void *fdt, const char *node_path,
Eric Auger78e24f22016-02-19 09:42:30 -0700426 const char *property, int *lenp, Error **errp)
Peter Maydellf0aa7132012-07-20 13:34:50 +0100427{
428 int len;
429 const void *r;
Eric Auger78e24f22016-02-19 09:42:30 -0700430
Peter Maydellf0aa7132012-07-20 13:34:50 +0100431 if (!lenp) {
432 lenp = &len;
433 }
434 r = fdt_getprop(fdt, findnode_nofail(fdt, node_path), property, lenp);
435 if (!r) {
Eric Auger78e24f22016-02-19 09:42:30 -0700436 error_setg(errp, "%s: Couldn't get %s/%s: %s", __func__,
437 node_path, property, fdt_strerror(*lenp));
Peter Maydellf0aa7132012-07-20 13:34:50 +0100438 }
439 return r;
440}
441
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000442uint32_t qemu_fdt_getprop_cell(void *fdt, const char *node_path,
Eric Auger58e71092016-02-19 09:42:30 -0700443 const char *property, int *lenp, Error **errp)
Peter Maydellf0aa7132012-07-20 13:34:50 +0100444{
445 int len;
Eric Auger58e71092016-02-19 09:42:30 -0700446 const uint32_t *p;
447
448 if (!lenp) {
449 lenp = &len;
450 }
451 p = qemu_fdt_getprop(fdt, node_path, property, lenp, errp);
452 if (!p) {
453 return 0;
454 } else if (*lenp != 4) {
455 error_setg(errp, "%s: %s/%s not 4 bytes long (not a cell?)",
456 __func__, node_path, property);
457 *lenp = -EINVAL;
458 return 0;
Peter Maydellf0aa7132012-07-20 13:34:50 +0100459 }
460 return be32_to_cpu(*p);
461}
462
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000463uint32_t qemu_fdt_get_phandle(void *fdt, const char *path)
Alexander Graf7d5fd102012-05-17 15:23:39 +0200464{
465 uint32_t r;
466
467 r = fdt_get_phandle(fdt, findnode_nofail(fdt, path));
Stefan Weil909a1962013-06-10 22:12:25 +0200468 if (r == 0) {
Li Liu508e2212014-08-26 14:38:03 +0800469 error_report("%s: Couldn't get phandle for %s: %s", __func__,
470 path, fdt_strerror(r));
Alexander Graf7d5fd102012-05-17 15:23:39 +0200471 exit(1);
472 }
473
474 return r;
475}
476
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000477int qemu_fdt_setprop_phandle(void *fdt, const char *node_path,
478 const char *property,
479 const char *target_node_path)
Alexander Graf8535ab12012-05-17 14:11:52 +0200480{
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000481 uint32_t phandle = qemu_fdt_get_phandle(fdt, target_node_path);
482 return qemu_fdt_setprop_cell(fdt, node_path, property, phandle);
Alexander Graf8535ab12012-05-17 14:11:52 +0200483}
484
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000485uint32_t qemu_fdt_alloc_phandle(void *fdt)
Alexander Graf3601b572012-05-17 16:58:55 +0200486{
Alexander Graf4b1b1c82012-06-06 01:01:23 +0200487 static int phandle = 0x0;
488
489 /*
490 * We need to find out if the user gave us special instruction at
Kamalesh Babulalcc47a162015-07-24 13:48:13 +0530491 * which phandle id to start allocating phandles.
Alexander Graf4b1b1c82012-06-06 01:01:23 +0200492 */
493 if (!phandle) {
Marcel Apfelbaum6cabe7f2015-02-04 17:43:53 +0200494 phandle = machine_phandle_start(current_machine);
Alexander Graf4b1b1c82012-06-06 01:01:23 +0200495 }
496
497 if (!phandle) {
498 /*
499 * None or invalid phandle given on the command line, so fall back to
500 * default starting point.
501 */
502 phandle = 0x8000;
503 }
Alexander Graf3601b572012-05-17 16:58:55 +0200504
505 return phandle++;
506}
507
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000508int qemu_fdt_nop_node(void *fdt, const char *node_path)
Alexander Grafd69a8e62011-07-21 01:52:57 +0200509{
Alexander Grafccbcfed2011-07-23 10:52:00 +0200510 int r;
Alexander Grafd69a8e62011-07-21 01:52:57 +0200511
Alexander Grafccbcfed2011-07-23 10:52:00 +0200512 r = fdt_nop_node(fdt, findnode_nofail(fdt, node_path));
513 if (r < 0) {
Li Liu508e2212014-08-26 14:38:03 +0800514 error_report("%s: Couldn't nop node %s: %s", __func__, node_path,
515 fdt_strerror(r));
Alexander Grafccbcfed2011-07-23 10:52:00 +0200516 exit(1);
517 }
Alexander Grafd69a8e62011-07-21 01:52:57 +0200518
Alexander Grafccbcfed2011-07-23 10:52:00 +0200519 return r;
Alexander Grafd69a8e62011-07-21 01:52:57 +0200520}
Alexander Graf80ad7812011-07-22 13:55:37 +0200521
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000522int qemu_fdt_add_subnode(void *fdt, const char *name)
Alexander Graf80ad7812011-07-22 13:55:37 +0200523{
Alexander Graf80ad7812011-07-22 13:55:37 +0200524 char *dupname = g_strdup(name);
525 char *basename = strrchr(dupname, '/');
526 int retval;
Alexander Grafc640d082012-05-17 11:40:42 +0200527 int parent = 0;
Alexander Graf80ad7812011-07-22 13:55:37 +0200528
529 if (!basename) {
Stefan Weilbff39b62011-10-17 22:12:09 +0200530 g_free(dupname);
Alexander Graf80ad7812011-07-22 13:55:37 +0200531 return -1;
532 }
533
534 basename[0] = '\0';
535 basename++;
536
Alexander Grafc640d082012-05-17 11:40:42 +0200537 if (dupname[0]) {
538 parent = findnode_nofail(fdt, dupname);
539 }
540
541 retval = fdt_add_subnode(fdt, parent, basename);
Alexander Grafccbcfed2011-07-23 10:52:00 +0200542 if (retval < 0) {
Yanan Wangb863f0b2021-10-20 22:21:19 +0800543 error_report("%s: Failed to create subnode %s: %s",
544 __func__, name, fdt_strerror(retval));
Alexander Grafccbcfed2011-07-23 10:52:00 +0200545 exit(1);
Alexander Graf80ad7812011-07-22 13:55:37 +0200546 }
547
Alexander Graf80ad7812011-07-22 13:55:37 +0200548 g_free(dupname);
549 return retval;
550}
Alexander Graf71193432012-09-23 08:37:59 +0200551
Yanan Wangb863f0b2021-10-20 22:21:19 +0800552/*
553 * qemu_fdt_add_path: Like qemu_fdt_add_subnode(), but will add
554 * all missing subnodes from the given path.
555 */
556int qemu_fdt_add_path(void *fdt, const char *path)
557{
558 const char *name;
559 const char *p = path;
560 int namelen, retval;
561 int parent = 0;
562
563 if (path[0] != '/') {
564 return -1;
565 }
566
567 while (p) {
568 name = p + 1;
569 p = strchr(name, '/');
570 namelen = p != NULL ? p - name : strlen(name);
571
572 retval = fdt_subnode_offset_namelen(fdt, parent, name, namelen);
573 if (retval < 0 && retval != -FDT_ERR_NOTFOUND) {
574 error_report("%s: Unexpected error in finding subnode %.*s: %s",
575 __func__, namelen, name, fdt_strerror(retval));
576 exit(1);
577 } else if (retval == -FDT_ERR_NOTFOUND) {
578 retval = fdt_add_subnode_namelen(fdt, parent, name, namelen);
579 if (retval < 0) {
580 error_report("%s: Failed to create subnode %.*s: %s",
581 __func__, namelen, name, fdt_strerror(retval));
582 exit(1);
583 }
584 }
585
586 parent = retval;
587 }
588
589 return retval;
590}
591
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000592void qemu_fdt_dumpdtb(void *fdt, int size)
Alexander Graf71193432012-09-23 08:37:59 +0200593{
Paolo Bonzinif2ce39b2020-11-02 09:44:36 -0500594 const char *dumpdtb = current_machine->dumpdtb;
Alexander Graf71193432012-09-23 08:37:59 +0200595
Markus Armbruster2ff3de62013-07-04 15:09:22 +0200596 if (dumpdtb) {
597 /* Dump the dtb to a file and quit */
Leonardo Bras9f252c72020-03-19 01:03:26 -0300598 if (g_file_set_contents(dumpdtb, fdt, size, NULL)) {
599 info_report("dtb dumped to %s. Exiting.", dumpdtb);
600 exit(0);
601 }
602 error_report("%s: Failed dumping dtb to %s", __func__, dumpdtb);
603 exit(1);
Alexander Graf71193432012-09-23 08:37:59 +0200604 }
Alexander Graf71193432012-09-23 08:37:59 +0200605}
Peter Maydell97c38f82013-07-16 13:25:05 +0100606
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000607int qemu_fdt_setprop_sized_cells_from_array(void *fdt,
608 const char *node_path,
609 const char *property,
610 int numvalues,
611 uint64_t *values)
Peter Maydell97c38f82013-07-16 13:25:05 +0100612{
613 uint32_t *propcells;
614 uint64_t value;
615 int cellnum, vnum, ncells;
616 uint32_t hival;
Sergey Fedorov2bf9feb2014-12-11 18:45:05 +0300617 int ret;
Peter Maydell97c38f82013-07-16 13:25:05 +0100618
619 propcells = g_new0(uint32_t, numvalues * 2);
620
621 cellnum = 0;
622 for (vnum = 0; vnum < numvalues; vnum++) {
623 ncells = values[vnum * 2];
624 if (ncells != 1 && ncells != 2) {
Sergey Fedorov2bf9feb2014-12-11 18:45:05 +0300625 ret = -1;
626 goto out;
Peter Maydell97c38f82013-07-16 13:25:05 +0100627 }
628 value = values[vnum * 2 + 1];
629 hival = cpu_to_be32(value >> 32);
630 if (ncells > 1) {
631 propcells[cellnum++] = hival;
632 } else if (hival != 0) {
Sergey Fedorov2bf9feb2014-12-11 18:45:05 +0300633 ret = -1;
634 goto out;
Peter Maydell97c38f82013-07-16 13:25:05 +0100635 }
636 propcells[cellnum++] = cpu_to_be32(value);
637 }
638
Sergey Fedorov2bf9feb2014-12-11 18:45:05 +0300639 ret = qemu_fdt_setprop(fdt, node_path, property, propcells,
640 cellnum * sizeof(uint32_t));
641out:
642 g_free(propcells);
643 return ret;
Peter Maydell97c38f82013-07-16 13:25:05 +0100644}