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 /* FW_CFG_VERSION bits */
41 #define FW_CFG_VERSION 0x01
42 #define FW_CFG_VERSION_DMA 0x02
44 /* FW_CFG_DMA_CONTROL bits */
45 #define FW_CFG_DMA_CTL_ERROR 0x01
46 #define FW_CFG_DMA_CTL_READ 0x02
47 #define FW_CFG_DMA_CTL_SKIP 0x04
48 #define FW_CFG_DMA_CTL_SELECT 0x08
49 #define FW_CFG_DMA_CTL_WRITE 0x10
51 #define FW_CFG_DMA_SIGNATURE 0x51454d5520434647ULL /* "QEMU CFG" */
57 void *callback_opaque
;
58 FWCfgCallback select_cb
;
59 FWCfgWriteCallback write_cb
;
65 static char *read_splashfile(char *filename
, gsize
*file_sizep
,
72 unsigned int filehead
;
75 res
= g_file_get_contents(filename
, &content
, file_sizep
, &err
);
77 error_report("failed to read splash file '%s'", filename
);
83 if (*file_sizep
< 30) {
88 filehead
= ((content
[0] & 0xff) + (content
[1] << 8)) & 0xffff;
89 if (filehead
== 0xd8ff) {
91 } else if (filehead
== 0x4d42) {
98 if (file_type
== BMP_FILE
) {
99 bmp_bpp
= (content
[28] + (content
[29] << 8)) & 0xffff;
106 *file_typep
= file_type
;
111 error_report("splash file '%s' format not recognized; must be JPEG "
112 "or 24 bit BMP", filename
);
117 static void fw_cfg_bootsplash(FWCfgState
*s
)
119 int boot_splash_time
= -1;
120 const char *boot_splash_filename
= NULL
;
122 char *filename
, *file_data
;
127 /* get user configuration */
128 QemuOptsList
*plist
= qemu_find_opts("boot-opts");
129 QemuOpts
*opts
= QTAILQ_FIRST(&plist
->head
);
131 temp
= qemu_opt_get(opts
, "splash");
133 boot_splash_filename
= temp
;
135 temp
= qemu_opt_get(opts
, "splash-time");
138 boot_splash_time
= strtol(p
, &p
, 10);
142 /* insert splash time if user configurated */
143 if (boot_splash_time
>= 0) {
144 /* validate the input */
145 if (boot_splash_time
> 0xffff) {
146 error_report("splash time is big than 65535, force it to 65535.");
147 boot_splash_time
= 0xffff;
149 /* use little endian format */
150 qemu_extra_params_fw
[0] = (uint8_t)(boot_splash_time
& 0xff);
151 qemu_extra_params_fw
[1] = (uint8_t)((boot_splash_time
>> 8) & 0xff);
152 fw_cfg_add_file(s
, "etc/boot-menu-wait", qemu_extra_params_fw
, 2);
155 /* insert splash file if user configurated */
156 if (boot_splash_filename
!= NULL
) {
157 filename
= qemu_find_file(QEMU_FILE_TYPE_BIOS
, boot_splash_filename
);
158 if (filename
== NULL
) {
159 error_report("failed to find file '%s'.", boot_splash_filename
);
163 /* loading file data */
164 file_data
= read_splashfile(filename
, &file_size
, &file_type
);
165 if (file_data
== NULL
) {
169 g_free(boot_splash_filedata
);
170 boot_splash_filedata
= (uint8_t *)file_data
;
171 boot_splash_filedata_size
= file_size
;
174 if (file_type
== JPG_FILE
) {
175 fw_cfg_add_file(s
, "bootsplash.jpg",
176 boot_splash_filedata
, boot_splash_filedata_size
);
178 fw_cfg_add_file(s
, "bootsplash.bmp",
179 boot_splash_filedata
, boot_splash_filedata_size
);
185 static void fw_cfg_reboot(FWCfgState
*s
)
187 int reboot_timeout
= -1;
191 /* get user configuration */
192 QemuOptsList
*plist
= qemu_find_opts("boot-opts");
193 QemuOpts
*opts
= QTAILQ_FIRST(&plist
->head
);
195 temp
= qemu_opt_get(opts
, "reboot-timeout");
198 reboot_timeout
= strtol(p
, &p
, 10);
201 /* validate the input */
202 if (reboot_timeout
> 0xffff) {
203 error_report("reboot timeout is larger than 65535, force it to 65535.");
204 reboot_timeout
= 0xffff;
206 fw_cfg_add_file(s
, "etc/boot-fail-wait", g_memdup(&reboot_timeout
, 4), 4);
209 static void fw_cfg_write(FWCfgState
*s
, uint8_t value
)
211 /* nothing, write support removed in QEMU v2.4+ */
214 static inline uint16_t fw_cfg_file_slots(const FWCfgState
*s
)
216 return s
->file_slots
;
219 /* Note: this function returns an exclusive limit. */
220 static inline uint32_t fw_cfg_max_entry(const FWCfgState
*s
)
222 return FW_CFG_FILE_FIRST
+ fw_cfg_file_slots(s
);
225 static int fw_cfg_select(FWCfgState
*s
, uint16_t key
)
231 if ((key
& FW_CFG_ENTRY_MASK
) >= fw_cfg_max_entry(s
)) {
232 s
->cur_entry
= FW_CFG_INVALID
;
237 /* entry successfully selected, now run callback if present */
238 arch
= !!(key
& FW_CFG_ARCH_LOCAL
);
239 e
= &s
->entries
[arch
][key
& FW_CFG_ENTRY_MASK
];
241 e
->select_cb(e
->callback_opaque
);
245 trace_fw_cfg_select(s
, key
, ret
);
249 static uint64_t fw_cfg_data_read(void *opaque
, hwaddr addr
, unsigned size
)
251 FWCfgState
*s
= opaque
;
252 int arch
= !!(s
->cur_entry
& FW_CFG_ARCH_LOCAL
);
253 FWCfgEntry
*e
= (s
->cur_entry
== FW_CFG_INVALID
) ? NULL
:
254 &s
->entries
[arch
][s
->cur_entry
& FW_CFG_ENTRY_MASK
];
257 assert(size
> 0 && size
<= sizeof(value
));
258 if (s
->cur_entry
!= FW_CFG_INVALID
&& e
->data
&& s
->cur_offset
< e
->len
) {
259 /* The least significant 'size' bytes of the return value are
260 * expected to contain a string preserving portion of the item
261 * data, padded with zeros on the right in case we run out early.
262 * In technical terms, we're composing the host-endian representation
263 * of the big endian interpretation of the fw_cfg string.
266 value
= (value
<< 8) | e
->data
[s
->cur_offset
++];
267 } while (--size
&& s
->cur_offset
< e
->len
);
268 /* If size is still not zero, we *did* run out early, so continue
269 * left-shifting, to add the appropriate number of padding zeros
275 trace_fw_cfg_read(s
, value
);
279 static void fw_cfg_data_mem_write(void *opaque
, hwaddr addr
,
280 uint64_t value
, unsigned size
)
282 FWCfgState
*s
= opaque
;
286 fw_cfg_write(s
, value
>> (8 * --i
));
290 static void fw_cfg_dma_transfer(FWCfgState
*s
)
296 int read
= 0, write
= 0;
299 /* Reset the address before the next access */
300 dma_addr
= s
->dma_addr
;
303 if (dma_memory_read(s
->dma_as
, dma_addr
, &dma
, sizeof(dma
))) {
304 stl_be_dma(s
->dma_as
, dma_addr
+ offsetof(FWCfgDmaAccess
, control
),
305 FW_CFG_DMA_CTL_ERROR
);
309 dma
.address
= be64_to_cpu(dma
.address
);
310 dma
.length
= be32_to_cpu(dma
.length
);
311 dma
.control
= be32_to_cpu(dma
.control
);
313 if (dma
.control
& FW_CFG_DMA_CTL_SELECT
) {
314 fw_cfg_select(s
, dma
.control
>> 16);
317 arch
= !!(s
->cur_entry
& FW_CFG_ARCH_LOCAL
);
318 e
= (s
->cur_entry
== FW_CFG_INVALID
) ? NULL
:
319 &s
->entries
[arch
][s
->cur_entry
& FW_CFG_ENTRY_MASK
];
321 if (dma
.control
& FW_CFG_DMA_CTL_READ
) {
324 } else if (dma
.control
& FW_CFG_DMA_CTL_WRITE
) {
327 } else if (dma
.control
& FW_CFG_DMA_CTL_SKIP
) {
336 while (dma
.length
> 0 && !(dma
.control
& FW_CFG_DMA_CTL_ERROR
)) {
337 if (s
->cur_entry
== FW_CFG_INVALID
|| !e
->data
||
338 s
->cur_offset
>= e
->len
) {
341 /* If the access is not a read access, it will be a skip access,
345 if (dma_memory_set(s
->dma_as
, dma
.address
, 0, len
)) {
346 dma
.control
|= FW_CFG_DMA_CTL_ERROR
;
350 dma
.control
|= FW_CFG_DMA_CTL_ERROR
;
353 if (dma
.length
<= (e
->len
- s
->cur_offset
)) {
356 len
= (e
->len
- s
->cur_offset
);
359 /* If the access is not a read access, it will be a skip access,
363 if (dma_memory_write(s
->dma_as
, dma
.address
,
364 &e
->data
[s
->cur_offset
], len
)) {
365 dma
.control
|= FW_CFG_DMA_CTL_ERROR
;
369 if (!e
->allow_write
||
371 dma_memory_read(s
->dma_as
, dma
.address
,
372 &e
->data
[s
->cur_offset
], len
)) {
373 dma
.control
|= FW_CFG_DMA_CTL_ERROR
;
374 } else if (e
->write_cb
) {
375 e
->write_cb(e
->callback_opaque
, s
->cur_offset
, len
);
379 s
->cur_offset
+= len
;
387 stl_be_dma(s
->dma_as
, dma_addr
+ offsetof(FWCfgDmaAccess
, control
),
390 trace_fw_cfg_read(s
, 0);
393 static uint64_t fw_cfg_dma_mem_read(void *opaque
, hwaddr addr
,
396 /* Return a signature value (and handle various read sizes) */
397 return extract64(FW_CFG_DMA_SIGNATURE
, (8 - addr
- size
) * 8, size
* 8);
400 static void fw_cfg_dma_mem_write(void *opaque
, hwaddr addr
,
401 uint64_t value
, unsigned size
)
403 FWCfgState
*s
= opaque
;
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
);
414 } else if (size
== 8 && addr
== 0) {
416 fw_cfg_dma_transfer(s
);
420 static bool fw_cfg_dma_mem_valid(void *opaque
, hwaddr addr
,
421 unsigned size
, bool is_write
)
423 return !is_write
|| ((size
== 4 && (addr
== 0 || addr
== 4)) ||
424 (size
== 8 && addr
== 0));
427 static bool fw_cfg_data_mem_valid(void *opaque
, hwaddr addr
,
428 unsigned size
, bool is_write
)
433 static void fw_cfg_ctl_mem_write(void *opaque
, hwaddr addr
,
434 uint64_t value
, unsigned size
)
436 fw_cfg_select(opaque
, (uint16_t)value
);
439 static bool fw_cfg_ctl_mem_valid(void *opaque
, hwaddr addr
,
440 unsigned size
, bool is_write
)
442 return is_write
&& size
== 2;
445 static void fw_cfg_comb_write(void *opaque
, hwaddr addr
,
446 uint64_t value
, unsigned size
)
450 fw_cfg_write(opaque
, (uint8_t)value
);
453 fw_cfg_select(opaque
, (uint16_t)value
);
458 static bool fw_cfg_comb_valid(void *opaque
, hwaddr addr
,
459 unsigned size
, bool is_write
)
461 return (size
== 1) || (is_write
&& size
== 2);
464 static const MemoryRegionOps fw_cfg_ctl_mem_ops
= {
465 .write
= fw_cfg_ctl_mem_write
,
466 .endianness
= DEVICE_BIG_ENDIAN
,
467 .valid
.accepts
= fw_cfg_ctl_mem_valid
,
470 static const MemoryRegionOps fw_cfg_data_mem_ops
= {
471 .read
= fw_cfg_data_read
,
472 .write
= fw_cfg_data_mem_write
,
473 .endianness
= DEVICE_BIG_ENDIAN
,
475 .min_access_size
= 1,
476 .max_access_size
= 1,
477 .accepts
= fw_cfg_data_mem_valid
,
481 static const MemoryRegionOps fw_cfg_comb_mem_ops
= {
482 .read
= fw_cfg_data_read
,
483 .write
= fw_cfg_comb_write
,
484 .endianness
= DEVICE_LITTLE_ENDIAN
,
485 .valid
.accepts
= fw_cfg_comb_valid
,
488 static const MemoryRegionOps fw_cfg_dma_mem_ops
= {
489 .read
= fw_cfg_dma_mem_read
,
490 .write
= fw_cfg_dma_mem_write
,
491 .endianness
= DEVICE_BIG_ENDIAN
,
492 .valid
.accepts
= fw_cfg_dma_mem_valid
,
493 .valid
.max_access_size
= 8,
494 .impl
.max_access_size
= 8,
497 static void fw_cfg_reset(DeviceState
*d
)
499 FWCfgState
*s
= FW_CFG(d
);
501 /* we never register a read callback for FW_CFG_SIGNATURE */
502 fw_cfg_select(s
, FW_CFG_SIGNATURE
);
505 /* Save restore 32 bit int as uint16_t
506 This is a Big hack, but it is how the old state did it.
507 Or we broke compatibility in the state, or we can't use struct tm
510 static int get_uint32_as_uint16(QEMUFile
*f
, void *pv
, size_t size
,
514 *v
= qemu_get_be16(f
);
518 static int put_unused(QEMUFile
*f
, void *pv
, size_t size
, VMStateField
*field
,
521 fprintf(stderr
, "uint32_as_uint16 is only used for backward compatibility.\n");
522 fprintf(stderr
, "This functions shouldn't be called.\n");
527 static const VMStateInfo vmstate_hack_uint32_as_uint16
= {
528 .name
= "int32_as_uint16",
529 .get
= get_uint32_as_uint16
,
533 #define VMSTATE_UINT16_HACK(_f, _s, _t) \
534 VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_hack_uint32_as_uint16, uint32_t)
537 static bool is_version_1(void *opaque
, int version_id
)
539 return version_id
== 1;
542 bool fw_cfg_dma_enabled(void *opaque
)
544 FWCfgState
*s
= opaque
;
546 return s
->dma_enabled
;
549 static 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()
558 static const VMStateDescription vmstate_fw_cfg
= {
561 .minimum_version_id
= 1,
562 .fields
= (VMStateField
[]) {
563 VMSTATE_UINT16(cur_entry
, FWCfgState
),
564 VMSTATE_UINT16_HACK(cur_offset
, FWCfgState
, is_version_1
),
565 VMSTATE_UINT32_V(cur_offset
, FWCfgState
, 2),
566 VMSTATE_END_OF_LIST()
568 .subsections
= (const VMStateDescription
*[]) {
574 static void fw_cfg_add_bytes_callback(FWCfgState
*s
, uint16_t key
,
575 FWCfgCallback select_cb
,
576 FWCfgWriteCallback write_cb
,
577 void *callback_opaque
,
578 void *data
, size_t len
,
581 int arch
= !!(key
& FW_CFG_ARCH_LOCAL
);
583 key
&= FW_CFG_ENTRY_MASK
;
585 assert(key
< fw_cfg_max_entry(s
) && len
< UINT32_MAX
);
586 assert(s
->entries
[arch
][key
].data
== NULL
); /* avoid key conflict */
588 s
->entries
[arch
][key
].data
= data
;
589 s
->entries
[arch
][key
].len
= (uint32_t)len
;
590 s
->entries
[arch
][key
].select_cb
= select_cb
;
591 s
->entries
[arch
][key
].write_cb
= write_cb
;
592 s
->entries
[arch
][key
].callback_opaque
= callback_opaque
;
593 s
->entries
[arch
][key
].allow_write
= !read_only
;
596 static void *fw_cfg_modify_bytes_read(FWCfgState
*s
, uint16_t key
,
597 void *data
, size_t len
)
600 int arch
= !!(key
& FW_CFG_ARCH_LOCAL
);
602 key
&= FW_CFG_ENTRY_MASK
;
604 assert(key
< fw_cfg_max_entry(s
) && len
< UINT32_MAX
);
606 /* return the old data to the function caller, avoid memory leak */
607 ptr
= s
->entries
[arch
][key
].data
;
608 s
->entries
[arch
][key
].data
= data
;
609 s
->entries
[arch
][key
].len
= len
;
610 s
->entries
[arch
][key
].callback_opaque
= NULL
;
611 s
->entries
[arch
][key
].allow_write
= false;
616 void fw_cfg_add_bytes(FWCfgState
*s
, uint16_t key
, void *data
, size_t len
)
618 fw_cfg_add_bytes_callback(s
, key
, NULL
, NULL
, NULL
, data
, len
, true);
621 void fw_cfg_add_string(FWCfgState
*s
, uint16_t key
, const char *value
)
623 size_t sz
= strlen(value
) + 1;
625 fw_cfg_add_bytes(s
, key
, g_memdup(value
, sz
), sz
);
628 void fw_cfg_add_i16(FWCfgState
*s
, uint16_t key
, uint16_t value
)
632 copy
= g_malloc(sizeof(value
));
633 *copy
= cpu_to_le16(value
);
634 fw_cfg_add_bytes(s
, key
, copy
, sizeof(value
));
637 void fw_cfg_modify_i16(FWCfgState
*s
, uint16_t key
, uint16_t value
)
639 uint16_t *copy
, *old
;
641 copy
= g_malloc(sizeof(value
));
642 *copy
= cpu_to_le16(value
);
643 old
= fw_cfg_modify_bytes_read(s
, key
, copy
, sizeof(value
));
647 void fw_cfg_add_i32(FWCfgState
*s
, uint16_t key
, uint32_t value
)
651 copy
= g_malloc(sizeof(value
));
652 *copy
= cpu_to_le32(value
);
653 fw_cfg_add_bytes(s
, key
, copy
, sizeof(value
));
656 void fw_cfg_add_i64(FWCfgState
*s
, uint16_t key
, uint64_t value
)
660 copy
= g_malloc(sizeof(value
));
661 *copy
= cpu_to_le64(value
);
662 fw_cfg_add_bytes(s
, key
, copy
, sizeof(value
));
665 void fw_cfg_set_order_override(FWCfgState
*s
, int order
)
667 assert(s
->fw_cfg_order_override
== 0);
668 s
->fw_cfg_order_override
= order
;
671 void fw_cfg_reset_order_override(FWCfgState
*s
)
673 assert(s
->fw_cfg_order_override
!= 0);
674 s
->fw_cfg_order_override
= 0;
678 * This is the legacy order list. For legacy systems, files are in
679 * the fw_cfg in the order defined below, by the "order" value. Note
680 * that some entries (VGA ROMs, NIC option ROMS, etc.) go into a
681 * specific area, but there may be more than one and they occur in the
682 * order that the user specifies them on the command line. Those are
683 * handled in a special manner, using the order override above.
685 * For non-legacy, the files are sorted by filename to avoid this kind
686 * of complexity in the future.
688 * This is only for x86, other arches don't implement versioning so
689 * they won't set legacy mode.
695 { "etc/boot-menu-wait", 10 },
696 { "bootsplash.jpg", 11 },
697 { "bootsplash.bmp", 12 },
698 { "etc/boot-fail-wait", 15 },
699 { "etc/smbios/smbios-tables", 20 },
700 { "etc/smbios/smbios-anchor", 30 },
702 { "etc/reserved-memory-end", 50 },
703 { "genroms/kvmvapic.bin", 55 },
704 { "genroms/linuxboot.bin", 60 },
705 { }, /* VGA ROMs from pc_vga_init come here, 70. */
706 { }, /* NIC option ROMs from pc_nic_init come here, 80. */
707 { "etc/system-states", 90 },
708 { }, /* User ROMs come here, 100. */
709 { }, /* Device FW comes here, 110. */
710 { "etc/extra-pci-roots", 120 },
711 { "etc/acpi/tables", 130 },
712 { "etc/table-loader", 140 },
713 { "etc/tpm/log", 150 },
714 { "etc/acpi/rsdp", 160 },
715 { "bootorder", 170 },
717 #define FW_CFG_ORDER_OVERRIDE_LAST 200
720 static int get_fw_cfg_order(FWCfgState
*s
, const char *name
)
724 if (s
->fw_cfg_order_override
> 0) {
725 return s
->fw_cfg_order_override
;
728 for (i
= 0; i
< ARRAY_SIZE(fw_cfg_order
); i
++) {
729 if (fw_cfg_order
[i
].name
== NULL
) {
733 if (strcmp(name
, fw_cfg_order
[i
].name
) == 0) {
734 return fw_cfg_order
[i
].order
;
738 /* Stick unknown stuff at the end. */
739 warn_report("Unknown firmware file in legacy mode: %s", name
);
740 return FW_CFG_ORDER_OVERRIDE_LAST
;
743 void fw_cfg_add_file_callback(FWCfgState
*s
, const char *filename
,
744 FWCfgCallback select_cb
,
745 FWCfgWriteCallback write_cb
,
746 void *callback_opaque
,
747 void *data
, size_t len
, bool read_only
)
751 MachineClass
*mc
= MACHINE_GET_CLASS(qdev_get_machine());
755 dsize
= sizeof(uint32_t) + sizeof(FWCfgFile
) * fw_cfg_file_slots(s
);
756 s
->files
= g_malloc0(dsize
);
757 fw_cfg_add_bytes(s
, FW_CFG_FILE_DIR
, s
->files
, dsize
);
760 count
= be32_to_cpu(s
->files
->count
);
761 assert(count
< fw_cfg_file_slots(s
));
763 /* Find the insertion point. */
764 if (mc
->legacy_fw_cfg_order
) {
766 * Sort by order. For files with the same order, we keep them
767 * in the sequence in which they were added.
769 order
= get_fw_cfg_order(s
, filename
);
771 index
> 0 && order
< s
->entry_order
[index
- 1];
774 /* Sort by file name. */
776 index
> 0 && strcmp(filename
, s
->files
->f
[index
- 1].name
) < 0;
781 * Move all the entries from the index point and after down one
782 * to create a slot for the new entry. Because calculations are
783 * being done with the index, make it so that "i" is the current
784 * index and "i - 1" is the one being copied from, thus the
785 * unusual start and end in the for statement.
787 for (i
= count
+ 1; i
> index
; i
--) {
788 s
->files
->f
[i
] = s
->files
->f
[i
- 1];
789 s
->files
->f
[i
].select
= cpu_to_be16(FW_CFG_FILE_FIRST
+ i
);
790 s
->entries
[0][FW_CFG_FILE_FIRST
+ i
] =
791 s
->entries
[0][FW_CFG_FILE_FIRST
+ i
- 1];
792 s
->entry_order
[i
] = s
->entry_order
[i
- 1];
795 memset(&s
->files
->f
[index
], 0, sizeof(FWCfgFile
));
796 memset(&s
->entries
[0][FW_CFG_FILE_FIRST
+ index
], 0, sizeof(FWCfgEntry
));
798 pstrcpy(s
->files
->f
[index
].name
, sizeof(s
->files
->f
[index
].name
), filename
);
799 for (i
= 0; i
<= count
; i
++) {
801 strcmp(s
->files
->f
[index
].name
, s
->files
->f
[i
].name
) == 0) {
802 error_report("duplicate fw_cfg file name: %s",
803 s
->files
->f
[index
].name
);
808 fw_cfg_add_bytes_callback(s
, FW_CFG_FILE_FIRST
+ index
,
810 callback_opaque
, data
, len
,
813 s
->files
->f
[index
].size
= cpu_to_be32(len
);
814 s
->files
->f
[index
].select
= cpu_to_be16(FW_CFG_FILE_FIRST
+ index
);
815 s
->entry_order
[index
] = order
;
816 trace_fw_cfg_add_file(s
, index
, s
->files
->f
[index
].name
, len
);
818 s
->files
->count
= cpu_to_be32(count
+1);
821 void fw_cfg_add_file(FWCfgState
*s
, const char *filename
,
822 void *data
, size_t len
)
824 fw_cfg_add_file_callback(s
, filename
, NULL
, NULL
, NULL
, data
, len
, true);
827 void *fw_cfg_modify_file(FWCfgState
*s
, const char *filename
,
828 void *data
, size_t len
)
835 index
= be32_to_cpu(s
->files
->count
);
836 assert(index
< fw_cfg_file_slots(s
));
838 for (i
= 0; i
< index
; i
++) {
839 if (strcmp(filename
, s
->files
->f
[i
].name
) == 0) {
840 ptr
= fw_cfg_modify_bytes_read(s
, FW_CFG_FILE_FIRST
+ i
,
842 s
->files
->f
[i
].size
= cpu_to_be32(len
);
847 fw_cfg_add_file_callback(s
, filename
, NULL
, NULL
, NULL
, data
, len
, true);
851 static void fw_cfg_machine_reset(void *opaque
)
855 FWCfgState
*s
= opaque
;
856 char *bootindex
= get_boot_devices_list(&len
, false);
858 ptr
= fw_cfg_modify_file(s
, "bootorder", (uint8_t *)bootindex
, len
);
862 static void fw_cfg_machine_ready(struct Notifier
*n
, void *data
)
864 FWCfgState
*s
= container_of(n
, FWCfgState
, machine_ready
);
865 qemu_register_reset(fw_cfg_machine_reset
, s
);
870 static void fw_cfg_common_realize(DeviceState
*dev
, Error
**errp
)
872 FWCfgState
*s
= FW_CFG(dev
);
873 MachineState
*machine
= MACHINE(qdev_get_machine());
874 uint32_t version
= FW_CFG_VERSION
;
876 if (!fw_cfg_find()) {
877 error_setg(errp
, "at most one %s device is permitted", TYPE_FW_CFG
);
881 fw_cfg_add_bytes(s
, FW_CFG_SIGNATURE
, (char *)"QEMU", 4);
882 fw_cfg_add_bytes(s
, FW_CFG_UUID
, &qemu_uuid
, 16);
883 fw_cfg_add_i16(s
, FW_CFG_NOGRAPHIC
, (uint16_t)!machine
->enable_graphics
);
884 fw_cfg_add_i16(s
, FW_CFG_BOOT_MENU
, (uint16_t)boot_menu
);
885 fw_cfg_bootsplash(s
);
888 if (s
->dma_enabled
) {
889 version
|= FW_CFG_VERSION_DMA
;
892 fw_cfg_add_i32(s
, FW_CFG_ID
, version
);
894 s
->machine_ready
.notify
= fw_cfg_machine_ready
;
895 qemu_add_machine_init_done_notifier(&s
->machine_ready
);
898 FWCfgState
*fw_cfg_init_io_dma(uint32_t iobase
, uint32_t dma_iobase
,
899 AddressSpace
*dma_as
)
905 bool dma_requested
= dma_iobase
&& dma_as
;
907 dev
= qdev_create(NULL
, TYPE_FW_CFG_IO
);
908 if (!dma_requested
) {
909 qdev_prop_set_bit(dev
, "dma_enabled", false);
912 object_property_add_child(OBJECT(qdev_get_machine()), TYPE_FW_CFG
,
914 qdev_init_nofail(dev
);
916 sbd
= SYS_BUS_DEVICE(dev
);
917 ios
= FW_CFG_IO(dev
);
918 sysbus_add_io(sbd
, iobase
, &ios
->comb_iomem
);
922 if (s
->dma_enabled
) {
923 /* 64 bits for the address field */
926 sysbus_add_io(sbd
, dma_iobase
, &s
->dma_iomem
);
932 FWCfgState
*fw_cfg_init_io(uint32_t iobase
)
934 return fw_cfg_init_io_dma(iobase
, 0, NULL
);
937 FWCfgState
*fw_cfg_init_mem_wide(hwaddr ctl_addr
,
938 hwaddr data_addr
, uint32_t data_width
,
939 hwaddr dma_addr
, AddressSpace
*dma_as
)
944 bool dma_requested
= dma_addr
&& dma_as
;
946 dev
= qdev_create(NULL
, TYPE_FW_CFG_MEM
);
947 qdev_prop_set_uint32(dev
, "data_width", data_width
);
948 if (!dma_requested
) {
949 qdev_prop_set_bit(dev
, "dma_enabled", false);
952 object_property_add_child(OBJECT(qdev_get_machine()), TYPE_FW_CFG
,
954 qdev_init_nofail(dev
);
956 sbd
= SYS_BUS_DEVICE(dev
);
957 sysbus_mmio_map(sbd
, 0, ctl_addr
);
958 sysbus_mmio_map(sbd
, 1, data_addr
);
962 if (s
->dma_enabled
) {
965 sysbus_mmio_map(sbd
, 2, dma_addr
);
971 FWCfgState
*fw_cfg_init_mem(hwaddr ctl_addr
, hwaddr data_addr
)
973 return fw_cfg_init_mem_wide(ctl_addr
, data_addr
,
974 fw_cfg_data_mem_ops
.valid
.max_access_size
,
979 FWCfgState
*fw_cfg_find(void)
981 /* Returns NULL unless there is exactly one fw_cfg device */
982 return FW_CFG(object_resolve_path_type("", TYPE_FW_CFG
, NULL
));
986 static void fw_cfg_class_init(ObjectClass
*klass
, void *data
)
988 DeviceClass
*dc
= DEVICE_CLASS(klass
);
990 dc
->reset
= fw_cfg_reset
;
991 dc
->vmsd
= &vmstate_fw_cfg
;
994 static const TypeInfo fw_cfg_info
= {
996 .parent
= TYPE_SYS_BUS_DEVICE
,
998 .instance_size
= sizeof(FWCfgState
),
999 .class_init
= fw_cfg_class_init
,
1002 static void fw_cfg_file_slots_allocate(FWCfgState
*s
, Error
**errp
)
1004 uint16_t file_slots_max
;
1006 if (fw_cfg_file_slots(s
) < FW_CFG_FILE_SLOTS_MIN
) {
1007 error_setg(errp
, "\"file_slots\" must be at least 0x%x",
1008 FW_CFG_FILE_SLOTS_MIN
);
1012 /* (UINT16_MAX & FW_CFG_ENTRY_MASK) is the highest inclusive selector value
1013 * that we permit. The actual (exclusive) value coming from the
1014 * configuration is (FW_CFG_FILE_FIRST + fw_cfg_file_slots(s)). */
1015 file_slots_max
= (UINT16_MAX
& FW_CFG_ENTRY_MASK
) - FW_CFG_FILE_FIRST
+ 1;
1016 if (fw_cfg_file_slots(s
) > file_slots_max
) {
1017 error_setg(errp
, "\"file_slots\" must not exceed 0x%" PRIx16
,
1022 s
->entries
[0] = g_new0(FWCfgEntry
, fw_cfg_max_entry(s
));
1023 s
->entries
[1] = g_new0(FWCfgEntry
, fw_cfg_max_entry(s
));
1024 s
->entry_order
= g_new0(int, fw_cfg_max_entry(s
));
1027 static Property fw_cfg_io_properties
[] = {
1028 DEFINE_PROP_BOOL("dma_enabled", FWCfgIoState
, parent_obj
.dma_enabled
,
1030 DEFINE_PROP_UINT16("x-file-slots", FWCfgIoState
, parent_obj
.file_slots
,
1031 FW_CFG_FILE_SLOTS_DFLT
),
1032 DEFINE_PROP_END_OF_LIST(),
1035 static void fw_cfg_io_realize(DeviceState
*dev
, Error
**errp
)
1037 FWCfgIoState
*s
= FW_CFG_IO(dev
);
1038 Error
*local_err
= NULL
;
1040 fw_cfg_file_slots_allocate(FW_CFG(s
), &local_err
);
1042 error_propagate(errp
, local_err
);
1046 /* when using port i/o, the 8-bit data register ALWAYS overlaps
1047 * with half of the 16-bit control register. Hence, the total size
1048 * of the i/o region used is FW_CFG_CTL_SIZE */
1049 memory_region_init_io(&s
->comb_iomem
, OBJECT(s
), &fw_cfg_comb_mem_ops
,
1050 FW_CFG(s
), "fwcfg", FW_CFG_CTL_SIZE
);
1052 if (FW_CFG(s
)->dma_enabled
) {
1053 memory_region_init_io(&FW_CFG(s
)->dma_iomem
, OBJECT(s
),
1054 &fw_cfg_dma_mem_ops
, FW_CFG(s
), "fwcfg.dma",
1055 sizeof(dma_addr_t
));
1058 fw_cfg_common_realize(dev
, errp
);
1061 static void fw_cfg_io_class_init(ObjectClass
*klass
, void *data
)
1063 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1065 dc
->realize
= fw_cfg_io_realize
;
1066 dc
->props
= fw_cfg_io_properties
;
1069 static const TypeInfo fw_cfg_io_info
= {
1070 .name
= TYPE_FW_CFG_IO
,
1071 .parent
= TYPE_FW_CFG
,
1072 .instance_size
= sizeof(FWCfgIoState
),
1073 .class_init
= fw_cfg_io_class_init
,
1077 static Property fw_cfg_mem_properties
[] = {
1078 DEFINE_PROP_UINT32("data_width", FWCfgMemState
, data_width
, -1),
1079 DEFINE_PROP_BOOL("dma_enabled", FWCfgMemState
, parent_obj
.dma_enabled
,
1081 DEFINE_PROP_UINT16("x-file-slots", FWCfgMemState
, parent_obj
.file_slots
,
1082 FW_CFG_FILE_SLOTS_DFLT
),
1083 DEFINE_PROP_END_OF_LIST(),
1086 static void fw_cfg_mem_realize(DeviceState
*dev
, Error
**errp
)
1088 FWCfgMemState
*s
= FW_CFG_MEM(dev
);
1089 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
1090 const MemoryRegionOps
*data_ops
= &fw_cfg_data_mem_ops
;
1091 Error
*local_err
= NULL
;
1093 fw_cfg_file_slots_allocate(FW_CFG(s
), &local_err
);
1095 error_propagate(errp
, local_err
);
1099 memory_region_init_io(&s
->ctl_iomem
, OBJECT(s
), &fw_cfg_ctl_mem_ops
,
1100 FW_CFG(s
), "fwcfg.ctl", FW_CFG_CTL_SIZE
);
1101 sysbus_init_mmio(sbd
, &s
->ctl_iomem
);
1103 if (s
->data_width
> data_ops
->valid
.max_access_size
) {
1104 /* memberwise copy because the "old_mmio" member is const */
1105 s
->wide_data_ops
.read
= data_ops
->read
;
1106 s
->wide_data_ops
.write
= data_ops
->write
;
1107 s
->wide_data_ops
.endianness
= data_ops
->endianness
;
1108 s
->wide_data_ops
.valid
= data_ops
->valid
;
1109 s
->wide_data_ops
.impl
= data_ops
->impl
;
1111 s
->wide_data_ops
.valid
.max_access_size
= s
->data_width
;
1112 s
->wide_data_ops
.impl
.max_access_size
= s
->data_width
;
1113 data_ops
= &s
->wide_data_ops
;
1115 memory_region_init_io(&s
->data_iomem
, OBJECT(s
), data_ops
, FW_CFG(s
),
1116 "fwcfg.data", data_ops
->valid
.max_access_size
);
1117 sysbus_init_mmio(sbd
, &s
->data_iomem
);
1119 if (FW_CFG(s
)->dma_enabled
) {
1120 memory_region_init_io(&FW_CFG(s
)->dma_iomem
, OBJECT(s
),
1121 &fw_cfg_dma_mem_ops
, FW_CFG(s
), "fwcfg.dma",
1122 sizeof(dma_addr_t
));
1123 sysbus_init_mmio(sbd
, &FW_CFG(s
)->dma_iomem
);
1126 fw_cfg_common_realize(dev
, errp
);
1129 static void fw_cfg_mem_class_init(ObjectClass
*klass
, void *data
)
1131 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1133 dc
->realize
= fw_cfg_mem_realize
;
1134 dc
->props
= fw_cfg_mem_properties
;
1137 static const TypeInfo fw_cfg_mem_info
= {
1138 .name
= TYPE_FW_CFG_MEM
,
1139 .parent
= TYPE_FW_CFG
,
1140 .instance_size
= sizeof(FWCfgMemState
),
1141 .class_init
= fw_cfg_mem_class_init
,
1145 static void fw_cfg_register_types(void)
1147 type_register_static(&fw_cfg_info
);
1148 type_register_static(&fw_cfg_io_info
);
1149 type_register_static(&fw_cfg_mem_info
);
1152 type_init(fw_cfg_register_types
)