xref: /qemu/hw/nvram/fw_cfg.c (revision 67cc32eb)
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_NAME "fw_cfg"
35 #define FW_CFG_PATH "/machine/" FW_CFG_NAME
36 
37 #define TYPE_FW_CFG     "fw_cfg"
38 #define TYPE_FW_CFG_IO  "fw_cfg_io"
39 #define TYPE_FW_CFG_MEM "fw_cfg_mem"
40 
41 #define FW_CFG(obj)     OBJECT_CHECK(FWCfgState,    (obj), TYPE_FW_CFG)
42 #define FW_CFG_IO(obj)  OBJECT_CHECK(FWCfgIoState,  (obj), TYPE_FW_CFG_IO)
43 #define FW_CFG_MEM(obj) OBJECT_CHECK(FWCfgMemState, (obj), TYPE_FW_CFG_MEM)
44 
45 typedef struct FWCfgEntry {
46     uint32_t len;
47     uint8_t *data;
48     void *callback_opaque;
49     FWCfgReadCallback read_callback;
50 } FWCfgEntry;
51 
52 struct FWCfgState {
53     /*< private >*/
54     SysBusDevice parent_obj;
55     /*< public >*/
56 
57     FWCfgEntry entries[2][FW_CFG_MAX_ENTRY];
58     FWCfgFiles *files;
59     uint16_t cur_entry;
60     uint32_t cur_offset;
61     Notifier machine_ready;
62 };
63 
64 struct FWCfgIoState {
65     /*< private >*/
66     FWCfgState parent_obj;
67     /*< public >*/
68 
69     MemoryRegion comb_iomem;
70     uint32_t iobase;
71 };
72 
73 struct FWCfgMemState {
74     /*< private >*/
75     FWCfgState parent_obj;
76     /*< public >*/
77 
78     MemoryRegion ctl_iomem, data_iomem;
79     uint32_t data_width;
80     MemoryRegionOps wide_data_ops;
81 };
82 
83 #define JPG_FILE 0
84 #define BMP_FILE 1
85 
86 static char *read_splashfile(char *filename, gsize *file_sizep,
87                              int *file_typep)
88 {
89     GError *err = NULL;
90     gboolean res;
91     gchar *content;
92     int file_type;
93     unsigned int filehead;
94     int bmp_bpp;
95 
96     res = g_file_get_contents(filename, &content, file_sizep, &err);
97     if (res == FALSE) {
98         error_report("failed to read splash file '%s'", filename);
99         g_error_free(err);
100         return NULL;
101     }
102 
103     /* check file size */
104     if (*file_sizep < 30) {
105         goto error;
106     }
107 
108     /* check magic ID */
109     filehead = ((content[0] & 0xff) + (content[1] << 8)) & 0xffff;
110     if (filehead == 0xd8ff) {
111         file_type = JPG_FILE;
112     } else if (filehead == 0x4d42) {
113         file_type = BMP_FILE;
114     } else {
115         goto error;
116     }
117 
118     /* check BMP bpp */
119     if (file_type == BMP_FILE) {
120         bmp_bpp = (content[28] + (content[29] << 8)) & 0xffff;
121         if (bmp_bpp != 24) {
122             goto error;
123         }
124     }
125 
126     /* return values */
127     *file_typep = file_type;
128 
129     return content;
130 
131 error:
132     error_report("splash file '%s' format not recognized; must be JPEG "
133                  "or 24 bit BMP", filename);
134     g_free(content);
135     return NULL;
136 }
137 
138 static void fw_cfg_bootsplash(FWCfgState *s)
139 {
140     int boot_splash_time = -1;
141     const char *boot_splash_filename = NULL;
142     char *p;
143     char *filename, *file_data;
144     gsize file_size;
145     int file_type;
146     const char *temp;
147 
148     /* get user configuration */
149     QemuOptsList *plist = qemu_find_opts("boot-opts");
150     QemuOpts *opts = QTAILQ_FIRST(&plist->head);
151     if (opts != NULL) {
152         temp = qemu_opt_get(opts, "splash");
153         if (temp != NULL) {
154             boot_splash_filename = temp;
155         }
156         temp = qemu_opt_get(opts, "splash-time");
157         if (temp != NULL) {
158             p = (char *)temp;
159             boot_splash_time = strtol(p, (char **)&p, 10);
160         }
161     }
162 
163     /* insert splash time if user configurated */
164     if (boot_splash_time >= 0) {
165         /* validate the input */
166         if (boot_splash_time > 0xffff) {
167             error_report("splash time is big than 65535, force it to 65535.");
168             boot_splash_time = 0xffff;
169         }
170         /* use little endian format */
171         qemu_extra_params_fw[0] = (uint8_t)(boot_splash_time & 0xff);
172         qemu_extra_params_fw[1] = (uint8_t)((boot_splash_time >> 8) & 0xff);
173         fw_cfg_add_file(s, "etc/boot-menu-wait", qemu_extra_params_fw, 2);
174     }
175 
176     /* insert splash file if user configurated */
177     if (boot_splash_filename != NULL) {
178         filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, boot_splash_filename);
179         if (filename == NULL) {
180             error_report("failed to find file '%s'.", boot_splash_filename);
181             return;
182         }
183 
184         /* loading file data */
185         file_data = read_splashfile(filename, &file_size, &file_type);
186         if (file_data == NULL) {
187             g_free(filename);
188             return;
189         }
190         g_free(boot_splash_filedata);
191         boot_splash_filedata = (uint8_t *)file_data;
192         boot_splash_filedata_size = file_size;
193 
194         /* insert data */
195         if (file_type == JPG_FILE) {
196             fw_cfg_add_file(s, "bootsplash.jpg",
197                     boot_splash_filedata, boot_splash_filedata_size);
198         } else {
199             fw_cfg_add_file(s, "bootsplash.bmp",
200                     boot_splash_filedata, boot_splash_filedata_size);
201         }
202         g_free(filename);
203     }
204 }
205 
206 static void fw_cfg_reboot(FWCfgState *s)
207 {
208     int reboot_timeout = -1;
209     char *p;
210     const char *temp;
211 
212     /* get user configuration */
213     QemuOptsList *plist = qemu_find_opts("boot-opts");
214     QemuOpts *opts = QTAILQ_FIRST(&plist->head);
215     if (opts != NULL) {
216         temp = qemu_opt_get(opts, "reboot-timeout");
217         if (temp != NULL) {
218             p = (char *)temp;
219             reboot_timeout = strtol(p, (char **)&p, 10);
220         }
221     }
222     /* validate the input */
223     if (reboot_timeout > 0xffff) {
224         error_report("reboot timeout is larger than 65535, force it to 65535.");
225         reboot_timeout = 0xffff;
226     }
227     fw_cfg_add_file(s, "etc/boot-fail-wait", g_memdup(&reboot_timeout, 4), 4);
228 }
229 
230 static void fw_cfg_write(FWCfgState *s, uint8_t value)
231 {
232     /* nothing, write support removed in QEMU v2.4+ */
233 }
234 
235 static int fw_cfg_select(FWCfgState *s, uint16_t key)
236 {
237     int ret;
238 
239     s->cur_offset = 0;
240     if ((key & FW_CFG_ENTRY_MASK) >= FW_CFG_MAX_ENTRY) {
241         s->cur_entry = FW_CFG_INVALID;
242         ret = 0;
243     } else {
244         s->cur_entry = key;
245         ret = 1;
246     }
247 
248     trace_fw_cfg_select(s, key, ret);
249     return ret;
250 }
251 
252 static uint8_t fw_cfg_read(FWCfgState *s)
253 {
254     int arch = !!(s->cur_entry & FW_CFG_ARCH_LOCAL);
255     FWCfgEntry *e = &s->entries[arch][s->cur_entry & FW_CFG_ENTRY_MASK];
256     uint8_t ret;
257 
258     if (s->cur_entry == FW_CFG_INVALID || !e->data || s->cur_offset >= e->len)
259         ret = 0;
260     else {
261         if (e->read_callback) {
262             e->read_callback(e->callback_opaque, s->cur_offset);
263         }
264         ret = e->data[s->cur_offset++];
265     }
266 
267     trace_fw_cfg_read(s, ret);
268     return ret;
269 }
270 
271 static uint64_t fw_cfg_data_mem_read(void *opaque, hwaddr addr,
272                                      unsigned size)
273 {
274     FWCfgState *s = opaque;
275     uint64_t value = 0;
276     unsigned i;
277 
278     for (i = 0; i < size; ++i) {
279         value = (value << 8) | fw_cfg_read(s);
280     }
281     return value;
282 }
283 
284 static void fw_cfg_data_mem_write(void *opaque, hwaddr addr,
285                                   uint64_t value, unsigned size)
286 {
287     FWCfgState *s = opaque;
288     unsigned i = size;
289 
290     do {
291         fw_cfg_write(s, value >> (8 * --i));
292     } while (i);
293 }
294 
295 static bool fw_cfg_data_mem_valid(void *opaque, hwaddr addr,
296                                   unsigned size, bool is_write)
297 {
298     return addr == 0;
299 }
300 
301 static void fw_cfg_ctl_mem_write(void *opaque, hwaddr addr,
302                                  uint64_t value, unsigned size)
303 {
304     fw_cfg_select(opaque, (uint16_t)value);
305 }
306 
307 static bool fw_cfg_ctl_mem_valid(void *opaque, hwaddr addr,
308                                  unsigned size, bool is_write)
309 {
310     return is_write && size == 2;
311 }
312 
313 static uint64_t fw_cfg_comb_read(void *opaque, hwaddr addr,
314                                  unsigned size)
315 {
316     return fw_cfg_read(opaque);
317 }
318 
319 static void fw_cfg_comb_write(void *opaque, hwaddr addr,
320                               uint64_t value, unsigned size)
321 {
322     switch (size) {
323     case 1:
324         fw_cfg_write(opaque, (uint8_t)value);
325         break;
326     case 2:
327         fw_cfg_select(opaque, (uint16_t)value);
328         break;
329     }
330 }
331 
332 static bool fw_cfg_comb_valid(void *opaque, hwaddr addr,
333                                   unsigned size, bool is_write)
334 {
335     return (size == 1) || (is_write && size == 2);
336 }
337 
338 static const MemoryRegionOps fw_cfg_ctl_mem_ops = {
339     .write = fw_cfg_ctl_mem_write,
340     .endianness = DEVICE_BIG_ENDIAN,
341     .valid.accepts = fw_cfg_ctl_mem_valid,
342 };
343 
344 static const MemoryRegionOps fw_cfg_data_mem_ops = {
345     .read = fw_cfg_data_mem_read,
346     .write = fw_cfg_data_mem_write,
347     .endianness = DEVICE_BIG_ENDIAN,
348     .valid = {
349         .min_access_size = 1,
350         .max_access_size = 1,
351         .accepts = fw_cfg_data_mem_valid,
352     },
353 };
354 
355 static const MemoryRegionOps fw_cfg_comb_mem_ops = {
356     .read = fw_cfg_comb_read,
357     .write = fw_cfg_comb_write,
358     .endianness = DEVICE_LITTLE_ENDIAN,
359     .valid.accepts = fw_cfg_comb_valid,
360 };
361 
362 static void fw_cfg_reset(DeviceState *d)
363 {
364     FWCfgState *s = FW_CFG(d);
365 
366     fw_cfg_select(s, 0);
367 }
368 
369 /* Save restore 32 bit int as uint16_t
370    This is a Big hack, but it is how the old state did it.
371    Or we broke compatibility in the state, or we can't use struct tm
372  */
373 
374 static int get_uint32_as_uint16(QEMUFile *f, void *pv, size_t size)
375 {
376     uint32_t *v = pv;
377     *v = qemu_get_be16(f);
378     return 0;
379 }
380 
381 static void put_unused(QEMUFile *f, void *pv, size_t size)
382 {
383     fprintf(stderr, "uint32_as_uint16 is only used for backward compatibility.\n");
384     fprintf(stderr, "This functions shouldn't be called.\n");
385 }
386 
387 static const VMStateInfo vmstate_hack_uint32_as_uint16 = {
388     .name = "int32_as_uint16",
389     .get  = get_uint32_as_uint16,
390     .put  = put_unused,
391 };
392 
393 #define VMSTATE_UINT16_HACK(_f, _s, _t)                                    \
394     VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_hack_uint32_as_uint16, uint32_t)
395 
396 
397 static bool is_version_1(void *opaque, int version_id)
398 {
399     return version_id == 1;
400 }
401 
402 static const VMStateDescription vmstate_fw_cfg = {
403     .name = "fw_cfg",
404     .version_id = 2,
405     .minimum_version_id = 1,
406     .fields = (VMStateField[]) {
407         VMSTATE_UINT16(cur_entry, FWCfgState),
408         VMSTATE_UINT16_HACK(cur_offset, FWCfgState, is_version_1),
409         VMSTATE_UINT32_V(cur_offset, FWCfgState, 2),
410         VMSTATE_END_OF_LIST()
411     }
412 };
413 
414 static void fw_cfg_add_bytes_read_callback(FWCfgState *s, uint16_t key,
415                                            FWCfgReadCallback callback,
416                                            void *callback_opaque,
417                                            void *data, size_t len)
418 {
419     int arch = !!(key & FW_CFG_ARCH_LOCAL);
420 
421     key &= FW_CFG_ENTRY_MASK;
422 
423     assert(key < FW_CFG_MAX_ENTRY && len < UINT32_MAX);
424     assert(s->entries[arch][key].data == NULL); /* avoid key conflict */
425 
426     s->entries[arch][key].data = data;
427     s->entries[arch][key].len = (uint32_t)len;
428     s->entries[arch][key].read_callback = callback;
429     s->entries[arch][key].callback_opaque = callback_opaque;
430 }
431 
432 static void *fw_cfg_modify_bytes_read(FWCfgState *s, uint16_t key,
433                                               void *data, size_t len)
434 {
435     void *ptr;
436     int arch = !!(key & FW_CFG_ARCH_LOCAL);
437 
438     key &= FW_CFG_ENTRY_MASK;
439 
440     assert(key < FW_CFG_MAX_ENTRY && len < UINT32_MAX);
441 
442     /* return the old data to the function caller, avoid memory leak */
443     ptr = s->entries[arch][key].data;
444     s->entries[arch][key].data = data;
445     s->entries[arch][key].len = len;
446     s->entries[arch][key].callback_opaque = NULL;
447 
448     return ptr;
449 }
450 
451 void fw_cfg_add_bytes(FWCfgState *s, uint16_t key, void *data, size_t len)
452 {
453     fw_cfg_add_bytes_read_callback(s, key, NULL, NULL, data, len);
454 }
455 
456 void fw_cfg_add_string(FWCfgState *s, uint16_t key, const char *value)
457 {
458     size_t sz = strlen(value) + 1;
459 
460     fw_cfg_add_bytes(s, key, g_memdup(value, sz), sz);
461 }
462 
463 void fw_cfg_add_i16(FWCfgState *s, uint16_t key, uint16_t value)
464 {
465     uint16_t *copy;
466 
467     copy = g_malloc(sizeof(value));
468     *copy = cpu_to_le16(value);
469     fw_cfg_add_bytes(s, key, copy, sizeof(value));
470 }
471 
472 void fw_cfg_modify_i16(FWCfgState *s, uint16_t key, uint16_t value)
473 {
474     uint16_t *copy, *old;
475 
476     copy = g_malloc(sizeof(value));
477     *copy = cpu_to_le16(value);
478     old = fw_cfg_modify_bytes_read(s, key, copy, sizeof(value));
479     g_free(old);
480 }
481 
482 void fw_cfg_add_i32(FWCfgState *s, uint16_t key, uint32_t value)
483 {
484     uint32_t *copy;
485 
486     copy = g_malloc(sizeof(value));
487     *copy = cpu_to_le32(value);
488     fw_cfg_add_bytes(s, key, copy, sizeof(value));
489 }
490 
491 void fw_cfg_add_i64(FWCfgState *s, uint16_t key, uint64_t value)
492 {
493     uint64_t *copy;
494 
495     copy = g_malloc(sizeof(value));
496     *copy = cpu_to_le64(value);
497     fw_cfg_add_bytes(s, key, copy, sizeof(value));
498 }
499 
500 void fw_cfg_add_file_callback(FWCfgState *s,  const char *filename,
501                               FWCfgReadCallback callback, void *callback_opaque,
502                               void *data, size_t len)
503 {
504     int i, index;
505     size_t dsize;
506 
507     if (!s->files) {
508         dsize = sizeof(uint32_t) + sizeof(FWCfgFile) * FW_CFG_FILE_SLOTS;
509         s->files = g_malloc0(dsize);
510         fw_cfg_add_bytes(s, FW_CFG_FILE_DIR, s->files, dsize);
511     }
512 
513     index = be32_to_cpu(s->files->count);
514     assert(index < FW_CFG_FILE_SLOTS);
515 
516     pstrcpy(s->files->f[index].name, sizeof(s->files->f[index].name),
517             filename);
518     for (i = 0; i < index; i++) {
519         if (strcmp(s->files->f[index].name, s->files->f[i].name) == 0) {
520             error_report("duplicate fw_cfg file name: %s",
521                          s->files->f[index].name);
522             exit(1);
523         }
524     }
525 
526     fw_cfg_add_bytes_read_callback(s, FW_CFG_FILE_FIRST + index,
527                                    callback, callback_opaque, data, len);
528 
529     s->files->f[index].size   = cpu_to_be32(len);
530     s->files->f[index].select = cpu_to_be16(FW_CFG_FILE_FIRST + index);
531     trace_fw_cfg_add_file(s, index, s->files->f[index].name, len);
532 
533     s->files->count = cpu_to_be32(index+1);
534 }
535 
536 void fw_cfg_add_file(FWCfgState *s,  const char *filename,
537                      void *data, size_t len)
538 {
539     fw_cfg_add_file_callback(s, filename, NULL, NULL, data, len);
540 }
541 
542 void *fw_cfg_modify_file(FWCfgState *s, const char *filename,
543                         void *data, size_t len)
544 {
545     int i, index;
546     void *ptr = NULL;
547 
548     assert(s->files);
549 
550     index = be32_to_cpu(s->files->count);
551     assert(index < FW_CFG_FILE_SLOTS);
552 
553     for (i = 0; i < index; i++) {
554         if (strcmp(filename, s->files->f[i].name) == 0) {
555             ptr = fw_cfg_modify_bytes_read(s, FW_CFG_FILE_FIRST + i,
556                                            data, len);
557             s->files->f[i].size   = cpu_to_be32(len);
558             return ptr;
559         }
560     }
561     /* add new one */
562     fw_cfg_add_file_callback(s, filename, NULL, NULL, data, len);
563     return NULL;
564 }
565 
566 static void fw_cfg_machine_reset(void *opaque)
567 {
568     void *ptr;
569     size_t len;
570     FWCfgState *s = opaque;
571     char *bootindex = get_boot_devices_list(&len, false);
572 
573     ptr = fw_cfg_modify_file(s, "bootorder", (uint8_t *)bootindex, len);
574     g_free(ptr);
575 }
576 
577 static void fw_cfg_machine_ready(struct Notifier *n, void *data)
578 {
579     FWCfgState *s = container_of(n, FWCfgState, machine_ready);
580     qemu_register_reset(fw_cfg_machine_reset, s);
581 }
582 
583 
584 
585 static void fw_cfg_init1(DeviceState *dev)
586 {
587     FWCfgState *s = FW_CFG(dev);
588 
589     assert(!object_resolve_path(FW_CFG_PATH, NULL));
590 
591     object_property_add_child(qdev_get_machine(), FW_CFG_NAME, OBJECT(s), NULL);
592 
593     qdev_init_nofail(dev);
594 
595     fw_cfg_add_bytes(s, FW_CFG_SIGNATURE, (char *)"QEMU", 4);
596     fw_cfg_add_i32(s, FW_CFG_ID, 1);
597     fw_cfg_add_bytes(s, FW_CFG_UUID, qemu_uuid, 16);
598     fw_cfg_add_i16(s, FW_CFG_NOGRAPHIC, (uint16_t)(display_type == DT_NOGRAPHIC));
599     fw_cfg_add_i16(s, FW_CFG_NB_CPUS, (uint16_t)smp_cpus);
600     fw_cfg_add_i16(s, FW_CFG_BOOT_MENU, (uint16_t)boot_menu);
601     fw_cfg_bootsplash(s);
602     fw_cfg_reboot(s);
603 
604     s->machine_ready.notify = fw_cfg_machine_ready;
605     qemu_add_machine_init_done_notifier(&s->machine_ready);
606 }
607 
608 FWCfgState *fw_cfg_init_io(uint32_t iobase)
609 {
610     DeviceState *dev;
611 
612     dev = qdev_create(NULL, TYPE_FW_CFG_IO);
613     qdev_prop_set_uint32(dev, "iobase", iobase);
614     fw_cfg_init1(dev);
615 
616     return FW_CFG(dev);
617 }
618 
619 FWCfgState *fw_cfg_init_mem_wide(hwaddr ctl_addr, hwaddr data_addr,
620                                  uint32_t data_width)
621 {
622     DeviceState *dev;
623     SysBusDevice *sbd;
624 
625     dev = qdev_create(NULL, TYPE_FW_CFG_MEM);
626     qdev_prop_set_uint32(dev, "data_width", data_width);
627 
628     fw_cfg_init1(dev);
629 
630     sbd = SYS_BUS_DEVICE(dev);
631     sysbus_mmio_map(sbd, 0, ctl_addr);
632     sysbus_mmio_map(sbd, 1, data_addr);
633 
634     return FW_CFG(dev);
635 }
636 
637 FWCfgState *fw_cfg_init_mem(hwaddr ctl_addr, hwaddr data_addr)
638 {
639     return fw_cfg_init_mem_wide(ctl_addr, data_addr,
640                                 fw_cfg_data_mem_ops.valid.max_access_size);
641 }
642 
643 
644 FWCfgState *fw_cfg_find(void)
645 {
646     return FW_CFG(object_resolve_path(FW_CFG_PATH, NULL));
647 }
648 
649 static void fw_cfg_class_init(ObjectClass *klass, void *data)
650 {
651     DeviceClass *dc = DEVICE_CLASS(klass);
652 
653     dc->reset = fw_cfg_reset;
654     dc->vmsd = &vmstate_fw_cfg;
655 }
656 
657 static const TypeInfo fw_cfg_info = {
658     .name          = TYPE_FW_CFG,
659     .parent        = TYPE_SYS_BUS_DEVICE,
660     .instance_size = sizeof(FWCfgState),
661     .class_init    = fw_cfg_class_init,
662 };
663 
664 
665 static Property fw_cfg_io_properties[] = {
666     DEFINE_PROP_UINT32("iobase", FWCfgIoState, iobase, -1),
667     DEFINE_PROP_END_OF_LIST(),
668 };
669 
670 static void fw_cfg_io_realize(DeviceState *dev, Error **errp)
671 {
672     FWCfgIoState *s = FW_CFG_IO(dev);
673     SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
674 
675     memory_region_init_io(&s->comb_iomem, OBJECT(s), &fw_cfg_comb_mem_ops,
676                           FW_CFG(s), "fwcfg", FW_CFG_SIZE);
677     sysbus_add_io(sbd, s->iobase, &s->comb_iomem);
678 }
679 
680 static void fw_cfg_io_class_init(ObjectClass *klass, void *data)
681 {
682     DeviceClass *dc = DEVICE_CLASS(klass);
683 
684     dc->realize = fw_cfg_io_realize;
685     dc->props = fw_cfg_io_properties;
686 }
687 
688 static const TypeInfo fw_cfg_io_info = {
689     .name          = TYPE_FW_CFG_IO,
690     .parent        = TYPE_FW_CFG,
691     .instance_size = sizeof(FWCfgIoState),
692     .class_init    = fw_cfg_io_class_init,
693 };
694 
695 
696 static Property fw_cfg_mem_properties[] = {
697     DEFINE_PROP_UINT32("data_width", FWCfgMemState, data_width, -1),
698     DEFINE_PROP_END_OF_LIST(),
699 };
700 
701 static void fw_cfg_mem_realize(DeviceState *dev, Error **errp)
702 {
703     FWCfgMemState *s = FW_CFG_MEM(dev);
704     SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
705     const MemoryRegionOps *data_ops = &fw_cfg_data_mem_ops;
706 
707     memory_region_init_io(&s->ctl_iomem, OBJECT(s), &fw_cfg_ctl_mem_ops,
708                           FW_CFG(s), "fwcfg.ctl", FW_CFG_SIZE);
709     sysbus_init_mmio(sbd, &s->ctl_iomem);
710 
711     if (s->data_width > data_ops->valid.max_access_size) {
712         /* memberwise copy because the "old_mmio" member is const */
713         s->wide_data_ops.read       = data_ops->read;
714         s->wide_data_ops.write      = data_ops->write;
715         s->wide_data_ops.endianness = data_ops->endianness;
716         s->wide_data_ops.valid      = data_ops->valid;
717         s->wide_data_ops.impl       = data_ops->impl;
718 
719         s->wide_data_ops.valid.max_access_size = s->data_width;
720         s->wide_data_ops.impl.max_access_size  = s->data_width;
721         data_ops = &s->wide_data_ops;
722     }
723     memory_region_init_io(&s->data_iomem, OBJECT(s), data_ops, FW_CFG(s),
724                           "fwcfg.data", data_ops->valid.max_access_size);
725     sysbus_init_mmio(sbd, &s->data_iomem);
726 }
727 
728 static void fw_cfg_mem_class_init(ObjectClass *klass, void *data)
729 {
730     DeviceClass *dc = DEVICE_CLASS(klass);
731 
732     dc->realize = fw_cfg_mem_realize;
733     dc->props = fw_cfg_mem_properties;
734 }
735 
736 static const TypeInfo fw_cfg_mem_info = {
737     .name          = TYPE_FW_CFG_MEM,
738     .parent        = TYPE_FW_CFG,
739     .instance_size = sizeof(FWCfgMemState),
740     .class_init    = fw_cfg_mem_class_init,
741 };
742 
743 
744 static void fw_cfg_register_types(void)
745 {
746     type_register_static(&fw_cfg_info);
747     type_register_static(&fw_cfg_io_info);
748     type_register_static(&fw_cfg_mem_info);
749 }
750 
751 type_init(fw_cfg_register_types)
752