Jordan Justen | cbc5b5f | 2012-02-21 23:18:51 -0800 | [diff] [blame] | 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 | |
Paolo Bonzini | 9c17d61 | 2012-12-17 18:20:04 +0100 | [diff] [blame] | 26 | #include "sysemu/blockdev.h" |
Paolo Bonzini | b4a42f8 | 2013-02-04 11:37:52 +0100 | [diff] [blame] | 27 | #include "qemu/error-report.h" |
Paolo Bonzini | 83c9f4c | 2013-02-04 15:40:22 +0100 | [diff] [blame] | 28 | #include "hw/sysbus.h" |
| 29 | #include "hw/hw.h" |
Paolo Bonzini | 0d09e41 | 2013-02-05 17:06:20 +0100 | [diff] [blame] | 30 | #include "hw/i386/pc.h" |
Jordan Justen | bd183c7 | 2012-02-21 23:18:53 -0800 | [diff] [blame] | 31 | #include "hw/boards.h" |
Paolo Bonzini | 83c9f4c | 2013-02-04 15:40:22 +0100 | [diff] [blame] | 32 | #include "hw/loader.h" |
Paolo Bonzini | 9c17d61 | 2012-12-17 18:20:04 +0100 | [diff] [blame] | 33 | #include "sysemu/sysemu.h" |
Paolo Bonzini | 0d09e41 | 2013-02-05 17:06:20 +0100 | [diff] [blame] | 34 | #include "hw/block/flash.h" |
Paolo Bonzini | 9c17d61 | 2012-12-17 18:20:04 +0100 | [diff] [blame] | 35 | #include "sysemu/kvm.h" |
Jordan Justen | cbc5b5f | 2012-02-21 23:18:51 -0800 | [diff] [blame] | 36 | |
| 37 | #define BIOS_FILENAME "bios.bin" |
| 38 | |
Jordan Justen | 90ccf9f | 2012-02-21 23:18:52 -0800 | [diff] [blame] | 39 | typedef struct PcSysFwDevice { |
| 40 | SysBusDevice busdev; |
| 41 | uint8_t rom_only; |
| 42 | } PcSysFwDevice; |
| 43 | |
Jordan Justen | bd183c7 | 2012-02-21 23:18:53 -0800 | [diff] [blame] | 44 | static 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)); |
| 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, |
| 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 | |
| 78 | static void pc_fw_add_pflash_drv(void) |
| 79 | { |
| 80 | QemuOpts *opts; |
| 81 | QEMUMachine *machine; |
| 82 | char *filename; |
| 83 | |
| 84 | if (bios_name == NULL) { |
| 85 | bios_name = BIOS_FILENAME; |
| 86 | } |
| 87 | filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name); |
Markus Armbruster | e7b1d0e | 2012-12-05 15:28:05 +0100 | [diff] [blame] | 88 | if (!filename) { |
| 89 | error_report("Can't open BIOS image %s", bios_name); |
| 90 | exit(1); |
| 91 | } |
Jordan Justen | bd183c7 | 2012-02-21 23:18:53 -0800 | [diff] [blame] | 92 | |
| 93 | opts = drive_add(IF_PFLASH, -1, filename, "readonly=on"); |
Stefan Weil | 9cf1f00 | 2012-04-28 02:20:20 +0000 | [diff] [blame] | 94 | |
| 95 | g_free(filename); |
| 96 | |
Jordan Justen | bd183c7 | 2012-02-21 23:18:53 -0800 | [diff] [blame] | 97 | if (opts == NULL) { |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | machine = find_default_machine(); |
| 102 | if (machine == NULL) { |
| 103 | return; |
| 104 | } |
| 105 | |
Christian Borntraeger | 2d0d283 | 2012-11-20 15:30:34 +0100 | [diff] [blame] | 106 | if (!drive_init(opts, machine->block_default_type)) { |
Markus Armbruster | 654598c | 2012-11-23 19:12:18 +0100 | [diff] [blame] | 107 | qemu_opts_del(opts); |
| 108 | } |
Jordan Justen | bd183c7 | 2012-02-21 23:18:53 -0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | static void pc_system_flash_init(MemoryRegion *rom_memory, |
| 112 | DriveInfo *pflash_drv) |
| 113 | { |
| 114 | BlockDriverState *bdrv; |
| 115 | int64_t size; |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 116 | hwaddr phys_addr; |
Jordan Justen | bd183c7 | 2012-02-21 23:18:53 -0800 | [diff] [blame] | 117 | int sector_bits, sector_size; |
| 118 | pflash_t *system_flash; |
| 119 | MemoryRegion *flash_mem; |
| 120 | |
| 121 | bdrv = pflash_drv->bdrv; |
| 122 | size = bdrv_getlength(pflash_drv->bdrv); |
| 123 | sector_bits = 12; |
| 124 | sector_size = 1 << sector_bits; |
| 125 | |
| 126 | if ((size % sector_size) != 0) { |
| 127 | fprintf(stderr, |
| 128 | "qemu: PC system firmware (pflash) must be a multiple of 0x%x\n", |
| 129 | sector_size); |
| 130 | exit(1); |
| 131 | } |
| 132 | |
| 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); |
| 138 | |
| 139 | pc_isa_bios_init(rom_memory, flash_mem, size); |
| 140 | } |
| 141 | |
| 142 | static void old_pc_system_rom_init(MemoryRegion *rom_memory) |
Jordan Justen | cbc5b5f | 2012-02-21 23:18:51 -0800 | [diff] [blame] | 143 | { |
| 144 | char *filename; |
| 145 | MemoryRegion *bios, *isa_bios; |
| 146 | int bios_size, isa_bios_size; |
| 147 | int ret; |
| 148 | |
| 149 | /* BIOS load */ |
| 150 | if (bios_name == NULL) { |
| 151 | bios_name = BIOS_FILENAME; |
| 152 | } |
| 153 | filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name); |
| 154 | if (filename) { |
| 155 | bios_size = get_image_size(filename); |
| 156 | } else { |
| 157 | bios_size = -1; |
| 158 | } |
| 159 | if (bios_size <= 0 || |
| 160 | (bios_size % 65536) != 0) { |
| 161 | goto bios_error; |
| 162 | } |
| 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); |
| 168 | if (ret != 0) { |
| 169 | bios_error: |
| 170 | fprintf(stderr, "qemu: could not load PC BIOS '%s'\n", bios_name); |
| 171 | exit(1); |
| 172 | } |
| 173 | if (filename) { |
| 174 | g_free(filename); |
| 175 | } |
| 176 | |
| 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; |
| 181 | } |
| 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, |
| 187 | isa_bios, |
| 188 | 1); |
| 189 | memory_region_set_readonly(isa_bios, true); |
| 190 | |
| 191 | /* map all the bios at the top of memory */ |
| 192 | memory_region_add_subregion(rom_memory, |
| 193 | (uint32_t)(-bios_size), |
| 194 | bios); |
| 195 | } |
| 196 | |
Markus Armbruster | 9953f88 | 2013-04-12 17:25:03 +0200 | [diff] [blame^] | 197 | /* |
| 198 | * Bug-compatible flash vs. ROM selection enabled? |
| 199 | * A few older machines enable this. |
| 200 | */ |
| 201 | bool pc_sysfw_flash_vs_rom_bug_compatible; |
| 202 | |
Jordan Justen | cbc5b5f | 2012-02-21 23:18:51 -0800 | [diff] [blame] | 203 | void pc_system_firmware_init(MemoryRegion *rom_memory) |
| 204 | { |
Jordan Justen | bd183c7 | 2012-02-21 23:18:53 -0800 | [diff] [blame] | 205 | DriveInfo *pflash_drv; |
Jordan Justen | 90ccf9f | 2012-02-21 23:18:52 -0800 | [diff] [blame] | 206 | PcSysFwDevice *sysfw_dev; |
| 207 | |
Markus Armbruster | 9953f88 | 2013-04-12 17:25:03 +0200 | [diff] [blame^] | 208 | /* |
| 209 | * TODO This device exists only so that users can switch between |
| 210 | * use of flash and ROM for the BIOS. The ability to switch was |
| 211 | * created because flash doesn't work with KVM. Once it does, we |
| 212 | * should drop this device for new machine types. |
| 213 | */ |
Jordan Justen | 90ccf9f | 2012-02-21 23:18:52 -0800 | [diff] [blame] | 214 | sysfw_dev = (PcSysFwDevice*) qdev_create(NULL, "pc-sysfw"); |
| 215 | |
Anthony Liguori | 1d38574 | 2012-04-18 17:33:15 -0500 | [diff] [blame] | 216 | qdev_init_nofail(DEVICE(sysfw_dev)); |
| 217 | |
Jordan Justen | bd183c7 | 2012-02-21 23:18:53 -0800 | [diff] [blame] | 218 | if (sysfw_dev->rom_only) { |
| 219 | old_pc_system_rom_init(rom_memory); |
| 220 | return; |
| 221 | } |
| 222 | |
| 223 | pflash_drv = drive_get(IF_PFLASH, 0, 0); |
| 224 | |
| 225 | /* Currently KVM cannot execute from device memory. |
| 226 | Use old rom based firmware initialization for KVM. */ |
Markus Armbruster | 9953f88 | 2013-04-12 17:25:03 +0200 | [diff] [blame^] | 227 | /* |
| 228 | * This is a Bad Idea, because it makes enabling/disabling KVM |
| 229 | * guest-visible. Do it only in bug-compatibility mode. |
| 230 | */ |
| 231 | if (pc_sysfw_flash_vs_rom_bug_compatible && kvm_enabled()) { |
Jordan Justen | bd183c7 | 2012-02-21 23:18:53 -0800 | [diff] [blame] | 232 | if (pflash_drv != NULL) { |
| 233 | fprintf(stderr, "qemu: pflash cannot be used with kvm enabled\n"); |
| 234 | exit(1); |
| 235 | } else { |
| 236 | sysfw_dev->rom_only = 1; |
| 237 | old_pc_system_rom_init(rom_memory); |
| 238 | return; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | /* If a pflash drive is not found, then create one using |
| 243 | the bios filename. */ |
| 244 | if (pflash_drv == NULL) { |
| 245 | pc_fw_add_pflash_drv(); |
| 246 | pflash_drv = drive_get(IF_PFLASH, 0, 0); |
| 247 | } |
| 248 | |
| 249 | if (pflash_drv != NULL) { |
| 250 | pc_system_flash_init(rom_memory, pflash_drv); |
| 251 | } else { |
| 252 | fprintf(stderr, "qemu: PC system firmware (pflash) not available\n"); |
| 253 | exit(1); |
| 254 | } |
Jordan Justen | cbc5b5f | 2012-02-21 23:18:51 -0800 | [diff] [blame] | 255 | } |
| 256 | |
Jordan Justen | 90ccf9f | 2012-02-21 23:18:52 -0800 | [diff] [blame] | 257 | static Property pcsysfw_properties[] = { |
Markus Armbruster | 9953f88 | 2013-04-12 17:25:03 +0200 | [diff] [blame^] | 258 | DEFINE_PROP_UINT8("rom_only", PcSysFwDevice, rom_only, 1), |
Jordan Justen | 90ccf9f | 2012-02-21 23:18:52 -0800 | [diff] [blame] | 259 | DEFINE_PROP_END_OF_LIST(), |
| 260 | }; |
| 261 | |
Anthony Liguori | 1d38574 | 2012-04-18 17:33:15 -0500 | [diff] [blame] | 262 | static int pcsysfw_init(DeviceState *dev) |
| 263 | { |
| 264 | return 0; |
| 265 | } |
| 266 | |
Jordan Justen | 90ccf9f | 2012-02-21 23:18:52 -0800 | [diff] [blame] | 267 | static void pcsysfw_class_init (ObjectClass *klass, void *data) |
| 268 | { |
| 269 | DeviceClass *dc = DEVICE_CLASS (klass); |
| 270 | |
| 271 | dc->desc = "PC System Firmware"; |
Anthony Liguori | 1d38574 | 2012-04-18 17:33:15 -0500 | [diff] [blame] | 272 | dc->init = pcsysfw_init; |
Jordan Justen | 90ccf9f | 2012-02-21 23:18:52 -0800 | [diff] [blame] | 273 | dc->props = pcsysfw_properties; |
| 274 | } |
| 275 | |
Andreas Färber | 8c43a6f | 2013-01-10 16:19:07 +0100 | [diff] [blame] | 276 | static const TypeInfo pcsysfw_info = { |
Jordan Justen | 90ccf9f | 2012-02-21 23:18:52 -0800 | [diff] [blame] | 277 | .name = "pc-sysfw", |
| 278 | .parent = TYPE_SYS_BUS_DEVICE, |
| 279 | .instance_size = sizeof (PcSysFwDevice), |
| 280 | .class_init = pcsysfw_class_init, |
| 281 | }; |
| 282 | |
| 283 | static void pcsysfw_register (void) |
| 284 | { |
| 285 | type_register_static (&pcsysfw_info); |
| 286 | } |
| 287 | |
| 288 | type_init (pcsysfw_register); |
Jordan Justen | cbc5b5f | 2012-02-21 23:18:51 -0800 | [diff] [blame] | 289 | |