]> git.proxmox.com Git - mirror_qemu.git/blame - hw/fw_cfg.c
mcf5208: convert to memory API
[mirror_qemu.git] / hw / 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 */
24#include "hw.h"
084a197a 25#include "sysemu.h"
3cce6243
BS
26#include "isa.h"
27#include "fw_cfg.h"
3a5c16fc 28#include "sysbus.h"
3d3b8303 29#include "qemu-error.h"
3cce6243
BS
30
31/* debug firmware config */
32//#define DEBUG_FW_CFG
33
34#ifdef DEBUG_FW_CFG
001faf32
BS
35#define FW_CFG_DPRINTF(fmt, ...) \
36 do { printf("FW_CFG: " fmt , ## __VA_ARGS__); } while (0)
3cce6243 37#else
001faf32 38#define FW_CFG_DPRINTF(fmt, ...)
3cce6243
BS
39#endif
40
41#define FW_CFG_SIZE 2
42
b96ae2da 43typedef struct FWCfgEntry {
ff06108b 44 uint32_t len;
3cce6243
BS
45 uint8_t *data;
46 void *callback_opaque;
47 FWCfgCallback callback;
48} FWCfgEntry;
49
b96ae2da 50struct FWCfgState {
3a5c16fc
BS
51 SysBusDevice busdev;
52 uint32_t ctl_iobase, data_iobase;
3cce6243 53 FWCfgEntry entries[2][FW_CFG_MAX_ENTRY];
abe147e0 54 FWCfgFiles *files;
3cce6243 55 uint16_t cur_entry;
ff06108b 56 uint32_t cur_offset;
962630f2 57 Notifier machine_ready;
c2b5bda4 58};
3cce6243 59
3d3b8303
WX
60#define JPG_FILE 0
61#define BMP_FILE 1
62
63static FILE *probe_splashfile(char *filename, int *file_sizep, int *file_typep)
64{
65 FILE *fp = NULL;
66 int fop_ret;
67 int file_size;
68 int file_type = -1;
69 unsigned char buf[2] = {0, 0};
70 unsigned int filehead_value = 0;
71 int bmp_bpp;
72
73 fp = fopen(filename, "rb");
74 if (fp == NULL) {
75 error_report("failed to open file '%s'.", filename);
76 return fp;
77 }
78 /* check file size */
79 fseek(fp, 0L, SEEK_END);
80 file_size = ftell(fp);
81 if (file_size < 2) {
82 error_report("file size is less than 2 bytes '%s'.", filename);
83 fclose(fp);
84 fp = NULL;
85 return fp;
86 }
87 /* check magic ID */
88 fseek(fp, 0L, SEEK_SET);
89 fop_ret = fread(buf, 1, 2, fp);
257a7375
DG
90 if (fop_ret != 2) {
91 error_report("Could not read header from '%s': %s",
92 filename, strerror(errno));
93 fclose(fp);
94 fp = NULL;
95 return fp;
96 }
3d3b8303
WX
97 filehead_value = (buf[0] + (buf[1] << 8)) & 0xffff;
98 if (filehead_value == 0xd8ff) {
99 file_type = JPG_FILE;
100 } else {
101 if (filehead_value == 0x4d42) {
102 file_type = BMP_FILE;
103 }
104 }
105 if (file_type < 0) {
106 error_report("'%s' not jpg/bmp file,head:0x%x.",
107 filename, filehead_value);
108 fclose(fp);
109 fp = NULL;
110 return fp;
111 }
112 /* check BMP bpp */
113 if (file_type == BMP_FILE) {
114 fseek(fp, 28, SEEK_SET);
115 fop_ret = fread(buf, 1, 2, fp);
116 bmp_bpp = (buf[0] + (buf[1] << 8)) & 0xffff;
117 if (bmp_bpp != 24) {
118 error_report("only 24bpp bmp file is supported.");
119 fclose(fp);
120 fp = NULL;
121 return fp;
122 }
123 }
124 /* return values */
125 *file_sizep = file_size;
126 *file_typep = file_type;
127 return fp;
128}
129
130static void fw_cfg_bootsplash(FWCfgState *s)
131{
132 int boot_splash_time = -1;
133 const char *boot_splash_filename = NULL;
134 char *p;
135 char *filename;
136 FILE *fp;
137 int fop_ret;
138 int file_size;
139 int file_type = -1;
140 const char *temp;
141
142 /* get user configuration */
143 QemuOptsList *plist = qemu_find_opts("boot-opts");
144 QemuOpts *opts = QTAILQ_FIRST(&plist->head);
145 if (opts != NULL) {
146 temp = qemu_opt_get(opts, "splash");
147 if (temp != NULL) {
148 boot_splash_filename = temp;
149 }
150 temp = qemu_opt_get(opts, "splash-time");
151 if (temp != NULL) {
152 p = (char *)temp;
153 boot_splash_time = strtol(p, (char **)&p, 10);
154 }
155 }
156
157 /* insert splash time if user configurated */
158 if (boot_splash_time >= 0) {
159 /* validate the input */
160 if (boot_splash_time > 0xffff) {
161 error_report("splash time is big than 65535, force it to 65535.");
162 boot_splash_time = 0xffff;
163 }
164 /* use little endian format */
165 qemu_extra_params_fw[0] = (uint8_t)(boot_splash_time & 0xff);
166 qemu_extra_params_fw[1] = (uint8_t)((boot_splash_time >> 8) & 0xff);
167 fw_cfg_add_file(s, "etc/boot-menu-wait", qemu_extra_params_fw, 2);
168 }
169
170 /* insert splash file if user configurated */
171 if (boot_splash_filename != NULL) {
172 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, boot_splash_filename);
173 if (filename == NULL) {
174 error_report("failed to find file '%s'.", boot_splash_filename);
175 return;
176 }
177 /* probing the file */
178 fp = probe_splashfile(filename, &file_size, &file_type);
179 if (fp == NULL) {
7267c094 180 g_free(filename);
3d3b8303
WX
181 return;
182 }
183 /* loading file data */
184 if (boot_splash_filedata != NULL) {
7267c094 185 g_free(boot_splash_filedata);
3d3b8303 186 }
7267c094 187 boot_splash_filedata = g_malloc(file_size);
3d3b8303
WX
188 boot_splash_filedata_size = file_size;
189 fseek(fp, 0L, SEEK_SET);
190 fop_ret = fread(boot_splash_filedata, 1, file_size, fp);
257a7375
DG
191 if (fop_ret != file_size) {
192 error_report("failed to read data from '%s'.",
193 boot_splash_filename);
194 fclose(fp);
195 return;
196 }
3d3b8303
WX
197 fclose(fp);
198 /* insert data */
199 if (file_type == JPG_FILE) {
200 fw_cfg_add_file(s, "bootsplash.jpg",
201 boot_splash_filedata, boot_splash_filedata_size);
202 } else {
203 fw_cfg_add_file(s, "bootsplash.bmp",
204 boot_splash_filedata, boot_splash_filedata_size);
205 }
7267c094 206 g_free(filename);
3d3b8303
WX
207 }
208}
209
3cce6243
BS
210static void fw_cfg_write(FWCfgState *s, uint8_t value)
211{
212 int arch = !!(s->cur_entry & FW_CFG_ARCH_LOCAL);
213 FWCfgEntry *e = &s->entries[arch][s->cur_entry & FW_CFG_ENTRY_MASK];
214
215 FW_CFG_DPRINTF("write %d\n", value);
216
217 if (s->cur_entry & FW_CFG_WRITE_CHANNEL && s->cur_offset < e->len) {
218 e->data[s->cur_offset++] = value;
219 if (s->cur_offset == e->len) {
220 e->callback(e->callback_opaque, e->data);
221 s->cur_offset = 0;
222 }
223 }
224}
225
226static int fw_cfg_select(FWCfgState *s, uint16_t key)
227{
228 int ret;
229
230 s->cur_offset = 0;
231 if ((key & FW_CFG_ENTRY_MASK) >= FW_CFG_MAX_ENTRY) {
232 s->cur_entry = FW_CFG_INVALID;
233 ret = 0;
234 } else {
235 s->cur_entry = key;
236 ret = 1;
237 }
238
239 FW_CFG_DPRINTF("select key %d (%sfound)\n", key, ret ? "" : "not ");
240
241 return ret;
242}
243
244static uint8_t fw_cfg_read(FWCfgState *s)
245{
246 int arch = !!(s->cur_entry & FW_CFG_ARCH_LOCAL);
247 FWCfgEntry *e = &s->entries[arch][s->cur_entry & FW_CFG_ENTRY_MASK];
248 uint8_t ret;
249
250 if (s->cur_entry == FW_CFG_INVALID || !e->data || s->cur_offset >= e->len)
251 ret = 0;
252 else
253 ret = e->data[s->cur_offset++];
254
255 FW_CFG_DPRINTF("read %d\n", ret);
256
257 return ret;
258}
259
260static uint32_t fw_cfg_io_readb(void *opaque, uint32_t addr)
261{
262 return fw_cfg_read(opaque);
263}
264
265static void fw_cfg_io_writeb(void *opaque, uint32_t addr, uint32_t value)
266{
7442511c 267 fw_cfg_write(opaque, (uint8_t)value);
3cce6243
BS
268}
269
270static void fw_cfg_io_writew(void *opaque, uint32_t addr, uint32_t value)
271{
272 fw_cfg_select(opaque, (uint16_t)value);
273}
274
c227f099 275static uint32_t fw_cfg_mem_readb(void *opaque, target_phys_addr_t addr)
3cce6243
BS
276{
277 return fw_cfg_read(opaque);
278}
279
c227f099 280static void fw_cfg_mem_writeb(void *opaque, target_phys_addr_t addr,
3cce6243
BS
281 uint32_t value)
282{
7442511c 283 fw_cfg_write(opaque, (uint8_t)value);
3cce6243
BS
284}
285
c227f099 286static void fw_cfg_mem_writew(void *opaque, target_phys_addr_t addr,
3cce6243
BS
287 uint32_t value)
288{
289 fw_cfg_select(opaque, (uint16_t)value);
290}
291
d60efc6b 292static CPUReadMemoryFunc * const fw_cfg_ctl_mem_read[3] = {
3cce6243
BS
293 NULL,
294 NULL,
295 NULL,
296};
297
d60efc6b 298static CPUWriteMemoryFunc * const fw_cfg_ctl_mem_write[3] = {
3cce6243
BS
299 NULL,
300 fw_cfg_mem_writew,
301 NULL,
302};
303
d60efc6b 304static CPUReadMemoryFunc * const fw_cfg_data_mem_read[3] = {
3cce6243
BS
305 fw_cfg_mem_readb,
306 NULL,
307 NULL,
308};
309
d60efc6b 310static CPUWriteMemoryFunc * const fw_cfg_data_mem_write[3] = {
3cce6243
BS
311 fw_cfg_mem_writeb,
312 NULL,
313 NULL,
314};
315
3a5c16fc 316static void fw_cfg_reset(DeviceState *d)
3cce6243 317{
3a5c16fc 318 FWCfgState *s = DO_UPCAST(FWCfgState, busdev.qdev, d);
3cce6243
BS
319
320 fw_cfg_select(s, 0);
321}
322
ff06108b
JQ
323/* Save restore 32 bit int as uint16_t
324 This is a Big hack, but it is how the old state did it.
325 Or we broke compatibility in the state, or we can't use struct tm
326 */
327
328static int get_uint32_as_uint16(QEMUFile *f, void *pv, size_t size)
329{
330 uint32_t *v = pv;
331 *v = qemu_get_be16(f);
332 return 0;
333}
334
335static void put_unused(QEMUFile *f, void *pv, size_t size)
336{
66c80e75 337 fprintf(stderr, "uint32_as_uint16 is only used for backward compatibility.\n");
ff06108b
JQ
338 fprintf(stderr, "This functions shouldn't be called.\n");
339}
340
d05ac8fa 341static const VMStateInfo vmstate_hack_uint32_as_uint16 = {
ff06108b
JQ
342 .name = "int32_as_uint16",
343 .get = get_uint32_as_uint16,
344 .put = put_unused,
345};
346
347#define VMSTATE_UINT16_HACK(_f, _s, _t) \
348 VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_hack_uint32_as_uint16, uint32_t)
349
350
351static bool is_version_1(void *opaque, int version_id)
352{
353 return version_id == 1;
354}
355
7d2edd40
JQ
356static const VMStateDescription vmstate_fw_cfg = {
357 .name = "fw_cfg",
ff06108b 358 .version_id = 2,
7d2edd40
JQ
359 .minimum_version_id = 1,
360 .minimum_version_id_old = 1,
361 .fields = (VMStateField []) {
362 VMSTATE_UINT16(cur_entry, FWCfgState),
ff06108b
JQ
363 VMSTATE_UINT16_HACK(cur_offset, FWCfgState, is_version_1),
364 VMSTATE_UINT32_V(cur_offset, FWCfgState, 2),
7d2edd40
JQ
365 VMSTATE_END_OF_LIST()
366 }
367};
3cce6243 368
c2b5bda4 369int fw_cfg_add_bytes(FWCfgState *s, uint16_t key, uint8_t *data, uint32_t len)
3cce6243 370{
3cce6243
BS
371 int arch = !!(key & FW_CFG_ARCH_LOCAL);
372
373 key &= FW_CFG_ENTRY_MASK;
374
375 if (key >= FW_CFG_MAX_ENTRY)
376 return 0;
377
378 s->entries[arch][key].data = data;
379 s->entries[arch][key].len = len;
380
381 return 1;
382}
383
c2b5bda4 384int fw_cfg_add_i16(FWCfgState *s, uint16_t key, uint16_t value)
3cce6243
BS
385{
386 uint16_t *copy;
387
7267c094 388 copy = g_malloc(sizeof(value));
3cce6243 389 *copy = cpu_to_le16(value);
c2b5bda4 390 return fw_cfg_add_bytes(s, key, (uint8_t *)copy, sizeof(value));
3cce6243
BS
391}
392
c2b5bda4 393int fw_cfg_add_i32(FWCfgState *s, uint16_t key, uint32_t value)
3cce6243
BS
394{
395 uint32_t *copy;
396
7267c094 397 copy = g_malloc(sizeof(value));
3cce6243 398 *copy = cpu_to_le32(value);
c2b5bda4 399 return fw_cfg_add_bytes(s, key, (uint8_t *)copy, sizeof(value));
3cce6243
BS
400}
401
c2b5bda4 402int fw_cfg_add_i64(FWCfgState *s, uint16_t key, uint64_t value)
3cce6243
BS
403{
404 uint64_t *copy;
405
7267c094 406 copy = g_malloc(sizeof(value));
3cce6243 407 *copy = cpu_to_le64(value);
c2b5bda4 408 return fw_cfg_add_bytes(s, key, (uint8_t *)copy, sizeof(value));
3cce6243
BS
409}
410
c2b5bda4 411int fw_cfg_add_callback(FWCfgState *s, uint16_t key, FWCfgCallback callback,
3cce6243
BS
412 void *callback_opaque, uint8_t *data, size_t len)
413{
3cce6243
BS
414 int arch = !!(key & FW_CFG_ARCH_LOCAL);
415
85df0de4
BS
416 if (!(key & FW_CFG_WRITE_CHANNEL))
417 return 0;
418
3cce6243
BS
419 key &= FW_CFG_ENTRY_MASK;
420
85df0de4 421 if (key >= FW_CFG_MAX_ENTRY || len > 65535)
3cce6243
BS
422 return 0;
423
424 s->entries[arch][key].data = data;
425 s->entries[arch][key].len = len;
426 s->entries[arch][key].callback_opaque = callback_opaque;
427 s->entries[arch][key].callback = callback;
428
429 return 1;
430}
431
de1f34cb
GN
432int fw_cfg_add_file(FWCfgState *s, const char *filename, uint8_t *data,
433 uint32_t len)
abe147e0 434{
de9352bc 435 int i, index;
abe147e0
GH
436
437 if (!s->files) {
438 int dsize = sizeof(uint32_t) + sizeof(FWCfgFile) * FW_CFG_FILE_SLOTS;
7267c094 439 s->files = g_malloc0(dsize);
abe147e0
GH
440 fw_cfg_add_bytes(s, FW_CFG_FILE_DIR, (uint8_t*)s->files, dsize);
441 }
442
443 index = be32_to_cpu(s->files->count);
444 if (index == FW_CFG_FILE_SLOTS) {
445 fprintf(stderr, "fw_cfg: out of file slots\n");
446 return 0;
447 }
448
449 fw_cfg_add_bytes(s, FW_CFG_FILE_FIRST + index, data, len);
450
de1f34cb
GN
451 pstrcpy(s->files->f[index].name, sizeof(s->files->f[index].name),
452 filename);
de9352bc
GH
453 for (i = 0; i < index; i++) {
454 if (strcmp(s->files->f[index].name, s->files->f[i].name) == 0) {
455 FW_CFG_DPRINTF("%s: skip duplicate: %s\n", __FUNCTION__,
456 s->files->f[index].name);
457 return 1;
458 }
abe147e0 459 }
de9352bc 460
abe147e0
GH
461 s->files->f[index].size = cpu_to_be32(len);
462 s->files->f[index].select = cpu_to_be16(FW_CFG_FILE_FIRST + index);
463 FW_CFG_DPRINTF("%s: #%d: %s (%d bytes)\n", __FUNCTION__,
464 index, s->files->f[index].name, len);
465
466 s->files->count = cpu_to_be32(index+1);
467 return 1;
468}
469
9e8dd451 470static void fw_cfg_machine_ready(struct Notifier *n, void *data)
962630f2
GN
471{
472 uint32_t len;
473 FWCfgState *s = container_of(n, FWCfgState, machine_ready);
474 char *bootindex = get_boot_devices_list(&len);
475
476 fw_cfg_add_file(s, "bootorder", (uint8_t*)bootindex, len);
477}
478
c2b5bda4
GH
479FWCfgState *fw_cfg_init(uint32_t ctl_port, uint32_t data_port,
480 target_phys_addr_t ctl_addr, target_phys_addr_t data_addr)
3cce6243 481{
3a5c16fc
BS
482 DeviceState *dev;
483 SysBusDevice *d;
3cce6243 484 FWCfgState *s;
3cce6243 485
3a5c16fc
BS
486 dev = qdev_create(NULL, "fw_cfg");
487 qdev_prop_set_uint32(dev, "ctl_iobase", ctl_port);
488 qdev_prop_set_uint32(dev, "data_iobase", data_port);
489 qdev_init_nofail(dev);
490 d = sysbus_from_qdev(dev);
491
492 s = DO_UPCAST(FWCfgState, busdev.qdev, dev);
3cce6243 493
3cce6243 494 if (ctl_addr) {
3a5c16fc 495 sysbus_mmio_map(d, 0, ctl_addr);
3cce6243
BS
496 }
497 if (data_addr) {
3a5c16fc 498 sysbus_mmio_map(d, 1, data_addr);
3cce6243
BS
499 }
500 fw_cfg_add_bytes(s, FW_CFG_SIGNATURE, (uint8_t *)"QEMU", 4);
084a197a 501 fw_cfg_add_bytes(s, FW_CFG_UUID, qemu_uuid, 16);
993fbfdb 502 fw_cfg_add_i16(s, FW_CFG_NOGRAPHIC, (uint16_t)(display_type == DT_NOGRAPHIC));
905fdcb5 503 fw_cfg_add_i16(s, FW_CFG_NB_CPUS, (uint16_t)smp_cpus);
6be68d7e 504 fw_cfg_add_i16(s, FW_CFG_MAX_CPUS, (uint16_t)max_cpus);
95387491 505 fw_cfg_add_i16(s, FW_CFG_BOOT_MENU, (uint16_t)boot_menu);
3d3b8303 506 fw_cfg_bootsplash(s);
962630f2
GN
507
508 s->machine_ready.notify = fw_cfg_machine_ready;
509 qemu_add_machine_init_done_notifier(&s->machine_ready);
510
3cce6243
BS
511 return s;
512}
3a5c16fc
BS
513
514static int fw_cfg_init1(SysBusDevice *dev)
515{
516 FWCfgState *s = FROM_SYSBUS(FWCfgState, dev);
517 int io_ctl_memory, io_data_memory;
518
519 io_ctl_memory = cpu_register_io_memory(fw_cfg_ctl_mem_read,
2507c12a
AG
520 fw_cfg_ctl_mem_write, s,
521 DEVICE_NATIVE_ENDIAN);
3a5c16fc
BS
522 sysbus_init_mmio(dev, FW_CFG_SIZE, io_ctl_memory);
523
524 io_data_memory = cpu_register_io_memory(fw_cfg_data_mem_read,
2507c12a
AG
525 fw_cfg_data_mem_write, s,
526 DEVICE_NATIVE_ENDIAN);
3a5c16fc
BS
527 sysbus_init_mmio(dev, FW_CFG_SIZE, io_data_memory);
528
529 if (s->ctl_iobase) {
530 register_ioport_write(s->ctl_iobase, 2, 2, fw_cfg_io_writew, s);
531 }
532 if (s->data_iobase) {
533 register_ioport_read(s->data_iobase, 1, 1, fw_cfg_io_readb, s);
534 register_ioport_write(s->data_iobase, 1, 1, fw_cfg_io_writeb, s);
535 }
536 return 0;
537}
538
539static SysBusDeviceInfo fw_cfg_info = {
540 .init = fw_cfg_init1,
541 .qdev.name = "fw_cfg",
542 .qdev.size = sizeof(FWCfgState),
543 .qdev.vmsd = &vmstate_fw_cfg,
544 .qdev.reset = fw_cfg_reset,
545 .qdev.no_user = 1,
546 .qdev.props = (Property[]) {
547 DEFINE_PROP_HEX32("ctl_iobase", FWCfgState, ctl_iobase, -1),
548 DEFINE_PROP_HEX32("data_iobase", FWCfgState, data_iobase, -1),
549 DEFINE_PROP_END_OF_LIST(),
550 },
551};
552
553static void fw_cfg_register_devices(void)
554{
555 sysbus_register_withprop(&fw_cfg_info);
556}
557
558device_init(fw_cfg_register_devices)