]>
Commit | Line | Data |
---|---|---|
cbc5b5f3 JJ |
1 | /* |
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 | ||
9c17d615 | 26 | #include "sysemu/blockdev.h" |
b4a42f81 | 27 | #include "qemu/error-report.h" |
83c9f4ca PB |
28 | #include "hw/sysbus.h" |
29 | #include "hw/hw.h" | |
0d09e41a | 30 | #include "hw/i386/pc.h" |
bd183c79 | 31 | #include "hw/boards.h" |
83c9f4ca | 32 | #include "hw/loader.h" |
9c17d615 | 33 | #include "sysemu/sysemu.h" |
0d09e41a | 34 | #include "hw/block/flash.h" |
9c17d615 | 35 | #include "sysemu/kvm.h" |
cbc5b5f3 JJ |
36 | |
37 | #define BIOS_FILENAME "bios.bin" | |
38 | ||
90ccf9f6 JJ |
39 | typedef struct PcSysFwDevice { |
40 | SysBusDevice busdev; | |
41 | uint8_t rom_only; | |
dade922f | 42 | uint8_t isapc_ram_fw; |
90ccf9f6 JJ |
43 | } PcSysFwDevice; |
44 | ||
bd183c79 JJ |
45 | static void pc_isa_bios_init(MemoryRegion *rom_memory, |
46 | MemoryRegion *flash_mem, | |
47 | int ram_size) | |
48 | { | |
49 | int isa_bios_size; | |
50 | MemoryRegion *isa_bios; | |
51 | uint64_t flash_size; | |
52 | void *flash_ptr, *isa_bios_ptr; | |
53 | ||
54 | flash_size = memory_region_size(flash_mem); | |
55 | ||
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; | |
60 | } | |
61 | isa_bios = g_malloc(sizeof(*isa_bios)); | |
2c9b15ca | 62 | memory_region_init_ram(isa_bios, NULL, "isa-bios", isa_bios_size); |
bd183c79 JJ |
63 | vmstate_register_ram_global(isa_bios); |
64 | memory_region_add_subregion_overlap(rom_memory, | |
65 | 0x100000 - isa_bios_size, | |
66 | isa_bios, | |
67 | 1); | |
68 | ||
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); | |
72 | memcpy(isa_bios_ptr, | |
73 | ((uint8_t*)flash_ptr) + (flash_size - isa_bios_size), | |
74 | isa_bios_size); | |
75 | ||
76 | memory_region_set_readonly(isa_bios, true); | |
77 | } | |
78 | ||
79 | static void pc_fw_add_pflash_drv(void) | |
80 | { | |
81 | QemuOpts *opts; | |
82 | QEMUMachine *machine; | |
83 | char *filename; | |
84 | ||
85 | if (bios_name == NULL) { | |
86 | bios_name = BIOS_FILENAME; | |
87 | } | |
88 | filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name); | |
e7b1d0ea MA |
89 | if (!filename) { |
90 | error_report("Can't open BIOS image %s", bios_name); | |
91 | exit(1); | |
92 | } | |
bd183c79 JJ |
93 | |
94 | opts = drive_add(IF_PFLASH, -1, filename, "readonly=on"); | |
9cf1f002 SW |
95 | |
96 | g_free(filename); | |
97 | ||
bd183c79 JJ |
98 | if (opts == NULL) { |
99 | return; | |
100 | } | |
101 | ||
102 | machine = find_default_machine(); | |
103 | if (machine == NULL) { | |
104 | return; | |
105 | } | |
106 | ||
2d0d2837 | 107 | if (!drive_init(opts, machine->block_default_type)) { |
654598c9 MA |
108 | qemu_opts_del(opts); |
109 | } | |
bd183c79 JJ |
110 | } |
111 | ||
112 | static void pc_system_flash_init(MemoryRegion *rom_memory, | |
113 | DriveInfo *pflash_drv) | |
114 | { | |
115 | BlockDriverState *bdrv; | |
116 | int64_t size; | |
a8170e5e | 117 | hwaddr phys_addr; |
bd183c79 JJ |
118 | int sector_bits, sector_size; |
119 | pflash_t *system_flash; | |
120 | MemoryRegion *flash_mem; | |
121 | ||
122 | bdrv = pflash_drv->bdrv; | |
123 | size = bdrv_getlength(pflash_drv->bdrv); | |
124 | sector_bits = 12; | |
125 | sector_size = 1 << sector_bits; | |
126 | ||
127 | if ((size % sector_size) != 0) { | |
128 | fprintf(stderr, | |
129 | "qemu: PC system firmware (pflash) must be a multiple of 0x%x\n", | |
130 | sector_size); | |
131 | exit(1); | |
132 | } | |
133 | ||
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); | |
139 | ||
140 | pc_isa_bios_init(rom_memory, flash_mem, size); | |
141 | } | |
142 | ||
dade922f | 143 | static void old_pc_system_rom_init(MemoryRegion *rom_memory, bool isapc_ram_fw) |
cbc5b5f3 JJ |
144 | { |
145 | char *filename; | |
146 | MemoryRegion *bios, *isa_bios; | |
147 | int bios_size, isa_bios_size; | |
148 | int ret; | |
149 | ||
150 | /* BIOS load */ | |
151 | if (bios_name == NULL) { | |
152 | bios_name = BIOS_FILENAME; | |
153 | } | |
154 | filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name); | |
155 | if (filename) { | |
156 | bios_size = get_image_size(filename); | |
157 | } else { | |
158 | bios_size = -1; | |
159 | } | |
160 | if (bios_size <= 0 || | |
161 | (bios_size % 65536) != 0) { | |
162 | goto bios_error; | |
163 | } | |
164 | bios = g_malloc(sizeof(*bios)); | |
2c9b15ca | 165 | memory_region_init_ram(bios, NULL, "pc.bios", bios_size); |
cbc5b5f3 | 166 | vmstate_register_ram_global(bios); |
dade922f JJ |
167 | if (!isapc_ram_fw) { |
168 | memory_region_set_readonly(bios, true); | |
169 | } | |
cbc5b5f3 JJ |
170 | ret = rom_add_file_fixed(bios_name, (uint32_t)(-bios_size), -1); |
171 | if (ret != 0) { | |
172 | bios_error: | |
173 | fprintf(stderr, "qemu: could not load PC BIOS '%s'\n", bios_name); | |
174 | exit(1); | |
175 | } | |
176 | if (filename) { | |
177 | g_free(filename); | |
178 | } | |
179 | ||
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; | |
184 | } | |
185 | isa_bios = g_malloc(sizeof(*isa_bios)); | |
2c9b15ca | 186 | memory_region_init_alias(isa_bios, NULL, "isa-bios", bios, |
cbc5b5f3 JJ |
187 | bios_size - isa_bios_size, isa_bios_size); |
188 | memory_region_add_subregion_overlap(rom_memory, | |
189 | 0x100000 - isa_bios_size, | |
190 | isa_bios, | |
191 | 1); | |
dade922f JJ |
192 | if (!isapc_ram_fw) { |
193 | memory_region_set_readonly(isa_bios, true); | |
194 | } | |
cbc5b5f3 JJ |
195 | |
196 | /* map all the bios at the top of memory */ | |
197 | memory_region_add_subregion(rom_memory, | |
198 | (uint32_t)(-bios_size), | |
199 | bios); | |
200 | } | |
201 | ||
9953f882 MA |
202 | /* |
203 | * Bug-compatible flash vs. ROM selection enabled? | |
204 | * A few older machines enable this. | |
205 | */ | |
206 | bool pc_sysfw_flash_vs_rom_bug_compatible; | |
207 | ||
cbc5b5f3 JJ |
208 | void pc_system_firmware_init(MemoryRegion *rom_memory) |
209 | { | |
bd183c79 | 210 | DriveInfo *pflash_drv; |
90ccf9f6 JJ |
211 | PcSysFwDevice *sysfw_dev; |
212 | ||
9953f882 MA |
213 | /* |
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 | |
9e1c2ec8 | 217 | * should drop this device. |
9953f882 | 218 | */ |
90ccf9f6 JJ |
219 | sysfw_dev = (PcSysFwDevice*) qdev_create(NULL, "pc-sysfw"); |
220 | ||
1d38574f AL |
221 | qdev_init_nofail(DEVICE(sysfw_dev)); |
222 | ||
bd183c79 JJ |
223 | pflash_drv = drive_get(IF_PFLASH, 0, 0); |
224 | ||
dafb82e0 JJ |
225 | if (pc_sysfw_flash_vs_rom_bug_compatible) { |
226 | /* | |
227 | * This is a Bad Idea, because it makes enabling/disabling KVM | |
228 | * guest-visible. Do it only in bug-compatibility mode. | |
229 | */ | |
230 | if (kvm_enabled()) { | |
231 | if (pflash_drv != NULL) { | |
232 | fprintf(stderr, "qemu: pflash cannot be used with kvm enabled\n"); | |
233 | exit(1); | |
234 | } else { | |
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 | |
239 | * undesirable */ | |
240 | sysfw_dev->rom_only = 1; | |
241 | } | |
bd183c79 | 242 | } |
dafb82e0 JJ |
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"); | |
250 | exit(1); | |
251 | } | |
252 | ||
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); | |
256 | return; | |
bd183c79 JJ |
257 | } |
258 | ||
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); | |
264 | } | |
265 | ||
266 | if (pflash_drv != NULL) { | |
267 | pc_system_flash_init(rom_memory, pflash_drv); | |
268 | } else { | |
269 | fprintf(stderr, "qemu: PC system firmware (pflash) not available\n"); | |
270 | exit(1); | |
271 | } | |
cbc5b5f3 JJ |
272 | } |
273 | ||
90ccf9f6 | 274 | static Property pcsysfw_properties[] = { |
dade922f | 275 | DEFINE_PROP_UINT8("isapc_ram_fw", PcSysFwDevice, isapc_ram_fw, 0), |
9e1c2ec8 | 276 | DEFINE_PROP_UINT8("rom_only", PcSysFwDevice, rom_only, 0), |
90ccf9f6 JJ |
277 | DEFINE_PROP_END_OF_LIST(), |
278 | }; | |
279 | ||
1d38574f AL |
280 | static int pcsysfw_init(DeviceState *dev) |
281 | { | |
282 | return 0; | |
283 | } | |
284 | ||
90ccf9f6 JJ |
285 | static void pcsysfw_class_init (ObjectClass *klass, void *data) |
286 | { | |
287 | DeviceClass *dc = DEVICE_CLASS (klass); | |
288 | ||
289 | dc->desc = "PC System Firmware"; | |
1d38574f | 290 | dc->init = pcsysfw_init; |
90ccf9f6 JJ |
291 | dc->props = pcsysfw_properties; |
292 | } | |
293 | ||
8c43a6f0 | 294 | static const TypeInfo pcsysfw_info = { |
90ccf9f6 JJ |
295 | .name = "pc-sysfw", |
296 | .parent = TYPE_SYS_BUS_DEVICE, | |
297 | .instance_size = sizeof (PcSysFwDevice), | |
298 | .class_init = pcsysfw_class_init, | |
299 | }; | |
300 | ||
301 | static void pcsysfw_register (void) | |
302 | { | |
303 | type_register_static (&pcsysfw_info); | |
304 | } | |
305 | ||
306 | type_init (pcsysfw_register); | |
cbc5b5f3 | 307 |