xref: /openbmc/qemu/hw/nvram/fw_cfg.c (revision 48779e501810c5046ff8af7b9cf9c99bec2928a1)
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     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_modify_i16(FWCfgState *s, uint16_t key, uint16_t value)
488 {
489     uint16_t *copy, *old;
490 
491     copy = g_malloc(sizeof(value));
492     *copy = cpu_to_le16(value);
493     old = fw_cfg_modify_bytes_read(s, key, copy, sizeof(value));
494     g_free(old);
495 }
496 
497 void fw_cfg_add_i32(FWCfgState *s, uint16_t key, uint32_t value)
498 {
499     uint32_t *copy;
500 
501     copy = g_malloc(sizeof(value));
502     *copy = cpu_to_le32(value);
503     fw_cfg_add_bytes(s, key, copy, sizeof(value));
504 }
505 
506 void fw_cfg_add_i64(FWCfgState *s, uint16_t key, uint64_t value)
507 {
508     uint64_t *copy;
509 
510     copy = g_malloc(sizeof(value));
511     *copy = cpu_to_le64(value);
512     fw_cfg_add_bytes(s, key, copy, sizeof(value));
513 }
514 
515 void fw_cfg_add_callback(FWCfgState *s, uint16_t key, FWCfgCallback callback,
516                          void *callback_opaque, void *data, size_t len)
517 {
518     int arch = !!(key & FW_CFG_ARCH_LOCAL);
519 
520     assert(key & FW_CFG_WRITE_CHANNEL);
521 
522     key &= FW_CFG_ENTRY_MASK;
523 
524     assert(key < FW_CFG_MAX_ENTRY && len <= UINT32_MAX);
525 
526     s->entries[arch][key].data = data;
527     s->entries[arch][key].len = (uint32_t)len;
528     s->entries[arch][key].callback_opaque = callback_opaque;
529     s->entries[arch][key].callback = callback;
530 }
531 
532 void fw_cfg_add_file_callback(FWCfgState *s,  const char *filename,
533                               FWCfgReadCallback callback, void *callback_opaque,
534                               void *data, size_t len)
535 {
536     int i, index;
537     size_t dsize;
538 
539     if (!s->files) {
540         dsize = sizeof(uint32_t) + sizeof(FWCfgFile) * FW_CFG_FILE_SLOTS;
541         s->files = g_malloc0(dsize);
542         fw_cfg_add_bytes(s, FW_CFG_FILE_DIR, s->files, dsize);
543     }
544 
545     index = be32_to_cpu(s->files->count);
546     assert(index < FW_CFG_FILE_SLOTS);
547 
548     fw_cfg_add_bytes_read_callback(s, FW_CFG_FILE_FIRST + index,
549                                    callback, callback_opaque, data, len);
550 
551     pstrcpy(s->files->f[index].name, sizeof(s->files->f[index].name),
552             filename);
553     for (i = 0; i < index; i++) {
554         if (strcmp(s->files->f[index].name, s->files->f[i].name) == 0) {
555             trace_fw_cfg_add_file_dupe(s, s->files->f[index].name);
556             return;
557         }
558     }
559 
560     s->files->f[index].size   = cpu_to_be32(len);
561     s->files->f[index].select = cpu_to_be16(FW_CFG_FILE_FIRST + index);
562     trace_fw_cfg_add_file(s, index, s->files->f[index].name, len);
563 
564     s->files->count = cpu_to_be32(index+1);
565 }
566 
567 void fw_cfg_add_file(FWCfgState *s,  const char *filename,
568                      void *data, size_t len)
569 {
570     fw_cfg_add_file_callback(s, filename, NULL, NULL, data, len);
571 }
572 
573 void *fw_cfg_modify_file(FWCfgState *s, const char *filename,
574                         void *data, size_t len)
575 {
576     int i, index;
577     void *ptr = NULL;
578 
579     assert(s->files);
580 
581     index = be32_to_cpu(s->files->count);
582     assert(index < FW_CFG_FILE_SLOTS);
583 
584     for (i = 0; i < index; i++) {
585         if (strcmp(filename, s->files->f[i].name) == 0) {
586             ptr = fw_cfg_modify_bytes_read(s, FW_CFG_FILE_FIRST + i,
587                                            data, len);
588             s->files->f[i].size   = cpu_to_be32(len);
589             return ptr;
590         }
591     }
592     /* add new one */
593     fw_cfg_add_file_callback(s, filename, NULL, NULL, data, len);
594     return NULL;
595 }
596 
597 static void fw_cfg_machine_reset(void *opaque)
598 {
599     void *ptr;
600     size_t len;
601     FWCfgState *s = opaque;
602     char *bootindex = get_boot_devices_list(&len, false);
603 
604     ptr = fw_cfg_modify_file(s, "bootorder", (uint8_t *)bootindex, len);
605     g_free(ptr);
606 }
607 
608 static void fw_cfg_machine_ready(struct Notifier *n, void *data)
609 {
610     FWCfgState *s = container_of(n, FWCfgState, machine_ready);
611     qemu_register_reset(fw_cfg_machine_reset, s);
612 }
613 
614 
615 
616 static void fw_cfg_init1(DeviceState *dev)
617 {
618     FWCfgState *s = FW_CFG(dev);
619 
620     assert(!object_resolve_path(FW_CFG_PATH, NULL));
621 
622     object_property_add_child(qdev_get_machine(), FW_CFG_NAME, OBJECT(s), NULL);
623 
624     qdev_init_nofail(dev);
625 
626     fw_cfg_add_bytes(s, FW_CFG_SIGNATURE, (char *)"QEMU", 4);
627     fw_cfg_add_i32(s, FW_CFG_ID, 1);
628     fw_cfg_add_bytes(s, FW_CFG_UUID, qemu_uuid, 16);
629     fw_cfg_add_i16(s, FW_CFG_NOGRAPHIC, (uint16_t)(display_type == DT_NOGRAPHIC));
630     fw_cfg_add_i16(s, FW_CFG_NB_CPUS, (uint16_t)smp_cpus);
631     fw_cfg_add_i16(s, FW_CFG_BOOT_MENU, (uint16_t)boot_menu);
632     fw_cfg_bootsplash(s);
633     fw_cfg_reboot(s);
634 
635     s->machine_ready.notify = fw_cfg_machine_ready;
636     qemu_add_machine_init_done_notifier(&s->machine_ready);
637 }
638 
639 FWCfgState *fw_cfg_init_io(uint32_t iobase)
640 {
641     DeviceState *dev;
642 
643     dev = qdev_create(NULL, TYPE_FW_CFG_IO);
644     qdev_prop_set_uint32(dev, "iobase", iobase);
645     fw_cfg_init1(dev);
646 
647     return FW_CFG(dev);
648 }
649 
650 FWCfgState *fw_cfg_init_mem_wide(hwaddr ctl_addr, hwaddr data_addr,
651                                  uint32_t data_width)
652 {
653     DeviceState *dev;
654     SysBusDevice *sbd;
655 
656     dev = qdev_create(NULL, TYPE_FW_CFG_MEM);
657     qdev_prop_set_uint32(dev, "data_width", data_width);
658 
659     fw_cfg_init1(dev);
660 
661     sbd = SYS_BUS_DEVICE(dev);
662     sysbus_mmio_map(sbd, 0, ctl_addr);
663     sysbus_mmio_map(sbd, 1, data_addr);
664 
665     return FW_CFG(dev);
666 }
667 
668 FWCfgState *fw_cfg_init_mem(hwaddr ctl_addr, hwaddr data_addr)
669 {
670     return fw_cfg_init_mem_wide(ctl_addr, data_addr,
671                                 fw_cfg_data_mem_ops.valid.max_access_size);
672 }
673 
674 
675 FWCfgState *fw_cfg_find(void)
676 {
677     return FW_CFG(object_resolve_path(FW_CFG_PATH, NULL));
678 }
679 
680 static void fw_cfg_class_init(ObjectClass *klass, void *data)
681 {
682     DeviceClass *dc = DEVICE_CLASS(klass);
683 
684     dc->reset = fw_cfg_reset;
685     dc->vmsd = &vmstate_fw_cfg;
686 }
687 
688 static const TypeInfo fw_cfg_info = {
689     .name          = TYPE_FW_CFG,
690     .parent        = TYPE_SYS_BUS_DEVICE,
691     .instance_size = sizeof(FWCfgState),
692     .class_init    = fw_cfg_class_init,
693 };
694 
695 
696 static Property fw_cfg_io_properties[] = {
697     DEFINE_PROP_UINT32("iobase", FWCfgIoState, iobase, -1),
698     DEFINE_PROP_END_OF_LIST(),
699 };
700 
701 static void fw_cfg_io_realize(DeviceState *dev, Error **errp)
702 {
703     FWCfgIoState *s = FW_CFG_IO(dev);
704     SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
705 
706     memory_region_init_io(&s->comb_iomem, OBJECT(s), &fw_cfg_comb_mem_ops,
707                           FW_CFG(s), "fwcfg", FW_CFG_SIZE);
708     sysbus_add_io(sbd, s->iobase, &s->comb_iomem);
709 }
710 
711 static void fw_cfg_io_class_init(ObjectClass *klass, void *data)
712 {
713     DeviceClass *dc = DEVICE_CLASS(klass);
714 
715     dc->realize = fw_cfg_io_realize;
716     dc->props = fw_cfg_io_properties;
717 }
718 
719 static const TypeInfo fw_cfg_io_info = {
720     .name          = TYPE_FW_CFG_IO,
721     .parent        = TYPE_FW_CFG,
722     .instance_size = sizeof(FWCfgIoState),
723     .class_init    = fw_cfg_io_class_init,
724 };
725 
726 
727 static Property fw_cfg_mem_properties[] = {
728     DEFINE_PROP_UINT32("data_width", FWCfgMemState, data_width, -1),
729     DEFINE_PROP_END_OF_LIST(),
730 };
731 
732 static void fw_cfg_mem_realize(DeviceState *dev, Error **errp)
733 {
734     FWCfgMemState *s = FW_CFG_MEM(dev);
735     SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
736     const MemoryRegionOps *data_ops = &fw_cfg_data_mem_ops;
737 
738     memory_region_init_io(&s->ctl_iomem, OBJECT(s), &fw_cfg_ctl_mem_ops,
739                           FW_CFG(s), "fwcfg.ctl", FW_CFG_SIZE);
740     sysbus_init_mmio(sbd, &s->ctl_iomem);
741 
742     if (s->data_width > data_ops->valid.max_access_size) {
743         /* memberwise copy because the "old_mmio" member is const */
744         s->wide_data_ops.read       = data_ops->read;
745         s->wide_data_ops.write      = data_ops->write;
746         s->wide_data_ops.endianness = data_ops->endianness;
747         s->wide_data_ops.valid      = data_ops->valid;
748         s->wide_data_ops.impl       = data_ops->impl;
749 
750         s->wide_data_ops.valid.max_access_size = s->data_width;
751         s->wide_data_ops.impl.max_access_size  = s->data_width;
752         data_ops = &s->wide_data_ops;
753     }
754     memory_region_init_io(&s->data_iomem, OBJECT(s), data_ops, FW_CFG(s),
755                           "fwcfg.data", data_ops->valid.max_access_size);
756     sysbus_init_mmio(sbd, &s->data_iomem);
757 }
758 
759 static void fw_cfg_mem_class_init(ObjectClass *klass, void *data)
760 {
761     DeviceClass *dc = DEVICE_CLASS(klass);
762 
763     dc->realize = fw_cfg_mem_realize;
764     dc->props = fw_cfg_mem_properties;
765 }
766 
767 static const TypeInfo fw_cfg_mem_info = {
768     .name          = TYPE_FW_CFG_MEM,
769     .parent        = TYPE_FW_CFG,
770     .instance_size = sizeof(FWCfgMemState),
771     .class_init    = fw_cfg_mem_class_init,
772 };
773 
774 
775 static void fw_cfg_register_types(void)
776 {
777     type_register_static(&fw_cfg_info);
778     type_register_static(&fw_cfg_io_info);
779     type_register_static(&fw_cfg_mem_info);
780 }
781 
782 type_init(fw_cfg_register_types)
783