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