]> git.ipfire.org Git - thirdparty/pciutils.git/blob - lib/dump.c
lspci: add VirtIO SharedMemory capability support
[thirdparty/pciutils.git] / lib / dump.c
1 /*
2 * The PCI Library -- Reading of Bus Dumps
3 *
4 * Copyright (c) 1997--2008 Martin Mares <mj@ucw.cz>
5 *
6 * Can be freely distributed and used under the terms of the GNU GPL v2+.
7 *
8 * SPDX-License-Identifier: GPL-2.0-or-later
9 */
10
11 #include <stdio.h>
12 #include <ctype.h>
13 #include <string.h>
14 #include <errno.h>
15
16 #include "internal.h"
17
18 struct dump_data {
19 int len, allocated;
20 byte data[1];
21 };
22
23 static void
24 dump_config(struct pci_access *a)
25 {
26 pci_define_param(a, "dump.name", "", "Name of the bus dump file to read from");
27 }
28
29 static int
30 dump_detect(struct pci_access *a)
31 {
32 char *name = pci_get_param(a, "dump.name");
33 return name && name[0];
34 }
35
36 static void
37 dump_alloc_data(struct pci_dev *dev, int len)
38 {
39 struct dump_data *dd = pci_malloc(dev->access, sizeof(struct dump_data) + len - 1);
40 dd->allocated = len;
41 dd->len = 0;
42 memset(dd->data, 0xff, len);
43 dev->backend_data = dd;
44 }
45
46 static int
47 dump_validate(char *s, char *fmt)
48 {
49 while (*fmt)
50 {
51 if (*fmt == '#' ? !isxdigit(*s) : *fmt != *s)
52 return 0;
53 fmt++, s++;
54 }
55 return 1;
56 }
57
58 static void
59 dump_init(struct pci_access *a)
60 {
61 char *name = pci_get_param(a, "dump.name");
62 FILE *f;
63 char buf[256];
64 struct pci_dev *dev = NULL;
65 int len, mn, bn, dn, fn, i, j;
66
67 if (!name)
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))
72 {
73 char *z = strchr(buf, '\n');
74 if (!z)
75 {
76 fclose(f);
77 a->error("dump: line too long or unterminated");
78 }
79 *z-- = 0;
80 if (z >= buf && *z == '\r')
81 *z-- = 0;
82 len = z - buf + 1;
83 mn = 0;
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)
87 {
88 dev = pci_get_dev(a, mn, bn, dn, fn);
89 dump_alloc_data(dev, 256);
90 pci_link_dev(a, dev);
91 }
92 else if (!len)
93 dev = NULL;
94 else if (dev &&
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)
99 {
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)
104 {
105 if (i >= 4096)
106 {
107 fclose(f);
108 a->error("dump: At most 4096 bytes of config space are supported");
109 }
110 if (i >= dd->allocated) /* Need to re-allocate the buffer */
111 {
112 dump_alloc_data(dev, 4096);
113 memcpy(((struct dump_data *) dev->backend_data)->data, dd->data, 256);
114 pci_mfree(dd);
115 dd = dev->backend_data;
116 }
117 dd->data[i++] = j;
118 if (i > dd->len)
119 dd->len = i;
120 z += 2;
121 if (*z)
122 z++;
123 }
124 if (*z)
125 {
126 fclose(f);
127 a->error("dump: Malformed line");
128 }
129 }
130 }
131 fclose(f);
132 }
133
134 static void
135 dump_cleanup(struct pci_access *a UNUSED)
136 {
137 }
138
139 static void
140 dump_scan(struct pci_access *a UNUSED)
141 {
142 }
143
144 static int
145 dump_read(struct pci_dev *d, int pos, byte *buf, int len)
146 {
147 struct dump_data *dd;
148 if (!(dd = d->backend_data))
149 {
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))
152 e = e->next;
153 if (!e)
154 return 0;
155 dd = e->backend_data;
156 }
157 if (pos + len > dd->len)
158 return 0;
159 memcpy(buf, dd->data + pos, len);
160 return 1;
161 }
162
163 static int
164 dump_write(struct pci_dev *d UNUSED, int pos UNUSED, byte *buf UNUSED, int len UNUSED)
165 {
166 d->access->error("Writing to dump files is not supported.");
167 return 0;
168 }
169
170 static void
171 dump_cleanup_dev(struct pci_dev *d)
172 {
173 if (d->backend_data)
174 {
175 pci_mfree(d->backend_data);
176 d->backend_data = NULL;
177 }
178 }
179
180 struct pci_methods pm_dump = {
181 .name = "dump",
182 .help = "Reading of register dumps (set the `dump.name' parameter)",
183 .config = dump_config,
184 .detect = dump_detect,
185 .init = dump_init,
186 .cleanup = dump_cleanup,
187 .scan = dump_scan,
188 .fill_info = pci_generic_fill_info,
189 .read = dump_read,
190 .write = dump_write,
191 .cleanup_dev = dump_cleanup_dev,
192 };