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