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 v2+.
8 * SPDX-License-Identifier: GPL-2.0-or-later
24 dump_config(struct pci_access *a)
26 pci_define_param(a, "dump.name", "", "Name of the bus dump file to read from");
30 dump_detect(struct pci_access *a)
32 char *name = pci_get_param(a, "dump.name");
33 return name && name[0];
37 dump_alloc_data(struct pci_dev *dev, int len)
39 struct dump_data *dd = pci_malloc(dev->access, sizeof(struct dump_data) + len - 1);
42 memset(dd->data, 0xff, len);
43 dev->backend_data = dd;
47 dump_validate(char *s, char *fmt)
51 if (*fmt == '#' ? !isxdigit(*s) : *fmt != *s)
59 dump_init(struct pci_access *a)
61 char *name = pci_get_param(a, "dump.name");
64 struct pci_dev *dev = NULL;
65 int len, mn, bn, dn, fn, i, j;
68 a->error("dump: File name not given.");
69 if (!(f = fopen(name, "r")))
70 a->error("dump: Cannot open %s: %s", name, strerror(errno));
71 while (fgets(buf, sizeof(buf)-1, f))
73 char *z = strchr(buf, '\n');
77 a->error("dump: line too long or unterminated");
80 if (z >= buf && *z == '\r')
84 if (dump_validate(buf, "##:##.# ") && sscanf(buf, "%x:%x.%d", &bn, &dn, &fn) == 3 ||
85 dump_validate(buf, "####:##:##.# ") && sscanf(buf, "%x:%x:%x.%d", &mn, &bn, &dn, &fn) == 4 ||
86 dump_validate(buf, "#####:##:##.# ") && sscanf(buf, "%x:%x:%x.%d", &mn, &bn, &dn, &fn) == 4)
88 dev = pci_get_dev(a, mn, bn, dn, fn);
89 dump_alloc_data(dev, 256);
95 (dump_validate(buf, "##: ") || dump_validate(buf, "###: ") || dump_validate(buf, "####: ") ||
96 dump_validate(buf, "#####: ") || dump_validate(buf, "######: ") ||
97 dump_validate(buf, "#######: ") || dump_validate(buf, "########: ")) &&
98 sscanf(buf, "%x: ", &i) == 1)
100 struct dump_data *dd = dev->backend_data;
101 z = strchr(buf, ' ') + 1;
102 while (isxdigit(z[0]) && isxdigit(z[1]) && (!z[2] || z[2] == ' ') &&
103 sscanf(z, "%x", &j) == 1 && j < 256)
108 a->error("dump: At most 4096 bytes of config space are supported");
110 if (i >= dd->allocated) /* Need to re-allocate the buffer */
112 dump_alloc_data(dev, 4096);
113 memcpy(((struct dump_data *) dev->backend_data)->data, dd->data, 256);
115 dd = dev->backend_data;
127 a->error("dump: Malformed line");
135 dump_cleanup(struct pci_access *a UNUSED)
140 dump_scan(struct pci_access *a UNUSED)
145 dump_read(struct pci_dev *d, int pos, byte *buf, int len)
147 struct dump_data *dd;
148 if (!(dd = d->backend_data))
150 struct pci_dev *e = d->access->devices;
151 while (e && (e->domain != d->domain || e->bus != d->bus || e->dev != d->dev || e->func != d->func))
155 dd = e->backend_data;
157 if (pos + len > dd->len)
159 memcpy(buf, dd->data + pos, len);
164 dump_write(struct pci_dev *d UNUSED, int pos UNUSED, byte *buf UNUSED, int len UNUSED)
166 d->access->error("Writing to dump files is not supported.");
171 dump_cleanup_dev(struct pci_dev *d)
175 pci_mfree(d->backend_data);
176 d->backend_data = NULL;
180 struct pci_methods pm_dump = {
182 .help = "Reading of register dumps (set the `dump.name' parameter)",
183 .config = dump_config,
184 .detect = dump_detect,
186 .cleanup = dump_cleanup,
188 .fill_info = pci_generic_fill_info,
191 .cleanup_dev = dump_cleanup_dev,