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 "qemu/osdep.h"
27 #include "sysemu/sysemu.h"
28 #include "sysemu/dma.h"
29 #include "hw/boards.h"
30 #include "hw/isa/isa.h"
31 #include "hw/nvram/fw_cfg.h"
32 #include "hw/sysbus.h"
34 #include "qemu/error-report.h"
35 #include "qemu/option.h"
36 #include "qemu/config-file.h"
37 #include "qemu/cutils.h"
38 #include "qapi/error.h"
40 #define FW_CFG_FILE_SLOTS_DFLT 0x20
42 /* FW_CFG_VERSION bits */
43 #define FW_CFG_VERSION 0x01
44 #define FW_CFG_VERSION_DMA 0x02
46 /* FW_CFG_DMA_CONTROL bits */
47 #define FW_CFG_DMA_CTL_ERROR 0x01
48 #define FW_CFG_DMA_CTL_READ 0x02
49 #define FW_CFG_DMA_CTL_SKIP 0x04
50 #define FW_CFG_DMA_CTL_SELECT 0x08
51 #define FW_CFG_DMA_CTL_WRITE 0x10
53 #define FW_CFG_DMA_SIGNATURE 0x51454d5520434647ULL /* "QEMU CFG" */
59 void *callback_opaque
;
60 FWCfgCallback select_cb
;
61 FWCfgWriteCallback write_cb
;
67 static char *read_splashfile(char *filename
, gsize
*file_sizep
,
74 unsigned int filehead
;
77 res
= g_file_get_contents(filename
, &content
, file_sizep
, &err
);
79 error_report("failed to read splash file '%s'", filename
);
85 if (*file_sizep
< 30) {
90 filehead
= ((content
[0] & 0xff) + (content
[1] << 8)) & 0xffff;
91 if (filehead
== 0xd8ff) {
93 } else if (filehead
== 0x4d42) {
100 if (file_type
== BMP_FILE
) {
101 bmp_bpp
= (content
[28] + (content
[29] << 8)) & 0xffff;
108 *file_typep
= file_type
;
113 error_report("splash file '%s' format not recognized; must be JPEG "
114 "or 24 bit BMP", filename
);
119 static void fw_cfg_bootsplash(FWCfgState
*s
)
121 int boot_splash_time
= -1;
122 const char *boot_splash_filename
= NULL
;
124 char *filename
, *file_data
;
129 /* get user configuration */
130 QemuOptsList
*plist
= qemu_find_opts("boot-opts");
131 QemuOpts
*opts
= QTAILQ_FIRST(&plist
->head
);
133 temp
= qemu_opt_get(opts
, "splash");
135 boot_splash_filename
= temp
;
137 temp
= qemu_opt_get(opts
, "splash-time");
140 boot_splash_time
= strtol(p
, &p
, 10);
144 /* insert splash time if user configurated */
145 if (boot_splash_time
>= 0) {
146 /* validate the input */
147 if (boot_splash_time
> 0xffff) {
148 error_report("splash time is big than 65535, force it to 65535.");
149 boot_splash_time
= 0xffff;
151 /* use little endian format */
152 qemu_extra_params_fw
[0] = (uint8_t)(boot_splash_time
& 0xff);
153 qemu_extra_params_fw
[1] = (uint8_t)((boot_splash_time
>> 8) & 0xff);
154 fw_cfg_add_file(s
, "etc/boot-menu-wait", qemu_extra_params_fw
, 2);
157 /* insert splash file if user configurated */
158 if (boot_splash_filename
!= NULL
) {
159 filename
= qemu_find_file(QEMU_FILE_TYPE_BIOS
, boot_splash_filename
);
160 if (filename
== NULL
) {
161 error_report("failed to find file '%s'.", boot_splash_filename
);
165 /* loading file data */
166 file_data
= read_splashfile(filename
, &file_size
, &file_type
);
167 if (file_data
== NULL
) {
171 g_free(boot_splash_filedata
);
172 boot_splash_filedata
= (uint8_t *)file_data
;
173 boot_splash_filedata_size
= file_size
;
176 if (file_type
== JPG_FILE
) {
177 fw_cfg_add_file(s
, "bootsplash.jpg",
178 boot_splash_filedata
, boot_splash_filedata_size
);
180 fw_cfg_add_file(s
, "bootsplash.bmp",
181 boot_splash_filedata
, boot_splash_filedata_size
);
187 static void fw_cfg_reboot(FWCfgState
*s
)
189 int reboot_timeout
= -1;
193 /* get user configuration */
194 QemuOptsList
*plist
= qemu_find_opts("boot-opts");
195 QemuOpts
*opts
= QTAILQ_FIRST(&plist
->head
);
197 temp
= qemu_opt_get(opts
, "reboot-timeout");
200 reboot_timeout
= strtol(p
, &p
, 10);
203 /* validate the input */
204 if (reboot_timeout
> 0xffff) {
205 error_report("reboot timeout is larger than 65535, force it to 65535.");
206 reboot_timeout
= 0xffff;
208 fw_cfg_add_file(s
, "etc/boot-fail-wait", g_memdup(&reboot_timeout
, 4), 4);
211 static void fw_cfg_write(FWCfgState
*s
, uint8_t value
)
213 /* nothing, write support removed in QEMU v2.4+ */
216 static inline uint16_t fw_cfg_file_slots(const FWCfgState
*s
)
218 return s
->file_slots
;
221 /* Note: this function returns an exclusive limit. */
222 static inline uint32_t fw_cfg_max_entry(const FWCfgState
*s
)
224 return FW_CFG_FILE_FIRST
+ fw_cfg_file_slots(s
);
227 static int fw_cfg_select(FWCfgState
*s
, uint16_t key
)
233 if ((key
& FW_CFG_ENTRY_MASK
) >= fw_cfg_max_entry(s
)) {
234 s
->cur_entry
= FW_CFG_INVALID
;
239 /* entry successfully selected, now run callback if present */
240 arch
= !!(key
& FW_CFG_ARCH_LOCAL
);
241 e
= &s
->entries
[arch
][key
& FW_CFG_ENTRY_MASK
];
243 e
->select_cb(e
->callback_opaque
);
247 trace_fw_cfg_select(s
, key
, ret
);
251 static uint64_t fw_cfg_data_read(void *opaque
, hwaddr addr
, unsigned size
)
253 FWCfgState
*s
= opaque
;
254 int arch
= !!(s
->cur_entry
& FW_CFG_ARCH_LOCAL
);
255 FWCfgEntry
*e
= (s
->cur_entry
== FW_CFG_INVALID
) ? NULL
:
256 &s
->entries
[arch
][s
->cur_entry
& FW_CFG_ENTRY_MASK
];
259 assert(size
> 0 && size
<= sizeof(value
));
260 if (s
->cur_entry
!= FW_CFG_INVALID
&& e
->data
&& s
->cur_offset
< e
->len
) {
261 /* The least significant 'size' bytes of the return value are
262 * expected to contain a string preserving portion of the item
263 * data, padded with zeros on the right in case we run out early.
264 * In technical terms, we're composing the host-endian representation
265 * of the big endian interpretation of the fw_cfg string.
268 value
= (value
<< 8) | e
->data
[s
->cur_offset
++];
269 } while (--size
&& s
->cur_offset
< e
->len
);
270 /* If size is still not zero, we *did* run out early, so continue
271 * left-shifting, to add the appropriate number of padding zeros
277 trace_fw_cfg_read(s
, value
);
281 static void fw_cfg_data_mem_write(void *opaque
, hwaddr addr
,
282 uint64_t value
, unsigned size
)
284 FWCfgState
*s
= opaque
;
288 fw_cfg_write(s
, value
>> (8 * --i
));
292 static void fw_cfg_dma_transfer(FWCfgState
*s
)
298 int read
= 0, write
= 0;
301 /* Reset the address before the next access */
302 dma_addr
= s
->dma_addr
;
305 if (dma_memory_read(s
->dma_as
, dma_addr
, &dma
, sizeof(dma
))) {
306 stl_be_dma(s
->dma_as
, dma_addr
+ offsetof(FWCfgDmaAccess
, control
),
307 FW_CFG_DMA_CTL_ERROR
);
311 dma
.address
= be64_to_cpu(dma
.address
);
312 dma
.length
= be32_to_cpu(dma
.length
);
313 dma
.control
= be32_to_cpu(dma
.control
);
315 if (dma
.control
& FW_CFG_DMA_CTL_SELECT
) {
316 fw_cfg_select(s
, dma
.control
>> 16);
319 arch
= !!(s
->cur_entry
& FW_CFG_ARCH_LOCAL
);
320 e
= (s
->cur_entry
== FW_CFG_INVALID
) ? NULL
:
321 &s
->entries
[arch
][s
->cur_entry
& FW_CFG_ENTRY_MASK
];
323 if (dma
.control
& FW_CFG_DMA_CTL_READ
) {
326 } else if (dma
.control
& FW_CFG_DMA_CTL_WRITE
) {
329 } else if (dma
.control
& FW_CFG_DMA_CTL_SKIP
) {
338 while (dma
.length
> 0 && !(dma
.control
& FW_CFG_DMA_CTL_ERROR
)) {
339 if (s
->cur_entry
== FW_CFG_INVALID
|| !e
->data
||
340 s
->cur_offset
>= e
->len
) {
343 /* If the access is not a read access, it will be a skip access,
347 if (dma_memory_set(s
->dma_as
, dma
.address
, 0, len
)) {
348 dma
.control
|= FW_CFG_DMA_CTL_ERROR
;
352 dma
.control
|= FW_CFG_DMA_CTL_ERROR
;
355 if (dma
.length
<= (e
->len
- s
->cur_offset
)) {
358 len
= (e
->len
- s
->cur_offset
);
361 /* If the access is not a read access, it will be a skip access,
365 if (dma_memory_write(s
->dma_as
, dma
.address
,
366 &e
->data
[s
->cur_offset
], len
)) {
367 dma
.control
|= FW_CFG_DMA_CTL_ERROR
;
371 if (!e
->allow_write
||
373 dma_memory_read(s
->dma_as
, dma
.address
,
374 &e
->data
[s
->cur_offset
], len
)) {
375 dma
.control
|= FW_CFG_DMA_CTL_ERROR
;
376 } else if (e
->write_cb
) {
377 e
->write_cb(e
->callback_opaque
, s
->cur_offset
, len
);
381 s
->cur_offset
+= len
;
389 stl_be_dma(s
->dma_as
, dma_addr
+ offsetof(FWCfgDmaAccess
, control
),
392 trace_fw_cfg_read(s
, 0);
395 static uint64_t fw_cfg_dma_mem_read(void *opaque
, hwaddr addr
,
398 /* Return a signature value (and handle various read sizes) */
399 return extract64(FW_CFG_DMA_SIGNATURE
, (8 - addr
- size
) * 8, size
* 8);
402 static void fw_cfg_dma_mem_write(void *opaque
, hwaddr addr
,
403 uint64_t value
, unsigned size
)
405 FWCfgState
*s
= opaque
;
409 /* FWCfgDmaAccess high address */
410 s
->dma_addr
= value
<< 32;
411 } else if (addr
== 4) {
412 /* FWCfgDmaAccess low address */
413 s
->dma_addr
|= value
;
414 fw_cfg_dma_transfer(s
);
416 } else if (size
== 8 && addr
== 0) {
418 fw_cfg_dma_transfer(s
);
422 static bool fw_cfg_dma_mem_valid(void *opaque
, hwaddr addr
,
423 unsigned size
, bool is_write
,
426 return !is_write
|| ((size
== 4 && (addr
== 0 || addr
== 4)) ||
427 (size
== 8 && addr
== 0));
430 static bool fw_cfg_data_mem_valid(void *opaque
, hwaddr addr
,
431 unsigned size
, bool is_write
,
437 static uint64_t fw_cfg_ctl_mem_read(void *opaque
, hwaddr addr
, unsigned size
)
442 static void fw_cfg_ctl_mem_write(void *opaque
, hwaddr addr
,
443 uint64_t value
, unsigned size
)
445 fw_cfg_select(opaque
, (uint16_t)value
);
448 static bool fw_cfg_ctl_mem_valid(void *opaque
, hwaddr addr
,
449 unsigned size
, bool is_write
,
452 return is_write
&& size
== 2;
455 static void fw_cfg_comb_write(void *opaque
, hwaddr addr
,
456 uint64_t value
, unsigned size
)
460 fw_cfg_write(opaque
, (uint8_t)value
);
463 fw_cfg_select(opaque
, (uint16_t)value
);
468 static bool fw_cfg_comb_valid(void *opaque
, hwaddr addr
,
469 unsigned size
, bool is_write
,
472 return (size
== 1) || (is_write
&& size
== 2);
475 static const MemoryRegionOps fw_cfg_ctl_mem_ops
= {
476 .read
= fw_cfg_ctl_mem_read
,
477 .write
= fw_cfg_ctl_mem_write
,
478 .endianness
= DEVICE_BIG_ENDIAN
,
479 .valid
.accepts
= fw_cfg_ctl_mem_valid
,
482 static const MemoryRegionOps fw_cfg_data_mem_ops
= {
483 .read
= fw_cfg_data_read
,
484 .write
= fw_cfg_data_mem_write
,
485 .endianness
= DEVICE_BIG_ENDIAN
,
487 .min_access_size
= 1,
488 .max_access_size
= 1,
489 .accepts
= fw_cfg_data_mem_valid
,
493 static const MemoryRegionOps fw_cfg_comb_mem_ops
= {
494 .read
= fw_cfg_data_read
,
495 .write
= fw_cfg_comb_write
,
496 .endianness
= DEVICE_LITTLE_ENDIAN
,
497 .valid
.accepts
= fw_cfg_comb_valid
,
500 static const MemoryRegionOps fw_cfg_dma_mem_ops
= {
501 .read
= fw_cfg_dma_mem_read
,
502 .write
= fw_cfg_dma_mem_write
,
503 .endianness
= DEVICE_BIG_ENDIAN
,
504 .valid
.accepts
= fw_cfg_dma_mem_valid
,
505 .valid
.max_access_size
= 8,
506 .impl
.max_access_size
= 8,
509 static void fw_cfg_reset(DeviceState
*d
)
511 FWCfgState
*s
= FW_CFG(d
);
513 /* we never register a read callback for FW_CFG_SIGNATURE */
514 fw_cfg_select(s
, FW_CFG_SIGNATURE
);
517 /* Save restore 32 bit int as uint16_t
518 This is a Big hack, but it is how the old state did it.
519 Or we broke compatibility in the state, or we can't use struct tm
522 static int get_uint32_as_uint16(QEMUFile
*f
, void *pv
, size_t size
,
526 *v
= qemu_get_be16(f
);
530 static int put_unused(QEMUFile
*f
, void *pv
, size_t size
, VMStateField
*field
,
533 fprintf(stderr
, "uint32_as_uint16 is only used for backward compatibility.\n");
534 fprintf(stderr
, "This functions shouldn't be called.\n");
539 static const VMStateInfo vmstate_hack_uint32_as_uint16
= {
540 .name
= "int32_as_uint16",
541 .get
= get_uint32_as_uint16
,
545 #define VMSTATE_UINT16_HACK(_f, _s, _t) \
546 VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_hack_uint32_as_uint16, uint32_t)
549 static bool is_version_1(void *opaque
, int version_id
)
551 return version_id
== 1;
554 bool fw_cfg_dma_enabled(void *opaque
)
556 FWCfgState
*s
= opaque
;
558 return s
->dma_enabled
;
561 static const VMStateDescription vmstate_fw_cfg_dma
= {
562 .name
= "fw_cfg/dma",
563 .needed
= fw_cfg_dma_enabled
,
564 .fields
= (VMStateField
[]) {
565 VMSTATE_UINT64(dma_addr
, FWCfgState
),
566 VMSTATE_END_OF_LIST()
570 static const VMStateDescription vmstate_fw_cfg
= {
573 .minimum_version_id
= 1,
574 .fields
= (VMStateField
[]) {
575 VMSTATE_UINT16(cur_entry
, FWCfgState
),
576 VMSTATE_UINT16_HACK(cur_offset
, FWCfgState
, is_version_1
),
577 VMSTATE_UINT32_V(cur_offset
, FWCfgState
, 2),
578 VMSTATE_END_OF_LIST()
580 .subsections
= (const VMStateDescription
*[]) {
586 static void fw_cfg_add_bytes_callback(FWCfgState
*s
, uint16_t key
,
587 FWCfgCallback select_cb
,
588 FWCfgWriteCallback write_cb
,
589 void *callback_opaque
,
590 void *data
, size_t len
,
593 int arch
= !!(key
& FW_CFG_ARCH_LOCAL
);
595 key
&= FW_CFG_ENTRY_MASK
;
597 assert(key
< fw_cfg_max_entry(s
) && len
< UINT32_MAX
);
598 assert(s
->entries
[arch
][key
].data
== NULL
); /* avoid key conflict */
600 s
->entries
[arch
][key
].data
= data
;
601 s
->entries
[arch
][key
].len
= (uint32_t)len
;
602 s
->entries
[arch
][key
].select_cb
= select_cb
;
603 s
->entries
[arch
][key
].write_cb
= write_cb
;
604 s
->entries
[arch
][key
].callback_opaque
= callback_opaque
;
605 s
->entries
[arch
][key
].allow_write
= !read_only
;
608 static void *fw_cfg_modify_bytes_read(FWCfgState
*s
, uint16_t key
,
609 void *data
, size_t len
)
612 int arch
= !!(key
& FW_CFG_ARCH_LOCAL
);
614 key
&= FW_CFG_ENTRY_MASK
;
616 assert(key
< fw_cfg_max_entry(s
) && len
< UINT32_MAX
);
618 /* return the old data to the function caller, avoid memory leak */
619 ptr
= s
->entries
[arch
][key
].data
;
620 s
->entries
[arch
][key
].data
= data
;
621 s
->entries
[arch
][key
].len
= len
;
622 s
->entries
[arch
][key
].callback_opaque
= NULL
;
623 s
->entries
[arch
][key
].allow_write
= false;
628 void fw_cfg_add_bytes(FWCfgState
*s
, uint16_t key
, void *data
, size_t len
)
630 fw_cfg_add_bytes_callback(s
, key
, NULL
, NULL
, NULL
, data
, len
, true);
633 void fw_cfg_add_string(FWCfgState
*s
, uint16_t key
, const char *value
)
635 size_t sz
= strlen(value
) + 1;
637 fw_cfg_add_bytes(s
, key
, g_memdup(value
, sz
), sz
);
640 void fw_cfg_add_i16(FWCfgState
*s
, uint16_t key
, uint16_t value
)
644 copy
= g_malloc(sizeof(value
));
645 *copy
= cpu_to_le16(value
);
646 fw_cfg_add_bytes(s
, key
, copy
, sizeof(value
));
649 void fw_cfg_modify_i16(FWCfgState
*s
, uint16_t key
, uint16_t value
)
651 uint16_t *copy
, *old
;
653 copy
= g_malloc(sizeof(value
));
654 *copy
= cpu_to_le16(value
);
655 old
= fw_cfg_modify_bytes_read(s
, key
, copy
, sizeof(value
));
659 void fw_cfg_add_i32(FWCfgState
*s
, uint16_t key
, uint32_t value
)
663 copy
= g_malloc(sizeof(value
));
664 *copy
= cpu_to_le32(value
);
665 fw_cfg_add_bytes(s
, key
, copy
, sizeof(value
));
668 void fw_cfg_add_i64(FWCfgState
*s
, uint16_t key
, uint64_t value
)
672 copy
= g_malloc(sizeof(value
));
673 *copy
= cpu_to_le64(value
);
674 fw_cfg_add_bytes(s
, key
, copy
, sizeof(value
));
677 void fw_cfg_set_order_override(FWCfgState
*s
, int order
)
679 assert(s
->fw_cfg_order_override
== 0);
680 s
->fw_cfg_order_override
= order
;
683 void fw_cfg_reset_order_override(FWCfgState
*s
)
685 assert(s
->fw_cfg_order_override
!= 0);
686 s
->fw_cfg_order_override
= 0;
690 * This is the legacy order list. For legacy systems, files are in
691 * the fw_cfg in the order defined below, by the "order" value. Note
692 * that some entries (VGA ROMs, NIC option ROMS, etc.) go into a
693 * specific area, but there may be more than one and they occur in the
694 * order that the user specifies them on the command line. Those are
695 * handled in a special manner, using the order override above.
697 * For non-legacy, the files are sorted by filename to avoid this kind
698 * of complexity in the future.
700 * This is only for x86, other arches don't implement versioning so
701 * they won't set legacy mode.
707 { "etc/boot-menu-wait", 10 },
708 { "bootsplash.jpg", 11 },
709 { "bootsplash.bmp", 12 },
710 { "etc/boot-fail-wait", 15 },
711 { "etc/smbios/smbios-tables", 20 },
712 { "etc/smbios/smbios-anchor", 30 },
714 { "etc/reserved-memory-end", 50 },
715 { "genroms/kvmvapic.bin", 55 },
716 { "genroms/linuxboot.bin", 60 },
717 { }, /* VGA ROMs from pc_vga_init come here, 70. */
718 { }, /* NIC option ROMs from pc_nic_init come here, 80. */
719 { "etc/system-states", 90 },
720 { }, /* User ROMs come here, 100. */
721 { }, /* Device FW comes here, 110. */
722 { "etc/extra-pci-roots", 120 },
723 { "etc/acpi/tables", 130 },
724 { "etc/table-loader", 140 },
725 { "etc/tpm/log", 150 },
726 { "etc/acpi/rsdp", 160 },
727 { "bootorder", 170 },
729 #define FW_CFG_ORDER_OVERRIDE_LAST 200
732 static int get_fw_cfg_order(FWCfgState
*s
, const char *name
)
736 if (s
->fw_cfg_order_override
> 0) {
737 return s
->fw_cfg_order_override
;
740 for (i
= 0; i
< ARRAY_SIZE(fw_cfg_order
); i
++) {
741 if (fw_cfg_order
[i
].name
== NULL
) {
745 if (strcmp(name
, fw_cfg_order
[i
].name
) == 0) {
746 return fw_cfg_order
[i
].order
;
750 /* Stick unknown stuff at the end. */
751 warn_report("Unknown firmware file in legacy mode: %s", name
);
752 return FW_CFG_ORDER_OVERRIDE_LAST
;
755 void fw_cfg_add_file_callback(FWCfgState
*s
, const char *filename
,
756 FWCfgCallback select_cb
,
757 FWCfgWriteCallback write_cb
,
758 void *callback_opaque
,
759 void *data
, size_t len
, bool read_only
)
763 MachineClass
*mc
= MACHINE_GET_CLASS(qdev_get_machine());
767 dsize
= sizeof(uint32_t) + sizeof(FWCfgFile
) * fw_cfg_file_slots(s
);
768 s
->files
= g_malloc0(dsize
);
769 fw_cfg_add_bytes(s
, FW_CFG_FILE_DIR
, s
->files
, dsize
);
772 count
= be32_to_cpu(s
->files
->count
);
773 assert(count
< fw_cfg_file_slots(s
));
775 /* Find the insertion point. */
776 if (mc
->legacy_fw_cfg_order
) {
778 * Sort by order. For files with the same order, we keep them
779 * in the sequence in which they were added.
781 order
= get_fw_cfg_order(s
, filename
);
783 index
> 0 && order
< s
->entry_order
[index
- 1];
786 /* Sort by file name. */
788 index
> 0 && strcmp(filename
, s
->files
->f
[index
- 1].name
) < 0;
793 * Move all the entries from the index point and after down one
794 * to create a slot for the new entry. Because calculations are
795 * being done with the index, make it so that "i" is the current
796 * index and "i - 1" is the one being copied from, thus the
797 * unusual start and end in the for statement.
799 for (i
= count
; i
> index
; i
--) {
800 s
->files
->f
[i
] = s
->files
->f
[i
- 1];
801 s
->files
->f
[i
].select
= cpu_to_be16(FW_CFG_FILE_FIRST
+ i
);
802 s
->entries
[0][FW_CFG_FILE_FIRST
+ i
] =
803 s
->entries
[0][FW_CFG_FILE_FIRST
+ i
- 1];
804 s
->entry_order
[i
] = s
->entry_order
[i
- 1];
807 memset(&s
->files
->f
[index
], 0, sizeof(FWCfgFile
));
808 memset(&s
->entries
[0][FW_CFG_FILE_FIRST
+ index
], 0, sizeof(FWCfgEntry
));
810 pstrcpy(s
->files
->f
[index
].name
, sizeof(s
->files
->f
[index
].name
), filename
);
811 for (i
= 0; i
<= count
; i
++) {
813 strcmp(s
->files
->f
[index
].name
, s
->files
->f
[i
].name
) == 0) {
814 error_report("duplicate fw_cfg file name: %s",
815 s
->files
->f
[index
].name
);
820 fw_cfg_add_bytes_callback(s
, FW_CFG_FILE_FIRST
+ index
,
822 callback_opaque
, data
, len
,
825 s
->files
->f
[index
].size
= cpu_to_be32(len
);
826 s
->files
->f
[index
].select
= cpu_to_be16(FW_CFG_FILE_FIRST
+ index
);
827 s
->entry_order
[index
] = order
;
828 trace_fw_cfg_add_file(s
, index
, s
->files
->f
[index
].name
, len
);
830 s
->files
->count
= cpu_to_be32(count
+1);
833 void fw_cfg_add_file(FWCfgState
*s
, const char *filename
,
834 void *data
, size_t len
)
836 fw_cfg_add_file_callback(s
, filename
, NULL
, NULL
, NULL
, data
, len
, true);
839 void *fw_cfg_modify_file(FWCfgState
*s
, const char *filename
,
840 void *data
, size_t len
)
847 index
= be32_to_cpu(s
->files
->count
);
849 for (i
= 0; i
< index
; i
++) {
850 if (strcmp(filename
, s
->files
->f
[i
].name
) == 0) {
851 ptr
= fw_cfg_modify_bytes_read(s
, FW_CFG_FILE_FIRST
+ i
,
853 s
->files
->f
[i
].size
= cpu_to_be32(len
);
858 assert(index
< fw_cfg_file_slots(s
));
861 fw_cfg_add_file_callback(s
, filename
, NULL
, NULL
, NULL
, data
, len
, true);
865 static void fw_cfg_machine_reset(void *opaque
)
869 FWCfgState
*s
= opaque
;
870 char *bootindex
= get_boot_devices_list(&len
);
872 ptr
= fw_cfg_modify_file(s
, "bootorder", (uint8_t *)bootindex
, len
);
876 static void fw_cfg_machine_ready(struct Notifier
*n
, void *data
)
878 FWCfgState
*s
= container_of(n
, FWCfgState
, machine_ready
);
879 qemu_register_reset(fw_cfg_machine_reset
, s
);
884 static void fw_cfg_common_realize(DeviceState
*dev
, Error
**errp
)
886 FWCfgState
*s
= FW_CFG(dev
);
887 MachineState
*machine
= MACHINE(qdev_get_machine());
888 uint32_t version
= FW_CFG_VERSION
;
890 if (!fw_cfg_find()) {
891 error_setg(errp
, "at most one %s device is permitted", TYPE_FW_CFG
);
895 fw_cfg_add_bytes(s
, FW_CFG_SIGNATURE
, (char *)"QEMU", 4);
896 fw_cfg_add_bytes(s
, FW_CFG_UUID
, &qemu_uuid
, 16);
897 fw_cfg_add_i16(s
, FW_CFG_NOGRAPHIC
, (uint16_t)!machine
->enable_graphics
);
898 fw_cfg_add_i16(s
, FW_CFG_BOOT_MENU
, (uint16_t)boot_menu
);
899 fw_cfg_bootsplash(s
);
902 if (s
->dma_enabled
) {
903 version
|= FW_CFG_VERSION_DMA
;
906 fw_cfg_add_i32(s
, FW_CFG_ID
, version
);
908 s
->machine_ready
.notify
= fw_cfg_machine_ready
;
909 qemu_add_machine_init_done_notifier(&s
->machine_ready
);
912 FWCfgState
*fw_cfg_init_io_dma(uint32_t iobase
, uint32_t dma_iobase
,
913 AddressSpace
*dma_as
)
919 bool dma_requested
= dma_iobase
&& dma_as
;
921 dev
= qdev_create(NULL
, TYPE_FW_CFG_IO
);
922 if (!dma_requested
) {
923 qdev_prop_set_bit(dev
, "dma_enabled", false);
926 object_property_add_child(OBJECT(qdev_get_machine()), TYPE_FW_CFG
,
928 qdev_init_nofail(dev
);
930 sbd
= SYS_BUS_DEVICE(dev
);
931 ios
= FW_CFG_IO(dev
);
932 sysbus_add_io(sbd
, iobase
, &ios
->comb_iomem
);
936 if (s
->dma_enabled
) {
937 /* 64 bits for the address field */
940 sysbus_add_io(sbd
, dma_iobase
, &s
->dma_iomem
);
946 FWCfgState
*fw_cfg_init_io(uint32_t iobase
)
948 return fw_cfg_init_io_dma(iobase
, 0, NULL
);
951 FWCfgState
*fw_cfg_init_mem_wide(hwaddr ctl_addr
,
952 hwaddr data_addr
, uint32_t data_width
,
953 hwaddr dma_addr
, AddressSpace
*dma_as
)
958 bool dma_requested
= dma_addr
&& dma_as
;
960 dev
= qdev_create(NULL
, TYPE_FW_CFG_MEM
);
961 qdev_prop_set_uint32(dev
, "data_width", data_width
);
962 if (!dma_requested
) {
963 qdev_prop_set_bit(dev
, "dma_enabled", false);
966 object_property_add_child(OBJECT(qdev_get_machine()), TYPE_FW_CFG
,
968 qdev_init_nofail(dev
);
970 sbd
= SYS_BUS_DEVICE(dev
);
971 sysbus_mmio_map(sbd
, 0, ctl_addr
);
972 sysbus_mmio_map(sbd
, 1, data_addr
);
976 if (s
->dma_enabled
) {
979 sysbus_mmio_map(sbd
, 2, dma_addr
);
985 FWCfgState
*fw_cfg_init_mem(hwaddr ctl_addr
, hwaddr data_addr
)
987 return fw_cfg_init_mem_wide(ctl_addr
, data_addr
,
988 fw_cfg_data_mem_ops
.valid
.max_access_size
,
993 FWCfgState
*fw_cfg_find(void)
995 /* Returns NULL unless there is exactly one fw_cfg device */
996 return FW_CFG(object_resolve_path_type("", TYPE_FW_CFG
, NULL
));
1000 static void fw_cfg_class_init(ObjectClass
*klass
, void *data
)
1002 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1004 dc
->reset
= fw_cfg_reset
;
1005 dc
->vmsd
= &vmstate_fw_cfg
;
1008 static const TypeInfo fw_cfg_info
= {
1009 .name
= TYPE_FW_CFG
,
1010 .parent
= TYPE_SYS_BUS_DEVICE
,
1012 .instance_size
= sizeof(FWCfgState
),
1013 .class_init
= fw_cfg_class_init
,
1016 static void fw_cfg_file_slots_allocate(FWCfgState
*s
, Error
**errp
)
1018 uint16_t file_slots_max
;
1020 if (fw_cfg_file_slots(s
) < FW_CFG_FILE_SLOTS_MIN
) {
1021 error_setg(errp
, "\"file_slots\" must be at least 0x%x",
1022 FW_CFG_FILE_SLOTS_MIN
);
1026 /* (UINT16_MAX & FW_CFG_ENTRY_MASK) is the highest inclusive selector value
1027 * that we permit. The actual (exclusive) value coming from the
1028 * configuration is (FW_CFG_FILE_FIRST + fw_cfg_file_slots(s)). */
1029 file_slots_max
= (UINT16_MAX
& FW_CFG_ENTRY_MASK
) - FW_CFG_FILE_FIRST
+ 1;
1030 if (fw_cfg_file_slots(s
) > file_slots_max
) {
1031 error_setg(errp
, "\"file_slots\" must not exceed 0x%" PRIx16
,
1036 s
->entries
[0] = g_new0(FWCfgEntry
, fw_cfg_max_entry(s
));
1037 s
->entries
[1] = g_new0(FWCfgEntry
, fw_cfg_max_entry(s
));
1038 s
->entry_order
= g_new0(int, fw_cfg_max_entry(s
));
1041 static Property fw_cfg_io_properties
[] = {
1042 DEFINE_PROP_BOOL("dma_enabled", FWCfgIoState
, parent_obj
.dma_enabled
,
1044 DEFINE_PROP_UINT16("x-file-slots", FWCfgIoState
, parent_obj
.file_slots
,
1045 FW_CFG_FILE_SLOTS_DFLT
),
1046 DEFINE_PROP_END_OF_LIST(),
1049 static void fw_cfg_io_realize(DeviceState
*dev
, Error
**errp
)
1051 FWCfgIoState
*s
= FW_CFG_IO(dev
);
1052 Error
*local_err
= NULL
;
1054 fw_cfg_file_slots_allocate(FW_CFG(s
), &local_err
);
1056 error_propagate(errp
, local_err
);
1060 /* when using port i/o, the 8-bit data register ALWAYS overlaps
1061 * with half of the 16-bit control register. Hence, the total size
1062 * of the i/o region used is FW_CFG_CTL_SIZE */
1063 memory_region_init_io(&s
->comb_iomem
, OBJECT(s
), &fw_cfg_comb_mem_ops
,
1064 FW_CFG(s
), "fwcfg", FW_CFG_CTL_SIZE
);
1066 if (FW_CFG(s
)->dma_enabled
) {
1067 memory_region_init_io(&FW_CFG(s
)->dma_iomem
, OBJECT(s
),
1068 &fw_cfg_dma_mem_ops
, FW_CFG(s
), "fwcfg.dma",
1069 sizeof(dma_addr_t
));
1072 fw_cfg_common_realize(dev
, errp
);
1075 static void fw_cfg_io_class_init(ObjectClass
*klass
, void *data
)
1077 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1079 dc
->realize
= fw_cfg_io_realize
;
1080 dc
->props
= fw_cfg_io_properties
;
1083 static const TypeInfo fw_cfg_io_info
= {
1084 .name
= TYPE_FW_CFG_IO
,
1085 .parent
= TYPE_FW_CFG
,
1086 .instance_size
= sizeof(FWCfgIoState
),
1087 .class_init
= fw_cfg_io_class_init
,
1091 static Property fw_cfg_mem_properties
[] = {
1092 DEFINE_PROP_UINT32("data_width", FWCfgMemState
, data_width
, -1),
1093 DEFINE_PROP_BOOL("dma_enabled", FWCfgMemState
, parent_obj
.dma_enabled
,
1095 DEFINE_PROP_UINT16("x-file-slots", FWCfgMemState
, parent_obj
.file_slots
,
1096 FW_CFG_FILE_SLOTS_DFLT
),
1097 DEFINE_PROP_END_OF_LIST(),
1100 static void fw_cfg_mem_realize(DeviceState
*dev
, Error
**errp
)
1102 FWCfgMemState
*s
= FW_CFG_MEM(dev
);
1103 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
1104 const MemoryRegionOps
*data_ops
= &fw_cfg_data_mem_ops
;
1105 Error
*local_err
= NULL
;
1107 fw_cfg_file_slots_allocate(FW_CFG(s
), &local_err
);
1109 error_propagate(errp
, local_err
);
1113 memory_region_init_io(&s
->ctl_iomem
, OBJECT(s
), &fw_cfg_ctl_mem_ops
,
1114 FW_CFG(s
), "fwcfg.ctl", FW_CFG_CTL_SIZE
);
1115 sysbus_init_mmio(sbd
, &s
->ctl_iomem
);
1117 if (s
->data_width
> data_ops
->valid
.max_access_size
) {
1118 s
->wide_data_ops
= *data_ops
;
1120 s
->wide_data_ops
.valid
.max_access_size
= s
->data_width
;
1121 s
->wide_data_ops
.impl
.max_access_size
= s
->data_width
;
1122 data_ops
= &s
->wide_data_ops
;
1124 memory_region_init_io(&s
->data_iomem
, OBJECT(s
), data_ops
, FW_CFG(s
),
1125 "fwcfg.data", data_ops
->valid
.max_access_size
);
1126 sysbus_init_mmio(sbd
, &s
->data_iomem
);
1128 if (FW_CFG(s
)->dma_enabled
) {
1129 memory_region_init_io(&FW_CFG(s
)->dma_iomem
, OBJECT(s
),
1130 &fw_cfg_dma_mem_ops
, FW_CFG(s
), "fwcfg.dma",
1131 sizeof(dma_addr_t
));
1132 sysbus_init_mmio(sbd
, &FW_CFG(s
)->dma_iomem
);
1135 fw_cfg_common_realize(dev
, errp
);
1138 static void fw_cfg_mem_class_init(ObjectClass
*klass
, void *data
)
1140 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1142 dc
->realize
= fw_cfg_mem_realize
;
1143 dc
->props
= fw_cfg_mem_properties
;
1146 static const TypeInfo fw_cfg_mem_info
= {
1147 .name
= TYPE_FW_CFG_MEM
,
1148 .parent
= TYPE_FW_CFG
,
1149 .instance_size
= sizeof(FWCfgMemState
),
1150 .class_init
= fw_cfg_mem_class_init
,
1154 static void fw_cfg_register_types(void)
1156 type_register_static(&fw_cfg_info
);
1157 type_register_static(&fw_cfg_io_info
);
1158 type_register_static(&fw_cfg_mem_info
);
1161 type_init(fw_cfg_register_types
)