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