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 "qemu-common.h" 28 #include "qapi/error.h" 29 #include "sysemu/block-backend.h" 30 #include "qemu/error-report.h" 31 #include "qemu/option.h" 32 #include "qemu/units.h" 33 #include "hw/sysbus.h" 34 #include "hw/i386/x86.h" 35 #include "hw/i386/pc.h" 36 #include "hw/loader.h" 37 #include "hw/qdev-properties.h" 38 #include "hw/block/flash.h" 39 #include "sysemu/kvm.h" 40 #include "sysemu/sev.h" 41 42 #define FLASH_SECTOR_SIZE 4096 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 * KiB); 57 isa_bios = g_malloc(sizeof(*isa_bios)); 58 memory_region_init_ram(isa_bios, NULL, "isa-bios", isa_bios_size, 59 &error_fatal); 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 static PFlashCFI01 *pc_pflash_create(PCMachineState *pcms, 76 const char *name, 77 const char *alias_prop_name) 78 { 79 DeviceState *dev = qdev_new(TYPE_PFLASH_CFI01); 80 81 qdev_prop_set_uint64(dev, "sector-length", FLASH_SECTOR_SIZE); 82 qdev_prop_set_uint8(dev, "width", 1); 83 qdev_prop_set_string(dev, "name", name); 84 object_property_add_child(OBJECT(pcms), name, OBJECT(dev)); 85 object_property_add_alias(OBJECT(pcms), alias_prop_name, 86 OBJECT(dev), "drive"); 87 /* 88 * The returned reference is tied to the child property and 89 * will be removed with object_unparent. 90 */ 91 object_unref(OBJECT(dev)); 92 return PFLASH_CFI01(dev); 93 } 94 95 void pc_system_flash_create(PCMachineState *pcms) 96 { 97 PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms); 98 99 if (pcmc->pci_enabled) { 100 pcms->flash[0] = pc_pflash_create(pcms, "system.flash0", 101 "pflash0"); 102 pcms->flash[1] = pc_pflash_create(pcms, "system.flash1", 103 "pflash1"); 104 } 105 } 106 107 void pc_system_flash_cleanup_unused(PCMachineState *pcms) 108 { 109 char *prop_name; 110 int i; 111 Object *dev_obj; 112 113 assert(PC_MACHINE_GET_CLASS(pcms)->pci_enabled); 114 115 for (i = 0; i < ARRAY_SIZE(pcms->flash); i++) { 116 dev_obj = OBJECT(pcms->flash[i]); 117 if (!object_property_get_bool(dev_obj, "realized", &error_abort)) { 118 prop_name = g_strdup_printf("pflash%d", i); 119 object_property_del(OBJECT(pcms), prop_name); 120 g_free(prop_name); 121 object_unparent(dev_obj); 122 pcms->flash[i] = NULL; 123 } 124 } 125 } 126 127 #define OVMF_TABLE_FOOTER_GUID "96b582de-1fb2-45f7-baea-a366c55a082d" 128 129 static uint8_t *ovmf_table; 130 static int ovmf_table_len; 131 132 static void pc_system_parse_ovmf_flash(uint8_t *flash_ptr, size_t flash_size) 133 { 134 uint8_t *ptr; 135 QemuUUID guid; 136 int tot_len; 137 138 /* should only be called once */ 139 if (ovmf_table) { 140 return; 141 } 142 143 if (flash_size < TARGET_PAGE_SIZE) { 144 return; 145 } 146 147 /* 148 * if this is OVMF there will be a table footer 149 * guid 48 bytes before the end of the flash file. If it's 150 * not found, silently abort the flash parsing. 151 */ 152 qemu_uuid_parse(OVMF_TABLE_FOOTER_GUID, &guid); 153 guid = qemu_uuid_bswap(guid); /* guids are LE */ 154 ptr = flash_ptr + flash_size - 48; 155 if (!qemu_uuid_is_equal((QemuUUID *)ptr, &guid)) { 156 return; 157 } 158 159 /* if found, just before is two byte table length */ 160 ptr -= sizeof(uint16_t); 161 tot_len = le16_to_cpu(*(uint16_t *)ptr) - sizeof(guid) - sizeof(uint16_t); 162 163 if (tot_len <= 0) { 164 return; 165 } 166 167 ovmf_table = g_malloc(tot_len); 168 ovmf_table_len = tot_len; 169 170 /* 171 * ptr is the foot of the table, so copy it all to the newly 172 * allocated ovmf_table and then set the ovmf_table pointer 173 * to the table foot 174 */ 175 memcpy(ovmf_table, ptr - tot_len, tot_len); 176 ovmf_table += tot_len; 177 } 178 179 bool pc_system_ovmf_table_find(const char *entry, uint8_t **data, 180 int *data_len) 181 { 182 uint8_t *ptr = ovmf_table; 183 int tot_len = ovmf_table_len; 184 QemuUUID entry_guid; 185 186 if (qemu_uuid_parse(entry, &entry_guid) < 0) { 187 return false; 188 } 189 190 if (!ptr) { 191 return false; 192 } 193 194 entry_guid = qemu_uuid_bswap(entry_guid); /* guids are LE */ 195 while (tot_len >= sizeof(QemuUUID) + sizeof(uint16_t)) { 196 int len; 197 QemuUUID *guid; 198 199 /* 200 * The data structure is 201 * arbitrary length data 202 * 2 byte length of entire entry 203 * 16 byte guid 204 */ 205 guid = (QemuUUID *)(ptr - sizeof(QemuUUID)); 206 len = le16_to_cpu(*(uint16_t *)(ptr - sizeof(QemuUUID) - 207 sizeof(uint16_t))); 208 209 /* 210 * just in case the table is corrupt, wouldn't want to spin in 211 * the zero case 212 */ 213 if (len < sizeof(QemuUUID) + sizeof(uint16_t)) { 214 return false; 215 } else if (len > tot_len) { 216 return false; 217 } 218 219 ptr -= len; 220 tot_len -= len; 221 if (qemu_uuid_is_equal(guid, &entry_guid)) { 222 if (data) { 223 *data = ptr; 224 } 225 if (data_len) { 226 *data_len = len - sizeof(QemuUUID) - sizeof(uint16_t); 227 } 228 return true; 229 } 230 } 231 return false; 232 } 233 234 /* 235 * Map the pcms->flash[] from 4GiB downward, and realize. 236 * Map them in descending order, i.e. pcms->flash[0] at the top, 237 * without gaps. 238 * Stop at the first pcms->flash[0] lacking a block backend. 239 * Set each flash's size from its block backend. Fatal error if the 240 * size isn't a non-zero multiple of 4KiB, or the total size exceeds 241 * pcms->max_fw_size. 242 * 243 * If pcms->flash[0] has a block backend, its memory is passed to 244 * pc_isa_bios_init(). Merging several flash devices for isa-bios is 245 * not supported. 246 */ 247 static void pc_system_flash_map(PCMachineState *pcms, 248 MemoryRegion *rom_memory) 249 { 250 hwaddr total_size = 0; 251 int i; 252 BlockBackend *blk; 253 int64_t size; 254 PFlashCFI01 *system_flash; 255 MemoryRegion *flash_mem; 256 void *flash_ptr; 257 int flash_size; 258 int ret; 259 260 assert(PC_MACHINE_GET_CLASS(pcms)->pci_enabled); 261 262 for (i = 0; i < ARRAY_SIZE(pcms->flash); i++) { 263 system_flash = pcms->flash[i]; 264 blk = pflash_cfi01_get_blk(system_flash); 265 if (!blk) { 266 break; 267 } 268 size = blk_getlength(blk); 269 if (size < 0) { 270 error_report("can't get size of block device %s: %s", 271 blk_name(blk), strerror(-size)); 272 exit(1); 273 } 274 if (size == 0 || !QEMU_IS_ALIGNED(size, FLASH_SECTOR_SIZE)) { 275 error_report("system firmware block device %s has invalid size " 276 "%" PRId64, 277 blk_name(blk), size); 278 info_report("its size must be a non-zero multiple of 0x%x", 279 FLASH_SECTOR_SIZE); 280 exit(1); 281 } 282 if ((hwaddr)size != size 283 || total_size > HWADDR_MAX - size 284 || total_size + size > pcms->max_fw_size) { 285 error_report("combined size of system firmware exceeds " 286 "%" PRIu64 " bytes", 287 pcms->max_fw_size); 288 exit(1); 289 } 290 291 total_size += size; 292 qdev_prop_set_uint32(DEVICE(system_flash), "num-blocks", 293 size / FLASH_SECTOR_SIZE); 294 sysbus_realize_and_unref(SYS_BUS_DEVICE(system_flash), &error_fatal); 295 sysbus_mmio_map(SYS_BUS_DEVICE(system_flash), 0, 296 0x100000000ULL - total_size); 297 298 if (i == 0) { 299 flash_mem = pflash_cfi01_get_memory(system_flash); 300 pc_isa_bios_init(rom_memory, flash_mem, size); 301 302 /* Encrypt the pflash boot ROM */ 303 if (sev_enabled()) { 304 flash_ptr = memory_region_get_ram_ptr(flash_mem); 305 flash_size = memory_region_size(flash_mem); 306 /* 307 * OVMF places a GUIDed structures in the flash, so 308 * search for them 309 */ 310 pc_system_parse_ovmf_flash(flash_ptr, flash_size); 311 312 ret = sev_es_save_reset_vector(flash_ptr, flash_size); 313 if (ret) { 314 error_report("failed to locate and/or save reset vector"); 315 exit(1); 316 } 317 318 sev_encrypt_flash(flash_ptr, flash_size, &error_fatal); 319 } 320 } 321 } 322 } 323 324 void pc_system_firmware_init(PCMachineState *pcms, 325 MemoryRegion *rom_memory) 326 { 327 PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms); 328 int i; 329 BlockBackend *pflash_blk[ARRAY_SIZE(pcms->flash)]; 330 331 if (!pcmc->pci_enabled) { 332 x86_bios_rom_init(MACHINE(pcms), "bios.bin", rom_memory, true); 333 return; 334 } 335 336 /* Map legacy -drive if=pflash to machine properties */ 337 for (i = 0; i < ARRAY_SIZE(pcms->flash); i++) { 338 pflash_cfi01_legacy_drive(pcms->flash[i], 339 drive_get(IF_PFLASH, 0, i)); 340 pflash_blk[i] = pflash_cfi01_get_blk(pcms->flash[i]); 341 } 342 343 /* Reject gaps */ 344 for (i = 1; i < ARRAY_SIZE(pcms->flash); i++) { 345 if (pflash_blk[i] && !pflash_blk[i - 1]) { 346 error_report("pflash%d requires pflash%d", i, i - 1); 347 exit(1); 348 } 349 } 350 351 if (!pflash_blk[0]) { 352 /* Machine property pflash0 not set, use ROM mode */ 353 x86_bios_rom_init(MACHINE(pcms), "bios.bin", rom_memory, false); 354 } else { 355 if (kvm_enabled() && !kvm_readonly_mem_enabled()) { 356 /* 357 * Older KVM cannot execute from device memory. So, flash 358 * memory cannot be used unless the readonly memory kvm 359 * capability is present. 360 */ 361 error_report("pflash with kvm requires KVM readonly memory support"); 362 exit(1); 363 } 364 365 pc_system_flash_map(pcms, rom_memory); 366 } 367 368 pc_system_flash_cleanup_unused(pcms); 369 } 370