xref: /openbmc/qemu/hw/nvram/fw_cfg.c (revision fe1127da)
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 
25 #include "qemu/osdep.h"
26 #include "qemu-common.h"
27 #include "sysemu/sysemu.h"
28 #include "sysemu/dma.h"
29 #include "sysemu/reset.h"
30 #include "hw/boards.h"
31 #include "hw/nvram/fw_cfg.h"
32 #include "hw/qdev-properties.h"
33 #include "hw/sysbus.h"
34 #include "migration/qemu-file-types.h"
35 #include "migration/vmstate.h"
36 #include "trace.h"
37 #include "qemu/error-report.h"
38 #include "qemu/option.h"
39 #include "qemu/config-file.h"
40 #include "qemu/cutils.h"
41 #include "qapi/error.h"
42 #include "hw/acpi/aml-build.h"
43 #include "hw/pci/pci_bus.h"
44 
45 #define FW_CFG_FILE_SLOTS_DFLT 0x20
46 
47 /* FW_CFG_VERSION bits */
48 #define FW_CFG_VERSION      0x01
49 #define FW_CFG_VERSION_DMA  0x02
50 
51 /* FW_CFG_DMA_CONTROL bits */
52 #define FW_CFG_DMA_CTL_ERROR   0x01
53 #define FW_CFG_DMA_CTL_READ    0x02
54 #define FW_CFG_DMA_CTL_SKIP    0x04
55 #define FW_CFG_DMA_CTL_SELECT  0x08
56 #define FW_CFG_DMA_CTL_WRITE   0x10
57 
58 #define FW_CFG_DMA_SIGNATURE 0x51454d5520434647ULL /* "QEMU CFG" */
59 
60 struct FWCfgEntry {
61     uint32_t len;
62     bool allow_write;
63     uint8_t *data;
64     void *callback_opaque;
65     FWCfgCallback select_cb;
66     FWCfgWriteCallback write_cb;
67 };
68 
69 /**
70  * key_name:
71  *
72  * @key: The uint16 selector key.
73  *
74  * Returns: The stringified name if the selector refers to a well-known
75  *          numerically defined item, or NULL on key lookup failure.
76  */
77 static const char *key_name(uint16_t key)
78 {
79     static const char *fw_cfg_wellknown_keys[FW_CFG_FILE_FIRST] = {
80         [FW_CFG_SIGNATURE] = "signature",
81         [FW_CFG_ID] = "id",
82         [FW_CFG_UUID] = "uuid",
83         [FW_CFG_RAM_SIZE] = "ram_size",
84         [FW_CFG_NOGRAPHIC] = "nographic",
85         [FW_CFG_NB_CPUS] = "nb_cpus",
86         [FW_CFG_MACHINE_ID] = "machine_id",
87         [FW_CFG_KERNEL_ADDR] = "kernel_addr",
88         [FW_CFG_KERNEL_SIZE] = "kernel_size",
89         [FW_CFG_KERNEL_CMDLINE] = "kernel_cmdline",
90         [FW_CFG_INITRD_ADDR] = "initrd_addr",
91         [FW_CFG_INITRD_SIZE] = "initdr_size",
92         [FW_CFG_BOOT_DEVICE] = "boot_device",
93         [FW_CFG_NUMA] = "numa",
94         [FW_CFG_BOOT_MENU] = "boot_menu",
95         [FW_CFG_MAX_CPUS] = "max_cpus",
96         [FW_CFG_KERNEL_ENTRY] = "kernel_entry",
97         [FW_CFG_KERNEL_DATA] = "kernel_data",
98         [FW_CFG_INITRD_DATA] = "initrd_data",
99         [FW_CFG_CMDLINE_ADDR] = "cmdline_addr",
100         [FW_CFG_CMDLINE_SIZE] = "cmdline_size",
101         [FW_CFG_CMDLINE_DATA] = "cmdline_data",
102         [FW_CFG_SETUP_ADDR] = "setup_addr",
103         [FW_CFG_SETUP_SIZE] = "setup_size",
104         [FW_CFG_SETUP_DATA] = "setup_data",
105         [FW_CFG_FILE_DIR] = "file_dir",
106     };
107 
108     if (key & FW_CFG_ARCH_LOCAL) {
109         return fw_cfg_arch_key_name(key);
110     }
111     if (key < FW_CFG_FILE_FIRST) {
112         return fw_cfg_wellknown_keys[key];
113     }
114 
115     return NULL;
116 }
117 
118 static inline const char *trace_key_name(uint16_t key)
119 {
120     const char *name = key_name(key);
121 
122     return name ? name : "unknown";
123 }
124 
125 #define JPG_FILE 0
126 #define BMP_FILE 1
127 
128 static char *read_splashfile(char *filename, gsize *file_sizep,
129                              int *file_typep)
130 {
131     GError *err = NULL;
132     gchar *content;
133     int file_type;
134     unsigned int filehead;
135     int bmp_bpp;
136 
137     if (!g_file_get_contents(filename, &content, file_sizep, &err)) {
138         error_report("failed to read splash file '%s': %s",
139                      filename, err->message);
140         g_error_free(err);
141         return NULL;
142     }
143 
144     /* check file size */
145     if (*file_sizep < 30) {
146         goto error;
147     }
148 
149     /* check magic ID */
150     filehead = lduw_le_p(content);
151     if (filehead == 0xd8ff) {
152         file_type = JPG_FILE;
153     } else if (filehead == 0x4d42) {
154         file_type = BMP_FILE;
155     } else {
156         goto error;
157     }
158 
159     /* check BMP bpp */
160     if (file_type == BMP_FILE) {
161         bmp_bpp = lduw_le_p(&content[28]);
162         if (bmp_bpp != 24) {
163             goto error;
164         }
165     }
166 
167     /* return values */
168     *file_typep = file_type;
169 
170     return content;
171 
172 error:
173     error_report("splash file '%s' format not recognized; must be JPEG "
174                  "or 24 bit BMP", filename);
175     g_free(content);
176     return NULL;
177 }
178 
179 static void fw_cfg_bootsplash(FWCfgState *s)
180 {
181     const char *boot_splash_filename = NULL;
182     const char *boot_splash_time = NULL;
183     char *filename, *file_data;
184     gsize file_size;
185     int file_type;
186 
187     /* get user configuration */
188     QemuOptsList *plist = qemu_find_opts("boot-opts");
189     QemuOpts *opts = QTAILQ_FIRST(&plist->head);
190     boot_splash_filename = qemu_opt_get(opts, "splash");
191     boot_splash_time = qemu_opt_get(opts, "splash-time");
192 
193     /* insert splash time if user configurated */
194     if (boot_splash_time) {
195         int64_t bst_val = qemu_opt_get_number(opts, "splash-time", -1);
196         uint16_t bst_le16;
197 
198         /* validate the input */
199         if (bst_val < 0 || bst_val > 0xffff) {
200             error_report("splash-time is invalid,"
201                          "it should be a value between 0 and 65535");
202             exit(1);
203         }
204         /* use little endian format */
205         bst_le16 = cpu_to_le16(bst_val);
206         fw_cfg_add_file(s, "etc/boot-menu-wait",
207                         g_memdup(&bst_le16, sizeof bst_le16), sizeof bst_le16);
208     }
209 
210     /* insert splash file if user configurated */
211     if (boot_splash_filename) {
212         filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, boot_splash_filename);
213         if (filename == NULL) {
214             error_report("failed to find file '%s'", boot_splash_filename);
215             return;
216         }
217 
218         /* loading file data */
219         file_data = read_splashfile(filename, &file_size, &file_type);
220         if (file_data == NULL) {
221             g_free(filename);
222             return;
223         }
224         g_free(boot_splash_filedata);
225         boot_splash_filedata = (uint8_t *)file_data;
226 
227         /* insert data */
228         if (file_type == JPG_FILE) {
229             fw_cfg_add_file(s, "bootsplash.jpg",
230                             boot_splash_filedata, file_size);
231         } else {
232             fw_cfg_add_file(s, "bootsplash.bmp",
233                             boot_splash_filedata, file_size);
234         }
235         g_free(filename);
236     }
237 }
238 
239 static void fw_cfg_reboot(FWCfgState *s)
240 {
241     const char *reboot_timeout = NULL;
242     uint64_t rt_val = -1;
243     uint32_t rt_le32;
244 
245     /* get user configuration */
246     QemuOptsList *plist = qemu_find_opts("boot-opts");
247     QemuOpts *opts = QTAILQ_FIRST(&plist->head);
248     reboot_timeout = qemu_opt_get(opts, "reboot-timeout");
249 
250     if (reboot_timeout) {
251         rt_val = qemu_opt_get_number(opts, "reboot-timeout", -1);
252 
253         /* validate the input */
254         if (rt_val > 0xffff && rt_val != (uint64_t)-1) {
255             error_report("reboot timeout is invalid,"
256                          "it should be a value between -1 and 65535");
257             exit(1);
258         }
259     }
260 
261     rt_le32 = cpu_to_le32(rt_val);
262     fw_cfg_add_file(s, "etc/boot-fail-wait", g_memdup(&rt_le32, 4), 4);
263 }
264 
265 static void fw_cfg_write(FWCfgState *s, uint8_t value)
266 {
267     /* nothing, write support removed in QEMU v2.4+ */
268 }
269 
270 static inline uint16_t fw_cfg_file_slots(const FWCfgState *s)
271 {
272     return s->file_slots;
273 }
274 
275 /* Note: this function returns an exclusive limit. */
276 static inline uint32_t fw_cfg_max_entry(const FWCfgState *s)
277 {
278     return FW_CFG_FILE_FIRST + fw_cfg_file_slots(s);
279 }
280 
281 static int fw_cfg_select(FWCfgState *s, uint16_t key)
282 {
283     int arch, ret;
284     FWCfgEntry *e;
285 
286     s->cur_offset = 0;
287     if ((key & FW_CFG_ENTRY_MASK) >= fw_cfg_max_entry(s)) {
288         s->cur_entry = FW_CFG_INVALID;
289         ret = 0;
290     } else {
291         s->cur_entry = key;
292         ret = 1;
293         /* entry successfully selected, now run callback if present */
294         arch = !!(key & FW_CFG_ARCH_LOCAL);
295         e = &s->entries[arch][key & FW_CFG_ENTRY_MASK];
296         if (e->select_cb) {
297             e->select_cb(e->callback_opaque);
298         }
299     }
300 
301     trace_fw_cfg_select(s, key, trace_key_name(key), ret);
302     return ret;
303 }
304 
305 static uint64_t fw_cfg_data_read(void *opaque, hwaddr addr, unsigned size)
306 {
307     FWCfgState *s = opaque;
308     int arch = !!(s->cur_entry & FW_CFG_ARCH_LOCAL);
309     FWCfgEntry *e = (s->cur_entry == FW_CFG_INVALID) ? NULL :
310                     &s->entries[arch][s->cur_entry & FW_CFG_ENTRY_MASK];
311     uint64_t value = 0;
312 
313     assert(size > 0 && size <= sizeof(value));
314     if (s->cur_entry != FW_CFG_INVALID && e->data && s->cur_offset < e->len) {
315         /* The least significant 'size' bytes of the return value are
316          * expected to contain a string preserving portion of the item
317          * data, padded with zeros on the right in case we run out early.
318          * In technical terms, we're composing the host-endian representation
319          * of the big endian interpretation of the fw_cfg string.
320          */
321         do {
322             value = (value << 8) | e->data[s->cur_offset++];
323         } while (--size && s->cur_offset < e->len);
324         /* If size is still not zero, we *did* run out early, so continue
325          * left-shifting, to add the appropriate number of padding zeros
326          * on the right.
327          */
328         value <<= 8 * size;
329     }
330 
331     trace_fw_cfg_read(s, value);
332     return value;
333 }
334 
335 static void fw_cfg_data_mem_write(void *opaque, hwaddr addr,
336                                   uint64_t value, unsigned size)
337 {
338     FWCfgState *s = opaque;
339     unsigned i = size;
340 
341     do {
342         fw_cfg_write(s, value >> (8 * --i));
343     } while (i);
344 }
345 
346 static void fw_cfg_dma_transfer(FWCfgState *s)
347 {
348     dma_addr_t len;
349     FWCfgDmaAccess dma;
350     int arch;
351     FWCfgEntry *e;
352     int read = 0, write = 0;
353     dma_addr_t dma_addr;
354 
355     /* Reset the address before the next access */
356     dma_addr = s->dma_addr;
357     s->dma_addr = 0;
358 
359     if (dma_memory_read(s->dma_as, dma_addr, &dma, sizeof(dma))) {
360         stl_be_dma(s->dma_as, dma_addr + offsetof(FWCfgDmaAccess, control),
361                    FW_CFG_DMA_CTL_ERROR);
362         return;
363     }
364 
365     dma.address = be64_to_cpu(dma.address);
366     dma.length = be32_to_cpu(dma.length);
367     dma.control = be32_to_cpu(dma.control);
368 
369     if (dma.control & FW_CFG_DMA_CTL_SELECT) {
370         fw_cfg_select(s, dma.control >> 16);
371     }
372 
373     arch = !!(s->cur_entry & FW_CFG_ARCH_LOCAL);
374     e = (s->cur_entry == FW_CFG_INVALID) ? NULL :
375         &s->entries[arch][s->cur_entry & FW_CFG_ENTRY_MASK];
376 
377     if (dma.control & FW_CFG_DMA_CTL_READ) {
378         read = 1;
379         write = 0;
380     } else if (dma.control & FW_CFG_DMA_CTL_WRITE) {
381         read = 0;
382         write = 1;
383     } else if (dma.control & FW_CFG_DMA_CTL_SKIP) {
384         read = 0;
385         write = 0;
386     } else {
387         dma.length = 0;
388     }
389 
390     dma.control = 0;
391 
392     while (dma.length > 0 && !(dma.control & FW_CFG_DMA_CTL_ERROR)) {
393         if (s->cur_entry == FW_CFG_INVALID || !e->data ||
394                                 s->cur_offset >= e->len) {
395             len = dma.length;
396 
397             /* If the access is not a read access, it will be a skip access,
398              * tested before.
399              */
400             if (read) {
401                 if (dma_memory_set(s->dma_as, dma.address, 0, len)) {
402                     dma.control |= FW_CFG_DMA_CTL_ERROR;
403                 }
404             }
405             if (write) {
406                 dma.control |= FW_CFG_DMA_CTL_ERROR;
407             }
408         } else {
409             if (dma.length <= (e->len - s->cur_offset)) {
410                 len = dma.length;
411             } else {
412                 len = (e->len - s->cur_offset);
413             }
414 
415             /* If the access is not a read access, it will be a skip access,
416              * tested before.
417              */
418             if (read) {
419                 if (dma_memory_write(s->dma_as, dma.address,
420                                     &e->data[s->cur_offset], len)) {
421                     dma.control |= FW_CFG_DMA_CTL_ERROR;
422                 }
423             }
424             if (write) {
425                 if (!e->allow_write ||
426                     len != dma.length ||
427                     dma_memory_read(s->dma_as, dma.address,
428                                     &e->data[s->cur_offset], len)) {
429                     dma.control |= FW_CFG_DMA_CTL_ERROR;
430                 } else if (e->write_cb) {
431                     e->write_cb(e->callback_opaque, s->cur_offset, len);
432                 }
433             }
434 
435             s->cur_offset += len;
436         }
437 
438         dma.address += len;
439         dma.length  -= len;
440 
441     }
442 
443     stl_be_dma(s->dma_as, dma_addr + offsetof(FWCfgDmaAccess, control),
444                 dma.control);
445 
446     trace_fw_cfg_read(s, 0);
447 }
448 
449 static uint64_t fw_cfg_dma_mem_read(void *opaque, hwaddr addr,
450                                     unsigned size)
451 {
452     /* Return a signature value (and handle various read sizes) */
453     return extract64(FW_CFG_DMA_SIGNATURE, (8 - addr - size) * 8, size * 8);
454 }
455 
456 static void fw_cfg_dma_mem_write(void *opaque, hwaddr addr,
457                                  uint64_t value, unsigned size)
458 {
459     FWCfgState *s = opaque;
460 
461     if (size == 4) {
462         if (addr == 0) {
463             /* FWCfgDmaAccess high address */
464             s->dma_addr = value << 32;
465         } else if (addr == 4) {
466             /* FWCfgDmaAccess low address */
467             s->dma_addr |= value;
468             fw_cfg_dma_transfer(s);
469         }
470     } else if (size == 8 && addr == 0) {
471         s->dma_addr = value;
472         fw_cfg_dma_transfer(s);
473     }
474 }
475 
476 static bool fw_cfg_dma_mem_valid(void *opaque, hwaddr addr,
477                                  unsigned size, bool is_write,
478                                  MemTxAttrs attrs)
479 {
480     return !is_write || ((size == 4 && (addr == 0 || addr == 4)) ||
481                          (size == 8 && addr == 0));
482 }
483 
484 static bool fw_cfg_data_mem_valid(void *opaque, hwaddr addr,
485                                   unsigned size, bool is_write,
486                                   MemTxAttrs attrs)
487 {
488     return addr == 0;
489 }
490 
491 static uint64_t fw_cfg_ctl_mem_read(void *opaque, hwaddr addr, unsigned size)
492 {
493     return 0;
494 }
495 
496 static void fw_cfg_ctl_mem_write(void *opaque, hwaddr addr,
497                                  uint64_t value, unsigned size)
498 {
499     fw_cfg_select(opaque, (uint16_t)value);
500 }
501 
502 static bool fw_cfg_ctl_mem_valid(void *opaque, hwaddr addr,
503                                  unsigned size, bool is_write,
504                                  MemTxAttrs attrs)
505 {
506     return is_write && size == 2;
507 }
508 
509 static void fw_cfg_comb_write(void *opaque, hwaddr addr,
510                               uint64_t value, unsigned size)
511 {
512     switch (size) {
513     case 1:
514         fw_cfg_write(opaque, (uint8_t)value);
515         break;
516     case 2:
517         fw_cfg_select(opaque, (uint16_t)value);
518         break;
519     }
520 }
521 
522 static bool fw_cfg_comb_valid(void *opaque, hwaddr addr,
523                               unsigned size, bool is_write,
524                               MemTxAttrs attrs)
525 {
526     return (size == 1) || (is_write && size == 2);
527 }
528 
529 static const MemoryRegionOps fw_cfg_ctl_mem_ops = {
530     .read = fw_cfg_ctl_mem_read,
531     .write = fw_cfg_ctl_mem_write,
532     .endianness = DEVICE_BIG_ENDIAN,
533     .valid.accepts = fw_cfg_ctl_mem_valid,
534 };
535 
536 static const MemoryRegionOps fw_cfg_data_mem_ops = {
537     .read = fw_cfg_data_read,
538     .write = fw_cfg_data_mem_write,
539     .endianness = DEVICE_BIG_ENDIAN,
540     .valid = {
541         .min_access_size = 1,
542         .max_access_size = 1,
543         .accepts = fw_cfg_data_mem_valid,
544     },
545 };
546 
547 static const MemoryRegionOps fw_cfg_comb_mem_ops = {
548     .read = fw_cfg_data_read,
549     .write = fw_cfg_comb_write,
550     .endianness = DEVICE_LITTLE_ENDIAN,
551     .valid.accepts = fw_cfg_comb_valid,
552 };
553 
554 static const MemoryRegionOps fw_cfg_dma_mem_ops = {
555     .read = fw_cfg_dma_mem_read,
556     .write = fw_cfg_dma_mem_write,
557     .endianness = DEVICE_BIG_ENDIAN,
558     .valid.accepts = fw_cfg_dma_mem_valid,
559     .valid.max_access_size = 8,
560     .impl.max_access_size = 8,
561 };
562 
563 static void fw_cfg_reset(DeviceState *d)
564 {
565     FWCfgState *s = FW_CFG(d);
566 
567     /* we never register a read callback for FW_CFG_SIGNATURE */
568     fw_cfg_select(s, FW_CFG_SIGNATURE);
569 }
570 
571 /* Save restore 32 bit int as uint16_t
572    This is a Big hack, but it is how the old state did it.
573    Or we broke compatibility in the state, or we can't use struct tm
574  */
575 
576 static int get_uint32_as_uint16(QEMUFile *f, void *pv, size_t size,
577                                 const VMStateField *field)
578 {
579     uint32_t *v = pv;
580     *v = qemu_get_be16(f);
581     return 0;
582 }
583 
584 static int put_unused(QEMUFile *f, void *pv, size_t size,
585                       const VMStateField *field, QJSON *vmdesc)
586 {
587     fprintf(stderr, "uint32_as_uint16 is only used for backward compatibility.\n");
588     fprintf(stderr, "This functions shouldn't be called.\n");
589 
590     return 0;
591 }
592 
593 static const VMStateInfo vmstate_hack_uint32_as_uint16 = {
594     .name = "int32_as_uint16",
595     .get  = get_uint32_as_uint16,
596     .put  = put_unused,
597 };
598 
599 #define VMSTATE_UINT16_HACK(_f, _s, _t)                                    \
600     VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_hack_uint32_as_uint16, uint32_t)
601 
602 
603 static bool is_version_1(void *opaque, int version_id)
604 {
605     return version_id == 1;
606 }
607 
608 bool fw_cfg_dma_enabled(void *opaque)
609 {
610     FWCfgState *s = opaque;
611 
612     return s->dma_enabled;
613 }
614 
615 static bool fw_cfg_acpi_mr_restore(void *opaque)
616 {
617     FWCfgState *s = opaque;
618     bool mr_aligned;
619 
620     mr_aligned = QEMU_IS_ALIGNED(s->table_mr_size, qemu_real_host_page_size) &&
621                  QEMU_IS_ALIGNED(s->linker_mr_size, qemu_real_host_page_size) &&
622                  QEMU_IS_ALIGNED(s->rsdp_mr_size, qemu_real_host_page_size);
623     return s->acpi_mr_restore && !mr_aligned;
624 }
625 
626 static void fw_cfg_update_mr(FWCfgState *s, uint16_t key, size_t size)
627 {
628     MemoryRegion *mr;
629     ram_addr_t offset;
630     int arch = !!(key & FW_CFG_ARCH_LOCAL);
631     void *ptr;
632 
633     key &= FW_CFG_ENTRY_MASK;
634     assert(key < fw_cfg_max_entry(s));
635 
636     ptr = s->entries[arch][key].data;
637     mr = memory_region_from_host(ptr, &offset);
638 
639     memory_region_ram_resize(mr, size, &error_abort);
640 }
641 
642 static int fw_cfg_acpi_mr_restore_post_load(void *opaque, int version_id)
643 {
644     FWCfgState *s = opaque;
645     int i, index;
646 
647     assert(s->files);
648 
649     index = be32_to_cpu(s->files->count);
650 
651     for (i = 0; i < index; i++) {
652         if (!strcmp(s->files->f[i].name, ACPI_BUILD_TABLE_FILE)) {
653             fw_cfg_update_mr(s, FW_CFG_FILE_FIRST + i, s->table_mr_size);
654         } else if (!strcmp(s->files->f[i].name, ACPI_BUILD_LOADER_FILE)) {
655             fw_cfg_update_mr(s, FW_CFG_FILE_FIRST + i, s->linker_mr_size);
656         } else if (!strcmp(s->files->f[i].name, ACPI_BUILD_RSDP_FILE)) {
657             fw_cfg_update_mr(s, FW_CFG_FILE_FIRST + i, s->rsdp_mr_size);
658         }
659     }
660 
661     return 0;
662 }
663 
664 static const VMStateDescription vmstate_fw_cfg_dma = {
665     .name = "fw_cfg/dma",
666     .needed = fw_cfg_dma_enabled,
667     .fields = (VMStateField[]) {
668         VMSTATE_UINT64(dma_addr, FWCfgState),
669         VMSTATE_END_OF_LIST()
670     },
671 };
672 
673 static const VMStateDescription vmstate_fw_cfg_acpi_mr = {
674     .name = "fw_cfg/acpi_mr",
675     .version_id = 1,
676     .minimum_version_id = 1,
677     .needed = fw_cfg_acpi_mr_restore,
678     .post_load = fw_cfg_acpi_mr_restore_post_load,
679     .fields = (VMStateField[]) {
680         VMSTATE_UINT64(table_mr_size, FWCfgState),
681         VMSTATE_UINT64(linker_mr_size, FWCfgState),
682         VMSTATE_UINT64(rsdp_mr_size, FWCfgState),
683         VMSTATE_END_OF_LIST()
684     },
685 };
686 
687 static const VMStateDescription vmstate_fw_cfg = {
688     .name = "fw_cfg",
689     .version_id = 2,
690     .minimum_version_id = 1,
691     .fields = (VMStateField[]) {
692         VMSTATE_UINT16(cur_entry, FWCfgState),
693         VMSTATE_UINT16_HACK(cur_offset, FWCfgState, is_version_1),
694         VMSTATE_UINT32_V(cur_offset, FWCfgState, 2),
695         VMSTATE_END_OF_LIST()
696     },
697     .subsections = (const VMStateDescription*[]) {
698         &vmstate_fw_cfg_dma,
699         &vmstate_fw_cfg_acpi_mr,
700         NULL,
701     }
702 };
703 
704 static void fw_cfg_add_bytes_callback(FWCfgState *s, uint16_t key,
705                                       FWCfgCallback select_cb,
706                                       FWCfgWriteCallback write_cb,
707                                       void *callback_opaque,
708                                       void *data, size_t len,
709                                       bool read_only)
710 {
711     int arch = !!(key & FW_CFG_ARCH_LOCAL);
712 
713     key &= FW_CFG_ENTRY_MASK;
714 
715     assert(key < fw_cfg_max_entry(s) && len < UINT32_MAX);
716     assert(s->entries[arch][key].data == NULL); /* avoid key conflict */
717 
718     s->entries[arch][key].data = data;
719     s->entries[arch][key].len = (uint32_t)len;
720     s->entries[arch][key].select_cb = select_cb;
721     s->entries[arch][key].write_cb = write_cb;
722     s->entries[arch][key].callback_opaque = callback_opaque;
723     s->entries[arch][key].allow_write = !read_only;
724 }
725 
726 static void *fw_cfg_modify_bytes_read(FWCfgState *s, uint16_t key,
727                                               void *data, size_t len)
728 {
729     void *ptr;
730     int arch = !!(key & FW_CFG_ARCH_LOCAL);
731 
732     key &= FW_CFG_ENTRY_MASK;
733 
734     assert(key < fw_cfg_max_entry(s) && len < UINT32_MAX);
735 
736     /* return the old data to the function caller, avoid memory leak */
737     ptr = s->entries[arch][key].data;
738     s->entries[arch][key].data = data;
739     s->entries[arch][key].len = len;
740     s->entries[arch][key].callback_opaque = NULL;
741     s->entries[arch][key].allow_write = false;
742 
743     return ptr;
744 }
745 
746 void fw_cfg_add_bytes(FWCfgState *s, uint16_t key, void *data, size_t len)
747 {
748     trace_fw_cfg_add_bytes(key, trace_key_name(key), len);
749     fw_cfg_add_bytes_callback(s, key, NULL, NULL, NULL, data, len, true);
750 }
751 
752 void fw_cfg_add_string(FWCfgState *s, uint16_t key, const char *value)
753 {
754     size_t sz = strlen(value) + 1;
755 
756     trace_fw_cfg_add_string(key, trace_key_name(key), value);
757     fw_cfg_add_bytes(s, key, g_memdup(value, sz), sz);
758 }
759 
760 void fw_cfg_modify_string(FWCfgState *s, uint16_t key, const char *value)
761 {
762     size_t sz = strlen(value) + 1;
763     char *old;
764 
765     old = fw_cfg_modify_bytes_read(s, key, g_memdup(value, sz), sz);
766     g_free(old);
767 }
768 
769 void fw_cfg_add_i16(FWCfgState *s, uint16_t key, uint16_t value)
770 {
771     uint16_t *copy;
772 
773     copy = g_malloc(sizeof(value));
774     *copy = cpu_to_le16(value);
775     trace_fw_cfg_add_i16(key, trace_key_name(key), value);
776     fw_cfg_add_bytes(s, key, copy, sizeof(value));
777 }
778 
779 void fw_cfg_modify_i16(FWCfgState *s, uint16_t key, uint16_t value)
780 {
781     uint16_t *copy, *old;
782 
783     copy = g_malloc(sizeof(value));
784     *copy = cpu_to_le16(value);
785     old = fw_cfg_modify_bytes_read(s, key, copy, sizeof(value));
786     g_free(old);
787 }
788 
789 void fw_cfg_add_i32(FWCfgState *s, uint16_t key, uint32_t value)
790 {
791     uint32_t *copy;
792 
793     copy = g_malloc(sizeof(value));
794     *copy = cpu_to_le32(value);
795     trace_fw_cfg_add_i32(key, trace_key_name(key), value);
796     fw_cfg_add_bytes(s, key, copy, sizeof(value));
797 }
798 
799 void fw_cfg_modify_i32(FWCfgState *s, uint16_t key, uint32_t value)
800 {
801     uint32_t *copy, *old;
802 
803     copy = g_malloc(sizeof(value));
804     *copy = cpu_to_le32(value);
805     old = fw_cfg_modify_bytes_read(s, key, copy, sizeof(value));
806     g_free(old);
807 }
808 
809 void fw_cfg_add_i64(FWCfgState *s, uint16_t key, uint64_t value)
810 {
811     uint64_t *copy;
812 
813     copy = g_malloc(sizeof(value));
814     *copy = cpu_to_le64(value);
815     trace_fw_cfg_add_i64(key, trace_key_name(key), value);
816     fw_cfg_add_bytes(s, key, copy, sizeof(value));
817 }
818 
819 void fw_cfg_modify_i64(FWCfgState *s, uint16_t key, uint64_t value)
820 {
821     uint64_t *copy, *old;
822 
823     copy = g_malloc(sizeof(value));
824     *copy = cpu_to_le64(value);
825     old = fw_cfg_modify_bytes_read(s, key, copy, sizeof(value));
826     g_free(old);
827 }
828 
829 void fw_cfg_set_order_override(FWCfgState *s, int order)
830 {
831     assert(s->fw_cfg_order_override == 0);
832     s->fw_cfg_order_override = order;
833 }
834 
835 void fw_cfg_reset_order_override(FWCfgState *s)
836 {
837     assert(s->fw_cfg_order_override != 0);
838     s->fw_cfg_order_override = 0;
839 }
840 
841 /*
842  * This is the legacy order list.  For legacy systems, files are in
843  * the fw_cfg in the order defined below, by the "order" value.  Note
844  * that some entries (VGA ROMs, NIC option ROMS, etc.) go into a
845  * specific area, but there may be more than one and they occur in the
846  * order that the user specifies them on the command line.  Those are
847  * handled in a special manner, using the order override above.
848  *
849  * For non-legacy, the files are sorted by filename to avoid this kind
850  * of complexity in the future.
851  *
852  * This is only for x86, other arches don't implement versioning so
853  * they won't set legacy mode.
854  */
855 static struct {
856     const char *name;
857     int order;
858 } fw_cfg_order[] = {
859     { "etc/boot-menu-wait", 10 },
860     { "bootsplash.jpg", 11 },
861     { "bootsplash.bmp", 12 },
862     { "etc/boot-fail-wait", 15 },
863     { "etc/smbios/smbios-tables", 20 },
864     { "etc/smbios/smbios-anchor", 30 },
865     { "etc/e820", 40 },
866     { "etc/reserved-memory-end", 50 },
867     { "genroms/kvmvapic.bin", 55 },
868     { "genroms/linuxboot.bin", 60 },
869     { }, /* VGA ROMs from pc_vga_init come here, 70. */
870     { }, /* NIC option ROMs from pc_nic_init come here, 80. */
871     { "etc/system-states", 90 },
872     { }, /* User ROMs come here, 100. */
873     { }, /* Device FW comes here, 110. */
874     { "etc/extra-pci-roots", 120 },
875     { "etc/acpi/tables", 130 },
876     { "etc/table-loader", 140 },
877     { "etc/tpm/log", 150 },
878     { "etc/acpi/rsdp", 160 },
879     { "bootorder", 170 },
880 
881 #define FW_CFG_ORDER_OVERRIDE_LAST 200
882 };
883 
884 /*
885  * Any sub-page size update to these table MRs will be lost during migration,
886  * as we use aligned size in ram_load_precopy() -> qemu_ram_resize() path.
887  * In order to avoid the inconsistency in sizes save them seperately and
888  * migrate over in vmstate post_load().
889  */
890 static void fw_cfg_acpi_mr_save(FWCfgState *s, const char *filename, size_t len)
891 {
892     if (!strcmp(filename, ACPI_BUILD_TABLE_FILE)) {
893         s->table_mr_size = len;
894     } else if (!strcmp(filename, ACPI_BUILD_LOADER_FILE)) {
895         s->linker_mr_size = len;
896     } else if (!strcmp(filename, ACPI_BUILD_RSDP_FILE)) {
897         s->rsdp_mr_size = len;
898     }
899 }
900 
901 static int get_fw_cfg_order(FWCfgState *s, const char *name)
902 {
903     int i;
904 
905     if (s->fw_cfg_order_override > 0) {
906         return s->fw_cfg_order_override;
907     }
908 
909     for (i = 0; i < ARRAY_SIZE(fw_cfg_order); i++) {
910         if (fw_cfg_order[i].name == NULL) {
911             continue;
912         }
913 
914         if (strcmp(name, fw_cfg_order[i].name) == 0) {
915             return fw_cfg_order[i].order;
916         }
917     }
918 
919     /* Stick unknown stuff at the end. */
920     warn_report("Unknown firmware file in legacy mode: %s", name);
921     return FW_CFG_ORDER_OVERRIDE_LAST;
922 }
923 
924 void fw_cfg_add_file_callback(FWCfgState *s,  const char *filename,
925                               FWCfgCallback select_cb,
926                               FWCfgWriteCallback write_cb,
927                               void *callback_opaque,
928                               void *data, size_t len, bool read_only)
929 {
930     int i, index, count;
931     size_t dsize;
932     MachineClass *mc = MACHINE_GET_CLASS(qdev_get_machine());
933     int order = 0;
934 
935     if (!s->files) {
936         dsize = sizeof(uint32_t) + sizeof(FWCfgFile) * fw_cfg_file_slots(s);
937         s->files = g_malloc0(dsize);
938         fw_cfg_add_bytes(s, FW_CFG_FILE_DIR, s->files, dsize);
939     }
940 
941     count = be32_to_cpu(s->files->count);
942     assert(count < fw_cfg_file_slots(s));
943 
944     /* Find the insertion point. */
945     if (mc->legacy_fw_cfg_order) {
946         /*
947          * Sort by order. For files with the same order, we keep them
948          * in the sequence in which they were added.
949          */
950         order = get_fw_cfg_order(s, filename);
951         for (index = count;
952              index > 0 && order < s->entry_order[index - 1];
953              index--);
954     } else {
955         /* Sort by file name. */
956         for (index = count;
957              index > 0 && strcmp(filename, s->files->f[index - 1].name) < 0;
958              index--);
959     }
960 
961     /*
962      * Move all the entries from the index point and after down one
963      * to create a slot for the new entry.  Because calculations are
964      * being done with the index, make it so that "i" is the current
965      * index and "i - 1" is the one being copied from, thus the
966      * unusual start and end in the for statement.
967      */
968     for (i = count; i > index; i--) {
969         s->files->f[i] = s->files->f[i - 1];
970         s->files->f[i].select = cpu_to_be16(FW_CFG_FILE_FIRST + i);
971         s->entries[0][FW_CFG_FILE_FIRST + i] =
972             s->entries[0][FW_CFG_FILE_FIRST + i - 1];
973         s->entry_order[i] = s->entry_order[i - 1];
974     }
975 
976     memset(&s->files->f[index], 0, sizeof(FWCfgFile));
977     memset(&s->entries[0][FW_CFG_FILE_FIRST + index], 0, sizeof(FWCfgEntry));
978 
979     pstrcpy(s->files->f[index].name, sizeof(s->files->f[index].name), filename);
980     for (i = 0; i <= count; i++) {
981         if (i != index &&
982             strcmp(s->files->f[index].name, s->files->f[i].name) == 0) {
983             error_report("duplicate fw_cfg file name: %s",
984                          s->files->f[index].name);
985             exit(1);
986         }
987     }
988 
989     fw_cfg_add_bytes_callback(s, FW_CFG_FILE_FIRST + index,
990                               select_cb, write_cb,
991                               callback_opaque, data, len,
992                               read_only);
993 
994     s->files->f[index].size   = cpu_to_be32(len);
995     s->files->f[index].select = cpu_to_be16(FW_CFG_FILE_FIRST + index);
996     s->entry_order[index] = order;
997     trace_fw_cfg_add_file(s, index, s->files->f[index].name, len);
998 
999     s->files->count = cpu_to_be32(count+1);
1000     fw_cfg_acpi_mr_save(s, filename, len);
1001 }
1002 
1003 void fw_cfg_add_file(FWCfgState *s,  const char *filename,
1004                      void *data, size_t len)
1005 {
1006     fw_cfg_add_file_callback(s, filename, NULL, NULL, NULL, data, len, true);
1007 }
1008 
1009 void *fw_cfg_modify_file(FWCfgState *s, const char *filename,
1010                         void *data, size_t len)
1011 {
1012     int i, index;
1013     void *ptr = NULL;
1014 
1015     assert(s->files);
1016 
1017     index = be32_to_cpu(s->files->count);
1018 
1019     for (i = 0; i < index; i++) {
1020         if (strcmp(filename, s->files->f[i].name) == 0) {
1021             ptr = fw_cfg_modify_bytes_read(s, FW_CFG_FILE_FIRST + i,
1022                                            data, len);
1023             s->files->f[i].size   = cpu_to_be32(len);
1024             fw_cfg_acpi_mr_save(s, filename, len);
1025             return ptr;
1026         }
1027     }
1028 
1029     assert(index < fw_cfg_file_slots(s));
1030 
1031     /* add new one */
1032     fw_cfg_add_file_callback(s, filename, NULL, NULL, NULL, data, len, true);
1033     return NULL;
1034 }
1035 
1036 bool fw_cfg_add_from_generator(FWCfgState *s, const char *filename,
1037                                const char *gen_id, Error **errp)
1038 {
1039     FWCfgDataGeneratorClass *klass;
1040     GByteArray *array;
1041     Object *obj;
1042     gsize size;
1043 
1044     obj = object_resolve_path_component(object_get_objects_root(), gen_id);
1045     if (!obj) {
1046         error_setg(errp, "Cannot find object ID '%s'", gen_id);
1047         return false;
1048     }
1049     if (!object_dynamic_cast(obj, TYPE_FW_CFG_DATA_GENERATOR_INTERFACE)) {
1050         error_setg(errp, "Object ID '%s' is not a '%s' subclass",
1051                    gen_id, TYPE_FW_CFG_DATA_GENERATOR_INTERFACE);
1052         return false;
1053     }
1054     klass = FW_CFG_DATA_GENERATOR_GET_CLASS(obj);
1055     array = klass->get_data(obj, errp);
1056     if (!array) {
1057         return false;
1058     }
1059     size = array->len;
1060     fw_cfg_add_file(s, filename, g_byte_array_free(array, FALSE), size);
1061 
1062     return true;
1063 }
1064 
1065 void fw_cfg_add_extra_pci_roots(PCIBus *bus, FWCfgState *s)
1066 {
1067     int extra_hosts = 0;
1068 
1069     if (!bus) {
1070         return;
1071     }
1072 
1073     QLIST_FOREACH(bus, &bus->child, sibling) {
1074         /* look for expander root buses */
1075         if (pci_bus_is_root(bus)) {
1076             extra_hosts++;
1077         }
1078     }
1079 
1080     if (extra_hosts && s) {
1081         uint64_t *val = g_malloc(sizeof(*val));
1082         *val = cpu_to_le64(extra_hosts);
1083         fw_cfg_add_file(s, "etc/extra-pci-roots", val, sizeof(*val));
1084     }
1085 }
1086 
1087 static void fw_cfg_machine_reset(void *opaque)
1088 {
1089     MachineClass *mc = MACHINE_GET_CLASS(qdev_get_machine());
1090     FWCfgState *s = opaque;
1091     void *ptr;
1092     size_t len;
1093     char *buf;
1094 
1095     buf = get_boot_devices_list(&len);
1096     ptr = fw_cfg_modify_file(s, "bootorder", (uint8_t *)buf, len);
1097     g_free(ptr);
1098 
1099     if (!mc->legacy_fw_cfg_order) {
1100         buf = get_boot_devices_lchs_list(&len);
1101         ptr = fw_cfg_modify_file(s, "bios-geometry", (uint8_t *)buf, len);
1102         g_free(ptr);
1103     }
1104 }
1105 
1106 static void fw_cfg_machine_ready(struct Notifier *n, void *data)
1107 {
1108     FWCfgState *s = container_of(n, FWCfgState, machine_ready);
1109     qemu_register_reset(fw_cfg_machine_reset, s);
1110 }
1111 
1112 static Property fw_cfg_properties[] = {
1113     DEFINE_PROP_BOOL("acpi-mr-restore", FWCfgState, acpi_mr_restore, true),
1114     DEFINE_PROP_END_OF_LIST(),
1115 };
1116 
1117 static void fw_cfg_common_realize(DeviceState *dev, Error **errp)
1118 {
1119     FWCfgState *s = FW_CFG(dev);
1120     MachineState *machine = MACHINE(qdev_get_machine());
1121     uint32_t version = FW_CFG_VERSION;
1122 
1123     if (!fw_cfg_find()) {
1124         error_setg(errp, "at most one %s device is permitted", TYPE_FW_CFG);
1125         return;
1126     }
1127 
1128     fw_cfg_add_bytes(s, FW_CFG_SIGNATURE, (char *)"QEMU", 4);
1129     fw_cfg_add_bytes(s, FW_CFG_UUID, &qemu_uuid, 16);
1130     fw_cfg_add_i16(s, FW_CFG_NOGRAPHIC, (uint16_t)!machine->enable_graphics);
1131     fw_cfg_add_i16(s, FW_CFG_BOOT_MENU, (uint16_t)boot_menu);
1132     fw_cfg_bootsplash(s);
1133     fw_cfg_reboot(s);
1134 
1135     if (s->dma_enabled) {
1136         version |= FW_CFG_VERSION_DMA;
1137     }
1138 
1139     fw_cfg_add_i32(s, FW_CFG_ID, version);
1140 
1141     s->machine_ready.notify = fw_cfg_machine_ready;
1142     qemu_add_machine_init_done_notifier(&s->machine_ready);
1143 }
1144 
1145 FWCfgState *fw_cfg_init_io_dma(uint32_t iobase, uint32_t dma_iobase,
1146                                 AddressSpace *dma_as)
1147 {
1148     DeviceState *dev;
1149     SysBusDevice *sbd;
1150     FWCfgIoState *ios;
1151     FWCfgState *s;
1152     bool dma_requested = dma_iobase && dma_as;
1153 
1154     dev = qdev_new(TYPE_FW_CFG_IO);
1155     if (!dma_requested) {
1156         qdev_prop_set_bit(dev, "dma_enabled", false);
1157     }
1158 
1159     object_property_add_child(OBJECT(qdev_get_machine()), TYPE_FW_CFG,
1160                               OBJECT(dev));
1161 
1162     sbd = SYS_BUS_DEVICE(dev);
1163     sysbus_realize_and_unref(sbd, &error_fatal);
1164     ios = FW_CFG_IO(dev);
1165     sysbus_add_io(sbd, iobase, &ios->comb_iomem);
1166 
1167     s = FW_CFG(dev);
1168 
1169     if (s->dma_enabled) {
1170         /* 64 bits for the address field */
1171         s->dma_as = dma_as;
1172         s->dma_addr = 0;
1173         sysbus_add_io(sbd, dma_iobase, &s->dma_iomem);
1174     }
1175 
1176     return s;
1177 }
1178 
1179 FWCfgState *fw_cfg_init_io(uint32_t iobase)
1180 {
1181     return fw_cfg_init_io_dma(iobase, 0, NULL);
1182 }
1183 
1184 FWCfgState *fw_cfg_init_mem_wide(hwaddr ctl_addr,
1185                                  hwaddr data_addr, uint32_t data_width,
1186                                  hwaddr dma_addr, AddressSpace *dma_as)
1187 {
1188     DeviceState *dev;
1189     SysBusDevice *sbd;
1190     FWCfgState *s;
1191     bool dma_requested = dma_addr && dma_as;
1192 
1193     dev = qdev_new(TYPE_FW_CFG_MEM);
1194     qdev_prop_set_uint32(dev, "data_width", data_width);
1195     if (!dma_requested) {
1196         qdev_prop_set_bit(dev, "dma_enabled", false);
1197     }
1198 
1199     object_property_add_child(OBJECT(qdev_get_machine()), TYPE_FW_CFG,
1200                               OBJECT(dev));
1201 
1202     sbd = SYS_BUS_DEVICE(dev);
1203     sysbus_realize_and_unref(sbd, &error_fatal);
1204     sysbus_mmio_map(sbd, 0, ctl_addr);
1205     sysbus_mmio_map(sbd, 1, data_addr);
1206 
1207     s = FW_CFG(dev);
1208 
1209     if (s->dma_enabled) {
1210         s->dma_as = dma_as;
1211         s->dma_addr = 0;
1212         sysbus_mmio_map(sbd, 2, dma_addr);
1213     }
1214 
1215     return s;
1216 }
1217 
1218 FWCfgState *fw_cfg_init_mem(hwaddr ctl_addr, hwaddr data_addr)
1219 {
1220     return fw_cfg_init_mem_wide(ctl_addr, data_addr,
1221                                 fw_cfg_data_mem_ops.valid.max_access_size,
1222                                 0, NULL);
1223 }
1224 
1225 
1226 FWCfgState *fw_cfg_find(void)
1227 {
1228     /* Returns NULL unless there is exactly one fw_cfg device */
1229     return FW_CFG(object_resolve_path_type("", TYPE_FW_CFG, NULL));
1230 }
1231 
1232 
1233 static void fw_cfg_class_init(ObjectClass *klass, void *data)
1234 {
1235     DeviceClass *dc = DEVICE_CLASS(klass);
1236 
1237     dc->reset = fw_cfg_reset;
1238     dc->vmsd = &vmstate_fw_cfg;
1239 
1240     device_class_set_props(dc, fw_cfg_properties);
1241 }
1242 
1243 static const TypeInfo fw_cfg_info = {
1244     .name          = TYPE_FW_CFG,
1245     .parent        = TYPE_SYS_BUS_DEVICE,
1246     .abstract      = true,
1247     .instance_size = sizeof(FWCfgState),
1248     .class_init    = fw_cfg_class_init,
1249 };
1250 
1251 static void fw_cfg_file_slots_allocate(FWCfgState *s, Error **errp)
1252 {
1253     uint16_t file_slots_max;
1254 
1255     if (fw_cfg_file_slots(s) < FW_CFG_FILE_SLOTS_MIN) {
1256         error_setg(errp, "\"file_slots\" must be at least 0x%x",
1257                    FW_CFG_FILE_SLOTS_MIN);
1258         return;
1259     }
1260 
1261     /* (UINT16_MAX & FW_CFG_ENTRY_MASK) is the highest inclusive selector value
1262      * that we permit. The actual (exclusive) value coming from the
1263      * configuration is (FW_CFG_FILE_FIRST + fw_cfg_file_slots(s)). */
1264     file_slots_max = (UINT16_MAX & FW_CFG_ENTRY_MASK) - FW_CFG_FILE_FIRST + 1;
1265     if (fw_cfg_file_slots(s) > file_slots_max) {
1266         error_setg(errp, "\"file_slots\" must not exceed 0x%" PRIx16,
1267                    file_slots_max);
1268         return;
1269     }
1270 
1271     s->entries[0] = g_new0(FWCfgEntry, fw_cfg_max_entry(s));
1272     s->entries[1] = g_new0(FWCfgEntry, fw_cfg_max_entry(s));
1273     s->entry_order = g_new0(int, fw_cfg_max_entry(s));
1274 }
1275 
1276 static Property fw_cfg_io_properties[] = {
1277     DEFINE_PROP_BOOL("dma_enabled", FWCfgIoState, parent_obj.dma_enabled,
1278                      true),
1279     DEFINE_PROP_UINT16("x-file-slots", FWCfgIoState, parent_obj.file_slots,
1280                        FW_CFG_FILE_SLOTS_DFLT),
1281     DEFINE_PROP_END_OF_LIST(),
1282 };
1283 
1284 static void fw_cfg_io_realize(DeviceState *dev, Error **errp)
1285 {
1286     ERRP_GUARD();
1287     FWCfgIoState *s = FW_CFG_IO(dev);
1288 
1289     fw_cfg_file_slots_allocate(FW_CFG(s), errp);
1290     if (*errp) {
1291         return;
1292     }
1293 
1294     /* when using port i/o, the 8-bit data register ALWAYS overlaps
1295      * with half of the 16-bit control register. Hence, the total size
1296      * of the i/o region used is FW_CFG_CTL_SIZE */
1297     memory_region_init_io(&s->comb_iomem, OBJECT(s), &fw_cfg_comb_mem_ops,
1298                           FW_CFG(s), "fwcfg", FW_CFG_CTL_SIZE);
1299 
1300     if (FW_CFG(s)->dma_enabled) {
1301         memory_region_init_io(&FW_CFG(s)->dma_iomem, OBJECT(s),
1302                               &fw_cfg_dma_mem_ops, FW_CFG(s), "fwcfg.dma",
1303                               sizeof(dma_addr_t));
1304     }
1305 
1306     fw_cfg_common_realize(dev, errp);
1307 }
1308 
1309 static void fw_cfg_io_class_init(ObjectClass *klass, void *data)
1310 {
1311     DeviceClass *dc = DEVICE_CLASS(klass);
1312 
1313     dc->realize = fw_cfg_io_realize;
1314     device_class_set_props(dc, fw_cfg_io_properties);
1315 }
1316 
1317 static const TypeInfo fw_cfg_io_info = {
1318     .name          = TYPE_FW_CFG_IO,
1319     .parent        = TYPE_FW_CFG,
1320     .instance_size = sizeof(FWCfgIoState),
1321     .class_init    = fw_cfg_io_class_init,
1322 };
1323 
1324 
1325 static Property fw_cfg_mem_properties[] = {
1326     DEFINE_PROP_UINT32("data_width", FWCfgMemState, data_width, -1),
1327     DEFINE_PROP_BOOL("dma_enabled", FWCfgMemState, parent_obj.dma_enabled,
1328                      true),
1329     DEFINE_PROP_UINT16("x-file-slots", FWCfgMemState, parent_obj.file_slots,
1330                        FW_CFG_FILE_SLOTS_DFLT),
1331     DEFINE_PROP_END_OF_LIST(),
1332 };
1333 
1334 static void fw_cfg_mem_realize(DeviceState *dev, Error **errp)
1335 {
1336     ERRP_GUARD();
1337     FWCfgMemState *s = FW_CFG_MEM(dev);
1338     SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
1339     const MemoryRegionOps *data_ops = &fw_cfg_data_mem_ops;
1340 
1341     fw_cfg_file_slots_allocate(FW_CFG(s), errp);
1342     if (*errp) {
1343         return;
1344     }
1345 
1346     memory_region_init_io(&s->ctl_iomem, OBJECT(s), &fw_cfg_ctl_mem_ops,
1347                           FW_CFG(s), "fwcfg.ctl", FW_CFG_CTL_SIZE);
1348     sysbus_init_mmio(sbd, &s->ctl_iomem);
1349 
1350     if (s->data_width > data_ops->valid.max_access_size) {
1351         s->wide_data_ops = *data_ops;
1352 
1353         s->wide_data_ops.valid.max_access_size = s->data_width;
1354         s->wide_data_ops.impl.max_access_size  = s->data_width;
1355         data_ops = &s->wide_data_ops;
1356     }
1357     memory_region_init_io(&s->data_iomem, OBJECT(s), data_ops, FW_CFG(s),
1358                           "fwcfg.data", data_ops->valid.max_access_size);
1359     sysbus_init_mmio(sbd, &s->data_iomem);
1360 
1361     if (FW_CFG(s)->dma_enabled) {
1362         memory_region_init_io(&FW_CFG(s)->dma_iomem, OBJECT(s),
1363                               &fw_cfg_dma_mem_ops, FW_CFG(s), "fwcfg.dma",
1364                               sizeof(dma_addr_t));
1365         sysbus_init_mmio(sbd, &FW_CFG(s)->dma_iomem);
1366     }
1367 
1368     fw_cfg_common_realize(dev, errp);
1369 }
1370 
1371 static void fw_cfg_mem_class_init(ObjectClass *klass, void *data)
1372 {
1373     DeviceClass *dc = DEVICE_CLASS(klass);
1374 
1375     dc->realize = fw_cfg_mem_realize;
1376     device_class_set_props(dc, fw_cfg_mem_properties);
1377 }
1378 
1379 static const TypeInfo fw_cfg_mem_info = {
1380     .name          = TYPE_FW_CFG_MEM,
1381     .parent        = TYPE_FW_CFG,
1382     .instance_size = sizeof(FWCfgMemState),
1383     .class_init    = fw_cfg_mem_class_init,
1384 };
1385 
1386 static void fw_cfg_register_types(void)
1387 {
1388     type_register_static(&fw_cfg_info);
1389     type_register_static(&fw_cfg_io_info);
1390     type_register_static(&fw_cfg_mem_info);
1391 }
1392 
1393 type_init(fw_cfg_register_types)
1394