xen_pt_load_rom.c 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * This is splited from hw/i386/kvm/pci-assign.c
  3. */
  4. #include "qemu/osdep.h"
  5. #include "qapi/error.h"
  6. #include "qemu/error-report.h"
  7. #include "hw/loader.h"
  8. #include "hw/pci/pci.h"
  9. #include "xen_pt.h"
  10. /*
  11. * Scan the assigned devices for the devices that have an option ROM, and then
  12. * load the corresponding ROM data to RAM. If an error occurs while loading an
  13. * option ROM, we just ignore that option ROM and continue with the next one.
  14. */
  15. void *pci_assign_dev_load_option_rom(PCIDevice *dev,
  16. int *size, unsigned int domain,
  17. unsigned int bus, unsigned int slot,
  18. unsigned int function)
  19. {
  20. char name[32], rom_file[64];
  21. FILE *fp;
  22. uint8_t val;
  23. struct stat st;
  24. void *ptr = NULL;
  25. Object *owner = OBJECT(dev);
  26. /* If loading ROM from file, pci handles it */
  27. if (dev->romfile || !dev->rom_bar) {
  28. return NULL;
  29. }
  30. snprintf(rom_file, sizeof(rom_file),
  31. "/sys/bus/pci/devices/%04x:%02x:%02x.%01x/rom",
  32. domain, bus, slot, function);
  33. /* Write "1" to the ROM file to enable it */
  34. fp = fopen(rom_file, "r+");
  35. if (fp == NULL) {
  36. if (errno != ENOENT) {
  37. error_report("pci-assign: Cannot open %s: %s", rom_file, strerror(errno));
  38. }
  39. return NULL;
  40. }
  41. if (fstat(fileno(fp), &st) == -1) {
  42. error_report("pci-assign: Cannot stat %s: %s", rom_file, strerror(errno));
  43. goto close_rom;
  44. }
  45. val = 1;
  46. if (fwrite(&val, 1, 1, fp) != 1) {
  47. goto close_rom;
  48. }
  49. fseek(fp, 0, SEEK_SET);
  50. if (dev->romsize != UINT_MAX) {
  51. if (st.st_size > dev->romsize) {
  52. error_report("ROM BAR \"%s\" (%ld bytes) is too large for ROM size %u",
  53. rom_file, (long) st.st_size, dev->romsize);
  54. goto close_rom;
  55. }
  56. } else {
  57. dev->romsize = st.st_size;
  58. }
  59. snprintf(name, sizeof(name), "%s.rom", object_get_typename(owner));
  60. memory_region_init_ram(&dev->rom, owner, name, dev->romsize, &error_abort);
  61. ptr = memory_region_get_ram_ptr(&dev->rom);
  62. memset(ptr, 0xff, dev->romsize);
  63. if (!fread(ptr, 1, st.st_size, fp)) {
  64. error_report("pci-assign: Cannot read from host %s", rom_file);
  65. error_printf("Device option ROM contents are probably invalid "
  66. "(check dmesg).\nSkip option ROM probe with rombar=0, "
  67. "or load from file with romfile=\n");
  68. goto close_rom;
  69. }
  70. pci_register_bar(dev, PCI_ROM_SLOT, 0, &dev->rom);
  71. dev->has_rom = true;
  72. *size = st.st_size;
  73. close_rom:
  74. /* Write "0" to disable ROM */
  75. fseek(fp, 0, SEEK_SET);
  76. val = 0;
  77. if (!fwrite(&val, 1, 1, fp)) {
  78. XEN_PT_WARN(dev, "%s\n", "Failed to disable pci-sysfs rom file");
  79. }
  80. fclose(fp);
  81. return ptr;
  82. }