Statistics
| Branch: | Revision:

root / hw / pc_sysfw.c @ b072a3c8

History | View | Annotate | Download (7.5 kB)

1
/*
2
 * QEMU PC System Firmware
3
 *
4
 * Copyright (c) 2003-2004 Fabrice Bellard
5
 * Copyright (c) 2011-2012 Intel Corporation
6
 *
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:
13
 *
14
 * The above copyright notice and this permission notice shall be included in
15
 * all copies or substantial portions of the Software.
16
 *
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.
24
 */
25

    
26
#include "sysbus.h"
27
#include "hw.h"
28
#include "pc.h"
29
#include "hw/boards.h"
30
#include "loader.h"
31
#include "sysemu.h"
32
#include "flash.h"
33
#include "kvm.h"
34

    
35
#define BIOS_FILENAME "bios.bin"
36

    
37
typedef struct PcSysFwDevice {
38
    SysBusDevice busdev;
39
    uint8_t rom_only;
40
} PcSysFwDevice;
41

    
42
static void pc_isa_bios_init(MemoryRegion *rom_memory,
43
                             MemoryRegion *flash_mem,
44
                             int ram_size)
45
{
46
    int isa_bios_size;
47
    MemoryRegion *isa_bios;
48
    uint64_t flash_size;
49
    void *flash_ptr, *isa_bios_ptr;
50

    
51
    flash_size = memory_region_size(flash_mem);
52

    
53
    /* map the last 128KB of the BIOS in ISA space */
54
    isa_bios_size = flash_size;
55
    if (isa_bios_size > (128 * 1024)) {
56
        isa_bios_size = 128 * 1024;
57
    }
58
    isa_bios = g_malloc(sizeof(*isa_bios));
59
    memory_region_init_ram(isa_bios, "isa-bios", isa_bios_size);
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
    /* copy ISA rom image from top of flash memory */
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
static void pc_fw_add_pflash_drv(void)
77
{
78
    QemuOpts *opts;
79
    QEMUMachine *machine;
80
    char *filename;
81

    
82
    if (bios_name == NULL) {
83
        bios_name = BIOS_FILENAME;
84
    }
85
    filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
86

    
87
    opts = drive_add(IF_PFLASH, -1, filename, "readonly=on");
88
    if (opts == NULL) {
89
      return;
90
    }
91

    
92
    machine = find_default_machine();
93
    if (machine == NULL) {
94
      return;
95
    }
96

    
97
    drive_init(opts, machine->use_scsi);
98
}
99

    
100
static void pc_system_flash_init(MemoryRegion *rom_memory,
101
                                 DriveInfo *pflash_drv)
102
{
103
    BlockDriverState *bdrv;
104
    int64_t size;
105
    target_phys_addr_t phys_addr;
106
    int sector_bits, sector_size;
107
    pflash_t *system_flash;
108
    MemoryRegion *flash_mem;
109

    
110
    bdrv = pflash_drv->bdrv;
111
    size = bdrv_getlength(pflash_drv->bdrv);
112
    sector_bits = 12;
113
    sector_size = 1 << sector_bits;
114

    
115
    if ((size % sector_size) != 0) {
116
        fprintf(stderr,
117
                "qemu: PC system firmware (pflash) must be a multiple of 0x%x\n",
118
                sector_size);
119
        exit(1);
120
    }
121

    
122
    phys_addr = 0x100000000ULL - size;
123
    system_flash = pflash_cfi01_register(phys_addr, NULL, "system.flash", size,
124
                                         bdrv, sector_size, size >> sector_bits,
125
                                         1, 0x0000, 0x0000, 0x0000, 0x0000, 0);
126
    flash_mem = pflash_cfi01_get_memory(system_flash);
127

    
128
    pc_isa_bios_init(rom_memory, flash_mem, size);
129
}
130

    
131
static void old_pc_system_rom_init(MemoryRegion *rom_memory)
132
{
133
    char *filename;
134
    MemoryRegion *bios, *isa_bios;
135
    int bios_size, isa_bios_size;
136
    int ret;
137

    
138
    /* BIOS load */
139
    if (bios_name == NULL) {
140
        bios_name = BIOS_FILENAME;
141
    }
142
    filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
143
    if (filename) {
144
        bios_size = get_image_size(filename);
145
    } else {
146
        bios_size = -1;
147
    }
148
    if (bios_size <= 0 ||
149
        (bios_size % 65536) != 0) {
150
        goto bios_error;
151
    }
152
    bios = g_malloc(sizeof(*bios));
153
    memory_region_init_ram(bios, "pc.bios", bios_size);
154
    vmstate_register_ram_global(bios);
155
    memory_region_set_readonly(bios, true);
156
    ret = rom_add_file_fixed(bios_name, (uint32_t)(-bios_size), -1);
157
    if (ret != 0) {
158
    bios_error:
159
        fprintf(stderr, "qemu: could not load PC BIOS '%s'\n", bios_name);
160
        exit(1);
161
    }
162
    if (filename) {
163
        g_free(filename);
164
    }
165

    
166
    /* map the last 128KB of the BIOS in ISA space */
167
    isa_bios_size = bios_size;
168
    if (isa_bios_size > (128 * 1024)) {
169
        isa_bios_size = 128 * 1024;
170
    }
171
    isa_bios = g_malloc(sizeof(*isa_bios));
172
    memory_region_init_alias(isa_bios, "isa-bios", bios,
173
                             bios_size - isa_bios_size, isa_bios_size);
174
    memory_region_add_subregion_overlap(rom_memory,
175
                                        0x100000 - isa_bios_size,
176
                                        isa_bios,
177
                                        1);
178
    memory_region_set_readonly(isa_bios, true);
179

    
180
    /* map all the bios at the top of memory */
181
    memory_region_add_subregion(rom_memory,
182
                                (uint32_t)(-bios_size),
183
                                bios);
184
}
185

    
186
void pc_system_firmware_init(MemoryRegion *rom_memory)
187
{
188
    DriveInfo *pflash_drv;
189
    PcSysFwDevice *sysfw_dev;
190

    
191
    sysfw_dev = (PcSysFwDevice*) qdev_create(NULL, "pc-sysfw");
192

    
193
    if (sysfw_dev->rom_only) {
194
        old_pc_system_rom_init(rom_memory);
195
        return;
196
    }
197

    
198
    pflash_drv = drive_get(IF_PFLASH, 0, 0);
199

    
200
    /* Currently KVM cannot execute from device memory.
201
       Use old rom based firmware initialization for KVM. */
202
    if (kvm_enabled()) {
203
        if (pflash_drv != NULL) {
204
            fprintf(stderr, "qemu: pflash cannot be used with kvm enabled\n");
205
            exit(1);
206
        } else {
207
            sysfw_dev->rom_only = 1;
208
            old_pc_system_rom_init(rom_memory);
209
            return;
210
        }
211
    }
212

    
213
    /* If a pflash drive is not found, then create one using
214
       the bios filename. */
215
    if (pflash_drv == NULL) {
216
        pc_fw_add_pflash_drv();
217
        pflash_drv = drive_get(IF_PFLASH, 0, 0);
218
    }
219

    
220
    if (pflash_drv != NULL) {
221
        pc_system_flash_init(rom_memory, pflash_drv);
222
    } else {
223
        fprintf(stderr, "qemu: PC system firmware (pflash) not available\n");
224
        exit(1);
225
    }
226
}
227

    
228
static Property pcsysfw_properties[] = {
229
    DEFINE_PROP_UINT8("rom_only", PcSysFwDevice, rom_only, 0),
230
    DEFINE_PROP_END_OF_LIST(),
231
};
232

    
233
static void pcsysfw_class_init (ObjectClass *klass, void *data)
234
{
235
    DeviceClass *dc = DEVICE_CLASS (klass);
236

    
237
    dc->desc = "PC System Firmware";
238
    dc->props = pcsysfw_properties;
239
}
240

    
241
static TypeInfo pcsysfw_info = {
242
    .name          = "pc-sysfw",
243
    .parent        = TYPE_SYS_BUS_DEVICE,
244
    .instance_size = sizeof (PcSysFwDevice),
245
    .class_init    = pcsysfw_class_init,
246
};
247

    
248
static void pcsysfw_register (void)
249
{
250
    type_register_static (&pcsysfw_info);
251
}
252

    
253
type_init (pcsysfw_register);
254