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 "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"
44 #define FW_CFG_FILE_SLOTS_DFLT 0x20
46 /* FW_CFG_VERSION bits */
47 #define FW_CFG_VERSION 0x01
48 #define FW_CFG_VERSION_DMA 0x02
50 /* FW_CFG_DMA_CONTROL bits */
51 #define FW_CFG_DMA_CTL_ERROR 0x01
52 #define FW_CFG_DMA_CTL_READ 0x02
53 #define FW_CFG_DMA_CTL_SKIP 0x04
54 #define FW_CFG_DMA_CTL_SELECT 0x08
55 #define FW_CFG_DMA_CTL_WRITE 0x10
57 #define FW_CFG_DMA_SIGNATURE 0x51454d5520434647ULL /* "QEMU CFG" */
63 void *callback_opaque
;
64 FWCfgCallback select_cb
;
65 FWCfgWriteCallback write_cb
;
71 * @key: The uint16 selector key.
73 * Returns: The stringified name if the selector refers to a well-known
74 * numerically defined item, or NULL on key lookup failure.
76 static const char *key_name(uint16_t key
)
78 static const char *fw_cfg_wellknown_keys
[FW_CFG_FILE_FIRST
] = {
79 [FW_CFG_SIGNATURE
] = "signature",
81 [FW_CFG_UUID
] = "uuid",
82 [FW_CFG_RAM_SIZE
] = "ram_size",
83 [FW_CFG_NOGRAPHIC
] = "nographic",
84 [FW_CFG_NB_CPUS
] = "nb_cpus",
85 [FW_CFG_MACHINE_ID
] = "machine_id",
86 [FW_CFG_KERNEL_ADDR
] = "kernel_addr",
87 [FW_CFG_KERNEL_SIZE
] = "kernel_size",
88 [FW_CFG_KERNEL_CMDLINE
] = "kernel_cmdline",
89 [FW_CFG_INITRD_ADDR
] = "initrd_addr",
90 [FW_CFG_INITRD_SIZE
] = "initdr_size",
91 [FW_CFG_BOOT_DEVICE
] = "boot_device",
92 [FW_CFG_NUMA
] = "numa",
93 [FW_CFG_BOOT_MENU
] = "boot_menu",
94 [FW_CFG_MAX_CPUS
] = "max_cpus",
95 [FW_CFG_KERNEL_ENTRY
] = "kernel_entry",
96 [FW_CFG_KERNEL_DATA
] = "kernel_data",
97 [FW_CFG_INITRD_DATA
] = "initrd_data",
98 [FW_CFG_CMDLINE_ADDR
] = "cmdline_addr",
99 [FW_CFG_CMDLINE_SIZE
] = "cmdline_size",
100 [FW_CFG_CMDLINE_DATA
] = "cmdline_data",
101 [FW_CFG_SETUP_ADDR
] = "setup_addr",
102 [FW_CFG_SETUP_SIZE
] = "setup_size",
103 [FW_CFG_SETUP_DATA
] = "setup_data",
104 [FW_CFG_FILE_DIR
] = "file_dir",
107 if (key
& FW_CFG_ARCH_LOCAL
) {
108 return fw_cfg_arch_key_name(key
);
110 if (key
< FW_CFG_FILE_FIRST
) {
111 return fw_cfg_wellknown_keys
[key
];
117 static inline const char *trace_key_name(uint16_t key
)
119 const char *name
= key_name(key
);
121 return name
? name
: "unknown";
127 static char *read_splashfile(char *filename
, gsize
*file_sizep
,
133 unsigned int filehead
;
136 if (!g_file_get_contents(filename
, &content
, file_sizep
, &err
)) {
137 error_report("failed to read splash file '%s': %s",
138 filename
, err
->message
);
143 /* check file size */
144 if (*file_sizep
< 30) {
149 filehead
= lduw_le_p(content
);
150 if (filehead
== 0xd8ff) {
151 file_type
= JPG_FILE
;
152 } else if (filehead
== 0x4d42) {
153 file_type
= BMP_FILE
;
159 if (file_type
== BMP_FILE
) {
160 bmp_bpp
= lduw_le_p(&content
[28]);
167 *file_typep
= file_type
;
172 error_report("splash file '%s' format not recognized; must be JPEG "
173 "or 24 bit BMP", filename
);
178 static void fw_cfg_bootsplash(FWCfgState
*s
)
180 const char *boot_splash_filename
= NULL
;
181 const char *boot_splash_time
= NULL
;
182 char *filename
, *file_data
;
186 /* get user configuration */
187 QemuOptsList
*plist
= qemu_find_opts("boot-opts");
188 QemuOpts
*opts
= QTAILQ_FIRST(&plist
->head
);
189 boot_splash_filename
= qemu_opt_get(opts
, "splash");
190 boot_splash_time
= qemu_opt_get(opts
, "splash-time");
192 /* insert splash time if user configurated */
193 if (boot_splash_time
) {
194 int64_t bst_val
= qemu_opt_get_number(opts
, "splash-time", -1);
197 /* validate the input */
198 if (bst_val
< 0 || bst_val
> 0xffff) {
199 error_report("splash-time is invalid,"
200 "it should be a value between 0 and 65535");
203 /* use little endian format */
204 bst_le16
= cpu_to_le16(bst_val
);
205 fw_cfg_add_file(s
, "etc/boot-menu-wait",
206 g_memdup(&bst_le16
, sizeof bst_le16
), sizeof bst_le16
);
209 /* insert splash file if user configurated */
210 if (boot_splash_filename
) {
211 filename
= qemu_find_file(QEMU_FILE_TYPE_BIOS
, boot_splash_filename
);
212 if (filename
== NULL
) {
213 error_report("failed to find file '%s'", boot_splash_filename
);
217 /* loading file data */
218 file_data
= read_splashfile(filename
, &file_size
, &file_type
);
219 if (file_data
== NULL
) {
223 g_free(boot_splash_filedata
);
224 boot_splash_filedata
= (uint8_t *)file_data
;
227 if (file_type
== JPG_FILE
) {
228 fw_cfg_add_file(s
, "bootsplash.jpg",
229 boot_splash_filedata
, file_size
);
231 fw_cfg_add_file(s
, "bootsplash.bmp",
232 boot_splash_filedata
, file_size
);
238 static void fw_cfg_reboot(FWCfgState
*s
)
240 const char *reboot_timeout
= NULL
;
241 uint64_t rt_val
= -1;
244 /* get user configuration */
245 QemuOptsList
*plist
= qemu_find_opts("boot-opts");
246 QemuOpts
*opts
= QTAILQ_FIRST(&plist
->head
);
247 reboot_timeout
= qemu_opt_get(opts
, "reboot-timeout");
249 if (reboot_timeout
) {
250 rt_val
= qemu_opt_get_number(opts
, "reboot-timeout", -1);
252 /* validate the input */
253 if (rt_val
> 0xffff && rt_val
!= (uint64_t)-1) {
254 error_report("reboot timeout is invalid,"
255 "it should be a value between -1 and 65535");
260 rt_le32
= cpu_to_le32(rt_val
);
261 fw_cfg_add_file(s
, "etc/boot-fail-wait", g_memdup(&rt_le32
, 4), 4);
264 static void fw_cfg_write(FWCfgState
*s
, uint8_t value
)
266 /* nothing, write support removed in QEMU v2.4+ */
269 static inline uint16_t fw_cfg_file_slots(const FWCfgState
*s
)
271 return s
->file_slots
;
274 /* Note: this function returns an exclusive limit. */
275 static inline uint32_t fw_cfg_max_entry(const FWCfgState
*s
)
277 return FW_CFG_FILE_FIRST
+ fw_cfg_file_slots(s
);
280 static int fw_cfg_select(FWCfgState
*s
, uint16_t key
)
286 if ((key
& FW_CFG_ENTRY_MASK
) >= fw_cfg_max_entry(s
)) {
287 s
->cur_entry
= FW_CFG_INVALID
;
292 /* entry successfully selected, now run callback if present */
293 arch
= !!(key
& FW_CFG_ARCH_LOCAL
);
294 e
= &s
->entries
[arch
][key
& FW_CFG_ENTRY_MASK
];
296 e
->select_cb(e
->callback_opaque
);
300 trace_fw_cfg_select(s
, key
, trace_key_name(key
), ret
);
304 static uint64_t fw_cfg_data_read(void *opaque
, hwaddr addr
, unsigned size
)
306 FWCfgState
*s
= opaque
;
307 int arch
= !!(s
->cur_entry
& FW_CFG_ARCH_LOCAL
);
308 FWCfgEntry
*e
= (s
->cur_entry
== FW_CFG_INVALID
) ? NULL
:
309 &s
->entries
[arch
][s
->cur_entry
& FW_CFG_ENTRY_MASK
];
312 assert(size
> 0 && size
<= sizeof(value
));
313 if (s
->cur_entry
!= FW_CFG_INVALID
&& e
->data
&& s
->cur_offset
< e
->len
) {
314 /* The least significant 'size' bytes of the return value are
315 * expected to contain a string preserving portion of the item
316 * data, padded with zeros on the right in case we run out early.
317 * In technical terms, we're composing the host-endian representation
318 * of the big endian interpretation of the fw_cfg string.
321 value
= (value
<< 8) | e
->data
[s
->cur_offset
++];
322 } while (--size
&& s
->cur_offset
< e
->len
);
323 /* If size is still not zero, we *did* run out early, so continue
324 * left-shifting, to add the appropriate number of padding zeros
330 trace_fw_cfg_read(s
, value
);
334 static void fw_cfg_data_mem_write(void *opaque
, hwaddr addr
,
335 uint64_t value
, unsigned size
)
337 FWCfgState
*s
= opaque
;
341 fw_cfg_write(s
, value
>> (8 * --i
));
345 static void fw_cfg_dma_transfer(FWCfgState
*s
)
351 int read
= 0, write
= 0;
354 /* Reset the address before the next access */
355 dma_addr
= s
->dma_addr
;
358 if (dma_memory_read(s
->dma_as
, dma_addr
, &dma
, sizeof(dma
))) {
359 stl_be_dma(s
->dma_as
, dma_addr
+ offsetof(FWCfgDmaAccess
, control
),
360 FW_CFG_DMA_CTL_ERROR
);
364 dma
.address
= be64_to_cpu(dma
.address
);
365 dma
.length
= be32_to_cpu(dma
.length
);
366 dma
.control
= be32_to_cpu(dma
.control
);
368 if (dma
.control
& FW_CFG_DMA_CTL_SELECT
) {
369 fw_cfg_select(s
, dma
.control
>> 16);
372 arch
= !!(s
->cur_entry
& FW_CFG_ARCH_LOCAL
);
373 e
= (s
->cur_entry
== FW_CFG_INVALID
) ? NULL
:
374 &s
->entries
[arch
][s
->cur_entry
& FW_CFG_ENTRY_MASK
];
376 if (dma
.control
& FW_CFG_DMA_CTL_READ
) {
379 } else if (dma
.control
& FW_CFG_DMA_CTL_WRITE
) {
382 } else if (dma
.control
& FW_CFG_DMA_CTL_SKIP
) {
391 while (dma
.length
> 0 && !(dma
.control
& FW_CFG_DMA_CTL_ERROR
)) {
392 if (s
->cur_entry
== FW_CFG_INVALID
|| !e
->data
||
393 s
->cur_offset
>= e
->len
) {
396 /* If the access is not a read access, it will be a skip access,
400 if (dma_memory_set(s
->dma_as
, dma
.address
, 0, len
)) {
401 dma
.control
|= FW_CFG_DMA_CTL_ERROR
;
405 dma
.control
|= FW_CFG_DMA_CTL_ERROR
;
408 if (dma
.length
<= (e
->len
- s
->cur_offset
)) {
411 len
= (e
->len
- s
->cur_offset
);
414 /* If the access is not a read access, it will be a skip access,
418 if (dma_memory_write(s
->dma_as
, dma
.address
,
419 &e
->data
[s
->cur_offset
], len
)) {
420 dma
.control
|= FW_CFG_DMA_CTL_ERROR
;
424 if (!e
->allow_write
||
426 dma_memory_read(s
->dma_as
, dma
.address
,
427 &e
->data
[s
->cur_offset
], len
)) {
428 dma
.control
|= FW_CFG_DMA_CTL_ERROR
;
429 } else if (e
->write_cb
) {
430 e
->write_cb(e
->callback_opaque
, s
->cur_offset
, len
);
434 s
->cur_offset
+= len
;
442 stl_be_dma(s
->dma_as
, dma_addr
+ offsetof(FWCfgDmaAccess
, control
),
445 trace_fw_cfg_read(s
, 0);
448 static uint64_t fw_cfg_dma_mem_read(void *opaque
, hwaddr addr
,
451 /* Return a signature value (and handle various read sizes) */
452 return extract64(FW_CFG_DMA_SIGNATURE
, (8 - addr
- size
) * 8, size
* 8);
455 static void fw_cfg_dma_mem_write(void *opaque
, hwaddr addr
,
456 uint64_t value
, unsigned size
)
458 FWCfgState
*s
= opaque
;
462 /* FWCfgDmaAccess high address */
463 s
->dma_addr
= value
<< 32;
464 } else if (addr
== 4) {
465 /* FWCfgDmaAccess low address */
466 s
->dma_addr
|= value
;
467 fw_cfg_dma_transfer(s
);
469 } else if (size
== 8 && addr
== 0) {
471 fw_cfg_dma_transfer(s
);
475 static bool fw_cfg_dma_mem_valid(void *opaque
, hwaddr addr
,
476 unsigned size
, bool is_write
,
479 return !is_write
|| ((size
== 4 && (addr
== 0 || addr
== 4)) ||
480 (size
== 8 && addr
== 0));
483 static bool fw_cfg_data_mem_valid(void *opaque
, hwaddr addr
,
484 unsigned size
, bool is_write
,
490 static uint64_t fw_cfg_ctl_mem_read(void *opaque
, hwaddr addr
, unsigned size
)
495 static void fw_cfg_ctl_mem_write(void *opaque
, hwaddr addr
,
496 uint64_t value
, unsigned size
)
498 fw_cfg_select(opaque
, (uint16_t)value
);
501 static bool fw_cfg_ctl_mem_valid(void *opaque
, hwaddr addr
,
502 unsigned size
, bool is_write
,
505 return is_write
&& size
== 2;
508 static void fw_cfg_comb_write(void *opaque
, hwaddr addr
,
509 uint64_t value
, unsigned size
)
513 fw_cfg_write(opaque
, (uint8_t)value
);
516 fw_cfg_select(opaque
, (uint16_t)value
);
521 static bool fw_cfg_comb_valid(void *opaque
, hwaddr addr
,
522 unsigned size
, bool is_write
,
525 return (size
== 1) || (is_write
&& size
== 2);
528 static const MemoryRegionOps fw_cfg_ctl_mem_ops
= {
529 .read
= fw_cfg_ctl_mem_read
,
530 .write
= fw_cfg_ctl_mem_write
,
531 .endianness
= DEVICE_BIG_ENDIAN
,
532 .valid
.accepts
= fw_cfg_ctl_mem_valid
,
535 static const MemoryRegionOps fw_cfg_data_mem_ops
= {
536 .read
= fw_cfg_data_read
,
537 .write
= fw_cfg_data_mem_write
,
538 .endianness
= DEVICE_BIG_ENDIAN
,
540 .min_access_size
= 1,
541 .max_access_size
= 1,
542 .accepts
= fw_cfg_data_mem_valid
,
546 static const MemoryRegionOps fw_cfg_comb_mem_ops
= {
547 .read
= fw_cfg_data_read
,
548 .write
= fw_cfg_comb_write
,
549 .endianness
= DEVICE_LITTLE_ENDIAN
,
550 .valid
.accepts
= fw_cfg_comb_valid
,
553 static const MemoryRegionOps fw_cfg_dma_mem_ops
= {
554 .read
= fw_cfg_dma_mem_read
,
555 .write
= fw_cfg_dma_mem_write
,
556 .endianness
= DEVICE_BIG_ENDIAN
,
557 .valid
.accepts
= fw_cfg_dma_mem_valid
,
558 .valid
.max_access_size
= 8,
559 .impl
.max_access_size
= 8,
562 static void fw_cfg_reset(DeviceState
*d
)
564 FWCfgState
*s
= FW_CFG(d
);
566 /* we never register a read callback for FW_CFG_SIGNATURE */
567 fw_cfg_select(s
, FW_CFG_SIGNATURE
);
570 /* Save restore 32 bit int as uint16_t
571 This is a Big hack, but it is how the old state did it.
572 Or we broke compatibility in the state, or we can't use struct tm
575 static int get_uint32_as_uint16(QEMUFile
*f
, void *pv
, size_t size
,
576 const VMStateField
*field
)
579 *v
= qemu_get_be16(f
);
583 static int put_unused(QEMUFile
*f
, void *pv
, size_t size
,
584 const VMStateField
*field
, QJSON
*vmdesc
)
586 fprintf(stderr
, "uint32_as_uint16 is only used for backward compatibility.\n");
587 fprintf(stderr
, "This functions shouldn't be called.\n");
592 static const VMStateInfo vmstate_hack_uint32_as_uint16
= {
593 .name
= "int32_as_uint16",
594 .get
= get_uint32_as_uint16
,
598 #define VMSTATE_UINT16_HACK(_f, _s, _t) \
599 VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_hack_uint32_as_uint16, uint32_t)
602 static bool is_version_1(void *opaque
, int version_id
)
604 return version_id
== 1;
607 bool fw_cfg_dma_enabled(void *opaque
)
609 FWCfgState
*s
= opaque
;
611 return s
->dma_enabled
;
614 static bool fw_cfg_acpi_mr_restore(void *opaque
)
616 FWCfgState
*s
= opaque
;
619 mr_aligned
= QEMU_IS_ALIGNED(s
->table_mr_size
, qemu_real_host_page_size
) &&
620 QEMU_IS_ALIGNED(s
->linker_mr_size
, qemu_real_host_page_size
) &&
621 QEMU_IS_ALIGNED(s
->rsdp_mr_size
, qemu_real_host_page_size
);
622 return s
->acpi_mr_restore
&& !mr_aligned
;
625 static void fw_cfg_update_mr(FWCfgState
*s
, uint16_t key
, size_t size
)
629 int arch
= !!(key
& FW_CFG_ARCH_LOCAL
);
632 key
&= FW_CFG_ENTRY_MASK
;
633 assert(key
< fw_cfg_max_entry(s
));
635 ptr
= s
->entries
[arch
][key
].data
;
636 mr
= memory_region_from_host(ptr
, &offset
);
638 memory_region_ram_resize(mr
, size
, &error_abort
);
641 static int fw_cfg_acpi_mr_restore_post_load(void *opaque
, int version_id
)
643 FWCfgState
*s
= opaque
;
648 index
= be32_to_cpu(s
->files
->count
);
650 for (i
= 0; i
< index
; i
++) {
651 if (!strcmp(s
->files
->f
[i
].name
, ACPI_BUILD_TABLE_FILE
)) {
652 fw_cfg_update_mr(s
, FW_CFG_FILE_FIRST
+ i
, s
->table_mr_size
);
653 } else if (!strcmp(s
->files
->f
[i
].name
, ACPI_BUILD_LOADER_FILE
)) {
654 fw_cfg_update_mr(s
, FW_CFG_FILE_FIRST
+ i
, s
->linker_mr_size
);
655 } else if (!strcmp(s
->files
->f
[i
].name
, ACPI_BUILD_RSDP_FILE
)) {
656 fw_cfg_update_mr(s
, FW_CFG_FILE_FIRST
+ i
, s
->rsdp_mr_size
);
663 static const VMStateDescription vmstate_fw_cfg_dma
= {
664 .name
= "fw_cfg/dma",
665 .needed
= fw_cfg_dma_enabled
,
666 .fields
= (VMStateField
[]) {
667 VMSTATE_UINT64(dma_addr
, FWCfgState
),
668 VMSTATE_END_OF_LIST()
672 static const VMStateDescription vmstate_fw_cfg_acpi_mr
= {
673 .name
= "fw_cfg/acpi_mr",
675 .minimum_version_id
= 1,
676 .needed
= fw_cfg_acpi_mr_restore
,
677 .post_load
= fw_cfg_acpi_mr_restore_post_load
,
678 .fields
= (VMStateField
[]) {
679 VMSTATE_UINT64(table_mr_size
, FWCfgState
),
680 VMSTATE_UINT64(linker_mr_size
, FWCfgState
),
681 VMSTATE_UINT64(rsdp_mr_size
, FWCfgState
),
682 VMSTATE_END_OF_LIST()
686 static const VMStateDescription vmstate_fw_cfg
= {
689 .minimum_version_id
= 1,
690 .fields
= (VMStateField
[]) {
691 VMSTATE_UINT16(cur_entry
, FWCfgState
),
692 VMSTATE_UINT16_HACK(cur_offset
, FWCfgState
, is_version_1
),
693 VMSTATE_UINT32_V(cur_offset
, FWCfgState
, 2),
694 VMSTATE_END_OF_LIST()
696 .subsections
= (const VMStateDescription
*[]) {
698 &vmstate_fw_cfg_acpi_mr
,
703 static void fw_cfg_add_bytes_callback(FWCfgState
*s
, uint16_t key
,
704 FWCfgCallback select_cb
,
705 FWCfgWriteCallback write_cb
,
706 void *callback_opaque
,
707 void *data
, size_t len
,
710 int arch
= !!(key
& FW_CFG_ARCH_LOCAL
);
712 key
&= FW_CFG_ENTRY_MASK
;
714 assert(key
< fw_cfg_max_entry(s
) && len
< UINT32_MAX
);
715 assert(s
->entries
[arch
][key
].data
== NULL
); /* avoid key conflict */
717 s
->entries
[arch
][key
].data
= data
;
718 s
->entries
[arch
][key
].len
= (uint32_t)len
;
719 s
->entries
[arch
][key
].select_cb
= select_cb
;
720 s
->entries
[arch
][key
].write_cb
= write_cb
;
721 s
->entries
[arch
][key
].callback_opaque
= callback_opaque
;
722 s
->entries
[arch
][key
].allow_write
= !read_only
;
725 static void *fw_cfg_modify_bytes_read(FWCfgState
*s
, uint16_t key
,
726 void *data
, size_t len
)
729 int arch
= !!(key
& FW_CFG_ARCH_LOCAL
);
731 key
&= FW_CFG_ENTRY_MASK
;
733 assert(key
< fw_cfg_max_entry(s
) && len
< UINT32_MAX
);
735 /* return the old data to the function caller, avoid memory leak */
736 ptr
= s
->entries
[arch
][key
].data
;
737 s
->entries
[arch
][key
].data
= data
;
738 s
->entries
[arch
][key
].len
= len
;
739 s
->entries
[arch
][key
].callback_opaque
= NULL
;
740 s
->entries
[arch
][key
].allow_write
= false;
745 void fw_cfg_add_bytes(FWCfgState
*s
, uint16_t key
, void *data
, size_t len
)
747 trace_fw_cfg_add_bytes(key
, trace_key_name(key
), len
);
748 fw_cfg_add_bytes_callback(s
, key
, NULL
, NULL
, NULL
, data
, len
, true);
751 void fw_cfg_add_string(FWCfgState
*s
, uint16_t key
, const char *value
)
753 size_t sz
= strlen(value
) + 1;
755 trace_fw_cfg_add_string(key
, trace_key_name(key
), value
);
756 fw_cfg_add_bytes(s
, key
, g_memdup(value
, sz
), sz
);
759 void fw_cfg_modify_string(FWCfgState
*s
, uint16_t key
, const char *value
)
761 size_t sz
= strlen(value
) + 1;
764 old
= fw_cfg_modify_bytes_read(s
, key
, g_memdup(value
, sz
), sz
);
768 void fw_cfg_add_i16(FWCfgState
*s
, uint16_t key
, uint16_t value
)
772 copy
= g_malloc(sizeof(value
));
773 *copy
= cpu_to_le16(value
);
774 trace_fw_cfg_add_i16(key
, trace_key_name(key
), value
);
775 fw_cfg_add_bytes(s
, key
, copy
, sizeof(value
));
778 void fw_cfg_modify_i16(FWCfgState
*s
, uint16_t key
, uint16_t value
)
780 uint16_t *copy
, *old
;
782 copy
= g_malloc(sizeof(value
));
783 *copy
= cpu_to_le16(value
);
784 old
= fw_cfg_modify_bytes_read(s
, key
, copy
, sizeof(value
));
788 void fw_cfg_add_i32(FWCfgState
*s
, uint16_t key
, uint32_t value
)
792 copy
= g_malloc(sizeof(value
));
793 *copy
= cpu_to_le32(value
);
794 trace_fw_cfg_add_i32(key
, trace_key_name(key
), value
);
795 fw_cfg_add_bytes(s
, key
, copy
, sizeof(value
));
798 void fw_cfg_modify_i32(FWCfgState
*s
, uint16_t key
, uint32_t value
)
800 uint32_t *copy
, *old
;
802 copy
= g_malloc(sizeof(value
));
803 *copy
= cpu_to_le32(value
);
804 old
= fw_cfg_modify_bytes_read(s
, key
, copy
, sizeof(value
));
808 void fw_cfg_add_i64(FWCfgState
*s
, uint16_t key
, uint64_t value
)
812 copy
= g_malloc(sizeof(value
));
813 *copy
= cpu_to_le64(value
);
814 trace_fw_cfg_add_i64(key
, trace_key_name(key
), value
);
815 fw_cfg_add_bytes(s
, key
, copy
, sizeof(value
));
818 void fw_cfg_modify_i64(FWCfgState
*s
, uint16_t key
, uint64_t value
)
820 uint64_t *copy
, *old
;
822 copy
= g_malloc(sizeof(value
));
823 *copy
= cpu_to_le64(value
);
824 old
= fw_cfg_modify_bytes_read(s
, key
, copy
, sizeof(value
));
828 void fw_cfg_set_order_override(FWCfgState
*s
, int order
)
830 assert(s
->fw_cfg_order_override
== 0);
831 s
->fw_cfg_order_override
= order
;
834 void fw_cfg_reset_order_override(FWCfgState
*s
)
836 assert(s
->fw_cfg_order_override
!= 0);
837 s
->fw_cfg_order_override
= 0;
841 * This is the legacy order list. For legacy systems, files are in
842 * the fw_cfg in the order defined below, by the "order" value. Note
843 * that some entries (VGA ROMs, NIC option ROMS, etc.) go into a
844 * specific area, but there may be more than one and they occur in the
845 * order that the user specifies them on the command line. Those are
846 * handled in a special manner, using the order override above.
848 * For non-legacy, the files are sorted by filename to avoid this kind
849 * of complexity in the future.
851 * This is only for x86, other arches don't implement versioning so
852 * they won't set legacy mode.
858 { "etc/boot-menu-wait", 10 },
859 { "bootsplash.jpg", 11 },
860 { "bootsplash.bmp", 12 },
861 { "etc/boot-fail-wait", 15 },
862 { "etc/smbios/smbios-tables", 20 },
863 { "etc/smbios/smbios-anchor", 30 },
865 { "etc/reserved-memory-end", 50 },
866 { "genroms/kvmvapic.bin", 55 },
867 { "genroms/linuxboot.bin", 60 },
868 { }, /* VGA ROMs from pc_vga_init come here, 70. */
869 { }, /* NIC option ROMs from pc_nic_init come here, 80. */
870 { "etc/system-states", 90 },
871 { }, /* User ROMs come here, 100. */
872 { }, /* Device FW comes here, 110. */
873 { "etc/extra-pci-roots", 120 },
874 { "etc/acpi/tables", 130 },
875 { "etc/table-loader", 140 },
876 { "etc/tpm/log", 150 },
877 { "etc/acpi/rsdp", 160 },
878 { "bootorder", 170 },
880 #define FW_CFG_ORDER_OVERRIDE_LAST 200
884 * Any sub-page size update to these table MRs will be lost during migration,
885 * as we use aligned size in ram_load_precopy() -> qemu_ram_resize() path.
886 * In order to avoid the inconsistency in sizes save them seperately and
887 * migrate over in vmstate post_load().
889 static void fw_cfg_acpi_mr_save(FWCfgState
*s
, const char *filename
, size_t len
)
891 if (!strcmp(filename
, ACPI_BUILD_TABLE_FILE
)) {
892 s
->table_mr_size
= len
;
893 } else if (!strcmp(filename
, ACPI_BUILD_LOADER_FILE
)) {
894 s
->linker_mr_size
= len
;
895 } else if (!strcmp(filename
, ACPI_BUILD_RSDP_FILE
)) {
896 s
->rsdp_mr_size
= len
;
900 static int get_fw_cfg_order(FWCfgState
*s
, const char *name
)
904 if (s
->fw_cfg_order_override
> 0) {
905 return s
->fw_cfg_order_override
;
908 for (i
= 0; i
< ARRAY_SIZE(fw_cfg_order
); i
++) {
909 if (fw_cfg_order
[i
].name
== NULL
) {
913 if (strcmp(name
, fw_cfg_order
[i
].name
) == 0) {
914 return fw_cfg_order
[i
].order
;
918 /* Stick unknown stuff at the end. */
919 warn_report("Unknown firmware file in legacy mode: %s", name
);
920 return FW_CFG_ORDER_OVERRIDE_LAST
;
923 void fw_cfg_add_file_callback(FWCfgState
*s
, const char *filename
,
924 FWCfgCallback select_cb
,
925 FWCfgWriteCallback write_cb
,
926 void *callback_opaque
,
927 void *data
, size_t len
, bool read_only
)
931 MachineClass
*mc
= MACHINE_GET_CLASS(qdev_get_machine());
935 dsize
= sizeof(uint32_t) + sizeof(FWCfgFile
) * fw_cfg_file_slots(s
);
936 s
->files
= g_malloc0(dsize
);
937 fw_cfg_add_bytes(s
, FW_CFG_FILE_DIR
, s
->files
, dsize
);
940 count
= be32_to_cpu(s
->files
->count
);
941 assert(count
< fw_cfg_file_slots(s
));
943 /* Find the insertion point. */
944 if (mc
->legacy_fw_cfg_order
) {
946 * Sort by order. For files with the same order, we keep them
947 * in the sequence in which they were added.
949 order
= get_fw_cfg_order(s
, filename
);
951 index
> 0 && order
< s
->entry_order
[index
- 1];
954 /* Sort by file name. */
956 index
> 0 && strcmp(filename
, s
->files
->f
[index
- 1].name
) < 0;
961 * Move all the entries from the index point and after down one
962 * to create a slot for the new entry. Because calculations are
963 * being done with the index, make it so that "i" is the current
964 * index and "i - 1" is the one being copied from, thus the
965 * unusual start and end in the for statement.
967 for (i
= count
; i
> index
; i
--) {
968 s
->files
->f
[i
] = s
->files
->f
[i
- 1];
969 s
->files
->f
[i
].select
= cpu_to_be16(FW_CFG_FILE_FIRST
+ i
);
970 s
->entries
[0][FW_CFG_FILE_FIRST
+ i
] =
971 s
->entries
[0][FW_CFG_FILE_FIRST
+ i
- 1];
972 s
->entry_order
[i
] = s
->entry_order
[i
- 1];
975 memset(&s
->files
->f
[index
], 0, sizeof(FWCfgFile
));
976 memset(&s
->entries
[0][FW_CFG_FILE_FIRST
+ index
], 0, sizeof(FWCfgEntry
));
978 pstrcpy(s
->files
->f
[index
].name
, sizeof(s
->files
->f
[index
].name
), filename
);
979 for (i
= 0; i
<= count
; i
++) {
981 strcmp(s
->files
->f
[index
].name
, s
->files
->f
[i
].name
) == 0) {
982 error_report("duplicate fw_cfg file name: %s",
983 s
->files
->f
[index
].name
);
988 fw_cfg_add_bytes_callback(s
, FW_CFG_FILE_FIRST
+ index
,
990 callback_opaque
, data
, len
,
993 s
->files
->f
[index
].size
= cpu_to_be32(len
);
994 s
->files
->f
[index
].select
= cpu_to_be16(FW_CFG_FILE_FIRST
+ index
);
995 s
->entry_order
[index
] = order
;
996 trace_fw_cfg_add_file(s
, index
, s
->files
->f
[index
].name
, len
);
998 s
->files
->count
= cpu_to_be32(count
+1);
999 fw_cfg_acpi_mr_save(s
, filename
, len
);
1002 void fw_cfg_add_file(FWCfgState
*s
, const char *filename
,
1003 void *data
, size_t len
)
1005 fw_cfg_add_file_callback(s
, filename
, NULL
, NULL
, NULL
, data
, len
, true);
1008 void *fw_cfg_modify_file(FWCfgState
*s
, const char *filename
,
1009 void *data
, size_t len
)
1016 index
= be32_to_cpu(s
->files
->count
);
1018 for (i
= 0; i
< index
; i
++) {
1019 if (strcmp(filename
, s
->files
->f
[i
].name
) == 0) {
1020 ptr
= fw_cfg_modify_bytes_read(s
, FW_CFG_FILE_FIRST
+ i
,
1022 s
->files
->f
[i
].size
= cpu_to_be32(len
);
1023 fw_cfg_acpi_mr_save(s
, filename
, len
);
1028 assert(index
< fw_cfg_file_slots(s
));
1031 fw_cfg_add_file_callback(s
, filename
, NULL
, NULL
, NULL
, data
, len
, true);
1035 bool fw_cfg_add_from_generator(FWCfgState
*s
, const char *filename
,
1036 const char *gen_id
, Error
**errp
)
1038 FWCfgDataGeneratorClass
*klass
;
1043 obj
= object_resolve_path_component(object_get_objects_root(), gen_id
);
1045 error_setg(errp
, "Cannot find object ID '%s'", gen_id
);
1048 if (!object_dynamic_cast(obj
, TYPE_FW_CFG_DATA_GENERATOR_INTERFACE
)) {
1049 error_setg(errp
, "Object ID '%s' is not a '%s' subclass",
1050 gen_id
, TYPE_FW_CFG_DATA_GENERATOR_INTERFACE
);
1053 klass
= FW_CFG_DATA_GENERATOR_GET_CLASS(obj
);
1054 array
= klass
->get_data(obj
, errp
);
1059 fw_cfg_add_file(s
, filename
, g_byte_array_free(array
, TRUE
), size
);
1064 static void fw_cfg_machine_reset(void *opaque
)
1066 MachineClass
*mc
= MACHINE_GET_CLASS(qdev_get_machine());
1067 FWCfgState
*s
= opaque
;
1072 buf
= get_boot_devices_list(&len
);
1073 ptr
= fw_cfg_modify_file(s
, "bootorder", (uint8_t *)buf
, len
);
1076 if (!mc
->legacy_fw_cfg_order
) {
1077 buf
= get_boot_devices_lchs_list(&len
);
1078 ptr
= fw_cfg_modify_file(s
, "bios-geometry", (uint8_t *)buf
, len
);
1083 static void fw_cfg_machine_ready(struct Notifier
*n
, void *data
)
1085 FWCfgState
*s
= container_of(n
, FWCfgState
, machine_ready
);
1086 qemu_register_reset(fw_cfg_machine_reset
, s
);
1089 static Property fw_cfg_properties
[] = {
1090 DEFINE_PROP_BOOL("acpi-mr-restore", FWCfgState
, acpi_mr_restore
, true),
1091 DEFINE_PROP_END_OF_LIST(),
1094 static void fw_cfg_common_realize(DeviceState
*dev
, Error
**errp
)
1096 FWCfgState
*s
= FW_CFG(dev
);
1097 MachineState
*machine
= MACHINE(qdev_get_machine());
1098 uint32_t version
= FW_CFG_VERSION
;
1100 if (!fw_cfg_find()) {
1101 error_setg(errp
, "at most one %s device is permitted", TYPE_FW_CFG
);
1105 fw_cfg_add_bytes(s
, FW_CFG_SIGNATURE
, (char *)"QEMU", 4);
1106 fw_cfg_add_bytes(s
, FW_CFG_UUID
, &qemu_uuid
, 16);
1107 fw_cfg_add_i16(s
, FW_CFG_NOGRAPHIC
, (uint16_t)!machine
->enable_graphics
);
1108 fw_cfg_add_i16(s
, FW_CFG_BOOT_MENU
, (uint16_t)boot_menu
);
1109 fw_cfg_bootsplash(s
);
1112 if (s
->dma_enabled
) {
1113 version
|= FW_CFG_VERSION_DMA
;
1116 fw_cfg_add_i32(s
, FW_CFG_ID
, version
);
1118 s
->machine_ready
.notify
= fw_cfg_machine_ready
;
1119 qemu_add_machine_init_done_notifier(&s
->machine_ready
);
1122 FWCfgState
*fw_cfg_init_io_dma(uint32_t iobase
, uint32_t dma_iobase
,
1123 AddressSpace
*dma_as
)
1129 bool dma_requested
= dma_iobase
&& dma_as
;
1131 dev
= qdev_new(TYPE_FW_CFG_IO
);
1132 if (!dma_requested
) {
1133 qdev_prop_set_bit(dev
, "dma_enabled", false);
1136 object_property_add_child(OBJECT(qdev_get_machine()), TYPE_FW_CFG
,
1139 sbd
= SYS_BUS_DEVICE(dev
);
1140 sysbus_realize_and_unref(sbd
, &error_fatal
);
1141 ios
= FW_CFG_IO(dev
);
1142 sysbus_add_io(sbd
, iobase
, &ios
->comb_iomem
);
1146 if (s
->dma_enabled
) {
1147 /* 64 bits for the address field */
1150 sysbus_add_io(sbd
, dma_iobase
, &s
->dma_iomem
);
1156 FWCfgState
*fw_cfg_init_io(uint32_t iobase
)
1158 return fw_cfg_init_io_dma(iobase
, 0, NULL
);
1161 FWCfgState
*fw_cfg_init_mem_wide(hwaddr ctl_addr
,
1162 hwaddr data_addr
, uint32_t data_width
,
1163 hwaddr dma_addr
, AddressSpace
*dma_as
)
1168 bool dma_requested
= dma_addr
&& dma_as
;
1170 dev
= qdev_new(TYPE_FW_CFG_MEM
);
1171 qdev_prop_set_uint32(dev
, "data_width", data_width
);
1172 if (!dma_requested
) {
1173 qdev_prop_set_bit(dev
, "dma_enabled", false);
1176 object_property_add_child(OBJECT(qdev_get_machine()), TYPE_FW_CFG
,
1179 sbd
= SYS_BUS_DEVICE(dev
);
1180 sysbus_realize_and_unref(sbd
, &error_fatal
);
1181 sysbus_mmio_map(sbd
, 0, ctl_addr
);
1182 sysbus_mmio_map(sbd
, 1, data_addr
);
1186 if (s
->dma_enabled
) {
1189 sysbus_mmio_map(sbd
, 2, dma_addr
);
1195 FWCfgState
*fw_cfg_init_mem(hwaddr ctl_addr
, hwaddr data_addr
)
1197 return fw_cfg_init_mem_wide(ctl_addr
, data_addr
,
1198 fw_cfg_data_mem_ops
.valid
.max_access_size
,
1203 FWCfgState
*fw_cfg_find(void)
1205 /* Returns NULL unless there is exactly one fw_cfg device */
1206 return FW_CFG(object_resolve_path_type("", TYPE_FW_CFG
, NULL
));
1210 static void fw_cfg_class_init(ObjectClass
*klass
, void *data
)
1212 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1214 dc
->reset
= fw_cfg_reset
;
1215 dc
->vmsd
= &vmstate_fw_cfg
;
1217 device_class_set_props(dc
, fw_cfg_properties
);
1220 static const TypeInfo fw_cfg_info
= {
1221 .name
= TYPE_FW_CFG
,
1222 .parent
= TYPE_SYS_BUS_DEVICE
,
1224 .instance_size
= sizeof(FWCfgState
),
1225 .class_init
= fw_cfg_class_init
,
1228 static void fw_cfg_file_slots_allocate(FWCfgState
*s
, Error
**errp
)
1230 uint16_t file_slots_max
;
1232 if (fw_cfg_file_slots(s
) < FW_CFG_FILE_SLOTS_MIN
) {
1233 error_setg(errp
, "\"file_slots\" must be at least 0x%x",
1234 FW_CFG_FILE_SLOTS_MIN
);
1238 /* (UINT16_MAX & FW_CFG_ENTRY_MASK) is the highest inclusive selector value
1239 * that we permit. The actual (exclusive) value coming from the
1240 * configuration is (FW_CFG_FILE_FIRST + fw_cfg_file_slots(s)). */
1241 file_slots_max
= (UINT16_MAX
& FW_CFG_ENTRY_MASK
) - FW_CFG_FILE_FIRST
+ 1;
1242 if (fw_cfg_file_slots(s
) > file_slots_max
) {
1243 error_setg(errp
, "\"file_slots\" must not exceed 0x%" PRIx16
,
1248 s
->entries
[0] = g_new0(FWCfgEntry
, fw_cfg_max_entry(s
));
1249 s
->entries
[1] = g_new0(FWCfgEntry
, fw_cfg_max_entry(s
));
1250 s
->entry_order
= g_new0(int, fw_cfg_max_entry(s
));
1253 static Property fw_cfg_io_properties
[] = {
1254 DEFINE_PROP_BOOL("dma_enabled", FWCfgIoState
, parent_obj
.dma_enabled
,
1256 DEFINE_PROP_UINT16("x-file-slots", FWCfgIoState
, parent_obj
.file_slots
,
1257 FW_CFG_FILE_SLOTS_DFLT
),
1258 DEFINE_PROP_END_OF_LIST(),
1261 static void fw_cfg_io_realize(DeviceState
*dev
, Error
**errp
)
1264 FWCfgIoState
*s
= FW_CFG_IO(dev
);
1266 fw_cfg_file_slots_allocate(FW_CFG(s
), errp
);
1271 /* when using port i/o, the 8-bit data register ALWAYS overlaps
1272 * with half of the 16-bit control register. Hence, the total size
1273 * of the i/o region used is FW_CFG_CTL_SIZE */
1274 memory_region_init_io(&s
->comb_iomem
, OBJECT(s
), &fw_cfg_comb_mem_ops
,
1275 FW_CFG(s
), "fwcfg", FW_CFG_CTL_SIZE
);
1277 if (FW_CFG(s
)->dma_enabled
) {
1278 memory_region_init_io(&FW_CFG(s
)->dma_iomem
, OBJECT(s
),
1279 &fw_cfg_dma_mem_ops
, FW_CFG(s
), "fwcfg.dma",
1280 sizeof(dma_addr_t
));
1283 fw_cfg_common_realize(dev
, errp
);
1286 static void fw_cfg_io_class_init(ObjectClass
*klass
, void *data
)
1288 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1290 dc
->realize
= fw_cfg_io_realize
;
1291 device_class_set_props(dc
, fw_cfg_io_properties
);
1294 static const TypeInfo fw_cfg_io_info
= {
1295 .name
= TYPE_FW_CFG_IO
,
1296 .parent
= TYPE_FW_CFG
,
1297 .instance_size
= sizeof(FWCfgIoState
),
1298 .class_init
= fw_cfg_io_class_init
,
1302 static Property fw_cfg_mem_properties
[] = {
1303 DEFINE_PROP_UINT32("data_width", FWCfgMemState
, data_width
, -1),
1304 DEFINE_PROP_BOOL("dma_enabled", FWCfgMemState
, parent_obj
.dma_enabled
,
1306 DEFINE_PROP_UINT16("x-file-slots", FWCfgMemState
, parent_obj
.file_slots
,
1307 FW_CFG_FILE_SLOTS_DFLT
),
1308 DEFINE_PROP_END_OF_LIST(),
1311 static void fw_cfg_mem_realize(DeviceState
*dev
, Error
**errp
)
1314 FWCfgMemState
*s
= FW_CFG_MEM(dev
);
1315 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
1316 const MemoryRegionOps
*data_ops
= &fw_cfg_data_mem_ops
;
1318 fw_cfg_file_slots_allocate(FW_CFG(s
), errp
);
1323 memory_region_init_io(&s
->ctl_iomem
, OBJECT(s
), &fw_cfg_ctl_mem_ops
,
1324 FW_CFG(s
), "fwcfg.ctl", FW_CFG_CTL_SIZE
);
1325 sysbus_init_mmio(sbd
, &s
->ctl_iomem
);
1327 if (s
->data_width
> data_ops
->valid
.max_access_size
) {
1328 s
->wide_data_ops
= *data_ops
;
1330 s
->wide_data_ops
.valid
.max_access_size
= s
->data_width
;
1331 s
->wide_data_ops
.impl
.max_access_size
= s
->data_width
;
1332 data_ops
= &s
->wide_data_ops
;
1334 memory_region_init_io(&s
->data_iomem
, OBJECT(s
), data_ops
, FW_CFG(s
),
1335 "fwcfg.data", data_ops
->valid
.max_access_size
);
1336 sysbus_init_mmio(sbd
, &s
->data_iomem
);
1338 if (FW_CFG(s
)->dma_enabled
) {
1339 memory_region_init_io(&FW_CFG(s
)->dma_iomem
, OBJECT(s
),
1340 &fw_cfg_dma_mem_ops
, FW_CFG(s
), "fwcfg.dma",
1341 sizeof(dma_addr_t
));
1342 sysbus_init_mmio(sbd
, &FW_CFG(s
)->dma_iomem
);
1345 fw_cfg_common_realize(dev
, errp
);
1348 static void fw_cfg_mem_class_init(ObjectClass
*klass
, void *data
)
1350 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1352 dc
->realize
= fw_cfg_mem_realize
;
1353 device_class_set_props(dc
, fw_cfg_mem_properties
);
1356 static const TypeInfo fw_cfg_mem_info
= {
1357 .name
= TYPE_FW_CFG_MEM
,
1358 .parent
= TYPE_FW_CFG
,
1359 .instance_size
= sizeof(FWCfgMemState
),
1360 .class_init
= fw_cfg_mem_class_init
,
1363 static const TypeInfo fw_cfg_data_generator_interface_info
= {
1364 .parent
= TYPE_INTERFACE
,
1365 .name
= TYPE_FW_CFG_DATA_GENERATOR_INTERFACE
,
1366 .class_size
= sizeof(FWCfgDataGeneratorClass
),
1369 static void fw_cfg_register_types(void)
1371 type_register_static(&fw_cfg_info
);
1372 type_register_static(&fw_cfg_io_info
);
1373 type_register_static(&fw_cfg_mem_info
);
1374 type_register_static(&fw_cfg_data_generator_interface_info
);
1377 type_init(fw_cfg_register_types
)