pci: add romsize property
[qemu.git] / hw / xen / xen_pt_load_rom.c
blob03422a8a71487dcf0d7eb976d5b549678d9a8103
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"
12 * Scan the assigned devices for the devices that have an option ROM, and then
13 * load the corresponding ROM data to RAM. If an error occurs while loading an
14 * option ROM, we just ignore that option ROM and continue with the next one.
16 void *pci_assign_dev_load_option_rom(PCIDevice *dev,
17 int *size, unsigned int domain,
18 unsigned int bus, unsigned int slot,
19 unsigned int function)
21 char name[32], rom_file[64];
22 FILE *fp;
23 uint8_t val;
24 struct stat st;
25 void *ptr = NULL;
26 Object *owner = OBJECT(dev);
28 /* If loading ROM from file, pci handles it */
29 if (dev->romfile || !dev->rom_bar) {
30 return NULL;
33 snprintf(rom_file, sizeof(rom_file),
34 "/sys/bus/pci/devices/%04x:%02x:%02x.%01x/rom",
35 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));
43 return NULL;
45 if (fstat(fileno(fp), &st) == -1) {
46 error_report("pci-assign: Cannot stat %s: %s", rom_file, strerror(errno));
47 goto close_rom;
50 val = 1;
51 if (fwrite(&val, 1, 1, fp) != 1) {
52 goto close_rom;
54 fseek(fp, 0, SEEK_SET);
56 if (dev->romsize != -1) {
57 if (st.st_size > dev->romsize) {
58 error_report("ROM BAR \"%s\" (%ld bytes) is too large for ROM size %u",
59 rom_file, (long) st.st_size, dev->romsize);
60 goto close_rom;
62 } else {
63 dev->romsize = st.st_size;
66 snprintf(name, sizeof(name), "%s.rom", object_get_typename(owner));
67 memory_region_init_ram(&dev->rom, owner, name, dev->romsize, &error_abort);
68 ptr = memory_region_get_ram_ptr(&dev->rom);
69 memset(ptr, 0xff, dev->romsize);
71 if (!fread(ptr, 1, st.st_size, fp)) {
72 error_report("pci-assign: Cannot read from host %s", rom_file);
73 error_printf("Device option ROM contents are probably invalid "
74 "(check dmesg).\nSkip option ROM probe with rombar=0, "
75 "or load from file with romfile=\n");
76 goto close_rom;
79 pci_register_bar(dev, PCI_ROM_SLOT, 0, &dev->rom);
80 dev->has_rom = true;
81 *size = st.st_size;
82 close_rom:
83 /* Write "0" to disable ROM */
84 fseek(fp, 0, SEEK_SET);
85 val = 0;
86 if (!fwrite(&val, 1, 1, fp)) {
87 XEN_PT_WARN(dev, "%s\n", "Failed to disable pci-sysfs rom file");
89 fclose(fp);
91 return ptr;