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