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/nvram/fw_cfg.h"
31 #include "hw/sysbus.h"
33 #include "qemu/error-report.h"
34 #include "qemu/option.h"
35 #include "qemu/config-file.h"
36 #include "qemu/cutils.h"
37 #include "qapi/error.h"
39 #define FW_CFG_FILE_SLOTS_DFLT 0x20
41 /* FW_CFG_VERSION bits */
42 #define FW_CFG_VERSION 0x01
43 #define FW_CFG_VERSION_DMA 0x02
45 /* FW_CFG_DMA_CONTROL bits */
46 #define FW_CFG_DMA_CTL_ERROR 0x01
47 #define FW_CFG_DMA_CTL_READ 0x02
48 #define FW_CFG_DMA_CTL_SKIP 0x04
49 #define FW_CFG_DMA_CTL_SELECT 0x08
50 #define FW_CFG_DMA_CTL_WRITE 0x10
52 #define FW_CFG_DMA_SIGNATURE 0x51454d5520434647ULL /* "QEMU CFG" */
58 void *callback_opaque
;
59 FWCfgCallback select_cb
;
60 FWCfgWriteCallback write_cb
;
66 static char *read_splashfile(char *filename
, gsize
*file_sizep
,
72 unsigned int filehead
;
75 if (!g_file_get_contents(filename
, &content
, file_sizep
, &err
)) {
76 error_report("failed to read splash file '%s': %s",
77 filename
, err
->message
);
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 const char *boot_splash_filename
= NULL
;
120 const char *boot_splash_time
= NULL
;
121 char *filename
, *file_data
;
125 /* get user configuration */
126 QemuOptsList
*plist
= qemu_find_opts("boot-opts");
127 QemuOpts
*opts
= QTAILQ_FIRST(&plist
->head
);
128 boot_splash_filename
= qemu_opt_get(opts
, "splash");
129 boot_splash_time
= qemu_opt_get(opts
, "splash-time");
131 /* insert splash time if user configurated */
132 if (boot_splash_time
) {
133 int64_t bst_val
= qemu_opt_get_number(opts
, "splash-time", -1);
136 /* validate the input */
137 if (bst_val
< 0 || bst_val
> 0xffff) {
138 error_report("splash-time is invalid,"
139 "it should be a value between 0 and 65535");
142 /* use little endian format */
143 bst_le16
= cpu_to_le16(bst_val
);
144 fw_cfg_add_file(s
, "etc/boot-menu-wait",
145 g_memdup(&bst_le16
, sizeof bst_le16
), sizeof bst_le16
);
148 /* insert splash file if user configurated */
149 if (boot_splash_filename
) {
150 filename
= qemu_find_file(QEMU_FILE_TYPE_BIOS
, boot_splash_filename
);
151 if (filename
== NULL
) {
152 error_report("failed to find file '%s'", boot_splash_filename
);
156 /* loading file data */
157 file_data
= read_splashfile(filename
, &file_size
, &file_type
);
158 if (file_data
== NULL
) {
162 g_free(boot_splash_filedata
);
163 boot_splash_filedata
= (uint8_t *)file_data
;
164 boot_splash_filedata_size
= file_size
;
167 if (file_type
== JPG_FILE
) {
168 fw_cfg_add_file(s
, "bootsplash.jpg",
169 boot_splash_filedata
, boot_splash_filedata_size
);
171 fw_cfg_add_file(s
, "bootsplash.bmp",
172 boot_splash_filedata
, boot_splash_filedata_size
);
178 static void fw_cfg_reboot(FWCfgState
*s
)
180 const char *reboot_timeout
= NULL
;
183 /* get user configuration */
184 QemuOptsList
*plist
= qemu_find_opts("boot-opts");
185 QemuOpts
*opts
= QTAILQ_FIRST(&plist
->head
);
186 reboot_timeout
= qemu_opt_get(opts
, "reboot-timeout");
188 if (reboot_timeout
) {
189 rt_val
= qemu_opt_get_number(opts
, "reboot-timeout", -1);
190 /* validate the input */
191 if (rt_val
< 0 || rt_val
> 0xffff) {
192 error_report("reboot timeout is invalid,"
193 "it should be a value between 0 and 65535");
198 fw_cfg_add_file(s
, "etc/boot-fail-wait", g_memdup(&rt_val
, 4), 4);
201 static void fw_cfg_write(FWCfgState
*s
, uint8_t value
)
203 /* nothing, write support removed in QEMU v2.4+ */
206 static inline uint16_t fw_cfg_file_slots(const FWCfgState
*s
)
208 return s
->file_slots
;
211 /* Note: this function returns an exclusive limit. */
212 static inline uint32_t fw_cfg_max_entry(const FWCfgState
*s
)
214 return FW_CFG_FILE_FIRST
+ fw_cfg_file_slots(s
);
217 static int fw_cfg_select(FWCfgState
*s
, uint16_t key
)
223 if ((key
& FW_CFG_ENTRY_MASK
) >= fw_cfg_max_entry(s
)) {
224 s
->cur_entry
= FW_CFG_INVALID
;
229 /* entry successfully selected, now run callback if present */
230 arch
= !!(key
& FW_CFG_ARCH_LOCAL
);
231 e
= &s
->entries
[arch
][key
& FW_CFG_ENTRY_MASK
];
233 e
->select_cb(e
->callback_opaque
);
237 trace_fw_cfg_select(s
, key
, ret
);
241 static uint64_t fw_cfg_data_read(void *opaque
, hwaddr addr
, unsigned size
)
243 FWCfgState
*s
= opaque
;
244 int arch
= !!(s
->cur_entry
& FW_CFG_ARCH_LOCAL
);
245 FWCfgEntry
*e
= (s
->cur_entry
== FW_CFG_INVALID
) ? NULL
:
246 &s
->entries
[arch
][s
->cur_entry
& FW_CFG_ENTRY_MASK
];
249 assert(size
> 0 && size
<= sizeof(value
));
250 if (s
->cur_entry
!= FW_CFG_INVALID
&& e
->data
&& s
->cur_offset
< e
->len
) {
251 /* The least significant 'size' bytes of the return value are
252 * expected to contain a string preserving portion of the item
253 * data, padded with zeros on the right in case we run out early.
254 * In technical terms, we're composing the host-endian representation
255 * of the big endian interpretation of the fw_cfg string.
258 value
= (value
<< 8) | e
->data
[s
->cur_offset
++];
259 } while (--size
&& s
->cur_offset
< e
->len
);
260 /* If size is still not zero, we *did* run out early, so continue
261 * left-shifting, to add the appropriate number of padding zeros
267 trace_fw_cfg_read(s
, value
);
271 static void fw_cfg_data_mem_write(void *opaque
, hwaddr addr
,
272 uint64_t value
, unsigned size
)
274 FWCfgState
*s
= opaque
;
278 fw_cfg_write(s
, value
>> (8 * --i
));
282 static void fw_cfg_dma_transfer(FWCfgState
*s
)
288 int read
= 0, write
= 0;
291 /* Reset the address before the next access */
292 dma_addr
= s
->dma_addr
;
295 if (dma_memory_read(s
->dma_as
, dma_addr
, &dma
, sizeof(dma
))) {
296 stl_be_dma(s
->dma_as
, dma_addr
+ offsetof(FWCfgDmaAccess
, control
),
297 FW_CFG_DMA_CTL_ERROR
);
301 dma
.address
= be64_to_cpu(dma
.address
);
302 dma
.length
= be32_to_cpu(dma
.length
);
303 dma
.control
= be32_to_cpu(dma
.control
);
305 if (dma
.control
& FW_CFG_DMA_CTL_SELECT
) {
306 fw_cfg_select(s
, dma
.control
>> 16);
309 arch
= !!(s
->cur_entry
& FW_CFG_ARCH_LOCAL
);
310 e
= (s
->cur_entry
== FW_CFG_INVALID
) ? NULL
:
311 &s
->entries
[arch
][s
->cur_entry
& FW_CFG_ENTRY_MASK
];
313 if (dma
.control
& FW_CFG_DMA_CTL_READ
) {
316 } else if (dma
.control
& FW_CFG_DMA_CTL_WRITE
) {
319 } else if (dma
.control
& FW_CFG_DMA_CTL_SKIP
) {
328 while (dma
.length
> 0 && !(dma
.control
& FW_CFG_DMA_CTL_ERROR
)) {
329 if (s
->cur_entry
== FW_CFG_INVALID
|| !e
->data
||
330 s
->cur_offset
>= e
->len
) {
333 /* If the access is not a read access, it will be a skip access,
337 if (dma_memory_set(s
->dma_as
, dma
.address
, 0, len
)) {
338 dma
.control
|= FW_CFG_DMA_CTL_ERROR
;
342 dma
.control
|= FW_CFG_DMA_CTL_ERROR
;
345 if (dma
.length
<= (e
->len
- s
->cur_offset
)) {
348 len
= (e
->len
- s
->cur_offset
);
351 /* If the access is not a read access, it will be a skip access,
355 if (dma_memory_write(s
->dma_as
, dma
.address
,
356 &e
->data
[s
->cur_offset
], len
)) {
357 dma
.control
|= FW_CFG_DMA_CTL_ERROR
;
361 if (!e
->allow_write
||
363 dma_memory_read(s
->dma_as
, dma
.address
,
364 &e
->data
[s
->cur_offset
], len
)) {
365 dma
.control
|= FW_CFG_DMA_CTL_ERROR
;
366 } else if (e
->write_cb
) {
367 e
->write_cb(e
->callback_opaque
, s
->cur_offset
, len
);
371 s
->cur_offset
+= len
;
379 stl_be_dma(s
->dma_as
, dma_addr
+ offsetof(FWCfgDmaAccess
, control
),
382 trace_fw_cfg_read(s
, 0);
385 static uint64_t fw_cfg_dma_mem_read(void *opaque
, hwaddr addr
,
388 /* Return a signature value (and handle various read sizes) */
389 return extract64(FW_CFG_DMA_SIGNATURE
, (8 - addr
- size
) * 8, size
* 8);
392 static void fw_cfg_dma_mem_write(void *opaque
, hwaddr addr
,
393 uint64_t value
, unsigned size
)
395 FWCfgState
*s
= opaque
;
399 /* FWCfgDmaAccess high address */
400 s
->dma_addr
= value
<< 32;
401 } else if (addr
== 4) {
402 /* FWCfgDmaAccess low address */
403 s
->dma_addr
|= value
;
404 fw_cfg_dma_transfer(s
);
406 } else if (size
== 8 && addr
== 0) {
408 fw_cfg_dma_transfer(s
);
412 static bool fw_cfg_dma_mem_valid(void *opaque
, hwaddr addr
,
413 unsigned size
, bool is_write
,
416 return !is_write
|| ((size
== 4 && (addr
== 0 || addr
== 4)) ||
417 (size
== 8 && addr
== 0));
420 static bool fw_cfg_data_mem_valid(void *opaque
, hwaddr addr
,
421 unsigned size
, bool is_write
,
427 static uint64_t fw_cfg_ctl_mem_read(void *opaque
, hwaddr addr
, unsigned size
)
432 static void fw_cfg_ctl_mem_write(void *opaque
, hwaddr addr
,
433 uint64_t value
, unsigned size
)
435 fw_cfg_select(opaque
, (uint16_t)value
);
438 static bool fw_cfg_ctl_mem_valid(void *opaque
, hwaddr addr
,
439 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
,
462 return (size
== 1) || (is_write
&& size
== 2);
465 static const MemoryRegionOps fw_cfg_ctl_mem_ops
= {
466 .read
= fw_cfg_ctl_mem_read
,
467 .write
= fw_cfg_ctl_mem_write
,
468 .endianness
= DEVICE_BIG_ENDIAN
,
469 .valid
.accepts
= fw_cfg_ctl_mem_valid
,
472 static const MemoryRegionOps fw_cfg_data_mem_ops
= {
473 .read
= fw_cfg_data_read
,
474 .write
= fw_cfg_data_mem_write
,
475 .endianness
= DEVICE_BIG_ENDIAN
,
477 .min_access_size
= 1,
478 .max_access_size
= 1,
479 .accepts
= fw_cfg_data_mem_valid
,
483 static const MemoryRegionOps fw_cfg_comb_mem_ops
= {
484 .read
= fw_cfg_data_read
,
485 .write
= fw_cfg_comb_write
,
486 .endianness
= DEVICE_LITTLE_ENDIAN
,
487 .valid
.accepts
= fw_cfg_comb_valid
,
490 static const MemoryRegionOps fw_cfg_dma_mem_ops
= {
491 .read
= fw_cfg_dma_mem_read
,
492 .write
= fw_cfg_dma_mem_write
,
493 .endianness
= DEVICE_BIG_ENDIAN
,
494 .valid
.accepts
= fw_cfg_dma_mem_valid
,
495 .valid
.max_access_size
= 8,
496 .impl
.max_access_size
= 8,
499 static void fw_cfg_reset(DeviceState
*d
)
501 FWCfgState
*s
= FW_CFG(d
);
503 /* we never register a read callback for FW_CFG_SIGNATURE */
504 fw_cfg_select(s
, FW_CFG_SIGNATURE
);
507 /* Save restore 32 bit int as uint16_t
508 This is a Big hack, but it is how the old state did it.
509 Or we broke compatibility in the state, or we can't use struct tm
512 static int get_uint32_as_uint16(QEMUFile
*f
, void *pv
, size_t size
,
513 const VMStateField
*field
)
516 *v
= qemu_get_be16(f
);
520 static int put_unused(QEMUFile
*f
, void *pv
, size_t size
,
521 const VMStateField
*field
, QJSON
*vmdesc
)
523 fprintf(stderr
, "uint32_as_uint16 is only used for backward compatibility.\n");
524 fprintf(stderr
, "This functions shouldn't be called.\n");
529 static const VMStateInfo vmstate_hack_uint32_as_uint16
= {
530 .name
= "int32_as_uint16",
531 .get
= get_uint32_as_uint16
,
535 #define VMSTATE_UINT16_HACK(_f, _s, _t) \
536 VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_hack_uint32_as_uint16, uint32_t)
539 static bool is_version_1(void *opaque
, int version_id
)
541 return version_id
== 1;
544 bool fw_cfg_dma_enabled(void *opaque
)
546 FWCfgState
*s
= opaque
;
548 return s
->dma_enabled
;
551 static const VMStateDescription vmstate_fw_cfg_dma
= {
552 .name
= "fw_cfg/dma",
553 .needed
= fw_cfg_dma_enabled
,
554 .fields
= (VMStateField
[]) {
555 VMSTATE_UINT64(dma_addr
, FWCfgState
),
556 VMSTATE_END_OF_LIST()
560 static const VMStateDescription vmstate_fw_cfg
= {
563 .minimum_version_id
= 1,
564 .fields
= (VMStateField
[]) {
565 VMSTATE_UINT16(cur_entry
, FWCfgState
),
566 VMSTATE_UINT16_HACK(cur_offset
, FWCfgState
, is_version_1
),
567 VMSTATE_UINT32_V(cur_offset
, FWCfgState
, 2),
568 VMSTATE_END_OF_LIST()
570 .subsections
= (const VMStateDescription
*[]) {
576 static void fw_cfg_add_bytes_callback(FWCfgState
*s
, uint16_t key
,
577 FWCfgCallback select_cb
,
578 FWCfgWriteCallback write_cb
,
579 void *callback_opaque
,
580 void *data
, size_t len
,
583 int arch
= !!(key
& FW_CFG_ARCH_LOCAL
);
585 key
&= FW_CFG_ENTRY_MASK
;
587 assert(key
< fw_cfg_max_entry(s
) && len
< UINT32_MAX
);
588 assert(s
->entries
[arch
][key
].data
== NULL
); /* avoid key conflict */
590 s
->entries
[arch
][key
].data
= data
;
591 s
->entries
[arch
][key
].len
= (uint32_t)len
;
592 s
->entries
[arch
][key
].select_cb
= select_cb
;
593 s
->entries
[arch
][key
].write_cb
= write_cb
;
594 s
->entries
[arch
][key
].callback_opaque
= callback_opaque
;
595 s
->entries
[arch
][key
].allow_write
= !read_only
;
598 static void *fw_cfg_modify_bytes_read(FWCfgState
*s
, uint16_t key
,
599 void *data
, size_t len
)
602 int arch
= !!(key
& FW_CFG_ARCH_LOCAL
);
604 key
&= FW_CFG_ENTRY_MASK
;
606 assert(key
< fw_cfg_max_entry(s
) && len
< UINT32_MAX
);
608 /* return the old data to the function caller, avoid memory leak */
609 ptr
= s
->entries
[arch
][key
].data
;
610 s
->entries
[arch
][key
].data
= data
;
611 s
->entries
[arch
][key
].len
= len
;
612 s
->entries
[arch
][key
].callback_opaque
= NULL
;
613 s
->entries
[arch
][key
].allow_write
= false;
618 void fw_cfg_add_bytes(FWCfgState
*s
, uint16_t key
, void *data
, size_t len
)
620 fw_cfg_add_bytes_callback(s
, key
, NULL
, NULL
, NULL
, data
, len
, true);
623 void fw_cfg_add_string(FWCfgState
*s
, uint16_t key
, const char *value
)
625 size_t sz
= strlen(value
) + 1;
627 fw_cfg_add_bytes(s
, key
, g_memdup(value
, sz
), sz
);
630 void fw_cfg_add_i16(FWCfgState
*s
, uint16_t key
, uint16_t value
)
634 copy
= g_malloc(sizeof(value
));
635 *copy
= cpu_to_le16(value
);
636 fw_cfg_add_bytes(s
, key
, copy
, sizeof(value
));
639 void fw_cfg_modify_i16(FWCfgState
*s
, uint16_t key
, uint16_t value
)
641 uint16_t *copy
, *old
;
643 copy
= g_malloc(sizeof(value
));
644 *copy
= cpu_to_le16(value
);
645 old
= fw_cfg_modify_bytes_read(s
, key
, copy
, sizeof(value
));
649 void fw_cfg_add_i32(FWCfgState
*s
, uint16_t key
, uint32_t value
)
653 copy
= g_malloc(sizeof(value
));
654 *copy
= cpu_to_le32(value
);
655 fw_cfg_add_bytes(s
, key
, copy
, sizeof(value
));
658 void fw_cfg_add_i64(FWCfgState
*s
, uint16_t key
, uint64_t value
)
662 copy
= g_malloc(sizeof(value
));
663 *copy
= cpu_to_le64(value
);
664 fw_cfg_add_bytes(s
, key
, copy
, sizeof(value
));
667 void fw_cfg_set_order_override(FWCfgState
*s
, int order
)
669 assert(s
->fw_cfg_order_override
== 0);
670 s
->fw_cfg_order_override
= order
;
673 void fw_cfg_reset_order_override(FWCfgState
*s
)
675 assert(s
->fw_cfg_order_override
!= 0);
676 s
->fw_cfg_order_override
= 0;
680 * This is the legacy order list. For legacy systems, files are in
681 * the fw_cfg in the order defined below, by the "order" value. Note
682 * that some entries (VGA ROMs, NIC option ROMS, etc.) go into a
683 * specific area, but there may be more than one and they occur in the
684 * order that the user specifies them on the command line. Those are
685 * handled in a special manner, using the order override above.
687 * For non-legacy, the files are sorted by filename to avoid this kind
688 * of complexity in the future.
690 * This is only for x86, other arches don't implement versioning so
691 * they won't set legacy mode.
697 { "etc/boot-menu-wait", 10 },
698 { "bootsplash.jpg", 11 },
699 { "bootsplash.bmp", 12 },
700 { "etc/boot-fail-wait", 15 },
701 { "etc/smbios/smbios-tables", 20 },
702 { "etc/smbios/smbios-anchor", 30 },
704 { "etc/reserved-memory-end", 50 },
705 { "genroms/kvmvapic.bin", 55 },
706 { "genroms/linuxboot.bin", 60 },
707 { }, /* VGA ROMs from pc_vga_init come here, 70. */
708 { }, /* NIC option ROMs from pc_nic_init come here, 80. */
709 { "etc/system-states", 90 },
710 { }, /* User ROMs come here, 100. */
711 { }, /* Device FW comes here, 110. */
712 { "etc/extra-pci-roots", 120 },
713 { "etc/acpi/tables", 130 },
714 { "etc/table-loader", 140 },
715 { "etc/tpm/log", 150 },
716 { "etc/acpi/rsdp", 160 },
717 { "bootorder", 170 },
719 #define FW_CFG_ORDER_OVERRIDE_LAST 200
722 static int get_fw_cfg_order(FWCfgState
*s
, const char *name
)
726 if (s
->fw_cfg_order_override
> 0) {
727 return s
->fw_cfg_order_override
;
730 for (i
= 0; i
< ARRAY_SIZE(fw_cfg_order
); i
++) {
731 if (fw_cfg_order
[i
].name
== NULL
) {
735 if (strcmp(name
, fw_cfg_order
[i
].name
) == 0) {
736 return fw_cfg_order
[i
].order
;
740 /* Stick unknown stuff at the end. */
741 warn_report("Unknown firmware file in legacy mode: %s", name
);
742 return FW_CFG_ORDER_OVERRIDE_LAST
;
745 void fw_cfg_add_file_callback(FWCfgState
*s
, const char *filename
,
746 FWCfgCallback select_cb
,
747 FWCfgWriteCallback write_cb
,
748 void *callback_opaque
,
749 void *data
, size_t len
, bool read_only
)
753 MachineClass
*mc
= MACHINE_GET_CLASS(qdev_get_machine());
757 dsize
= sizeof(uint32_t) + sizeof(FWCfgFile
) * fw_cfg_file_slots(s
);
758 s
->files
= g_malloc0(dsize
);
759 fw_cfg_add_bytes(s
, FW_CFG_FILE_DIR
, s
->files
, dsize
);
762 count
= be32_to_cpu(s
->files
->count
);
763 assert(count
< fw_cfg_file_slots(s
));
765 /* Find the insertion point. */
766 if (mc
->legacy_fw_cfg_order
) {
768 * Sort by order. For files with the same order, we keep them
769 * in the sequence in which they were added.
771 order
= get_fw_cfg_order(s
, filename
);
773 index
> 0 && order
< s
->entry_order
[index
- 1];
776 /* Sort by file name. */
778 index
> 0 && strcmp(filename
, s
->files
->f
[index
- 1].name
) < 0;
783 * Move all the entries from the index point and after down one
784 * to create a slot for the new entry. Because calculations are
785 * being done with the index, make it so that "i" is the current
786 * index and "i - 1" is the one being copied from, thus the
787 * unusual start and end in the for statement.
789 for (i
= count
; i
> index
; i
--) {
790 s
->files
->f
[i
] = s
->files
->f
[i
- 1];
791 s
->files
->f
[i
].select
= cpu_to_be16(FW_CFG_FILE_FIRST
+ i
);
792 s
->entries
[0][FW_CFG_FILE_FIRST
+ i
] =
793 s
->entries
[0][FW_CFG_FILE_FIRST
+ i
- 1];
794 s
->entry_order
[i
] = s
->entry_order
[i
- 1];
797 memset(&s
->files
->f
[index
], 0, sizeof(FWCfgFile
));
798 memset(&s
->entries
[0][FW_CFG_FILE_FIRST
+ index
], 0, sizeof(FWCfgEntry
));
800 pstrcpy(s
->files
->f
[index
].name
, sizeof(s
->files
->f
[index
].name
), filename
);
801 for (i
= 0; i
<= count
; i
++) {
803 strcmp(s
->files
->f
[index
].name
, s
->files
->f
[i
].name
) == 0) {
804 error_report("duplicate fw_cfg file name: %s",
805 s
->files
->f
[index
].name
);
810 fw_cfg_add_bytes_callback(s
, FW_CFG_FILE_FIRST
+ index
,
812 callback_opaque
, data
, len
,
815 s
->files
->f
[index
].size
= cpu_to_be32(len
);
816 s
->files
->f
[index
].select
= cpu_to_be16(FW_CFG_FILE_FIRST
+ index
);
817 s
->entry_order
[index
] = order
;
818 trace_fw_cfg_add_file(s
, index
, s
->files
->f
[index
].name
, len
);
820 s
->files
->count
= cpu_to_be32(count
+1);
823 void fw_cfg_add_file(FWCfgState
*s
, const char *filename
,
824 void *data
, size_t len
)
826 fw_cfg_add_file_callback(s
, filename
, NULL
, NULL
, NULL
, data
, len
, true);
829 void *fw_cfg_modify_file(FWCfgState
*s
, const char *filename
,
830 void *data
, size_t len
)
837 index
= be32_to_cpu(s
->files
->count
);
839 for (i
= 0; i
< index
; i
++) {
840 if (strcmp(filename
, s
->files
->f
[i
].name
) == 0) {
841 ptr
= fw_cfg_modify_bytes_read(s
, FW_CFG_FILE_FIRST
+ i
,
843 s
->files
->f
[i
].size
= cpu_to_be32(len
);
848 assert(index
< fw_cfg_file_slots(s
));
851 fw_cfg_add_file_callback(s
, filename
, NULL
, NULL
, NULL
, data
, len
, true);
855 static void fw_cfg_machine_reset(void *opaque
)
859 FWCfgState
*s
= opaque
;
860 char *bootindex
= get_boot_devices_list(&len
);
862 ptr
= fw_cfg_modify_file(s
, "bootorder", (uint8_t *)bootindex
, len
);
866 static void fw_cfg_machine_ready(struct Notifier
*n
, void *data
)
868 FWCfgState
*s
= container_of(n
, FWCfgState
, machine_ready
);
869 qemu_register_reset(fw_cfg_machine_reset
, s
);
874 static void fw_cfg_common_realize(DeviceState
*dev
, Error
**errp
)
876 FWCfgState
*s
= FW_CFG(dev
);
877 MachineState
*machine
= MACHINE(qdev_get_machine());
878 uint32_t version
= FW_CFG_VERSION
;
880 if (!fw_cfg_find()) {
881 error_setg(errp
, "at most one %s device is permitted", TYPE_FW_CFG
);
885 fw_cfg_add_bytes(s
, FW_CFG_SIGNATURE
, (char *)"QEMU", 4);
886 fw_cfg_add_bytes(s
, FW_CFG_UUID
, &qemu_uuid
, 16);
887 fw_cfg_add_i16(s
, FW_CFG_NOGRAPHIC
, (uint16_t)!machine
->enable_graphics
);
888 fw_cfg_add_i16(s
, FW_CFG_BOOT_MENU
, (uint16_t)boot_menu
);
889 fw_cfg_bootsplash(s
);
892 if (s
->dma_enabled
) {
893 version
|= FW_CFG_VERSION_DMA
;
896 fw_cfg_add_i32(s
, FW_CFG_ID
, version
);
898 s
->machine_ready
.notify
= fw_cfg_machine_ready
;
899 qemu_add_machine_init_done_notifier(&s
->machine_ready
);
902 FWCfgState
*fw_cfg_init_io_dma(uint32_t iobase
, uint32_t dma_iobase
,
903 AddressSpace
*dma_as
)
909 bool dma_requested
= dma_iobase
&& dma_as
;
911 dev
= qdev_create(NULL
, TYPE_FW_CFG_IO
);
912 if (!dma_requested
) {
913 qdev_prop_set_bit(dev
, "dma_enabled", false);
916 object_property_add_child(OBJECT(qdev_get_machine()), TYPE_FW_CFG
,
918 qdev_init_nofail(dev
);
920 sbd
= SYS_BUS_DEVICE(dev
);
921 ios
= FW_CFG_IO(dev
);
922 sysbus_add_io(sbd
, iobase
, &ios
->comb_iomem
);
926 if (s
->dma_enabled
) {
927 /* 64 bits for the address field */
930 sysbus_add_io(sbd
, dma_iobase
, &s
->dma_iomem
);
936 FWCfgState
*fw_cfg_init_io(uint32_t iobase
)
938 return fw_cfg_init_io_dma(iobase
, 0, NULL
);
941 FWCfgState
*fw_cfg_init_mem_wide(hwaddr ctl_addr
,
942 hwaddr data_addr
, uint32_t data_width
,
943 hwaddr dma_addr
, AddressSpace
*dma_as
)
948 bool dma_requested
= dma_addr
&& dma_as
;
950 dev
= qdev_create(NULL
, TYPE_FW_CFG_MEM
);
951 qdev_prop_set_uint32(dev
, "data_width", data_width
);
952 if (!dma_requested
) {
953 qdev_prop_set_bit(dev
, "dma_enabled", false);
956 object_property_add_child(OBJECT(qdev_get_machine()), TYPE_FW_CFG
,
958 qdev_init_nofail(dev
);
960 sbd
= SYS_BUS_DEVICE(dev
);
961 sysbus_mmio_map(sbd
, 0, ctl_addr
);
962 sysbus_mmio_map(sbd
, 1, data_addr
);
966 if (s
->dma_enabled
) {
969 sysbus_mmio_map(sbd
, 2, dma_addr
);
975 FWCfgState
*fw_cfg_init_mem(hwaddr ctl_addr
, hwaddr data_addr
)
977 return fw_cfg_init_mem_wide(ctl_addr
, data_addr
,
978 fw_cfg_data_mem_ops
.valid
.max_access_size
,
983 FWCfgState
*fw_cfg_find(void)
985 /* Returns NULL unless there is exactly one fw_cfg device */
986 return FW_CFG(object_resolve_path_type("", TYPE_FW_CFG
, NULL
));
990 static void fw_cfg_class_init(ObjectClass
*klass
, void *data
)
992 DeviceClass
*dc
= DEVICE_CLASS(klass
);
994 dc
->reset
= fw_cfg_reset
;
995 dc
->vmsd
= &vmstate_fw_cfg
;
998 static const TypeInfo fw_cfg_info
= {
1000 .parent
= TYPE_SYS_BUS_DEVICE
,
1002 .instance_size
= sizeof(FWCfgState
),
1003 .class_init
= fw_cfg_class_init
,
1006 static void fw_cfg_file_slots_allocate(FWCfgState
*s
, Error
**errp
)
1008 uint16_t file_slots_max
;
1010 if (fw_cfg_file_slots(s
) < FW_CFG_FILE_SLOTS_MIN
) {
1011 error_setg(errp
, "\"file_slots\" must be at least 0x%x",
1012 FW_CFG_FILE_SLOTS_MIN
);
1016 /* (UINT16_MAX & FW_CFG_ENTRY_MASK) is the highest inclusive selector value
1017 * that we permit. The actual (exclusive) value coming from the
1018 * configuration is (FW_CFG_FILE_FIRST + fw_cfg_file_slots(s)). */
1019 file_slots_max
= (UINT16_MAX
& FW_CFG_ENTRY_MASK
) - FW_CFG_FILE_FIRST
+ 1;
1020 if (fw_cfg_file_slots(s
) > file_slots_max
) {
1021 error_setg(errp
, "\"file_slots\" must not exceed 0x%" PRIx16
,
1026 s
->entries
[0] = g_new0(FWCfgEntry
, fw_cfg_max_entry(s
));
1027 s
->entries
[1] = g_new0(FWCfgEntry
, fw_cfg_max_entry(s
));
1028 s
->entry_order
= g_new0(int, fw_cfg_max_entry(s
));
1031 static Property fw_cfg_io_properties
[] = {
1032 DEFINE_PROP_BOOL("dma_enabled", FWCfgIoState
, parent_obj
.dma_enabled
,
1034 DEFINE_PROP_UINT16("x-file-slots", FWCfgIoState
, parent_obj
.file_slots
,
1035 FW_CFG_FILE_SLOTS_DFLT
),
1036 DEFINE_PROP_END_OF_LIST(),
1039 static void fw_cfg_io_realize(DeviceState
*dev
, Error
**errp
)
1041 FWCfgIoState
*s
= FW_CFG_IO(dev
);
1042 Error
*local_err
= NULL
;
1044 fw_cfg_file_slots_allocate(FW_CFG(s
), &local_err
);
1046 error_propagate(errp
, local_err
);
1050 /* when using port i/o, the 8-bit data register ALWAYS overlaps
1051 * with half of the 16-bit control register. Hence, the total size
1052 * of the i/o region used is FW_CFG_CTL_SIZE */
1053 memory_region_init_io(&s
->comb_iomem
, OBJECT(s
), &fw_cfg_comb_mem_ops
,
1054 FW_CFG(s
), "fwcfg", FW_CFG_CTL_SIZE
);
1056 if (FW_CFG(s
)->dma_enabled
) {
1057 memory_region_init_io(&FW_CFG(s
)->dma_iomem
, OBJECT(s
),
1058 &fw_cfg_dma_mem_ops
, FW_CFG(s
), "fwcfg.dma",
1059 sizeof(dma_addr_t
));
1062 fw_cfg_common_realize(dev
, errp
);
1065 static void fw_cfg_io_class_init(ObjectClass
*klass
, void *data
)
1067 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1069 dc
->realize
= fw_cfg_io_realize
;
1070 dc
->props
= fw_cfg_io_properties
;
1073 static const TypeInfo fw_cfg_io_info
= {
1074 .name
= TYPE_FW_CFG_IO
,
1075 .parent
= TYPE_FW_CFG
,
1076 .instance_size
= sizeof(FWCfgIoState
),
1077 .class_init
= fw_cfg_io_class_init
,
1081 static Property fw_cfg_mem_properties
[] = {
1082 DEFINE_PROP_UINT32("data_width", FWCfgMemState
, data_width
, -1),
1083 DEFINE_PROP_BOOL("dma_enabled", FWCfgMemState
, parent_obj
.dma_enabled
,
1085 DEFINE_PROP_UINT16("x-file-slots", FWCfgMemState
, parent_obj
.file_slots
,
1086 FW_CFG_FILE_SLOTS_DFLT
),
1087 DEFINE_PROP_END_OF_LIST(),
1090 static void fw_cfg_mem_realize(DeviceState
*dev
, Error
**errp
)
1092 FWCfgMemState
*s
= FW_CFG_MEM(dev
);
1093 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
1094 const MemoryRegionOps
*data_ops
= &fw_cfg_data_mem_ops
;
1095 Error
*local_err
= NULL
;
1097 fw_cfg_file_slots_allocate(FW_CFG(s
), &local_err
);
1099 error_propagate(errp
, local_err
);
1103 memory_region_init_io(&s
->ctl_iomem
, OBJECT(s
), &fw_cfg_ctl_mem_ops
,
1104 FW_CFG(s
), "fwcfg.ctl", FW_CFG_CTL_SIZE
);
1105 sysbus_init_mmio(sbd
, &s
->ctl_iomem
);
1107 if (s
->data_width
> data_ops
->valid
.max_access_size
) {
1108 s
->wide_data_ops
= *data_ops
;
1110 s
->wide_data_ops
.valid
.max_access_size
= s
->data_width
;
1111 s
->wide_data_ops
.impl
.max_access_size
= s
->data_width
;
1112 data_ops
= &s
->wide_data_ops
;
1114 memory_region_init_io(&s
->data_iomem
, OBJECT(s
), data_ops
, FW_CFG(s
),
1115 "fwcfg.data", data_ops
->valid
.max_access_size
);
1116 sysbus_init_mmio(sbd
, &s
->data_iomem
);
1118 if (FW_CFG(s
)->dma_enabled
) {
1119 memory_region_init_io(&FW_CFG(s
)->dma_iomem
, OBJECT(s
),
1120 &fw_cfg_dma_mem_ops
, FW_CFG(s
), "fwcfg.dma",
1121 sizeof(dma_addr_t
));
1122 sysbus_init_mmio(sbd
, &FW_CFG(s
)->dma_iomem
);
1125 fw_cfg_common_realize(dev
, errp
);
1128 static void fw_cfg_mem_class_init(ObjectClass
*klass
, void *data
)
1130 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1132 dc
->realize
= fw_cfg_mem_realize
;
1133 dc
->props
= fw_cfg_mem_properties
;
1136 static const TypeInfo fw_cfg_mem_info
= {
1137 .name
= TYPE_FW_CFG_MEM
,
1138 .parent
= TYPE_FW_CFG
,
1139 .instance_size
= sizeof(FWCfgMemState
),
1140 .class_init
= fw_cfg_mem_class_init
,
1144 static void fw_cfg_register_types(void)
1146 type_register_static(&fw_cfg_info
);
1147 type_register_static(&fw_cfg_io_info
);
1148 type_register_static(&fw_cfg_mem_info
);
1151 type_init(fw_cfg_register_types
)