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