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
35 #define TYPE_FW_CFG "fw_cfg"
36 #define FW_CFG_NAME "fw_cfg"
37 #define FW_CFG_PATH "/machine/" FW_CFG_NAME
38 #define FW_CFG(obj) OBJECT_CHECK(FWCfgState, (obj), TYPE_FW_CFG)
40 typedef struct FWCfgEntry
{
43 void *callback_opaque
;
44 FWCfgCallback callback
;
45 FWCfgReadCallback read_callback
;
50 SysBusDevice parent_obj
;
53 MemoryRegion ctl_iomem
, data_iomem
, comb_iomem
;
54 uint32_t ctl_iobase
, data_iobase
;
55 FWCfgEntry entries
[2][FW_CFG_MAX_ENTRY
];
59 Notifier machine_ready
;
65 static char *read_splashfile(char *filename
, gsize
*file_sizep
,
72 unsigned int filehead
;
75 res
= g_file_get_contents(filename
, &content
, file_sizep
, &err
);
77 error_report("failed to read splash file '%s'", filename
);
83 if (*file_sizep
< 30) {
88 filehead
= ((content
[0] & 0xff) + (content
[1] << 8)) & 0xffff;
89 if (filehead
== 0xd8ff) {
91 } else if (filehead
== 0x4d42) {
98 if (file_type
== BMP_FILE
) {
99 bmp_bpp
= (content
[28] + (content
[29] << 8)) & 0xffff;
106 *file_typep
= file_type
;
111 error_report("splash file '%s' format not recognized; must be JPEG "
112 "or 24 bit BMP", filename
);
117 static void fw_cfg_bootsplash(FWCfgState
*s
)
119 int boot_splash_time
= -1;
120 const char *boot_splash_filename
= NULL
;
122 char *filename
, *file_data
;
127 /* get user configuration */
128 QemuOptsList
*plist
= qemu_find_opts("boot-opts");
129 QemuOpts
*opts
= QTAILQ_FIRST(&plist
->head
);
131 temp
= qemu_opt_get(opts
, "splash");
133 boot_splash_filename
= temp
;
135 temp
= qemu_opt_get(opts
, "splash-time");
138 boot_splash_time
= strtol(p
, (char **)&p
, 10);
142 /* insert splash time if user configurated */
143 if (boot_splash_time
>= 0) {
144 /* validate the input */
145 if (boot_splash_time
> 0xffff) {
146 error_report("splash time is big than 65535, force it to 65535.");
147 boot_splash_time
= 0xffff;
149 /* use little endian format */
150 qemu_extra_params_fw
[0] = (uint8_t)(boot_splash_time
& 0xff);
151 qemu_extra_params_fw
[1] = (uint8_t)((boot_splash_time
>> 8) & 0xff);
152 fw_cfg_add_file(s
, "etc/boot-menu-wait", qemu_extra_params_fw
, 2);
155 /* insert splash file if user configurated */
156 if (boot_splash_filename
!= NULL
) {
157 filename
= qemu_find_file(QEMU_FILE_TYPE_BIOS
, boot_splash_filename
);
158 if (filename
== NULL
) {
159 error_report("failed to find file '%s'.", boot_splash_filename
);
163 /* loading file data */
164 file_data
= read_splashfile(filename
, &file_size
, &file_type
);
165 if (file_data
== NULL
) {
169 if (boot_splash_filedata
!= NULL
) {
170 g_free(boot_splash_filedata
);
172 boot_splash_filedata
= (uint8_t *)file_data
;
173 boot_splash_filedata_size
= file_size
;
176 if (file_type
== JPG_FILE
) {
177 fw_cfg_add_file(s
, "bootsplash.jpg",
178 boot_splash_filedata
, boot_splash_filedata_size
);
180 fw_cfg_add_file(s
, "bootsplash.bmp",
181 boot_splash_filedata
, boot_splash_filedata_size
);
187 static void fw_cfg_reboot(FWCfgState
*s
)
189 int reboot_timeout
= -1;
193 /* get user configuration */
194 QemuOptsList
*plist
= qemu_find_opts("boot-opts");
195 QemuOpts
*opts
= QTAILQ_FIRST(&plist
->head
);
197 temp
= qemu_opt_get(opts
, "reboot-timeout");
200 reboot_timeout
= strtol(p
, (char **)&p
, 10);
203 /* validate the input */
204 if (reboot_timeout
> 0xffff) {
205 error_report("reboot timeout is larger than 65535, force it to 65535.");
206 reboot_timeout
= 0xffff;
208 fw_cfg_add_file(s
, "etc/boot-fail-wait", g_memdup(&reboot_timeout
, 4), 4);
211 static void fw_cfg_write(FWCfgState
*s
, uint8_t value
)
213 int arch
= !!(s
->cur_entry
& FW_CFG_ARCH_LOCAL
);
214 FWCfgEntry
*e
= &s
->entries
[arch
][s
->cur_entry
& FW_CFG_ENTRY_MASK
];
216 trace_fw_cfg_write(s
, value
);
218 if (s
->cur_entry
& FW_CFG_WRITE_CHANNEL
&& e
->callback
&&
219 s
->cur_offset
< e
->len
) {
220 e
->data
[s
->cur_offset
++] = value
;
221 if (s
->cur_offset
== e
->len
) {
222 e
->callback(e
->callback_opaque
, e
->data
);
228 static int fw_cfg_select(FWCfgState
*s
, uint16_t key
)
233 if ((key
& FW_CFG_ENTRY_MASK
) >= FW_CFG_MAX_ENTRY
) {
234 s
->cur_entry
= FW_CFG_INVALID
;
241 trace_fw_cfg_select(s
, key
, ret
);
245 static uint8_t fw_cfg_read(FWCfgState
*s
)
247 int arch
= !!(s
->cur_entry
& FW_CFG_ARCH_LOCAL
);
248 FWCfgEntry
*e
= &s
->entries
[arch
][s
->cur_entry
& FW_CFG_ENTRY_MASK
];
251 if (s
->cur_entry
== FW_CFG_INVALID
|| !e
->data
|| s
->cur_offset
>= e
->len
)
254 if (e
->read_callback
) {
255 e
->read_callback(e
->callback_opaque
, s
->cur_offset
);
257 ret
= e
->data
[s
->cur_offset
++];
260 trace_fw_cfg_read(s
, ret
);
264 static uint64_t fw_cfg_data_mem_read(void *opaque
, hwaddr addr
,
267 return fw_cfg_read(opaque
);
270 static void fw_cfg_data_mem_write(void *opaque
, hwaddr addr
,
271 uint64_t value
, unsigned size
)
273 fw_cfg_write(opaque
, (uint8_t)value
);
276 static void fw_cfg_ctl_mem_write(void *opaque
, hwaddr addr
,
277 uint64_t value
, unsigned size
)
279 fw_cfg_select(opaque
, (uint16_t)value
);
282 static bool fw_cfg_ctl_mem_valid(void *opaque
, hwaddr addr
,
283 unsigned size
, bool is_write
)
285 return is_write
&& size
== 2;
288 static uint64_t fw_cfg_comb_read(void *opaque
, hwaddr addr
,
291 return fw_cfg_read(opaque
);
294 static void fw_cfg_comb_write(void *opaque
, hwaddr addr
,
295 uint64_t value
, unsigned size
)
299 fw_cfg_write(opaque
, (uint8_t)value
);
302 fw_cfg_select(opaque
, (uint16_t)value
);
307 static bool fw_cfg_comb_valid(void *opaque
, hwaddr addr
,
308 unsigned size
, bool is_write
)
310 return (size
== 1) || (is_write
&& size
== 2);
313 static const MemoryRegionOps fw_cfg_ctl_mem_ops
= {
314 .write
= fw_cfg_ctl_mem_write
,
315 .endianness
= DEVICE_NATIVE_ENDIAN
,
316 .valid
.accepts
= fw_cfg_ctl_mem_valid
,
319 static const MemoryRegionOps fw_cfg_data_mem_ops
= {
320 .read
= fw_cfg_data_mem_read
,
321 .write
= fw_cfg_data_mem_write
,
322 .endianness
= DEVICE_NATIVE_ENDIAN
,
324 .min_access_size
= 1,
325 .max_access_size
= 1,
329 static const MemoryRegionOps fw_cfg_comb_mem_ops
= {
330 .read
= fw_cfg_comb_read
,
331 .write
= fw_cfg_comb_write
,
332 .endianness
= DEVICE_LITTLE_ENDIAN
,
333 .valid
.accepts
= fw_cfg_comb_valid
,
336 static void fw_cfg_reset(DeviceState
*d
)
338 FWCfgState
*s
= FW_CFG(d
);
343 /* Save restore 32 bit int as uint16_t
344 This is a Big hack, but it is how the old state did it.
345 Or we broke compatibility in the state, or we can't use struct tm
348 static int get_uint32_as_uint16(QEMUFile
*f
, void *pv
, size_t size
)
351 *v
= qemu_get_be16(f
);
355 static void put_unused(QEMUFile
*f
, void *pv
, size_t size
)
357 fprintf(stderr
, "uint32_as_uint16 is only used for backward compatibility.\n");
358 fprintf(stderr
, "This functions shouldn't be called.\n");
361 static const VMStateInfo vmstate_hack_uint32_as_uint16
= {
362 .name
= "int32_as_uint16",
363 .get
= get_uint32_as_uint16
,
367 #define VMSTATE_UINT16_HACK(_f, _s, _t) \
368 VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_hack_uint32_as_uint16, uint32_t)
371 static bool is_version_1(void *opaque
, int version_id
)
373 return version_id
== 1;
376 static const VMStateDescription vmstate_fw_cfg
= {
379 .minimum_version_id
= 1,
380 .minimum_version_id_old
= 1,
381 .fields
= (VMStateField
[]) {
382 VMSTATE_UINT16(cur_entry
, FWCfgState
),
383 VMSTATE_UINT16_HACK(cur_offset
, FWCfgState
, is_version_1
),
384 VMSTATE_UINT32_V(cur_offset
, FWCfgState
, 2),
385 VMSTATE_END_OF_LIST()
389 static void fw_cfg_add_bytes_read_callback(FWCfgState
*s
, uint16_t key
,
390 FWCfgReadCallback callback
,
391 void *callback_opaque
,
392 void *data
, size_t len
)
394 int arch
= !!(key
& FW_CFG_ARCH_LOCAL
);
396 key
&= FW_CFG_ENTRY_MASK
;
398 assert(key
< FW_CFG_MAX_ENTRY
&& len
< UINT32_MAX
);
400 s
->entries
[arch
][key
].data
= data
;
401 s
->entries
[arch
][key
].len
= (uint32_t)len
;
402 s
->entries
[arch
][key
].read_callback
= callback
;
403 s
->entries
[arch
][key
].callback_opaque
= callback_opaque
;
406 void fw_cfg_add_bytes(FWCfgState
*s
, uint16_t key
, void *data
, size_t len
)
408 fw_cfg_add_bytes_read_callback(s
, key
, NULL
, NULL
, data
, len
);
411 void fw_cfg_add_string(FWCfgState
*s
, uint16_t key
, const char *value
)
413 size_t sz
= strlen(value
) + 1;
415 return fw_cfg_add_bytes(s
, key
, g_memdup(value
, sz
), sz
);
418 void fw_cfg_add_i16(FWCfgState
*s
, uint16_t key
, uint16_t value
)
422 copy
= g_malloc(sizeof(value
));
423 *copy
= cpu_to_le16(value
);
424 fw_cfg_add_bytes(s
, key
, copy
, sizeof(value
));
427 void fw_cfg_add_i32(FWCfgState
*s
, uint16_t key
, uint32_t value
)
431 copy
= g_malloc(sizeof(value
));
432 *copy
= cpu_to_le32(value
);
433 fw_cfg_add_bytes(s
, key
, copy
, sizeof(value
));
436 void fw_cfg_add_i64(FWCfgState
*s
, uint16_t key
, uint64_t value
)
440 copy
= g_malloc(sizeof(value
));
441 *copy
= cpu_to_le64(value
);
442 fw_cfg_add_bytes(s
, key
, copy
, sizeof(value
));
445 void fw_cfg_add_callback(FWCfgState
*s
, uint16_t key
, FWCfgCallback callback
,
446 void *callback_opaque
, void *data
, size_t len
)
448 int arch
= !!(key
& FW_CFG_ARCH_LOCAL
);
450 assert(key
& FW_CFG_WRITE_CHANNEL
);
452 key
&= FW_CFG_ENTRY_MASK
;
454 assert(key
< FW_CFG_MAX_ENTRY
&& len
<= UINT32_MAX
);
456 s
->entries
[arch
][key
].data
= data
;
457 s
->entries
[arch
][key
].len
= (uint32_t)len
;
458 s
->entries
[arch
][key
].callback_opaque
= callback_opaque
;
459 s
->entries
[arch
][key
].callback
= callback
;
462 void fw_cfg_add_file_callback(FWCfgState
*s
, const char *filename
,
463 FWCfgReadCallback callback
, void *callback_opaque
,
464 void *data
, size_t len
)
470 dsize
= sizeof(uint32_t) + sizeof(FWCfgFile
) * FW_CFG_FILE_SLOTS
;
471 s
->files
= g_malloc0(dsize
);
472 fw_cfg_add_bytes(s
, FW_CFG_FILE_DIR
, s
->files
, dsize
);
475 index
= be32_to_cpu(s
->files
->count
);
476 assert(index
< FW_CFG_FILE_SLOTS
);
478 fw_cfg_add_bytes_read_callback(s
, FW_CFG_FILE_FIRST
+ index
,
479 callback
, callback_opaque
, data
, len
);
481 pstrcpy(s
->files
->f
[index
].name
, sizeof(s
->files
->f
[index
].name
),
483 for (i
= 0; i
< index
; i
++) {
484 if (strcmp(s
->files
->f
[index
].name
, s
->files
->f
[i
].name
) == 0) {
485 trace_fw_cfg_add_file_dupe(s
, s
->files
->f
[index
].name
);
490 s
->files
->f
[index
].size
= cpu_to_be32(len
);
491 s
->files
->f
[index
].select
= cpu_to_be16(FW_CFG_FILE_FIRST
+ index
);
492 trace_fw_cfg_add_file(s
, index
, s
->files
->f
[index
].name
, len
);
494 s
->files
->count
= cpu_to_be32(index
+1);
497 void fw_cfg_add_file(FWCfgState
*s
, const char *filename
,
498 void *data
, size_t len
)
500 fw_cfg_add_file_callback(s
, filename
, NULL
, NULL
, data
, len
);
503 static void fw_cfg_machine_ready(struct Notifier
*n
, void *data
)
506 FWCfgState
*s
= container_of(n
, FWCfgState
, machine_ready
);
507 char *bootindex
= get_boot_devices_list(&len
, false);
509 fw_cfg_add_file(s
, "bootorder", (uint8_t*)bootindex
, len
);
512 FWCfgState
*fw_cfg_init(uint32_t ctl_port
, uint32_t data_port
,
513 hwaddr ctl_addr
, hwaddr data_addr
)
519 dev
= qdev_create(NULL
, TYPE_FW_CFG
);
520 qdev_prop_set_uint32(dev
, "ctl_iobase", ctl_port
);
521 qdev_prop_set_uint32(dev
, "data_iobase", data_port
);
522 d
= SYS_BUS_DEVICE(dev
);
526 assert(!object_resolve_path(FW_CFG_PATH
, NULL
));
528 object_property_add_child(qdev_get_machine(), FW_CFG_NAME
, OBJECT(s
), NULL
);
530 qdev_init_nofail(dev
);
533 sysbus_mmio_map(d
, 0, ctl_addr
);
536 sysbus_mmio_map(d
, 1, data_addr
);
538 fw_cfg_add_bytes(s
, FW_CFG_SIGNATURE
, (char *)"QEMU", 4);
539 fw_cfg_add_bytes(s
, FW_CFG_UUID
, qemu_uuid
, 16);
540 fw_cfg_add_i16(s
, FW_CFG_NOGRAPHIC
, (uint16_t)(display_type
== DT_NOGRAPHIC
));
541 fw_cfg_add_i16(s
, FW_CFG_NB_CPUS
, (uint16_t)smp_cpus
);
542 fw_cfg_add_i16(s
, FW_CFG_BOOT_MENU
, (uint16_t)boot_menu
);
543 fw_cfg_bootsplash(s
);
546 s
->machine_ready
.notify
= fw_cfg_machine_ready
;
547 qemu_add_machine_init_done_notifier(&s
->machine_ready
);
552 static void fw_cfg_initfn(Object
*obj
)
554 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
555 FWCfgState
*s
= FW_CFG(obj
);
557 memory_region_init_io(&s
->ctl_iomem
, OBJECT(s
), &fw_cfg_ctl_mem_ops
, s
,
558 "fwcfg.ctl", FW_CFG_SIZE
);
559 sysbus_init_mmio(sbd
, &s
->ctl_iomem
);
560 memory_region_init_io(&s
->data_iomem
, OBJECT(s
), &fw_cfg_data_mem_ops
, s
,
561 "fwcfg.data", FW_CFG_DATA_SIZE
);
562 sysbus_init_mmio(sbd
, &s
->data_iomem
);
563 /* In case ctl and data overlap: */
564 memory_region_init_io(&s
->comb_iomem
, OBJECT(s
), &fw_cfg_comb_mem_ops
, s
,
565 "fwcfg", FW_CFG_SIZE
);
568 static void fw_cfg_realize(DeviceState
*dev
, Error
**errp
)
570 FWCfgState
*s
= FW_CFG(dev
);
571 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
574 if (s
->ctl_iobase
+ 1 == s
->data_iobase
) {
575 sysbus_add_io(sbd
, s
->ctl_iobase
, &s
->comb_iomem
);
578 sysbus_add_io(sbd
, s
->ctl_iobase
, &s
->ctl_iomem
);
580 if (s
->data_iobase
) {
581 sysbus_add_io(sbd
, s
->data_iobase
, &s
->data_iomem
);
586 static Property fw_cfg_properties
[] = {
587 DEFINE_PROP_UINT32("ctl_iobase", FWCfgState
, ctl_iobase
, -1),
588 DEFINE_PROP_UINT32("data_iobase", FWCfgState
, data_iobase
, -1),
589 DEFINE_PROP_END_OF_LIST(),
592 FWCfgState
*fw_cfg_find(void)
594 return FW_CFG(object_resolve_path(FW_CFG_PATH
, NULL
));
597 static void fw_cfg_class_init(ObjectClass
*klass
, void *data
)
599 DeviceClass
*dc
= DEVICE_CLASS(klass
);
601 dc
->realize
= fw_cfg_realize
;
602 dc
->reset
= fw_cfg_reset
;
603 dc
->vmsd
= &vmstate_fw_cfg
;
604 dc
->props
= fw_cfg_properties
;
607 static const TypeInfo fw_cfg_info
= {
609 .parent
= TYPE_SYS_BUS_DEVICE
,
610 .instance_size
= sizeof(FWCfgState
),
611 .instance_init
= fw_cfg_initfn
,
612 .class_init
= fw_cfg_class_init
,
615 static void fw_cfg_register_types(void)
617 type_register_static(&fw_cfg_info
);
620 type_init(fw_cfg_register_types
)