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