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
26 * $FreeBSD: src/sys/boot/common/module.c,v 1.25 2003/08/25 23:30:41 obrien Exp $
27 * $DragonFly: src/sys/boot/common/module.c,v 1.4 2004/12/20 13:13:19 joerg Exp $
31 * file/module function dispatcher, support, etc.
36 #include <sys/param.h>
37 #include <sys/linker.h>
38 #include <sys/module.h>
39 #include <sys/queue.h>
41 #include "bootstrap.h"
43 #define MDIR_REMOVED 0x0001
44 #define MDIR_NOHINTS 0x0002
47 char *d_path
; /* path of modules directory */
48 u_char
*d_hints
; /* content of linker.hints file */
49 int d_hintsz
; /* size of hints data */
51 STAILQ_ENTRY(moduledir
) d_link
;
54 static int file_load(char *filename
, vm_offset_t dest
, struct preloaded_file
**result
);
55 static int file_loadraw(char *type
, char *name
);
56 static int file_load_dependencies(struct preloaded_file
*base_mod
);
57 static char * file_search(const char *name
, char **extlist
);
58 static struct kernel_module
* file_findmodule(struct preloaded_file
*fp
, char *modname
, struct mod_depend
*verinfo
);
59 static int file_havepath(const char *name
);
60 static char *mod_searchmodule(char *name
, struct mod_depend
*verinfo
);
61 static void file_insert_tail(struct preloaded_file
*mp
);
62 struct file_metadata
* metadata_next(struct file_metadata
*base_mp
, int type
);
63 static void moduledir_readhints(struct moduledir
*mdp
);
64 static void moduledir_rebuild(void);
66 /* load address should be tweaked by first module loaded (kernel) */
67 static vm_offset_t loadaddr
= 0;
69 static const char *default_searchpath
="/;/boot;/modules";
71 static STAILQ_HEAD(, moduledir
) moduledir_list
= STAILQ_HEAD_INITIALIZER(moduledir_list
);
73 struct preloaded_file
*preloaded_files
= NULL
;
75 static char *kld_ext_list
[] = {
83 * load an object, either a disk file or code module.
85 * To load a file, the syntax is:
87 * load -t <type> <path>
89 * code modules are loaded as:
91 * load <path> <options>
94 COMMAND_SET(load
, "load", "load a kernel or module", command_load
);
97 command_load(int argc
, char *argv
[])
100 int dofile
, dokld
, ch
, error
;
107 command_errmsg
= "no filename specified";
110 while ((ch
= getopt(argc
, argv
, "kt:")) != -1) {
121 /* getopt has already reported an error */
125 argv
+= (optind
- 1);
126 argc
-= (optind
- 1);
129 * Request to load a raw file?
132 if ((argc
!= 2) || (typestr
== NULL
) || (*typestr
== 0)) {
133 command_errmsg
= "invalid load type";
136 return(file_loadraw(typestr
, argv
[1]));
139 * Do we have explicit KLD load ?
141 if (dokld
|| file_havepath(argv
[1])) {
142 error
= mod_loadkld(argv
[1], argc
- 2, argv
+ 2);
144 sprintf(command_errbuf
, "warning: KLD '%s' already loaded", argv
[1]);
145 return (error
== 0 ? CMD_OK
: CMD_ERROR
);
148 * Looks like a request for a module.
150 error
= mod_load(argv
[1], NULL
, argc
- 2, argv
+ 2);
152 sprintf(command_errbuf
, "warning: module '%s' already loaded", argv
[1]);
153 return (error
== 0 ? CMD_OK
: CMD_ERROR
);
156 COMMAND_SET(unload
, "unload", "unload all modules", command_unload
);
159 command_unload(int argc
, char *argv
[])
161 struct preloaded_file
*fp
;
163 while (preloaded_files
!= NULL
) {
164 fp
= preloaded_files
;
165 preloaded_files
= preloaded_files
->f_next
;
169 unsetenv("kernelname");
173 COMMAND_SET(lsmod
, "lsmod", "list loaded modules", command_lsmod
);
176 command_lsmod(int argc
, char *argv
[])
178 struct preloaded_file
*fp
;
179 struct kernel_module
*mp
;
180 struct file_metadata
*md
;
187 while ((ch
= getopt(argc
, argv
, "v")) != -1) {
194 /* getopt has already reported an error */
200 for (fp
= preloaded_files
; fp
; fp
= fp
->f_next
) {
201 sprintf(lbuf
, " %p: %s (%s, 0x%lx)\n",
202 (void *) fp
->f_addr
, fp
->f_name
, fp
->f_type
, (long) fp
->f_size
);
204 if (fp
->f_args
!= NULL
) {
205 pager_output(" args: ");
206 pager_output(fp
->f_args
);
210 pager_output(" modules: ");
211 for (mp
= fp
->f_modules
; mp
; mp
= mp
->m_next
) {
212 sprintf(lbuf
, "%s.%d ", mp
->m_name
, mp
->m_version
);
218 /* XXX could add some formatting smarts here to display some better */
219 for (md
= fp
->f_metadata
; md
!= NULL
; md
= md
->md_next
) {
220 sprintf(lbuf
, " 0x%04x, 0x%lx\n", md
->md_type
, (long) md
->md_size
);
230 * File level interface, functions file_*
233 file_load(char *filename
, vm_offset_t dest
, struct preloaded_file
**result
)
235 struct preloaded_file
*fp
;
240 for (i
= 0, fp
= NULL
; file_formats
[i
] && fp
== NULL
; i
++) {
241 error
= (file_formats
[i
]->l_load
)(filename
, loadaddr
, &fp
);
243 fp
->f_loader
= i
; /* remember the loader */
248 continue; /* Unknown to this handler? */
250 sprintf(command_errbuf
, "can't load file '%s': %s",
251 filename
, strerror(error
));
259 file_load_dependencies(struct preloaded_file
*base_file
) {
260 struct file_metadata
*md
;
261 struct preloaded_file
*fp
;
262 struct mod_depend
*verinfo
;
263 struct kernel_module
*mp
;
267 md
= file_findmetadata(base_file
, MODINFOMD_DEPLIST
);
272 verinfo
= (struct mod_depend
*)md
->md_data
;
273 dmodname
= (char *)(verinfo
+ 1);
274 if (file_findmodule(NULL
, dmodname
, verinfo
) == NULL
) {
275 printf("loading required module '%s'\n", dmodname
);
276 error
= mod_load(dmodname
, verinfo
, 0, NULL
);
280 * If module loaded via kld name which isn't listed
281 * in the linker.hints file, we should check if it have
284 mp
= file_findmodule(NULL
, dmodname
, verinfo
);
286 sprintf(command_errbuf
, "module '%s' exists but with wrong version",
292 md
= metadata_next(md
, MODINFOMD_DEPLIST
);
296 /* Load failed; discard everything */
297 while (base_file
!= NULL
) {
299 base_file
= base_file
->f_next
;
305 * We've been asked to load (name) as (type), so just suck it in,
306 * no arguments or anything.
309 file_loadraw(char *type
, char *name
)
311 struct preloaded_file
*fp
;
316 /* We can't load first */
317 if ((file_findfile(NULL
, NULL
)) == NULL
) {
318 command_errmsg
= "can't load file before kernel";
322 /* locate the file on the load path */
323 cp
= file_search(name
, NULL
);
325 sprintf(command_errbuf
, "can't find '%s'", name
);
330 if ((fd
= open(name
, O_RDONLY
)) < 0) {
331 sprintf(command_errbuf
, "can't open '%s': %s", name
, strerror(errno
));
338 /* read in 4k chunks; size is not really important */
339 got
= archsw
.arch_readin(fd
, laddr
, 4096);
340 if (got
== 0) /* end of file */
342 if (got
< 0) { /* error */
343 sprintf(command_errbuf
, "error reading '%s': %s", name
, strerror(errno
));
351 /* Looks OK so far; create & populate control structure */
354 fp
->f_type
= strdup(type
);
356 fp
->f_metadata
= NULL
;
358 fp
->f_addr
= loadaddr
;
359 fp
->f_size
= laddr
- loadaddr
;
361 /* recognise space consumption */
364 /* Add to the list of loaded files */
365 file_insert_tail(fp
);
371 * Load the module (name), pass it (argc),(argv), add container file
372 * to the list of loaded files.
373 * If module is already loaded just assign new argc/argv.
376 mod_load(char *modname
, struct mod_depend
*verinfo
, int argc
, char *argv
[])
378 struct kernel_module
*mp
;
382 if (file_havepath(modname
)) {
383 printf("Warning: mod_load() called instead of mod_loadkld() for module '%s'\n", modname
);
384 return (mod_loadkld(modname
, argc
, argv
));
386 /* see if module is already loaded */
387 mp
= file_findmodule(NULL
, modname
, verinfo
);
392 mp
->m_args
= unargv(argc
, argv
);
394 sprintf(command_errbuf
, "warning: module '%s' already loaded", mp
->m_name
);
397 /* locate file with the module on the search path */
398 filename
= mod_searchmodule(modname
, verinfo
);
399 if (filename
== NULL
) {
400 sprintf(command_errbuf
, "can't find '%s'", modname
);
403 err
= mod_loadkld(filename
, argc
, argv
);
408 * Load specified KLD. If path is omitted, then try to locate it via
412 mod_loadkld(const char *kldname
, int argc
, char *argv
[])
414 struct preloaded_file
*fp
, *last_file
;
419 * Get fully qualified KLD name
421 filename
= file_search(kldname
, kld_ext_list
);
422 if (filename
== NULL
) {
423 sprintf(command_errbuf
, "can't find '%s'", kldname
);
427 * Check if KLD already loaded
429 fp
= file_findfile(filename
, NULL
);
431 sprintf(command_errbuf
, "warning: KLD '%s' already loaded", filename
);
435 for (last_file
= preloaded_files
;
436 last_file
!= NULL
&& last_file
->f_next
!= NULL
;
437 last_file
= last_file
->f_next
)
441 err
= file_load(filename
, loadaddr
, &fp
);
444 fp
->f_args
= unargv(argc
, argv
);
445 loadaddr
= fp
->f_addr
+ fp
->f_size
;
446 file_insert_tail(fp
); /* Add to the list of loaded files */
447 if (file_load_dependencies(fp
) != 0) {
449 last_file
->f_next
= NULL
;
450 loadaddr
= last_file
->f_addr
+ last_file
->f_size
;
456 sprintf(command_errbuf
, "don't know how to load module '%s'", filename
);
464 * Find a file matching (name) and (type).
465 * NULL may be passed as a wildcard to either.
467 struct preloaded_file
*
468 file_findfile(char *name
, char *type
)
470 struct preloaded_file
*fp
;
472 for (fp
= preloaded_files
; fp
!= NULL
; fp
= fp
->f_next
) {
473 if (((name
== NULL
) || !strcmp(name
, fp
->f_name
)) &&
474 ((type
== NULL
) || !strcmp(type
, fp
->f_type
)))
481 * Find a module matching (name) inside of given file.
482 * NULL may be passed as a wildcard.
484 struct kernel_module
*
485 file_findmodule(struct preloaded_file
*fp
, char *modname
,
486 struct mod_depend
*verinfo
)
488 struct kernel_module
*mp
, *best
;
492 for (fp
= preloaded_files
; fp
; fp
= fp
->f_next
) {
493 mp
= file_findmodule(fp
, modname
, verinfo
);
501 for (mp
= fp
->f_modules
; mp
; mp
= mp
->m_next
) {
502 if (strcmp(modname
, mp
->m_name
) == 0) {
505 mver
= mp
->m_version
;
506 if (mver
== verinfo
->md_ver_preferred
)
508 if (mver
>= verinfo
->md_ver_minimum
&&
509 mver
<= verinfo
->md_ver_maximum
&&
519 * Make a copy of (size) bytes of data from (p), and associate them as
520 * metadata of (type) to the module (mp).
523 file_addmetadata(struct preloaded_file
*fp
, int type
, size_t size
, void *p
)
525 struct file_metadata
*md
;
527 md
= malloc(sizeof(struct file_metadata
) - sizeof(md
->md_data
) + size
);
530 bcopy(p
, md
->md_data
, size
);
531 md
->md_next
= fp
->f_metadata
;
536 * Find a metadata object of (type) associated with the file (fp)
538 struct file_metadata
*
539 file_findmetadata(struct preloaded_file
*fp
, int type
)
541 struct file_metadata
*md
;
543 for (md
= fp
->f_metadata
; md
!= NULL
; md
= md
->md_next
)
544 if (md
->md_type
== type
)
549 struct file_metadata
*
550 metadata_next(struct file_metadata
*md
, int type
)
554 while((md
= md
->md_next
) != NULL
)
555 if (md
->md_type
== type
)
560 static char *emptyextlist
[] = { "", NULL
};
563 * Check if the given file is in place and return full path to it.
566 file_lookup(const char *path
, const char *name
, int namelen
, char **extlist
)
569 char *result
, *cp
, **cpp
;
570 int pathlen
, extlen
, len
;
572 pathlen
= strlen(path
);
575 extlist
= emptyextlist
;
576 for (cpp
= extlist
; *cpp
; cpp
++) {
581 result
= malloc(pathlen
+ namelen
+ extlen
+ 2);
584 bcopy(path
, result
, pathlen
);
585 if (pathlen
> 0 && result
[pathlen
- 1] != '/')
586 result
[pathlen
++] = '/';
587 cp
= result
+ pathlen
;
588 bcopy(name
, cp
, namelen
);
590 for (cpp
= extlist
; *cpp
; cpp
++) {
592 if (stat(result
, &st
) == 0 && S_ISREG(st
.st_mode
))
600 * Check if file name have any qualifiers
603 file_havepath(const char *name
)
607 archsw
.arch_getdev(NULL
, name
, &cp
);
608 return (cp
!= name
|| strchr(name
, '/') != NULL
);
612 * Attempt to find the file (name) on the module searchpath.
613 * If (name) is qualified in any way, we simply check it and
614 * return it or NULL. If it is not qualified, then we attempt
615 * to construct a path using entries in the environment variable
618 * The path we return a pointer to need never be freed, as we manage
622 file_search(const char *name
, char **extlist
)
624 struct moduledir
*mdp
;
629 /* Don't look for nothing */
634 return(strdup(name
));
636 if (file_havepath(name
)) {
637 /* Qualified, so just see if it exists */
638 if (stat(name
, &sb
) == 0)
639 return(strdup(name
));
644 namelen
= strlen(name
);
645 STAILQ_FOREACH(mdp
, &moduledir_list
, d_link
) {
646 result
= file_lookup(mdp
->d_path
, name
, namelen
, extlist
);
653 #define INT_ALIGN(base, ptr) ptr = \
654 (base) + (((ptr) - (base) + sizeof(int) - 1) & ~(sizeof(int) - 1))
657 mod_search_hints(struct moduledir
*mdp
, const char *modname
,
658 struct mod_depend
*verinfo
)
660 u_char
*cp
, *recptr
, *bufend
, *best
;
662 int *intp
, bestver
, blen
, clen
, found
, ival
, modnamelen
, reclen
;
664 moduledir_readhints(mdp
);
665 modnamelen
= strlen(modname
);
669 if (mdp
->d_hints
== NULL
)
671 recptr
= mdp
->d_hints
;
672 bufend
= recptr
+ mdp
->d_hintsz
;
675 while (recptr
< bufend
&& !found
) {
683 if (clen
!= modnamelen
|| bcmp(cp
, modname
, clen
) != 0)
686 INT_ALIGN(mdp
->d_hints
, cp
);
690 if (verinfo
== NULL
|| ival
== verinfo
->md_ver_preferred
) {
694 if (ival
>= verinfo
->md_ver_minimum
&&
695 ival
<= verinfo
->md_ver_maximum
&&
705 recptr
+= reclen
+ sizeof(int);
708 * Finally check if KLD is in the place
711 result
= file_lookup(mdp
->d_path
, cp
, clen
, NULL
);
713 result
= file_lookup(mdp
->d_path
, best
, blen
, NULL
);
716 * If nothing found or hints is absent - fallback to the old way
717 * by using "kldname[.ko]" as module name.
719 if (!found
&& !bestver
&& result
== NULL
)
720 result
= file_lookup(mdp
->d_path
, modname
, modnamelen
, kld_ext_list
);
725 * Attempt to locate the file containing the module (name)
728 mod_searchmodule(char *name
, struct mod_depend
*verinfo
)
730 struct moduledir
*mdp
;
735 * Now we ready to lookup module in the given directories
738 STAILQ_FOREACH(mdp
, &moduledir_list
, d_link
) {
739 result
= mod_search_hints(mdp
, name
, verinfo
);
748 file_addmodule(struct preloaded_file
*fp
, char *modname
, int version
,
749 struct kernel_module
**newmp
)
751 struct kernel_module
*mp
;
752 struct mod_depend mdepend
;
754 bzero(&mdepend
, sizeof(mdepend
));
755 mdepend
.md_ver_preferred
= version
;
756 mp
= file_findmodule(fp
, modname
, &mdepend
);
759 mp
= malloc(sizeof(struct kernel_module
));
762 bzero(mp
, sizeof(struct kernel_module
));
763 mp
->m_name
= strdup(modname
);
764 mp
->m_version
= version
;
766 mp
->m_next
= fp
->f_modules
;
777 file_discard(struct preloaded_file
*fp
)
779 struct file_metadata
*md
, *md1
;
780 struct kernel_module
*mp
, *mp1
;
797 if (fp
->f_name
!= NULL
)
799 if (fp
->f_type
!= NULL
)
801 if (fp
->f_args
!= NULL
)
807 * Allocate a new file; must be used instead of malloc()
808 * to ensure safe initialisation.
810 struct preloaded_file
*
813 struct preloaded_file
*fp
;
815 if ((fp
= malloc(sizeof(struct preloaded_file
))) != NULL
) {
816 bzero(fp
, sizeof(struct preloaded_file
));
822 * Add a module to the chain
825 file_insert_tail(struct preloaded_file
*fp
)
827 struct preloaded_file
*cm
;
829 /* Append to list of loaded file */
831 if (preloaded_files
== NULL
) {
832 preloaded_files
= fp
;
834 for (cm
= preloaded_files
; cm
->f_next
!= NULL
; cm
= cm
->f_next
)
841 moduledir_fullpath(struct moduledir
*mdp
, const char *fname
)
845 cp
= malloc(strlen(mdp
->d_path
) + strlen(fname
) + 2);
848 strcpy(cp
, mdp
->d_path
);
855 * Read linker.hints file into memory performing some sanity checks.
858 moduledir_readhints(struct moduledir
*mdp
)
862 int fd
, size
, version
;
864 if (mdp
->d_hints
!= NULL
|| (mdp
->d_flags
& MDIR_NOHINTS
))
866 path
= moduledir_fullpath(mdp
, "linker.hints");
867 if (stat(path
, &st
) != 0 || st
.st_size
< (sizeof(version
) + sizeof(int)) ||
868 st
.st_size
> 100 * 1024 || (fd
= open(path
, O_RDONLY
)) < 0) {
870 mdp
->d_flags
|= MDIR_NOHINTS
;
874 size
= read(fd
, &version
, sizeof(version
));
875 if (size
!= sizeof(version
) || version
!= LINKER_HINTS_VERSION
)
877 size
= st
.st_size
- size
;
878 mdp
->d_hints
= malloc(size
);
879 if (mdp
->d_hints
== NULL
)
881 if (read(fd
, mdp
->d_hints
, size
) != size
)
883 mdp
->d_hintsz
= size
;
892 mdp
->d_flags
|= MDIR_NOHINTS
;
897 * Extract directories from the ';' separated list, remove duplicates.
900 moduledir_rebuild(void)
902 struct moduledir
*mdp
, *mtmp
;
903 const char *path
, *cp
, *ep
;
906 path
= getenv("module_path");
908 path
= default_searchpath
;
910 * Rebuild list of module directories if it changed
912 STAILQ_FOREACH(mdp
, &moduledir_list
, d_link
)
913 mdp
->d_flags
|= MDIR_REMOVED
;
915 for (ep
= path
; *ep
!= 0; ep
++) {
917 for (; *ep
!= 0 && *ep
!= ';'; ep
++)
920 * Ignore trailing slashes
922 for (cplen
= ep
- cp
; cplen
> 1 && cp
[cplen
- 1] == '/'; cplen
--)
924 STAILQ_FOREACH(mdp
, &moduledir_list
, d_link
) {
925 if (strlen(mdp
->d_path
) != cplen
|| bcmp(cp
, mdp
->d_path
, cplen
) != 0)
927 mdp
->d_flags
&= ~MDIR_REMOVED
;
931 mdp
= malloc(sizeof(*mdp
) + cplen
+ 1);
934 mdp
->d_path
= (char*)(mdp
+ 1);
935 bcopy(cp
, mdp
->d_path
, cplen
);
936 mdp
->d_path
[cplen
] = 0;
939 STAILQ_INSERT_TAIL(&moduledir_list
, mdp
, d_link
);
945 * Delete unused directories if any
947 mdp
= STAILQ_FIRST(&moduledir_list
);
949 if ((mdp
->d_flags
& MDIR_REMOVED
) == 0) {
950 mdp
= STAILQ_NEXT(mdp
, d_link
);
955 mdp
= STAILQ_NEXT(mdp
, d_link
);
956 STAILQ_REMOVE(&moduledir_list
, mtmp
, moduledir
, d_link
);