xref: /openbmc/qemu/hw/nvram/fw_cfg.c (revision 66708822cd3007ae1ec5104d274a861148725e7a)
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 FW_CFG_NAME "fw_cfg"
36 #define FW_CFG_PATH "/machine/" FW_CFG_NAME
37 
38 #define TYPE_FW_CFG     "fw_cfg"
39 #define TYPE_FW_CFG_IO  "fw_cfg_io"
40 #define TYPE_FW_CFG_MEM "fw_cfg_mem"
41 
42 #define FW_CFG(obj)     OBJECT_CHECK(FWCfgState,    (obj), TYPE_FW_CFG)
43 #define FW_CFG_IO(obj)  OBJECT_CHECK(FWCfgIoState,  (obj), TYPE_FW_CFG_IO)
44 #define FW_CFG_MEM(obj) OBJECT_CHECK(FWCfgMemState, (obj), TYPE_FW_CFG_MEM)
45 
46 typedef struct FWCfgEntry {
47     uint32_t len;
48     uint8_t *data;
49     void *callback_opaque;
50     FWCfgCallback callback;
51     FWCfgReadCallback read_callback;
52 } FWCfgEntry;
53 
54 struct FWCfgState {
55     /*< private >*/
56     SysBusDevice parent_obj;
57     /*< public >*/
58 
59     FWCfgEntry entries[2][FW_CFG_MAX_ENTRY];
60     FWCfgFiles *files;
61     uint16_t cur_entry;
62     uint32_t cur_offset;
63     Notifier machine_ready;
64 };
65 
66 struct FWCfgIoState {
67     /*< private >*/
68     FWCfgState parent_obj;
69     /*< public >*/
70 
71     MemoryRegion comb_iomem;
72     uint32_t iobase;
73 };
74 
75 struct FWCfgMemState {
76     /*< private >*/
77     FWCfgState parent_obj;
78     /*< public >*/
79 
80     MemoryRegion ctl_iomem, data_iomem;
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         if (boot_splash_filedata != NULL) {
191             g_free(boot_splash_filedata);
192         }
193         boot_splash_filedata = (uint8_t *)file_data;
194         boot_splash_filedata_size = file_size;
195 
196         /* insert data */
197         if (file_type == JPG_FILE) {
198             fw_cfg_add_file(s, "bootsplash.jpg",
199                     boot_splash_filedata, boot_splash_filedata_size);
200         } else {
201             fw_cfg_add_file(s, "bootsplash.bmp",
202                     boot_splash_filedata, boot_splash_filedata_size);
203         }
204         g_free(filename);
205     }
206 }
207 
208 static void fw_cfg_reboot(FWCfgState *s)
209 {
210     int reboot_timeout = -1;
211     char *p;
212     const char *temp;
213 
214     /* get user configuration */
215     QemuOptsList *plist = qemu_find_opts("boot-opts");
216     QemuOpts *opts = QTAILQ_FIRST(&plist->head);
217     if (opts != NULL) {
218         temp = qemu_opt_get(opts, "reboot-timeout");
219         if (temp != NULL) {
220             p = (char *)temp;
221             reboot_timeout = strtol(p, (char **)&p, 10);
222         }
223     }
224     /* validate the input */
225     if (reboot_timeout > 0xffff) {
226         error_report("reboot timeout is larger than 65535, force it to 65535.");
227         reboot_timeout = 0xffff;
228     }
229     fw_cfg_add_file(s, "etc/boot-fail-wait", g_memdup(&reboot_timeout, 4), 4);
230 }
231 
232 static void fw_cfg_write(FWCfgState *s, uint8_t value)
233 {
234     int arch = !!(s->cur_entry & FW_CFG_ARCH_LOCAL);
235     FWCfgEntry *e = &s->entries[arch][s->cur_entry & FW_CFG_ENTRY_MASK];
236 
237     trace_fw_cfg_write(s, value);
238 
239     if (s->cur_entry & FW_CFG_WRITE_CHANNEL && e->callback &&
240         s->cur_offset < e->len) {
241         e->data[s->cur_offset++] = value;
242         if (s->cur_offset == e->len) {
243             e->callback(e->callback_opaque, e->data);
244             s->cur_offset = 0;
245         }
246     }
247 }
248 
249 static int fw_cfg_select(FWCfgState *s, uint16_t key)
250 {
251     int ret;
252 
253     s->cur_offset = 0;
254     if ((key & FW_CFG_ENTRY_MASK) >= FW_CFG_MAX_ENTRY) {
255         s->cur_entry = FW_CFG_INVALID;
256         ret = 0;
257     } else {
258         s->cur_entry = key;
259         ret = 1;
260     }
261 
262     trace_fw_cfg_select(s, key, ret);
263     return ret;
264 }
265 
266 static uint8_t fw_cfg_read(FWCfgState *s)
267 {
268     int arch = !!(s->cur_entry & FW_CFG_ARCH_LOCAL);
269     FWCfgEntry *e = &s->entries[arch][s->cur_entry & FW_CFG_ENTRY_MASK];
270     uint8_t ret;
271 
272     if (s->cur_entry == FW_CFG_INVALID || !e->data || s->cur_offset >= e->len)
273         ret = 0;
274     else {
275         if (e->read_callback) {
276             e->read_callback(e->callback_opaque, s->cur_offset);
277         }
278         ret = e->data[s->cur_offset++];
279     }
280 
281     trace_fw_cfg_read(s, ret);
282     return ret;
283 }
284 
285 static uint64_t fw_cfg_data_mem_read(void *opaque, hwaddr addr,
286                                      unsigned size)
287 {
288     return fw_cfg_read(opaque);
289 }
290 
291 static void fw_cfg_data_mem_write(void *opaque, hwaddr addr,
292                                   uint64_t value, unsigned size)
293 {
294     fw_cfg_write(opaque, (uint8_t)value);
295 }
296 
297 static void fw_cfg_ctl_mem_write(void *opaque, hwaddr addr,
298                                  uint64_t value, unsigned size)
299 {
300     fw_cfg_select(opaque, (uint16_t)value);
301 }
302 
303 static bool fw_cfg_ctl_mem_valid(void *opaque, hwaddr addr,
304                                  unsigned size, bool is_write)
305 {
306     return is_write && size == 2;
307 }
308 
309 static uint64_t fw_cfg_comb_read(void *opaque, hwaddr addr,
310                                  unsigned size)
311 {
312     return fw_cfg_read(opaque);
313 }
314 
315 static void fw_cfg_comb_write(void *opaque, hwaddr addr,
316                               uint64_t value, unsigned size)
317 {
318     switch (size) {
319     case 1:
320         fw_cfg_write(opaque, (uint8_t)value);
321         break;
322     case 2:
323         fw_cfg_select(opaque, (uint16_t)value);
324         break;
325     }
326 }
327 
328 static bool fw_cfg_comb_valid(void *opaque, hwaddr addr,
329                                   unsigned size, bool is_write)
330 {
331     return (size == 1) || (is_write && size == 2);
332 }
333 
334 static const MemoryRegionOps fw_cfg_ctl_mem_ops = {
335     .write = fw_cfg_ctl_mem_write,
336     .endianness = DEVICE_NATIVE_ENDIAN,
337     .valid.accepts = fw_cfg_ctl_mem_valid,
338 };
339 
340 static const MemoryRegionOps fw_cfg_data_mem_ops = {
341     .read = fw_cfg_data_mem_read,
342     .write = fw_cfg_data_mem_write,
343     .endianness = DEVICE_NATIVE_ENDIAN,
344     .valid = {
345         .min_access_size = 1,
346         .max_access_size = 1,
347     },
348 };
349 
350 static const MemoryRegionOps fw_cfg_comb_mem_ops = {
351     .read = fw_cfg_comb_read,
352     .write = fw_cfg_comb_write,
353     .endianness = DEVICE_LITTLE_ENDIAN,
354     .valid.accepts = fw_cfg_comb_valid,
355 };
356 
357 static void fw_cfg_reset(DeviceState *d)
358 {
359     FWCfgState *s = FW_CFG(d);
360 
361     fw_cfg_select(s, 0);
362 }
363 
364 /* Save restore 32 bit int as uint16_t
365    This is a Big hack, but it is how the old state did it.
366    Or we broke compatibility in the state, or we can't use struct tm
367  */
368 
369 static int get_uint32_as_uint16(QEMUFile *f, void *pv, size_t size)
370 {
371     uint32_t *v = pv;
372     *v = qemu_get_be16(f);
373     return 0;
374 }
375 
376 static void put_unused(QEMUFile *f, void *pv, size_t size)
377 {
378     fprintf(stderr, "uint32_as_uint16 is only used for backward compatibility.\n");
379     fprintf(stderr, "This functions shouldn't be called.\n");
380 }
381 
382 static const VMStateInfo vmstate_hack_uint32_as_uint16 = {
383     .name = "int32_as_uint16",
384     .get  = get_uint32_as_uint16,
385     .put  = put_unused,
386 };
387 
388 #define VMSTATE_UINT16_HACK(_f, _s, _t)                                    \
389     VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_hack_uint32_as_uint16, uint32_t)
390 
391 
392 static bool is_version_1(void *opaque, int version_id)
393 {
394     return version_id == 1;
395 }
396 
397 static const VMStateDescription vmstate_fw_cfg = {
398     .name = "fw_cfg",
399     .version_id = 2,
400     .minimum_version_id = 1,
401     .fields = (VMStateField[]) {
402         VMSTATE_UINT16(cur_entry, FWCfgState),
403         VMSTATE_UINT16_HACK(cur_offset, FWCfgState, is_version_1),
404         VMSTATE_UINT32_V(cur_offset, FWCfgState, 2),
405         VMSTATE_END_OF_LIST()
406     }
407 };
408 
409 static void fw_cfg_add_bytes_read_callback(FWCfgState *s, uint16_t key,
410                                            FWCfgReadCallback callback,
411                                            void *callback_opaque,
412                                            void *data, size_t len)
413 {
414     int arch = !!(key & FW_CFG_ARCH_LOCAL);
415 
416     key &= FW_CFG_ENTRY_MASK;
417 
418     assert(key < FW_CFG_MAX_ENTRY && len < UINT32_MAX);
419 
420     s->entries[arch][key].data = data;
421     s->entries[arch][key].len = (uint32_t)len;
422     s->entries[arch][key].read_callback = callback;
423     s->entries[arch][key].callback_opaque = callback_opaque;
424 }
425 
426 static void *fw_cfg_modify_bytes_read(FWCfgState *s, uint16_t key,
427                                               void *data, size_t len)
428 {
429     void *ptr;
430     int arch = !!(key & FW_CFG_ARCH_LOCAL);
431 
432     key &= FW_CFG_ENTRY_MASK;
433 
434     assert(key < FW_CFG_MAX_ENTRY && len < UINT32_MAX);
435 
436     /* return the old data to the function caller, avoid memory leak */
437     ptr = s->entries[arch][key].data;
438     s->entries[arch][key].data = data;
439     s->entries[arch][key].len = len;
440     s->entries[arch][key].callback_opaque = NULL;
441     s->entries[arch][key].callback = NULL;
442 
443     return ptr;
444 }
445 
446 void fw_cfg_add_bytes(FWCfgState *s, uint16_t key, void *data, size_t len)
447 {
448     fw_cfg_add_bytes_read_callback(s, key, NULL, NULL, data, len);
449 }
450 
451 void fw_cfg_add_string(FWCfgState *s, uint16_t key, const char *value)
452 {
453     size_t sz = strlen(value) + 1;
454 
455     return fw_cfg_add_bytes(s, key, g_memdup(value, sz), sz);
456 }
457 
458 void fw_cfg_add_i16(FWCfgState *s, uint16_t key, uint16_t value)
459 {
460     uint16_t *copy;
461 
462     copy = g_malloc(sizeof(value));
463     *copy = cpu_to_le16(value);
464     fw_cfg_add_bytes(s, key, copy, sizeof(value));
465 }
466 
467 void fw_cfg_add_i32(FWCfgState *s, uint16_t key, uint32_t value)
468 {
469     uint32_t *copy;
470 
471     copy = g_malloc(sizeof(value));
472     *copy = cpu_to_le32(value);
473     fw_cfg_add_bytes(s, key, copy, sizeof(value));
474 }
475 
476 void fw_cfg_add_i64(FWCfgState *s, uint16_t key, uint64_t value)
477 {
478     uint64_t *copy;
479 
480     copy = g_malloc(sizeof(value));
481     *copy = cpu_to_le64(value);
482     fw_cfg_add_bytes(s, key, copy, sizeof(value));
483 }
484 
485 void fw_cfg_add_callback(FWCfgState *s, uint16_t key, FWCfgCallback callback,
486                          void *callback_opaque, void *data, size_t len)
487 {
488     int arch = !!(key & FW_CFG_ARCH_LOCAL);
489 
490     assert(key & FW_CFG_WRITE_CHANNEL);
491 
492     key &= FW_CFG_ENTRY_MASK;
493 
494     assert(key < FW_CFG_MAX_ENTRY && len <= UINT32_MAX);
495 
496     s->entries[arch][key].data = data;
497     s->entries[arch][key].len = (uint32_t)len;
498     s->entries[arch][key].callback_opaque = callback_opaque;
499     s->entries[arch][key].callback = callback;
500 }
501 
502 void fw_cfg_add_file_callback(FWCfgState *s,  const char *filename,
503                               FWCfgReadCallback callback, void *callback_opaque,
504                               void *data, size_t len)
505 {
506     int i, index;
507     size_t dsize;
508 
509     if (!s->files) {
510         dsize = sizeof(uint32_t) + sizeof(FWCfgFile) * FW_CFG_FILE_SLOTS;
511         s->files = g_malloc0(dsize);
512         fw_cfg_add_bytes(s, FW_CFG_FILE_DIR, s->files, dsize);
513     }
514 
515     index = be32_to_cpu(s->files->count);
516     assert(index < FW_CFG_FILE_SLOTS);
517 
518     fw_cfg_add_bytes_read_callback(s, FW_CFG_FILE_FIRST + index,
519                                    callback, callback_opaque, data, len);
520 
521     pstrcpy(s->files->f[index].name, sizeof(s->files->f[index].name),
522             filename);
523     for (i = 0; i < index; i++) {
524         if (strcmp(s->files->f[index].name, s->files->f[i].name) == 0) {
525             trace_fw_cfg_add_file_dupe(s, s->files->f[index].name);
526             return;
527         }
528     }
529 
530     s->files->f[index].size   = cpu_to_be32(len);
531     s->files->f[index].select = cpu_to_be16(FW_CFG_FILE_FIRST + index);
532     trace_fw_cfg_add_file(s, index, s->files->f[index].name, len);
533 
534     s->files->count = cpu_to_be32(index+1);
535 }
536 
537 void fw_cfg_add_file(FWCfgState *s,  const char *filename,
538                      void *data, size_t len)
539 {
540     fw_cfg_add_file_callback(s, filename, NULL, NULL, data, len);
541 }
542 
543 void *fw_cfg_modify_file(FWCfgState *s, const char *filename,
544                         void *data, size_t len)
545 {
546     int i, index;
547     void *ptr = NULL;
548 
549     assert(s->files);
550 
551     index = be32_to_cpu(s->files->count);
552     assert(index < FW_CFG_FILE_SLOTS);
553 
554     for (i = 0; i < index; i++) {
555         if (strcmp(filename, s->files->f[i].name) == 0) {
556             ptr = fw_cfg_modify_bytes_read(s, FW_CFG_FILE_FIRST + i,
557                                            data, len);
558             s->files->f[i].size   = cpu_to_be32(len);
559             return ptr;
560         }
561     }
562     /* add new one */
563     fw_cfg_add_file_callback(s, filename, NULL, NULL, data, len);
564     return NULL;
565 }
566 
567 static void fw_cfg_machine_reset(void *opaque)
568 {
569     void *ptr;
570     size_t len;
571     FWCfgState *s = opaque;
572     char *bootindex = get_boot_devices_list(&len, false);
573 
574     ptr = fw_cfg_modify_file(s, "bootorder", (uint8_t *)bootindex, len);
575     g_free(ptr);
576 }
577 
578 static void fw_cfg_machine_ready(struct Notifier *n, void *data)
579 {
580     FWCfgState *s = container_of(n, FWCfgState, machine_ready);
581     qemu_register_reset(fw_cfg_machine_reset, s);
582 }
583 
584 
585 
586 static void fw_cfg_init1(DeviceState *dev)
587 {
588     FWCfgState *s = FW_CFG(dev);
589 
590     assert(!object_resolve_path(FW_CFG_PATH, NULL));
591 
592     object_property_add_child(qdev_get_machine(), FW_CFG_NAME, OBJECT(s), NULL);
593 
594     qdev_init_nofail(dev);
595 
596     fw_cfg_add_bytes(s, FW_CFG_SIGNATURE, (char *)"QEMU", 4);
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(hwaddr ctl_addr, hwaddr data_addr)
620 {
621     DeviceState *dev;
622     SysBusDevice *sbd;
623 
624     dev = qdev_create(NULL, TYPE_FW_CFG_MEM);
625     fw_cfg_init1(dev);
626 
627     sbd = SYS_BUS_DEVICE(dev);
628     sysbus_mmio_map(sbd, 0, ctl_addr);
629     sysbus_mmio_map(sbd, 1, data_addr);
630 
631     return FW_CFG(dev);
632 }
633 
634 
635 FWCfgState *fw_cfg_find(void)
636 {
637     return FW_CFG(object_resolve_path(FW_CFG_PATH, NULL));
638 }
639 
640 static void fw_cfg_class_init(ObjectClass *klass, void *data)
641 {
642     DeviceClass *dc = DEVICE_CLASS(klass);
643 
644     dc->reset = fw_cfg_reset;
645     dc->vmsd = &vmstate_fw_cfg;
646 }
647 
648 static const TypeInfo fw_cfg_info = {
649     .name          = TYPE_FW_CFG,
650     .parent        = TYPE_SYS_BUS_DEVICE,
651     .instance_size = sizeof(FWCfgState),
652     .class_init    = fw_cfg_class_init,
653 };
654 
655 
656 static Property fw_cfg_io_properties[] = {
657     DEFINE_PROP_UINT32("iobase", FWCfgIoState, iobase, -1),
658     DEFINE_PROP_END_OF_LIST(),
659 };
660 
661 static void fw_cfg_io_realize(DeviceState *dev, Error **errp)
662 {
663     FWCfgIoState *s = FW_CFG_IO(dev);
664     SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
665 
666     memory_region_init_io(&s->comb_iomem, OBJECT(s), &fw_cfg_comb_mem_ops,
667                           FW_CFG(s), "fwcfg", FW_CFG_SIZE);
668     sysbus_add_io(sbd, s->iobase, &s->comb_iomem);
669 }
670 
671 static void fw_cfg_io_class_init(ObjectClass *klass, void *data)
672 {
673     DeviceClass *dc = DEVICE_CLASS(klass);
674 
675     dc->realize = fw_cfg_io_realize;
676     dc->props = fw_cfg_io_properties;
677 }
678 
679 static const TypeInfo fw_cfg_io_info = {
680     .name          = TYPE_FW_CFG_IO,
681     .parent        = TYPE_FW_CFG,
682     .instance_size = sizeof(FWCfgIoState),
683     .class_init    = fw_cfg_io_class_init,
684 };
685 
686 
687 static void fw_cfg_mem_realize(DeviceState *dev, Error **errp)
688 {
689     FWCfgMemState *s = FW_CFG_MEM(dev);
690     SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
691 
692     memory_region_init_io(&s->ctl_iomem, OBJECT(s), &fw_cfg_ctl_mem_ops,
693                           FW_CFG(s), "fwcfg.ctl", FW_CFG_SIZE);
694     sysbus_init_mmio(sbd, &s->ctl_iomem);
695 
696     memory_region_init_io(&s->data_iomem, OBJECT(s), &fw_cfg_data_mem_ops,
697                           FW_CFG(s), "fwcfg.data", FW_CFG_DATA_SIZE);
698     sysbus_init_mmio(sbd, &s->data_iomem);
699 }
700 
701 static void fw_cfg_mem_class_init(ObjectClass *klass, void *data)
702 {
703     DeviceClass *dc = DEVICE_CLASS(klass);
704 
705     dc->realize = fw_cfg_mem_realize;
706 }
707 
708 static const TypeInfo fw_cfg_mem_info = {
709     .name          = TYPE_FW_CFG_MEM,
710     .parent        = TYPE_FW_CFG,
711     .instance_size = sizeof(FWCfgMemState),
712     .class_init    = fw_cfg_mem_class_init,
713 };
714 
715 
716 static void fw_cfg_register_types(void)
717 {
718     type_register_static(&fw_cfg_info);
719     type_register_static(&fw_cfg_io_info);
720     type_register_static(&fw_cfg_mem_info);
721 }
722 
723 type_init(fw_cfg_register_types)
724