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"
31 #include "hw/boards.h"
32 #include "hw/loader.h"
33 #include "sysemu/sysemu.h"
35 #include "sysemu/kvm.h"
37 #define BIOS_FILENAME "bios.bin"
39 typedef struct PcSysFwDevice
{
44 static void pc_isa_bios_init(MemoryRegion
*rom_memory
,
45 MemoryRegion
*flash_mem
,
49 MemoryRegion
*isa_bios
;
51 void *flash_ptr
, *isa_bios_ptr
;
53 flash_size
= memory_region_size(flash_mem
);
55 /* map the last 128KB of the BIOS in ISA space */
56 isa_bios_size
= flash_size
;
57 if (isa_bios_size
> (128 * 1024)) {
58 isa_bios_size
= 128 * 1024;
60 isa_bios
= g_malloc(sizeof(*isa_bios
));
61 memory_region_init_ram(isa_bios
, "isa-bios", isa_bios_size
);
62 vmstate_register_ram_global(isa_bios
);
63 memory_region_add_subregion_overlap(rom_memory
,
64 0x100000 - isa_bios_size
,
68 /* copy ISA rom image from top of flash memory */
69 flash_ptr
= memory_region_get_ram_ptr(flash_mem
);
70 isa_bios_ptr
= memory_region_get_ram_ptr(isa_bios
);
72 ((uint8_t*)flash_ptr
) + (flash_size
- isa_bios_size
),
75 memory_region_set_readonly(isa_bios
, true);
78 static void pc_fw_add_pflash_drv(void)
84 if (bios_name
== NULL
) {
85 bios_name
= BIOS_FILENAME
;
87 filename
= qemu_find_file(QEMU_FILE_TYPE_BIOS
, bios_name
);
89 error_report("Can't open BIOS image %s", bios_name
);
93 opts
= drive_add(IF_PFLASH
, -1, filename
, "readonly=on");
101 machine
= find_default_machine();
102 if (machine
== NULL
) {
106 if (!drive_init(opts
, machine
->block_default_type
)) {
111 static void pc_system_flash_init(MemoryRegion
*rom_memory
,
112 DriveInfo
*pflash_drv
)
114 BlockDriverState
*bdrv
;
117 int sector_bits
, sector_size
;
118 pflash_t
*system_flash
;
119 MemoryRegion
*flash_mem
;
121 bdrv
= pflash_drv
->bdrv
;
122 size
= bdrv_getlength(pflash_drv
->bdrv
);
124 sector_size
= 1 << sector_bits
;
126 if ((size
% sector_size
) != 0) {
128 "qemu: PC system firmware (pflash) must be a multiple of 0x%x\n",
133 phys_addr
= 0x100000000ULL
- size
;
134 system_flash
= pflash_cfi01_register(phys_addr
, NULL
, "system.flash", size
,
135 bdrv
, sector_size
, size
>> sector_bits
,
136 1, 0x0000, 0x0000, 0x0000, 0x0000, 0);
137 flash_mem
= pflash_cfi01_get_memory(system_flash
);
139 pc_isa_bios_init(rom_memory
, flash_mem
, size
);
142 static void old_pc_system_rom_init(MemoryRegion
*rom_memory
)
145 MemoryRegion
*bios
, *isa_bios
;
146 int bios_size
, isa_bios_size
;
150 if (bios_name
== NULL
) {
151 bios_name
= BIOS_FILENAME
;
153 filename
= qemu_find_file(QEMU_FILE_TYPE_BIOS
, bios_name
);
155 bios_size
= get_image_size(filename
);
159 if (bios_size
<= 0 ||
160 (bios_size
% 65536) != 0) {
163 bios
= g_malloc(sizeof(*bios
));
164 memory_region_init_ram(bios
, "pc.bios", bios_size
);
165 vmstate_register_ram_global(bios
);
166 memory_region_set_readonly(bios
, true);
167 ret
= rom_add_file_fixed(bios_name
, (uint32_t)(-bios_size
), -1);
170 fprintf(stderr
, "qemu: could not load PC BIOS '%s'\n", bios_name
);
177 /* map the last 128KB of the BIOS in ISA space */
178 isa_bios_size
= bios_size
;
179 if (isa_bios_size
> (128 * 1024)) {
180 isa_bios_size
= 128 * 1024;
182 isa_bios
= g_malloc(sizeof(*isa_bios
));
183 memory_region_init_alias(isa_bios
, "isa-bios", bios
,
184 bios_size
- isa_bios_size
, isa_bios_size
);
185 memory_region_add_subregion_overlap(rom_memory
,
186 0x100000 - isa_bios_size
,
189 memory_region_set_readonly(isa_bios
, true);
191 /* map all the bios at the top of memory */
192 memory_region_add_subregion(rom_memory
,
193 (uint32_t)(-bios_size
),
197 void pc_system_firmware_init(MemoryRegion
*rom_memory
)
199 DriveInfo
*pflash_drv
;
200 PcSysFwDevice
*sysfw_dev
;
202 sysfw_dev
= (PcSysFwDevice
*) qdev_create(NULL
, "pc-sysfw");
204 qdev_init_nofail(DEVICE(sysfw_dev
));
206 if (sysfw_dev
->rom_only
) {
207 old_pc_system_rom_init(rom_memory
);
211 pflash_drv
= drive_get(IF_PFLASH
, 0, 0);
213 /* Currently KVM cannot execute from device memory.
214 Use old rom based firmware initialization for KVM. */
216 if (pflash_drv
!= NULL
) {
217 fprintf(stderr
, "qemu: pflash cannot be used with kvm enabled\n");
220 sysfw_dev
->rom_only
= 1;
221 old_pc_system_rom_init(rom_memory
);
226 /* If a pflash drive is not found, then create one using
227 the bios filename. */
228 if (pflash_drv
== NULL
) {
229 pc_fw_add_pflash_drv();
230 pflash_drv
= drive_get(IF_PFLASH
, 0, 0);
233 if (pflash_drv
!= NULL
) {
234 pc_system_flash_init(rom_memory
, pflash_drv
);
236 fprintf(stderr
, "qemu: PC system firmware (pflash) not available\n");
241 static Property pcsysfw_properties
[] = {
242 DEFINE_PROP_UINT8("rom_only", PcSysFwDevice
, rom_only
, 0),
243 DEFINE_PROP_END_OF_LIST(),
246 static int pcsysfw_init(DeviceState
*dev
)
251 static void pcsysfw_class_init (ObjectClass
*klass
, void *data
)
253 DeviceClass
*dc
= DEVICE_CLASS (klass
);
255 dc
->desc
= "PC System Firmware";
256 dc
->init
= pcsysfw_init
;
257 dc
->props
= pcsysfw_properties
;
260 static const TypeInfo pcsysfw_info
= {
262 .parent
= TYPE_SYS_BUS_DEVICE
,
263 .instance_size
= sizeof (PcSysFwDevice
),
264 .class_init
= pcsysfw_class_init
,
267 static void pcsysfw_register (void)
269 type_register_static (&pcsysfw_info
);
272 type_init (pcsysfw_register
);