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 "qemu/osdep.h"
27#include "qapi/error.h"
28#include "sysemu/block-backend.h"
29#include "qemu/error-report.h"
30#include "qemu/option.h"
31#include "hw/sysbus.h"
32#include "hw/hw.h"
33#include "hw/i386/pc.h"
34#include "hw/boards.h"
35#include "hw/loader.h"
36#include "sysemu/sysemu.h"
37#include "hw/block/flash.h"
38#include "sysemu/kvm.h"
39
40#define BIOS_FILENAME "bios.bin"
41
42typedef struct PcSysFwDevice {
43 SysBusDevice busdev;
44 uint8_t isapc_ram_fw;
45} PcSysFwDevice;
46
47static void pc_isa_bios_init(MemoryRegion *rom_memory,
48 MemoryRegion *flash_mem,
49 int ram_size)
50{
51 int isa_bios_size;
52 MemoryRegion *isa_bios;
53 uint64_t flash_size;
54 void *flash_ptr, *isa_bios_ptr;
55
56 flash_size = memory_region_size(flash_mem);
57
58
59 isa_bios_size = MIN(flash_size, 128 * 1024);
60 isa_bios = g_malloc(sizeof(*isa_bios));
61 memory_region_init_ram(isa_bios, NULL, "isa-bios", isa_bios_size,
62 &error_fatal);
63 memory_region_add_subregion_overlap(rom_memory,
64 0x100000 - isa_bios_size,
65 isa_bios,
66 1);
67
68
69 flash_ptr = memory_region_get_ram_ptr(flash_mem);
70 isa_bios_ptr = memory_region_get_ram_ptr(isa_bios);
71 memcpy(isa_bios_ptr,
72 ((uint8_t*)flash_ptr) + (flash_size - isa_bios_size),
73 isa_bios_size);
74
75 memory_region_set_readonly(isa_bios, true);
76}
77
78#define FLASH_MAP_UNIT_MAX 2
79
80
81
82
83
84
85
86#define FLASH_MAP_BASE_MIN ((hwaddr)(0x100000000ULL - 8*1024*1024))
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104static void pc_system_flash_init(MemoryRegion *rom_memory)
105{
106 int unit;
107 DriveInfo *pflash_drv;
108 BlockBackend *blk;
109 int64_t size;
110 char *fatal_errmsg = NULL;
111 hwaddr phys_addr = 0x100000000ULL;
112 int sector_bits, sector_size;
113 pflash_t *system_flash;
114 MemoryRegion *flash_mem;
115 char name[64];
116 void *flash_ptr;
117 int ret, flash_size;
118
119 sector_bits = 12;
120 sector_size = 1 << sector_bits;
121
122 for (unit = 0;
123 (unit < FLASH_MAP_UNIT_MAX &&
124 (pflash_drv = drive_get(IF_PFLASH, 0, unit)) != NULL);
125 ++unit) {
126 blk = blk_by_legacy_dinfo(pflash_drv);
127 size = blk_getlength(blk);
128 if (size < 0) {
129 fatal_errmsg = g_strdup_printf("failed to get backing file size");
130 } else if (size == 0) {
131 fatal_errmsg = g_strdup_printf("PC system firmware (pflash) "
132 "cannot have zero size");
133 } else if ((size % sector_size) != 0) {
134 fatal_errmsg = g_strdup_printf("PC system firmware (pflash) "
135 "must be a multiple of 0x%x", sector_size);
136 } else if (phys_addr < size || phys_addr - size < FLASH_MAP_BASE_MIN) {
137 fatal_errmsg = g_strdup_printf("oversized backing file, pflash "
138 "segments cannot be mapped under "
139 TARGET_FMT_plx, FLASH_MAP_BASE_MIN);
140 }
141 if (fatal_errmsg != NULL) {
142 Location loc;
143
144
145
146
147
148 loc_push_none(&loc);
149 if (pflash_drv->opts != NULL) {
150 qemu_opts_loc_restore(pflash_drv->opts);
151 }
152 error_report("%s", fatal_errmsg);
153 loc_pop(&loc);
154 g_free(fatal_errmsg);
155 exit(1);
156 }
157
158 phys_addr -= size;
159
160
161 snprintf(name, sizeof name, "system.flash%d", unit);
162 system_flash = pflash_cfi01_register(phys_addr, NULL , name,
163 size, blk, sector_size,
164 size >> sector_bits,
165 1 ,
166 0x0000 ,
167 0x0000 ,
168 0x0000 ,
169 0x0000 ,
170 0 );
171 if (unit == 0) {
172 flash_mem = pflash_cfi01_get_memory(system_flash);
173 pc_isa_bios_init(rom_memory, flash_mem, size);
174
175
176 if (kvm_memcrypt_enabled()) {
177 flash_ptr = memory_region_get_ram_ptr(flash_mem);
178 flash_size = memory_region_size(flash_mem);
179 ret = kvm_memcrypt_encrypt_data(flash_ptr, flash_size);
180 if (ret) {
181 error_report("failed to encrypt pflash rom");
182 exit(1);
183 }
184 }
185 }
186 }
187}
188
189static void old_pc_system_rom_init(MemoryRegion *rom_memory, bool isapc_ram_fw)
190{
191 char *filename;
192 MemoryRegion *bios, *isa_bios;
193 int bios_size, isa_bios_size;
194 int ret;
195
196
197 if (bios_name == NULL) {
198 bios_name = BIOS_FILENAME;
199 }
200 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
201 if (filename) {
202 bios_size = get_image_size(filename);
203 } else {
204 bios_size = -1;
205 }
206 if (bios_size <= 0 ||
207 (bios_size % 65536) != 0) {
208 goto bios_error;
209 }
210 bios = g_malloc(sizeof(*bios));
211 memory_region_init_ram(bios, NULL, "pc.bios", bios_size, &error_fatal);
212 if (!isapc_ram_fw) {
213 memory_region_set_readonly(bios, true);
214 }
215 ret = rom_add_file_fixed(bios_name, (uint32_t)(-bios_size), -1);
216 if (ret != 0) {
217 bios_error:
218 fprintf(stderr, "qemu: could not load PC BIOS '%s'\n", bios_name);
219 exit(1);
220 }
221 g_free(filename);
222
223
224 isa_bios_size = bios_size;
225 if (isa_bios_size > (128 * 1024)) {
226 isa_bios_size = 128 * 1024;
227 }
228 isa_bios = g_malloc(sizeof(*isa_bios));
229 memory_region_init_alias(isa_bios, NULL, "isa-bios", bios,
230 bios_size - isa_bios_size, isa_bios_size);
231 memory_region_add_subregion_overlap(rom_memory,
232 0x100000 - isa_bios_size,
233 isa_bios,
234 1);
235 if (!isapc_ram_fw) {
236 memory_region_set_readonly(isa_bios, true);
237 }
238
239
240 memory_region_add_subregion(rom_memory,
241 (uint32_t)(-bios_size),
242 bios);
243}
244
245void pc_system_firmware_init(MemoryRegion *rom_memory, bool isapc_ram_fw)
246{
247 DriveInfo *pflash_drv;
248
249 pflash_drv = drive_get(IF_PFLASH, 0, 0);
250
251 if (isapc_ram_fw || pflash_drv == NULL) {
252
253 old_pc_system_rom_init(rom_memory, isapc_ram_fw);
254 return;
255 }
256
257 if (kvm_enabled() && !kvm_readonly_mem_enabled()) {
258
259
260 fprintf(stderr, "qemu: pflash with kvm requires KVM readonly memory support\n");
261 exit(1);
262 }
263
264 pc_system_flash_init(rom_memory);
265}
266