blob: 1902caba91016d062b1da7a8e9f7ee969253005c [file] [log] [blame]
Jordan Justencbc5b5f2012-02-21 23:18:51 -08001/*
2 * QEMU PC System Firmware
3 *
4 * Copyright (c) 2003-2004 Fabrice Bellard
5 * Copyright (c) 2011-2012 Intel Corporation
6 *
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:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
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
23 * THE SOFTWARE.
24 */
25
Paolo Bonzini9c17d612012-12-17 18:20:04 +010026#include "sysemu/blockdev.h"
Paolo Bonzinib4a42f82013-02-04 11:37:52 +010027#include "qemu/error-report.h"
Paolo Bonzini83c9f4c2013-02-04 15:40:22 +010028#include "hw/sysbus.h"
29#include "hw/hw.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010030#include "hw/i386/pc.h"
Jordan Justenbd183c72012-02-21 23:18:53 -080031#include "hw/boards.h"
Paolo Bonzini83c9f4c2013-02-04 15:40:22 +010032#include "hw/loader.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010033#include "sysemu/sysemu.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010034#include "hw/block/flash.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010035#include "sysemu/kvm.h"
Jordan Justencbc5b5f2012-02-21 23:18:51 -080036
37#define BIOS_FILENAME "bios.bin"
38
Jordan Justen90ccf9f2012-02-21 23:18:52 -080039typedef struct PcSysFwDevice {
40 SysBusDevice busdev;
Jordan Justendade9222013-05-29 01:27:24 -070041 uint8_t isapc_ram_fw;
Jordan Justen90ccf9f2012-02-21 23:18:52 -080042} PcSysFwDevice;
43
Jordan Justenbd183c72012-02-21 23:18:53 -080044static void pc_isa_bios_init(MemoryRegion *rom_memory,
45 MemoryRegion *flash_mem,
46 int ram_size)
47{
48 int isa_bios_size;
49 MemoryRegion *isa_bios;
50 uint64_t flash_size;
51 void *flash_ptr, *isa_bios_ptr;
52
53 flash_size = memory_region_size(flash_mem);
54
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;
59 }
60 isa_bios = g_malloc(sizeof(*isa_bios));
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -040061 memory_region_init_ram(isa_bios, NULL, "isa-bios", isa_bios_size);
Jordan Justenbd183c72012-02-21 23:18:53 -080062 vmstate_register_ram_global(isa_bios);
63 memory_region_add_subregion_overlap(rom_memory,
64 0x100000 - isa_bios_size,
65 isa_bios,
66 1);
67
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);
71 memcpy(isa_bios_ptr,
72 ((uint8_t*)flash_ptr) + (flash_size - isa_bios_size),
73 isa_bios_size);
74
75 memory_region_set_readonly(isa_bios, true);
76}
77
Jordan Justenbd183c72012-02-21 23:18:53 -080078static void pc_system_flash_init(MemoryRegion *rom_memory,
79 DriveInfo *pflash_drv)
80{
81 BlockDriverState *bdrv;
82 int64_t size;
Avi Kivitya8170e52012-10-23 12:30:10 +020083 hwaddr phys_addr;
Jordan Justenbd183c72012-02-21 23:18:53 -080084 int sector_bits, sector_size;
85 pflash_t *system_flash;
86 MemoryRegion *flash_mem;
87
88 bdrv = pflash_drv->bdrv;
89 size = bdrv_getlength(pflash_drv->bdrv);
90 sector_bits = 12;
91 sector_size = 1 << sector_bits;
92
93 if ((size % sector_size) != 0) {
94 fprintf(stderr,
95 "qemu: PC system firmware (pflash) must be a multiple of 0x%x\n",
96 sector_size);
97 exit(1);
98 }
99
100 phys_addr = 0x100000000ULL - size;
101 system_flash = pflash_cfi01_register(phys_addr, NULL, "system.flash", size,
102 bdrv, sector_size, size >> sector_bits,
103 1, 0x0000, 0x0000, 0x0000, 0x0000, 0);
104 flash_mem = pflash_cfi01_get_memory(system_flash);
105
106 pc_isa_bios_init(rom_memory, flash_mem, size);
107}
108
Jordan Justendade9222013-05-29 01:27:24 -0700109static void old_pc_system_rom_init(MemoryRegion *rom_memory, bool isapc_ram_fw)
Jordan Justencbc5b5f2012-02-21 23:18:51 -0800110{
111 char *filename;
112 MemoryRegion *bios, *isa_bios;
113 int bios_size, isa_bios_size;
114 int ret;
115
116 /* BIOS load */
117 if (bios_name == NULL) {
118 bios_name = BIOS_FILENAME;
119 }
120 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
121 if (filename) {
122 bios_size = get_image_size(filename);
123 } else {
124 bios_size = -1;
125 }
126 if (bios_size <= 0 ||
127 (bios_size % 65536) != 0) {
128 goto bios_error;
129 }
130 bios = g_malloc(sizeof(*bios));
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400131 memory_region_init_ram(bios, NULL, "pc.bios", bios_size);
Jordan Justencbc5b5f2012-02-21 23:18:51 -0800132 vmstate_register_ram_global(bios);
Jordan Justendade9222013-05-29 01:27:24 -0700133 if (!isapc_ram_fw) {
134 memory_region_set_readonly(bios, true);
135 }
Jordan Justencbc5b5f2012-02-21 23:18:51 -0800136 ret = rom_add_file_fixed(bios_name, (uint32_t)(-bios_size), -1);
137 if (ret != 0) {
138 bios_error:
139 fprintf(stderr, "qemu: could not load PC BIOS '%s'\n", bios_name);
140 exit(1);
141 }
142 if (filename) {
143 g_free(filename);
144 }
145
146 /* map the last 128KB of the BIOS in ISA space */
147 isa_bios_size = bios_size;
148 if (isa_bios_size > (128 * 1024)) {
149 isa_bios_size = 128 * 1024;
150 }
151 isa_bios = g_malloc(sizeof(*isa_bios));
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400152 memory_region_init_alias(isa_bios, NULL, "isa-bios", bios,
Jordan Justencbc5b5f2012-02-21 23:18:51 -0800153 bios_size - isa_bios_size, isa_bios_size);
154 memory_region_add_subregion_overlap(rom_memory,
155 0x100000 - isa_bios_size,
156 isa_bios,
157 1);
Jordan Justendade9222013-05-29 01:27:24 -0700158 if (!isapc_ram_fw) {
159 memory_region_set_readonly(isa_bios, true);
160 }
Jordan Justencbc5b5f2012-02-21 23:18:51 -0800161
162 /* map all the bios at the top of memory */
163 memory_region_add_subregion(rom_memory,
164 (uint32_t)(-bios_size),
165 bios);
166}
167
168void pc_system_firmware_init(MemoryRegion *rom_memory)
169{
Jordan Justenbd183c72012-02-21 23:18:53 -0800170 DriveInfo *pflash_drv;
Jordan Justen90ccf9f2012-02-21 23:18:52 -0800171 PcSysFwDevice *sysfw_dev;
172
Markus Armbruster9953f882013-04-12 17:25:03 +0200173 /*
174 * TODO This device exists only so that users can switch between
175 * use of flash and ROM for the BIOS. The ability to switch was
176 * created because flash doesn't work with KVM. Once it does, we
Paolo Bonzini9e1c2ec2013-05-10 14:38:03 +0200177 * should drop this device.
Markus Armbruster9953f882013-04-12 17:25:03 +0200178 */
Jordan Justen90ccf9f2012-02-21 23:18:52 -0800179 sysfw_dev = (PcSysFwDevice*) qdev_create(NULL, "pc-sysfw");
180
Anthony Liguori1d385742012-04-18 17:33:15 -0500181 qdev_init_nofail(DEVICE(sysfw_dev));
182
Jordan Justendafb82e2013-05-29 01:27:27 -0700183 pflash_drv = drive_get(IF_PFLASH, 0, 0);
184
Paolo Bonzinia9044102013-08-09 12:35:01 -0500185 if (sysfw_dev->isapc_ram_fw || pflash_drv == NULL) {
Jordan Justendafb82e2013-05-29 01:27:27 -0700186 /* When a pflash drive is not found, use rom-mode */
Paolo Bonzinia9044102013-08-09 12:35:01 -0500187 old_pc_system_rom_init(rom_memory, sysfw_dev->isapc_ram_fw);
188 return;
189 }
190
191 if (kvm_enabled() && !kvm_readonly_mem_enabled()) {
Jordan Justendafb82e2013-05-29 01:27:27 -0700192 /* Older KVM cannot execute from device memory. So, flash memory
193 * cannot be used unless the readonly memory kvm capability is present. */
194 fprintf(stderr, "qemu: pflash with kvm requires KVM readonly memory support\n");
195 exit(1);
196 }
197
Paolo Bonzinia9044102013-08-09 12:35:01 -0500198 pc_system_flash_init(rom_memory, pflash_drv);
Jordan Justencbc5b5f2012-02-21 23:18:51 -0800199}
200
Jordan Justen90ccf9f2012-02-21 23:18:52 -0800201static Property pcsysfw_properties[] = {
Jordan Justendade9222013-05-29 01:27:24 -0700202 DEFINE_PROP_UINT8("isapc_ram_fw", PcSysFwDevice, isapc_ram_fw, 0),
Jordan Justen90ccf9f2012-02-21 23:18:52 -0800203 DEFINE_PROP_END_OF_LIST(),
204};
205
Anthony Liguori1d385742012-04-18 17:33:15 -0500206static int pcsysfw_init(DeviceState *dev)
207{
208 return 0;
209}
210
Jordan Justen90ccf9f2012-02-21 23:18:52 -0800211static void pcsysfw_class_init (ObjectClass *klass, void *data)
212{
213 DeviceClass *dc = DEVICE_CLASS (klass);
214
Marcel Apfelbaum125ee0e2013-07-29 17:17:45 +0300215 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
Jordan Justen90ccf9f2012-02-21 23:18:52 -0800216 dc->desc = "PC System Firmware";
Anthony Liguori1d385742012-04-18 17:33:15 -0500217 dc->init = pcsysfw_init;
Jordan Justen90ccf9f2012-02-21 23:18:52 -0800218 dc->props = pcsysfw_properties;
219}
220
Andreas Färber8c43a6f2013-01-10 16:19:07 +0100221static const TypeInfo pcsysfw_info = {
Jordan Justen90ccf9f2012-02-21 23:18:52 -0800222 .name = "pc-sysfw",
223 .parent = TYPE_SYS_BUS_DEVICE,
224 .instance_size = sizeof (PcSysFwDevice),
225 .class_init = pcsysfw_class_init,
226};
227
228static void pcsysfw_register (void)
229{
230 type_register_static (&pcsysfw_info);
231}
232
233type_init (pcsysfw_register);
Jordan Justencbc5b5f2012-02-21 23:18:51 -0800234