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 "exec/address-spaces.h"
32
#include "hw/nvram/fw_cfg.h"
33
#include "hw/qdev-properties.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"
47
#define FW_CFG_FILE_SLOTS_DFLT 0x20
50
#define FW_CFG_VERSION 0x01
51
#define FW_CFG_VERSION_DMA 0x02
54
#define FW_CFG_DMA_CTL_ERROR 0x01
55
#define FW_CFG_DMA_CTL_READ 0x02
56
#define FW_CFG_DMA_CTL_SKIP 0x04
57
#define FW_CFG_DMA_CTL_SELECT 0x08
58
#define FW_CFG_DMA_CTL_WRITE 0x10
60
#define FW_CFG_DMA_SIGNATURE 0x51454d5520434647ULL
66
void *callback_opaque;
67
FWCfgCallback select_cb;
68
FWCfgWriteCallback write_cb;
79
static const char *key_name(uint16_t key)
81
static const char *fw_cfg_wellknown_keys[FW_CFG_FILE_FIRST] = {
82
[FW_CFG_SIGNATURE] = "signature",
84
[FW_CFG_UUID] = "uuid",
85
[FW_CFG_RAM_SIZE] = "ram_size",
86
[FW_CFG_NOGRAPHIC] = "nographic",
87
[FW_CFG_NB_CPUS] = "nb_cpus",
88
[FW_CFG_MACHINE_ID] = "machine_id",
89
[FW_CFG_KERNEL_ADDR] = "kernel_addr",
90
[FW_CFG_KERNEL_SIZE] = "kernel_size",
91
[FW_CFG_KERNEL_CMDLINE] = "kernel_cmdline",
92
[FW_CFG_INITRD_ADDR] = "initrd_addr",
93
[FW_CFG_INITRD_SIZE] = "initdr_size",
94
[FW_CFG_BOOT_DEVICE] = "boot_device",
95
[FW_CFG_NUMA] = "numa",
96
[FW_CFG_BOOT_MENU] = "boot_menu",
97
[FW_CFG_MAX_CPUS] = "max_cpus",
98
[FW_CFG_KERNEL_ENTRY] = "kernel_entry",
99
[FW_CFG_KERNEL_DATA] = "kernel_data",
100
[FW_CFG_INITRD_DATA] = "initrd_data",
101
[FW_CFG_CMDLINE_ADDR] = "cmdline_addr",
102
[FW_CFG_CMDLINE_SIZE] = "cmdline_size",
103
[FW_CFG_CMDLINE_DATA] = "cmdline_data",
104
[FW_CFG_SETUP_ADDR] = "setup_addr",
105
[FW_CFG_SETUP_SIZE] = "setup_size",
106
[FW_CFG_SETUP_DATA] = "setup_data",
107
[FW_CFG_FILE_DIR] = "file_dir",
110
if (key & FW_CFG_ARCH_LOCAL) {
111
return fw_cfg_arch_key_name(key);
113
if (key < FW_CFG_FILE_FIRST) {
114
return fw_cfg_wellknown_keys[key];
120
static inline const char *trace_key_name(uint16_t key)
122
const char *name = key_name(key);
124
return name ? name : "unknown";
130
static char *read_splashfile(char *filename, gsize *file_sizep,
136
unsigned int filehead;
139
if (!g_file_get_contents(filename, &content, file_sizep, &err)) {
140
error_report("failed to read splash file '%s': %s",
141
filename, err->message);
147
if (*file_sizep < 30) {
152
filehead = lduw_le_p(content);
153
if (filehead == 0xd8ff) {
154
file_type = JPG_FILE;
155
} else if (filehead == 0x4d42) {
156
file_type = BMP_FILE;
162
if (file_type == BMP_FILE) {
163
bmp_bpp = lduw_le_p(&content[28]);
170
*file_typep = file_type;
175
error_report("splash file '%s' format not recognized; must be JPEG "
176
"or 24 bit BMP", filename);
181
static void fw_cfg_bootsplash(FWCfgState *s)
183
char *filename, *file_data;
188
if (current_machine->boot_config.has_splash_time) {
189
int64_t bst_val = current_machine->boot_config.splash_time;
193
if (bst_val < 0 || bst_val > 0xffff) {
194
error_report("splash-time is invalid,"
195
"it should be a value between 0 and 65535");
199
bst_le16 = cpu_to_le16(bst_val);
200
fw_cfg_add_file(s, "etc/boot-menu-wait",
201
g_memdup(&bst_le16, sizeof bst_le16), sizeof bst_le16);
205
if (current_machine->boot_config.splash) {
206
const char *boot_splash_filename = current_machine->boot_config.splash;
207
filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, boot_splash_filename);
208
if (filename == NULL) {
209
error_report("failed to find file '%s'", boot_splash_filename);
214
file_data = read_splashfile(filename, &file_size, &file_type);
215
if (file_data == NULL) {
219
g_free(boot_splash_filedata);
220
boot_splash_filedata = (uint8_t *)file_data;
223
if (file_type == JPG_FILE) {
224
fw_cfg_add_file(s, "bootsplash.jpg",
225
boot_splash_filedata, file_size);
227
fw_cfg_add_file(s, "bootsplash.bmp",
228
boot_splash_filedata, file_size);
234
static void fw_cfg_reboot(FWCfgState *s)
236
uint64_t rt_val = -1;
239
if (current_machine->boot_config.has_reboot_timeout) {
240
rt_val = current_machine->boot_config.reboot_timeout;
243
if (rt_val > 0xffff && rt_val != (uint64_t)-1) {
244
error_report("reboot timeout is invalid,"
245
"it should be a value between -1 and 65535");
250
rt_le32 = cpu_to_le32(rt_val);
251
fw_cfg_add_file(s, "etc/boot-fail-wait", g_memdup(&rt_le32, 4), 4);
254
static void fw_cfg_write(FWCfgState *s, uint8_t value)
259
static inline uint16_t fw_cfg_file_slots(const FWCfgState *s)
261
return s->file_slots;
265
static inline uint32_t fw_cfg_max_entry(const FWCfgState *s)
267
return FW_CFG_FILE_FIRST + fw_cfg_file_slots(s);
270
static int fw_cfg_select(FWCfgState *s, uint16_t key)
276
if ((key & FW_CFG_ENTRY_MASK) >= fw_cfg_max_entry(s)) {
277
s->cur_entry = FW_CFG_INVALID;
283
arch = !!(key & FW_CFG_ARCH_LOCAL);
284
e = &s->entries[arch][key & FW_CFG_ENTRY_MASK];
286
e->select_cb(e->callback_opaque);
290
trace_fw_cfg_select(s, key, trace_key_name(key), ret);
294
static uint64_t fw_cfg_data_read(void *opaque, hwaddr addr, unsigned size)
296
FWCfgState *s = opaque;
297
int arch = !!(s->cur_entry & FW_CFG_ARCH_LOCAL);
298
FWCfgEntry *e = (s->cur_entry == FW_CFG_INVALID) ? NULL :
299
&s->entries[arch][s->cur_entry & FW_CFG_ENTRY_MASK];
302
assert(size > 0 && size <= sizeof(value));
303
if (s->cur_entry != FW_CFG_INVALID && e->data && s->cur_offset < e->len) {
311
value = (value << 8) | e->data[s->cur_offset++];
312
} while (--size && s->cur_offset < e->len);
320
trace_fw_cfg_read(s, value);
324
static void fw_cfg_data_mem_write(void *opaque, hwaddr addr,
325
uint64_t value, unsigned size)
327
FWCfgState *s = opaque;
331
fw_cfg_write(s, value >> (8 * --i));
335
static void fw_cfg_dma_transfer(FWCfgState *s)
341
int read = 0, write = 0;
345
dma_addr = s->dma_addr;
348
if (dma_memory_read(s->dma_as, dma_addr,
349
&dma, sizeof(dma), MEMTXATTRS_UNSPECIFIED)) {
350
stl_be_dma(s->dma_as, dma_addr + offsetof(FWCfgDmaAccess, control),
351
FW_CFG_DMA_CTL_ERROR, MEMTXATTRS_UNSPECIFIED);
355
dma.address = be64_to_cpu(dma.address);
356
dma.length = be32_to_cpu(dma.length);
357
dma.control = be32_to_cpu(dma.control);
359
if (dma.control & FW_CFG_DMA_CTL_SELECT) {
360
fw_cfg_select(s, dma.control >> 16);
363
arch = !!(s->cur_entry & FW_CFG_ARCH_LOCAL);
364
e = (s->cur_entry == FW_CFG_INVALID) ? NULL :
365
&s->entries[arch][s->cur_entry & FW_CFG_ENTRY_MASK];
367
if (dma.control & FW_CFG_DMA_CTL_READ) {
370
} else if (dma.control & FW_CFG_DMA_CTL_WRITE) {
373
} else if (dma.control & FW_CFG_DMA_CTL_SKIP) {
382
while (dma.length > 0 && !(dma.control & FW_CFG_DMA_CTL_ERROR)) {
383
if (s->cur_entry == FW_CFG_INVALID || !e->data ||
384
s->cur_offset >= e->len) {
391
if (dma_memory_set(s->dma_as, dma.address, 0, len,
392
MEMTXATTRS_UNSPECIFIED)) {
393
dma.control |= FW_CFG_DMA_CTL_ERROR;
397
dma.control |= FW_CFG_DMA_CTL_ERROR;
400
if (dma.length <= (e->len - s->cur_offset)) {
403
len = (e->len - s->cur_offset);
410
if (dma_memory_write(s->dma_as, dma.address,
411
&e->data[s->cur_offset], len,
412
MEMTXATTRS_UNSPECIFIED)) {
413
dma.control |= FW_CFG_DMA_CTL_ERROR;
417
if (!e->allow_write ||
419
dma_memory_read(s->dma_as, dma.address,
420
&e->data[s->cur_offset], len,
421
MEMTXATTRS_UNSPECIFIED)) {
422
dma.control |= FW_CFG_DMA_CTL_ERROR;
423
} else if (e->write_cb) {
424
e->write_cb(e->callback_opaque, s->cur_offset, len);
428
s->cur_offset += len;
436
stl_be_dma(s->dma_as, dma_addr + offsetof(FWCfgDmaAccess, control),
437
dma.control, MEMTXATTRS_UNSPECIFIED);
439
trace_fw_cfg_read(s, 0);
442
static uint64_t fw_cfg_dma_mem_read(void *opaque, hwaddr addr,
446
return extract64(FW_CFG_DMA_SIGNATURE, (8 - addr - size) * 8, size * 8);
449
static void fw_cfg_dma_mem_write(void *opaque, hwaddr addr,
450
uint64_t value, unsigned size)
452
FWCfgState *s = opaque;
457
s->dma_addr = value << 32;
458
} else if (addr == 4) {
460
s->dma_addr |= value;
461
fw_cfg_dma_transfer(s);
463
} else if (size == 8 && addr == 0) {
465
fw_cfg_dma_transfer(s);
469
static bool fw_cfg_dma_mem_valid(void *opaque, hwaddr addr,
470
unsigned size, bool is_write,
473
return !is_write || ((size == 4 && (addr == 0 || addr == 4)) ||
474
(size == 8 && addr == 0));
477
static bool fw_cfg_data_mem_valid(void *opaque, hwaddr addr,
478
unsigned size, bool is_write,
484
static uint64_t fw_cfg_ctl_mem_read(void *opaque, hwaddr addr, unsigned size)
489
static void fw_cfg_ctl_mem_write(void *opaque, hwaddr addr,
490
uint64_t value, unsigned size)
492
fw_cfg_select(opaque, (uint16_t)value);
495
static bool fw_cfg_ctl_mem_valid(void *opaque, hwaddr addr,
496
unsigned size, bool is_write,
499
return is_write && size == 2;
502
static void fw_cfg_comb_write(void *opaque, hwaddr addr,
503
uint64_t value, unsigned size)
507
fw_cfg_write(opaque, (uint8_t)value);
510
fw_cfg_select(opaque, (uint16_t)value);
515
static bool fw_cfg_comb_valid(void *opaque, hwaddr addr,
516
unsigned size, bool is_write,
519
return (size == 1) || (is_write && size == 2);
522
static const MemoryRegionOps fw_cfg_ctl_mem_ops = {
523
.read = fw_cfg_ctl_mem_read,
524
.write = fw_cfg_ctl_mem_write,
525
.endianness = DEVICE_BIG_ENDIAN,
526
.valid.accepts = fw_cfg_ctl_mem_valid,
529
static const MemoryRegionOps fw_cfg_data_mem_ops = {
530
.read = fw_cfg_data_read,
531
.write = fw_cfg_data_mem_write,
532
.endianness = DEVICE_BIG_ENDIAN,
534
.min_access_size = 1,
535
.max_access_size = 1,
536
.accepts = fw_cfg_data_mem_valid,
540
static const MemoryRegionOps fw_cfg_comb_mem_ops = {
541
.read = fw_cfg_data_read,
542
.write = fw_cfg_comb_write,
543
.endianness = DEVICE_LITTLE_ENDIAN,
544
.valid.accepts = fw_cfg_comb_valid,
547
static const MemoryRegionOps fw_cfg_dma_mem_ops = {
548
.read = fw_cfg_dma_mem_read,
549
.write = fw_cfg_dma_mem_write,
550
.endianness = DEVICE_BIG_ENDIAN,
551
.valid.accepts = fw_cfg_dma_mem_valid,
552
.valid.max_access_size = 8,
553
.impl.max_access_size = 8,
556
static void fw_cfg_reset(DeviceState *d)
558
FWCfgState *s = FW_CFG(d);
561
fw_cfg_select(s, FW_CFG_SIGNATURE);
569
static int get_uint32_as_uint16(QEMUFile *f, void *pv, size_t size,
570
const VMStateField *field)
573
*v = qemu_get_be16(f);
577
static int put_unused(QEMUFile *f, void *pv, size_t size,
578
const VMStateField *field, JSONWriter *vmdesc)
580
fprintf(stderr, "uint32_as_uint16 is only used for backward compatibility.\n");
581
fprintf(stderr, "This functions shouldn't be called.\n");
586
static const VMStateInfo vmstate_hack_uint32_as_uint16 = {
587
.name = "int32_as_uint16",
588
.get = get_uint32_as_uint16,
592
#define VMSTATE_UINT16_HACK(_f, _s, _t) \
593
VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_hack_uint32_as_uint16, uint32_t)
596
static bool is_version_1(void *opaque, int version_id)
598
return version_id == 1;
601
bool fw_cfg_dma_enabled(void *opaque)
603
FWCfgState *s = opaque;
605
return s->dma_enabled;
608
static bool fw_cfg_acpi_mr_restore(void *opaque)
610
FWCfgState *s = opaque;
613
mr_aligned = QEMU_IS_ALIGNED(s->table_mr_size, qemu_real_host_page_size()) &&
614
QEMU_IS_ALIGNED(s->linker_mr_size, qemu_real_host_page_size()) &&
615
QEMU_IS_ALIGNED(s->rsdp_mr_size, qemu_real_host_page_size());
616
return s->acpi_mr_restore && !mr_aligned;
619
static void fw_cfg_update_mr(FWCfgState *s, uint16_t key, size_t size)
623
int arch = !!(key & FW_CFG_ARCH_LOCAL);
626
key &= FW_CFG_ENTRY_MASK;
627
assert(key < fw_cfg_max_entry(s));
629
ptr = s->entries[arch][key].data;
630
mr = memory_region_from_host(ptr, &offset);
632
memory_region_ram_resize(mr, size, &error_abort);
635
static int fw_cfg_acpi_mr_restore_post_load(void *opaque, int version_id)
637
FWCfgState *s = opaque;
642
index = be32_to_cpu(s->files->count);
644
for (i = 0; i < index; i++) {
645
if (!strcmp(s->files->f[i].name, ACPI_BUILD_TABLE_FILE)) {
646
fw_cfg_update_mr(s, FW_CFG_FILE_FIRST + i, s->table_mr_size);
647
} else if (!strcmp(s->files->f[i].name, ACPI_BUILD_LOADER_FILE)) {
648
fw_cfg_update_mr(s, FW_CFG_FILE_FIRST + i, s->linker_mr_size);
649
} else if (!strcmp(s->files->f[i].name, ACPI_BUILD_RSDP_FILE)) {
650
fw_cfg_update_mr(s, FW_CFG_FILE_FIRST + i, s->rsdp_mr_size);
657
static const VMStateDescription vmstate_fw_cfg_dma = {
658
.name = "fw_cfg/dma",
659
.needed = fw_cfg_dma_enabled,
660
.fields = (const VMStateField[]) {
661
VMSTATE_UINT64(dma_addr, FWCfgState),
662
VMSTATE_END_OF_LIST()
666
static const VMStateDescription vmstate_fw_cfg_acpi_mr = {
667
.name = "fw_cfg/acpi_mr",
669
.minimum_version_id = 1,
670
.needed = fw_cfg_acpi_mr_restore,
671
.post_load = fw_cfg_acpi_mr_restore_post_load,
672
.fields = (const VMStateField[]) {
673
VMSTATE_UINT64(table_mr_size, FWCfgState),
674
VMSTATE_UINT64(linker_mr_size, FWCfgState),
675
VMSTATE_UINT64(rsdp_mr_size, FWCfgState),
676
VMSTATE_END_OF_LIST()
680
static const VMStateDescription vmstate_fw_cfg = {
683
.minimum_version_id = 1,
684
.fields = (const VMStateField[]) {
685
VMSTATE_UINT16(cur_entry, FWCfgState),
686
VMSTATE_UINT16_HACK(cur_offset, FWCfgState, is_version_1),
687
VMSTATE_UINT32_V(cur_offset, FWCfgState, 2),
688
VMSTATE_END_OF_LIST()
690
.subsections = (const VMStateDescription * const []) {
692
&vmstate_fw_cfg_acpi_mr,
697
static void fw_cfg_add_bytes_callback(FWCfgState *s, uint16_t key,
698
FWCfgCallback select_cb,
699
FWCfgWriteCallback write_cb,
700
void *callback_opaque,
701
void *data, size_t len,
704
int arch = !!(key & FW_CFG_ARCH_LOCAL);
706
key &= FW_CFG_ENTRY_MASK;
708
assert(key < fw_cfg_max_entry(s) && len < UINT32_MAX);
709
assert(s->entries[arch][key].data == NULL);
711
s->entries[arch][key].data = data;
712
s->entries[arch][key].len = (uint32_t)len;
713
s->entries[arch][key].select_cb = select_cb;
714
s->entries[arch][key].write_cb = write_cb;
715
s->entries[arch][key].callback_opaque = callback_opaque;
716
s->entries[arch][key].allow_write = !read_only;
719
static void *fw_cfg_modify_bytes_read(FWCfgState *s, uint16_t key,
720
void *data, size_t len)
723
int arch = !!(key & FW_CFG_ARCH_LOCAL);
725
key &= FW_CFG_ENTRY_MASK;
727
assert(key < fw_cfg_max_entry(s) && len < UINT32_MAX);
730
ptr = s->entries[arch][key].data;
731
s->entries[arch][key].data = data;
732
s->entries[arch][key].len = len;
733
s->entries[arch][key].callback_opaque = NULL;
734
s->entries[arch][key].allow_write = false;
739
void fw_cfg_add_bytes(FWCfgState *s, uint16_t key, void *data, size_t len)
741
trace_fw_cfg_add_bytes(key, trace_key_name(key), len);
742
fw_cfg_add_bytes_callback(s, key, NULL, NULL, NULL, data, len, true);
745
void fw_cfg_add_string(FWCfgState *s, uint16_t key, const char *value)
747
size_t sz = strlen(value) + 1;
749
trace_fw_cfg_add_string(key, trace_key_name(key), value);
750
fw_cfg_add_bytes(s, key, g_memdup(value, sz), sz);
753
void fw_cfg_modify_string(FWCfgState *s, uint16_t key, const char *value)
755
size_t sz = strlen(value) + 1;
758
old = fw_cfg_modify_bytes_read(s, key, g_memdup(value, sz), sz);
762
void fw_cfg_add_i16(FWCfgState *s, uint16_t key, uint16_t value)
766
copy = g_malloc(sizeof(value));
767
*copy = cpu_to_le16(value);
768
trace_fw_cfg_add_i16(key, trace_key_name(key), value);
769
fw_cfg_add_bytes(s, key, copy, sizeof(value));
772
void fw_cfg_modify_i16(FWCfgState *s, uint16_t key, uint16_t value)
774
uint16_t *copy, *old;
776
copy = g_malloc(sizeof(value));
777
*copy = cpu_to_le16(value);
778
old = fw_cfg_modify_bytes_read(s, key, copy, sizeof(value));
782
void fw_cfg_add_i32(FWCfgState *s, uint16_t key, uint32_t value)
786
copy = g_malloc(sizeof(value));
787
*copy = cpu_to_le32(value);
788
trace_fw_cfg_add_i32(key, trace_key_name(key), value);
789
fw_cfg_add_bytes(s, key, copy, sizeof(value));
792
void fw_cfg_modify_i32(FWCfgState *s, uint16_t key, uint32_t value)
794
uint32_t *copy, *old;
796
copy = g_malloc(sizeof(value));
797
*copy = cpu_to_le32(value);
798
old = fw_cfg_modify_bytes_read(s, key, copy, sizeof(value));
802
void fw_cfg_add_i64(FWCfgState *s, uint16_t key, uint64_t value)
806
copy = g_malloc(sizeof(value));
807
*copy = cpu_to_le64(value);
808
trace_fw_cfg_add_i64(key, trace_key_name(key), value);
809
fw_cfg_add_bytes(s, key, copy, sizeof(value));
812
void fw_cfg_modify_i64(FWCfgState *s, uint16_t key, uint64_t value)
814
uint64_t *copy, *old;
816
copy = g_malloc(sizeof(value));
817
*copy = cpu_to_le64(value);
818
old = fw_cfg_modify_bytes_read(s, key, copy, sizeof(value));
822
void fw_cfg_set_order_override(FWCfgState *s, int order)
824
assert(s->fw_cfg_order_override == 0);
825
s->fw_cfg_order_override = order;
828
void fw_cfg_reset_order_override(FWCfgState *s)
830
assert(s->fw_cfg_order_override != 0);
831
s->fw_cfg_order_override = 0;
852
{ "etc/boot-menu-wait", 10 },
853
{ "bootsplash.jpg", 11 },
854
{ "bootsplash.bmp", 12 },
855
{ "etc/boot-fail-wait", 15 },
856
{ "etc/smbios/smbios-tables", 20 },
857
{ "etc/smbios/smbios-anchor", 30 },
859
{ "etc/reserved-memory-end", 50 },
860
{ "genroms/kvmvapic.bin", 55 },
861
{ "genroms/linuxboot.bin", 60 },
864
{ "etc/system-states", 90 },
867
{ "etc/extra-pci-roots", 120 },
868
{ "etc/acpi/tables", 130 },
869
{ "etc/table-loader", 140 },
870
{ "etc/tpm/log", 150 },
871
{ "etc/acpi/rsdp", 160 },
872
{ "bootorder", 170 },
873
{ "etc/msr_feature_control", 180 },
875
#define FW_CFG_ORDER_OVERRIDE_LAST 200
884
static void fw_cfg_acpi_mr_save(FWCfgState *s, const char *filename, size_t len)
886
if (!strcmp(filename, ACPI_BUILD_TABLE_FILE)) {
887
s->table_mr_size = len;
888
} else if (!strcmp(filename, ACPI_BUILD_LOADER_FILE)) {
889
s->linker_mr_size = len;
890
} else if (!strcmp(filename, ACPI_BUILD_RSDP_FILE)) {
891
s->rsdp_mr_size = len;
895
static int get_fw_cfg_order(FWCfgState *s, const char *name)
899
if (s->fw_cfg_order_override > 0) {
900
return s->fw_cfg_order_override;
903
for (i = 0; i < ARRAY_SIZE(fw_cfg_order); i++) {
904
if (fw_cfg_order[i].name == NULL) {
908
if (strcmp(name, fw_cfg_order[i].name) == 0) {
909
return fw_cfg_order[i].order;
914
warn_report("Unknown firmware file in legacy mode: %s", name);
915
return FW_CFG_ORDER_OVERRIDE_LAST;
918
void fw_cfg_add_file_callback(FWCfgState *s, const char *filename,
919
FWCfgCallback select_cb,
920
FWCfgWriteCallback write_cb,
921
void *callback_opaque,
922
void *data, size_t len, bool read_only)
926
MachineClass *mc = MACHINE_GET_CLASS(qdev_get_machine());
930
dsize = sizeof(uint32_t) + sizeof(FWCfgFile) * fw_cfg_file_slots(s);
931
s->files = g_malloc0(dsize);
932
fw_cfg_add_bytes(s, FW_CFG_FILE_DIR, s->files, dsize);
935
count = be32_to_cpu(s->files->count);
936
assert(count < fw_cfg_file_slots(s));
939
if (mc->legacy_fw_cfg_order) {
944
order = get_fw_cfg_order(s, filename);
946
index > 0 && order < s->entry_order[index - 1];
951
index > 0 && strcmp(filename, s->files->f[index - 1].name) < 0;
962
for (i = count; i > index; i--) {
963
s->files->f[i] = s->files->f[i - 1];
964
s->files->f[i].select = cpu_to_be16(FW_CFG_FILE_FIRST + i);
965
s->entries[0][FW_CFG_FILE_FIRST + i] =
966
s->entries[0][FW_CFG_FILE_FIRST + i - 1];
967
s->entry_order[i] = s->entry_order[i - 1];
970
memset(&s->files->f[index], 0, sizeof(FWCfgFile));
971
memset(&s->entries[0][FW_CFG_FILE_FIRST + index], 0, sizeof(FWCfgEntry));
973
pstrcpy(s->files->f[index].name, sizeof(s->files->f[index].name), filename);
974
for (i = 0; i <= count; i++) {
976
strcmp(s->files->f[index].name, s->files->f[i].name) == 0) {
977
error_report("duplicate fw_cfg file name: %s",
978
s->files->f[index].name);
983
fw_cfg_add_bytes_callback(s, FW_CFG_FILE_FIRST + index,
985
callback_opaque, data, len,
988
s->files->f[index].size = cpu_to_be32(len);
989
s->files->f[index].select = cpu_to_be16(FW_CFG_FILE_FIRST + index);
990
s->entry_order[index] = order;
991
trace_fw_cfg_add_file(s, index, s->files->f[index].name, len);
993
s->files->count = cpu_to_be32(count+1);
994
fw_cfg_acpi_mr_save(s, filename, len);
997
void fw_cfg_add_file(FWCfgState *s, const char *filename,
998
void *data, size_t len)
1000
fw_cfg_add_file_callback(s, filename, NULL, NULL, NULL, data, len, true);
1003
void *fw_cfg_modify_file(FWCfgState *s, const char *filename,
1004
void *data, size_t len)
1011
index = be32_to_cpu(s->files->count);
1013
for (i = 0; i < index; i++) {
1014
if (strcmp(filename, s->files->f[i].name) == 0) {
1015
ptr = fw_cfg_modify_bytes_read(s, FW_CFG_FILE_FIRST + i,
1017
s->files->f[i].size = cpu_to_be32(len);
1018
fw_cfg_acpi_mr_save(s, filename, len);
1023
assert(index < fw_cfg_file_slots(s));
1026
fw_cfg_add_file_callback(s, filename, NULL, NULL, NULL, data, len, true);
1030
bool fw_cfg_add_from_generator(FWCfgState *s, const char *filename,
1031
const char *gen_id, Error **errp)
1033
FWCfgDataGeneratorClass *klass;
1038
obj = object_resolve_path_component(object_get_objects_root(), gen_id);
1040
error_setg(errp, "Cannot find object ID '%s'", gen_id);
1043
if (!object_dynamic_cast(obj, TYPE_FW_CFG_DATA_GENERATOR_INTERFACE)) {
1044
error_setg(errp, "Object ID '%s' is not a '%s' subclass",
1045
gen_id, TYPE_FW_CFG_DATA_GENERATOR_INTERFACE);
1048
klass = FW_CFG_DATA_GENERATOR_GET_CLASS(obj);
1049
array = klass->get_data(obj, errp);
1054
fw_cfg_add_file(s, filename, g_byte_array_free(array, FALSE), size);
1059
void fw_cfg_add_extra_pci_roots(PCIBus *bus, FWCfgState *s)
1061
int extra_hosts = 0;
1067
QLIST_FOREACH(bus, &bus->child, sibling) {
1069
if (pci_bus_is_root(bus)) {
1074
if (extra_hosts && s) {
1075
uint64_t *val = g_malloc(sizeof(*val));
1076
*val = cpu_to_le64(extra_hosts);
1077
fw_cfg_add_file(s, "etc/extra-pci-roots", val, sizeof(*val));
1081
static void fw_cfg_machine_reset(void *opaque)
1083
MachineClass *mc = MACHINE_GET_CLASS(qdev_get_machine());
1084
FWCfgState *s = opaque;
1089
buf = get_boot_devices_list(&len);
1090
ptr = fw_cfg_modify_file(s, "bootorder", (uint8_t *)buf, len);
1093
if (!mc->legacy_fw_cfg_order) {
1094
buf = get_boot_devices_lchs_list(&len);
1095
ptr = fw_cfg_modify_file(s, "bios-geometry", (uint8_t *)buf, len);
1100
static void fw_cfg_machine_ready(struct Notifier *n, void *data)
1102
FWCfgState *s = container_of(n, FWCfgState, machine_ready);
1103
qemu_register_reset(fw_cfg_machine_reset, s);
1106
static Property fw_cfg_properties[] = {
1107
DEFINE_PROP_BOOL("acpi-mr-restore", FWCfgState, acpi_mr_restore, true),
1108
DEFINE_PROP_END_OF_LIST(),
1111
static void fw_cfg_common_realize(DeviceState *dev, Error **errp)
1113
FWCfgState *s = FW_CFG(dev);
1114
MachineState *machine = MACHINE(qdev_get_machine());
1115
uint32_t version = FW_CFG_VERSION;
1117
if (!fw_cfg_find()) {
1118
error_setg(errp, "at most one %s device is permitted", TYPE_FW_CFG);
1122
fw_cfg_add_bytes(s, FW_CFG_SIGNATURE, (char *)"QEMU", 4);
1123
fw_cfg_add_bytes(s, FW_CFG_UUID, &qemu_uuid, 16);
1124
fw_cfg_add_i16(s, FW_CFG_NOGRAPHIC, (uint16_t)!machine->enable_graphics);
1125
fw_cfg_add_i16(s, FW_CFG_BOOT_MENU, (uint16_t)(machine->boot_config.has_menu && machine->boot_config.menu));
1126
fw_cfg_bootsplash(s);
1129
if (s->dma_enabled) {
1130
version |= FW_CFG_VERSION_DMA;
1133
fw_cfg_add_i32(s, FW_CFG_ID, version);
1135
s->machine_ready.notify = fw_cfg_machine_ready;
1136
qemu_add_machine_init_done_notifier(&s->machine_ready);
1139
FWCfgState *fw_cfg_init_io_dma(uint32_t iobase, uint32_t dma_iobase,
1140
AddressSpace *dma_as)
1146
MemoryRegion *iomem = get_system_io();
1147
bool dma_requested = dma_iobase && dma_as;
1149
dev = qdev_new(TYPE_FW_CFG_IO);
1150
if (!dma_requested) {
1151
qdev_prop_set_bit(dev, "dma_enabled", false);
1154
object_property_add_child(OBJECT(qdev_get_machine()), TYPE_FW_CFG,
1157
sbd = SYS_BUS_DEVICE(dev);
1158
sysbus_realize_and_unref(sbd, &error_fatal);
1159
ios = FW_CFG_IO(dev);
1160
memory_region_add_subregion(iomem, iobase, &ios->comb_iomem);
1164
if (s->dma_enabled) {
1168
memory_region_add_subregion(iomem, dma_iobase, &s->dma_iomem);
1174
FWCfgState *fw_cfg_init_io(uint32_t iobase)
1176
return fw_cfg_init_io_dma(iobase, 0, NULL);
1179
FWCfgState *fw_cfg_init_mem_wide(hwaddr ctl_addr,
1180
hwaddr data_addr, uint32_t data_width,
1181
hwaddr dma_addr, AddressSpace *dma_as)
1186
bool dma_requested = dma_addr && dma_as;
1188
dev = qdev_new(TYPE_FW_CFG_MEM);
1189
qdev_prop_set_uint32(dev, "data_width", data_width);
1190
if (!dma_requested) {
1191
qdev_prop_set_bit(dev, "dma_enabled", false);
1194
object_property_add_child(OBJECT(qdev_get_machine()), TYPE_FW_CFG,
1197
sbd = SYS_BUS_DEVICE(dev);
1198
sysbus_realize_and_unref(sbd, &error_fatal);
1199
sysbus_mmio_map(sbd, 0, ctl_addr);
1200
sysbus_mmio_map(sbd, 1, data_addr);
1204
if (s->dma_enabled) {
1207
sysbus_mmio_map(sbd, 2, dma_addr);
1213
FWCfgState *fw_cfg_init_mem(hwaddr ctl_addr, hwaddr data_addr)
1215
return fw_cfg_init_mem_wide(ctl_addr, data_addr,
1216
fw_cfg_data_mem_ops.valid.max_access_size,
1221
FWCfgState *fw_cfg_find(void)
1224
return FW_CFG(object_resolve_path_type("", TYPE_FW_CFG, NULL));
1227
void load_image_to_fw_cfg(FWCfgState *fw_cfg, uint16_t size_key,
1228
uint16_t data_key, const char *image_name,
1229
bool try_decompress)
1234
if (image_name == NULL) {
1238
if (try_decompress) {
1239
size = load_image_gzipped_buffer(image_name,
1240
LOAD_IMAGE_MAX_GUNZIP_BYTES, &data);
1243
if (size == (size_t)-1) {
1247
if (!g_file_get_contents(image_name, &contents, &length, NULL)) {
1248
error_report("failed to load \"%s\"", image_name);
1252
data = (uint8_t *)contents;
1255
fw_cfg_add_i32(fw_cfg, size_key, size);
1256
fw_cfg_add_bytes(fw_cfg, data_key, data, size);
1259
static void fw_cfg_class_init(ObjectClass *klass, void *data)
1261
DeviceClass *dc = DEVICE_CLASS(klass);
1263
dc->reset = fw_cfg_reset;
1264
dc->vmsd = &vmstate_fw_cfg;
1266
device_class_set_props(dc, fw_cfg_properties);
1269
static const TypeInfo fw_cfg_info = {
1270
.name = TYPE_FW_CFG,
1271
.parent = TYPE_SYS_BUS_DEVICE,
1273
.instance_size = sizeof(FWCfgState),
1274
.class_init = fw_cfg_class_init,
1277
static void fw_cfg_file_slots_allocate(FWCfgState *s, Error **errp)
1279
uint16_t file_slots_max;
1281
if (fw_cfg_file_slots(s) < FW_CFG_FILE_SLOTS_MIN) {
1282
error_setg(errp, "\"file_slots\" must be at least 0x%x",
1283
FW_CFG_FILE_SLOTS_MIN);
1290
file_slots_max = (UINT16_MAX & FW_CFG_ENTRY_MASK) - FW_CFG_FILE_FIRST + 1;
1291
if (fw_cfg_file_slots(s) > file_slots_max) {
1292
error_setg(errp, "\"file_slots\" must not exceed 0x%" PRIx16,
1297
s->entries[0] = g_new0(FWCfgEntry, fw_cfg_max_entry(s));
1298
s->entries[1] = g_new0(FWCfgEntry, fw_cfg_max_entry(s));
1299
s->entry_order = g_new0(int, fw_cfg_max_entry(s));
1302
static Property fw_cfg_io_properties[] = {
1303
DEFINE_PROP_BOOL("dma_enabled", FWCfgIoState, parent_obj.dma_enabled,
1305
DEFINE_PROP_UINT16("x-file-slots", FWCfgIoState, parent_obj.file_slots,
1306
FW_CFG_FILE_SLOTS_DFLT),
1307
DEFINE_PROP_END_OF_LIST(),
1310
static void fw_cfg_io_realize(DeviceState *dev, Error **errp)
1313
FWCfgIoState *s = FW_CFG_IO(dev);
1315
fw_cfg_file_slots_allocate(FW_CFG(s), errp);
1323
memory_region_init_io(&s->comb_iomem, OBJECT(s), &fw_cfg_comb_mem_ops,
1324
FW_CFG(s), "fwcfg", FW_CFG_CTL_SIZE);
1326
if (FW_CFG(s)->dma_enabled) {
1327
memory_region_init_io(&FW_CFG(s)->dma_iomem, OBJECT(s),
1328
&fw_cfg_dma_mem_ops, FW_CFG(s), "fwcfg.dma",
1329
sizeof(dma_addr_t));
1332
fw_cfg_common_realize(dev, errp);
1335
static void fw_cfg_io_class_init(ObjectClass *klass, void *data)
1337
DeviceClass *dc = DEVICE_CLASS(klass);
1339
dc->realize = fw_cfg_io_realize;
1340
device_class_set_props(dc, fw_cfg_io_properties);
1343
static const TypeInfo fw_cfg_io_info = {
1344
.name = TYPE_FW_CFG_IO,
1345
.parent = TYPE_FW_CFG,
1346
.instance_size = sizeof(FWCfgIoState),
1347
.class_init = fw_cfg_io_class_init,
1351
static Property fw_cfg_mem_properties[] = {
1352
DEFINE_PROP_UINT32("data_width", FWCfgMemState, data_width, -1),
1353
DEFINE_PROP_BOOL("dma_enabled", FWCfgMemState, parent_obj.dma_enabled,
1355
DEFINE_PROP_UINT16("x-file-slots", FWCfgMemState, parent_obj.file_slots,
1356
FW_CFG_FILE_SLOTS_DFLT),
1357
DEFINE_PROP_END_OF_LIST(),
1360
static void fw_cfg_mem_realize(DeviceState *dev, Error **errp)
1363
FWCfgMemState *s = FW_CFG_MEM(dev);
1364
SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
1365
const MemoryRegionOps *data_ops = &fw_cfg_data_mem_ops;
1367
fw_cfg_file_slots_allocate(FW_CFG(s), errp);
1372
memory_region_init_io(&s->ctl_iomem, OBJECT(s), &fw_cfg_ctl_mem_ops,
1373
FW_CFG(s), "fwcfg.ctl", FW_CFG_CTL_SIZE);
1374
sysbus_init_mmio(sbd, &s->ctl_iomem);
1376
if (s->data_width > data_ops->valid.max_access_size) {
1377
s->wide_data_ops = *data_ops;
1379
s->wide_data_ops.valid.max_access_size = s->data_width;
1380
s->wide_data_ops.impl.max_access_size = s->data_width;
1381
data_ops = &s->wide_data_ops;
1383
memory_region_init_io(&s->data_iomem, OBJECT(s), data_ops, FW_CFG(s),
1384
"fwcfg.data", data_ops->valid.max_access_size);
1385
sysbus_init_mmio(sbd, &s->data_iomem);
1387
if (FW_CFG(s)->dma_enabled) {
1388
memory_region_init_io(&FW_CFG(s)->dma_iomem, OBJECT(s),
1389
&fw_cfg_dma_mem_ops, FW_CFG(s), "fwcfg.dma",
1390
sizeof(dma_addr_t));
1391
sysbus_init_mmio(sbd, &FW_CFG(s)->dma_iomem);
1394
fw_cfg_common_realize(dev, errp);
1397
static void fw_cfg_mem_class_init(ObjectClass *klass, void *data)
1399
DeviceClass *dc = DEVICE_CLASS(klass);
1401
dc->realize = fw_cfg_mem_realize;
1402
device_class_set_props(dc, fw_cfg_mem_properties);
1405
static const TypeInfo fw_cfg_mem_info = {
1406
.name = TYPE_FW_CFG_MEM,
1407
.parent = TYPE_FW_CFG,
1408
.instance_size = sizeof(FWCfgMemState),
1409
.class_init = fw_cfg_mem_class_init,
1412
static void fw_cfg_register_types(void)
1414
type_register_static(&fw_cfg_info);
1415
type_register_static(&fw_cfg_io_info);
1416
type_register_static(&fw_cfg_mem_info);
1419
type_init(fw_cfg_register_types)