Coverage Report

Created: 2026-11-20 08:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qemu/hw/core/guest-loader.c
Line
Count
Source
1
/*
2
 * Guest Loader
3
 *
4
 * Copyright (C) 2020 Linaro
5
 * Written by Alex Bennée <alex.bennee@linaro.org>
6
 * (based on the generic-loader by Li Guang <lig.fnst@cn.fujitsu.com>)
7
 *
8
 * SPDX-License-Identifier: GPL-2.0-or-later
9
 *
10
 * This work is licensed under the terms of the GNU GPL, version 2 or later.
11
 * See the COPYING file in the top-level directory.
12
 */
13
14
/*
15
 * Much like the generic-loader this is treated as a special device
16
 * inside QEMU. However unlike the generic-loader this device is used
17
 * to load guest images for hypervisors. As part of that process the
18
 * hypervisor needs to have platform information passed to it by the
19
 * lower levels of the stack (e.g. firmware/bootloader). If you boot
20
 * the hypervisor directly you use the guest-loader to load the Dom0
21
 * or equivalent guest images in the right place in the same way a
22
 * boot loader would.
23
 *
24
 * This is only relevant for full system emulation.
25
 */
26
27
#include "qemu/osdep.h"
28
#include "hw/core/cpu.h"
29
#include "system/dma.h"
30
#include "hw/core/loader.h"
31
#include "hw/core/qdev-properties.h"
32
#include "qapi/error.h"
33
#include "qemu/module.h"
34
#include "guest-loader.h"
35
#include "system/device_tree.h"
36
#include "hw/core/boards.h"
37
38
/*
39
 * Insert some FDT nodes for the loaded blob.
40
 */
41
static void loader_insert_platform_data(GuestLoaderState *s, int size,
42
                                        Error **errp)
43
0
{
44
0
    MachineState *machine = MACHINE(qdev_get_machine());
45
0
    void *fdt = machine->fdt;
46
0
    g_autofree char *node = g_strdup_printf("/chosen/module@0x%08" PRIx64,
47
0
                                            s->addr);
48
0
    uint64_t reg_attr[2] = {cpu_to_be64(s->addr), cpu_to_be64(size)};
49
50
0
    if (!fdt) {
51
0
        error_setg(errp, "Cannot modify FDT fields if the machine has none");
52
0
        return;
53
0
    }
54
55
0
    qemu_fdt_add_subnode(fdt, node);
56
0
    qemu_fdt_setprop(fdt, node, "reg", &reg_attr, sizeof(reg_attr));
57
58
0
    if (s->kernel) {
59
0
        const char *compat[2] = { "multiboot,module", "multiboot,kernel" };
60
0
        if (qemu_fdt_setprop_string_array(fdt, node, "compatible",
61
0
                                          (char **) &compat,
62
0
                                          ARRAY_SIZE(compat)) < 0) {
63
0
            error_setg(errp, "couldn't set %s/compatible", node);
64
0
            return;
65
0
        }
66
0
        if (s->args) {
67
0
            if (qemu_fdt_setprop_string(fdt, node, "bootargs", s->args) < 0) {
68
0
                error_setg(errp, "couldn't set %s/bootargs", node);
69
0
            }
70
0
        }
71
0
    } else if (s->initrd) {
72
0
        const char *compat[2] = { "multiboot,module", "multiboot,ramdisk" };
73
0
        if (qemu_fdt_setprop_string_array(fdt, node, "compatible",
74
0
                                          (char **) &compat,
75
0
                                          ARRAY_SIZE(compat)) < 0) {
76
0
            error_setg(errp, "couldn't set %s/compatible", node);
77
0
            return;
78
0
        }
79
0
    }
80
0
}
81
82
static void guest_loader_realize(DeviceState *dev, Error **errp)
83
0
{
84
0
    GuestLoaderState *s = GUEST_LOADER(dev);
85
0
    char *file = s->kernel ? s->kernel : s->initrd;
86
0
    int size = 0;
87
88
    /* Perform some error checking on the user's options */
89
0
    if (s->kernel && s->initrd) {
90
0
        error_setg(errp, "Cannot specify a kernel and initrd in same stanza");
91
0
        return;
92
0
    } else if (!s->kernel && !s->initrd)  {
93
0
        error_setg(errp, "Need to specify a kernel or initrd image");
94
0
        return;
95
0
    } else if (!s->addr) {
96
0
        error_setg(errp, "Need to specify the address of guest blob");
97
0
        return;
98
0
    } else if (s->args && !s->kernel) {
99
0
        error_setg(errp, "Boot args only relevant to kernel blobs");
100
0
    }
101
102
    /* Default to the maximum size being the machine's ram size */
103
0
    size = load_image_targphys_as(file, s->addr, current_machine->ram_size,
104
0
                                  NULL, errp);
105
0
    if (size < 0) {
106
0
        error_prepend(errp, "Cannot load specified image %s: ", file);
107
0
        return;
108
0
    }
109
110
    /* Now the image is loaded we need to update the platform data */
111
0
    loader_insert_platform_data(s, size, errp);
112
0
}
113
114
static const Property guest_loader_props[] = {
115
    DEFINE_PROP_UINT64("addr", GuestLoaderState, addr, 0),
116
    DEFINE_PROP_STRING("kernel", GuestLoaderState, kernel),
117
    DEFINE_PROP_STRING("bootargs", GuestLoaderState, args),
118
    DEFINE_PROP_STRING("initrd", GuestLoaderState, initrd),
119
};
120
121
static void guest_loader_class_init(ObjectClass *klass, const void *data)
122
63
{
123
63
    DeviceClass *dc = DEVICE_CLASS(klass);
124
125
63
    dc->realize = guest_loader_realize;
126
63
    device_class_set_props(dc, guest_loader_props);
127
63
    dc->desc = "Guest Loader";
128
63
    set_bit(DEVICE_CATEGORY_MISC, dc->categories);
129
63
}
130
131
static const TypeInfo guest_loader_info = {
132
    .name = TYPE_GUEST_LOADER,
133
    .parent = TYPE_DEVICE,
134
    .instance_size = sizeof(GuestLoaderState),
135
    .class_init = guest_loader_class_init,
136
};
137
138
static void guest_loader_register_type(void)
139
63
{
140
63
    type_register_static(&guest_loader_info);
141
63
}
142
143
type_init(guest_loader_register_type)