hw/i386/pc: Document pc_system_ovmf_table_find
[qemu/kevin.git] / hw / i386 / pc_sysfw.c
blob6ddce92a86171cb0109cae81263b7f4ec327c4c2
1 /*
2 * QEMU PC System Firmware
4 * Copyright (c) 2003-2004 Fabrice Bellard
5 * Copyright (c) 2011-2012 Intel Corporation
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:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
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.
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"
42 #define FLASH_SECTOR_SIZE 4096
44 static void pc_isa_bios_init(MemoryRegion *rom_memory,
45 MemoryRegion *flash_mem,
46 int ram_size)
48 int isa_bios_size;
49 MemoryRegion *isa_bios;
50 uint64_t flash_size;
51 void *flash_ptr, *isa_bios_ptr;
53 flash_size = memory_region_size(flash_mem);
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);
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);
72 memory_region_set_readonly(isa_bios, true);
75 static PFlashCFI01 *pc_pflash_create(PCMachineState *pcms,
76 const char *name,
77 const char *alias_prop_name)
79 DeviceState *dev = qdev_new(TYPE_PFLASH_CFI01);
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");
88 * The returned reference is tied to the child property and
89 * will be removed with object_unparent.
91 object_unref(OBJECT(dev));
92 return PFLASH_CFI01(dev);
95 void pc_system_flash_create(PCMachineState *pcms)
97 PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
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");
107 void pc_system_flash_cleanup_unused(PCMachineState *pcms)
109 char *prop_name;
110 int i;
111 Object *dev_obj;
113 assert(PC_MACHINE_GET_CLASS(pcms)->pci_enabled);
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;
127 #define OVMF_TABLE_FOOTER_GUID "96b582de-1fb2-45f7-baea-a366c55a082d"
129 static bool ovmf_flash_parsed;
130 static uint8_t *ovmf_table;
131 static int ovmf_table_len;
133 static void pc_system_parse_ovmf_flash(uint8_t *flash_ptr, size_t flash_size)
135 uint8_t *ptr;
136 QemuUUID guid;
137 int tot_len;
139 /* should only be called once */
140 if (ovmf_flash_parsed) {
141 return;
144 ovmf_flash_parsed = true;
146 if (flash_size < TARGET_PAGE_SIZE) {
147 return;
151 * if this is OVMF there will be a table footer
152 * guid 48 bytes before the end of the flash file. If it's
153 * not found, silently abort the flash parsing.
155 qemu_uuid_parse(OVMF_TABLE_FOOTER_GUID, &guid);
156 guid = qemu_uuid_bswap(guid); /* guids are LE */
157 ptr = flash_ptr + flash_size - 48;
158 if (!qemu_uuid_is_equal((QemuUUID *)ptr, &guid)) {
159 return;
162 /* if found, just before is two byte table length */
163 ptr -= sizeof(uint16_t);
164 tot_len = le16_to_cpu(*(uint16_t *)ptr) - sizeof(guid) - sizeof(uint16_t);
166 if (tot_len <= 0) {
167 return;
170 ovmf_table = g_malloc(tot_len);
171 ovmf_table_len = tot_len;
174 * ptr is the foot of the table, so copy it all to the newly
175 * allocated ovmf_table and then set the ovmf_table pointer
176 * to the table foot
178 memcpy(ovmf_table, ptr - tot_len, tot_len);
179 ovmf_table += tot_len;
183 * pc_system_ovmf_table_find - Find the data associated with an entry in OVMF's
184 * reset vector GUIDed table.
186 * @entry: GUID string of the entry to lookup
187 * @data: Filled with a pointer to the entry's value (if not NULL)
188 * @data_len: Filled with the length of the entry's value (if not NULL). Pass
189 * NULL here if the length of data is known.
191 * Return: true if the entry was found in the OVMF table; false otherwise.
193 bool pc_system_ovmf_table_find(const char *entry, uint8_t **data,
194 int *data_len)
196 uint8_t *ptr = ovmf_table;
197 int tot_len = ovmf_table_len;
198 QemuUUID entry_guid;
200 assert(ovmf_flash_parsed);
202 if (qemu_uuid_parse(entry, &entry_guid) < 0) {
203 return false;
206 if (!ptr) {
207 return false;
210 entry_guid = qemu_uuid_bswap(entry_guid); /* guids are LE */
211 while (tot_len >= sizeof(QemuUUID) + sizeof(uint16_t)) {
212 int len;
213 QemuUUID *guid;
216 * The data structure is
217 * arbitrary length data
218 * 2 byte length of entire entry
219 * 16 byte guid
221 guid = (QemuUUID *)(ptr - sizeof(QemuUUID));
222 len = le16_to_cpu(*(uint16_t *)(ptr - sizeof(QemuUUID) -
223 sizeof(uint16_t)));
226 * just in case the table is corrupt, wouldn't want to spin in
227 * the zero case
229 if (len < sizeof(QemuUUID) + sizeof(uint16_t)) {
230 return false;
231 } else if (len > tot_len) {
232 return false;
235 ptr -= len;
236 tot_len -= len;
237 if (qemu_uuid_is_equal(guid, &entry_guid)) {
238 if (data) {
239 *data = ptr;
241 if (data_len) {
242 *data_len = len - sizeof(QemuUUID) - sizeof(uint16_t);
244 return true;
247 return false;
251 * Map the pcms->flash[] from 4GiB downward, and realize.
252 * Map them in descending order, i.e. pcms->flash[0] at the top,
253 * without gaps.
254 * Stop at the first pcms->flash[0] lacking a block backend.
255 * Set each flash's size from its block backend. Fatal error if the
256 * size isn't a non-zero multiple of 4KiB, or the total size exceeds
257 * pcms->max_fw_size.
259 * If pcms->flash[0] has a block backend, its memory is passed to
260 * pc_isa_bios_init(). Merging several flash devices for isa-bios is
261 * not supported.
263 static void pc_system_flash_map(PCMachineState *pcms,
264 MemoryRegion *rom_memory)
266 hwaddr total_size = 0;
267 int i;
268 BlockBackend *blk;
269 int64_t size;
270 PFlashCFI01 *system_flash;
271 MemoryRegion *flash_mem;
272 void *flash_ptr;
273 int flash_size;
274 int ret;
276 assert(PC_MACHINE_GET_CLASS(pcms)->pci_enabled);
278 for (i = 0; i < ARRAY_SIZE(pcms->flash); i++) {
279 system_flash = pcms->flash[i];
280 blk = pflash_cfi01_get_blk(system_flash);
281 if (!blk) {
282 break;
284 size = blk_getlength(blk);
285 if (size < 0) {
286 error_report("can't get size of block device %s: %s",
287 blk_name(blk), strerror(-size));
288 exit(1);
290 if (size == 0 || !QEMU_IS_ALIGNED(size, FLASH_SECTOR_SIZE)) {
291 error_report("system firmware block device %s has invalid size "
292 "%" PRId64,
293 blk_name(blk), size);
294 info_report("its size must be a non-zero multiple of 0x%x",
295 FLASH_SECTOR_SIZE);
296 exit(1);
298 if ((hwaddr)size != size
299 || total_size > HWADDR_MAX - size
300 || total_size + size > pcms->max_fw_size) {
301 error_report("combined size of system firmware exceeds "
302 "%" PRIu64 " bytes",
303 pcms->max_fw_size);
304 exit(1);
307 total_size += size;
308 qdev_prop_set_uint32(DEVICE(system_flash), "num-blocks",
309 size / FLASH_SECTOR_SIZE);
310 sysbus_realize_and_unref(SYS_BUS_DEVICE(system_flash), &error_fatal);
311 sysbus_mmio_map(SYS_BUS_DEVICE(system_flash), 0,
312 0x100000000ULL - total_size);
314 if (i == 0) {
315 flash_mem = pflash_cfi01_get_memory(system_flash);
316 pc_isa_bios_init(rom_memory, flash_mem, size);
318 /* Encrypt the pflash boot ROM */
319 if (sev_enabled()) {
320 flash_ptr = memory_region_get_ram_ptr(flash_mem);
321 flash_size = memory_region_size(flash_mem);
323 * OVMF places a GUIDed structures in the flash, so
324 * search for them
326 pc_system_parse_ovmf_flash(flash_ptr, flash_size);
328 ret = sev_es_save_reset_vector(flash_ptr, flash_size);
329 if (ret) {
330 error_report("failed to locate and/or save reset vector");
331 exit(1);
334 sev_encrypt_flash(flash_ptr, flash_size, &error_fatal);
340 void pc_system_firmware_init(PCMachineState *pcms,
341 MemoryRegion *rom_memory)
343 PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
344 int i;
345 BlockBackend *pflash_blk[ARRAY_SIZE(pcms->flash)];
347 if (!pcmc->pci_enabled) {
348 x86_bios_rom_init(MACHINE(pcms), "bios.bin", rom_memory, true);
349 return;
352 /* Map legacy -drive if=pflash to machine properties */
353 for (i = 0; i < ARRAY_SIZE(pcms->flash); i++) {
354 pflash_cfi01_legacy_drive(pcms->flash[i],
355 drive_get(IF_PFLASH, 0, i));
356 pflash_blk[i] = pflash_cfi01_get_blk(pcms->flash[i]);
359 /* Reject gaps */
360 for (i = 1; i < ARRAY_SIZE(pcms->flash); i++) {
361 if (pflash_blk[i] && !pflash_blk[i - 1]) {
362 error_report("pflash%d requires pflash%d", i, i - 1);
363 exit(1);
367 if (!pflash_blk[0]) {
368 /* Machine property pflash0 not set, use ROM mode */
369 x86_bios_rom_init(MACHINE(pcms), "bios.bin", rom_memory, false);
370 } else {
371 if (kvm_enabled() && !kvm_readonly_mem_enabled()) {
373 * Older KVM cannot execute from device memory. So, flash
374 * memory cannot be used unless the readonly memory kvm
375 * capability is present.
377 error_report("pflash with kvm requires KVM readonly memory support");
378 exit(1);
381 pc_system_flash_map(pcms, rom_memory);
384 pc_system_flash_cleanup_unused(pcms);