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