]> git.proxmox.com Git - qemu.git/blob - hw/fw_cfg.c
Change fw_cfg_add_file() to get full file path as a parameter.
[qemu.git] / hw / fw_cfg.c
1 /*
2 * QEMU Firmware configuration device emulation
3 *
4 * Copyright (c) 2008 Gleb Natapov
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 #include "hw.h"
25 #include "sysemu.h"
26 #include "isa.h"
27 #include "fw_cfg.h"
28 #include "sysbus.h"
29
30 /* debug firmware config */
31 //#define DEBUG_FW_CFG
32
33 #ifdef DEBUG_FW_CFG
34 #define FW_CFG_DPRINTF(fmt, ...) \
35 do { printf("FW_CFG: " fmt , ## __VA_ARGS__); } while (0)
36 #else
37 #define FW_CFG_DPRINTF(fmt, ...)
38 #endif
39
40 #define FW_CFG_SIZE 2
41
42 typedef struct FWCfgEntry {
43 uint32_t len;
44 uint8_t *data;
45 void *callback_opaque;
46 FWCfgCallback callback;
47 } FWCfgEntry;
48
49 struct FWCfgState {
50 SysBusDevice busdev;
51 uint32_t ctl_iobase, data_iobase;
52 FWCfgEntry entries[2][FW_CFG_MAX_ENTRY];
53 FWCfgFiles *files;
54 uint16_t cur_entry;
55 uint32_t cur_offset;
56 };
57
58 static void fw_cfg_write(FWCfgState *s, uint8_t value)
59 {
60 int arch = !!(s->cur_entry & FW_CFG_ARCH_LOCAL);
61 FWCfgEntry *e = &s->entries[arch][s->cur_entry & FW_CFG_ENTRY_MASK];
62
63 FW_CFG_DPRINTF("write %d\n", value);
64
65 if (s->cur_entry & FW_CFG_WRITE_CHANNEL && s->cur_offset < e->len) {
66 e->data[s->cur_offset++] = value;
67 if (s->cur_offset == e->len) {
68 e->callback(e->callback_opaque, e->data);
69 s->cur_offset = 0;
70 }
71 }
72 }
73
74 static int fw_cfg_select(FWCfgState *s, uint16_t key)
75 {
76 int ret;
77
78 s->cur_offset = 0;
79 if ((key & FW_CFG_ENTRY_MASK) >= FW_CFG_MAX_ENTRY) {
80 s->cur_entry = FW_CFG_INVALID;
81 ret = 0;
82 } else {
83 s->cur_entry = key;
84 ret = 1;
85 }
86
87 FW_CFG_DPRINTF("select key %d (%sfound)\n", key, ret ? "" : "not ");
88
89 return ret;
90 }
91
92 static uint8_t fw_cfg_read(FWCfgState *s)
93 {
94 int arch = !!(s->cur_entry & FW_CFG_ARCH_LOCAL);
95 FWCfgEntry *e = &s->entries[arch][s->cur_entry & FW_CFG_ENTRY_MASK];
96 uint8_t ret;
97
98 if (s->cur_entry == FW_CFG_INVALID || !e->data || s->cur_offset >= e->len)
99 ret = 0;
100 else
101 ret = e->data[s->cur_offset++];
102
103 FW_CFG_DPRINTF("read %d\n", ret);
104
105 return ret;
106 }
107
108 static uint32_t fw_cfg_io_readb(void *opaque, uint32_t addr)
109 {
110 return fw_cfg_read(opaque);
111 }
112
113 static void fw_cfg_io_writeb(void *opaque, uint32_t addr, uint32_t value)
114 {
115 fw_cfg_write(opaque, (uint8_t)value);
116 }
117
118 static void fw_cfg_io_writew(void *opaque, uint32_t addr, uint32_t value)
119 {
120 fw_cfg_select(opaque, (uint16_t)value);
121 }
122
123 static uint32_t fw_cfg_mem_readb(void *opaque, target_phys_addr_t addr)
124 {
125 return fw_cfg_read(opaque);
126 }
127
128 static void fw_cfg_mem_writeb(void *opaque, target_phys_addr_t addr,
129 uint32_t value)
130 {
131 fw_cfg_write(opaque, (uint8_t)value);
132 }
133
134 static void fw_cfg_mem_writew(void *opaque, target_phys_addr_t addr,
135 uint32_t value)
136 {
137 fw_cfg_select(opaque, (uint16_t)value);
138 }
139
140 static CPUReadMemoryFunc * const fw_cfg_ctl_mem_read[3] = {
141 NULL,
142 NULL,
143 NULL,
144 };
145
146 static CPUWriteMemoryFunc * const fw_cfg_ctl_mem_write[3] = {
147 NULL,
148 fw_cfg_mem_writew,
149 NULL,
150 };
151
152 static CPUReadMemoryFunc * const fw_cfg_data_mem_read[3] = {
153 fw_cfg_mem_readb,
154 NULL,
155 NULL,
156 };
157
158 static CPUWriteMemoryFunc * const fw_cfg_data_mem_write[3] = {
159 fw_cfg_mem_writeb,
160 NULL,
161 NULL,
162 };
163
164 static void fw_cfg_reset(DeviceState *d)
165 {
166 FWCfgState *s = DO_UPCAST(FWCfgState, busdev.qdev, d);
167
168 fw_cfg_select(s, 0);
169 }
170
171 /* Save restore 32 bit int as uint16_t
172 This is a Big hack, but it is how the old state did it.
173 Or we broke compatibility in the state, or we can't use struct tm
174 */
175
176 static int get_uint32_as_uint16(QEMUFile *f, void *pv, size_t size)
177 {
178 uint32_t *v = pv;
179 *v = qemu_get_be16(f);
180 return 0;
181 }
182
183 static void put_unused(QEMUFile *f, void *pv, size_t size)
184 {
185 fprintf(stderr, "uint32_as_uint16 is only used for backward compatibility.\n");
186 fprintf(stderr, "This functions shouldn't be called.\n");
187 }
188
189 static const VMStateInfo vmstate_hack_uint32_as_uint16 = {
190 .name = "int32_as_uint16",
191 .get = get_uint32_as_uint16,
192 .put = put_unused,
193 };
194
195 #define VMSTATE_UINT16_HACK(_f, _s, _t) \
196 VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_hack_uint32_as_uint16, uint32_t)
197
198
199 static bool is_version_1(void *opaque, int version_id)
200 {
201 return version_id == 1;
202 }
203
204 static const VMStateDescription vmstate_fw_cfg = {
205 .name = "fw_cfg",
206 .version_id = 2,
207 .minimum_version_id = 1,
208 .minimum_version_id_old = 1,
209 .fields = (VMStateField []) {
210 VMSTATE_UINT16(cur_entry, FWCfgState),
211 VMSTATE_UINT16_HACK(cur_offset, FWCfgState, is_version_1),
212 VMSTATE_UINT32_V(cur_offset, FWCfgState, 2),
213 VMSTATE_END_OF_LIST()
214 }
215 };
216
217 int fw_cfg_add_bytes(FWCfgState *s, uint16_t key, uint8_t *data, uint32_t len)
218 {
219 int arch = !!(key & FW_CFG_ARCH_LOCAL);
220
221 key &= FW_CFG_ENTRY_MASK;
222
223 if (key >= FW_CFG_MAX_ENTRY)
224 return 0;
225
226 s->entries[arch][key].data = data;
227 s->entries[arch][key].len = len;
228
229 return 1;
230 }
231
232 int fw_cfg_add_i16(FWCfgState *s, uint16_t key, uint16_t value)
233 {
234 uint16_t *copy;
235
236 copy = qemu_malloc(sizeof(value));
237 *copy = cpu_to_le16(value);
238 return fw_cfg_add_bytes(s, key, (uint8_t *)copy, sizeof(value));
239 }
240
241 int fw_cfg_add_i32(FWCfgState *s, uint16_t key, uint32_t value)
242 {
243 uint32_t *copy;
244
245 copy = qemu_malloc(sizeof(value));
246 *copy = cpu_to_le32(value);
247 return fw_cfg_add_bytes(s, key, (uint8_t *)copy, sizeof(value));
248 }
249
250 int fw_cfg_add_i64(FWCfgState *s, uint16_t key, uint64_t value)
251 {
252 uint64_t *copy;
253
254 copy = qemu_malloc(sizeof(value));
255 *copy = cpu_to_le64(value);
256 return fw_cfg_add_bytes(s, key, (uint8_t *)copy, sizeof(value));
257 }
258
259 int fw_cfg_add_callback(FWCfgState *s, uint16_t key, FWCfgCallback callback,
260 void *callback_opaque, uint8_t *data, size_t len)
261 {
262 int arch = !!(key & FW_CFG_ARCH_LOCAL);
263
264 if (!(key & FW_CFG_WRITE_CHANNEL))
265 return 0;
266
267 key &= FW_CFG_ENTRY_MASK;
268
269 if (key >= FW_CFG_MAX_ENTRY || len > 65535)
270 return 0;
271
272 s->entries[arch][key].data = data;
273 s->entries[arch][key].len = len;
274 s->entries[arch][key].callback_opaque = callback_opaque;
275 s->entries[arch][key].callback = callback;
276
277 return 1;
278 }
279
280 int fw_cfg_add_file(FWCfgState *s, const char *filename, uint8_t *data,
281 uint32_t len)
282 {
283 int i, index;
284
285 if (!s->files) {
286 int dsize = sizeof(uint32_t) + sizeof(FWCfgFile) * FW_CFG_FILE_SLOTS;
287 s->files = qemu_mallocz(dsize);
288 fw_cfg_add_bytes(s, FW_CFG_FILE_DIR, (uint8_t*)s->files, dsize);
289 }
290
291 index = be32_to_cpu(s->files->count);
292 if (index == FW_CFG_FILE_SLOTS) {
293 fprintf(stderr, "fw_cfg: out of file slots\n");
294 return 0;
295 }
296
297 fw_cfg_add_bytes(s, FW_CFG_FILE_FIRST + index, data, len);
298
299 pstrcpy(s->files->f[index].name, sizeof(s->files->f[index].name),
300 filename);
301 for (i = 0; i < index; i++) {
302 if (strcmp(s->files->f[index].name, s->files->f[i].name) == 0) {
303 FW_CFG_DPRINTF("%s: skip duplicate: %s\n", __FUNCTION__,
304 s->files->f[index].name);
305 return 1;
306 }
307 }
308
309 s->files->f[index].size = cpu_to_be32(len);
310 s->files->f[index].select = cpu_to_be16(FW_CFG_FILE_FIRST + index);
311 FW_CFG_DPRINTF("%s: #%d: %s (%d bytes)\n", __FUNCTION__,
312 index, s->files->f[index].name, len);
313
314 s->files->count = cpu_to_be32(index+1);
315 return 1;
316 }
317
318 FWCfgState *fw_cfg_init(uint32_t ctl_port, uint32_t data_port,
319 target_phys_addr_t ctl_addr, target_phys_addr_t data_addr)
320 {
321 DeviceState *dev;
322 SysBusDevice *d;
323 FWCfgState *s;
324
325 dev = qdev_create(NULL, "fw_cfg");
326 qdev_prop_set_uint32(dev, "ctl_iobase", ctl_port);
327 qdev_prop_set_uint32(dev, "data_iobase", data_port);
328 qdev_init_nofail(dev);
329 d = sysbus_from_qdev(dev);
330
331 s = DO_UPCAST(FWCfgState, busdev.qdev, dev);
332
333 if (ctl_addr) {
334 sysbus_mmio_map(d, 0, ctl_addr);
335 }
336 if (data_addr) {
337 sysbus_mmio_map(d, 1, data_addr);
338 }
339 fw_cfg_add_bytes(s, FW_CFG_SIGNATURE, (uint8_t *)"QEMU", 4);
340 fw_cfg_add_bytes(s, FW_CFG_UUID, qemu_uuid, 16);
341 fw_cfg_add_i16(s, FW_CFG_NOGRAPHIC, (uint16_t)(display_type == DT_NOGRAPHIC));
342 fw_cfg_add_i16(s, FW_CFG_NB_CPUS, (uint16_t)smp_cpus);
343 fw_cfg_add_i16(s, FW_CFG_MAX_CPUS, (uint16_t)max_cpus);
344 fw_cfg_add_i16(s, FW_CFG_BOOT_MENU, (uint16_t)boot_menu);
345
346 return s;
347 }
348
349 static int fw_cfg_init1(SysBusDevice *dev)
350 {
351 FWCfgState *s = FROM_SYSBUS(FWCfgState, dev);
352 int io_ctl_memory, io_data_memory;
353
354 io_ctl_memory = cpu_register_io_memory(fw_cfg_ctl_mem_read,
355 fw_cfg_ctl_mem_write, s,
356 DEVICE_NATIVE_ENDIAN);
357 sysbus_init_mmio(dev, FW_CFG_SIZE, io_ctl_memory);
358
359 io_data_memory = cpu_register_io_memory(fw_cfg_data_mem_read,
360 fw_cfg_data_mem_write, s,
361 DEVICE_NATIVE_ENDIAN);
362 sysbus_init_mmio(dev, FW_CFG_SIZE, io_data_memory);
363
364 if (s->ctl_iobase) {
365 register_ioport_write(s->ctl_iobase, 2, 2, fw_cfg_io_writew, s);
366 }
367 if (s->data_iobase) {
368 register_ioport_read(s->data_iobase, 1, 1, fw_cfg_io_readb, s);
369 register_ioport_write(s->data_iobase, 1, 1, fw_cfg_io_writeb, s);
370 }
371 return 0;
372 }
373
374 static SysBusDeviceInfo fw_cfg_info = {
375 .init = fw_cfg_init1,
376 .qdev.name = "fw_cfg",
377 .qdev.size = sizeof(FWCfgState),
378 .qdev.vmsd = &vmstate_fw_cfg,
379 .qdev.reset = fw_cfg_reset,
380 .qdev.no_user = 1,
381 .qdev.props = (Property[]) {
382 DEFINE_PROP_HEX32("ctl_iobase", FWCfgState, ctl_iobase, -1),
383 DEFINE_PROP_HEX32("data_iobase", FWCfgState, data_iobase, -1),
384 DEFINE_PROP_END_OF_LIST(),
385 },
386 };
387
388 static void fw_cfg_register_devices(void)
389 {
390 sysbus_register_withprop(&fw_cfg_info);
391 }
392
393 device_init(fw_cfg_register_devices)