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 "qemu/osdep.h" 27 #include "qapi/error.h" 28 #include "sysemu/block-backend.h" 29 #include "qemu/error-report.h" 30 #include "qemu/option.h" 31 #include "qemu/units.h" 32 #include "hw/sysbus.h" 33 #include "hw/i386/x86.h" 34 #include "hw/i386/pc.h" 35 #include "hw/loader.h" 36 #include "hw/qdev-properties.h" 37 #include "hw/block/flash.h" 38 #include "sysemu/kvm.h" 39 #include "sev.h" 40 41 #define FLASH_SECTOR_SIZE 4096 42 43 static void pc_isa_bios_init(MemoryRegion *rom_memory, 44 MemoryRegion *flash_mem) 45 { 46 int isa_bios_size; 47 MemoryRegion *isa_bios; 48 uint64_t flash_size; 49 void *flash_ptr, *isa_bios_ptr; 50 51 flash_size = memory_region_size(flash_mem); 52 53 /* map the last 128KB of the BIOS in ISA space */ 54 isa_bios_size = MIN(flash_size, 128 * KiB); 55 isa_bios = g_malloc(sizeof(*isa_bios)); 56 memory_region_init_ram(isa_bios, NULL, "isa-bios", isa_bios_size, 57 &error_fatal); 58 memory_region_add_subregion_overlap(rom_memory, 59 0x100000 - isa_bios_size, 60 isa_bios, 61 1); 62 63 /* copy ISA rom image from top of flash memory */ 64 flash_ptr = memory_region_get_ram_ptr(flash_mem); 65 isa_bios_ptr = memory_region_get_ram_ptr(isa_bios); 66 memcpy(isa_bios_ptr, 67 ((uint8_t*)flash_ptr) + (flash_size - isa_bios_size), 68 isa_bios_size); 69 70 memory_region_set_readonly(isa_bios, true); 71 } 72 73 static PFlashCFI01 *pc_pflash_create(PCMachineState *pcms, 74 const char *name, 75 const char *alias_prop_name) 76 { 77 DeviceState *dev = qdev_new(TYPE_PFLASH_CFI01); 78 79 qdev_prop_set_uint64(dev, "sector-length", FLASH_SECTOR_SIZE); 80 qdev_prop_set_uint8(dev, "width", 1); 81 qdev_prop_set_string(dev, "name", name); 82 object_property_add_child(OBJECT(pcms), name, OBJECT(dev)); 83 object_property_add_alias(OBJECT(pcms), alias_prop_name, 84 OBJECT(dev), "drive"); 85 /* 86 * The returned reference is tied to the child property and 87 * will be removed with object_unparent. 88 */ 89 object_unref(OBJECT(dev)); 90 return PFLASH_CFI01(dev); 91 } 92 93 void pc_system_flash_create(PCMachineState *pcms) 94 { 95 PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms); 96 97 if (pcmc->pci_enabled) { 98 pcms->flash[0] = pc_pflash_create(pcms, "system.flash0", 99 "pflash0"); 100 pcms->flash[1] = pc_pflash_create(pcms, "system.flash1", 101 "pflash1"); 102 } 103 } 104 105 void pc_system_flash_cleanup_unused(PCMachineState *pcms) 106 { 107 char *prop_name; 108 int i; 109 110 assert(PC_MACHINE_GET_CLASS(pcms)->pci_enabled); 111 112 for (i = 0; i < ARRAY_SIZE(pcms->flash); i++) { 113 if (!qdev_is_realized(DEVICE(pcms->flash[i]))) { 114 prop_name = g_strdup_printf("pflash%d", i); 115 object_property_del(OBJECT(pcms), prop_name); 116 g_free(prop_name); 117 object_unparent(OBJECT(pcms->flash[i])); 118 pcms->flash[i] = NULL; 119 } 120 } 121 } 122 123 /* 124 * Map the pcms->flash[] from 4GiB downward, and realize. 125 * Map them in descending order, i.e. pcms->flash[0] at the top, 126 * without gaps. 127 * Stop at the first pcms->flash[0] lacking a block backend. 128 * Set each flash's size from its block backend. Fatal error if the 129 * size isn't a non-zero multiple of 4KiB, or the total size exceeds 130 * pcms->max_fw_size. 131 * 132 * If pcms->flash[0] has a block backend, its memory is passed to 133 * pc_isa_bios_init(). Merging several flash devices for isa-bios is 134 * not supported. 135 */ 136 static void pc_system_flash_map(PCMachineState *pcms, 137 MemoryRegion *rom_memory) 138 { 139 hwaddr total_size = 0; 140 int i; 141 BlockBackend *blk; 142 int64_t size; 143 PFlashCFI01 *system_flash; 144 MemoryRegion *flash_mem; 145 void *flash_ptr; 146 int flash_size; 147 148 assert(PC_MACHINE_GET_CLASS(pcms)->pci_enabled); 149 150 for (i = 0; i < ARRAY_SIZE(pcms->flash); i++) { 151 system_flash = pcms->flash[i]; 152 blk = pflash_cfi01_get_blk(system_flash); 153 if (!blk) { 154 break; 155 } 156 size = blk_getlength(blk); 157 if (size < 0) { 158 error_report("can't get size of block device %s: %s", 159 blk_name(blk), strerror(-size)); 160 exit(1); 161 } 162 if (size == 0 || !QEMU_IS_ALIGNED(size, FLASH_SECTOR_SIZE)) { 163 error_report("system firmware block device %s has invalid size " 164 "%" PRId64, 165 blk_name(blk), size); 166 info_report("its size must be a non-zero multiple of 0x%x", 167 FLASH_SECTOR_SIZE); 168 exit(1); 169 } 170 if ((hwaddr)size != size 171 || total_size > HWADDR_MAX - size 172 || total_size + size > pcms->max_fw_size) { 173 error_report("combined size of system firmware exceeds " 174 "%" PRIu64 " bytes", 175 pcms->max_fw_size); 176 exit(1); 177 } 178 179 total_size += size; 180 qdev_prop_set_uint32(DEVICE(system_flash), "num-blocks", 181 size / FLASH_SECTOR_SIZE); 182 sysbus_realize_and_unref(SYS_BUS_DEVICE(system_flash), &error_fatal); 183 sysbus_mmio_map(SYS_BUS_DEVICE(system_flash), 0, 184 0x100000000ULL - total_size); 185 186 if (i == 0) { 187 flash_mem = pflash_cfi01_get_memory(system_flash); 188 pc_isa_bios_init(rom_memory, flash_mem); 189 190 /* Encrypt the pflash boot ROM */ 191 if (sev_enabled()) { 192 flash_ptr = memory_region_get_ram_ptr(flash_mem); 193 flash_size = memory_region_size(flash_mem); 194 x86_firmware_configure(flash_ptr, flash_size); 195 } 196 } 197 } 198 } 199 200 void pc_system_firmware_init(PCMachineState *pcms, 201 MemoryRegion *rom_memory) 202 { 203 PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms); 204 int i; 205 BlockBackend *pflash_blk[ARRAY_SIZE(pcms->flash)]; 206 207 if (!pcmc->pci_enabled) { 208 x86_bios_rom_init(MACHINE(pcms), "bios.bin", rom_memory, true); 209 return; 210 } 211 212 /* Map legacy -drive if=pflash to machine properties */ 213 for (i = 0; i < ARRAY_SIZE(pcms->flash); i++) { 214 pflash_cfi01_legacy_drive(pcms->flash[i], 215 drive_get(IF_PFLASH, 0, i)); 216 pflash_blk[i] = pflash_cfi01_get_blk(pcms->flash[i]); 217 } 218 219 /* Reject gaps */ 220 for (i = 1; i < ARRAY_SIZE(pcms->flash); i++) { 221 if (pflash_blk[i] && !pflash_blk[i - 1]) { 222 error_report("pflash%d requires pflash%d", i, i - 1); 223 exit(1); 224 } 225 } 226 227 if (!pflash_blk[0]) { 228 /* Machine property pflash0 not set, use ROM mode */ 229 x86_bios_rom_init(MACHINE(pcms), "bios.bin", rom_memory, false); 230 } else { 231 if (kvm_enabled() && !kvm_readonly_mem_enabled()) { 232 /* 233 * Older KVM cannot execute from device memory. So, flash 234 * memory cannot be used unless the readonly memory kvm 235 * capability is present. 236 */ 237 error_report("pflash with kvm requires KVM readonly memory support"); 238 exit(1); 239 } 240 241 pc_system_flash_map(pcms, rom_memory); 242 } 243 244 pc_system_flash_cleanup_unused(pcms); 245 } 246 247 void x86_firmware_configure(void *ptr, int size) 248 { 249 int ret; 250 251 /* 252 * OVMF places a GUIDed structures in the flash, so 253 * search for them 254 */ 255 pc_system_parse_ovmf_flash(ptr, size); 256 257 if (sev_enabled()) { 258 ret = sev_es_save_reset_vector(ptr, size); 259 if (ret) { 260 error_report("failed to locate and/or save reset vector"); 261 exit(1); 262 } 263 264 sev_encrypt_flash(ptr, size, &error_fatal); 265 } 266 } 267