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 "sysemu/sysemu.h"
26 #include "hw/isa/isa.h"
27 #include "hw/nvram/fw_cfg.h"
28 #include "hw/sysbus.h"
30 #include "qemu/error-report.h"
31 #include "qemu/config-file.h"
34 #define FW_CFG_DATA_SIZE 1
36 typedef struct FWCfgEntry
{
39 void *callback_opaque
;
40 FWCfgCallback callback
;
45 MemoryRegion ctl_iomem
, data_iomem
, comb_iomem
;
46 uint32_t ctl_iobase
, data_iobase
;
47 FWCfgEntry entries
[2][FW_CFG_MAX_ENTRY
];
51 Notifier machine_ready
;
57 static char *read_splashfile(char *filename
, size_t *file_sizep
,
64 unsigned int filehead
;
67 res
= g_file_get_contents(filename
, &content
, file_sizep
, &err
);
69 error_report("failed to read splash file '%s'", filename
);
75 if (*file_sizep
< 30) {
80 filehead
= ((content
[0] & 0xff) + (content
[1] << 8)) & 0xffff;
81 if (filehead
== 0xd8ff) {
83 } else if (filehead
== 0x4d42) {
90 if (file_type
== BMP_FILE
) {
91 bmp_bpp
= (content
[28] + (content
[29] << 8)) & 0xffff;
98 *file_typep
= file_type
;
103 error_report("splash file '%s' format not recognized; must be JPEG "
104 "or 24 bit BMP", filename
);
109 static void fw_cfg_bootsplash(FWCfgState
*s
)
111 int boot_splash_time
= -1;
112 const char *boot_splash_filename
= NULL
;
114 char *filename
, *file_data
;
119 /* get user configuration */
120 QemuOptsList
*plist
= qemu_find_opts("boot-opts");
121 QemuOpts
*opts
= QTAILQ_FIRST(&plist
->head
);
123 temp
= qemu_opt_get(opts
, "splash");
125 boot_splash_filename
= temp
;
127 temp
= qemu_opt_get(opts
, "splash-time");
130 boot_splash_time
= strtol(p
, (char **)&p
, 10);
134 /* insert splash time if user configurated */
135 if (boot_splash_time
>= 0) {
136 /* validate the input */
137 if (boot_splash_time
> 0xffff) {
138 error_report("splash time is big than 65535, force it to 65535.");
139 boot_splash_time
= 0xffff;
141 /* use little endian format */
142 qemu_extra_params_fw
[0] = (uint8_t)(boot_splash_time
& 0xff);
143 qemu_extra_params_fw
[1] = (uint8_t)((boot_splash_time
>> 8) & 0xff);
144 fw_cfg_add_file(s
, "etc/boot-menu-wait", qemu_extra_params_fw
, 2);
147 /* insert splash file if user configurated */
148 if (boot_splash_filename
!= NULL
) {
149 filename
= qemu_find_file(QEMU_FILE_TYPE_BIOS
, boot_splash_filename
);
150 if (filename
== NULL
) {
151 error_report("failed to find file '%s'.", boot_splash_filename
);
155 /* loading file data */
156 file_data
= read_splashfile(filename
, &file_size
, &file_type
);
157 if (file_data
== NULL
) {
161 if (boot_splash_filedata
!= NULL
) {
162 g_free(boot_splash_filedata
);
164 boot_splash_filedata
= (uint8_t *)file_data
;
165 boot_splash_filedata_size
= file_size
;
168 if (file_type
== JPG_FILE
) {
169 fw_cfg_add_file(s
, "bootsplash.jpg",
170 boot_splash_filedata
, boot_splash_filedata_size
);
172 fw_cfg_add_file(s
, "bootsplash.bmp",
173 boot_splash_filedata
, boot_splash_filedata_size
);
179 static void fw_cfg_reboot(FWCfgState
*s
)
181 int reboot_timeout
= -1;
185 /* get user configuration */
186 QemuOptsList
*plist
= qemu_find_opts("boot-opts");
187 QemuOpts
*opts
= QTAILQ_FIRST(&plist
->head
);
189 temp
= qemu_opt_get(opts
, "reboot-timeout");
192 reboot_timeout
= strtol(p
, (char **)&p
, 10);
195 /* validate the input */
196 if (reboot_timeout
> 0xffff) {
197 error_report("reboot timeout is larger than 65535, force it to 65535.");
198 reboot_timeout
= 0xffff;
200 fw_cfg_add_file(s
, "etc/boot-fail-wait", g_memdup(&reboot_timeout
, 4), 4);
203 static void fw_cfg_write(FWCfgState
*s
, uint8_t value
)
205 int arch
= !!(s
->cur_entry
& FW_CFG_ARCH_LOCAL
);
206 FWCfgEntry
*e
= &s
->entries
[arch
][s
->cur_entry
& FW_CFG_ENTRY_MASK
];
208 trace_fw_cfg_write(s
, value
);
210 if (s
->cur_entry
& FW_CFG_WRITE_CHANNEL
&& e
->callback
&&
211 s
->cur_offset
< e
->len
) {
212 e
->data
[s
->cur_offset
++] = value
;
213 if (s
->cur_offset
== e
->len
) {
214 e
->callback(e
->callback_opaque
, e
->data
);
220 static int fw_cfg_select(FWCfgState
*s
, uint16_t key
)
225 if ((key
& FW_CFG_ENTRY_MASK
) >= FW_CFG_MAX_ENTRY
) {
226 s
->cur_entry
= FW_CFG_INVALID
;
233 trace_fw_cfg_select(s
, key
, ret
);
237 static uint8_t fw_cfg_read(FWCfgState
*s
)
239 int arch
= !!(s
->cur_entry
& FW_CFG_ARCH_LOCAL
);
240 FWCfgEntry
*e
= &s
->entries
[arch
][s
->cur_entry
& FW_CFG_ENTRY_MASK
];
243 if (s
->cur_entry
== FW_CFG_INVALID
|| !e
->data
|| s
->cur_offset
>= e
->len
)
246 ret
= e
->data
[s
->cur_offset
++];
248 trace_fw_cfg_read(s
, ret
);
252 static uint64_t fw_cfg_data_mem_read(void *opaque
, hwaddr addr
,
255 return fw_cfg_read(opaque
);
258 static void fw_cfg_data_mem_write(void *opaque
, hwaddr addr
,
259 uint64_t value
, unsigned size
)
261 fw_cfg_write(opaque
, (uint8_t)value
);
264 static void fw_cfg_ctl_mem_write(void *opaque
, hwaddr addr
,
265 uint64_t value
, unsigned size
)
267 fw_cfg_select(opaque
, (uint16_t)value
);
270 static bool fw_cfg_ctl_mem_valid(void *opaque
, hwaddr addr
,
271 unsigned size
, bool is_write
)
273 return is_write
&& size
== 2;
276 static uint64_t fw_cfg_comb_read(void *opaque
, hwaddr addr
,
279 return fw_cfg_read(opaque
);
282 static void fw_cfg_comb_write(void *opaque
, hwaddr addr
,
283 uint64_t value
, unsigned size
)
287 fw_cfg_write(opaque
, (uint8_t)value
);
290 fw_cfg_select(opaque
, (uint16_t)value
);
295 static bool fw_cfg_comb_valid(void *opaque
, hwaddr addr
,
296 unsigned size
, bool is_write
)
298 return (size
== 1) || (is_write
&& size
== 2);
301 static const MemoryRegionOps fw_cfg_ctl_mem_ops
= {
302 .write
= fw_cfg_ctl_mem_write
,
303 .endianness
= DEVICE_NATIVE_ENDIAN
,
304 .valid
.accepts
= fw_cfg_ctl_mem_valid
,
307 static const MemoryRegionOps fw_cfg_data_mem_ops
= {
308 .read
= fw_cfg_data_mem_read
,
309 .write
= fw_cfg_data_mem_write
,
310 .endianness
= DEVICE_NATIVE_ENDIAN
,
312 .min_access_size
= 1,
313 .max_access_size
= 1,
317 static const MemoryRegionOps fw_cfg_comb_mem_ops
= {
318 .read
= fw_cfg_comb_read
,
319 .write
= fw_cfg_comb_write
,
320 .endianness
= DEVICE_NATIVE_ENDIAN
,
321 .valid
.accepts
= fw_cfg_comb_valid
,
324 static void fw_cfg_reset(DeviceState
*d
)
326 FWCfgState
*s
= DO_UPCAST(FWCfgState
, busdev
.qdev
, d
);
331 /* Save restore 32 bit int as uint16_t
332 This is a Big hack, but it is how the old state did it.
333 Or we broke compatibility in the state, or we can't use struct tm
336 static int get_uint32_as_uint16(QEMUFile
*f
, void *pv
, size_t size
)
339 *v
= qemu_get_be16(f
);
343 static void put_unused(QEMUFile
*f
, void *pv
, size_t size
)
345 fprintf(stderr
, "uint32_as_uint16 is only used for backward compatibility.\n");
346 fprintf(stderr
, "This functions shouldn't be called.\n");
349 static const VMStateInfo vmstate_hack_uint32_as_uint16
= {
350 .name
= "int32_as_uint16",
351 .get
= get_uint32_as_uint16
,
355 #define VMSTATE_UINT16_HACK(_f, _s, _t) \
356 VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_hack_uint32_as_uint16, uint32_t)
359 static bool is_version_1(void *opaque
, int version_id
)
361 return version_id
== 1;
364 static const VMStateDescription vmstate_fw_cfg
= {
367 .minimum_version_id
= 1,
368 .minimum_version_id_old
= 1,
369 .fields
= (VMStateField
[]) {
370 VMSTATE_UINT16(cur_entry
, FWCfgState
),
371 VMSTATE_UINT16_HACK(cur_offset
, FWCfgState
, is_version_1
),
372 VMSTATE_UINT32_V(cur_offset
, FWCfgState
, 2),
373 VMSTATE_END_OF_LIST()
377 void fw_cfg_add_bytes(FWCfgState
*s
, uint16_t key
, void *data
, size_t len
)
379 int arch
= !!(key
& FW_CFG_ARCH_LOCAL
);
381 key
&= FW_CFG_ENTRY_MASK
;
383 assert(key
< FW_CFG_MAX_ENTRY
&& len
< UINT32_MAX
);
385 s
->entries
[arch
][key
].data
= data
;
386 s
->entries
[arch
][key
].len
= (uint32_t)len
;
389 void fw_cfg_add_string(FWCfgState
*s
, uint16_t key
, const char *value
)
391 size_t sz
= strlen(value
) + 1;
393 return fw_cfg_add_bytes(s
, key
, g_memdup(value
, sz
), sz
);
396 void fw_cfg_add_i16(FWCfgState
*s
, uint16_t key
, uint16_t value
)
400 copy
= g_malloc(sizeof(value
));
401 *copy
= cpu_to_le16(value
);
402 fw_cfg_add_bytes(s
, key
, copy
, sizeof(value
));
405 void fw_cfg_add_i32(FWCfgState
*s
, uint16_t key
, uint32_t value
)
409 copy
= g_malloc(sizeof(value
));
410 *copy
= cpu_to_le32(value
);
411 fw_cfg_add_bytes(s
, key
, copy
, sizeof(value
));
414 void fw_cfg_add_i64(FWCfgState
*s
, uint16_t key
, uint64_t value
)
418 copy
= g_malloc(sizeof(value
));
419 *copy
= cpu_to_le64(value
);
420 fw_cfg_add_bytes(s
, key
, copy
, sizeof(value
));
423 void fw_cfg_add_callback(FWCfgState
*s
, uint16_t key
, FWCfgCallback callback
,
424 void *callback_opaque
, void *data
, size_t len
)
426 int arch
= !!(key
& FW_CFG_ARCH_LOCAL
);
428 assert(key
& FW_CFG_WRITE_CHANNEL
);
430 key
&= FW_CFG_ENTRY_MASK
;
432 assert(key
< FW_CFG_MAX_ENTRY
&& len
<= UINT32_MAX
);
434 s
->entries
[arch
][key
].data
= data
;
435 s
->entries
[arch
][key
].len
= (uint32_t)len
;
436 s
->entries
[arch
][key
].callback_opaque
= callback_opaque
;
437 s
->entries
[arch
][key
].callback
= callback
;
440 void fw_cfg_add_file(FWCfgState
*s
, const char *filename
,
441 void *data
, size_t len
)
447 dsize
= sizeof(uint32_t) + sizeof(FWCfgFile
) * FW_CFG_FILE_SLOTS
;
448 s
->files
= g_malloc0(dsize
);
449 fw_cfg_add_bytes(s
, FW_CFG_FILE_DIR
, s
->files
, dsize
);
452 index
= be32_to_cpu(s
->files
->count
);
453 assert(index
< FW_CFG_FILE_SLOTS
);
455 fw_cfg_add_bytes(s
, FW_CFG_FILE_FIRST
+ index
, data
, len
);
457 pstrcpy(s
->files
->f
[index
].name
, sizeof(s
->files
->f
[index
].name
),
459 for (i
= 0; i
< index
; i
++) {
460 if (strcmp(s
->files
->f
[index
].name
, s
->files
->f
[i
].name
) == 0) {
461 trace_fw_cfg_add_file_dupe(s
, s
->files
->f
[index
].name
);
466 s
->files
->f
[index
].size
= cpu_to_be32(len
);
467 s
->files
->f
[index
].select
= cpu_to_be16(FW_CFG_FILE_FIRST
+ index
);
468 trace_fw_cfg_add_file(s
, index
, s
->files
->f
[index
].name
, len
);
470 s
->files
->count
= cpu_to_be32(index
+1);
473 static void fw_cfg_machine_ready(struct Notifier
*n
, void *data
)
476 FWCfgState
*s
= container_of(n
, FWCfgState
, machine_ready
);
477 char *bootindex
= get_boot_devices_list(&len
);
479 fw_cfg_add_file(s
, "bootorder", (uint8_t*)bootindex
, len
);
482 FWCfgState
*fw_cfg_init(uint32_t ctl_port
, uint32_t data_port
,
483 hwaddr ctl_addr
, hwaddr data_addr
)
489 dev
= qdev_create(NULL
, "fw_cfg");
490 qdev_prop_set_uint32(dev
, "ctl_iobase", ctl_port
);
491 qdev_prop_set_uint32(dev
, "data_iobase", data_port
);
492 d
= SYS_BUS_DEVICE(dev
);
494 s
= DO_UPCAST(FWCfgState
, busdev
.qdev
, dev
);
496 if (!object_resolve_path("/machine/fw_cfg", NULL
)) {
497 object_property_add_child(qdev_get_machine(), "fw_cfg", OBJECT(s
),
501 qdev_init_nofail(dev
);
504 sysbus_mmio_map(d
, 0, ctl_addr
);
507 sysbus_mmio_map(d
, 1, data_addr
);
509 fw_cfg_add_bytes(s
, FW_CFG_SIGNATURE
, (char *)"QEMU", 4);
510 fw_cfg_add_bytes(s
, FW_CFG_UUID
, qemu_uuid
, 16);
511 fw_cfg_add_i16(s
, FW_CFG_NOGRAPHIC
, (uint16_t)(display_type
== DT_NOGRAPHIC
));
512 fw_cfg_add_i16(s
, FW_CFG_NB_CPUS
, (uint16_t)smp_cpus
);
513 fw_cfg_add_i16(s
, FW_CFG_BOOT_MENU
, (uint16_t)boot_menu
);
514 fw_cfg_bootsplash(s
);
517 s
->machine_ready
.notify
= fw_cfg_machine_ready
;
518 qemu_add_machine_init_done_notifier(&s
->machine_ready
);
523 static int fw_cfg_init1(SysBusDevice
*dev
)
525 FWCfgState
*s
= FROM_SYSBUS(FWCfgState
, dev
);
527 memory_region_init_io(&s
->ctl_iomem
, &fw_cfg_ctl_mem_ops
, s
,
528 "fwcfg.ctl", FW_CFG_SIZE
);
529 sysbus_init_mmio(dev
, &s
->ctl_iomem
);
530 memory_region_init_io(&s
->data_iomem
, &fw_cfg_data_mem_ops
, s
,
531 "fwcfg.data", FW_CFG_DATA_SIZE
);
532 sysbus_init_mmio(dev
, &s
->data_iomem
);
533 /* In case ctl and data overlap: */
534 memory_region_init_io(&s
->comb_iomem
, &fw_cfg_comb_mem_ops
, s
,
535 "fwcfg", FW_CFG_SIZE
);
537 if (s
->ctl_iobase
+ 1 == s
->data_iobase
) {
538 sysbus_add_io(dev
, s
->ctl_iobase
, &s
->comb_iomem
);
541 sysbus_add_io(dev
, s
->ctl_iobase
, &s
->ctl_iomem
);
543 if (s
->data_iobase
) {
544 sysbus_add_io(dev
, s
->data_iobase
, &s
->data_iomem
);
550 static Property fw_cfg_properties
[] = {
551 DEFINE_PROP_HEX32("ctl_iobase", FWCfgState
, ctl_iobase
, -1),
552 DEFINE_PROP_HEX32("data_iobase", FWCfgState
, data_iobase
, -1),
553 DEFINE_PROP_END_OF_LIST(),
556 static void fw_cfg_class_init(ObjectClass
*klass
, void *data
)
558 DeviceClass
*dc
= DEVICE_CLASS(klass
);
559 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
561 k
->init
= fw_cfg_init1
;
563 dc
->reset
= fw_cfg_reset
;
564 dc
->vmsd
= &vmstate_fw_cfg
;
565 dc
->props
= fw_cfg_properties
;
568 static const TypeInfo fw_cfg_info
= {
570 .parent
= TYPE_SYS_BUS_DEVICE
,
571 .instance_size
= sizeof(FWCfgState
),
572 .class_init
= fw_cfg_class_init
,
575 static void fw_cfg_register_types(void)
577 type_register_static(&fw_cfg_info
);
580 type_init(fw_cfg_register_types
)