xref: /freebsd/sbin/devmatch/devmatch.c (revision 19fae0f66023a97a9b464b3beeeabb2081f575b3)
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 <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 int all_flag;
61 static int dump_flag;
62 static char *linker_hints;
63 static char *nomatch_str;
64 static int quiet_flag;
65 static int unbound_flag;
66 static int 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 (*(int *)(intptr_t)hints != LINKER_HINTS_VERSION) {
133 		warnx("Linker hints version %d doesn't match expected %d.",
134 		    *(int *)(intptr_t)hints, LINKER_HINTS_VERSION);
135 		free(hints);
136 		hints = NULL;
137 	}
138 	if (hints != NULL)
139 		hints_end = (void *)((intptr_t)hints + (intptr_t)len);
140 }
141 
142 static int
143 getint(void **ptr)
144 {
145 	int *p = *ptr;
146 	int rv;
147 
148 	p = (int *)roundup2((intptr_t)p, sizeof(int));
149 	rv = *p++;
150 	*ptr = p;
151 	return rv;
152 }
153 
154 static void
155 getstr(void **ptr, char *val)
156 {
157 	int *p = *ptr;
158 	char *c = (char *)p;
159 	int len = *(uint8_t *)c;
160 
161 	memcpy(val, c + 1, len);
162 	val[len] = 0;
163 	c += len + 1;
164 	*ptr = (void *)c;
165 }
166 
167 static int
168 pnpval_as_int(const char *val, const char *pnpinfo)
169 {
170 	int rv;
171 	char key[256];
172 	char *cp;
173 
174 	if (pnpinfo == NULL)
175 		return -1;
176 
177 	cp = strchr(val, ';');
178 	key[0] = ' ';
179 	if (cp == NULL)
180 		strlcpy(key + 1, val, sizeof(key) - 1);
181 	else {
182 		memcpy(key + 1, val, cp - val);
183 		key[cp - val + 1] = '\0';
184 	}
185 	strlcat(key, "=", sizeof(key));
186 	if (strncmp(key + 1, pnpinfo, strlen(key + 1)) == 0)
187 		rv = strtol(pnpinfo + strlen(key + 1), NULL, 0);
188 	else {
189 		cp = strstr(pnpinfo, key);
190 		if (cp == NULL)
191 			rv = -1;
192 		else
193 			rv = strtol(cp + strlen(key), NULL, 0);
194 	}
195 	return rv;
196 }
197 
198 static void
199 quoted_strcpy(char *dst, const char *src)
200 {
201 	char q = ' ';
202 
203 	if (*src == '\'' || *src == '"')
204 		q = *src++;
205 	while (*src && *src != q)
206 		*dst++ = *src++; // XXX backtick quoting
207 	*dst++ = '\0';
208 	// XXX overflow
209 }
210 
211 static char *
212 pnpval_as_str(const char *val, const char *pnpinfo)
213 {
214 	static char retval[256];
215 	char key[256];
216 	char *cp;
217 
218 	if (pnpinfo == NULL) {
219 		*retval = '\0';
220 		return retval;
221 	}
222 
223 	cp = strchr(val, ';');
224 	key[0] = ' ';
225 	if (cp == NULL)
226 		strlcpy(key + 1, val, sizeof(key) - 1);
227 	else {
228 		memcpy(key + 1, val, cp - val);
229 		key[cp - val + 1] = '\0';
230 	}
231 	strlcat(key, "=", sizeof(key));
232 	if (strncmp(key + 1, pnpinfo, strlen(key + 1)) == 0)
233 		quoted_strcpy(retval, pnpinfo + strlen(key + 1));
234 	else {
235 		cp = strstr(pnpinfo, key);
236 		if (cp == NULL)
237 			strcpy(retval, "MISSING");
238 		else
239 			quoted_strcpy(retval, cp + strlen(key));
240 	}
241 	return retval;
242 }
243 
244 static void
245 search_hints(const char *bus, const char *dev, const char *pnpinfo)
246 {
247 	char val1[256], val2[256];
248 	int ival, len, ents, i, notme, mask, bit, v, found;
249 	void *ptr, *walker;
250 	char *lastmod = NULL, *cp, *s;
251 
252 	walker = hints;
253 	getint(&walker);
254 	found = 0;
255 	if (verbose_flag)
256 		printf("Searching bus %s dev %s for pnpinfo %s\n",
257 		    bus, dev, pnpinfo);
258 	while (walker < hints_end) {
259 		len = getint(&walker);
260 		ival = getint(&walker);
261 		ptr = walker;
262 		switch (ival) {
263 		case MDT_VERSION:
264 			getstr(&ptr, val1);
265 			ival = getint(&ptr);
266 			getstr(&ptr, val2);
267 			if (dump_flag || verbose_flag)
268 				printf("Version: if %s.%d kmod %s\n", val1, ival, val2);
269 			break;
270 		case MDT_MODULE:
271 			getstr(&ptr, val1);
272 			getstr(&ptr, val2);
273 			if (lastmod)
274 				free(lastmod);
275 			lastmod = strdup(val2);
276 			if (dump_flag || verbose_flag)
277 				printf("Module %s in %s\n", val1, val2);
278 			break;
279 		case MDT_PNP_INFO:
280 			if (!dump_flag && !unbound_flag && lastmod && strcmp(lastmod, "kernel") == 0)
281 				break;
282 			getstr(&ptr, val1);
283 			getstr(&ptr, val2);
284 			ents = getint(&ptr);
285 			if (dump_flag || verbose_flag)
286 				printf("PNP info for bus %s format %s %d entries (%s)\n",
287 				    val1, val2, ents, lastmod);
288 			if (strcmp(val1, "usb") == 0) {
289 				if (verbose_flag)
290 					printf("Treating usb as uhub -- bug in source table still?\n");
291 				strcpy(val1, "uhub");
292 			}
293 			if (bus && strcmp(val1, bus) != 0) {
294 				if (verbose_flag)
295 					printf("Skipped because table for bus %s, looking for %s\n",
296 					    val1, bus);
297 				break;
298 			}
299 			for (i = 0; i < ents; i++) {
300 				if (verbose_flag)
301 					printf("---------- Entry %d ----------\n", i);
302 				if (dump_flag)
303 					printf("   ");
304 				cp = val2;
305 				notme = 0;
306 				mask = -1;
307 				bit = -1;
308 				do {
309 					switch (*cp) {
310 						/* All integer fields */
311 					case 'I':
312 					case 'J':
313 					case 'G':
314 					case 'L':
315 					case 'M':
316 						ival = getint(&ptr);
317 						if (dump_flag) {
318 							printf("%#x:", ival);
319 							break;
320 						}
321 						if (bit >= 0 && ((1 << bit) & mask) == 0)
322 							break;
323 						if (cp[2] == '#') {
324 							if (verbose_flag) {
325 								printf("Ignoring %s (%c) table=%#x tomatch=%#x\n",
326 								    cp + 2, *cp, v, ival);
327 							}
328 							break;
329 						}
330 						v = pnpval_as_int(cp + 2, pnpinfo);
331 						if (verbose_flag)
332 							printf("Matching %s (%c) table=%#x tomatch=%#x\n",
333 							    cp + 2, *cp, v, ival);
334 						switch (*cp) {
335 						case 'J':
336 							if (ival == -1)
337 								break;
338 							/*FALLTHROUGH*/
339 						case 'I':
340 							if (v != ival)
341 								notme++;
342 							break;
343 						case 'G':
344 							if (v < ival)
345 								notme++;
346 							break;
347 						case 'L':
348 							if (v > ival)
349 								notme++;
350 							break;
351 						case 'M':
352 							mask = ival;
353 							break;
354 						}
355 						break;
356 						/* String fields */
357 					case 'D':
358 					case 'Z':
359 						getstr(&ptr, val1);
360 						if (dump_flag) {
361 							printf("'%s':", val1);
362 							break;
363 						}
364 						if (*cp == 'D')
365 							break;
366 						if (bit >= 0 && ((1 << bit) & mask) == 0)
367 							break;
368 						if (cp[2] == '#') {
369 							if (verbose_flag) {
370 								printf("Ignoring %s (%c) table=%#x tomatch=%#x\n",
371 								    cp + 2, *cp, v, ival);
372 							}
373 							break;
374 						}
375 						s = pnpval_as_str(cp + 2, pnpinfo);
376 						if (verbose_flag)
377 							printf("Matching %s (%c) table=%s tomatch=%s\n",
378 							    cp + 2, *cp, s, val1);
379 						if (strcmp(s, val1) != 0)
380 							notme++;
381 						break;
382 						/* Key override fields, required to be last in the string */
383 					case 'T':
384 						/*
385 						 * This is imperfect and only does one key and will be redone
386 						 * to be more general for multiple keys. Currently, nothing
387 						 * does that.
388 						 */
389 						if (dump_flag)				/* No per-row data stored */
390 							break;
391 						if (cp[strlen(cp) - 1] == ';')		/* Skip required ; at end */
392 							cp[strlen(cp) - 1] = '\0';	/* in case it's not there */
393 						if ((s = strstr(pnpinfo, cp + 2)) == NULL)
394 							notme++;
395 						else if (s > pnpinfo && s[-1] != ' ')
396 							notme++;
397 						break;
398 					default:
399 						fprintf(stderr, "Unknown field type %c\n:", *cp);
400 						break;
401 					}
402 					bit++;
403 					cp = strchr(cp, ';');
404 					if (cp)
405 						cp++;
406 				} while (cp && *cp);
407 				if (dump_flag)
408 					printf("\n");
409 				else if (!notme) {
410 					if (!unbound_flag) {
411 						if (all_flag)
412 							printf("%s: %s", *dev ? dev : "unattached", lastmod);
413 						else
414 							printf("%s\n", lastmod);
415 						if (verbose_flag)
416 							printf("Matches --- %s ---\n", lastmod);
417 					}
418 					found++;
419 				}
420 			}
421 			break;
422 		default:
423 			if (dump_flag)
424 				printf("Unknown Type %d len %d\n", ival, len);
425 			break;
426 		}
427 		walker = (void *)(len - sizeof(int) + (intptr_t)walker);
428 	}
429 	if (unbound_flag && found == 0 && *pnpinfo) {
430 		if (verbose_flag)
431 			printf("------------------------- ");
432 		printf("%s on %s pnpinfo %s", *dev ? dev : "unattached", bus, pnpinfo);
433 		if (verbose_flag)
434 			printf(" -------------------------");
435 		printf("\n");
436 	}
437 	free(lastmod);
438 }
439 
440 static int
441 find_unmatched(struct devinfo_dev *dev, void *arg)
442 {
443 	struct devinfo_dev *parent;
444 	char *bus, *p;
445 
446 	do {
447 		if (!all_flag && dev->dd_name[0] != '\0')
448 			break;
449 		if (!(dev->dd_flags & DF_ENABLED))
450 			break;
451 		if (dev->dd_flags & DF_ATTACHED_ONCE)
452 			break;
453 		parent = devinfo_handle_to_device(dev->dd_parent);
454 		bus = strdup(parent->dd_name);
455 		p = bus + strlen(bus) - 1;
456 		while (p >= bus && isdigit(*p))
457 			p--;
458 		*++p = '\0';
459 		if (verbose_flag)
460 			printf("Searching %s %s bus at %s for pnpinfo %s\n",
461 			    dev->dd_name, bus, dev->dd_location, dev->dd_pnpinfo);
462 		search_hints(bus, dev->dd_name, dev->dd_pnpinfo);
463 		free(bus);
464 	} while (0);
465 
466 	return (devinfo_foreach_device_child(dev, find_unmatched, arg));
467 }
468 
469 struct exact_info
470 {
471 	const char *bus;
472 	const char *loc;
473 	struct devinfo_dev *dev;
474 };
475 
476 /*
477  * Look for the exact location specified by the nomatch event.  The
478  * loc and pnpinfo run together to get the string we're looking for,
479  * so we have to synthesize the same thing that subr_bus.c is
480  * generating in devnomatch/devaddq to do the string comparison.
481  */
482 static int
483 find_exact_dev(struct devinfo_dev *dev, void *arg)
484 {
485 	struct devinfo_dev *parent;
486 	char *loc;
487 	struct exact_info *info;
488 
489 	info = arg;
490 	do {
491 		if (info->dev != NULL)
492 			break;
493 		if (!(dev->dd_flags & DF_ENABLED))
494 			break;
495 		parent = devinfo_handle_to_device(dev->dd_parent);
496 		if (strcmp(info->bus, parent->dd_name) != 0)
497 			break;
498 		asprintf(&loc, "%s %s", parent->dd_pnpinfo,
499 		    parent->dd_location);
500 		if (strcmp(loc, info->loc) == 0)
501 			info->dev = dev;
502 		free(loc);
503 	} while (0);
504 
505 	return (devinfo_foreach_device_child(dev, find_exact_dev, arg));
506 }
507 
508 static void
509 find_nomatch(char *nomatch)
510 {
511 	char *bus, *pnpinfo, *tmp, *busnameunit;
512 	struct exact_info info;
513 
514 	/*
515 	 * Find our bus name. It will include the unit number. We have to search
516 	 * backwards to avoid false positive for any PNP string that has ' on '
517 	 * in them, which would come earlier in the string. Like if there were
518 	 * an 'Old Bard' ethernet card made by 'Stratford on Avon Hardware' or
519 	 * something silly like that.
520 	 */
521 	tmp = nomatch + strlen(nomatch) - 4;
522 	while (tmp > nomatch && strncmp(tmp, " on ", 4) != 0)
523 		tmp--;
524 	if (tmp == nomatch)
525 		errx(1, "No bus found in nomatch string: '%s'", nomatch);
526 	bus = tmp + 4;
527 	*tmp = '\0';
528 	busnameunit = strdup(bus);
529 	if (busnameunit == NULL)
530 		errx(1, "Can't allocate memory for strings");
531 	tmp = bus + strlen(bus) - 1;
532 	while (tmp > bus && isdigit(*tmp))
533 		tmp--;
534 	*++tmp = '\0';
535 
536 	/*
537 	 * Note: the NOMATCH events place both the bus location as well as the
538 	 * pnp info after the 'at' and we don't know where one stops and the
539 	 * other begins, so we pass the whole thing to our search routine.
540 	 */
541 	if (*nomatch == '?')
542 		nomatch++;
543 	if (strncmp(nomatch, " at ", 4) != 0)
544 		errx(1, "Malformed NOMATCH string: '%s'", nomatch);
545 	pnpinfo = nomatch + 4;
546 
547 	/*
548 	 * See if we can find the devinfo_dev for this device. If we
549 	 * can, and it's been attached before, we should filter it out
550 	 * so that a kldunload foo doesn't cause an immediate reload.
551 	 */
552 	info.loc = pnpinfo;
553 	info.bus = busnameunit;
554 	info.dev = NULL;
555 	devinfo_foreach_device_child(root, find_exact_dev, (void *)&info);
556 	if (info.dev != NULL && info.dev->dd_flags & DF_ATTACHED_ONCE)
557 		exit(0);
558 	search_hints(bus, "", pnpinfo);
559 
560 	exit(0);
561 }
562 
563 static void
564 usage(void)
565 {
566 
567 	errx(1, "devmatch [-adv] [-p nomatch] [-h linker-hints]");
568 }
569 
570 int
571 main(int argc, char **argv)
572 {
573 	int ch;
574 
575 	while ((ch = getopt_long(argc, argv, "adh:p:quv",
576 		    longopts, NULL)) != -1) {
577 		switch (ch) {
578 		case 'a':
579 			all_flag++;
580 			break;
581 		case 'd':
582 			dump_flag++;
583 			break;
584 		case 'h':
585 			linker_hints = optarg;
586 			break;
587 		case 'p':
588 			nomatch_str = optarg;
589 			break;
590 		case 'q':
591 			quiet_flag++;
592 			break;
593 		case 'u':
594 			unbound_flag++;
595 			break;
596 		case 'v':
597 			verbose_flag++;
598 			break;
599 		default:
600 			usage();
601 		}
602 	}
603 	argc -= optind;
604 	argv += optind;
605 
606 	if (argc >= 1)
607 		usage();
608 
609 	read_linker_hints();
610 	if (dump_flag) {
611 		search_hints(NULL, NULL, NULL);
612 		exit(0);
613 	}
614 
615 	if (devinfo_init())
616 		err(1, "devinfo_init");
617 	if ((root = devinfo_handle_to_device(DEVINFO_ROOT_DEVICE)) == NULL)
618 		errx(1, "can't find root device");
619 	if (nomatch_str != NULL)
620 		find_nomatch(nomatch_str);
621 	else
622 		devinfo_foreach_device_child(root, find_unmatched, (void *)0);
623 	devinfo_free();
624 }
625