]>
Commit | Line | Data |
---|---|---|
f652e6af AJ |
1 | /* |
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 <[email protected]> | |
8 | * Hollis Blanchard <[email protected]> | |
9 | * | |
10 | * This work is licensed under the GNU GPL license version 2 or later. | |
11 | * | |
12 | */ | |
13 | ||
d38ea87a | 14 | #include "qemu/osdep.h" |
f652e6af | 15 | |
60e43e98 EA |
16 | #ifdef CONFIG_LINUX |
17 | #include <dirent.h> | |
18 | #endif | |
19 | ||
da34e65c | 20 | #include "qapi/error.h" |
f652e6af | 21 | #include "qemu-common.h" |
db013f81 | 22 | #include "qemu/error-report.h" |
9c17d615 | 23 | #include "sysemu/device_tree.h" |
2ff3de68 | 24 | #include "sysemu/sysemu.h" |
39b7f20e | 25 | #include "hw/loader.h" |
6cabe7fa | 26 | #include "hw/boards.h" |
1de7afc9 | 27 | #include "qemu/config-file.h" |
f652e6af AJ |
28 | |
29 | #include <libfdt.h> | |
30 | ||
ce36252c AG |
31 | #define FDT_MAX_SIZE 0x10000 |
32 | ||
33 | void *create_device_tree(int *sizep) | |
34 | { | |
35 | void *fdt; | |
36 | int ret; | |
37 | ||
38 | *sizep = FDT_MAX_SIZE; | |
39 | fdt = g_malloc0(FDT_MAX_SIZE); | |
40 | ret = fdt_create(fdt, FDT_MAX_SIZE); | |
41 | if (ret < 0) { | |
42 | goto fail; | |
43 | } | |
ef6de70e PM |
44 | ret = fdt_finish_reservemap(fdt); |
45 | if (ret < 0) { | |
46 | goto fail; | |
47 | } | |
ce36252c AG |
48 | ret = fdt_begin_node(fdt, ""); |
49 | if (ret < 0) { | |
50 | goto fail; | |
51 | } | |
52 | ret = fdt_end_node(fdt); | |
53 | if (ret < 0) { | |
54 | goto fail; | |
55 | } | |
56 | ret = fdt_finish(fdt); | |
57 | if (ret < 0) { | |
58 | goto fail; | |
59 | } | |
60 | ret = fdt_open_into(fdt, fdt, *sizep); | |
61 | if (ret) { | |
508e221f | 62 | error_report("Unable to copy device tree in memory"); |
ce36252c AG |
63 | exit(1); |
64 | } | |
65 | ||
66 | return fdt; | |
67 | fail: | |
508e221f | 68 | error_report("%s Couldn't create dt: %s", __func__, fdt_strerror(ret)); |
ce36252c AG |
69 | exit(1); |
70 | } | |
71 | ||
7ec632b4 | 72 | void *load_device_tree(const char *filename_path, int *sizep) |
f652e6af | 73 | { |
7ec632b4 | 74 | int dt_size; |
f652e6af | 75 | int dt_file_load_size; |
f652e6af | 76 | int ret; |
7ec632b4 | 77 | void *fdt = NULL; |
f652e6af | 78 | |
7ec632b4 PB |
79 | *sizep = 0; |
80 | dt_size = get_image_size(filename_path); | |
81 | if (dt_size < 0) { | |
db013f81 LL |
82 | error_report("Unable to get size of device tree file '%s'", |
83 | filename_path); | |
f652e6af AJ |
84 | goto fail; |
85 | } | |
86 | ||
7ec632b4 | 87 | /* Expand to 2x size to give enough room for manipulation. */ |
ded57c5f | 88 | dt_size += 10000; |
7ec632b4 | 89 | dt_size *= 2; |
f652e6af | 90 | /* First allocate space in qemu for device tree */ |
7267c094 | 91 | fdt = g_malloc0(dt_size); |
f652e6af | 92 | |
7ec632b4 PB |
93 | dt_file_load_size = load_image(filename_path, fdt); |
94 | if (dt_file_load_size < 0) { | |
db013f81 LL |
95 | error_report("Unable to open device tree file '%s'", |
96 | filename_path); | |
7ec632b4 PB |
97 | goto fail; |
98 | } | |
f652e6af | 99 | |
7ec632b4 | 100 | ret = fdt_open_into(fdt, fdt, dt_size); |
f652e6af | 101 | if (ret) { |
db013f81 | 102 | error_report("Unable to copy device tree in memory"); |
f652e6af AJ |
103 | goto fail; |
104 | } | |
105 | ||
106 | /* Check sanity of device tree */ | |
107 | if (fdt_check_header(fdt)) { | |
db013f81 LL |
108 | error_report("Device tree file loaded into memory is invalid: %s", |
109 | filename_path); | |
f652e6af AJ |
110 | goto fail; |
111 | } | |
7ec632b4 | 112 | *sizep = dt_size; |
f652e6af AJ |
113 | return fdt; |
114 | ||
115 | fail: | |
7267c094 | 116 | g_free(fdt); |
f652e6af AJ |
117 | return NULL; |
118 | } | |
119 | ||
60e43e98 EA |
120 | #ifdef CONFIG_LINUX |
121 | ||
122 | #define SYSFS_DT_BASEDIR "/proc/device-tree" | |
123 | ||
124 | /** | |
125 | * read_fstree: this function is inspired from dtc read_fstree | |
126 | * @fdt: preallocated fdt blob buffer, to be populated | |
127 | * @dirname: directory to scan under SYSFS_DT_BASEDIR | |
128 | * the search is recursive and the tree is searched down to the | |
129 | * leaves (property files). | |
130 | * | |
131 | * the function asserts in case of error | |
132 | */ | |
133 | static void read_fstree(void *fdt, const char *dirname) | |
134 | { | |
135 | DIR *d; | |
136 | struct dirent *de; | |
137 | struct stat st; | |
138 | const char *root_dir = SYSFS_DT_BASEDIR; | |
139 | const char *parent_node; | |
140 | ||
141 | if (strstr(dirname, root_dir) != dirname) { | |
142 | error_setg(&error_fatal, "%s: %s must be searched within %s", | |
143 | __func__, dirname, root_dir); | |
144 | } | |
145 | parent_node = &dirname[strlen(SYSFS_DT_BASEDIR)]; | |
146 | ||
147 | d = opendir(dirname); | |
148 | if (!d) { | |
149 | error_setg(&error_fatal, "%s cannot open %s", __func__, dirname); | |
150 | } | |
151 | ||
152 | while ((de = readdir(d)) != NULL) { | |
153 | char *tmpnam; | |
154 | ||
155 | if (!g_strcmp0(de->d_name, ".") | |
156 | || !g_strcmp0(de->d_name, "..")) { | |
157 | continue; | |
158 | } | |
159 | ||
160 | tmpnam = g_strdup_printf("%s/%s", dirname, de->d_name); | |
161 | ||
162 | if (lstat(tmpnam, &st) < 0) { | |
163 | error_setg(&error_fatal, "%s cannot lstat %s", __func__, tmpnam); | |
164 | } | |
165 | ||
166 | if (S_ISREG(st.st_mode)) { | |
167 | gchar *val; | |
168 | gsize len; | |
169 | ||
170 | if (!g_file_get_contents(tmpnam, &val, &len, NULL)) { | |
171 | error_setg(&error_fatal, "%s not able to extract info from %s", | |
172 | __func__, tmpnam); | |
173 | } | |
174 | ||
175 | if (strlen(parent_node) > 0) { | |
176 | qemu_fdt_setprop(fdt, parent_node, | |
177 | de->d_name, val, len); | |
178 | } else { | |
179 | qemu_fdt_setprop(fdt, "/", de->d_name, val, len); | |
180 | } | |
181 | g_free(val); | |
182 | } else if (S_ISDIR(st.st_mode)) { | |
183 | char *node_name; | |
184 | ||
185 | node_name = g_strdup_printf("%s/%s", | |
186 | parent_node, de->d_name); | |
187 | qemu_fdt_add_subnode(fdt, node_name); | |
188 | g_free(node_name); | |
189 | read_fstree(fdt, tmpnam); | |
190 | } | |
191 | ||
192 | g_free(tmpnam); | |
193 | } | |
194 | ||
195 | closedir(d); | |
196 | } | |
197 | ||
198 | /* load_device_tree_from_sysfs: extract the dt blob from host sysfs */ | |
199 | void *load_device_tree_from_sysfs(void) | |
200 | { | |
201 | void *host_fdt; | |
202 | int host_fdt_size; | |
203 | ||
204 | host_fdt = create_device_tree(&host_fdt_size); | |
205 | read_fstree(host_fdt, SYSFS_DT_BASEDIR); | |
206 | if (fdt_check_header(host_fdt)) { | |
207 | error_setg(&error_fatal, | |
208 | "%s host device tree extracted into memory is invalid", | |
209 | __func__); | |
210 | } | |
211 | return host_fdt; | |
212 | } | |
213 | ||
214 | #endif /* CONFIG_LINUX */ | |
215 | ||
ccbcfedd | 216 | static int findnode_nofail(void *fdt, const char *node_path) |
f652e6af AJ |
217 | { |
218 | int offset; | |
219 | ||
220 | offset = fdt_path_offset(fdt, node_path); | |
ccbcfedd | 221 | if (offset < 0) { |
508e221f LL |
222 | error_report("%s Couldn't find node %s: %s", __func__, node_path, |
223 | fdt_strerror(offset)); | |
ccbcfedd AG |
224 | exit(1); |
225 | } | |
226 | ||
227 | return offset; | |
228 | } | |
229 | ||
6d79566a EA |
230 | char **qemu_fdt_node_path(void *fdt, const char *name, char *compat, |
231 | Error **errp) | |
232 | { | |
233 | int offset, len, ret; | |
234 | const char *iter_name; | |
235 | unsigned int path_len = 16, n = 0; | |
236 | GSList *path_list = NULL, *iter; | |
237 | char **path_array; | |
238 | ||
239 | offset = fdt_node_offset_by_compatible(fdt, -1, compat); | |
240 | ||
241 | while (offset >= 0) { | |
242 | iter_name = fdt_get_name(fdt, offset, &len); | |
243 | if (!iter_name) { | |
244 | offset = len; | |
245 | break; | |
246 | } | |
247 | if (!strcmp(iter_name, name)) { | |
248 | char *path; | |
249 | ||
250 | path = g_malloc(path_len); | |
251 | while ((ret = fdt_get_path(fdt, offset, path, path_len)) | |
252 | == -FDT_ERR_NOSPACE) { | |
253 | path_len += 16; | |
254 | path = g_realloc(path, path_len); | |
255 | } | |
256 | path_list = g_slist_prepend(path_list, path); | |
257 | n++; | |
258 | } | |
259 | offset = fdt_node_offset_by_compatible(fdt, offset, compat); | |
260 | } | |
261 | ||
262 | if (offset < 0 && offset != -FDT_ERR_NOTFOUND) { | |
263 | error_setg(errp, "%s: abort parsing dt for %s/%s: %s", | |
264 | __func__, name, compat, fdt_strerror(offset)); | |
265 | for (iter = path_list; iter; iter = iter->next) { | |
266 | g_free(iter->data); | |
267 | } | |
268 | g_slist_free(path_list); | |
269 | return NULL; | |
270 | } | |
271 | ||
272 | path_array = g_new(char *, n + 1); | |
273 | path_array[n--] = NULL; | |
274 | ||
275 | for (iter = path_list; iter; iter = iter->next) { | |
276 | path_array[n--] = iter->data; | |
277 | } | |
278 | ||
279 | g_slist_free(path_list); | |
280 | ||
281 | return path_array; | |
282 | } | |
283 | ||
5a4348d1 | 284 | int qemu_fdt_setprop(void *fdt, const char *node_path, |
be5907f2 | 285 | const char *property, const void *val, int size) |
ccbcfedd AG |
286 | { |
287 | int r; | |
288 | ||
be5907f2 | 289 | r = fdt_setprop(fdt, findnode_nofail(fdt, node_path), property, val, size); |
ccbcfedd | 290 | if (r < 0) { |
508e221f LL |
291 | error_report("%s: Couldn't set %s/%s: %s", __func__, node_path, |
292 | property, fdt_strerror(r)); | |
ccbcfedd AG |
293 | exit(1); |
294 | } | |
f652e6af | 295 | |
ccbcfedd | 296 | return r; |
f652e6af AJ |
297 | } |
298 | ||
5a4348d1 PC |
299 | int qemu_fdt_setprop_cell(void *fdt, const char *node_path, |
300 | const char *property, uint32_t val) | |
f652e6af | 301 | { |
ccbcfedd | 302 | int r; |
f652e6af | 303 | |
ccbcfedd AG |
304 | r = fdt_setprop_cell(fdt, findnode_nofail(fdt, node_path), property, val); |
305 | if (r < 0) { | |
508e221f LL |
306 | error_report("%s: Couldn't set %s/%s = %#08x: %s", __func__, |
307 | node_path, property, val, fdt_strerror(r)); | |
ccbcfedd AG |
308 | exit(1); |
309 | } | |
f652e6af | 310 | |
ccbcfedd | 311 | return r; |
f652e6af AJ |
312 | } |
313 | ||
5a4348d1 PC |
314 | int qemu_fdt_setprop_u64(void *fdt, const char *node_path, |
315 | const char *property, uint64_t val) | |
bb28eb37 AG |
316 | { |
317 | val = cpu_to_be64(val); | |
5a4348d1 | 318 | return qemu_fdt_setprop(fdt, node_path, property, &val, sizeof(val)); |
bb28eb37 AG |
319 | } |
320 | ||
5a4348d1 PC |
321 | int qemu_fdt_setprop_string(void *fdt, const char *node_path, |
322 | const char *property, const char *string) | |
f652e6af | 323 | { |
ccbcfedd | 324 | int r; |
f652e6af | 325 | |
ccbcfedd AG |
326 | r = fdt_setprop_string(fdt, findnode_nofail(fdt, node_path), property, string); |
327 | if (r < 0) { | |
508e221f LL |
328 | error_report("%s: Couldn't set %s/%s = %s: %s", __func__, |
329 | node_path, property, string, fdt_strerror(r)); | |
ccbcfedd AG |
330 | exit(1); |
331 | } | |
f652e6af | 332 | |
ccbcfedd | 333 | return r; |
f652e6af | 334 | } |
d69a8e63 | 335 | |
5a4348d1 | 336 | const void *qemu_fdt_getprop(void *fdt, const char *node_path, |
78e24f23 | 337 | const char *property, int *lenp, Error **errp) |
f0aa713f PM |
338 | { |
339 | int len; | |
340 | const void *r; | |
78e24f23 | 341 | |
f0aa713f PM |
342 | if (!lenp) { |
343 | lenp = &len; | |
344 | } | |
345 | r = fdt_getprop(fdt, findnode_nofail(fdt, node_path), property, lenp); | |
346 | if (!r) { | |
78e24f23 EA |
347 | error_setg(errp, "%s: Couldn't get %s/%s: %s", __func__, |
348 | node_path, property, fdt_strerror(*lenp)); | |
f0aa713f PM |
349 | } |
350 | return r; | |
351 | } | |
352 | ||
5a4348d1 | 353 | uint32_t qemu_fdt_getprop_cell(void *fdt, const char *node_path, |
58e71097 | 354 | const char *property, int *lenp, Error **errp) |
f0aa713f PM |
355 | { |
356 | int len; | |
58e71097 EA |
357 | const uint32_t *p; |
358 | ||
359 | if (!lenp) { | |
360 | lenp = &len; | |
361 | } | |
362 | p = qemu_fdt_getprop(fdt, node_path, property, lenp, errp); | |
363 | if (!p) { | |
364 | return 0; | |
365 | } else if (*lenp != 4) { | |
366 | error_setg(errp, "%s: %s/%s not 4 bytes long (not a cell?)", | |
367 | __func__, node_path, property); | |
368 | *lenp = -EINVAL; | |
369 | return 0; | |
f0aa713f PM |
370 | } |
371 | return be32_to_cpu(*p); | |
372 | } | |
373 | ||
5a4348d1 | 374 | uint32_t qemu_fdt_get_phandle(void *fdt, const char *path) |
7d5fd108 AG |
375 | { |
376 | uint32_t r; | |
377 | ||
378 | r = fdt_get_phandle(fdt, findnode_nofail(fdt, path)); | |
909a196d | 379 | if (r == 0) { |
508e221f LL |
380 | error_report("%s: Couldn't get phandle for %s: %s", __func__, |
381 | path, fdt_strerror(r)); | |
7d5fd108 AG |
382 | exit(1); |
383 | } | |
384 | ||
385 | return r; | |
386 | } | |
387 | ||
5a4348d1 PC |
388 | int qemu_fdt_setprop_phandle(void *fdt, const char *node_path, |
389 | const char *property, | |
390 | const char *target_node_path) | |
8535ab12 | 391 | { |
5a4348d1 PC |
392 | uint32_t phandle = qemu_fdt_get_phandle(fdt, target_node_path); |
393 | return qemu_fdt_setprop_cell(fdt, node_path, property, phandle); | |
8535ab12 AG |
394 | } |
395 | ||
5a4348d1 | 396 | uint32_t qemu_fdt_alloc_phandle(void *fdt) |
3601b572 | 397 | { |
4b1b1c89 AG |
398 | static int phandle = 0x0; |
399 | ||
400 | /* | |
401 | * We need to find out if the user gave us special instruction at | |
cc47a16b | 402 | * which phandle id to start allocating phandles. |
4b1b1c89 AG |
403 | */ |
404 | if (!phandle) { | |
6cabe7fa | 405 | phandle = machine_phandle_start(current_machine); |
4b1b1c89 AG |
406 | } |
407 | ||
408 | if (!phandle) { | |
409 | /* | |
410 | * None or invalid phandle given on the command line, so fall back to | |
411 | * default starting point. | |
412 | */ | |
413 | phandle = 0x8000; | |
414 | } | |
3601b572 AG |
415 | |
416 | return phandle++; | |
417 | } | |
418 | ||
5a4348d1 | 419 | int qemu_fdt_nop_node(void *fdt, const char *node_path) |
d69a8e63 | 420 | { |
ccbcfedd | 421 | int r; |
d69a8e63 | 422 | |
ccbcfedd AG |
423 | r = fdt_nop_node(fdt, findnode_nofail(fdt, node_path)); |
424 | if (r < 0) { | |
508e221f LL |
425 | error_report("%s: Couldn't nop node %s: %s", __func__, node_path, |
426 | fdt_strerror(r)); | |
ccbcfedd AG |
427 | exit(1); |
428 | } | |
d69a8e63 | 429 | |
ccbcfedd | 430 | return r; |
d69a8e63 | 431 | } |
80ad7816 | 432 | |
5a4348d1 | 433 | int qemu_fdt_add_subnode(void *fdt, const char *name) |
80ad7816 | 434 | { |
80ad7816 AG |
435 | char *dupname = g_strdup(name); |
436 | char *basename = strrchr(dupname, '/'); | |
437 | int retval; | |
c640d088 | 438 | int parent = 0; |
80ad7816 AG |
439 | |
440 | if (!basename) { | |
bff39b63 | 441 | g_free(dupname); |
80ad7816 AG |
442 | return -1; |
443 | } | |
444 | ||
445 | basename[0] = '\0'; | |
446 | basename++; | |
447 | ||
c640d088 AG |
448 | if (dupname[0]) { |
449 | parent = findnode_nofail(fdt, dupname); | |
450 | } | |
451 | ||
452 | retval = fdt_add_subnode(fdt, parent, basename); | |
ccbcfedd | 453 | if (retval < 0) { |
508e221f LL |
454 | error_report("FDT: Failed to create subnode %s: %s", name, |
455 | fdt_strerror(retval)); | |
ccbcfedd | 456 | exit(1); |
80ad7816 AG |
457 | } |
458 | ||
80ad7816 AG |
459 | g_free(dupname); |
460 | return retval; | |
461 | } | |
71193433 | 462 | |
5a4348d1 | 463 | void qemu_fdt_dumpdtb(void *fdt, int size) |
71193433 | 464 | { |
2ff3de68 | 465 | const char *dumpdtb = qemu_opt_get(qemu_get_machine_opts(), "dumpdtb"); |
71193433 | 466 | |
2ff3de68 MA |
467 | if (dumpdtb) { |
468 | /* Dump the dtb to a file and quit */ | |
469 | exit(g_file_set_contents(dumpdtb, fdt, size, NULL) ? 0 : 1); | |
470 | } | |
71193433 | 471 | } |
97c38f8c | 472 | |
5a4348d1 PC |
473 | int qemu_fdt_setprop_sized_cells_from_array(void *fdt, |
474 | const char *node_path, | |
475 | const char *property, | |
476 | int numvalues, | |
477 | uint64_t *values) | |
97c38f8c PM |
478 | { |
479 | uint32_t *propcells; | |
480 | uint64_t value; | |
481 | int cellnum, vnum, ncells; | |
482 | uint32_t hival; | |
2bf9febc | 483 | int ret; |
97c38f8c PM |
484 | |
485 | propcells = g_new0(uint32_t, numvalues * 2); | |
486 | ||
487 | cellnum = 0; | |
488 | for (vnum = 0; vnum < numvalues; vnum++) { | |
489 | ncells = values[vnum * 2]; | |
490 | if (ncells != 1 && ncells != 2) { | |
2bf9febc SF |
491 | ret = -1; |
492 | goto out; | |
97c38f8c PM |
493 | } |
494 | value = values[vnum * 2 + 1]; | |
495 | hival = cpu_to_be32(value >> 32); | |
496 | if (ncells > 1) { | |
497 | propcells[cellnum++] = hival; | |
498 | } else if (hival != 0) { | |
2bf9febc SF |
499 | ret = -1; |
500 | goto out; | |
97c38f8c PM |
501 | } |
502 | propcells[cellnum++] = cpu_to_be32(value); | |
503 | } | |
504 | ||
2bf9febc SF |
505 | ret = qemu_fdt_setprop(fdt, node_path, property, propcells, |
506 | cellnum * sizeof(uint32_t)); | |
507 | out: | |
508 | g_free(propcells); | |
509 | return ret; | |
97c38f8c | 510 | } |