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