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