xref: /freebsd/sbin/devmatch/devmatch.c (revision 19261079)
1 /*-
2  * Copyright (c) 2017 Netflix, Inc.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  */
25 
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
28 
29 #include <sys/param.h>
30 #include <ctype.h>
31 #include <devinfo.h>
32 #include <err.h>
33 #include <errno.h>
34 #include <fcntl.h>
35 #include <getopt.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40 #include <sys/linker.h>
41 #include <sys/module.h>
42 #include <sys/stat.h>
43 #include <sys/sysctl.h>
44 
45 /* options descriptor */
46 static struct option longopts[] = {
47 	{ "all",		no_argument,		NULL,	'a' },
48 	{ "dump",		no_argument,		NULL,	'd' },
49 	{ "hints",		required_argument,	NULL,	'h' },
50 	{ "nomatch",		required_argument,	NULL,	'p' },
51 	{ "unbound",		no_argument,		NULL,	'u' },
52 	{ "verbose",		no_argument,		NULL,	'v' },
53 	{ NULL,			0,			NULL,	0 }
54 };
55 
56 #define	DEVMATCH_MAX_HITS 256
57 
58 static int all_flag;
59 static int dump_flag;
60 static char *linker_hints;
61 static char *nomatch_str;
62 static int unbound_flag;
63 static int verbose_flag;
64 
65 static void *hints;
66 static void *hints_end;
67 static struct devinfo_dev *root;
68 
69 static void *
70 read_hints(const char *fn, size_t *len)
71 {
72 	void *h;
73 	int fd;
74 	struct stat sb;
75 
76 	fd = open(fn, O_RDONLY);
77 	if (fd < 0) {
78 		if (errno == ENOENT)
79 			return NULL;
80 		err(1, "Can't open %s for reading", fn);
81 	}
82 	if (fstat(fd, &sb) != 0)
83 		err(1, "Can't fstat %s\n", fn);
84 	h = malloc(sb.st_size);
85 	if (h == NULL)
86 		err(1, "not enough space to read hints file of %ju bytes", (uintmax_t)sb.st_size);
87 	if (read(fd, h, sb.st_size) != sb.st_size)
88 		err(1, "Can't read in %ju bytes from %s", (uintmax_t)sb.st_size, fn);
89 	close(fd);
90 	*len = sb.st_size;
91 	return h;
92 }
93 
94 static void
95 read_linker_hints(void)
96 {
97 	char fn[MAXPATHLEN];
98 	char *modpath, *p, *q;
99 	size_t buflen, len;
100 
101 	if (linker_hints == NULL) {
102 		if (sysctlbyname("kern.module_path", NULL, &buflen, NULL, 0) < 0)
103 			errx(1, "Can't find kernel module path.");
104 		modpath = malloc(buflen);
105 		if (modpath == NULL)
106 			err(1, "Can't get memory for modpath.");
107 		if (sysctlbyname("kern.module_path", modpath, &buflen, NULL, 0) < 0)
108 			errx(1, "Can't find kernel module path.");
109 		p = modpath;
110 		while ((q = strsep(&p, ";")) != NULL) {
111 			snprintf(fn, sizeof(fn), "%s/linker.hints", q);
112 			hints = read_hints(fn, &len);
113 			if (hints == NULL)
114 				continue;
115 			break;
116 		}
117 		if (q == NULL)
118 			errx(1, "Can't read linker hints file.");
119 	} else {
120 		hints = read_hints(linker_hints, &len);
121 		if (hints == NULL)
122 			err(1, "Can't open %s for reading", fn);
123 	}
124 
125 	if (*(int *)(intptr_t)hints != LINKER_HINTS_VERSION) {
126 		warnx("Linker hints version %d doesn't match expected %d.",
127 		    *(int *)(intptr_t)hints, LINKER_HINTS_VERSION);
128 		free(hints);
129 		hints = NULL;
130 	}
131 	if (hints != NULL)
132 		hints_end = (void *)((intptr_t)hints + (intptr_t)len);
133 }
134 
135 static int
136 getint(void **ptr)
137 {
138 	int *p = *ptr;
139 	int rv;
140 
141 	p = (int *)roundup2((intptr_t)p, sizeof(int));
142 	rv = *p++;
143 	*ptr = p;
144 	return rv;
145 }
146 
147 static void
148 getstr(void **ptr, char *val)
149 {
150 	int *p = *ptr;
151 	char *c = (char *)p;
152 	int len = *(uint8_t *)c;
153 
154 	memcpy(val, c + 1, len);
155 	val[len] = 0;
156 	c += len + 1;
157 	*ptr = (void *)c;
158 }
159 
160 static int
161 pnpval_as_int(const char *val, const char *pnpinfo)
162 {
163 	int rv;
164 	char key[256];
165 	char *cp;
166 
167 	if (pnpinfo == NULL)
168 		return -1;
169 
170 	cp = strchr(val, ';');
171 	key[0] = ' ';
172 	if (cp == NULL)
173 		strlcpy(key + 1, val, sizeof(key) - 1);
174 	else {
175 		memcpy(key + 1, val, cp - val);
176 		key[cp - val + 1] = '\0';
177 	}
178 	strlcat(key, "=", sizeof(key));
179 	if (strncmp(key + 1, pnpinfo, strlen(key + 1)) == 0)
180 		rv = strtol(pnpinfo + strlen(key + 1), NULL, 0);
181 	else {
182 		cp = strstr(pnpinfo, key);
183 		if (cp == NULL)
184 			rv = -1;
185 		else
186 			rv = strtol(cp + strlen(key), NULL, 0);
187 	}
188 	return rv;
189 }
190 
191 static void
192 quoted_strcpy(char *dst, const char *src)
193 {
194 	char q = ' ';
195 
196 	if (*src == '\'' || *src == '"')
197 		q = *src++;
198 	while (*src && *src != q)
199 		*dst++ = *src++; // XXX backtick quoting
200 	*dst++ = '\0';
201 	// XXX overflow
202 }
203 
204 static char *
205 pnpval_as_str(const char *val, const char *pnpinfo)
206 {
207 	static char retval[256];
208 	char key[256];
209 	char *cp;
210 
211 	if (pnpinfo == NULL) {
212 		*retval = '\0';
213 		return retval;
214 	}
215 
216 	cp = strchr(val, ';');
217 	key[0] = ' ';
218 	if (cp == NULL)
219 		strlcpy(key + 1, val, sizeof(key) - 1);
220 	else {
221 		memcpy(key + 1, val, cp - val);
222 		key[cp - val + 1] = '\0';
223 	}
224 	strlcat(key, "=", sizeof(key));
225 	if (strncmp(key + 1, pnpinfo, strlen(key + 1)) == 0)
226 		quoted_strcpy(retval, pnpinfo + strlen(key + 1));
227 	else {
228 		cp = strstr(pnpinfo, key);
229 		if (cp == NULL)
230 			strcpy(retval, "MISSING");
231 		else
232 			quoted_strcpy(retval, cp + strlen(key));
233 	}
234 	return retval;
235 }
236 
237 static void
238 search_hints(const char *bus, const char *dev, const char *pnpinfo)
239 {
240 	char val1[256], val2[256];
241 	int ival, len, ents, i, notme, mask, bit, v, found;
242 	void *ptr, *walker;
243 	char *lastmod = NULL, *cp, *s;
244 
245 	walker = hints;
246 	getint(&walker);
247 	found = 0;
248 	if (verbose_flag)
249 		printf("Searching bus %s dev %s for pnpinfo %s\n",
250 		    bus, dev, pnpinfo);
251 	while (walker < hints_end) {
252 		len = getint(&walker);
253 		ival = getint(&walker);
254 		ptr = walker;
255 		switch (ival) {
256 		case MDT_VERSION:
257 			getstr(&ptr, val1);
258 			ival = getint(&ptr);
259 			getstr(&ptr, val2);
260 			if (dump_flag || verbose_flag)
261 				printf("Version: if %s.%d kmod %s\n", val1, ival, val2);
262 			break;
263 		case MDT_MODULE:
264 			getstr(&ptr, val1);
265 			getstr(&ptr, val2);
266 			if (lastmod)
267 				free(lastmod);
268 			lastmod = strdup(val2);
269 			if (dump_flag || verbose_flag)
270 				printf("Module %s in %s\n", val1, val2);
271 			break;
272 		case MDT_PNP_INFO:
273 			if (!dump_flag && !unbound_flag && lastmod && strcmp(lastmod, "kernel") == 0)
274 				break;
275 			getstr(&ptr, val1);
276 			getstr(&ptr, val2);
277 			ents = getint(&ptr);
278 			if (dump_flag || verbose_flag)
279 				printf("PNP info for bus %s format %s %d entries (%s)\n",
280 				    val1, val2, ents, lastmod);
281 			if (strcmp(val1, "usb") == 0) {
282 				if (verbose_flag)
283 					printf("Treating usb as uhub -- bug in source table still?\n");
284 				strcpy(val1, "uhub");
285 			}
286 			if (bus && strcmp(val1, bus) != 0) {
287 				if (verbose_flag)
288 					printf("Skipped because table for bus %s, looking for %s\n",
289 					    val1, bus);
290 				break;
291 			}
292 			for (i = 0; i < ents; i++) {
293 				if (verbose_flag)
294 					printf("---------- Entry %d ----------\n", i);
295 				if (dump_flag)
296 					printf("   ");
297 				cp = val2;
298 				notme = 0;
299 				mask = -1;
300 				bit = -1;
301 				do {
302 					switch (*cp) {
303 						/* All integer fields */
304 					case 'I':
305 					case 'J':
306 					case 'G':
307 					case 'L':
308 					case 'M':
309 						ival = getint(&ptr);
310 						if (dump_flag) {
311 							printf("%#x:", ival);
312 							break;
313 						}
314 						if (bit >= 0 && ((1 << bit) & mask) == 0)
315 							break;
316 						if (strcmp(cp + 2, "#") == 0) {
317 							if (verbose_flag) {
318 								printf("Ignoring %s (%c) table=%#x tomatch=%#x\n",
319 								    cp + 2, *cp, v, ival);
320 							}
321 							break;
322 						}
323 						v = pnpval_as_int(cp + 2, pnpinfo);
324 						if (verbose_flag)
325 							printf("Matching %s (%c) table=%#x tomatch=%#x\n",
326 							    cp + 2, *cp, v, ival);
327 						switch (*cp) {
328 						case 'J':
329 							if (ival == -1)
330 								break;
331 							/*FALLTHROUGH*/
332 						case 'I':
333 							if (v != ival)
334 								notme++;
335 							break;
336 						case 'G':
337 							if (v < ival)
338 								notme++;
339 							break;
340 						case 'L':
341 							if (v > ival)
342 								notme++;
343 							break;
344 						case 'M':
345 							mask = ival;
346 							break;
347 						}
348 						break;
349 						/* String fields */
350 					case 'D':
351 					case 'Z':
352 						getstr(&ptr, val1);
353 						if (dump_flag) {
354 							printf("'%s':", val1);
355 							break;
356 						}
357 						if (*cp == 'D')
358 							break;
359 						if (bit >= 0 && ((1 << bit) & mask) == 0)
360 							break;
361 						if (strcmp(cp + 2, "#") == 0) {
362 							if (verbose_flag) {
363 								printf("Ignoring %s (%c) table=%#x tomatch=%#x\n",
364 								    cp + 2, *cp, v, ival);
365 							}
366 							break;
367 						}
368 						s = pnpval_as_str(cp + 2, pnpinfo);
369 						if (verbose_flag)
370 							printf("Matching %s (%c) table=%s tomatch=%s\n",
371 							    cp + 2, *cp, s, val1);
372 						if (strcmp(s, val1) != 0)
373 							notme++;
374 						break;
375 						/* Key override fields, required to be last in the string */
376 					case 'T':
377 						/*
378 						 * This is imperfect and only does one key and will be redone
379 						 * to be more general for multiple keys. Currently, nothing
380 						 * does that.
381 						 */
382 						if (dump_flag)				/* No per-row data stored */
383 							break;
384 						if (cp[strlen(cp) - 1] == ';')		/* Skip required ; at end */
385 							cp[strlen(cp) - 1] = '\0';	/* in case it's not there */
386 						if ((s = strstr(pnpinfo, cp + 2)) == NULL)
387 							notme++;
388 						else if (s > pnpinfo && s[-1] != ' ')
389 							notme++;
390 						break;
391 					default:
392 						fprintf(stderr, "Unknown field type %c\n:", *cp);
393 						break;
394 					}
395 					bit++;
396 					cp = strchr(cp, ';');
397 					if (cp)
398 						cp++;
399 				} while (cp && *cp);
400 				if (dump_flag)
401 					printf("\n");
402 				else if (!notme) {
403 					if (!unbound_flag) {
404 						if (all_flag)
405 							printf("%s: %s", *dev ? dev : "unattached", lastmod);
406 						else
407 							printf("%s\n", lastmod);
408 						if (verbose_flag)
409 							printf("Matches --- %s ---\n", lastmod);
410 					}
411 					found++;
412 				}
413 			}
414 			break;
415 		default:
416 			if (dump_flag)
417 				printf("Unknown Type %d len %d\n", ival, len);
418 			break;
419 		}
420 		walker = (void *)(len - sizeof(int) + (intptr_t)walker);
421 	}
422 	if (unbound_flag && found == 0 && *pnpinfo) {
423 		if (verbose_flag)
424 			printf("------------------------- ");
425 		printf("%s on %s pnpinfo %s", *dev ? dev : "unattached", bus, pnpinfo);
426 		if (verbose_flag)
427 			printf(" -------------------------");
428 		printf("\n");
429 	}
430 	free(lastmod);
431 }
432 
433 static int
434 find_unmatched(struct devinfo_dev *dev, void *arg)
435 {
436 	struct devinfo_dev *parent;
437 	char *bus, *p;
438 
439 	do {
440 		if (!all_flag && dev->dd_name[0] != '\0')
441 			break;
442 		if (!(dev->dd_flags & DF_ENABLED))
443 			break;
444 		if (dev->dd_flags & DF_ATTACHED_ONCE)
445 			break;
446 		parent = devinfo_handle_to_device(dev->dd_parent);
447 		bus = strdup(parent->dd_name);
448 		p = bus + strlen(bus) - 1;
449 		while (p >= bus && isdigit(*p))
450 			p--;
451 		*++p = '\0';
452 		if (verbose_flag)
453 			printf("Searching %s %s bus at %s for pnpinfo %s\n",
454 			    dev->dd_name, bus, dev->dd_location, dev->dd_pnpinfo);
455 		search_hints(bus, dev->dd_name, dev->dd_pnpinfo);
456 		free(bus);
457 	} while (0);
458 
459 	return (devinfo_foreach_device_child(dev, find_unmatched, arg));
460 }
461 
462 struct exact_info
463 {
464 	const char *bus;
465 	const char *loc;
466 	struct devinfo_dev *dev;
467 };
468 
469 /*
470  * Look for the exact location specified by the nomatch event.  The
471  * loc and pnpinfo run together to get the string we're looking for,
472  * so we have to synthesize the same thing that subr_bus.c is
473  * generating in devnomatch/devaddq to do the string comparison.
474  */
475 static int
476 find_exact_dev(struct devinfo_dev *dev, void *arg)
477 {
478 	struct devinfo_dev *parent;
479 	char *loc;
480 	struct exact_info *info;
481 
482 	info = arg;
483 	do {
484 		if (info->dev != NULL)
485 			break;
486 		if (!(dev->dd_flags & DF_ENABLED))
487 			break;
488 		parent = devinfo_handle_to_device(dev->dd_parent);
489 		if (strcmp(info->bus, parent->dd_name) != 0)
490 			break;
491 		asprintf(&loc, "%s %s", parent->dd_pnpinfo,
492 		    parent->dd_location);
493 		if (strcmp(loc, info->loc) == 0)
494 			info->dev = dev;
495 		free(loc);
496 	} while (0);
497 
498 	return (devinfo_foreach_device_child(dev, find_exact_dev, arg));
499 }
500 
501 static void
502 find_nomatch(char *nomatch)
503 {
504 	char *bus, *pnpinfo, *tmp, *busnameunit;
505 	struct exact_info info;
506 
507 	/*
508 	 * Find our bus name. It will include the unit number. We have to search
509 	 * backwards to avoid false positive for any PNP string that has ' on '
510 	 * in them, which would come earlier in the string. Like if there were
511 	 * an 'Old Bard' ethernet card made by 'Stratford on Avon Hardware' or
512 	 * something silly like that.
513 	 */
514 	tmp = nomatch + strlen(nomatch) - 4;
515 	while (tmp > nomatch && strncmp(tmp, " on ", 4) != 0)
516 		tmp--;
517 	if (tmp == nomatch)
518 		errx(1, "No bus found in nomatch string: '%s'", nomatch);
519 	bus = tmp + 4;
520 	*tmp = '\0';
521 	busnameunit = strdup(bus);
522 	if (busnameunit == NULL)
523 		errx(1, "Can't allocate memory for strings");
524 	tmp = bus + strlen(bus) - 1;
525 	while (tmp > bus && isdigit(*tmp))
526 		tmp--;
527 	*++tmp = '\0';
528 
529 	/*
530 	 * Note: the NOMATCH events place both the bus location as well as the
531 	 * pnp info after the 'at' and we don't know where one stops and the
532 	 * other begins, so we pass the whole thing to our search routine.
533 	 */
534 	if (*nomatch == '?')
535 		nomatch++;
536 	if (strncmp(nomatch, " at ", 4) != 0)
537 		errx(1, "Malformed NOMATCH string: '%s'", nomatch);
538 	pnpinfo = nomatch + 4;
539 
540 	/*
541 	 * See if we can find the devinfo_dev for this device. If we
542 	 * can, and it's been attached before, we should filter it out
543 	 * so that a kldunload foo doesn't cause an immediate reload.
544 	 */
545 	info.loc = pnpinfo;
546 	info.bus = busnameunit;
547 	info.dev = NULL;
548 	devinfo_foreach_device_child(root, find_exact_dev, (void *)&info);
549 	if (info.dev != NULL && info.dev->dd_flags & DF_ATTACHED_ONCE)
550 		exit(0);
551 	search_hints(bus, "", pnpinfo);
552 
553 	exit(0);
554 }
555 
556 static void
557 usage(void)
558 {
559 
560 	errx(1, "devmatch [-adv] [-p nomatch] [-h linker-hints]");
561 }
562 
563 int
564 main(int argc, char **argv)
565 {
566 	int ch;
567 
568 	while ((ch = getopt_long(argc, argv, "adh:p:uv",
569 		    longopts, NULL)) != -1) {
570 		switch (ch) {
571 		case 'a':
572 			all_flag++;
573 			break;
574 		case 'd':
575 			dump_flag++;
576 			break;
577 		case 'h':
578 			linker_hints = optarg;
579 			break;
580 		case 'p':
581 			nomatch_str = optarg;
582 			break;
583 		case 'u':
584 			unbound_flag++;
585 			break;
586 		case 'v':
587 			verbose_flag++;
588 			break;
589 		default:
590 			usage();
591 		}
592 	}
593 	argc -= optind;
594 	argv += optind;
595 
596 	if (argc >= 1)
597 		usage();
598 
599 	read_linker_hints();
600 	if (dump_flag) {
601 		search_hints(NULL, NULL, NULL);
602 		exit(0);
603 	}
604 
605 	if (devinfo_init())
606 		err(1, "devinfo_init");
607 	if ((root = devinfo_handle_to_device(DEVINFO_ROOT_DEVICE)) == NULL)
608 		errx(1, "can't find root device");
609 	if (nomatch_str != NULL)
610 		find_nomatch(nomatch_str);
611 	else
612 		devinfo_foreach_device_child(root, find_unmatched, (void *)0);
613 	devinfo_free();
614 }
615