Coverage Report

Created: 2026-11-20 08:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qemu/hw/i386/pc_sysfw.c
Line
Count
Source
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 "qemu/osdep.h"
27
#include "qapi/error.h"
28
#include "system/block-backend.h"
29
#include "qemu/error-report.h"
30
#include "qemu/option.h"
31
#include "qemu/units.h"
32
#include "hw/core/sysbus.h"
33
#include "hw/i386/x86.h"
34
#include "hw/i386/pc.h"
35
#include "hw/core/loader.h"
36
#include "hw/core/qdev-properties.h"
37
#include "hw/block/flash.h"
38
#include "system/kvm.h"
39
#include "target/i386/sev.h"
40
41
126
#define FLASH_SECTOR_SIZE 4096
42
43
static void pc_isa_bios_init(PCMachineState *pcms, MemoryRegion *isa_bios,
44
                             MemoryRegion *rom_memory, MemoryRegion *flash_mem)
45
0
{
46
0
    int isa_bios_size;
47
0
    uint64_t flash_size;
48
0
    void *flash_ptr, *isa_bios_ptr;
49
50
0
    flash_size = memory_region_size(flash_mem);
51
52
    /* map the last 128KB of the BIOS in ISA space */
53
0
    isa_bios_size = MIN(flash_size, 128 * KiB);
54
0
    if (machine_require_guest_memfd_private(MACHINE(pcms))) {
55
0
        memory_region_init_ram_guest_memfd_private(
56
0
            isa_bios, NULL, "isa-bios", isa_bios_size, &error_fatal);
57
0
    } else {
58
0
        memory_region_init_ram(isa_bios, NULL, "isa-bios", isa_bios_size,
59
0
                               &error_fatal);
60
0
    }
61
0
    memory_region_add_subregion_overlap(rom_memory,
62
0
                                        0x100000 - isa_bios_size,
63
0
                                        isa_bios,
64
0
                                        1);
65
66
    /* copy ISA rom image from top of flash memory */
67
0
    flash_ptr = memory_region_get_ram_ptr(flash_mem);
68
0
    isa_bios_ptr = memory_region_get_ram_ptr(isa_bios);
69
0
    memcpy(isa_bios_ptr,
70
0
           ((uint8_t*)flash_ptr) + (flash_size - isa_bios_size),
71
0
           isa_bios_size);
72
73
0
    if (!machine_require_guest_memfd_private(current_machine)) {
74
0
        memory_region_set_readonly(isa_bios, true);
75
0
    }
76
0
}
77
78
static PFlashCFI01 *pc_pflash_create(PCMachineState *pcms,
79
                                     const char *name,
80
                                     const char *alias_prop_name)
81
126
{
82
126
    DeviceState *dev = qdev_new(TYPE_PFLASH_CFI01);
83
84
126
    qdev_prop_set_uint64(dev, "sector-length", FLASH_SECTOR_SIZE);
85
126
    qdev_prop_set_uint8(dev, "width", 1);
86
126
    qdev_prop_set_string(dev, "name", name);
87
126
    object_property_add_child(OBJECT(pcms), name, OBJECT(dev));
88
126
    object_property_set_alias(OBJECT(pcms), alias_prop_name, OBJECT(dev));
89
    /*
90
     * The returned reference is tied to the child property and
91
     * will be removed with object_unparent.
92
     */
93
126
    object_unref(OBJECT(dev));
94
126
    return PFLASH_CFI01(dev);
95
126
}
96
97
void pc_system_flash_create(PCMachineState *pcms)
98
63
{
99
63
    PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
100
101
63
    if (pcmc->pci_enabled) {
102
63
        pcms->flash[0] = pc_pflash_create(pcms, "system.flash0",
103
63
                                          "pflash0");
104
63
        pcms->flash[1] = pc_pflash_create(pcms, "system.flash1",
105
63
                                          "pflash1");
106
63
    }
107
63
}
108
109
void pc_system_flash_cleanup_unused(PCMachineState *pcms)
110
63
{
111
63
    int i;
112
113
63
    assert(PC_MACHINE_GET_CLASS(pcms)->pci_enabled);
114
115
189
    for (i = 0; i < ARRAY_SIZE(pcms->flash); i++) {
116
126
        if (!qdev_is_realized(DEVICE(pcms->flash[i]))) {
117
126
            object_unparent(OBJECT(pcms->flash[i]));
118
126
            pcms->flash[i] = NULL;
119
126
        }
120
126
    }
121
63
}
122
123
/*
124
 * Map the pcms->flash[] from 4GiB downward, and realize.
125
 * Map them in descending order, i.e. pcms->flash[0] at the top,
126
 * without gaps.
127
 * Stop at the first pcms->flash[0] lacking a block backend.
128
 * Set each flash's size from its block backend.  Fatal error if the
129
 * size isn't a non-zero multiple of 4KiB, or the total size exceeds
130
 * pcms->max_fw_size.
131
 *
132
 * If pcms->flash[0] has a block backend, its memory is passed to
133
 * pc_isa_bios_init().  Merging several flash devices for isa-bios is
134
 * not supported.
135
 */
136
static void pc_system_flash_map(PCMachineState *pcms,
137
                                MemoryRegion *rom_memory)
