2 * QEMU Firmware configuration device emulation
4 * Copyright (c) 2008 Gleb Natapov
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
25 #include "sysemu/sysemu.h"
26 #include "hw/isa/isa.h"
27 #include "hw/nvram/fw_cfg.h"
28 #include "hw/sysbus.h"
30 #include "qemu/error-report.h"
31 #include "qemu/config-file.h"
34 #define FW_CFG_NAME "fw_cfg"
35 #define FW_CFG_PATH "/machine/" FW_CFG_NAME
37 #define TYPE_FW_CFG "fw_cfg"
38 #define TYPE_FW_CFG_IO "fw_cfg_io"
39 #define TYPE_FW_CFG_MEM "fw_cfg_mem"
41 #define FW_CFG(obj) OBJECT_CHECK(FWCfgState, (obj), TYPE_FW_CFG)
42 #define FW_CFG_IO(obj) OBJECT_CHECK(FWCfgIoState, (obj), TYPE_FW_CFG_IO)
43 #define FW_CFG_MEM(obj) OBJECT_CHECK(FWCfgMemState, (obj), TYPE_FW_CFG_MEM)
45 typedef struct FWCfgEntry {
48 void *callback_opaque;
49 FWCfgCallback callback;
50 FWCfgReadCallback read_callback;
55 SysBusDevice parent_obj;
58 FWCfgEntry entries[2][FW_CFG_MAX_ENTRY];
62 Notifier machine_ready;
67 FWCfgState parent_obj;
70 MemoryRegion comb_iomem;
74 struct FWCfgMemState {
76 FWCfgState parent_obj;
79 MemoryRegion ctl_iomem, data_iomem;
85 static char *read_splashfile(char *filename, gsize *file_sizep,
92 unsigned int filehead;
95 res = g_file_get_contents(filename, &content, file_sizep, &err);
97 error_report("failed to read splash file '%s'", filename);
102 /* check file size */
103 if (*file_sizep < 30) {
108 filehead = ((content[0] & 0xff) + (content[1] << 8)) & 0xffff;
109 if (filehead == 0xd8ff) {
110 file_type = JPG_FILE;
111 } else if (filehead == 0x4d42) {
112 file_type = BMP_FILE;
118 if (file_type == BMP_FILE) {
119 bmp_bpp = (content[28] + (content[29] << 8)) & 0xffff;
126 *file_typep = file_type;
131 error_report("splash file '%s' format not recognized; must be JPEG "
132 "or 24 bit BMP", filename);
137 static void fw_cfg_bootsplash(FWCfgState *s)
139 int boot_splash_time = -1;
140 const char *boot_splash_filename = NULL;
142 char *filename, *file_data;
147 /* get user configuration */
148 QemuOptsList *plist = qemu_find_opts("boot-opts");
149 QemuOpts *opts = QTAILQ_FIRST(&plist->head);
151 temp = qemu_opt_get(opts, "splash");
153 boot_splash_filename = temp;
155 temp = qemu_opt_get(opts, "splash-time");
158 boot_splash_time = strtol(p, (char **)&p, 10);
162 /* insert splash time if user configurated */
163 if (boot_splash_time >= 0) {
164 /* validate the input */
165 if (boot_splash_time > 0xffff) {
166 error_report("splash time is big than 65535, force it to 65535.");
167 boot_splash_time = 0xffff;
169 /* use little endian format */
170 qemu_extra_params_fw[0] = (uint8_t)(boot_splash_time & 0xff);
171 qemu_extra_params_fw[1] = (uint8_t)((boot_splash_time >> 8) & 0xff);
172 fw_cfg_add_file(s, "etc/boot-menu-wait", qemu_extra_params_fw, 2);
175 /* insert splash file if user configurated */
176 if (boot_splash_filename != NULL) {
177 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, boot_splash_filename);
178 if (filename == NULL) {
179 error_report("failed to find file '%s'.", boot_splash_filename);
183 /* loading file data */
184 file_data = read_splashfile(filename, &file_size, &file_type);
185 if (file_data == NULL) {
189 if (boot_splash_filedata != NULL) {
190 g_free(boot_splash_filedata);
192 boot_splash_filedata = (uint8_t *)file_data;
193 boot_splash_filedata_size = file_size;
196 if (file_type == JPG_FILE) {
197 fw_cfg_add_file(s, "bootsplash.jpg",
198 boot_splash_filedata, boot_splash_filedata_size);
200 fw_cfg_add_file(s, "bootsplash.bmp",
201 boot_splash_filedata, boot_splash_filedata_size);
207 static void fw_cfg_reboot(FWCfgState *s)
209 int reboot_timeout = -1;
213 /* get user configuration */
214 QemuOptsList *plist = qemu_find_opts("boot-opts");
215 QemuOpts *opts = QTAILQ_FIRST(&plist->head);
217 temp = qemu_opt_get(opts, "reboot-timeout");
220 reboot_timeout = strtol(p, (char **)&p, 10);
223 /* validate the input */
224 if (reboot_timeout > 0xffff) {
225 error_report("reboot timeout is larger than 65535, force it to 65535.");
226 reboot_timeout = 0xffff;
228 fw_cfg_add_file(s, "etc/boot-fail-wait", g_memdup(&reboot_timeout, 4), 4);
231 static void fw_cfg_write(FWCfgState *s, uint8_t value)
233 int arch = !!(s->cur_entry & FW_CFG_ARCH_LOCAL);
234 FWCfgEntry *e = &s->entries[arch][s->cur_entry & FW_CFG_ENTRY_MASK];
236 trace_fw_cfg_write(s, value);
238 if (s->cur_entry & FW_CFG_WRITE_CHANNEL && e->callback &&
239 s->cur_offset < e->len) {
240 e->data[s->cur_offset++] = value;
241 if (s->cur_offset == e->len) {
242 e->callback(e->callback_opaque, e->data);
248 static int fw_cfg_select(FWCfgState *s, uint16_t key)
253 if ((key & FW_CFG_ENTRY_MASK) >= FW_CFG_MAX_ENTRY) {
254 s->cur_entry = FW_CFG_INVALID;
261 trace_fw_cfg_select(s, key, ret);
265 static uint8_t fw_cfg_read(FWCfgState *s)
267 int arch = !!(s->cur_entry & FW_CFG_ARCH_LOCAL);
268 FWCfgEntry *e = &s->entries[arch][s->cur_entry & FW_CFG_ENTRY_MASK];
271 if (s->cur_entry == FW_CFG_INVALID || !e->data || s->cur_offset >= e->len)
274 if (e->read_callback) {
275 e->read_callback(e->callback_opaque, s->cur_offset);
277 ret = e->data[s->cur_offset++];
280 trace_fw_cfg_read(s, ret);
284 static uint64_t fw_cfg_data_mem_read(void *opaque, hwaddr addr,
287 return fw_cfg_read(opaque);
290 static void fw_cfg_data_mem_write(void *opaque, hwaddr addr,
291 uint64_t value, unsigned size)
293 fw_cfg_write(opaque, (uint8_t)value);
296 static void fw_cfg_ctl_mem_write(void *opaque, hwaddr addr,
297 uint64_t value, unsigned size)
299 fw_cfg_select(opaque, (uint16_t)value);
302 static bool fw_cfg_ctl_mem_valid(void *opaque, hwaddr addr,
303 unsigned size, bool is_write)
305 return is_write && size == 2;
308 static uint64_t fw_cfg_comb_read(void *opaque, hwaddr addr,
311 return fw_cfg_read(opaque);
314 static void fw_cfg_comb_write(void *opaque, hwaddr addr,
315 uint64_t value, unsigned size)
319 fw_cfg_write(opaque, (uint8_t)value);
322 fw_cfg_select(opaque, (uint16_t)value);
327 static bool fw_cfg_comb_valid(void *opaque, hwaddr addr,
328 unsigned size, bool is_write)
330 return (size == 1) || (is_write && size == 2);
333 static const MemoryRegionOps fw_cfg_ctl_mem_ops = {
334 .write = fw_cfg_ctl_mem_write,
335 .endianness = DEVICE_NATIVE_ENDIAN,
336 .valid.accepts = fw_cfg_ctl_mem_valid,
339 static const MemoryRegionOps fw_cfg_data_mem_ops = {
340 .read = fw_cfg_data_mem_read,
341 .write = fw_cfg_data_mem_write,
342 .endianness = DEVICE_NATIVE_ENDIAN,
344 .min_access_size = 1,
345 .max_access_size = 1,
349 static const MemoryRegionOps fw_cfg_comb_mem_ops = {
350 .read = fw_cfg_comb_read,
351 .write = fw_cfg_comb_write,
352 .endianness = DEVICE_LITTLE_ENDIAN,
353 .valid.accepts = fw_cfg_comb_valid,
356 static void fw_cfg_reset(DeviceState *d)
358 FWCfgState *s = FW_CFG(d);
363 /* Save restore 32 bit int as uint16_t
364 This is a Big hack, but it is how the old state did it.
365 Or we broke compatibility in the state, or we can't use struct tm
368 static int get_uint32_as_uint16(QEMUFile *f, void *pv, size_t size)
371 *v = qemu_get_be16(f);
375 static void put_unused(QEMUFile *f, void *pv, size_t size)
377 fprintf(stderr, "uint32_as_uint16 is only used for backward compatibility.\n");
378 fprintf(stderr, "This functions shouldn't be called.\n");
381 static const VMStateInfo vmstate_hack_uint32_as_uint16 = {
382 .name = "int32_as_uint16",
383 .get = get_uint32_as_uint16,
387 #define VMSTATE_UINT16_HACK(_f, _s, _t) \
388 VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_hack_uint32_as_uint16, uint32_t)
391 static bool is_version_1(void *opaque, int version_id)
393 return version_id == 1;
396 static const VMStateDescription vmstate_fw_cfg = {
399 .minimum_version_id = 1,
400 .fields = (VMStateField[]) {
401 VMSTATE_UINT16(cur_entry, FWCfgState),
402 VMSTATE_UINT16_HACK(cur_offset, FWCfgState, is_version_1),
403 VMSTATE_UINT32_V(cur_offset, FWCfgState, 2),
404 VMSTATE_END_OF_LIST()
408 static void fw_cfg_add_bytes_read_callback(FWCfgState *s, uint16_t key,
409 FWCfgReadCallback callback,
410 void *callback_opaque,
411 void *data, size_t len)
413 int arch = !!(key & FW_CFG_ARCH_LOCAL);
415 key &= FW_CFG_ENTRY_MASK;
417 assert(key < FW_CFG_MAX_ENTRY && len < UINT32_MAX);
419 s->entries[arch][key].data = data;
420 s->entries[arch][key].len = (uint32_t)len;
421 s->entries[arch][key].read_callback = callback;
422 s->entries[arch][key].callback_opaque = callback_opaque;
425 static void *fw_cfg_modify_bytes_read(FWCfgState *s, uint16_t key,
426 void *data, size_t len)
429 int arch = !!(key & FW_CFG_ARCH_LOCAL);
431 key &= FW_CFG_ENTRY_MASK;
433 assert(key < FW_CFG_MAX_ENTRY && len < UINT32_MAX);
435 /* return the old data to the function caller, avoid memory leak */
436 ptr = s->entries[arch][key].data;
437 s->entries[arch][key].data = data;
438 s->entries[arch][key].len = len;
439 s->entries[arch][key].callback_opaque = NULL;
440 s->entries[arch][key].callback = NULL;
445 void fw_cfg_add_bytes(FWCfgState *s, uint16_t key, void *data, size_t len)
447 fw_cfg_add_bytes_read_callback(s, key, NULL, NULL, data, len);
450 void fw_cfg_add_string(FWCfgState *s, uint16_t key, const char *value)
452 size_t sz = strlen(value) + 1;
454 return fw_cfg_add_bytes(s, key, g_memdup(value, sz), sz);
457 void fw_cfg_add_i16(FWCfgState *s, uint16_t key, uint16_t value)
461 copy = g_malloc(sizeof(value));
462 *copy = cpu_to_le16(value);
463 fw_cfg_add_bytes(s, key, copy, sizeof(value));
466 void fw_cfg_add_i32(FWCfgState *s, uint16_t key, uint32_t value)
470 copy = g_malloc(sizeof(value));
471 *copy = cpu_to_le32(value);
472 fw_cfg_add_bytes(s, key, copy, sizeof(value));
475 void fw_cfg_add_i64(FWCfgState *s, uint16_t key, uint64_t value)
479 copy = g_malloc(sizeof(value));
480 *copy = cpu_to_le64(value);
481 fw_cfg_add_bytes(s, key, copy, sizeof(value));
484 void fw_cfg_add_callback(FWCfgState *s, uint16_t key, FWCfgCallback callback,
485 void *callback_opaque, void *data, size_t len)
487 int arch = !!(key & FW_CFG_ARCH_LOCAL);
489 assert(key & FW_CFG_WRITE_CHANNEL);
491 key &= FW_CFG_ENTRY_MASK;
493 assert(key < FW_CFG_MAX_ENTRY && len <= UINT32_MAX);
495 s->entries[arch][key].data = data;
496 s->entries[arch][key].len = (uint32_t)len;
497 s->entries[arch][key].callback_opaque = callback_opaque;
498 s->entries[arch][key].callback = callback;
501 void fw_cfg_add_file_callback(FWCfgState *s, const char *filename,
502 FWCfgReadCallback callback, void *callback_opaque,
503 void *data, size_t len)
509 dsize = sizeof(uint32_t) + sizeof(FWCfgFile) * FW_CFG_FILE_SLOTS;
510 s->files = g_malloc0(dsize);
511 fw_cfg_add_bytes(s, FW_CFG_FILE_DIR, s->files, dsize);
514 index = be32_to_cpu(s->files->count);
515 assert(index < FW_CFG_FILE_SLOTS);
517 fw_cfg_add_bytes_read_callback(s, FW_CFG_FILE_FIRST + index,
518 callback, callback_opaque, data, len);
520 pstrcpy(s->files->f[index].name, sizeof(s->files->f[index].name),
522 for (i = 0; i < index; i++) {
523 if (strcmp(s->files->f[index].name, s->files->f[i].name) == 0) {
524 trace_fw_cfg_add_file_dupe(s, s->files->f[index].name);
529 s->files->f[index].size = cpu_to_be32(len);
530 s->files->f[index].select = cpu_to_be16(FW_CFG_FILE_FIRST + index);
531 trace_fw_cfg_add_file(s, index, s->files->f[index].name, len);
533 s->files->count = cpu_to_be32(index+1);
536 void fw_cfg_add_file(FWCfgState *s, const char *filename,
537 void *data, size_t len)
539 fw_cfg_add_file_callback(s, filename, NULL, NULL, data, len);
542 void *fw_cfg_modify_file(FWCfgState *s, const char *filename,
543 void *data, size_t len)
550 index = be32_to_cpu(s->files->count);
551 assert(index < FW_CFG_FILE_SLOTS);
553 for (i = 0; i < index; i++) {
554 if (strcmp(filename, s->files->f[i].name) == 0) {
555 ptr = fw_cfg_modify_bytes_read(s, FW_CFG_FILE_FIRST + i,
557 s->files->f[i].size = cpu_to_be32(len);
562 fw_cfg_add_file_callback(s, filename, NULL, NULL, data, len);
566 static void fw_cfg_machine_reset(void *opaque)
570 FWCfgState *s = opaque;
571 char *bootindex = get_boot_devices_list(&len, false);
573 ptr = fw_cfg_modify_file(s, "bootorder", (uint8_t *)bootindex, len);
577 static void fw_cfg_machine_ready(struct Notifier *n, void *data)
579 FWCfgState *s = container_of(n, FWCfgState, machine_ready);
580 qemu_register_reset(fw_cfg_machine_reset, s);
585 static void fw_cfg_init1(DeviceState *dev)
587 FWCfgState *s = FW_CFG(dev);
589 assert(!object_resolve_path(FW_CFG_PATH, NULL));
591 object_property_add_child(qdev_get_machine(), FW_CFG_NAME, OBJECT(s), NULL);
593 qdev_init_nofail(dev);
595 fw_cfg_add_bytes(s, FW_CFG_SIGNATURE, (char *)"QEMU", 4);
596 fw_cfg_add_bytes(s, FW_CFG_UUID, qemu_uuid, 16);
597 fw_cfg_add_i16(s, FW_CFG_NOGRAPHIC, (uint16_t)(display_type == DT_NOGRAPHIC));
598 fw_cfg_add_i16(s, FW_CFG_NB_CPUS, (uint16_t)smp_cpus);
599 fw_cfg_add_i16(s, FW_CFG_BOOT_MENU, (uint16_t)boot_menu);
600 fw_cfg_bootsplash(s);
603 s->machine_ready.notify = fw_cfg_machine_ready;
604 qemu_add_machine_init_done_notifier(&s->machine_ready);
607 FWCfgState *fw_cfg_init_io(uint32_t iobase)
611 dev = qdev_create(NULL, TYPE_FW_CFG_IO);
612 qdev_prop_set_uint32(dev, "iobase", iobase);
618 FWCfgState *fw_cfg_init_mem(hwaddr ctl_addr, hwaddr data_addr)
623 dev = qdev_create(NULL, TYPE_FW_CFG_MEM);
626 sbd = SYS_BUS_DEVICE(dev);
627 sysbus_mmio_map(sbd, 0, ctl_addr);
628 sysbus_mmio_map(sbd, 1, data_addr);
634 FWCfgState *fw_cfg_find(void)
636 return FW_CFG(object_resolve_path(FW_CFG_PATH, NULL));
639 static void fw_cfg_class_init(ObjectClass *klass, void *data)
641 DeviceClass *dc = DEVICE_CLASS(klass);
643 dc->reset = fw_cfg_reset;
644 dc->vmsd = &vmstate_fw_cfg;
647 static const TypeInfo fw_cfg_info = {
649 .parent = TYPE_SYS_BUS_DEVICE,
650 .instance_size = sizeof(FWCfgState),
651 .class_init = fw_cfg_class_init,
655 static Property fw_cfg_io_properties[] = {
656 DEFINE_PROP_UINT32("iobase", FWCfgIoState, iobase, -1),
657 DEFINE_PROP_END_OF_LIST(),
660 static void fw_cfg_io_realize(DeviceState *dev, Error **errp)
662 FWCfgIoState *s = FW_CFG_IO(dev);
663 SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
665 memory_region_init_io(&s->comb_iomem, OBJECT(s), &fw_cfg_comb_mem_ops,
666 FW_CFG(s), "fwcfg", FW_CFG_SIZE);
667 sysbus_add_io(sbd, s->iobase, &s->comb_iomem);
670 static void fw_cfg_io_class_init(ObjectClass *klass, void *data)
672 DeviceClass *dc = DEVICE_CLASS(klass);
674 dc->realize = fw_cfg_io_realize;
675 dc->props = fw_cfg_io_properties;
678 static const TypeInfo fw_cfg_io_info = {
679 .name = TYPE_FW_CFG_IO,
680 .parent = TYPE_FW_CFG,
681 .instance_size = sizeof(FWCfgIoState),
682 .class_init = fw_cfg_io_class_init,
686 static void fw_cfg_mem_realize(DeviceState *dev, Error **errp)
688 FWCfgMemState *s = FW_CFG_MEM(dev);
689 SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
691 memory_region_init_io(&s->ctl_iomem, OBJECT(s), &fw_cfg_ctl_mem_ops,
692 FW_CFG(s), "fwcfg.ctl", FW_CFG_SIZE);
693 sysbus_init_mmio(sbd, &s->ctl_iomem);
695 memory_region_init_io(&s->data_iomem, OBJECT(s), &fw_cfg_data_mem_ops,
696 FW_CFG(s), "fwcfg.data",
697 fw_cfg_data_mem_ops.valid.max_access_size);
698 sysbus_init_mmio(sbd, &s->data_iomem);
701 static void fw_cfg_mem_class_init(ObjectClass *klass, void *data)
703 DeviceClass *dc = DEVICE_CLASS(klass);
705 dc->realize = fw_cfg_mem_realize;
708 static const TypeInfo fw_cfg_mem_info = {
709 .name = TYPE_FW_CFG_MEM,
710 .parent = TYPE_FW_CFG,
711 .instance_size = sizeof(FWCfgMemState),
712 .class_init = fw_cfg_mem_class_init,
716 static void fw_cfg_register_types(void)
718 type_register_static(&fw_cfg_info);
719 type_register_static(&fw_cfg_io_info);
720 type_register_static(&fw_cfg_mem_info);
723 type_init(fw_cfg_register_types)