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