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 "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 ="modules;KERNEL";
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[] = {
80 COMMAND_SET(slow, "slow", "slow print", command_slow);
85 command_slow(int argc, char *argv[])
98 for (i = 0; i < 10; ++i)
104 * load an object, either a disk file or code module.
106 * To load a file, the syntax is:
108 * load -t <type> <path>
110 * code modules are loaded as:
112 * load <path> <options>
114 COMMAND_SET(load, "load", "load a kernel or module", command_load);
117 command_load(int argc, char *argv[])
120 int dofile, dokld, ch, error;
127 command_errmsg = "no filename specified";
130 while ((ch = getopt(argc, argv, "kt:")) != -1) {
141 /* getopt has already reported an error */
145 argv += (optind - 1);
146 argc -= (optind - 1);
149 * Request to load a raw file?
152 if ((argc != 2) || (typestr == NULL) || (*typestr == 0)) {
153 command_errmsg = "invalid load type";
156 return(file_loadraw(typestr, argv[1]));
159 * Do we have explicit KLD load ?
161 if (dokld || file_havepath(argv[1])) {
162 error = mod_loadkld(argv[1], argc - 2, argv + 2);
164 sprintf(command_errbuf, "warning: KLD '%s' already loaded", argv[1]);
165 return (error == 0 ? CMD_OK : CMD_ERROR);
168 * Looks like a request for a module.
170 error = mod_load(argv[1], NULL, argc - 2, argv + 2);
172 sprintf(command_errbuf, "warning: module '%s' already loaded", argv[1]);
173 return (error == 0 ? CMD_OK : CMD_ERROR);
176 COMMAND_SET(unload, "unload", "unload all modules", command_unload);
179 command_unload(int argc, char *argv[])
181 struct preloaded_file *fp;
183 while (preloaded_files != NULL) {
184 fp = preloaded_files;
185 preloaded_files = preloaded_files->f_next;
189 unsetenv("kernelname");
193 COMMAND_SET(crc, "crc", "calculate crc for file", command_crc);
195 uint32_t iscsi_crc32(const void *buf, size_t size);
196 uint32_t iscsi_crc32_ext(const void *buf, size_t size, uint32_t ocrc);
199 command_crc(int argc, char *argv[])
210 command_errmsg = "no filename specified";
216 printf("size\tcrc\t name\n");
217 for (i = 1; i < argc; ++i) {
218 /* locate the file on the load path */
219 cp = file_search(argv[i], NULL);
221 sprintf(command_errbuf, "can't find '%s'", argv[i]);
227 if ((fd = rel_open(name, NULL, O_RDONLY)) < 0) {
228 sprintf(command_errbuf,
229 "can't open '%s': %s",
230 name, strerror(errno));
238 got = read(fd, buf, 8192);
242 printf("error reading '%s': %s\n",
243 name, strerror(errno));
247 crc = iscsi_crc32(buf, got);
249 crc = iscsi_crc32_ext(buf, got, crc);
252 printf("%7d %08x %s\n", tot, crc, name);
262 COMMAND_SET(lsmod, "lsmod", "list loaded modules", command_lsmod);
265 command_lsmod(int argc, char *argv[])
267 struct preloaded_file *fp;
268 struct kernel_module *mp;
269 struct file_metadata *md;
276 while ((ch = getopt(argc, argv, "v")) != -1) {
283 /* getopt has already reported an error */
289 for (fp = preloaded_files; fp; fp = fp->f_next) {
290 sprintf(lbuf, " %p: %s (%s, 0x%lx)\n",
291 (void *) fp->f_addr, fp->f_name, fp->f_type, (long) fp->f_size);
293 if (fp->f_args != NULL) {
294 pager_output(" args: ");
295 pager_output(fp->f_args);
299 pager_output(" modules: ");
300 for (mp = fp->f_modules; mp; mp = mp->m_next) {
301 sprintf(lbuf, "%s.%d ", mp->m_name, mp->m_version);
307 /* XXX could add some formatting smarts here to display some better */
308 for (md = fp->f_metadata; md != NULL; md = md->md_next) {
309 sprintf(lbuf, " 0x%04x, 0x%lx\n", md->md_type, (long) md->md_size);
319 * File level interface, functions file_*
322 file_load(char *filename, vm_offset_t dest, struct preloaded_file **result)
324 struct preloaded_file *fp;
329 for (i = 0, fp = NULL; file_formats[i] && fp == NULL; i++) {
330 error = (file_formats[i]->l_load)(filename, loadaddr, &fp);
332 fp->f_loader = i; /* remember the loader */
337 continue; /* Unknown to this handler? */
339 sprintf(command_errbuf, "can't load file '%s': %s",
340 filename, strerror(error));
348 file_load_dependencies(struct preloaded_file *base_file) {
349 struct file_metadata *md;
350 struct preloaded_file *fp;
351 struct mod_depend *verinfo;
352 struct kernel_module *mp;
356 md = file_findmetadata(base_file, MODINFOMD_DEPLIST);
361 verinfo = (struct mod_depend*)md->md_data;
362 dmodname = (char *)(verinfo + 1);
363 if (file_findmodule(NULL, dmodname, verinfo) == NULL) {
364 printf("loading required module '%s'\n", dmodname);
365 error = mod_load(dmodname, verinfo, 0, NULL);
369 * If module loaded via kld name which isn't listed
370 * in the linker.hints file, we should check if it have
373 mp = file_findmodule(NULL, dmodname, verinfo);
375 sprintf(command_errbuf, "module '%s' exists but with wrong version",
381 md = metadata_next(md, MODINFOMD_DEPLIST);
385 /* Load failed; discard everything */
386 while (base_file != NULL) {
388 base_file = base_file->f_next;
394 * We've been asked to load (name) as (type), so just suck it in,
395 * no arguments or anything.
398 file_loadraw(char *type, char *name)
400 struct preloaded_file *fp;
405 /* We can't load first */
406 if ((file_findfile(NULL, NULL)) == NULL) {
407 command_errmsg = "can't load file before kernel";
411 /* locate the file on the load path */
412 cp = file_search(name, NULL);
414 sprintf(command_errbuf, "can't find '%s'", name);
419 if ((fd = rel_open(name, NULL, O_RDONLY)) < 0) {
420 sprintf(command_errbuf, "can't open '%s': %s", name, strerror(errno));
427 /* read in 4k chunks; size is not really important */
428 got = archsw.arch_readin(fd, laddr, 4096);
429 if (got == 0) /* end of file */
431 if (got < 0) { /* error */
432 sprintf(command_errbuf, "error reading '%s': %s", name, strerror(errno));
440 /* Looks OK so far; create & populate control structure */
443 fp->f_type = strdup(type);
445 fp->f_metadata = NULL;
447 fp->f_addr = loadaddr;
448 fp->f_size = laddr - loadaddr;
450 /* recognise space consumption */
453 /* Add to the list of loaded files */
454 file_insert_tail(fp);
460 * Load the module (name), pass it (argc),(argv), add container file
461 * to the list of loaded files.
462 * If module is already loaded just assign new argc/argv.
465 mod_load(char *modname, struct mod_depend *verinfo, int argc, char *argv[])
467 struct kernel_module *mp;
471 if (file_havepath(modname)) {
472 printf("Warning: mod_load() called instead of mod_loadkld() for module '%s'\n", modname);
473 return (mod_loadkld(modname, argc, argv));
475 /* see if module is already loaded */
476 mp = file_findmodule(NULL, modname, verinfo);
481 mp->m_args = unargv(argc, argv);
483 sprintf(command_errbuf, "warning: module '%s' already loaded", mp->m_name);
486 /* locate file with the module on the search path */
487 filename = mod_searchmodule(modname, verinfo);
488 if (filename == NULL) {
489 sprintf(command_errbuf, "can't find '%s'", modname);
492 err = mod_loadkld(filename, argc, argv);
497 * Load specified KLD. If path is omitted, then try to locate it via
501 mod_loadkld(const char *kldname, int argc, char *argv[])
503 struct preloaded_file *fp, *last_file;
508 * Get fully qualified KLD name
510 filename = file_search(kldname, kld_ext_list);
511 if (filename == NULL) {
512 sprintf(command_errbuf, "can't find '%s'", kldname);
516 * Check if KLD already loaded
518 fp = file_findfile(filename, NULL);
520 sprintf(command_errbuf, "warning: KLD '%s' already loaded", filename);
524 for (last_file = preloaded_files;
525 last_file != NULL && last_file->f_next != NULL;
526 last_file = last_file->f_next)
530 err = file_load(filename, loadaddr, &fp);
533 fp->f_args = unargv(argc, argv);
534 loadaddr = fp->f_addr + fp->f_size;
535 file_insert_tail(fp); /* Add to the list of loaded files */
536 if (file_load_dependencies(fp) != 0) {
538 last_file->f_next = NULL;
539 loadaddr = last_file->f_addr + last_file->f_size;
545 sprintf(command_errbuf, "don't know how to load module '%s'", filename);
553 * Find a file matching (name) and (type).
554 * NULL may be passed as a wildcard to either.
556 struct preloaded_file *
557 file_findfile(char *name, char *type)
559 struct preloaded_file *fp;
561 for (fp = preloaded_files; fp != NULL; fp = fp->f_next) {
562 if (((name == NULL) || !strcmp(name, fp->f_name)) &&
563 ((type == NULL) || !strcmp(type, fp->f_type)))
570 * Find a module matching (name) inside of given file.
571 * NULL may be passed as a wildcard.
573 struct kernel_module *
574 file_findmodule(struct preloaded_file *fp, char *modname,
575 struct mod_depend *verinfo)
577 struct kernel_module *mp, *best;
581 for (fp = preloaded_files; fp; fp = fp->f_next) {
582 mp = file_findmodule(fp, modname, verinfo);
590 for (mp = fp->f_modules; mp; mp = mp->m_next) {
591 if (strcmp(modname, mp->m_name) == 0) {
594 mver = mp->m_version;
595 if (mver == verinfo->md_ver_preferred)
597 if (mver >= verinfo->md_ver_minimum &&
598 mver <= verinfo->md_ver_maximum &&
608 * Make a copy of (size) bytes of data from (p), and associate them as
609 * metadata of (type) to the module (mp).
612 file_addmetadata(struct preloaded_file *fp, int type, size_t size, void *p)
614 struct file_metadata *md;
616 md = malloc(sizeof(struct file_metadata) - sizeof(md->md_data) + size);
619 bcopy(p, md->md_data, size);
620 md->md_next = fp->f_metadata;
625 * Find a metadata object of (type) associated with the file (fp)
627 struct file_metadata *
628 file_findmetadata(struct preloaded_file *fp, int type)
630 struct file_metadata *md;
632 for (md = fp->f_metadata; md != NULL; md = md->md_next)
633 if (md->md_type == type)
638 struct file_metadata *
639 metadata_next(struct file_metadata *md, int type)
643 while((md = md->md_next) != NULL)
644 if (md->md_type == type)
649 static char *emptyextlist[] = { "", NULL };
652 * Check if the given file is in place and return full path to it.
655 file_lookup(const char *path, const char *name, int namelen, char **extlist)
658 char *result, *cp, **cpp;
659 int pathlen, extlen, len;
661 pathlen = strlen(path);
664 extlist = emptyextlist;
665 for (cpp = extlist; *cpp; cpp++) {
670 result = malloc(pathlen + namelen + extlen + 2 + 7 + 1);
673 bcopy(path, result, pathlen);
674 if (pathlen > 0 && result[pathlen - 1] != '/')
675 result[pathlen++] = '/';
676 cp = result + pathlen;
677 bcopy(name, cp, namelen);
679 for (cpp = extlist; *cpp; cpp++) {
681 if (rel_stat(result, &st) == 0) {
682 if (S_ISREG(st.st_mode)) {
684 } else if (S_ISDIR(st.st_mode)) {
685 strcat(result, "/kernel");
686 if (rel_stat(result, &st) == 0 && S_ISREG(st.st_mode)) {
697 * Check if file name have any qualifiers
700 file_havepath(const char *name)
704 archsw.arch_getdev(NULL, name, &cp);
705 return (cp != name || strchr(name, '/') != NULL);
709 * Attempt to find the file (name) on the module searchpath.
710 * If (name) is qualified in any way, we simply check it and
711 * return it or NULL. If it is not qualified, then we attempt
712 * to construct a path using entries in the environment variable
715 * The path we return a pointer to need never be freed, as we manage
719 file_search(const char *name, char **extlist)
721 struct moduledir *mdp;
726 /* Don't look for nothing */
731 return(strdup(name));
734 * Qualified name. If it is a directory tag on
737 if (file_havepath(name)) {
738 /* Qualified, so just see if it exists */
739 if (rel_stat(name, &sb) == 0) {
740 if (S_ISDIR(sb.st_mode)) {
741 result = malloc(strlen(name) + 7 + 1);
742 sprintf(result, "%s/kernel", name);
745 return(strdup(name));
752 namelen = strlen(name);
753 STAILQ_FOREACH(mdp, &moduledir_list, d_link) {
754 result = file_lookup(mdp->d_path, name, namelen, extlist);
761 #define INT_ALIGN(base, ptr) \
762 ptr = (base) + roundup2((ptr) - (base), sizeof(int))
765 mod_search_hints(struct moduledir *mdp, const char *modname,
766 struct mod_depend *verinfo)
768 u_char *cp, *recptr, *bufend, *best;
770 int *intp, bestver, blen, clen, found, ival, modnamelen, reclen;
772 moduledir_readhints(mdp);
773 modnamelen = strlen(modname);
777 if (mdp->d_hints == NULL)
779 recptr = mdp->d_hints;
780 bufend = recptr + mdp->d_hintsz;
783 while (recptr < bufend && !found) {
791 if (clen != modnamelen || bcmp(cp, modname, clen) != 0)
794 INT_ALIGN(mdp->d_hints, cp);
798 if (verinfo == NULL || ival == verinfo->md_ver_preferred) {
802 if (ival >= verinfo->md_ver_minimum &&
803 ival <= verinfo->md_ver_maximum &&
813 recptr += reclen + sizeof(int);
816 * Finally check if KLD is in the place
819 result = file_lookup(mdp->d_path, cp, clen, NULL);
821 result = file_lookup(mdp->d_path, best, blen, NULL);
824 * If nothing found or hints is absent - fallback to the old way
825 * by using "kldname[.ko]" as module name.
827 if (!found && !bestver && result == NULL)
828 result = file_lookup(mdp->d_path, modname, modnamelen, kld_ext_list);
833 * Attempt to locate the file containing the module (name)
836 mod_searchmodule(char *name, struct mod_depend *verinfo)
838 struct moduledir *mdp;
843 * Now we ready to lookup module in the given directories
846 STAILQ_FOREACH(mdp, &moduledir_list, d_link) {
847 result = mod_search_hints(mdp, name, verinfo);
856 file_addmodule(struct preloaded_file *fp, char *modname, int version,
857 struct kernel_module **newmp)
859 struct kernel_module *mp;
860 struct mod_depend mdepend;
862 bzero(&mdepend, sizeof(mdepend));
863 mdepend.md_ver_preferred = version;
864 mp = file_findmodule(fp, modname, &mdepend);
867 mp = malloc(sizeof(struct kernel_module));
870 bzero(mp, sizeof(struct kernel_module));
871 mp->m_name = strdup(modname);
872 mp->m_version = version;
874 mp->m_next = fp->f_modules;
885 file_discard(struct preloaded_file *fp)
887 struct file_metadata *md, *md1;
888 struct kernel_module *mp, *mp1;
905 if (fp->f_name != NULL)
907 if (fp->f_type != NULL)
909 if (fp->f_args != NULL)
915 * Allocate a new file; must be used instead of malloc()
916 * to ensure safe initialisation.
918 struct preloaded_file *
921 struct preloaded_file *fp;
923 if ((fp = malloc(sizeof(struct preloaded_file))) != NULL) {
924 bzero(fp, sizeof(struct preloaded_file));
930 * Add a module to the chain
933 file_insert_tail(struct preloaded_file *fp)
935 struct preloaded_file *cm;
937 /* Append to list of loaded file */
939 if (preloaded_files == NULL) {
940 preloaded_files = fp;
942 for (cm = preloaded_files; cm->f_next != NULL; cm = cm->f_next)
949 moduledir_fullpath(struct moduledir *mdp, const char *fname)
953 cp = malloc(strlen(mdp->d_path) + strlen(fname) + 2);
956 strcpy(cp, mdp->d_path);
963 * Read linker.hints file into memory performing some sanity checks.
966 moduledir_readhints(struct moduledir *mdp)
970 int fd, size, version;
972 if (mdp->d_hints != NULL || (mdp->d_flags & MDIR_NOHINTS))
974 path = moduledir_fullpath(mdp, "linker.hints");
975 if (rel_stat(path, &st) != 0 || st.st_size < (sizeof(version) + sizeof(int)) ||
976 st.st_size > 100 * 1024 || (fd = rel_open(path, NULL, O_RDONLY)) < 0) {
978 mdp->d_flags |= MDIR_NOHINTS;
982 size = read(fd, &version, sizeof(version));
983 if (size != sizeof(version) || version != LINKER_HINTS_VERSION)
985 size = st.st_size - size;
986 mdp->d_hints = malloc(size);
987 if (mdp->d_hints == NULL)
989 if (read(fd, mdp->d_hints, size) != size)
991 mdp->d_hintsz = size;
1000 mdp->d_flags |= MDIR_NOHINTS;
1005 * Extract directories from the ';' separated list, remove duplicates.
1008 moduledir_rebuild(void)
1010 struct moduledir *mdp, *mtmp;
1011 const char *path, *cp, *ep;
1014 path = getenv("module_path");
1016 path = default_searchpath;
1018 * Rebuild list of module directories if it changed
1020 STAILQ_FOREACH(mdp, &moduledir_list, d_link)
1021 mdp->d_flags |= MDIR_REMOVED;
1023 for (ep = path; *ep != 0; ep++) {
1025 for (; *ep != 0 && *ep != ';'; ep++)
1028 * Ignore trailing slashes
1030 for (cplen = ep - cp; cplen > 1 && cp[cplen - 1] == '/'; cplen--)
1032 STAILQ_FOREACH(mdp, &moduledir_list, d_link) {
1033 if (strlen(mdp->d_path) != cplen || bcmp(cp, mdp->d_path, cplen) != 0)
1035 mdp->d_flags &= ~MDIR_REMOVED;
1039 mdp = malloc(sizeof(*mdp) + cplen + 1);
1042 mdp->d_path = (char*)(mdp + 1);
1043 bcopy(cp, mdp->d_path, cplen);
1044 mdp->d_path[cplen] = 0;
1045 mdp->d_hints = NULL;
1047 STAILQ_INSERT_TAIL(&moduledir_list, mdp, d_link);
1053 * Delete unused directories if any
1055 mdp = STAILQ_FIRST(&moduledir_list);
1057 if ((mdp->d_flags & MDIR_REMOVED) == 0) {
1058 mdp = STAILQ_NEXT(mdp, d_link);
1063 mdp = STAILQ_NEXT(mdp, d_link);
1064 STAILQ_REMOVE(&moduledir_list, mtmp, moduledir, d_link);