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 | |
Peter Maydell | b6a0aa0 | 2016-01-26 18:17:03 +0000 | [diff] [blame] | 26 | #include "qemu/osdep.h" |
Markus Armbruster | da34e65 | 2016-03-14 09:01:28 +0100 | [diff] [blame] | 27 | #include "qapi/error.h" |
Markus Armbruster | fa1d36d | 2014-10-07 13:59:13 +0200 | [diff] [blame] | 28 | #include "sysemu/block-backend.h" |
Paolo Bonzini | b4a42f8 | 2013-02-04 11:37:52 +0100 | [diff] [blame] | 29 | #include "qemu/error-report.h" |
Paolo Bonzini | 83c9f4c | 2013-02-04 15:40:22 +0100 | [diff] [blame] | 30 | #include "hw/sysbus.h" |
| 31 | #include "hw/hw.h" |
Paolo Bonzini | 0d09e41 | 2013-02-05 17:06:20 +0100 | [diff] [blame] | 32 | #include "hw/i386/pc.h" |
Jordan Justen | bd183c7 | 2012-02-21 23:18:53 -0800 | [diff] [blame] | 33 | #include "hw/boards.h" |
Paolo Bonzini | 83c9f4c | 2013-02-04 15:40:22 +0100 | [diff] [blame] | 34 | #include "hw/loader.h" |
Paolo Bonzini | 9c17d61 | 2012-12-17 18:20:04 +0100 | [diff] [blame] | 35 | #include "sysemu/sysemu.h" |
Paolo Bonzini | 0d09e41 | 2013-02-05 17:06:20 +0100 | [diff] [blame] | 36 | #include "hw/block/flash.h" |
Paolo Bonzini | 9c17d61 | 2012-12-17 18:20:04 +0100 | [diff] [blame] | 37 | #include "sysemu/kvm.h" |
Jordan Justen | cbc5b5f | 2012-02-21 23:18:51 -0800 | [diff] [blame] | 38 | |
| 39 | #define BIOS_FILENAME "bios.bin" |
| 40 | |
Jordan Justen | 90ccf9f | 2012-02-21 23:18:52 -0800 | [diff] [blame] | 41 | typedef struct PcSysFwDevice { |
| 42 | SysBusDevice busdev; |
Jordan Justen | dade922 | 2013-05-29 01:27:24 -0700 | [diff] [blame] | 43 | uint8_t isapc_ram_fw; |
Jordan Justen | 90ccf9f | 2012-02-21 23:18:52 -0800 | [diff] [blame] | 44 | } PcSysFwDevice; |
| 45 | |
Jordan Justen | bd183c7 | 2012-02-21 23:18:53 -0800 | [diff] [blame] | 46 | static void pc_isa_bios_init(MemoryRegion *rom_memory, |
| 47 | MemoryRegion *flash_mem, |
| 48 | int ram_size) |
| 49 | { |
| 50 | int isa_bios_size; |
| 51 | MemoryRegion *isa_bios; |
| 52 | uint64_t flash_size; |
| 53 | void *flash_ptr, *isa_bios_ptr; |
| 54 | |
| 55 | flash_size = memory_region_size(flash_mem); |
| 56 | |
| 57 | /* map the last 128KB of the BIOS in ISA space */ |
Markus Armbruster | 7f87af3 | 2013-07-31 15:11:12 +0200 | [diff] [blame] | 58 | isa_bios_size = MIN(flash_size, 128 * 1024); |
Jordan Justen | bd183c7 | 2012-02-21 23:18:53 -0800 | [diff] [blame] | 59 | isa_bios = g_malloc(sizeof(*isa_bios)); |
Peter Maydell | 98a99ce | 2017-07-07 15:42:53 +0100 | [diff] [blame^] | 60 | memory_region_init_ram(isa_bios, NULL, "isa-bios", isa_bios_size, |
Markus Armbruster | f8ed85a | 2015-09-11 16:51:43 +0200 | [diff] [blame] | 61 | &error_fatal); |
Jordan Justen | bd183c7 | 2012-02-21 23:18:53 -0800 | [diff] [blame] | 62 | memory_region_add_subregion_overlap(rom_memory, |
| 63 | 0x100000 - isa_bios_size, |
| 64 | isa_bios, |
| 65 | 1); |
| 66 | |
| 67 | /* copy ISA rom image from top of flash memory */ |
| 68 | flash_ptr = memory_region_get_ram_ptr(flash_mem); |
| 69 | isa_bios_ptr = memory_region_get_ram_ptr(isa_bios); |
| 70 | memcpy(isa_bios_ptr, |
| 71 | ((uint8_t*)flash_ptr) + (flash_size - isa_bios_size), |
| 72 | isa_bios_size); |
| 73 | |
| 74 | memory_region_set_readonly(isa_bios, true); |
| 75 | } |
| 76 | |
Laszlo Ersek | 637a5ac | 2013-11-28 00:52:52 +0100 | [diff] [blame] | 77 | #define FLASH_MAP_UNIT_MAX 2 |
| 78 | |
| 79 | /* We don't have a theoretically justifiable exact lower bound on the base |
| 80 | * address of any flash mapping. In practice, the IO-APIC MMIO range is |
| 81 | * [0xFEE00000..0xFEE01000[ -- see IO_APIC_DEFAULT_ADDRESS --, leaving free |
| 82 | * only 18MB-4KB below 4G. For now, restrict the cumulative mapping to 8MB in |
| 83 | * size. |
| 84 | */ |
| 85 | #define FLASH_MAP_BASE_MIN ((hwaddr)(0x100000000ULL - 8*1024*1024)) |
| 86 | |
| 87 | /* This function maps flash drives from 4G downward, in order of their unit |
| 88 | * numbers. The mapping starts at unit#0, with unit number increments of 1, and |
| 89 | * stops before the first missing flash drive, or before |
| 90 | * unit#FLASH_MAP_UNIT_MAX, whichever is reached first. |
| 91 | * |
| 92 | * Addressing within one flash drive is of course not reversed. |
| 93 | * |
| 94 | * An error message is printed and the process exits if: |
| 95 | * - the size of the backing file for a flash drive is non-positive, or not a |
| 96 | * multiple of the required sector size, or |
| 97 | * - the current mapping's base address would fall below FLASH_MAP_BASE_MIN. |
| 98 | * |
| 99 | * The drive with unit#0 (if available) is mapped at the highest address, and |
| 100 | * it is passed to pc_isa_bios_init(). Merging several drives for isa-bios is |
| 101 | * not supported. |
| 102 | */ |
| 103 | static void pc_system_flash_init(MemoryRegion *rom_memory) |
Jordan Justen | bd183c7 | 2012-02-21 23:18:53 -0800 | [diff] [blame] | 104 | { |
Laszlo Ersek | 637a5ac | 2013-11-28 00:52:52 +0100 | [diff] [blame] | 105 | int unit; |
| 106 | DriveInfo *pflash_drv; |
Markus Armbruster | 4be7463 | 2014-10-07 13:59:18 +0200 | [diff] [blame] | 107 | BlockBackend *blk; |
Jordan Justen | bd183c7 | 2012-02-21 23:18:53 -0800 | [diff] [blame] | 108 | int64_t size; |
Laszlo Ersek | 637a5ac | 2013-11-28 00:52:52 +0100 | [diff] [blame] | 109 | char *fatal_errmsg = NULL; |
| 110 | hwaddr phys_addr = 0x100000000ULL; |
Jordan Justen | bd183c7 | 2012-02-21 23:18:53 -0800 | [diff] [blame] | 111 | int sector_bits, sector_size; |
| 112 | pflash_t *system_flash; |
| 113 | MemoryRegion *flash_mem; |
Laszlo Ersek | 637a5ac | 2013-11-28 00:52:52 +0100 | [diff] [blame] | 114 | char name[64]; |
Jordan Justen | bd183c7 | 2012-02-21 23:18:53 -0800 | [diff] [blame] | 115 | |
Jordan Justen | bd183c7 | 2012-02-21 23:18:53 -0800 | [diff] [blame] | 116 | sector_bits = 12; |
| 117 | sector_size = 1 << sector_bits; |
| 118 | |
Laszlo Ersek | 637a5ac | 2013-11-28 00:52:52 +0100 | [diff] [blame] | 119 | for (unit = 0; |
| 120 | (unit < FLASH_MAP_UNIT_MAX && |
| 121 | (pflash_drv = drive_get(IF_PFLASH, 0, unit)) != NULL); |
| 122 | ++unit) { |
Markus Armbruster | 4be7463 | 2014-10-07 13:59:18 +0200 | [diff] [blame] | 123 | blk = blk_by_legacy_dinfo(pflash_drv); |
| 124 | size = blk_getlength(blk); |
Laszlo Ersek | 637a5ac | 2013-11-28 00:52:52 +0100 | [diff] [blame] | 125 | if (size < 0) { |
| 126 | fatal_errmsg = g_strdup_printf("failed to get backing file size"); |
| 127 | } else if (size == 0) { |
| 128 | fatal_errmsg = g_strdup_printf("PC system firmware (pflash) " |
| 129 | "cannot have zero size"); |
| 130 | } else if ((size % sector_size) != 0) { |
| 131 | fatal_errmsg = g_strdup_printf("PC system firmware (pflash) " |
| 132 | "must be a multiple of 0x%x", sector_size); |
| 133 | } else if (phys_addr < size || phys_addr - size < FLASH_MAP_BASE_MIN) { |
| 134 | fatal_errmsg = g_strdup_printf("oversized backing file, pflash " |
| 135 | "segments cannot be mapped under " |
| 136 | TARGET_FMT_plx, FLASH_MAP_BASE_MIN); |
| 137 | } |
| 138 | if (fatal_errmsg != NULL) { |
| 139 | Location loc; |
| 140 | |
| 141 | /* push a new, "none" location on the location stack; overwrite its |
| 142 | * contents with the location saved in the option; print the error |
| 143 | * (includes location); pop the top |
| 144 | */ |
| 145 | loc_push_none(&loc); |
| 146 | if (pflash_drv->opts != NULL) { |
| 147 | qemu_opts_loc_restore(pflash_drv->opts); |
| 148 | } |
| 149 | error_report("%s", fatal_errmsg); |
| 150 | loc_pop(&loc); |
| 151 | g_free(fatal_errmsg); |
| 152 | exit(1); |
| 153 | } |
| 154 | |
| 155 | phys_addr -= size; |
| 156 | |
| 157 | /* pflash_cfi01_register() creates a deep copy of the name */ |
| 158 | snprintf(name, sizeof name, "system.flash%d", unit); |
| 159 | system_flash = pflash_cfi01_register(phys_addr, NULL /* qdev */, name, |
Markus Armbruster | 4be7463 | 2014-10-07 13:59:18 +0200 | [diff] [blame] | 160 | size, blk, sector_size, |
Laszlo Ersek | 637a5ac | 2013-11-28 00:52:52 +0100 | [diff] [blame] | 161 | size >> sector_bits, |
| 162 | 1 /* width */, |
| 163 | 0x0000 /* id0 */, |
| 164 | 0x0000 /* id1 */, |
| 165 | 0x0000 /* id2 */, |
| 166 | 0x0000 /* id3 */, |
| 167 | 0 /* be */); |
| 168 | if (unit == 0) { |
| 169 | flash_mem = pflash_cfi01_get_memory(system_flash); |
| 170 | pc_isa_bios_init(rom_memory, flash_mem, size); |
| 171 | } |
Jordan Justen | bd183c7 | 2012-02-21 23:18:53 -0800 | [diff] [blame] | 172 | } |
Jordan Justen | bd183c7 | 2012-02-21 23:18:53 -0800 | [diff] [blame] | 173 | } |
| 174 | |
Jordan Justen | dade922 | 2013-05-29 01:27:24 -0700 | [diff] [blame] | 175 | static void old_pc_system_rom_init(MemoryRegion *rom_memory, bool isapc_ram_fw) |
Jordan Justen | cbc5b5f | 2012-02-21 23:18:51 -0800 | [diff] [blame] | 176 | { |
| 177 | char *filename; |
| 178 | MemoryRegion *bios, *isa_bios; |
| 179 | int bios_size, isa_bios_size; |
| 180 | int ret; |
| 181 | |
| 182 | /* BIOS load */ |
| 183 | if (bios_name == NULL) { |
| 184 | bios_name = BIOS_FILENAME; |
| 185 | } |
| 186 | filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name); |
| 187 | if (filename) { |
| 188 | bios_size = get_image_size(filename); |
| 189 | } else { |
| 190 | bios_size = -1; |
| 191 | } |
| 192 | if (bios_size <= 0 || |
| 193 | (bios_size % 65536) != 0) { |
| 194 | goto bios_error; |
| 195 | } |
| 196 | bios = g_malloc(sizeof(*bios)); |
Peter Maydell | 98a99ce | 2017-07-07 15:42:53 +0100 | [diff] [blame^] | 197 | memory_region_init_ram(bios, NULL, "pc.bios", bios_size, &error_fatal); |
Jordan Justen | dade922 | 2013-05-29 01:27:24 -0700 | [diff] [blame] | 198 | if (!isapc_ram_fw) { |
| 199 | memory_region_set_readonly(bios, true); |
| 200 | } |
Jordan Justen | cbc5b5f | 2012-02-21 23:18:51 -0800 | [diff] [blame] | 201 | ret = rom_add_file_fixed(bios_name, (uint32_t)(-bios_size), -1); |
| 202 | if (ret != 0) { |
| 203 | bios_error: |
| 204 | fprintf(stderr, "qemu: could not load PC BIOS '%s'\n", bios_name); |
| 205 | exit(1); |
| 206 | } |
Markus Armbruster | 18fc805 | 2014-12-04 14:46:43 +0100 | [diff] [blame] | 207 | g_free(filename); |
Jordan Justen | cbc5b5f | 2012-02-21 23:18:51 -0800 | [diff] [blame] | 208 | |
| 209 | /* map the last 128KB of the BIOS in ISA space */ |
| 210 | isa_bios_size = bios_size; |
| 211 | if (isa_bios_size > (128 * 1024)) { |
| 212 | isa_bios_size = 128 * 1024; |
| 213 | } |
| 214 | isa_bios = g_malloc(sizeof(*isa_bios)); |
Paolo Bonzini | 2c9b15c | 2013-06-06 05:41:28 -0400 | [diff] [blame] | 215 | memory_region_init_alias(isa_bios, NULL, "isa-bios", bios, |
Jordan Justen | cbc5b5f | 2012-02-21 23:18:51 -0800 | [diff] [blame] | 216 | bios_size - isa_bios_size, isa_bios_size); |
| 217 | memory_region_add_subregion_overlap(rom_memory, |
| 218 | 0x100000 - isa_bios_size, |
| 219 | isa_bios, |
| 220 | 1); |
Jordan Justen | dade922 | 2013-05-29 01:27:24 -0700 | [diff] [blame] | 221 | if (!isapc_ram_fw) { |
| 222 | memory_region_set_readonly(isa_bios, true); |
| 223 | } |
Jordan Justen | cbc5b5f | 2012-02-21 23:18:51 -0800 | [diff] [blame] | 224 | |
| 225 | /* map all the bios at the top of memory */ |
| 226 | memory_region_add_subregion(rom_memory, |
| 227 | (uint32_t)(-bios_size), |
| 228 | bios); |
| 229 | } |
| 230 | |
Paolo Bonzini | 6dd2a5c | 2013-08-09 12:35:02 -0500 | [diff] [blame] | 231 | void pc_system_firmware_init(MemoryRegion *rom_memory, bool isapc_ram_fw) |
Jordan Justen | cbc5b5f | 2012-02-21 23:18:51 -0800 | [diff] [blame] | 232 | { |
Jordan Justen | bd183c7 | 2012-02-21 23:18:53 -0800 | [diff] [blame] | 233 | DriveInfo *pflash_drv; |
Anthony Liguori | 1d38574 | 2012-04-18 17:33:15 -0500 | [diff] [blame] | 234 | |
Jordan Justen | dafb82e | 2013-05-29 01:27:27 -0700 | [diff] [blame] | 235 | pflash_drv = drive_get(IF_PFLASH, 0, 0); |
| 236 | |
Paolo Bonzini | 6dd2a5c | 2013-08-09 12:35:02 -0500 | [diff] [blame] | 237 | if (isapc_ram_fw || pflash_drv == NULL) { |
Jordan Justen | dafb82e | 2013-05-29 01:27:27 -0700 | [diff] [blame] | 238 | /* When a pflash drive is not found, use rom-mode */ |
Paolo Bonzini | 6dd2a5c | 2013-08-09 12:35:02 -0500 | [diff] [blame] | 239 | old_pc_system_rom_init(rom_memory, isapc_ram_fw); |
Paolo Bonzini | a904410 | 2013-08-09 12:35:01 -0500 | [diff] [blame] | 240 | return; |
| 241 | } |
| 242 | |
| 243 | if (kvm_enabled() && !kvm_readonly_mem_enabled()) { |
Jordan Justen | dafb82e | 2013-05-29 01:27:27 -0700 | [diff] [blame] | 244 | /* Older KVM cannot execute from device memory. So, flash memory |
| 245 | * cannot be used unless the readonly memory kvm capability is present. */ |
| 246 | fprintf(stderr, "qemu: pflash with kvm requires KVM readonly memory support\n"); |
| 247 | exit(1); |
| 248 | } |
| 249 | |
Laszlo Ersek | 637a5ac | 2013-11-28 00:52:52 +0100 | [diff] [blame] | 250 | pc_system_flash_init(rom_memory); |
Jordan Justen | cbc5b5f | 2012-02-21 23:18:51 -0800 | [diff] [blame] | 251 | } |