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
24 #include "qemu/osdep.h"
26 #include "sysemu/sysemu.h"
27 #include "sysemu/dma.h"
28 #include "hw/boards.h"
29 #include "hw/isa/isa.h"
30 #include "hw/nvram/fw_cfg.h"
31 #include "hw/sysbus.h"
33 #include "qemu/error-report.h"
34 #include "qemu/config-file.h"
35 #include "qemu/cutils.h"
36 #include "qapi/error.h"
38 #define FW_CFG_FILE_SLOTS_DFLT 0x20
40 #define TYPE_FW_CFG "fw_cfg"
41 #define TYPE_FW_CFG_IO "fw_cfg_io"
42 #define TYPE_FW_CFG_MEM "fw_cfg_mem"
44 #define FW_CFG(obj) OBJECT_CHECK(FWCfgState, (obj), TYPE_FW_CFG)
45 #define FW_CFG_IO(obj) OBJECT_CHECK(FWCfgIoState, (obj), TYPE_FW_CFG_IO)
46 #define FW_CFG_MEM(obj) OBJECT_CHECK(FWCfgMemState, (obj), TYPE_FW_CFG_MEM)
48 /* FW_CFG_VERSION bits */
49 #define FW_CFG_VERSION 0x01
50 #define FW_CFG_VERSION_DMA 0x02
52 /* FW_CFG_DMA_CONTROL bits */
53 #define FW_CFG_DMA_CTL_ERROR 0x01
54 #define FW_CFG_DMA_CTL_READ 0x02
55 #define FW_CFG_DMA_CTL_SKIP 0x04
56 #define FW_CFG_DMA_CTL_SELECT 0x08
57 #define FW_CFG_DMA_CTL_WRITE 0x10
59 #define FW_CFG_DMA_SIGNATURE 0x51454d5520434647ULL /* "QEMU CFG" */
61 typedef struct FWCfgEntry {
65 void *callback_opaque;
66 FWCfgReadCallback read_callback;
71 SysBusDevice parent_obj;
75 FWCfgEntry *entries[2];
80 Notifier machine_ready;
82 int fw_cfg_order_override;
87 MemoryRegion dma_iomem;
92 FWCfgState parent_obj;
95 MemoryRegion comb_iomem;
98 struct FWCfgMemState {
100 FWCfgState parent_obj;
103 MemoryRegion ctl_iomem, data_iomem;
105 MemoryRegionOps wide_data_ops;
111 static char *read_splashfile(char *filename, gsize *file_sizep,
118 unsigned int filehead;
121 res = g_file_get_contents(filename, &content, file_sizep, &err);
123 error_report("failed to read splash file '%s'", filename);
128 /* check file size */
129 if (*file_sizep < 30) {
134 filehead = ((content[0] & 0xff) + (content[1] << 8)) & 0xffff;
135 if (filehead == 0xd8ff) {
136 file_type = JPG_FILE;
137 } else if (filehead == 0x4d42) {
138 file_type = BMP_FILE;
144 if (file_type == BMP_FILE) {
145 bmp_bpp = (content[28] + (content[29] << 8)) & 0xffff;
152 *file_typep = file_type;
157 error_report("splash file '%s' format not recognized; must be JPEG "
158 "or 24 bit BMP", filename);
163 static void fw_cfg_bootsplash(FWCfgState *s)
165 int boot_splash_time = -1;
166 const char *boot_splash_filename = NULL;
168 char *filename, *file_data;
173 /* get user configuration */
174 QemuOptsList *plist = qemu_find_opts("boot-opts");
175 QemuOpts *opts = QTAILQ_FIRST(&plist->head);
177 temp = qemu_opt_get(opts, "splash");
179 boot_splash_filename = temp;
181 temp = qemu_opt_get(opts, "splash-time");
184 boot_splash_time = strtol(p, &p, 10);
188 /* insert splash time if user configurated */
189 if (boot_splash_time >= 0) {
190 /* validate the input */
191 if (boot_splash_time > 0xffff) {
192 error_report("splash time is big than 65535, force it to 65535.");
193 boot_splash_time = 0xffff;
195 /* use little endian format */
196 qemu_extra_params_fw[0] = (uint8_t)(boot_splash_time & 0xff);
197 qemu_extra_params_fw[1] = (uint8_t)((boot_splash_time >> 8) & 0xff);
198 fw_cfg_add_file(s, "etc/boot-menu-wait", qemu_extra_params_fw, 2);
201 /* insert splash file if user configurated */
202 if (boot_splash_filename != NULL) {
203 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, boot_splash_filename);
204 if (filename == NULL) {
205 error_report("failed to find file '%s'.", boot_splash_filename);
209 /* loading file data */
210 file_data = read_splashfile(filename, &file_size, &file_type);
211 if (file_data == NULL) {
215 g_free(boot_splash_filedata);
216 boot_splash_filedata = (uint8_t *)file_data;
217 boot_splash_filedata_size = file_size;
220 if (file_type == JPG_FILE) {
221 fw_cfg_add_file(s, "bootsplash.jpg",
222 boot_splash_filedata, boot_splash_filedata_size);
224 fw_cfg_add_file(s, "bootsplash.bmp",
225 boot_splash_filedata, boot_splash_filedata_size);
231 static void fw_cfg_reboot(FWCfgState *s)
233 int reboot_timeout = -1;
237 /* get user configuration */
238 QemuOptsList *plist = qemu_find_opts("boot-opts");
239 QemuOpts *opts = QTAILQ_FIRST(&plist->head);
241 temp = qemu_opt_get(opts, "reboot-timeout");
244 reboot_timeout = strtol(p, &p, 10);
247 /* validate the input */
248 if (reboot_timeout > 0xffff) {
249 error_report("reboot timeout is larger than 65535, force it to 65535.");
250 reboot_timeout = 0xffff;
252 fw_cfg_add_file(s, "etc/boot-fail-wait", g_memdup(&reboot_timeout, 4), 4);
255 static void fw_cfg_write(FWCfgState *s, uint8_t value)
257 /* nothing, write support removed in QEMU v2.4+ */
260 static inline uint16_t fw_cfg_file_slots(const FWCfgState *s)
262 return s->file_slots;
265 /* Note: this function returns an exclusive limit. */
266 static inline uint32_t fw_cfg_max_entry(const FWCfgState *s)
268 return FW_CFG_FILE_FIRST + fw_cfg_file_slots(s);
271 static int fw_cfg_select(FWCfgState *s, uint16_t key)
277 if ((key & FW_CFG_ENTRY_MASK) >= fw_cfg_max_entry(s)) {
278 s->cur_entry = FW_CFG_INVALID;
283 /* entry successfully selected, now run callback if present */
284 arch = !!(key & FW_CFG_ARCH_LOCAL);
285 e = &s->entries[arch][key & FW_CFG_ENTRY_MASK];
286 if (e->read_callback) {
287 e->read_callback(e->callback_opaque);
291 trace_fw_cfg_select(s, key, ret);
295 static uint64_t fw_cfg_data_read(void *opaque, hwaddr addr, unsigned size)
297 FWCfgState *s = opaque;
298 int arch = !!(s->cur_entry & FW_CFG_ARCH_LOCAL);
299 FWCfgEntry *e = (s->cur_entry == FW_CFG_INVALID) ? NULL :
300 &s->entries[arch][s->cur_entry & FW_CFG_ENTRY_MASK];
303 assert(size > 0 && size <= sizeof(value));
304 if (s->cur_entry != FW_CFG_INVALID && e->data && s->cur_offset < e->len) {
305 /* The least significant 'size' bytes of the return value are
306 * expected to contain a string preserving portion of the item
307 * data, padded with zeros on the right in case we run out early.
308 * In technical terms, we're composing the host-endian representation
309 * of the big endian interpretation of the fw_cfg string.
312 value = (value << 8) | e->data[s->cur_offset++];
313 } while (--size && s->cur_offset < e->len);
314 /* If size is still not zero, we *did* run out early, so continue
315 * left-shifting, to add the appropriate number of padding zeros
321 trace_fw_cfg_read(s, value);
325 static void fw_cfg_data_mem_write(void *opaque, hwaddr addr,
326 uint64_t value, unsigned size)
328 FWCfgState *s = opaque;
332 fw_cfg_write(s, value >> (8 * --i));
336 static void fw_cfg_dma_transfer(FWCfgState *s)
342 int read = 0, write = 0;
345 /* Reset the address before the next access */
346 dma_addr = s->dma_addr;
349 if (dma_memory_read(s->dma_as, dma_addr, &dma, sizeof(dma))) {
350 stl_be_dma(s->dma_as, dma_addr + offsetof(FWCfgDmaAccess, control),
351 FW_CFG_DMA_CTL_ERROR);
355 dma.address = be64_to_cpu(dma.address);
356 dma.length = be32_to_cpu(dma.length);
357 dma.control = be32_to_cpu(dma.control);
359 if (dma.control & FW_CFG_DMA_CTL_SELECT) {
360 fw_cfg_select(s, dma.control >> 16);
363 arch = !!(s->cur_entry & FW_CFG_ARCH_LOCAL);
364 e = (s->cur_entry == FW_CFG_INVALID) ? NULL :
365 &s->entries[arch][s->cur_entry & FW_CFG_ENTRY_MASK];
367 if (dma.control & FW_CFG_DMA_CTL_READ) {
370 } else if (dma.control & FW_CFG_DMA_CTL_WRITE) {
373 } else if (dma.control & FW_CFG_DMA_CTL_SKIP) {
382 while (dma.length > 0 && !(dma.control & FW_CFG_DMA_CTL_ERROR)) {
383 if (s->cur_entry == FW_CFG_INVALID || !e->data ||
384 s->cur_offset >= e->len) {
387 /* If the access is not a read access, it will be a skip access,
391 if (dma_memory_set(s->dma_as, dma.address, 0, len)) {
392 dma.control |= FW_CFG_DMA_CTL_ERROR;
396 dma.control |= FW_CFG_DMA_CTL_ERROR;
399 if (dma.length <= (e->len - s->cur_offset)) {
402 len = (e->len - s->cur_offset);
405 /* If the access is not a read access, it will be a skip access,
409 if (dma_memory_write(s->dma_as, dma.address,
410 &e->data[s->cur_offset], len)) {
411 dma.control |= FW_CFG_DMA_CTL_ERROR;
415 if (!e->allow_write ||
417 dma_memory_read(s->dma_as, dma.address,
418 &e->data[s->cur_offset], len)) {
419 dma.control |= FW_CFG_DMA_CTL_ERROR;
423 s->cur_offset += len;
431 stl_be_dma(s->dma_as, dma_addr + offsetof(FWCfgDmaAccess, control),
434 trace_fw_cfg_read(s, 0);
437 static uint64_t fw_cfg_dma_mem_read(void *opaque, hwaddr addr,
440 /* Return a signature value (and handle various read sizes) */
441 return extract64(FW_CFG_DMA_SIGNATURE, (8 - addr - size) * 8, size * 8);
444 static void fw_cfg_dma_mem_write(void *opaque, hwaddr addr,
445 uint64_t value, unsigned size)
447 FWCfgState *s = opaque;
451 /* FWCfgDmaAccess high address */
452 s->dma_addr = value << 32;
453 } else if (addr == 4) {
454 /* FWCfgDmaAccess low address */
455 s->dma_addr |= value;
456 fw_cfg_dma_transfer(s);
458 } else if (size == 8 && addr == 0) {
460 fw_cfg_dma_transfer(s);
464 static bool fw_cfg_dma_mem_valid(void *opaque, hwaddr addr,
465 unsigned size, bool is_write)
467 return !is_write || ((size == 4 && (addr == 0 || addr == 4)) ||
468 (size == 8 && addr == 0));
471 static bool fw_cfg_data_mem_valid(void *opaque, hwaddr addr,
472 unsigned size, bool is_write)
477 static void fw_cfg_ctl_mem_write(void *opaque, hwaddr addr,
478 uint64_t value, unsigned size)
480 fw_cfg_select(opaque, (uint16_t)value);
483 static bool fw_cfg_ctl_mem_valid(void *opaque, hwaddr addr,
484 unsigned size, bool is_write)
486 return is_write && size == 2;
489 static void fw_cfg_comb_write(void *opaque, hwaddr addr,
490 uint64_t value, unsigned size)
494 fw_cfg_write(opaque, (uint8_t)value);
497 fw_cfg_select(opaque, (uint16_t)value);
502 static bool fw_cfg_comb_valid(void *opaque, hwaddr addr,
503 unsigned size, bool is_write)
505 return (size == 1) || (is_write && size == 2);
508 static const MemoryRegionOps fw_cfg_ctl_mem_ops = {
509 .write = fw_cfg_ctl_mem_write,
510 .endianness = DEVICE_BIG_ENDIAN,
511 .valid.accepts = fw_cfg_ctl_mem_valid,
514 static const MemoryRegionOps fw_cfg_data_mem_ops = {
515 .read = fw_cfg_data_read,
516 .write = fw_cfg_data_mem_write,
517 .endianness = DEVICE_BIG_ENDIAN,
519 .min_access_size = 1,
520 .max_access_size = 1,
521 .accepts = fw_cfg_data_mem_valid,
525 static const MemoryRegionOps fw_cfg_comb_mem_ops = {
526 .read = fw_cfg_data_read,
527 .write = fw_cfg_comb_write,
528 .endianness = DEVICE_LITTLE_ENDIAN,
529 .valid.accepts = fw_cfg_comb_valid,
532 static const MemoryRegionOps fw_cfg_dma_mem_ops = {
533 .read = fw_cfg_dma_mem_read,
534 .write = fw_cfg_dma_mem_write,
535 .endianness = DEVICE_BIG_ENDIAN,
536 .valid.accepts = fw_cfg_dma_mem_valid,
537 .valid.max_access_size = 8,
538 .impl.max_access_size = 8,
541 static void fw_cfg_reset(DeviceState *d)
543 FWCfgState *s = FW_CFG(d);
545 /* we never register a read callback for FW_CFG_SIGNATURE */
546 fw_cfg_select(s, FW_CFG_SIGNATURE);
549 /* Save restore 32 bit int as uint16_t
550 This is a Big hack, but it is how the old state did it.
551 Or we broke compatibility in the state, or we can't use struct tm
554 static int get_uint32_as_uint16(QEMUFile *f, void *pv, size_t size,
558 *v = qemu_get_be16(f);
562 static int put_unused(QEMUFile *f, void *pv, size_t size, VMStateField *field,
565 fprintf(stderr, "uint32_as_uint16 is only used for backward compatibility.\n");
566 fprintf(stderr, "This functions shouldn't be called.\n");
571 static const VMStateInfo vmstate_hack_uint32_as_uint16 = {
572 .name = "int32_as_uint16",
573 .get = get_uint32_as_uint16,
577 #define VMSTATE_UINT16_HACK(_f, _s, _t) \
578 VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_hack_uint32_as_uint16, uint32_t)
581 static bool is_version_1(void *opaque, int version_id)
583 return version_id == 1;
586 bool fw_cfg_dma_enabled(void *opaque)
588 FWCfgState *s = opaque;
590 return s->dma_enabled;
593 static const VMStateDescription vmstate_fw_cfg_dma = {
594 .name = "fw_cfg/dma",
595 .needed = fw_cfg_dma_enabled,
596 .fields = (VMStateField[]) {
597 VMSTATE_UINT64(dma_addr, FWCfgState),
598 VMSTATE_END_OF_LIST()
602 static const VMStateDescription vmstate_fw_cfg = {
605 .minimum_version_id = 1,
606 .fields = (VMStateField[]) {
607 VMSTATE_UINT16(cur_entry, FWCfgState),
608 VMSTATE_UINT16_HACK(cur_offset, FWCfgState, is_version_1),
609 VMSTATE_UINT32_V(cur_offset, FWCfgState, 2),
610 VMSTATE_END_OF_LIST()
612 .subsections = (const VMStateDescription*[]) {
618 static void fw_cfg_add_bytes_read_callback(FWCfgState *s, uint16_t key,
619 FWCfgReadCallback callback,
620 void *callback_opaque,
621 void *data, size_t len,
624 int arch = !!(key & FW_CFG_ARCH_LOCAL);
626 key &= FW_CFG_ENTRY_MASK;
628 assert(key < fw_cfg_max_entry(s) && len < UINT32_MAX);
629 assert(s->entries[arch][key].data == NULL); /* avoid key conflict */
631 s->entries[arch][key].data = data;
632 s->entries[arch][key].len = (uint32_t)len;
633 s->entries[arch][key].read_callback = callback;
634 s->entries[arch][key].callback_opaque = callback_opaque;
635 s->entries[arch][key].allow_write = !read_only;
638 static void *fw_cfg_modify_bytes_read(FWCfgState *s, uint16_t key,
639 void *data, size_t len)
642 int arch = !!(key & FW_CFG_ARCH_LOCAL);
644 key &= FW_CFG_ENTRY_MASK;
646 assert(key < fw_cfg_max_entry(s) && len < UINT32_MAX);
648 /* return the old data to the function caller, avoid memory leak */
649 ptr = s->entries[arch][key].data;
650 s->entries[arch][key].data = data;
651 s->entries[arch][key].len = len;
652 s->entries[arch][key].callback_opaque = NULL;
653 s->entries[arch][key].allow_write = false;
658 void fw_cfg_add_bytes(FWCfgState *s, uint16_t key, void *data, size_t len)
660 fw_cfg_add_bytes_read_callback(s, key, NULL, NULL, data, len, true);
663 void fw_cfg_add_string(FWCfgState *s, uint16_t key, const char *value)
665 size_t sz = strlen(value) + 1;
667 fw_cfg_add_bytes(s, key, g_memdup(value, sz), sz);
670 void fw_cfg_add_i16(FWCfgState *s, uint16_t key, uint16_t value)
674 copy = g_malloc(sizeof(value));
675 *copy = cpu_to_le16(value);
676 fw_cfg_add_bytes(s, key, copy, sizeof(value));
679 void fw_cfg_modify_i16(FWCfgState *s, uint16_t key, uint16_t value)
681 uint16_t *copy, *old;
683 copy = g_malloc(sizeof(value));
684 *copy = cpu_to_le16(value);
685 old = fw_cfg_modify_bytes_read(s, key, copy, sizeof(value));
689 void fw_cfg_add_i32(FWCfgState *s, uint16_t key, uint32_t value)
693 copy = g_malloc(sizeof(value));
694 *copy = cpu_to_le32(value);
695 fw_cfg_add_bytes(s, key, copy, sizeof(value));
698 void fw_cfg_add_i64(FWCfgState *s, uint16_t key, uint64_t value)
702 copy = g_malloc(sizeof(value));
703 *copy = cpu_to_le64(value);
704 fw_cfg_add_bytes(s, key, copy, sizeof(value));
707 void fw_cfg_set_order_override(FWCfgState *s, int order)
709 assert(s->fw_cfg_order_override == 0);
710 s->fw_cfg_order_override = order;
713 void fw_cfg_reset_order_override(FWCfgState *s)
715 assert(s->fw_cfg_order_override != 0);
716 s->fw_cfg_order_override = 0;
720 * This is the legacy order list. For legacy systems, files are in
721 * the fw_cfg in the order defined below, by the "order" value. Note
722 * that some entries (VGA ROMs, NIC option ROMS, etc.) go into a
723 * specific area, but there may be more than one and they occur in the
724 * order that the user specifies them on the command line. Those are
725 * handled in a special manner, using the order override above.
727 * For non-legacy, the files are sorted by filename to avoid this kind
728 * of complexity in the future.
730 * This is only for x86, other arches don't implement versioning so
731 * they won't set legacy mode.
737 { "etc/boot-menu-wait", 10 },
738 { "bootsplash.jpg", 11 },
739 { "bootsplash.bmp", 12 },
740 { "etc/boot-fail-wait", 15 },
741 { "etc/smbios/smbios-tables", 20 },
742 { "etc/smbios/smbios-anchor", 30 },
744 { "etc/reserved-memory-end", 50 },
745 { "genroms/kvmvapic.bin", 55 },
746 { "genroms/linuxboot.bin", 60 },
747 { }, /* VGA ROMs from pc_vga_init come here, 70. */
748 { }, /* NIC option ROMs from pc_nic_init come here, 80. */
749 { "etc/system-states", 90 },
750 { }, /* User ROMs come here, 100. */
751 { }, /* Device FW comes here, 110. */
752 { "etc/extra-pci-roots", 120 },
753 { "etc/acpi/tables", 130 },
754 { "etc/table-loader", 140 },
755 { "etc/tpm/log", 150 },
756 { "etc/acpi/rsdp", 160 },
757 { "bootorder", 170 },
759 #define FW_CFG_ORDER_OVERRIDE_LAST 200
762 static int get_fw_cfg_order(FWCfgState *s, const char *name)
766 if (s->fw_cfg_order_override > 0) {
767 return s->fw_cfg_order_override;
770 for (i = 0; i < ARRAY_SIZE(fw_cfg_order); i++) {
771 if (fw_cfg_order[i].name == NULL) {
775 if (strcmp(name, fw_cfg_order[i].name) == 0) {
776 return fw_cfg_order[i].order;
780 /* Stick unknown stuff at the end. */
781 warn_report("Unknown firmware file in legacy mode: %s", name);
782 return FW_CFG_ORDER_OVERRIDE_LAST;
785 void fw_cfg_add_file_callback(FWCfgState *s, const char *filename,
786 FWCfgReadCallback callback, void *callback_opaque,
787 void *data, size_t len, bool read_only)
791 MachineClass *mc = MACHINE_GET_CLASS(qdev_get_machine());
795 dsize = sizeof(uint32_t) + sizeof(FWCfgFile) * fw_cfg_file_slots(s);
796 s->files = g_malloc0(dsize);
797 fw_cfg_add_bytes(s, FW_CFG_FILE_DIR, s->files, dsize);
800 count = be32_to_cpu(s->files->count);
801 assert(count < fw_cfg_file_slots(s));
803 /* Find the insertion point. */
804 if (mc->legacy_fw_cfg_order) {
806 * Sort by order. For files with the same order, we keep them
807 * in the sequence in which they were added.
809 order = get_fw_cfg_order(s, filename);
811 index > 0 && order < s->entry_order[index - 1];
814 /* Sort by file name. */
816 index > 0 && strcmp(filename, s->files->f[index - 1].name) < 0;
821 * Move all the entries from the index point and after down one
822 * to create a slot for the new entry. Because calculations are
823 * being done with the index, make it so that "i" is the current
824 * index and "i - 1" is the one being copied from, thus the
825 * unusual start and end in the for statement.
827 for (i = count + 1; i > index; i--) {
828 s->files->f[i] = s->files->f[i - 1];
829 s->files->f[i].select = cpu_to_be16(FW_CFG_FILE_FIRST + i);
830 s->entries[0][FW_CFG_FILE_FIRST + i] =
831 s->entries[0][FW_CFG_FILE_FIRST + i - 1];
832 s->entry_order[i] = s->entry_order[i - 1];
835 memset(&s->files->f[index], 0, sizeof(FWCfgFile));
836 memset(&s->entries[0][FW_CFG_FILE_FIRST + index], 0, sizeof(FWCfgEntry));
838 pstrcpy(s->files->f[index].name, sizeof(s->files->f[index].name), filename);
839 for (i = 0; i <= count; i++) {
841 strcmp(s->files->f[index].name, s->files->f[i].name) == 0) {
842 error_report("duplicate fw_cfg file name: %s",
843 s->files->f[index].name);
848 fw_cfg_add_bytes_read_callback(s, FW_CFG_FILE_FIRST + index,
849 callback, callback_opaque, data, len,
852 s->files->f[index].size = cpu_to_be32(len);
853 s->files->f[index].select = cpu_to_be16(FW_CFG_FILE_FIRST + index);
854 s->entry_order[index] = order;
855 trace_fw_cfg_add_file(s, index, s->files->f[index].name, len);
857 s->files->count = cpu_to_be32(count+1);
860 void fw_cfg_add_file(FWCfgState *s, const char *filename,
861 void *data, size_t len)
863 fw_cfg_add_file_callback(s, filename, NULL, NULL, data, len, true);
866 void *fw_cfg_modify_file(FWCfgState *s, const char *filename,
867 void *data, size_t len)
874 index = be32_to_cpu(s->files->count);
875 assert(index < fw_cfg_file_slots(s));
877 for (i = 0; i < index; i++) {
878 if (strcmp(filename, s->files->f[i].name) == 0) {
879 ptr = fw_cfg_modify_bytes_read(s, FW_CFG_FILE_FIRST + i,
881 s->files->f[i].size = cpu_to_be32(len);
886 fw_cfg_add_file_callback(s, filename, NULL, NULL, data, len, true);
890 static void fw_cfg_machine_reset(void *opaque)
894 FWCfgState *s = opaque;
895 char *bootindex = get_boot_devices_list(&len, false);
897 ptr = fw_cfg_modify_file(s, "bootorder", (uint8_t *)bootindex, len);
901 static void fw_cfg_machine_ready(struct Notifier *n, void *data)
903 FWCfgState *s = container_of(n, FWCfgState, machine_ready);
904 qemu_register_reset(fw_cfg_machine_reset, s);
909 static void fw_cfg_common_realize(DeviceState *dev, Error **errp)
911 FWCfgState *s = FW_CFG(dev);
912 MachineState *machine = MACHINE(qdev_get_machine());
913 uint32_t version = FW_CFG_VERSION;
915 if (!fw_cfg_find()) {
916 error_setg(errp, "at most one %s device is permitted", TYPE_FW_CFG);
920 fw_cfg_add_bytes(s, FW_CFG_SIGNATURE, (char *)"QEMU", 4);
921 fw_cfg_add_bytes(s, FW_CFG_UUID, &qemu_uuid, 16);
922 fw_cfg_add_i16(s, FW_CFG_NOGRAPHIC, (uint16_t)!machine->enable_graphics);
923 fw_cfg_add_i16(s, FW_CFG_BOOT_MENU, (uint16_t)boot_menu);
924 fw_cfg_bootsplash(s);
927 if (s->dma_enabled) {
928 version |= FW_CFG_VERSION_DMA;
931 fw_cfg_add_i32(s, FW_CFG_ID, version);
933 s->machine_ready.notify = fw_cfg_machine_ready;
934 qemu_add_machine_init_done_notifier(&s->machine_ready);
937 FWCfgState *fw_cfg_init_io_dma(uint32_t iobase, uint32_t dma_iobase,
938 AddressSpace *dma_as)
944 bool dma_requested = dma_iobase && dma_as;
946 dev = qdev_create(NULL, TYPE_FW_CFG_IO);
947 if (!dma_requested) {
948 qdev_prop_set_bit(dev, "dma_enabled", false);
951 object_property_add_child(OBJECT(qdev_get_machine()), TYPE_FW_CFG,
953 qdev_init_nofail(dev);
955 sbd = SYS_BUS_DEVICE(dev);
956 ios = FW_CFG_IO(dev);
957 sysbus_add_io(sbd, iobase, &ios->comb_iomem);
961 if (s->dma_enabled) {
962 /* 64 bits for the address field */
965 sysbus_add_io(sbd, dma_iobase, &s->dma_iomem);
971 FWCfgState *fw_cfg_init_io(uint32_t iobase)
973 return fw_cfg_init_io_dma(iobase, 0, NULL);
976 FWCfgState *fw_cfg_init_mem_wide(hwaddr ctl_addr,
977 hwaddr data_addr, uint32_t data_width,
978 hwaddr dma_addr, AddressSpace *dma_as)
983 bool dma_requested = dma_addr && dma_as;
985 dev = qdev_create(NULL, TYPE_FW_CFG_MEM);
986 qdev_prop_set_uint32(dev, "data_width", data_width);
987 if (!dma_requested) {
988 qdev_prop_set_bit(dev, "dma_enabled", false);
991 object_property_add_child(OBJECT(qdev_get_machine()), TYPE_FW_CFG,
993 qdev_init_nofail(dev);
995 sbd = SYS_BUS_DEVICE(dev);
996 sysbus_mmio_map(sbd, 0, ctl_addr);
997 sysbus_mmio_map(sbd, 1, data_addr);
1001 if (s->dma_enabled) {
1004 sysbus_mmio_map(sbd, 2, dma_addr);
1010 FWCfgState *fw_cfg_init_mem(hwaddr ctl_addr, hwaddr data_addr)
1012 return fw_cfg_init_mem_wide(ctl_addr, data_addr,
1013 fw_cfg_data_mem_ops.valid.max_access_size,
1018 FWCfgState *fw_cfg_find(void)
1020 /* Returns NULL unless there is exactly one fw_cfg device */
1021 return FW_CFG(object_resolve_path_type("", TYPE_FW_CFG, NULL));
1025 static void fw_cfg_class_init(ObjectClass *klass, void *data)
1027 DeviceClass *dc = DEVICE_CLASS(klass);
1029 dc->reset = fw_cfg_reset;
1030 dc->vmsd = &vmstate_fw_cfg;
1033 static const TypeInfo fw_cfg_info = {
1034 .name = TYPE_FW_CFG,
1035 .parent = TYPE_SYS_BUS_DEVICE,
1037 .instance_size = sizeof(FWCfgState),
1038 .class_init = fw_cfg_class_init,
1041 static void fw_cfg_file_slots_allocate(FWCfgState *s, Error **errp)
1043 uint16_t file_slots_max;
1045 if (fw_cfg_file_slots(s) < FW_CFG_FILE_SLOTS_MIN) {
1046 error_setg(errp, "\"file_slots\" must be at least 0x%x",
1047 FW_CFG_FILE_SLOTS_MIN);
1051 /* (UINT16_MAX & FW_CFG_ENTRY_MASK) is the highest inclusive selector value
1052 * that we permit. The actual (exclusive) value coming from the
1053 * configuration is (FW_CFG_FILE_FIRST + fw_cfg_file_slots(s)). */
1054 file_slots_max = (UINT16_MAX & FW_CFG_ENTRY_MASK) - FW_CFG_FILE_FIRST + 1;
1055 if (fw_cfg_file_slots(s) > file_slots_max) {
1056 error_setg(errp, "\"file_slots\" must not exceed 0x%" PRIx16,
1061 s->entries[0] = g_new0(FWCfgEntry, fw_cfg_max_entry(s));
1062 s->entries[1] = g_new0(FWCfgEntry, fw_cfg_max_entry(s));
1063 s->entry_order = g_new0(int, fw_cfg_max_entry(s));
1066 static Property fw_cfg_io_properties[] = {
1067 DEFINE_PROP_BOOL("dma_enabled", FWCfgIoState, parent_obj.dma_enabled,
1069 DEFINE_PROP_UINT16("x-file-slots", FWCfgIoState, parent_obj.file_slots,
1070 FW_CFG_FILE_SLOTS_DFLT),
1071 DEFINE_PROP_END_OF_LIST(),
1074 static void fw_cfg_io_realize(DeviceState *dev, Error **errp)
1076 FWCfgIoState *s = FW_CFG_IO(dev);
1077 Error *local_err = NULL;
1079 fw_cfg_file_slots_allocate(FW_CFG(s), &local_err);
1081 error_propagate(errp, local_err);
1085 /* when using port i/o, the 8-bit data register ALWAYS overlaps
1086 * with half of the 16-bit control register. Hence, the total size
1087 * of the i/o region used is FW_CFG_CTL_SIZE */
1088 memory_region_init_io(&s->comb_iomem, OBJECT(s), &fw_cfg_comb_mem_ops,
1089 FW_CFG(s), "fwcfg", FW_CFG_CTL_SIZE);
1091 if (FW_CFG(s)->dma_enabled) {
1092 memory_region_init_io(&FW_CFG(s)->dma_iomem, OBJECT(s),
1093 &fw_cfg_dma_mem_ops, FW_CFG(s), "fwcfg.dma",
1094 sizeof(dma_addr_t));
1097 fw_cfg_common_realize(dev, errp);
1100 static void fw_cfg_io_class_init(ObjectClass *klass, void *data)
1102 DeviceClass *dc = DEVICE_CLASS(klass);
1104 dc->realize = fw_cfg_io_realize;
1105 dc->props = fw_cfg_io_properties;
1108 static const TypeInfo fw_cfg_io_info = {
1109 .name = TYPE_FW_CFG_IO,
1110 .parent = TYPE_FW_CFG,
1111 .instance_size = sizeof(FWCfgIoState),
1112 .class_init = fw_cfg_io_class_init,
1116 static Property fw_cfg_mem_properties[] = {
1117 DEFINE_PROP_UINT32("data_width", FWCfgMemState, data_width, -1),
1118 DEFINE_PROP_BOOL("dma_enabled", FWCfgMemState, parent_obj.dma_enabled,
1120 DEFINE_PROP_UINT16("x-file-slots", FWCfgMemState, parent_obj.file_slots,
1121 FW_CFG_FILE_SLOTS_DFLT),
1122 DEFINE_PROP_END_OF_LIST(),
1125 static void fw_cfg_mem_realize(DeviceState *dev, Error **errp)
1127 FWCfgMemState *s = FW_CFG_MEM(dev);
1128 SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
1129 const MemoryRegionOps *data_ops = &fw_cfg_data_mem_ops;
1130 Error *local_err = NULL;
1132 fw_cfg_file_slots_allocate(FW_CFG(s), &local_err);
1134 error_propagate(errp, local_err);
1138 memory_region_init_io(&s->ctl_iomem, OBJECT(s), &fw_cfg_ctl_mem_ops,
1139 FW_CFG(s), "fwcfg.ctl", FW_CFG_CTL_SIZE);
1140 sysbus_init_mmio(sbd, &s->ctl_iomem);
1142 if (s->data_width > data_ops->valid.max_access_size) {
1143 /* memberwise copy because the "old_mmio" member is const */
1144 s->wide_data_ops.read = data_ops->read;
1145 s->wide_data_ops.write = data_ops->write;
1146 s->wide_data_ops.endianness = data_ops->endianness;
1147 s->wide_data_ops.valid = data_ops->valid;
1148 s->wide_data_ops.impl = data_ops->impl;
1150 s->wide_data_ops.valid.max_access_size = s->data_width;
1151 s->wide_data_ops.impl.max_access_size = s->data_width;
1152 data_ops = &s->wide_data_ops;
1154 memory_region_init_io(&s->data_iomem, OBJECT(s), data_ops, FW_CFG(s),
1155 "fwcfg.data", data_ops->valid.max_access_size);
1156 sysbus_init_mmio(sbd, &s->data_iomem);
1158 if (FW_CFG(s)->dma_enabled) {
1159 memory_region_init_io(&FW_CFG(s)->dma_iomem, OBJECT(s),
1160 &fw_cfg_dma_mem_ops, FW_CFG(s), "fwcfg.dma",
1161 sizeof(dma_addr_t));
1162 sysbus_init_mmio(sbd, &FW_CFG(s)->dma_iomem);
1165 fw_cfg_common_realize(dev, errp);
1168 static void fw_cfg_mem_class_init(ObjectClass *klass, void *data)
1170 DeviceClass *dc = DEVICE_CLASS(klass);
1172 dc->realize = fw_cfg_mem_realize;
1173 dc->props = fw_cfg_mem_properties;
1176 static const TypeInfo fw_cfg_mem_info = {
1177 .name = TYPE_FW_CFG_MEM,
1178 .parent = TYPE_FW_CFG,
1179 .instance_size = sizeof(FWCfgMemState),
1180 .class_init = fw_cfg_mem_class_init,
1184 static void fw_cfg_register_types(void)
1186 type_register_static(&fw_cfg_info);
1187 type_register_static(&fw_cfg_io_info);
1188 type_register_static(&fw_cfg_mem_info);
1191 type_init(fw_cfg_register_types)