]>
Commit | Line | Data |
---|---|---|
cbc5b5f3 JJ |
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 | ||
b6a0aa05 | 26 | #include "qemu/osdep.h" |
da34e65c | 27 | #include "qapi/error.h" |
fa1d36df | 28 | #include "sysemu/block-backend.h" |
b4a42f81 | 29 | #include "qemu/error-report.h" |
922a01a0 | 30 | #include "qemu/option.h" |
d471bf3e | 31 | #include "qemu/units.h" |
83c9f4ca | 32 | #include "hw/sysbus.h" |
549e984e | 33 | #include "hw/i386/x86.h" |
0d09e41a | 34 | #include "hw/i386/pc.h" |
83c9f4ca | 35 | #include "hw/loader.h" |
a27bd6c7 | 36 | #include "hw/qdev-properties.h" |
0d09e41a | 37 | #include "hw/block/flash.h" |
9c17d615 | 38 | #include "sysemu/kvm.h" |
deae846f | 39 | #include "sev.h" |
cbc5b5f3 | 40 | |
ebc29e1b MA |
41 | #define FLASH_SECTOR_SIZE 4096 |
42 | ||
32d3ee87 | 43 | static void pc_isa_bios_init(MemoryRegion *isa_bios, MemoryRegion *rom_memory, |
f4b63768 | 44 | MemoryRegion *flash_mem) |
bd183c79 JJ |
45 | { |
46 | int isa_bios_size; | |
bd183c79 JJ |
47 | uint64_t flash_size; |
48 | void *flash_ptr, *isa_bios_ptr; | |
49 | ||
50 | flash_size = memory_region_size(flash_mem); | |
51 | ||
52 | /* map the last 128KB of the BIOS in ISA space */ | |
d471bf3e | 53 | isa_bios_size = MIN(flash_size, 128 * KiB); |
98a99ce0 | 54 | memory_region_init_ram(isa_bios, NULL, "isa-bios", isa_bios_size, |
f8ed85ac | 55 | &error_fatal); |
bd183c79 JJ |
56 | memory_region_add_subregion_overlap(rom_memory, |
57 | 0x100000 - isa_bios_size, | |
58 | isa_bios, | |
59 | 1); | |
60 | ||
61 | /* copy ISA rom image from top of flash memory */ | |
62 | flash_ptr = memory_region_get_ram_ptr(flash_mem); | |
63 | isa_bios_ptr = memory_region_get_ram_ptr(isa_bios); | |
64 | memcpy(isa_bios_ptr, | |
65 | ((uint8_t*)flash_ptr) + (flash_size - isa_bios_size), | |
66 | isa_bios_size); | |
67 | ||
68 | memory_region_set_readonly(isa_bios, true); | |
69 | } | |
70 | ||
ebc29e1b MA |
71 | static PFlashCFI01 *pc_pflash_create(PCMachineState *pcms, |
72 | const char *name, | |
73 | const char *alias_prop_name) | |
74 | { | |
df707969 | 75 | DeviceState *dev = qdev_new(TYPE_PFLASH_CFI01); |
637a5acb | 76 | |
ebc29e1b MA |
77 | qdev_prop_set_uint64(dev, "sector-length", FLASH_SECTOR_SIZE); |
78 | qdev_prop_set_uint8(dev, "width", 1); | |
79 | qdev_prop_set_string(dev, "name", name); | |
d2623129 | 80 | object_property_add_child(OBJECT(pcms), name, OBJECT(dev)); |
ebc29e1b | 81 | object_property_add_alias(OBJECT(pcms), alias_prop_name, |
d2623129 | 82 | OBJECT(dev), "drive"); |
0b33521e AB |
83 | /* |
84 | * The returned reference is tied to the child property and | |
85 | * will be removed with object_unparent. | |
86 | */ | |
87 | object_unref(OBJECT(dev)); | |
ebc29e1b MA |
88 | return PFLASH_CFI01(dev); |
89 | } | |
637a5acb | 90 | |
f2cb9f34 | 91 | void pc_system_flash_create(PCMachineState *pcms) |
0fbe8d7c BB |
92 | { |
93 | PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms); | |
94 | ||
95 | if (pcmc->pci_enabled) { | |
96 | pcms->flash[0] = pc_pflash_create(pcms, "system.flash0", | |
97 | "pflash0"); | |
98 | pcms->flash[1] = pc_pflash_create(pcms, "system.flash1", | |
99 | "pflash1"); | |
100 | } | |
101 | } | |
102 | ||
f2cb9f34 | 103 | void pc_system_flash_cleanup_unused(PCMachineState *pcms) |
ebc29e1b MA |
104 | { |
105 | char *prop_name; | |
106 | int i; | |
ebc29e1b MA |
107 | |
108 | assert(PC_MACHINE_GET_CLASS(pcms)->pci_enabled); | |
109 | ||
110 | for (i = 0; i < ARRAY_SIZE(pcms->flash); i++) { | |
58183abf | 111 | if (!qdev_is_realized(DEVICE(pcms->flash[i]))) { |
ebc29e1b | 112 | prop_name = g_strdup_printf("pflash%d", i); |
df4fe0b2 | 113 | object_property_del(OBJECT(pcms), prop_name); |
ebc29e1b | 114 | g_free(prop_name); |
58183abf | 115 | object_unparent(OBJECT(pcms->flash[i])); |
ebc29e1b MA |
116 | pcms->flash[i] = NULL; |
117 | } | |
118 | } | |
119 | } | |
120 | ||
121 | /* | |
122 | * Map the pcms->flash[] from 4GiB downward, and realize. | |
123 | * Map them in descending order, i.e. pcms->flash[0] at the top, | |
124 | * without gaps. | |
125 | * Stop at the first pcms->flash[0] lacking a block backend. | |
126 | * Set each flash's size from its block backend. Fatal error if the | |
127 | * size isn't a non-zero multiple of 4KiB, or the total size exceeds | |
0657c657 | 128 | * pcms->max_fw_size. |
637a5acb | 129 | * |
ebc29e1b MA |
130 | * If pcms->flash[0] has a block backend, its memory is passed to |
131 | * pc_isa_bios_init(). Merging several flash devices for isa-bios is | |
637a5acb LE |
132 | * not supported. |
133 | */ | |
ebc29e1b MA |
134 | static void pc_system_flash_map(PCMachineState *pcms, |
135 | MemoryRegion *rom_memory) | |
bd183c79 | 136 | { |
32d3ee87 | 137 | X86MachineState *x86ms = X86_MACHINE(pcms); |
ebc29e1b MA |
138 | hwaddr total_size = 0; |
139 | int i; | |
4be74634 | 140 | BlockBackend *blk; |
bd183c79 | 141 | int64_t size; |
16434065 | 142 | PFlashCFI01 *system_flash; |
bd183c79 | 143 | MemoryRegion *flash_mem; |
952e0668 | 144 | void *flash_ptr; |
aacdb844 | 145 | int flash_size; |
bd183c79 | 146 | |
ebc29e1b MA |
147 | assert(PC_MACHINE_GET_CLASS(pcms)->pci_enabled); |
148 | ||
149 | for (i = 0; i < ARRAY_SIZE(pcms->flash); i++) { | |
150 | system_flash = pcms->flash[i]; | |
151 | blk = pflash_cfi01_get_blk(system_flash); | |
152 | if (!blk) { | |
153 | break; | |
154 | } | |
4be74634 | 155 | size = blk_getlength(blk); |
637a5acb | 156 | if (size < 0) { |
ebc29e1b MA |
157 | error_report("can't get size of block device %s: %s", |
158 | blk_name(blk), strerror(-size)); | |
159 | exit(1); | |
637a5acb | 160 | } |
4cdd0a77 | 161 | if (size == 0 || !QEMU_IS_ALIGNED(size, FLASH_SECTOR_SIZE)) { |
ebc29e1b MA |
162 | error_report("system firmware block device %s has invalid size " |
163 | "%" PRId64, | |
164 | blk_name(blk), size); | |
165 | info_report("its size must be a non-zero multiple of 0x%x", | |
166 | FLASH_SECTOR_SIZE); | |
167 | exit(1); | |
168 | } | |
169 | if ((hwaddr)size != size | |
170 | || total_size > HWADDR_MAX - size | |
0657c657 | 171 | || total_size + size > pcms->max_fw_size) { |
ebc29e1b MA |
172 | error_report("combined size of system firmware exceeds " |
173 | "%" PRIu64 " bytes", | |
0657c657 | 174 | pcms->max_fw_size); |
637a5acb LE |
175 | exit(1); |
176 | } | |
177 | ||
ebc29e1b MA |
178 | total_size += size; |
179 | qdev_prop_set_uint32(DEVICE(system_flash), "num-blocks", | |
180 | size / FLASH_SECTOR_SIZE); | |
3c6ef471 | 181 | sysbus_realize_and_unref(SYS_BUS_DEVICE(system_flash), &error_fatal); |
ebc29e1b MA |
182 | sysbus_mmio_map(SYS_BUS_DEVICE(system_flash), 0, |
183 | 0x100000000ULL - total_size); | |
184 | ||
185 | if (i == 0) { | |
637a5acb | 186 | flash_mem = pflash_cfi01_get_memory(system_flash); |
32d3ee87 | 187 | pc_isa_bios_init(&x86ms->isa_bios, rom_memory, flash_mem); |
952e0668 | 188 | |
9617cddb JB |
189 | /* Encrypt the pflash boot ROM */ |
190 | if (sev_enabled()) { | |
191 | flash_ptr = memory_region_get_ram_ptr(flash_mem); | |
192 | flash_size = memory_region_size(flash_mem); | |
966f1ca5 | 193 | x86_firmware_configure(flash_ptr, flash_size); |
9617cddb | 194 | } |
637a5acb | 195 | } |
bd183c79 | 196 | } |
bd183c79 JJ |
197 | } |
198 | ||
5e640a9e PMD |
199 | void pc_system_firmware_init(PCMachineState *pcms, |
200 | MemoryRegion *rom_memory) | |
cbc5b5f3 | 201 | { |
5e640a9e | 202 | PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms); |
ebc29e1b | 203 | int i; |
ebc29e1b | 204 | BlockBackend *pflash_blk[ARRAY_SIZE(pcms->flash)]; |
1d38574f | 205 | |
ebc29e1b | 206 | if (!pcmc->pci_enabled) { |
84835184 | 207 | x86_bios_rom_init(X86_MACHINE(pcms), "bios.bin", rom_memory, true); |
dafb82e0 | 208 | return; |
bd183c79 JJ |
209 | } |
210 | ||
ebc29e1b MA |
211 | /* Map legacy -drive if=pflash to machine properties */ |
212 | for (i = 0; i < ARRAY_SIZE(pcms->flash); i++) { | |
2d731dbd MA |
213 | pflash_cfi01_legacy_drive(pcms->flash[i], |
214 | drive_get(IF_PFLASH, 0, i)); | |
c8d8ef00 | 215 | pflash_blk[i] = pflash_cfi01_get_blk(pcms->flash[i]); |
ebc29e1b MA |
216 | } |
217 | ||
218 | /* Reject gaps */ | |
219 | for (i = 1; i < ARRAY_SIZE(pcms->flash); i++) { | |
220 | if (pflash_blk[i] && !pflash_blk[i - 1]) { | |
221 | error_report("pflash%d requires pflash%d", i, i - 1); | |
222 | exit(1); | |
223 | } | |
224 | } | |
225 | ||
226 | if (!pflash_blk[0]) { | |
227 | /* Machine property pflash0 not set, use ROM mode */ | |
84835184 | 228 | x86_bios_rom_init(X86_MACHINE(pcms), "bios.bin", rom_memory, false); |
ebc29e1b MA |
229 | } else { |
230 | if (kvm_enabled() && !kvm_readonly_mem_enabled()) { | |
231 | /* | |
232 | * Older KVM cannot execute from device memory. So, flash | |
233 | * memory cannot be used unless the readonly memory kvm | |
234 | * capability is present. | |
235 | */ | |
236 | error_report("pflash with kvm requires KVM readonly memory support"); | |
237 | exit(1); | |
238 | } | |
239 | ||
240 | pc_system_flash_map(pcms, rom_memory); | |
bd183c79 | 241 | } |
a904410a | 242 | |
ebc29e1b | 243 | pc_system_flash_cleanup_unused(pcms); |
cbc5b5f3 | 244 | } |
966f1ca5 GH |
245 | |
246 | void x86_firmware_configure(void *ptr, int size) | |
247 | { | |
248 | int ret; | |
249 | ||
250 | /* | |
251 | * OVMF places a GUIDed structures in the flash, so | |
252 | * search for them | |
253 | */ | |
254 | pc_system_parse_ovmf_flash(ptr, size); | |
255 | ||
256 | if (sev_enabled()) { | |
257 | ret = sev_es_save_reset_vector(ptr, size); | |
258 | if (ret) { | |
259 | error_report("failed to locate and/or save reset vector"); | |
260 | exit(1); | |
261 | } | |
262 | ||
263 | sev_encrypt_flash(ptr, size, &error_fatal); | |
264 | } | |
265 | } |