]> git.proxmox.com Git - qemu.git/blob - hw/pc_sysfw.c
misc: move include files to include/qemu/
[qemu.git] / hw / 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 "blockdev.h"
27 #include "sysbus.h"
28 #include "hw.h"
29 #include "pc.h"
30 #include "hw/boards.h"
31 #include "loader.h"
32 #include "sysemu.h"
33 #include "flash.h"
34 #include "kvm.h"
35
36 #define BIOS_FILENAME "bios.bin"
37
38 typedef struct PcSysFwDevice {
39 SysBusDevice busdev;
40 uint8_t rom_only;
41 } PcSysFwDevice;
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 = flash_size;
56 if (isa_bios_size > (128 * 1024)) {
57 isa_bios_size = 128 * 1024;
58 }
59 isa_bios = g_malloc(sizeof(*isa_bios));
60 memory_region_init_ram(isa_bios, "isa-bios", isa_bios_size);
61 vmstate_register_ram_global(isa_bios);
62 memory_region_add_subregion_overlap(rom_memory,
63 0x100000 - isa_bios_size,
64 isa_bios,
65 1);
66
67 /* copy ISA rom image from top of flash memory */
68 flash_ptr = memory_region_get_ram_ptr(flash_mem);
69 isa_bios_ptr = memory_region_get_ram_ptr(isa_bios);
70 memcpy(isa_bios_ptr,
71 ((uint8_t*)flash_ptr) + (flash_size - isa_bios_size),
72 isa_bios_size);
73
74 memory_region_set_readonly(isa_bios, true);
75 }
76
77 static void pc_fw_add_pflash_drv(void)
78 {
79 QemuOpts *opts;
80 QEMUMachine *machine;
81 char *filename;
82
83 if (bios_name == NULL) {
84 bios_name = BIOS_FILENAME;
85 }
86 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
87
88 opts = drive_add(IF_PFLASH, -1, filename, "readonly=on");
89
90 g_free(filename);
91
92 if (opts == NULL) {
93 return;
94 }
95
96 machine = find_default_machine();
97 if (machine == NULL) {
98 return;
99 }
100
101 if (!drive_init(opts, machine->block_default_type)) {
102 qemu_opts_del(opts);
103 }
104 }
105
106 static void pc_system_flash_init(MemoryRegion *rom_memory,
107 DriveInfo *pflash_drv)
108 {
109 BlockDriverState *bdrv;
110 int64_t size;
111 hwaddr phys_addr;
112 int sector_bits, sector_size;
113 pflash_t *system_flash;
114 MemoryRegion *flash_mem;
115
116 bdrv = pflash_drv->bdrv;
117 size = bdrv_getlength(pflash_drv->bdrv);
118 sector_bits = 12;
119 sector_size = 1 << sector_bits;
120
121 if ((size % sector_size) != 0) {
122 fprintf(stderr,
123 "qemu: PC system firmware (pflash) must be a multiple of 0x%x\n",
124 sector_size);
125 exit(1);
126 }
127
128 phys_addr = 0x100000000ULL - size;
129 system_flash = pflash_cfi01_register(phys_addr, NULL, "system.flash", size,
130 bdrv, sector_size, size >> sector_bits,
131 1, 0x0000, 0x0000, 0x0000, 0x0000, 0);
132 flash_mem = pflash_cfi01_get_memory(system_flash);
133
134 pc_isa_bios_init(rom_memory, flash_mem, size);
135 }
136
137 static void old_pc_system_rom_init(MemoryRegion *rom_memory)
138 {
139 char *filename;
140 MemoryRegion *bios, *isa_bios;
141 int bios_size, isa_bios_size;
142 int ret;
143
144 /* BIOS load */
145 if (bios_name == NULL) {
146 bios_name = BIOS_FILENAME;
147 }
148 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
149 if (filename) {
150 bios_size = get_image_size(filename);
151 } else {
152 bios_size = -1;
153 }
154 if (bios_size <= 0 ||
155 (bios_size % 65536) != 0) {
156 goto bios_error;
157 }
158 bios = g_malloc(sizeof(*bios));
159 memory_region_init_ram(bios, "pc.bios", bios_size);
160 vmstate_register_ram_global(bios);
161 memory_region_set_readonly(bios, true);
162 ret = rom_add_file_fixed(bios_name, (uint32_t)(-bios_size), -1);
163 if (ret != 0) {
164 bios_error:
165 fprintf(stderr, "qemu: could not load PC BIOS '%s'\n", bios_name);
166 exit(1);
167 }
168 if (filename) {
169 g_free(filename);
170 }
171
172 /* map the last 128KB of the BIOS in ISA space */
173 isa_bios_size = bios_size;
174 if (isa_bios_size > (128 * 1024)) {
175 isa_bios_size = 128 * 1024;
176 }
177 isa_bios = g_malloc(sizeof(*isa_bios));
178 memory_region_init_alias(isa_bios, "isa-bios", bios,
179 bios_size - isa_bios_size, isa_bios_size);
180 memory_region_add_subregion_overlap(rom_memory,
181 0x100000 - isa_bios_size,
182 isa_bios,
183 1);
184 memory_region_set_readonly(isa_bios, true);
185
186 /* map all the bios at the top of memory */
187 memory_region_add_subregion(rom_memory,
188 (uint32_t)(-bios_size),
189 bios);
190 }
191
192 void pc_system_firmware_init(MemoryRegion *rom_memory)
193 {
194 DriveInfo *pflash_drv;
195 PcSysFwDevice *sysfw_dev;
196
197 sysfw_dev = (PcSysFwDevice*) qdev_create(NULL, "pc-sysfw");
198
199 qdev_init_nofail(DEVICE(sysfw_dev));
200
201 if (sysfw_dev->rom_only) {
202 old_pc_system_rom_init(rom_memory);
203 return;
204 }
205
206 pflash_drv = drive_get(IF_PFLASH, 0, 0);
207
208 /* Currently KVM cannot execute from device memory.
209 Use old rom based firmware initialization for KVM. */
210 if (kvm_enabled()) {
211 if (pflash_drv != NULL) {
212 fprintf(stderr, "qemu: pflash cannot be used with kvm enabled\n");
213 exit(1);
214 } else {
215 sysfw_dev->rom_only = 1;
216 old_pc_system_rom_init(rom_memory);
217 return;
218 }
219 }
220
221 /* If a pflash drive is not found, then create one using
222 the bios filename. */
223 if (pflash_drv == NULL) {
224 pc_fw_add_pflash_drv();
225 pflash_drv = drive_get(IF_PFLASH, 0, 0);
226 }
227
228 if (pflash_drv != NULL) {
229 pc_system_flash_init(rom_memory, pflash_drv);
230 } else {
231 fprintf(stderr, "qemu: PC system firmware (pflash) not available\n");
232 exit(1);
233 }
234 }
235
236 static Property pcsysfw_properties[] = {
237 DEFINE_PROP_UINT8("rom_only", PcSysFwDevice, rom_only, 0),
238 DEFINE_PROP_END_OF_LIST(),
239 };
240
241 static int pcsysfw_init(DeviceState *dev)
242 {
243 return 0;
244 }
245
246 static void pcsysfw_class_init (ObjectClass *klass, void *data)
247 {
248 DeviceClass *dc = DEVICE_CLASS (klass);
249
250 dc->desc = "PC System Firmware";
251 dc->init = pcsysfw_init;
252 dc->props = pcsysfw_properties;
253 }
254
255 static TypeInfo pcsysfw_info = {
256 .name = "pc-sysfw",
257 .parent = TYPE_SYS_BUS_DEVICE,
258 .instance_size = sizeof (PcSysFwDevice),
259 .class_init = pcsysfw_class_init,
260 };
261
262 static void pcsysfw_register (void)
263 {
264 type_register_static (&pcsysfw_info);
265 }
266
267 type_init (pcsysfw_register);
268