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
30 * file/module function dispatcher, support, etc.
35 #include <sys/param.h>
36 #include <sys/linker.h>
37 #include <sys/module.h>
38 #include <sys/queue.h>
40 #include "bootstrap.h"
42 #define MDIR_REMOVED 0x0001
43 #define MDIR_NOHINTS 0x0002
46 char *d_path; /* path of modules directory */
47 u_char *d_hints; /* content of linker.hints file */
48 int d_hintsz; /* size of hints data */
50 STAILQ_ENTRY(moduledir) d_link;
53 static int file_load(char *filename, vm_offset_t dest, struct preloaded_file **result);
54 static int file_loadraw(char *type, char *name);
55 static int file_load_dependencies(struct preloaded_file *base_mod);
56 static char * file_search(const char *name, char **extlist);
57 static struct kernel_module * file_findmodule(struct preloaded_file *fp, char *modname, struct mod_depend *verinfo);
58 static int file_havepath(const char *name);
59 static char *mod_searchmodule(char *name, struct mod_depend *verinfo);
60 static void file_insert_tail(struct preloaded_file *mp);
61 struct file_metadata* metadata_next(struct file_metadata *base_mp, int type);
62 static void moduledir_readhints(struct moduledir *mdp);
63 static void moduledir_rebuild(void);
65 /* load address should be tweaked by first module loaded (kernel) */
66 static vm_offset_t loadaddr = 0;
68 static const char *default_searchpath ="/boot/kernel;/boot/modules;/modules";
70 static STAILQ_HEAD(, moduledir) moduledir_list = STAILQ_HEAD_INITIALIZER(moduledir_list);
72 struct preloaded_file *preloaded_files = NULL;
74 static char *kld_ext_list[] = {
82 * load an object, either a disk file or code module.
84 * To load a file, the syntax is:
86 * load -t <type> <path>
88 * code modules are loaded as:
90 * load <path> <options>
93 COMMAND_SET(load, "load", "load a kernel or module", command_load);
96 command_load(int argc, char *argv[])
99 int dofile, dokld, ch, error;
106 command_errmsg = "no filename specified";
109 while ((ch = getopt(argc, argv, "kt:")) != -1) {
120 /* getopt has already reported an error */
124 argv += (optind - 1);
125 argc -= (optind - 1);
128 * Request to load a raw file?
131 if ((argc != 2) || (typestr == NULL) || (*typestr == 0)) {
132 command_errmsg = "invalid load type";
135 return(file_loadraw(typestr, argv[1]));
138 * Do we have explicit KLD load ?
140 if (dokld || file_havepath(argv[1])) {
141 error = mod_loadkld(argv[1], argc - 2, argv + 2);
143 sprintf(command_errbuf, "warning: KLD '%s' already loaded", argv[1]);
144 return (error == 0 ? CMD_OK : CMD_ERROR);
147 * Looks like a request for a module.
149 error = mod_load(argv[1], NULL, argc - 2, argv + 2);
151 sprintf(command_errbuf, "warning: module '%s' already loaded", argv[1]);
152 return (error == 0 ? CMD_OK : CMD_ERROR);
155 COMMAND_SET(unload, "unload", "unload all modules", command_unload);
158 command_unload(int argc, char *argv[])
160 struct preloaded_file *fp;
162 while (preloaded_files != NULL) {
163 fp = preloaded_files;
164 preloaded_files = preloaded_files->f_next;
168 unsetenv("kernelname");
172 COMMAND_SET(lsmod, "lsmod", "list loaded modules", command_lsmod);
175 command_lsmod(int argc, char *argv[])
177 struct preloaded_file *fp;
178 struct kernel_module *mp;
179 struct file_metadata *md;
186 while ((ch = getopt(argc, argv, "v")) != -1) {
193 /* getopt has already reported an error */
199 for (fp = preloaded_files; fp; fp = fp->f_next) {
200 sprintf(lbuf, " %p: %s (%s, 0x%lx)\n",
201 (void *) fp->f_addr, fp->f_name, fp->f_type, (long) fp->f_size);
203 if (fp->f_args != NULL) {
204 pager_output(" args: ");
205 pager_output(fp->f_args);
209 pager_output(" modules: ");
210 for (mp = fp->f_modules; mp; mp = mp->m_next) {
211 sprintf(lbuf, "%s.%d ", mp->m_name, mp->m_version);
217 /* XXX could add some formatting smarts here to display some better */
218 for (md = fp->f_metadata; md != NULL; md = md->md_next) {
219 sprintf(lbuf, " 0x%04x, 0x%lx\n", md->md_type, (long) md->md_size);
229 * File level interface, functions file_*
232 file_load(char *filename, vm_offset_t dest, struct preloaded_file **result)
234 struct preloaded_file *fp;
239 for (i = 0, fp = NULL; file_formats[i] && fp == NULL; i++) {
240 error = (file_formats[i]->l_load)(filename, loadaddr, &fp);
242 fp->f_loader = i; /* remember the loader */
247 continue; /* Unknown to this handler? */
249 sprintf(command_errbuf, "can't load file '%s': %s",
250 filename, strerror(error));
258 file_load_dependencies(struct preloaded_file *base_file) {
259 struct file_metadata *md;
260 struct preloaded_file *fp;
261 struct mod_depend *verinfo;
262 struct kernel_module *mp;
266 md = file_findmetadata(base_file, MODINFOMD_DEPLIST);
271 verinfo = (struct mod_depend*)md->md_data;
272 dmodname = (char *)(verinfo + 1);
273 if (file_findmodule(NULL, dmodname, verinfo) == NULL) {
274 printf("loading required module '%s'\n", dmodname);
275 error = mod_load(dmodname, verinfo, 0, NULL);
279 * If module loaded via kld name which isn't listed
280 * in the linker.hints file, we should check if it have
283 mp = file_findmodule(NULL, dmodname, verinfo);
285 sprintf(command_errbuf, "module '%s' exists but with wrong version",
291 md = metadata_next(md, MODINFOMD_DEPLIST);
295 /* Load failed; discard everything */
296 while (base_file != NULL) {
298 base_file = base_file->f_next;
304 * We've been asked to load (name) as (type), so just suck it in,
305 * no arguments or anything.
308 file_loadraw(char *type, char *name)
310 struct preloaded_file *fp;
315 /* We can't load first */
316 if ((file_findfile(NULL, NULL)) == NULL) {
317 command_errmsg = "can't load file before kernel";
321 /* locate the file on the load path */
322 cp = file_search(name, NULL);
324 sprintf(command_errbuf, "can't find '%s'", name);
329 if ((fd = open(name, O_RDONLY)) < 0) {
330 sprintf(command_errbuf, "can't open '%s': %s", name, strerror(errno));
337 /* read in 4k chunks; size is not really important */
338 got = archsw.arch_readin(fd, laddr, 4096);
339 if (got == 0) /* end of file */
341 if (got < 0) { /* error */
342 sprintf(command_errbuf, "error reading '%s': %s", name, strerror(errno));
350 /* Looks OK so far; create & populate control structure */
353 fp->f_type = strdup(type);
355 fp->f_metadata = NULL;
357 fp->f_addr = loadaddr;
358 fp->f_size = laddr - loadaddr;
360 /* recognise space consumption */
363 /* Add to the list of loaded files */
364 file_insert_tail(fp);
370 * Load the module (name), pass it (argc),(argv), add container file
371 * to the list of loaded files.
372 * If module is already loaded just assign new argc/argv.
375 mod_load(char *modname, struct mod_depend *verinfo, int argc, char *argv[])
377 struct kernel_module *mp;
381 if (file_havepath(modname)) {
382 printf("Warning: mod_load() called instead of mod_loadkld() for module '%s'\n", modname);
383 return (mod_loadkld(modname, argc, argv));
385 /* see if module is already loaded */
386 mp = file_findmodule(NULL, modname, verinfo);
391 mp->m_args = unargv(argc, argv);
393 sprintf(command_errbuf, "warning: module '%s' already loaded", mp->m_name);
396 /* locate file with the module on the search path */
397 filename = mod_searchmodule(modname, verinfo);
398 if (filename == NULL) {
399 sprintf(command_errbuf, "can't find '%s'", modname);
402 err = mod_loadkld(filename, argc, argv);
407 * Load specified KLD. If path is omitted, then try to locate it via
411 mod_loadkld(const char *kldname, int argc, char *argv[])
413 struct preloaded_file *fp, *last_file;
418 * Get fully qualified KLD name
420 filename = file_search(kldname, kld_ext_list);
421 if (filename == NULL) {
422 sprintf(command_errbuf, "can't find '%s'", kldname);
426 * Check if KLD already loaded
428 fp = file_findfile(filename, NULL);
430 sprintf(command_errbuf, "warning: KLD '%s' already loaded", filename);
434 for (last_file = preloaded_files;
435 last_file != NULL && last_file->f_next != NULL;
436 last_file = last_file->f_next)
440 err = file_load(filename, loadaddr, &fp);
443 fp->f_args = unargv(argc, argv);
444 loadaddr = fp->f_addr + fp->f_size;
445 file_insert_tail(fp); /* Add to the list of loaded files */
446 if (file_load_dependencies(fp) != 0) {
448 last_file->f_next = NULL;
449 loadaddr = last_file->f_addr + last_file->f_size;
455 sprintf(command_errbuf, "don't know how to load module '%s'", filename);
463 * Find a file matching (name) and (type).
464 * NULL may be passed as a wildcard to either.
466 struct preloaded_file *
467 file_findfile(char *name, char *type)
469 struct preloaded_file *fp;
471 for (fp = preloaded_files; fp != NULL; fp = fp->f_next) {
472 if (((name == NULL) || !strcmp(name, fp->f_name)) &&
473 ((type == NULL) || !strcmp(type, fp->f_type)))
480 * Find a module matching (name) inside of given file.
481 * NULL may be passed as a wildcard.
483 struct kernel_module *
484 file_findmodule(struct preloaded_file *fp, char *modname,
485 struct mod_depend *verinfo)
487 struct kernel_module *mp, *best;
491 for (fp = preloaded_files; fp; fp = fp->f_next) {
492 mp = file_findmodule(fp, modname, verinfo);
500 for (mp = fp->f_modules; mp; mp = mp->m_next) {
501 if (strcmp(modname, mp->m_name) == 0) {
504 mver = mp->m_version;
505 if (mver == verinfo->md_ver_preferred)
507 if (mver >= verinfo->md_ver_minimum &&
508 mver <= verinfo->md_ver_maximum &&
518 * Make a copy of (size) bytes of data from (p), and associate them as
519 * metadata of (type) to the module (mp).
522 file_addmetadata(struct preloaded_file *fp, int type, size_t size, void *p)
524 struct file_metadata *md;
526 md = malloc(sizeof(struct file_metadata) - sizeof(md->md_data) + size);
529 bcopy(p, md->md_data, size);
530 md->md_next = fp->f_metadata;
535 * Find a metadata object of (type) associated with the file (fp)
537 struct file_metadata *
538 file_findmetadata(struct preloaded_file *fp, int type)
540 struct file_metadata *md;
542 for (md = fp->f_metadata; md != NULL; md = md->md_next)
543 if (md->md_type == type)
548 struct file_metadata *
549 metadata_next(struct file_metadata *md, int type)
553 while((md = md->md_next) != NULL)
554 if (md->md_type == type)
559 static char *emptyextlist[] = { "", NULL };
562 * Check if the given file is in place and return full path to it.
565 file_lookup(const char *path, const char *name, int namelen, char **extlist)
568 char *result, *cp, **cpp;
569 int pathlen, extlen, len;
571 pathlen = strlen(path);
574 extlist = emptyextlist;
575 for (cpp = extlist; *cpp; cpp++) {
580 result = malloc(pathlen + namelen + extlen + 2);
583 bcopy(path, result, pathlen);
584 if (pathlen > 0 && result[pathlen - 1] != '/')
585 result[pathlen++] = '/';
586 cp = result + pathlen;
587 bcopy(name, cp, namelen);
589 for (cpp = extlist; *cpp; cpp++) {
591 if (stat(result, &st) == 0 && S_ISREG(st.st_mode))
599 * Check if file name have any qualifiers
602 file_havepath(const char *name)
606 archsw.arch_getdev(NULL, name, &cp);
607 return (cp != name || strchr(name, '/') != NULL);
611 * Attempt to find the file (name) on the module searchpath.
612 * If (name) is qualified in any way, we simply check it and
613 * return it or NULL. If it is not qualified, then we attempt
614 * to construct a path using entries in the environment variable
617 * The path we return a pointer to need never be freed, as we manage
621 file_search(const char *name, char **extlist)
623 struct moduledir *mdp;
628 /* Don't look for nothing */
633 return(strdup(name));
635 if (file_havepath(name)) {
636 /* Qualified, so just see if it exists */
637 if (stat(name, &sb) == 0)
638 return(strdup(name));
643 namelen = strlen(name);
644 STAILQ_FOREACH(mdp, &moduledir_list, d_link) {
645 result = file_lookup(mdp->d_path, name, namelen, extlist);
652 #define INT_ALIGN(base, ptr) ptr = \
653 (base) + (((ptr) - (base) + sizeof(int) - 1) & ~(sizeof(int) - 1))
656 mod_search_hints(struct moduledir *mdp, const char *modname,
657 struct mod_depend *verinfo)
659 u_char *cp, *recptr, *bufend, *best;
661 int *intp, bestver, blen, clen, found, ival, modnamelen, reclen;
663 moduledir_readhints(mdp);
664 modnamelen = strlen(modname);
668 if (mdp->d_hints == NULL)
670 recptr = mdp->d_hints;
671 bufend = recptr + mdp->d_hintsz;
674 while (recptr < bufend && !found) {
682 if (clen != modnamelen || bcmp(cp, modname, clen) != 0)
685 INT_ALIGN(mdp->d_hints, cp);
689 if (verinfo == NULL || ival == verinfo->md_ver_preferred) {
693 if (ival >= verinfo->md_ver_minimum &&
694 ival <= verinfo->md_ver_maximum &&
704 recptr += reclen + sizeof(int);
707 * Finally check if KLD is in the place
710 result = file_lookup(mdp->d_path, cp, clen, NULL);
712 result = file_lookup(mdp->d_path, best, blen, NULL);
715 * If nothing found or hints is absent - fallback to the old way
716 * by using "kldname[.ko]" as module name.
718 if (!found && !bestver && result == NULL)
719 result = file_lookup(mdp->d_path, modname, modnamelen, kld_ext_list);
724 * Attempt to locate the file containing the module (name)
727 mod_searchmodule(char *name, struct mod_depend *verinfo)
729 struct moduledir *mdp;
734 * Now we ready to lookup module in the given directories
737 STAILQ_FOREACH(mdp, &moduledir_list, d_link) {
738 result = mod_search_hints(mdp, name, verinfo);
747 file_addmodule(struct preloaded_file *fp, char *modname, int version,
748 struct kernel_module **newmp)
750 struct kernel_module *mp;
751 struct mod_depend mdepend;
753 bzero(&mdepend, sizeof(mdepend));
754 mdepend.md_ver_preferred = version;
755 mp = file_findmodule(fp, modname, &mdepend);
758 mp = malloc(sizeof(struct kernel_module));
761 bzero(mp, sizeof(struct kernel_module));
762 mp->m_name = strdup(modname);
763 mp->m_version = version;
765 mp->m_next = fp->f_modules;
776 file_discard(struct preloaded_file *fp)
778 struct file_metadata *md, *md1;
779 struct kernel_module *mp, *mp1;
796 if (fp->f_name != NULL)
798 if (fp->f_type != NULL)
800 if (fp->f_args != NULL)
806 * Allocate a new file; must be used instead of malloc()
807 * to ensure safe initialisation.
809 struct preloaded_file *
812 struct preloaded_file *fp;
814 if ((fp = malloc(sizeof(struct preloaded_file))) != NULL) {
815 bzero(fp, sizeof(struct preloaded_file));
821 * Add a module to the chain
824 file_insert_tail(struct preloaded_file *fp)
826 struct preloaded_file *cm;
828 /* Append to list of loaded file */
830 if (preloaded_files == NULL) {
831 preloaded_files = fp;
833 for (cm = preloaded_files; cm->f_next != NULL; cm = cm->f_next)
840 moduledir_fullpath(struct moduledir *mdp, const char *fname)
844 cp = malloc(strlen(mdp->d_path) + strlen(fname) + 2);
847 strcpy(cp, mdp->d_path);
854 * Read linker.hints file into memory performing some sanity checks.
857 moduledir_readhints(struct moduledir *mdp)
861 int fd, size, version;
863 if (mdp->d_hints != NULL || (mdp->d_flags & MDIR_NOHINTS))
865 path = moduledir_fullpath(mdp, "linker.hints");
866 if (stat(path, &st) != 0 || st.st_size < (sizeof(version) + sizeof(int)) ||
867 st.st_size > 100 * 1024 || (fd = open(path, O_RDONLY)) < 0) {
869 mdp->d_flags |= MDIR_NOHINTS;
873 size = read(fd, &version, sizeof(version));
874 if (size != sizeof(version) || version != LINKER_HINTS_VERSION)
876 size = st.st_size - size;
877 mdp->d_hints = malloc(size);
878 if (mdp->d_hints == NULL)
880 if (read(fd, mdp->d_hints, size) != size)
882 mdp->d_hintsz = size;
891 mdp->d_flags |= MDIR_NOHINTS;
896 * Extract directories from the ';' separated list, remove duplicates.
899 moduledir_rebuild(void)
901 struct moduledir *mdp, *mtmp;
902 const char *path, *cp, *ep;
905 path = getenv("module_path");
907 path = default_searchpath;
909 * Rebuild list of module directories if it changed
911 STAILQ_FOREACH(mdp, &moduledir_list, d_link)
912 mdp->d_flags |= MDIR_REMOVED;
914 for (ep = path; *ep != 0; ep++) {
916 for (; *ep != 0 && *ep != ';'; ep++)
919 * Ignore trailing slashes
921 for (cplen = ep - cp; cplen > 1 && cp[cplen - 1] == '/'; cplen--)
923 STAILQ_FOREACH(mdp, &moduledir_list, d_link) {
924 if (strlen(mdp->d_path) != cplen || bcmp(cp, mdp->d_path, cplen) != 0)
926 mdp->d_flags &= ~MDIR_REMOVED;
930 mdp = malloc(sizeof(*mdp) + cplen + 1);
933 mdp->d_path = (char*)(mdp + 1);
934 bcopy(cp, mdp->d_path, cplen);
935 mdp->d_path[cplen] = 0;
938 STAILQ_INSERT_TAIL(&moduledir_list, mdp, d_link);
944 * Delete unused directories if any
946 mdp = STAILQ_FIRST(&moduledir_list);
948 if ((mdp->d_flags & MDIR_REMOVED) == 0) {
949 mdp = STAILQ_NEXT(mdp, d_link);
954 mdp = STAILQ_NEXT(mdp, d_link);
955 STAILQ_REMOVE(&moduledir_list, mtmp, moduledir, d_link);