xref: /openbmc/qemu/hw/nvram/fw_cfg.c (revision acb0ef58)
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/hw.h"
25 #include "sysemu/sysemu.h"
26 #include "hw/isa/isa.h"
27 #include "hw/nvram/fw_cfg.h"
28 #include "hw/sysbus.h"
29 #include "trace.h"
30 #include "qemu/error-report.h"
31 #include "qemu/config-file.h"
32 
33 #define FW_CFG_SIZE 2
34 #define FW_CFG_DATA_SIZE 1
35 #define TYPE_FW_CFG "fw_cfg"
36 #define FW_CFG_NAME "fw_cfg"
37 #define FW_CFG_PATH "/machine/" FW_CFG_NAME
38 #define FW_CFG(obj) OBJECT_CHECK(FWCfgState, (obj), TYPE_FW_CFG)
39 
40 typedef struct FWCfgEntry {
41     uint32_t len;
42     uint8_t *data;
43     void *callback_opaque;
44     FWCfgCallback callback;
45     FWCfgReadCallback read_callback;
46 } FWCfgEntry;
47 
48 struct FWCfgState {
49     /*< private >*/
50     SysBusDevice parent_obj;
51     /*< public >*/
52 
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, gsize *file_sizep,
66                              int *file_typep)
67 {
68     GError *err = NULL;
69     gboolean res;
70     gchar *content;
71     int file_type;
72     unsigned int filehead;
73     int bmp_bpp;
74 
75     res = g_file_get_contents(filename, &content, 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;
80     }
81 
82     /* check file size */
83     if (*file_sizep < 30) {
84         goto error;
85     }
86 
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;
95     }
96 
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;
102         }
103     }
104 
105     /* return values */
106     *file_typep = file_type;
107 
108     return content;
109 
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;
115 }
116 
117 static void fw_cfg_bootsplash(FWCfgState *s)
118 {
119     int boot_splash_time = -1;
120     const char *boot_splash_filename = NULL;
121     char *p;
122     char *filename, *file_data;
123     gsize file_size;
124     int file_type;
125     const char *temp;
126 
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;
134         }
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);
139         }
140     }
141 
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;
148         }
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);
153     }
154 
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;
161         }
162 
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;
168         }
169         if (boot_splash_filedata != NULL) {
170             g_free(boot_splash_filedata);
171         }
172         boot_splash_filedata = (uint8_t *)file_data;
173         boot_splash_filedata_size = file_size;
174 
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);
182         }
183         g_free(filename);
184     }
185 }
186 
187 static void fw_cfg_reboot(FWCfgState *s)
188 {
189     int reboot_timeout = -1;
190     char *p;
191     const char *temp;
192 
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);
201         }
202     }
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;
207     }
208     fw_cfg_add_file(s, "etc/boot-fail-wait", g_memdup(&reboot_timeout, 4), 4);
209 }
210 
211 static void fw_cfg_write(FWCfgState *s, uint8_t value)
212 {
213     int arch = !!(s->cur_entry & FW_CFG_ARCH_LOCAL);
214     FWCfgEntry *e = &s->entries[arch][s->cur_entry & FW_CFG_ENTRY_MASK];
215 
216     trace_fw_cfg_write(s, value);
217 
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;
224         }
225     }
226 }
227 
228 static int fw_cfg_select(FWCfgState *s, uint16_t key)
229 {
230     int ret;
231 
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;
239     }
240 
241     trace_fw_cfg_select(s, key, ret);
242     return ret;
243 }
244 
245 static uint8_t fw_cfg_read(FWCfgState *s)
246 {
247     int arch = !!(s->cur_entry & FW_CFG_ARCH_LOCAL);
248     FWCfgEntry *e = &s->entries[arch][s->cur_entry & FW_CFG_ENTRY_MASK];
249     uint8_t ret;
250 
251     if (s->cur_entry == FW_CFG_INVALID || !e->data || s->cur_offset >= e->len)
252         ret = 0;
253     else {
254         if (e->read_callback) {
255             e->read_callback(e->callback_opaque, s->cur_offset);
256         }
257         ret = e->data[s->cur_offset++];
258     }
259 
260     trace_fw_cfg_read(s, ret);
261     return ret;
262 }
263 
264 static uint64_t fw_cfg_data_mem_read(void *opaque, hwaddr addr,
265                                      unsigned size)
266 {
267     return fw_cfg_read(opaque);
268 }
269 
270 static void fw_cfg_data_mem_write(void *opaque, hwaddr addr,
271                                   uint64_t value, unsigned size)
272 {
273     fw_cfg_write(opaque, (uint8_t)value);
274 }
275 
276 static void fw_cfg_ctl_mem_write(void *opaque, hwaddr addr,
277                                  uint64_t value, unsigned size)
278 {
279     fw_cfg_select(opaque, (uint16_t)value);
280 }
281 
282 static bool fw_cfg_ctl_mem_valid(void *opaque, hwaddr addr,
283                                  unsigned size, bool is_write)
284 {
285     return is_write && size == 2;
286 }
287 
288 static uint64_t fw_cfg_comb_read(void *opaque, hwaddr addr,
289                                  unsigned size)
290 {
291     return fw_cfg_read(opaque);
292 }
293 
294 static void fw_cfg_comb_write(void *opaque, hwaddr addr,
295                               uint64_t value, unsigned size)
296 {
297     switch (size) {
298     case 1:
299         fw_cfg_write(opaque, (uint8_t)value);
300         break;
301     case 2:
302         fw_cfg_select(opaque, (uint16_t)value);
303         break;
304     }
305 }
306 
307 static bool fw_cfg_comb_valid(void *opaque, hwaddr addr,
308                                   unsigned size, bool is_write)
309 {
310     return (size == 1) || (is_write && size == 2);
311 }
312 
313 static const MemoryRegionOps fw_cfg_ctl_mem_ops = {
314     .write = fw_cfg_ctl_mem_write,
315     .endianness = DEVICE_NATIVE_ENDIAN,
316     .valid.accepts = fw_cfg_ctl_mem_valid,
317 };
318 
319 static const MemoryRegionOps fw_cfg_data_mem_ops = {
320     .read = fw_cfg_data_mem_read,
321     .write = fw_cfg_data_mem_write,
322     .endianness = DEVICE_NATIVE_ENDIAN,
323     .valid = {
324         .min_access_size = 1,
325         .max_access_size = 1,
326     },
327 };
328 
329 static const MemoryRegionOps fw_cfg_comb_mem_ops = {
330     .read = fw_cfg_comb_read,
331     .write = fw_cfg_comb_write,
332     .endianness = DEVICE_LITTLE_ENDIAN,
333     .valid.accepts = fw_cfg_comb_valid,
334 };
335 
336 static void fw_cfg_reset(DeviceState *d)
337 {
338     FWCfgState *s = FW_CFG(d);
339 
340     fw_cfg_select(s, 0);
341 }
342 
343 /* Save restore 32 bit int as uint16_t
344    This is a Big hack, but it is how the old state did it.
345    Or we broke compatibility in the state, or we can't use struct tm
346  */
347 
348 static int get_uint32_as_uint16(QEMUFile *f, void *pv, size_t size)
349 {
350     uint32_t *v = pv;
351     *v = qemu_get_be16(f);
352     return 0;
353 }
354 
355 static void put_unused(QEMUFile *f, void *pv, size_t size)
356 {
357     fprintf(stderr, "uint32_as_uint16 is only used for backward compatibility.\n");
358     fprintf(stderr, "This functions shouldn't be called.\n");
359 }
360 
361 static const VMStateInfo vmstate_hack_uint32_as_uint16 = {
362     .name = "int32_as_uint16",
363     .get  = get_uint32_as_uint16,
364     .put  = put_unused,
365 };
366 
367 #define VMSTATE_UINT16_HACK(_f, _s, _t)                                    \
368     VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_hack_uint32_as_uint16, uint32_t)
369 
370 
371 static bool is_version_1(void *opaque, int version_id)
372 {
373     return version_id == 1;
374 }
375 
376 static const VMStateDescription vmstate_fw_cfg = {
377     .name = "fw_cfg",
378     .version_id = 2,
379     .minimum_version_id = 1,
380     .fields = (VMStateField[]) {
381         VMSTATE_UINT16(cur_entry, FWCfgState),
382         VMSTATE_UINT16_HACK(cur_offset, FWCfgState, is_version_1),
383         VMSTATE_UINT32_V(cur_offset, FWCfgState, 2),
384         VMSTATE_END_OF_LIST()
385     }
386 };
387 
388 static void fw_cfg_add_bytes_read_callback(FWCfgState *s, uint16_t key,
389                                            FWCfgReadCallback callback,
390                                            void *callback_opaque,
391                                            void *data, size_t len)
392 {
393     int arch = !!(key & FW_CFG_ARCH_LOCAL);
394 
395     key &= FW_CFG_ENTRY_MASK;
396 
397     assert(key < FW_CFG_MAX_ENTRY && len < UINT32_MAX);
398 
399     s->entries[arch][key].data = data;
400     s->entries[arch][key].len = (uint32_t)len;
401     s->entries[arch][key].read_callback = callback;
402     s->entries[arch][key].callback_opaque = callback_opaque;
403 }
404 
405 void fw_cfg_add_bytes(FWCfgState *s, uint16_t key, void *data, size_t len)
406 {
407     fw_cfg_add_bytes_read_callback(s, key, NULL, NULL, data, len);
408 }
409 
410 void fw_cfg_add_string(FWCfgState *s, uint16_t key, const char *value)
411 {
412     size_t sz = strlen(value) + 1;
413 
414     return fw_cfg_add_bytes(s, key, g_memdup(value, sz), sz);
415 }
416 
417 void fw_cfg_add_i16(FWCfgState *s, uint16_t key, uint16_t value)
418 {
419     uint16_t *copy;
420 
421     copy = g_malloc(sizeof(value));
422     *copy = cpu_to_le16(value);
423     fw_cfg_add_bytes(s, key, copy, sizeof(value));
424 }
425 
426 void fw_cfg_add_i32(FWCfgState *s, uint16_t key, uint32_t value)
427 {
428     uint32_t *copy;
429 
430     copy = g_malloc(sizeof(value));
431     *copy = cpu_to_le32(value);
432     fw_cfg_add_bytes(s, key, copy, sizeof(value));
433 }
434 
435 void fw_cfg_add_i64(FWCfgState *s, uint16_t key, uint64_t value)
436 {
437     uint64_t *copy;
438 
439     copy = g_malloc(sizeof(value));
440     *copy = cpu_to_le64(value);
441     fw_cfg_add_bytes(s, key, copy, sizeof(value));
442 }
443 
444 void fw_cfg_add_callback(FWCfgState *s, uint16_t key, FWCfgCallback callback,
445                          void *callback_opaque, void *data, size_t len)
446 {
447     int arch = !!(key & FW_CFG_ARCH_LOCAL);
448 
449     assert(key & FW_CFG_WRITE_CHANNEL);
450 
451     key &= FW_CFG_ENTRY_MASK;
452 
453     assert(key < FW_CFG_MAX_ENTRY && len <= UINT32_MAX);
454 
455     s->entries[arch][key].data = data;
456     s->entries[arch][key].len = (uint32_t)len;
457     s->entries[arch][key].callback_opaque = callback_opaque;
458     s->entries[arch][key].callback = callback;
459 }
460 
461 void fw_cfg_add_file_callback(FWCfgState *s,  const char *filename,
462                               FWCfgReadCallback callback, void *callback_opaque,
463                               void *data, size_t len)
464 {
465     int i, index;
466     size_t dsize;
467 
468     if (!s->files) {
469         dsize = sizeof(uint32_t) + sizeof(FWCfgFile) * FW_CFG_FILE_SLOTS;
470         s->files = g_malloc0(dsize);
471         fw_cfg_add_bytes(s, FW_CFG_FILE_DIR, s->files, dsize);
472     }
473 
474     index = be32_to_cpu(s->files->count);
475     assert(index < FW_CFG_FILE_SLOTS);
476 
477     fw_cfg_add_bytes_read_callback(s, FW_CFG_FILE_FIRST + index,
478                                    callback, callback_opaque, data, len);
479 
480     pstrcpy(s->files->f[index].name, sizeof(s->files->f[index].name),
481             filename);
482     for (i = 0; i < index; i++) {
483         if (strcmp(s->files->f[index].name, s->files->f[i].name) == 0) {
484             trace_fw_cfg_add_file_dupe(s, s->files->f[index].name);
485             return;
486         }
487     }
488 
489     s->files->f[index].size   = cpu_to_be32(len);
490     s->files->f[index].select = cpu_to_be16(FW_CFG_FILE_FIRST + index);
491     trace_fw_cfg_add_file(s, index, s->files->f[index].name, len);
492 
493     s->files->count = cpu_to_be32(index+1);
494 }
495 
496 void fw_cfg_add_file(FWCfgState *s,  const char *filename,
497                      void *data, size_t len)
498 {
499     fw_cfg_add_file_callback(s, filename, NULL, NULL, data, len);
500 }
501 
502 static void fw_cfg_machine_ready(struct Notifier *n, void *data)
503 {
504     size_t len;
505     FWCfgState *s = container_of(n, FWCfgState, machine_ready);
506     char *bootindex = get_boot_devices_list(&len, false);
507 
508     fw_cfg_add_file(s, "bootorder", (uint8_t*)bootindex, len);
509 }
510 
511 FWCfgState *fw_cfg_init(uint32_t ctl_port, uint32_t data_port,
512                         hwaddr ctl_addr, hwaddr data_addr)
513 {
514     DeviceState *dev;
515     SysBusDevice *d;
516     FWCfgState *s;
517 
518     dev = qdev_create(NULL, TYPE_FW_CFG);
519     qdev_prop_set_uint32(dev, "ctl_iobase", ctl_port);
520     qdev_prop_set_uint32(dev, "data_iobase", data_port);
521     d = SYS_BUS_DEVICE(dev);
522 
523     s = FW_CFG(dev);
524 
525     assert(!object_resolve_path(FW_CFG_PATH, NULL));
526 
527     object_property_add_child(qdev_get_machine(), FW_CFG_NAME, OBJECT(s), NULL);
528 
529     qdev_init_nofail(dev);
530 
531     if (ctl_addr) {
532         sysbus_mmio_map(d, 0, ctl_addr);
533     }
534     if (data_addr) {
535         sysbus_mmio_map(d, 1, data_addr);
536     }
537     fw_cfg_add_bytes(s, FW_CFG_SIGNATURE, (char *)"QEMU", 4);
538     fw_cfg_add_bytes(s, FW_CFG_UUID, qemu_uuid, 16);
539     fw_cfg_add_i16(s, FW_CFG_NOGRAPHIC, (uint16_t)(display_type == DT_NOGRAPHIC));
540     fw_cfg_add_i16(s, FW_CFG_NB_CPUS, (uint16_t)smp_cpus);
541     fw_cfg_add_i16(s, FW_CFG_BOOT_MENU, (uint16_t)boot_menu);
542     fw_cfg_bootsplash(s);
543     fw_cfg_reboot(s);
544 
545     s->machine_ready.notify = fw_cfg_machine_ready;
546     qemu_add_machine_init_done_notifier(&s->machine_ready);
547 
548     return s;
549 }
550 
551 static void fw_cfg_initfn(Object *obj)
552 {
553     SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
554     FWCfgState *s = FW_CFG(obj);
555 
556     memory_region_init_io(&s->ctl_iomem, OBJECT(s), &fw_cfg_ctl_mem_ops, s,
557                           "fwcfg.ctl", FW_CFG_SIZE);
558     sysbus_init_mmio(sbd, &s->ctl_iomem);
559     memory_region_init_io(&s->data_iomem, OBJECT(s), &fw_cfg_data_mem_ops, s,
560                           "fwcfg.data", FW_CFG_DATA_SIZE);
561     sysbus_init_mmio(sbd, &s->data_iomem);
562     /* In case ctl and data overlap: */
563     memory_region_init_io(&s->comb_iomem, OBJECT(s), &fw_cfg_comb_mem_ops, s,
564                           "fwcfg", FW_CFG_SIZE);
565 }
566 
567 static void fw_cfg_realize(DeviceState *dev, Error **errp)
568 {
569     FWCfgState *s = FW_CFG(dev);
570     SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
571 
572 
573     if (s->ctl_iobase + 1 == s->data_iobase) {
574         sysbus_add_io(sbd, s->ctl_iobase, &s->comb_iomem);
575     } else {
576         if (s->ctl_iobase) {
577             sysbus_add_io(sbd, s->ctl_iobase, &s->ctl_iomem);
578         }
579         if (s->data_iobase) {
580             sysbus_add_io(sbd, s->data_iobase, &s->data_iomem);
581         }
582     }
583 }
584 
585 static Property fw_cfg_properties[] = {
586     DEFINE_PROP_UINT32("ctl_iobase", FWCfgState, ctl_iobase, -1),
587     DEFINE_PROP_UINT32("data_iobase", FWCfgState, data_iobase, -1),
588     DEFINE_PROP_END_OF_LIST(),
589 };
590 
591 FWCfgState *fw_cfg_find(void)
592 {
593     return FW_CFG(object_resolve_path(FW_CFG_PATH, NULL));
594 }
595 
596 static void fw_cfg_class_init(ObjectClass *klass, void *data)
597 {
598     DeviceClass *dc = DEVICE_CLASS(klass);
599 
600     dc->realize = fw_cfg_realize;
601     dc->reset = fw_cfg_reset;
602     dc->vmsd = &vmstate_fw_cfg;
603     dc->props = fw_cfg_properties;
604 }
605 
606 static const TypeInfo fw_cfg_info = {
607     .name          = TYPE_FW_CFG,
608     .parent        = TYPE_SYS_BUS_DEVICE,
609     .instance_size = sizeof(FWCfgState),
610     .instance_init = fw_cfg_initfn,
611     .class_init    = fw_cfg_class_init,
612 };
613 
614 static void fw_cfg_register_types(void)
615 {
616     type_register_static(&fw_cfg_info);
617 }
618 
619 type_init(fw_cfg_register_types)
620