]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/devmatch/devmatch.c
Implement --hints to read hints file directly
[FreeBSD/FreeBSD.git] / sbin / devmatch / devmatch.c
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 static int all_flag;
58 static int dump_flag;
59 static char *linker_hints;
60 static char *nomatch_str;
61 static int unbound_flag;
62 static int verbose_flag;
63
64 static void *hints;
65 static void *hints_end;
66
67 static void *
68 read_hints(const char *fn, size_t *len)
69 {
70         void *h;
71         int fd;
72         struct stat sb;
73
74         fd = open(fn, O_RDONLY);
75         if (fd < 0) {
76                 if (errno == ENOENT)
77                         return NULL;
78                 err(1, "Can't open %s for reading", fn);
79         }
80         if (fstat(fd, &sb) != 0)
81                 err(1, "Can't fstat %s\n", fn);
82         h = malloc(sb.st_size);
83         if (h == NULL)
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);
87         close(fd);
88         *len = sb.st_size;
89         return h;
90 }
91
92 static void
93 read_linker_hints(void)
94 {
95         char fn[MAXPATHLEN];
96         char *modpath, *p, *q;
97         size_t buflen, len;
98
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);
103                 if (modpath == NULL)
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.");
107                 p = modpath;
108                 while ((q = strsep(&p, ";")) != NULL) {
109                         snprintf(fn, sizeof(fn), "%s/linker.hints", q);
110                         hints = read_hints(fn, &len);
111                         if (hints == NULL)
112                                 continue;
113                         break;
114                 }
115                 if (q == NULL) {
116                         warnx("Can't read linker hints file.");
117                         free(hints);
118                         hints = NULL;
119                         return;
120                 }
121         } else {
122                 hints = read_hints(linker_hints, &len);
123                 if (hints == NULL)
124                         err(1, "Can't open %s for reading", fn);
125         }
126
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);
130                 free(hints);
131                 hints = NULL;
132         }
133         if (hints != NULL)
134                 hints_end = (void *)((intptr_t)hints + (intptr_t)len);
135 }
136
137 static int
138 getint(void **ptr)
139 {
140         int *p = *ptr;
141         int rv;
142
143         p = (int *)roundup2((intptr_t)p, sizeof(int));
144         rv = *p++;
145         *ptr = p;
146         return rv;
147 }
148
149 static void
150 getstr(void **ptr, char *val)
151 {
152         int *p = *ptr;
153         char *c = (char *)p;
154         int len = *(uint8_t *)c;
155
156         memcpy(val, c + 1, len);
157         val[len] = 0;
158         c += len + 1;
159         *ptr = (void *)c;
160 }
161
162 static int
163 pnpval_as_int(const char *val, const char *pnpinfo)
164 {
165         int rv;
166         char key[256];
167         char *cp;
168
169         if (pnpinfo == NULL)
170                 return -1;
171
172         cp = strchr(val, ';');
173         key[0] = ' ';
174         if (cp == NULL)
175                 strlcpy(key + 1, val, sizeof(key) - 1);
176         else {
177                 memcpy(key + 1, val, cp - val);
178                 key[cp - val + 1] = '\0';
179         }
180         strlcat(key, "=", sizeof(key));
181         if (strncmp(key + 1, pnpinfo, strlen(key + 1)) == 0)
182                 rv = strtol(pnpinfo + strlen(key + 1), NULL, 0);
183         else {
184                 cp = strstr(pnpinfo, key);
185                 if (cp == NULL)
186                         rv = -1;
187                 else
188                         rv = strtol(cp + strlen(key), NULL, 0);
189         }
190         return rv;
191 }
192
193 static void
194 quoted_strcpy(char *dst, const char *src)
195 {
196         char q = ' ';
197
198         if (*src == '\'' || *src == '"')
199                 q = *src++;
200         while (*src && *src != q)
201                 *dst++ = *src++; // XXX backtick quoting
202         *dst++ = '\0';
203         // XXX overflow
204 }
205
206 static char *
207 pnpval_as_str(const char *val, const char *pnpinfo)
208 {
209         static char retval[256];
210         char key[256];
211         char *cp;
212
213         if (pnpinfo == NULL) {
214                 *retval = '\0';
215                 return retval;
216         }
217
218         cp = strchr(val, ';');
219         key[0] = ' ';
220         if (cp == NULL)
221                 strlcpy(key + 1, val, sizeof(key) - 1);
222         else {
223                 memcpy(key + 1, val, cp - val);
224                 key[cp - val + 1] = '\0';
225         }
226         strlcat(key, "=", sizeof(key));
227         if (strncmp(key + 1, pnpinfo, strlen(key + 1)) == 0)
228                 quoted_strcpy(retval, pnpinfo + strlen(key + 1));
229         else {
230                 cp = strstr(pnpinfo, key);
231                 if (cp == NULL)
232                         strcpy(retval, "MISSING");
233                 else
234                         quoted_strcpy(retval, cp + strlen(key));
235         }
236         return retval;
237 }
238
239 static void
240 search_hints(const char *bus, const char *dev, const char *pnpinfo)
241 {
242         char val1[256], val2[256];
243         int ival, len, ents, i, notme, mask, bit, v, found;
244         void *ptr, *walker;
245         char *lastmod = NULL, *cp, *s;
246
247         walker = hints;
248         getint(&walker);
249         found = 0;
250         while (walker < hints_end) {
251                 len = getint(&walker);
252                 ival = getint(&walker);
253                 ptr = walker;
254                 switch (ival) {
255                 case MDT_VERSION:
256                         getstr(&ptr, val1);
257                         ival = getint(&ptr);
258                         getstr(&ptr, val2);
259                         if (dump_flag)
260                                 printf("Version: if %s.%d kmod %s\n", val1, ival, val2);
261                         break;
262                 case MDT_MODULE:
263                         getstr(&ptr, val1);
264                         getstr(&ptr, val2);
265                         if (lastmod)
266                                 free(lastmod);
267                         lastmod = strdup(val2);
268                         if (dump_flag)
269                                 printf("Module %s in %s\n", val1, val2);
270                         break;
271                 case MDT_PNP_INFO:
272                         if (!dump_flag && !unbound_flag && lastmod && strcmp(lastmod, "kernel") == 0)
273                                 break;
274                         getstr(&ptr, val1);
275                         getstr(&ptr, val2);
276                         ents = getint(&ptr);
277                         if (bus && strcmp(val1, bus) != 0)
278                                 break;
279                         if (dump_flag)
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++) {
283                                 if (dump_flag)
284                                         printf("   ");
285                                 cp = val2;
286                                 notme = 0;
287                                 mask = -1;
288                                 bit = -1;
289                                 do {
290                                         switch (*cp) {
291                                                 /* All integer fields */
292                                         case 'I':
293                                         case 'J':
294                                         case 'G':
295                                         case 'L':
296                                         case 'M':
297                                                 ival = getint(&ptr);
298                                                 if (dump_flag) {
299                                                         printf("%#x:", ival);
300                                                         break;
301                                                 }
302                                                 if (bit >= 0 && ((1 << bit) & mask) == 0)
303                                                         break;
304                                                 v = pnpval_as_int(cp + 2, pnpinfo);
305                                                 switch (*cp) {
306                                                 case 'J':
307                                                         if (ival == -1)
308                                                                 break;
309                                                         /*FALLTHROUGH*/
310                                                 case 'I':
311                                                         if (v != ival)
312                                                                 notme++;
313                                                         break;
314                                                 case 'G':
315                                                         if (v < ival)
316                                                                 notme++;
317                                                         break;
318                                                 case 'L':
319                                                         if (v > ival)
320                                                                 notme++;
321                                                         break;
322                                                 case 'M':
323                                                         mask = ival;
324                                                         break;
325                                                 }
326                                                 break;
327                                                 /* String fields */
328                                         case 'D':
329                                         case 'Z':
330                                                 getstr(&ptr, val1);
331                                                 if (dump_flag) {
332                                                         printf("'%s':", val1);
333                                                         break;
334                                                 }
335                                                 if (*cp == 'D')
336                                                         break;
337                                                 s = pnpval_as_str(cp + 2, pnpinfo);
338                                                 if (strcmp(s, val1) != 0)
339                                                         notme++;
340                                                 break;
341                                                 /* Key override fields, required to be last in the string */
342                                         case 'T':
343                                                 /*
344                                                  * This is imperfect and only does one key and will be redone
345                                                  * to be more general for multiple keys. Currently, nothing
346                                                  * does that.
347                                                  */
348                                                 if (dump_flag)                          /* No per-row data stored */
349                                                         break;
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)
353                                                         notme++;
354                                                 else if (s > pnpinfo && s[-1] != ' ')
355                                                         notme++;
356                                                 break;
357                                         default:
358                                                 fprintf(stderr, "Unknown field type %c\n:", *cp);
359                                                 break;
360                                         }
361                                         bit++;
362                                         cp = strchr(cp, ';');
363                                         if (cp)
364                                                 cp++;
365                                 } while (cp && *cp);
366                                 if (dump_flag)
367                                         printf("\n");
368                                 else if (!notme) {
369                                         if (!unbound_flag) {
370                                                 if (all_flag)
371                                                         printf("%s: ", *dev ? dev : "unattached" );
372                                                 printf("%s\n", lastmod);
373                                         }
374                                         found++;
375                                 }
376                         }
377                         break;
378                 default:
379                         if (dump_flag)
380                                 printf("Unknown Type %d len %d\n", ival, len);
381                         break;
382                 }
383                 walker = (void *)(len - sizeof(int) + (intptr_t)walker);
384         }
385         if (unbound_flag && found == 0 && *pnpinfo) {
386                 if (verbose_flag)
387                         printf("------------------------- ");
388                 printf("%s on %s pnpinfo %s", *dev ? dev : "unattached", bus, pnpinfo);
389                 if (verbose_flag)
390                         printf(" -------------------------");
391                 printf("\n");
392         }
393         free(lastmod);
394 }
395
396 static int
397 find_unmatched(struct devinfo_dev *dev, void *arg)
398 {
399         struct devinfo_dev *parent;
400         char *bus, *p;
401
402         do {
403                 if (!all_flag && dev->dd_name[0] != '\0')
404                         break;
405                 if (!(dev->dd_flags & DF_ENABLED))
406                         break;
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))
411                         p--;
412                 *++p = '\0';
413                 if (verbose_flag)
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);
417                 free(bus);
418         } while (0);
419
420         return (devinfo_foreach_device_child(dev, find_unmatched, arg));
421 }
422
423 static void
424 find_nomatch(char *nomatch)
425 {
426         char *bus, *pnpinfo, *tmp;
427
428         /*
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.
434          */
435         tmp = nomatch + strlen(nomatch) - 4;
436         while (tmp > nomatch && strncmp(tmp, " on ", 4) != 0)
437                 tmp--;
438         if (tmp == nomatch)
439                 errx(1, "No bus found in nomatch string: '%s'", nomatch);
440         bus = tmp + 4;
441         *tmp = '\0';
442         tmp = bus + strlen(bus) - 1;
443         while (tmp > bus && isdigit(*tmp))
444                 tmp--;
445         *++tmp = '\0';
446
447         /*
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.
451          */
452         if (*nomatch == '?')
453                 nomatch++;
454         if (strncmp(nomatch, " at ", 4) != 0)
455                 errx(1, "Malformed NOMATCH string: '%s'", nomatch);
456         pnpinfo = nomatch + 4;
457
458         search_hints(bus, "", pnpinfo);
459
460         exit(0);
461 }
462
463 static void
464 usage(void)
465 {
466
467         errx(1, "devmatch [-adv] [-p nomatch] [-h linker-hints]");
468 }
469
470 int
471 main(int argc, char **argv)
472 {
473         struct devinfo_dev *root;
474         int ch;
475
476         while ((ch = getopt_long(argc, argv, "adh:p:uv",
477                     longopts, NULL)) != -1) {
478                 switch (ch) {
479                 case 'a':
480                         all_flag++;
481                         break;
482                 case 'd':
483                         dump_flag++;
484                         break;
485                 case 'h':
486                         linker_hints = optarg;
487                         break;
488                 case 'p':
489                         nomatch_str = optarg;
490                         break;
491                 case 'u':
492                         unbound_flag++;
493                         break;
494                 case 'v':
495                         verbose_flag++;
496                         break;
497                 default:
498                         usage();
499                 }
500         }
501         argc -= optind;
502         argv += optind;
503
504         if (argc >= 1)
505                 usage();
506
507         read_linker_hints();
508         if (dump_flag) {
509                 search_hints(NULL, NULL, NULL);
510                 exit(0);
511         }
512
513         if (nomatch_str != NULL)
514                 find_nomatch(nomatch_str);
515         if (devinfo_init())
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);
520         devinfo_free();
521 }