138
0
{
139
0
    X86MachineState *x86ms = X86_MACHINE(pcms);
140
0
    PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
141
0
    hwaddr total_size = 0;
142
0
    int i;
143
0
    BlockBackend *blk;
144
0
    int64_t size;
145
0
    PFlashCFI01 *system_flash;
146
0
    MemoryRegion *flash_mem;
147
0
    void *flash_ptr;
148
0
    int flash_size;
149
150
0
    assert(PC_MACHINE_GET_CLASS(pcms)->pci_enabled);
151
152
0
    for (i = 0; i < ARRAY_SIZE(pcms->flash); i++) {
153
0
        hwaddr gpa;
154
155
0
        system_flash = pcms->flash[i];
156
0
        blk = pflash_cfi01_get_blk(system_flash);
157
0
        if (!blk) {
158
0
            break;
159
0
        }
160
0
        size = blk_getlength(blk);
161
0
        if (size < 0) {
162
0
            error_report("can't get size of block device %s: %s",
163
0
                         blk_name(blk), strerror(-size));
164
0
            exit(1);
165
0
        }
166
0
        if (size == 0 || !QEMU_IS_ALIGNED(size, FLASH_SECTOR_SIZE)) {
167
0
            error_report("system firmware block device %s has invalid size "
168
0
                         "%" PRId64,
169
0
                         blk_name(blk), size);
170
0
            info_report("its size must be a non-zero multiple of 0x%x",
171
0
                        FLASH_SECTOR_SIZE);
172
0
            exit(1);
173
0
        }
174
0
        if ((hwaddr)size != size
175
0
            || total_size > HWADDR_MAX - size
176
0
            || total_size + size > pcms->max_fw_size) {
177
0
            error_report("combined size of system firmware exceeds "
178
0
                         "%" PRIu64 " bytes",
179
0
                         pcms->max_fw_size);
180
0
            exit(1);
181
0
        }
182
183
0
        total_size += size;
184
0
        gpa = 0x100000000ULL - total_size; /* where the flash is mapped */
185
0
        qdev_prop_set_uint32(DEVICE(system_flash), "num-blocks",
186
0
                             size / FLASH_SECTOR_SIZE);
187
0
        sysbus_realize_and_unref(SYS_BUS_DEVICE(system_flash), &error_fatal);
188
0
        sysbus_mmio_map(SYS_BUS_DEVICE(system_flash), 0, gpa);
189
190
0
        if (i == 0) {
191
0
            flash_mem = pflash_cfi01_get_memory(system_flash);
192
0
            if (pcmc->isa_bios_alias) {
193
0
                x86_isa_bios_init(&x86ms->isa_bios, rom_memory, flash_mem,
194
0
                                  true);
195
0
            } else {
196
0
                pc_isa_bios_init(pcms, &x86ms->isa_bios, rom_memory, flash_mem);
197
0
            }
198
199
            /* Encrypt the pflash boot ROM */
200
0
            if (sev_enabled()) {
201
0
                flash_ptr = memory_region_get_ram_ptr(flash_mem);
202
0
                flash_size = memory_region_size(flash_mem);
203
0
                x86_firmware_configure(gpa, flash_ptr, flash_size);
204
0
            }
205
0
        }
206
0
    }
207
0
}
208
209
void pc_system_firmware_init(PCMachineState *pcms,
210
                             MemoryRegion *rom_memory)
211
63
{
212
63
    PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
213
63
    int i;
214
63
    BlockBackend *pflash_blk[ARRAY_SIZE(pcms->flash)];
215
216
63
    if (!pcmc->pci_enabled) {
217
        /*
218
         * If an IGVM file is specified then the firmware must be provided
219
         * in the IGVM file.
220
         */
221
0
        if (!X86_MACHINE(pcms)->igvm) {
222
0
            x86_bios_rom_init(X86_MACHINE(pcms), "bios.bin", rom_memory, true);
223
0
        }
224
0
        return;
225
0
    }
226
227
    /* Map legacy -drive if=pflash to machine properties */
228
189
    for (i = 0; i < ARRAY_SIZE(pcms->flash); i++) {
229
126
        pflash_cfi01_legacy_drive(pcms->flash[i],
230
126
                                  drive_get(IF_PFLASH, 0, i));
231
126
        pflash_blk[i] = pflash_cfi01_get_blk(pcms->flash[i]);
232
126
    }
233
234
    /* Reject gaps */
235
126
    for (i = 1; i < ARRAY_SIZE(pcms->flash); i++) {
236
63
        if (pflash_blk[i] && !pflash_blk[i - 1]) {
237
0
            error_report("pflash%d requires pflash%d", i, i - 1);
238
0
            exit(1);
239
0
        }
240
63
    }
241
242
63
    if (!pflash_blk[0]) {
243
        /*
244
         * Machine property pflash0 not set, use ROM mode unless using IGVM,
245
         * in which case the firmware must be provided by the IGVM file.
246
         */
247
63
        if (!X86_MACHINE(pcms)->igvm) {
248
63
            x86_bios_rom_init(X86_MACHINE(pcms), "bios.bin", rom_memory, false);
249
63
        }
250
63
    } else {
251
0
        if (kvm_enabled() && !kvm_readonly_mem_enabled()) {
252
            /*
253
             * Older KVM cannot execute from device memory. So, flash
254
             * memory cannot be used unless the readonly memory kvm
255
             * capability is present.
256
             */
257
0
            error_report("pflash with kvm requires KVM readonly memory support");
258
0
            exit(1);
259
0
        }
260
261
0
        pc_system_flash_map(pcms, rom_memory);
262
0
    }
263
264
63
    pc_system_flash_cleanup_unused(pcms);
265
266
    /*
267
     * The user should not have specified any pflash devices when using IGVM
268
     * to configure the guest.
269
     */
270
63
    if (X86_MACHINE(pcms)->igvm) {
271
0
        for (i = 0; i < ARRAY_SIZE(pcms->flash); i++) {
272
0
            if (pcms->flash[i]) {
273
0
                error_report("pflash devices cannot be configured when "
274
0
                             "using IGVM");
275
0
                exit(1);
276
0
            }
277
0
        }
278
0
    }
279
63
}