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 { "unbound", no_argument, NULL, 'u' }, 51 { "verbose", no_argument, NULL, 'v' }, 52 { NULL, 0, NULL, 0 } 53 }; 54 55 static int all_flag; 56 static int dump_flag; 57 static int unbound_flag; 58 static int verbose_flag; 59 60 static void *hints; 61 static void *hints_end; 62 63 static void 64 read_linker_hints(void) 65 { 66 char fn[MAXPATHLEN]; 67 struct stat sb; 68 char *modpath, *p, *q; 69 size_t buflen; 70 int fd; 71 72 if (sysctlbyname("kern.module_path", NULL, &buflen, NULL, 0) < 0) 73 errx(1, "Can't find kernel module path."); 74 modpath = malloc(buflen); 75 if (modpath == NULL) 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."); 79 p = modpath; 80 while ((q = strsep(&p, ";")) != NULL) { 81 snprintf(fn, sizeof(fn), "%s/linker.hints", q); 82 fd = open(fn, O_RDONLY); 83 if (fd < 0) { 84 if (errno == ENOENT) 85 continue; 86 err(1, "Can't open %s for reading", fn); 87 } 88 if (fstat(fd, &sb) != 0) 89 err(1, "Can't fstat %s\n", fn); 90 hints = malloc(sb.st_size); 91 if (hints == NULL) 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); 95 close(fd); 96 break; 97 } 98 if (q == NULL) { 99 warnx("Can't read linker hints file."); 100 free(hints); 101 hints = NULL; 102 return; 103 } 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); 107 free(hints); 108 hints = NULL; 109 } 110 if (hints != NULL) 111 hints_end = (void *)((intptr_t)hints + (intptr_t)sb.st_size); 112 } 113 114 static int 115 getint(void **ptr) 116 { 117 int *p = *ptr; 118 int rv; 119 120 p = (int *)roundup2((intptr_t)p, sizeof(int)); 121 rv = *p++; 122 *ptr = p; 123 return rv; 124 } 125 126 static void 127 getstr(void **ptr, char *val) 128 { 129 int *p = *ptr; 130 char *c = (char *)p; 131 int len = *(uint8_t *)c; 132 133 memcpy(val, c + 1, len); 134 val[len] = 0; 135 c += len + 1; 136 *ptr = (void *)c; 137 } 138 139 static int 140 pnpval_as_int(const char *val, const char *pnpinfo) 141 { 142 int rv; 143 char key[256]; 144 char *cp; 145 146 if (pnpinfo == NULL) 147 return -1; 148 149 cp = strchr(val, ';'); 150 key[0] = ' '; 151 if (cp == NULL) 152 strlcpy(key + 1, val, sizeof(key) - 1); 153 else { 154 memcpy(key + 1, val, cp - val); 155 key[cp - val + 1] = '\0'; 156 } 157 strlcat(key, "=", sizeof(key)); 158 if (strncmp(key + 1, pnpinfo, strlen(key + 1)) == 0) 159 rv = strtol(pnpinfo + strlen(key + 1), NULL, 0); 160 else { 161 cp = strstr(pnpinfo, key); 162 if (cp == NULL) 163 rv = -1; 164 else 165 rv = strtol(cp + strlen(key), NULL, 0); 166 } 167 return rv; 168 } 169 170 static void 171 quoted_strcpy(char *dst, const char *src) 172 { 173 char q = ' '; 174 175 if (*src == '\'' || *src == '"') 176 q = *src++; 177 while (*src && *src != q) 178 *dst++ = *src++; // XXX backtick quoting 179 *dst++ = '\0'; 180 // XXX overflow 181 } 182 183 static char * 184 pnpval_as_str(const char *val, const char *pnpinfo) 185 { 186 static char retval[256]; 187 char key[256]; 188 char *cp; 189 190 if (pnpinfo == NULL) { 191 *retval = '\0'; 192 return retval; 193 } 194 195 cp = strchr(val, ';'); 196 key[0] = ' '; 197 if (cp == NULL) 198 strlcpy(key + 1, val, sizeof(key) - 1); 199 else { 200 memcpy(key + 1, val, cp - val); 201 key[cp - val + 1] = '\0'; 202 } 203 strlcat(key, "=", sizeof(key)); 204 if (strncmp(key + 1, pnpinfo, strlen(key + 1)) == 0) 205 quoted_strcpy(retval, pnpinfo + strlen(key + 1)); 206 else { 207 cp = strstr(pnpinfo, key); 208 if (cp == NULL) 209 strcpy(retval, "MISSING"); 210 else 211 quoted_strcpy(retval, cp + strlen(key)); 212 } 213 return retval; 214 } 215 216 static void 217 search_hints(const char *bus, const char *dev, const char *pnpinfo) 218 { 219 char val1[256], val2[256]; 220 int ival, len, ents, i, notme, mask, bit, v, found; 221 void *ptr, *walker; 222 char *lastmod = NULL, *cp, *s; 223 224 walker = hints; 225 getint(&walker); 226 found = 0; 227 while (walker < hints_end) { 228 len = getint(&walker); 229 ival = getint(&walker); 230 ptr = walker; 231 switch (ival) { 232 case MDT_VERSION: 233 getstr(&ptr, val1); 234 ival = getint(&ptr); 235 getstr(&ptr, val2); 236 if (dump_flag) 237 printf("Version: if %s.%d kmod %s\n", val1, ival, val2); 238 break; 239 case MDT_MODULE: 240 getstr(&ptr, val1); 241 getstr(&ptr, val2); 242 if (lastmod) 243 free(lastmod); 244 lastmod = strdup(val2); 245 if (dump_flag) 246 printf("Module %s in %s\n", val1, val2); 247 break; 248 case MDT_PNP_INFO: 249 if (!dump_flag && !unbound_flag && lastmod && strcmp(lastmod, "kernel") == 0) 250 break; 251 getstr(&ptr, val1); 252 getstr(&ptr, val2); 253 ents = getint(&ptr); 254 if (bus && strcmp(val1, bus) != 0) 255 break; 256 if (dump_flag) 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++) { 260 if (dump_flag) 261 printf(" "); 262 cp = val2; 263 notme = 0; 264 mask = -1; 265 bit = -1; 266 do { 267 switch (*cp) { 268 case 'I': 269 case 'J': 270 case 'G': 271 case 'L': 272 case 'M': 273 ival = getint(&ptr); 274 if (dump_flag) { 275 printf("%#x:", ival); 276 break; 277 } 278 if (bit >= 0 && ((1 << bit) & mask) == 0) 279 break; 280 v = pnpval_as_int(cp + 2, pnpinfo); 281 switch (*cp) { 282 case 'J': 283 if (ival == -1) 284 break; 285 /*FALLTHROUGH*/ 286 case 'I': 287 if (v != ival && ival != 0) 288 notme++; 289 break; 290 case 'G': 291 if (v < ival) 292 notme++; 293 break; 294 case 'L': 295 if (v > ival) 296 notme++; 297 break; 298 case 'M': 299 mask = ival; 300 break; 301 } 302 break; 303 case 'D': 304 case 'Z': 305 getstr(&ptr, val1); 306 if (dump_flag) { 307 printf("'%s':", val1); 308 break; 309 } 310 if (*cp == 'D') 311 break; 312 s = pnpval_as_str(cp + 2, pnpinfo); 313 if (strcmp(s, val1) != 0) 314 notme++; 315 break; 316 default: 317 break; 318 } 319 bit++; 320 cp = strchr(cp, ';'); 321 if (cp) 322 cp++; 323 } while (cp && *cp); 324 if (dump_flag) 325 printf("\n"); 326 else if (!notme) { 327 if (!unbound_flag) { 328 if (all_flag) 329 printf("%s: ", *dev ? dev : "unattached" ); 330 printf("%s\n", lastmod); 331 } 332 found++; 333 } 334 } 335 break; 336 default: 337 if (dump_flag) 338 printf("Unknown Type %d len %d\n", ival, len); 339 break; 340 } 341 walker = (void *)(len - sizeof(int) + (intptr_t)walker); 342 } 343 if (unbound_flag && found == 0 && *pnpinfo) { 344 if (verbose_flag) 345 printf("------------------------- "); 346 printf("%s on %s pnpinfo %s", *dev ? dev : "unattached", bus, pnpinfo); 347 if (verbose_flag) 348 printf(" -------------------------"); 349 printf("\n"); 350 } 351 free(lastmod); 352 } 353 354 static int 355 find_unmatched(struct devinfo_dev *dev, void *arg) 356 { 357 struct devinfo_dev *parent; 358 char *bus, *p; 359 360 do { 361 if (!all_flag && dev->dd_name[0] != '\0') 362 break; 363 if (!(dev->dd_flags & DF_ENABLED)) 364 break; 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)) 369 p--; 370 *++p = '\0'; 371 if (verbose_flag) 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); 375 free(bus); 376 } while (0); 377 378 return (devinfo_foreach_device_child(dev, find_unmatched, arg)); 379 } 380 381 static void 382 usage(void) 383 { 384 385 errx(1, "devmatch [-adv]"); 386 } 387 388 int 389 main(int argc, char **argv) 390 { 391 struct devinfo_dev *root; 392 int ch; 393 394 while ((ch = getopt_long(argc, argv, "aduv", 395 longopts, NULL)) != -1) { 396 switch (ch) { 397 case 'a': 398 all_flag++; 399 break; 400 case 'd': 401 dump_flag++; 402 break; 403 case 'u': 404 unbound_flag++; 405 break; 406 case 'v': 407 verbose_flag++; 408 break; 409 default: 410 usage(); 411 } 412 } 413 argc -= optind; 414 argv += optind; 415 416 if (argc >= 1) 417 usage(); 418 419 read_linker_hints(); 420 if (dump_flag) { 421 search_hints(NULL, NULL, NULL); 422 exit(0); 423 } 424 425 if (devinfo_init()) 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); 430 devinfo_free(); 431 } 432