memory: introduce memory_region_test_and_clear_dirty
[qemu/ar7.git] / hw / fw_cfg.c
blob26f7125fe2993550669157bfb5751b60f21ce1a5
1 /*
2 * QEMU Firmware configuration device emulation
4 * Copyright (c) 2008 Gleb Natapov
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
24 #include "hw.h"
25 #include "sysemu/sysemu.h"
26 #include "isa.h"
27 #include "fw_cfg.h"
28 #include "sysbus.h"
29 #include "qemu/error-report.h"
30 #include "qemu/config-file.h"
32 /* debug firmware config */
33 //#define DEBUG_FW_CFG
35 #ifdef DEBUG_FW_CFG
36 #define FW_CFG_DPRINTF(fmt, ...) \
37 do { printf("FW_CFG: " fmt , ## __VA_ARGS__); } while (0)
38 #else
39 #define FW_CFG_DPRINTF(fmt, ...)
40 #endif
42 #define FW_CFG_SIZE 2
43 #define FW_CFG_DATA_SIZE 1
45 typedef struct FWCfgEntry {
46 uint32_t len;
47 uint8_t *data;
48 void *callback_opaque;
49 FWCfgCallback callback;
50 } FWCfgEntry;
52 struct FWCfgState {
53 SysBusDevice busdev;
54 MemoryRegion ctl_iomem, data_iomem, comb_iomem;
55 uint32_t ctl_iobase, data_iobase;
56 FWCfgEntry entries[2][FW_CFG_MAX_ENTRY];
57 FWCfgFiles *files;
58 uint16_t cur_entry;
59 uint32_t cur_offset;
60 Notifier machine_ready;
63 #define JPG_FILE 0
64 #define BMP_FILE 1
66 static char *read_splashfile(char *filename, int *file_sizep, int *file_typep)
68 GError *err = NULL;
69 gboolean res;
70 gchar *content;
71 int file_type = -1;
72 unsigned int filehead = 0;
73 int bmp_bpp;
75 res = g_file_get_contents(filename, &content, (gsize *)file_sizep, &err);
76 if (res == FALSE) {
77 error_report("failed to read splash file '%s'", filename);
78 g_error_free(err);
79 return NULL;
82 /* check file size */
83 if (*file_sizep < 30) {
84 goto error;
87 /* check magic ID */
88 filehead = ((content[0] & 0xff) + (content[1] << 8)) & 0xffff;
89 if (filehead == 0xd8ff) {
90 file_type = JPG_FILE;
91 } else if (filehead == 0x4d42) {
92 file_type = BMP_FILE;
93 } else {
94 goto error;
97 /* check BMP bpp */
98 if (file_type == BMP_FILE) {
99 bmp_bpp = (content[28] + (content[29] << 8)) & 0xffff;
100 if (bmp_bpp != 24) {
101 goto error;
105 /* return values */
106 *file_typep = file_type;
108 return content;
110 error:
111 error_report("splash file '%s' format not recognized; must be JPEG "
112 "or 24 bit BMP", filename);
113 g_free(content);
114 return NULL;
117 static void fw_cfg_bootsplash(FWCfgState *s)
119 int boot_splash_time = -1;
120 const char *boot_splash_filename = NULL;
121 char *p;
122 char *filename, *file_data;
123 int file_size;
124 int file_type = -1;
125 const char *temp;
127 /* get user configuration */
128 QemuOptsList *plist = qemu_find_opts("boot-opts");
129 QemuOpts *opts = QTAILQ_FIRST(&plist->head);
130 if (opts != NULL) {
131 temp = qemu_opt_get(opts, "splash");
132 if (temp != NULL) {
133 boot_splash_filename = temp;
135 temp = qemu_opt_get(opts, "splash-time");
136 if (temp != NULL) {
137 p = (char *)temp;
138 boot_splash_time = strtol(p, (char **)&p, 10);
142 /* insert splash time if user configurated */
143 if (boot_splash_time >= 0) {
144 /* validate the input */
145 if (boot_splash_time > 0xffff) {
146 error_report("splash time is big than 65535, force it to 65535.");
147 boot_splash_time = 0xffff;
149 /* use little endian format */
150 qemu_extra_params_fw[0] = (uint8_t)(boot_splash_time & 0xff);
151 qemu_extra_params_fw[1] = (uint8_t)((boot_splash_time >> 8) & 0xff);
152 fw_cfg_add_file(s, "etc/boot-menu-wait", qemu_extra_params_fw, 2);
155 /* insert splash file if user configurated */
156 if (boot_splash_filename != NULL) {
157 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, boot_splash_filename);
158 if (filename == NULL) {
159 error_report("failed to find file '%s'.", boot_splash_filename);
160 return;
163 /* loading file data */
164 file_data = read_splashfile(filename, &file_size, &file_type);
165 if (file_data == NULL) {
166 g_free(filename);
167 return;
169 if (boot_splash_filedata != NULL) {
170 g_free(boot_splash_filedata);
172 boot_splash_filedata = (uint8_t *)file_data;
173 boot_splash_filedata_size = file_size;
175 /* insert data */
176 if (file_type == JPG_FILE) {
177 fw_cfg_add_file(s, "bootsplash.jpg",
178 boot_splash_filedata, boot_splash_filedata_size);
179 } else {
180 fw_cfg_add_file(s, "bootsplash.bmp",
181 boot_splash_filedata, boot_splash_filedata_size);
183 g_free(filename);
187 static void fw_cfg_reboot(FWCfgState *s)
189 int reboot_timeout = -1;
190 char *p;
191 const char *temp;
193 /* get user configuration */
194 QemuOptsList *plist = qemu_find_opts("boot-opts");
195 QemuOpts *opts = QTAILQ_FIRST(&plist->head);
196 if (opts != NULL) {
197 temp = qemu_opt_get(opts, "reboot-timeout");
198 if (temp != NULL) {
199 p = (char *)temp;
200 reboot_timeout = strtol(p, (char **)&p, 10);
203 /* validate the input */
204 if (reboot_timeout > 0xffff) {
205 error_report("reboot timeout is larger than 65535, force it to 65535.");
206 reboot_timeout = 0xffff;
208 fw_cfg_add_file(s, "etc/boot-fail-wait", g_memdup(&reboot_timeout, 4), 4);
211 static void fw_cfg_write(FWCfgState *s, uint8_t value)
213 int arch = !!(s->cur_entry & FW_CFG_ARCH_LOCAL);
214 FWCfgEntry *e = &s->entries[arch][s->cur_entry & FW_CFG_ENTRY_MASK];
216 FW_CFG_DPRINTF("write %d\n", value);
218 if (s->cur_entry & FW_CFG_WRITE_CHANNEL && e->callback &&
219 s->cur_offset < e->len) {
220 e->data[s->cur_offset++] = value;
221 if (s->cur_offset == e->len) {
222 e->callback(e->callback_opaque, e->data);
223 s->cur_offset = 0;
228 static int fw_cfg_select(FWCfgState *s, uint16_t key)
230 int ret;
232 s->cur_offset = 0;
233 if ((key & FW_CFG_ENTRY_MASK) >= FW_CFG_MAX_ENTRY) {
234 s->cur_entry = FW_CFG_INVALID;
235 ret = 0;
236 } else {
237 s->cur_entry = key;
238 ret = 1;
241 FW_CFG_DPRINTF("select key %d (%sfound)\n", key, ret ? "" : "not ");
243 return ret;
246 static uint8_t fw_cfg_read(FWCfgState *s)
248 int arch = !!(s->cur_entry & FW_CFG_ARCH_LOCAL);
249 FWCfgEntry *e = &s->entries[arch][s->cur_entry & FW_CFG_ENTRY_MASK];
250 uint8_t ret;
252 if (s->cur_entry == FW_CFG_INVALID || !e->data || s->cur_offset >= e->len)
253 ret = 0;
254 else
255 ret = e->data[s->cur_offset++];
257 FW_CFG_DPRINTF("read %d\n", ret);
259 return ret;
262 static uint64_t fw_cfg_data_mem_read(void *opaque, hwaddr addr,
263 unsigned size)
265 return fw_cfg_read(opaque);
268 static void fw_cfg_data_mem_write(void *opaque, hwaddr addr,
269 uint64_t value, unsigned size)
271 fw_cfg_write(opaque, (uint8_t)value);
274 static void fw_cfg_ctl_mem_write(void *opaque, hwaddr addr,
275 uint64_t value, unsigned size)
277 fw_cfg_select(opaque, (uint16_t)value);
280 static bool fw_cfg_ctl_mem_valid(void *opaque, hwaddr addr,
281 unsigned size, bool is_write)
283 return is_write && size == 2;
286 static uint64_t fw_cfg_comb_read(void *opaque, hwaddr addr,
287 unsigned size)
289 return fw_cfg_read(opaque);
292 static void fw_cfg_comb_write(void *opaque, hwaddr addr,
293 uint64_t value, unsigned size)
295 switch (size) {
296 case 1:
297 fw_cfg_write(opaque, (uint8_t)value);
298 break;
299 case 2:
300 fw_cfg_select(opaque, (uint16_t)value);
301 break;
305 static bool fw_cfg_comb_valid(void *opaque, hwaddr addr,
306 unsigned size, bool is_write)
308 return (size == 1) || (is_write && size == 2);
311 static const MemoryRegionOps fw_cfg_ctl_mem_ops = {
312 .write = fw_cfg_ctl_mem_write,
313 .endianness = DEVICE_NATIVE_ENDIAN,
314 .valid.accepts = fw_cfg_ctl_mem_valid,
317 static const MemoryRegionOps fw_cfg_data_mem_ops = {
318 .read = fw_cfg_data_mem_read,
319 .write = fw_cfg_data_mem_write,
320 .endianness = DEVICE_NATIVE_ENDIAN,
321 .valid = {
322 .min_access_size = 1,
323 .max_access_size = 1,
327 static const MemoryRegionOps fw_cfg_comb_mem_ops = {
328 .read = fw_cfg_comb_read,
329 .write = fw_cfg_comb_write,
330 .endianness = DEVICE_NATIVE_ENDIAN,
331 .valid.accepts = fw_cfg_comb_valid,
334 static void fw_cfg_reset(DeviceState *d)
336 FWCfgState *s = DO_UPCAST(FWCfgState, busdev.qdev, d);
338 fw_cfg_select(s, 0);
341 /* Save restore 32 bit int as uint16_t
342 This is a Big hack, but it is how the old state did it.
343 Or we broke compatibility in the state, or we can't use struct tm
346 static int get_uint32_as_uint16(QEMUFile *f, void *pv, size_t size)
348 uint32_t *v = pv;
349 *v = qemu_get_be16(f);
350 return 0;
353 static void put_unused(QEMUFile *f, void *pv, size_t size)
355 fprintf(stderr, "uint32_as_uint16 is only used for backward compatibility.\n");
356 fprintf(stderr, "This functions shouldn't be called.\n");
359 static const VMStateInfo vmstate_hack_uint32_as_uint16 = {
360 .name = "int32_as_uint16",
361 .get = get_uint32_as_uint16,
362 .put = put_unused,
365 #define VMSTATE_UINT16_HACK(_f, _s, _t) \
366 VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_hack_uint32_as_uint16, uint32_t)
369 static bool is_version_1(void *opaque, int version_id)
371 return version_id == 1;
374 static const VMStateDescription vmstate_fw_cfg = {
375 .name = "fw_cfg",
376 .version_id = 2,
377 .minimum_version_id = 1,
378 .minimum_version_id_old = 1,
379 .fields = (VMStateField []) {
380 VMSTATE_UINT16(cur_entry, FWCfgState),
381 VMSTATE_UINT16_HACK(cur_offset, FWCfgState, is_version_1),
382 VMSTATE_UINT32_V(cur_offset, FWCfgState, 2),
383 VMSTATE_END_OF_LIST()
387 int fw_cfg_add_bytes(FWCfgState *s, uint16_t key, uint8_t *data, uint32_t len)
389 int arch = !!(key & FW_CFG_ARCH_LOCAL);
391 key &= FW_CFG_ENTRY_MASK;
393 if (key >= FW_CFG_MAX_ENTRY)
394 return 0;
396 s->entries[arch][key].data = data;
397 s->entries[arch][key].len = len;
399 return 1;
402 int fw_cfg_add_i16(FWCfgState *s, uint16_t key, uint16_t value)
404 uint16_t *copy;
406 copy = g_malloc(sizeof(value));
407 *copy = cpu_to_le16(value);
408 return fw_cfg_add_bytes(s, key, (uint8_t *)copy, sizeof(value));
411 int fw_cfg_add_i32(FWCfgState *s, uint16_t key, uint32_t value)
413 uint32_t *copy;
415 copy = g_malloc(sizeof(value));
416 *copy = cpu_to_le32(value);
417 return fw_cfg_add_bytes(s, key, (uint8_t *)copy, sizeof(value));
420 int fw_cfg_add_i64(FWCfgState *s, uint16_t key, uint64_t value)
422 uint64_t *copy;
424 copy = g_malloc(sizeof(value));
425 *copy = cpu_to_le64(value);
426 return fw_cfg_add_bytes(s, key, (uint8_t *)copy, sizeof(value));
429 int fw_cfg_add_callback(FWCfgState *s, uint16_t key, FWCfgCallback callback,
430 void *callback_opaque, uint8_t *data, size_t len)
432 int arch = !!(key & FW_CFG_ARCH_LOCAL);
434 if (!(key & FW_CFG_WRITE_CHANNEL))
435 return 0;
437 key &= FW_CFG_ENTRY_MASK;
439 if (key >= FW_CFG_MAX_ENTRY || len > 65535)
440 return 0;
442 s->entries[arch][key].data = data;
443 s->entries[arch][key].len = len;
444 s->entries[arch][key].callback_opaque = callback_opaque;
445 s->entries[arch][key].callback = callback;
447 return 1;
450 int fw_cfg_add_file(FWCfgState *s, const char *filename, uint8_t *data,
451 uint32_t len)
453 int i, index;
455 if (!s->files) {
456 int dsize = sizeof(uint32_t) + sizeof(FWCfgFile) * FW_CFG_FILE_SLOTS;
457 s->files = g_malloc0(dsize);
458 fw_cfg_add_bytes(s, FW_CFG_FILE_DIR, (uint8_t*)s->files, dsize);
461 index = be32_to_cpu(s->files->count);
462 if (index == FW_CFG_FILE_SLOTS) {
463 fprintf(stderr, "fw_cfg: out of file slots\n");
464 return 0;
467 fw_cfg_add_bytes(s, FW_CFG_FILE_FIRST + index, data, len);
469 pstrcpy(s->files->f[index].name, sizeof(s->files->f[index].name),
470 filename);
471 for (i = 0; i < index; i++) {
472 if (strcmp(s->files->f[index].name, s->files->f[i].name) == 0) {
473 FW_CFG_DPRINTF("%s: skip duplicate: %s\n", __FUNCTION__,
474 s->files->f[index].name);
475 return 1;
479 s->files->f[index].size = cpu_to_be32(len);
480 s->files->f[index].select = cpu_to_be16(FW_CFG_FILE_FIRST + index);
481 FW_CFG_DPRINTF("%s: #%d: %s (%d bytes)\n", __FUNCTION__,
482 index, s->files->f[index].name, len);
484 s->files->count = cpu_to_be32(index+1);
485 return 1;
488 static void fw_cfg_machine_ready(struct Notifier *n, void *data)
490 uint32_t len;
491 FWCfgState *s = container_of(n, FWCfgState, machine_ready);
492 char *bootindex = get_boot_devices_list(&len);
494 fw_cfg_add_file(s, "bootorder", (uint8_t*)bootindex, len);
497 FWCfgState *fw_cfg_init(uint32_t ctl_port, uint32_t data_port,
498 hwaddr ctl_addr, hwaddr data_addr)
500 DeviceState *dev;
501 SysBusDevice *d;
502 FWCfgState *s;
504 dev = qdev_create(NULL, "fw_cfg");
505 qdev_prop_set_uint32(dev, "ctl_iobase", ctl_port);
506 qdev_prop_set_uint32(dev, "data_iobase", data_port);
507 qdev_init_nofail(dev);
508 d = sysbus_from_qdev(dev);
510 s = DO_UPCAST(FWCfgState, busdev.qdev, dev);
512 if (ctl_addr) {
513 sysbus_mmio_map(d, 0, ctl_addr);
515 if (data_addr) {
516 sysbus_mmio_map(d, 1, data_addr);
518 fw_cfg_add_bytes(s, FW_CFG_SIGNATURE, (uint8_t *)"QEMU", 4);
519 fw_cfg_add_bytes(s, FW_CFG_UUID, qemu_uuid, 16);
520 fw_cfg_add_i16(s, FW_CFG_NOGRAPHIC, (uint16_t)(display_type == DT_NOGRAPHIC));
521 fw_cfg_add_i16(s, FW_CFG_NB_CPUS, (uint16_t)smp_cpus);
522 fw_cfg_add_i16(s, FW_CFG_MAX_CPUS, (uint16_t)max_cpus);
523 fw_cfg_add_i16(s, FW_CFG_BOOT_MENU, (uint16_t)boot_menu);
524 fw_cfg_bootsplash(s);
525 fw_cfg_reboot(s);
527 s->machine_ready.notify = fw_cfg_machine_ready;
528 qemu_add_machine_init_done_notifier(&s->machine_ready);
530 return s;
533 static int fw_cfg_init1(SysBusDevice *dev)
535 FWCfgState *s = FROM_SYSBUS(FWCfgState, dev);
537 memory_region_init_io(&s->ctl_iomem, &fw_cfg_ctl_mem_ops, s,
538 "fwcfg.ctl", FW_CFG_SIZE);
539 sysbus_init_mmio(dev, &s->ctl_iomem);
540 memory_region_init_io(&s->data_iomem, &fw_cfg_data_mem_ops, s,
541 "fwcfg.data", FW_CFG_DATA_SIZE);
542 sysbus_init_mmio(dev, &s->data_iomem);
543 /* In case ctl and data overlap: */
544 memory_region_init_io(&s->comb_iomem, &fw_cfg_comb_mem_ops, s,
545 "fwcfg", FW_CFG_SIZE);
547 if (s->ctl_iobase + 1 == s->data_iobase) {
548 sysbus_add_io(dev, s->ctl_iobase, &s->comb_iomem);
549 } else {
550 if (s->ctl_iobase) {
551 sysbus_add_io(dev, s->ctl_iobase, &s->ctl_iomem);
553 if (s->data_iobase) {
554 sysbus_add_io(dev, s->data_iobase, &s->data_iomem);
557 return 0;
560 static Property fw_cfg_properties[] = {
561 DEFINE_PROP_HEX32("ctl_iobase", FWCfgState, ctl_iobase, -1),
562 DEFINE_PROP_HEX32("data_iobase", FWCfgState, data_iobase, -1),
563 DEFINE_PROP_END_OF_LIST(),
566 static void fw_cfg_class_init(ObjectClass *klass, void *data)
568 DeviceClass *dc = DEVICE_CLASS(klass);
569 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
571 k->init = fw_cfg_init1;
572 dc->no_user = 1;
573 dc->reset = fw_cfg_reset;
574 dc->vmsd = &vmstate_fw_cfg;
575 dc->props = fw_cfg_properties;
578 static TypeInfo fw_cfg_info = {
579 .name = "fw_cfg",
580 .parent = TYPE_SYS_BUS_DEVICE,
581 .instance_size = sizeof(FWCfgState),
582 .class_init = fw_cfg_class_init,
585 static void fw_cfg_register_types(void)
587 type_register_static(&fw_cfg_info);
590 type_init(fw_cfg_register_types)