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