]> rtime.felk.cvut.cz Git - lisovros/qemu_apohw.git/blob - hw/fw_cfg.c
f9535328f0b99e1b55720c9ed43e70538e139bfe
[lisovros/qemu_apohw.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 #include "qemu-error.h"
30
31 /* debug firmware config */
32 //#define DEBUG_FW_CFG
33
34 #ifdef DEBUG_FW_CFG
35 #define FW_CFG_DPRINTF(fmt, ...)                        \
36     do { printf("FW_CFG: " fmt , ## __VA_ARGS__); } while (0)
37 #else
38 #define FW_CFG_DPRINTF(fmt, ...)
39 #endif
40
41 #define FW_CFG_SIZE 2
42 #define FW_CFG_DATA_SIZE 1
43
44 typedef struct FWCfgEntry {
45     uint32_t len;
46     uint8_t *data;
47     void *callback_opaque;
48     FWCfgCallback callback;
49 } FWCfgEntry;
50
51 struct FWCfgState {
52     SysBusDevice busdev;
53     MemoryRegion ctl_iomem, data_iomem, comb_iomem;
54     uint32_t ctl_iobase, data_iobase;
55     FWCfgEntry entries[2][FW_CFG_MAX_ENTRY];
56     FWCfgFiles *files;
57     uint16_t cur_entry;
58     uint32_t cur_offset;
59     Notifier machine_ready;
60 };
61
62 #define JPG_FILE 0
63 #define BMP_FILE 1
64
65 static char *read_splashfile(char *filename, int *file_sizep, int *file_typep)
66 {
67     GError *err = NULL;
68     gboolean res;
69     gchar *content;
70     int file_type = -1;
71     unsigned int filehead = 0;
72     int bmp_bpp;
73
74     res = g_file_get_contents(filename, &content, (gsize *)file_sizep, &err);
75     if (res == FALSE) {
76         error_report("failed to read splash file '%s'", filename);
77         g_error_free(err);
78         return NULL;
79     }
80
81     /* check file size */
82     if (*file_sizep < 30) {
83         goto error;
84     }
85
86     /* check magic ID */
87     filehead = ((content[0] & 0xff) + (content[1] << 8)) & 0xffff;
88     if (filehead == 0xd8ff) {
89         file_type = JPG_FILE;
90     } else if (filehead == 0x4d42) {
91         file_type = BMP_FILE;
92     } else {
93         goto error;
94     }
95
96     /* check BMP bpp */
97     if (file_type == BMP_FILE) {
98         bmp_bpp = (content[28] + (content[29] << 8)) & 0xffff;
99         if (bmp_bpp != 24) {
100             goto error;
101         }
102     }
103
104     /* return values */
105     *file_typep = file_type;
106
107     return content;
108
109 error:
110     error_report("splash file '%s' format not recognized; must be JPEG "
111                  "or 24 bit BMP", filename);
112     g_free(content);
113     return NULL;
114 }
115
116 static void fw_cfg_bootsplash(FWCfgState *s)
117 {
118     int boot_splash_time = -1;
119     const char *boot_splash_filename = NULL;
120     char *p;
121     char *filename, *file_data;
122     int file_size;
123     int file_type = -1;
124     const char *temp;
125
126     /* get user configuration */
127     QemuOptsList *plist = qemu_find_opts("boot-opts");
128     QemuOpts *opts = QTAILQ_FIRST(&plist->head);
129     if (opts != NULL) {
130         temp = qemu_opt_get(opts, "splash");
131         if (temp != NULL) {
132             boot_splash_filename = temp;
133         }
134         temp = qemu_opt_get(opts, "splash-time");
135         if (temp != NULL) {
136             p = (char *)temp;
137             boot_splash_time = strtol(p, (char **)&p, 10);
138         }
139     }
140
141     /* insert splash time if user configurated */
142     if (boot_splash_time >= 0) {
143         /* validate the input */
144         if (boot_splash_time > 0xffff) {
145             error_report("splash time is big than 65535, force it to 65535.");
146             boot_splash_time = 0xffff;
147         }
148         /* use little endian format */
149         qemu_extra_params_fw[0] = (uint8_t)(boot_splash_time & 0xff);
150         qemu_extra_params_fw[1] = (uint8_t)((boot_splash_time >> 8) & 0xff);
151         fw_cfg_add_file(s, "etc/boot-menu-wait", qemu_extra_params_fw, 2);
152     }
153
154     /* insert splash file if user configurated */
155     if (boot_splash_filename != NULL) {
156         filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, boot_splash_filename);
157         if (filename == NULL) {
158             error_report("failed to find file '%s'.", boot_splash_filename);
159             return;
160         }
161
162         /* loading file data */
163         file_data = read_splashfile(filename, &file_size, &file_type);
164         if (file_data == NULL) {
165             g_free(filename);
166             return;
167         }
168         if (boot_splash_filedata != NULL) {
169             g_free(boot_splash_filedata);
170         }
171         boot_splash_filedata = (uint8_t *)file_data;
172         boot_splash_filedata_size = file_size;
173
174         /* insert data */
175         if (file_type == JPG_FILE) {
176             fw_cfg_add_file(s, "bootsplash.jpg",
177                     boot_splash_filedata, boot_splash_filedata_size);
178         } else {
179             fw_cfg_add_file(s, "bootsplash.bmp",
180                     boot_splash_filedata, boot_splash_filedata_size);
181         }
182         g_free(filename);
183     }
184 }
185
186 static void fw_cfg_write(FWCfgState *s, uint8_t value)
187 {
188     int arch = !!(s->cur_entry & FW_CFG_ARCH_LOCAL);
189     FWCfgEntry *e = &s->entries[arch][s->cur_entry & FW_CFG_ENTRY_MASK];
190
191     FW_CFG_DPRINTF("write %d\n", value);
192
193     if (s->cur_entry & FW_CFG_WRITE_CHANNEL && e->callback &&
194         s->cur_offset < e->len) {
195         e->data[s->cur_offset++] = value;
196         if (s->cur_offset == e->len) {
197             e->callback(e->callback_opaque, e->data);
198             s->cur_offset = 0;
199         }
200     }
201 }
202
203 static int fw_cfg_select(FWCfgState *s, uint16_t key)
204 {
205     int ret;
206
207     s->cur_offset = 0;
208     if ((key & FW_CFG_ENTRY_MASK) >= FW_CFG_MAX_ENTRY) {
209         s->cur_entry = FW_CFG_INVALID;
210         ret = 0;
211     } else {
212         s->cur_entry = key;
213         ret = 1;
214     }
215
216     FW_CFG_DPRINTF("select key %d (%sfound)\n", key, ret ? "" : "not ");
217
218     return ret;
219 }
220
221 static uint8_t fw_cfg_read(FWCfgState *s)
222 {
223     int arch = !!(s->cur_entry & FW_CFG_ARCH_LOCAL);
224     FWCfgEntry *e = &s->entries[arch][s->cur_entry & FW_CFG_ENTRY_MASK];
225     uint8_t ret;
226
227     if (s->cur_entry == FW_CFG_INVALID || !e->data || s->cur_offset >= e->len)
228         ret = 0;
229     else
230         ret = e->data[s->cur_offset++];
231
232     FW_CFG_DPRINTF("read %d\n", ret);
233
234     return ret;
235 }
236
237 static uint64_t fw_cfg_data_mem_read(void *opaque, target_phys_addr_t addr,
238                                      unsigned size)
239 {
240     return fw_cfg_read(opaque);
241 }
242
243 static void fw_cfg_data_mem_write(void *opaque, target_phys_addr_t addr,
244                                   uint64_t value, unsigned size)
245 {
246     fw_cfg_write(opaque, (uint8_t)value);
247 }
248
249 static void fw_cfg_ctl_mem_write(void *opaque, target_phys_addr_t addr,
250                                  uint64_t value, unsigned size)
251 {
252     fw_cfg_select(opaque, (uint16_t)value);
253 }
254
255 static bool fw_cfg_ctl_mem_valid(void *opaque, target_phys_addr_t addr,
256                                  unsigned size, bool is_write)
257 {
258     return is_write && size == 2;
259 }
260
261 static uint64_t fw_cfg_comb_read(void *opaque, target_phys_addr_t addr,
262                                  unsigned size)
263 {
264     return fw_cfg_read(opaque);
265 }
266
267 static void fw_cfg_comb_write(void *opaque, target_phys_addr_t addr,
268                               uint64_t value, unsigned size)
269 {
270     switch (size) {
271     case 1:
272         fw_cfg_write(opaque, (uint8_t)value);
273         break;
274     case 2:
275         fw_cfg_select(opaque, (uint16_t)value);
276         break;
277     }
278 }
279
280 static bool fw_cfg_comb_valid(void *opaque, target_phys_addr_t addr,
281                                   unsigned size, bool is_write)
282 {
283     return (size == 1) || (is_write && size == 2);
284 }
285
286 static const MemoryRegionOps fw_cfg_ctl_mem_ops = {
287     .write = fw_cfg_ctl_mem_write,
288     .endianness = DEVICE_NATIVE_ENDIAN,
289     .valid.accepts = fw_cfg_ctl_mem_valid,
290 };
291
292 static const MemoryRegionOps fw_cfg_data_mem_ops = {
293     .read = fw_cfg_data_mem_read,
294     .write = fw_cfg_data_mem_write,
295     .endianness = DEVICE_NATIVE_ENDIAN,
296     .valid = {
297         .min_access_size = 1,
298         .max_access_size = 1,
299     },
300 };
301
302 static const MemoryRegionOps fw_cfg_comb_mem_ops = {
303     .read = fw_cfg_comb_read,
304     .write = fw_cfg_comb_write,
305     .endianness = DEVICE_NATIVE_ENDIAN,
306     .valid.accepts = fw_cfg_comb_valid,
307 };
308
309 static void fw_cfg_reset(DeviceState *d)
310 {
311     FWCfgState *s = DO_UPCAST(FWCfgState, busdev.qdev, d);
312
313     fw_cfg_select(s, 0);
314 }
315
316 /* Save restore 32 bit int as uint16_t
317    This is a Big hack, but it is how the old state did it.
318    Or we broke compatibility in the state, or we can't use struct tm
319  */
320
321 static int get_uint32_as_uint16(QEMUFile *f, void *pv, size_t size)
322 {
323     uint32_t *v = pv;
324     *v = qemu_get_be16(f);
325     return 0;
326 }
327
328 static void put_unused(QEMUFile *f, void *pv, size_t size)
329 {
330     fprintf(stderr, "uint32_as_uint16 is only used for backward compatibility.\n");
331     fprintf(stderr, "This functions shouldn't be called.\n");
332 }
333
334 static const VMStateInfo vmstate_hack_uint32_as_uint16 = {
335     .name = "int32_as_uint16",
336     .get  = get_uint32_as_uint16,
337     .put  = put_unused,
338 };
339
340 #define VMSTATE_UINT16_HACK(_f, _s, _t)                                    \
341     VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_hack_uint32_as_uint16, uint32_t)
342
343
344 static bool is_version_1(void *opaque, int version_id)
345 {
346     return version_id == 1;
347 }
348
349 static const VMStateDescription vmstate_fw_cfg = {
350     .name = "fw_cfg",
351     .version_id = 2,
352     .minimum_version_id = 1,
353     .minimum_version_id_old = 1,
354     .fields      = (VMStateField []) {
355         VMSTATE_UINT16(cur_entry, FWCfgState),
356         VMSTATE_UINT16_HACK(cur_offset, FWCfgState, is_version_1),
357         VMSTATE_UINT32_V(cur_offset, FWCfgState, 2),
358         VMSTATE_END_OF_LIST()
359     }
360 };
361
362 int fw_cfg_add_bytes(FWCfgState *s, uint16_t key, uint8_t *data, uint32_t len)
363 {
364     int arch = !!(key & FW_CFG_ARCH_LOCAL);
365
366     key &= FW_CFG_ENTRY_MASK;
367
368     if (key >= FW_CFG_MAX_ENTRY)
369         return 0;
370
371     s->entries[arch][key].data = data;
372     s->entries[arch][key].len = len;
373
374     return 1;
375 }
376
377 int fw_cfg_add_i16(FWCfgState *s, uint16_t key, uint16_t value)
378 {
379     uint16_t *copy;
380
381     copy = g_malloc(sizeof(value));
382     *copy = cpu_to_le16(value);
383     return fw_cfg_add_bytes(s, key, (uint8_t *)copy, sizeof(value));
384 }
385
386 int fw_cfg_add_i32(FWCfgState *s, uint16_t key, uint32_t value)
387 {
388     uint32_t *copy;
389
390     copy = g_malloc(sizeof(value));
391     *copy = cpu_to_le32(value);
392     return fw_cfg_add_bytes(s, key, (uint8_t *)copy, sizeof(value));
393 }
394
395 int fw_cfg_add_i64(FWCfgState *s, uint16_t key, uint64_t value)
396 {
397     uint64_t *copy;
398
399     copy = g_malloc(sizeof(value));
400     *copy = cpu_to_le64(value);
401     return fw_cfg_add_bytes(s, key, (uint8_t *)copy, sizeof(value));
402 }
403
404 int fw_cfg_add_callback(FWCfgState *s, uint16_t key, FWCfgCallback callback,
405                         void *callback_opaque, uint8_t *data, size_t len)
406 {
407     int arch = !!(key & FW_CFG_ARCH_LOCAL);
408
409     if (!(key & FW_CFG_WRITE_CHANNEL))
410         return 0;
411
412     key &= FW_CFG_ENTRY_MASK;
413
414     if (key >= FW_CFG_MAX_ENTRY || len > 65535)
415         return 0;
416
417     s->entries[arch][key].data = data;
418     s->entries[arch][key].len = len;
419     s->entries[arch][key].callback_opaque = callback_opaque;
420     s->entries[arch][key].callback = callback;
421
422     return 1;
423 }
424
425 int fw_cfg_add_file(FWCfgState *s,  const char *filename, uint8_t *data,
426                     uint32_t len)
427 {
428     int i, index;
429
430     if (!s->files) {
431         int dsize = sizeof(uint32_t) + sizeof(FWCfgFile) * FW_CFG_FILE_SLOTS;
432         s->files = g_malloc0(dsize);
433         fw_cfg_add_bytes(s, FW_CFG_FILE_DIR, (uint8_t*)s->files, dsize);
434     }
435
436     index = be32_to_cpu(s->files->count);
437     if (index == FW_CFG_FILE_SLOTS) {
438         fprintf(stderr, "fw_cfg: out of file slots\n");
439         return 0;
440     }
441
442     fw_cfg_add_bytes(s, FW_CFG_FILE_FIRST + index, data, len);
443
444     pstrcpy(s->files->f[index].name, sizeof(s->files->f[index].name),
445             filename);
446     for (i = 0; i < index; i++) {
447         if (strcmp(s->files->f[index].name, s->files->f[i].name) == 0) {
448             FW_CFG_DPRINTF("%s: skip duplicate: %s\n", __FUNCTION__,
449                            s->files->f[index].name);
450             return 1;
451         }
452     }
453
454     s->files->f[index].size   = cpu_to_be32(len);
455     s->files->f[index].select = cpu_to_be16(FW_CFG_FILE_FIRST + index);
456     FW_CFG_DPRINTF("%s: #%d: %s (%d bytes)\n", __FUNCTION__,
457                    index, s->files->f[index].name, len);
458
459     s->files->count = cpu_to_be32(index+1);
460     return 1;
461 }
462
463 static void fw_cfg_machine_ready(struct Notifier *n, void *data)
464 {
465     uint32_t len;
466     FWCfgState *s = container_of(n, FWCfgState, machine_ready);
467     char *bootindex = get_boot_devices_list(&len);
468
469     fw_cfg_add_file(s, "bootorder", (uint8_t*)bootindex, len);
470 }
471
472 FWCfgState *fw_cfg_init(uint32_t ctl_port, uint32_t data_port,
473                         target_phys_addr_t ctl_addr, target_phys_addr_t data_addr)
474 {
475     DeviceState *dev;
476     SysBusDevice *d;
477     FWCfgState *s;
478
479     dev = qdev_create(NULL, "fw_cfg");
480     qdev_prop_set_uint32(dev, "ctl_iobase", ctl_port);
481     qdev_prop_set_uint32(dev, "data_iobase", data_port);
482     qdev_init_nofail(dev);
483     d = sysbus_from_qdev(dev);
484
485     s = DO_UPCAST(FWCfgState, busdev.qdev, dev);
486
487     if (ctl_addr) {
488         sysbus_mmio_map(d, 0, ctl_addr);
489     }
490     if (data_addr) {
491         sysbus_mmio_map(d, 1, data_addr);
492     }
493     fw_cfg_add_bytes(s, FW_CFG_SIGNATURE, (uint8_t *)"QEMU", 4);
494     fw_cfg_add_bytes(s, FW_CFG_UUID, qemu_uuid, 16);
495     fw_cfg_add_i16(s, FW_CFG_NOGRAPHIC, (uint16_t)(display_type == DT_NOGRAPHIC));
496     fw_cfg_add_i16(s, FW_CFG_NB_CPUS, (uint16_t)smp_cpus);
497     fw_cfg_add_i16(s, FW_CFG_MAX_CPUS, (uint16_t)max_cpus);
498     fw_cfg_add_i16(s, FW_CFG_BOOT_MENU, (uint16_t)boot_menu);
499     fw_cfg_bootsplash(s);
500
501     s->machine_ready.notify = fw_cfg_machine_ready;
502     qemu_add_machine_init_done_notifier(&s->machine_ready);
503
504     return s;
505 }
506
507 static int fw_cfg_init1(SysBusDevice *dev)
508 {
509     FWCfgState *s = FROM_SYSBUS(FWCfgState, dev);
510
511     memory_region_init_io(&s->ctl_iomem, &fw_cfg_ctl_mem_ops, s,
512                           "fwcfg.ctl", FW_CFG_SIZE);
513     sysbus_init_mmio(dev, &s->ctl_iomem);
514     memory_region_init_io(&s->data_iomem, &fw_cfg_data_mem_ops, s,
515                           "fwcfg.data", FW_CFG_DATA_SIZE);
516     sysbus_init_mmio(dev, &s->data_iomem);
517     /* In case ctl and data overlap: */
518     memory_region_init_io(&s->comb_iomem, &fw_cfg_comb_mem_ops, s,
519                           "fwcfg", FW_CFG_SIZE);
520
521     if (s->ctl_iobase + 1 == s->data_iobase) {
522         sysbus_add_io(dev, s->ctl_iobase, &s->comb_iomem);
523     } else {
524         if (s->ctl_iobase) {
525             sysbus_add_io(dev, s->ctl_iobase, &s->ctl_iomem);
526         }
527         if (s->data_iobase) {
528             sysbus_add_io(dev, s->data_iobase, &s->data_iomem);
529         }
530     }
531     return 0;
532 }
533
534 static SysBusDeviceInfo fw_cfg_info = {
535     .init = fw_cfg_init1,
536     .qdev.name = "fw_cfg",
537     .qdev.size = sizeof(FWCfgState),
538     .qdev.vmsd = &vmstate_fw_cfg,
539     .qdev.reset = fw_cfg_reset,
540     .qdev.no_user = 1,
541     .qdev.props = (Property[]) {
542         DEFINE_PROP_HEX32("ctl_iobase", FWCfgState, ctl_iobase, -1),
543         DEFINE_PROP_HEX32("data_iobase", FWCfgState, data_iobase, -1),
544         DEFINE_PROP_END_OF_LIST(),
545     },
546 };
547
548 static void fw_cfg_register_devices(void)
549 {
550     sysbus_register_withprop(&fw_cfg_info);
551 }
552
553 device_init(fw_cfg_register_devices)