blob: 2e38259d34f8f6d3ac74f239a45b80fcf17d0829 [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"
Jason A. Donenfelde1e618b2022-10-25 02:43:18 +020025#include "qemu/guest-random.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010026#include "sysemu/device_tree.h"
Blue Swirl39b7f202009-09-22 17:51:36 +000027#include "hw/loader.h"
Marcel Apfelbaum6cabe7f2015-02-04 17:43:53 +020028#include "hw/boards.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010029#include "qemu/config-file.h"
Daniel Henrique Barbozabf353ad2022-09-26 14:38:40 -030030#include "qapi/qapi-commands-machine.h"
31#include "qapi/qmp/qdict.h"
32#include "monitor/hmp.h"
aurel32f652e6a2008-12-16 10:43:48 +000033
34#include <libfdt.h>
35
Geert Uytterhoeven14ec3cb2018-04-26 11:04:38 +010036#define FDT_MAX_SIZE 0x100000
Alexander Grafce362522012-05-17 15:33:54 +020037
38void *create_device_tree(int *sizep)
39{
40 void *fdt;
41 int ret;
42
43 *sizep = FDT_MAX_SIZE;
44 fdt = g_malloc0(FDT_MAX_SIZE);
45 ret = fdt_create(fdt, FDT_MAX_SIZE);
46 if (ret < 0) {
47 goto fail;
48 }
Peter Maydellef6de702013-11-22 17:17:09 +000049 ret = fdt_finish_reservemap(fdt);
50 if (ret < 0) {
51 goto fail;
52 }
Alexander Grafce362522012-05-17 15:33:54 +020053 ret = fdt_begin_node(fdt, "");
54 if (ret < 0) {
55 goto fail;
56 }
57 ret = fdt_end_node(fdt);
58 if (ret < 0) {
59 goto fail;
60 }
61 ret = fdt_finish(fdt);
62 if (ret < 0) {
63 goto fail;
64 }
65 ret = fdt_open_into(fdt, fdt, *sizep);
66 if (ret) {
Bernhard Beschowd4fae972022-01-16 12:46:49 +010067 error_report("%s: Unable to copy device tree into memory: %s",
68 __func__, fdt_strerror(ret));
Alexander Grafce362522012-05-17 15:33:54 +020069 exit(1);
70 }
71
72 return fdt;
73fail:
Li Liu508e2212014-08-26 14:38:03 +080074 error_report("%s Couldn't create dt: %s", __func__, fdt_strerror(ret));
Alexander Grafce362522012-05-17 15:33:54 +020075 exit(1);
76}
77
pbrook7ec632b2009-04-10 16:23:59 +000078void *load_device_tree(const char *filename_path, int *sizep)
aurel32f652e6a2008-12-16 10:43:48 +000079{
pbrook7ec632b2009-04-10 16:23:59 +000080 int dt_size;
aurel32f652e6a2008-12-16 10:43:48 +000081 int dt_file_load_size;
aurel32f652e6a2008-12-16 10:43:48 +000082 int ret;
pbrook7ec632b2009-04-10 16:23:59 +000083 void *fdt = NULL;
aurel32f652e6a2008-12-16 10:43:48 +000084
pbrook7ec632b2009-04-10 16:23:59 +000085 *sizep = 0;
86 dt_size = get_image_size(filename_path);
87 if (dt_size < 0) {
Li Liudb013f82014-08-26 14:38:02 +080088 error_report("Unable to get size of device tree file '%s'",
89 filename_path);
aurel32f652e6a2008-12-16 10:43:48 +000090 goto fail;
91 }
Markus Armbruster065e6292019-04-09 19:40:18 +020092 if (dt_size > INT_MAX / 2 - 10000) {
93 error_report("Device tree file '%s' is too large", filename_path);
94 goto fail;
95 }
aurel32f652e6a2008-12-16 10:43:48 +000096
pbrook7ec632b2009-04-10 16:23:59 +000097 /* Expand to 2x size to give enough room for manipulation. */
Alexander Grafded57c52011-07-23 10:54:11 +020098 dt_size += 10000;
pbrook7ec632b2009-04-10 16:23:59 +000099 dt_size *= 2;
aurel32f652e6a2008-12-16 10:43:48 +0000100 /* First allocate space in qemu for device tree */
Anthony Liguori7267c092011-08-20 22:09:37 -0500101 fdt = g_malloc0(dt_size);
aurel32f652e6a2008-12-16 10:43:48 +0000102
Peter Maydellda885fe2018-12-14 13:30:52 +0000103 dt_file_load_size = load_image_size(filename_path, fdt, dt_size);
pbrook7ec632b2009-04-10 16:23:59 +0000104 if (dt_file_load_size < 0) {
Li Liudb013f82014-08-26 14:38:02 +0800105 error_report("Unable to open device tree file '%s'",
106 filename_path);
pbrook7ec632b2009-04-10 16:23:59 +0000107 goto fail;
108 }
aurel32f652e6a2008-12-16 10:43:48 +0000109
pbrook7ec632b2009-04-10 16:23:59 +0000110 ret = fdt_open_into(fdt, fdt, dt_size);
aurel32f652e6a2008-12-16 10:43:48 +0000111 if (ret) {
Bernhard Beschowd4fae972022-01-16 12:46:49 +0100112 error_report("%s: Unable to copy device tree into memory: %s",
113 __func__, fdt_strerror(ret));
aurel32f652e6a2008-12-16 10:43:48 +0000114 goto fail;
115 }
116
117 /* Check sanity of device tree */
118 if (fdt_check_header(fdt)) {
Li Liudb013f82014-08-26 14:38:02 +0800119 error_report("Device tree file loaded into memory is invalid: %s",
120 filename_path);
aurel32f652e6a2008-12-16 10:43:48 +0000121 goto fail;
122 }
pbrook7ec632b2009-04-10 16:23:59 +0000123 *sizep = dt_size;
aurel32f652e6a2008-12-16 10:43:48 +0000124 return fdt;
125
126fail:
Anthony Liguori7267c092011-08-20 22:09:37 -0500127 g_free(fdt);
aurel32f652e6a2008-12-16 10:43:48 +0000128 return NULL;
129}
130
Eric Auger60e43e92016-02-19 09:42:29 -0700131#ifdef CONFIG_LINUX
132
133#define SYSFS_DT_BASEDIR "/proc/device-tree"
134
135/**
136 * read_fstree: this function is inspired from dtc read_fstree
137 * @fdt: preallocated fdt blob buffer, to be populated
138 * @dirname: directory to scan under SYSFS_DT_BASEDIR
139 * the search is recursive and the tree is searched down to the
140 * leaves (property files).
141 *
142 * the function asserts in case of error
143 */
144static void read_fstree(void *fdt, const char *dirname)
145{
146 DIR *d;
147 struct dirent *de;
148 struct stat st;
149 const char *root_dir = SYSFS_DT_BASEDIR;
150 const char *parent_node;
151
152 if (strstr(dirname, root_dir) != dirname) {
Philippe Mathieu-Daudé38754e42018-06-29 15:11:00 +0100153 error_report("%s: %s must be searched within %s",
154 __func__, dirname, root_dir);
155 exit(1);
Eric Auger60e43e92016-02-19 09:42:29 -0700156 }
157 parent_node = &dirname[strlen(SYSFS_DT_BASEDIR)];
158
159 d = opendir(dirname);
160 if (!d) {
Philippe Mathieu-Daudé38754e42018-06-29 15:11:00 +0100161 error_report("%s cannot open %s", __func__, dirname);
162 exit(1);
Eric Auger60e43e92016-02-19 09:42:29 -0700163 }
164
165 while ((de = readdir(d)) != NULL) {
166 char *tmpnam;
167
168 if (!g_strcmp0(de->d_name, ".")
169 || !g_strcmp0(de->d_name, "..")) {
170 continue;
171 }
172
173 tmpnam = g_strdup_printf("%s/%s", dirname, de->d_name);
174
175 if (lstat(tmpnam, &st) < 0) {
Philippe Mathieu-Daudé38754e42018-06-29 15:11:00 +0100176 error_report("%s cannot lstat %s", __func__, tmpnam);
177 exit(1);
Eric Auger60e43e92016-02-19 09:42:29 -0700178 }
179
180 if (S_ISREG(st.st_mode)) {
181 gchar *val;
182 gsize len;
183
184 if (!g_file_get_contents(tmpnam, &val, &len, NULL)) {
Philippe Mathieu-Daudé38754e42018-06-29 15:11:00 +0100185 error_report("%s not able to extract info from %s",
186 __func__, tmpnam);
187 exit(1);
Eric Auger60e43e92016-02-19 09:42:29 -0700188 }
189
190 if (strlen(parent_node) > 0) {
191 qemu_fdt_setprop(fdt, parent_node,
192 de->d_name, val, len);
193 } else {
194 qemu_fdt_setprop(fdt, "/", de->d_name, val, len);
195 }
196 g_free(val);
197 } else if (S_ISDIR(st.st_mode)) {
198 char *node_name;
199
200 node_name = g_strdup_printf("%s/%s",
201 parent_node, de->d_name);
202 qemu_fdt_add_subnode(fdt, node_name);
203 g_free(node_name);
204 read_fstree(fdt, tmpnam);
205 }
206
207 g_free(tmpnam);
208 }
209
210 closedir(d);
211}
212
213/* load_device_tree_from_sysfs: extract the dt blob from host sysfs */
214void *load_device_tree_from_sysfs(void)
215{
216 void *host_fdt;
217 int host_fdt_size;
218
219 host_fdt = create_device_tree(&host_fdt_size);
220 read_fstree(host_fdt, SYSFS_DT_BASEDIR);
221 if (fdt_check_header(host_fdt)) {
Philippe Mathieu-Daudé38754e42018-06-29 15:11:00 +0100222 error_report("%s host device tree extracted into memory is invalid",
223 __func__);
224 exit(1);
Eric Auger60e43e92016-02-19 09:42:29 -0700225 }
226 return host_fdt;
227}
228
229#endif /* CONFIG_LINUX */
230
Alexander Grafccbcfed2011-07-23 10:52:00 +0200231static int findnode_nofail(void *fdt, const char *node_path)
aurel32f652e6a2008-12-16 10:43:48 +0000232{
233 int offset;
234
235 offset = fdt_path_offset(fdt, node_path);
Alexander Grafccbcfed2011-07-23 10:52:00 +0200236 if (offset < 0) {
Li Liu508e2212014-08-26 14:38:03 +0800237 error_report("%s Couldn't find node %s: %s", __func__, node_path,
238 fdt_strerror(offset));
Alexander Grafccbcfed2011-07-23 10:52:00 +0200239 exit(1);
240 }
aurel32f652e6a2008-12-16 10:43:48 +0000241
Alexander Grafccbcfed2011-07-23 10:52:00 +0200242 return offset;
243}
244
Eric Augerf963cc22018-06-29 15:11:01 +0100245char **qemu_fdt_node_unit_path(void *fdt, const char *name, Error **errp)
246{
247 char *prefix = g_strdup_printf("%s@", name);
248 unsigned int path_len = 16, n = 0;
249 GSList *path_list = NULL, *iter;
250 const char *iter_name;
251 int offset, len, ret;
252 char **path_array;
253
254 offset = fdt_next_node(fdt, -1, NULL);
255
256 while (offset >= 0) {
257 iter_name = fdt_get_name(fdt, offset, &len);
258 if (!iter_name) {
259 offset = len;
260 break;
261 }
262 if (!strcmp(iter_name, name) || g_str_has_prefix(iter_name, prefix)) {
263 char *path;
264
265 path = g_malloc(path_len);
266 while ((ret = fdt_get_path(fdt, offset, path, path_len))
267 == -FDT_ERR_NOSPACE) {
268 path_len += 16;
269 path = g_realloc(path, path_len);
270 }
271 path_list = g_slist_prepend(path_list, path);
272 n++;
273 }
274 offset = fdt_next_node(fdt, offset, NULL);
275 }
276 g_free(prefix);
277
278 if (offset < 0 && offset != -FDT_ERR_NOTFOUND) {
279 error_setg(errp, "%s: abort parsing dt for %s node units: %s",
280 __func__, name, fdt_strerror(offset));
281 for (iter = path_list; iter; iter = iter->next) {
282 g_free(iter->data);
283 }
284 g_slist_free(path_list);
285 return NULL;
286 }
287
288 path_array = g_new(char *, n + 1);
289 path_array[n--] = NULL;
290
291 for (iter = path_list; iter; iter = iter->next) {
292 path_array[n--] = iter->data;
293 }
294
295 g_slist_free(path_list);
296
297 return path_array;
298}
299
Edgar E. Iglesias958bae12020-04-23 14:11:12 +0200300char **qemu_fdt_node_path(void *fdt, const char *name, const char *compat,
Eric Auger6d795662016-02-19 09:42:30 -0700301 Error **errp)
302{
303 int offset, len, ret;
304 const char *iter_name;
305 unsigned int path_len = 16, n = 0;
306 GSList *path_list = NULL, *iter;
307 char **path_array;
308
309 offset = fdt_node_offset_by_compatible(fdt, -1, compat);
310
311 while (offset >= 0) {
312 iter_name = fdt_get_name(fdt, offset, &len);
313 if (!iter_name) {
314 offset = len;
315 break;
316 }
Edgar E. Iglesias80972d32020-04-23 14:11:11 +0200317 if (!name || !strcmp(iter_name, name)) {
Eric Auger6d795662016-02-19 09:42:30 -0700318 char *path;
319
320 path = g_malloc(path_len);
321 while ((ret = fdt_get_path(fdt, offset, path, path_len))
322 == -FDT_ERR_NOSPACE) {
323 path_len += 16;
324 path = g_realloc(path, path_len);
325 }
326 path_list = g_slist_prepend(path_list, path);
327 n++;
328 }
329 offset = fdt_node_offset_by_compatible(fdt, offset, compat);
330 }
331
332 if (offset < 0 && offset != -FDT_ERR_NOTFOUND) {
333 error_setg(errp, "%s: abort parsing dt for %s/%s: %s",
334 __func__, name, compat, fdt_strerror(offset));
335 for (iter = path_list; iter; iter = iter->next) {
336 g_free(iter->data);
337 }
338 g_slist_free(path_list);
339 return NULL;
340 }
341
342 path_array = g_new(char *, n + 1);
343 path_array[n--] = NULL;
344
345 for (iter = path_list; iter; iter = iter->next) {
346 path_array[n--] = iter->data;
347 }
348
349 g_slist_free(path_list);
350
351 return path_array;
352}
353
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000354int qemu_fdt_setprop(void *fdt, const char *node_path,
Peter Crosthwaitebe5907f2013-11-11 18:15:21 +1000355 const char *property, const void *val, int size)
Alexander Grafccbcfed2011-07-23 10:52:00 +0200356{
357 int r;
358
Peter Crosthwaitebe5907f2013-11-11 18:15:21 +1000359 r = fdt_setprop(fdt, findnode_nofail(fdt, node_path), property, val, size);
Alexander Grafccbcfed2011-07-23 10:52:00 +0200360 if (r < 0) {
Li Liu508e2212014-08-26 14:38:03 +0800361 error_report("%s: Couldn't set %s/%s: %s", __func__, node_path,
362 property, fdt_strerror(r));
Alexander Grafccbcfed2011-07-23 10:52:00 +0200363 exit(1);
364 }
365
366 return r;
aurel32f652e6a2008-12-16 10:43:48 +0000367}
368
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000369int qemu_fdt_setprop_cell(void *fdt, const char *node_path,
370 const char *property, uint32_t val)
aurel32f652e6a2008-12-16 10:43:48 +0000371{
Alexander Grafccbcfed2011-07-23 10:52:00 +0200372 int r;
aurel32f652e6a2008-12-16 10:43:48 +0000373
Alexander Grafccbcfed2011-07-23 10:52:00 +0200374 r = fdt_setprop_cell(fdt, findnode_nofail(fdt, node_path), property, val);
375 if (r < 0) {
Li Liu508e2212014-08-26 14:38:03 +0800376 error_report("%s: Couldn't set %s/%s = %#08x: %s", __func__,
377 node_path, property, val, fdt_strerror(r));
Alexander Grafccbcfed2011-07-23 10:52:00 +0200378 exit(1);
379 }
aurel32f652e6a2008-12-16 10:43:48 +0000380
Alexander Grafccbcfed2011-07-23 10:52:00 +0200381 return r;
aurel32f652e6a2008-12-16 10:43:48 +0000382}
383
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000384int qemu_fdt_setprop_u64(void *fdt, const char *node_path,
385 const char *property, uint64_t val)
Alexander Grafbb28eb32012-05-18 01:53:01 +0200386{
387 val = cpu_to_be64(val);
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000388 return qemu_fdt_setprop(fdt, node_path, property, &val, sizeof(val));
Alexander Grafbb28eb32012-05-18 01:53:01 +0200389}
390
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000391int qemu_fdt_setprop_string(void *fdt, const char *node_path,
392 const char *property, const char *string)
aurel32f652e6a2008-12-16 10:43:48 +0000393{
Alexander Grafccbcfed2011-07-23 10:52:00 +0200394 int r;
aurel32f652e6a2008-12-16 10:43:48 +0000395
Alexander Grafccbcfed2011-07-23 10:52:00 +0200396 r = fdt_setprop_string(fdt, findnode_nofail(fdt, node_path), property, string);
397 if (r < 0) {
Li Liu508e2212014-08-26 14:38:03 +0800398 error_report("%s: Couldn't set %s/%s = %s: %s", __func__,
399 node_path, property, string, fdt_strerror(r));
Alexander Grafccbcfed2011-07-23 10:52:00 +0200400 exit(1);
401 }
aurel32f652e6a2008-12-16 10:43:48 +0000402
Alexander Grafccbcfed2011-07-23 10:52:00 +0200403 return r;
aurel32f652e6a2008-12-16 10:43:48 +0000404}
Alexander Grafd69a8e62011-07-21 01:52:57 +0200405
Alex Bennée78da6a12021-03-03 17:36:38 +0000406/*
407 * libfdt doesn't allow us to add string arrays directly but they are
408 * test a series of null terminated strings with a length. We build
409 * the string up here so we can calculate the final length.
410 */
411int qemu_fdt_setprop_string_array(void *fdt, const char *node_path,
412 const char *prop, char **array, int len)
413{
414 int ret, i, total_len = 0;
415 char *str, *p;
416 for (i = 0; i < len; i++) {
417 total_len += strlen(array[i]) + 1;
418 }
419 p = str = g_malloc0(total_len);
420 for (i = 0; i < len; i++) {
Alistair Francis5567fa82023-09-25 14:30:23 +1000421 int offset = strlen(array[i]) + 1;
422 pstrcpy(p, offset, array[i]);
423 p += offset;
Alex Bennée78da6a12021-03-03 17:36:38 +0000424 }
425
426 ret = qemu_fdt_setprop(fdt, node_path, prop, str, total_len);
427 g_free(str);
428 return ret;
429}
430
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000431const void *qemu_fdt_getprop(void *fdt, const char *node_path,
Eric Auger78e24f22016-02-19 09:42:30 -0700432 const char *property, int *lenp, Error **errp)
Peter Maydellf0aa7132012-07-20 13:34:50 +0100433{
434 int len;
435 const void *r;
Eric Auger78e24f22016-02-19 09:42:30 -0700436
Peter Maydellf0aa7132012-07-20 13:34:50 +0100437 if (!lenp) {
438 lenp = &len;
439 }
440 r = fdt_getprop(fdt, findnode_nofail(fdt, node_path), property, lenp);
441 if (!r) {
Eric Auger78e24f22016-02-19 09:42:30 -0700442 error_setg(errp, "%s: Couldn't get %s/%s: %s", __func__,
443 node_path, property, fdt_strerror(*lenp));
Peter Maydellf0aa7132012-07-20 13:34:50 +0100444 }
445 return r;
446}
447
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000448uint32_t qemu_fdt_getprop_cell(void *fdt, const char *node_path,
Eric Auger58e71092016-02-19 09:42:30 -0700449 const char *property, int *lenp, Error **errp)
Peter Maydellf0aa7132012-07-20 13:34:50 +0100450{
451 int len;
Eric Auger58e71092016-02-19 09:42:30 -0700452 const uint32_t *p;
453
454 if (!lenp) {
455 lenp = &len;
456 }
457 p = qemu_fdt_getprop(fdt, node_path, property, lenp, errp);
458 if (!p) {
459 return 0;
460 } else if (*lenp != 4) {
461 error_setg(errp, "%s: %s/%s not 4 bytes long (not a cell?)",
462 __func__, node_path, property);
463 *lenp = -EINVAL;
464 return 0;
Peter Maydellf0aa7132012-07-20 13:34:50 +0100465 }
466 return be32_to_cpu(*p);
467}
468
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000469uint32_t qemu_fdt_get_phandle(void *fdt, const char *path)
Alexander Graf7d5fd102012-05-17 15:23:39 +0200470{
471 uint32_t r;
472
473 r = fdt_get_phandle(fdt, findnode_nofail(fdt, path));
Stefan Weil909a1962013-06-10 22:12:25 +0200474 if (r == 0) {
Li Liu508e2212014-08-26 14:38:03 +0800475 error_report("%s: Couldn't get phandle for %s: %s", __func__,
476 path, fdt_strerror(r));
Alexander Graf7d5fd102012-05-17 15:23:39 +0200477 exit(1);
478 }
479
480 return r;
481}
482
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000483int qemu_fdt_setprop_phandle(void *fdt, const char *node_path,
484 const char *property,
485 const char *target_node_path)
Alexander Graf8535ab12012-05-17 14:11:52 +0200486{
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000487 uint32_t phandle = qemu_fdt_get_phandle(fdt, target_node_path);
488 return qemu_fdt_setprop_cell(fdt, node_path, property, phandle);
Alexander Graf8535ab12012-05-17 14:11:52 +0200489}
490
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000491uint32_t qemu_fdt_alloc_phandle(void *fdt)
Alexander Graf3601b572012-05-17 16:58:55 +0200492{
Alexander Graf4b1b1c82012-06-06 01:01:23 +0200493 static int phandle = 0x0;
494
495 /*
496 * We need to find out if the user gave us special instruction at
Kamalesh Babulalcc47a162015-07-24 13:48:13 +0530497 * which phandle id to start allocating phandles.
Alexander Graf4b1b1c82012-06-06 01:01:23 +0200498 */
499 if (!phandle) {
Marcel Apfelbaum6cabe7f2015-02-04 17:43:53 +0200500 phandle = machine_phandle_start(current_machine);
Alexander Graf4b1b1c82012-06-06 01:01:23 +0200501 }
502
503 if (!phandle) {
504 /*
505 * None or invalid phandle given on the command line, so fall back to
506 * default starting point.
507 */
508 phandle = 0x8000;
509 }
Alexander Graf3601b572012-05-17 16:58:55 +0200510
511 return phandle++;
512}
513
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000514int qemu_fdt_nop_node(void *fdt, const char *node_path)
Alexander Grafd69a8e62011-07-21 01:52:57 +0200515{
Alexander Grafccbcfed2011-07-23 10:52:00 +0200516 int r;
Alexander Grafd69a8e62011-07-21 01:52:57 +0200517
Alexander Grafccbcfed2011-07-23 10:52:00 +0200518 r = fdt_nop_node(fdt, findnode_nofail(fdt, node_path));
519 if (r < 0) {
Li Liu508e2212014-08-26 14:38:03 +0800520 error_report("%s: Couldn't nop node %s: %s", __func__, node_path,
521 fdt_strerror(r));
Alexander Grafccbcfed2011-07-23 10:52:00 +0200522 exit(1);
523 }
Alexander Grafd69a8e62011-07-21 01:52:57 +0200524
Alexander Grafccbcfed2011-07-23 10:52:00 +0200525 return r;
Alexander Grafd69a8e62011-07-21 01:52:57 +0200526}
Alexander Graf80ad7812011-07-22 13:55:37 +0200527
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000528int qemu_fdt_add_subnode(void *fdt, const char *name)
Alexander Graf80ad7812011-07-22 13:55:37 +0200529{
Alexander Graf80ad7812011-07-22 13:55:37 +0200530 char *dupname = g_strdup(name);
531 char *basename = strrchr(dupname, '/');
532 int retval;
Alexander Grafc640d082012-05-17 11:40:42 +0200533 int parent = 0;
Alexander Graf80ad7812011-07-22 13:55:37 +0200534
535 if (!basename) {
Stefan Weilbff39b62011-10-17 22:12:09 +0200536 g_free(dupname);
Alexander Graf80ad7812011-07-22 13:55:37 +0200537 return -1;
538 }
539
540 basename[0] = '\0';
541 basename++;
542
Alexander Grafc640d082012-05-17 11:40:42 +0200543 if (dupname[0]) {
544 parent = findnode_nofail(fdt, dupname);
545 }
546
547 retval = fdt_add_subnode(fdt, parent, basename);
Alexander Grafccbcfed2011-07-23 10:52:00 +0200548 if (retval < 0) {
Yanan Wangb863f0b2021-10-20 22:21:19 +0800549 error_report("%s: Failed to create subnode %s: %s",
550 __func__, name, fdt_strerror(retval));
Alexander Grafccbcfed2011-07-23 10:52:00 +0200551 exit(1);
Alexander Graf80ad7812011-07-22 13:55:37 +0200552 }
553
Alexander Graf80ad7812011-07-22 13:55:37 +0200554 g_free(dupname);
555 return retval;
556}
Alexander Graf71193432012-09-23 08:37:59 +0200557
Yanan Wangb863f0b2021-10-20 22:21:19 +0800558/*
559 * qemu_fdt_add_path: Like qemu_fdt_add_subnode(), but will add
560 * all missing subnodes from the given path.
561 */
562int qemu_fdt_add_path(void *fdt, const char *path)
563{
564 const char *name;
Yanan Wangb863f0b2021-10-20 22:21:19 +0800565 int namelen, retval;
566 int parent = 0;
567
568 if (path[0] != '/') {
569 return -1;
570 }
571
Thomas Huthcfeeeb42022-01-07 14:38:44 +0100572 do {
Yanan Wang22599b72022-01-11 11:27:58 +0800573 name = path + 1;
574 path = strchr(name, '/');
575 namelen = path != NULL ? path - name : strlen(name);
Yanan Wangb863f0b2021-10-20 22:21:19 +0800576
577 retval = fdt_subnode_offset_namelen(fdt, parent, name, namelen);
578 if (retval < 0 && retval != -FDT_ERR_NOTFOUND) {
579 error_report("%s: Unexpected error in finding subnode %.*s: %s",
580 __func__, namelen, name, fdt_strerror(retval));
581 exit(1);
582 } else if (retval == -FDT_ERR_NOTFOUND) {
583 retval = fdt_add_subnode_namelen(fdt, parent, name, namelen);
584 if (retval < 0) {
585 error_report("%s: Failed to create subnode %.*s: %s",
586 __func__, namelen, name, fdt_strerror(retval));
587 exit(1);
588 }
589 }
590
591 parent = retval;
Yanan Wang22599b72022-01-11 11:27:58 +0800592 } while (path);
Yanan Wangb863f0b2021-10-20 22:21:19 +0800593
594 return retval;
595}
596
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000597void qemu_fdt_dumpdtb(void *fdt, int size)
Alexander Graf71193432012-09-23 08:37:59 +0200598{
Paolo Bonzinif2ce39b2020-11-02 09:44:36 -0500599 const char *dumpdtb = current_machine->dumpdtb;
Alexander Graf71193432012-09-23 08:37:59 +0200600
Markus Armbruster2ff3de62013-07-04 15:09:22 +0200601 if (dumpdtb) {
602 /* Dump the dtb to a file and quit */
Leonardo Bras9f252c72020-03-19 01:03:26 -0300603 if (g_file_set_contents(dumpdtb, fdt, size, NULL)) {
604 info_report("dtb dumped to %s. Exiting.", dumpdtb);
605 exit(0);
606 }
607 error_report("%s: Failed dumping dtb to %s", __func__, dumpdtb);
608 exit(1);
Alexander Graf71193432012-09-23 08:37:59 +0200609 }
Alexander Graf71193432012-09-23 08:37:59 +0200610}
Peter Maydell97c38f82013-07-16 13:25:05 +0100611
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000612int qemu_fdt_setprop_sized_cells_from_array(void *fdt,
613 const char *node_path,
614 const char *property,
615 int numvalues,
616 uint64_t *values)
Peter Maydell97c38f82013-07-16 13:25:05 +0100617{
618 uint32_t *propcells;
619 uint64_t value;
620 int cellnum, vnum, ncells;
621 uint32_t hival;
Sergey Fedorov2bf9feb2014-12-11 18:45:05 +0300622 int ret;
Peter Maydell97c38f82013-07-16 13:25:05 +0100623
624 propcells = g_new0(uint32_t, numvalues * 2);
625
626 cellnum = 0;
627 for (vnum = 0; vnum < numvalues; vnum++) {
628 ncells = values[vnum * 2];
629 if (ncells != 1 && ncells != 2) {
Sergey Fedorov2bf9feb2014-12-11 18:45:05 +0300630 ret = -1;
631 goto out;
Peter Maydell97c38f82013-07-16 13:25:05 +0100632 }
633 value = values[vnum * 2 + 1];
634 hival = cpu_to_be32(value >> 32);
635 if (ncells > 1) {
636 propcells[cellnum++] = hival;
637 } else if (hival != 0) {
Sergey Fedorov2bf9feb2014-12-11 18:45:05 +0300638 ret = -1;
639 goto out;
Peter Maydell97c38f82013-07-16 13:25:05 +0100640 }
641 propcells[cellnum++] = cpu_to_be32(value);
642 }
643
Sergey Fedorov2bf9feb2014-12-11 18:45:05 +0300644 ret = qemu_fdt_setprop(fdt, node_path, property, propcells,
645 cellnum * sizeof(uint32_t));
646out:
647 g_free(propcells);
648 return ret;
Peter Maydell97c38f82013-07-16 13:25:05 +0100649}
Daniel Henrique Barbozabf353ad2022-09-26 14:38:40 -0300650
651void qmp_dumpdtb(const char *filename, Error **errp)
652{
653 g_autoptr(GError) err = NULL;
654 uint32_t size;
655
656 if (!current_machine->fdt) {
657 error_setg(errp, "This machine doesn't have a FDT");
658 return;
659 }
660
661 size = fdt_totalsize(current_machine->fdt);
662
663 g_assert(size > 0);
664
665 if (!g_file_set_contents(filename, current_machine->fdt, size, &err)) {
666 error_setg(errp, "Error saving FDT to file %s: %s",
667 filename, err->message);
668 }
669}
670
Jason A. Donenfelde1e618b2022-10-25 02:43:18 +0200671void qemu_fdt_randomize_seeds(void *fdt)
672{
673 int noffset, poffset, len;
674 const char *name;
675 uint8_t *data;
676
677 for (noffset = fdt_next_node(fdt, 0, NULL);
678 noffset >= 0;
679 noffset = fdt_next_node(fdt, noffset, NULL)) {
680 for (poffset = fdt_first_property_offset(fdt, noffset);
681 poffset >= 0;
682 poffset = fdt_next_property_offset(fdt, poffset)) {
683 data = (uint8_t *)fdt_getprop_by_offset(fdt, poffset, &name, &len);
684 if (!data || strcmp(name, "rng-seed"))
685 continue;
686 qemu_guest_getrandom_nofail(data, len);
687 }
688 }
689}