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' },
59 static char *linker_hints;
60 static char *nomatch_str;
61 static int unbound_flag;
62 static int verbose_flag;
65 static void *hints_end;
68 read_hints(const char *fn, size_t *len)
74 fd = open(fn, O_RDONLY);
78 err(1, "Can't open %s for reading", fn);
80 if (fstat(fd, &sb) != 0)
81 err(1, "Can't fstat %s\n", fn);
82 h = malloc(sb.st_size);
84 err(1, "not enough space to read hints file of %ju bytes", (uintmax_t)sb.st_size);
85 if (read(fd, h, sb.st_size) != sb.st_size)
86 err(1, "Can't read in %ju bytes from %s", (uintmax_t)sb.st_size, fn);
93 read_linker_hints(void)
96 char *modpath, *p, *q;
99 if (linker_hints == NULL) {
100 if (sysctlbyname("kern.module_path", NULL, &buflen, NULL, 0) < 0)
101 errx(1, "Can't find kernel module path.");
102 modpath = malloc(buflen);
104 err(1, "Can't get memory for modpath.");
105 if (sysctlbyname("kern.module_path", modpath, &buflen, NULL, 0) < 0)
106 errx(1, "Can't find kernel module path.");
108 while ((q = strsep(&p, ";")) != NULL) {
109 snprintf(fn, sizeof(fn), "%s/linker.hints", q);
110 hints = read_hints(fn, &len);
116 warnx("Can't read linker hints file.");
122 hints = read_hints(linker_hints, &len);
124 err(1, "Can't open %s for reading", fn);
127 if (*(int *)(intptr_t)hints != LINKER_HINTS_VERSION) {
128 warnx("Linker hints version %d doesn't match expected %d.",
129 *(int *)(intptr_t)hints, LINKER_HINTS_VERSION);
134 hints_end = (void *)((intptr_t)hints + (intptr_t)len);
143 p = (int *)roundup2((intptr_t)p, sizeof(int));
150 getstr(void **ptr, char *val)
154 int len = *(uint8_t *)c;
156 memcpy(val, c + 1, len);
163 pnpval_as_int(const char *val, const char *pnpinfo)
172 cp = strchr(val, ';');
175 strlcpy(key + 1, val, sizeof(key) - 1);
177 memcpy(key + 1, val, cp - val);
178 key[cp - val + 1] = '\0';
180 strlcat(key, "=", sizeof(key));
181 if (strncmp(key + 1, pnpinfo, strlen(key + 1)) == 0)
182 rv = strtol(pnpinfo + strlen(key + 1), NULL, 0);
184 cp = strstr(pnpinfo, key);
188 rv = strtol(cp + strlen(key), NULL, 0);
194 quoted_strcpy(char *dst, const char *src)
198 if (*src == '\'' || *src == '"')
200 while (*src && *src != q)
201 *dst++ = *src++; // XXX backtick quoting
207 pnpval_as_str(const char *val, const char *pnpinfo)
209 static char retval[256];
213 if (pnpinfo == NULL) {
218 cp = strchr(val, ';');
221 strlcpy(key + 1, val, sizeof(key) - 1);
223 memcpy(key + 1, val, cp - val);
224 key[cp - val + 1] = '\0';
226 strlcat(key, "=", sizeof(key));
227 if (strncmp(key + 1, pnpinfo, strlen(key + 1)) == 0)
228 quoted_strcpy(retval, pnpinfo + strlen(key + 1));
230 cp = strstr(pnpinfo, key);
232 strcpy(retval, "MISSING");
234 quoted_strcpy(retval, cp + strlen(key));
240 search_hints(const char *bus, const char *dev, const char *pnpinfo)
242 char val1[256], val2[256];
243 int ival, len, ents, i, notme, mask, bit, v, found;
245 char *lastmod = NULL, *cp, *s;
250 while (walker < hints_end) {
251 len = getint(&walker);
252 ival = getint(&walker);
260 printf("Version: if %s.%d kmod %s\n", val1, ival, val2);
267 lastmod = strdup(val2);
269 printf("Module %s in %s\n", val1, val2);
272 if (!dump_flag && !unbound_flag && lastmod && strcmp(lastmod, "kernel") == 0)
277 if (bus && strcmp(val1, bus) != 0)
280 printf("PNP info for bus %s format %s %d entries (%s)\n",
281 val1, val2, ents, lastmod);
282 for (i = 0; i < ents; i++) {
291 /* All integer fields */
299 printf("%#x:", ival);
302 if (bit >= 0 && ((1 << bit) & mask) == 0)
304 v = pnpval_as_int(cp + 2, pnpinfo);
332 printf("'%s':", val1);
337 s = pnpval_as_str(cp + 2, pnpinfo);
338 if (strcmp(s, val1) != 0)
341 /* Key override fields, required to be last in the string */
344 * This is imperfect and only does one key and will be redone
345 * to be more general for multiple keys. Currently, nothing
348 if (dump_flag) /* No per-row data stored */
350 if (cp[strlen(cp) - 1] == ';') /* Skip required ; at end */
351 cp[strlen(cp) - 1] = '\0'; /* in case it's not there */
352 if ((s = strstr(pnpinfo, cp + 2)) == NULL)
354 else if (s > pnpinfo && s[-1] != ' ')
358 fprintf(stderr, "Unknown field type %c\n:", *cp);
362 cp = strchr(cp, ';');
371 printf("%s: ", *dev ? dev : "unattached" );
372 printf("%s\n", lastmod);
380 printf("Unknown Type %d len %d\n", ival, len);
383 walker = (void *)(len - sizeof(int) + (intptr_t)walker);
385 if (unbound_flag && found == 0 && *pnpinfo) {
387 printf("------------------------- ");
388 printf("%s on %s pnpinfo %s", *dev ? dev : "unattached", bus, pnpinfo);
390 printf(" -------------------------");
397 find_unmatched(struct devinfo_dev *dev, void *arg)
399 struct devinfo_dev *parent;
403 if (!all_flag && dev->dd_name[0] != '\0')
405 if (!(dev->dd_flags & DF_ENABLED))
407 parent = devinfo_handle_to_device(dev->dd_parent);
408 bus = strdup(parent->dd_name);
409 p = bus + strlen(bus) - 1;
410 while (p >= bus && isdigit(*p))
414 printf("Searching %s %s bus at %s for pnpinfo %s\n",
415 dev->dd_name, bus, dev->dd_location, dev->dd_pnpinfo);
416 search_hints(bus, dev->dd_name, dev->dd_pnpinfo);
420 return (devinfo_foreach_device_child(dev, find_unmatched, arg));
424 find_nomatch(char *nomatch)
426 char *bus, *pnpinfo, *tmp;
429 * Find our bus name. It will include the unit number. We have to search
430 * backwards to avoid false positive for any PNP string that has ' on '
431 * in them, which would come earlier in the string. Like if there were
432 * an 'Old Bard' ethernet card made by 'Stratford on Avon Hardware' or
433 * something silly like that.
435 tmp = nomatch + strlen(nomatch) - 4;
436 while (tmp > nomatch && strncmp(tmp, " on ", 4) != 0)
439 errx(1, "No bus found in nomatch string: '%s'", nomatch);
442 tmp = bus + strlen(bus) - 1;
443 while (tmp > bus && isdigit(*tmp))
448 * Note: the NOMATCH events place both the bus location as well as the
449 * pnp info after the 'at' and we don't know where one stops and the
450 * other begins, so we pass the whole thing to our search routine.
454 if (strncmp(nomatch, " at ", 4) != 0)
455 errx(1, "Malformed NOMATCH string: '%s'", nomatch);
456 pnpinfo = nomatch + 4;
458 search_hints(bus, "", pnpinfo);
467 errx(1, "devmatch [-adv] [-p nomatch] [-h linker-hints]");
471 main(int argc, char **argv)
473 struct devinfo_dev *root;
476 while ((ch = getopt_long(argc, argv, "adh:p:uv",
477 longopts, NULL)) != -1) {
486 linker_hints = optarg;
489 nomatch_str = optarg;
509 search_hints(NULL, NULL, NULL);
513 if (nomatch_str != NULL)
514 find_nomatch(nomatch_str);
516 err(1, "devinfo_init");
517 if ((root = devinfo_handle_to_device(DEVINFO_ROOT_DEVICE)) == NULL)
518 errx(1, "can't find root device");
519 devinfo_foreach_device_child(root, find_unmatched, (void *)0);