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