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"
29 #include "qemu/error-report.h"
30 #include "qemu/config-file.h"
32 /* debug firmware config */
33 //#define DEBUG_FW_CFG
36 #define FW_CFG_DPRINTF(fmt, ...) \
37 do { printf("FW_CFG: " fmt , ## __VA_ARGS__); } while (0)
39 #define FW_CFG_DPRINTF(fmt, ...)
43 #define FW_CFG_DATA_SIZE 1
45 typedef struct FWCfgEntry
{
48 void *callback_opaque
;
49 FWCfgCallback callback
;
54 MemoryRegion ctl_iomem
, data_iomem
, comb_iomem
;
55 uint32_t ctl_iobase
, data_iobase
;
56 FWCfgEntry entries
[2][FW_CFG_MAX_ENTRY
];
60 Notifier machine_ready
;
66 static char *read_splashfile(char *filename
, int *file_sizep
, int *file_typep
)
72 unsigned int filehead
= 0;
75 res
= g_file_get_contents(filename
, &content
, (gsize
*)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 FW_CFG_DPRINTF("write %d\n", 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 FW_CFG_DPRINTF("select key %d (%sfound)\n", key
, ret
? "" : "not ");
246 static uint8_t fw_cfg_read(FWCfgState
*s
)
248 int arch
= !!(s
->cur_entry
& FW_CFG_ARCH_LOCAL
);
249 FWCfgEntry
*e
= &s
->entries
[arch
][s
->cur_entry
& FW_CFG_ENTRY_MASK
];
252 if (s
->cur_entry
== FW_CFG_INVALID
|| !e
->data
|| s
->cur_offset
>= e
->len
)
255 ret
= e
->data
[s
->cur_offset
++];
257 FW_CFG_DPRINTF("read %d\n", ret
);
262 static uint64_t fw_cfg_data_mem_read(void *opaque
, hwaddr addr
,
265 return fw_cfg_read(opaque
);
268 static void fw_cfg_data_mem_write(void *opaque
, hwaddr addr
,
269 uint64_t value
, unsigned size
)
271 fw_cfg_write(opaque
, (uint8_t)value
);
274 static void fw_cfg_ctl_mem_write(void *opaque
, hwaddr addr
,
275 uint64_t value
, unsigned size
)
277 fw_cfg_select(opaque
, (uint16_t)value
);
280 static bool fw_cfg_ctl_mem_valid(void *opaque
, hwaddr addr
,
281 unsigned size
, bool is_write
)
283 return is_write
&& size
== 2;
286 static uint64_t fw_cfg_comb_read(void *opaque
, hwaddr addr
,
289 return fw_cfg_read(opaque
);
292 static void fw_cfg_comb_write(void *opaque
, hwaddr addr
,
293 uint64_t value
, unsigned size
)
297 fw_cfg_write(opaque
, (uint8_t)value
);
300 fw_cfg_select(opaque
, (uint16_t)value
);
305 static bool fw_cfg_comb_valid(void *opaque
, hwaddr addr
,
306 unsigned size
, bool is_write
)
308 return (size
== 1) || (is_write
&& size
== 2);
311 static const MemoryRegionOps fw_cfg_ctl_mem_ops
= {
312 .write
= fw_cfg_ctl_mem_write
,
313 .endianness
= DEVICE_NATIVE_ENDIAN
,
314 .valid
.accepts
= fw_cfg_ctl_mem_valid
,
317 static const MemoryRegionOps fw_cfg_data_mem_ops
= {
318 .read
= fw_cfg_data_mem_read
,
319 .write
= fw_cfg_data_mem_write
,
320 .endianness
= DEVICE_NATIVE_ENDIAN
,
322 .min_access_size
= 1,
323 .max_access_size
= 1,
327 static const MemoryRegionOps fw_cfg_comb_mem_ops
= {
328 .read
= fw_cfg_comb_read
,
329 .write
= fw_cfg_comb_write
,
330 .endianness
= DEVICE_NATIVE_ENDIAN
,
331 .valid
.accepts
= fw_cfg_comb_valid
,
334 static void fw_cfg_reset(DeviceState
*d
)
336 FWCfgState
*s
= DO_UPCAST(FWCfgState
, busdev
.qdev
, d
);
341 /* Save restore 32 bit int as uint16_t
342 This is a Big hack, but it is how the old state did it.
343 Or we broke compatibility in the state, or we can't use struct tm
346 static int get_uint32_as_uint16(QEMUFile
*f
, void *pv
, size_t size
)
349 *v
= qemu_get_be16(f
);
353 static void put_unused(QEMUFile
*f
, void *pv
, size_t size
)
355 fprintf(stderr
, "uint32_as_uint16 is only used for backward compatibility.\n");
356 fprintf(stderr
, "This functions shouldn't be called.\n");
359 static const VMStateInfo vmstate_hack_uint32_as_uint16
= {
360 .name
= "int32_as_uint16",
361 .get
= get_uint32_as_uint16
,
365 #define VMSTATE_UINT16_HACK(_f, _s, _t) \
366 VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_hack_uint32_as_uint16, uint32_t)
369 static bool is_version_1(void *opaque
, int version_id
)
371 return version_id
== 1;
374 static const VMStateDescription vmstate_fw_cfg
= {
377 .minimum_version_id
= 1,
378 .minimum_version_id_old
= 1,
379 .fields
= (VMStateField
[]) {
380 VMSTATE_UINT16(cur_entry
, FWCfgState
),
381 VMSTATE_UINT16_HACK(cur_offset
, FWCfgState
, is_version_1
),
382 VMSTATE_UINT32_V(cur_offset
, FWCfgState
, 2),
383 VMSTATE_END_OF_LIST()
387 int fw_cfg_add_bytes(FWCfgState
*s
, uint16_t key
, uint8_t *data
, uint32_t len
)
389 int arch
= !!(key
& FW_CFG_ARCH_LOCAL
);
391 key
&= FW_CFG_ENTRY_MASK
;
393 if (key
>= FW_CFG_MAX_ENTRY
)
396 s
->entries
[arch
][key
].data
= data
;
397 s
->entries
[arch
][key
].len
= len
;
402 int fw_cfg_add_i16(FWCfgState
*s
, uint16_t key
, uint16_t value
)
406 copy
= g_malloc(sizeof(value
));
407 *copy
= cpu_to_le16(value
);
408 return fw_cfg_add_bytes(s
, key
, (uint8_t *)copy
, sizeof(value
));
411 int fw_cfg_add_i32(FWCfgState
*s
, uint16_t key
, uint32_t value
)
415 copy
= g_malloc(sizeof(value
));
416 *copy
= cpu_to_le32(value
);
417 return fw_cfg_add_bytes(s
, key
, (uint8_t *)copy
, sizeof(value
));
420 int fw_cfg_add_i64(FWCfgState
*s
, uint16_t key
, uint64_t value
)
424 copy
= g_malloc(sizeof(value
));
425 *copy
= cpu_to_le64(value
);
426 return fw_cfg_add_bytes(s
, key
, (uint8_t *)copy
, sizeof(value
));
429 int fw_cfg_add_callback(FWCfgState
*s
, uint16_t key
, FWCfgCallback callback
,
430 void *callback_opaque
, uint8_t *data
, size_t len
)
432 int arch
= !!(key
& FW_CFG_ARCH_LOCAL
);
434 if (!(key
& FW_CFG_WRITE_CHANNEL
))
437 key
&= FW_CFG_ENTRY_MASK
;
439 if (key
>= FW_CFG_MAX_ENTRY
|| len
> 65535)
442 s
->entries
[arch
][key
].data
= data
;
443 s
->entries
[arch
][key
].len
= len
;
444 s
->entries
[arch
][key
].callback_opaque
= callback_opaque
;
445 s
->entries
[arch
][key
].callback
= callback
;
450 int fw_cfg_add_file(FWCfgState
*s
, const char *filename
, uint8_t *data
,
456 int dsize
= sizeof(uint32_t) + sizeof(FWCfgFile
) * FW_CFG_FILE_SLOTS
;
457 s
->files
= g_malloc0(dsize
);
458 fw_cfg_add_bytes(s
, FW_CFG_FILE_DIR
, (uint8_t*)s
->files
, dsize
);
461 index
= be32_to_cpu(s
->files
->count
);
462 if (index
== FW_CFG_FILE_SLOTS
) {
463 fprintf(stderr
, "fw_cfg: out of file slots\n");
467 fw_cfg_add_bytes(s
, FW_CFG_FILE_FIRST
+ index
, data
, len
);
469 pstrcpy(s
->files
->f
[index
].name
, sizeof(s
->files
->f
[index
].name
),
471 for (i
= 0; i
< index
; i
++) {
472 if (strcmp(s
->files
->f
[index
].name
, s
->files
->f
[i
].name
) == 0) {
473 FW_CFG_DPRINTF("%s: skip duplicate: %s\n", __FUNCTION__
,
474 s
->files
->f
[index
].name
);
479 s
->files
->f
[index
].size
= cpu_to_be32(len
);
480 s
->files
->f
[index
].select
= cpu_to_be16(FW_CFG_FILE_FIRST
+ index
);
481 FW_CFG_DPRINTF("%s: #%d: %s (%d bytes)\n", __FUNCTION__
,
482 index
, s
->files
->f
[index
].name
, len
);
484 s
->files
->count
= cpu_to_be32(index
+1);
488 static void fw_cfg_machine_ready(struct Notifier
*n
, void *data
)
491 FWCfgState
*s
= container_of(n
, FWCfgState
, machine_ready
);
492 char *bootindex
= get_boot_devices_list(&len
);
494 fw_cfg_add_file(s
, "bootorder", (uint8_t*)bootindex
, len
);
497 FWCfgState
*fw_cfg_init(uint32_t ctl_port
, uint32_t data_port
,
498 hwaddr ctl_addr
, hwaddr data_addr
)
504 dev
= qdev_create(NULL
, "fw_cfg");
505 qdev_prop_set_uint32(dev
, "ctl_iobase", ctl_port
);
506 qdev_prop_set_uint32(dev
, "data_iobase", data_port
);
507 qdev_init_nofail(dev
);
508 d
= sysbus_from_qdev(dev
);
510 s
= DO_UPCAST(FWCfgState
, busdev
.qdev
, dev
);
513 sysbus_mmio_map(d
, 0, ctl_addr
);
516 sysbus_mmio_map(d
, 1, data_addr
);
518 fw_cfg_add_bytes(s
, FW_CFG_SIGNATURE
, (uint8_t *)"QEMU", 4);
519 fw_cfg_add_bytes(s
, FW_CFG_UUID
, qemu_uuid
, 16);
520 fw_cfg_add_i16(s
, FW_CFG_NOGRAPHIC
, (uint16_t)(display_type
== DT_NOGRAPHIC
));
521 fw_cfg_add_i16(s
, FW_CFG_NB_CPUS
, (uint16_t)smp_cpus
);
522 fw_cfg_add_i16(s
, FW_CFG_MAX_CPUS
, (uint16_t)max_cpus
);
523 fw_cfg_add_i16(s
, FW_CFG_BOOT_MENU
, (uint16_t)boot_menu
);
524 fw_cfg_bootsplash(s
);
527 s
->machine_ready
.notify
= fw_cfg_machine_ready
;
528 qemu_add_machine_init_done_notifier(&s
->machine_ready
);
533 static int fw_cfg_init1(SysBusDevice
*dev
)
535 FWCfgState
*s
= FROM_SYSBUS(FWCfgState
, dev
);
537 memory_region_init_io(&s
->ctl_iomem
, &fw_cfg_ctl_mem_ops
, s
,
538 "fwcfg.ctl", FW_CFG_SIZE
);
539 sysbus_init_mmio(dev
, &s
->ctl_iomem
);
540 memory_region_init_io(&s
->data_iomem
, &fw_cfg_data_mem_ops
, s
,
541 "fwcfg.data", FW_CFG_DATA_SIZE
);
542 sysbus_init_mmio(dev
, &s
->data_iomem
);
543 /* In case ctl and data overlap: */
544 memory_region_init_io(&s
->comb_iomem
, &fw_cfg_comb_mem_ops
, s
,
545 "fwcfg", FW_CFG_SIZE
);
547 if (s
->ctl_iobase
+ 1 == s
->data_iobase
) {
548 sysbus_add_io(dev
, s
->ctl_iobase
, &s
->comb_iomem
);
551 sysbus_add_io(dev
, s
->ctl_iobase
, &s
->ctl_iomem
);
553 if (s
->data_iobase
) {
554 sysbus_add_io(dev
, s
->data_iobase
, &s
->data_iomem
);
560 static Property fw_cfg_properties
[] = {
561 DEFINE_PROP_HEX32("ctl_iobase", FWCfgState
, ctl_iobase
, -1),
562 DEFINE_PROP_HEX32("data_iobase", FWCfgState
, data_iobase
, -1),
563 DEFINE_PROP_END_OF_LIST(),
566 static void fw_cfg_class_init(ObjectClass
*klass
, void *data
)
568 DeviceClass
*dc
= DEVICE_CLASS(klass
);
569 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
571 k
->init
= fw_cfg_init1
;
573 dc
->reset
= fw_cfg_reset
;
574 dc
->vmsd
= &vmstate_fw_cfg
;
575 dc
->props
= fw_cfg_properties
;
578 static TypeInfo fw_cfg_info
= {
580 .parent
= TYPE_SYS_BUS_DEVICE
,
581 .instance_size
= sizeof(FWCfgState
),
582 .class_init
= fw_cfg_class_init
,
585 static void fw_cfg_register_types(void)
587 type_register_static(&fw_cfg_info
);
590 type_init(fw_cfg_register_types
)