2 * QEMU PC System Firmware
4 * Copyright (c) 2003-2004 Fabrice Bellard
5 * Copyright (c) 2011-2012 Intel Corporation
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 #include "sysemu/blockdev.h"
27 #include "qemu/error-report.h"
28 #include "hw/sysbus.h"
30 #include "hw/i386/pc.h"
31 #include "hw/boards.h"
32 #include "hw/loader.h"
33 #include "sysemu/sysemu.h"
34 #include "hw/block/flash.h"
35 #include "sysemu/kvm.h"
37 #define BIOS_FILENAME "bios.bin"
39 typedef struct PcSysFwDevice
{
45 static void pc_isa_bios_init(MemoryRegion
*rom_memory
,
46 MemoryRegion
*flash_mem
,
50 MemoryRegion
*isa_bios
;
52 void *flash_ptr
, *isa_bios_ptr
;
54 flash_size
= memory_region_size(flash_mem
);
56 /* map the last 128KB of the BIOS in ISA space */
57 isa_bios_size
= flash_size
;
58 if (isa_bios_size
> (128 * 1024)) {
59 isa_bios_size
= 128 * 1024;
61 isa_bios
= g_malloc(sizeof(*isa_bios
));
62 memory_region_init_ram(isa_bios
, NULL
, "isa-bios", isa_bios_size
);
63 vmstate_register_ram_global(isa_bios
);
64 memory_region_add_subregion_overlap(rom_memory
,
65 0x100000 - isa_bios_size
,
69 /* copy ISA rom image from top of flash memory */
70 flash_ptr
= memory_region_get_ram_ptr(flash_mem
);
71 isa_bios_ptr
= memory_region_get_ram_ptr(isa_bios
);
73 ((uint8_t*)flash_ptr
) + (flash_size
- isa_bios_size
),
76 memory_region_set_readonly(isa_bios
, true);
79 static void pc_fw_add_pflash_drv(void)
85 if (bios_name
== NULL
) {
86 bios_name
= BIOS_FILENAME
;
88 filename
= qemu_find_file(QEMU_FILE_TYPE_BIOS
, bios_name
);
90 error_report("Can't open BIOS image %s", bios_name
);
94 opts
= drive_add(IF_PFLASH
, -1, filename
, "readonly=on");
102 machine
= find_default_machine();
103 if (machine
== NULL
) {
107 if (!drive_init(opts
, machine
->block_default_type
)) {
112 static void pc_system_flash_init(MemoryRegion
*rom_memory
,
113 DriveInfo
*pflash_drv
)
115 BlockDriverState
*bdrv
;
118 int sector_bits
, sector_size
;
119 pflash_t
*system_flash
;
120 MemoryRegion
*flash_mem
;
122 bdrv
= pflash_drv
->bdrv
;
123 size
= bdrv_getlength(pflash_drv
->bdrv
);
125 sector_size
= 1 << sector_bits
;
127 if ((size
% sector_size
) != 0) {
129 "qemu: PC system firmware (pflash) must be a multiple of 0x%x\n",
134 phys_addr
= 0x100000000ULL
- size
;
135 system_flash
= pflash_cfi01_register(phys_addr
, NULL
, "system.flash", size
,
136 bdrv
, sector_size
, size
>> sector_bits
,
137 1, 0x0000, 0x0000, 0x0000, 0x0000, 0);
138 flash_mem
= pflash_cfi01_get_memory(system_flash
);
140 pc_isa_bios_init(rom_memory
, flash_mem
, size
);
143 static void old_pc_system_rom_init(MemoryRegion
*rom_memory
, bool isapc_ram_fw
)
146 MemoryRegion
*bios
, *isa_bios
;
147 int bios_size
, isa_bios_size
;
151 if (bios_name
== NULL
) {
152 bios_name
= BIOS_FILENAME
;
154 filename
= qemu_find_file(QEMU_FILE_TYPE_BIOS
, bios_name
);
156 bios_size
= get_image_size(filename
);
160 if (bios_size
<= 0 ||
161 (bios_size
% 65536) != 0) {
164 bios
= g_malloc(sizeof(*bios
));
165 memory_region_init_ram(bios
, NULL
, "pc.bios", bios_size
);
166 vmstate_register_ram_global(bios
);
168 memory_region_set_readonly(bios
, true);
170 ret
= rom_add_file_fixed(bios_name
, (uint32_t)(-bios_size
), -1);
173 fprintf(stderr
, "qemu: could not load PC BIOS '%s'\n", bios_name
);
180 /* map the last 128KB of the BIOS in ISA space */
181 isa_bios_size
= bios_size
;
182 if (isa_bios_size
> (128 * 1024)) {
183 isa_bios_size
= 128 * 1024;
185 isa_bios
= g_malloc(sizeof(*isa_bios
));
186 memory_region_init_alias(isa_bios
, NULL
, "isa-bios", bios
,
187 bios_size
- isa_bios_size
, isa_bios_size
);
188 memory_region_add_subregion_overlap(rom_memory
,
189 0x100000 - isa_bios_size
,
193 memory_region_set_readonly(isa_bios
, true);
196 /* map all the bios at the top of memory */
197 memory_region_add_subregion(rom_memory
,
198 (uint32_t)(-bios_size
),
203 * Bug-compatible flash vs. ROM selection enabled?
204 * A few older machines enable this.
206 bool pc_sysfw_flash_vs_rom_bug_compatible
;
208 void pc_system_firmware_init(MemoryRegion
*rom_memory
)
210 DriveInfo
*pflash_drv
;
211 PcSysFwDevice
*sysfw_dev
;
214 * TODO This device exists only so that users can switch between
215 * use of flash and ROM for the BIOS. The ability to switch was
216 * created because flash doesn't work with KVM. Once it does, we
217 * should drop this device.
219 sysfw_dev
= (PcSysFwDevice
*) qdev_create(NULL
, "pc-sysfw");
221 qdev_init_nofail(DEVICE(sysfw_dev
));
223 pflash_drv
= drive_get(IF_PFLASH
, 0, 0);
225 if (pc_sysfw_flash_vs_rom_bug_compatible
) {
227 * This is a Bad Idea, because it makes enabling/disabling KVM
228 * guest-visible. Do it only in bug-compatibility mode.
231 if (pflash_drv
!= NULL
) {
232 fprintf(stderr
, "qemu: pflash cannot be used with kvm enabled\n");
235 /* In old pc_sysfw_flash_vs_rom_bug_compatible mode, we assume
236 * that KVM cannot execute from device memory. In this case, we
237 * use old rom based firmware initialization for KVM. But, since
238 * this is different from non-kvm mode, this behavior is
240 sysfw_dev
->rom_only
= 1;
243 } else if (pflash_drv
== NULL
) {
244 /* When a pflash drive is not found, use rom-mode */
245 sysfw_dev
->rom_only
= 1;
246 } else if (kvm_enabled() && !kvm_readonly_mem_enabled()) {
247 /* Older KVM cannot execute from device memory. So, flash memory
248 * cannot be used unless the readonly memory kvm capability is present. */
249 fprintf(stderr
, "qemu: pflash with kvm requires KVM readonly memory support\n");
253 /* If rom-mode is active, use the old pc system rom initialization. */
254 if (sysfw_dev
->rom_only
) {
255 old_pc_system_rom_init(rom_memory
, sysfw_dev
->isapc_ram_fw
);
259 /* If a pflash drive is not found, then create one using
260 the bios filename. */
261 if (pflash_drv
== NULL
) {
262 pc_fw_add_pflash_drv();
263 pflash_drv
= drive_get(IF_PFLASH
, 0, 0);
266 if (pflash_drv
!= NULL
) {
267 pc_system_flash_init(rom_memory
, pflash_drv
);
269 fprintf(stderr
, "qemu: PC system firmware (pflash) not available\n");
274 static Property pcsysfw_properties
[] = {
275 DEFINE_PROP_UINT8("isapc_ram_fw", PcSysFwDevice
, isapc_ram_fw
, 0),
276 DEFINE_PROP_UINT8("rom_only", PcSysFwDevice
, rom_only
, 0),
277 DEFINE_PROP_END_OF_LIST(),
280 static int pcsysfw_init(DeviceState
*dev
)
285 static void pcsysfw_class_init (ObjectClass
*klass
, void *data
)
287 DeviceClass
*dc
= DEVICE_CLASS (klass
);
289 dc
->desc
= "PC System Firmware";
290 dc
->init
= pcsysfw_init
;
291 dc
->props
= pcsysfw_properties
;
294 static const TypeInfo pcsysfw_info
= {
296 .parent
= TYPE_SYS_BUS_DEVICE
,
297 .instance_size
= sizeof (PcSysFwDevice
),
298 .class_init
= pcsysfw_class_init
,
301 static void pcsysfw_register (void)
303 type_register_static (&pcsysfw_info
);
306 type_init (pcsysfw_register
);