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 $
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 "libi386/libi386.h"
43 #include "bootstrap.h"
45 #define MDIR_REMOVED 0x0001
46 #define MDIR_NOHINTS 0x0002
49 char *d_path; /* path of modules directory */
50 u_char *d_hints; /* content of linker.hints file */
51 int d_hintsz; /* size of hints data */
53 STAILQ_ENTRY(moduledir) d_link;
56 static int file_load(char *filename, vm_offset_t dest, struct preloaded_file **result);
57 static int file_loadraw(char *type, char *name);
58 static int file_load_dependencies(struct preloaded_file *base_mod);
59 static char * file_search(const char *name, char **extlist);
60 static struct kernel_module * file_findmodule(struct preloaded_file *fp, char *modname, struct mod_depend *verinfo);
61 static int file_havepath(const char *name);
62 static char *mod_searchmodule(char *name, struct mod_depend *verinfo);
63 static void file_insert_tail(struct preloaded_file *mp);
64 struct file_metadata* metadata_next(struct file_metadata *base_mp, int type);
65 static void moduledir_readhints(struct moduledir *mdp);
66 static void moduledir_rebuild(void);
68 /* load address should be tweaked by first module loaded (kernel) */
69 static vm_offset_t loadaddr = 0;
71 static const char *default_searchpath ="modules;KERNEL";
72 static const char *local_module_path = "../modules.local";
74 static STAILQ_HEAD(, moduledir) moduledir_list = STAILQ_HEAD_INITIALIZER(moduledir_list);
76 struct preloaded_file *preloaded_files = NULL;
78 static char *kld_ext_list[] = {
84 COMMAND_SET(slow, "slow", "slow print", command_slow);
89 command_slow(int argc, char *argv[])
101 printf("SP-%c\n", c);
102 for (i = 0; i < 10; ++i)
108 * load an object, either a disk file or code module.
110 * To load a file, the syntax is:
112 * load -t <type> <path>
114 * code modules are loaded as:
116 * load <path> <options>
118 COMMAND_SET(load, "load", "load a kernel or module", command_load);
121 command_load(int argc, char *argv[])
124 int dofile, dokld, ch, error;
131 command_errmsg = "no filename specified";
134 while ((ch = getopt(argc, argv, "kt:")) != -1) {
145 /* getopt has already reported an error */
149 argv += (optind - 1);
150 argc -= (optind - 1);
153 * Request to load a raw file?
156 if ((argc != 2) || (typestr == NULL) || (*typestr == 0)) {
157 command_errmsg = "invalid load type";
160 return(file_loadraw(typestr, argv[1]));
163 * Do we have explicit KLD load ?
165 if (dokld || file_havepath(argv[1])) {
166 error = mod_loadkld(argv[1], argc - 2, argv + 2);
168 sprintf(command_errbuf, "warning: KLD '%s' already loaded", argv[1]);
169 return (error == 0 ? CMD_OK : CMD_ERROR);
172 * Looks like a request for a module.
174 error = mod_load(argv[1], NULL, argc - 2, argv + 2);
176 sprintf(command_errbuf, "warning: module '%s' already loaded", argv[1]);
177 return (error == 0 ? CMD_OK : CMD_ERROR);
180 COMMAND_SET(unload, "unload", "unload all modules", command_unload);
183 command_unload(int argc, char *argv[])
185 struct preloaded_file *fp;
187 while (preloaded_files != NULL) {
188 fp = preloaded_files;
189 preloaded_files = preloaded_files->f_next;
193 unsetenv("kernelname");
197 COMMAND_SET(crc, "crc", "calculate crc for file", command_crc);
199 uint32_t iscsi_crc32(const void *buf, size_t size);
200 uint32_t iscsi_crc32_ext(const void *buf, size_t size, uint32_t ocrc);
203 command_crc(int argc, char *argv[])
214 command_errmsg = "no filename specified";
220 printf("size\tcrc\t name\n");
221 for (i = 1; i < argc; ++i) {
222 /* locate the file on the load path */
223 cp = file_search(argv[i], NULL);
225 sprintf(command_errbuf, "can't find '%s'", argv[i]);
231 if ((fd = rel_open(name, NULL, O_RDONLY)) < 0) {
232 sprintf(command_errbuf,
233 "can't open '%s': %s",
234 name, strerror(errno));
242 got = read(fd, buf, 8192);
246 printf("error reading '%s': %s\n",
247 name, strerror(errno));
251 crc = iscsi_crc32(buf, got);
253 crc = iscsi_crc32_ext(buf, got, crc);
256 printf("%7d %08x %s\n", tot, crc, name);
266 COMMAND_SET(lsmod, "lsmod", "list loaded modules", command_lsmod);
269 command_lsmod(int argc, char *argv[])
271 struct preloaded_file *fp;
272 struct kernel_module *mp;
273 struct file_metadata *md;
280 while ((ch = getopt(argc, argv, "v")) != -1) {
287 /* getopt has already reported an error */
293 for (fp = preloaded_files; fp; fp = fp->f_next) {
294 sprintf(lbuf, " %p: %s (%s, 0x%lx)\n",
295 (void *) fp->f_addr, fp->f_name, fp->f_type, (long) fp->f_size);
297 if (fp->f_args != NULL) {
298 pager_output(" args: ");
299 pager_output(fp->f_args);
303 pager_output(" modules: ");
304 for (mp = fp->f_modules; mp; mp = mp->m_next) {
305 sprintf(lbuf, "%s.%d ", mp->m_name, mp->m_version);
311 /* XXX could add some formatting smarts here to display some better */
312 for (md = fp->f_metadata; md != NULL; md = md->md_next) {
313 sprintf(lbuf, " 0x%04x, 0x%lx\n", md->md_type, (long) md->md_size);
323 * File level interface, functions file_*
326 file_load(char *filename, vm_offset_t dest, struct preloaded_file **result)
328 struct preloaded_file *fp;
333 for (i = 0, fp = NULL; file_formats[i] && fp == NULL; i++) {
334 error = (file_formats[i]->l_load)(filename, loadaddr, &fp);
336 fp->f_loader = i; /* remember the loader */
341 continue; /* Unknown to this handler? */
343 sprintf(command_errbuf, "can't load file '%s': %s",
344 filename, strerror(error));
352 file_load_dependencies(struct preloaded_file *base_file)
354 struct file_metadata *md;
355 struct preloaded_file *fp;
356 struct mod_depend *verinfo;
357 struct kernel_module *mp;
361 md = file_findmetadata(base_file, MODINFOMD_DEPLIST);
366 verinfo = (struct mod_depend*)md->md_data;
367 dmodname = (char *)(verinfo + 1);
368 if (file_findmodule(NULL, dmodname, verinfo) == NULL) {
369 printf("loading required module '%s'\n", dmodname);
370 error = mod_load(dmodname, verinfo, 0, NULL);
374 * If module loaded via kld name which isn't listed
375 * in the linker.hints file, we should check if it have
378 mp = file_findmodule(NULL, dmodname, verinfo);
380 sprintf(command_errbuf, "module '%s' exists but with wrong version",
386 md = metadata_next(md, MODINFOMD_DEPLIST);
390 /* Load failed; discard everything */
391 while (base_file != NULL) {
393 base_file = base_file->f_next;
400 * We've been asked to load (name) as (type), so just suck it in,
401 * no arguments or anything.
404 file_loadraw(char *type, char *name)
406 struct preloaded_file *fp;
411 /* We can't load first */
412 if ((file_findfile(NULL, NULL)) == NULL) {
413 command_errmsg = "can't load file before kernel";
417 /* locate the file on the load path */
418 cp = file_search(name, NULL);
420 sprintf(command_errbuf, "can't find '%s'", name);
425 if ((fd = rel_open(name, NULL, O_RDONLY)) < 0) {
426 sprintf(command_errbuf, "can't open '%s': %s", name, strerror(errno));
433 /* read in 4k chunks; size is not really important */
435 if (laddr + 4096 > heapbase) {
436 sprintf(command_errbuf, "error reading '%s': out of load memory",
443 got = archsw.arch_readin(fd, laddr, 4096);
444 if (got == 0) /* end of file */
446 if (got < 0) { /* error */
447 sprintf(command_errbuf, "error reading '%s': %s",
448 name, strerror(errno));
456 /* Looks OK so far; create & populate control structure */
459 fp->f_type = strdup(type);
461 fp->f_metadata = NULL;
463 fp->f_addr = loadaddr;
464 fp->f_size = laddr - loadaddr;
466 /* recognise space consumption */
469 /* Add to the list of loaded files */
470 file_insert_tail(fp);
476 * Load the module (name), pass it (argc),(argv), add container file
477 * to the list of loaded files.
478 * If module is already loaded just assign new argc/argv.
481 mod_load(char *modname, struct mod_depend *verinfo, int argc, char *argv[])
483 struct kernel_module *mp;
487 if (file_havepath(modname)) {
488 printf("Warning: mod_load() called instead of mod_loadkld() for module '%s'\n", modname);
489 return (mod_loadkld(modname, argc, argv));
491 /* see if module is already loaded */
492 mp = file_findmodule(NULL, modname, verinfo);
497 mp->m_args = unargv(argc, argv);
499 sprintf(command_errbuf, "warning: module '%s' already loaded", mp->m_name);
502 /* locate file with the module on the search path */
503 filename = mod_searchmodule(modname, verinfo);
504 if (filename == NULL) {
505 sprintf(command_errbuf, "can't find '%s'", modname);
508 err = mod_loadkld(filename, argc, argv);
513 * Load specified KLD. If path is omitted, then try to locate it via
517 mod_loadkld(const char *kldname, int argc, char *argv[])
519 struct preloaded_file *fp, *last_file;
524 * Get fully qualified KLD name
526 filename = file_search(kldname, kld_ext_list);
527 if (filename == NULL) {
528 sprintf(command_errbuf, "can't find '%s'", kldname);
532 * Check if KLD already loaded
534 fp = file_findfile(filename, NULL);
536 sprintf(command_errbuf, "warning: KLD '%s' already loaded", filename);
540 for (last_file = preloaded_files;
541 last_file != NULL && last_file->f_next != NULL;
542 last_file = last_file->f_next)
546 err = file_load(filename, loadaddr, &fp);
549 fp->f_args = unargv(argc, argv);
550 loadaddr = fp->f_addr + fp->f_size;
551 file_insert_tail(fp); /* Add to the list of loaded files */
552 if (file_load_dependencies(fp) != 0) {
554 last_file->f_next = NULL;
555 loadaddr = last_file->f_addr + last_file->f_size;
561 sprintf(command_errbuf, "don't know how to load module '%s'", filename);
569 * Find a file matching (name) and (type).
570 * NULL may be passed as a wildcard to either.
572 struct preloaded_file *
573 file_findfile(char *name, char *type)
575 struct preloaded_file *fp;
577 for (fp = preloaded_files; fp != NULL; fp = fp->f_next) {
578 if (((name == NULL) || !strcmp(name, fp->f_name)) &&
579 ((type == NULL) || !strcmp(type, fp->f_type)))
586 * Find a module matching (name) inside of given file.
587 * NULL may be passed as a wildcard.
589 struct kernel_module *
590 file_findmodule(struct preloaded_file *fp, char *modname,
591 struct mod_depend *verinfo)
593 struct kernel_module *mp, *best;
597 for (fp = preloaded_files; fp; fp = fp->f_next) {
598 mp = file_findmodule(fp, modname, verinfo);
606 for (mp = fp->f_modules; mp; mp = mp->m_next) {
607 if (strcmp(modname, mp->m_name) == 0) {
610 mver = mp->m_version;
611 if (mver == verinfo->md_ver_preferred)
613 if (mver >= verinfo->md_ver_minimum &&
614 mver <= verinfo->md_ver_maximum &&
624 * Make a copy of (size) bytes of data from (p), and associate them as
625 * metadata of (type) to the module (mp).
628 file_addmetadata(struct preloaded_file *fp, int type, size_t size, void *p)
630 struct file_metadata *md;
632 md = malloc(sizeof(struct file_metadata) - sizeof(md->md_data) + size);
635 bcopy(p, md->md_data, size);
636 md->md_next = fp->f_metadata;
641 * Find a metadata object of (type) associated with the file (fp)
643 struct file_metadata *
644 file_findmetadata(struct preloaded_file *fp, int type)
646 struct file_metadata *md;
648 for (md = fp->f_metadata; md != NULL; md = md->md_next)
649 if (md->md_type == type)
654 struct file_metadata *
655 metadata_next(struct file_metadata *md, int type)
659 while((md = md->md_next) != NULL)
660 if (md->md_type == type)
665 static char *emptyextlist[] = { "", NULL };
668 * Check if the given file is in place and return full path to it.
671 file_lookup(const char *path, const char *name, int namelen, char **extlist)
674 char *result, *cp, **cpp;
675 int pathlen, extlen, len;
677 pathlen = strlen(path);
680 extlist = emptyextlist;
681 for (cpp = extlist; *cpp; cpp++) {
686 result = malloc(pathlen + namelen + extlen + 2 + 7 + 1);
689 bcopy(path, result, pathlen);
690 if (pathlen > 0 && result[pathlen - 1] != '/')
691 result[pathlen++] = '/';
692 cp = result + pathlen;
693 bcopy(name, cp, namelen);
695 for (cpp = extlist; *cpp; cpp++) {
697 if (rel_stat(result, &st) == 0) {
698 if (S_ISREG(st.st_mode)) {
700 } else if (S_ISDIR(st.st_mode)) {
701 strcat(result, "/kernel");
702 if (rel_stat(result, &st) == 0 && S_ISREG(st.st_mode)) {
713 * Check if file name have any qualifiers
716 file_havepath(const char *name)
720 archsw.arch_getdev(NULL, name, &cp);
721 return (cp != name || strchr(name, '/') != NULL);
725 * Attempt to find the file (name) on the module searchpath.
726 * If (name) is qualified in any way, we simply check it and
727 * return it or NULL. If it is not qualified, then we attempt
728 * to construct a path using entries in the environment variable
731 * The path we return a pointer to need never be freed, as we manage
735 file_search(const char *name, char **extlist)
737 struct moduledir *mdp;
742 /* Don't look for nothing */
747 return(strdup(name));
750 * Qualified name. If it is a directory tag on
753 if (file_havepath(name)) {
754 /* Qualified, so just see if it exists */
755 if (rel_stat(name, &sb) == 0) {
756 if (S_ISDIR(sb.st_mode)) {
757 result = malloc(strlen(name) + 7 + 1);
758 sprintf(result, "%s/kernel", name);
761 return(strdup(name));
768 namelen = strlen(name);
769 STAILQ_FOREACH(mdp, &moduledir_list, d_link) {
770 result = file_lookup(mdp->d_path, name, namelen, extlist);
777 #define INT_ALIGN(base, ptr) \
778 ptr = (base) + roundup2((ptr) - (base), sizeof(int))
781 mod_search_hints(struct moduledir *mdp, const char *modname,
782 struct mod_depend *verinfo)
784 u_char *cp, *recptr, *bufend, *best;
786 int *intp, bestver, blen, clen, found, ival, modnamelen, reclen;
788 moduledir_readhints(mdp);
789 modnamelen = strlen(modname);
793 if (mdp->d_hints == NULL)
795 recptr = mdp->d_hints;
796 bufend = recptr + mdp->d_hintsz;
799 while (recptr < bufend && !found) {
807 if (clen != modnamelen || bcmp(cp, modname, clen) != 0)
810 INT_ALIGN(mdp->d_hints, cp);
814 if (verinfo == NULL || ival == verinfo->md_ver_preferred) {
818 if (ival >= verinfo->md_ver_minimum &&
819 ival <= verinfo->md_ver_maximum &&
829 recptr += reclen + sizeof(int);
832 * Finally check if KLD is in the place
835 result = file_lookup(mdp->d_path, cp, clen, NULL);
837 result = file_lookup(mdp->d_path, best, blen, NULL);
840 * If nothing found or hints is absent - fallback to the old way
841 * by using "kldname[.ko]" as module name.
843 if (!found && !bestver && result == NULL)
844 result = file_lookup(mdp->d_path, modname, modnamelen, kld_ext_list);
849 * Attempt to locate the file containing the module (name)
852 mod_searchmodule(char *name, struct mod_depend *verinfo)
854 struct moduledir *mdp;
859 * Now we ready to lookup module in the given directories
862 STAILQ_FOREACH(mdp, &moduledir_list, d_link) {
863 result = mod_search_hints(mdp, name, verinfo);
872 file_addmodule(struct preloaded_file *fp, char *modname, int version,
873 struct kernel_module **newmp)
875 struct kernel_module *mp;
876 struct mod_depend mdepend;
878 bzero(&mdepend, sizeof(mdepend));
879 mdepend.md_ver_preferred = version;
880 mp = file_findmodule(fp, modname, &mdepend);
883 mp = malloc(sizeof(struct kernel_module));
886 bzero(mp, sizeof(struct kernel_module));
887 mp->m_name = strdup(modname);
888 mp->m_version = version;
890 mp->m_next = fp->f_modules;
901 file_discard(struct preloaded_file *fp)
903 struct file_metadata *md, *md1;
904 struct kernel_module *mp, *mp1;
921 if (fp->f_name != NULL)
923 if (fp->f_type != NULL)
925 if (fp->f_args != NULL)
931 * Allocate a new file; must be used instead of malloc()
932 * to ensure safe initialisation.
934 struct preloaded_file *
937 struct preloaded_file *fp;
939 if ((fp = malloc(sizeof(struct preloaded_file))) != NULL) {
940 bzero(fp, sizeof(struct preloaded_file));
946 * Add a module to the chain
949 file_insert_tail(struct preloaded_file *fp)
951 struct preloaded_file *cm;
953 /* Append to list of loaded file */
955 if (preloaded_files == NULL) {
956 preloaded_files = fp;
958 for (cm = preloaded_files; cm->f_next != NULL; cm = cm->f_next)
965 moduledir_fullpath(struct moduledir *mdp, const char *fname)
969 cp = malloc(strlen(mdp->d_path) + strlen(fname) + 2);
972 strcpy(cp, mdp->d_path);
979 * Read linker.hints file into memory performing some sanity checks.
982 moduledir_readhints(struct moduledir *mdp)
986 int fd, size, version;
988 if (mdp->d_hints != NULL || (mdp->d_flags & MDIR_NOHINTS))
990 path = moduledir_fullpath(mdp, "linker.hints");
991 if (rel_stat(path, &st) != 0 ||
992 st.st_size < (ssize_t)(sizeof(version) + sizeof(int)) ||
993 st.st_size > 100 * 1024 || (fd = rel_open(path, NULL, O_RDONLY)) < 0) {
995 mdp->d_flags |= MDIR_NOHINTS;
999 size = read(fd, &version, sizeof(version));
1000 if (size != sizeof(version) || version != LINKER_HINTS_VERSION)
1002 size = st.st_size - size;
1003 mdp->d_hints = malloc(size);
1004 if (mdp->d_hints == NULL)
1006 if (read(fd, mdp->d_hints, size) != size)
1008 mdp->d_hintsz = size;
1015 mdp->d_hints = NULL;
1017 mdp->d_flags |= MDIR_NOHINTS;
1022 * Extract directories from the ';' separated list, remove duplicates.
1025 moduledir_rebuild(void)
1027 struct moduledir *mdp, *mtmp;
1028 const char *path, *cp, *ep, *modlocal;
1031 path = getenv("module_path");
1033 path = default_searchpath;
1035 * Rebuild list of module directories if it changed
1037 STAILQ_FOREACH(mdp, &moduledir_list, d_link)
1038 mdp->d_flags |= MDIR_REMOVED;
1040 for (ep = path; *ep != 0; ep++) {
1042 for (; *ep != 0 && *ep != ';'; ep++)
1045 * Ignore trailing slashes
1047 for (cplen = ep - cp; cplen > 1 && cp[cplen - 1] == '/'; cplen--)
1049 STAILQ_FOREACH(mdp, &moduledir_list, d_link) {
1050 if (strlen(mdp->d_path) != cplen || bcmp(cp, mdp->d_path, cplen) != 0)
1052 mdp->d_flags &= ~MDIR_REMOVED;
1056 mdp = malloc(sizeof(*mdp) + cplen + 1);
1059 mdp->d_path = (char*)(mdp + 1);
1060 bcopy(cp, mdp->d_path, cplen);
1061 mdp->d_path[cplen] = 0;
1062 mdp->d_hints = NULL;
1064 STAILQ_INSERT_TAIL(&moduledir_list, mdp, d_link);
1070 * Include modules.local if requested
1072 modlocal = getenv("local_modules");
1073 if (modlocal != NULL && strcmp(modlocal, "YES") == 0) {
1074 cp = local_module_path;
1075 cplen = strlen(local_module_path);
1076 STAILQ_FOREACH(mdp, &moduledir_list, d_link) {
1077 if (strlen(mdp->d_path) != cplen || bcmp(cp, mdp->d_path, cplen) != 0)
1079 mdp->d_flags &= ~MDIR_REMOVED;
1083 mdp = malloc(sizeof(*mdp) + cplen + 1);
1086 mdp->d_path = (char*)(mdp + 1);
1087 bcopy(local_module_path, mdp->d_path, cplen);
1088 mdp->d_path[cplen] = 0;
1089 mdp->d_hints = NULL;
1091 STAILQ_INSERT_TAIL(&moduledir_list, mdp, d_link);
1095 * Delete unused directories if any
1097 mdp = STAILQ_FIRST(&moduledir_list);
1099 if ((mdp->d_flags & MDIR_REMOVED) == 0) {
1100 mdp = STAILQ_NEXT(mdp, d_link);
1105 mdp = STAILQ_NEXT(mdp, d_link);
1106 STAILQ_REMOVE(&moduledir_list, mtmp, moduledir, d_link);