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 { "unbound", no_argument, NULL, 'u' },
51 { "verbose", no_argument, NULL, 'v' },
57 static int unbound_flag;
58 static int verbose_flag;
61 static void *hints_end;
64 read_linker_hints(void)
68 char *modpath, *p, *q;
72 if (sysctlbyname("kern.module_path", NULL, &buflen, NULL, 0) < 0)
73 errx(1, "Can't find kernel module path.");
74 modpath = malloc(buflen);
76 err(1, "Can't get memory for modpath.");
77 if (sysctlbyname("kern.module_path", modpath, &buflen, NULL, 0) < 0)
78 errx(1, "Can't find kernel module path.");
80 while ((q = strsep(&p, ";")) != NULL) {
81 snprintf(fn, sizeof(fn), "%s/linker.hints", q);
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 hints = 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, hints, sb.st_size) != sb.st_size)
94 err(1, "Can't read in %ju bytes from %s", (uintmax_t)sb.st_size, fn);
99 warnx("Can't read linker hints file.");
104 if (*(int *)(intptr_t)hints != LINKER_HINTS_VERSION) {
105 warnx("Linker hints version %d doesn't match expected %d.",
106 *(int *)(intptr_t)hints, LINKER_HINTS_VERSION);
111 hints_end = (void *)((intptr_t)hints + (intptr_t)sb.st_size);
120 p = (int *)roundup2((intptr_t)p, sizeof(int));
127 getstr(void **ptr, char *val)
131 int len = *(uint8_t *)c;
133 memcpy(val, c + 1, len);
140 pnpval_as_int(const char *val, const char *pnpinfo)
149 cp = strchr(val, ';');
152 strlcpy(key + 1, val, sizeof(key) - 1);
154 memcpy(key + 1, val, cp - val);
155 key[cp - val + 1] = '\0';
157 strlcat(key, "=", sizeof(key));
158 if (strncmp(key + 1, pnpinfo, strlen(key + 1)) == 0)
159 rv = strtol(pnpinfo + strlen(key + 1), NULL, 0);
161 cp = strstr(pnpinfo, key);
165 rv = strtol(cp + strlen(key), NULL, 0);
171 quoted_strcpy(char *dst, const char *src)
175 if (*src == '\'' || *src == '"')
177 while (*src && *src != q)
178 *dst++ = *src++; // XXX backtick quoting
184 pnpval_as_str(const char *val, const char *pnpinfo)
186 static char retval[256];
190 if (pnpinfo == NULL) {
195 cp = strchr(val, ';');
198 strlcpy(key + 1, val, sizeof(key) - 1);
200 memcpy(key + 1, val, cp - val);
201 key[cp - val + 1] = '\0';
203 strlcat(key, "=", sizeof(key));
204 if (strncmp(key + 1, pnpinfo, strlen(key + 1)) == 0)
205 quoted_strcpy(retval, pnpinfo + strlen(key + 1));
207 cp = strstr(pnpinfo, key);
209 strcpy(retval, "MISSING");
211 quoted_strcpy(retval, cp + strlen(key));
217 search_hints(const char *bus, const char *dev, const char *pnpinfo)
219 char val1[256], val2[256];
220 int ival, len, ents, i, notme, mask, bit, v, found;
222 char *lastmod = NULL, *cp, *s;
227 while (walker < hints_end) {
228 len = getint(&walker);
229 ival = getint(&walker);
237 printf("Version: if %s.%d kmod %s\n", val1, ival, val2);
244 lastmod = strdup(val2);
246 printf("Module %s in %s\n", val1, val2);
249 if (!dump_flag && !unbound_flag && lastmod && strcmp(lastmod, "kernel") == 0)
254 if (bus && strcmp(val1, bus) != 0)
257 printf("PNP info for bus %s format %s %d entries (%s)\n",
258 val1, val2, ents, lastmod);
259 for (i = 0; i < ents; i++) {
275 printf("%#x:", ival);
278 if (bit >= 0 && ((1 << bit) & mask) == 0)
280 v = pnpval_as_int(cp + 2, pnpinfo);
287 if (v != ival && ival != 0)
307 printf("'%s':", val1);
312 s = pnpval_as_str(cp + 2, pnpinfo);
313 if (strcmp(s, val1) != 0)
320 cp = strchr(cp, ';');
329 printf("%s: ", *dev ? dev : "unattached" );
330 printf("%s\n", lastmod);
338 printf("Unknown Type %d len %d\n", ival, len);
341 walker = (void *)(len - sizeof(int) + (intptr_t)walker);
343 if (unbound_flag && found == 0 && *pnpinfo) {
345 printf("------------------------- ");
346 printf("%s on %s pnpinfo %s", *dev ? dev : "unattached", bus, pnpinfo);
348 printf(" -------------------------");
355 find_unmatched(struct devinfo_dev *dev, void *arg)
357 struct devinfo_dev *parent;
361 if (!all_flag && dev->dd_name[0] != '\0')
363 if (!(dev->dd_flags & DF_ENABLED))
365 parent = devinfo_handle_to_device(dev->dd_parent);
366 bus = strdup(parent->dd_name);
367 p = bus + strlen(bus) - 1;
368 while (p >= bus && isdigit(*p))
372 printf("Searching %s %s bus at %s for pnpinfo %s\n",
373 dev->dd_name, bus, dev->dd_location, dev->dd_pnpinfo);
374 search_hints(bus, dev->dd_name, dev->dd_pnpinfo);
378 return (devinfo_foreach_device_child(dev, find_unmatched, arg));
385 errx(1, "devmatch [-adv]");
389 main(int argc, char **argv)
391 struct devinfo_dev *root;
394 while ((ch = getopt_long(argc, argv, "aduv",
395 longopts, NULL)) != -1) {
421 search_hints(NULL, NULL, NULL);
426 err(1, "devinfo_init");
427 if ((root = devinfo_handle_to_device(DEVINFO_ROOT_DEVICE)) == NULL)
428 errx(1, "can't find root device");
429 devinfo_foreach_device_child(root, find_unmatched, (void *)0);