]> git.proxmox.com Git - mirror_qemu.git/blame - hw/xen/xen_pt_load_rom.c
sysbus: Convert to sysbus_realize() etc. with Coccinelle
[mirror_qemu.git] / hw / xen / xen_pt_load_rom.c
CommitLineData
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 6#include "qemu/error-report.h"
bcd7461e 7#include "hw/loader.h"
bcd7461e 8#include "hw/pci/pci.h"
ab37bfc7 9#include "xen_pt.h"
bcd7461e
TC
10
11/*
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.
15 */
6dad8260 16void *pci_assign_dev_load_option_rom(PCIDevice *dev,
bcd7461e
TC
17 int *size, unsigned int domain,
18 unsigned int bus, unsigned int slot,
19 unsigned int function)
20{
21 char name[32], rom_file[64];
22 FILE *fp;
23 uint8_t val;
24 struct stat st;
25 void *ptr = NULL;
6dad8260 26 Object *owner = OBJECT(dev);
bcd7461e
TC
27
28 /* If loading ROM from file, pci handles it */
29 if (dev->romfile || !dev->rom_bar) {
30 return NULL;
31 }
32
33 snprintf(rom_file, sizeof(rom_file),
34 "/sys/bus/pci/devices/%04x:%02x:%02x.%01x/rom",
35 domain, bus, slot, function);
36
bcd7461e
TC
37 /* Write "1" to the ROM file to enable it */
38 fp = fopen(rom_file, "r+");
39 if (fp == NULL) {
9ed26903
PB
40 if (errno != ENOENT) {
41 error_report("pci-assign: Cannot open %s: %s", rom_file, strerror(errno));
42 }
bcd7461e
TC
43 return NULL;
44 }
9ed26903
PB
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
bcd7461e
TC
50 val = 1;
51 if (fwrite(&val, 1, 1, fp) != 1) {
52 goto close_rom;
53 }
54 fseek(fp, 0, SEEK_SET);
55
56 snprintf(name, sizeof(name), "%s.rom", object_get_typename(owner));
6dad8260 57 memory_region_init_ram(&dev->rom, owner, name, st.st_size, &error_abort);
bcd7461e
TC
58 ptr = memory_region_get_ram_ptr(&dev->rom);
59 memset(ptr, 0xff, st.st_size);
60
61 if (!fread(ptr, 1, st.st_size, fp)) {
62 error_report("pci-assign: Cannot read from host %s", rom_file);
63 error_printf("Device option ROM contents are probably invalid "
64 "(check dmesg).\nSkip option ROM probe with rombar=0, "
65 "or load from file with romfile=\n");
66 goto close_rom;
67 }
68
69 pci_register_bar(dev, PCI_ROM_SLOT, 0, &dev->rom);
70 dev->has_rom = true;
71 *size = st.st_size;
72close_rom:
73 /* Write "0" to disable ROM */
74 fseek(fp, 0, SEEK_SET);
75 val = 0;
76 if (!fwrite(&val, 1, 1, fp)) {
ab37bfc7 77 XEN_PT_WARN(dev, "%s\n", "Failed to disable pci-sysfs rom file");
bcd7461e
TC
78 }
79 fclose(fp);
80
81 return ptr;
82}