2 * QEMU PC System Firmware
4 * Copyright (c) 2003-2004 Fabrice Bellard
5 * Copyright (c) 2011-2012 Intel Corporation
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:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
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
26 #include "sysemu/blockdev.h"
27 #include "qemu/error-report.h"
28 #include "hw/sysbus.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"
38 #include "../../tizen/src/maru_err_table.h"
41 #define BIOS_FILENAME "bios.bin"
43 typedef struct PcSysFwDevice {
48 static void pc_isa_bios_init(MemoryRegion *rom_memory,
49 MemoryRegion *flash_mem,
53 MemoryRegion *isa_bios;
55 void *flash_ptr, *isa_bios_ptr;
57 flash_size = memory_region_size(flash_mem);
59 /* map the last 128KB of the BIOS in ISA space */
60 isa_bios_size = MIN(flash_size, 128 * 1024);
61 isa_bios = g_malloc(sizeof(*isa_bios));
62 memory_region_init_ram(isa_bios, NULL, "isa-bios", isa_bios_size);
63 vmstate_register_ram_global(isa_bios);
64 memory_region_add_subregion_overlap(rom_memory,
65 0x100000 - isa_bios_size,
69 /* copy ISA rom image from top of flash memory */
70 flash_ptr = memory_region_get_ram_ptr(flash_mem);
71 isa_bios_ptr = memory_region_get_ram_ptr(isa_bios);
73 ((uint8_t*)flash_ptr) + (flash_size - isa_bios_size),
76 memory_region_set_readonly(isa_bios, true);
79 static void pc_system_flash_init(MemoryRegion *rom_memory,
80 DriveInfo *pflash_drv)
82 BlockDriverState *bdrv;
85 int sector_bits, sector_size;
86 pflash_t *system_flash;
87 MemoryRegion *flash_mem;
89 bdrv = pflash_drv->bdrv;
90 size = bdrv_getlength(pflash_drv->bdrv);
92 sector_size = 1 << sector_bits;
94 if ((size % sector_size) != 0) {
96 "qemu: PC system firmware (pflash) must be a multiple of 0x%x\n",
101 phys_addr = 0x100000000ULL - size;
102 system_flash = pflash_cfi01_register(phys_addr, NULL, "system.flash", size,
103 bdrv, sector_size, size >> sector_bits,
104 1, 0x0000, 0x0000, 0x0000, 0x0000, 0);
105 flash_mem = pflash_cfi01_get_memory(system_flash);
107 pc_isa_bios_init(rom_memory, flash_mem, size);
110 static void old_pc_system_rom_init(MemoryRegion *rom_memory, bool isapc_ram_fw)
113 MemoryRegion *bios, *isa_bios;
114 int bios_size, isa_bios_size;
118 if (bios_name == NULL) {
119 bios_name = BIOS_FILENAME;
121 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
123 bios_size = get_image_size(filename);
127 if (bios_size <= 0 ||
128 (bios_size % 65536) != 0) {
131 bios = g_malloc(sizeof(*bios));
132 memory_region_init_ram(bios, NULL, "pc.bios", bios_size);
133 vmstate_register_ram_global(bios);
135 memory_region_set_readonly(bios, true);
137 ret = rom_add_file_fixed(bios_name, (uint32_t)(-bios_size), -1);
140 fprintf(stderr, "qemu: could not load PC BIOS '%s'\n", bios_name);
142 maru_register_exit_msg(MARU_EXIT_BIOS_FILE_EXCEPTION, bios_name);
150 /* map the last 128KB of the BIOS in ISA space */
151 isa_bios_size = bios_size;
152 if (isa_bios_size > (128 * 1024)) {
153 isa_bios_size = 128 * 1024;
155 isa_bios = g_malloc(sizeof(*isa_bios));
156 memory_region_init_alias(isa_bios, NULL, "isa-bios", bios,
157 bios_size - isa_bios_size, isa_bios_size);
158 memory_region_add_subregion_overlap(rom_memory,
159 0x100000 - isa_bios_size,
163 memory_region_set_readonly(isa_bios, true);
166 /* map all the bios at the top of memory */
167 memory_region_add_subregion(rom_memory,
168 (uint32_t)(-bios_size),
172 void pc_system_firmware_init(MemoryRegion *rom_memory, bool isapc_ram_fw)
174 DriveInfo *pflash_drv;
176 pflash_drv = drive_get(IF_PFLASH, 0, 0);
178 if (isapc_ram_fw || pflash_drv == NULL) {
179 /* When a pflash drive is not found, use rom-mode */
180 old_pc_system_rom_init(rom_memory, isapc_ram_fw);
184 if (kvm_enabled() && !kvm_readonly_mem_enabled()) {
185 /* Older KVM cannot execute from device memory. So, flash memory
186 * cannot be used unless the readonly memory kvm capability is present. */
187 fprintf(stderr, "qemu: pflash with kvm requires KVM readonly memory support\n");
191 pc_system_flash_init(rom_memory, pflash_drv);