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