qemu-sockets: do not test path with access() before unlinking
[qemu/ar7.git] / hw / nvram / fw_cfg.c
blob73b0a813a724091fbf3c0f8cb695c10c99ad4dbf
1 /*
2 * QEMU Firmware configuration device emulation
4 * Copyright (c) 2008 Gleb Natapov
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
24 #include "hw/hw.h"
25 #include "sysemu/sysemu.h"
26 #include "sysemu/dma.h"
27 #include "hw/isa/isa.h"
28 #include "hw/nvram/fw_cfg.h"
29 #include "hw/sysbus.h"
30 #include "trace.h"
31 #include "qemu/error-report.h"
32 #include "qemu/config-file.h"
34 #define FW_CFG_CTL_SIZE 2
35 #define FW_CFG_NAME "fw_cfg"
36 #define FW_CFG_PATH "/machine/" FW_CFG_NAME
38 #define TYPE_FW_CFG "fw_cfg"
39 #define TYPE_FW_CFG_IO "fw_cfg_io"
40 #define TYPE_FW_CFG_MEM "fw_cfg_mem"
42 #define FW_CFG(obj) OBJECT_CHECK(FWCfgState, (obj), TYPE_FW_CFG)
43 #define FW_CFG_IO(obj) OBJECT_CHECK(FWCfgIoState, (obj), TYPE_FW_CFG_IO)
44 #define FW_CFG_MEM(obj) OBJECT_CHECK(FWCfgMemState, (obj), TYPE_FW_CFG_MEM)
46 /* FW_CFG_VERSION bits */
47 #define FW_CFG_VERSION 0x01
48 #define FW_CFG_VERSION_DMA 0x02
50 /* FW_CFG_DMA_CONTROL bits */
51 #define FW_CFG_DMA_CTL_ERROR 0x01
52 #define FW_CFG_DMA_CTL_READ 0x02
53 #define FW_CFG_DMA_CTL_SKIP 0x04
54 #define FW_CFG_DMA_CTL_SELECT 0x08
56 #define FW_CFG_DMA_SIGNATURE 0x51454d5520434647ULL /* "QEMU CFG" */
58 typedef struct FWCfgEntry {
59 uint32_t len;
60 uint8_t *data;
61 void *callback_opaque;
62 FWCfgReadCallback read_callback;
63 } FWCfgEntry;
65 struct FWCfgState {
66 /*< private >*/
67 SysBusDevice parent_obj;
68 /*< public >*/
70 FWCfgEntry entries[2][FW_CFG_MAX_ENTRY];
71 FWCfgFiles *files;
72 uint16_t cur_entry;
73 uint32_t cur_offset;
74 Notifier machine_ready;
76 bool dma_enabled;
77 dma_addr_t dma_addr;
78 AddressSpace *dma_as;
79 MemoryRegion dma_iomem;
82 struct FWCfgIoState {
83 /*< private >*/
84 FWCfgState parent_obj;
85 /*< public >*/
87 MemoryRegion comb_iomem;
88 uint32_t iobase, dma_iobase;
91 struct FWCfgMemState {
92 /*< private >*/
93 FWCfgState parent_obj;
94 /*< public >*/
96 MemoryRegion ctl_iomem, data_iomem;
97 uint32_t data_width;
98 MemoryRegionOps wide_data_ops;
101 #define JPG_FILE 0
102 #define BMP_FILE 1
104 static char *read_splashfile(char *filename, gsize *file_sizep,
105 int *file_typep)
107 GError *err = NULL;
108 gboolean res;
109 gchar *content;
110 int file_type;
111 unsigned int filehead;
112 int bmp_bpp;
114 res = g_file_get_contents(filename, &content, file_sizep, &err);
115 if (res == FALSE) {
116 error_report("failed to read splash file '%s'", filename);
117 g_error_free(err);
118 return NULL;
121 /* check file size */
122 if (*file_sizep < 30) {
123 goto error;
126 /* check magic ID */
127 filehead = ((content[0] & 0xff) + (content[1] << 8)) & 0xffff;
128 if (filehead == 0xd8ff) {
129 file_type = JPG_FILE;
130 } else if (filehead == 0x4d42) {
131 file_type = BMP_FILE;
132 } else {
133 goto error;
136 /* check BMP bpp */
137 if (file_type == BMP_FILE) {
138 bmp_bpp = (content[28] + (content[29] << 8)) & 0xffff;
139 if (bmp_bpp != 24) {
140 goto error;
144 /* return values */
145 *file_typep = file_type;
147 return content;
149 error:
150 error_report("splash file '%s' format not recognized; must be JPEG "
151 "or 24 bit BMP", filename);
152 g_free(content);
153 return NULL;
156 static void fw_cfg_bootsplash(FWCfgState *s)
158 int boot_splash_time = -1;
159 const char *boot_splash_filename = NULL;
160 char *p;
161 char *filename, *file_data;
162 gsize file_size;
163 int file_type;
164 const char *temp;
166 /* get user configuration */
167 QemuOptsList *plist = qemu_find_opts("boot-opts");
168 QemuOpts *opts = QTAILQ_FIRST(&plist->head);
169 if (opts != NULL) {
170 temp = qemu_opt_get(opts, "splash");
171 if (temp != NULL) {
172 boot_splash_filename = temp;
174 temp = qemu_opt_get(opts, "splash-time");
175 if (temp != NULL) {
176 p = (char *)temp;
177 boot_splash_time = strtol(p, (char **)&p, 10);
181 /* insert splash time if user configurated */
182 if (boot_splash_time >= 0) {
183 /* validate the input */
184 if (boot_splash_time > 0xffff) {
185 error_report("splash time is big than 65535, force it to 65535.");
186 boot_splash_time = 0xffff;
188 /* use little endian format */
189 qemu_extra_params_fw[0] = (uint8_t)(boot_splash_time & 0xff);
190 qemu_extra_params_fw[1] = (uint8_t)((boot_splash_time >> 8) & 0xff);
191 fw_cfg_add_file(s, "etc/boot-menu-wait", qemu_extra_params_fw, 2);
194 /* insert splash file if user configurated */
195 if (boot_splash_filename != NULL) {
196 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, boot_splash_filename);
197 if (filename == NULL) {
198 error_report("failed to find file '%s'.", boot_splash_filename);
199 return;
202 /* loading file data */
203 file_data = read_splashfile(filename, &file_size, &file_type);
204 if (file_data == NULL) {
205 g_free(filename);
206 return;
208 g_free(boot_splash_filedata);
209 boot_splash_filedata = (uint8_t *)file_data;
210 boot_splash_filedata_size = file_size;
212 /* insert data */
213 if (file_type == JPG_FILE) {
214 fw_cfg_add_file(s, "bootsplash.jpg",
215 boot_splash_filedata, boot_splash_filedata_size);
216 } else {
217 fw_cfg_add_file(s, "bootsplash.bmp",
218 boot_splash_filedata, boot_splash_filedata_size);
220 g_free(filename);
224 static void fw_cfg_reboot(FWCfgState *s)
226 int reboot_timeout = -1;
227 char *p;
228 const char *temp;
230 /* get user configuration */
231 QemuOptsList *plist = qemu_find_opts("boot-opts");
232 QemuOpts *opts = QTAILQ_FIRST(&plist->head);
233 if (opts != NULL) {
234 temp = qemu_opt_get(opts, "reboot-timeout");
235 if (temp != NULL) {
236 p = (char *)temp;
237 reboot_timeout = strtol(p, (char **)&p, 10);
240 /* validate the input */
241 if (reboot_timeout > 0xffff) {
242 error_report("reboot timeout is larger than 65535, force it to 65535.");
243 reboot_timeout = 0xffff;
245 fw_cfg_add_file(s, "etc/boot-fail-wait", g_memdup(&reboot_timeout, 4), 4);
248 static void fw_cfg_write(FWCfgState *s, uint8_t value)
250 /* nothing, write support removed in QEMU v2.4+ */
253 static int fw_cfg_select(FWCfgState *s, uint16_t key)
255 int ret;
257 s->cur_offset = 0;
258 if ((key & FW_CFG_ENTRY_MASK) >= FW_CFG_MAX_ENTRY) {
259 s->cur_entry = FW_CFG_INVALID;
260 ret = 0;
261 } else {
262 s->cur_entry = key;
263 ret = 1;
266 trace_fw_cfg_select(s, key, ret);
267 return ret;
270 static uint8_t fw_cfg_read(FWCfgState *s)
272 int arch = !!(s->cur_entry & FW_CFG_ARCH_LOCAL);
273 FWCfgEntry *e = &s->entries[arch][s->cur_entry & FW_CFG_ENTRY_MASK];
274 uint8_t ret;
276 if (s->cur_entry == FW_CFG_INVALID || !e->data || s->cur_offset >= e->len)
277 ret = 0;
278 else {
279 if (e->read_callback) {
280 e->read_callback(e->callback_opaque, s->cur_offset);
282 ret = e->data[s->cur_offset++];
285 trace_fw_cfg_read(s, ret);
286 return ret;
289 static uint64_t fw_cfg_data_mem_read(void *opaque, hwaddr addr,
290 unsigned size)
292 FWCfgState *s = opaque;
293 uint64_t value = 0;
294 unsigned i;
296 for (i = 0; i < size; ++i) {
297 value = (value << 8) | fw_cfg_read(s);
299 return value;
302 static void fw_cfg_data_mem_write(void *opaque, hwaddr addr,
303 uint64_t value, unsigned size)
305 FWCfgState *s = opaque;
306 unsigned i = size;
308 do {
309 fw_cfg_write(s, value >> (8 * --i));
310 } while (i);
313 static void fw_cfg_dma_transfer(FWCfgState *s)
315 dma_addr_t len;
316 FWCfgDmaAccess dma;
317 int arch;
318 FWCfgEntry *e;
319 int read;
320 dma_addr_t dma_addr;
322 /* Reset the address before the next access */
323 dma_addr = s->dma_addr;
324 s->dma_addr = 0;
326 if (dma_memory_read(s->dma_as, dma_addr, &dma, sizeof(dma))) {
327 stl_be_dma(s->dma_as, dma_addr + offsetof(FWCfgDmaAccess, control),
328 FW_CFG_DMA_CTL_ERROR);
329 return;
332 dma.address = be64_to_cpu(dma.address);
333 dma.length = be32_to_cpu(dma.length);
334 dma.control = be32_to_cpu(dma.control);
336 if (dma.control & FW_CFG_DMA_CTL_SELECT) {
337 fw_cfg_select(s, dma.control >> 16);
340 arch = !!(s->cur_entry & FW_CFG_ARCH_LOCAL);
341 e = &s->entries[arch][s->cur_entry & FW_CFG_ENTRY_MASK];
343 if (dma.control & FW_CFG_DMA_CTL_READ) {
344 read = 1;
345 } else if (dma.control & FW_CFG_DMA_CTL_SKIP) {
346 read = 0;
347 } else {
348 dma.length = 0;
351 dma.control = 0;
353 while (dma.length > 0 && !(dma.control & FW_CFG_DMA_CTL_ERROR)) {
354 if (s->cur_entry == FW_CFG_INVALID || !e->data ||
355 s->cur_offset >= e->len) {
356 len = dma.length;
358 /* If the access is not a read access, it will be a skip access,
359 * tested before.
361 if (read) {
362 if (dma_memory_set(s->dma_as, dma.address, 0, len)) {
363 dma.control |= FW_CFG_DMA_CTL_ERROR;
367 } else {
368 if (dma.length <= (e->len - s->cur_offset)) {
369 len = dma.length;
370 } else {
371 len = (e->len - s->cur_offset);
374 if (e->read_callback) {
375 e->read_callback(e->callback_opaque, s->cur_offset);
378 /* If the access is not a read access, it will be a skip access,
379 * tested before.
381 if (read) {
382 if (dma_memory_write(s->dma_as, dma.address,
383 &e->data[s->cur_offset], len)) {
384 dma.control |= FW_CFG_DMA_CTL_ERROR;
388 s->cur_offset += len;
391 dma.address += len;
392 dma.length -= len;
396 stl_be_dma(s->dma_as, dma_addr + offsetof(FWCfgDmaAccess, control),
397 dma.control);
399 trace_fw_cfg_read(s, 0);
402 static uint64_t fw_cfg_dma_mem_read(void *opaque, hwaddr addr,
403 unsigned size)
405 /* Return a signature value (and handle various read sizes) */
406 return extract64(FW_CFG_DMA_SIGNATURE, (8 - addr - size) * 8, size * 8);
409 static void fw_cfg_dma_mem_write(void *opaque, hwaddr addr,
410 uint64_t value, unsigned size)
412 FWCfgState *s = opaque;
414 if (size == 4) {
415 if (addr == 0) {
416 /* FWCfgDmaAccess high address */
417 s->dma_addr = value << 32;
418 } else if (addr == 4) {
419 /* FWCfgDmaAccess low address */
420 s->dma_addr |= value;
421 fw_cfg_dma_transfer(s);
423 } else if (size == 8 && addr == 0) {
424 s->dma_addr = value;
425 fw_cfg_dma_transfer(s);
429 static bool fw_cfg_dma_mem_valid(void *opaque, hwaddr addr,
430 unsigned size, bool is_write)
432 return !is_write || ((size == 4 && (addr == 0 || addr == 4)) ||
433 (size == 8 && addr == 0));
436 static bool fw_cfg_data_mem_valid(void *opaque, hwaddr addr,
437 unsigned size, bool is_write)
439 return addr == 0;
442 static void fw_cfg_ctl_mem_write(void *opaque, hwaddr addr,
443 uint64_t value, unsigned size)
445 fw_cfg_select(opaque, (uint16_t)value);
448 static bool fw_cfg_ctl_mem_valid(void *opaque, hwaddr addr,
449 unsigned size, bool is_write)
451 return is_write && size == 2;
454 static uint64_t fw_cfg_comb_read(void *opaque, hwaddr addr,
455 unsigned size)
457 return fw_cfg_read(opaque);
460 static void fw_cfg_comb_write(void *opaque, hwaddr addr,
461 uint64_t value, unsigned size)
463 switch (size) {
464 case 1:
465 fw_cfg_write(opaque, (uint8_t)value);
466 break;
467 case 2:
468 fw_cfg_select(opaque, (uint16_t)value);
469 break;
473 static bool fw_cfg_comb_valid(void *opaque, hwaddr addr,
474 unsigned size, bool is_write)
476 return (size == 1) || (is_write && size == 2);
479 static const MemoryRegionOps fw_cfg_ctl_mem_ops = {
480 .write = fw_cfg_ctl_mem_write,
481 .endianness = DEVICE_BIG_ENDIAN,
482 .valid.accepts = fw_cfg_ctl_mem_valid,
485 static const MemoryRegionOps fw_cfg_data_mem_ops = {
486 .read = fw_cfg_data_mem_read,
487 .write = fw_cfg_data_mem_write,
488 .endianness = DEVICE_BIG_ENDIAN,
489 .valid = {
490 .min_access_size = 1,
491 .max_access_size = 1,
492 .accepts = fw_cfg_data_mem_valid,
496 static const MemoryRegionOps fw_cfg_comb_mem_ops = {
497 .read = fw_cfg_comb_read,
498 .write = fw_cfg_comb_write,
499 .endianness = DEVICE_LITTLE_ENDIAN,
500 .valid.accepts = fw_cfg_comb_valid,
503 static const MemoryRegionOps fw_cfg_dma_mem_ops = {
504 .read = fw_cfg_dma_mem_read,
505 .write = fw_cfg_dma_mem_write,
506 .endianness = DEVICE_BIG_ENDIAN,
507 .valid.accepts = fw_cfg_dma_mem_valid,
508 .valid.max_access_size = 8,
509 .impl.max_access_size = 8,
512 static void fw_cfg_reset(DeviceState *d)
514 FWCfgState *s = FW_CFG(d);
516 fw_cfg_select(s, 0);
519 /* Save restore 32 bit int as uint16_t
520 This is a Big hack, but it is how the old state did it.
521 Or we broke compatibility in the state, or we can't use struct tm
524 static int get_uint32_as_uint16(QEMUFile *f, void *pv, size_t size)
526 uint32_t *v = pv;
527 *v = qemu_get_be16(f);
528 return 0;
531 static void put_unused(QEMUFile *f, void *pv, size_t size)
533 fprintf(stderr, "uint32_as_uint16 is only used for backward compatibility.\n");
534 fprintf(stderr, "This functions shouldn't be called.\n");
537 static const VMStateInfo vmstate_hack_uint32_as_uint16 = {
538 .name = "int32_as_uint16",
539 .get = get_uint32_as_uint16,
540 .put = put_unused,
543 #define VMSTATE_UINT16_HACK(_f, _s, _t) \
544 VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_hack_uint32_as_uint16, uint32_t)
547 static bool is_version_1(void *opaque, int version_id)
549 return version_id == 1;
552 static bool fw_cfg_dma_enabled(void *opaque)
554 FWCfgState *s = opaque;
556 return s->dma_enabled;
559 static const VMStateDescription vmstate_fw_cfg_dma = {
560 .name = "fw_cfg/dma",
561 .needed = fw_cfg_dma_enabled,
562 .fields = (VMStateField[]) {
563 VMSTATE_UINT64(dma_addr, FWCfgState),
564 VMSTATE_END_OF_LIST()
568 static const VMStateDescription vmstate_fw_cfg = {
569 .name = "fw_cfg",
570 .version_id = 2,
571 .minimum_version_id = 1,
572 .fields = (VMStateField[]) {
573 VMSTATE_UINT16(cur_entry, FWCfgState),
574 VMSTATE_UINT16_HACK(cur_offset, FWCfgState, is_version_1),
575 VMSTATE_UINT32_V(cur_offset, FWCfgState, 2),
576 VMSTATE_END_OF_LIST()
578 .subsections = (const VMStateDescription*[]) {
579 &vmstate_fw_cfg_dma,
580 NULL,
584 static void fw_cfg_add_bytes_read_callback(FWCfgState *s, uint16_t key,
585 FWCfgReadCallback callback,
586 void *callback_opaque,
587 void *data, size_t len)
589 int arch = !!(key & FW_CFG_ARCH_LOCAL);
591 key &= FW_CFG_ENTRY_MASK;
593 assert(key < FW_CFG_MAX_ENTRY && len < UINT32_MAX);
594 assert(s->entries[arch][key].data == NULL); /* avoid key conflict */
596 s->entries[arch][key].data = data;
597 s->entries[arch][key].len = (uint32_t)len;
598 s->entries[arch][key].read_callback = callback;
599 s->entries[arch][key].callback_opaque = callback_opaque;
602 static void *fw_cfg_modify_bytes_read(FWCfgState *s, uint16_t key,
603 void *data, size_t len)
605 void *ptr;
606 int arch = !!(key & FW_CFG_ARCH_LOCAL);
608 key &= FW_CFG_ENTRY_MASK;
610 assert(key < FW_CFG_MAX_ENTRY && len < UINT32_MAX);
612 /* return the old data to the function caller, avoid memory leak */
613 ptr = s->entries[arch][key].data;
614 s->entries[arch][key].data = data;
615 s->entries[arch][key].len = len;
616 s->entries[arch][key].callback_opaque = NULL;
618 return ptr;
621 void fw_cfg_add_bytes(FWCfgState *s, uint16_t key, void *data, size_t len)
623 fw_cfg_add_bytes_read_callback(s, key, NULL, NULL, data, len);
626 void fw_cfg_add_string(FWCfgState *s, uint16_t key, const char *value)
628 size_t sz = strlen(value) + 1;
630 fw_cfg_add_bytes(s, key, g_memdup(value, sz), sz);
633 void fw_cfg_add_i16(FWCfgState *s, uint16_t key, uint16_t value)
635 uint16_t *copy;
637 copy = g_malloc(sizeof(value));
638 *copy = cpu_to_le16(value);
639 fw_cfg_add_bytes(s, key, copy, sizeof(value));
642 void fw_cfg_modify_i16(FWCfgState *s, uint16_t key, uint16_t value)
644 uint16_t *copy, *old;
646 copy = g_malloc(sizeof(value));
647 *copy = cpu_to_le16(value);
648 old = fw_cfg_modify_bytes_read(s, key, copy, sizeof(value));
649 g_free(old);
652 void fw_cfg_add_i32(FWCfgState *s, uint16_t key, uint32_t value)
654 uint32_t *copy;
656 copy = g_malloc(sizeof(value));
657 *copy = cpu_to_le32(value);
658 fw_cfg_add_bytes(s, key, copy, sizeof(value));
661 void fw_cfg_add_i64(FWCfgState *s, uint16_t key, uint64_t value)
663 uint64_t *copy;
665 copy = g_malloc(sizeof(value));
666 *copy = cpu_to_le64(value);
667 fw_cfg_add_bytes(s, key, copy, sizeof(value));
670 void fw_cfg_add_file_callback(FWCfgState *s, const char *filename,
671 FWCfgReadCallback callback, void *callback_opaque,
672 void *data, size_t len)
674 int i, index;
675 size_t dsize;
677 if (!s->files) {
678 dsize = sizeof(uint32_t) + sizeof(FWCfgFile) * FW_CFG_FILE_SLOTS;
679 s->files = g_malloc0(dsize);
680 fw_cfg_add_bytes(s, FW_CFG_FILE_DIR, s->files, dsize);
683 index = be32_to_cpu(s->files->count);
684 assert(index < FW_CFG_FILE_SLOTS);
686 pstrcpy(s->files->f[index].name, sizeof(s->files->f[index].name),
687 filename);
688 for (i = 0; i < index; i++) {
689 if (strcmp(s->files->f[index].name, s->files->f[i].name) == 0) {
690 error_report("duplicate fw_cfg file name: %s",
691 s->files->f[index].name);
692 exit(1);
696 fw_cfg_add_bytes_read_callback(s, FW_CFG_FILE_FIRST + index,
697 callback, callback_opaque, data, len);
699 s->files->f[index].size = cpu_to_be32(len);
700 s->files->f[index].select = cpu_to_be16(FW_CFG_FILE_FIRST + index);
701 trace_fw_cfg_add_file(s, index, s->files->f[index].name, len);
703 s->files->count = cpu_to_be32(index+1);
706 void fw_cfg_add_file(FWCfgState *s, const char *filename,
707 void *data, size_t len)
709 fw_cfg_add_file_callback(s, filename, NULL, NULL, data, len);
712 void *fw_cfg_modify_file(FWCfgState *s, const char *filename,
713 void *data, size_t len)
715 int i, index;
716 void *ptr = NULL;
718 assert(s->files);
720 index = be32_to_cpu(s->files->count);
721 assert(index < FW_CFG_FILE_SLOTS);
723 for (i = 0; i < index; i++) {
724 if (strcmp(filename, s->files->f[i].name) == 0) {
725 ptr = fw_cfg_modify_bytes_read(s, FW_CFG_FILE_FIRST + i,
726 data, len);
727 s->files->f[i].size = cpu_to_be32(len);
728 return ptr;
731 /* add new one */
732 fw_cfg_add_file_callback(s, filename, NULL, NULL, data, len);
733 return NULL;
736 static void fw_cfg_machine_reset(void *opaque)
738 void *ptr;
739 size_t len;
740 FWCfgState *s = opaque;
741 char *bootindex = get_boot_devices_list(&len, false);
743 ptr = fw_cfg_modify_file(s, "bootorder", (uint8_t *)bootindex, len);
744 g_free(ptr);
747 static void fw_cfg_machine_ready(struct Notifier *n, void *data)
749 FWCfgState *s = container_of(n, FWCfgState, machine_ready);
750 qemu_register_reset(fw_cfg_machine_reset, s);
755 static void fw_cfg_init1(DeviceState *dev)
757 FWCfgState *s = FW_CFG(dev);
759 assert(!object_resolve_path(FW_CFG_PATH, NULL));
761 object_property_add_child(qdev_get_machine(), FW_CFG_NAME, OBJECT(s), NULL);
763 qdev_init_nofail(dev);
765 fw_cfg_add_bytes(s, FW_CFG_SIGNATURE, (char *)"QEMU", 4);
766 fw_cfg_add_bytes(s, FW_CFG_UUID, qemu_uuid, 16);
767 fw_cfg_add_i16(s, FW_CFG_NOGRAPHIC, (uint16_t)(display_type == DT_NOGRAPHIC));
768 fw_cfg_add_i16(s, FW_CFG_NB_CPUS, (uint16_t)smp_cpus);
769 fw_cfg_add_i16(s, FW_CFG_BOOT_MENU, (uint16_t)boot_menu);
770 fw_cfg_bootsplash(s);
771 fw_cfg_reboot(s);
773 s->machine_ready.notify = fw_cfg_machine_ready;
774 qemu_add_machine_init_done_notifier(&s->machine_ready);
777 FWCfgState *fw_cfg_init_io_dma(uint32_t iobase, uint32_t dma_iobase,
778 AddressSpace *dma_as)
780 DeviceState *dev;
781 FWCfgState *s;
782 uint32_t version = FW_CFG_VERSION;
783 bool dma_enabled = dma_iobase && dma_as;
785 dev = qdev_create(NULL, TYPE_FW_CFG_IO);
786 qdev_prop_set_uint32(dev, "iobase", iobase);
787 qdev_prop_set_uint32(dev, "dma_iobase", dma_iobase);
788 qdev_prop_set_bit(dev, "dma_enabled", dma_enabled);
790 fw_cfg_init1(dev);
791 s = FW_CFG(dev);
793 if (dma_enabled) {
794 /* 64 bits for the address field */
795 s->dma_as = dma_as;
796 s->dma_addr = 0;
798 version |= FW_CFG_VERSION_DMA;
801 fw_cfg_add_i32(s, FW_CFG_ID, version);
803 return s;
806 FWCfgState *fw_cfg_init_io(uint32_t iobase)
808 return fw_cfg_init_io_dma(iobase, 0, NULL);
811 FWCfgState *fw_cfg_init_mem_wide(hwaddr ctl_addr,
812 hwaddr data_addr, uint32_t data_width,
813 hwaddr dma_addr, AddressSpace *dma_as)
815 DeviceState *dev;
816 SysBusDevice *sbd;
817 FWCfgState *s;
818 uint32_t version = FW_CFG_VERSION;
819 bool dma_enabled = dma_addr && dma_as;
821 dev = qdev_create(NULL, TYPE_FW_CFG_MEM);
822 qdev_prop_set_uint32(dev, "data_width", data_width);
823 qdev_prop_set_bit(dev, "dma_enabled", dma_enabled);
825 fw_cfg_init1(dev);
827 sbd = SYS_BUS_DEVICE(dev);
828 sysbus_mmio_map(sbd, 0, ctl_addr);
829 sysbus_mmio_map(sbd, 1, data_addr);
831 s = FW_CFG(dev);
833 if (dma_enabled) {
834 s->dma_as = dma_as;
835 s->dma_addr = 0;
836 sysbus_mmio_map(sbd, 2, dma_addr);
837 version |= FW_CFG_VERSION_DMA;
840 fw_cfg_add_i32(s, FW_CFG_ID, version);
842 return s;
845 FWCfgState *fw_cfg_init_mem(hwaddr ctl_addr, hwaddr data_addr)
847 return fw_cfg_init_mem_wide(ctl_addr, data_addr,
848 fw_cfg_data_mem_ops.valid.max_access_size,
849 0, NULL);
853 FWCfgState *fw_cfg_find(void)
855 return FW_CFG(object_resolve_path(FW_CFG_PATH, NULL));
858 static void fw_cfg_class_init(ObjectClass *klass, void *data)
860 DeviceClass *dc = DEVICE_CLASS(klass);
862 dc->reset = fw_cfg_reset;
863 dc->vmsd = &vmstate_fw_cfg;
866 static const TypeInfo fw_cfg_info = {
867 .name = TYPE_FW_CFG,
868 .parent = TYPE_SYS_BUS_DEVICE,
869 .instance_size = sizeof(FWCfgState),
870 .class_init = fw_cfg_class_init,
874 static Property fw_cfg_io_properties[] = {
875 DEFINE_PROP_UINT32("iobase", FWCfgIoState, iobase, -1),
876 DEFINE_PROP_UINT32("dma_iobase", FWCfgIoState, dma_iobase, -1),
877 DEFINE_PROP_BOOL("dma_enabled", FWCfgIoState, parent_obj.dma_enabled,
878 false),
879 DEFINE_PROP_END_OF_LIST(),
882 static void fw_cfg_io_realize(DeviceState *dev, Error **errp)
884 FWCfgIoState *s = FW_CFG_IO(dev);
885 SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
887 memory_region_init_io(&s->comb_iomem, OBJECT(s), &fw_cfg_comb_mem_ops,
888 FW_CFG(s), "fwcfg", FW_CFG_CTL_SIZE);
889 sysbus_add_io(sbd, s->iobase, &s->comb_iomem);
891 if (FW_CFG(s)->dma_enabled) {
892 memory_region_init_io(&FW_CFG(s)->dma_iomem, OBJECT(s),
893 &fw_cfg_dma_mem_ops, FW_CFG(s), "fwcfg.dma",
894 sizeof(dma_addr_t));
895 sysbus_add_io(sbd, s->dma_iobase, &FW_CFG(s)->dma_iomem);
899 static void fw_cfg_io_class_init(ObjectClass *klass, void *data)
901 DeviceClass *dc = DEVICE_CLASS(klass);
903 dc->realize = fw_cfg_io_realize;
904 dc->props = fw_cfg_io_properties;
907 static const TypeInfo fw_cfg_io_info = {
908 .name = TYPE_FW_CFG_IO,
909 .parent = TYPE_FW_CFG,
910 .instance_size = sizeof(FWCfgIoState),
911 .class_init = fw_cfg_io_class_init,
915 static Property fw_cfg_mem_properties[] = {
916 DEFINE_PROP_UINT32("data_width", FWCfgMemState, data_width, -1),
917 DEFINE_PROP_BOOL("dma_enabled", FWCfgMemState, parent_obj.dma_enabled,
918 false),
919 DEFINE_PROP_END_OF_LIST(),
922 static void fw_cfg_mem_realize(DeviceState *dev, Error **errp)
924 FWCfgMemState *s = FW_CFG_MEM(dev);
925 SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
926 const MemoryRegionOps *data_ops = &fw_cfg_data_mem_ops;
928 memory_region_init_io(&s->ctl_iomem, OBJECT(s), &fw_cfg_ctl_mem_ops,
929 FW_CFG(s), "fwcfg.ctl", FW_CFG_CTL_SIZE);
930 sysbus_init_mmio(sbd, &s->ctl_iomem);
932 if (s->data_width > data_ops->valid.max_access_size) {
933 /* memberwise copy because the "old_mmio" member is const */
934 s->wide_data_ops.read = data_ops->read;
935 s->wide_data_ops.write = data_ops->write;
936 s->wide_data_ops.endianness = data_ops->endianness;
937 s->wide_data_ops.valid = data_ops->valid;
938 s->wide_data_ops.impl = data_ops->impl;
940 s->wide_data_ops.valid.max_access_size = s->data_width;
941 s->wide_data_ops.impl.max_access_size = s->data_width;
942 data_ops = &s->wide_data_ops;
944 memory_region_init_io(&s->data_iomem, OBJECT(s), data_ops, FW_CFG(s),
945 "fwcfg.data", data_ops->valid.max_access_size);
946 sysbus_init_mmio(sbd, &s->data_iomem);
948 if (FW_CFG(s)->dma_enabled) {
949 memory_region_init_io(&FW_CFG(s)->dma_iomem, OBJECT(s),
950 &fw_cfg_dma_mem_ops, FW_CFG(s), "fwcfg.dma",
951 sizeof(dma_addr_t));
952 sysbus_init_mmio(sbd, &FW_CFG(s)->dma_iomem);
956 static void fw_cfg_mem_class_init(ObjectClass *klass, void *data)
958 DeviceClass *dc = DEVICE_CLASS(klass);
960 dc->realize = fw_cfg_mem_realize;
961 dc->props = fw_cfg_mem_properties;
964 static const TypeInfo fw_cfg_mem_info = {
965 .name = TYPE_FW_CFG_MEM,
966 .parent = TYPE_FW_CFG,
967 .instance_size = sizeof(FWCfgMemState),
968 .class_init = fw_cfg_mem_class_init,
972 static void fw_cfg_register_types(void)
974 type_register_static(&fw_cfg_info);
975 type_register_static(&fw_cfg_io_info);
976 type_register_static(&fw_cfg_mem_info);
979 type_init(fw_cfg_register_types)