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/datadir.h"
27 #include "sysemu/sysemu.h"
28 #include "sysemu/dma.h"
29 #include "sysemu/reset.h"
30 #include "hw/boards.h"
31 #include "hw/nvram/fw_cfg.h"
32 #include "hw/qdev-properties.h"
33 #include "hw/sysbus.h"
34 #include "migration/qemu-file-types.h"
35 #include "migration/vmstate.h"
37 #include "qemu/error-report.h"
38 #include "qemu/option.h"
39 #include "qemu/config-file.h"
40 #include "qemu/cutils.h"
41 #include "qapi/error.h"
42 #include "hw/acpi/aml-build.h"
43 #include "hw/pci/pci_bus.h"
45 #define FW_CFG_FILE_SLOTS_DFLT 0x20
47 /* FW_CFG_VERSION bits */
48 #define FW_CFG_VERSION 0x01
49 #define FW_CFG_VERSION_DMA 0x02
51 /* FW_CFG_DMA_CONTROL bits */
52 #define FW_CFG_DMA_CTL_ERROR 0x01
53 #define FW_CFG_DMA_CTL_READ 0x02
54 #define FW_CFG_DMA_CTL_SKIP 0x04
55 #define FW_CFG_DMA_CTL_SELECT 0x08
56 #define FW_CFG_DMA_CTL_WRITE 0x10
58 #define FW_CFG_DMA_SIGNATURE 0x51454d5520434647ULL /* "QEMU CFG" */
64 void *callback_opaque
;
65 FWCfgCallback select_cb
;
66 FWCfgWriteCallback write_cb
;
72 * @key: The uint16 selector key.
74 * Returns: The stringified name if the selector refers to a well-known
75 * numerically defined item, or NULL on key lookup failure.
77 static const char *key_name(uint16_t key
)
79 static const char *fw_cfg_wellknown_keys
[FW_CFG_FILE_FIRST
] = {
80 [FW_CFG_SIGNATURE
] = "signature",
82 [FW_CFG_UUID
] = "uuid",
83 [FW_CFG_RAM_SIZE
] = "ram_size",
84 [FW_CFG_NOGRAPHIC
] = "nographic",
85 [FW_CFG_NB_CPUS
] = "nb_cpus",
86 [FW_CFG_MACHINE_ID
] = "machine_id",
87 [FW_CFG_KERNEL_ADDR
] = "kernel_addr",
88 [FW_CFG_KERNEL_SIZE
] = "kernel_size",
89 [FW_CFG_KERNEL_CMDLINE
] = "kernel_cmdline",
90 [FW_CFG_INITRD_ADDR
] = "initrd_addr",
91 [FW_CFG_INITRD_SIZE
] = "initdr_size",
92 [FW_CFG_BOOT_DEVICE
] = "boot_device",
93 [FW_CFG_NUMA
] = "numa",
94 [FW_CFG_BOOT_MENU
] = "boot_menu",
95 [FW_CFG_MAX_CPUS
] = "max_cpus",
96 [FW_CFG_KERNEL_ENTRY
] = "kernel_entry",
97 [FW_CFG_KERNEL_DATA
] = "kernel_data",
98 [FW_CFG_INITRD_DATA
] = "initrd_data",
99 [FW_CFG_CMDLINE_ADDR
] = "cmdline_addr",
100 [FW_CFG_CMDLINE_SIZE
] = "cmdline_size",
101 [FW_CFG_CMDLINE_DATA
] = "cmdline_data",
102 [FW_CFG_SETUP_ADDR
] = "setup_addr",
103 [FW_CFG_SETUP_SIZE
] = "setup_size",
104 [FW_CFG_SETUP_DATA
] = "setup_data",
105 [FW_CFG_FILE_DIR
] = "file_dir",
108 if (key
& FW_CFG_ARCH_LOCAL
) {
109 return fw_cfg_arch_key_name(key
);
111 if (key
< FW_CFG_FILE_FIRST
) {
112 return fw_cfg_wellknown_keys
[key
];
118 static inline const char *trace_key_name(uint16_t key
)
120 const char *name
= key_name(key
);
122 return name
? name
: "unknown";
128 static char *read_splashfile(char *filename
, gsize
*file_sizep
,
134 unsigned int filehead
;
137 if (!g_file_get_contents(filename
, &content
, file_sizep
, &err
)) {
138 error_report("failed to read splash file '%s': %s",
139 filename
, err
->message
);
144 /* check file size */
145 if (*file_sizep
< 30) {
150 filehead
= lduw_le_p(content
);
151 if (filehead
== 0xd8ff) {
152 file_type
= JPG_FILE
;
153 } else if (filehead
== 0x4d42) {
154 file_type
= BMP_FILE
;
160 if (file_type
== BMP_FILE
) {
161 bmp_bpp
= lduw_le_p(&content
[28]);
168 *file_typep
= file_type
;
173 error_report("splash file '%s' format not recognized; must be JPEG "
174 "or 24 bit BMP", filename
);
179 static void fw_cfg_bootsplash(FWCfgState
*s
)
181 char *filename
, *file_data
;
185 /* insert splash time if user configurated */
186 if (current_machine
->boot_config
.has_splash_time
) {
187 int64_t bst_val
= current_machine
->boot_config
.splash_time
;
190 /* validate the input */
191 if (bst_val
< 0 || bst_val
> 0xffff) {
192 error_report("splash-time is invalid,"
193 "it should be a value between 0 and 65535");
196 /* use little endian format */
197 bst_le16
= cpu_to_le16(bst_val
);
198 fw_cfg_add_file(s
, "etc/boot-menu-wait",
199 g_memdup(&bst_le16
, sizeof bst_le16
), sizeof bst_le16
);
202 /* insert splash file if user configurated */
203 if (current_machine
->boot_config
.has_splash
) {
204 const char *boot_splash_filename
= current_machine
->boot_config
.splash
;
205 filename
= qemu_find_file(QEMU_FILE_TYPE_BIOS
, boot_splash_filename
);
206 if (filename
== NULL
) {
207 error_report("failed to find file '%s'", boot_splash_filename
);
211 /* loading file data */
212 file_data
= read_splashfile(filename
, &file_size
, &file_type
);
213 if (file_data
== NULL
) {
217 g_free(boot_splash_filedata
);
218 boot_splash_filedata
= (uint8_t *)file_data
;
221 if (file_type
== JPG_FILE
) {
222 fw_cfg_add_file(s
, "bootsplash.jpg",
223 boot_splash_filedata
, file_size
);
225 fw_cfg_add_file(s
, "bootsplash.bmp",
226 boot_splash_filedata
, file_size
);
232 static void fw_cfg_reboot(FWCfgState
*s
)
234 uint64_t rt_val
= -1;
237 if (current_machine
->boot_config
.has_reboot_timeout
) {
238 rt_val
= current_machine
->boot_config
.reboot_timeout
;
240 /* validate the input */
241 if (rt_val
> 0xffff && rt_val
!= (uint64_t)-1) {
242 error_report("reboot timeout is invalid,"
243 "it should be a value between -1 and 65535");
248 rt_le32
= cpu_to_le32(rt_val
);
249 fw_cfg_add_file(s
, "etc/boot-fail-wait", g_memdup(&rt_le32
, 4), 4);
252 static void fw_cfg_write(FWCfgState
*s
, uint8_t value
)
254 /* nothing, write support removed in QEMU v2.4+ */
257 static inline uint16_t fw_cfg_file_slots(const FWCfgState
*s
)
259 return s
->file_slots
;
262 /* Note: this function returns an exclusive limit. */
263 static inline uint32_t fw_cfg_max_entry(const FWCfgState
*s
)
265 return FW_CFG_FILE_FIRST
+ fw_cfg_file_slots(s
);
268 static int fw_cfg_select(FWCfgState
*s
, uint16_t key
)
274 if ((key
& FW_CFG_ENTRY_MASK
) >= fw_cfg_max_entry(s
)) {
275 s
->cur_entry
= FW_CFG_INVALID
;
280 /* entry successfully selected, now run callback if present */
281 arch
= !!(key
& FW_CFG_ARCH_LOCAL
);
282 e
= &s
->entries
[arch
][key
& FW_CFG_ENTRY_MASK
];
284 e
->select_cb(e
->callback_opaque
);
288 trace_fw_cfg_select(s
, key
, trace_key_name(key
), ret
);
292 static uint64_t fw_cfg_data_read(void *opaque
, hwaddr addr
, unsigned size
)
294 FWCfgState
*s
= opaque
;
295 int arch
= !!(s
->cur_entry
& FW_CFG_ARCH_LOCAL
);
296 FWCfgEntry
*e
= (s
->cur_entry
== FW_CFG_INVALID
) ? NULL
:
297 &s
->entries
[arch
][s
->cur_entry
& FW_CFG_ENTRY_MASK
];
300 assert(size
> 0 && size
<= sizeof(value
));
301 if (s
->cur_entry
!= FW_CFG_INVALID
&& e
->data
&& s
->cur_offset
< e
->len
) {
302 /* The least significant 'size' bytes of the return value are
303 * expected to contain a string preserving portion of the item
304 * data, padded with zeros on the right in case we run out early.
305 * In technical terms, we're composing the host-endian representation
306 * of the big endian interpretation of the fw_cfg string.
309 value
= (value
<< 8) | e
->data
[s
->cur_offset
++];
310 } while (--size
&& s
->cur_offset
< e
->len
);
311 /* If size is still not zero, we *did* run out early, so continue
312 * left-shifting, to add the appropriate number of padding zeros
318 trace_fw_cfg_read(s
, value
);
322 static void fw_cfg_data_mem_write(void *opaque
, hwaddr addr
,
323 uint64_t value
, unsigned size
)
325 FWCfgState
*s
= opaque
;
329 fw_cfg_write(s
, value
>> (8 * --i
));
333 static void fw_cfg_dma_transfer(FWCfgState
*s
)
339 int read
= 0, write
= 0;
342 /* Reset the address before the next access */
343 dma_addr
= s
->dma_addr
;
346 if (dma_memory_read(s
->dma_as
, dma_addr
,
347 &dma
, sizeof(dma
), MEMTXATTRS_UNSPECIFIED
)) {
348 stl_be_dma(s
->dma_as
, dma_addr
+ offsetof(FWCfgDmaAccess
, control
),
349 FW_CFG_DMA_CTL_ERROR
, MEMTXATTRS_UNSPECIFIED
);
353 dma
.address
= be64_to_cpu(dma
.address
);
354 dma
.length
= be32_to_cpu(dma
.length
);
355 dma
.control
= be32_to_cpu(dma
.control
);
357 if (dma
.control
& FW_CFG_DMA_CTL_SELECT
) {
358 fw_cfg_select(s
, dma
.control
>> 16);
361 arch
= !!(s
->cur_entry
& FW_CFG_ARCH_LOCAL
);
362 e
= (s
->cur_entry
== FW_CFG_INVALID
) ? NULL
:
363 &s
->entries
[arch
][s
->cur_entry
& FW_CFG_ENTRY_MASK
];
365 if (dma
.control
& FW_CFG_DMA_CTL_READ
) {
368 } else if (dma
.control
& FW_CFG_DMA_CTL_WRITE
) {
371 } else if (dma
.control
& FW_CFG_DMA_CTL_SKIP
) {
380 while (dma
.length
> 0 && !(dma
.control
& FW_CFG_DMA_CTL_ERROR
)) {
381 if (s
->cur_entry
== FW_CFG_INVALID
|| !e
->data
||
382 s
->cur_offset
>= e
->len
) {
385 /* If the access is not a read access, it will be a skip access,
389 if (dma_memory_set(s
->dma_as
, dma
.address
, 0, len
,
390 MEMTXATTRS_UNSPECIFIED
)) {
391 dma
.control
|= FW_CFG_DMA_CTL_ERROR
;
395 dma
.control
|= FW_CFG_DMA_CTL_ERROR
;
398 if (dma
.length
<= (e
->len
- s
->cur_offset
)) {
401 len
= (e
->len
- s
->cur_offset
);
404 /* If the access is not a read access, it will be a skip access,
408 if (dma_memory_write(s
->dma_as
, dma
.address
,
409 &e
->data
[s
->cur_offset
], len
,
410 MEMTXATTRS_UNSPECIFIED
)) {
411 dma
.control
|= FW_CFG_DMA_CTL_ERROR
;
415 if (!e
->allow_write
||
417 dma_memory_read(s
->dma_as
, dma
.address
,
418 &e
->data
[s
->cur_offset
], len
,
419 MEMTXATTRS_UNSPECIFIED
)) {
420 dma
.control
|= FW_CFG_DMA_CTL_ERROR
;
421 } else if (e
->write_cb
) {
422 e
->write_cb(e
->callback_opaque
, s
->cur_offset
, len
);
426 s
->cur_offset
+= len
;
434 stl_be_dma(s
->dma_as
, dma_addr
+ offsetof(FWCfgDmaAccess
, control
),
435 dma
.control
, MEMTXATTRS_UNSPECIFIED
);
437 trace_fw_cfg_read(s
, 0);
440 static uint64_t fw_cfg_dma_mem_read(void *opaque
, hwaddr addr
,
443 /* Return a signature value (and handle various read sizes) */
444 return extract64(FW_CFG_DMA_SIGNATURE
, (8 - addr
- size
) * 8, size
* 8);
447 static void fw_cfg_dma_mem_write(void *opaque
, hwaddr addr
,
448 uint64_t value
, unsigned size
)
450 FWCfgState
*s
= opaque
;
454 /* FWCfgDmaAccess high address */
455 s
->dma_addr
= value
<< 32;
456 } else if (addr
== 4) {
457 /* FWCfgDmaAccess low address */
458 s
->dma_addr
|= value
;
459 fw_cfg_dma_transfer(s
);
461 } else if (size
== 8 && addr
== 0) {
463 fw_cfg_dma_transfer(s
);
467 static bool fw_cfg_dma_mem_valid(void *opaque
, hwaddr addr
,
468 unsigned size
, bool is_write
,
471 return !is_write
|| ((size
== 4 && (addr
== 0 || addr
== 4)) ||
472 (size
== 8 && addr
== 0));
475 static bool fw_cfg_data_mem_valid(void *opaque
, hwaddr addr
,
476 unsigned size
, bool is_write
,
482 static uint64_t fw_cfg_ctl_mem_read(void *opaque
, hwaddr addr
, unsigned size
)
487 static void fw_cfg_ctl_mem_write(void *opaque
, hwaddr addr
,
488 uint64_t value
, unsigned size
)
490 fw_cfg_select(opaque
, (uint16_t)value
);
493 static bool fw_cfg_ctl_mem_valid(void *opaque
, hwaddr addr
,
494 unsigned size
, bool is_write
,
497 return is_write
&& size
== 2;
500 static void fw_cfg_comb_write(void *opaque
, hwaddr addr
,
501 uint64_t value
, unsigned size
)
505 fw_cfg_write(opaque
, (uint8_t)value
);
508 fw_cfg_select(opaque
, (uint16_t)value
);
513 static bool fw_cfg_comb_valid(void *opaque
, hwaddr addr
,
514 unsigned size
, bool is_write
,
517 return (size
== 1) || (is_write
&& size
== 2);
520 static const MemoryRegionOps fw_cfg_ctl_mem_ops
= {
521 .read
= fw_cfg_ctl_mem_read
,
522 .write
= fw_cfg_ctl_mem_write
,
523 .endianness
= DEVICE_BIG_ENDIAN
,
524 .valid
.accepts
= fw_cfg_ctl_mem_valid
,
527 static const MemoryRegionOps fw_cfg_data_mem_ops
= {
528 .read
= fw_cfg_data_read
,
529 .write
= fw_cfg_data_mem_write
,
530 .endianness
= DEVICE_BIG_ENDIAN
,
532 .min_access_size
= 1,
533 .max_access_size
= 1,
534 .accepts
= fw_cfg_data_mem_valid
,
538 static const MemoryRegionOps fw_cfg_comb_mem_ops
= {
539 .read
= fw_cfg_data_read
,
540 .write
= fw_cfg_comb_write
,
541 .endianness
= DEVICE_LITTLE_ENDIAN
,
542 .valid
.accepts
= fw_cfg_comb_valid
,
545 static const MemoryRegionOps fw_cfg_dma_mem_ops
= {
546 .read
= fw_cfg_dma_mem_read
,
547 .write
= fw_cfg_dma_mem_write
,
548 .endianness
= DEVICE_BIG_ENDIAN
,
549 .valid
.accepts
= fw_cfg_dma_mem_valid
,
550 .valid
.max_access_size
= 8,
551 .impl
.max_access_size
= 8,
554 static void fw_cfg_reset(DeviceState
*d
)
556 FWCfgState
*s
= FW_CFG(d
);
558 /* we never register a read callback for FW_CFG_SIGNATURE */
559 fw_cfg_select(s
, FW_CFG_SIGNATURE
);
562 /* Save restore 32 bit int as uint16_t
563 This is a Big hack, but it is how the old state did it.
564 Or we broke compatibility in the state, or we can't use struct tm
567 static int get_uint32_as_uint16(QEMUFile
*f
, void *pv
, size_t size
,
568 const VMStateField
*field
)
571 *v
= qemu_get_be16(f
);
575 static int put_unused(QEMUFile
*f
, void *pv
, size_t size
,
576 const VMStateField
*field
, JSONWriter
*vmdesc
)
578 fprintf(stderr
, "uint32_as_uint16 is only used for backward compatibility.\n");
579 fprintf(stderr
, "This functions shouldn't be called.\n");
584 static const VMStateInfo vmstate_hack_uint32_as_uint16
= {
585 .name
= "int32_as_uint16",
586 .get
= get_uint32_as_uint16
,
590 #define VMSTATE_UINT16_HACK(_f, _s, _t) \
591 VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_hack_uint32_as_uint16, uint32_t)
594 static bool is_version_1(void *opaque
, int version_id
)
596 return version_id
== 1;
599 bool fw_cfg_dma_enabled(void *opaque
)
601 FWCfgState
*s
= opaque
;
603 return s
->dma_enabled
;
606 static bool fw_cfg_acpi_mr_restore(void *opaque
)
608 FWCfgState
*s
= opaque
;
611 mr_aligned
= QEMU_IS_ALIGNED(s
->table_mr_size
, qemu_real_host_page_size()) &&
612 QEMU_IS_ALIGNED(s
->linker_mr_size
, qemu_real_host_page_size()) &&
613 QEMU_IS_ALIGNED(s
->rsdp_mr_size
, qemu_real_host_page_size());
614 return s
->acpi_mr_restore
&& !mr_aligned
;
617 static void fw_cfg_update_mr(FWCfgState
*s
, uint16_t key
, size_t size
)
621 int arch
= !!(key
& FW_CFG_ARCH_LOCAL
);
624 key
&= FW_CFG_ENTRY_MASK
;
625 assert(key
< fw_cfg_max_entry(s
));
627 ptr
= s
->entries
[arch
][key
].data
;
628 mr
= memory_region_from_host(ptr
, &offset
);
630 memory_region_ram_resize(mr
, size
, &error_abort
);
633 static int fw_cfg_acpi_mr_restore_post_load(void *opaque
, int version_id
)
635 FWCfgState
*s
= opaque
;
640 index
= be32_to_cpu(s
->files
->count
);
642 for (i
= 0; i
< index
; i
++) {
643 if (!strcmp(s
->files
->f
[i
].name
, ACPI_BUILD_TABLE_FILE
)) {
644 fw_cfg_update_mr(s
, FW_CFG_FILE_FIRST
+ i
, s
->table_mr_size
);
645 } else if (!strcmp(s
->files
->f
[i
].name
, ACPI_BUILD_LOADER_FILE
)) {
646 fw_cfg_update_mr(s
, FW_CFG_FILE_FIRST
+ i
, s
->linker_mr_size
);
647 } else if (!strcmp(s
->files
->f
[i
].name
, ACPI_BUILD_RSDP_FILE
)) {
648 fw_cfg_update_mr(s
, FW_CFG_FILE_FIRST
+ i
, s
->rsdp_mr_size
);
655 static const VMStateDescription vmstate_fw_cfg_dma
= {
656 .name
= "fw_cfg/dma",
657 .needed
= fw_cfg_dma_enabled
,
658 .fields
= (VMStateField
[]) {
659 VMSTATE_UINT64(dma_addr
, FWCfgState
),
660 VMSTATE_END_OF_LIST()
664 static const VMStateDescription vmstate_fw_cfg_acpi_mr
= {
665 .name
= "fw_cfg/acpi_mr",
667 .minimum_version_id
= 1,
668 .needed
= fw_cfg_acpi_mr_restore
,
669 .post_load
= fw_cfg_acpi_mr_restore_post_load
,
670 .fields
= (VMStateField
[]) {
671 VMSTATE_UINT64(table_mr_size
, FWCfgState
),
672 VMSTATE_UINT64(linker_mr_size
, FWCfgState
),
673 VMSTATE_UINT64(rsdp_mr_size
, FWCfgState
),
674 VMSTATE_END_OF_LIST()
678 static const VMStateDescription vmstate_fw_cfg
= {
681 .minimum_version_id
= 1,
682 .fields
= (VMStateField
[]) {
683 VMSTATE_UINT16(cur_entry
, FWCfgState
),
684 VMSTATE_UINT16_HACK(cur_offset
, FWCfgState
, is_version_1
),
685 VMSTATE_UINT32_V(cur_offset
, FWCfgState
, 2),
686 VMSTATE_END_OF_LIST()
688 .subsections
= (const VMStateDescription
*[]) {
690 &vmstate_fw_cfg_acpi_mr
,
695 static void fw_cfg_add_bytes_callback(FWCfgState
*s
, uint16_t key
,
696 FWCfgCallback select_cb
,
697 FWCfgWriteCallback write_cb
,
698 void *callback_opaque
,
699 void *data
, size_t len
,
702 int arch
= !!(key
& FW_CFG_ARCH_LOCAL
);
704 key
&= FW_CFG_ENTRY_MASK
;
706 assert(key
< fw_cfg_max_entry(s
) && len
< UINT32_MAX
);
707 assert(s
->entries
[arch
][key
].data
== NULL
); /* avoid key conflict */
709 s
->entries
[arch
][key
].data
= data
;
710 s
->entries
[arch
][key
].len
= (uint32_t)len
;
711 s
->entries
[arch
][key
].select_cb
= select_cb
;
712 s
->entries
[arch
][key
].write_cb
= write_cb
;
713 s
->entries
[arch
][key
].callback_opaque
= callback_opaque
;
714 s
->entries
[arch
][key
].allow_write
= !read_only
;
717 static void *fw_cfg_modify_bytes_read(FWCfgState
*s
, uint16_t key
,
718 void *data
, size_t len
)
721 int arch
= !!(key
& FW_CFG_ARCH_LOCAL
);
723 key
&= FW_CFG_ENTRY_MASK
;
725 assert(key
< fw_cfg_max_entry(s
) && len
< UINT32_MAX
);
727 /* return the old data to the function caller, avoid memory leak */
728 ptr
= s
->entries
[arch
][key
].data
;
729 s
->entries
[arch
][key
].data
= data
;
730 s
->entries
[arch
][key
].len
= len
;
731 s
->entries
[arch
][key
].callback_opaque
= NULL
;
732 s
->entries
[arch
][key
].allow_write
= false;
737 void fw_cfg_add_bytes(FWCfgState
*s
, uint16_t key
, void *data
, size_t len
)
739 trace_fw_cfg_add_bytes(key
, trace_key_name(key
), len
);
740 fw_cfg_add_bytes_callback(s
, key
, NULL
, NULL
, NULL
, data
, len
, true);
743 void fw_cfg_add_string(FWCfgState
*s
, uint16_t key
, const char *value
)
745 size_t sz
= strlen(value
) + 1;
747 trace_fw_cfg_add_string(key
, trace_key_name(key
), value
);
748 fw_cfg_add_bytes(s
, key
, g_memdup(value
, sz
), sz
);
751 void fw_cfg_modify_string(FWCfgState
*s
, uint16_t key
, const char *value
)
753 size_t sz
= strlen(value
) + 1;
756 old
= fw_cfg_modify_bytes_read(s
, key
, g_memdup(value
, sz
), sz
);
760 void fw_cfg_add_i16(FWCfgState
*s
, uint16_t key
, uint16_t value
)
764 copy
= g_malloc(sizeof(value
));
765 *copy
= cpu_to_le16(value
);
766 trace_fw_cfg_add_i16(key
, trace_key_name(key
), value
);
767 fw_cfg_add_bytes(s
, key
, copy
, sizeof(value
));
770 void fw_cfg_modify_i16(FWCfgState
*s
, uint16_t key
, uint16_t value
)
772 uint16_t *copy
, *old
;
774 copy
= g_malloc(sizeof(value
));
775 *copy
= cpu_to_le16(value
);
776 old
= fw_cfg_modify_bytes_read(s
, key
, copy
, sizeof(value
));
780 void fw_cfg_add_i32(FWCfgState
*s
, uint16_t key
, uint32_t value
)
784 copy
= g_malloc(sizeof(value
));
785 *copy
= cpu_to_le32(value
);
786 trace_fw_cfg_add_i32(key
, trace_key_name(key
), value
);
787 fw_cfg_add_bytes(s
, key
, copy
, sizeof(value
));
790 void fw_cfg_modify_i32(FWCfgState
*s
, uint16_t key
, uint32_t value
)
792 uint32_t *copy
, *old
;
794 copy
= g_malloc(sizeof(value
));
795 *copy
= cpu_to_le32(value
);
796 old
= fw_cfg_modify_bytes_read(s
, key
, copy
, sizeof(value
));
800 void fw_cfg_add_i64(FWCfgState
*s
, uint16_t key
, uint64_t value
)
804 copy
= g_malloc(sizeof(value
));
805 *copy
= cpu_to_le64(value
);
806 trace_fw_cfg_add_i64(key
, trace_key_name(key
), value
);
807 fw_cfg_add_bytes(s
, key
, copy
, sizeof(value
));
810 void fw_cfg_modify_i64(FWCfgState
*s
, uint16_t key
, uint64_t value
)
812 uint64_t *copy
, *old
;
814 copy
= g_malloc(sizeof(value
));
815 *copy
= cpu_to_le64(value
);
816 old
= fw_cfg_modify_bytes_read(s
, key
, copy
, sizeof(value
));
820 void fw_cfg_set_order_override(FWCfgState
*s
, int order
)
822 assert(s
->fw_cfg_order_override
== 0);
823 s
->fw_cfg_order_override
= order
;
826 void fw_cfg_reset_order_override(FWCfgState
*s
)
828 assert(s
->fw_cfg_order_override
!= 0);
829 s
->fw_cfg_order_override
= 0;
833 * This is the legacy order list. For legacy systems, files are in
834 * the fw_cfg in the order defined below, by the "order" value. Note
835 * that some entries (VGA ROMs, NIC option ROMS, etc.) go into a
836 * specific area, but there may be more than one and they occur in the
837 * order that the user specifies them on the command line. Those are
838 * handled in a special manner, using the order override above.
840 * For non-legacy, the files are sorted by filename to avoid this kind
841 * of complexity in the future.
843 * This is only for x86, other arches don't implement versioning so
844 * they won't set legacy mode.
850 { "etc/boot-menu-wait", 10 },
851 { "bootsplash.jpg", 11 },
852 { "bootsplash.bmp", 12 },
853 { "etc/boot-fail-wait", 15 },
854 { "etc/smbios/smbios-tables", 20 },
855 { "etc/smbios/smbios-anchor", 30 },
857 { "etc/reserved-memory-end", 50 },
858 { "genroms/kvmvapic.bin", 55 },
859 { "genroms/linuxboot.bin", 60 },
860 { }, /* VGA ROMs from pc_vga_init come here, 70. */
861 { }, /* NIC option ROMs from pc_nic_init come here, 80. */
862 { "etc/system-states", 90 },
863 { }, /* User ROMs come here, 100. */
864 { }, /* Device FW comes here, 110. */
865 { "etc/extra-pci-roots", 120 },
866 { "etc/acpi/tables", 130 },
867 { "etc/table-loader", 140 },
868 { "etc/tpm/log", 150 },
869 { "etc/acpi/rsdp", 160 },
870 { "bootorder", 170 },
871 { "etc/msr_feature_control", 180 },
873 #define FW_CFG_ORDER_OVERRIDE_LAST 200
877 * Any sub-page size update to these table MRs will be lost during migration,
878 * as we use aligned size in ram_load_precopy() -> qemu_ram_resize() path.
879 * In order to avoid the inconsistency in sizes save them seperately and
880 * migrate over in vmstate post_load().
882 static void fw_cfg_acpi_mr_save(FWCfgState
*s
, const char *filename
, size_t len
)
884 if (!strcmp(filename
, ACPI_BUILD_TABLE_FILE
)) {
885 s
->table_mr_size
= len
;
886 } else if (!strcmp(filename
, ACPI_BUILD_LOADER_FILE
)) {
887 s
->linker_mr_size
= len
;
888 } else if (!strcmp(filename
, ACPI_BUILD_RSDP_FILE
)) {
889 s
->rsdp_mr_size
= len
;
893 static int get_fw_cfg_order(FWCfgState
*s
, const char *name
)
897 if (s
->fw_cfg_order_override
> 0) {
898 return s
->fw_cfg_order_override
;
901 for (i
= 0; i
< ARRAY_SIZE(fw_cfg_order
); i
++) {
902 if (fw_cfg_order
[i
].name
== NULL
) {
906 if (strcmp(name
, fw_cfg_order
[i
].name
) == 0) {
907 return fw_cfg_order
[i
].order
;
911 /* Stick unknown stuff at the end. */
912 warn_report("Unknown firmware file in legacy mode: %s", name
);
913 return FW_CFG_ORDER_OVERRIDE_LAST
;
916 void fw_cfg_add_file_callback(FWCfgState
*s
, const char *filename
,
917 FWCfgCallback select_cb
,
918 FWCfgWriteCallback write_cb
,
919 void *callback_opaque
,
920 void *data
, size_t len
, bool read_only
)
924 MachineClass
*mc
= MACHINE_GET_CLASS(qdev_get_machine());
928 dsize
= sizeof(uint32_t) + sizeof(FWCfgFile
) * fw_cfg_file_slots(s
);
929 s
->files
= g_malloc0(dsize
);
930 fw_cfg_add_bytes(s
, FW_CFG_FILE_DIR
, s
->files
, dsize
);
933 count
= be32_to_cpu(s
->files
->count
);
934 assert(count
< fw_cfg_file_slots(s
));
936 /* Find the insertion point. */
937 if (mc
->legacy_fw_cfg_order
) {
939 * Sort by order. For files with the same order, we keep them
940 * in the sequence in which they were added.
942 order
= get_fw_cfg_order(s
, filename
);
944 index
> 0 && order
< s
->entry_order
[index
- 1];
947 /* Sort by file name. */
949 index
> 0 && strcmp(filename
, s
->files
->f
[index
- 1].name
) < 0;
954 * Move all the entries from the index point and after down one
955 * to create a slot for the new entry. Because calculations are
956 * being done with the index, make it so that "i" is the current
957 * index and "i - 1" is the one being copied from, thus the
958 * unusual start and end in the for statement.
960 for (i
= count
; i
> index
; i
--) {
961 s
->files
->f
[i
] = s
->files
->f
[i
- 1];
962 s
->files
->f
[i
].select
= cpu_to_be16(FW_CFG_FILE_FIRST
+ i
);
963 s
->entries
[0][FW_CFG_FILE_FIRST
+ i
] =
964 s
->entries
[0][FW_CFG_FILE_FIRST
+ i
- 1];
965 s
->entry_order
[i
] = s
->entry_order
[i
- 1];
968 memset(&s
->files
->f
[index
], 0, sizeof(FWCfgFile
));
969 memset(&s
->entries
[0][FW_CFG_FILE_FIRST
+ index
], 0, sizeof(FWCfgEntry
));
971 pstrcpy(s
->files
->f
[index
].name
, sizeof(s
->files
->f
[index
].name
), filename
);
972 for (i
= 0; i
<= count
; i
++) {
974 strcmp(s
->files
->f
[index
].name
, s
->files
->f
[i
].name
) == 0) {
975 error_report("duplicate fw_cfg file name: %s",
976 s
->files
->f
[index
].name
);
981 fw_cfg_add_bytes_callback(s
, FW_CFG_FILE_FIRST
+ index
,
983 callback_opaque
, data
, len
,
986 s
->files
->f
[index
].size
= cpu_to_be32(len
);
987 s
->files
->f
[index
].select
= cpu_to_be16(FW_CFG_FILE_FIRST
+ index
);
988 s
->entry_order
[index
] = order
;
989 trace_fw_cfg_add_file(s
, index
, s
->files
->f
[index
].name
, len
);
991 s
->files
->count
= cpu_to_be32(count
+1);
992 fw_cfg_acpi_mr_save(s
, filename
, len
);
995 void fw_cfg_add_file(FWCfgState
*s
, const char *filename
,
996 void *data
, size_t len
)
998 fw_cfg_add_file_callback(s
, filename
, NULL
, NULL
, NULL
, data
, len
, true);
1001 void *fw_cfg_modify_file(FWCfgState
*s
, const char *filename
,
1002 void *data
, size_t len
)
1009 index
= be32_to_cpu(s
->files
->count
);
1011 for (i
= 0; i
< index
; i
++) {
1012 if (strcmp(filename
, s
->files
->f
[i
].name
) == 0) {
1013 ptr
= fw_cfg_modify_bytes_read(s
, FW_CFG_FILE_FIRST
+ i
,
1015 s
->files
->f
[i
].size
= cpu_to_be32(len
);
1016 fw_cfg_acpi_mr_save(s
, filename
, len
);
1021 assert(index
< fw_cfg_file_slots(s
));
1024 fw_cfg_add_file_callback(s
, filename
, NULL
, NULL
, NULL
, data
, len
, true);
1028 bool fw_cfg_add_from_generator(FWCfgState
*s
, const char *filename
,
1029 const char *gen_id
, Error
**errp
)
1031 FWCfgDataGeneratorClass
*klass
;
1036 obj
= object_resolve_path_component(object_get_objects_root(), gen_id
);
1038 error_setg(errp
, "Cannot find object ID '%s'", gen_id
);
1041 if (!object_dynamic_cast(obj
, TYPE_FW_CFG_DATA_GENERATOR_INTERFACE
)) {
1042 error_setg(errp
, "Object ID '%s' is not a '%s' subclass",
1043 gen_id
, TYPE_FW_CFG_DATA_GENERATOR_INTERFACE
);
1046 klass
= FW_CFG_DATA_GENERATOR_GET_CLASS(obj
);
1047 array
= klass
->get_data(obj
, errp
);
1052 fw_cfg_add_file(s
, filename
, g_byte_array_free(array
, FALSE
), size
);
1057 void fw_cfg_add_extra_pci_roots(PCIBus
*bus
, FWCfgState
*s
)
1059 int extra_hosts
= 0;
1065 QLIST_FOREACH(bus
, &bus
->child
, sibling
) {
1066 /* look for expander root buses */
1067 if (pci_bus_is_root(bus
)) {
1072 if (extra_hosts
&& s
) {
1073 uint64_t *val
= g_malloc(sizeof(*val
));
1074 *val
= cpu_to_le64(extra_hosts
);
1075 fw_cfg_add_file(s
, "etc/extra-pci-roots", val
, sizeof(*val
));
1079 static void fw_cfg_machine_reset(void *opaque
)
1081 MachineClass
*mc
= MACHINE_GET_CLASS(qdev_get_machine());
1082 FWCfgState
*s
= opaque
;
1087 buf
= get_boot_devices_list(&len
);
1088 ptr
= fw_cfg_modify_file(s
, "bootorder", (uint8_t *)buf
, len
);
1091 if (!mc
->legacy_fw_cfg_order
) {
1092 buf
= get_boot_devices_lchs_list(&len
);
1093 ptr
= fw_cfg_modify_file(s
, "bios-geometry", (uint8_t *)buf
, len
);
1098 static void fw_cfg_machine_ready(struct Notifier
*n
, void *data
)
1100 FWCfgState
*s
= container_of(n
, FWCfgState
, machine_ready
);
1101 qemu_register_reset(fw_cfg_machine_reset
, s
);
1104 static Property fw_cfg_properties
[] = {
1105 DEFINE_PROP_BOOL("acpi-mr-restore", FWCfgState
, acpi_mr_restore
, true),
1106 DEFINE_PROP_END_OF_LIST(),
1109 static void fw_cfg_common_realize(DeviceState
*dev
, Error
**errp
)
1111 FWCfgState
*s
= FW_CFG(dev
);
1112 MachineState
*machine
= MACHINE(qdev_get_machine());
1113 uint32_t version
= FW_CFG_VERSION
;
1115 if (!fw_cfg_find()) {
1116 error_setg(errp
, "at most one %s device is permitted", TYPE_FW_CFG
);
1120 fw_cfg_add_bytes(s
, FW_CFG_SIGNATURE
, (char *)"QEMU", 4);
1121 fw_cfg_add_bytes(s
, FW_CFG_UUID
, &qemu_uuid
, 16);
1122 fw_cfg_add_i16(s
, FW_CFG_NOGRAPHIC
, (uint16_t)!machine
->enable_graphics
);
1123 fw_cfg_add_i16(s
, FW_CFG_BOOT_MENU
, (uint16_t)(machine
->boot_config
.has_menu
&& machine
->boot_config
.menu
));
1124 fw_cfg_bootsplash(s
);
1127 if (s
->dma_enabled
) {
1128 version
|= FW_CFG_VERSION_DMA
;
1131 fw_cfg_add_i32(s
, FW_CFG_ID
, version
);
1133 s
->machine_ready
.notify
= fw_cfg_machine_ready
;
1134 qemu_add_machine_init_done_notifier(&s
->machine_ready
);
1137 FWCfgState
*fw_cfg_init_io_dma(uint32_t iobase
, uint32_t dma_iobase
,
1138 AddressSpace
*dma_as
)
1144 bool dma_requested
= dma_iobase
&& dma_as
;
1146 dev
= qdev_new(TYPE_FW_CFG_IO
);
1147 if (!dma_requested
) {
1148 qdev_prop_set_bit(dev
, "dma_enabled", false);
1151 object_property_add_child(OBJECT(qdev_get_machine()), TYPE_FW_CFG
,
1154 sbd
= SYS_BUS_DEVICE(dev
);
1155 sysbus_realize_and_unref(sbd
, &error_fatal
);
1156 ios
= FW_CFG_IO(dev
);
1157 sysbus_add_io(sbd
, iobase
, &ios
->comb_iomem
);
1161 if (s
->dma_enabled
) {
1162 /* 64 bits for the address field */
1165 sysbus_add_io(sbd
, dma_iobase
, &s
->dma_iomem
);
1171 FWCfgState
*fw_cfg_init_io(uint32_t iobase
)
1173 return fw_cfg_init_io_dma(iobase
, 0, NULL
);
1176 FWCfgState
*fw_cfg_init_mem_wide(hwaddr ctl_addr
,
1177 hwaddr data_addr
, uint32_t data_width
,
1178 hwaddr dma_addr
, AddressSpace
*dma_as
)
1183 bool dma_requested
= dma_addr
&& dma_as
;
1185 dev
= qdev_new(TYPE_FW_CFG_MEM
);
1186 qdev_prop_set_uint32(dev
, "data_width", data_width
);
1187 if (!dma_requested
) {
1188 qdev_prop_set_bit(dev
, "dma_enabled", false);
1191 object_property_add_child(OBJECT(qdev_get_machine()), TYPE_FW_CFG
,
1194 sbd
= SYS_BUS_DEVICE(dev
);
1195 sysbus_realize_and_unref(sbd
, &error_fatal
);
1196 sysbus_mmio_map(sbd
, 0, ctl_addr
);
1197 sysbus_mmio_map(sbd
, 1, data_addr
);
1201 if (s
->dma_enabled
) {
1204 sysbus_mmio_map(sbd
, 2, dma_addr
);
1210 FWCfgState
*fw_cfg_init_mem(hwaddr ctl_addr
, hwaddr data_addr
)
1212 return fw_cfg_init_mem_wide(ctl_addr
, data_addr
,
1213 fw_cfg_data_mem_ops
.valid
.max_access_size
,
1218 FWCfgState
*fw_cfg_find(void)
1220 /* Returns NULL unless there is exactly one fw_cfg device */
1221 return FW_CFG(object_resolve_path_type("", TYPE_FW_CFG
, NULL
));
1225 static void fw_cfg_class_init(ObjectClass
*klass
, void *data
)
1227 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1229 dc
->reset
= fw_cfg_reset
;
1230 dc
->vmsd
= &vmstate_fw_cfg
;
1232 device_class_set_props(dc
, fw_cfg_properties
);
1235 static const TypeInfo fw_cfg_info
= {
1236 .name
= TYPE_FW_CFG
,
1237 .parent
= TYPE_SYS_BUS_DEVICE
,
1239 .instance_size
= sizeof(FWCfgState
),
1240 .class_init
= fw_cfg_class_init
,
1243 static void fw_cfg_file_slots_allocate(FWCfgState
*s
, Error
**errp
)
1245 uint16_t file_slots_max
;
1247 if (fw_cfg_file_slots(s
) < FW_CFG_FILE_SLOTS_MIN
) {
1248 error_setg(errp
, "\"file_slots\" must be at least 0x%x",
1249 FW_CFG_FILE_SLOTS_MIN
);
1253 /* (UINT16_MAX & FW_CFG_ENTRY_MASK) is the highest inclusive selector value
1254 * that we permit. The actual (exclusive) value coming from the
1255 * configuration is (FW_CFG_FILE_FIRST + fw_cfg_file_slots(s)). */
1256 file_slots_max
= (UINT16_MAX
& FW_CFG_ENTRY_MASK
) - FW_CFG_FILE_FIRST
+ 1;
1257 if (fw_cfg_file_slots(s
) > file_slots_max
) {
1258 error_setg(errp
, "\"file_slots\" must not exceed 0x%" PRIx16
,
1263 s
->entries
[0] = g_new0(FWCfgEntry
, fw_cfg_max_entry(s
));
1264 s
->entries
[1] = g_new0(FWCfgEntry
, fw_cfg_max_entry(s
));
1265 s
->entry_order
= g_new0(int, fw_cfg_max_entry(s
));
1268 static Property fw_cfg_io_properties
[] = {
1269 DEFINE_PROP_BOOL("dma_enabled", FWCfgIoState
, parent_obj
.dma_enabled
,
1271 DEFINE_PROP_UINT16("x-file-slots", FWCfgIoState
, parent_obj
.file_slots
,
1272 FW_CFG_FILE_SLOTS_DFLT
),
1273 DEFINE_PROP_END_OF_LIST(),
1276 static void fw_cfg_io_realize(DeviceState
*dev
, Error
**errp
)
1279 FWCfgIoState
*s
= FW_CFG_IO(dev
);
1281 fw_cfg_file_slots_allocate(FW_CFG(s
), errp
);
1286 /* when using port i/o, the 8-bit data register ALWAYS overlaps
1287 * with half of the 16-bit control register. Hence, the total size
1288 * of the i/o region used is FW_CFG_CTL_SIZE */
1289 memory_region_init_io(&s
->comb_iomem
, OBJECT(s
), &fw_cfg_comb_mem_ops
,
1290 FW_CFG(s
), "fwcfg", FW_CFG_CTL_SIZE
);
1292 if (FW_CFG(s
)->dma_enabled
) {
1293 memory_region_init_io(&FW_CFG(s
)->dma_iomem
, OBJECT(s
),
1294 &fw_cfg_dma_mem_ops
, FW_CFG(s
), "fwcfg.dma",
1295 sizeof(dma_addr_t
));
1298 fw_cfg_common_realize(dev
, errp
);
1301 static void fw_cfg_io_class_init(ObjectClass
*klass
, void *data
)
1303 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1305 dc
->realize
= fw_cfg_io_realize
;
1306 device_class_set_props(dc
, fw_cfg_io_properties
);
1309 static const TypeInfo fw_cfg_io_info
= {
1310 .name
= TYPE_FW_CFG_IO
,
1311 .parent
= TYPE_FW_CFG
,
1312 .instance_size
= sizeof(FWCfgIoState
),
1313 .class_init
= fw_cfg_io_class_init
,
1317 static Property fw_cfg_mem_properties
[] = {
1318 DEFINE_PROP_UINT32("data_width", FWCfgMemState
, data_width
, -1),
1319 DEFINE_PROP_BOOL("dma_enabled", FWCfgMemState
, parent_obj
.dma_enabled
,
1321 DEFINE_PROP_UINT16("x-file-slots", FWCfgMemState
, parent_obj
.file_slots
,
1322 FW_CFG_FILE_SLOTS_DFLT
),
1323 DEFINE_PROP_END_OF_LIST(),
1326 static void fw_cfg_mem_realize(DeviceState
*dev
, Error
**errp
)
1329 FWCfgMemState
*s
= FW_CFG_MEM(dev
);
1330 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
1331 const MemoryRegionOps
*data_ops
= &fw_cfg_data_mem_ops
;
1333 fw_cfg_file_slots_allocate(FW_CFG(s
), errp
);
1338 memory_region_init_io(&s
->ctl_iomem
, OBJECT(s
), &fw_cfg_ctl_mem_ops
,
1339 FW_CFG(s
), "fwcfg.ctl", FW_CFG_CTL_SIZE
);
1340 sysbus_init_mmio(sbd
, &s
->ctl_iomem
);
1342 if (s
->data_width
> data_ops
->valid
.max_access_size
) {
1343 s
->wide_data_ops
= *data_ops
;
1345 s
->wide_data_ops
.valid
.max_access_size
= s
->data_width
;
1346 s
->wide_data_ops
.impl
.max_access_size
= s
->data_width
;
1347 data_ops
= &s
->wide_data_ops
;
1349 memory_region_init_io(&s
->data_iomem
, OBJECT(s
), data_ops
, FW_CFG(s
),
1350 "fwcfg.data", data_ops
->valid
.max_access_size
);
1351 sysbus_init_mmio(sbd
, &s
->data_iomem
);
1353 if (FW_CFG(s
)->dma_enabled
) {
1354 memory_region_init_io(&FW_CFG(s
)->dma_iomem
, OBJECT(s
),
1355 &fw_cfg_dma_mem_ops
, FW_CFG(s
), "fwcfg.dma",
1356 sizeof(dma_addr_t
));
1357 sysbus_init_mmio(sbd
, &FW_CFG(s
)->dma_iomem
);
1360 fw_cfg_common_realize(dev
, errp
);
1363 static void fw_cfg_mem_class_init(ObjectClass
*klass
, void *data
)
1365 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1367 dc
->realize
= fw_cfg_mem_realize
;
1368 device_class_set_props(dc
, fw_cfg_mem_properties
);
1371 static const TypeInfo fw_cfg_mem_info
= {
1372 .name
= TYPE_FW_CFG_MEM
,
1373 .parent
= TYPE_FW_CFG
,
1374 .instance_size
= sizeof(FWCfgMemState
),
1375 .class_init
= fw_cfg_mem_class_init
,
1378 static void fw_cfg_register_types(void)
1380 type_register_static(&fw_cfg_info
);
1381 type_register_static(&fw_cfg_io_info
);
1382 type_register_static(&fw_cfg_mem_info
);
1385 type_init(fw_cfg_register_types
)