qom: Use returned bool to check for failure, Coccinelle part
[qemu/ar7.git] / hw / i386 / pc_sysfw.c
blobec2a3b3e7eff2e6018b5a6134f3cc787351466a8
1 /*
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
23 * THE SOFTWARE.
26 #include "qemu/osdep.h"
27 #include "qemu-common.h"
28 #include "qapi/error.h"
29 #include "sysemu/block-backend.h"
30 #include "qemu/error-report.h"
31 #include "qemu/option.h"
32 #include "qemu/units.h"
33 #include "hw/sysbus.h"
34 #include "hw/i386/x86.h"
35 #include "hw/i386/pc.h"
36 #include "hw/loader.h"
37 #include "hw/qdev-properties.h"
38 #include "sysemu/sysemu.h"
39 #include "hw/block/flash.h"
40 #include "sysemu/kvm.h"
43 * We don't have a theoretically justifiable exact lower bound on the base
44 * address of any flash mapping. In practice, the IO-APIC MMIO range is
45 * [0xFEE00000..0xFEE01000] -- see IO_APIC_DEFAULT_ADDRESS --, leaving free
46 * only 18MB-4KB below 4G. For now, restrict the cumulative mapping to 8MB in
47 * size.
49 #define FLASH_SIZE_LIMIT (8 * MiB)
51 #define FLASH_SECTOR_SIZE 4096
53 static void pc_isa_bios_init(MemoryRegion *rom_memory,
54 MemoryRegion *flash_mem,
55 int ram_size)
57 int isa_bios_size;
58 MemoryRegion *isa_bios;
59 uint64_t flash_size;
60 void *flash_ptr, *isa_bios_ptr;
62 flash_size = memory_region_size(flash_mem);
64 /* map the last 128KB of the BIOS in ISA space */
65 isa_bios_size = MIN(flash_size, 128 * KiB);
66 isa_bios = g_malloc(sizeof(*isa_bios));
67 memory_region_init_ram(isa_bios, NULL, "isa-bios", isa_bios_size,
68 &error_fatal);
69 memory_region_add_subregion_overlap(rom_memory,
70 0x100000 - isa_bios_size,
71 isa_bios,
72 1);
74 /* copy ISA rom image from top of flash memory */
75 flash_ptr = memory_region_get_ram_ptr(flash_mem);
76 isa_bios_ptr = memory_region_get_ram_ptr(isa_bios);
77 memcpy(isa_bios_ptr,
78 ((uint8_t*)flash_ptr) + (flash_size - isa_bios_size),
79 isa_bios_size);
81 memory_region_set_readonly(isa_bios, true);
84 static PFlashCFI01 *pc_pflash_create(PCMachineState *pcms,
85 const char *name,
86 const char *alias_prop_name)
88 DeviceState *dev = qdev_new(TYPE_PFLASH_CFI01);
90 qdev_prop_set_uint64(dev, "sector-length", FLASH_SECTOR_SIZE);
91 qdev_prop_set_uint8(dev, "width", 1);
92 qdev_prop_set_string(dev, "name", name);
93 object_property_add_child(OBJECT(pcms), name, OBJECT(dev));
94 object_property_add_alias(OBJECT(pcms), alias_prop_name,
95 OBJECT(dev), "drive");
96 return PFLASH_CFI01(dev);
99 void pc_system_flash_create(PCMachineState *pcms)
101 PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
103 if (pcmc->pci_enabled) {
104 pcms->flash[0] = pc_pflash_create(pcms, "system.flash0",
105 "pflash0");
106 pcms->flash[1] = pc_pflash_create(pcms, "system.flash1",
107 "pflash1");
111 static void pc_system_flash_cleanup_unused(PCMachineState *pcms)
113 char *prop_name;
114 int i;
115 Object *dev_obj;
117 assert(PC_MACHINE_GET_CLASS(pcms)->pci_enabled);
119 for (i = 0; i < ARRAY_SIZE(pcms->flash); i++) {
120 dev_obj = OBJECT(pcms->flash[i]);
121 if (!object_property_get_bool(dev_obj, "realized", &error_abort)) {
122 prop_name = g_strdup_printf("pflash%d", i);
123 object_property_del(OBJECT(pcms), prop_name);
124 g_free(prop_name);
125 object_unparent(dev_obj);
126 pcms->flash[i] = NULL;
132 * Map the pcms->flash[] from 4GiB downward, and realize.
133 * Map them in descending order, i.e. pcms->flash[0] at the top,
134 * without gaps.
135 * Stop at the first pcms->flash[0] lacking a block backend.
136 * Set each flash's size from its block backend. Fatal error if the
137 * size isn't a non-zero multiple of 4KiB, or the total size exceeds
138 * FLASH_SIZE_LIMIT.
140 * If pcms->flash[0] has a block backend, its memory is passed to
141 * pc_isa_bios_init(). Merging several flash devices for isa-bios is
142 * not supported.
144 static void pc_system_flash_map(PCMachineState *pcms,
145 MemoryRegion *rom_memory)
147 hwaddr total_size = 0;
148 int i;
149 BlockBackend *blk;
150 int64_t size;
151 PFlashCFI01 *system_flash;
152 MemoryRegion *flash_mem;
153 void *flash_ptr;
154 int ret, flash_size;
156 assert(PC_MACHINE_GET_CLASS(pcms)->pci_enabled);
158 for (i = 0; i < ARRAY_SIZE(pcms->flash); i++) {
159 system_flash = pcms->flash[i];
160 blk = pflash_cfi01_get_blk(system_flash);
161 if (!blk) {
162 break;
164 size = blk_getlength(blk);
165 if (size < 0) {
166 error_report("can't get size of block device %s: %s",
167 blk_name(blk), strerror(-size));
168 exit(1);
170 if (size == 0 || !QEMU_IS_ALIGNED(size, FLASH_SECTOR_SIZE)) {
171 error_report("system firmware block device %s has invalid size "
172 "%" PRId64,
173 blk_name(blk), size);
174 info_report("its size must be a non-zero multiple of 0x%x",
175 FLASH_SECTOR_SIZE);
176 exit(1);
178 if ((hwaddr)size != size
179 || total_size > HWADDR_MAX - size
180 || total_size + size > FLASH_SIZE_LIMIT) {
181 error_report("combined size of system firmware exceeds "
182 "%" PRIu64 " bytes",
183 FLASH_SIZE_LIMIT);
184 exit(1);
187 total_size += size;
188 qdev_prop_set_uint32(DEVICE(system_flash), "num-blocks",
189 size / FLASH_SECTOR_SIZE);
190 sysbus_realize_and_unref(SYS_BUS_DEVICE(system_flash), &error_fatal);
191 sysbus_mmio_map(SYS_BUS_DEVICE(system_flash), 0,
192 0x100000000ULL - total_size);
194 if (i == 0) {
195 flash_mem = pflash_cfi01_get_memory(system_flash);
196 pc_isa_bios_init(rom_memory, flash_mem, size);
198 /* Encrypt the pflash boot ROM */
199 if (kvm_memcrypt_enabled()) {
200 flash_ptr = memory_region_get_ram_ptr(flash_mem);
201 flash_size = memory_region_size(flash_mem);
202 ret = kvm_memcrypt_encrypt_data(flash_ptr, flash_size);
203 if (ret) {
204 error_report("failed to encrypt pflash rom");
205 exit(1);
212 void pc_system_firmware_init(PCMachineState *pcms,
213 MemoryRegion *rom_memory)
215 PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
216 int i;
217 BlockBackend *pflash_blk[ARRAY_SIZE(pcms->flash)];
219 if (!pcmc->pci_enabled) {
220 x86_bios_rom_init(rom_memory, true);
221 return;
224 /* Map legacy -drive if=pflash to machine properties */
225 for (i = 0; i < ARRAY_SIZE(pcms->flash); i++) {
226 pflash_cfi01_legacy_drive(pcms->flash[i],
227 drive_get(IF_PFLASH, 0, i));
228 pflash_blk[i] = pflash_cfi01_get_blk(pcms->flash[i]);
231 /* Reject gaps */
232 for (i = 1; i < ARRAY_SIZE(pcms->flash); i++) {
233 if (pflash_blk[i] && !pflash_blk[i - 1]) {
234 error_report("pflash%d requires pflash%d", i, i - 1);
235 exit(1);
239 if (!pflash_blk[0]) {
240 /* Machine property pflash0 not set, use ROM mode */
241 x86_bios_rom_init(rom_memory, false);
242 } else {
243 if (kvm_enabled() && !kvm_readonly_mem_enabled()) {
245 * Older KVM cannot execute from device memory. So, flash
246 * memory cannot be used unless the readonly memory kvm
247 * capability is present.
249 error_report("pflash with kvm requires KVM readonly memory support");
250 exit(1);
253 pc_system_flash_map(pcms, rom_memory);
256 pc_system_flash_cleanup_unused(pcms);