2 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * file/module function dispatcher, support, etc.
33 #include <sys/param.h>
34 #include <sys/linker.h>
35 #include <sys/module.h>
36 #include <sys/queue.h>
37 #include <sys/stdint.h>
41 #if defined(LOADER_FDT_SUPPORT)
42 #include <fdt_platform.h>
45 #include "bootstrap.h"
47 #define MDIR_REMOVED 0x0001
48 #define MDIR_NOHINTS 0x0002
51 char *d_path
; /* path of modules directory */
52 u_char
*d_hints
; /* content of linker.hints file */
53 int d_hintsz
; /* size of hints data */
55 STAILQ_ENTRY(moduledir
) d_link
;
58 static int file_load(char *filename
, vm_offset_t dest
, struct preloaded_file
**result
);
59 static int file_load_dependencies(struct preloaded_file
*base_mod
);
60 static char * file_search(const char *name
, char **extlist
);
61 static struct kernel_module
* file_findmodule(struct preloaded_file
*fp
, char *modname
, struct mod_depend
*verinfo
);
62 static int file_havepath(const char *name
);
63 static char *mod_searchmodule(char *name
, struct mod_depend
*verinfo
);
64 static char * mod_searchmodule_pnpinfo(const char *bus
, const char *pnpinfo
);
65 static void file_insert_tail(struct preloaded_file
*mp
);
66 static void file_remove(struct preloaded_file
*fp
);
67 static void file_remove_tail(struct preloaded_file
*fp
);
68 struct file_metadata
* metadata_next(struct file_metadata
*base_mp
, int type
);
69 static void moduledir_readhints(struct moduledir
*mdp
);
70 static void moduledir_rebuild(void);
72 /* load address should be tweaked by first module loaded (kernel) */
73 static vm_offset_t loadaddr
= 0;
75 #if defined(LOADER_FDT_SUPPORT)
76 static const char *default_searchpath
=
77 "/boot/kernel;/boot/modules;/boot/dtb";
79 static const char *default_searchpath
= "/boot/kernel;/boot/modules";
82 static STAILQ_HEAD(, moduledir
) moduledir_list
=
83 STAILQ_HEAD_INITIALIZER(moduledir_list
);
85 struct preloaded_file
*preloaded_files
= NULL
;
87 static char *kld_ext_list
[] = {
95 * load an object, either a disk file or code module.
97 * To load a file, the syntax is:
99 * load -t <type> <path>
101 * code modules are loaded as:
103 * load <path> <options>
106 COMMAND_SET(load
, "load", "load a kernel or module", command_load
);
109 command_load(int argc
, char *argv
[])
111 struct preloaded_file
*fp
;
113 #ifdef LOADER_VERIEXEC
116 char *args
= "dkp:s:t:";
120 int dofile
, dokld
, ch
, error
;
127 command_errmsg
= "no filename specified";
130 #ifdef LOADER_VERIEXEC
134 while ((ch
= getopt(argc
, argv
, args
)) != -1) {
136 #ifdef LOADER_VERIEXEC
144 #ifdef LOADER_VERIEXEC
158 /* getopt has already reported an error */
162 argv
+= (optind
- 1);
163 argc
-= (optind
- 1);
166 * Request to load a raw file?
169 if ((argc
!= 2) || (typestr
== NULL
) || (*typestr
== 0)) {
170 command_errmsg
= "invalid load type";
174 #ifdef LOADER_VERIEXEC
175 if (strncmp(typestr
, "manifest", 8) == 0) {
178 return (load_manifest(argv
[1], prefix
, skip
, NULL
));
180 #ifdef LOADER_VERIEXEC_PASS_MANIFEST
181 if (strncmp(typestr
, "pass_manifest", 13) == 0) {
184 return (pass_manifest(argv
[1], prefix
));
189 fp
= file_findfile(argv
[1], typestr
);
191 snprintf(command_errbuf
, sizeof(command_errbuf
),
192 "warning: file '%s' already loaded", argv
[1]);
196 if (file_loadraw(argv
[1], typestr
, 1) != NULL
)
199 /* Failing to load mfs_root is never going to end well! */
200 if (strcmp("mfs_root", typestr
) == 0)
206 * Do we have explicit KLD load ?
208 if (dokld
|| file_havepath(argv
[1])) {
209 error
= mod_loadkld(argv
[1], argc
- 2, argv
+ 2);
210 if (error
== EEXIST
) {
211 snprintf(command_errbuf
, sizeof(command_errbuf
),
212 "warning: KLD '%s' already loaded", argv
[1]);
216 return (error
== 0 ? CMD_OK
: CMD_CRIT
);
219 * Looks like a request for a module.
221 error
= mod_load(argv
[1], NULL
, argc
- 2, argv
+ 2);
222 if (error
== EEXIST
) {
223 snprintf(command_errbuf
, sizeof(command_errbuf
),
224 "warning: module '%s' already loaded", argv
[1]);
228 return (error
== 0 ? CMD_OK
: CMD_CRIT
);
231 #ifdef LOADER_GELI_SUPPORT
232 COMMAND_SET(load_geli
, "load_geli", "load a geli key", command_load_geli
);
235 command_load_geli(int argc
, char *argv
[])
242 command_errmsg
= "usage is [-n key#] <prov> <file>";
249 while ((ch
= getopt(argc
, argv
, "n:")) != -1) {
252 num
= strtol(optarg
, &cp
, 0);
254 snprintf(command_errbuf
, sizeof(command_errbuf
),
255 "bad key index '%s'", optarg
);
261 /* getopt has already reported an error */
265 argv
+= (optind
- 1);
266 argc
-= (optind
- 1);
267 sprintf(typestr
, "%s:geli_keyfile%d", argv
[1], num
);
268 return (file_loadraw(argv
[2], typestr
, 1) ? CMD_OK
: CMD_ERROR
);
275 struct preloaded_file
*fp
;
277 while (preloaded_files
!= NULL
) {
278 fp
= preloaded_files
;
279 preloaded_files
= preloaded_files
->f_next
;
283 unsetenv("kernelname");
286 COMMAND_SET(unload
, "unload", "unload all modules", command_unload
);
289 command_unload(int argc
, char *argv
[])
295 COMMAND_SET(lsmod
, "lsmod", "list loaded modules", command_lsmod
);
298 command_lsmod(int argc
, char *argv
[])
300 struct preloaded_file
*fp
;
301 struct kernel_module
*mp
;
302 struct file_metadata
*md
;
304 int ch
, verbose
, ret
= 0;
309 while ((ch
= getopt(argc
, argv
, "v")) != -1) {
316 /* getopt has already reported an error */
322 for (fp
= preloaded_files
; fp
; fp
= fp
->f_next
) {
323 snprintf(lbuf
, sizeof(lbuf
), " %p: ", (void *) fp
->f_addr
);
325 pager_output(fp
->f_name
);
326 snprintf(lbuf
, sizeof(lbuf
), " (%s, 0x%lx)\n", fp
->f_type
,
328 if (pager_output(lbuf
))
330 if (fp
->f_args
!= NULL
) {
331 pager_output(" args: ");
332 pager_output(fp
->f_args
);
333 if (pager_output("\n"))
337 pager_output(" modules: ");
338 for (mp
= fp
->f_modules
; mp
; mp
= mp
->m_next
) {
339 snprintf(lbuf
, sizeof(lbuf
), "%s.%d ", mp
->m_name
,
343 if (pager_output("\n"))
347 /* XXX could add some formatting smarts here to display some better */
348 for (md
= fp
->f_metadata
; md
!= NULL
; md
= md
->md_next
) {
349 snprintf(lbuf
, sizeof(lbuf
), " 0x%04x, 0x%lx\n",
350 md
->md_type
, (long) md
->md_size
);
351 if (pager_output(lbuf
))
362 COMMAND_SET(pnpmatch
, "pnpmatch", "list matched modules based on pnpinfo", command_pnpmatch
);
364 static int pnp_dump_flag
= 0;
365 static int pnp_unbound_flag
= 0;
366 static int pnp_verbose_flag
= 0;
369 command_pnpmatch(int argc
, char *argv
[])
374 pnp_verbose_flag
= 0;
378 while ((ch
= getopt(argc
, argv
, "vd")) != -1) {
381 pnp_verbose_flag
= 1;
388 /* getopt has already reported an error */
396 command_errmsg
= "Usage: pnpmatch <busname> compat=<compatdata>";
400 module
= mod_searchmodule_pnpinfo(argv
[0], argv
[1]);
402 printf("Matched module: %s\n", module
);
404 printf("No module matches %s on bus %s\n", argv
[1], argv
[0]);
409 COMMAND_SET(pnpload
, "pnpload", "load matched modules based on pnpinfo", command_pnpload
);
412 command_pnpload(int argc
, char *argv
[])
417 pnp_verbose_flag
= 0;
421 while ((ch
= getopt(argc
, argv
, "vd")) != -1) {
424 pnp_verbose_flag
= 1;
431 /* getopt has already reported an error */
439 command_errmsg
= "Usage: pnpload <busname> compat=<compatdata>";
443 module
= mod_searchmodule_pnpinfo(argv
[0], argv
[1]);
445 error
= mod_load(module
, NULL
, 0, NULL
);
446 if (error
== EEXIST
) {
447 snprintf(command_errbuf
, sizeof(command_errbuf
),
448 "warning: module '%s' already loaded", argv
[1]);
452 return (error
== 0 ? CMD_OK
: CMD_CRIT
);
455 #if defined(LOADER_FDT_SUPPORT)
457 pnpautoload_fdt_bus(const char *busname
) {
458 const char *pnpstring
;
459 const char *compatstr
;
460 char *pnpinfo
= NULL
;
462 int tag
= 0, len
, pnplen
;
466 pnpstring
= fdt_devmatch_next(&tag
, &len
);
467 if (pnpstring
== NULL
)
470 compatstr
= pnpstring
;
471 for (pnplen
= 0; pnplen
!= len
; compatstr
= pnpstring
+ pnplen
) {
472 pnplen
+= strlen(compatstr
) + 1;
473 asprintf(&pnpinfo
, "compat=%s", compatstr
);
475 module
= mod_searchmodule_pnpinfo(busname
, pnpinfo
);
477 error
= mod_loadkld(module
, 0, NULL
);
479 printf("Cannot load module %s\n", module
);
491 void (*load
)(const char *busname
);
494 struct pnp_bus pnp_buses
[] = {
495 #if defined(LOADER_FDT_SUPPORT)
496 {"simplebus", pnpautoload_fdt_bus
},
497 {"ofwbus", pnpautoload_fdt_bus
},
498 {"iicbus", pnpautoload_fdt_bus
},
499 {"spibus", pnpautoload_fdt_bus
},
503 COMMAND_SET(pnpautoload
, "pnpautoload", "auto load modules based on pnpinfo", command_pnpautoload
);
506 command_pnpautoload(int argc
, char *argv
[])
512 pnp_verbose_flag
= 0;
518 while ((ch
= getopt(argc
, argv
, "v")) != -1) {
525 /* getopt has already reported an error */
529 argv
+= (optind
- 1);
530 argc
-= (optind
- 1);
535 for (i
= 0; i
< nitems(pnp_buses
); i
++) {
536 if (argc
== 2 && strcmp(argv
[1], pnp_buses
[i
].name
) != 0) {
538 printf("Skipping bus %s\n", pnp_buses
[i
].name
);
542 printf("Autoloading modules for %s\n", pnp_buses
[i
].name
);
543 pnp_buses
[i
].load(pnp_buses
[i
].name
);
547 printf("Unsupported bus %s\n", argv
[1]);
553 * File level interface, functions file_*
556 file_load(char *filename
, vm_offset_t dest
, struct preloaded_file
**result
)
558 static int last_file_format
= 0;
559 struct preloaded_file
*fp
;
564 if (archsw
.arch_loadaddr
!= NULL
)
565 dest
= archsw
.arch_loadaddr(LOAD_RAW
, filename
, dest
);
568 for (i
= last_file_format
, fp
= NULL
;
569 file_formats
[i
] && fp
== NULL
; i
++) {
570 error
= (file_formats
[i
]->l_load
)(filename
, dest
, &fp
);
572 fp
->f_loader
= last_file_format
= i
; /* remember the loader */
575 } else if (last_file_format
== i
&& i
!= 0) {
576 /* Restart from the beginning */
578 last_file_format
= 0;
583 continue; /* Unknown to this handler? */
585 snprintf(command_errbuf
, sizeof(command_errbuf
),
586 "can't load file '%s': %s", filename
, strerror(error
));
595 file_load_dependencies(struct preloaded_file
*base_file
)
597 struct file_metadata
*md
;
598 struct preloaded_file
*fp
;
599 struct mod_depend
*verinfo
;
600 struct kernel_module
*mp
;
604 md
= file_findmetadata(base_file
, MODINFOMD_DEPLIST
);
609 verinfo
= (struct mod_depend
*)md
->md_data
;
610 dmodname
= (char *)(verinfo
+ 1);
611 if (file_findmodule(NULL
, dmodname
, verinfo
) == NULL
) {
612 if (module_verbose
> MODULE_VERBOSE_SILENT
)
613 printf("loading required module '%s'\n", dmodname
);
614 error
= mod_load(dmodname
, verinfo
, 0, NULL
);
618 * If module loaded via kld name which isn't listed
619 * in the linker.hints file, we should check if it have
622 mp
= file_findmodule(NULL
, dmodname
, verinfo
);
624 snprintf(command_errbuf
, sizeof(command_errbuf
),
625 "module '%s' exists but with wrong version", dmodname
);
630 md
= metadata_next(md
, MODINFOMD_DEPLIST
);
634 /* Load failed; discard everything */
635 while (base_file
!= NULL
) {
637 base_file
= base_file
->f_next
;
643 #ifdef LOADER_VERIEXEC_VECTX
644 #define VECTX_HANDLE(fd) vctx
646 #define VECTX_HANDLE(fd) fd
651 * We've been asked to load (fname) as (type), so just suck it in,
652 * no arguments or anything.
654 struct preloaded_file
*
655 file_loadraw(const char *fname
, const char *type
, int insert
)
657 struct preloaded_file
*fp
;
661 #ifdef LOADER_VERIEXEC_VECTX
667 /* We can't load first */
668 if ((file_findfile(NULL
, NULL
)) == NULL
) {
669 command_errmsg
= "can't load file before kernel";
674 /* locate the file on the load path */
675 name
= file_search(fname
, NULL
);
677 snprintf(command_errbuf
, sizeof(command_errbuf
),
678 "can't find '%s'", fname
);
683 if ((fd
= open(name
, O_RDONLY
)) < 0) {
684 snprintf(command_errbuf
, sizeof(command_errbuf
),
685 "can't open '%s': %s", name
, strerror(errno
));
691 #ifdef LOADER_VERIEXEC_VECTX
692 vctx
= vectx_open(fd
, name
, 0L, NULL
, &verror
, __func__
);
694 sprintf(command_errbuf
, "can't verify '%s': %s",
695 name
, ve_error_get());
703 #ifdef LOADER_VERIEXEC
704 if (verify_file(fd
, name
, 0, VE_MUST
, __func__
) < 0) {
705 sprintf(command_errbuf
, "can't verify '%s': %s",
706 name
, ve_error_get());
715 if (archsw
.arch_loadaddr
!= NULL
)
716 loadaddr
= archsw
.arch_loadaddr(LOAD_RAW
, name
, loadaddr
);
718 if (module_verbose
> MODULE_VERBOSE_SILENT
)
723 /* read in 4k chunks; size is not really important */
724 got
= archsw
.arch_readin(VECTX_HANDLE(fd
), laddr
, 4096);
725 if (got
== 0) /* end of file */
727 if (got
< 0) { /* error */
728 snprintf(command_errbuf
, sizeof(command_errbuf
),
729 "error reading '%s': %s", name
, strerror(errno
));
732 #ifdef LOADER_VERIEXEC_VECTX
741 if (module_verbose
> MODULE_VERBOSE_SILENT
)
742 printf("size=%#jx\n", (uintmax_t)(laddr
- loadaddr
));
743 #ifdef LOADER_VERIEXEC_VECTX
744 verror
= vectx_close(vctx
, VE_MUST
, __func__
);
754 /* Looks OK so far; create & populate control structure */
757 snprintf(command_errbuf
, sizeof (command_errbuf
),
758 "no memory to load %s", name
);
765 fp
->f_type
= strdup(type
);
767 fp
->f_metadata
= NULL
;
769 fp
->f_addr
= loadaddr
;
770 fp
->f_size
= laddr
- loadaddr
;
772 if (fp
->f_type
== NULL
) {
773 snprintf(command_errbuf
, sizeof (command_errbuf
),
774 "no memory to load %s", name
);
780 /* recognise space consumption */
783 /* Add to the list of loaded files */
785 file_insert_tail(fp
);
792 * Load the module (name), pass it (argc),(argv), add container file
793 * to the list of loaded files.
794 * If module is already loaded just assign new argc/argv.
797 mod_load(char *modname
, struct mod_depend
*verinfo
, int argc
, char *argv
[])
799 struct kernel_module
*mp
;
804 if (file_havepath(modname
)) {
805 printf("Warning: mod_load() called instead of mod_loadkld() for module '%s'\n", modname
);
807 return (mod_loadkld(modname
, argc
, argv
));
809 /* see if module is already loaded */
810 mp
= file_findmodule(NULL
, modname
, verinfo
);
814 mp
->m_args
= unargv(argc
, argv
);
816 snprintf(command_errbuf
, sizeof(command_errbuf
),
817 "warning: module '%s' already loaded", mp
->m_name
);
821 /* locate file with the module on the search path */
822 filename
= mod_searchmodule(modname
, verinfo
);
823 if (filename
== NULL
) {
824 snprintf(command_errbuf
, sizeof(command_errbuf
),
825 "can't find '%s'", modname
);
829 err
= mod_loadkld(filename
, argc
, argv
);
836 * Load specified KLD. If path is omitted, then try to locate it via
840 mod_loadkld(const char *kldname
, int argc
, char *argv
[])
842 struct preloaded_file
*fp
;
845 vm_offset_t loadaddr_saved
;
849 * Get fully qualified KLD name
851 filename
= file_search(kldname
, kld_ext_list
);
852 if (filename
== NULL
) {
853 snprintf(command_errbuf
, sizeof(command_errbuf
),
854 "can't find '%s'", kldname
);
859 * Check if KLD already loaded
861 fp
= file_findfile(filename
, NULL
);
863 snprintf(command_errbuf
, sizeof(command_errbuf
),
864 "warning: KLD '%s' already loaded", filename
);
871 err
= file_load(filename
, loadaddr
, &fp
);
874 fp
->f_args
= unargv(argc
, argv
);
875 loadaddr_saved
= loadaddr
;
876 loadaddr
= fp
->f_addr
+ fp
->f_size
;
877 file_insert_tail(fp
); /* Add to the list of loaded files */
878 if (file_load_dependencies(fp
) != 0) {
880 file_remove_tail(fp
);
881 loadaddr
= loadaddr_saved
;
887 snprintf(command_errbuf
, sizeof(command_errbuf
),
888 "don't know how to load module '%s'", filename
);
898 * Find a file matching (name) and (type).
899 * NULL may be passed as a wildcard to either.
901 struct preloaded_file
*
902 file_findfile(const char *name
, const char *type
)
904 struct preloaded_file
*fp
;
906 for (fp
= preloaded_files
; fp
!= NULL
; fp
= fp
->f_next
) {
907 if (((name
== NULL
) || !strcmp(name
, fp
->f_name
)) &&
908 ((type
== NULL
) || !strcmp(type
, fp
->f_type
)))
915 * Find a module matching (name) inside of given file.
916 * NULL may be passed as a wildcard.
918 struct kernel_module
*
919 file_findmodule(struct preloaded_file
*fp
, char *modname
,
920 struct mod_depend
*verinfo
)
922 struct kernel_module
*mp
, *best
;
926 for (fp
= preloaded_files
; fp
; fp
= fp
->f_next
) {
927 mp
= file_findmodule(fp
, modname
, verinfo
);
935 for (mp
= fp
->f_modules
; mp
; mp
= mp
->m_next
) {
936 if (strcmp(modname
, mp
->m_name
) == 0) {
939 mver
= mp
->m_version
;
940 if (mver
== verinfo
->md_ver_preferred
)
942 if (mver
>= verinfo
->md_ver_minimum
&&
943 mver
<= verinfo
->md_ver_maximum
&&
953 * Make a copy of (size) bytes of data from (p), and associate them as
954 * metadata of (type) to the module (mp).
957 file_addmetadata(struct preloaded_file
*fp
, int type
, size_t size
, void *p
)
959 struct file_metadata
*md
;
961 md
= malloc(sizeof(struct file_metadata
) - sizeof(md
->md_data
) + size
);
965 bcopy(p
, md
->md_data
, size
);
966 md
->md_next
= fp
->f_metadata
;
972 * Find a metadata object of (type) associated with the file (fp)
974 struct file_metadata
*
975 file_findmetadata(struct preloaded_file
*fp
, int type
)
977 struct file_metadata
*md
;
979 for (md
= fp
->f_metadata
; md
!= NULL
; md
= md
->md_next
)
980 if (md
->md_type
== type
)
986 * Remove all metadata from the file.
989 file_removemetadata(struct preloaded_file
*fp
)
991 struct file_metadata
*md
, *next
;
993 for (md
= fp
->f_metadata
; md
!= NULL
; md
= next
)
998 fp
->f_metadata
= NULL
;
1002 * Add a buffer to the list of preloaded "files".
1005 file_addbuf(const char *name
, const char *type
, size_t len
, void *buf
)
1007 struct preloaded_file
*fp
;
1010 /* We can't load first */
1011 if ((file_findfile(NULL
, NULL
)) == NULL
) {
1012 command_errmsg
= "can't load file before kernel";
1016 /* Figure out where to load the data. */
1018 if (archsw
.arch_loadaddr
!= NULL
)
1019 dest
= archsw
.arch_loadaddr(LOAD_RAW
, (void *)name
, dest
);
1021 /* Create & populate control structure */
1024 snprintf(command_errbuf
, sizeof (command_errbuf
),
1025 "no memory to load %s", name
);
1028 fp
->f_name
= strdup(name
);
1029 fp
->f_type
= strdup(type
);
1031 fp
->f_metadata
= NULL
;
1035 if ((fp
->f_name
== NULL
) || (fp
->f_type
== NULL
)) {
1036 snprintf(command_errbuf
, sizeof (command_errbuf
),
1037 "no memory to load %s", name
);
1043 /* Copy the data in. */
1044 archsw
.arch_copyin(buf
, fp
->f_addr
, len
);
1045 loadaddr
= fp
->f_addr
+ len
;
1047 /* Add to the list of loaded files */
1048 file_insert_tail(fp
);
1052 struct file_metadata
*
1053 metadata_next(struct file_metadata
*md
, int type
)
1058 while((md
= md
->md_next
) != NULL
)
1059 if (md
->md_type
== type
)
1064 static char *emptyextlist
[] = { "", NULL
};
1067 * Check if the given file is in place and return full path to it.
1070 file_lookup(const char *path
, const char *name
, int namelen
, char **extlist
)
1073 char *result
, *cp
, **cpp
;
1074 int pathlen
, extlen
, len
;
1076 pathlen
= strlen(path
);
1078 if (extlist
== NULL
)
1079 extlist
= emptyextlist
;
1080 for (cpp
= extlist
; *cpp
; cpp
++) {
1085 result
= malloc(pathlen
+ namelen
+ extlen
+ 2);
1088 bcopy(path
, result
, pathlen
);
1089 if (pathlen
> 0 && result
[pathlen
- 1] != '/')
1090 result
[pathlen
++] = '/';
1091 cp
= result
+ pathlen
;
1092 bcopy(name
, cp
, namelen
);
1094 for (cpp
= extlist
; *cpp
; cpp
++) {
1096 if (stat(result
, &st
) == 0 && S_ISREG(st
.st_mode
))
1104 * Check if file name have any qualifiers
1107 file_havepath(const char *name
)
1111 archsw
.arch_getdev(NULL
, name
, &cp
);
1112 return (cp
!= name
|| strchr(name
, '/') != NULL
);
1116 * Attempt to find the file (name) on the module searchpath.
1117 * If (name) is qualified in any way, we simply check it and
1118 * return it or NULL. If it is not qualified, then we attempt
1119 * to construct a path using entries in the environment variable
1122 * The path we return a pointer to need never be freed, as we manage
1126 file_search(const char *name
, char **extlist
)
1128 struct moduledir
*mdp
;
1133 /* Don't look for nothing */
1138 return(strdup(name
));
1140 if (file_havepath(name
)) {
1141 /* Qualified, so just see if it exists */
1142 if (stat(name
, &sb
) == 0)
1143 return(strdup(name
));
1146 moduledir_rebuild();
1148 namelen
= strlen(name
);
1149 STAILQ_FOREACH(mdp
, &moduledir_list
, d_link
) {
1150 result
= file_lookup(mdp
->d_path
, name
, namelen
, extlist
);
1157 #define INT_ALIGN(base, ptr) ptr = \
1158 (base) + roundup2((ptr) - (base), sizeof(int))
1161 mod_search_hints(struct moduledir
*mdp
, const char *modname
,
1162 struct mod_depend
*verinfo
)
1164 u_char
*cp
, *recptr
, *bufend
, *best
;
1166 int *intp
, bestver
, blen
, clen
, found
, ival
, modnamelen
, reclen
;
1168 moduledir_readhints(mdp
);
1169 modnamelen
= strlen(modname
);
1173 if (mdp
->d_hints
== NULL
)
1175 recptr
= mdp
->d_hints
;
1176 bufend
= recptr
+ mdp
->d_hintsz
;
1179 while (recptr
< bufend
&& !found
) {
1180 intp
= (int*)recptr
;
1187 if (clen
!= modnamelen
|| bcmp(cp
, modname
, clen
) != 0)
1190 INT_ALIGN(mdp
->d_hints
, cp
);
1194 if (verinfo
== NULL
|| ival
== verinfo
->md_ver_preferred
) {
1198 if (ival
>= verinfo
->md_ver_minimum
&&
1199 ival
<= verinfo
->md_ver_maximum
&&
1209 recptr
+= reclen
+ sizeof(int);
1212 * Finally check if KLD is in the place
1215 result
= file_lookup(mdp
->d_path
, (const char *)cp
, clen
, NULL
);
1217 result
= file_lookup(mdp
->d_path
, (const char *)best
, blen
, NULL
);
1220 * If nothing found or hints is absent - fallback to the old way
1221 * by using "kldname[.ko]" as module name.
1223 if (!found
&& !bestver
&& result
== NULL
)
1224 result
= file_lookup(mdp
->d_path
, modname
, modnamelen
, kld_ext_list
);
1234 p
= (int *)roundup2((intptr_t)p
, sizeof(int));
1241 getstr(void **ptr
, char *val
)
1244 char *c
= (char *)p
;
1245 int len
= *(uint8_t *)c
;
1247 memcpy(val
, c
+ 1, len
);
1254 pnpval_as_int(const char *val
, const char *pnpinfo
)
1260 if (pnpinfo
== NULL
)
1263 cp
= strchr(val
, ';');
1266 strlcpy(key
+ 1, val
, sizeof(key
) - 1);
1268 memcpy(key
+ 1, val
, cp
- val
);
1269 key
[cp
- val
+ 1] = '\0';
1271 strlcat(key
, "=", sizeof(key
));
1272 if (strncmp(key
+ 1, pnpinfo
, strlen(key
+ 1)) == 0)
1273 rv
= strtol(pnpinfo
+ strlen(key
+ 1), NULL
, 0);
1275 cp
= strstr(pnpinfo
, key
);
1279 rv
= strtol(cp
+ strlen(key
), NULL
, 0);
1285 quoted_strcpy(char *dst
, const char *src
)
1289 if (*src
== '\'' || *src
== '"')
1291 while (*src
&& *src
!= q
)
1292 *dst
++ = *src
++; // XXX backtick quoting
1298 pnpval_as_str(const char *val
, const char *pnpinfo
)
1300 static char retval
[256];
1304 if (pnpinfo
== NULL
) {
1309 cp
= strchr(val
, ';');
1312 strlcpy(key
+ 1, val
, sizeof(key
) - 1);
1314 memcpy(key
+ 1, val
, cp
- val
);
1315 key
[cp
- val
+ 1] = '\0';
1317 strlcat(key
, "=", sizeof(key
));
1318 if (strncmp(key
+ 1, pnpinfo
, strlen(key
+ 1)) == 0)
1319 quoted_strcpy(retval
, pnpinfo
+ strlen(key
+ 1));
1321 cp
= strstr(pnpinfo
, key
);
1323 strcpy(retval
, "MISSING");
1325 quoted_strcpy(retval
, cp
+ strlen(key
));
1331 devmatch_search_hints(struct moduledir
*mdp
, const char *bus
, const char *dev
, const char *pnpinfo
)
1333 char val1
[256], val2
[256];
1334 int ival
, len
, ents
, i
, notme
, mask
, bit
, v
, found
;
1335 void *ptr
, *walker
, *hints_end
;
1336 char *lastmod
= NULL
, *cp
, *s
;
1338 moduledir_readhints(mdp
);
1340 if (mdp
->d_hints
== NULL
)
1342 walker
= mdp
->d_hints
;
1343 hints_end
= walker
+ mdp
->d_hintsz
;
1344 while (walker
< hints_end
&& !found
) {
1345 len
= getint(&walker
);
1346 ival
= getint(&walker
);
1351 ival
= getint(&ptr
);
1353 if (pnp_dump_flag
|| pnp_verbose_flag
)
1354 printf("Version: if %s.%d kmod %s\n", val1
, ival
, val2
);
1361 lastmod
= strdup(val2
);
1362 if (pnp_dump_flag
|| pnp_verbose_flag
)
1363 printf("module %s in %s\n", val1
, val1
);
1366 if (!pnp_dump_flag
&& !pnp_unbound_flag
&& lastmod
&& strcmp(lastmod
, "kernel") == 0)
1370 ents
= getint(&ptr
);
1371 if (pnp_dump_flag
|| pnp_verbose_flag
)
1372 printf("PNP info for bus %s format %s %d entries (%s)\n",
1373 val1
, val2
, ents
, lastmod
);
1374 if (strcmp(val1
, "usb") == 0) {
1375 if (pnp_verbose_flag
)
1376 printf("Treating usb as uhub -- bug in source table still?\n");
1377 strcpy(val1
, "uhub");
1379 if (bus
&& strcmp(val1
, bus
) != 0) {
1380 if (pnp_verbose_flag
)
1381 printf("Skipped because table for bus %s, looking for %s\n",
1385 for (i
= 0; i
< ents
; i
++) {
1386 if (pnp_verbose_flag
)
1387 printf("---------- Entry %d ----------\n", i
);
1396 /* All integer fields */
1402 ival
= getint(&ptr
);
1403 if (pnp_dump_flag
) {
1404 printf("%#x:", ival
);
1407 if (bit
>= 0 && ((1 << bit
) & mask
) == 0)
1409 v
= pnpval_as_int(cp
+ 2, pnpinfo
);
1410 if (pnp_verbose_flag
)
1411 printf("Matching %s (%c) table=%#x tomatch=%#x\n",
1412 cp
+ 2, *cp
, v
, ival
);
1439 if (pnp_dump_flag
) {
1440 printf("'%s':", val1
);
1445 s
= pnpval_as_str(cp
+ 2, pnpinfo
);
1446 if (strcmp(s
, val1
) != 0)
1449 /* Key override fields, required to be last in the string */
1452 * This is imperfect and only does one key and will be redone
1453 * to be more general for multiple keys. Currently, nothing
1456 if (pnp_dump_flag
) /* No per-row data stored */
1458 if (cp
[strlen(cp
) - 1] == ';') /* Skip required ; at end */
1459 cp
[strlen(cp
) - 1] = '\0'; /* in case it's not there */
1460 if ((s
= strstr(pnpinfo
, cp
+ 2)) == NULL
)
1462 else if (s
> pnpinfo
&& s
[-1] != ' ')
1466 printf("Unknown field type %c\n:", *cp
);
1470 cp
= strchr(cp
, ';');
1473 } while (cp
&& *cp
);
1477 if (!pnp_unbound_flag
) {
1478 if (pnp_verbose_flag
)
1479 printf("Matches --- %s ---\n", lastmod
);
1488 walker
= (void *)(len
- sizeof(int) + (intptr_t)walker
);
1490 if (pnp_unbound_flag
&& found
== 0 && *pnpinfo
) {
1491 if (pnp_verbose_flag
)
1492 printf("------------------------- ");
1493 printf("%s on %s pnpinfo %s", *dev
? dev
: "unattached", bus
, pnpinfo
);
1494 if (pnp_verbose_flag
)
1495 printf(" -------------------------");
1507 * Attempt to locate the file containing the module (name)
1510 mod_searchmodule(char *name
, struct mod_depend
*verinfo
)
1512 struct moduledir
*mdp
;
1515 moduledir_rebuild();
1517 * Now we ready to lookup module in the given directories
1520 STAILQ_FOREACH(mdp
, &moduledir_list
, d_link
) {
1521 result
= mod_search_hints(mdp
, name
, verinfo
);
1530 mod_searchmodule_pnpinfo(const char *bus
, const char *pnpinfo
)
1532 struct moduledir
*mdp
;
1535 moduledir_rebuild();
1537 * Now we ready to lookup module in the given directories
1540 STAILQ_FOREACH(mdp
, &moduledir_list
, d_link
) {
1541 result
= devmatch_search_hints(mdp
, bus
, NULL
, pnpinfo
);
1550 file_addmodule(struct preloaded_file
*fp
, char *modname
, int version
,
1551 struct kernel_module
**newmp
)
1553 struct kernel_module
*mp
;
1554 struct mod_depend mdepend
;
1556 bzero(&mdepend
, sizeof(mdepend
));
1557 mdepend
.md_ver_preferred
= version
;
1558 mp
= file_findmodule(fp
, modname
, &mdepend
);
1561 mp
= calloc(1, sizeof(struct kernel_module
));
1564 mp
->m_name
= strdup(modname
);
1565 if (mp
->m_name
== NULL
) {
1569 mp
->m_version
= version
;
1571 mp
->m_next
= fp
->f_modules
;
1582 file_discard(struct preloaded_file
*fp
)
1584 struct file_metadata
*md
, *md1
;
1585 struct kernel_module
*mp
, *mp1
;
1588 md
= fp
->f_metadata
;
1608 * Allocate a new file; must be used instead of malloc()
1609 * to ensure safe initialisation.
1611 struct preloaded_file
*
1615 return (calloc(1, sizeof(struct preloaded_file
)));
1619 * Add a module to the chain
1622 file_insert_tail(struct preloaded_file
*fp
)
1624 struct preloaded_file
*cm
;
1626 /* Append to list of loaded file */
1628 if (preloaded_files
== NULL
) {
1629 preloaded_files
= fp
;
1631 for (cm
= preloaded_files
; cm
->f_next
!= NULL
; cm
= cm
->f_next
)
1638 * Remove module from the chain
1641 file_remove_impl(struct preloaded_file
*fp
, bool keep_tail
)
1643 struct preloaded_file
*cm
, *next
;
1645 if (preloaded_files
== NULL
)
1653 if (preloaded_files
== fp
) {
1654 preloaded_files
= next
;
1658 for (cm
= preloaded_files
; cm
->f_next
!= NULL
; cm
= cm
->f_next
) {
1659 if (cm
->f_next
== fp
) {
1667 file_remove(struct preloaded_file
*fp
)
1670 file_remove_impl(fp
, true);
1674 file_remove_tail(struct preloaded_file
*fp
)
1677 file_remove_impl(fp
, false);
1681 moduledir_fullpath(struct moduledir
*mdp
, const char *fname
)
1685 cp
= malloc(strlen(mdp
->d_path
) + strlen(fname
) + 2);
1688 strcpy(cp
, mdp
->d_path
);
1695 * Read linker.hints file into memory performing some sanity checks.
1698 moduledir_readhints(struct moduledir
*mdp
)
1702 int fd
, size
, version
;
1704 if (mdp
->d_hints
!= NULL
|| (mdp
->d_flags
& MDIR_NOHINTS
))
1706 path
= moduledir_fullpath(mdp
, "linker.hints");
1707 if (stat(path
, &st
) != 0 ||
1708 st
.st_size
< (ssize_t
)(sizeof(version
) + sizeof(int)) ||
1709 st
.st_size
> LINKER_HINTS_MAX
|| (fd
= open(path
, O_RDONLY
)) < 0) {
1711 mdp
->d_flags
|= MDIR_NOHINTS
;
1715 size
= read(fd
, &version
, sizeof(version
));
1716 if (size
!= sizeof(version
) || version
!= LINKER_HINTS_VERSION
)
1718 size
= st
.st_size
- size
;
1719 mdp
->d_hints
= malloc(size
);
1720 if (mdp
->d_hints
== NULL
)
1722 if (read(fd
, mdp
->d_hints
, size
) != size
)
1724 mdp
->d_hintsz
= size
;
1730 mdp
->d_hints
= NULL
;
1731 mdp
->d_flags
|= MDIR_NOHINTS
;
1736 * Extract directories from the ';' separated list, remove duplicates.
1739 moduledir_rebuild(void)
1741 struct moduledir
*mdp
, *mtmp
;
1742 const char *path
, *cp
, *ep
;
1745 path
= getenv("module_path");
1747 path
= default_searchpath
;
1749 * Rebuild list of module directories if it changed
1751 STAILQ_FOREACH(mdp
, &moduledir_list
, d_link
)
1752 mdp
->d_flags
|= MDIR_REMOVED
;
1754 for (ep
= path
; *ep
!= 0; ep
++) {
1756 for (; *ep
!= 0 && *ep
!= ';'; ep
++)
1759 * Ignore trailing slashes
1761 for (cplen
= ep
- cp
; cplen
> 1 && cp
[cplen
- 1] == '/'; cplen
--)
1763 STAILQ_FOREACH(mdp
, &moduledir_list
, d_link
) {
1764 if (strlen(mdp
->d_path
) != cplen
|| bcmp(cp
, mdp
->d_path
, cplen
) != 0)
1766 mdp
->d_flags
&= ~MDIR_REMOVED
;
1770 mdp
= malloc(sizeof(*mdp
) + cplen
+ 1);
1773 mdp
->d_path
= (char*)(mdp
+ 1);
1774 bcopy(cp
, mdp
->d_path
, cplen
);
1775 mdp
->d_path
[cplen
] = 0;
1776 mdp
->d_hints
= NULL
;
1778 STAILQ_INSERT_TAIL(&moduledir_list
, mdp
, d_link
);
1784 * Delete unused directories if any
1786 mdp
= STAILQ_FIRST(&moduledir_list
);
1788 if ((mdp
->d_flags
& MDIR_REMOVED
) == 0) {
1789 mdp
= STAILQ_NEXT(mdp
, d_link
);
1793 mdp
= STAILQ_NEXT(mdp
, d_link
);
1794 STAILQ_REMOVE(&moduledir_list
, mtmp
, moduledir
, d_link
);