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 | |
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/pc.h" |
35 | #include "hw/loader.h" |
36 | #include "hw/qdev-properties.h" |
37 | #include "sysemu/sysemu.h" |
38 | #include "hw/block/flash.h" |
39 | #include "sysemu/kvm.h" |
40 | |
41 | #define BIOS_FILENAME "bios.bin" |
42 | |
43 | /* |
44 | * We don't have a theoretically justifiable exact lower bound on the base |
45 | * address of any flash mapping. In practice, the IO-APIC MMIO range is |
46 | * [0xFEE00000..0xFEE01000] -- see IO_APIC_DEFAULT_ADDRESS --, leaving free |
47 | * only 18MB-4KB below 4G. For now, restrict the cumulative mapping to 8MB in |
48 | * size. |
49 | */ |
50 | #define FLASH_SIZE_LIMIT (8 * MiB) |
51 | |
52 | #define FLASH_SECTOR_SIZE 4096 |
53 | |
54 | static void pc_isa_bios_init(MemoryRegion *rom_memory, |
55 | MemoryRegion *flash_mem, |
56 | int ram_size) |
57 | { |
58 | int isa_bios_size; |
59 | MemoryRegion *isa_bios; |
60 | uint64_t flash_size; |
61 | void *flash_ptr, *isa_bios_ptr; |
62 | |
63 | flash_size = memory_region_size(flash_mem); |
64 | |
65 | /* map the last 128KB of the BIOS in ISA space */ |
66 | isa_bios_size = MIN(flash_size, 128 * KiB); |
67 | isa_bios = g_malloc(sizeof(*isa_bios)); |
68 | memory_region_init_ram(isa_bios, NULL, "isa-bios" , isa_bios_size, |
69 | &error_fatal); |
70 | memory_region_add_subregion_overlap(rom_memory, |
71 | 0x100000 - isa_bios_size, |
72 | isa_bios, |
73 | 1); |
74 | |
75 | /* copy ISA rom image from top of flash memory */ |
76 | flash_ptr = memory_region_get_ram_ptr(flash_mem); |
77 | isa_bios_ptr = memory_region_get_ram_ptr(isa_bios); |
78 | memcpy(isa_bios_ptr, |
79 | ((uint8_t*)flash_ptr) + (flash_size - isa_bios_size), |
80 | isa_bios_size); |
81 | |
82 | memory_region_set_readonly(isa_bios, true); |
83 | } |
84 | |
85 | static PFlashCFI01 *pc_pflash_create(PCMachineState *pcms, |
86 | const char *name, |
87 | const char *alias_prop_name) |
88 | { |
89 | DeviceState *dev = qdev_create(NULL, TYPE_PFLASH_CFI01); |
90 | |
91 | qdev_prop_set_uint64(dev, "sector-length" , FLASH_SECTOR_SIZE); |
92 | qdev_prop_set_uint8(dev, "width" , 1); |
93 | qdev_prop_set_string(dev, "name" , name); |
94 | object_property_add_child(OBJECT(pcms), name, OBJECT(dev), |
95 | &error_abort); |
96 | object_property_add_alias(OBJECT(pcms), alias_prop_name, |
97 | OBJECT(dev), "drive" , &error_abort); |
98 | return PFLASH_CFI01(dev); |
99 | } |
100 | |
101 | void pc_system_flash_create(PCMachineState *pcms) |
102 | { |
103 | PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms); |
104 | |
105 | if (pcmc->pci_enabled) { |
106 | pcms->flash[0] = pc_pflash_create(pcms, "system.flash0" , |
107 | "pflash0" ); |
108 | pcms->flash[1] = pc_pflash_create(pcms, "system.flash1" , |
109 | "pflash1" ); |
110 | } |
111 | } |
112 | |
113 | static void pc_system_flash_cleanup_unused(PCMachineState *pcms) |
114 | { |
115 | char *prop_name; |
116 | int i; |
117 | Object *dev_obj; |
118 | |
119 | assert(PC_MACHINE_GET_CLASS(pcms)->pci_enabled); |
120 | |
121 | for (i = 0; i < ARRAY_SIZE(pcms->flash); i++) { |
122 | dev_obj = OBJECT(pcms->flash[i]); |
123 | if (!object_property_get_bool(dev_obj, "realized" , &error_abort)) { |
124 | prop_name = g_strdup_printf("pflash%d" , i); |
125 | object_property_del(OBJECT(pcms), prop_name, &error_abort); |
126 | g_free(prop_name); |
127 | object_unparent(dev_obj); |
128 | pcms->flash[i] = NULL; |
129 | } |
130 | } |
131 | } |
132 | |
133 | /* |
134 | * Map the pcms->flash[] from 4GiB downward, and realize. |
135 | * Map them in descending order, i.e. pcms->flash[0] at the top, |
136 | * without gaps. |
137 | * Stop at the first pcms->flash[0] lacking a block backend. |
138 | * Set each flash's size from its block backend. Fatal error if the |
139 | * size isn't a non-zero multiple of 4KiB, or the total size exceeds |
140 | * FLASH_SIZE_LIMIT. |
141 | * |
142 | * If pcms->flash[0] has a block backend, its memory is passed to |
143 | * pc_isa_bios_init(). Merging several flash devices for isa-bios is |
144 | * not supported. |
145 | */ |
146 | static void pc_system_flash_map(PCMachineState *pcms, |
147 | MemoryRegion *rom_memory) |
148 | { |
149 | hwaddr total_size = 0; |
150 | int i; |
151 | BlockBackend *blk; |
152 | int64_t size; |
153 | PFlashCFI01 *system_flash; |
154 | MemoryRegion *flash_mem; |
155 | void *flash_ptr; |
156 | int ret, flash_size; |
157 | |
158 | assert(PC_MACHINE_GET_CLASS(pcms)->pci_enabled); |
159 | |
160 | for (i = 0; i < ARRAY_SIZE(pcms->flash); i++) { |
161 | system_flash = pcms->flash[i]; |
162 | blk = pflash_cfi01_get_blk(system_flash); |
163 | if (!blk) { |
164 | break; |
165 | } |
166 | size = blk_getlength(blk); |
167 | if (size < 0) { |
168 | error_report("can't get size of block device %s: %s" , |
169 | blk_name(blk), strerror(-size)); |
170 | exit(1); |
171 | } |
172 | if (size == 0 || size % FLASH_SECTOR_SIZE != 0) { |
173 | error_report("system firmware block device %s has invalid size " |
174 | "%" PRId64, |
175 | blk_name(blk), size); |
176 | info_report("its size must be a non-zero multiple of 0x%x" , |
177 | FLASH_SECTOR_SIZE); |
178 | exit(1); |
179 | } |
180 | if ((hwaddr)size != size |
181 | || total_size > HWADDR_MAX - size |
182 | || total_size + size > FLASH_SIZE_LIMIT) { |
183 | error_report("combined size of system firmware exceeds " |
184 | "%" PRIu64 " bytes" , |
185 | FLASH_SIZE_LIMIT); |
186 | exit(1); |
187 | } |
188 | |
189 | total_size += size; |
190 | qdev_prop_set_uint32(DEVICE(system_flash), "num-blocks" , |
191 | size / FLASH_SECTOR_SIZE); |
192 | qdev_init_nofail(DEVICE(system_flash)); |
193 | sysbus_mmio_map(SYS_BUS_DEVICE(system_flash), 0, |
194 | 0x100000000ULL - total_size); |
195 | |
196 | if (i == 0) { |
197 | flash_mem = pflash_cfi01_get_memory(system_flash); |
198 | pc_isa_bios_init(rom_memory, flash_mem, size); |
199 | |
200 | /* Encrypt the pflash boot ROM */ |
201 | if (kvm_memcrypt_enabled()) { |
202 | flash_ptr = memory_region_get_ram_ptr(flash_mem); |
203 | flash_size = memory_region_size(flash_mem); |
204 | ret = kvm_memcrypt_encrypt_data(flash_ptr, flash_size); |
205 | if (ret) { |
206 | error_report("failed to encrypt pflash rom" ); |
207 | exit(1); |
208 | } |
209 | } |
210 | } |
211 | } |
212 | } |
213 | |
214 | static void old_pc_system_rom_init(MemoryRegion *rom_memory, bool isapc_ram_fw) |
215 | { |
216 | char *filename; |
217 | MemoryRegion *bios, *isa_bios; |
218 | int bios_size, isa_bios_size; |
219 | int ret; |
220 | |
221 | /* BIOS load */ |
222 | if (bios_name == NULL) { |
223 | bios_name = BIOS_FILENAME; |
224 | } |
225 | filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name); |
226 | if (filename) { |
227 | bios_size = get_image_size(filename); |
228 | } else { |
229 | bios_size = -1; |
230 | } |
231 | if (bios_size <= 0 || |
232 | (bios_size % 65536) != 0) { |
233 | goto bios_error; |
234 | } |
235 | bios = g_malloc(sizeof(*bios)); |
236 | memory_region_init_ram(bios, NULL, "pc.bios" , bios_size, &error_fatal); |
237 | if (!isapc_ram_fw) { |
238 | memory_region_set_readonly(bios, true); |
239 | } |
240 | ret = rom_add_file_fixed(bios_name, (uint32_t)(-bios_size), -1); |
241 | if (ret != 0) { |
242 | bios_error: |
243 | fprintf(stderr, "qemu: could not load PC BIOS '%s'\n" , bios_name); |
244 | exit(1); |
245 | } |
246 | g_free(filename); |
247 | |
248 | /* map the last 128KB of the BIOS in ISA space */ |
249 | isa_bios_size = MIN(bios_size, 128 * KiB); |
250 | isa_bios = g_malloc(sizeof(*isa_bios)); |
251 | memory_region_init_alias(isa_bios, NULL, "isa-bios" , bios, |
252 | bios_size - isa_bios_size, isa_bios_size); |
253 | memory_region_add_subregion_overlap(rom_memory, |
254 | 0x100000 - isa_bios_size, |
255 | isa_bios, |
256 | 1); |
257 | if (!isapc_ram_fw) { |
258 | memory_region_set_readonly(isa_bios, true); |
259 | } |
260 | |
261 | /* map all the bios at the top of memory */ |
262 | memory_region_add_subregion(rom_memory, |
263 | (uint32_t)(-bios_size), |
264 | bios); |
265 | } |
266 | |
267 | void pc_system_firmware_init(PCMachineState *pcms, |
268 | MemoryRegion *rom_memory) |
269 | { |
270 | PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms); |
271 | int i; |
272 | BlockBackend *pflash_blk[ARRAY_SIZE(pcms->flash)]; |
273 | |
274 | if (!pcmc->pci_enabled) { |
275 | old_pc_system_rom_init(rom_memory, true); |
276 | return; |
277 | } |
278 | |
279 | /* Map legacy -drive if=pflash to machine properties */ |
280 | for (i = 0; i < ARRAY_SIZE(pcms->flash); i++) { |
281 | pflash_cfi01_legacy_drive(pcms->flash[i], |
282 | drive_get(IF_PFLASH, 0, i)); |
283 | pflash_blk[i] = pflash_cfi01_get_blk(pcms->flash[i]); |
284 | } |
285 | |
286 | /* Reject gaps */ |
287 | for (i = 1; i < ARRAY_SIZE(pcms->flash); i++) { |
288 | if (pflash_blk[i] && !pflash_blk[i - 1]) { |
289 | error_report("pflash%d requires pflash%d" , i, i - 1); |
290 | exit(1); |
291 | } |
292 | } |
293 | |
294 | if (!pflash_blk[0]) { |
295 | /* Machine property pflash0 not set, use ROM mode */ |
296 | old_pc_system_rom_init(rom_memory, false); |
297 | } else { |
298 | if (kvm_enabled() && !kvm_readonly_mem_enabled()) { |
299 | /* |
300 | * Older KVM cannot execute from device memory. So, flash |
301 | * memory cannot be used unless the readonly memory kvm |
302 | * capability is present. |
303 | */ |
304 | error_report("pflash with kvm requires KVM readonly memory support" ); |
305 | exit(1); |
306 | } |
307 | |
308 | pc_system_flash_map(pcms, rom_memory); |
309 | } |
310 | |
311 | pc_system_flash_cleanup_unused(pcms); |
312 | } |
313 | |