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 FW_CFG_NAME "fw_cfg"
41 #define FW_CFG_PATH "/machine/" FW_CFG_NAME
43 #define TYPE_FW_CFG "fw_cfg"
44 #define TYPE_FW_CFG_IO "fw_cfg_io"
45 #define TYPE_FW_CFG_MEM "fw_cfg_mem"
47 #define FW_CFG(obj) OBJECT_CHECK(FWCfgState, (obj), TYPE_FW_CFG)
48 #define FW_CFG_IO(obj) OBJECT_CHECK(FWCfgIoState, (obj), TYPE_FW_CFG_IO)
49 #define FW_CFG_MEM(obj) OBJECT_CHECK(FWCfgMemState, (obj), TYPE_FW_CFG_MEM)
51 /* FW_CFG_VERSION bits */
52 #define FW_CFG_VERSION 0x01
53 #define FW_CFG_VERSION_DMA 0x02
55 /* FW_CFG_DMA_CONTROL bits */
56 #define FW_CFG_DMA_CTL_ERROR 0x01
57 #define FW_CFG_DMA_CTL_READ 0x02
58 #define FW_CFG_DMA_CTL_SKIP 0x04
59 #define FW_CFG_DMA_CTL_SELECT 0x08
60 #define FW_CFG_DMA_CTL_WRITE 0x10
62 #define FW_CFG_DMA_SIGNATURE 0x51454d5520434647ULL /* "QEMU CFG" */
64 typedef struct FWCfgEntry
{
68 void *callback_opaque
;
69 FWCfgReadCallback read_callback
;
74 SysBusDevice parent_obj
;
78 FWCfgEntry
*entries
[2];
83 Notifier machine_ready
;
85 int fw_cfg_order_override
;
90 MemoryRegion dma_iomem
;
95 FWCfgState parent_obj
;
98 MemoryRegion comb_iomem
;
101 struct FWCfgMemState
{
103 FWCfgState parent_obj
;
106 MemoryRegion ctl_iomem
, data_iomem
;
108 MemoryRegionOps wide_data_ops
;
114 static char *read_splashfile(char *filename
, gsize
*file_sizep
,
121 unsigned int filehead
;
124 res
= g_file_get_contents(filename
, &content
, file_sizep
, &err
);
126 error_report("failed to read splash file '%s'", filename
);
131 /* check file size */
132 if (*file_sizep
< 30) {
137 filehead
= ((content
[0] & 0xff) + (content
[1] << 8)) & 0xffff;
138 if (filehead
== 0xd8ff) {
139 file_type
= JPG_FILE
;
140 } else if (filehead
== 0x4d42) {
141 file_type
= BMP_FILE
;
147 if (file_type
== BMP_FILE
) {
148 bmp_bpp
= (content
[28] + (content
[29] << 8)) & 0xffff;
155 *file_typep
= file_type
;
160 error_report("splash file '%s' format not recognized; must be JPEG "
161 "or 24 bit BMP", filename
);
166 static void fw_cfg_bootsplash(FWCfgState
*s
)
168 int boot_splash_time
= -1;
169 const char *boot_splash_filename
= NULL
;
171 char *filename
, *file_data
;
176 /* get user configuration */
177 QemuOptsList
*plist
= qemu_find_opts("boot-opts");
178 QemuOpts
*opts
= QTAILQ_FIRST(&plist
->head
);
180 temp
= qemu_opt_get(opts
, "splash");
182 boot_splash_filename
= temp
;
184 temp
= qemu_opt_get(opts
, "splash-time");
187 boot_splash_time
= strtol(p
, &p
, 10);
191 /* insert splash time if user configurated */
192 if (boot_splash_time
>= 0) {
193 /* validate the input */
194 if (boot_splash_time
> 0xffff) {
195 error_report("splash time is big than 65535, force it to 65535.");
196 boot_splash_time
= 0xffff;
198 /* use little endian format */
199 qemu_extra_params_fw
[0] = (uint8_t)(boot_splash_time
& 0xff);
200 qemu_extra_params_fw
[1] = (uint8_t)((boot_splash_time
>> 8) & 0xff);
201 fw_cfg_add_file(s
, "etc/boot-menu-wait", qemu_extra_params_fw
, 2);
204 /* insert splash file if user configurated */
205 if (boot_splash_filename
!= NULL
) {
206 filename
= qemu_find_file(QEMU_FILE_TYPE_BIOS
, boot_splash_filename
);
207 if (filename
== NULL
) {
208 error_report("failed to find file '%s'.", boot_splash_filename
);
212 /* loading file data */
213 file_data
= read_splashfile(filename
, &file_size
, &file_type
);
214 if (file_data
== NULL
) {
218 g_free(boot_splash_filedata
);
219 boot_splash_filedata
= (uint8_t *)file_data
;
220 boot_splash_filedata_size
= file_size
;
223 if (file_type
== JPG_FILE
) {
224 fw_cfg_add_file(s
, "bootsplash.jpg",
225 boot_splash_filedata
, boot_splash_filedata_size
);
227 fw_cfg_add_file(s
, "bootsplash.bmp",
228 boot_splash_filedata
, boot_splash_filedata_size
);
234 static void fw_cfg_reboot(FWCfgState
*s
)
236 int reboot_timeout
= -1;
240 /* get user configuration */
241 QemuOptsList
*plist
= qemu_find_opts("boot-opts");
242 QemuOpts
*opts
= QTAILQ_FIRST(&plist
->head
);
244 temp
= qemu_opt_get(opts
, "reboot-timeout");
247 reboot_timeout
= strtol(p
, &p
, 10);
250 /* validate the input */
251 if (reboot_timeout
> 0xffff) {
252 error_report("reboot timeout is larger than 65535, force it to 65535.");
253 reboot_timeout
= 0xffff;
255 fw_cfg_add_file(s
, "etc/boot-fail-wait", g_memdup(&reboot_timeout
, 4), 4);
258 static void fw_cfg_write(FWCfgState
*s
, uint8_t value
)
260 /* nothing, write support removed in QEMU v2.4+ */
263 static inline uint16_t fw_cfg_file_slots(const FWCfgState
*s
)
265 return s
->file_slots
;
268 /* Note: this function returns an exclusive limit. */
269 static inline uint32_t fw_cfg_max_entry(const FWCfgState
*s
)
271 return FW_CFG_FILE_FIRST
+ fw_cfg_file_slots(s
);
274 static int fw_cfg_select(FWCfgState
*s
, uint16_t key
)
280 if ((key
& FW_CFG_ENTRY_MASK
) >= fw_cfg_max_entry(s
)) {
281 s
->cur_entry
= FW_CFG_INVALID
;
286 /* entry successfully selected, now run callback if present */
287 arch
= !!(key
& FW_CFG_ARCH_LOCAL
);
288 e
= &s
->entries
[arch
][key
& FW_CFG_ENTRY_MASK
];
289 if (e
->read_callback
) {
290 e
->read_callback(e
->callback_opaque
);
294 trace_fw_cfg_select(s
, key
, ret
);
298 static uint64_t fw_cfg_data_read(void *opaque
, hwaddr addr
, unsigned size
)
300 FWCfgState
*s
= opaque
;
301 int arch
= !!(s
->cur_entry
& FW_CFG_ARCH_LOCAL
);
302 FWCfgEntry
*e
= (s
->cur_entry
== FW_CFG_INVALID
) ? NULL
:
303 &s
->entries
[arch
][s
->cur_entry
& FW_CFG_ENTRY_MASK
];
306 assert(size
> 0 && size
<= sizeof(value
));
307 if (s
->cur_entry
!= FW_CFG_INVALID
&& e
->data
&& s
->cur_offset
< e
->len
) {
308 /* The least significant 'size' bytes of the return value are
309 * expected to contain a string preserving portion of the item
310 * data, padded with zeros on the right in case we run out early.
311 * In technical terms, we're composing the host-endian representation
312 * of the big endian interpretation of the fw_cfg string.
315 value
= (value
<< 8) | e
->data
[s
->cur_offset
++];
316 } while (--size
&& s
->cur_offset
< e
->len
);
317 /* If size is still not zero, we *did* run out early, so continue
318 * left-shifting, to add the appropriate number of padding zeros
324 trace_fw_cfg_read(s
, value
);
328 static void fw_cfg_data_mem_write(void *opaque
, hwaddr addr
,
329 uint64_t value
, unsigned size
)
331 FWCfgState
*s
= opaque
;
335 fw_cfg_write(s
, value
>> (8 * --i
));
339 static void fw_cfg_dma_transfer(FWCfgState
*s
)
345 int read
= 0, write
= 0;
348 /* Reset the address before the next access */
349 dma_addr
= s
->dma_addr
;
352 if (dma_memory_read(s
->dma_as
, dma_addr
, &dma
, sizeof(dma
))) {
353 stl_be_dma(s
->dma_as
, dma_addr
+ offsetof(FWCfgDmaAccess
, control
),
354 FW_CFG_DMA_CTL_ERROR
);
358 dma
.address
= be64_to_cpu(dma
.address
);
359 dma
.length
= be32_to_cpu(dma
.length
);
360 dma
.control
= be32_to_cpu(dma
.control
);
362 if (dma
.control
& FW_CFG_DMA_CTL_SELECT
) {
363 fw_cfg_select(s
, dma
.control
>> 16);
366 arch
= !!(s
->cur_entry
& FW_CFG_ARCH_LOCAL
);
367 e
= (s
->cur_entry
== FW_CFG_INVALID
) ? NULL
:
368 &s
->entries
[arch
][s
->cur_entry
& FW_CFG_ENTRY_MASK
];
370 if (dma
.control
& FW_CFG_DMA_CTL_READ
) {
373 } else if (dma
.control
& FW_CFG_DMA_CTL_WRITE
) {
376 } else if (dma
.control
& FW_CFG_DMA_CTL_SKIP
) {
385 while (dma
.length
> 0 && !(dma
.control
& FW_CFG_DMA_CTL_ERROR
)) {
386 if (s
->cur_entry
== FW_CFG_INVALID
|| !e
->data
||
387 s
->cur_offset
>= e
->len
) {
390 /* If the access is not a read access, it will be a skip access,
394 if (dma_memory_set(s
->dma_as
, dma
.address
, 0, len
)) {
395 dma
.control
|= FW_CFG_DMA_CTL_ERROR
;
399 dma
.control
|= FW_CFG_DMA_CTL_ERROR
;
402 if (dma
.length
<= (e
->len
- s
->cur_offset
)) {
405 len
= (e
->len
- s
->cur_offset
);
408 /* If the access is not a read access, it will be a skip access,
412 if (dma_memory_write(s
->dma_as
, dma
.address
,
413 &e
->data
[s
->cur_offset
], len
)) {
414 dma
.control
|= FW_CFG_DMA_CTL_ERROR
;
418 if (!e
->allow_write
||
420 dma_memory_read(s
->dma_as
, dma
.address
,
421 &e
->data
[s
->cur_offset
], len
)) {
422 dma
.control
|= FW_CFG_DMA_CTL_ERROR
;
426 s
->cur_offset
+= len
;
434 stl_be_dma(s
->dma_as
, dma_addr
+ offsetof(FWCfgDmaAccess
, control
),
437 trace_fw_cfg_read(s
, 0);
440 static uint64_t fw_cfg_dma_mem_read(void *opaque
, hwaddr addr
,
443 /* Return a signature value (and handle various read sizes) */
444 return extract64(FW_CFG_DMA_SIGNATURE
, (8 - addr
- size
) * 8, size
* 8);
447 static void fw_cfg_dma_mem_write(void *opaque
, hwaddr addr
,
448 uint64_t value
, unsigned size
)
450 FWCfgState
*s
= opaque
;
454 /* FWCfgDmaAccess high address */
455 s
->dma_addr
= value
<< 32;
456 } else if (addr
== 4) {
457 /* FWCfgDmaAccess low address */
458 s
->dma_addr
|= value
;
459 fw_cfg_dma_transfer(s
);
461 } else if (size
== 8 && addr
== 0) {
463 fw_cfg_dma_transfer(s
);
467 static bool fw_cfg_dma_mem_valid(void *opaque
, hwaddr addr
,
468 unsigned size
, bool is_write
)
470 return !is_write
|| ((size
== 4 && (addr
== 0 || addr
== 4)) ||
471 (size
== 8 && addr
== 0));
474 static bool fw_cfg_data_mem_valid(void *opaque
, hwaddr addr
,
475 unsigned size
, bool is_write
)
480 static void fw_cfg_ctl_mem_write(void *opaque
, hwaddr addr
,
481 uint64_t value
, unsigned size
)
483 fw_cfg_select(opaque
, (uint16_t)value
);
486 static bool fw_cfg_ctl_mem_valid(void *opaque
, hwaddr addr
,
487 unsigned size
, bool is_write
)
489 return is_write
&& size
== 2;
492 static void fw_cfg_comb_write(void *opaque
, hwaddr addr
,
493 uint64_t value
, unsigned size
)
497 fw_cfg_write(opaque
, (uint8_t)value
);
500 fw_cfg_select(opaque
, (uint16_t)value
);
505 static bool fw_cfg_comb_valid(void *opaque
, hwaddr addr
,
506 unsigned size
, bool is_write
)
508 return (size
== 1) || (is_write
&& size
== 2);
511 static const MemoryRegionOps fw_cfg_ctl_mem_ops
= {
512 .write
= fw_cfg_ctl_mem_write
,
513 .endianness
= DEVICE_BIG_ENDIAN
,
514 .valid
.accepts
= fw_cfg_ctl_mem_valid
,
517 static const MemoryRegionOps fw_cfg_data_mem_ops
= {
518 .read
= fw_cfg_data_read
,
519 .write
= fw_cfg_data_mem_write
,
520 .endianness
= DEVICE_BIG_ENDIAN
,
522 .min_access_size
= 1,
523 .max_access_size
= 1,
524 .accepts
= fw_cfg_data_mem_valid
,
528 static const MemoryRegionOps fw_cfg_comb_mem_ops
= {
529 .read
= fw_cfg_data_read
,
530 .write
= fw_cfg_comb_write
,
531 .endianness
= DEVICE_LITTLE_ENDIAN
,
532 .valid
.accepts
= fw_cfg_comb_valid
,
535 static const MemoryRegionOps fw_cfg_dma_mem_ops
= {
536 .read
= fw_cfg_dma_mem_read
,
537 .write
= fw_cfg_dma_mem_write
,
538 .endianness
= DEVICE_BIG_ENDIAN
,
539 .valid
.accepts
= fw_cfg_dma_mem_valid
,
540 .valid
.max_access_size
= 8,
541 .impl
.max_access_size
= 8,
544 static void fw_cfg_reset(DeviceState
*d
)
546 FWCfgState
*s
= FW_CFG(d
);
548 /* we never register a read callback for FW_CFG_SIGNATURE */
549 fw_cfg_select(s
, FW_CFG_SIGNATURE
);
552 /* Save restore 32 bit int as uint16_t
553 This is a Big hack, but it is how the old state did it.
554 Or we broke compatibility in the state, or we can't use struct tm
557 static int get_uint32_as_uint16(QEMUFile
*f
, void *pv
, size_t size
,
561 *v
= qemu_get_be16(f
);
565 static int put_unused(QEMUFile
*f
, void *pv
, size_t size
, VMStateField
*field
,
568 fprintf(stderr
, "uint32_as_uint16 is only used for backward compatibility.\n");
569 fprintf(stderr
, "This functions shouldn't be called.\n");
574 static const VMStateInfo vmstate_hack_uint32_as_uint16
= {
575 .name
= "int32_as_uint16",
576 .get
= get_uint32_as_uint16
,
580 #define VMSTATE_UINT16_HACK(_f, _s, _t) \
581 VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_hack_uint32_as_uint16, uint32_t)
584 static bool is_version_1(void *opaque
, int version_id
)
586 return version_id
== 1;
589 bool fw_cfg_dma_enabled(void *opaque
)
591 FWCfgState
*s
= opaque
;
593 return s
->dma_enabled
;
596 static const VMStateDescription vmstate_fw_cfg_dma
= {
597 .name
= "fw_cfg/dma",
598 .needed
= fw_cfg_dma_enabled
,
599 .fields
= (VMStateField
[]) {
600 VMSTATE_UINT64(dma_addr
, FWCfgState
),
601 VMSTATE_END_OF_LIST()
605 static const VMStateDescription vmstate_fw_cfg
= {
608 .minimum_version_id
= 1,
609 .fields
= (VMStateField
[]) {
610 VMSTATE_UINT16(cur_entry
, FWCfgState
),
611 VMSTATE_UINT16_HACK(cur_offset
, FWCfgState
, is_version_1
),
612 VMSTATE_UINT32_V(cur_offset
, FWCfgState
, 2),
613 VMSTATE_END_OF_LIST()
615 .subsections
= (const VMStateDescription
*[]) {
621 static void fw_cfg_add_bytes_read_callback(FWCfgState
*s
, uint16_t key
,
622 FWCfgReadCallback callback
,
623 void *callback_opaque
,
624 void *data
, size_t len
,
627 int arch
= !!(key
& FW_CFG_ARCH_LOCAL
);
629 key
&= FW_CFG_ENTRY_MASK
;
631 assert(key
< fw_cfg_max_entry(s
) && len
< UINT32_MAX
);
632 assert(s
->entries
[arch
][key
].data
== NULL
); /* avoid key conflict */
634 s
->entries
[arch
][key
].data
= data
;
635 s
->entries
[arch
][key
].len
= (uint32_t)len
;
636 s
->entries
[arch
][key
].read_callback
= callback
;
637 s
->entries
[arch
][key
].callback_opaque
= callback_opaque
;
638 s
->entries
[arch
][key
].allow_write
= !read_only
;
641 static void *fw_cfg_modify_bytes_read(FWCfgState
*s
, uint16_t key
,
642 void *data
, size_t len
)
645 int arch
= !!(key
& FW_CFG_ARCH_LOCAL
);
647 key
&= FW_CFG_ENTRY_MASK
;
649 assert(key
< fw_cfg_max_entry(s
) && len
< UINT32_MAX
);
651 /* return the old data to the function caller, avoid memory leak */
652 ptr
= s
->entries
[arch
][key
].data
;
653 s
->entries
[arch
][key
].data
= data
;
654 s
->entries
[arch
][key
].len
= len
;
655 s
->entries
[arch
][key
].callback_opaque
= NULL
;
656 s
->entries
[arch
][key
].allow_write
= false;
661 void fw_cfg_add_bytes(FWCfgState
*s
, uint16_t key
, void *data
, size_t len
)
663 fw_cfg_add_bytes_read_callback(s
, key
, NULL
, NULL
, data
, len
, true);
666 void fw_cfg_add_string(FWCfgState
*s
, uint16_t key
, const char *value
)
668 size_t sz
= strlen(value
) + 1;
670 fw_cfg_add_bytes(s
, key
, g_memdup(value
, sz
), sz
);
673 void fw_cfg_add_i16(FWCfgState
*s
, uint16_t key
, uint16_t value
)
677 copy
= g_malloc(sizeof(value
));
678 *copy
= cpu_to_le16(value
);
679 fw_cfg_add_bytes(s
, key
, copy
, sizeof(value
));
682 void fw_cfg_modify_i16(FWCfgState
*s
, uint16_t key
, uint16_t value
)
684 uint16_t *copy
, *old
;
686 copy
= g_malloc(sizeof(value
));
687 *copy
= cpu_to_le16(value
);
688 old
= fw_cfg_modify_bytes_read(s
, key
, copy
, sizeof(value
));
692 void fw_cfg_add_i32(FWCfgState
*s
, uint16_t key
, uint32_t value
)
696 copy
= g_malloc(sizeof(value
));
697 *copy
= cpu_to_le32(value
);
698 fw_cfg_add_bytes(s
, key
, copy
, sizeof(value
));
701 void fw_cfg_add_i64(FWCfgState
*s
, uint16_t key
, uint64_t value
)
705 copy
= g_malloc(sizeof(value
));
706 *copy
= cpu_to_le64(value
);
707 fw_cfg_add_bytes(s
, key
, copy
, sizeof(value
));
710 void fw_cfg_set_order_override(FWCfgState
*s
, int order
)
712 assert(s
->fw_cfg_order_override
== 0);
713 s
->fw_cfg_order_override
= order
;
716 void fw_cfg_reset_order_override(FWCfgState
*s
)
718 assert(s
->fw_cfg_order_override
!= 0);
719 s
->fw_cfg_order_override
= 0;
723 * This is the legacy order list. For legacy systems, files are in
724 * the fw_cfg in the order defined below, by the "order" value. Note
725 * that some entries (VGA ROMs, NIC option ROMS, etc.) go into a
726 * specific area, but there may be more than one and they occur in the
727 * order that the user specifies them on the command line. Those are
728 * handled in a special manner, using the order override above.
730 * For non-legacy, the files are sorted by filename to avoid this kind
731 * of complexity in the future.
733 * This is only for x86, other arches don't implement versioning so
734 * they won't set legacy mode.
740 { "etc/boot-menu-wait", 10 },
741 { "bootsplash.jpg", 11 },
742 { "bootsplash.bmp", 12 },
743 { "etc/boot-fail-wait", 15 },
744 { "etc/smbios/smbios-tables", 20 },
745 { "etc/smbios/smbios-anchor", 30 },
747 { "etc/reserved-memory-end", 50 },
748 { "genroms/kvmvapic.bin", 55 },
749 { "genroms/linuxboot.bin", 60 },
750 { }, /* VGA ROMs from pc_vga_init come here, 70. */
751 { }, /* NIC option ROMs from pc_nic_init come here, 80. */
752 { "etc/system-states", 90 },
753 { }, /* User ROMs come here, 100. */
754 { }, /* Device FW comes here, 110. */
755 { "etc/extra-pci-roots", 120 },
756 { "etc/acpi/tables", 130 },
757 { "etc/table-loader", 140 },
758 { "etc/tpm/log", 150 },
759 { "etc/acpi/rsdp", 160 },
760 { "bootorder", 170 },
762 #define FW_CFG_ORDER_OVERRIDE_LAST 200
765 static int get_fw_cfg_order(FWCfgState
*s
, const char *name
)
769 if (s
->fw_cfg_order_override
> 0) {
770 return s
->fw_cfg_order_override
;
773 for (i
= 0; i
< ARRAY_SIZE(fw_cfg_order
); i
++) {
774 if (fw_cfg_order
[i
].name
== NULL
) {
778 if (strcmp(name
, fw_cfg_order
[i
].name
) == 0) {
779 return fw_cfg_order
[i
].order
;
783 /* Stick unknown stuff at the end. */
784 warn_report("Unknown firmware file in legacy mode: %s", name
);
785 return FW_CFG_ORDER_OVERRIDE_LAST
;
788 void fw_cfg_add_file_callback(FWCfgState
*s
, const char *filename
,
789 FWCfgReadCallback callback
, void *callback_opaque
,
790 void *data
, size_t len
, bool read_only
)
794 MachineClass
*mc
= MACHINE_GET_CLASS(qdev_get_machine());
798 dsize
= sizeof(uint32_t) + sizeof(FWCfgFile
) * fw_cfg_file_slots(s
);
799 s
->files
= g_malloc0(dsize
);
800 fw_cfg_add_bytes(s
, FW_CFG_FILE_DIR
, s
->files
, dsize
);
803 count
= be32_to_cpu(s
->files
->count
);
804 assert(count
< fw_cfg_file_slots(s
));
806 /* Find the insertion point. */
807 if (mc
->legacy_fw_cfg_order
) {
809 * Sort by order. For files with the same order, we keep them
810 * in the sequence in which they were added.
812 order
= get_fw_cfg_order(s
, filename
);
814 index
> 0 && order
< s
->entry_order
[index
- 1];
817 /* Sort by file name. */
819 index
> 0 && strcmp(filename
, s
->files
->f
[index
- 1].name
) < 0;
824 * Move all the entries from the index point and after down one
825 * to create a slot for the new entry. Because calculations are
826 * being done with the index, make it so that "i" is the current
827 * index and "i - 1" is the one being copied from, thus the
828 * unusual start and end in the for statement.
830 for (i
= count
+ 1; i
> index
; i
--) {
831 s
->files
->f
[i
] = s
->files
->f
[i
- 1];
832 s
->files
->f
[i
].select
= cpu_to_be16(FW_CFG_FILE_FIRST
+ i
);
833 s
->entries
[0][FW_CFG_FILE_FIRST
+ i
] =
834 s
->entries
[0][FW_CFG_FILE_FIRST
+ i
- 1];
835 s
->entry_order
[i
] = s
->entry_order
[i
- 1];
838 memset(&s
->files
->f
[index
], 0, sizeof(FWCfgFile
));
839 memset(&s
->entries
[0][FW_CFG_FILE_FIRST
+ index
], 0, sizeof(FWCfgEntry
));
841 pstrcpy(s
->files
->f
[index
].name
, sizeof(s
->files
->f
[index
].name
), filename
);
842 for (i
= 0; i
<= count
; i
++) {
844 strcmp(s
->files
->f
[index
].name
, s
->files
->f
[i
].name
) == 0) {
845 error_report("duplicate fw_cfg file name: %s",
846 s
->files
->f
[index
].name
);
851 fw_cfg_add_bytes_read_callback(s
, FW_CFG_FILE_FIRST
+ index
,
852 callback
, callback_opaque
, data
, len
,
855 s
->files
->f
[index
].size
= cpu_to_be32(len
);
856 s
->files
->f
[index
].select
= cpu_to_be16(FW_CFG_FILE_FIRST
+ index
);
857 s
->entry_order
[index
] = order
;
858 trace_fw_cfg_add_file(s
, index
, s
->files
->f
[index
].name
, len
);
860 s
->files
->count
= cpu_to_be32(count
+1);
863 void fw_cfg_add_file(FWCfgState
*s
, const char *filename
,
864 void *data
, size_t len
)
866 fw_cfg_add_file_callback(s
, filename
, NULL
, NULL
, data
, len
, true);
869 void *fw_cfg_modify_file(FWCfgState
*s
, const char *filename
,
870 void *data
, size_t len
)
877 index
= be32_to_cpu(s
->files
->count
);
878 assert(index
< fw_cfg_file_slots(s
));
880 for (i
= 0; i
< index
; i
++) {
881 if (strcmp(filename
, s
->files
->f
[i
].name
) == 0) {
882 ptr
= fw_cfg_modify_bytes_read(s
, FW_CFG_FILE_FIRST
+ i
,
884 s
->files
->f
[i
].size
= cpu_to_be32(len
);
889 fw_cfg_add_file_callback(s
, filename
, NULL
, NULL
, data
, len
, true);
893 static void fw_cfg_machine_reset(void *opaque
)
897 FWCfgState
*s
= opaque
;
898 char *bootindex
= get_boot_devices_list(&len
, false);
900 ptr
= fw_cfg_modify_file(s
, "bootorder", (uint8_t *)bootindex
, len
);
904 static void fw_cfg_machine_ready(struct Notifier
*n
, void *data
)
906 FWCfgState
*s
= container_of(n
, FWCfgState
, machine_ready
);
907 qemu_register_reset(fw_cfg_machine_reset
, s
);
912 static void fw_cfg_init1(DeviceState
*dev
)
914 FWCfgState
*s
= FW_CFG(dev
);
915 MachineState
*machine
= MACHINE(qdev_get_machine());
916 uint32_t version
= FW_CFG_VERSION
;
918 assert(!object_resolve_path(FW_CFG_PATH
, NULL
));
920 object_property_add_child(OBJECT(machine
), FW_CFG_NAME
, OBJECT(s
), NULL
);
922 qdev_init_nofail(dev
);
924 fw_cfg_add_bytes(s
, FW_CFG_SIGNATURE
, (char *)"QEMU", 4);
925 fw_cfg_add_bytes(s
, FW_CFG_UUID
, &qemu_uuid
, 16);
926 fw_cfg_add_i16(s
, FW_CFG_NOGRAPHIC
, (uint16_t)!machine
->enable_graphics
);
927 fw_cfg_add_i16(s
, FW_CFG_BOOT_MENU
, (uint16_t)boot_menu
);
928 fw_cfg_bootsplash(s
);
931 if (s
->dma_enabled
) {
932 version
|= FW_CFG_VERSION_DMA
;
935 fw_cfg_add_i32(s
, FW_CFG_ID
, version
);
937 s
->machine_ready
.notify
= fw_cfg_machine_ready
;
938 qemu_add_machine_init_done_notifier(&s
->machine_ready
);
941 FWCfgState
*fw_cfg_init_io_dma(uint32_t iobase
, uint32_t dma_iobase
,
942 AddressSpace
*dma_as
)
948 bool dma_requested
= dma_iobase
&& dma_as
;
950 dev
= qdev_create(NULL
, TYPE_FW_CFG_IO
);
951 if (!dma_requested
) {
952 qdev_prop_set_bit(dev
, "dma_enabled", false);
957 sbd
= SYS_BUS_DEVICE(dev
);
958 ios
= FW_CFG_IO(dev
);
959 sysbus_add_io(sbd
, iobase
, &ios
->comb_iomem
);
963 if (s
->dma_enabled
) {
964 /* 64 bits for the address field */
967 sysbus_add_io(sbd
, dma_iobase
, &s
->dma_iomem
);
973 FWCfgState
*fw_cfg_init_io(uint32_t iobase
)
975 return fw_cfg_init_io_dma(iobase
, 0, NULL
);
978 FWCfgState
*fw_cfg_init_mem_wide(hwaddr ctl_addr
,
979 hwaddr data_addr
, uint32_t data_width
,
980 hwaddr dma_addr
, AddressSpace
*dma_as
)
985 bool dma_requested
= dma_addr
&& dma_as
;
987 dev
= qdev_create(NULL
, TYPE_FW_CFG_MEM
);
988 qdev_prop_set_uint32(dev
, "data_width", data_width
);
989 if (!dma_requested
) {
990 qdev_prop_set_bit(dev
, "dma_enabled", false);
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
));
1024 static void fw_cfg_class_init(ObjectClass
*klass
, void *data
)
1026 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1028 dc
->reset
= fw_cfg_reset
;
1029 dc
->vmsd
= &vmstate_fw_cfg
;
1032 static const TypeInfo fw_cfg_info
= {
1033 .name
= TYPE_FW_CFG
,
1034 .parent
= TYPE_SYS_BUS_DEVICE
,
1036 .instance_size
= sizeof(FWCfgState
),
1037 .class_init
= fw_cfg_class_init
,
1040 static void fw_cfg_file_slots_allocate(FWCfgState
*s
, Error
**errp
)
1042 uint16_t file_slots_max
;
1044 if (fw_cfg_file_slots(s
) < FW_CFG_FILE_SLOTS_MIN
) {
1045 error_setg(errp
, "\"file_slots\" must be at least 0x%x",
1046 FW_CFG_FILE_SLOTS_MIN
);
1050 /* (UINT16_MAX & FW_CFG_ENTRY_MASK) is the highest inclusive selector value
1051 * that we permit. The actual (exclusive) value coming from the
1052 * configuration is (FW_CFG_FILE_FIRST + fw_cfg_file_slots(s)). */
1053 file_slots_max
= (UINT16_MAX
& FW_CFG_ENTRY_MASK
) - FW_CFG_FILE_FIRST
+ 1;
1054 if (fw_cfg_file_slots(s
) > file_slots_max
) {
1055 error_setg(errp
, "\"file_slots\" must not exceed 0x%" PRIx16
,
1060 s
->entries
[0] = g_new0(FWCfgEntry
, fw_cfg_max_entry(s
));
1061 s
->entries
[1] = g_new0(FWCfgEntry
, fw_cfg_max_entry(s
));
1062 s
->entry_order
= g_new0(int, fw_cfg_max_entry(s
));
1065 static Property fw_cfg_io_properties
[] = {
1066 DEFINE_PROP_BOOL("dma_enabled", FWCfgIoState
, parent_obj
.dma_enabled
,
1068 DEFINE_PROP_UINT16("x-file-slots", FWCfgIoState
, parent_obj
.file_slots
,
1069 FW_CFG_FILE_SLOTS_DFLT
),
1070 DEFINE_PROP_END_OF_LIST(),
1073 static void fw_cfg_io_realize(DeviceState
*dev
, Error
**errp
)
1075 FWCfgIoState
*s
= FW_CFG_IO(dev
);
1076 Error
*local_err
= NULL
;
1078 fw_cfg_file_slots_allocate(FW_CFG(s
), &local_err
);
1080 error_propagate(errp
, local_err
);
1084 /* when using port i/o, the 8-bit data register ALWAYS overlaps
1085 * with half of the 16-bit control register. Hence, the total size
1086 * of the i/o region used is FW_CFG_CTL_SIZE */
1087 memory_region_init_io(&s
->comb_iomem
, OBJECT(s
), &fw_cfg_comb_mem_ops
,
1088 FW_CFG(s
), "fwcfg", FW_CFG_CTL_SIZE
);
1090 if (FW_CFG(s
)->dma_enabled
) {
1091 memory_region_init_io(&FW_CFG(s
)->dma_iomem
, OBJECT(s
),
1092 &fw_cfg_dma_mem_ops
, FW_CFG(s
), "fwcfg.dma",
1093 sizeof(dma_addr_t
));
1097 static void fw_cfg_io_class_init(ObjectClass
*klass
, void *data
)
1099 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1101 dc
->realize
= fw_cfg_io_realize
;
1102 dc
->props
= fw_cfg_io_properties
;
1105 static const TypeInfo fw_cfg_io_info
= {
1106 .name
= TYPE_FW_CFG_IO
,
1107 .parent
= TYPE_FW_CFG
,
1108 .instance_size
= sizeof(FWCfgIoState
),
1109 .class_init
= fw_cfg_io_class_init
,
1113 static Property fw_cfg_mem_properties
[] = {
1114 DEFINE_PROP_UINT32("data_width", FWCfgMemState
, data_width
, -1),
1115 DEFINE_PROP_BOOL("dma_enabled", FWCfgMemState
, parent_obj
.dma_enabled
,
1117 DEFINE_PROP_UINT16("x-file-slots", FWCfgMemState
, parent_obj
.file_slots
,
1118 FW_CFG_FILE_SLOTS_DFLT
),
1119 DEFINE_PROP_END_OF_LIST(),
1122 static void fw_cfg_mem_realize(DeviceState
*dev
, Error
**errp
)
1124 FWCfgMemState
*s
= FW_CFG_MEM(dev
);
1125 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
1126 const MemoryRegionOps
*data_ops
= &fw_cfg_data_mem_ops
;
1127 Error
*local_err
= NULL
;
1129 fw_cfg_file_slots_allocate(FW_CFG(s
), &local_err
);
1131 error_propagate(errp
, local_err
);
1135 memory_region_init_io(&s
->ctl_iomem
, OBJECT(s
), &fw_cfg_ctl_mem_ops
,
1136 FW_CFG(s
), "fwcfg.ctl", FW_CFG_CTL_SIZE
);
1137 sysbus_init_mmio(sbd
, &s
->ctl_iomem
);
1139 if (s
->data_width
> data_ops
->valid
.max_access_size
) {
1140 /* memberwise copy because the "old_mmio" member is const */
1141 s
->wide_data_ops
.read
= data_ops
->read
;
1142 s
->wide_data_ops
.write
= data_ops
->write
;
1143 s
->wide_data_ops
.endianness
= data_ops
->endianness
;
1144 s
->wide_data_ops
.valid
= data_ops
->valid
;
1145 s
->wide_data_ops
.impl
= data_ops
->impl
;
1147 s
->wide_data_ops
.valid
.max_access_size
= s
->data_width
;
1148 s
->wide_data_ops
.impl
.max_access_size
= s
->data_width
;
1149 data_ops
= &s
->wide_data_ops
;
1151 memory_region_init_io(&s
->data_iomem
, OBJECT(s
), data_ops
, FW_CFG(s
),
1152 "fwcfg.data", data_ops
->valid
.max_access_size
);
1153 sysbus_init_mmio(sbd
, &s
->data_iomem
);
1155 if (FW_CFG(s
)->dma_enabled
) {
1156 memory_region_init_io(&FW_CFG(s
)->dma_iomem
, OBJECT(s
),
1157 &fw_cfg_dma_mem_ops
, FW_CFG(s
), "fwcfg.dma",
1158 sizeof(dma_addr_t
));
1159 sysbus_init_mmio(sbd
, &FW_CFG(s
)->dma_iomem
);
1163 static void fw_cfg_mem_class_init(ObjectClass
*klass
, void *data
)
1165 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1167 dc
->realize
= fw_cfg_mem_realize
;
1168 dc
->props
= fw_cfg_mem_properties
;
1171 static const TypeInfo fw_cfg_mem_info
= {
1172 .name
= TYPE_FW_CFG_MEM
,
1173 .parent
= TYPE_FW_CFG
,
1174 .instance_size
= sizeof(FWCfgMemState
),
1175 .class_init
= fw_cfg_mem_class_init
,
1179 static void fw_cfg_register_types(void)
1181 type_register_static(&fw_cfg_info
);
1182 type_register_static(&fw_cfg_io_info
);
1183 type_register_static(&fw_cfg_mem_info
);
1186 type_init(fw_cfg_register_types
)