2 * Copyright (c) 2017 Netflix, Inc
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
41 #include <sys/linker.h>
42 #include <sys/module.h>
44 #include <sys/sysctl.h>
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' },
57 #define DEVMATCH_MAX_HITS 256
59 static struct match_data {
62 } match_data[DEVMATCH_MAX_HITS];
67 static char *linker_hints;
68 static char *nomatch_str;
69 static int unbound_flag;
70 static int verbose_flag;
73 static void *hints_end;
76 read_hints(const char *fn, size_t *len)
82 fd = open(fn, O_RDONLY);
86 err(1, "Can't open %s for reading", fn);
88 if (fstat(fd, &sb) != 0)
89 err(1, "Can't fstat %s\n", fn);
90 h = malloc(sb.st_size);
92 err(1, "not enough space to read hints file of %ju bytes", (uintmax_t)sb.st_size);
93 if (read(fd, h, sb.st_size) != sb.st_size)
94 err(1, "Can't read in %ju bytes from %s", (uintmax_t)sb.st_size, fn);
101 read_linker_hints(void)
104 char *modpath, *p, *q;
107 if (linker_hints == NULL) {
108 if (sysctlbyname("kern.module_path", NULL, &buflen, NULL, 0) < 0)
109 errx(1, "Can't find kernel module path.");
110 modpath = malloc(buflen);
112 err(1, "Can't get memory for modpath.");
113 if (sysctlbyname("kern.module_path", modpath, &buflen, NULL, 0) < 0)
114 errx(1, "Can't find kernel module path.");
116 while ((q = strsep(&p, ";")) != NULL) {
117 snprintf(fn, sizeof(fn), "%s/linker.hints", q);
118 hints = read_hints(fn, &len);
124 errx(1, "Can't read linker hints file.");
126 hints = read_hints(linker_hints, &len);
128 err(1, "Can't open %s for reading", fn);
131 if (*(int *)(intptr_t)hints != LINKER_HINTS_VERSION) {
132 warnx("Linker hints version %d doesn't match expected %d.",
133 *(int *)(intptr_t)hints, LINKER_HINTS_VERSION);
138 hints_end = (void *)((intptr_t)hints + (intptr_t)len);
147 p = (int *)roundup2((intptr_t)p, sizeof(int));
154 getstr(void **ptr, char *val)
158 int len = *(uint8_t *)c;
160 memcpy(val, c + 1, len);
167 pnpval_as_int(const char *val, const char *pnpinfo)
176 cp = strchr(val, ';');
179 strlcpy(key + 1, val, sizeof(key) - 1);
181 memcpy(key + 1, val, cp - val);
182 key[cp - val + 1] = '\0';
184 strlcat(key, "=", sizeof(key));
185 if (strncmp(key + 1, pnpinfo, strlen(key + 1)) == 0)
186 rv = strtol(pnpinfo + strlen(key + 1), NULL, 0);
188 cp = strstr(pnpinfo, key);
192 rv = strtol(cp + strlen(key), NULL, 0);
198 quoted_strcpy(char *dst, const char *src)
202 if (*src == '\'' || *src == '"')
204 while (*src && *src != q)
205 *dst++ = *src++; // XXX backtick quoting
211 pnpval_as_str(const char *val, const char *pnpinfo)
213 static char retval[256];
217 if (pnpinfo == NULL) {
222 cp = strchr(val, ';');
225 strlcpy(key + 1, val, sizeof(key) - 1);
227 memcpy(key + 1, val, cp - val);
228 key[cp - val + 1] = '\0';
230 strlcat(key, "=", sizeof(key));
231 if (strncmp(key + 1, pnpinfo, strlen(key + 1)) == 0)
232 quoted_strcpy(retval, pnpinfo + strlen(key + 1));
234 cp = strstr(pnpinfo, key);
236 strcpy(retval, "MISSING");
238 quoted_strcpy(retval, cp + strlen(key));
244 match_data_compare(const void *_pa, const void *_pb)
246 const struct match_data *pa = _pa;
247 const struct match_data *pb = _pb;
249 /* biggest value first */
250 if (pa->priority > pb->priority)
252 else if (pa->priority < pb->priority)
255 /* then sort by string */
256 return (strcmp(pa->descr, pb->descr));
265 for (x = 0; x != 16; x++) {
266 if ((input >> x) & 1)
267 retval |= (0x8000 >> x);
273 search_hints(const char *bus, const char *dev, const char *pnpinfo)
275 char val1[256], val2[256];
276 int ival, len, ents, i, notme, mask, bit, v, found;
278 char *lastmod = NULL, *cp, *s;
284 printf("Searching bus %s dev %s for pnpinfo %s\n",
286 while (walker < hints_end) {
287 len = getint(&walker);
288 ival = getint(&walker);
295 if (dump_flag || verbose_flag)
296 printf("Version: if %s.%d kmod %s\n", val1, ival, val2);
303 lastmod = strdup(val2);
304 if (dump_flag || verbose_flag)
305 printf("Module %s in %s\n", val1, val2);
308 if (!dump_flag && !unbound_flag && lastmod && strcmp(lastmod, "kernel") == 0)
313 if (dump_flag || verbose_flag)
314 printf("PNP info for bus %s format %s %d entries (%s)\n",
315 val1, val2, ents, lastmod);
316 if (strcmp(val1, "usb") == 0) {
318 printf("Treating usb as uhub -- bug in source table still?\n");
319 strcpy(val1, "uhub");
321 if (bus && strcmp(val1, bus) != 0) {
323 printf("Skipped because table for bus %s, looking for %s\n",
327 for (i = 0; i < ents; i++) {
329 printf("---------- Entry %d ----------\n", i);
338 /* All integer fields */
346 printf("%#x:", ival);
349 if (bit >= 0 && ((1 << bit) & mask) == 0)
351 v = pnpval_as_int(cp + 2, pnpinfo);
353 printf("Matching %s (%c) table=%#x tomatch=%#x\n",
354 cp + 2, *cp, v, ival);
382 printf("'%s':", val1);
387 s = pnpval_as_str(cp + 2, pnpinfo);
388 if (strcmp(s, val1) != 0)
391 /* Key override fields, required to be last in the string */
394 * This is imperfect and only does one key and will be redone
395 * to be more general for multiple keys. Currently, nothing
398 if (dump_flag) /* No per-row data stored */
400 if (cp[strlen(cp) - 1] == ';') /* Skip required ; at end */
401 cp[strlen(cp) - 1] = '\0'; /* in case it's not there */
402 if ((s = strstr(pnpinfo, cp + 2)) == NULL)
404 else if (s > pnpinfo && s[-1] != ' ')
408 fprintf(stderr, "Unknown field type %c\n:", *cp);
412 cp = strchr(cp, ';');
423 asprintf(&descr, "%s: %s", *dev ? dev : "unattached", lastmod);
425 asprintf(&descr, "%s", lastmod);
427 printf("Matches --- %s ---\n", lastmod);
429 if (descr != NULL && hit_index < DEVMATCH_MAX_HITS) {
430 match_data[hit_index].descr = descr;
431 match_data[hit_index].priority = bitrev16(mask);
443 printf("Unknown Type %d len %d\n", ival, len);
446 walker = (void *)(len - sizeof(int) + (intptr_t)walker);
448 if (hit_index != 0) {
449 /* sort hits by priority */
450 mergesort(match_data, hit_index, sizeof(match_data[0]), &match_data_compare);
453 for (i = 0; i != hit_index; i++) {
454 puts(match_data[i].descr);
455 free(match_data[i].descr);
458 /* reset hit_index */
461 if (unbound_flag && found == 0 && *pnpinfo) {
463 printf("------------------------- ");
464 printf("%s on %s pnpinfo %s", *dev ? dev : "unattached", bus, pnpinfo);
466 printf(" -------------------------");
473 find_unmatched(struct devinfo_dev *dev, void *arg)
475 struct devinfo_dev *parent;
479 if (!all_flag && dev->dd_name[0] != '\0')
481 if (!(dev->dd_flags & DF_ENABLED))
483 parent = devinfo_handle_to_device(dev->dd_parent);
484 bus = strdup(parent->dd_name);
485 p = bus + strlen(bus) - 1;
486 while (p >= bus && isdigit(*p))
490 printf("Searching %s %s bus at %s for pnpinfo %s\n",
491 dev->dd_name, bus, dev->dd_location, dev->dd_pnpinfo);
492 search_hints(bus, dev->dd_name, dev->dd_pnpinfo);
496 return (devinfo_foreach_device_child(dev, find_unmatched, arg));
500 find_nomatch(char *nomatch)
502 char *bus, *pnpinfo, *tmp;
505 * Find our bus name. It will include the unit number. We have to search
506 * backwards to avoid false positive for any PNP string that has ' on '
507 * in them, which would come earlier in the string. Like if there were
508 * an 'Old Bard' ethernet card made by 'Stratford on Avon Hardware' or
509 * something silly like that.
511 tmp = nomatch + strlen(nomatch) - 4;
512 while (tmp > nomatch && strncmp(tmp, " on ", 4) != 0)
515 errx(1, "No bus found in nomatch string: '%s'", nomatch);
518 tmp = bus + strlen(bus) - 1;
519 while (tmp > bus && isdigit(*tmp))
524 * Note: the NOMATCH events place both the bus location as well as the
525 * pnp info after the 'at' and we don't know where one stops and the
526 * other begins, so we pass the whole thing to our search routine.
530 if (strncmp(nomatch, " at ", 4) != 0)
531 errx(1, "Malformed NOMATCH string: '%s'", nomatch);
532 pnpinfo = nomatch + 4;
534 search_hints(bus, "", pnpinfo);
543 errx(1, "devmatch [-adv] [-p nomatch] [-h linker-hints]");
547 main(int argc, char **argv)
549 struct devinfo_dev *root;
552 while ((ch = getopt_long(argc, argv, "adh:p:uv",
553 longopts, NULL)) != -1) {
562 linker_hints = optarg;
565 nomatch_str = optarg;
585 search_hints(NULL, NULL, NULL);
589 if (nomatch_str != NULL)
590 find_nomatch(nomatch_str);
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 devinfo_foreach_device_child(root, find_unmatched, (void *)0);