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