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 "qemu/osdep.h" 25 #include "hw/hw.h" 26 #include "sysemu/sysemu.h" 27 #include "sysemu/dma.h" 28 #include "hw/boards.h" 29 #include "hw/isa/isa.h" 30 #include "hw/nvram/fw_cfg.h" 31 #include "hw/sysbus.h" 32 #include "trace.h" 33 #include "qemu/error-report.h" 34 #include "qemu/config-file.h" 35 #include "qemu/cutils.h" 36 37 #define FW_CFG_NAME "fw_cfg" 38 #define FW_CFG_PATH "/machine/" FW_CFG_NAME 39 40 #define TYPE_FW_CFG "fw_cfg" 41 #define TYPE_FW_CFG_IO "fw_cfg_io" 42 #define TYPE_FW_CFG_MEM "fw_cfg_mem" 43 44 #define FW_CFG(obj) OBJECT_CHECK(FWCfgState, (obj), TYPE_FW_CFG) 45 #define FW_CFG_IO(obj) OBJECT_CHECK(FWCfgIoState, (obj), TYPE_FW_CFG_IO) 46 #define FW_CFG_MEM(obj) OBJECT_CHECK(FWCfgMemState, (obj), TYPE_FW_CFG_MEM) 47 48 /* FW_CFG_VERSION bits */ 49 #define FW_CFG_VERSION 0x01 50 #define FW_CFG_VERSION_DMA 0x02 51 52 /* FW_CFG_DMA_CONTROL bits */ 53 #define FW_CFG_DMA_CTL_ERROR 0x01 54 #define FW_CFG_DMA_CTL_READ 0x02 55 #define FW_CFG_DMA_CTL_SKIP 0x04 56 #define FW_CFG_DMA_CTL_SELECT 0x08 57 #define FW_CFG_DMA_CTL_WRITE 0x10 58 59 #define FW_CFG_DMA_SIGNATURE 0x51454d5520434647ULL /* "QEMU CFG" */ 60 61 typedef struct FWCfgEntry { 62 uint32_t len; 63 bool allow_write; 64 uint8_t *data; 65 void *callback_opaque; 66 FWCfgReadCallback read_callback; 67 } FWCfgEntry; 68 69 struct FWCfgState { 70 /*< private >*/ 71 SysBusDevice parent_obj; 72 /*< public >*/ 73 74 FWCfgEntry entries[2][FW_CFG_MAX_ENTRY]; 75 int entry_order[FW_CFG_MAX_ENTRY]; 76 FWCfgFiles *files; 77 uint16_t cur_entry; 78 uint32_t cur_offset; 79 Notifier machine_ready; 80 81 int fw_cfg_order_override; 82 83 bool dma_enabled; 84 dma_addr_t dma_addr; 85 AddressSpace *dma_as; 86 MemoryRegion dma_iomem; 87 }; 88 89 struct FWCfgIoState { 90 /*< private >*/ 91 FWCfgState parent_obj; 92 /*< public >*/ 93 94 MemoryRegion comb_iomem; 95 uint32_t iobase, dma_iobase; 96 }; 97 98 struct FWCfgMemState { 99 /*< private >*/ 100 FWCfgState parent_obj; 101 /*< public >*/ 102 103 MemoryRegion ctl_iomem, data_iomem; 104 uint32_t data_width; 105 MemoryRegionOps wide_data_ops; 106 }; 107 108 #define JPG_FILE 0 109 #define BMP_FILE 1 110 111 static char *read_splashfile(char *filename, gsize *file_sizep, 112 int *file_typep) 113 { 114 GError *err = NULL; 115 gboolean res; 116 gchar *content; 117 int file_type; 118 unsigned int filehead; 119 int bmp_bpp; 120 121 res = g_file_get_contents(filename, &content, file_sizep, &err); 122 if (res == FALSE) { 123 error_report("failed to read splash file '%s'", filename); 124 g_error_free(err); 125 return NULL; 126 } 127 128 /* check file size */ 129 if (*file_sizep < 30) { 130 goto error; 131 } 132 133 /* check magic ID */ 134 filehead = ((content[0] & 0xff) + (content[1] << 8)) & 0xffff; 135 if (filehead == 0xd8ff) { 136 file_type = JPG_FILE; 137 } else if (filehead == 0x4d42) { 138 file_type = BMP_FILE; 139 } else { 140 goto error; 141 } 142 143 /* check BMP bpp */ 144 if (file_type == BMP_FILE) { 145 bmp_bpp = (content[28] + (content[29] << 8)) & 0xffff; 146 if (bmp_bpp != 24) { 147 goto error; 148 } 149 } 150 151 /* return values */ 152 *file_typep = file_type; 153 154 return content; 155 156 error: 157 error_report("splash file '%s' format not recognized; must be JPEG " 158 "or 24 bit BMP", filename); 159 g_free(content); 160 return NULL; 161 } 162 163 static void fw_cfg_bootsplash(FWCfgState *s) 164 { 165 int boot_splash_time = -1; 166 const char *boot_splash_filename = NULL; 167 char *p; 168 char *filename, *file_data; 169 gsize file_size; 170 int file_type; 171 const char *temp; 172 173 /* get user configuration */ 174 QemuOptsList *plist = qemu_find_opts("boot-opts"); 175 QemuOpts *opts = QTAILQ_FIRST(&plist->head); 176 if (opts != NULL) { 177 temp = qemu_opt_get(opts, "splash"); 178 if (temp != NULL) { 179 boot_splash_filename = temp; 180 } 181 temp = qemu_opt_get(opts, "splash-time"); 182 if (temp != NULL) { 183 p = (char *)temp; 184 boot_splash_time = strtol(p, &p, 10); 185 } 186 } 187 188 /* insert splash time if user configurated */ 189 if (boot_splash_time >= 0) { 190 /* validate the input */ 191 if (boot_splash_time > 0xffff) { 192 error_report("splash time is big than 65535, force it to 65535."); 193 boot_splash_time = 0xffff; 194 } 195 /* use little endian format */ 196 qemu_extra_params_fw[0] = (uint8_t)(boot_splash_time & 0xff); 197 qemu_extra_params_fw[1] = (uint8_t)((boot_splash_time >> 8) & 0xff); 198 fw_cfg_add_file(s, "etc/boot-menu-wait", qemu_extra_params_fw, 2); 199 } 200 201 /* insert splash file if user configurated */ 202 if (boot_splash_filename != NULL) { 203 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, boot_splash_filename); 204 if (filename == NULL) { 205 error_report("failed to find file '%s'.", boot_splash_filename); 206 return; 207 } 208 209 /* loading file data */ 210 file_data = read_splashfile(filename, &file_size, &file_type); 211 if (file_data == NULL) { 212 g_free(filename); 213 return; 214 } 215 g_free(boot_splash_filedata); 216 boot_splash_filedata = (uint8_t *)file_data; 217 boot_splash_filedata_size = file_size; 218 219 /* insert data */ 220 if (file_type == JPG_FILE) { 221 fw_cfg_add_file(s, "bootsplash.jpg", 222 boot_splash_filedata, boot_splash_filedata_size); 223 } else { 224 fw_cfg_add_file(s, "bootsplash.bmp", 225 boot_splash_filedata, boot_splash_filedata_size); 226 } 227 g_free(filename); 228 } 229 } 230 231 static void fw_cfg_reboot(FWCfgState *s) 232 { 233 int reboot_timeout = -1; 234 char *p; 235 const char *temp; 236 237 /* get user configuration */ 238 QemuOptsList *plist = qemu_find_opts("boot-opts"); 239 QemuOpts *opts = QTAILQ_FIRST(&plist->head); 240 if (opts != NULL) { 241 temp = qemu_opt_get(opts, "reboot-timeout"); 242 if (temp != NULL) { 243 p = (char *)temp; 244 reboot_timeout = strtol(p, &p, 10); 245 } 246 } 247 /* validate the input */ 248 if (reboot_timeout > 0xffff) { 249 error_report("reboot timeout is larger than 65535, force it to 65535."); 250 reboot_timeout = 0xffff; 251 } 252 fw_cfg_add_file(s, "etc/boot-fail-wait", g_memdup(&reboot_timeout, 4), 4); 253 } 254 255 static void fw_cfg_write(FWCfgState *s, uint8_t value) 256 { 257 /* nothing, write support removed in QEMU v2.4+ */ 258 } 259 260 static int fw_cfg_select(FWCfgState *s, uint16_t key) 261 { 262 int arch, ret; 263 FWCfgEntry *e; 264 265 s->cur_offset = 0; 266 if ((key & FW_CFG_ENTRY_MASK) >= FW_CFG_MAX_ENTRY) { 267 s->cur_entry = FW_CFG_INVALID; 268 ret = 0; 269 } else { 270 s->cur_entry = key; 271 ret = 1; 272 /* entry successfully selected, now run callback if present */ 273 arch = !!(key & FW_CFG_ARCH_LOCAL); 274 e = &s->entries[arch][key & FW_CFG_ENTRY_MASK]; 275 if (e->read_callback) { 276 e->read_callback(e->callback_opaque); 277 } 278 } 279 280 trace_fw_cfg_select(s, key, ret); 281 return ret; 282 } 283 284 static uint64_t fw_cfg_data_read(void *opaque, hwaddr addr, unsigned size) 285 { 286 FWCfgState *s = opaque; 287 int arch = !!(s->cur_entry & FW_CFG_ARCH_LOCAL); 288 FWCfgEntry *e = (s->cur_entry == FW_CFG_INVALID) ? NULL : 289 &s->entries[arch][s->cur_entry & FW_CFG_ENTRY_MASK]; 290 uint64_t value = 0; 291 292 assert(size > 0 && size <= sizeof(value)); 293 if (s->cur_entry != FW_CFG_INVALID && e->data && s->cur_offset < e->len) { 294 /* The least significant 'size' bytes of the return value are 295 * expected to contain a string preserving portion of the item 296 * data, padded with zeros on the right in case we run out early. 297 * In technical terms, we're composing the host-endian representation 298 * of the big endian interpretation of the fw_cfg string. 299 */ 300 do { 301 value = (value << 8) | e->data[s->cur_offset++]; 302 } while (--size && s->cur_offset < e->len); 303 /* If size is still not zero, we *did* run out early, so continue 304 * left-shifting, to add the appropriate number of padding zeros 305 * on the right. 306 */ 307 value <<= 8 * size; 308 } 309 310 trace_fw_cfg_read(s, value); 311 return value; 312 } 313 314 static void fw_cfg_data_mem_write(void *opaque, hwaddr addr, 315 uint64_t value, unsigned size) 316 { 317 FWCfgState *s = opaque; 318 unsigned i = size; 319 320 do { 321 fw_cfg_write(s, value >> (8 * --i)); 322 } while (i); 323 } 324 325 static void fw_cfg_dma_transfer(FWCfgState *s) 326 { 327 dma_addr_t len; 328 FWCfgDmaAccess dma; 329 int arch; 330 FWCfgEntry *e; 331 int read = 0, write = 0; 332 dma_addr_t dma_addr; 333 334 /* Reset the address before the next access */ 335 dma_addr = s->dma_addr; 336 s->dma_addr = 0; 337 338 if (dma_memory_read(s->dma_as, dma_addr, &dma, sizeof(dma))) { 339 stl_be_dma(s->dma_as, dma_addr + offsetof(FWCfgDmaAccess, control), 340 FW_CFG_DMA_CTL_ERROR); 341 return; 342 } 343 344 dma.address = be64_to_cpu(dma.address); 345 dma.length = be32_to_cpu(dma.length); 346 dma.control = be32_to_cpu(dma.control); 347 348 if (dma.control & FW_CFG_DMA_CTL_SELECT) { 349 fw_cfg_select(s, dma.control >> 16); 350 } 351 352 arch = !!(s->cur_entry & FW_CFG_ARCH_LOCAL); 353 e = (s->cur_entry == FW_CFG_INVALID) ? NULL : 354 &s->entries[arch][s->cur_entry & FW_CFG_ENTRY_MASK]; 355 356 if (dma.control & FW_CFG_DMA_CTL_READ) { 357 read = 1; 358 write = 0; 359 } else if (dma.control & FW_CFG_DMA_CTL_WRITE) { 360 read = 0; 361 write = 1; 362 } else if (dma.control & FW_CFG_DMA_CTL_SKIP) { 363 read = 0; 364 write = 0; 365 } else { 366 dma.length = 0; 367 } 368 369 dma.control = 0; 370 371 while (dma.length > 0 && !(dma.control & FW_CFG_DMA_CTL_ERROR)) { 372 if (s->cur_entry == FW_CFG_INVALID || !e->data || 373 s->cur_offset >= e->len) { 374 len = dma.length; 375 376 /* If the access is not a read access, it will be a skip access, 377 * tested before. 378 */ 379 if (read) { 380 if (dma_memory_set(s->dma_as, dma.address, 0, len)) { 381 dma.control |= FW_CFG_DMA_CTL_ERROR; 382 } 383 } 384 if (write) { 385 dma.control |= FW_CFG_DMA_CTL_ERROR; 386 } 387 } else { 388 if (dma.length <= (e->len - s->cur_offset)) { 389 len = dma.length; 390 } else { 391 len = (e->len - s->cur_offset); 392 } 393 394 /* If the access is not a read access, it will be a skip access, 395 * tested before. 396 */ 397 if (read) { 398 if (dma_memory_write(s->dma_as, dma.address, 399 &e->data[s->cur_offset], len)) { 400 dma.control |= FW_CFG_DMA_CTL_ERROR; 401 } 402 } 403 if (write) { 404 if (!e->allow_write || 405 len != dma.length || 406 dma_memory_read(s->dma_as, dma.address, 407 &e->data[s->cur_offset], len)) { 408 dma.control |= FW_CFG_DMA_CTL_ERROR; 409 } 410 } 411 412 s->cur_offset += len; 413 } 414 415 dma.address += len; 416 dma.length -= len; 417 418 } 419 420 stl_be_dma(s->dma_as, dma_addr + offsetof(FWCfgDmaAccess, control), 421 dma.control); 422 423 trace_fw_cfg_read(s, 0); 424 } 425 426 static uint64_t fw_cfg_dma_mem_read(void *opaque, hwaddr addr, 427 unsigned size) 428 { 429 /* Return a signature value (and handle various read sizes) */ 430 return extract64(FW_CFG_DMA_SIGNATURE, (8 - addr - size) * 8, size * 8); 431 } 432 433 static void fw_cfg_dma_mem_write(void *opaque, hwaddr addr, 434 uint64_t value, unsigned size) 435 { 436 FWCfgState *s = opaque; 437 438 if (size == 4) { 439 if (addr == 0) { 440 /* FWCfgDmaAccess high address */ 441 s->dma_addr = value << 32; 442 } else if (addr == 4) { 443 /* FWCfgDmaAccess low address */ 444 s->dma_addr |= value; 445 fw_cfg_dma_transfer(s); 446 } 447 } else if (size == 8 && addr == 0) { 448 s->dma_addr = value; 449 fw_cfg_dma_transfer(s); 450 } 451 } 452 453 static bool fw_cfg_dma_mem_valid(void *opaque, hwaddr addr, 454 unsigned size, bool is_write) 455 { 456 return !is_write || ((size == 4 && (addr == 0 || addr == 4)) || 457 (size == 8 && addr == 0)); 458 } 459 460 static bool fw_cfg_data_mem_valid(void *opaque, hwaddr addr, 461 unsigned size, bool is_write) 462 { 463 return addr == 0; 464 } 465 466 static void fw_cfg_ctl_mem_write(void *opaque, hwaddr addr, 467 uint64_t value, unsigned size) 468 { 469 fw_cfg_select(opaque, (uint16_t)value); 470 } 471 472 static bool fw_cfg_ctl_mem_valid(void *opaque, hwaddr addr, 473 unsigned size, bool is_write) 474 { 475 return is_write && size == 2; 476 } 477 478 static void fw_cfg_comb_write(void *opaque, hwaddr addr, 479 uint64_t value, unsigned size) 480 { 481 switch (size) { 482 case 1: 483 fw_cfg_write(opaque, (uint8_t)value); 484 break; 485 case 2: 486 fw_cfg_select(opaque, (uint16_t)value); 487 break; 488 } 489 } 490 491 static bool fw_cfg_comb_valid(void *opaque, hwaddr addr, 492 unsigned size, bool is_write) 493 { 494 return (size == 1) || (is_write && size == 2); 495 } 496 497 static const MemoryRegionOps fw_cfg_ctl_mem_ops = { 498 .write = fw_cfg_ctl_mem_write, 499 .endianness = DEVICE_BIG_ENDIAN, 500 .valid.accepts = fw_cfg_ctl_mem_valid, 501 }; 502 503 static const MemoryRegionOps fw_cfg_data_mem_ops = { 504 .read = fw_cfg_data_read, 505 .write = fw_cfg_data_mem_write, 506 .endianness = DEVICE_BIG_ENDIAN, 507 .valid = { 508 .min_access_size = 1, 509 .max_access_size = 1, 510 .accepts = fw_cfg_data_mem_valid, 511 }, 512 }; 513 514 static const MemoryRegionOps fw_cfg_comb_mem_ops = { 515 .read = fw_cfg_data_read, 516 .write = fw_cfg_comb_write, 517 .endianness = DEVICE_LITTLE_ENDIAN, 518 .valid.accepts = fw_cfg_comb_valid, 519 }; 520 521 static const MemoryRegionOps fw_cfg_dma_mem_ops = { 522 .read = fw_cfg_dma_mem_read, 523 .write = fw_cfg_dma_mem_write, 524 .endianness = DEVICE_BIG_ENDIAN, 525 .valid.accepts = fw_cfg_dma_mem_valid, 526 .valid.max_access_size = 8, 527 .impl.max_access_size = 8, 528 }; 529 530 static void fw_cfg_reset(DeviceState *d) 531 { 532 FWCfgState *s = FW_CFG(d); 533 534 /* we never register a read callback for FW_CFG_SIGNATURE */ 535 fw_cfg_select(s, FW_CFG_SIGNATURE); 536 } 537 538 /* Save restore 32 bit int as uint16_t 539 This is a Big hack, but it is how the old state did it. 540 Or we broke compatibility in the state, or we can't use struct tm 541 */ 542 543 static int get_uint32_as_uint16(QEMUFile *f, void *pv, size_t size) 544 { 545 uint32_t *v = pv; 546 *v = qemu_get_be16(f); 547 return 0; 548 } 549 550 static void put_unused(QEMUFile *f, void *pv, size_t size) 551 { 552 fprintf(stderr, "uint32_as_uint16 is only used for backward compatibility.\n"); 553 fprintf(stderr, "This functions shouldn't be called.\n"); 554 } 555 556 static const VMStateInfo vmstate_hack_uint32_as_uint16 = { 557 .name = "int32_as_uint16", 558 .get = get_uint32_as_uint16, 559 .put = put_unused, 560 }; 561 562 #define VMSTATE_UINT16_HACK(_f, _s, _t) \ 563 VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_hack_uint32_as_uint16, uint32_t) 564 565 566 static bool is_version_1(void *opaque, int version_id) 567 { 568 return version_id == 1; 569 } 570 571 bool fw_cfg_dma_enabled(void *opaque) 572 { 573 FWCfgState *s = opaque; 574 575 return s->dma_enabled; 576 } 577 578 static const VMStateDescription vmstate_fw_cfg_dma = { 579 .name = "fw_cfg/dma", 580 .needed = fw_cfg_dma_enabled, 581 .fields = (VMStateField[]) { 582 VMSTATE_UINT64(dma_addr, FWCfgState), 583 VMSTATE_END_OF_LIST() 584 }, 585 }; 586 587 static const VMStateDescription vmstate_fw_cfg = { 588 .name = "fw_cfg", 589 .version_id = 2, 590 .minimum_version_id = 1, 591 .fields = (VMStateField[]) { 592 VMSTATE_UINT16(cur_entry, FWCfgState), 593 VMSTATE_UINT16_HACK(cur_offset, FWCfgState, is_version_1), 594 VMSTATE_UINT32_V(cur_offset, FWCfgState, 2), 595 VMSTATE_END_OF_LIST() 596 }, 597 .subsections = (const VMStateDescription*[]) { 598 &vmstate_fw_cfg_dma, 599 NULL, 600 } 601 }; 602 603 static void fw_cfg_add_bytes_read_callback(FWCfgState *s, uint16_t key, 604 FWCfgReadCallback callback, 605 void *callback_opaque, 606 void *data, size_t len, 607 bool read_only) 608 { 609 int arch = !!(key & FW_CFG_ARCH_LOCAL); 610 611 key &= FW_CFG_ENTRY_MASK; 612 613 assert(key < FW_CFG_MAX_ENTRY && len < UINT32_MAX); 614 assert(s->entries[arch][key].data == NULL); /* avoid key conflict */ 615 616 s->entries[arch][key].data = data; 617 s->entries[arch][key].len = (uint32_t)len; 618 s->entries[arch][key].read_callback = callback; 619 s->entries[arch][key].callback_opaque = callback_opaque; 620 s->entries[arch][key].allow_write = !read_only; 621 } 622 623 static void *fw_cfg_modify_bytes_read(FWCfgState *s, uint16_t key, 624 void *data, size_t len) 625 { 626 void *ptr; 627 int arch = !!(key & FW_CFG_ARCH_LOCAL); 628 629 key &= FW_CFG_ENTRY_MASK; 630 631 assert(key < FW_CFG_MAX_ENTRY && len < UINT32_MAX); 632 633 /* return the old data to the function caller, avoid memory leak */ 634 ptr = s->entries[arch][key].data; 635 s->entries[arch][key].data = data; 636 s->entries[arch][key].len = len; 637 s->entries[arch][key].callback_opaque = NULL; 638 s->entries[arch][key].allow_write = false; 639 640 return ptr; 641 } 642 643 void fw_cfg_add_bytes(FWCfgState *s, uint16_t key, void *data, size_t len) 644 { 645 fw_cfg_add_bytes_read_callback(s, key, NULL, NULL, data, len, true); 646 } 647 648 void fw_cfg_add_string(FWCfgState *s, uint16_t key, const char *value) 649 { 650 size_t sz = strlen(value) + 1; 651 652 fw_cfg_add_bytes(s, key, g_memdup(value, sz), sz); 653 } 654 655 void fw_cfg_add_i16(FWCfgState *s, uint16_t key, uint16_t value) 656 { 657 uint16_t *copy; 658 659 copy = g_malloc(sizeof(value)); 660 *copy = cpu_to_le16(value); 661 fw_cfg_add_bytes(s, key, copy, sizeof(value)); 662 } 663 664 void fw_cfg_modify_i16(FWCfgState *s, uint16_t key, uint16_t value) 665 { 666 uint16_t *copy, *old; 667 668 copy = g_malloc(sizeof(value)); 669 *copy = cpu_to_le16(value); 670 old = fw_cfg_modify_bytes_read(s, key, copy, sizeof(value)); 671 g_free(old); 672 } 673 674 void fw_cfg_add_i32(FWCfgState *s, uint16_t key, uint32_t value) 675 { 676 uint32_t *copy; 677 678 copy = g_malloc(sizeof(value)); 679 *copy = cpu_to_le32(value); 680 fw_cfg_add_bytes(s, key, copy, sizeof(value)); 681 } 682 683 void fw_cfg_add_i64(FWCfgState *s, uint16_t key, uint64_t value) 684 { 685 uint64_t *copy; 686 687 copy = g_malloc(sizeof(value)); 688 *copy = cpu_to_le64(value); 689 fw_cfg_add_bytes(s, key, copy, sizeof(value)); 690 } 691 692 void fw_cfg_set_order_override(FWCfgState *s, int order) 693 { 694 assert(s->fw_cfg_order_override == 0); 695 s->fw_cfg_order_override = order; 696 } 697 698 void fw_cfg_reset_order_override(FWCfgState *s) 699 { 700 assert(s->fw_cfg_order_override != 0); 701 s->fw_cfg_order_override = 0; 702 } 703 704 /* 705 * This is the legacy order list. For legacy systems, files are in 706 * the fw_cfg in the order defined below, by the "order" value. Note 707 * that some entries (VGA ROMs, NIC option ROMS, etc.) go into a 708 * specific area, but there may be more than one and they occur in the 709 * order that the user specifies them on the command line. Those are 710 * handled in a special manner, using the order override above. 711 * 712 * For non-legacy, the files are sorted by filename to avoid this kind 713 * of complexity in the future. 714 * 715 * This is only for x86, other arches don't implement versioning so 716 * they won't set legacy mode. 717 */ 718 static struct { 719 const char *name; 720 int order; 721 } fw_cfg_order[] = { 722 { "etc/boot-menu-wait", 10 }, 723 { "bootsplash.jpg", 11 }, 724 { "bootsplash.bmp", 12 }, 725 { "etc/boot-fail-wait", 15 }, 726 { "etc/smbios/smbios-tables", 20 }, 727 { "etc/smbios/smbios-anchor", 30 }, 728 { "etc/e820", 40 }, 729 { "etc/reserved-memory-end", 50 }, 730 { "genroms/kvmvapic.bin", 55 }, 731 { "genroms/linuxboot.bin", 60 }, 732 { }, /* VGA ROMs from pc_vga_init come here, 70. */ 733 { }, /* NIC option ROMs from pc_nic_init come here, 80. */ 734 { "etc/system-states", 90 }, 735 { }, /* User ROMs come here, 100. */ 736 { }, /* Device FW comes here, 110. */ 737 { "etc/extra-pci-roots", 120 }, 738 { "etc/acpi/tables", 130 }, 739 { "etc/table-loader", 140 }, 740 { "etc/tpm/log", 150 }, 741 { "etc/acpi/rsdp", 160 }, 742 { "bootorder", 170 }, 743 744 #define FW_CFG_ORDER_OVERRIDE_LAST 200 745 }; 746 747 static int get_fw_cfg_order(FWCfgState *s, const char *name) 748 { 749 int i; 750 751 if (s->fw_cfg_order_override > 0) { 752 return s->fw_cfg_order_override; 753 } 754 755 for (i = 0; i < ARRAY_SIZE(fw_cfg_order); i++) { 756 if (fw_cfg_order[i].name == NULL) { 757 continue; 758 } 759 760 if (strcmp(name, fw_cfg_order[i].name) == 0) { 761 return fw_cfg_order[i].order; 762 } 763 } 764 765 /* Stick unknown stuff at the end. */ 766 error_report("warning: Unknown firmware file in legacy mode: %s", name); 767 return FW_CFG_ORDER_OVERRIDE_LAST; 768 } 769 770 void fw_cfg_add_file_callback(FWCfgState *s, const char *filename, 771 FWCfgReadCallback callback, void *callback_opaque, 772 void *data, size_t len, bool read_only) 773 { 774 int i, index, count; 775 size_t dsize; 776 MachineClass *mc = MACHINE_GET_CLASS(qdev_get_machine()); 777 int order = 0; 778 779 if (!s->files) { 780 dsize = sizeof(uint32_t) + sizeof(FWCfgFile) * FW_CFG_FILE_SLOTS; 781 s->files = g_malloc0(dsize); 782 fw_cfg_add_bytes(s, FW_CFG_FILE_DIR, s->files, dsize); 783 } 784 785 count = be32_to_cpu(s->files->count); 786 assert(count < FW_CFG_FILE_SLOTS); 787 788 /* Find the insertion point. */ 789 if (mc->legacy_fw_cfg_order) { 790 /* 791 * Sort by order. For files with the same order, we keep them 792 * in the sequence in which they were added. 793 */ 794 order = get_fw_cfg_order(s, filename); 795 for (index = count; 796 index > 0 && order < s->entry_order[index - 1]; 797 index--); 798 } else { 799 /* Sort by file name. */ 800 for (index = count; 801 index > 0 && strcmp(filename, s->files->f[index - 1].name) < 0; 802 index--); 803 } 804 805 /* 806 * Move all the entries from the index point and after down one 807 * to create a slot for the new entry. Because calculations are 808 * being done with the index, make it so that "i" is the current 809 * index and "i - 1" is the one being copied from, thus the 810 * unusual start and end in the for statement. 811 */ 812 for (i = count + 1; i > index; i--) { 813 s->files->f[i] = s->files->f[i - 1]; 814 s->files->f[i].select = cpu_to_be16(FW_CFG_FILE_FIRST + i); 815 s->entries[0][FW_CFG_FILE_FIRST + i] = 816 s->entries[0][FW_CFG_FILE_FIRST + i - 1]; 817 s->entry_order[i] = s->entry_order[i - 1]; 818 } 819 820 memset(&s->files->f[index], 0, sizeof(FWCfgFile)); 821 memset(&s->entries[0][FW_CFG_FILE_FIRST + index], 0, sizeof(FWCfgEntry)); 822 823 pstrcpy(s->files->f[index].name, sizeof(s->files->f[index].name), filename); 824 for (i = 0; i <= count; i++) { 825 if (i != index && 826 strcmp(s->files->f[index].name, s->files->f[i].name) == 0) { 827 error_report("duplicate fw_cfg file name: %s", 828 s->files->f[index].name); 829 exit(1); 830 } 831 } 832 833 fw_cfg_add_bytes_read_callback(s, FW_CFG_FILE_FIRST + index, 834 callback, callback_opaque, data, len, 835 read_only); 836 837 s->files->f[index].size = cpu_to_be32(len); 838 s->files->f[index].select = cpu_to_be16(FW_CFG_FILE_FIRST + index); 839 s->entry_order[index] = order; 840 trace_fw_cfg_add_file(s, index, s->files->f[index].name, len); 841 842 s->files->count = cpu_to_be32(count+1); 843 } 844 845 void fw_cfg_add_file(FWCfgState *s, const char *filename, 846 void *data, size_t len) 847 { 848 fw_cfg_add_file_callback(s, filename, NULL, NULL, data, len, true); 849 } 850 851 void *fw_cfg_modify_file(FWCfgState *s, const char *filename, 852 void *data, size_t len) 853 { 854 int i, index; 855 void *ptr = NULL; 856 857 assert(s->files); 858 859 index = be32_to_cpu(s->files->count); 860 assert(index < FW_CFG_FILE_SLOTS); 861 862 for (i = 0; i < index; i++) { 863 if (strcmp(filename, s->files->f[i].name) == 0) { 864 ptr = fw_cfg_modify_bytes_read(s, FW_CFG_FILE_FIRST + i, 865 data, len); 866 s->files->f[i].size = cpu_to_be32(len); 867 return ptr; 868 } 869 } 870 /* add new one */ 871 fw_cfg_add_file_callback(s, filename, NULL, NULL, data, len, true); 872 return NULL; 873 } 874 875 static void fw_cfg_machine_reset(void *opaque) 876 { 877 void *ptr; 878 size_t len; 879 FWCfgState *s = opaque; 880 char *bootindex = get_boot_devices_list(&len, false); 881 882 ptr = fw_cfg_modify_file(s, "bootorder", (uint8_t *)bootindex, len); 883 g_free(ptr); 884 } 885 886 static void fw_cfg_machine_ready(struct Notifier *n, void *data) 887 { 888 FWCfgState *s = container_of(n, FWCfgState, machine_ready); 889 qemu_register_reset(fw_cfg_machine_reset, s); 890 } 891 892 893 894 static void fw_cfg_init1(DeviceState *dev) 895 { 896 FWCfgState *s = FW_CFG(dev); 897 MachineState *machine = MACHINE(qdev_get_machine()); 898 899 assert(!object_resolve_path(FW_CFG_PATH, NULL)); 900 901 object_property_add_child(OBJECT(machine), FW_CFG_NAME, OBJECT(s), NULL); 902 903 qdev_init_nofail(dev); 904 905 fw_cfg_add_bytes(s, FW_CFG_SIGNATURE, (char *)"QEMU", 4); 906 fw_cfg_add_bytes(s, FW_CFG_UUID, &qemu_uuid, 16); 907 fw_cfg_add_i16(s, FW_CFG_NOGRAPHIC, (uint16_t)!machine->enable_graphics); 908 fw_cfg_add_i16(s, FW_CFG_BOOT_MENU, (uint16_t)boot_menu); 909 fw_cfg_bootsplash(s); 910 fw_cfg_reboot(s); 911 912 s->machine_ready.notify = fw_cfg_machine_ready; 913 qemu_add_machine_init_done_notifier(&s->machine_ready); 914 } 915 916 FWCfgState *fw_cfg_init_io_dma(uint32_t iobase, uint32_t dma_iobase, 917 AddressSpace *dma_as) 918 { 919 DeviceState *dev; 920 FWCfgState *s; 921 uint32_t version = FW_CFG_VERSION; 922 bool dma_requested = dma_iobase && dma_as; 923 924 dev = qdev_create(NULL, TYPE_FW_CFG_IO); 925 qdev_prop_set_uint32(dev, "iobase", iobase); 926 qdev_prop_set_uint32(dev, "dma_iobase", dma_iobase); 927 if (!dma_requested) { 928 qdev_prop_set_bit(dev, "dma_enabled", false); 929 } 930 931 fw_cfg_init1(dev); 932 s = FW_CFG(dev); 933 934 if (s->dma_enabled) { 935 /* 64 bits for the address field */ 936 s->dma_as = dma_as; 937 s->dma_addr = 0; 938 939 version |= FW_CFG_VERSION_DMA; 940 } 941 942 fw_cfg_add_i32(s, FW_CFG_ID, version); 943 944 return s; 945 } 946 947 FWCfgState *fw_cfg_init_io(uint32_t iobase) 948 { 949 return fw_cfg_init_io_dma(iobase, 0, NULL); 950 } 951 952 FWCfgState *fw_cfg_init_mem_wide(hwaddr ctl_addr, 953 hwaddr data_addr, uint32_t data_width, 954 hwaddr dma_addr, AddressSpace *dma_as) 955 { 956 DeviceState *dev; 957 SysBusDevice *sbd; 958 FWCfgState *s; 959 uint32_t version = FW_CFG_VERSION; 960 bool dma_requested = dma_addr && dma_as; 961 962 dev = qdev_create(NULL, TYPE_FW_CFG_MEM); 963 qdev_prop_set_uint32(dev, "data_width", data_width); 964 if (!dma_requested) { 965 qdev_prop_set_bit(dev, "dma_enabled", false); 966 } 967 968 fw_cfg_init1(dev); 969 970 sbd = SYS_BUS_DEVICE(dev); 971 sysbus_mmio_map(sbd, 0, ctl_addr); 972 sysbus_mmio_map(sbd, 1, data_addr); 973 974 s = FW_CFG(dev); 975 976 if (s->dma_enabled) { 977 s->dma_as = dma_as; 978 s->dma_addr = 0; 979 sysbus_mmio_map(sbd, 2, dma_addr); 980 version |= FW_CFG_VERSION_DMA; 981 } 982 983 fw_cfg_add_i32(s, FW_CFG_ID, version); 984 985 return s; 986 } 987 988 FWCfgState *fw_cfg_init_mem(hwaddr ctl_addr, hwaddr data_addr) 989 { 990 return fw_cfg_init_mem_wide(ctl_addr, data_addr, 991 fw_cfg_data_mem_ops.valid.max_access_size, 992 0, NULL); 993 } 994 995 996 FWCfgState *fw_cfg_find(void) 997 { 998 return FW_CFG(object_resolve_path(FW_CFG_PATH, NULL)); 999 } 1000 1001 static void fw_cfg_class_init(ObjectClass *klass, void *data) 1002 { 1003 DeviceClass *dc = DEVICE_CLASS(klass); 1004 1005 dc->reset = fw_cfg_reset; 1006 dc->vmsd = &vmstate_fw_cfg; 1007 } 1008 1009 static const TypeInfo fw_cfg_info = { 1010 .name = TYPE_FW_CFG, 1011 .parent = TYPE_SYS_BUS_DEVICE, 1012 .abstract = true, 1013 .instance_size = sizeof(FWCfgState), 1014 .class_init = fw_cfg_class_init, 1015 }; 1016 1017 1018 static Property fw_cfg_io_properties[] = { 1019 DEFINE_PROP_UINT32("iobase", FWCfgIoState, iobase, -1), 1020 DEFINE_PROP_UINT32("dma_iobase", FWCfgIoState, dma_iobase, -1), 1021 DEFINE_PROP_BOOL("dma_enabled", FWCfgIoState, parent_obj.dma_enabled, 1022 true), 1023 DEFINE_PROP_END_OF_LIST(), 1024 }; 1025 1026 static void fw_cfg_io_realize(DeviceState *dev, Error **errp) 1027 { 1028 FWCfgIoState *s = FW_CFG_IO(dev); 1029 SysBusDevice *sbd = SYS_BUS_DEVICE(dev); 1030 1031 /* when using port i/o, the 8-bit data register ALWAYS overlaps 1032 * with half of the 16-bit control register. Hence, the total size 1033 * of the i/o region used is FW_CFG_CTL_SIZE */ 1034 memory_region_init_io(&s->comb_iomem, OBJECT(s), &fw_cfg_comb_mem_ops, 1035 FW_CFG(s), "fwcfg", FW_CFG_CTL_SIZE); 1036 sysbus_add_io(sbd, s->iobase, &s->comb_iomem); 1037 1038 if (FW_CFG(s)->dma_enabled) { 1039 memory_region_init_io(&FW_CFG(s)->dma_iomem, OBJECT(s), 1040 &fw_cfg_dma_mem_ops, FW_CFG(s), "fwcfg.dma", 1041 sizeof(dma_addr_t)); 1042 sysbus_add_io(sbd, s->dma_iobase, &FW_CFG(s)->dma_iomem); 1043 } 1044 } 1045 1046 static void fw_cfg_io_class_init(ObjectClass *klass, void *data) 1047 { 1048 DeviceClass *dc = DEVICE_CLASS(klass); 1049 1050 dc->realize = fw_cfg_io_realize; 1051 dc->props = fw_cfg_io_properties; 1052 } 1053 1054 static const TypeInfo fw_cfg_io_info = { 1055 .name = TYPE_FW_CFG_IO, 1056 .parent = TYPE_FW_CFG, 1057 .instance_size = sizeof(FWCfgIoState), 1058 .class_init = fw_cfg_io_class_init, 1059 }; 1060 1061 1062 static Property fw_cfg_mem_properties[] = { 1063 DEFINE_PROP_UINT32("data_width", FWCfgMemState, data_width, -1), 1064 DEFINE_PROP_BOOL("dma_enabled", FWCfgMemState, parent_obj.dma_enabled, 1065 true), 1066 DEFINE_PROP_END_OF_LIST(), 1067 }; 1068 1069 static void fw_cfg_mem_realize(DeviceState *dev, Error **errp) 1070 { 1071 FWCfgMemState *s = FW_CFG_MEM(dev); 1072 SysBusDevice *sbd = SYS_BUS_DEVICE(dev); 1073 const MemoryRegionOps *data_ops = &fw_cfg_data_mem_ops; 1074 1075 memory_region_init_io(&s->ctl_iomem, OBJECT(s), &fw_cfg_ctl_mem_ops, 1076 FW_CFG(s), "fwcfg.ctl", FW_CFG_CTL_SIZE); 1077 sysbus_init_mmio(sbd, &s->ctl_iomem); 1078 1079 if (s->data_width > data_ops->valid.max_access_size) { 1080 /* memberwise copy because the "old_mmio" member is const */ 1081 s->wide_data_ops.read = data_ops->read; 1082 s->wide_data_ops.write = data_ops->write; 1083 s->wide_data_ops.endianness = data_ops->endianness; 1084 s->wide_data_ops.valid = data_ops->valid; 1085 s->wide_data_ops.impl = data_ops->impl; 1086 1087 s->wide_data_ops.valid.max_access_size = s->data_width; 1088 s->wide_data_ops.impl.max_access_size = s->data_width; 1089 data_ops = &s->wide_data_ops; 1090 } 1091 memory_region_init_io(&s->data_iomem, OBJECT(s), data_ops, FW_CFG(s), 1092 "fwcfg.data", data_ops->valid.max_access_size); 1093 sysbus_init_mmio(sbd, &s->data_iomem); 1094 1095 if (FW_CFG(s)->dma_enabled) { 1096 memory_region_init_io(&FW_CFG(s)->dma_iomem, OBJECT(s), 1097 &fw_cfg_dma_mem_ops, FW_CFG(s), "fwcfg.dma", 1098 sizeof(dma_addr_t)); 1099 sysbus_init_mmio(sbd, &FW_CFG(s)->dma_iomem); 1100 } 1101 } 1102 1103 static void fw_cfg_mem_class_init(ObjectClass *klass, void *data) 1104 { 1105 DeviceClass *dc = DEVICE_CLASS(klass); 1106 1107 dc->realize = fw_cfg_mem_realize; 1108 dc->props = fw_cfg_mem_properties; 1109 } 1110 1111 static const TypeInfo fw_cfg_mem_info = { 1112 .name = TYPE_FW_CFG_MEM, 1113 .parent = TYPE_FW_CFG, 1114 .instance_size = sizeof(FWCfgMemState), 1115 .class_init = fw_cfg_mem_class_init, 1116 }; 1117 1118 1119 static void fw_cfg_register_types(void) 1120 { 1121 type_register_static(&fw_cfg_info); 1122 type_register_static(&fw_cfg_io_info); 1123 type_register_static(&fw_cfg_mem_info); 1124 } 1125 1126 type_init(fw_cfg_register_types) 1127