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 "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"
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"
41 #define BIOS_FILENAME "bios.bin"
43 typedef struct PcSysFwDevice
{
48 static void pc_isa_bios_init(MemoryRegion
*rom_memory
,
49 MemoryRegion
*flash_mem
,
53 MemoryRegion
*isa_bios
;
55 void *flash_ptr
, *isa_bios_ptr
;
57 flash_size
= memory_region_size(flash_mem
);
59 /* map the last 128KB of the BIOS in ISA space */
60 isa_bios_size
= MIN(flash_size
, 128 * KiB
);
61 isa_bios
= g_malloc(sizeof(*isa_bios
));
62 memory_region_init_ram(isa_bios
, NULL
, "isa-bios", isa_bios_size
,
64 memory_region_add_subregion_overlap(rom_memory
,
65 0x100000 - isa_bios_size
,
69 /* copy ISA rom image from top of flash memory */
70 flash_ptr
= memory_region_get_ram_ptr(flash_mem
);
71 isa_bios_ptr
= memory_region_get_ram_ptr(isa_bios
);
73 ((uint8_t*)flash_ptr
) + (flash_size
- isa_bios_size
),
76 memory_region_set_readonly(isa_bios
, true);
79 #define FLASH_MAP_UNIT_MAX 2
81 /* We don't have a theoretically justifiable exact lower bound on the base
82 * address of any flash mapping. In practice, the IO-APIC MMIO range is
83 * [0xFEE00000..0xFEE01000[ -- see IO_APIC_DEFAULT_ADDRESS --, leaving free
84 * only 18MB-4KB below 4G. For now, restrict the cumulative mapping to 8MB in
87 #define FLASH_MAP_BASE_MIN ((hwaddr)(4 * GiB - 8 * MiB))
89 /* This function maps flash drives from 4G downward, in order of their unit
90 * numbers. The mapping starts at unit#0, with unit number increments of 1, and
91 * stops before the first missing flash drive, or before
92 * unit#FLASH_MAP_UNIT_MAX, whichever is reached first.
94 * Addressing within one flash drive is of course not reversed.
96 * An error message is printed and the process exits if:
97 * - the size of the backing file for a flash drive is non-positive, or not a
98 * multiple of the required sector size, or
99 * - the current mapping's base address would fall below FLASH_MAP_BASE_MIN.
101 * The drive with unit#0 (if available) is mapped at the highest address, and
102 * it is passed to pc_isa_bios_init(). Merging several drives for isa-bios is
105 static void pc_system_flash_init(MemoryRegion
*rom_memory
)
108 DriveInfo
*pflash_drv
;
111 char *fatal_errmsg
= NULL
;
112 hwaddr phys_addr
= 0x100000000ULL
;
113 int sector_bits
, sector_size
;
114 pflash_t
*system_flash
;
115 MemoryRegion
*flash_mem
;
121 sector_size
= 1 << sector_bits
;
124 (unit
< FLASH_MAP_UNIT_MAX
&&
125 (pflash_drv
= drive_get(IF_PFLASH
, 0, unit
)) != NULL
);
127 blk
= blk_by_legacy_dinfo(pflash_drv
);
128 size
= blk_getlength(blk
);
130 fatal_errmsg
= g_strdup_printf("failed to get backing file size");
131 } else if (size
== 0) {
132 fatal_errmsg
= g_strdup_printf("PC system firmware (pflash) "
133 "cannot have zero size");
134 } else if ((size
% sector_size
) != 0) {
135 fatal_errmsg
= g_strdup_printf("PC system firmware (pflash) "
136 "must be a multiple of 0x%x", sector_size
);
137 } else if (phys_addr
< size
|| phys_addr
- size
< FLASH_MAP_BASE_MIN
) {
138 fatal_errmsg
= g_strdup_printf("oversized backing file, pflash "
139 "segments cannot be mapped under "
140 TARGET_FMT_plx
, FLASH_MAP_BASE_MIN
);
142 if (fatal_errmsg
!= NULL
) {
145 /* push a new, "none" location on the location stack; overwrite its
146 * contents with the location saved in the option; print the error
147 * (includes location); pop the top
150 if (pflash_drv
->opts
!= NULL
) {
151 qemu_opts_loc_restore(pflash_drv
->opts
);
153 error_report("%s", fatal_errmsg
);
155 g_free(fatal_errmsg
);
161 /* pflash_cfi01_register() creates a deep copy of the name */
162 snprintf(name
, sizeof name
, "system.flash%d", unit
);
163 system_flash
= pflash_cfi01_register(phys_addr
, NULL
/* qdev */, name
,
164 size
, blk
, sector_size
,
173 flash_mem
= pflash_cfi01_get_memory(system_flash
);
174 pc_isa_bios_init(rom_memory
, flash_mem
, size
);
176 /* Encrypt the pflash boot ROM */
177 if (kvm_memcrypt_enabled()) {
178 flash_ptr
= memory_region_get_ram_ptr(flash_mem
);
179 flash_size
= memory_region_size(flash_mem
);
180 ret
= kvm_memcrypt_encrypt_data(flash_ptr
, flash_size
);
182 error_report("failed to encrypt pflash rom");
190 static void old_pc_system_rom_init(MemoryRegion
*rom_memory
, bool isapc_ram_fw
)
193 MemoryRegion
*bios
, *isa_bios
;
194 int bios_size
, isa_bios_size
;
198 if (bios_name
== NULL
) {
199 bios_name
= BIOS_FILENAME
;
201 filename
= qemu_find_file(QEMU_FILE_TYPE_BIOS
, bios_name
);
203 bios_size
= get_image_size(filename
);
207 if (bios_size
<= 0 ||
208 (bios_size
% 65536) != 0) {
211 bios
= g_malloc(sizeof(*bios
));
212 memory_region_init_ram(bios
, NULL
, "pc.bios", bios_size
, &error_fatal
);
214 memory_region_set_readonly(bios
, true);
216 ret
= rom_add_file_fixed(bios_name
, (uint32_t)(-bios_size
), -1);
219 fprintf(stderr
, "qemu: could not load PC BIOS '%s'\n", bios_name
);
224 /* map the last 128KB of the BIOS in ISA space */
225 isa_bios_size
= MIN(bios_size
, 128 * KiB
);
226 isa_bios
= g_malloc(sizeof(*isa_bios
));
227 memory_region_init_alias(isa_bios
, NULL
, "isa-bios", bios
,
228 bios_size
- isa_bios_size
, isa_bios_size
);
229 memory_region_add_subregion_overlap(rom_memory
,
230 0x100000 - isa_bios_size
,
234 memory_region_set_readonly(isa_bios
, true);
237 /* map all the bios at the top of memory */
238 memory_region_add_subregion(rom_memory
,
239 (uint32_t)(-bios_size
),
243 void pc_system_firmware_init(MemoryRegion
*rom_memory
, bool isapc_ram_fw
)
245 DriveInfo
*pflash_drv
;
247 pflash_drv
= drive_get(IF_PFLASH
, 0, 0);
249 if (isapc_ram_fw
|| pflash_drv
== NULL
) {
250 /* When a pflash drive is not found, use rom-mode */
251 old_pc_system_rom_init(rom_memory
, isapc_ram_fw
);
255 if (kvm_enabled() && !kvm_readonly_mem_enabled()) {
256 /* Older KVM cannot execute from device memory. So, flash memory
257 * cannot be used unless the readonly memory kvm capability is present. */
258 fprintf(stderr
, "qemu: pflash with kvm requires KVM readonly memory support\n");
262 pc_system_flash_init(rom_memory
);