]> Git Repo - qemu.git/blame - hw/nvram/fw_cfg.c
fw_cfg: add generic non-DMA read method
[qemu.git] / hw / nvram / fw_cfg.c
CommitLineData
3cce6243
BS
1/*
2 * QEMU Firmware configuration device emulation
3 *
4 * Copyright (c) 2008 Gleb Natapov
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
83c9f4ca 24#include "hw/hw.h"
9c17d615 25#include "sysemu/sysemu.h"
a4c0d1de 26#include "sysemu/dma.h"
0d09e41a
PB
27#include "hw/isa/isa.h"
28#include "hw/nvram/fw_cfg.h"
83c9f4ca 29#include "hw/sysbus.h"
f6e35343 30#include "trace.h"
1de7afc9
PB
31#include "qemu/error-report.h"
32#include "qemu/config-file.h"
3cce6243 33
a4c0d1de 34#define FW_CFG_CTL_SIZE 2
600c60b7
MT
35#define FW_CFG_NAME "fw_cfg"
36#define FW_CFG_PATH "/machine/" FW_CFG_NAME
5712db6a
LE
37
38#define TYPE_FW_CFG "fw_cfg"
39#define TYPE_FW_CFG_IO "fw_cfg_io"
40#define TYPE_FW_CFG_MEM "fw_cfg_mem"
41
42#define FW_CFG(obj) OBJECT_CHECK(FWCfgState, (obj), TYPE_FW_CFG)
43#define FW_CFG_IO(obj) OBJECT_CHECK(FWCfgIoState, (obj), TYPE_FW_CFG_IO)
44#define FW_CFG_MEM(obj) OBJECT_CHECK(FWCfgMemState, (obj), TYPE_FW_CFG_MEM)
3cce6243 45
a4c0d1de
MM
46/* FW_CFG_VERSION bits */
47#define FW_CFG_VERSION 0x01
48#define FW_CFG_VERSION_DMA 0x02
49
50/* FW_CFG_DMA_CONTROL bits */
51#define FW_CFG_DMA_CTL_ERROR 0x01
52#define FW_CFG_DMA_CTL_READ 0x02
53#define FW_CFG_DMA_CTL_SKIP 0x04
54#define FW_CFG_DMA_CTL_SELECT 0x08
55
2cc06a88
KC
56#define FW_CFG_DMA_SIGNATURE 0x51454d5520434647ULL /* "QEMU CFG" */
57
b96ae2da 58typedef struct FWCfgEntry {
ff06108b 59 uint32_t len;
3cce6243
BS
60 uint8_t *data;
61 void *callback_opaque;
d87072ce 62 FWCfgReadCallback read_callback;
3cce6243
BS
63} FWCfgEntry;
64
b96ae2da 65struct FWCfgState {
2ce92a11
HT
66 /*< private >*/
67 SysBusDevice parent_obj;
68 /*< public >*/
69
3cce6243 70 FWCfgEntry entries[2][FW_CFG_MAX_ENTRY];
abe147e0 71 FWCfgFiles *files;
3cce6243 72 uint16_t cur_entry;
ff06108b 73 uint32_t cur_offset;
962630f2 74 Notifier machine_ready;
a4c0d1de
MM
75
76 bool dma_enabled;
77 dma_addr_t dma_addr;
78 AddressSpace *dma_as;
79 MemoryRegion dma_iomem;
c2b5bda4 80};
3cce6243 81
5712db6a
LE
82struct FWCfgIoState {
83 /*< private >*/
84 FWCfgState parent_obj;
85 /*< public >*/
86
87 MemoryRegion comb_iomem;
a4c0d1de 88 uint32_t iobase, dma_iobase;
5712db6a
LE
89};
90
91struct FWCfgMemState {
92 /*< private >*/
93 FWCfgState parent_obj;
94 /*< public >*/
95
96 MemoryRegion ctl_iomem, data_iomem;
cfaadf0e
LE
97 uint32_t data_width;
98 MemoryRegionOps wide_data_ops;
5712db6a
LE
99};
100
3d3b8303
WX
101#define JPG_FILE 0
102#define BMP_FILE 1
103
3d1bba20 104static char *read_splashfile(char *filename, gsize *file_sizep,
d09acb9b 105 int *file_typep)
3d3b8303 106{
9477c87e
PB
107 GError *err = NULL;
108 gboolean res;
109 gchar *content;
9f8863eb
MA
110 int file_type;
111 unsigned int filehead;
3d3b8303
WX
112 int bmp_bpp;
113
d09acb9b 114 res = g_file_get_contents(filename, &content, file_sizep, &err);
9477c87e
PB
115 if (res == FALSE) {
116 error_report("failed to read splash file '%s'", filename);
117 g_error_free(err);
118 return NULL;
3d3b8303 119 }
9477c87e 120
3d3b8303 121 /* check file size */
9477c87e
PB
122 if (*file_sizep < 30) {
123 goto error;
3d3b8303 124 }
9477c87e 125
3d3b8303 126 /* check magic ID */
9477c87e
PB
127 filehead = ((content[0] & 0xff) + (content[1] << 8)) & 0xffff;
128 if (filehead == 0xd8ff) {
3d3b8303 129 file_type = JPG_FILE;
9477c87e
PB
130 } else if (filehead == 0x4d42) {
131 file_type = BMP_FILE;
3d3b8303 132 } else {
9477c87e 133 goto error;
3d3b8303 134 }
9477c87e 135
3d3b8303
WX
136 /* check BMP bpp */
137 if (file_type == BMP_FILE) {
9477c87e 138 bmp_bpp = (content[28] + (content[29] << 8)) & 0xffff;
3d3b8303 139 if (bmp_bpp != 24) {
9477c87e 140 goto error;
3d3b8303
WX
141 }
142 }
9477c87e 143
3d3b8303 144 /* return values */
3d3b8303 145 *file_typep = file_type;
9477c87e
PB
146
147 return content;
148
149error:
150 error_report("splash file '%s' format not recognized; must be JPEG "
151 "or 24 bit BMP", filename);
152 g_free(content);
153 return NULL;
3d3b8303
WX
154}
155
156static void fw_cfg_bootsplash(FWCfgState *s)
157{
158 int boot_splash_time = -1;
159 const char *boot_splash_filename = NULL;
160 char *p;
9477c87e 161 char *filename, *file_data;
3d1bba20 162 gsize file_size;
9f8863eb 163 int file_type;
3d3b8303
WX
164 const char *temp;
165
166 /* get user configuration */
167 QemuOptsList *plist = qemu_find_opts("boot-opts");
168 QemuOpts *opts = QTAILQ_FIRST(&plist->head);
169 if (opts != NULL) {
170 temp = qemu_opt_get(opts, "splash");
171 if (temp != NULL) {
172 boot_splash_filename = temp;
173 }
174 temp = qemu_opt_get(opts, "splash-time");
175 if (temp != NULL) {
176 p = (char *)temp;
177 boot_splash_time = strtol(p, (char **)&p, 10);
178 }
179 }
180
181 /* insert splash time if user configurated */
182 if (boot_splash_time >= 0) {
183 /* validate the input */
184 if (boot_splash_time > 0xffff) {
185 error_report("splash time is big than 65535, force it to 65535.");
186 boot_splash_time = 0xffff;
187 }
188 /* use little endian format */
189 qemu_extra_params_fw[0] = (uint8_t)(boot_splash_time & 0xff);
190 qemu_extra_params_fw[1] = (uint8_t)((boot_splash_time >> 8) & 0xff);
191 fw_cfg_add_file(s, "etc/boot-menu-wait", qemu_extra_params_fw, 2);
192 }
193
194 /* insert splash file if user configurated */
195 if (boot_splash_filename != NULL) {
196 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, boot_splash_filename);
197 if (filename == NULL) {
198 error_report("failed to find file '%s'.", boot_splash_filename);
199 return;
200 }
9477c87e
PB
201
202 /* loading file data */
203 file_data = read_splashfile(filename, &file_size, &file_type);
204 if (file_data == NULL) {
7267c094 205 g_free(filename);
3d3b8303
WX
206 return;
207 }
ef1e1e07 208 g_free(boot_splash_filedata);
9477c87e 209 boot_splash_filedata = (uint8_t *)file_data;
3d3b8303 210 boot_splash_filedata_size = file_size;
9477c87e 211
3d3b8303
WX
212 /* insert data */
213 if (file_type == JPG_FILE) {
214 fw_cfg_add_file(s, "bootsplash.jpg",
215 boot_splash_filedata, boot_splash_filedata_size);
216 } else {
217 fw_cfg_add_file(s, "bootsplash.bmp",
218 boot_splash_filedata, boot_splash_filedata_size);
219 }
7267c094 220 g_free(filename);
3d3b8303
WX
221 }
222}
223
ac05f349
AK
224static void fw_cfg_reboot(FWCfgState *s)
225{
226 int reboot_timeout = -1;
227 char *p;
228 const char *temp;
229
230 /* get user configuration */
231 QemuOptsList *plist = qemu_find_opts("boot-opts");
232 QemuOpts *opts = QTAILQ_FIRST(&plist->head);
233 if (opts != NULL) {
234 temp = qemu_opt_get(opts, "reboot-timeout");
235 if (temp != NULL) {
236 p = (char *)temp;
237 reboot_timeout = strtol(p, (char **)&p, 10);
238 }
239 }
240 /* validate the input */
241 if (reboot_timeout > 0xffff) {
242 error_report("reboot timeout is larger than 65535, force it to 65535.");
243 reboot_timeout = 0xffff;
244 }
245 fw_cfg_add_file(s, "etc/boot-fail-wait", g_memdup(&reboot_timeout, 4), 4);
246}
247
3cce6243
BS
248static void fw_cfg_write(FWCfgState *s, uint8_t value)
249{
023e3148 250 /* nothing, write support removed in QEMU v2.4+ */
3cce6243
BS
251}
252
253static int fw_cfg_select(FWCfgState *s, uint16_t key)
254{
3bef7e8a
GS
255 int arch, ret;
256 FWCfgEntry *e;
3cce6243
BS
257
258 s->cur_offset = 0;
259 if ((key & FW_CFG_ENTRY_MASK) >= FW_CFG_MAX_ENTRY) {
260 s->cur_entry = FW_CFG_INVALID;
261 ret = 0;
262 } else {
263 s->cur_entry = key;
264 ret = 1;
3bef7e8a
GS
265 /* entry successfully selected, now run callback if present */
266 arch = !!(key & FW_CFG_ARCH_LOCAL);
267 e = &s->entries[arch][key & FW_CFG_ENTRY_MASK];
268 if (e->read_callback) {
3f8752b4 269 e->read_callback(e->callback_opaque);
3bef7e8a 270 }
3cce6243
BS
271 }
272
f6e35343 273 trace_fw_cfg_select(s, key, ret);
3cce6243
BS
274 return ret;
275}
276
38bf2093
GS
277static uint64_t fw_cfg_data_read(void *opaque, hwaddr addr, unsigned size)
278{
279 FWCfgState *s = opaque;
280 int arch = !!(s->cur_entry & FW_CFG_ARCH_LOCAL);
281 FWCfgEntry *e = (s->cur_entry == FW_CFG_INVALID) ? NULL :
282 &s->entries[arch][s->cur_entry & FW_CFG_ENTRY_MASK];
283 uint64_t value = 0;
284
285 assert(size > 0 && size <= sizeof(value));
286 if (s->cur_entry != FW_CFG_INVALID && e->data && s->cur_offset < e->len) {
287 /* The least significant 'size' bytes of the return value are
288 * expected to contain a string preserving portion of the item
289 * data, padded with zeros on the right in case we run out early.
290 * In technical terms, we're composing the host-endian representation
291 * of the big endian interpretation of the fw_cfg string.
292 */
293 do {
294 value = (value << 8) | e->data[s->cur_offset++];
295 } while (--size && s->cur_offset < e->len);
296 /* If size is still not zero, we *did* run out early, so continue
297 * left-shifting, to add the appropriate number of padding zeros
298 * on the right.
299 */
300 value <<= 8 * size;
301 }
302
303 trace_fw_cfg_read(s, value);
304 return value;
305}
306
3cce6243
BS
307static uint8_t fw_cfg_read(FWCfgState *s)
308{
309 int arch = !!(s->cur_entry & FW_CFG_ARCH_LOCAL);
66f8fd9d
GS
310 FWCfgEntry *e = (s->cur_entry == FW_CFG_INVALID) ? NULL :
311 &s->entries[arch][s->cur_entry & FW_CFG_ENTRY_MASK];
3cce6243
BS
312 uint8_t ret;
313
314 if (s->cur_entry == FW_CFG_INVALID || !e->data || s->cur_offset >= e->len)
315 ret = 0;
d87072ce 316 else {
3cce6243 317 ret = e->data[s->cur_offset++];
d87072ce 318 }
3cce6243 319
f6e35343 320 trace_fw_cfg_read(s, ret);
3cce6243
BS
321 return ret;
322}
323
a8170e5e 324static void fw_cfg_data_mem_write(void *opaque, hwaddr addr,
561e1827 325 uint64_t value, unsigned size)
3cce6243 326{
cfaadf0e 327 FWCfgState *s = opaque;
36b62ae6 328 unsigned i = size;
cfaadf0e 329
36b62ae6
LE
330 do {
331 fw_cfg_write(s, value >> (8 * --i));
332 } while (i);
cfaadf0e
LE
333}
334
a4c0d1de
MM
335static void fw_cfg_dma_transfer(FWCfgState *s)
336{
337 dma_addr_t len;
338 FWCfgDmaAccess dma;
339 int arch;
340 FWCfgEntry *e;
341 int read;
342 dma_addr_t dma_addr;
343
344 /* Reset the address before the next access */
345 dma_addr = s->dma_addr;
346 s->dma_addr = 0;
347
348 if (dma_memory_read(s->dma_as, dma_addr, &dma, sizeof(dma))) {
349 stl_be_dma(s->dma_as, dma_addr + offsetof(FWCfgDmaAccess, control),
350 FW_CFG_DMA_CTL_ERROR);
351 return;
352 }
353
354 dma.address = be64_to_cpu(dma.address);
355 dma.length = be32_to_cpu(dma.length);
356 dma.control = be32_to_cpu(dma.control);
357
358 if (dma.control & FW_CFG_DMA_CTL_SELECT) {
359 fw_cfg_select(s, dma.control >> 16);
360 }
361
362 arch = !!(s->cur_entry & FW_CFG_ARCH_LOCAL);
66f8fd9d
GS
363 e = (s->cur_entry == FW_CFG_INVALID) ? NULL :
364 &s->entries[arch][s->cur_entry & FW_CFG_ENTRY_MASK];
a4c0d1de
MM
365
366 if (dma.control & FW_CFG_DMA_CTL_READ) {
367 read = 1;
368 } else if (dma.control & FW_CFG_DMA_CTL_SKIP) {
369 read = 0;
370 } else {
371 dma.length = 0;
372 }
373
374 dma.control = 0;
375
376 while (dma.length > 0 && !(dma.control & FW_CFG_DMA_CTL_ERROR)) {
377 if (s->cur_entry == FW_CFG_INVALID || !e->data ||
378 s->cur_offset >= e->len) {
379 len = dma.length;
380
381 /* If the access is not a read access, it will be a skip access,
382 * tested before.
383 */
384 if (read) {
385 if (dma_memory_set(s->dma_as, dma.address, 0, len)) {
386 dma.control |= FW_CFG_DMA_CTL_ERROR;
387 }
388 }
389
390 } else {
391 if (dma.length <= (e->len - s->cur_offset)) {
392 len = dma.length;
393 } else {
394 len = (e->len - s->cur_offset);
395 }
396
a4c0d1de
MM
397 /* If the access is not a read access, it will be a skip access,
398 * tested before.
399 */
400 if (read) {
401 if (dma_memory_write(s->dma_as, dma.address,
402 &e->data[s->cur_offset], len)) {
403 dma.control |= FW_CFG_DMA_CTL_ERROR;
404 }
405 }
406
407 s->cur_offset += len;
408 }
409
410 dma.address += len;
411 dma.length -= len;
412
413 }
414
415 stl_be_dma(s->dma_as, dma_addr + offsetof(FWCfgDmaAccess, control),
416 dma.control);
417
418 trace_fw_cfg_read(s, 0);
419}
420
2cc06a88
KC
421static uint64_t fw_cfg_dma_mem_read(void *opaque, hwaddr addr,
422 unsigned size)
423{
424 /* Return a signature value (and handle various read sizes) */
425 return extract64(FW_CFG_DMA_SIGNATURE, (8 - addr - size) * 8, size * 8);
426}
427
a4c0d1de
MM
428static void fw_cfg_dma_mem_write(void *opaque, hwaddr addr,
429 uint64_t value, unsigned size)
430{
431 FWCfgState *s = opaque;
432
433 if (size == 4) {
434 if (addr == 0) {
435 /* FWCfgDmaAccess high address */
436 s->dma_addr = value << 32;
437 } else if (addr == 4) {
438 /* FWCfgDmaAccess low address */
439 s->dma_addr |= value;
440 fw_cfg_dma_transfer(s);
441 }
442 } else if (size == 8 && addr == 0) {
443 s->dma_addr = value;
444 fw_cfg_dma_transfer(s);
445 }
446}
447
448static bool fw_cfg_dma_mem_valid(void *opaque, hwaddr addr,
449 unsigned size, bool is_write)
450{
2cc06a88
KC
451 return !is_write || ((size == 4 && (addr == 0 || addr == 4)) ||
452 (size == 8 && addr == 0));
a4c0d1de
MM
453}
454
cfaadf0e
LE
455static bool fw_cfg_data_mem_valid(void *opaque, hwaddr addr,
456 unsigned size, bool is_write)
457{
458 return addr == 0;
3cce6243
BS
459}
460
a8170e5e 461static void fw_cfg_ctl_mem_write(void *opaque, hwaddr addr,
561e1827 462 uint64_t value, unsigned size)
3cce6243
BS
463{
464 fw_cfg_select(opaque, (uint16_t)value);
465}
466
a8170e5e 467static bool fw_cfg_ctl_mem_valid(void *opaque, hwaddr addr,
561e1827 468 unsigned size, bool is_write)
3cce6243 469{
561e1827 470 return is_write && size == 2;
3cce6243
BS
471}
472
a8170e5e 473static uint64_t fw_cfg_comb_read(void *opaque, hwaddr addr,
561e1827 474 unsigned size)
3cce6243 475{
561e1827 476 return fw_cfg_read(opaque);
3cce6243
BS
477}
478
a8170e5e 479static void fw_cfg_comb_write(void *opaque, hwaddr addr,
561e1827 480 uint64_t value, unsigned size)
3cce6243 481{
561e1827
AK
482 switch (size) {
483 case 1:
484 fw_cfg_write(opaque, (uint8_t)value);
485 break;
486 case 2:
487 fw_cfg_select(opaque, (uint16_t)value);
488 break;
489 }
3cce6243
BS
490}
491
a8170e5e 492static bool fw_cfg_comb_valid(void *opaque, hwaddr addr,
561e1827
AK
493 unsigned size, bool is_write)
494{
495 return (size == 1) || (is_write && size == 2);
496}
3cce6243 497
561e1827
AK
498static const MemoryRegionOps fw_cfg_ctl_mem_ops = {
499 .write = fw_cfg_ctl_mem_write,
d789c845 500 .endianness = DEVICE_BIG_ENDIAN,
561e1827 501 .valid.accepts = fw_cfg_ctl_mem_valid,
3cce6243
BS
502};
503
561e1827 504static const MemoryRegionOps fw_cfg_data_mem_ops = {
38bf2093 505 .read = fw_cfg_data_read,
561e1827 506 .write = fw_cfg_data_mem_write,
d789c845 507 .endianness = DEVICE_BIG_ENDIAN,
561e1827
AK
508 .valid = {
509 .min_access_size = 1,
510 .max_access_size = 1,
cfaadf0e 511 .accepts = fw_cfg_data_mem_valid,
561e1827 512 },
3cce6243
BS
513};
514
561e1827
AK
515static const MemoryRegionOps fw_cfg_comb_mem_ops = {
516 .read = fw_cfg_comb_read,
517 .write = fw_cfg_comb_write,
6fdf98f2 518 .endianness = DEVICE_LITTLE_ENDIAN,
561e1827 519 .valid.accepts = fw_cfg_comb_valid,
3cce6243
BS
520};
521
a4c0d1de 522static const MemoryRegionOps fw_cfg_dma_mem_ops = {
2cc06a88 523 .read = fw_cfg_dma_mem_read,
a4c0d1de
MM
524 .write = fw_cfg_dma_mem_write,
525 .endianness = DEVICE_BIG_ENDIAN,
526 .valid.accepts = fw_cfg_dma_mem_valid,
527 .valid.max_access_size = 8,
528 .impl.max_access_size = 8,
529};
530
3a5c16fc 531static void fw_cfg_reset(DeviceState *d)
3cce6243 532{
2ce92a11 533 FWCfgState *s = FW_CFG(d);
3cce6243 534
3bef7e8a
GS
535 /* we never register a read callback for FW_CFG_SIGNATURE */
536 fw_cfg_select(s, FW_CFG_SIGNATURE);
3cce6243
BS
537}
538
ff06108b
JQ
539/* Save restore 32 bit int as uint16_t
540 This is a Big hack, but it is how the old state did it.
541 Or we broke compatibility in the state, or we can't use struct tm
542 */
543
544static int get_uint32_as_uint16(QEMUFile *f, void *pv, size_t size)
545{
546 uint32_t *v = pv;
547 *v = qemu_get_be16(f);
548 return 0;
549}
550
551static void put_unused(QEMUFile *f, void *pv, size_t size)
552{
66c80e75 553 fprintf(stderr, "uint32_as_uint16 is only used for backward compatibility.\n");
ff06108b
JQ
554 fprintf(stderr, "This functions shouldn't be called.\n");
555}
556
d05ac8fa 557static const VMStateInfo vmstate_hack_uint32_as_uint16 = {
ff06108b
JQ
558 .name = "int32_as_uint16",
559 .get = get_uint32_as_uint16,
560 .put = put_unused,
561};
562
563#define VMSTATE_UINT16_HACK(_f, _s, _t) \
564 VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_hack_uint32_as_uint16, uint32_t)
565
566
567static bool is_version_1(void *opaque, int version_id)
568{
569 return version_id == 1;
570}
571
a4c0d1de
MM
572static bool fw_cfg_dma_enabled(void *opaque)
573{
574 FWCfgState *s = opaque;
575
576 return s->dma_enabled;
577}
578
579static const VMStateDescription vmstate_fw_cfg_dma = {
580 .name = "fw_cfg/dma",
581 .needed = fw_cfg_dma_enabled,
582 .fields = (VMStateField[]) {
583 VMSTATE_UINT64(dma_addr, FWCfgState),
584 VMSTATE_END_OF_LIST()
585 },
586};
587
7d2edd40
JQ
588static const VMStateDescription vmstate_fw_cfg = {
589 .name = "fw_cfg",
ff06108b 590 .version_id = 2,
7d2edd40 591 .minimum_version_id = 1,
d49805ae 592 .fields = (VMStateField[]) {
7d2edd40 593 VMSTATE_UINT16(cur_entry, FWCfgState),
ff06108b
JQ
594 VMSTATE_UINT16_HACK(cur_offset, FWCfgState, is_version_1),
595 VMSTATE_UINT32_V(cur_offset, FWCfgState, 2),
7d2edd40 596 VMSTATE_END_OF_LIST()
a4c0d1de
MM
597 },
598 .subsections = (const VMStateDescription*[]) {
599 &vmstate_fw_cfg_dma,
600 NULL,
7d2edd40
JQ
601 }
602};
3cce6243 603
d87072ce
MT
604static void fw_cfg_add_bytes_read_callback(FWCfgState *s, uint16_t key,
605 FWCfgReadCallback callback,
606 void *callback_opaque,
607 void *data, size_t len)
3cce6243 608{
3cce6243
BS
609 int arch = !!(key & FW_CFG_ARCH_LOCAL);
610
611 key &= FW_CFG_ENTRY_MASK;
612
089da572 613 assert(key < FW_CFG_MAX_ENTRY && len < UINT32_MAX);
0f9b2141 614 assert(s->entries[arch][key].data == NULL); /* avoid key conflict */
3cce6243
BS
615
616 s->entries[arch][key].data = data;
089da572 617 s->entries[arch][key].len = (uint32_t)len;
d87072ce
MT
618 s->entries[arch][key].read_callback = callback;
619 s->entries[arch][key].callback_opaque = callback_opaque;
620}
621
bdbb5b17
GA
622static void *fw_cfg_modify_bytes_read(FWCfgState *s, uint16_t key,
623 void *data, size_t len)
624{
625 void *ptr;
626 int arch = !!(key & FW_CFG_ARCH_LOCAL);
627
628 key &= FW_CFG_ENTRY_MASK;
629
630 assert(key < FW_CFG_MAX_ENTRY && len < UINT32_MAX);
631
632 /* return the old data to the function caller, avoid memory leak */
633 ptr = s->entries[arch][key].data;
634 s->entries[arch][key].data = data;
635 s->entries[arch][key].len = len;
636 s->entries[arch][key].callback_opaque = NULL;
bdbb5b17
GA
637
638 return ptr;
639}
640
d87072ce
MT
641void fw_cfg_add_bytes(FWCfgState *s, uint16_t key, void *data, size_t len)
642{
643 fw_cfg_add_bytes_read_callback(s, key, NULL, NULL, data, len);
3cce6243
BS
644}
645
44687f75
MA
646void fw_cfg_add_string(FWCfgState *s, uint16_t key, const char *value)
647{
648 size_t sz = strlen(value) + 1;
649
e7ae771f 650 fw_cfg_add_bytes(s, key, g_memdup(value, sz), sz);
44687f75
MA
651}
652
4cad3867 653void fw_cfg_add_i16(FWCfgState *s, uint16_t key, uint16_t value)
3cce6243
BS
654{
655 uint16_t *copy;
656
7267c094 657 copy = g_malloc(sizeof(value));
3cce6243 658 *copy = cpu_to_le16(value);
089da572 659 fw_cfg_add_bytes(s, key, copy, sizeof(value));
3cce6243
BS
660}
661
1edd34b6
GS
662void fw_cfg_modify_i16(FWCfgState *s, uint16_t key, uint16_t value)
663{
664 uint16_t *copy, *old;
665
666 copy = g_malloc(sizeof(value));
667 *copy = cpu_to_le16(value);
668 old = fw_cfg_modify_bytes_read(s, key, copy, sizeof(value));
669 g_free(old);
670}
671
4cad3867 672void fw_cfg_add_i32(FWCfgState *s, uint16_t key, uint32_t value)
3cce6243
BS
673{
674 uint32_t *copy;
675
7267c094 676 copy = g_malloc(sizeof(value));
3cce6243 677 *copy = cpu_to_le32(value);
089da572 678 fw_cfg_add_bytes(s, key, copy, sizeof(value));
3cce6243
BS
679}
680
4cad3867 681void fw_cfg_add_i64(FWCfgState *s, uint16_t key, uint64_t value)
3cce6243
BS
682{
683 uint64_t *copy;
684
7267c094 685 copy = g_malloc(sizeof(value));
3cce6243 686 *copy = cpu_to_le64(value);
089da572 687 fw_cfg_add_bytes(s, key, copy, sizeof(value));
3cce6243
BS
688}
689
d87072ce
MT
690void fw_cfg_add_file_callback(FWCfgState *s, const char *filename,
691 FWCfgReadCallback callback, void *callback_opaque,
692 void *data, size_t len)
abe147e0 693{
de9352bc 694 int i, index;
089da572 695 size_t dsize;
abe147e0
GH
696
697 if (!s->files) {
089da572 698 dsize = sizeof(uint32_t) + sizeof(FWCfgFile) * FW_CFG_FILE_SLOTS;
7267c094 699 s->files = g_malloc0(dsize);
089da572 700 fw_cfg_add_bytes(s, FW_CFG_FILE_DIR, s->files, dsize);
abe147e0
GH
701 }
702
703 index = be32_to_cpu(s->files->count);
4cad3867 704 assert(index < FW_CFG_FILE_SLOTS);
abe147e0 705
de1f34cb
GN
706 pstrcpy(s->files->f[index].name, sizeof(s->files->f[index].name),
707 filename);
de9352bc
GH
708 for (i = 0; i < index; i++) {
709 if (strcmp(s->files->f[index].name, s->files->f[i].name) == 0) {
0eb973f9
GS
710 error_report("duplicate fw_cfg file name: %s",
711 s->files->f[index].name);
712 exit(1);
de9352bc 713 }
abe147e0 714 }
de9352bc 715
0eb973f9
GS
716 fw_cfg_add_bytes_read_callback(s, FW_CFG_FILE_FIRST + index,
717 callback, callback_opaque, data, len);
718
abe147e0
GH
719 s->files->f[index].size = cpu_to_be32(len);
720 s->files->f[index].select = cpu_to_be16(FW_CFG_FILE_FIRST + index);
f6e35343 721 trace_fw_cfg_add_file(s, index, s->files->f[index].name, len);
abe147e0
GH
722
723 s->files->count = cpu_to_be32(index+1);
abe147e0
GH
724}
725
d87072ce
MT
726void fw_cfg_add_file(FWCfgState *s, const char *filename,
727 void *data, size_t len)
728{
729 fw_cfg_add_file_callback(s, filename, NULL, NULL, data, len);
730}
731
bdbb5b17
GA
732void *fw_cfg_modify_file(FWCfgState *s, const char *filename,
733 void *data, size_t len)
734{
735 int i, index;
f3b37668 736 void *ptr = NULL;
bdbb5b17
GA
737
738 assert(s->files);
739
740 index = be32_to_cpu(s->files->count);
741 assert(index < FW_CFG_FILE_SLOTS);
742
743 for (i = 0; i < index; i++) {
744 if (strcmp(filename, s->files->f[i].name) == 0) {
f3b37668
GA
745 ptr = fw_cfg_modify_bytes_read(s, FW_CFG_FILE_FIRST + i,
746 data, len);
747 s->files->f[i].size = cpu_to_be32(len);
748 return ptr;
bdbb5b17
GA
749 }
750 }
751 /* add new one */
752 fw_cfg_add_file_callback(s, filename, NULL, NULL, data, len);
753 return NULL;
754}
755
756static void fw_cfg_machine_reset(void *opaque)
962630f2 757{
bdbb5b17 758 void *ptr;
0e7a7592 759 size_t len;
bdbb5b17 760 FWCfgState *s = opaque;
30e32af7 761 char *bootindex = get_boot_devices_list(&len, false);
962630f2 762
bdbb5b17
GA
763 ptr = fw_cfg_modify_file(s, "bootorder", (uint8_t *)bootindex, len);
764 g_free(ptr);
765}
766
767static void fw_cfg_machine_ready(struct Notifier *n, void *data)
768{
769 FWCfgState *s = container_of(n, FWCfgState, machine_ready);
770 qemu_register_reset(fw_cfg_machine_reset, s);
962630f2
GN
771}
772
3cce6243 773
3a5c16fc 774
5712db6a
LE
775static void fw_cfg_init1(DeviceState *dev)
776{
777 FWCfgState *s = FW_CFG(dev);
3cce6243 778
cac12210
MT
779 assert(!object_resolve_path(FW_CFG_PATH, NULL));
780
781 object_property_add_child(qdev_get_machine(), FW_CFG_NAME, OBJECT(s), NULL);
10a584b2
HT
782
783 qdev_init_nofail(dev);
784
089da572 785 fw_cfg_add_bytes(s, FW_CFG_SIGNATURE, (char *)"QEMU", 4);
084a197a 786 fw_cfg_add_bytes(s, FW_CFG_UUID, qemu_uuid, 16);
993fbfdb 787 fw_cfg_add_i16(s, FW_CFG_NOGRAPHIC, (uint16_t)(display_type == DT_NOGRAPHIC));
905fdcb5 788 fw_cfg_add_i16(s, FW_CFG_NB_CPUS, (uint16_t)smp_cpus);
95387491 789 fw_cfg_add_i16(s, FW_CFG_BOOT_MENU, (uint16_t)boot_menu);
3d3b8303 790 fw_cfg_bootsplash(s);
ac05f349 791 fw_cfg_reboot(s);
962630f2
GN
792
793 s->machine_ready.notify = fw_cfg_machine_ready;
794 qemu_add_machine_init_done_notifier(&s->machine_ready);
3cce6243 795}
3a5c16fc 796
a4c0d1de
MM
797FWCfgState *fw_cfg_init_io_dma(uint32_t iobase, uint32_t dma_iobase,
798 AddressSpace *dma_as)
3a5c16fc 799{
5712db6a 800 DeviceState *dev;
a4c0d1de
MM
801 FWCfgState *s;
802 uint32_t version = FW_CFG_VERSION;
803 bool dma_enabled = dma_iobase && dma_as;
3a5c16fc 804
5712db6a
LE
805 dev = qdev_create(NULL, TYPE_FW_CFG_IO);
806 qdev_prop_set_uint32(dev, "iobase", iobase);
a4c0d1de
MM
807 qdev_prop_set_uint32(dev, "dma_iobase", dma_iobase);
808 qdev_prop_set_bit(dev, "dma_enabled", dma_enabled);
809
5712db6a 810 fw_cfg_init1(dev);
a4c0d1de
MM
811 s = FW_CFG(dev);
812
813 if (dma_enabled) {
814 /* 64 bits for the address field */
815 s->dma_as = dma_as;
816 s->dma_addr = 0;
817
818 version |= FW_CFG_VERSION_DMA;
819 }
820
821 fw_cfg_add_i32(s, FW_CFG_ID, version);
5712db6a 822
a4c0d1de
MM
823 return s;
824}
825
826FWCfgState *fw_cfg_init_io(uint32_t iobase)
827{
828 return fw_cfg_init_io_dma(iobase, 0, NULL);
56383955
HT
829}
830
a4c0d1de
MM
831FWCfgState *fw_cfg_init_mem_wide(hwaddr ctl_addr,
832 hwaddr data_addr, uint32_t data_width,
833 hwaddr dma_addr, AddressSpace *dma_as)
56383955 834{
5712db6a
LE
835 DeviceState *dev;
836 SysBusDevice *sbd;
a4c0d1de
MM
837 FWCfgState *s;
838 uint32_t version = FW_CFG_VERSION;
839 bool dma_enabled = dma_addr && dma_as;
56383955 840
5712db6a 841 dev = qdev_create(NULL, TYPE_FW_CFG_MEM);
6c87e3d5 842 qdev_prop_set_uint32(dev, "data_width", data_width);
a4c0d1de 843 qdev_prop_set_bit(dev, "dma_enabled", dma_enabled);
cfaadf0e 844
5712db6a
LE
845 fw_cfg_init1(dev);
846
847 sbd = SYS_BUS_DEVICE(dev);
848 sysbus_mmio_map(sbd, 0, ctl_addr);
849 sysbus_mmio_map(sbd, 1, data_addr);
850
a4c0d1de
MM
851 s = FW_CFG(dev);
852
853 if (dma_enabled) {
854 s->dma_as = dma_as;
855 s->dma_addr = 0;
856 sysbus_mmio_map(sbd, 2, dma_addr);
857 version |= FW_CFG_VERSION_DMA;
858 }
859
860 fw_cfg_add_i32(s, FW_CFG_ID, version);
861
862 return s;
5712db6a
LE
863}
864
6c87e3d5
LE
865FWCfgState *fw_cfg_init_mem(hwaddr ctl_addr, hwaddr data_addr)
866{
867 return fw_cfg_init_mem_wide(ctl_addr, data_addr,
a4c0d1de
MM
868 fw_cfg_data_mem_ops.valid.max_access_size,
869 0, NULL);
6c87e3d5
LE
870}
871
5712db6a 872
600c60b7
MT
873FWCfgState *fw_cfg_find(void)
874{
2ce92a11 875 return FW_CFG(object_resolve_path(FW_CFG_PATH, NULL));
600c60b7
MT
876}
877
999e12bb
AL
878static void fw_cfg_class_init(ObjectClass *klass, void *data)
879{
39bffca2 880 DeviceClass *dc = DEVICE_CLASS(klass);
999e12bb 881
39bffca2
AL
882 dc->reset = fw_cfg_reset;
883 dc->vmsd = &vmstate_fw_cfg;
999e12bb
AL
884}
885
8c43a6f0 886static const TypeInfo fw_cfg_info = {
600c60b7 887 .name = TYPE_FW_CFG,
39bffca2
AL
888 .parent = TYPE_SYS_BUS_DEVICE,
889 .instance_size = sizeof(FWCfgState),
890 .class_init = fw_cfg_class_init,
3a5c16fc
BS
891};
892
5712db6a
LE
893
894static Property fw_cfg_io_properties[] = {
895 DEFINE_PROP_UINT32("iobase", FWCfgIoState, iobase, -1),
a4c0d1de
MM
896 DEFINE_PROP_UINT32("dma_iobase", FWCfgIoState, dma_iobase, -1),
897 DEFINE_PROP_BOOL("dma_enabled", FWCfgIoState, parent_obj.dma_enabled,
898 false),
5712db6a
LE
899 DEFINE_PROP_END_OF_LIST(),
900};
901
902static void fw_cfg_io_realize(DeviceState *dev, Error **errp)
903{
904 FWCfgIoState *s = FW_CFG_IO(dev);
905 SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
906
907 memory_region_init_io(&s->comb_iomem, OBJECT(s), &fw_cfg_comb_mem_ops,
a4c0d1de 908 FW_CFG(s), "fwcfg", FW_CFG_CTL_SIZE);
5712db6a 909 sysbus_add_io(sbd, s->iobase, &s->comb_iomem);
a4c0d1de
MM
910
911 if (FW_CFG(s)->dma_enabled) {
912 memory_region_init_io(&FW_CFG(s)->dma_iomem, OBJECT(s),
913 &fw_cfg_dma_mem_ops, FW_CFG(s), "fwcfg.dma",
914 sizeof(dma_addr_t));
915 sysbus_add_io(sbd, s->dma_iobase, &FW_CFG(s)->dma_iomem);
916 }
5712db6a
LE
917}
918
919static void fw_cfg_io_class_init(ObjectClass *klass, void *data)
920{
921 DeviceClass *dc = DEVICE_CLASS(klass);
922
923 dc->realize = fw_cfg_io_realize;
924 dc->props = fw_cfg_io_properties;
925}
926
927static const TypeInfo fw_cfg_io_info = {
928 .name = TYPE_FW_CFG_IO,
929 .parent = TYPE_FW_CFG,
930 .instance_size = sizeof(FWCfgIoState),
931 .class_init = fw_cfg_io_class_init,
932};
933
934
cfaadf0e
LE
935static Property fw_cfg_mem_properties[] = {
936 DEFINE_PROP_UINT32("data_width", FWCfgMemState, data_width, -1),
a4c0d1de
MM
937 DEFINE_PROP_BOOL("dma_enabled", FWCfgMemState, parent_obj.dma_enabled,
938 false),
cfaadf0e
LE
939 DEFINE_PROP_END_OF_LIST(),
940};
941
5712db6a
LE
942static void fw_cfg_mem_realize(DeviceState *dev, Error **errp)
943{
944 FWCfgMemState *s = FW_CFG_MEM(dev);
945 SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
cfaadf0e 946 const MemoryRegionOps *data_ops = &fw_cfg_data_mem_ops;
5712db6a
LE
947
948 memory_region_init_io(&s->ctl_iomem, OBJECT(s), &fw_cfg_ctl_mem_ops,
a4c0d1de 949 FW_CFG(s), "fwcfg.ctl", FW_CFG_CTL_SIZE);
5712db6a
LE
950 sysbus_init_mmio(sbd, &s->ctl_iomem);
951
cfaadf0e
LE
952 if (s->data_width > data_ops->valid.max_access_size) {
953 /* memberwise copy because the "old_mmio" member is const */
954 s->wide_data_ops.read = data_ops->read;
955 s->wide_data_ops.write = data_ops->write;
956 s->wide_data_ops.endianness = data_ops->endianness;
957 s->wide_data_ops.valid = data_ops->valid;
958 s->wide_data_ops.impl = data_ops->impl;
959
960 s->wide_data_ops.valid.max_access_size = s->data_width;
961 s->wide_data_ops.impl.max_access_size = s->data_width;
962 data_ops = &s->wide_data_ops;
963 }
964 memory_region_init_io(&s->data_iomem, OBJECT(s), data_ops, FW_CFG(s),
965 "fwcfg.data", data_ops->valid.max_access_size);
5712db6a 966 sysbus_init_mmio(sbd, &s->data_iomem);
a4c0d1de
MM
967
968 if (FW_CFG(s)->dma_enabled) {
969 memory_region_init_io(&FW_CFG(s)->dma_iomem, OBJECT(s),
970 &fw_cfg_dma_mem_ops, FW_CFG(s), "fwcfg.dma",
971 sizeof(dma_addr_t));
972 sysbus_init_mmio(sbd, &FW_CFG(s)->dma_iomem);
973 }
5712db6a
LE
974}
975
976static void fw_cfg_mem_class_init(ObjectClass *klass, void *data)
977{
978 DeviceClass *dc = DEVICE_CLASS(klass);
979
980 dc->realize = fw_cfg_mem_realize;
cfaadf0e 981 dc->props = fw_cfg_mem_properties;
5712db6a
LE
982}
983
984static const TypeInfo fw_cfg_mem_info = {
985 .name = TYPE_FW_CFG_MEM,
986 .parent = TYPE_FW_CFG,
987 .instance_size = sizeof(FWCfgMemState),
988 .class_init = fw_cfg_mem_class_init,
989};
990
991
83f7d43a 992static void fw_cfg_register_types(void)
3a5c16fc 993{
39bffca2 994 type_register_static(&fw_cfg_info);
5712db6a
LE
995 type_register_static(&fw_cfg_io_info);
996 type_register_static(&fw_cfg_mem_info);
3a5c16fc
BS
997}
998
83f7d43a 999type_init(fw_cfg_register_types)
This page took 0.801094 seconds and 4 git commands to generate.