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