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