pc_sysfw: Pass PCMachineState to pc_system_firmware_init()
[qemu.git] / hw / i386 / pc_sysfw.c
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 "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/hw.h"
34 #include "hw/i386/pc.h"
35 #include "hw/boards.h"
36 #include "hw/loader.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 static 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 /* map the last 128KB of the BIOS in ISA space */
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 /* copy ISA rom image from top of flash memory */
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
74 #define FLASH_MAP_UNIT_MAX 2
75
76 /* We don't have a theoretically justifiable exact lower bound on the base
77 * address of any flash mapping. In practice, the IO-APIC MMIO range is
78 * [0xFEE00000..0xFEE01000[ -- see IO_APIC_DEFAULT_ADDRESS --, leaving free
79 * only 18MB-4KB below 4G. For now, restrict the cumulative mapping to 8MB in
80 * size.
81 */
82 #define FLASH_MAP_BASE_MIN ((hwaddr)(4 * GiB - 8 * MiB))
83
84 /* This function maps flash drives from 4G downward, in order of their unit
85 * numbers. The mapping starts at unit#0, with unit number increments of 1, and
86 * stops before the first missing flash drive, or before
87 * unit#FLASH_MAP_UNIT_MAX, whichever is reached first.
88 *
89 * Addressing within one flash drive is of course not reversed.
90 *
91 * An error message is printed and the process exits if:
92 * - the size of the backing file for a flash drive is non-positive, or not a
93 * multiple of the required sector size, or
94 * - the current mapping's base address would fall below FLASH_MAP_BASE_MIN.
95 *
96 * The drive with unit#0 (if available) is mapped at the highest address, and
97 * it is passed to pc_isa_bios_init(). Merging several drives for isa-bios is
98 * not supported.
99 */
100 static void pc_system_flash_init(MemoryRegion *rom_memory)
101 {
102 int unit;
103 DriveInfo *pflash_drv;
104 BlockBackend *blk;
105 int64_t size;
106 char *fatal_errmsg = NULL;
107 hwaddr phys_addr = 0x100000000ULL;
108 uint32_t sector_size = 4096;
109 PFlashCFI01 *system_flash;
110 MemoryRegion *flash_mem;
111 char name[64];
112 void *flash_ptr;
113 int ret, flash_size;
114
115 for (unit = 0;
116 (unit < FLASH_MAP_UNIT_MAX &&
117 (pflash_drv = drive_get(IF_PFLASH, 0, unit)) != NULL);
118 ++unit) {
119 blk = blk_by_legacy_dinfo(pflash_drv);
120 size = blk_getlength(blk);
121 if (size < 0) {
122 fatal_errmsg = g_strdup_printf("failed to get backing file size");
123 } else if (size == 0) {
124 fatal_errmsg = g_strdup_printf("PC system firmware (pflash) "
125 "cannot have zero size");
126 } else if ((size % sector_size) != 0) {
127 fatal_errmsg = g_strdup_printf("PC system firmware (pflash) "
128 "must be a multiple of 0x%x", sector_size);
129 } else if (phys_addr < size || phys_addr - size < FLASH_MAP_BASE_MIN) {
130 fatal_errmsg = g_strdup_printf("oversized backing file, pflash "
131 "segments cannot be mapped under "
132 TARGET_FMT_plx, FLASH_MAP_BASE_MIN);
133 }
134 if (fatal_errmsg != NULL) {
135 Location loc;
136
137 /* push a new, "none" location on the location stack; overwrite its
138 * contents with the location saved in the option; print the error
139 * (includes location); pop the top
140 */
141 loc_push_none(&loc);
142 if (pflash_drv->opts != NULL) {
143 qemu_opts_loc_restore(pflash_drv->opts);
144 }
145 error_report("%s", fatal_errmsg);
146 loc_pop(&loc);
147 g_free(fatal_errmsg);
148 exit(1);
149 }
150
151 phys_addr -= size;
152
153 /* pflash_cfi01_register() creates a deep copy of the name */
154 snprintf(name, sizeof name, "system.flash%d", unit);
155 system_flash = pflash_cfi01_register(phys_addr, name,
156 size, blk, sector_size,
157 1 /* width */,
158 0x0000 /* id0 */,
159 0x0000 /* id1 */,
160 0x0000 /* id2 */,
161 0x0000 /* id3 */,
162 0 /* be */);
163 if (unit == 0) {
164 flash_mem = pflash_cfi01_get_memory(system_flash);
165 pc_isa_bios_init(rom_memory, flash_mem, size);
166
167 /* Encrypt the pflash boot ROM */
168 if (kvm_memcrypt_enabled()) {
169 flash_ptr = memory_region_get_ram_ptr(flash_mem);
170 flash_size = memory_region_size(flash_mem);
171 ret = kvm_memcrypt_encrypt_data(flash_ptr, flash_size);
172 if (ret) {
173 error_report("failed to encrypt pflash rom");
174 exit(1);
175 }
176 }
177 }
178 }
179 }
180
181 static void old_pc_system_rom_init(MemoryRegion *rom_memory, bool isapc_ram_fw)
182 {
183 char *filename;
184 MemoryRegion *bios, *isa_bios;
185 int bios_size, isa_bios_size;
186 int ret;
187
188 /* BIOS load */
189 if (bios_name == NULL) {
190 bios_name = BIOS_FILENAME;
191 }
192 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
193 if (filename) {
194 bios_size = get_image_size(filename);
195 } else {
196 bios_size = -1;
197 }
198 if (bios_size <= 0 ||
199 (bios_size % 65536) != 0) {
200 goto bios_error;
201 }
202 bios = g_malloc(sizeof(*bios));
203 memory_region_init_ram(bios, NULL, "pc.bios", bios_size, &error_fatal);
204 if (!isapc_ram_fw) {
205 memory_region_set_readonly(bios, true);
206 }
207 ret = rom_add_file_fixed(bios_name, (uint32_t)(-bios_size), -1);
208 if (ret != 0) {
209 bios_error:
210 fprintf(stderr, "qemu: could not load PC BIOS '%s'\n", bios_name);
211 exit(1);
212 }
213 g_free(filename);
214
215 /* map the last 128KB of the BIOS in ISA space */
216 isa_bios_size = MIN(bios_size, 128 * KiB);
217 isa_bios = g_malloc(sizeof(*isa_bios));
218 memory_region_init_alias(isa_bios, NULL, "isa-bios", bios,
219 bios_size - isa_bios_size, isa_bios_size);
220 memory_region_add_subregion_overlap(rom_memory,
221 0x100000 - isa_bios_size,
222 isa_bios,
223 1);
224 if (!isapc_ram_fw) {
225 memory_region_set_readonly(isa_bios, true);
226 }
227
228 /* map all the bios at the top of memory */
229 memory_region_add_subregion(rom_memory,
230 (uint32_t)(-bios_size),
231 bios);
232 }
233
234 void pc_system_firmware_init(PCMachineState *pcms,
235 MemoryRegion *rom_memory)
236 {
237 PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
238 bool isapc_ram_fw = !pcmc->pci_enabled;
239 DriveInfo *pflash_drv;
240
241 pflash_drv = drive_get(IF_PFLASH, 0, 0);
242
243 if (isapc_ram_fw || pflash_drv == NULL) {
244 /* When a pflash drive is not found, use rom-mode */
245 old_pc_system_rom_init(rom_memory, isapc_ram_fw);
246 return;
247 }
248
249 if (kvm_enabled() && !kvm_readonly_mem_enabled()) {
250 /* Older KVM cannot execute from device memory. So, flash memory
251 * cannot be used unless the readonly memory kvm capability is present. */
252 fprintf(stderr, "qemu: pflash with kvm requires KVM readonly memory support\n");
253 exit(1);
254 }
255
256 pc_system_flash_init(rom_memory);
257 }