2
0

xen_pt_load_rom.c 2.6 KB

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