1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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 "sysemu/sysemu.h"
39#include "hw/block/flash.h"
40#include "sysemu/kvm.h"
41#include "sysemu/sev.h"
42
43#define FLASH_SECTOR_SIZE 4096
44
45static void pc_isa_bios_init(MemoryRegion *rom_memory,
46 MemoryRegion *flash_mem,
47 int ram_size)
48{
49 int isa_bios_size;
50 MemoryRegion *isa_bios;
51 uint64_t flash_size;
52 void *flash_ptr, *isa_bios_ptr;
53
54 flash_size = memory_region_size(flash_mem);
55
56
57 isa_bios_size = MIN(flash_size, 128 * KiB);
58 isa_bios = g_malloc(sizeof(*isa_bios));
59 memory_region_init_ram(isa_bios, NULL, "isa-bios", isa_bios_size,
60 &error_fatal);
61 memory_region_add_subregion_overlap(rom_memory,
62 0x100000 - isa_bios_size,
63 isa_bios,
64 1);
65
66
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 memory_region_set_readonly(isa_bios, true);
74}
75
76static PFlashCFI01 *pc_pflash_create(PCMachineState *pcms,
77 const char *name,
78 const char *alias_prop_name)
79{
80 DeviceState *dev = qdev_new(TYPE_PFLASH_CFI01);
81
82 qdev_prop_set_uint64(dev, "sector-length", FLASH_SECTOR_SIZE);
83 qdev_prop_set_uint8(dev, "width", 1);
84 qdev_prop_set_string(dev, "name", name);
85 object_property_add_child(OBJECT(pcms), name, OBJECT(dev));
86 object_property_add_alias(OBJECT(pcms), alias_prop_name,
87 OBJECT(dev), "drive");
88
89
90
91
92 object_unref(OBJECT(dev));
93 return PFLASH_CFI01(dev);
94}
95
96void pc_system_flash_create(PCMachineState *pcms)
97{
98 PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
99
100 if (pcmc->pci_enabled) {
101 pcms->flash[0] = pc_pflash_create(pcms, "system.flash0",
102 "pflash0");
103 pcms->flash[1] = pc_pflash_create(pcms, "system.flash1",
104 "pflash1");
105 }
106}
107
108void pc_system_flash_cleanup_unused(PCMachineState *pcms)
109{
110 char *prop_name;
111 int i;
112 Object *dev_obj;
113
114 assert(PC_MACHINE_GET_CLASS(pcms)->pci_enabled);
115
116 for (i = 0; i < ARRAY_SIZE(pcms->flash); i++) {
117 dev_obj = OBJECT(pcms->flash[i]);
118 if (!object_property_get_bool(dev_obj, "realized", &error_abort)) {
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(dev_obj);
123 pcms->flash[i] = NULL;
124 }
125 }
126}
127
128#define OVMF_TABLE_FOOTER_GUID "96b582de-1fb2-45f7-baea-a366c55a082d"
129
130static uint8_t *ovmf_table;
131static int ovmf_table_len;
132
133static void pc_system_parse_ovmf_flash(uint8_t *flash_ptr, size_t flash_size)
134{
135 uint8_t *ptr;
136 QemuUUID guid;
137 int tot_len;
138
139
140 if (ovmf_table) {
141 return;
142 }
143
144 if (flash_size < TARGET_PAGE_SIZE) {
145 return;
146 }
147
148
149
150
151
152
153 qemu_uuid_parse(OVMF_TABLE_FOOTER_GUID, &guid);
154 guid = qemu_uuid_bswap(guid);
155 ptr = flash_ptr + flash_size - 48;
156 if (!qemu_uuid_is_equal((QemuUUID *)ptr, &guid)) {
157 return;
158 }
159
160
161 ptr -= sizeof(uint16_t);
162 tot_len = le16_to_cpu(*(uint16_t *)ptr) - sizeof(guid) - sizeof(uint16_t);
163
164 if (tot_len <= 0) {
165 return;
166 }
167
168 ovmf_table = g_malloc(tot_len);
169 ovmf_table_len = tot_len;
170
171
172
173
174
175
176 memcpy(ovmf_table, ptr - tot_len, tot_len);
177 ovmf_table += tot_len;
178}
179
180bool pc_system_ovmf_table_find(const char *entry, uint8_t **data,
181 int *data_len)
182{
183 uint8_t *ptr = ovmf_table;
184 int tot_len = ovmf_table_len;
185 QemuUUID entry_guid;
186
187 if (qemu_uuid_parse(entry, &entry_guid) < 0) {
188 return false;
189 }
190
191 if (!ptr) {
192 return false;
193 }
194
195 entry_guid = qemu_uuid_bswap(entry_guid);
196 while (tot_len >= sizeof(QemuUUID) + sizeof(uint16_t)) {
197 int len;
198 QemuUUID *guid;
199
200
201
202
203
204
205
206 guid = (QemuUUID *)(ptr - sizeof(QemuUUID));
207 len = le16_to_cpu(*(uint16_t *)(ptr - sizeof(QemuUUID) -
208 sizeof(uint16_t)));
209
210
211
212
213
214 if (len < sizeof(QemuUUID) + sizeof(uint16_t)) {
215 return false;
216 } else if (len > tot_len) {
217 return false;
218 }
219
220 ptr -= len;
221 tot_len -= len;
222 if (qemu_uuid_is_equal(guid, &entry_guid)) {
223 if (data) {
224 *data = ptr;
225 }
226 if (data_len) {
227 *data_len = len - sizeof(QemuUUID) - sizeof(uint16_t);
228 }
229 return true;
230 }
231 }
232 return false;
233}
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248static void pc_system_flash_map(PCMachineState *pcms,
249 MemoryRegion *rom_memory)
250{
251 hwaddr total_size = 0;
252 int i;
253 BlockBackend *blk;
254 int64_t size;
255 PFlashCFI01 *system_flash;
256 MemoryRegion *flash_mem;
257 void *flash_ptr;
258 int flash_size;
259 int ret;
260
261 assert(PC_MACHINE_GET_CLASS(pcms)->pci_enabled);
262
263 for (i = 0; i < ARRAY_SIZE(pcms->flash); i++) {
264 system_flash = pcms->flash[i];
265 blk = pflash_cfi01_get_blk(system_flash);
266 if (!blk) {
267 break;
268 }
269 size = blk_getlength(blk);
270 if (size < 0) {
271 error_report("can't get size of block device %s: %s",
272 blk_name(blk), strerror(-size));
273 exit(1);
274 }
275 if (size == 0 || !QEMU_IS_ALIGNED(size, FLASH_SECTOR_SIZE)) {
276 error_report("system firmware block device %s has invalid size "
277 "%" PRId64,
278 blk_name(blk), size);
279 info_report("its size must be a non-zero multiple of 0x%x",
280 FLASH_SECTOR_SIZE);
281 exit(1);
282 }
283 if ((hwaddr)size != size
284 || total_size > HWADDR_MAX - size
285 || total_size + size > pcms->max_fw_size) {
286 error_report("combined size of system firmware exceeds "
287 "%" PRIu64 " bytes",
288 pcms->max_fw_size);
289 exit(1);
290 }
291
292 total_size += size;
293 qdev_prop_set_uint32(DEVICE(system_flash), "num-blocks",
294 size / FLASH_SECTOR_SIZE);
295 sysbus_realize_and_unref(SYS_BUS_DEVICE(system_flash), &error_fatal);
296 sysbus_mmio_map(SYS_BUS_DEVICE(system_flash), 0,
297 0x100000000ULL - total_size);
298
299 if (i == 0) {
300 flash_mem = pflash_cfi01_get_memory(system_flash);
301 pc_isa_bios_init(rom_memory, flash_mem, size);
302
303
304 if (sev_enabled()) {
305 flash_ptr = memory_region_get_ram_ptr(flash_mem);
306 flash_size = memory_region_size(flash_mem);
307
308
309
310
311 pc_system_parse_ovmf_flash(flash_ptr, flash_size);
312
313 ret = sev_es_save_reset_vector(flash_ptr, flash_size);
314 if (ret) {
315 error_report("failed to locate and/or save reset vector");
316 exit(1);
317 }
318
319 sev_encrypt_flash(flash_ptr, flash_size, &error_fatal);
320 }
321 }
322 }
323}
324
325void pc_system_firmware_init(PCMachineState *pcms,
326 MemoryRegion *rom_memory)
327{
328 PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
329 int i;
330 BlockBackend *pflash_blk[ARRAY_SIZE(pcms->flash)];
331
332 if (!pcmc->pci_enabled) {
333 x86_bios_rom_init(MACHINE(pcms), "bios.bin", rom_memory, true);
334 return;
335 }
336
337
338 for (i = 0; i < ARRAY_SIZE(pcms->flash); i++) {
339 pflash_cfi01_legacy_drive(pcms->flash[i],
340 drive_get(IF_PFLASH, 0, i));
341 pflash_blk[i] = pflash_cfi01_get_blk(pcms->flash[i]);
342 }
343
344
345 for (i = 1; i < ARRAY_SIZE(pcms->flash); i++) {
346 if (pflash_blk[i] && !pflash_blk[i - 1]) {
347 error_report("pflash%d requires pflash%d", i, i - 1);
348 exit(1);
349 }
350 }
351
352 if (!pflash_blk[0]) {
353
354 x86_bios_rom_init(MACHINE(pcms), "bios.bin", rom_memory, false);
355 } else {
356 if (kvm_enabled() && !kvm_readonly_mem_enabled()) {
357
358
359
360
361
362 error_report("pflash with kvm requires KVM readonly memory support");
363 exit(1);
364 }
365
366 pc_system_flash_map(pcms, rom_memory);
367 }
368
369 pc_system_flash_cleanup_unused(pcms);
370}
371