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
28
29
30
31
32
33#include "qemu/osdep.h"
34#include "hw/core/cpu.h"
35#include "hw/sysbus.h"
36#include "sysemu/dma.h"
37#include "sysemu/reset.h"
38#include "hw/loader.h"
39#include "hw/qdev-properties.h"
40#include "qapi/error.h"
41#include "qemu/module.h"
42#include "hw/core/generic-loader.h"
43#include "exec/memory.h"
44
45#define CPU_NONE 0xFFFFFFFF
46
47static void generic_loader_reset(void *opaque)
48{
49 GenericLoaderState *s = GENERIC_LOADER(opaque);
50
51 if (s->set_pc) {
52 CPUClass *cc = CPU_GET_CLASS(s->cpu);
53 cpu_reset(s->cpu);
54 if (cc) {
55 cc->set_pc(s->cpu, s->addr);
56 }
57 }
58
59 if (s->data_len) {
60 MemTxAttrs attrs = { .unspecified = 0,
61 .secure = 0,
62 .user = 0,
63 .debug = 0,
64 .requester_id = 0,
65 };
66
67 attrs.debug = s->attrs.debug;
68 attrs.secure = s->attrs.secure;
69 attrs.requester_id = s->attrs.requester_id;
70
71 assert(s->data_len < sizeof(s->data));
72 address_space_rw(s->cpu->as, s->addr, attrs, (uint8_t *)&s->data,
73 s->data_len, true);
74 }
75}
76
77static void generic_loader_realize(DeviceState *dev, Error **errp)
78{
79 GenericLoaderState *s = GENERIC_LOADER(dev);
80 hwaddr entry;
81 int big_endian;
82 int size = 0;
83
84 s->set_pc = false;
85
86
87 if (s->data || s->data_len || s->data_be) {
88
89 if (s->file) {
90 error_setg(errp, "Specifying a file is not supported when loading "
91 "memory values");
92 return;
93 } else if (s->force_raw) {
94 error_setg(errp, "Specifying force-raw is not supported when "
95 "loading memory values");
96 return;
97 } else if (!s->data_len) {
98
99 error_setg(errp, "Both data and data-len must be specified");
100 return;
101 } else if (s->data_len > 8) {
102 error_setg(errp, "data-len cannot be greater then 8 bytes");
103 return;
104 }
105 } else if (s->file || s->force_raw) {
106
107 if (s->data || s->data_len || s->data_be) {
108 error_setg(errp, "data can not be specified when loading an "
109 "image");
110 return;
111 }
112
113
114
115 if (s->cpu_num != CPU_NONE) {
116 s->set_pc = true;
117 }
118 } else if (s->addr) {
119
120 if (s->data || s->data_len || s->data_be) {
121 error_setg(errp, "data can not be specified when setting a "
122 "program counter");
123 return;
124 } else if (s->cpu_num == CPU_NONE) {
125 error_setg(errp, "cpu_num must be specified when setting a "
126 "program counter");
127 return;
128 }
129 s->set_pc = true;
130 } else {
131
132 error_setg(errp, "please include valid arguments");
133 return;
134 }
135
136 qemu_register_reset_loader(generic_loader_reset, dev);
137
138 if (s->cpu_num != CPU_NONE) {
139 s->cpu = qemu_get_cpu(s->cpu_num);
140 if (!s->cpu) {
141 error_setg(errp, "Specified boot CPU#%d is nonexistent",
142 s->cpu_num);
143 return;
144 }
145 } else {
146 s->cpu = first_cpu;
147 }
148
149 big_endian = target_words_bigendian();
150
151 if (s->file) {
152 AddressSpace *as = s->cpu ? s->cpu->as : NULL;
153
154 if (!s->force_raw) {
155 size = load_elf_as(s->file, NULL, NULL, NULL, &entry, NULL, NULL,
156 NULL, big_endian, 0, 0, 0, as);
157
158 if (size < 0) {
159 size = load_uimage_as(s->file, &entry, NULL, NULL, NULL, NULL,
160 as);
161 }
162
163 if (size < 0) {
164 size = load_targphys_hex_as(s->file, &entry, as);
165 }
166 }
167
168 if (size < 0 || s->force_raw) {
169
170 size = load_image_targphys_as(s->file, s->addr, ram_size, as);
171 } else {
172 s->addr = entry;
173 }
174
175 if (size < 0) {
176 error_setg(errp, "Cannot load specified image %s", s->file);
177 return;
178 }
179 }
180
181
182 if (s->data_be) {
183 s->data = cpu_to_be64(s->data);
184 } else {
185 s->data = cpu_to_le64(s->data);
186 }
187
188
189
190
191
192 if (qdev_hotplug) {
193 generic_loader_reset(dev);
194 }
195}
196
197static void generic_loader_unrealize(DeviceState *dev)
198{
199 qemu_unregister_reset_loader(generic_loader_reset, dev);
200}
201
202static Property generic_loader_props[] = {
203 DEFINE_PROP_UINT64("addr", GenericLoaderState, addr, 0),
204 DEFINE_PROP_UINT64("data", GenericLoaderState, data, 0),
205 DEFINE_PROP_UINT8("data-len", GenericLoaderState, data_len, 0),
206 DEFINE_PROP_BOOL("data-be", GenericLoaderState, data_be, false),
207 DEFINE_PROP_UINT32("cpu-num", GenericLoaderState, cpu_num, CPU_NONE),
208 DEFINE_PROP_BOOL("force-raw", GenericLoaderState, force_raw, false),
209 DEFINE_PROP_STRING("file", GenericLoaderState, file),
210 DEFINE_PROP_UINT16("attrs-requester-id", GenericLoaderState,
211 attrs.requester_id, 0),
212 DEFINE_PROP_BOOL("attrs-debug", GenericLoaderState, attrs.debug, false),
213 DEFINE_PROP_BOOL("attrs-secure", GenericLoaderState, attrs.secure, false),
214 DEFINE_PROP_END_OF_LIST(),
215};
216
217static void generic_loader_class_init(ObjectClass *klass, void *data)
218{
219 DeviceClass *dc = DEVICE_CLASS(klass);
220
221
222
223
224
225
226
227 dc->realize = generic_loader_realize;
228 dc->unrealize = generic_loader_unrealize;
229 device_class_set_props(dc, generic_loader_props);
230 dc->desc = "Generic Loader";
231 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
232}
233
234static TypeInfo generic_loader_info = {
235 .name = TYPE_GENERIC_LOADER,
236 .parent = TYPE_DEVICE,
237 .instance_size = sizeof(GenericLoaderState),
238 .class_init = generic_loader_class_init,
239};
240
241static void generic_loader_register_type(void)
242{
243 type_register_static(&generic_loader_info);
244}
245
246type_init(generic_loader_register_type)
247