]> git.proxmox.com Git - mirror_qemu.git/blame - device_tree.c
virtio-net: correctly drop truncated packets
[mirror_qemu.git] / device_tree.c
CommitLineData
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
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
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
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 }
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;
67fail:
508e221f 68 error_report("%s Couldn't create dt: %s", __func__, fdt_strerror(ret));
ce36252c
AG
69 exit(1);
70}
71
7ec632b4 72void *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
115fail:
7267c094 116 g_free(fdt);
f652e6af
AJ
117 return NULL;
118}
119
ccbcfedd 120static int findnode_nofail(void *fdt, const char *node_path)
f652e6af
AJ
121{
122 int offset;
123
124 offset = fdt_path_offset(fdt, node_path);
ccbcfedd 125 if (offset < 0) {
508e221f
LL
126 error_report("%s Couldn't find node %s: %s", __func__, node_path,
127 fdt_strerror(offset));
ccbcfedd
AG
128 exit(1);
129 }
130
131 return offset;
132}
133
5a4348d1 134int qemu_fdt_setprop(void *fdt, const char *node_path,
be5907f2 135 const char *property, const void *val, int size)
ccbcfedd
AG
136{
137 int r;
138
be5907f2 139 r = fdt_setprop(fdt, findnode_nofail(fdt, node_path), property, val, size);
ccbcfedd 140 if (r < 0) {
508e221f
LL
141 error_report("%s: Couldn't set %s/%s: %s", __func__, node_path,
142 property, fdt_strerror(r));
ccbcfedd
AG
143 exit(1);
144 }
f652e6af 145
ccbcfedd 146 return r;
f652e6af
AJ
147}
148
5a4348d1
PC
149int qemu_fdt_setprop_cell(void *fdt, const char *node_path,
150 const char *property, uint32_t val)
f652e6af 151{
ccbcfedd 152 int r;
f652e6af 153
ccbcfedd
AG
154 r = fdt_setprop_cell(fdt, findnode_nofail(fdt, node_path), property, val);
155 if (r < 0) {
508e221f
LL
156 error_report("%s: Couldn't set %s/%s = %#08x: %s", __func__,
157 node_path, property, val, fdt_strerror(r));
ccbcfedd
AG
158 exit(1);
159 }
f652e6af 160
ccbcfedd 161 return r;
f652e6af
AJ
162}
163
5a4348d1
PC
164int qemu_fdt_setprop_u64(void *fdt, const char *node_path,
165 const char *property, uint64_t val)
bb28eb37
AG
166{
167 val = cpu_to_be64(val);
5a4348d1 168 return qemu_fdt_setprop(fdt, node_path, property, &val, sizeof(val));
bb28eb37
AG
169}
170
5a4348d1
PC
171int qemu_fdt_setprop_string(void *fdt, const char *node_path,
172 const char *property, const char *string)
f652e6af 173{
ccbcfedd 174 int r;
f652e6af 175
ccbcfedd
AG
176 r = fdt_setprop_string(fdt, findnode_nofail(fdt, node_path), property, string);
177 if (r < 0) {
508e221f
LL
178 error_report("%s: Couldn't set %s/%s = %s: %s", __func__,
179 node_path, property, string, fdt_strerror(r));
ccbcfedd
AG
180 exit(1);
181 }
f652e6af 182
ccbcfedd 183 return r;
f652e6af 184}
d69a8e63 185
5a4348d1
PC
186const void *qemu_fdt_getprop(void *fdt, const char *node_path,
187 const char *property, int *lenp)
f0aa713f
PM
188{
189 int len;
190 const void *r;
191 if (!lenp) {
192 lenp = &len;
193 }
194 r = fdt_getprop(fdt, findnode_nofail(fdt, node_path), property, lenp);
195 if (!r) {
508e221f
LL
196 error_report("%s: Couldn't get %s/%s: %s", __func__,
197 node_path, property, fdt_strerror(*lenp));
f0aa713f
PM
198 exit(1);
199 }
200 return r;
201}
202
5a4348d1
PC
203uint32_t qemu_fdt_getprop_cell(void *fdt, const char *node_path,
204 const char *property)
f0aa713f
PM
205{
206 int len;
5a4348d1 207 const uint32_t *p = qemu_fdt_getprop(fdt, node_path, property, &len);
f0aa713f 208 if (len != 4) {
508e221f
LL
209 error_report("%s: %s/%s not 4 bytes long (not a cell?)",
210 __func__, node_path, property);
f0aa713f
PM
211 exit(1);
212 }
213 return be32_to_cpu(*p);
214}
215
5a4348d1 216uint32_t qemu_fdt_get_phandle(void *fdt, const char *path)
7d5fd108
AG
217{
218 uint32_t r;
219
220 r = fdt_get_phandle(fdt, findnode_nofail(fdt, path));
909a196d 221 if (r == 0) {
508e221f
LL
222 error_report("%s: Couldn't get phandle for %s: %s", __func__,
223 path, fdt_strerror(r));
7d5fd108
AG
224 exit(1);
225 }
226
227 return r;
228}
229
5a4348d1
PC
230int qemu_fdt_setprop_phandle(void *fdt, const char *node_path,
231 const char *property,
232 const char *target_node_path)
8535ab12 233{
5a4348d1
PC
234 uint32_t phandle = qemu_fdt_get_phandle(fdt, target_node_path);
235 return qemu_fdt_setprop_cell(fdt, node_path, property, phandle);
8535ab12
AG
236}
237
5a4348d1 238uint32_t qemu_fdt_alloc_phandle(void *fdt)
3601b572 239{
4b1b1c89
AG
240 static int phandle = 0x0;
241
242 /*
243 * We need to find out if the user gave us special instruction at
cc47a16b 244 * which phandle id to start allocating phandles.
4b1b1c89
AG
245 */
246 if (!phandle) {
6cabe7fa 247 phandle = machine_phandle_start(current_machine);
4b1b1c89
AG
248 }
249
250 if (!phandle) {
251 /*
252 * None or invalid phandle given on the command line, so fall back to
253 * default starting point.
254 */
255 phandle = 0x8000;
256 }
3601b572
AG
257
258 return phandle++;
259}
260
5a4348d1 261int qemu_fdt_nop_node(void *fdt, const char *node_path)
d69a8e63 262{
ccbcfedd 263 int r;
d69a8e63 264
ccbcfedd
AG
265 r = fdt_nop_node(fdt, findnode_nofail(fdt, node_path));
266 if (r < 0) {
508e221f
LL
267 error_report("%s: Couldn't nop node %s: %s", __func__, node_path,
268 fdt_strerror(r));
ccbcfedd
AG
269 exit(1);
270 }
d69a8e63 271
ccbcfedd 272 return r;
d69a8e63 273}
80ad7816 274
5a4348d1 275int qemu_fdt_add_subnode(void *fdt, const char *name)
80ad7816 276{
80ad7816
AG
277 char *dupname = g_strdup(name);
278 char *basename = strrchr(dupname, '/');
279 int retval;
c640d088 280 int parent = 0;
80ad7816
AG
281
282 if (!basename) {
bff39b63 283 g_free(dupname);
80ad7816
AG
284 return -1;
285 }
286
287 basename[0] = '\0';
288 basename++;
289
c640d088
AG
290 if (dupname[0]) {
291 parent = findnode_nofail(fdt, dupname);
292 }
293
294 retval = fdt_add_subnode(fdt, parent, basename);
ccbcfedd 295 if (retval < 0) {
508e221f
LL
296 error_report("FDT: Failed to create subnode %s: %s", name,
297 fdt_strerror(retval));
ccbcfedd 298 exit(1);
80ad7816
AG
299 }
300
80ad7816
AG
301 g_free(dupname);
302 return retval;
303}
71193433 304
5a4348d1 305void qemu_fdt_dumpdtb(void *fdt, int size)
71193433 306{
2ff3de68 307 const char *dumpdtb = qemu_opt_get(qemu_get_machine_opts(), "dumpdtb");
71193433 308
2ff3de68
MA
309 if (dumpdtb) {
310 /* Dump the dtb to a file and quit */
311 exit(g_file_set_contents(dumpdtb, fdt, size, NULL) ? 0 : 1);
312 }
71193433 313}
97c38f8c 314
5a4348d1
PC
315int qemu_fdt_setprop_sized_cells_from_array(void *fdt,
316 const char *node_path,
317 const char *property,
318 int numvalues,
319 uint64_t *values)
97c38f8c
PM
320{
321 uint32_t *propcells;
322 uint64_t value;
323 int cellnum, vnum, ncells;
324 uint32_t hival;
2bf9febc 325 int ret;
97c38f8c
PM
326
327 propcells = g_new0(uint32_t, numvalues * 2);
328
329 cellnum = 0;
330 for (vnum = 0; vnum < numvalues; vnum++) {
331 ncells = values[vnum * 2];
332 if (ncells != 1 && ncells != 2) {
2bf9febc
SF
333 ret = -1;
334 goto out;
97c38f8c
PM
335 }
336 value = values[vnum * 2 + 1];
337 hival = cpu_to_be32(value >> 32);
338 if (ncells > 1) {
339 propcells[cellnum++] = hival;
340 } else if (hival != 0) {
2bf9febc
SF
341 ret = -1;
342 goto out;
97c38f8c
PM
343 }
344 propcells[cellnum++] = cpu_to_be32(value);
345 }
346
2bf9febc
SF
347 ret = qemu_fdt_setprop(fdt, node_path, property, propcells,
348 cellnum * sizeof(uint32_t));
349out:
350 g_free(propcells);
351 return ret;
97c38f8c 352}