s390x: upgrade status of KVM cores to "supported"
[qemu/ar7.git] / hw / core / generic-loader.c
blob3695dd439cd07699dd10d28afcc7605edc5d5625
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 "hw/core/generic-loader.h"
41 #define CPU_NONE 0xFFFFFFFF
43 static void generic_loader_reset(void *opaque)
45 GenericLoaderState *s = GENERIC_LOADER(opaque);
47 if (s->set_pc) {
48 CPUClass *cc = CPU_GET_CLASS(s->cpu);
49 cpu_reset(s->cpu);
50 if (cc) {
51 cc->set_pc(s->cpu, s->addr);
55 if (s->data_len) {
56 assert(s->data_len < sizeof(s->data));
57 dma_memory_write(s->cpu->as, s->addr, &s->data, s->data_len);
61 static void generic_loader_realize(DeviceState *dev, Error **errp)
63 GenericLoaderState *s = GENERIC_LOADER(dev);
64 hwaddr entry;
65 int big_endian;
66 int size = 0;
68 s->set_pc = false;
70 /* Perform some error checking on the user's options */
71 if (s->data || s->data_len || s->data_be) {
72 /* User is loading memory values */
73 if (s->file) {
74 error_setg(errp, "Specifying a file is not supported when loading "
75 "memory values");
76 return;
77 } else if (s->force_raw) {
78 error_setg(errp, "Specifying force-raw is not supported when "
79 "loading memory values");
80 return;
81 } else if (!s->data_len) {
82 /* We can't check for !data here as a value of 0 is still valid. */
83 error_setg(errp, "Both data and data-len must be specified");
84 return;
85 } else if (s->data_len > 8) {
86 error_setg(errp, "data-len cannot be greater then 8 bytes");
87 return;
89 } else if (s->file || s->force_raw) {
90 /* User is loading an image */
91 if (s->data || s->data_len || s->data_be) {
92 error_setg(errp, "data can not be specified when loading an "
93 "image");
94 return;
96 /* The user specified a file, only set the PC if they also specified
97 * a CPU to use.
99 if (s->cpu_num != CPU_NONE) {
100 s->set_pc = true;
102 } else if (s->addr) {
103 /* User is setting the PC */
104 if (s->data || s->data_len || s->data_be) {
105 error_setg(errp, "data can not be specified when setting a "
106 "program counter");
107 return;
108 } else if (s->cpu_num == CPU_NONE) {
109 error_setg(errp, "cpu_num must be specified when setting a "
110 "program counter");
111 return;
113 s->set_pc = true;
114 } else {
115 /* Did the user specify anything? */
116 error_setg(errp, "please include valid arguments");
117 return;
120 qemu_register_reset(generic_loader_reset, dev);
122 if (s->cpu_num != CPU_NONE) {
123 s->cpu = qemu_get_cpu(s->cpu_num);
124 if (!s->cpu) {
125 error_setg(errp, "Specified boot CPU#%d is nonexistent",
126 s->cpu_num);
127 return;
129 } else {
130 s->cpu = first_cpu;
133 big_endian = target_words_bigendian();
135 if (s->file) {
136 AddressSpace *as = s->cpu ? s->cpu->as : NULL;
138 if (!s->force_raw) {
139 size = load_elf_as(s->file, NULL, NULL, NULL, &entry, NULL, NULL,
140 big_endian, 0, 0, 0, as);
142 if (size < 0) {
143 size = load_uimage_as(s->file, &entry, NULL, NULL, NULL, NULL,
144 as);
147 if (size < 0) {
148 size = load_targphys_hex_as(s->file, &entry, as);
152 if (size < 0 || s->force_raw) {
153 /* Default to the maximum size being the machine's ram size */
154 size = load_image_targphys_as(s->file, s->addr, ram_size, as);
155 } else {
156 s->addr = entry;
159 if (size < 0) {
160 error_setg(errp, "Cannot load specified image %s", s->file);
161 return;
165 /* Convert the data endiannes */
166 if (s->data_be) {
167 s->data = cpu_to_be64(s->data);
168 } else {
169 s->data = cpu_to_le64(s->data);
173 static void generic_loader_unrealize(DeviceState *dev, Error **errp)
175 qemu_unregister_reset(generic_loader_reset, dev);
178 static Property generic_loader_props[] = {
179 DEFINE_PROP_UINT64("addr", GenericLoaderState, addr, 0),
180 DEFINE_PROP_UINT64("data", GenericLoaderState, data, 0),
181 DEFINE_PROP_UINT8("data-len", GenericLoaderState, data_len, 0),
182 DEFINE_PROP_BOOL("data-be", GenericLoaderState, data_be, false),
183 DEFINE_PROP_UINT32("cpu-num", GenericLoaderState, cpu_num, CPU_NONE),
184 DEFINE_PROP_BOOL("force-raw", GenericLoaderState, force_raw, false),
185 DEFINE_PROP_STRING("file", GenericLoaderState, file),
186 DEFINE_PROP_END_OF_LIST(),
189 static void generic_loader_class_init(ObjectClass *klass, void *data)
191 DeviceClass *dc = DEVICE_CLASS(klass);
193 /* The reset function is not registered here and is instead registered in
194 * the realize function to allow this device to be added via the device_add
195 * command in the QEMU monitor.
196 * TODO: Improve the device_add functionality to allow resets to be
197 * connected
199 dc->realize = generic_loader_realize;
200 dc->unrealize = generic_loader_unrealize;
201 dc->props = generic_loader_props;
202 dc->desc = "Generic Loader";
203 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
206 static TypeInfo generic_loader_info = {
207 .name = TYPE_GENERIC_LOADER,
208 .parent = TYPE_DEVICE,
209 .instance_size = sizeof(GenericLoaderState),
210 .class_init = generic_loader_class_init,
213 static void generic_loader_register_type(void)
215 type_register_static(&generic_loader_info);
218 type_init(generic_loader_register_type)