1 /*
2 * The PCI Library -- Device Filtering
3 *
4 * Copyright (c) 1998--2022 Martin Mares <[email protected]>
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 <stdlib.h>
12 #include <string.h>
13
14 #include "internal.h"
15
16 void pci_filter_init_v38(struct pci_access *a UNUSED, struct pci_filter *f) VERSIONED_ABI;
17 char *pci_filter_parse_slot_v38(struct pci_filter *f, char *str) VERSIONED_ABI;
18 char *pci_filter_parse_id_v38(struct pci_filter *f, char *str) VERSIONED_ABI;
19 int pci_filter_match_v38(struct pci_filter *f, struct pci_dev *d) VERSIONED_ABI;
20
21 void
pci_filter_init_v38(struct pci_access * a UNUSED,struct pci_filter * f)22 pci_filter_init_v38(struct pci_access *a UNUSED, struct pci_filter *f)
23 {
24 memset((byte *) f, 0, sizeof(*f));
25 f->domain = f->bus = f->slot = f->func = -1;
26 f->vendor = f->device = -1;
27 f->device_class = -1;
28 f->device_class_mask = ~0U;
29 f->prog_if = -1;
30 }
31
32 #define BUF_SIZE 64
33
34 static char *
split_to_fields(char * str,char * buffer,int sep,char ** fields,int num_fields)35 split_to_fields(char *str, char *buffer, int sep, char **fields, int num_fields)
36 {
37 if (buffer)
38 {
39 if (strlen(str) >= BUF_SIZE)
40 return "Expression too long";
41 strcpy(buffer, str);
42 str = buffer;
43 }
44
45 int i = 0;
46
47 for (;;)
48 {
49 if (i >= num_fields)
50 return "Too many fields";
51 fields[i++] = str;
52 while (*str && *str != sep)
53 str++;
54 if (!*str)
55 break;
56 *str++ = 0;
57 }
58
59 while (i < num_fields)
60 fields[i++] = NULL;
61
62 return NULL;
63 }
64
65 static int
field_defined(char * field)66 field_defined(char *field)
67 {
68 return field && field[0] && strcmp(field, "*");
69 }
70
71 static int
parse_hex_field(char * str,int * outp,unsigned int * maskp,unsigned int max)72 parse_hex_field(char *str, int *outp, unsigned int *maskp, unsigned int max)
73 {
74 unsigned int out = 0;
75 unsigned int mask = ~0U;
76 unsigned int bound = 0;
77
78 if (!field_defined(str))
79 return 1; // and keep the defaults
80
81 // Historically, filters allowed writing hexadecimal numbers with leading "0x".
82 // This was never intentional nor documented, but some people relied on it.
83 if (!maskp && str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
84 str += 2;
85
86 while (*str)
87 {
88 int c = *str++;
89 int d;
90
91 if ((c == 'x' || c == 'X') && maskp)
92 {
93 out = out << 4;
94 bound = (bound << 4) | 1;
95 mask = mask << 4;
96 }
97 else
98 {
99 if (c >= '0' && c <= '9')
100 d = c - '0';
101 else if (c >= 'A' && c <= 'F')
102 d = c - 'A' + 10;
103 else if (c >= 'a' && c <= 'f')
104 d = c - 'a' + 10;
105 else
106 return 0;
107
108 out = (out << 4) | d;
109 bound = (bound << 4) | d;
110 mask = (mask << 4) | 0xf;
111 }
112
113 if (bound > max)
114 return 0;
115 }
116
117 *outp = out;
118 if (maskp)
119 *maskp = mask;
120 return 1;
121 }
122
123 /* Slot filter syntax: [[[domain]:][bus]:][slot][.[func]] */
124
125 char *
pci_filter_parse_slot_v38(struct pci_filter * f,char * str)126 pci_filter_parse_slot_v38(struct pci_filter *f, char *str)
127 {
128 char buf[BUF_SIZE];
129 char *fields[3];
130 char *err;
131
132 if (err = split_to_fields(str, buf, ':', fields, 3))
133 return err;
134
135 int i = 0;
136 if (fields[2])
137 {
138 if (!parse_hex_field(fields[0], &f->domain, NULL, 0x7fffffff))
139 return "Invalid domain number";
140 i++;
141 }
142
143 if (fields[i+1])
144 {
145 if (!parse_hex_field(fields[i], &f->bus, NULL, 0xff))
146 return "Invalid bus number";
147 i++;
148 }
149
150 char *fdev = fields[i];
151 if (field_defined(fdev))
152 {
153 char *sfields[2];
154 if (split_to_fields(fdev, NULL, '.', sfields, 2))
155 return "Invalid slot/function number";
156
157 if (!parse_hex_field(sfields[0], &f->slot, NULL, 0x1f))
158 return "Invalid slot number";
159
160 if (!parse_hex_field(sfields[1], &f->func, NULL, 7))
161 return "Invalid function number";
162 }
163
164 return NULL;
165 }
166
167 /* ID filter syntax: [vendor]:[device][:class[:progif]] */
168
169 char *
pci_filter_parse_id_v38(struct pci_filter * f,char * str)170 pci_filter_parse_id_v38(struct pci_filter *f, char *str)
171 {
172 char buf[BUF_SIZE];
173 char *fields[4];
174 char *err;
175
176 if (err = split_to_fields(str, buf, ':', fields, 4))
177 return err;
178
179 if (!fields[1])
180 return "At least two fields must be given";
181
182 if (!parse_hex_field(fields[0], &f->vendor, NULL, 0xffff))
183 return "Invalid vendor ID";
184
185 if (!parse_hex_field(fields[1], &f->device, NULL, 0xffff))
186 return "Invalid device ID";
187
188 if (!parse_hex_field(fields[2], &f->device_class, &f->device_class_mask, 0xffff))
189 return "Invalid class code";
190
191 if (!parse_hex_field(fields[3], &f->prog_if, NULL, 0xff))
192 return "Invalid programming interface code";
193
194 return NULL;
195 }
196
197 int
pci_filter_match_v38(struct pci_filter * f,struct pci_dev * d)198 pci_filter_match_v38(struct pci_filter *f, struct pci_dev *d)
199 {
200 if ((f->domain >= 0 && f->domain != d->domain) ||
201 (f->bus >= 0 && f->bus != d->bus) ||
202 (f->slot >= 0 && f->slot != d->dev) ||
203 (f->func >= 0 && f->func != d->func))
204 return 0;
205 if (f->device >= 0 || f->vendor >= 0)
206 {
207 pci_fill_info_v313(d, PCI_FILL_IDENT);
208 if ((f->device >= 0 && f->device != d->device_id) ||
209 (f->vendor >= 0 && f->vendor != d->vendor_id))
210 return 0;
211 }
212 if (f->device_class >= 0)
213 {
214 pci_fill_info_v313(d, PCI_FILL_CLASS);
215 if ((f->device_class ^ d->device_class) & f->device_class_mask)
216 return 0;
217 }
218 if (f->prog_if >= 0)
219 {
220 pci_fill_info_v313(d, PCI_FILL_CLASS_EXT);
221 if (f->prog_if != d->prog_if)
222 return 0;
223 }
224 return 1;
225 }
226
227 /*
228 * Before pciutils v3.3, struct pci_filter had fewer fields,
229 * so we have to provide compatibility wrappers.
230 */
231
232 struct pci_filter_v30 {
233 int domain, bus, slot, func; /* -1 = ANY */
234 int vendor, device;
235 };
236
237 void pci_filter_init_v30(struct pci_access *a, struct pci_filter_v30 *f) VERSIONED_ABI;
238 char *pci_filter_parse_slot_v30(struct pci_filter_v30 *f, char *str) VERSIONED_ABI;
239 char *pci_filter_parse_id_v30(struct pci_filter_v30 *f, char *str) VERSIONED_ABI;
240 int pci_filter_match_v30(struct pci_filter_v30 *f, struct pci_dev *d) VERSIONED_ABI;
241
242 static void
pci_filter_import_v30(struct pci_filter_v30 * old,struct pci_filter * new)243 pci_filter_import_v30(struct pci_filter_v30 *old, struct pci_filter *new)
244 {
245 new->domain = old->domain;
246 new->bus = old->bus;
247 new->slot = old->slot;
248 new->func = old->func;
249 new->vendor = old->vendor;
250 new->device = old->device;
251 new->device_class = -1;
252 new->device_class_mask = ~0U;
253 new->prog_if = -1;
254 }
255
256 static void
pci_filter_export_v30(struct pci_filter * new,struct pci_filter_v30 * old)257 pci_filter_export_v30(struct pci_filter *new, struct pci_filter_v30 *old)
258 {
259 old->domain = new->domain;
260 old->bus = new->bus;
261 old->slot = new->slot;
262 old->func = new->func;
263 old->vendor = new->vendor;
264 old->device = new->device;
265 }
266
267 void
pci_filter_init_v30(struct pci_access * a,struct pci_filter_v30 * f)268 pci_filter_init_v30(struct pci_access *a, struct pci_filter_v30 *f)
269 {
270 struct pci_filter new;
271 pci_filter_init_v38(a, &new);
272 pci_filter_export_v30(&new, f);
273 }
274
275 char *
pci_filter_parse_slot_v30(struct pci_filter_v30 * f,char * str)276 pci_filter_parse_slot_v30(struct pci_filter_v30 *f, char *str)
277 {
278 struct pci_filter new;
279 char *err;
280 pci_filter_import_v30(f, &new);
281 if (err = pci_filter_parse_slot_v38(&new, str))
282 return err;
283 pci_filter_export_v30(&new, f);
284 return NULL;
285 }
286
287 char *
pci_filter_parse_id_v30(struct pci_filter_v30 * f,char * str)288 pci_filter_parse_id_v30(struct pci_filter_v30 *f, char *str)
289 {
290 struct pci_filter new;
291 char *err;
292 pci_filter_import_v30(f, &new);
293 if (err = pci_filter_parse_id_v38(&new, str))
294 return err;
295 if (new.device_class >= 0 || new.prog_if >= 0)
296 return "Filtering by class or programming interface not supported in this program";
297 pci_filter_export_v30(&new, f);
298 return NULL;
299 }
300
301 int
pci_filter_match_v30(struct pci_filter_v30 * f,struct pci_dev * d)302 pci_filter_match_v30(struct pci_filter_v30 *f, struct pci_dev *d)
303 {
304 struct pci_filter new;
305 pci_filter_import_v30(f, &new);
306 return pci_filter_match_v38(&new, d);
307 }
308
309 // Version 3.3 is the same as version 3.8, only device_class_mask and prog_if were not implemented
310 // (their positions in struct pci_filter were declared as RFU).
311
312 STATIC_ALIAS(void pci_filter_init(struct pci_access *a, struct pci_filter *f), pci_filter_init_v38(a, f));
313 DEFINE_ALIAS(void pci_filter_init_v33(struct pci_access *a, struct pci_filter *f), pci_filter_init_v38);
314 SYMBOL_VERSION(pci_filter_init_v30, pci_filter_init@LIBPCI_3.0);
315 SYMBOL_VERSION(pci_filter_init_v33, pci_filter_init@LIBPCI_3.3);
316 SYMBOL_VERSION(pci_filter_init_v38, pci_filter_init@@LIBPCI_3.8);
317
318 STATIC_ALIAS(char *pci_filter_parse_slot(struct pci_filter *f, char *str), pci_filter_parse_slot_v38(f, str));
319 DEFINE_ALIAS(char *pci_filter_parse_slot_v33(struct pci_filter *f, char *str), pci_filter_parse_slot_v38);
320 SYMBOL_VERSION(pci_filter_parse_slot_v30, pci_filter_parse_slot@LIBPCI_3.0);
321 SYMBOL_VERSION(pci_filter_parse_slot_v33, pci_filter_parse_slot@LIBPCI_3.3);
322 SYMBOL_VERSION(pci_filter_parse_slot_v38, pci_filter_parse_slot@@LIBPCI_3.8);
323
324 STATIC_ALIAS(char *pci_filter_parse_id(struct pci_filter *f, char *str), pci_filter_parse_id_v38(f, str));
325 DEFINE_ALIAS(char *pci_filter_parse_id_v33(struct pci_filter *f, char *str), pci_filter_parse_id_v38);
326 SYMBOL_VERSION(pci_filter_parse_id_v30, pci_filter_parse_id@LIBPCI_3.0);
327 SYMBOL_VERSION(pci_filter_parse_id_v33, pci_filter_parse_id@LIBPCI_3.3);
328 SYMBOL_VERSION(pci_filter_parse_id_v38, pci_filter_parse_id@@LIBPCI_3.8);
329
330 STATIC_ALIAS(int pci_filter_match(struct pci_filter *f, struct pci_dev *d), pci_filter_match_v38(f, d));
331 DEFINE_ALIAS(int pci_filter_match_v33(struct pci_filter *f, struct pci_dev *d), pci_filter_match_v38);
332 SYMBOL_VERSION(pci_filter_match_v30, pci_filter_match@LIBPCI_3.0);
333 SYMBOL_VERSION(pci_filter_match_v33, pci_filter_match@LIBPCI_3.3);
334 SYMBOL_VERSION(pci_filter_match_v38, pci_filter_match@@LIBPCI_3.8);
335