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