blob: ffec99ae296bdc85a91677fd982f991579cc5145 [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
14#include <stdio.h>
15#include <sys/types.h>
16#include <sys/stat.h>
17#include <fcntl.h>
18#include <unistd.h>
19#include <stdlib.h>
20
21#include "config.h"
22#include "qemu-common.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010023#include "sysemu/device_tree.h"
Markus Armbruster2ff3de62013-07-04 15:09:22 +020024#include "sysemu/sysemu.h"
Blue Swirl39b7f202009-09-22 17:51:36 +000025#include "hw/loader.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010026#include "qemu/option.h"
27#include "qemu/config-file.h"
aurel32f652e6a2008-12-16 10:43:48 +000028
29#include <libfdt.h>
30
Alexander Grafce362522012-05-17 15:33:54 +020031#define FDT_MAX_SIZE 0x10000
32
33void *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 }
44 ret = fdt_begin_node(fdt, "");
45 if (ret < 0) {
46 goto fail;
47 }
48 ret = fdt_end_node(fdt);
49 if (ret < 0) {
50 goto fail;
51 }
52 ret = fdt_finish(fdt);
53 if (ret < 0) {
54 goto fail;
55 }
56 ret = fdt_open_into(fdt, fdt, *sizep);
57 if (ret) {
58 fprintf(stderr, "Unable to copy device tree in memory\n");
59 exit(1);
60 }
61
62 return fdt;
63fail:
64 fprintf(stderr, "%s Couldn't create dt: %s\n", __func__, fdt_strerror(ret));
65 exit(1);
66}
67
pbrook7ec632b2009-04-10 16:23:59 +000068void *load_device_tree(const char *filename_path, int *sizep)
aurel32f652e6a2008-12-16 10:43:48 +000069{
pbrook7ec632b2009-04-10 16:23:59 +000070 int dt_size;
aurel32f652e6a2008-12-16 10:43:48 +000071 int dt_file_load_size;
aurel32f652e6a2008-12-16 10:43:48 +000072 int ret;
pbrook7ec632b2009-04-10 16:23:59 +000073 void *fdt = NULL;
aurel32f652e6a2008-12-16 10:43:48 +000074
pbrook7ec632b2009-04-10 16:23:59 +000075 *sizep = 0;
76 dt_size = get_image_size(filename_path);
77 if (dt_size < 0) {
aurel32f652e6a2008-12-16 10:43:48 +000078 printf("Unable to get size of device tree file '%s'\n",
79 filename_path);
80 goto fail;
81 }
82
pbrook7ec632b2009-04-10 16:23:59 +000083 /* Expand to 2x size to give enough room for manipulation. */
Alexander Grafded57c52011-07-23 10:54:11 +020084 dt_size += 10000;
pbrook7ec632b2009-04-10 16:23:59 +000085 dt_size *= 2;
aurel32f652e6a2008-12-16 10:43:48 +000086 /* First allocate space in qemu for device tree */
Anthony Liguori7267c092011-08-20 22:09:37 -050087 fdt = g_malloc0(dt_size);
aurel32f652e6a2008-12-16 10:43:48 +000088
pbrook7ec632b2009-04-10 16:23:59 +000089 dt_file_load_size = load_image(filename_path, fdt);
90 if (dt_file_load_size < 0) {
91 printf("Unable to open device tree file '%s'\n",
92 filename_path);
93 goto fail;
94 }
aurel32f652e6a2008-12-16 10:43:48 +000095
pbrook7ec632b2009-04-10 16:23:59 +000096 ret = fdt_open_into(fdt, fdt, dt_size);
aurel32f652e6a2008-12-16 10:43:48 +000097 if (ret) {
98 printf("Unable to copy device tree in memory\n");
99 goto fail;
100 }
101
102 /* Check sanity of device tree */
103 if (fdt_check_header(fdt)) {
104 printf ("Device tree file loaded into memory is invalid: %s\n",
105 filename_path);
106 goto fail;
107 }
pbrook7ec632b2009-04-10 16:23:59 +0000108 *sizep = dt_size;
aurel32f652e6a2008-12-16 10:43:48 +0000109 return fdt;
110
111fail:
Anthony Liguori7267c092011-08-20 22:09:37 -0500112 g_free(fdt);
aurel32f652e6a2008-12-16 10:43:48 +0000113 return NULL;
114}
115
Alexander Grafccbcfed2011-07-23 10:52:00 +0200116static int findnode_nofail(void *fdt, const char *node_path)
aurel32f652e6a2008-12-16 10:43:48 +0000117{
118 int offset;
119
120 offset = fdt_path_offset(fdt, node_path);
Alexander Grafccbcfed2011-07-23 10:52:00 +0200121 if (offset < 0) {
122 fprintf(stderr, "%s Couldn't find node %s: %s\n", __func__, node_path,
123 fdt_strerror(offset));
124 exit(1);
125 }
aurel32f652e6a2008-12-16 10:43:48 +0000126
Alexander Grafccbcfed2011-07-23 10:52:00 +0200127 return offset;
128}
129
130int qemu_devtree_setprop(void *fdt, const char *node_path,
Alexander Graf45e9dfb2012-06-20 20:39:59 +0200131 const char *property, const void *val_array, int size)
Alexander Grafccbcfed2011-07-23 10:52:00 +0200132{
133 int r;
134
135 r = fdt_setprop(fdt, findnode_nofail(fdt, node_path), property, val_array, size);
136 if (r < 0) {
137 fprintf(stderr, "%s: Couldn't set %s/%s: %s\n", __func__, node_path,
138 property, fdt_strerror(r));
139 exit(1);
140 }
141
142 return r;
aurel32f652e6a2008-12-16 10:43:48 +0000143}
144
145int qemu_devtree_setprop_cell(void *fdt, const char *node_path,
146 const char *property, uint32_t val)
147{
Alexander Grafccbcfed2011-07-23 10:52:00 +0200148 int r;
aurel32f652e6a2008-12-16 10:43:48 +0000149
Alexander Grafccbcfed2011-07-23 10:52:00 +0200150 r = fdt_setprop_cell(fdt, findnode_nofail(fdt, node_path), property, val);
151 if (r < 0) {
152 fprintf(stderr, "%s: Couldn't set %s/%s = %#08x: %s\n", __func__,
153 node_path, property, val, fdt_strerror(r));
154 exit(1);
155 }
aurel32f652e6a2008-12-16 10:43:48 +0000156
Alexander Grafccbcfed2011-07-23 10:52:00 +0200157 return r;
aurel32f652e6a2008-12-16 10:43:48 +0000158}
159
Alexander Grafbb28eb32012-05-18 01:53:01 +0200160int qemu_devtree_setprop_u64(void *fdt, const char *node_path,
161 const char *property, uint64_t val)
162{
163 val = cpu_to_be64(val);
164 return qemu_devtree_setprop(fdt, node_path, property, &val, sizeof(val));
165}
166
aurel32f652e6a2008-12-16 10:43:48 +0000167int qemu_devtree_setprop_string(void *fdt, const char *node_path,
168 const char *property, const char *string)
169{
Alexander Grafccbcfed2011-07-23 10:52:00 +0200170 int r;
aurel32f652e6a2008-12-16 10:43:48 +0000171
Alexander Grafccbcfed2011-07-23 10:52:00 +0200172 r = fdt_setprop_string(fdt, findnode_nofail(fdt, node_path), property, string);
173 if (r < 0) {
174 fprintf(stderr, "%s: Couldn't set %s/%s = %s: %s\n", __func__,
175 node_path, property, string, fdt_strerror(r));
176 exit(1);
177 }
aurel32f652e6a2008-12-16 10:43:48 +0000178
Alexander Grafccbcfed2011-07-23 10:52:00 +0200179 return r;
aurel32f652e6a2008-12-16 10:43:48 +0000180}
Alexander Grafd69a8e62011-07-21 01:52:57 +0200181
Peter Maydellf0aa7132012-07-20 13:34:50 +0100182const void *qemu_devtree_getprop(void *fdt, const char *node_path,
183 const char *property, int *lenp)
184{
185 int len;
186 const void *r;
187 if (!lenp) {
188 lenp = &len;
189 }
190 r = fdt_getprop(fdt, findnode_nofail(fdt, node_path), property, lenp);
191 if (!r) {
192 fprintf(stderr, "%s: Couldn't get %s/%s: %s\n", __func__,
193 node_path, property, fdt_strerror(*lenp));
194 exit(1);
195 }
196 return r;
197}
198
199uint32_t qemu_devtree_getprop_cell(void *fdt, const char *node_path,
200 const char *property)
201{
202 int len;
203 const uint32_t *p = qemu_devtree_getprop(fdt, node_path, property, &len);
204 if (len != 4) {
205 fprintf(stderr, "%s: %s/%s not 4 bytes long (not a cell?)\n",
206 __func__, node_path, property);
207 exit(1);
208 }
209 return be32_to_cpu(*p);
210}
211
Alexander Graf7d5fd102012-05-17 15:23:39 +0200212uint32_t qemu_devtree_get_phandle(void *fdt, const char *path)
213{
214 uint32_t r;
215
216 r = fdt_get_phandle(fdt, findnode_nofail(fdt, path));
Stefan Weil909a1962013-06-10 22:12:25 +0200217 if (r == 0) {
Alexander Graf7d5fd102012-05-17 15:23:39 +0200218 fprintf(stderr, "%s: Couldn't get phandle for %s: %s\n", __func__,
219 path, fdt_strerror(r));
220 exit(1);
221 }
222
223 return r;
224}
225
Alexander Graf8535ab12012-05-17 14:11:52 +0200226int qemu_devtree_setprop_phandle(void *fdt, const char *node_path,
227 const char *property,
228 const char *target_node_path)
229{
Alexander Graf7d5fd102012-05-17 15:23:39 +0200230 uint32_t phandle = qemu_devtree_get_phandle(fdt, target_node_path);
Alexander Graf8535ab12012-05-17 14:11:52 +0200231 return qemu_devtree_setprop_cell(fdt, node_path, property, phandle);
232}
233
Alexander Graf3601b572012-05-17 16:58:55 +0200234uint32_t qemu_devtree_alloc_phandle(void *fdt)
235{
Alexander Graf4b1b1c82012-06-06 01:01:23 +0200236 static int phandle = 0x0;
237
238 /*
239 * We need to find out if the user gave us special instruction at
240 * which phandle id to start allocting phandles.
241 */
242 if (!phandle) {
Markus Armbrusterc1b71b02013-07-04 15:09:23 +0200243 phandle = qemu_opt_get_number(qemu_get_machine_opts(),
244 "phandle_start", 0);
Alexander Graf4b1b1c82012-06-06 01:01:23 +0200245 }
246
247 if (!phandle) {
248 /*
249 * None or invalid phandle given on the command line, so fall back to
250 * default starting point.
251 */
252 phandle = 0x8000;
253 }
Alexander Graf3601b572012-05-17 16:58:55 +0200254
255 return phandle++;
256}
257
Alexander Grafd69a8e62011-07-21 01:52:57 +0200258int qemu_devtree_nop_node(void *fdt, const char *node_path)
259{
Alexander Grafccbcfed2011-07-23 10:52:00 +0200260 int r;
Alexander Grafd69a8e62011-07-21 01:52:57 +0200261
Alexander Grafccbcfed2011-07-23 10:52:00 +0200262 r = fdt_nop_node(fdt, findnode_nofail(fdt, node_path));
263 if (r < 0) {
264 fprintf(stderr, "%s: Couldn't nop node %s: %s\n", __func__, node_path,
265 fdt_strerror(r));
266 exit(1);
267 }
Alexander Grafd69a8e62011-07-21 01:52:57 +0200268
Alexander Grafccbcfed2011-07-23 10:52:00 +0200269 return r;
Alexander Grafd69a8e62011-07-21 01:52:57 +0200270}
Alexander Graf80ad7812011-07-22 13:55:37 +0200271
272int qemu_devtree_add_subnode(void *fdt, const char *name)
273{
Alexander Graf80ad7812011-07-22 13:55:37 +0200274 char *dupname = g_strdup(name);
275 char *basename = strrchr(dupname, '/');
276 int retval;
Alexander Grafc640d082012-05-17 11:40:42 +0200277 int parent = 0;
Alexander Graf80ad7812011-07-22 13:55:37 +0200278
279 if (!basename) {
Stefan Weilbff39b62011-10-17 22:12:09 +0200280 g_free(dupname);
Alexander Graf80ad7812011-07-22 13:55:37 +0200281 return -1;
282 }
283
284 basename[0] = '\0';
285 basename++;
286
Alexander Grafc640d082012-05-17 11:40:42 +0200287 if (dupname[0]) {
288 parent = findnode_nofail(fdt, dupname);
289 }
290
291 retval = fdt_add_subnode(fdt, parent, basename);
Alexander Grafccbcfed2011-07-23 10:52:00 +0200292 if (retval < 0) {
293 fprintf(stderr, "FDT: Failed to create subnode %s: %s\n", name,
294 fdt_strerror(retval));
295 exit(1);
Alexander Graf80ad7812011-07-22 13:55:37 +0200296 }
297
Alexander Graf80ad7812011-07-22 13:55:37 +0200298 g_free(dupname);
299 return retval;
300}
Alexander Graf71193432012-09-23 08:37:59 +0200301
302void qemu_devtree_dumpdtb(void *fdt, int size)
303{
Markus Armbruster2ff3de62013-07-04 15:09:22 +0200304 const char *dumpdtb = qemu_opt_get(qemu_get_machine_opts(), "dumpdtb");
Alexander Graf71193432012-09-23 08:37:59 +0200305
Markus Armbruster2ff3de62013-07-04 15:09:22 +0200306 if (dumpdtb) {
307 /* Dump the dtb to a file and quit */
308 exit(g_file_set_contents(dumpdtb, fdt, size, NULL) ? 0 : 1);
Alexander Graf71193432012-09-23 08:37:59 +0200309 }
Alexander Graf71193432012-09-23 08:37:59 +0200310}
Peter Maydell97c38f82013-07-16 13:25:05 +0100311
312int qemu_devtree_setprop_sized_cells_from_array(void *fdt,
313 const char *node_path,
314 const char *property,
315 int numvalues,
316 uint64_t *values)
317{
318 uint32_t *propcells;
319 uint64_t value;
320 int cellnum, vnum, ncells;
321 uint32_t hival;
322
323 propcells = g_new0(uint32_t, numvalues * 2);
324
325 cellnum = 0;
326 for (vnum = 0; vnum < numvalues; vnum++) {
327 ncells = values[vnum * 2];
328 if (ncells != 1 && ncells != 2) {
329 return -1;
330 }
331 value = values[vnum * 2 + 1];
332 hival = cpu_to_be32(value >> 32);
333 if (ncells > 1) {
334 propcells[cellnum++] = hival;
335 } else if (hival != 0) {
336 return -1;
337 }
338 propcells[cellnum++] = cpu_to_be32(value);
339 }
340
341 return qemu_devtree_setprop(fdt, node_path, property, propcells,
342 cellnum * sizeof(uint32_t));
343}