2 * Copyright (c) 2017 Netflix, Inc.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
29 #include <sys/param.h>
40 #include <sys/linker.h>
41 #include <sys/module.h>
43 #include <sys/sysctl.h>
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' },
56 #define DEVMATCH_MAX_HITS 256
60 static char *linker_hints;
61 static char *nomatch_str;
62 static int unbound_flag;
63 static int verbose_flag;
66 static void *hints_end;
67 static struct devinfo_dev *root;
70 read_hints(const char *fn, size_t *len)
76 fd = open(fn, O_RDONLY);
80 err(1, "Can't open %s for reading", fn);
82 if (fstat(fd, &sb) != 0)
83 err(1, "Can't fstat %s\n", fn);
84 h = malloc(sb.st_size);
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);
95 read_linker_hints(void)
98 char *modpath, *p, *q;
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);
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.");
110 while ((q = strsep(&p, ";")) != NULL) {
111 snprintf(fn, sizeof(fn), "%s/linker.hints", q);
112 hints = read_hints(fn, &len);
118 errx(1, "Can't read linker hints file.");
120 hints = read_hints(linker_hints, &len);
122 err(1, "Can't open %s for reading", fn);
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);
132 hints_end = (void *)((intptr_t)hints + (intptr_t)len);
141 p = (int *)roundup2((intptr_t)p, sizeof(int));
148 getstr(void **ptr, char *val)
152 int len = *(uint8_t *)c;
154 memcpy(val, c + 1, len);
161 pnpval_as_int(const char *val, const char *pnpinfo)
170 cp = strchr(val, ';');
173 strlcpy(key + 1, val, sizeof(key) - 1);
175 memcpy(key + 1, val, cp - val);
176 key[cp - val + 1] = '\0';
178 strlcat(key, "=", sizeof(key));
179 if (strncmp(key + 1, pnpinfo, strlen(key + 1)) == 0)
180 rv = strtol(pnpinfo + strlen(key + 1), NULL, 0);
182 cp = strstr(pnpinfo, key);
186 rv = strtol(cp + strlen(key), NULL, 0);
192 quoted_strcpy(char *dst, const char *src)
196 if (*src == '\'' || *src == '"')
198 while (*src && *src != q)
199 *dst++ = *src++; // XXX backtick quoting
205 pnpval_as_str(const char *val, const char *pnpinfo)
207 static char retval[256];
211 if (pnpinfo == NULL) {
216 cp = strchr(val, ';');
219 strlcpy(key + 1, val, sizeof(key) - 1);
221 memcpy(key + 1, val, cp - val);
222 key[cp - val + 1] = '\0';
224 strlcat(key, "=", sizeof(key));
225 if (strncmp(key + 1, pnpinfo, strlen(key + 1)) == 0)
226 quoted_strcpy(retval, pnpinfo + strlen(key + 1));
228 cp = strstr(pnpinfo, key);
230 strcpy(retval, "MISSING");
232 quoted_strcpy(retval, cp + strlen(key));
238 search_hints(const char *bus, const char *dev, const char *pnpinfo)
240 char val1[256], val2[256];
241 int ival, len, ents, i, notme, mask, bit, v, found;
243 char *lastmod = NULL, *cp, *s;
249 printf("Searching bus %s dev %s for pnpinfo %s\n",
251 while (walker < hints_end) {
252 len = getint(&walker);
253 ival = getint(&walker);
260 if (dump_flag || verbose_flag)
261 printf("Version: if %s.%d kmod %s\n", val1, ival, val2);
268 lastmod = strdup(val2);
269 if (dump_flag || verbose_flag)
270 printf("Module %s in %s\n", val1, val2);
273 if (!dump_flag && !unbound_flag && lastmod && strcmp(lastmod, "kernel") == 0)
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) {
283 printf("Treating usb as uhub -- bug in source table still?\n");
284 strcpy(val1, "uhub");
286 if (bus && strcmp(val1, bus) != 0) {
288 printf("Skipped because table for bus %s, looking for %s\n",
292 for (i = 0; i < ents; i++) {
294 printf("---------- Entry %d ----------\n", i);
303 /* All integer fields */
311 printf("%#x:", ival);
314 if (bit >= 0 && ((1 << bit) & mask) == 0)
316 v = pnpval_as_int(cp + 2, pnpinfo);
318 printf("Matching %s (%c) table=%#x tomatch=%#x\n",
319 cp + 2, *cp, v, ival);
347 printf("'%s':", val1);
352 if (bit >= 0 && ((1 << bit) & mask) == 0)
354 s = pnpval_as_str(cp + 2, pnpinfo);
356 printf("Matching %s (%c) table=%s tomatch=%s\n",
357 cp + 2, *cp, s, val1);
358 if (strcmp(s, val1) != 0)
361 /* Key override fields, required to be last in the string */
364 * This is imperfect and only does one key and will be redone
365 * to be more general for multiple keys. Currently, nothing
368 if (dump_flag) /* No per-row data stored */
370 if (cp[strlen(cp) - 1] == ';') /* Skip required ; at end */
371 cp[strlen(cp) - 1] = '\0'; /* in case it's not there */
372 if ((s = strstr(pnpinfo, cp + 2)) == NULL)
374 else if (s > pnpinfo && s[-1] != ' ')
378 fprintf(stderr, "Unknown field type %c\n:", *cp);
382 cp = strchr(cp, ';');
391 printf("%s: %s", *dev ? dev : "unattached", lastmod);
393 printf("%s\n", lastmod);
395 printf("Matches --- %s ---\n", lastmod);
403 printf("Unknown Type %d len %d\n", ival, len);
406 walker = (void *)(len - sizeof(int) + (intptr_t)walker);
408 if (unbound_flag && found == 0 && *pnpinfo) {
410 printf("------------------------- ");
411 printf("%s on %s pnpinfo %s", *dev ? dev : "unattached", bus, pnpinfo);
413 printf(" -------------------------");
420 find_unmatched(struct devinfo_dev *dev, void *arg)
422 struct devinfo_dev *parent;
426 if (!all_flag && dev->dd_name[0] != '\0')
428 if (!(dev->dd_flags & DF_ENABLED))
430 if (dev->dd_flags & DF_ATTACHED_ONCE)
432 parent = devinfo_handle_to_device(dev->dd_parent);
433 bus = strdup(parent->dd_name);
434 p = bus + strlen(bus) - 1;
435 while (p >= bus && isdigit(*p))
439 printf("Searching %s %s bus at %s for pnpinfo %s\n",
440 dev->dd_name, bus, dev->dd_location, dev->dd_pnpinfo);
441 search_hints(bus, dev->dd_name, dev->dd_pnpinfo);
445 return (devinfo_foreach_device_child(dev, find_unmatched, arg));
452 struct devinfo_dev *dev;
456 * Look for the exact location specified by the nomatch event. The
457 * loc and pnpinfo run together to get the string we're looking for,
458 * so we have to synthesize the same thing that subr_bus.c is
459 * generating in devnomatch/devaddq to do the string comparison.
462 find_exact_dev(struct devinfo_dev *dev, void *arg)
464 struct devinfo_dev *parent;
466 struct exact_info *info;
470 if (info->dev != NULL)
472 if (!(dev->dd_flags & DF_ENABLED))
474 parent = devinfo_handle_to_device(dev->dd_parent);
475 if (strcmp(info->bus, parent->dd_name) != 0)
477 asprintf(&loc, "%s %s", parent->dd_pnpinfo,
478 parent->dd_location);
479 if (strcmp(loc, info->loc) == 0)
484 return (devinfo_foreach_device_child(dev, find_exact_dev, arg));
488 find_nomatch(char *nomatch)
490 char *bus, *pnpinfo, *tmp, *busnameunit;
491 struct exact_info info;
494 * Find our bus name. It will include the unit number. We have to search
495 * backwards to avoid false positive for any PNP string that has ' on '
496 * in them, which would come earlier in the string. Like if there were
497 * an 'Old Bard' ethernet card made by 'Stratford on Avon Hardware' or
498 * something silly like that.
500 tmp = nomatch + strlen(nomatch) - 4;
501 while (tmp > nomatch && strncmp(tmp, " on ", 4) != 0)
504 errx(1, "No bus found in nomatch string: '%s'", nomatch);
507 busnameunit = strdup(bus);
508 if (busnameunit == NULL)
509 errx(1, "Can't allocate memory for strings");
510 tmp = bus + strlen(bus) - 1;
511 while (tmp > bus && isdigit(*tmp))
516 * Note: the NOMATCH events place both the bus location as well as the
517 * pnp info after the 'at' and we don't know where one stops and the
518 * other begins, so we pass the whole thing to our search routine.
522 if (strncmp(nomatch, " at ", 4) != 0)
523 errx(1, "Malformed NOMATCH string: '%s'", nomatch);
524 pnpinfo = nomatch + 4;
527 * See if we can find the devinfo_dev for this device. If we
528 * can, and it's been attached before, we should filter it out
529 * so that a kldunload foo doesn't cause an immediate reload.
532 info.bus = busnameunit;
534 devinfo_foreach_device_child(root, find_exact_dev, (void *)&info);
535 if (info.dev != NULL && info.dev->dd_flags & DF_ATTACHED_ONCE)
537 search_hints(bus, "", pnpinfo);
546 errx(1, "devmatch [-adv] [-p nomatch] [-h linker-hints]");
550 main(int argc, char **argv)
554 while ((ch = getopt_long(argc, argv, "adh:p:uv",
555 longopts, NULL)) != -1) {
564 linker_hints = optarg;
567 nomatch_str = optarg;
587 search_hints(NULL, NULL, NULL);
592 err(1, "devinfo_init");
593 if ((root = devinfo_handle_to_device(DEVINFO_ROOT_DEVICE)) == NULL)
594 errx(1, "can't find root device");
595 if (nomatch_str != NULL)
596 find_nomatch(nomatch_str);
598 devinfo_foreach_device_child(root, find_unmatched, (void *)0);