xref: /qemu/hw/core/generic-loader.c (revision 06b40d250ecfa1633209c2e431a7a38acfd03a98)
1 /*
2  * Generic Loader
3  *
4  * Copyright (C) 2014 Li Guang
5  * Copyright (C) 2016 Xilinx Inc.
6  * Written by Li Guang <lig.fnst@cn.fujitsu.com>
7  *
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.
12  *
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.
17  *
18  */
19 
20 /*
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.
25 
26  * This device allows the user to monkey patch memory. To be able to do
27  * this it needs a backend to manage the data, 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.
31  */
32 
33 #include "qemu/osdep.h"
34 #include "system/dma.h"
35 #include "system/reset.h"
36 #include "hw/boards.h"
37 #include "hw/loader.h"
38 #include "hw/qdev-properties.h"
39 #include "qapi/error.h"
40 #include "qemu/module.h"
41 #include "hw/core/generic-loader.h"
42 
43 #define CPU_NONE 0xFFFFFFFF
44 
generic_loader_reset(void * opaque)45 static void generic_loader_reset(void *opaque)
46 {
47     GenericLoaderState *s = GENERIC_LOADER(opaque);
48 
49     if (s->set_pc) {
50         cpu_reset(s->cpu);
51         cpu_set_pc(s->cpu, s->addr);
52     }
53 
54     if (s->data_len) {
55         assert(s->data_len <= sizeof(s->data));
56         dma_memory_write(s->cpu->as, s->addr, &s->data, s->data_len,
57                          MEMTXATTRS_UNSPECIFIED);
58     }
59 }
60 
generic_loader_realize(DeviceState * dev,Error ** errp)61 static void generic_loader_realize(DeviceState *dev, Error **errp)
62 {
63     GenericLoaderState *s = GENERIC_LOADER(dev);
64     hwaddr entry;
65     ssize_t size = 0;
66 
67     s->set_pc = false;
68 
69     /* Perform some error checking on the user's options */
70     if (s->data || s->data_len  || s->data_be) {
71         /* User is loading memory values */
72         if (s->file) {
73             error_setg(errp, "Specifying a file is not supported when loading "
74                        "memory values");
75             return;
76         } else if (s->force_raw) {
77             error_setg(errp, "Specifying force-raw is not supported when "
78                        "loading memory values");
79             return;
80         } else if (!s->data_len) {
81             /* We can't check for !data here as a value of 0 is still valid. */
82             error_setg(errp, "Both data and data-len must be specified");
83             return;
84         } else if (s->data_len > 8) {
85             error_setg(errp, "data-len cannot be greater then 8 bytes");
86             return;
87         }
88     } else if (s->file || s->force_raw)  {
89         /* User is loading an image */
90         if (s->data || s->data_len || s->data_be) {
91             error_setg(errp, "data can not be specified when loading an "
92                        "image");
93             return;
94         }
95         /* The user specified a file, only set the PC if they also specified
96          * a CPU to use.
97          */
98         if (s->cpu_num != CPU_NONE) {
99             s->set_pc = true;
100         }
101     } else if (s->addr) {
102         /* User is setting the PC */
103         if (s->data || s->data_len || s->data_be) {
104             error_setg(errp, "data can not be specified when setting a "
105                        "program counter");
106             return;
107         } else if (s->cpu_num == CPU_NONE) {
108             error_setg(errp, "cpu_num must be specified when setting a "
109                        "program counter");
110             return;
111         }
112         s->set_pc = true;
113     } else {
114         /* Did the user specify anything? */
115         error_setg(errp, "please include valid arguments");
116         return;
117     }
118 
119     qemu_register_reset(generic_loader_reset, dev);
120 
121     if (s->cpu_num != CPU_NONE) {
122         s->cpu = qemu_get_cpu(s->cpu_num);
123         if (!s->cpu) {
124             error_setg(errp, "Specified boot CPU#%d is nonexistent",
125                        s->cpu_num);
126             return;
127         }
128     } else {
129         s->cpu = first_cpu;
130     }
131 
132     if (s->file) {
133         AddressSpace *as = s->cpu ? s->cpu->as :  NULL;
134 
135         if (!s->force_raw) {
136             size = load_elf_as(s->file, NULL, NULL, NULL, &entry, NULL, NULL,
137                                NULL, ELFDATANONE, 0, 0, 0, as);
138 
139             if (size < 0) {
140                 size = load_uimage_as(s->file, &entry, NULL, NULL, NULL, NULL,
141                                       as);
142             }
143 
144             if (size < 0) {
145                 size = load_targphys_hex_as(s->file, &entry, as);
146             }
147         }
148 
149         if (size < 0 || s->force_raw) {
150             /* Default to the maximum size being the machine's ram size */
151             size = load_image_targphys_as(s->file, s->addr, current_machine->ram_size, as);
152         } else {
153             s->addr = entry;
154         }
155 
156         if (size < 0) {
157             error_setg(errp, "Cannot load specified image %s", s->file);
158             return;
159         }
160     }
161 
162     /* Convert the data endianness */
163     if (s->data_be) {
164         s->data = cpu_to_be64(s->data);
165     } else {
166         s->data = cpu_to_le64(s->data);
167     }
168 }
169 
generic_loader_unrealize(DeviceState * dev)170 static void generic_loader_unrealize(DeviceState *dev)
171 {
172     qemu_unregister_reset(generic_loader_reset, dev);
173 }
174 
175 static const Property generic_loader_props[] = {
176     DEFINE_PROP_UINT64("addr", GenericLoaderState, addr, 0),
177     DEFINE_PROP_UINT64("data", GenericLoaderState, data, 0),
178     DEFINE_PROP_UINT8("data-len", GenericLoaderState, data_len, 0),
179     DEFINE_PROP_BOOL("data-be", GenericLoaderState, data_be, false),
180     DEFINE_PROP_UINT32("cpu-num", GenericLoaderState, cpu_num, CPU_NONE),
181     DEFINE_PROP_BOOL("force-raw", GenericLoaderState, force_raw, false),
182     DEFINE_PROP_STRING("file", GenericLoaderState, file),
183 };
184 
generic_loader_class_init(ObjectClass * klass,const void * data)185 static void generic_loader_class_init(ObjectClass *klass, const void *data)
186 {
187     DeviceClass *dc = DEVICE_CLASS(klass);
188 
189     /* The reset function is not registered here and is instead registered in
190      * the realize function to allow this device to be added via the device_add
191      * command in the QEMU monitor.
192      * TODO: Improve the device_add functionality to allow resets to be
193      * connected
194      */
195     dc->realize = generic_loader_realize;
196     dc->unrealize = generic_loader_unrealize;
197     device_class_set_props(dc, generic_loader_props);
198     dc->desc = "Generic Loader";
199     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
200 }
201 
202 static const TypeInfo generic_loader_info = {
203     .name = TYPE_GENERIC_LOADER,
204     .parent = TYPE_DEVICE,
205     .instance_size = sizeof(GenericLoaderState),
206     .class_init = generic_loader_class_init,
207 };
208 
generic_loader_register_type(void)209 static void generic_loader_register_type(void)
210 {
211     type_register_static(&generic_loader_info);
212 }
213 
214 type_init(generic_loader_register_type)
215