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"
26 #include "qemu-common.h"
27 #include "qemu/datadir.h"
28 #include "sysemu/sysemu.h"
29 #include "sysemu/dma.h"
30 #include "sysemu/reset.h"
31 #include "hw/boards.h"
32 #include "hw/nvram/fw_cfg.h"
33 #include "hw/qdev-properties.h"
34 #include "hw/sysbus.h"
35 #include "migration/qemu-file-types.h"
36 #include "migration/vmstate.h"
38 #include "qemu/error-report.h"
39 #include "qemu/option.h"
40 #include "qemu/config-file.h"
41 #include "qemu/cutils.h"
42 #include "qapi/error.h"
43 #include "hw/acpi/aml-build.h"
44 #include "hw/pci/pci_bus.h"
46 #define FW_CFG_FILE_SLOTS_DFLT 0x20
48 /* FW_CFG_VERSION bits */
49 #define FW_CFG_VERSION 0x01
50 #define FW_CFG_VERSION_DMA 0x02
52 /* FW_CFG_DMA_CONTROL bits */
53 #define FW_CFG_DMA_CTL_ERROR 0x01
54 #define FW_CFG_DMA_CTL_READ 0x02
55 #define FW_CFG_DMA_CTL_SKIP 0x04
56 #define FW_CFG_DMA_CTL_SELECT 0x08
57 #define FW_CFG_DMA_CTL_WRITE 0x10
59 #define FW_CFG_DMA_SIGNATURE 0x51454d5520434647ULL /* "QEMU CFG" */
65 void *callback_opaque
;
66 FWCfgCallback select_cb
;
67 FWCfgWriteCallback write_cb
;
73 * @key: The uint16 selector key.
75 * Returns: The stringified name if the selector refers to a well-known
76 * numerically defined item, or NULL on key lookup failure.
78 static const char *key_name(uint16_t key
)
80 static const char *fw_cfg_wellknown_keys
[FW_CFG_FILE_FIRST
] = {
81 [FW_CFG_SIGNATURE
] = "signature",
83 [FW_CFG_UUID
] = "uuid",
84 [FW_CFG_RAM_SIZE
] = "ram_size",
85 [FW_CFG_NOGRAPHIC
] = "nographic",
86 [FW_CFG_NB_CPUS
] = "nb_cpus",
87 [FW_CFG_MACHINE_ID
] = "machine_id",
88 [FW_CFG_KERNEL_ADDR
] = "kernel_addr",
89 [FW_CFG_KERNEL_SIZE
] = "kernel_size",
90 [FW_CFG_KERNEL_CMDLINE
] = "kernel_cmdline",
91 [FW_CFG_INITRD_ADDR
] = "initrd_addr",
92 [FW_CFG_INITRD_SIZE
] = "initdr_size",
93 [FW_CFG_BOOT_DEVICE
] = "boot_device",
94 [FW_CFG_NUMA
] = "numa",
95 [FW_CFG_BOOT_MENU
] = "boot_menu",
96 [FW_CFG_MAX_CPUS
] = "max_cpus",
97 [FW_CFG_KERNEL_ENTRY
] = "kernel_entry",
98 [FW_CFG_KERNEL_DATA
] = "kernel_data",
99 [FW_CFG_INITRD_DATA
] = "initrd_data",
100 [FW_CFG_CMDLINE_ADDR
] = "cmdline_addr",
101 [FW_CFG_CMDLINE_SIZE
] = "cmdline_size",
102 [FW_CFG_CMDLINE_DATA
] = "cmdline_data",
103 [FW_CFG_SETUP_ADDR
] = "setup_addr",
104 [FW_CFG_SETUP_SIZE
] = "setup_size",
105 [FW_CFG_SETUP_DATA
] = "setup_data",
106 [FW_CFG_FILE_DIR
] = "file_dir",
109 if (key
& FW_CFG_ARCH_LOCAL
) {
110 return fw_cfg_arch_key_name(key
);
112 if (key
< FW_CFG_FILE_FIRST
) {
113 return fw_cfg_wellknown_keys
[key
];
119 static inline const char *trace_key_name(uint16_t key
)
121 const char *name
= key_name(key
);
123 return name
? name
: "unknown";
129 static char *read_splashfile(char *filename
, gsize
*file_sizep
,
135 unsigned int filehead
;
138 if (!g_file_get_contents(filename
, &content
, file_sizep
, &err
)) {
139 error_report("failed to read splash file '%s': %s",
140 filename
, err
->message
);
145 /* check file size */
146 if (*file_sizep
< 30) {
151 filehead
= lduw_le_p(content
);
152 if (filehead
== 0xd8ff) {
153 file_type
= JPG_FILE
;
154 } else if (filehead
== 0x4d42) {
155 file_type
= BMP_FILE
;
161 if (file_type
== BMP_FILE
) {
162 bmp_bpp
= lduw_le_p(&content
[28]);
169 *file_typep
= file_type
;
174 error_report("splash file '%s' format not recognized; must be JPEG "
175 "or 24 bit BMP", filename
);
180 static void fw_cfg_bootsplash(FWCfgState
*s
)
182 const char *boot_splash_filename
= NULL
;
183 const char *boot_splash_time
= NULL
;
184 char *filename
, *file_data
;
188 /* get user configuration */
189 QemuOptsList
*plist
= qemu_find_opts("boot-opts");
190 QemuOpts
*opts
= QTAILQ_FIRST(&plist
->head
);
191 boot_splash_filename
= qemu_opt_get(opts
, "splash");
192 boot_splash_time
= qemu_opt_get(opts
, "splash-time");
194 /* insert splash time if user configurated */
195 if (boot_splash_time
) {
196 int64_t bst_val
= qemu_opt_get_number(opts
, "splash-time", -1);
199 /* validate the input */
200 if (bst_val
< 0 || bst_val
> 0xffff) {
201 error_report("splash-time is invalid,"
202 "it should be a value between 0 and 65535");
205 /* use little endian format */
206 bst_le16
= cpu_to_le16(bst_val
);
207 fw_cfg_add_file(s
, "etc/boot-menu-wait",
208 g_memdup(&bst_le16
, sizeof bst_le16
), sizeof bst_le16
);
211 /* insert splash file if user configurated */
212 if (boot_splash_filename
) {
213 filename
= qemu_find_file(QEMU_FILE_TYPE_BIOS
, boot_splash_filename
);
214 if (filename
== NULL
) {
215 error_report("failed to find file '%s'", boot_splash_filename
);
219 /* loading file data */
220 file_data
= read_splashfile(filename
, &file_size
, &file_type
);
221 if (file_data
== NULL
) {
225 g_free(boot_splash_filedata
);
226 boot_splash_filedata
= (uint8_t *)file_data
;
229 if (file_type
== JPG_FILE
) {
230 fw_cfg_add_file(s
, "bootsplash.jpg",
231 boot_splash_filedata
, file_size
);
233 fw_cfg_add_file(s
, "bootsplash.bmp",
234 boot_splash_filedata
, file_size
);
240 static void fw_cfg_reboot(FWCfgState
*s
)
242 const char *reboot_timeout
= NULL
;
243 uint64_t rt_val
= -1;
246 /* get user configuration */
247 QemuOptsList
*plist
= qemu_find_opts("boot-opts");
248 QemuOpts
*opts
= QTAILQ_FIRST(&plist
->head
);
249 reboot_timeout
= qemu_opt_get(opts
, "reboot-timeout");
251 if (reboot_timeout
) {
252 rt_val
= qemu_opt_get_number(opts
, "reboot-timeout", -1);
254 /* validate the input */
255 if (rt_val
> 0xffff && rt_val
!= (uint64_t)-1) {
256 error_report("reboot timeout is invalid,"
257 "it should be a value between -1 and 65535");
262 rt_le32
= cpu_to_le32(rt_val
);
263 fw_cfg_add_file(s
, "etc/boot-fail-wait", g_memdup(&rt_le32
, 4), 4);
266 static void fw_cfg_write(FWCfgState
*s
, uint8_t value
)
268 /* nothing, write support removed in QEMU v2.4+ */
271 static inline uint16_t fw_cfg_file_slots(const FWCfgState
*s
)
273 return s
->file_slots
;
276 /* Note: this function returns an exclusive limit. */
277 static inline uint32_t fw_cfg_max_entry(const FWCfgState
*s
)
279 return FW_CFG_FILE_FIRST
+ fw_cfg_file_slots(s
);
282 static int fw_cfg_select(FWCfgState
*s
, uint16_t key
)
288 if ((key
& FW_CFG_ENTRY_MASK
) >= fw_cfg_max_entry(s
)) {
289 s
->cur_entry
= FW_CFG_INVALID
;
294 /* entry successfully selected, now run callback if present */
295 arch
= !!(key
& FW_CFG_ARCH_LOCAL
);
296 e
= &s
->entries
[arch
][key
& FW_CFG_ENTRY_MASK
];
298 e
->select_cb(e
->callback_opaque
);
302 trace_fw_cfg_select(s
, key
, trace_key_name(key
), ret
);
306 static uint64_t fw_cfg_data_read(void *opaque
, hwaddr addr
, unsigned size
)
308 FWCfgState
*s
= opaque
;
309 int arch
= !!(s
->cur_entry
& FW_CFG_ARCH_LOCAL
);
310 FWCfgEntry
*e
= (s
->cur_entry
== FW_CFG_INVALID
) ? NULL
:
311 &s
->entries
[arch
][s
->cur_entry
& FW_CFG_ENTRY_MASK
];
314 assert(size
> 0 && size
<= sizeof(value
));
315 if (s
->cur_entry
!= FW_CFG_INVALID
&& e
->data
&& s
->cur_offset
< e
->len
) {
316 /* The least significant 'size' bytes of the return value are
317 * expected to contain a string preserving portion of the item
318 * data, padded with zeros on the right in case we run out early.
319 * In technical terms, we're composing the host-endian representation
320 * of the big endian interpretation of the fw_cfg string.
323 value
= (value
<< 8) | e
->data
[s
->cur_offset
++];
324 } while (--size
&& s
->cur_offset
< e
->len
);
325 /* If size is still not zero, we *did* run out early, so continue
326 * left-shifting, to add the appropriate number of padding zeros
332 trace_fw_cfg_read(s
, value
);
336 static void fw_cfg_data_mem_write(void *opaque
, hwaddr addr
,
337 uint64_t value
, unsigned size
)
339 FWCfgState
*s
= opaque
;
343 fw_cfg_write(s
, value
>> (8 * --i
));
347 static void fw_cfg_dma_transfer(FWCfgState
*s
)
353 int read
= 0, write
= 0;
356 /* Reset the address before the next access */
357 dma_addr
= s
->dma_addr
;
360 if (dma_memory_read(s
->dma_as
, dma_addr
, &dma
, sizeof(dma
))) {
361 stl_be_dma(s
->dma_as
, dma_addr
+ offsetof(FWCfgDmaAccess
, control
),
362 FW_CFG_DMA_CTL_ERROR
);
366 dma
.address
= be64_to_cpu(dma
.address
);
367 dma
.length
= be32_to_cpu(dma
.length
);
368 dma
.control
= be32_to_cpu(dma
.control
);
370 if (dma
.control
& FW_CFG_DMA_CTL_SELECT
) {
371 fw_cfg_select(s
, dma
.control
>> 16);
374 arch
= !!(s
->cur_entry
& FW_CFG_ARCH_LOCAL
);
375 e
= (s
->cur_entry
== FW_CFG_INVALID
) ? NULL
:
376 &s
->entries
[arch
][s
->cur_entry
& FW_CFG_ENTRY_MASK
];
378 if (dma
.control
& FW_CFG_DMA_CTL_READ
) {
381 } else if (dma
.control
& FW_CFG_DMA_CTL_WRITE
) {
384 } else if (dma
.control
& FW_CFG_DMA_CTL_SKIP
) {
393 while (dma
.length
> 0 && !(dma
.control
& FW_CFG_DMA_CTL_ERROR
)) {
394 if (s
->cur_entry
== FW_CFG_INVALID
|| !e
->data
||
395 s
->cur_offset
>= e
->len
) {
398 /* If the access is not a read access, it will be a skip access,
402 if (dma_memory_set(s
->dma_as
, dma
.address
, 0, len
)) {
403 dma
.control
|= FW_CFG_DMA_CTL_ERROR
;
407 dma
.control
|= FW_CFG_DMA_CTL_ERROR
;
410 if (dma
.length
<= (e
->len
- s
->cur_offset
)) {
413 len
= (e
->len
- s
->cur_offset
);
416 /* If the access is not a read access, it will be a skip access,
420 if (dma_memory_write(s
->dma_as
, dma
.address
,
421 &e
->data
[s
->cur_offset
], len
)) {
422 dma
.control
|= FW_CFG_DMA_CTL_ERROR
;
426 if (!e
->allow_write
||
428 dma_memory_read(s
->dma_as
, dma
.address
,
429 &e
->data
[s
->cur_offset
], len
)) {
430 dma
.control
|= FW_CFG_DMA_CTL_ERROR
;
431 } else if (e
->write_cb
) {
432 e
->write_cb(e
->callback_opaque
, s
->cur_offset
, len
);
436 s
->cur_offset
+= len
;
444 stl_be_dma(s
->dma_as
, dma_addr
+ offsetof(FWCfgDmaAccess
, control
),
447 trace_fw_cfg_read(s
, 0);
450 static uint64_t fw_cfg_dma_mem_read(void *opaque
, hwaddr addr
,
453 /* Return a signature value (and handle various read sizes) */
454 return extract64(FW_CFG_DMA_SIGNATURE
, (8 - addr
- size
) * 8, size
* 8);
457 static void fw_cfg_dma_mem_write(void *opaque
, hwaddr addr
,
458 uint64_t value
, unsigned size
)
460 FWCfgState
*s
= opaque
;
464 /* FWCfgDmaAccess high address */
465 s
->dma_addr
= value
<< 32;
466 } else if (addr
== 4) {
467 /* FWCfgDmaAccess low address */
468 s
->dma_addr
|= value
;
469 fw_cfg_dma_transfer(s
);
471 } else if (size
== 8 && addr
== 0) {
473 fw_cfg_dma_transfer(s
);
477 static bool fw_cfg_dma_mem_valid(void *opaque
, hwaddr addr
,
478 unsigned size
, bool is_write
,
481 return !is_write
|| ((size
== 4 && (addr
== 0 || addr
== 4)) ||
482 (size
== 8 && addr
== 0));
485 static bool fw_cfg_data_mem_valid(void *opaque
, hwaddr addr
,
486 unsigned size
, bool is_write
,
492 static uint64_t fw_cfg_ctl_mem_read(void *opaque
, hwaddr addr
, unsigned size
)
497 static void fw_cfg_ctl_mem_write(void *opaque
, hwaddr addr
,
498 uint64_t value
, unsigned size
)
500 fw_cfg_select(opaque
, (uint16_t)value
);
503 static bool fw_cfg_ctl_mem_valid(void *opaque
, hwaddr addr
,
504 unsigned size
, bool is_write
,
507 return is_write
&& size
== 2;
510 static void fw_cfg_comb_write(void *opaque
, hwaddr addr
,
511 uint64_t value
, unsigned size
)
515 fw_cfg_write(opaque
, (uint8_t)value
);
518 fw_cfg_select(opaque
, (uint16_t)value
);
523 static bool fw_cfg_comb_valid(void *opaque
, hwaddr addr
,
524 unsigned size
, bool is_write
,
527 return (size
== 1) || (is_write
&& size
== 2);
530 static const MemoryRegionOps fw_cfg_ctl_mem_ops
= {
531 .read
= fw_cfg_ctl_mem_read
,
532 .write
= fw_cfg_ctl_mem_write
,
533 .endianness
= DEVICE_BIG_ENDIAN
,
534 .valid
.accepts
= fw_cfg_ctl_mem_valid
,
537 static const MemoryRegionOps fw_cfg_data_mem_ops
= {
538 .read
= fw_cfg_data_read
,
539 .write
= fw_cfg_data_mem_write
,
540 .endianness
= DEVICE_BIG_ENDIAN
,
542 .min_access_size
= 1,
543 .max_access_size
= 1,
544 .accepts
= fw_cfg_data_mem_valid
,
548 static const MemoryRegionOps fw_cfg_comb_mem_ops
= {
549 .read
= fw_cfg_data_read
,
550 .write
= fw_cfg_comb_write
,
551 .endianness
= DEVICE_LITTLE_ENDIAN
,
552 .valid
.accepts
= fw_cfg_comb_valid
,
555 static const MemoryRegionOps fw_cfg_dma_mem_ops
= {
556 .read
= fw_cfg_dma_mem_read
,
557 .write
= fw_cfg_dma_mem_write
,
558 .endianness
= DEVICE_BIG_ENDIAN
,
559 .valid
.accepts
= fw_cfg_dma_mem_valid
,
560 .valid
.max_access_size
= 8,
561 .impl
.max_access_size
= 8,
564 static void fw_cfg_reset(DeviceState
*d
)
566 FWCfgState
*s
= FW_CFG(d
);
568 /* we never register a read callback for FW_CFG_SIGNATURE */
569 fw_cfg_select(s
, FW_CFG_SIGNATURE
);
572 /* Save restore 32 bit int as uint16_t
573 This is a Big hack, but it is how the old state did it.
574 Or we broke compatibility in the state, or we can't use struct tm
577 static int get_uint32_as_uint16(QEMUFile
*f
, void *pv
, size_t size
,
578 const VMStateField
*field
)
581 *v
= qemu_get_be16(f
);
585 static int put_unused(QEMUFile
*f
, void *pv
, size_t size
,
586 const VMStateField
*field
, JSONWriter
*vmdesc
)
588 fprintf(stderr
, "uint32_as_uint16 is only used for backward compatibility.\n");
589 fprintf(stderr
, "This functions shouldn't be called.\n");
594 static const VMStateInfo vmstate_hack_uint32_as_uint16
= {
595 .name
= "int32_as_uint16",
596 .get
= get_uint32_as_uint16
,
600 #define VMSTATE_UINT16_HACK(_f, _s, _t) \
601 VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_hack_uint32_as_uint16, uint32_t)
604 static bool is_version_1(void *opaque
, int version_id
)
606 return version_id
== 1;
609 bool fw_cfg_dma_enabled(void *opaque
)
611 FWCfgState
*s
= opaque
;
613 return s
->dma_enabled
;
616 static bool fw_cfg_acpi_mr_restore(void *opaque
)
618 FWCfgState
*s
= opaque
;
621 mr_aligned
= QEMU_IS_ALIGNED(s
->table_mr_size
, qemu_real_host_page_size
) &&
622 QEMU_IS_ALIGNED(s
->linker_mr_size
, qemu_real_host_page_size
) &&
623 QEMU_IS_ALIGNED(s
->rsdp_mr_size
, qemu_real_host_page_size
);
624 return s
->acpi_mr_restore
&& !mr_aligned
;
627 static void fw_cfg_update_mr(FWCfgState
*s
, uint16_t key
, size_t size
)
631 int arch
= !!(key
& FW_CFG_ARCH_LOCAL
);
634 key
&= FW_CFG_ENTRY_MASK
;
635 assert(key
< fw_cfg_max_entry(s
));
637 ptr
= s
->entries
[arch
][key
].data
;
638 mr
= memory_region_from_host(ptr
, &offset
);
640 memory_region_ram_resize(mr
, size
, &error_abort
);
643 static int fw_cfg_acpi_mr_restore_post_load(void *opaque
, int version_id
)
645 FWCfgState
*s
= opaque
;
650 index
= be32_to_cpu(s
->files
->count
);
652 for (i
= 0; i
< index
; i
++) {
653 if (!strcmp(s
->files
->f
[i
].name
, ACPI_BUILD_TABLE_FILE
)) {
654 fw_cfg_update_mr(s
, FW_CFG_FILE_FIRST
+ i
, s
->table_mr_size
);
655 } else if (!strcmp(s
->files
->f
[i
].name
, ACPI_BUILD_LOADER_FILE
)) {
656 fw_cfg_update_mr(s
, FW_CFG_FILE_FIRST
+ i
, s
->linker_mr_size
);
657 } else if (!strcmp(s
->files
->f
[i
].name
, ACPI_BUILD_RSDP_FILE
)) {
658 fw_cfg_update_mr(s
, FW_CFG_FILE_FIRST
+ i
, s
->rsdp_mr_size
);
665 static const VMStateDescription vmstate_fw_cfg_dma
= {
666 .name
= "fw_cfg/dma",
667 .needed
= fw_cfg_dma_enabled
,
668 .fields
= (VMStateField
[]) {
669 VMSTATE_UINT64(dma_addr
, FWCfgState
),
670 VMSTATE_END_OF_LIST()
674 static const VMStateDescription vmstate_fw_cfg_acpi_mr
= {
675 .name
= "fw_cfg/acpi_mr",
677 .minimum_version_id
= 1,
678 .needed
= fw_cfg_acpi_mr_restore
,
679 .post_load
= fw_cfg_acpi_mr_restore_post_load
,
680 .fields
= (VMStateField
[]) {
681 VMSTATE_UINT64(table_mr_size
, FWCfgState
),
682 VMSTATE_UINT64(linker_mr_size
, FWCfgState
),
683 VMSTATE_UINT64(rsdp_mr_size
, FWCfgState
),
684 VMSTATE_END_OF_LIST()
688 static const VMStateDescription vmstate_fw_cfg
= {
691 .minimum_version_id
= 1,
692 .fields
= (VMStateField
[]) {
693 VMSTATE_UINT16(cur_entry
, FWCfgState
),
694 VMSTATE_UINT16_HACK(cur_offset
, FWCfgState
, is_version_1
),
695 VMSTATE_UINT32_V(cur_offset
, FWCfgState
, 2),
696 VMSTATE_END_OF_LIST()
698 .subsections
= (const VMStateDescription
*[]) {
700 &vmstate_fw_cfg_acpi_mr
,
705 static void fw_cfg_add_bytes_callback(FWCfgState
*s
, uint16_t key
,
706 FWCfgCallback select_cb
,
707 FWCfgWriteCallback write_cb
,
708 void *callback_opaque
,
709 void *data
, size_t len
,
712 int arch
= !!(key
& FW_CFG_ARCH_LOCAL
);
714 key
&= FW_CFG_ENTRY_MASK
;
716 assert(key
< fw_cfg_max_entry(s
) && len
< UINT32_MAX
);
717 assert(s
->entries
[arch
][key
].data
== NULL
); /* avoid key conflict */
719 s
->entries
[arch
][key
].data
= data
;
720 s
->entries
[arch
][key
].len
= (uint32_t)len
;
721 s
->entries
[arch
][key
].select_cb
= select_cb
;
722 s
->entries
[arch
][key
].write_cb
= write_cb
;
723 s
->entries
[arch
][key
].callback_opaque
= callback_opaque
;
724 s
->entries
[arch
][key
].allow_write
= !read_only
;
727 static void *fw_cfg_modify_bytes_read(FWCfgState
*s
, uint16_t key
,
728 void *data
, size_t len
)
731 int arch
= !!(key
& FW_CFG_ARCH_LOCAL
);
733 key
&= FW_CFG_ENTRY_MASK
;
735 assert(key
< fw_cfg_max_entry(s
) && len
< UINT32_MAX
);
737 /* return the old data to the function caller, avoid memory leak */
738 ptr
= s
->entries
[arch
][key
].data
;
739 s
->entries
[arch
][key
].data
= data
;
740 s
->entries
[arch
][key
].len
= len
;
741 s
->entries
[arch
][key
].callback_opaque
= NULL
;
742 s
->entries
[arch
][key
].allow_write
= false;
747 void fw_cfg_add_bytes(FWCfgState
*s
, uint16_t key
, void *data
, size_t len
)
749 trace_fw_cfg_add_bytes(key
, trace_key_name(key
), len
);
750 fw_cfg_add_bytes_callback(s
, key
, NULL
, NULL
, NULL
, data
, len
, true);
753 void fw_cfg_add_string(FWCfgState
*s
, uint16_t key
, const char *value
)
755 size_t sz
= strlen(value
) + 1;
757 trace_fw_cfg_add_string(key
, trace_key_name(key
), value
);
758 fw_cfg_add_bytes(s
, key
, g_memdup(value
, sz
), sz
);
761 void fw_cfg_modify_string(FWCfgState
*s
, uint16_t key
, const char *value
)
763 size_t sz
= strlen(value
) + 1;
766 old
= fw_cfg_modify_bytes_read(s
, key
, g_memdup(value
, sz
), sz
);
770 void fw_cfg_add_i16(FWCfgState
*s
, uint16_t key
, uint16_t value
)
774 copy
= g_malloc(sizeof(value
));
775 *copy
= cpu_to_le16(value
);
776 trace_fw_cfg_add_i16(key
, trace_key_name(key
), value
);
777 fw_cfg_add_bytes(s
, key
, copy
, sizeof(value
));
780 void fw_cfg_modify_i16(FWCfgState
*s
, uint16_t key
, uint16_t value
)
782 uint16_t *copy
, *old
;
784 copy
= g_malloc(sizeof(value
));
785 *copy
= cpu_to_le16(value
);
786 old
= fw_cfg_modify_bytes_read(s
, key
, copy
, sizeof(value
));
790 void fw_cfg_add_i32(FWCfgState
*s
, uint16_t key
, uint32_t value
)
794 copy
= g_malloc(sizeof(value
));
795 *copy
= cpu_to_le32(value
);
796 trace_fw_cfg_add_i32(key
, trace_key_name(key
), value
);
797 fw_cfg_add_bytes(s
, key
, copy
, sizeof(value
));
800 void fw_cfg_modify_i32(FWCfgState
*s
, uint16_t key
, uint32_t value
)
802 uint32_t *copy
, *old
;
804 copy
= g_malloc(sizeof(value
));
805 *copy
= cpu_to_le32(value
);
806 old
= fw_cfg_modify_bytes_read(s
, key
, copy
, sizeof(value
));
810 void fw_cfg_add_i64(FWCfgState
*s
, uint16_t key
, uint64_t value
)
814 copy
= g_malloc(sizeof(value
));
815 *copy
= cpu_to_le64(value
);
816 trace_fw_cfg_add_i64(key
, trace_key_name(key
), value
);
817 fw_cfg_add_bytes(s
, key
, copy
, sizeof(value
));
820 void fw_cfg_modify_i64(FWCfgState
*s
, uint16_t key
, uint64_t value
)
822 uint64_t *copy
, *old
;
824 copy
= g_malloc(sizeof(value
));
825 *copy
= cpu_to_le64(value
);
826 old
= fw_cfg_modify_bytes_read(s
, key
, copy
, sizeof(value
));
830 void fw_cfg_set_order_override(FWCfgState
*s
, int order
)
832 assert(s
->fw_cfg_order_override
== 0);
833 s
->fw_cfg_order_override
= order
;
836 void fw_cfg_reset_order_override(FWCfgState
*s
)
838 assert(s
->fw_cfg_order_override
!= 0);
839 s
->fw_cfg_order_override
= 0;
843 * This is the legacy order list. For legacy systems, files are in
844 * the fw_cfg in the order defined below, by the "order" value. Note
845 * that some entries (VGA ROMs, NIC option ROMS, etc.) go into a
846 * specific area, but there may be more than one and they occur in the
847 * order that the user specifies them on the command line. Those are
848 * handled in a special manner, using the order override above.
850 * For non-legacy, the files are sorted by filename to avoid this kind
851 * of complexity in the future.
853 * This is only for x86, other arches don't implement versioning so
854 * they won't set legacy mode.
860 { "etc/boot-menu-wait", 10 },
861 { "bootsplash.jpg", 11 },
862 { "bootsplash.bmp", 12 },
863 { "etc/boot-fail-wait", 15 },
864 { "etc/smbios/smbios-tables", 20 },
865 { "etc/smbios/smbios-anchor", 30 },
867 { "etc/reserved-memory-end", 50 },
868 { "genroms/kvmvapic.bin", 55 },
869 { "genroms/linuxboot.bin", 60 },
870 { }, /* VGA ROMs from pc_vga_init come here, 70. */
871 { }, /* NIC option ROMs from pc_nic_init come here, 80. */
872 { "etc/system-states", 90 },
873 { }, /* User ROMs come here, 100. */
874 { }, /* Device FW comes here, 110. */
875 { "etc/extra-pci-roots", 120 },
876 { "etc/acpi/tables", 130 },
877 { "etc/table-loader", 140 },
878 { "etc/tpm/log", 150 },
879 { "etc/acpi/rsdp", 160 },
880 { "bootorder", 170 },
882 #define FW_CFG_ORDER_OVERRIDE_LAST 200
886 * Any sub-page size update to these table MRs will be lost during migration,
887 * as we use aligned size in ram_load_precopy() -> qemu_ram_resize() path.
888 * In order to avoid the inconsistency in sizes save them seperately and
889 * migrate over in vmstate post_load().
891 static void fw_cfg_acpi_mr_save(FWCfgState
*s
, const char *filename
, size_t len
)
893 if (!strcmp(filename
, ACPI_BUILD_TABLE_FILE
)) {
894 s
->table_mr_size
= len
;
895 } else if (!strcmp(filename
, ACPI_BUILD_LOADER_FILE
)) {
896 s
->linker_mr_size
= len
;
897 } else if (!strcmp(filename
, ACPI_BUILD_RSDP_FILE
)) {
898 s
->rsdp_mr_size
= len
;
902 static int get_fw_cfg_order(FWCfgState
*s
, const char *name
)
906 if (s
->fw_cfg_order_override
> 0) {
907 return s
->fw_cfg_order_override
;
910 for (i
= 0; i
< ARRAY_SIZE(fw_cfg_order
); i
++) {
911 if (fw_cfg_order
[i
].name
== NULL
) {
915 if (strcmp(name
, fw_cfg_order
[i
].name
) == 0) {
916 return fw_cfg_order
[i
].order
;
920 /* Stick unknown stuff at the end. */
921 warn_report("Unknown firmware file in legacy mode: %s", name
);
922 return FW_CFG_ORDER_OVERRIDE_LAST
;
925 void fw_cfg_add_file_callback(FWCfgState
*s
, const char *filename
,
926 FWCfgCallback select_cb
,
927 FWCfgWriteCallback write_cb
,
928 void *callback_opaque
,
929 void *data
, size_t len
, bool read_only
)
933 MachineClass
*mc
= MACHINE_GET_CLASS(qdev_get_machine());
937 dsize
= sizeof(uint32_t) + sizeof(FWCfgFile
) * fw_cfg_file_slots(s
);
938 s
->files
= g_malloc0(dsize
);
939 fw_cfg_add_bytes(s
, FW_CFG_FILE_DIR
, s
->files
, dsize
);
942 count
= be32_to_cpu(s
->files
->count
);
943 assert(count
< fw_cfg_file_slots(s
));
945 /* Find the insertion point. */
946 if (mc
->legacy_fw_cfg_order
) {
948 * Sort by order. For files with the same order, we keep them
949 * in the sequence in which they were added.
951 order
= get_fw_cfg_order(s
, filename
);
953 index
> 0 && order
< s
->entry_order
[index
- 1];
956 /* Sort by file name. */
958 index
> 0 && strcmp(filename
, s
->files
->f
[index
- 1].name
) < 0;
963 * Move all the entries from the index point and after down one
964 * to create a slot for the new entry. Because calculations are
965 * being done with the index, make it so that "i" is the current
966 * index and "i - 1" is the one being copied from, thus the
967 * unusual start and end in the for statement.
969 for (i
= count
; i
> index
; i
--) {
970 s
->files
->f
[i
] = s
->files
->f
[i
- 1];
971 s
->files
->f
[i
].select
= cpu_to_be16(FW_CFG_FILE_FIRST
+ i
);
972 s
->entries
[0][FW_CFG_FILE_FIRST
+ i
] =
973 s
->entries
[0][FW_CFG_FILE_FIRST
+ i
- 1];
974 s
->entry_order
[i
] = s
->entry_order
[i
- 1];
977 memset(&s
->files
->f
[index
], 0, sizeof(FWCfgFile
));
978 memset(&s
->entries
[0][FW_CFG_FILE_FIRST
+ index
], 0, sizeof(FWCfgEntry
));
980 pstrcpy(s
->files
->f
[index
].name
, sizeof(s
->files
->f
[index
].name
), filename
);
981 for (i
= 0; i
<= count
; i
++) {
983 strcmp(s
->files
->f
[index
].name
, s
->files
->f
[i
].name
) == 0) {
984 error_report("duplicate fw_cfg file name: %s",
985 s
->files
->f
[index
].name
);
990 fw_cfg_add_bytes_callback(s
, FW_CFG_FILE_FIRST
+ index
,
992 callback_opaque
, data
, len
,
995 s
->files
->f
[index
].size
= cpu_to_be32(len
);
996 s
->files
->f
[index
].select
= cpu_to_be16(FW_CFG_FILE_FIRST
+ index
);
997 s
->entry_order
[index
] = order
;
998 trace_fw_cfg_add_file(s
, index
, s
->files
->f
[index
].name
, len
);
1000 s
->files
->count
= cpu_to_be32(count
+1);
1001 fw_cfg_acpi_mr_save(s
, filename
, len
);
1004 void fw_cfg_add_file(FWCfgState
*s
, const char *filename
,
1005 void *data
, size_t len
)
1007 fw_cfg_add_file_callback(s
, filename
, NULL
, NULL
, NULL
, data
, len
, true);
1010 void *fw_cfg_modify_file(FWCfgState
*s
, const char *filename
,
1011 void *data
, size_t len
)
1018 index
= be32_to_cpu(s
->files
->count
);
1020 for (i
= 0; i
< index
; i
++) {
1021 if (strcmp(filename
, s
->files
->f
[i
].name
) == 0) {
1022 ptr
= fw_cfg_modify_bytes_read(s
, FW_CFG_FILE_FIRST
+ i
,
1024 s
->files
->f
[i
].size
= cpu_to_be32(len
);
1025 fw_cfg_acpi_mr_save(s
, filename
, len
);
1030 assert(index
< fw_cfg_file_slots(s
));
1033 fw_cfg_add_file_callback(s
, filename
, NULL
, NULL
, NULL
, data
, len
, true);
1037 bool fw_cfg_add_from_generator(FWCfgState
*s
, const char *filename
,
1038 const char *gen_id
, Error
**errp
)
1040 FWCfgDataGeneratorClass
*klass
;
1045 obj
= object_resolve_path_component(object_get_objects_root(), gen_id
);
1047 error_setg(errp
, "Cannot find object ID '%s'", gen_id
);
1050 if (!object_dynamic_cast(obj
, TYPE_FW_CFG_DATA_GENERATOR_INTERFACE
)) {
1051 error_setg(errp
, "Object ID '%s' is not a '%s' subclass",
1052 gen_id
, TYPE_FW_CFG_DATA_GENERATOR_INTERFACE
);
1055 klass
= FW_CFG_DATA_GENERATOR_GET_CLASS(obj
);
1056 array
= klass
->get_data(obj
, errp
);
1061 fw_cfg_add_file(s
, filename
, g_byte_array_free(array
, FALSE
), size
);
1066 void fw_cfg_add_extra_pci_roots(PCIBus
*bus
, FWCfgState
*s
)
1068 int extra_hosts
= 0;
1074 QLIST_FOREACH(bus
, &bus
->child
, sibling
) {
1075 /* look for expander root buses */
1076 if (pci_bus_is_root(bus
)) {
1081 if (extra_hosts
&& s
) {
1082 uint64_t *val
= g_malloc(sizeof(*val
));
1083 *val
= cpu_to_le64(extra_hosts
);
1084 fw_cfg_add_file(s
, "etc/extra-pci-roots", val
, sizeof(*val
));
1088 static void fw_cfg_machine_reset(void *opaque
)
1090 MachineClass
*mc
= MACHINE_GET_CLASS(qdev_get_machine());
1091 FWCfgState
*s
= opaque
;
1096 buf
= get_boot_devices_list(&len
);
1097 ptr
= fw_cfg_modify_file(s
, "bootorder", (uint8_t *)buf
, len
);
1100 if (!mc
->legacy_fw_cfg_order
) {
1101 buf
= get_boot_devices_lchs_list(&len
);
1102 ptr
= fw_cfg_modify_file(s
, "bios-geometry", (uint8_t *)buf
, len
);
1107 static void fw_cfg_machine_ready(struct Notifier
*n
, void *data
)
1109 FWCfgState
*s
= container_of(n
, FWCfgState
, machine_ready
);
1110 qemu_register_reset(fw_cfg_machine_reset
, s
);
1113 static Property fw_cfg_properties
[] = {
1114 DEFINE_PROP_BOOL("acpi-mr-restore", FWCfgState
, acpi_mr_restore
, true),
1115 DEFINE_PROP_END_OF_LIST(),
1118 static void fw_cfg_common_realize(DeviceState
*dev
, Error
**errp
)
1120 FWCfgState
*s
= FW_CFG(dev
);
1121 MachineState
*machine
= MACHINE(qdev_get_machine());
1122 uint32_t version
= FW_CFG_VERSION
;
1124 if (!fw_cfg_find()) {
1125 error_setg(errp
, "at most one %s device is permitted", TYPE_FW_CFG
);
1129 fw_cfg_add_bytes(s
, FW_CFG_SIGNATURE
, (char *)"QEMU", 4);
1130 fw_cfg_add_bytes(s
, FW_CFG_UUID
, &qemu_uuid
, 16);
1131 fw_cfg_add_i16(s
, FW_CFG_NOGRAPHIC
, (uint16_t)!machine
->enable_graphics
);
1132 fw_cfg_add_i16(s
, FW_CFG_BOOT_MENU
, (uint16_t)boot_menu
);
1133 fw_cfg_bootsplash(s
);
1136 if (s
->dma_enabled
) {
1137 version
|= FW_CFG_VERSION_DMA
;
1140 fw_cfg_add_i32(s
, FW_CFG_ID
, version
);
1142 s
->machine_ready
.notify
= fw_cfg_machine_ready
;
1143 qemu_add_machine_init_done_notifier(&s
->machine_ready
);
1146 FWCfgState
*fw_cfg_init_io_dma(uint32_t iobase
, uint32_t dma_iobase
,
1147 AddressSpace
*dma_as
)
1153 bool dma_requested
= dma_iobase
&& dma_as
;
1155 dev
= qdev_new(TYPE_FW_CFG_IO
);
1156 if (!dma_requested
) {
1157 qdev_prop_set_bit(dev
, "dma_enabled", false);
1160 object_property_add_child(OBJECT(qdev_get_machine()), TYPE_FW_CFG
,
1163 sbd
= SYS_BUS_DEVICE(dev
);
1164 sysbus_realize_and_unref(sbd
, &error_fatal
);
1165 ios
= FW_CFG_IO(dev
);
1166 sysbus_add_io(sbd
, iobase
, &ios
->comb_iomem
);
1170 if (s
->dma_enabled
) {
1171 /* 64 bits for the address field */
1174 sysbus_add_io(sbd
, dma_iobase
, &s
->dma_iomem
);
1180 FWCfgState
*fw_cfg_init_io(uint32_t iobase
)
1182 return fw_cfg_init_io_dma(iobase
, 0, NULL
);
1185 FWCfgState
*fw_cfg_init_mem_wide(hwaddr ctl_addr
,
1186 hwaddr data_addr
, uint32_t data_width
,
1187 hwaddr dma_addr
, AddressSpace
*dma_as
)
1192 bool dma_requested
= dma_addr
&& dma_as
;
1194 dev
= qdev_new(TYPE_FW_CFG_MEM
);
1195 qdev_prop_set_uint32(dev
, "data_width", data_width
);
1196 if (!dma_requested
) {
1197 qdev_prop_set_bit(dev
, "dma_enabled", false);
1200 object_property_add_child(OBJECT(qdev_get_machine()), TYPE_FW_CFG
,
1203 sbd
= SYS_BUS_DEVICE(dev
);
1204 sysbus_realize_and_unref(sbd
, &error_fatal
);
1205 sysbus_mmio_map(sbd
, 0, ctl_addr
);
1206 sysbus_mmio_map(sbd
, 1, data_addr
);
1210 if (s
->dma_enabled
) {
1213 sysbus_mmio_map(sbd
, 2, dma_addr
);
1219 FWCfgState
*fw_cfg_init_mem(hwaddr ctl_addr
, hwaddr data_addr
)
1221 return fw_cfg_init_mem_wide(ctl_addr
, data_addr
,
1222 fw_cfg_data_mem_ops
.valid
.max_access_size
,
1227 FWCfgState
*fw_cfg_find(void)
1229 /* Returns NULL unless there is exactly one fw_cfg device */
1230 return FW_CFG(object_resolve_path_type("", TYPE_FW_CFG
, NULL
));
1234 static void fw_cfg_class_init(ObjectClass
*klass
, void *data
)
1236 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1238 dc
->reset
= fw_cfg_reset
;
1239 dc
->vmsd
= &vmstate_fw_cfg
;
1241 device_class_set_props(dc
, fw_cfg_properties
);
1244 static const TypeInfo fw_cfg_info
= {
1245 .name
= TYPE_FW_CFG
,
1246 .parent
= TYPE_SYS_BUS_DEVICE
,
1248 .instance_size
= sizeof(FWCfgState
),
1249 .class_init
= fw_cfg_class_init
,
1252 static void fw_cfg_file_slots_allocate(FWCfgState
*s
, Error
**errp
)
1254 uint16_t file_slots_max
;
1256 if (fw_cfg_file_slots(s
) < FW_CFG_FILE_SLOTS_MIN
) {
1257 error_setg(errp
, "\"file_slots\" must be at least 0x%x",
1258 FW_CFG_FILE_SLOTS_MIN
);
1262 /* (UINT16_MAX & FW_CFG_ENTRY_MASK) is the highest inclusive selector value
1263 * that we permit. The actual (exclusive) value coming from the
1264 * configuration is (FW_CFG_FILE_FIRST + fw_cfg_file_slots(s)). */
1265 file_slots_max
= (UINT16_MAX
& FW_CFG_ENTRY_MASK
) - FW_CFG_FILE_FIRST
+ 1;
1266 if (fw_cfg_file_slots(s
) > file_slots_max
) {
1267 error_setg(errp
, "\"file_slots\" must not exceed 0x%" PRIx16
,
1272 s
->entries
[0] = g_new0(FWCfgEntry
, fw_cfg_max_entry(s
));
1273 s
->entries
[1] = g_new0(FWCfgEntry
, fw_cfg_max_entry(s
));
1274 s
->entry_order
= g_new0(int, fw_cfg_max_entry(s
));
1277 static Property fw_cfg_io_properties
[] = {
1278 DEFINE_PROP_BOOL("dma_enabled", FWCfgIoState
, parent_obj
.dma_enabled
,
1280 DEFINE_PROP_UINT16("x-file-slots", FWCfgIoState
, parent_obj
.file_slots
,
1281 FW_CFG_FILE_SLOTS_DFLT
),
1282 DEFINE_PROP_END_OF_LIST(),
1285 static void fw_cfg_io_realize(DeviceState
*dev
, Error
**errp
)
1288 FWCfgIoState
*s
= FW_CFG_IO(dev
);
1290 fw_cfg_file_slots_allocate(FW_CFG(s
), errp
);
1295 /* when using port i/o, the 8-bit data register ALWAYS overlaps
1296 * with half of the 16-bit control register. Hence, the total size
1297 * of the i/o region used is FW_CFG_CTL_SIZE */
1298 memory_region_init_io(&s
->comb_iomem
, OBJECT(s
), &fw_cfg_comb_mem_ops
,
1299 FW_CFG(s
), "fwcfg", FW_CFG_CTL_SIZE
);
1301 if (FW_CFG(s
)->dma_enabled
) {
1302 memory_region_init_io(&FW_CFG(s
)->dma_iomem
, OBJECT(s
),
1303 &fw_cfg_dma_mem_ops
, FW_CFG(s
), "fwcfg.dma",
1304 sizeof(dma_addr_t
));
1307 fw_cfg_common_realize(dev
, errp
);
1310 static void fw_cfg_io_class_init(ObjectClass
*klass
, void *data
)
1312 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1314 dc
->realize
= fw_cfg_io_realize
;
1315 device_class_set_props(dc
, fw_cfg_io_properties
);
1318 static const TypeInfo fw_cfg_io_info
= {
1319 .name
= TYPE_FW_CFG_IO
,
1320 .parent
= TYPE_FW_CFG
,
1321 .instance_size
= sizeof(FWCfgIoState
),
1322 .class_init
= fw_cfg_io_class_init
,
1326 static Property fw_cfg_mem_properties
[] = {
1327 DEFINE_PROP_UINT32("data_width", FWCfgMemState
, data_width
, -1),
1328 DEFINE_PROP_BOOL("dma_enabled", FWCfgMemState
, parent_obj
.dma_enabled
,
1330 DEFINE_PROP_UINT16("x-file-slots", FWCfgMemState
, parent_obj
.file_slots
,
1331 FW_CFG_FILE_SLOTS_DFLT
),
1332 DEFINE_PROP_END_OF_LIST(),
1335 static void fw_cfg_mem_realize(DeviceState
*dev
, Error
**errp
)
1338 FWCfgMemState
*s
= FW_CFG_MEM(dev
);
1339 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
1340 const MemoryRegionOps
*data_ops
= &fw_cfg_data_mem_ops
;
1342 fw_cfg_file_slots_allocate(FW_CFG(s
), errp
);
1347 memory_region_init_io(&s
->ctl_iomem
, OBJECT(s
), &fw_cfg_ctl_mem_ops
,
1348 FW_CFG(s
), "fwcfg.ctl", FW_CFG_CTL_SIZE
);
1349 sysbus_init_mmio(sbd
, &s
->ctl_iomem
);
1351 if (s
->data_width
> data_ops
->valid
.max_access_size
) {
1352 s
->wide_data_ops
= *data_ops
;
1354 s
->wide_data_ops
.valid
.max_access_size
= s
->data_width
;
1355 s
->wide_data_ops
.impl
.max_access_size
= s
->data_width
;
1356 data_ops
= &s
->wide_data_ops
;
1358 memory_region_init_io(&s
->data_iomem
, OBJECT(s
), data_ops
, FW_CFG(s
),
1359 "fwcfg.data", data_ops
->valid
.max_access_size
);
1360 sysbus_init_mmio(sbd
, &s
->data_iomem
);
1362 if (FW_CFG(s
)->dma_enabled
) {
1363 memory_region_init_io(&FW_CFG(s
)->dma_iomem
, OBJECT(s
),
1364 &fw_cfg_dma_mem_ops
, FW_CFG(s
), "fwcfg.dma",
1365 sizeof(dma_addr_t
));
1366 sysbus_init_mmio(sbd
, &FW_CFG(s
)->dma_iomem
);
1369 fw_cfg_common_realize(dev
, errp
);
1372 static void fw_cfg_mem_class_init(ObjectClass
*klass
, void *data
)
1374 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1376 dc
->realize
= fw_cfg_mem_realize
;
1377 device_class_set_props(dc
, fw_cfg_mem_properties
);
1380 static const TypeInfo fw_cfg_mem_info
= {
1381 .name
= TYPE_FW_CFG_MEM
,
1382 .parent
= TYPE_FW_CFG
,
1383 .instance_size
= sizeof(FWCfgMemState
),
1384 .class_init
= fw_cfg_mem_class_init
,
1387 static void fw_cfg_register_types(void)
1389 type_register_static(&fw_cfg_info
);
1390 type_register_static(&fw_cfg_io_info
);
1391 type_register_static(&fw_cfg_mem_info
);
1394 type_init(fw_cfg_register_types
)