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