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 "qapi/error.h"
28#include "sysemu/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 "sysemu/kvm.h"
39#include "sev.h"
40
41#define FLASH_SECTOR_SIZE 4096
42
43static void pc_isa_bios_init(MemoryRegion *rom_memory,
44 MemoryRegion *flash_mem,
45 int ram_size)
46{
47 int isa_bios_size;
48 MemoryRegion *isa_bios;
49 uint64_t flash_size;
50 void *flash_ptr, *isa_bios_ptr;
51
52 flash_size = memory_region_size(flash_mem);
53
54
55 isa_bios_size = MIN(flash_size, 128 * KiB);
56 isa_bios = g_malloc(sizeof(*isa_bios));
57 memory_region_init_ram(isa_bios, NULL, "isa-bios", isa_bios_size,
58 &error_fatal);
59 memory_region_add_subregion_overlap(rom_memory,
60 0x100000 - isa_bios_size,
61 isa_bios,
62 1);
63
64
65 flash_ptr = memory_region_get_ram_ptr(flash_mem);
66 isa_bios_ptr = memory_region_get_ram_ptr(isa_bios);
67 memcpy(isa_bios_ptr,
68 ((uint8_t*)flash_ptr) + (flash_size - isa_bios_size),
69 isa_bios_size);
70
71 memory_region_set_readonly(isa_bios, true);
72}
73
74static PFlashCFI01 *pc_pflash_create(PCMachineState *pcms,
75 const char *name,
76 const char *alias_prop_name)
77{
78 DeviceState *dev = qdev_new(TYPE_PFLASH_CFI01);
79
80 qdev_prop_set_uint64(dev, "sector-length", FLASH_SECTOR_SIZE);
81 qdev_prop_set_uint8(dev, "width", 1);
82 qdev_prop_set_string(dev, "name", name);
83 object_property_add_child(OBJECT(pcms), name, OBJECT(dev));
84 object_property_add_alias(OBJECT(pcms), alias_prop_name,
85 OBJECT(dev), "drive");
86
87
88
89
90 object_unref(OBJECT(dev));
91 return PFLASH_CFI01(dev);
92}
93
94void pc_system_flash_create(PCMachineState *pcms)
95{
96 PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
97
98 if (pcmc->pci_enabled) {
99 pcms->flash[0] = pc_pflash_create(pcms, "system.flash0",
100 "pflash0");
101 pcms->flash[1] = pc_pflash_create(pcms, "system.flash1",
102 "pflash1");
103 }
104}
105
106void pc_system_flash_cleanup_unused(PCMachineState *pcms)
107{
108 char *prop_name;
109 int i;
110 Object *dev_obj;
111
112 assert(PC_MACHINE_GET_CLASS(pcms)->pci_enabled);
113
114 for (i = 0; i < ARRAY_SIZE(pcms->flash); i++) {
115 dev_obj = OBJECT(pcms->flash[i]);
116 if (!object_property_get_bool(dev_obj, "realized", &error_abort)) {
117 prop_name = g_strdup_printf("pflash%d", i);
118 object_property_del(OBJECT(pcms), prop_name);
119 g_free(prop_name);
120 object_unparent(dev_obj);
121 pcms->flash[i] = NULL;
122 }
123 }
124}
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139static void pc_system_flash_map(PCMachineState *pcms,
140 MemoryRegion *rom_memory)
141{
142 hwaddr total_size = 0;
143 int i;
144 BlockBackend *blk;
145 int64_t size;
146 PFlashCFI01 *system_flash;
147 MemoryRegion *flash_mem;
148 void *flash_ptr;
149 int flash_size;
150
151 assert(PC_MACHINE_GET_CLASS(pcms)->pci_enabled);
152
153 for (i = 0; i < ARRAY_SIZE(pcms->flash); i++) {
154 system_flash = pcms->flash[i];
155 blk = pflash_cfi01_get_blk(system_flash);
156 if (!blk) {
157 break;
158 }
159 size = blk_getlength(blk);
160 if (size < 0) {
161 error_report("can't get size of block device %s: %s",
162 blk_name(blk), strerror(-size));
163 exit(1);
164 }
165 if (size == 0 || !QEMU_IS_ALIGNED(size, FLASH_SECTOR_SIZE)) {
166 error_report("system firmware block device %s has invalid size "
167 "%" PRId64,
168 blk_name(blk), size);
169 info_report("its size must be a non-zero multiple of 0x%x",
170 FLASH_SECTOR_SIZE);
171 exit(1);
172 }
173 if ((hwaddr)size != size
174 || total_size > HWADDR_MAX - size
175 || total_size + size > pcms->max_fw_size) {
176 error_report("combined size of system firmware exceeds "
177 "%" PRIu64 " bytes",
178 pcms->max_fw_size);
179 exit(1);
180 }
181
182 total_size += size;
183 qdev_prop_set_uint32(DEVICE(system_flash), "num-blocks",
184 size / FLASH_SECTOR_SIZE);
185 sysbus_realize_and_unref(SYS_BUS_DEVICE(system_flash), &error_fatal);
186 sysbus_mmio_map(SYS_BUS_DEVICE(system_flash), 0,
187 0x100000000ULL - total_size);
188
189 if (i == 0) {
190 flash_mem = pflash_cfi01_get_memory(system_flash);
191 pc_isa_bios_init(rom_memory, flash_mem, size);
192
193
194 if (sev_enabled()) {
195 flash_ptr = memory_region_get_ram_ptr(flash_mem);
196 flash_size = memory_region_size(flash_mem);
197 x86_firmware_configure(flash_ptr, flash_size);
198 }
199 }
200 }
201}
202
203void pc_system_firmware_init(PCMachineState *pcms,
204 MemoryRegion *rom_memory)
205{
206 PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
207 int i;
208 BlockBackend *pflash_blk[ARRAY_SIZE(pcms->flash)];
209
210 if (!pcmc->pci_enabled) {
211 x86_bios_rom_init(MACHINE(pcms), "bios.bin", rom_memory, true);
212 return;
213 }
214
215
216 for (i = 0; i < ARRAY_SIZE(pcms->flash); i++) {
217 pflash_cfi01_legacy_drive(pcms->flash[i],
218 drive_get(IF_PFLASH, 0, i));
219 pflash_blk[i] = pflash_cfi01_get_blk(pcms->flash[i]);
220 }
221
222
223 for (i = 1; i < ARRAY_SIZE(pcms->flash); i++) {
224 if (pflash_blk[i] && !pflash_blk[i - 1]) {
225 error_report("pflash%d requires pflash%d", i, i - 1);
226 exit(1);
227 }
228 }
229
230 if (!pflash_blk[0]) {
231
232 x86_bios_rom_init(MACHINE(pcms), "bios.bin", rom_memory, false);
233 } else {
234 if (kvm_enabled() && !kvm_readonly_mem_enabled()) {
235
236
237
238
239
240 error_report("pflash with kvm requires KVM readonly memory support");
241 exit(1);
242 }
243
244 pc_system_flash_map(pcms, rom_memory);
245 }
246
247 pc_system_flash_cleanup_unused(pcms);
248}
249
250void x86_firmware_configure(void *ptr, int size)
251{
252 int ret;
253
254
255
256
257
258 pc_system_parse_ovmf_flash(ptr, size);
259
260 if (sev_enabled()) {
261 ret = sev_es_save_reset_vector(ptr, size);
262 if (ret) {
263 error_report("failed to locate and/or save reset vector");
264 exit(1);
265 }
266
267 sev_encrypt_flash(ptr, size, &error_fatal);
268 }
269}
270