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