2 * The PCI Library -- Reading of Bus Dumps
4 * Copyright (c) 1997--2008 Martin Mares <mj@ucw.cz>
6 * Can be freely distributed and used under the terms of the GNU GPL.
22 dump_config(struct pci_access *a)
24 pci_define_param(a, "dump.name", "", "Name of the bus dump file to read from");
28 dump_detect(struct pci_access *a)
30 char *name = pci_get_param(a, "dump.name");
31 return name && name[0];
35 dump_alloc_data(struct pci_dev *dev, int len)
37 struct dump_data *dd = pci_malloc(dev->access, sizeof(struct dump_data) + len - 1);
40 memset(dd->data, 0xff, len);
45 dump_validate(char *s, char *fmt)
49 if (*fmt == '#' ? !isxdigit(*s) : *fmt != *s)
57 dump_init(struct pci_access *a)
59 char *name = pci_get_param(a, "dump.name");
62 struct pci_dev *dev = NULL;
63 int len, mn, bn, dn, fn, i, j;
66 a->error("dump: File name not given.");
67 if (!(f = fopen(name, "r")))
68 a->error("dump: Cannot open %s: %s", name, strerror(errno));
69 while (fgets(buf, sizeof(buf)-1, f))
71 char *z = strchr(buf, '\n');
73 a->error("dump: line too long or unterminated");
75 if (z >= buf && *z == '\r')
79 if (dump_validate(buf, "##:##.# ") && sscanf(buf, "%x:%x.%d", &bn, &dn, &fn) == 3 ||
80 dump_validate(buf, "####:##:##.# ") && sscanf(buf, "%x:%x:%x.%d", &mn, &bn, &dn, &fn) == 4)
82 dev = pci_get_dev(a, mn, bn, dn, fn);
83 dump_alloc_data(dev, 256);
89 (dump_validate(buf, "##: ") || dump_validate(buf, "###: ")) &&
90 sscanf(buf, "%x: ", &i) == 1)
92 struct dump_data *dd = dev->aux;
93 z = strchr(buf, ' ') + 1;
94 while (isxdigit(z[0]) && isxdigit(z[1]) && (!z[2] || z[2] == ' ') &&
95 sscanf(z, "%x", &j) == 1 && j < 256)
98 a->error("dump: At most 4096 bytes of config space are supported");
99 if (i >= dd->allocated) /* Need to re-allocate the buffer */
101 dump_alloc_data(dev, 4096);
102 memcpy(((struct dump_data *) dev->aux)->data, dd->data, 256);
114 a->error("dump: Malformed line");
120 dump_cleanup(struct pci_access *a UNUSED)
125 dump_scan(struct pci_access *a UNUSED)
130 dump_read(struct pci_dev *d, int pos, byte *buf, int len)
132 struct dump_data *dd;
135 struct pci_dev *e = d->access->devices;
136 while (e && (e->domain != d->domain || e->bus != d->bus || e->dev != d->dev || e->func != d->func))
142 if (pos + len > dd->len)
144 memcpy(buf, dd->data + pos, len);
149 dump_write(struct pci_dev *d UNUSED, int pos UNUSED, byte *buf UNUSED, int len UNUSED)
151 d->access->error("Writing to dump files is not supported.");
156 dump_cleanup_dev(struct pci_dev *d)
165 struct pci_methods pm_dump = {
167 "Reading of register dumps (set the `dump.name' parameter)",
173 pci_generic_fill_info,