Merge remote-tracking branch 'remotes/kevin/tags/for-upstream' into staging
[qemu/ar7.git] / hw / core / generic-loader.c
blob79a493e577ba57f565fa098379930e730b420836
1 /*
2 * Generic Loader
4 * Copyright (C) 2014 Li Guang
5 * Copyright (C) 2016 Xilinx Inc.
6 * Written by Li Guang <lig.fnst@cn.fujitsu.com>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
21 * Internally inside QEMU this is a device. It is a strange device that
22 * provides no hardware interface but allows QEMU to monkey patch memory
23 * specified when it is created. To be able to do this it has a reset
24 * callback that does the memory operations.
26 * This device allows the user to monkey patch memory. To be able to do
27 * this it needs a backend to manage the datas, the same as other
28 * memory-related devices. In this case as the backend is so trivial we
29 * have merged it with the frontend instead of creating and maintaining a
30 * separate backend.
33 #include "qemu/osdep.h"
34 #include "qom/cpu.h"
35 #include "hw/sysbus.h"
36 #include "sysemu/dma.h"
37 #include "hw/loader.h"
38 #include "qapi/error.h"
39 #include "qemu/module.h"
40 #include "hw/core/generic-loader.h"
42 #define CPU_NONE 0xFFFFFFFF
44 static void generic_loader_reset(void *opaque)
46 GenericLoaderState *s = GENERIC_LOADER(opaque);
48 if (s->set_pc) {
49 CPUClass *cc = CPU_GET_CLASS(s->cpu);
50 cpu_reset(s->cpu);
51 if (cc) {
52 cc->set_pc(s->cpu, s->addr);
56 if (s->data_len) {
57 assert(s->data_len < sizeof(s->data));
58 dma_memory_write(s->cpu->as, s->addr, &s->data, s->data_len);
62 static void generic_loader_realize(DeviceState *dev, Error **errp)
64 GenericLoaderState *s = GENERIC_LOADER(dev);
65 hwaddr entry;
66 int big_endian;
67 int size = 0;
69 s->set_pc = false;
71 /* Perform some error checking on the user's options */
72 if (s->data || s->data_len || s->data_be) {
73 /* User is loading memory values */
74 if (s->file) {
75 error_setg(errp, "Specifying a file is not supported when loading "
76 "memory values");
77 return;
78 } else if (s->force_raw) {
79 error_setg(errp, "Specifying force-raw is not supported when "
80 "loading memory values");
81 return;
82 } else if (!s->data_len) {
83 /* We can't check for !data here as a value of 0 is still valid. */
84 error_setg(errp, "Both data and data-len must be specified");
85 return;
86 } else if (s->data_len > 8) {
87 error_setg(errp, "data-len cannot be greater then 8 bytes");
88 return;
90 } else if (s->file || s->force_raw) {
91 /* User is loading an image */
92 if (s->data || s->data_len || s->data_be) {
93 error_setg(errp, "data can not be specified when loading an "
94 "image");
95 return;
97 /* The user specified a file, only set the PC if they also specified
98 * a CPU to use.
100 if (s->cpu_num != CPU_NONE) {
101 s->set_pc = true;
103 } else if (s->addr) {
104 /* User is setting the PC */
105 if (s->data || s->data_len || s->data_be) {
106 error_setg(errp, "data can not be specified when setting a "
107 "program counter");
108 return;
109 } else if (s->cpu_num == CPU_NONE) {
110 error_setg(errp, "cpu_num must be specified when setting a "
111 "program counter");
112 return;
114 s->set_pc = true;
115 } else {
116 /* Did the user specify anything? */
117 error_setg(errp, "please include valid arguments");
118 return;
121 qemu_register_reset(generic_loader_reset, dev);
123 if (s->cpu_num != CPU_NONE) {
124 s->cpu = qemu_get_cpu(s->cpu_num);
125 if (!s->cpu) {
126 error_setg(errp, "Specified boot CPU#%d is nonexistent",
127 s->cpu_num);
128 return;
130 } else {
131 s->cpu = first_cpu;
134 big_endian = target_words_bigendian();
136 if (s->file) {
137 AddressSpace *as = s->cpu ? s->cpu->as : NULL;
139 if (!s->force_raw) {
140 size = load_elf_as(s->file, NULL, NULL, NULL, &entry, NULL, NULL,
141 big_endian, 0, 0, 0, as);
143 if (size < 0) {
144 size = load_uimage_as(s->file, &entry, NULL, NULL, NULL, NULL,
145 as);
148 if (size < 0) {
149 size = load_targphys_hex_as(s->file, &entry, as);
153 if (size < 0 || s->force_raw) {
154 /* Default to the maximum size being the machine's ram size */
155 size = load_image_targphys_as(s->file, s->addr, ram_size, as);
156 } else {
157 s->addr = entry;
160 if (size < 0) {
161 error_setg(errp, "Cannot load specified image %s", s->file);
162 return;
166 /* Convert the data endiannes */
167 if (s->data_be) {
168 s->data = cpu_to_be64(s->data);
169 } else {
170 s->data = cpu_to_le64(s->data);
174 static void generic_loader_unrealize(DeviceState *dev, Error **errp)
176 qemu_unregister_reset(generic_loader_reset, dev);
179 static Property generic_loader_props[] = {
180 DEFINE_PROP_UINT64("addr", GenericLoaderState, addr, 0),
181 DEFINE_PROP_UINT64("data", GenericLoaderState, data, 0),
182 DEFINE_PROP_UINT8("data-len", GenericLoaderState, data_len, 0),
183 DEFINE_PROP_BOOL("data-be", GenericLoaderState, data_be, false),
184 DEFINE_PROP_UINT32("cpu-num", GenericLoaderState, cpu_num, CPU_NONE),
185 DEFINE_PROP_BOOL("force-raw", GenericLoaderState, force_raw, false),
186 DEFINE_PROP_STRING("file", GenericLoaderState, file),
187 DEFINE_PROP_END_OF_LIST(),
190 static void generic_loader_class_init(ObjectClass *klass, void *data)
192 DeviceClass *dc = DEVICE_CLASS(klass);
194 /* The reset function is not registered here and is instead registered in
195 * the realize function to allow this device to be added via the device_add
196 * command in the QEMU monitor.
197 * TODO: Improve the device_add functionality to allow resets to be
198 * connected
200 dc->realize = generic_loader_realize;
201 dc->unrealize = generic_loader_unrealize;
202 dc->props = generic_loader_props;
203 dc->desc = "Generic Loader";
204 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
207 static TypeInfo generic_loader_info = {
208 .name = TYPE_GENERIC_LOADER,
209 .parent = TYPE_DEVICE,
210 .instance_size = sizeof(GenericLoaderState),
211 .class_init = generic_loader_class_init,
214 static void generic_loader_register_type(void)
216 type_register_static(&generic_loader_info);
219 type_init(generic_loader_register_type)