blob: 75913c5b2fe8eb555bea6275a28e0e910ff49e3d [file] [log] [blame]
Jordan Justencbc5b5f2012-02-21 23:18:51 -08001/*
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
Markus Armbrusterfa1d36d2014-10-07 13:59:13 +020026#include "sysemu/block-backend.h"
Paolo Bonzinib4a42f82013-02-04 11:37:52 +010027#include "qemu/error-report.h"
Paolo Bonzini83c9f4c2013-02-04 15:40:22 +010028#include "hw/sysbus.h"
29#include "hw/hw.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010030#include "hw/i386/pc.h"
Jordan Justenbd183c72012-02-21 23:18:53 -080031#include "hw/boards.h"
Paolo Bonzini83c9f4c2013-02-04 15:40:22 +010032#include "hw/loader.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010033#include "sysemu/sysemu.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010034#include "hw/block/flash.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010035#include "sysemu/kvm.h"
Jordan Justencbc5b5f2012-02-21 23:18:51 -080036
37#define BIOS_FILENAME "bios.bin"
38
Jordan Justen90ccf9f2012-02-21 23:18:52 -080039typedef struct PcSysFwDevice {
40 SysBusDevice busdev;
Jordan Justendade9222013-05-29 01:27:24 -070041 uint8_t isapc_ram_fw;
Jordan Justen90ccf9f2012-02-21 23:18:52 -080042} PcSysFwDevice;
43
Jordan Justenbd183c72012-02-21 23:18:53 -080044static void pc_isa_bios_init(MemoryRegion *rom_memory,
45 MemoryRegion *flash_mem,
46 int ram_size)
47{
48 int isa_bios_size;
49 MemoryRegion *isa_bios;
50 uint64_t flash_size;
51 void *flash_ptr, *isa_bios_ptr;
52
53 flash_size = memory_region_size(flash_mem);
54
55 /* map the last 128KB of the BIOS in ISA space */
Markus Armbruster7f87af32013-07-31 15:11:12 +020056 isa_bios_size = MIN(flash_size, 128 * 1024);
Jordan Justenbd183c72012-02-21 23:18:53 -080057 isa_bios = g_malloc(sizeof(*isa_bios));
Hu Tao49946532014-09-09 13:27:55 +080058 memory_region_init_ram(isa_bios, NULL, "isa-bios", isa_bios_size,
59 &error_abort);
Jordan Justenbd183c72012-02-21 23:18:53 -080060 vmstate_register_ram_global(isa_bios);
61 memory_region_add_subregion_overlap(rom_memory,
62 0x100000 - isa_bios_size,
63 isa_bios,
64 1);
65
66 /* copy ISA rom image from top of flash memory */
67 flash_ptr = memory_region_get_ram_ptr(flash_mem);
68 isa_bios_ptr = memory_region_get_ram_ptr(isa_bios);
69 memcpy(isa_bios_ptr,
70 ((uint8_t*)flash_ptr) + (flash_size - isa_bios_size),
71 isa_bios_size);
72
73 memory_region_set_readonly(isa_bios, true);
74}
75
Laszlo Ersek637a5ac2013-11-28 00:52:52 +010076#define FLASH_MAP_UNIT_MAX 2
77
78/* We don't have a theoretically justifiable exact lower bound on the base
79 * address of any flash mapping. In practice, the IO-APIC MMIO range is
80 * [0xFEE00000..0xFEE01000[ -- see IO_APIC_DEFAULT_ADDRESS --, leaving free
81 * only 18MB-4KB below 4G. For now, restrict the cumulative mapping to 8MB in
82 * size.
83 */
84#define FLASH_MAP_BASE_MIN ((hwaddr)(0x100000000ULL - 8*1024*1024))
85
86/* This function maps flash drives from 4G downward, in order of their unit
87 * numbers. The mapping starts at unit#0, with unit number increments of 1, and
88 * stops before the first missing flash drive, or before
89 * unit#FLASH_MAP_UNIT_MAX, whichever is reached first.
90 *
91 * Addressing within one flash drive is of course not reversed.
92 *
93 * An error message is printed and the process exits if:
94 * - the size of the backing file for a flash drive is non-positive, or not a
95 * multiple of the required sector size, or
96 * - the current mapping's base address would fall below FLASH_MAP_BASE_MIN.
97 *
98 * The drive with unit#0 (if available) is mapped at the highest address, and
99 * it is passed to pc_isa_bios_init(). Merging several drives for isa-bios is
100 * not supported.
101 */
102static void pc_system_flash_init(MemoryRegion *rom_memory)
Jordan Justenbd183c72012-02-21 23:18:53 -0800103{
Laszlo Ersek637a5ac2013-11-28 00:52:52 +0100104 int unit;
105 DriveInfo *pflash_drv;
Markus Armbruster4be74632014-10-07 13:59:18 +0200106 BlockBackend *blk;
Jordan Justenbd183c72012-02-21 23:18:53 -0800107 int64_t size;
Laszlo Ersek637a5ac2013-11-28 00:52:52 +0100108 char *fatal_errmsg = NULL;
109 hwaddr phys_addr = 0x100000000ULL;
Jordan Justenbd183c72012-02-21 23:18:53 -0800110 int sector_bits, sector_size;
111 pflash_t *system_flash;
112 MemoryRegion *flash_mem;
Laszlo Ersek637a5ac2013-11-28 00:52:52 +0100113 char name[64];
Jordan Justenbd183c72012-02-21 23:18:53 -0800114
Jordan Justenbd183c72012-02-21 23:18:53 -0800115 sector_bits = 12;
116 sector_size = 1 << sector_bits;
117
Laszlo Ersek637a5ac2013-11-28 00:52:52 +0100118 for (unit = 0;
119 (unit < FLASH_MAP_UNIT_MAX &&
120 (pflash_drv = drive_get(IF_PFLASH, 0, unit)) != NULL);
121 ++unit) {
Markus Armbruster4be74632014-10-07 13:59:18 +0200122 blk = blk_by_legacy_dinfo(pflash_drv);
123 size = blk_getlength(blk);
Laszlo Ersek637a5ac2013-11-28 00:52:52 +0100124 if (size < 0) {
125 fatal_errmsg = g_strdup_printf("failed to get backing file size");
126 } else if (size == 0) {
127 fatal_errmsg = g_strdup_printf("PC system firmware (pflash) "
128 "cannot have zero size");
129 } else if ((size % sector_size) != 0) {
130 fatal_errmsg = g_strdup_printf("PC system firmware (pflash) "
131 "must be a multiple of 0x%x", sector_size);
132 } else if (phys_addr < size || phys_addr - size < FLASH_MAP_BASE_MIN) {
133 fatal_errmsg = g_strdup_printf("oversized backing file, pflash "
134 "segments cannot be mapped under "
135 TARGET_FMT_plx, FLASH_MAP_BASE_MIN);
136 }
137 if (fatal_errmsg != NULL) {
138 Location loc;
139
140 /* push a new, "none" location on the location stack; overwrite its
141 * contents with the location saved in the option; print the error
142 * (includes location); pop the top
143 */
144 loc_push_none(&loc);
145 if (pflash_drv->opts != NULL) {
146 qemu_opts_loc_restore(pflash_drv->opts);
147 }
148 error_report("%s", fatal_errmsg);
149 loc_pop(&loc);
150 g_free(fatal_errmsg);
151 exit(1);
152 }
153
154 phys_addr -= size;
155
156 /* pflash_cfi01_register() creates a deep copy of the name */
157 snprintf(name, sizeof name, "system.flash%d", unit);
158 system_flash = pflash_cfi01_register(phys_addr, NULL /* qdev */, name,
Markus Armbruster4be74632014-10-07 13:59:18 +0200159 size, blk, sector_size,
Laszlo Ersek637a5ac2013-11-28 00:52:52 +0100160 size >> sector_bits,
161 1 /* width */,
162 0x0000 /* id0 */,
163 0x0000 /* id1 */,
164 0x0000 /* id2 */,
165 0x0000 /* id3 */,
166 0 /* be */);
167 if (unit == 0) {
168 flash_mem = pflash_cfi01_get_memory(system_flash);
169 pc_isa_bios_init(rom_memory, flash_mem, size);
170 }
Jordan Justenbd183c72012-02-21 23:18:53 -0800171 }
Jordan Justenbd183c72012-02-21 23:18:53 -0800172}
173
Jordan Justendade9222013-05-29 01:27:24 -0700174static void old_pc_system_rom_init(MemoryRegion *rom_memory, bool isapc_ram_fw)
Jordan Justencbc5b5f2012-02-21 23:18:51 -0800175{
176 char *filename;
177 MemoryRegion *bios, *isa_bios;
178 int bios_size, isa_bios_size;
179 int ret;
180
181 /* BIOS load */
182 if (bios_name == NULL) {
183 bios_name = BIOS_FILENAME;
184 }
185 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
186 if (filename) {
187 bios_size = get_image_size(filename);
188 } else {
189 bios_size = -1;
190 }
191 if (bios_size <= 0 ||
192 (bios_size % 65536) != 0) {
193 goto bios_error;
194 }
195 bios = g_malloc(sizeof(*bios));
Hu Tao49946532014-09-09 13:27:55 +0800196 memory_region_init_ram(bios, NULL, "pc.bios", bios_size, &error_abort);
Jordan Justencbc5b5f2012-02-21 23:18:51 -0800197 vmstate_register_ram_global(bios);
Jordan Justendade9222013-05-29 01:27:24 -0700198 if (!isapc_ram_fw) {
199 memory_region_set_readonly(bios, true);
200 }
Jordan Justencbc5b5f2012-02-21 23:18:51 -0800201 ret = rom_add_file_fixed(bios_name, (uint32_t)(-bios_size), -1);
202 if (ret != 0) {
203 bios_error:
204 fprintf(stderr, "qemu: could not load PC BIOS '%s'\n", bios_name);
205 exit(1);
206 }
207 if (filename) {
208 g_free(filename);
209 }
210
211 /* map the last 128KB of the BIOS in ISA space */
212 isa_bios_size = bios_size;
213 if (isa_bios_size > (128 * 1024)) {
214 isa_bios_size = 128 * 1024;
215 }
216 isa_bios = g_malloc(sizeof(*isa_bios));
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400217 memory_region_init_alias(isa_bios, NULL, "isa-bios", bios,
Jordan Justencbc5b5f2012-02-21 23:18:51 -0800218 bios_size - isa_bios_size, isa_bios_size);
219 memory_region_add_subregion_overlap(rom_memory,
220 0x100000 - isa_bios_size,
221 isa_bios,
222 1);
Jordan Justendade9222013-05-29 01:27:24 -0700223 if (!isapc_ram_fw) {
224 memory_region_set_readonly(isa_bios, true);
225 }
Jordan Justencbc5b5f2012-02-21 23:18:51 -0800226
227 /* map all the bios at the top of memory */
228 memory_region_add_subregion(rom_memory,
229 (uint32_t)(-bios_size),
230 bios);
231}
232
Paolo Bonzini6dd2a5c2013-08-09 12:35:02 -0500233void pc_system_firmware_init(MemoryRegion *rom_memory, bool isapc_ram_fw)
Jordan Justencbc5b5f2012-02-21 23:18:51 -0800234{
Jordan Justenbd183c72012-02-21 23:18:53 -0800235 DriveInfo *pflash_drv;
Anthony Liguori1d385742012-04-18 17:33:15 -0500236
Jordan Justendafb82e2013-05-29 01:27:27 -0700237 pflash_drv = drive_get(IF_PFLASH, 0, 0);
238
Paolo Bonzini6dd2a5c2013-08-09 12:35:02 -0500239 if (isapc_ram_fw || pflash_drv == NULL) {
Jordan Justendafb82e2013-05-29 01:27:27 -0700240 /* When a pflash drive is not found, use rom-mode */
Paolo Bonzini6dd2a5c2013-08-09 12:35:02 -0500241 old_pc_system_rom_init(rom_memory, isapc_ram_fw);
Paolo Bonzinia9044102013-08-09 12:35:01 -0500242 return;
243 }
244
245 if (kvm_enabled() && !kvm_readonly_mem_enabled()) {
Jordan Justendafb82e2013-05-29 01:27:27 -0700246 /* Older KVM cannot execute from device memory. So, flash memory
247 * cannot be used unless the readonly memory kvm capability is present. */
248 fprintf(stderr, "qemu: pflash with kvm requires KVM readonly memory support\n");
249 exit(1);
250 }
251
Laszlo Ersek637a5ac2013-11-28 00:52:52 +0100252 pc_system_flash_init(rom_memory);
Jordan Justencbc5b5f2012-02-21 23:18:51 -0800253}