xref: /openbmc/qemu/hw/nvram/fw_cfg.c (revision 52f91c37)
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     .minimum_version_id_old = 1,
381     .fields      = (VMStateField []) {
382         VMSTATE_UINT16(cur_entry, FWCfgState),
383         VMSTATE_UINT16_HACK(cur_offset, FWCfgState, is_version_1),
384         VMSTATE_UINT32_V(cur_offset, FWCfgState, 2),
385         VMSTATE_END_OF_LIST()
386     }
387 };
388 
389 static void fw_cfg_add_bytes_read_callback(FWCfgState *s, uint16_t key,
390                                            FWCfgReadCallback callback,
391                                            void *callback_opaque,
392                                            void *data, size_t len)
393 {
394     int arch = !!(key & FW_CFG_ARCH_LOCAL);
395 
396     key &= FW_CFG_ENTRY_MASK;
397 
398     assert(key < FW_CFG_MAX_ENTRY && len < UINT32_MAX);
399 
400     s->entries[arch][key].data = data;
401     s->entries[arch][key].len = (uint32_t)len;
402     s->entries[arch][key].read_callback = callback;
403     s->entries[arch][key].callback_opaque = callback_opaque;
404 }
405 
406 void fw_cfg_add_bytes(FWCfgState *s, uint16_t key, void *data, size_t len)
407 {
408     fw_cfg_add_bytes_read_callback(s, key, NULL, NULL, data, len);
409 }
410 
411 void fw_cfg_add_string(FWCfgState *s, uint16_t key, const char *value)
412 {
413     size_t sz = strlen(value) + 1;
414 
415     return fw_cfg_add_bytes(s, key, g_memdup(value, sz), sz);
416 }
417 
418 void fw_cfg_add_i16(FWCfgState *s, uint16_t key, uint16_t value)
419 {
420     uint16_t *copy;
421 
422     copy = g_malloc(sizeof(value));
423     *copy = cpu_to_le16(value);
424     fw_cfg_add_bytes(s, key, copy, sizeof(value));
425 }
426 
427 void fw_cfg_add_i32(FWCfgState *s, uint16_t key, uint32_t value)
428 {
429     uint32_t *copy;
430 
431     copy = g_malloc(sizeof(value));
432     *copy = cpu_to_le32(value);
433     fw_cfg_add_bytes(s, key, copy, sizeof(value));
434 }
435 
436 void fw_cfg_add_i64(FWCfgState *s, uint16_t key, uint64_t value)
437 {
438     uint64_t *copy;
439 
440     copy = g_malloc(sizeof(value));
441     *copy = cpu_to_le64(value);
442     fw_cfg_add_bytes(s, key, copy, sizeof(value));
443 }
444 
445 void fw_cfg_add_callback(FWCfgState *s, uint16_t key, FWCfgCallback callback,
446                          void *callback_opaque, void *data, size_t len)
447 {
448     int arch = !!(key & FW_CFG_ARCH_LOCAL);
449 
450     assert(key & FW_CFG_WRITE_CHANNEL);
451 
452     key &= FW_CFG_ENTRY_MASK;
453 
454     assert(key < FW_CFG_MAX_ENTRY && len <= UINT32_MAX);
455 
456     s->entries[arch][key].data = data;
457     s->entries[arch][key].len = (uint32_t)len;
458     s->entries[arch][key].callback_opaque = callback_opaque;
459     s->entries[arch][key].callback = callback;
460 }
461 
462 void fw_cfg_add_file_callback(FWCfgState *s,  const char *filename,
463                               FWCfgReadCallback callback, void *callback_opaque,
464                               void *data, size_t len)
465 {
466     int i, index;
467     size_t dsize;
468 
469     if (!s->files) {
470         dsize = sizeof(uint32_t) + sizeof(FWCfgFile) * FW_CFG_FILE_SLOTS;
471         s->files = g_malloc0(dsize);
472         fw_cfg_add_bytes(s, FW_CFG_FILE_DIR, s->files, dsize);
473     }
474 
475     index = be32_to_cpu(s->files->count);
476     assert(index < FW_CFG_FILE_SLOTS);
477 
478     fw_cfg_add_bytes_read_callback(s, FW_CFG_FILE_FIRST + index,
479                                    callback, callback_opaque, data, len);
480 
481     pstrcpy(s->files->f[index].name, sizeof(s->files->f[index].name),
482             filename);
483     for (i = 0; i < index; i++) {
484         if (strcmp(s->files->f[index].name, s->files->f[i].name) == 0) {
485             trace_fw_cfg_add_file_dupe(s, s->files->f[index].name);
486             return;
487         }
488     }
489 
490     s->files->f[index].size   = cpu_to_be32(len);
491     s->files->f[index].select = cpu_to_be16(FW_CFG_FILE_FIRST + index);
492     trace_fw_cfg_add_file(s, index, s->files->f[index].name, len);
493 
494     s->files->count = cpu_to_be32(index+1);
495 }
496 
497 void fw_cfg_add_file(FWCfgState *s,  const char *filename,
498                      void *data, size_t len)
499 {
500     fw_cfg_add_file_callback(s, filename, NULL, NULL, data, len);
501 }
502 
503 static void fw_cfg_machine_ready(struct Notifier *n, void *data)
504 {
505     size_t len;
506     FWCfgState *s = container_of(n, FWCfgState, machine_ready);
507     char *bootindex = get_boot_devices_list(&len, false);
508 
509     fw_cfg_add_file(s, "bootorder", (uint8_t*)bootindex, len);
510 }
511 
512 FWCfgState *fw_cfg_init(uint32_t ctl_port, uint32_t data_port,
513                         hwaddr ctl_addr, hwaddr data_addr)
514 {
515     DeviceState *dev;
516     SysBusDevice *d;
517     FWCfgState *s;
518 
519     dev = qdev_create(NULL, TYPE_FW_CFG);
520     qdev_prop_set_uint32(dev, "ctl_iobase", ctl_port);
521     qdev_prop_set_uint32(dev, "data_iobase", data_port);
522     d = SYS_BUS_DEVICE(dev);
523 
524     s = FW_CFG(dev);
525 
526     assert(!object_resolve_path(FW_CFG_PATH, NULL));
527 
528     object_property_add_child(qdev_get_machine(), FW_CFG_NAME, OBJECT(s), NULL);
529 
530     qdev_init_nofail(dev);
531 
532     if (ctl_addr) {
533         sysbus_mmio_map(d, 0, ctl_addr);
534     }
535     if (data_addr) {
536         sysbus_mmio_map(d, 1, data_addr);
537     }
538     fw_cfg_add_bytes(s, FW_CFG_SIGNATURE, (char *)"QEMU", 4);
539     fw_cfg_add_bytes(s, FW_CFG_UUID, qemu_uuid, 16);
540     fw_cfg_add_i16(s, FW_CFG_NOGRAPHIC, (uint16_t)(display_type == DT_NOGRAPHIC));
541     fw_cfg_add_i16(s, FW_CFG_NB_CPUS, (uint16_t)smp_cpus);
542     fw_cfg_add_i16(s, FW_CFG_BOOT_MENU, (uint16_t)boot_menu);
543     fw_cfg_bootsplash(s);
544     fw_cfg_reboot(s);
545 
546     s->machine_ready.notify = fw_cfg_machine_ready;
547     qemu_add_machine_init_done_notifier(&s->machine_ready);
548 
549     return s;
550 }
551 
552 static void fw_cfg_initfn(Object *obj)
553 {
554     SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
555     FWCfgState *s = FW_CFG(obj);
556 
557     memory_region_init_io(&s->ctl_iomem, OBJECT(s), &fw_cfg_ctl_mem_ops, s,
558                           "fwcfg.ctl", FW_CFG_SIZE);
559     sysbus_init_mmio(sbd, &s->ctl_iomem);
560     memory_region_init_io(&s->data_iomem, OBJECT(s), &fw_cfg_data_mem_ops, s,
561                           "fwcfg.data", FW_CFG_DATA_SIZE);
562     sysbus_init_mmio(sbd, &s->data_iomem);
563     /* In case ctl and data overlap: */
564     memory_region_init_io(&s->comb_iomem, OBJECT(s), &fw_cfg_comb_mem_ops, s,
565                           "fwcfg", FW_CFG_SIZE);
566 }
567 
568 static void fw_cfg_realize(DeviceState *dev, Error **errp)
569 {
570     FWCfgState *s = FW_CFG(dev);
571     SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
572 
573 
574     if (s->ctl_iobase + 1 == s->data_iobase) {
575         sysbus_add_io(sbd, s->ctl_iobase, &s->comb_iomem);
576     } else {
577         if (s->ctl_iobase) {
578             sysbus_add_io(sbd, s->ctl_iobase, &s->ctl_iomem);
579         }
580         if (s->data_iobase) {
581             sysbus_add_io(sbd, s->data_iobase, &s->data_iomem);
582         }
583     }
584 }
585 
586 static Property fw_cfg_properties[] = {
587     DEFINE_PROP_UINT32("ctl_iobase", FWCfgState, ctl_iobase, -1),
588     DEFINE_PROP_UINT32("data_iobase", FWCfgState, data_iobase, -1),
589     DEFINE_PROP_END_OF_LIST(),
590 };
591 
592 FWCfgState *fw_cfg_find(void)
593 {
594     return FW_CFG(object_resolve_path(FW_CFG_PATH, NULL));
595 }
596 
597 static void fw_cfg_class_init(ObjectClass *klass, void *data)
598 {
599     DeviceClass *dc = DEVICE_CLASS(klass);
600 
601     dc->realize = fw_cfg_realize;
602     dc->reset = fw_cfg_reset;
603     dc->vmsd = &vmstate_fw_cfg;
604     dc->props = fw_cfg_properties;
605 }
606 
607 static const TypeInfo fw_cfg_info = {
608     .name          = TYPE_FW_CFG,
609     .parent        = TYPE_SYS_BUS_DEVICE,
610     .instance_size = sizeof(FWCfgState),
611     .instance_init = fw_cfg_initfn,
612     .class_init    = fw_cfg_class_init,
613 };
614 
615 static void fw_cfg_register_types(void)
616 {
617     type_register_static(&fw_cfg_info);
618 }
619 
620 type_init(fw_cfg_register_types)
621