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
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"
43 #define FLASH_SECTOR_SIZE 4096
45 static void pc_isa_bios_init(MemoryRegion
*rom_memory
,
46 MemoryRegion
*flash_mem
,
50 MemoryRegion
*isa_bios
;
52 void *flash_ptr
, *isa_bios_ptr
;
54 flash_size
= memory_region_size(flash_mem
);
56 /* map the last 128KB of the BIOS in ISA space */
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
,
61 memory_region_add_subregion_overlap(rom_memory
,
62 0x100000 - isa_bios_size
,
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
);
70 ((uint8_t*)flash_ptr
) + (flash_size
- isa_bios_size
),
73 memory_region_set_readonly(isa_bios
, true);
76 static PFlashCFI01
*pc_pflash_create(PCMachineState
*pcms
,
78 const char *alias_prop_name
)
80 DeviceState
*dev
= qdev_new(TYPE_PFLASH_CFI01
);
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");
89 * The returned reference is tied to the child property and
90 * will be removed with object_unparent.
92 object_unref(OBJECT(dev
));
93 return PFLASH_CFI01(dev
);
96 void pc_system_flash_create(PCMachineState
*pcms
)
98 PCMachineClass
*pcmc
= PC_MACHINE_GET_CLASS(pcms
);
100 if (pcmc
->pci_enabled
) {
101 pcms
->flash
[0] = pc_pflash_create(pcms
, "system.flash0",
103 pcms
->flash
[1] = pc_pflash_create(pcms
, "system.flash1",
108 void pc_system_flash_cleanup_unused(PCMachineState
*pcms
)
114 assert(PC_MACHINE_GET_CLASS(pcms
)->pci_enabled
);
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
);
122 object_unparent(dev_obj
);
123 pcms
->flash
[i
] = NULL
;
128 #define OVMF_TABLE_FOOTER_GUID "96b582de-1fb2-45f7-baea-a366c55a082d"
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
)
139 /* should only be called once */
144 if (flash_size
< TARGET_PAGE_SIZE
) {
149 * if this is OVMF there will be a table footer
150 * guid 48 bytes before the end of the flash file. If it's
151 * not found, silently abort the flash parsing.
153 qemu_uuid_parse(OVMF_TABLE_FOOTER_GUID
, &guid
);
154 guid
= qemu_uuid_bswap(guid
); /* guids are LE */
155 ptr
= flash_ptr
+ flash_size
- 48;
156 if (!qemu_uuid_is_equal((QemuUUID
*)ptr
, &guid
)) {
160 /* if found, just before is two byte table length */
161 ptr
-= sizeof(uint16_t);
162 tot_len
= le16_to_cpu(*(uint16_t *)ptr
) - sizeof(guid
) - sizeof(uint16_t);
168 ovmf_table
= g_malloc(tot_len
);
169 ovmf_table_len
= tot_len
;
172 * ptr is the foot of the table, so copy it all to the newly
173 * allocated ovmf_table and then set the ovmf_table pointer
176 memcpy(ovmf_table
, ptr
- tot_len
, tot_len
);
177 ovmf_table
+= tot_len
;
180 bool pc_system_ovmf_table_find(const char *entry
, uint8_t **data
,
183 uint8_t *ptr
= ovmf_table
;
184 int tot_len
= ovmf_table_len
;
187 if (qemu_uuid_parse(entry
, &entry_guid
) < 0) {
195 entry_guid
= qemu_uuid_bswap(entry_guid
); /* guids are LE */
196 while (tot_len
>= sizeof(QemuUUID
) + sizeof(uint16_t)) {
201 * The data structure is
202 * arbitrary length data
203 * 2 byte length of entire entry
206 guid
= (QemuUUID
*)(ptr
- sizeof(QemuUUID
));
207 len
= le16_to_cpu(*(uint16_t *)(ptr
- sizeof(QemuUUID
) -
211 * just in case the table is corrupt, wouldn't want to spin in
214 if (len
< sizeof(QemuUUID
) + sizeof(uint16_t)) {
216 } else if (len
> tot_len
) {
222 if (qemu_uuid_is_equal(guid
, &entry_guid
)) {
227 *data_len
= len
- sizeof(QemuUUID
) - sizeof(uint16_t);
236 * Map the pcms->flash[] from 4GiB downward, and realize.
237 * Map them in descending order, i.e. pcms->flash[0] at the top,
239 * Stop at the first pcms->flash[0] lacking a block backend.
240 * Set each flash's size from its block backend. Fatal error if the
241 * size isn't a non-zero multiple of 4KiB, or the total size exceeds
244 * If pcms->flash[0] has a block backend, its memory is passed to
245 * pc_isa_bios_init(). Merging several flash devices for isa-bios is
248 static void pc_system_flash_map(PCMachineState
*pcms
,
249 MemoryRegion
*rom_memory
)
251 hwaddr total_size
= 0;
255 PFlashCFI01
*system_flash
;
256 MemoryRegion
*flash_mem
;
261 assert(PC_MACHINE_GET_CLASS(pcms
)->pci_enabled
);
263 for (i
= 0; i
< ARRAY_SIZE(pcms
->flash
); i
++) {
264 system_flash
= pcms
->flash
[i
];
265 blk
= pflash_cfi01_get_blk(system_flash
);
269 size
= blk_getlength(blk
);
271 error_report("can't get size of block device %s: %s",
272 blk_name(blk
), strerror(-size
));
275 if (size
== 0 || !QEMU_IS_ALIGNED(size
, FLASH_SECTOR_SIZE
)) {
276 error_report("system firmware block device %s has invalid size "
278 blk_name(blk
), size
);
279 info_report("its size must be a non-zero multiple of 0x%x",
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 "
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
);
300 flash_mem
= pflash_cfi01_get_memory(system_flash
);
301 pc_isa_bios_init(rom_memory
, flash_mem
, size
);
303 /* Encrypt the pflash boot ROM */
305 flash_ptr
= memory_region_get_ram_ptr(flash_mem
);
306 flash_size
= memory_region_size(flash_mem
);
308 * OVMF places a GUIDed structures in the flash, so
311 pc_system_parse_ovmf_flash(flash_ptr
, flash_size
);
313 ret
= sev_es_save_reset_vector(flash_ptr
, flash_size
);
315 error_report("failed to locate and/or save reset vector");
319 sev_encrypt_flash(flash_ptr
, flash_size
, &error_fatal
);
325 void pc_system_firmware_init(PCMachineState
*pcms
,
326 MemoryRegion
*rom_memory
)
328 PCMachineClass
*pcmc
= PC_MACHINE_GET_CLASS(pcms
);
330 BlockBackend
*pflash_blk
[ARRAY_SIZE(pcms
->flash
)];
332 if (!pcmc
->pci_enabled
) {
333 x86_bios_rom_init(MACHINE(pcms
), "bios.bin", rom_memory
, true);
337 /* Map legacy -drive if=pflash to machine properties */
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
]);
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);
352 if (!pflash_blk
[0]) {
353 /* Machine property pflash0 not set, use ROM mode */
354 x86_bios_rom_init(MACHINE(pcms
), "bios.bin", rom_memory
, false);
356 if (kvm_enabled() && !kvm_readonly_mem_enabled()) {
358 * Older KVM cannot execute from device memory. So, flash
359 * memory cannot be used unless the readonly memory kvm
360 * capability is present.
362 error_report("pflash with kvm requires KVM readonly memory support");
366 pc_system_flash_map(pcms
, rom_memory
);
369 pc_system_flash_cleanup_unused(pcms
);