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