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