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