1 /*- 2 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org> 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 /* 31 * file/module function dispatcher, support, etc. 32 */ 33 34 #include <stand.h> 35 #include <string.h> 36 #include <sys/param.h> 37 #include <sys/linker.h> 38 #include <sys/module.h> 39 #include <sys/queue.h> 40 #include <sys/stdint.h> 41 #include <sys/font.h> 42 #include <gfx_fb.h> 43 44 #if defined(LOADER_FDT_SUPPORT) 45 #include <fdt_platform.h> 46 #endif 47 48 #include "bootstrap.h" 49 50 #define MDIR_REMOVED 0x0001 51 #define MDIR_NOHINTS 0x0002 52 53 struct moduledir { 54 char *d_path; /* path of modules directory */ 55 u_char *d_hints; /* content of linker.hints file */ 56 int d_hintsz; /* size of hints data */ 57 int d_flags; 58 STAILQ_ENTRY(moduledir) d_link; 59 }; 60 61 static int file_load(char *filename, vm_offset_t dest, struct preloaded_file **result); 62 static int file_load_dependencies(struct preloaded_file *base_mod); 63 static char * file_search(const char *name, char **extlist); 64 static struct kernel_module * file_findmodule(struct preloaded_file *fp, char *modname, struct mod_depend *verinfo); 65 static int file_havepath(const char *name); 66 static char *mod_searchmodule(char *name, struct mod_depend *verinfo); 67 static char * mod_searchmodule_pnpinfo(const char *bus, const char *pnpinfo); 68 static void file_insert_tail(struct preloaded_file *mp); 69 static void file_remove(struct preloaded_file *fp); 70 struct file_metadata* metadata_next(struct file_metadata *base_mp, int type); 71 static void moduledir_readhints(struct moduledir *mdp); 72 static void moduledir_rebuild(void); 73 74 /* load address should be tweaked by first module loaded (kernel) */ 75 static vm_offset_t loadaddr = 0; 76 77 #if defined(LOADER_FDT_SUPPORT) 78 static const char *default_searchpath = 79 "/boot/kernel;/boot/modules;/boot/dtb"; 80 #else 81 static const char *default_searchpath = "/boot/kernel;/boot/modules"; 82 #endif 83 84 static STAILQ_HEAD(, moduledir) moduledir_list = 85 STAILQ_HEAD_INITIALIZER(moduledir_list); 86 87 struct preloaded_file *preloaded_files = NULL; 88 89 static char *kld_ext_list[] = { 90 ".ko", 91 "", 92 ".debug", 93 NULL 94 }; 95 96 /* 97 * load an object, either a disk file or code module. 98 * 99 * To load a file, the syntax is: 100 * 101 * load -t <type> <path> 102 * 103 * code modules are loaded as: 104 * 105 * load <path> <options> 106 */ 107 108 COMMAND_SET(load, "load", "load a kernel or module", command_load); 109 110 static int 111 command_load(int argc, char *argv[]) 112 { 113 struct preloaded_file *fp; 114 char *typestr; 115 #ifdef LOADER_VERIEXEC 116 char *prefix; 117 #endif 118 char *skip; 119 int dflag, dofile, dokld, ch, error; 120 121 dflag = dokld = dofile = 0; 122 optind = 1; 123 optreset = 1; 124 typestr = NULL; 125 if (argc == 1) { 126 command_errmsg = "no filename specified"; 127 return (CMD_CRIT); 128 } 129 #ifdef LOADER_VERIEXEC 130 prefix = NULL; 131 #endif 132 skip = NULL; 133 while ((ch = getopt(argc, argv, "dkp:s:t:")) != -1) { 134 switch(ch) { 135 case 'd': 136 dflag++; 137 break; 138 case 'k': 139 dokld = 1; 140 break; 141 #ifdef LOADER_VERIEXEC 142 case 'p': 143 prefix = optarg; 144 break; 145 #endif 146 case 's': 147 skip = optarg; 148 break; 149 case 't': 150 typestr = optarg; 151 dofile = 1; 152 break; 153 case '?': 154 default: 155 /* getopt has already reported an error */ 156 return (CMD_OK); 157 } 158 } 159 argv += (optind - 1); 160 argc -= (optind - 1); 161 162 /* 163 * Request to load a raw file? 164 */ 165 if (dofile) { 166 if ((argc != 2) || (typestr == NULL) || (*typestr == 0)) { 167 command_errmsg = "invalid load type"; 168 return (CMD_CRIT); 169 } 170 171 #ifdef LOADER_VERIEXEC 172 if (strncmp(typestr, "manifest", 8) == 0) { 173 if (dflag > 0) 174 ve_debug_set(dflag); 175 return (load_manifest(argv[1], prefix, skip, NULL)); 176 } 177 #ifdef LOADER_VERIEXEC_PASS_MANIFEST 178 if (strncmp(typestr, "pass_manifest", 13) == 0) { 179 if (dflag > 0) 180 ve_debug_set(dflag); 181 return (pass_manifest(argv[1], prefix)); 182 } 183 #endif 184 #endif 185 186 fp = file_findfile(argv[1], typestr); 187 if (fp) { 188 snprintf(command_errbuf, sizeof(command_errbuf), 189 "warning: file '%s' already loaded", argv[1]); 190 return (CMD_WARN); 191 } 192 193 if (file_loadraw(argv[1], typestr, 1) != NULL) 194 return (CMD_OK); 195 196 /* Failing to load mfs_root is never going to end well! */ 197 if (strcmp("mfs_root", typestr) == 0) 198 return (CMD_FATAL); 199 200 return (CMD_ERROR); 201 } 202 /* 203 * Do we have explicit KLD load ? 204 */ 205 if (dokld || file_havepath(argv[1])) { 206 error = mod_loadkld(argv[1], argc - 2, argv + 2); 207 if (error == EEXIST) { 208 snprintf(command_errbuf, sizeof(command_errbuf), 209 "warning: KLD '%s' already loaded", argv[1]); 210 return (CMD_WARN); 211 } 212 213 return (error == 0 ? CMD_OK : CMD_CRIT); 214 } 215 /* 216 * Looks like a request for a module. 217 */ 218 error = mod_load(argv[1], NULL, argc - 2, argv + 2); 219 if (error == EEXIST) { 220 snprintf(command_errbuf, sizeof(command_errbuf), 221 "warning: module '%s' already loaded", argv[1]); 222 return (CMD_WARN); 223 } 224 225 return (error == 0 ? CMD_OK : CMD_CRIT); 226 } 227 228 #ifdef LOADER_GELI_SUPPORT 229 COMMAND_SET(load_geli, "load_geli", "load a geli key", command_load_geli); 230 231 static int 232 command_load_geli(int argc, char *argv[]) 233 { 234 char typestr[80]; 235 char *cp; 236 int ch, num; 237 238 if (argc < 3) { 239 command_errmsg = "usage is [-n key#] <prov> <file>"; 240 return(CMD_ERROR); 241 } 242 243 num = 0; 244 optind = 1; 245 optreset = 1; 246 while ((ch = getopt(argc, argv, "n:")) != -1) { 247 switch(ch) { 248 case 'n': 249 num = strtol(optarg, &cp, 0); 250 if (cp == optarg) { 251 snprintf(command_errbuf, sizeof(command_errbuf), 252 "bad key index '%s'", optarg); 253 return(CMD_ERROR); 254 } 255 break; 256 case '?': 257 default: 258 /* getopt has already reported an error */ 259 return(CMD_OK); 260 } 261 } 262 argv += (optind - 1); 263 argc -= (optind - 1); 264 sprintf(typestr, "%s:geli_keyfile%d", argv[1], num); 265 return (file_loadraw(argv[2], typestr, 1) ? CMD_OK : CMD_ERROR); 266 } 267 #endif 268 269 void 270 unload(void) 271 { 272 struct preloaded_file *fp; 273 274 while (preloaded_files != NULL) { 275 fp = preloaded_files; 276 preloaded_files = preloaded_files->f_next; 277 file_discard(fp); 278 } 279 loadaddr = 0; 280 unsetenv("kernelname"); 281 /* Reset tg_kernel_supported to allow next load to check it again. */ 282 gfx_state.tg_kernel_supported = false; 283 } 284 285 COMMAND_SET(unload, "unload", "unload all modules", command_unload); 286 287 static int 288 command_unload(int argc, char *argv[]) 289 { 290 unload(); 291 return(CMD_OK); 292 } 293 294 COMMAND_SET(lsmod, "lsmod", "list loaded modules", command_lsmod); 295 296 static int 297 command_lsmod(int argc, char *argv[]) 298 { 299 struct preloaded_file *fp; 300 struct kernel_module *mp; 301 struct file_metadata *md; 302 char lbuf[80]; 303 int ch, verbose, ret = 0; 304 305 verbose = 0; 306 optind = 1; 307 optreset = 1; 308 while ((ch = getopt(argc, argv, "v")) != -1) { 309 switch(ch) { 310 case 'v': 311 verbose = 1; 312 break; 313 case '?': 314 default: 315 /* getopt has already reported an error */ 316 return(CMD_OK); 317 } 318 } 319 320 pager_open(); 321 for (fp = preloaded_files; fp; fp = fp->f_next) { 322 snprintf(lbuf, sizeof(lbuf), " %p: ", (void *) fp->f_addr); 323 pager_output(lbuf); 324 pager_output(fp->f_name); 325 snprintf(lbuf, sizeof(lbuf), " (%s, 0x%lx)\n", fp->f_type, 326 (long)fp->f_size); 327 if (pager_output(lbuf)) 328 break; 329 if (fp->f_args != NULL) { 330 pager_output(" args: "); 331 pager_output(fp->f_args); 332 if (pager_output("\n")) 333 break; 334 } 335 if (fp->f_modules) { 336 pager_output(" modules: "); 337 for (mp = fp->f_modules; mp; mp = mp->m_next) { 338 snprintf(lbuf, sizeof(lbuf), "%s.%d ", mp->m_name, 339 mp->m_version); 340 pager_output(lbuf); 341 } 342 if (pager_output("\n")) 343 break; 344 } 345 if (verbose) { 346 /* XXX could add some formatting smarts here to display some better */ 347 for (md = fp->f_metadata; md != NULL; md = md->md_next) { 348 snprintf(lbuf, sizeof(lbuf), " 0x%04x, 0x%lx\n", 349 md->md_type, (long) md->md_size); 350 if (pager_output(lbuf)) 351 break; 352 } 353 } 354 if (ret) 355 break; 356 } 357 pager_close(); 358 return(CMD_OK); 359 } 360 361 COMMAND_SET(pnpmatch, "pnpmatch", "list matched modules based on pnpinfo", command_pnpmatch); 362 363 static int pnp_dump_flag = 0; 364 static int pnp_unbound_flag = 0; 365 static int pnp_verbose_flag = 0; 366 367 static int 368 command_pnpmatch(int argc, char *argv[]) 369 { 370 char *module; 371 int ch; 372 373 pnp_verbose_flag = 0; 374 pnp_dump_flag = 0; 375 optind = 1; 376 optreset = 1; 377 while ((ch = getopt(argc, argv, "vd")) != -1) { 378 switch(ch) { 379 case 'v': 380 pnp_verbose_flag = 1; 381 break; 382 case 'd': 383 pnp_dump_flag = 1; 384 break; 385 case '?': 386 default: 387 /* getopt has already reported an error */ 388 return(CMD_OK); 389 } 390 } 391 argv += optind; 392 argc -= optind; 393 394 if (argc != 2) { 395 command_errmsg = "Usage: pnpmatch <busname> compat=<compatdata>"; 396 return (CMD_CRIT); 397 } 398 399 module = mod_searchmodule_pnpinfo(argv[0], argv[1]); 400 if (module) 401 printf("Matched module: %s\n", module); 402 else 403 printf("No module matches %s on bus %s\n", argv[1], argv[0]); 404 405 return (CMD_OK); 406 } 407 408 COMMAND_SET(pnpload, "pnpload", "load matched modules based on pnpinfo", command_pnpload); 409 410 static int 411 command_pnpload(int argc, char *argv[]) 412 { 413 char *module; 414 int ch, error; 415 416 pnp_verbose_flag = 0; 417 pnp_dump_flag = 0; 418 optind = 1; 419 optreset = 1; 420 while ((ch = getopt(argc, argv, "vd")) != -1) { 421 switch(ch) { 422 case 'v': 423 pnp_verbose_flag = 1; 424 break; 425 case 'd': 426 pnp_dump_flag = 1; 427 break; 428 case '?': 429 default: 430 /* getopt has already reported an error */ 431 return(CMD_OK); 432 } 433 } 434 argv += optind; 435 argc -= optind; 436 437 if (argc != 2) { 438 command_errmsg = "Usage: pnpload <busname> compat=<compatdata>"; 439 return (CMD_ERROR); 440 } 441 442 module = mod_searchmodule_pnpinfo(argv[0], argv[1]); 443 444 error = mod_load(module, NULL, 0, NULL); 445 if (error == EEXIST) { 446 snprintf(command_errbuf, sizeof(command_errbuf), 447 "warning: module '%s' already loaded", argv[1]); 448 return (CMD_WARN); 449 } 450 451 return (error == 0 ? CMD_OK : CMD_CRIT); 452 } 453 454 #if defined(LOADER_FDT_SUPPORT) 455 static void 456 pnpautoload_fdt_bus(const char *busname) { 457 const char *pnpstring; 458 const char *compatstr; 459 char *pnpinfo = NULL; 460 char *module = NULL; 461 int tag = 0, len, pnplen; 462 int error; 463 464 while (1) { 465 pnpstring = fdt_devmatch_next(&tag, &len); 466 if (pnpstring == NULL) 467 return; 468 469 compatstr = pnpstring; 470 for (pnplen = 0; pnplen != len; compatstr = pnpstring + pnplen) { 471 pnplen += strlen(compatstr) + 1; 472 asprintf(&pnpinfo, "compat=%s", compatstr); 473 474 module = mod_searchmodule_pnpinfo(busname, pnpinfo); 475 if (module) { 476 error = mod_loadkld(module, 0, NULL); 477 if (error) 478 printf("Cannot load module %s\n", module); 479 break; 480 } 481 } 482 free(pnpinfo); 483 free(module); 484 } 485 } 486 #endif 487 488 struct pnp_bus { 489 const char *name; 490 void (*load)(const char *busname); 491 }; 492 493 struct pnp_bus pnp_buses[] = { 494 #if defined(LOADER_FDT_SUPPORT) 495 {"simplebus", pnpautoload_fdt_bus}, 496 {"ofwbus", pnpautoload_fdt_bus}, 497 {"iicbus", pnpautoload_fdt_bus}, 498 {"spibus", pnpautoload_fdt_bus}, 499 #endif 500 }; 501 502 COMMAND_SET(pnpautoload, "pnpautoload", "auto load modules based on pnpinfo", command_pnpautoload); 503 504 static int 505 command_pnpautoload(int argc, char *argv[]) 506 { 507 int i; 508 int verbose; 509 int ch, match; 510 511 pnp_verbose_flag = 0; 512 pnp_dump_flag = 0; 513 verbose = 0; 514 optind = 1; 515 optreset = 1; 516 match = 0; 517 while ((ch = getopt(argc, argv, "v")) != -1) { 518 switch(ch) { 519 case 'v': 520 verbose = 1; 521 break; 522 case '?': 523 default: 524 /* getopt has already reported an error */ 525 return(CMD_OK); 526 } 527 } 528 argv += (optind - 1); 529 argc -= (optind - 1); 530 531 if (argc > 2) 532 return (CMD_ERROR); 533 534 for (i = 0; i < nitems(pnp_buses); i++) { 535 if (argc == 2 && strcmp(argv[1], pnp_buses[i].name) != 0) { 536 if (verbose) 537 printf("Skipping bus %s\n", pnp_buses[i].name); 538 continue; 539 } 540 if (verbose) 541 printf("Autoloading modules for %s\n", pnp_buses[i].name); 542 pnp_buses[i].load(pnp_buses[i].name); 543 match = 1; 544 } 545 if (match == 0) 546 printf("Unsupported bus %s\n", argv[1]); 547 548 return (CMD_OK); 549 } 550 551 /* 552 * File level interface, functions file_* 553 */ 554 int 555 file_load(char *filename, vm_offset_t dest, struct preloaded_file **result) 556 { 557 static int last_file_format = 0; 558 struct preloaded_file *fp; 559 int error; 560 int i; 561 562 if (archsw.arch_loadaddr != NULL) 563 dest = archsw.arch_loadaddr(LOAD_RAW, filename, dest); 564 565 error = EFTYPE; 566 for (i = last_file_format, fp = NULL; 567 file_formats[i] && fp == NULL; i++) { 568 error = (file_formats[i]->l_load)(filename, dest, &fp); 569 if (error == 0) { 570 fp->f_loader = last_file_format = i; /* remember the loader */ 571 *result = fp; 572 break; 573 } else if (last_file_format == i && i != 0) { 574 /* Restart from the beginning */ 575 i = -1; 576 last_file_format = 0; 577 fp = NULL; 578 continue; 579 } 580 if (error == EFTYPE) 581 continue; /* Unknown to this handler? */ 582 if (error) { 583 snprintf(command_errbuf, sizeof(command_errbuf), 584 "can't load file '%s': %s", filename, strerror(error)); 585 break; 586 } 587 } 588 return (error); 589 } 590 591 static int 592 file_load_dependencies(struct preloaded_file *base_file) 593 { 594 struct file_metadata *md; 595 struct preloaded_file *fp; 596 struct mod_depend *verinfo; 597 struct kernel_module *mp; 598 char *dmodname; 599 int error; 600 601 md = file_findmetadata(base_file, MODINFOMD_DEPLIST); 602 if (md == NULL) 603 return (0); 604 error = 0; 605 do { 606 verinfo = (struct mod_depend*)md->md_data; 607 dmodname = (char *)(verinfo + 1); 608 if (file_findmodule(NULL, dmodname, verinfo) == NULL) { 609 printf("loading required module '%s'\n", dmodname); 610 error = mod_load(dmodname, verinfo, 0, NULL); 611 if (error) 612 break; 613 /* 614 * If module loaded via kld name which isn't listed 615 * in the linker.hints file, we should check if it have 616 * required version. 617 */ 618 mp = file_findmodule(NULL, dmodname, verinfo); 619 if (mp == NULL) { 620 snprintf(command_errbuf, sizeof(command_errbuf), 621 "module '%s' exists but with wrong version", dmodname); 622 error = ENOENT; 623 break; 624 } 625 } 626 md = metadata_next(md, MODINFOMD_DEPLIST); 627 } while (md); 628 if (!error) 629 return (0); 630 /* Load failed; discard everything */ 631 while (base_file != NULL) { 632 fp = base_file; 633 base_file = base_file->f_next; 634 file_discard(fp); 635 } 636 return (error); 637 } 638 639 vm_offset_t 640 build_font_module(vm_offset_t addr) 641 { 642 vt_font_bitmap_data_t *bd; 643 struct vt_font *fd; 644 struct preloaded_file *fp; 645 size_t size; 646 uint32_t checksum; 647 int i; 648 struct font_info fi; 649 struct fontlist *fl; 650 uint64_t fontp; 651 652 if (STAILQ_EMPTY(&fonts)) 653 return (addr); 654 655 /* We can't load first */ 656 if ((file_findfile(NULL, NULL)) == NULL) { 657 printf("Can not load font module: %s\n", 658 "the kernel is not loaded"); 659 return (addr); 660 } 661 662 /* helper pointers */ 663 bd = NULL; 664 STAILQ_FOREACH(fl, &fonts, font_next) { 665 if (gfx_state.tg_font.vf_width == fl->font_data->vfbd_width && 666 gfx_state.tg_font.vf_height == fl->font_data->vfbd_height) { 667 /* 668 * Kernel does have better built in font. 669 */ 670 if (fl->font_flags == FONT_BUILTIN) 671 return (addr); 672 673 bd = fl->font_data; 674 break; 675 } 676 } 677 if (bd == NULL) 678 return (addr); 679 fd = bd->vfbd_font; 680 681 fi.fi_width = fd->vf_width; 682 checksum = fi.fi_width; 683 fi.fi_height = fd->vf_height; 684 checksum += fi.fi_height; 685 fi.fi_bitmap_size = bd->vfbd_uncompressed_size; 686 checksum += fi.fi_bitmap_size; 687 688 size = roundup2(sizeof (struct font_info), 8); 689 for (i = 0; i < VFNT_MAPS; i++) { 690 fi.fi_map_count[i] = fd->vf_map_count[i]; 691 checksum += fi.fi_map_count[i]; 692 size += fd->vf_map_count[i] * sizeof (struct vfnt_map); 693 size += roundup2(size, 8); 694 } 695 size += bd->vfbd_uncompressed_size; 696 697 fi.fi_checksum = -checksum; 698 699 fp = file_findfile(NULL, "elf kernel"); 700 if (fp == NULL) 701 fp = file_findfile(NULL, "elf64 kernel"); 702 if (fp == NULL) 703 panic("can't find kernel file"); 704 705 fontp = addr; 706 addr += archsw.arch_copyin(&fi, addr, sizeof (struct font_info)); 707 addr = roundup2(addr, 8); 708 709 /* Copy maps. */ 710 for (i = 0; i < VFNT_MAPS; i++) { 711 if (fd->vf_map_count[i] != 0) { 712 addr += archsw.arch_copyin(fd->vf_map[i], addr, 713 fd->vf_map_count[i] * sizeof (struct vfnt_map)); 714 addr = roundup2(addr, 8); 715 } 716 } 717 718 /* Copy the bitmap. */ 719 addr += archsw.arch_copyin(fd->vf_bytes, addr, fi.fi_bitmap_size); 720 721 /* Looks OK so far; populate control structure */ 722 file_addmetadata(fp, MODINFOMD_FONT, sizeof(fontp), &fontp); 723 return (addr); 724 } 725 726 #ifdef LOADER_VERIEXEC_VECTX 727 #define VECTX_HANDLE(fd) vctx 728 #else 729 #define VECTX_HANDLE(fd) fd 730 #endif 731 732 733 /* 734 * We've been asked to load (fname) as (type), so just suck it in, 735 * no arguments or anything. 736 */ 737 struct preloaded_file * 738 file_loadraw(const char *fname, char *type, int insert) 739 { 740 struct preloaded_file *fp; 741 char *name; 742 int fd, got; 743 vm_offset_t laddr; 744 #ifdef LOADER_VERIEXEC_VECTX 745 struct vectx *vctx; 746 int verror; 747 #endif 748 749 /* We can't load first */ 750 if ((file_findfile(NULL, NULL)) == NULL) { 751 command_errmsg = "can't load file before kernel"; 752 return(NULL); 753 } 754 755 /* locate the file on the load path */ 756 name = file_search(fname, NULL); 757 if (name == NULL) { 758 snprintf(command_errbuf, sizeof(command_errbuf), 759 "can't find '%s'", fname); 760 return(NULL); 761 } 762 763 if ((fd = open(name, O_RDONLY)) < 0) { 764 snprintf(command_errbuf, sizeof(command_errbuf), 765 "can't open '%s': %s", name, strerror(errno)); 766 free(name); 767 return(NULL); 768 } 769 770 #ifdef LOADER_VERIEXEC_VECTX 771 vctx = vectx_open(fd, name, 0L, NULL, &verror, __func__); 772 if (verror) { 773 sprintf(command_errbuf, "can't verify '%s': %s", 774 name, ve_error_get()); 775 free(name); 776 free(vctx); 777 close(fd); 778 return(NULL); 779 } 780 #else 781 #ifdef LOADER_VERIEXEC 782 if (verify_file(fd, name, 0, VE_MUST, __func__) < 0) { 783 sprintf(command_errbuf, "can't verify '%s': %s", 784 name, ve_error_get()); 785 free(name); 786 close(fd); 787 return(NULL); 788 } 789 #endif 790 #endif 791 792 if (archsw.arch_loadaddr != NULL) 793 loadaddr = archsw.arch_loadaddr(LOAD_RAW, name, loadaddr); 794 795 printf("%s ", name); 796 797 laddr = loadaddr; 798 for (;;) { 799 /* read in 4k chunks; size is not really important */ 800 got = archsw.arch_readin(VECTX_HANDLE(fd), laddr, 4096); 801 if (got == 0) /* end of file */ 802 break; 803 if (got < 0) { /* error */ 804 snprintf(command_errbuf, sizeof(command_errbuf), 805 "error reading '%s': %s", name, strerror(errno)); 806 free(name); 807 close(fd); 808 #ifdef LOADER_VERIEXEC_VECTX 809 free(vctx); 810 #endif 811 return(NULL); 812 } 813 laddr += got; 814 } 815 816 printf("size=%#jx\n", (uintmax_t)(laddr - loadaddr)); 817 #ifdef LOADER_VERIEXEC_VECTX 818 verror = vectx_close(vctx, VE_MUST, __func__); 819 if (verror) { 820 free(name); 821 close(fd); 822 free(vctx); 823 return(NULL); 824 } 825 #endif 826 827 /* Looks OK so far; create & populate control structure */ 828 fp = file_alloc(); 829 if (fp == NULL) { 830 snprintf(command_errbuf, sizeof (command_errbuf), 831 "no memory to load %s", name); 832 free(name); 833 close(fd); 834 return (NULL); 835 } 836 fp->f_name = name; 837 fp->f_type = strdup(type); 838 fp->f_args = NULL; 839 fp->f_metadata = NULL; 840 fp->f_loader = -1; 841 fp->f_addr = loadaddr; 842 fp->f_size = laddr - loadaddr; 843 844 if (fp->f_type == NULL) { 845 snprintf(command_errbuf, sizeof (command_errbuf), 846 "no memory to load %s", name); 847 free(name); 848 close(fd); 849 return (NULL); 850 } 851 /* recognise space consumption */ 852 loadaddr = laddr; 853 854 /* Add to the list of loaded files */ 855 if (insert != 0) 856 file_insert_tail(fp); 857 close(fd); 858 return(fp); 859 } 860 861 /* 862 * Load the module (name), pass it (argc),(argv), add container file 863 * to the list of loaded files. 864 * If module is already loaded just assign new argc/argv. 865 */ 866 int 867 mod_load(char *modname, struct mod_depend *verinfo, int argc, char *argv[]) 868 { 869 struct kernel_module *mp; 870 int err; 871 char *filename; 872 873 if (file_havepath(modname)) { 874 printf("Warning: mod_load() called instead of mod_loadkld() for module '%s'\n", modname); 875 return (mod_loadkld(modname, argc, argv)); 876 } 877 /* see if module is already loaded */ 878 mp = file_findmodule(NULL, modname, verinfo); 879 if (mp) { 880 #ifdef moduleargs 881 free(mp->m_args); 882 mp->m_args = unargv(argc, argv); 883 #endif 884 snprintf(command_errbuf, sizeof(command_errbuf), 885 "warning: module '%s' already loaded", mp->m_name); 886 return (0); 887 } 888 /* locate file with the module on the search path */ 889 filename = mod_searchmodule(modname, verinfo); 890 if (filename == NULL) { 891 snprintf(command_errbuf, sizeof(command_errbuf), 892 "can't find '%s'", modname); 893 return (ENOENT); 894 } 895 err = mod_loadkld(filename, argc, argv); 896 free(filename); 897 return (err); 898 } 899 900 /* 901 * Load specified KLD. If path is omitted, then try to locate it via 902 * search path. 903 */ 904 int 905 mod_loadkld(const char *kldname, int argc, char *argv[]) 906 { 907 struct preloaded_file *fp; 908 int err; 909 char *filename; 910 vm_offset_t loadaddr_saved; 911 912 /* 913 * Get fully qualified KLD name 914 */ 915 filename = file_search(kldname, kld_ext_list); 916 if (filename == NULL) { 917 snprintf(command_errbuf, sizeof(command_errbuf), 918 "can't find '%s'", kldname); 919 return (ENOENT); 920 } 921 /* 922 * Check if KLD already loaded 923 */ 924 fp = file_findfile(filename, NULL); 925 if (fp) { 926 snprintf(command_errbuf, sizeof(command_errbuf), 927 "warning: KLD '%s' already loaded", filename); 928 free(filename); 929 return (0); 930 } 931 932 do { 933 err = file_load(filename, loadaddr, &fp); 934 if (err) 935 break; 936 fp->f_args = unargv(argc, argv); 937 loadaddr_saved = loadaddr; 938 loadaddr = fp->f_addr + fp->f_size; 939 file_insert_tail(fp); /* Add to the list of loaded files */ 940 if (file_load_dependencies(fp) != 0) { 941 err = ENOENT; 942 file_remove(fp); 943 loadaddr = loadaddr_saved; 944 fp = NULL; 945 break; 946 } 947 } while(0); 948 if (err == EFTYPE) { 949 snprintf(command_errbuf, sizeof(command_errbuf), 950 "don't know how to load module '%s'", filename); 951 } 952 if (err) 953 file_discard(fp); 954 free(filename); 955 return (err); 956 } 957 958 /* 959 * Find a file matching (name) and (type). 960 * NULL may be passed as a wildcard to either. 961 */ 962 struct preloaded_file * 963 file_findfile(const char *name, const char *type) 964 { 965 struct preloaded_file *fp; 966 967 for (fp = preloaded_files; fp != NULL; fp = fp->f_next) { 968 if (((name == NULL) || !strcmp(name, fp->f_name)) && 969 ((type == NULL) || !strcmp(type, fp->f_type))) 970 break; 971 } 972 return (fp); 973 } 974 975 /* 976 * Find a module matching (name) inside of given file. 977 * NULL may be passed as a wildcard. 978 */ 979 struct kernel_module * 980 file_findmodule(struct preloaded_file *fp, char *modname, 981 struct mod_depend *verinfo) 982 { 983 struct kernel_module *mp, *best; 984 int bestver, mver; 985 986 if (fp == NULL) { 987 for (fp = preloaded_files; fp; fp = fp->f_next) { 988 mp = file_findmodule(fp, modname, verinfo); 989 if (mp) 990 return (mp); 991 } 992 return (NULL); 993 } 994 best = NULL; 995 bestver = 0; 996 for (mp = fp->f_modules; mp; mp = mp->m_next) { 997 if (strcmp(modname, mp->m_name) == 0) { 998 if (verinfo == NULL) 999 return (mp); 1000 mver = mp->m_version; 1001 if (mver == verinfo->md_ver_preferred) 1002 return (mp); 1003 if (mver >= verinfo->md_ver_minimum && 1004 mver <= verinfo->md_ver_maximum && 1005 mver > bestver) { 1006 best = mp; 1007 bestver = mver; 1008 } 1009 } 1010 } 1011 return (best); 1012 } 1013 /* 1014 * Make a copy of (size) bytes of data from (p), and associate them as 1015 * metadata of (type) to the module (mp). 1016 */ 1017 void 1018 file_addmetadata(struct preloaded_file *fp, int type, size_t size, void *p) 1019 { 1020 struct file_metadata *md; 1021 1022 md = malloc(sizeof(struct file_metadata) - sizeof(md->md_data) + size); 1023 if (md != NULL) { 1024 md->md_size = size; 1025 md->md_type = type; 1026 bcopy(p, md->md_data, size); 1027 md->md_next = fp->f_metadata; 1028 } 1029 fp->f_metadata = md; 1030 } 1031 1032 /* 1033 * Find a metadata object of (type) associated with the file (fp) 1034 */ 1035 struct file_metadata * 1036 file_findmetadata(struct preloaded_file *fp, int type) 1037 { 1038 struct file_metadata *md; 1039 1040 for (md = fp->f_metadata; md != NULL; md = md->md_next) 1041 if (md->md_type == type) 1042 break; 1043 return(md); 1044 } 1045 1046 /* 1047 * Remove all metadata from the file. 1048 */ 1049 void 1050 file_removemetadata(struct preloaded_file *fp) 1051 { 1052 struct file_metadata *md, *next; 1053 1054 for (md = fp->f_metadata; md != NULL; md = next) 1055 { 1056 next = md->md_next; 1057 free(md); 1058 } 1059 fp->f_metadata = NULL; 1060 } 1061 1062 /* 1063 * Add a buffer to the list of preloaded "files". 1064 */ 1065 int 1066 file_addbuf(const char *name, const char *type, size_t len, void *buf) 1067 { 1068 struct preloaded_file *fp; 1069 vm_offset_t dest; 1070 1071 /* We can't load first */ 1072 if ((file_findfile(NULL, NULL)) == NULL) { 1073 command_errmsg = "can't load file before kernel"; 1074 return (-1); 1075 } 1076 1077 /* Figure out where to load the data. */ 1078 dest = loadaddr; 1079 if (archsw.arch_loadaddr != NULL) 1080 dest = archsw.arch_loadaddr(LOAD_RAW, (void *)name, dest); 1081 1082 /* Create & populate control structure */ 1083 fp = file_alloc(); 1084 if (fp == NULL) { 1085 snprintf(command_errbuf, sizeof (command_errbuf), 1086 "no memory to load %s", name); 1087 return (-1); 1088 } 1089 fp->f_name = strdup(name); 1090 fp->f_type = strdup(type); 1091 fp->f_args = NULL; 1092 fp->f_metadata = NULL; 1093 fp->f_loader = -1; 1094 fp->f_addr = dest; 1095 fp->f_size = len; 1096 if ((fp->f_name == NULL) || (fp->f_type == NULL)) { 1097 snprintf(command_errbuf, sizeof (command_errbuf), 1098 "no memory to load %s", name); 1099 free(fp->f_name); 1100 free(fp->f_type); 1101 return (-1); 1102 } 1103 1104 /* Copy the data in. */ 1105 archsw.arch_copyin(buf, fp->f_addr, len); 1106 loadaddr = fp->f_addr + len; 1107 1108 /* Add to the list of loaded files */ 1109 file_insert_tail(fp); 1110 return(0); 1111 } 1112 1113 struct file_metadata * 1114 metadata_next(struct file_metadata *md, int type) 1115 { 1116 1117 if (md == NULL) 1118 return (NULL); 1119 while((md = md->md_next) != NULL) 1120 if (md->md_type == type) 1121 break; 1122 return (md); 1123 } 1124 1125 static char *emptyextlist[] = { "", NULL }; 1126 1127 /* 1128 * Check if the given file is in place and return full path to it. 1129 */ 1130 static char * 1131 file_lookup(const char *path, const char *name, int namelen, char **extlist) 1132 { 1133 struct stat st; 1134 char *result, *cp, **cpp; 1135 int pathlen, extlen, len; 1136 1137 pathlen = strlen(path); 1138 extlen = 0; 1139 if (extlist == NULL) 1140 extlist = emptyextlist; 1141 for (cpp = extlist; *cpp; cpp++) { 1142 len = strlen(*cpp); 1143 if (len > extlen) 1144 extlen = len; 1145 } 1146 result = malloc(pathlen + namelen + extlen + 2); 1147 if (result == NULL) 1148 return (NULL); 1149 bcopy(path, result, pathlen); 1150 if (pathlen > 0 && result[pathlen - 1] != '/') 1151 result[pathlen++] = '/'; 1152 cp = result + pathlen; 1153 bcopy(name, cp, namelen); 1154 cp += namelen; 1155 for (cpp = extlist; *cpp; cpp++) { 1156 strcpy(cp, *cpp); 1157 if (stat(result, &st) == 0 && S_ISREG(st.st_mode)) 1158 return result; 1159 } 1160 free(result); 1161 return NULL; 1162 } 1163 1164 /* 1165 * Check if file name have any qualifiers 1166 */ 1167 static int 1168 file_havepath(const char *name) 1169 { 1170 const char *cp; 1171 1172 archsw.arch_getdev(NULL, name, &cp); 1173 return (cp != name || strchr(name, '/') != NULL); 1174 } 1175 1176 /* 1177 * Attempt to find the file (name) on the module searchpath. 1178 * If (name) is qualified in any way, we simply check it and 1179 * return it or NULL. If it is not qualified, then we attempt 1180 * to construct a path using entries in the environment variable 1181 * module_path. 1182 * 1183 * The path we return a pointer to need never be freed, as we manage 1184 * it internally. 1185 */ 1186 static char * 1187 file_search(const char *name, char **extlist) 1188 { 1189 struct moduledir *mdp; 1190 struct stat sb; 1191 char *result; 1192 int namelen; 1193 1194 /* Don't look for nothing */ 1195 if (name == NULL) 1196 return(NULL); 1197 1198 if (*name == 0) 1199 return(strdup(name)); 1200 1201 if (file_havepath(name)) { 1202 /* Qualified, so just see if it exists */ 1203 if (stat(name, &sb) == 0) 1204 return(strdup(name)); 1205 return(NULL); 1206 } 1207 moduledir_rebuild(); 1208 result = NULL; 1209 namelen = strlen(name); 1210 STAILQ_FOREACH(mdp, &moduledir_list, d_link) { 1211 result = file_lookup(mdp->d_path, name, namelen, extlist); 1212 if (result) 1213 break; 1214 } 1215 return(result); 1216 } 1217 1218 #define INT_ALIGN(base, ptr) ptr = \ 1219 (base) + roundup2((ptr) - (base), sizeof(int)) 1220 1221 static char * 1222 mod_search_hints(struct moduledir *mdp, const char *modname, 1223 struct mod_depend *verinfo) 1224 { 1225 u_char *cp, *recptr, *bufend, *best; 1226 char *result; 1227 int *intp, bestver, blen, clen, found, ival, modnamelen, reclen; 1228 1229 moduledir_readhints(mdp); 1230 modnamelen = strlen(modname); 1231 found = 0; 1232 result = NULL; 1233 bestver = 0; 1234 if (mdp->d_hints == NULL) 1235 goto bad; 1236 recptr = mdp->d_hints; 1237 bufend = recptr + mdp->d_hintsz; 1238 clen = blen = 0; 1239 best = cp = NULL; 1240 while (recptr < bufend && !found) { 1241 intp = (int*)recptr; 1242 reclen = *intp++; 1243 ival = *intp++; 1244 cp = (u_char*)intp; 1245 switch (ival) { 1246 case MDT_VERSION: 1247 clen = *cp++; 1248 if (clen != modnamelen || bcmp(cp, modname, clen) != 0) 1249 break; 1250 cp += clen; 1251 INT_ALIGN(mdp->d_hints, cp); 1252 ival = *(int*)cp; 1253 cp += sizeof(int); 1254 clen = *cp++; 1255 if (verinfo == NULL || ival == verinfo->md_ver_preferred) { 1256 found = 1; 1257 break; 1258 } 1259 if (ival >= verinfo->md_ver_minimum && 1260 ival <= verinfo->md_ver_maximum && 1261 ival > bestver) { 1262 bestver = ival; 1263 best = cp; 1264 blen = clen; 1265 } 1266 break; 1267 default: 1268 break; 1269 } 1270 recptr += reclen + sizeof(int); 1271 } 1272 /* 1273 * Finally check if KLD is in the place 1274 */ 1275 if (found) 1276 result = file_lookup(mdp->d_path, (const char *)cp, clen, NULL); 1277 else if (best) 1278 result = file_lookup(mdp->d_path, (const char *)best, blen, NULL); 1279 bad: 1280 /* 1281 * If nothing found or hints is absent - fallback to the old way 1282 * by using "kldname[.ko]" as module name. 1283 */ 1284 if (!found && !bestver && result == NULL) 1285 result = file_lookup(mdp->d_path, modname, modnamelen, kld_ext_list); 1286 return result; 1287 } 1288 1289 static int 1290 getint(void **ptr) 1291 { 1292 int *p = *ptr; 1293 int rv; 1294 1295 p = (int *)roundup2((intptr_t)p, sizeof(int)); 1296 rv = *p++; 1297 *ptr = p; 1298 return rv; 1299 } 1300 1301 static void 1302 getstr(void **ptr, char *val) 1303 { 1304 int *p = *ptr; 1305 char *c = (char *)p; 1306 int len = *(uint8_t *)c; 1307 1308 memcpy(val, c + 1, len); 1309 val[len] = 0; 1310 c += len + 1; 1311 *ptr = (void *)c; 1312 } 1313 1314 static int 1315 pnpval_as_int(const char *val, const char *pnpinfo) 1316 { 1317 int rv; 1318 char key[256]; 1319 char *cp; 1320 1321 if (pnpinfo == NULL) 1322 return -1; 1323 1324 cp = strchr(val, ';'); 1325 key[0] = ' '; 1326 if (cp == NULL) 1327 strlcpy(key + 1, val, sizeof(key) - 1); 1328 else { 1329 memcpy(key + 1, val, cp - val); 1330 key[cp - val + 1] = '\0'; 1331 } 1332 strlcat(key, "=", sizeof(key)); 1333 if (strncmp(key + 1, pnpinfo, strlen(key + 1)) == 0) 1334 rv = strtol(pnpinfo + strlen(key + 1), NULL, 0); 1335 else { 1336 cp = strstr(pnpinfo, key); 1337 if (cp == NULL) 1338 rv = -1; 1339 else 1340 rv = strtol(cp + strlen(key), NULL, 0); 1341 } 1342 return rv; 1343 } 1344 1345 static void 1346 quoted_strcpy(char *dst, const char *src) 1347 { 1348 char q = ' '; 1349 1350 if (*src == '\'' || *src == '"') 1351 q = *src++; 1352 while (*src && *src != q) 1353 *dst++ = *src++; // XXX backtick quoting 1354 *dst++ = '\0'; 1355 // XXX overflow 1356 } 1357 1358 static char * 1359 pnpval_as_str(const char *val, const char *pnpinfo) 1360 { 1361 static char retval[256]; 1362 char key[256]; 1363 char *cp; 1364 1365 if (pnpinfo == NULL) { 1366 *retval = '\0'; 1367 return retval; 1368 } 1369 1370 cp = strchr(val, ';'); 1371 key[0] = ' '; 1372 if (cp == NULL) 1373 strlcpy(key + 1, val, sizeof(key) - 1); 1374 else { 1375 memcpy(key + 1, val, cp - val); 1376 key[cp - val + 1] = '\0'; 1377 } 1378 strlcat(key, "=", sizeof(key)); 1379 if (strncmp(key + 1, pnpinfo, strlen(key + 1)) == 0) 1380 quoted_strcpy(retval, pnpinfo + strlen(key + 1)); 1381 else { 1382 cp = strstr(pnpinfo, key); 1383 if (cp == NULL) 1384 strcpy(retval, "MISSING"); 1385 else 1386 quoted_strcpy(retval, cp + strlen(key)); 1387 } 1388 return retval; 1389 } 1390 1391 static char * 1392 devmatch_search_hints(struct moduledir *mdp, const char *bus, const char *dev, const char *pnpinfo) 1393 { 1394 char val1[256], val2[256]; 1395 int ival, len, ents, i, notme, mask, bit, v, found; 1396 void *ptr, *walker, *hints_end; 1397 char *lastmod = NULL, *cp, *s; 1398 1399 moduledir_readhints(mdp); 1400 found = 0; 1401 if (mdp->d_hints == NULL) 1402 goto bad; 1403 walker = mdp->d_hints; 1404 hints_end = walker + mdp->d_hintsz; 1405 while (walker < hints_end && !found) { 1406 len = getint(&walker); 1407 ival = getint(&walker); 1408 ptr = walker; 1409 switch (ival) { 1410 case MDT_VERSION: 1411 getstr(&ptr, val1); 1412 ival = getint(&ptr); 1413 getstr(&ptr, val2); 1414 if (pnp_dump_flag || pnp_verbose_flag) 1415 printf("Version: if %s.%d kmod %s\n", val1, ival, val2); 1416 break; 1417 case MDT_MODULE: 1418 getstr(&ptr, val1); 1419 getstr(&ptr, val2); 1420 if (lastmod) 1421 free(lastmod); 1422 lastmod = strdup(val2); 1423 if (pnp_dump_flag || pnp_verbose_flag) 1424 printf("module %s in %s\n", val1, val1); 1425 break; 1426 case MDT_PNP_INFO: 1427 if (!pnp_dump_flag && !pnp_unbound_flag && lastmod && strcmp(lastmod, "kernel") == 0) 1428 break; 1429 getstr(&ptr, val1); 1430 getstr(&ptr, val2); 1431 ents = getint(&ptr); 1432 if (pnp_dump_flag || pnp_verbose_flag) 1433 printf("PNP info for bus %s format %s %d entries (%s)\n", 1434 val1, val2, ents, lastmod); 1435 if (strcmp(val1, "usb") == 0) { 1436 if (pnp_verbose_flag) 1437 printf("Treating usb as uhub -- bug in source table still?\n"); 1438 strcpy(val1, "uhub"); 1439 } 1440 if (bus && strcmp(val1, bus) != 0) { 1441 if (pnp_verbose_flag) 1442 printf("Skipped because table for bus %s, looking for %s\n", 1443 val1, bus); 1444 break; 1445 } 1446 for (i = 0; i < ents; i++) { 1447 if (pnp_verbose_flag) 1448 printf("---------- Entry %d ----------\n", i); 1449 if (pnp_dump_flag) 1450 printf(" "); 1451 cp = val2; 1452 notme = 0; 1453 mask = -1; 1454 bit = -1; 1455 do { 1456 switch (*cp) { 1457 /* All integer fields */ 1458 case 'I': 1459 case 'J': 1460 case 'G': 1461 case 'L': 1462 case 'M': 1463 ival = getint(&ptr); 1464 if (pnp_dump_flag) { 1465 printf("%#x:", ival); 1466 break; 1467 } 1468 if (bit >= 0 && ((1 << bit) & mask) == 0) 1469 break; 1470 v = pnpval_as_int(cp + 2, pnpinfo); 1471 if (pnp_verbose_flag) 1472 printf("Matching %s (%c) table=%#x tomatch=%#x\n", 1473 cp + 2, *cp, v, ival); 1474 switch (*cp) { 1475 case 'J': 1476 if (ival == -1) 1477 break; 1478 /*FALLTHROUGH*/ 1479 case 'I': 1480 if (v != ival) 1481 notme++; 1482 break; 1483 case 'G': 1484 if (v < ival) 1485 notme++; 1486 break; 1487 case 'L': 1488 if (v > ival) 1489 notme++; 1490 break; 1491 case 'M': 1492 mask = ival; 1493 break; 1494 } 1495 break; 1496 /* String fields */ 1497 case 'D': 1498 case 'Z': 1499 getstr(&ptr, val1); 1500 if (pnp_dump_flag) { 1501 printf("'%s':", val1); 1502 break; 1503 } 1504 if (*cp == 'D') 1505 break; 1506 s = pnpval_as_str(cp + 2, pnpinfo); 1507 if (strcmp(s, val1) != 0) 1508 notme++; 1509 break; 1510 /* Key override fields, required to be last in the string */ 1511 case 'T': 1512 /* 1513 * This is imperfect and only does one key and will be redone 1514 * to be more general for multiple keys. Currently, nothing 1515 * does that. 1516 */ 1517 if (pnp_dump_flag) /* No per-row data stored */ 1518 break; 1519 if (cp[strlen(cp) - 1] == ';') /* Skip required ; at end */ 1520 cp[strlen(cp) - 1] = '\0'; /* in case it's not there */ 1521 if ((s = strstr(pnpinfo, cp + 2)) == NULL) 1522 notme++; 1523 else if (s > pnpinfo && s[-1] != ' ') 1524 notme++; 1525 break; 1526 default: 1527 printf("Unknown field type %c\n:", *cp); 1528 break; 1529 } 1530 bit++; 1531 cp = strchr(cp, ';'); 1532 if (cp) 1533 cp++; 1534 } while (cp && *cp); 1535 if (pnp_dump_flag) 1536 printf("\n"); 1537 else if (!notme) { 1538 if (!pnp_unbound_flag) { 1539 if (pnp_verbose_flag) 1540 printf("Matches --- %s ---\n", lastmod); 1541 } 1542 found++; 1543 } 1544 } 1545 break; 1546 default: 1547 break; 1548 } 1549 walker = (void *)(len - sizeof(int) + (intptr_t)walker); 1550 } 1551 if (pnp_unbound_flag && found == 0 && *pnpinfo) { 1552 if (pnp_verbose_flag) 1553 printf("------------------------- "); 1554 printf("%s on %s pnpinfo %s", *dev ? dev : "unattached", bus, pnpinfo); 1555 if (pnp_verbose_flag) 1556 printf(" -------------------------"); 1557 printf("\n"); 1558 } 1559 if (found != 0) 1560 return (lastmod); 1561 free(lastmod); 1562 1563 bad: 1564 return (NULL); 1565 } 1566 1567 /* 1568 * Attempt to locate the file containing the module (name) 1569 */ 1570 static char * 1571 mod_searchmodule(char *name, struct mod_depend *verinfo) 1572 { 1573 struct moduledir *mdp; 1574 char *result; 1575 1576 moduledir_rebuild(); 1577 /* 1578 * Now we ready to lookup module in the given directories 1579 */ 1580 result = NULL; 1581 STAILQ_FOREACH(mdp, &moduledir_list, d_link) { 1582 result = mod_search_hints(mdp, name, verinfo); 1583 if (result) 1584 break; 1585 } 1586 1587 return(result); 1588 } 1589 1590 static char * 1591 mod_searchmodule_pnpinfo(const char *bus, const char *pnpinfo) 1592 { 1593 struct moduledir *mdp; 1594 char *result; 1595 1596 moduledir_rebuild(); 1597 /* 1598 * Now we ready to lookup module in the given directories 1599 */ 1600 result = NULL; 1601 STAILQ_FOREACH(mdp, &moduledir_list, d_link) { 1602 result = devmatch_search_hints(mdp, bus, NULL, pnpinfo); 1603 if (result) 1604 break; 1605 } 1606 1607 return(result); 1608 } 1609 1610 int 1611 file_addmodule(struct preloaded_file *fp, char *modname, int version, 1612 struct kernel_module **newmp) 1613 { 1614 struct kernel_module *mp; 1615 struct mod_depend mdepend; 1616 1617 bzero(&mdepend, sizeof(mdepend)); 1618 mdepend.md_ver_preferred = version; 1619 mp = file_findmodule(fp, modname, &mdepend); 1620 if (mp) 1621 return (EEXIST); 1622 mp = calloc(1, sizeof(struct kernel_module)); 1623 if (mp == NULL) 1624 return (ENOMEM); 1625 mp->m_name = strdup(modname); 1626 if (mp->m_name == NULL) { 1627 free(mp); 1628 return (ENOMEM); 1629 } 1630 mp->m_version = version; 1631 mp->m_fp = fp; 1632 mp->m_next = fp->f_modules; 1633 fp->f_modules = mp; 1634 if (newmp) 1635 *newmp = mp; 1636 return (0); 1637 } 1638 1639 /* 1640 * Throw a file away 1641 */ 1642 void 1643 file_discard(struct preloaded_file *fp) 1644 { 1645 struct file_metadata *md, *md1; 1646 struct kernel_module *mp, *mp1; 1647 if (fp == NULL) 1648 return; 1649 md = fp->f_metadata; 1650 while (md) { 1651 md1 = md; 1652 md = md->md_next; 1653 free(md1); 1654 } 1655 mp = fp->f_modules; 1656 while (mp) { 1657 free(mp->m_name); 1658 mp1 = mp; 1659 mp = mp->m_next; 1660 free(mp1); 1661 } 1662 free(fp->f_name); 1663 free(fp->f_type); 1664 free(fp->f_args); 1665 free(fp); 1666 } 1667 1668 /* 1669 * Allocate a new file; must be used instead of malloc() 1670 * to ensure safe initialisation. 1671 */ 1672 struct preloaded_file * 1673 file_alloc(void) 1674 { 1675 1676 return (calloc(1, sizeof(struct preloaded_file))); 1677 } 1678 1679 /* 1680 * Add a module to the chain 1681 */ 1682 static void 1683 file_insert_tail(struct preloaded_file *fp) 1684 { 1685 struct preloaded_file *cm; 1686 1687 /* Append to list of loaded file */ 1688 fp->f_next = NULL; 1689 if (preloaded_files == NULL) { 1690 preloaded_files = fp; 1691 } else { 1692 for (cm = preloaded_files; cm->f_next != NULL; cm = cm->f_next) 1693 ; 1694 cm->f_next = fp; 1695 } 1696 } 1697 1698 /* 1699 * Remove module from the chain 1700 */ 1701 static void 1702 file_remove(struct preloaded_file *fp) 1703 { 1704 struct preloaded_file *cm; 1705 1706 if (preloaded_files == NULL) 1707 return; 1708 1709 if (preloaded_files == fp) { 1710 preloaded_files = fp->f_next; 1711 return; 1712 } 1713 for (cm = preloaded_files; cm->f_next != NULL; cm = cm->f_next) { 1714 if (cm->f_next == fp) { 1715 cm->f_next = fp->f_next; 1716 return; 1717 } 1718 } 1719 } 1720 1721 static char * 1722 moduledir_fullpath(struct moduledir *mdp, const char *fname) 1723 { 1724 char *cp; 1725 1726 cp = malloc(strlen(mdp->d_path) + strlen(fname) + 2); 1727 if (cp == NULL) 1728 return NULL; 1729 strcpy(cp, mdp->d_path); 1730 strcat(cp, "/"); 1731 strcat(cp, fname); 1732 return (cp); 1733 } 1734 1735 /* 1736 * Read linker.hints file into memory performing some sanity checks. 1737 */ 1738 static void 1739 moduledir_readhints(struct moduledir *mdp) 1740 { 1741 struct stat st; 1742 char *path; 1743 int fd, size, version; 1744 1745 if (mdp->d_hints != NULL || (mdp->d_flags & MDIR_NOHINTS)) 1746 return; 1747 path = moduledir_fullpath(mdp, "linker.hints"); 1748 if (stat(path, &st) != 0 || 1749 st.st_size < (ssize_t)(sizeof(version) + sizeof(int)) || 1750 st.st_size > LINKER_HINTS_MAX || (fd = open(path, O_RDONLY)) < 0) { 1751 free(path); 1752 mdp->d_flags |= MDIR_NOHINTS; 1753 return; 1754 } 1755 free(path); 1756 size = read(fd, &version, sizeof(version)); 1757 if (size != sizeof(version) || version != LINKER_HINTS_VERSION) 1758 goto bad; 1759 size = st.st_size - size; 1760 mdp->d_hints = malloc(size); 1761 if (mdp->d_hints == NULL) 1762 goto bad; 1763 if (read(fd, mdp->d_hints, size) != size) 1764 goto bad; 1765 mdp->d_hintsz = size; 1766 close(fd); 1767 return; 1768 bad: 1769 close(fd); 1770 free(mdp->d_hints); 1771 mdp->d_hints = NULL; 1772 mdp->d_flags |= MDIR_NOHINTS; 1773 return; 1774 } 1775 1776 /* 1777 * Extract directories from the ';' separated list, remove duplicates. 1778 */ 1779 static void 1780 moduledir_rebuild(void) 1781 { 1782 struct moduledir *mdp, *mtmp; 1783 const char *path, *cp, *ep; 1784 size_t cplen; 1785 1786 path = getenv("module_path"); 1787 if (path == NULL) 1788 path = default_searchpath; 1789 /* 1790 * Rebuild list of module directories if it changed 1791 */ 1792 STAILQ_FOREACH(mdp, &moduledir_list, d_link) 1793 mdp->d_flags |= MDIR_REMOVED; 1794 1795 for (ep = path; *ep != 0; ep++) { 1796 cp = ep; 1797 for (; *ep != 0 && *ep != ';'; ep++) 1798 ; 1799 /* 1800 * Ignore trailing slashes 1801 */ 1802 for (cplen = ep - cp; cplen > 1 && cp[cplen - 1] == '/'; cplen--) 1803 ; 1804 STAILQ_FOREACH(mdp, &moduledir_list, d_link) { 1805 if (strlen(mdp->d_path) != cplen || bcmp(cp, mdp->d_path, cplen) != 0) 1806 continue; 1807 mdp->d_flags &= ~MDIR_REMOVED; 1808 break; 1809 } 1810 if (mdp == NULL) { 1811 mdp = malloc(sizeof(*mdp) + cplen + 1); 1812 if (mdp == NULL) 1813 return; 1814 mdp->d_path = (char*)(mdp + 1); 1815 bcopy(cp, mdp->d_path, cplen); 1816 mdp->d_path[cplen] = 0; 1817 mdp->d_hints = NULL; 1818 mdp->d_flags = 0; 1819 STAILQ_INSERT_TAIL(&moduledir_list, mdp, d_link); 1820 } 1821 if (*ep == 0) 1822 break; 1823 } 1824 /* 1825 * Delete unused directories if any 1826 */ 1827 mdp = STAILQ_FIRST(&moduledir_list); 1828 while (mdp) { 1829 if ((mdp->d_flags & MDIR_REMOVED) == 0) { 1830 mdp = STAILQ_NEXT(mdp, d_link); 1831 } else { 1832 free(mdp->d_hints); 1833 mtmp = mdp; 1834 mdp = STAILQ_NEXT(mdp, d_link); 1835 STAILQ_REMOVE(&moduledir_list, mtmp, moduledir, d_link); 1836 free(mtmp); 1837 } 1838 } 1839 return; 1840 } 1841