1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27#include "qemu/osdep.h"
28#include "hw/core/cpu.h"
29#include "sysemu/dma.h"
30#include "hw/loader.h"
31#include "hw/qdev-properties.h"
32#include "qapi/error.h"
33#include "qemu/module.h"
34#include "guest-loader.h"
35#include "sysemu/device_tree.h"
36#include "hw/boards.h"
37
38
39
40
41static void loader_insert_platform_data(GuestLoaderState *s, int size,
42 Error **errp)
43{
44 MachineState *machine = MACHINE(qdev_get_machine());
45 void *fdt = machine->fdt;
46 g_autofree char *node = g_strdup_printf("/chosen/module@0x%08" PRIx64,
47 s->addr);
48 uint64_t reg_attr[2] = {cpu_to_be64(s->addr), cpu_to_be64(size)};
49
50 if (!fdt) {
51 error_setg(errp, "Cannot modify FDT fields if the machine has none");
52 return;
53 }
54
55 qemu_fdt_add_subnode(fdt, node);
56 qemu_fdt_setprop(fdt, node, "reg", ®_attr, sizeof(reg_attr));
57
58 if (s->kernel) {
59 const char *compat[2] = { "multiboot,module", "multiboot,kernel" };
60 if (qemu_fdt_setprop_string_array(fdt, node, "compatible",
61 (char **) &compat,
62 ARRAY_SIZE(compat)) < 0) {
63 error_setg(errp, "couldn't set %s/compatible", node);
64 return;
65 }
66 if (s->args) {
67 if (qemu_fdt_setprop_string(fdt, node, "bootargs", s->args) < 0) {
68 error_setg(errp, "couldn't set %s/bootargs", node);
69 }
70 }
71 } else if (s->initrd) {
72 const char *compat[2] = { "multiboot,module", "multiboot,ramdisk" };
73 if (qemu_fdt_setprop_string_array(fdt, node, "compatible",
74 (char **) &compat,
75 ARRAY_SIZE(compat)) < 0) {
76 error_setg(errp, "couldn't set %s/compatible", node);
77 return;
78 }
79 }
80}
81
82static void guest_loader_realize(DeviceState *dev, Error **errp)
83{
84 GuestLoaderState *s = GUEST_LOADER(dev);
85 char *file = s->kernel ? s->kernel : s->initrd;
86 int size = 0;
87
88
89 if (s->kernel && s->initrd) {
90 error_setg(errp, "Cannot specify a kernel and initrd in same stanza");
91 return;
92 } else if (!s->kernel && !s->initrd) {
93 error_setg(errp, "Need to specify a kernel or initrd image");
94 return;
95 } else if (!s->addr) {
96 error_setg(errp, "Need to specify the address of guest blob");
97 return;
98 } else if (s->args && !s->kernel) {
99 error_setg(errp, "Boot args only relevant to kernel blobs");
100 }
101
102
103 size = load_image_targphys_as(file, s->addr, current_machine->ram_size,
104 NULL);
105 if (size < 0) {
106 error_setg(errp, "Cannot load specified image %s", file);
107 return;
108 }
109
110
111 loader_insert_platform_data(s, size, errp);
112}
113
114static 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 DEFINE_PROP_END_OF_LIST(),
120};
121
122static void guest_loader_class_init(ObjectClass *klass, void *data)
123{
124 DeviceClass *dc = DEVICE_CLASS(klass);
125
126 dc->realize = guest_loader_realize;
127 device_class_set_props(dc, guest_loader_props);
128 dc->desc = "Guest Loader";
129 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
130}
131
132static TypeInfo guest_loader_info = {
133 .name = TYPE_GUEST_LOADER,
134 .parent = TYPE_DEVICE,
135 .instance_size = sizeof(GuestLoaderState),
136 .class_init = guest_loader_class_init,
137};
138
139static void guest_loader_register_type(void)
140{
141 type_register_static(&guest_loader_info);
142}
143
144type_init(guest_loader_register_type)
145