1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26#include "sysemu/block-backend.h"
27#include "qemu/error-report.h"
28#include "hw/sysbus.h"
29#include "hw/hw.h"
30#include "hw/i386/pc.h"
31#include "hw/boards.h"
32#include "hw/loader.h"
33#include "sysemu/sysemu.h"
34#include "hw/block/flash.h"
35#include "sysemu/kvm.h"
36
37#define BIOS_FILENAME "bios.bin"
38
39typedef struct PcSysFwDevice {
40 SysBusDevice busdev;
41 uint8_t isapc_ram_fw;
42} PcSysFwDevice;
43
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
56 isa_bios_size = MIN(flash_size, 128 * 1024);
57 isa_bios = g_malloc(sizeof(*isa_bios));
58 memory_region_init_ram(isa_bios, NULL, "isa-bios", isa_bios_size,
59 &error_fatal);
60 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
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
76#define FLASH_MAP_UNIT_MAX 2
77
78
79
80
81
82
83
84#define FLASH_MAP_BASE_MIN ((hwaddr)(0x100000000ULL - 8*1024*1024))
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102static void pc_system_flash_init(MemoryRegion *rom_memory)
103{
104 int unit;
105 DriveInfo *pflash_drv;
106 BlockBackend *blk;
107 int64_t size;
108 char *fatal_errmsg = NULL;
109 hwaddr phys_addr = 0x100000000ULL;
110 int sector_bits, sector_size;
111 pflash_t *system_flash;
112 MemoryRegion *flash_mem;
113 char name[64];
114
115 sector_bits = 12;
116 sector_size = 1 << sector_bits;
117
118 for (unit = 0;
119 (unit < FLASH_MAP_UNIT_MAX &&
120 (pflash_drv = drive_get(IF_PFLASH, 0, unit)) != NULL);
121 ++unit) {
122 blk = blk_by_legacy_dinfo(pflash_drv);
123 size = blk_getlength(blk);
124 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
141
142
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
157 snprintf(name, sizeof name, "system.flash%d", unit);
158 system_flash = pflash_cfi01_register(phys_addr, NULL , name,
159 size, blk, sector_size,
160 size >> sector_bits,
161 1 ,
162 0x0000 ,
163 0x0000 ,
164 0x0000 ,
165 0x0000 ,
166 0 );
167 if (unit == 0) {
168 flash_mem = pflash_cfi01_get_memory(system_flash);
169 pc_isa_bios_init(rom_memory, flash_mem, size);
170 }
171 }
172}
173
174static void old_pc_system_rom_init(MemoryRegion *rom_memory, bool isapc_ram_fw)
175{
176 char *filename;
177 MemoryRegion *bios, *isa_bios;
178 int bios_size, isa_bios_size;
179 int ret;
180
181
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));
196 memory_region_init_ram(bios, NULL, "pc.bios", bios_size, &error_fatal);
197 vmstate_register_ram_global(bios);
198 if (!isapc_ram_fw) {
199 memory_region_set_readonly(bios, true);
200 }
201 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 g_free(filename);
208
209
210 isa_bios_size = bios_size;
211 if (isa_bios_size > (128 * 1024)) {
212 isa_bios_size = 128 * 1024;
213 }
214 isa_bios = g_malloc(sizeof(*isa_bios));
215 memory_region_init_alias(isa_bios, NULL, "isa-bios", bios,
216 bios_size - isa_bios_size, isa_bios_size);
217 memory_region_add_subregion_overlap(rom_memory,
218 0x100000 - isa_bios_size,
219 isa_bios,
220 1);
221 if (!isapc_ram_fw) {
222 memory_region_set_readonly(isa_bios, true);
223 }
224
225
226 memory_region_add_subregion(rom_memory,
227 (uint32_t)(-bios_size),
228 bios);
229}
230
231void pc_system_firmware_init(MemoryRegion *rom_memory, bool isapc_ram_fw)
232{
233 DriveInfo *pflash_drv;
234
235 pflash_drv = drive_get(IF_PFLASH, 0, 0);
236
237 if (isapc_ram_fw || pflash_drv == NULL) {
238
239 old_pc_system_rom_init(rom_memory, isapc_ram_fw);
240 return;
241 }
242
243 if (kvm_enabled() && !kvm_readonly_mem_enabled()) {
244
245
246 fprintf(stderr, "qemu: pflash with kvm requires KVM readonly memory support\n");
247 exit(1);
248 }
249
250 pc_system_flash_init(rom_memory);
251}
252