]> git.proxmox.com Git - qemu.git/blame - hw/i386/pc_sysfw.c
Merge remote-tracking branch 'agraf/tags/signed-ppc-for-upstream-1.7' into staging
[qemu.git] / hw / i386 / pc_sysfw.c
CommitLineData
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
9c17d615 26#include "sysemu/blockdev.h"
b4a42f81 27#include "qemu/error-report.h"
83c9f4ca
PB
28#include "hw/sysbus.h"
29#include "hw/hw.h"
0d09e41a 30#include "hw/i386/pc.h"
bd183c79 31#include "hw/boards.h"
83c9f4ca 32#include "hw/loader.h"
9c17d615 33#include "sysemu/sysemu.h"
0d09e41a 34#include "hw/block/flash.h"
9c17d615 35#include "sysemu/kvm.h"
cbc5b5f3
JJ
36
37#define BIOS_FILENAME "bios.bin"
38
90ccf9f6
JJ
39typedef struct PcSysFwDevice {
40 SysBusDevice busdev;
dade922f 41 uint8_t isapc_ram_fw;
90ccf9f6
JJ
42} PcSysFwDevice;
43
bd183c79
JJ
44static 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 */
7f87af39 56 isa_bios_size = MIN(flash_size, 128 * 1024);
bd183c79 57 isa_bios = g_malloc(sizeof(*isa_bios));
2c9b15ca 58 memory_region_init_ram(isa_bios, NULL, "isa-bios", isa_bios_size);
bd183c79
JJ
59 vmstate_register_ram_global(isa_bios);
60 memory_region_add_subregion_overlap(rom_memory,
61 0x100000 - isa_bios_size,
62 isa_bios,
63 1);
64
65 /* copy ISA rom image from top of flash memory */
66 flash_ptr = memory_region_get_ram_ptr(flash_mem);
67 isa_bios_ptr = memory_region_get_ram_ptr(isa_bios);
68 memcpy(isa_bios_ptr,
69 ((uint8_t*)flash_ptr) + (flash_size - isa_bios_size),
70 isa_bios_size);
71
72 memory_region_set_readonly(isa_bios, true);
73}
74
bd183c79
JJ
75static void pc_system_flash_init(MemoryRegion *rom_memory,
76 DriveInfo *pflash_drv)
77{
78 BlockDriverState *bdrv;
79 int64_t size;
a8170e5e 80 hwaddr phys_addr;
bd183c79
JJ
81 int sector_bits, sector_size;
82 pflash_t *system_flash;
83 MemoryRegion *flash_mem;
84
85 bdrv = pflash_drv->bdrv;
86 size = bdrv_getlength(pflash_drv->bdrv);
87 sector_bits = 12;
88 sector_size = 1 << sector_bits;
89
90 if ((size % sector_size) != 0) {
91 fprintf(stderr,
92 "qemu: PC system firmware (pflash) must be a multiple of 0x%x\n",
93 sector_size);
94 exit(1);
95 }
96
97 phys_addr = 0x100000000ULL - size;
98 system_flash = pflash_cfi01_register(phys_addr, NULL, "system.flash", size,
99 bdrv, sector_size, size >> sector_bits,
100 1, 0x0000, 0x0000, 0x0000, 0x0000, 0);
101 flash_mem = pflash_cfi01_get_memory(system_flash);
102
103 pc_isa_bios_init(rom_memory, flash_mem, size);
104}
105
dade922f 106static void old_pc_system_rom_init(MemoryRegion *rom_memory, bool isapc_ram_fw)
cbc5b5f3
JJ
107{
108 char *filename;
109 MemoryRegion *bios, *isa_bios;
110 int bios_size, isa_bios_size;
111 int ret;
112
113 /* BIOS load */
114 if (bios_name == NULL) {
115 bios_name = BIOS_FILENAME;
116 }
117 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
118 if (filename) {
119 bios_size = get_image_size(filename);
120 } else {
121 bios_size = -1;
122 }
123 if (bios_size <= 0 ||
124 (bios_size % 65536) != 0) {
125 goto bios_error;
126 }
127 bios = g_malloc(sizeof(*bios));
2c9b15ca 128 memory_region_init_ram(bios, NULL, "pc.bios", bios_size);
cbc5b5f3 129 vmstate_register_ram_global(bios);
dade922f
JJ
130 if (!isapc_ram_fw) {
131 memory_region_set_readonly(bios, true);
132 }
cbc5b5f3
JJ
133 ret = rom_add_file_fixed(bios_name, (uint32_t)(-bios_size), -1);
134 if (ret != 0) {
135 bios_error:
136 fprintf(stderr, "qemu: could not load PC BIOS '%s'\n", bios_name);
137 exit(1);
138 }
139 if (filename) {
140 g_free(filename);
141 }
142
143 /* map the last 128KB of the BIOS in ISA space */
144 isa_bios_size = bios_size;
145 if (isa_bios_size > (128 * 1024)) {
146 isa_bios_size = 128 * 1024;
147 }
148 isa_bios = g_malloc(sizeof(*isa_bios));
2c9b15ca 149 memory_region_init_alias(isa_bios, NULL, "isa-bios", bios,
cbc5b5f3
JJ
150 bios_size - isa_bios_size, isa_bios_size);
151 memory_region_add_subregion_overlap(rom_memory,
152 0x100000 - isa_bios_size,
153 isa_bios,
154 1);
dade922f
JJ
155 if (!isapc_ram_fw) {
156 memory_region_set_readonly(isa_bios, true);
157 }
cbc5b5f3
JJ
158
159 /* map all the bios at the top of memory */
160 memory_region_add_subregion(rom_memory,
161 (uint32_t)(-bios_size),
162 bios);
163}
164
6dd2a5c9 165void pc_system_firmware_init(MemoryRegion *rom_memory, bool isapc_ram_fw)
cbc5b5f3 166{
bd183c79 167 DriveInfo *pflash_drv;
1d38574f 168
bd183c79
JJ
169 pflash_drv = drive_get(IF_PFLASH, 0, 0);
170
6dd2a5c9 171 if (isapc_ram_fw || pflash_drv == NULL) {
dafb82e0 172 /* When a pflash drive is not found, use rom-mode */
6dd2a5c9 173 old_pc_system_rom_init(rom_memory, isapc_ram_fw);
dafb82e0 174 return;
bd183c79
JJ
175 }
176
a904410a
PB
177 if (kvm_enabled() && !kvm_readonly_mem_enabled()) {
178 /* Older KVM cannot execute from device memory. So, flash memory
179 * cannot be used unless the readonly memory kvm capability is present. */
180 fprintf(stderr, "qemu: pflash with kvm requires KVM readonly memory support\n");
bd183c79
JJ
181 exit(1);
182 }
a904410a
PB
183
184 pc_system_flash_init(rom_memory, pflash_drv);
cbc5b5f3 185}