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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
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_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 #if defined(LOADER_FDT_SUPPORT)
69 static const char *default_searchpath =
70 "/boot/kernel;/boot/modules;/boot/dtb";
72 static const char *default_searchpath ="/boot/kernel;/boot/modules";
75 static STAILQ_HEAD(, moduledir) moduledir_list = STAILQ_HEAD_INITIALIZER(moduledir_list);
77 struct preloaded_file *preloaded_files = NULL;
79 static char *kld_ext_list[] = {
88 * load an object, either a disk file or code module.
90 * To load a file, the syntax is:
92 * load -t <type> <path>
94 * code modules are loaded as:
96 * load <path> <options>
99 COMMAND_SET(load, "load", "load a kernel or module", command_load);
102 command_load(int argc, char *argv[])
104 struct preloaded_file *fp;
106 int dofile, dokld, ch, error;
113 command_errmsg = "no filename specified";
116 while ((ch = getopt(argc, argv, "kt:")) != -1) {
127 /* getopt has already reported an error */
131 argv += (optind - 1);
132 argc -= (optind - 1);
135 * Request to load a raw file?
138 if ((argc != 2) || (typestr == NULL) || (*typestr == 0)) {
139 command_errmsg = "invalid load type";
143 fp = file_findfile(argv[1], typestr);
145 sprintf(command_errbuf, "warning: file '%s' already loaded", argv[1]);
149 if (file_loadraw(argv[1], typestr, 1) != NULL)
152 /* Failing to load mfs_root is never going to end well! */
153 if (strcmp("mfs_root", typestr) == 0)
159 * Do we have explicit KLD load ?
161 if (dokld || file_havepath(argv[1])) {
162 error = mod_loadkld(argv[1], argc - 2, argv + 2);
163 if (error == EEXIST) {
164 sprintf(command_errbuf, "warning: KLD '%s' already loaded", argv[1]);
168 return (error == 0 ? CMD_OK : CMD_CRIT);
171 * Looks like a request for a module.
173 error = mod_load(argv[1], NULL, argc - 2, argv + 2);
174 if (error == EEXIST) {
175 sprintf(command_errbuf, "warning: module '%s' already loaded", argv[1]);
179 return (error == 0 ? CMD_OK : CMD_CRIT);
182 COMMAND_SET(load_geli, "load_geli", "load a geli key", command_load_geli);
185 command_load_geli(int argc, char *argv[])
192 command_errmsg = "usage is [-n key#] <prov> <file>";
199 while ((ch = getopt(argc, argv, "n:")) != -1) {
202 num = strtol(optarg, &cp, 0);
204 sprintf(command_errbuf, "bad key index '%s'", optarg);
210 /* getopt has already reported an error */
214 argv += (optind - 1);
215 argc -= (optind - 1);
216 sprintf(typestr, "%s:geli_keyfile%d", argv[1], num);
217 return (file_loadraw(argv[2], typestr, 1) ? CMD_OK : CMD_ERROR);
220 COMMAND_SET(unload, "unload", "unload all modules", command_unload);
223 command_unload(int argc, char *argv[])
225 struct preloaded_file *fp;
227 while (preloaded_files != NULL) {
228 fp = preloaded_files;
229 preloaded_files = preloaded_files->f_next;
233 unsetenv("kernelname");
237 COMMAND_SET(lsmod, "lsmod", "list loaded modules", command_lsmod);
240 command_lsmod(int argc, char *argv[])
242 struct preloaded_file *fp;
243 struct kernel_module *mp;
244 struct file_metadata *md;
251 while ((ch = getopt(argc, argv, "v")) != -1) {
258 /* getopt has already reported an error */
264 for (fp = preloaded_files; fp; fp = fp->f_next) {
265 sprintf(lbuf, " %p: %s (%s, 0x%lx)\n",
266 (void *) fp->f_addr, fp->f_name, fp->f_type, (long) fp->f_size);
268 if (fp->f_args != NULL) {
269 pager_output(" args: ");
270 pager_output(fp->f_args);
274 pager_output(" modules: ");
275 for (mp = fp->f_modules; mp; mp = mp->m_next) {
276 sprintf(lbuf, "%s.%d ", mp->m_name, mp->m_version);
282 /* XXX could add some formatting smarts here to display some better */
283 for (md = fp->f_metadata; md != NULL; md = md->md_next) {
284 sprintf(lbuf, " 0x%04x, 0x%lx\n", md->md_type, (long) md->md_size);
294 * File level interface, functions file_*
297 file_load(char *filename, vm_offset_t dest, struct preloaded_file **result)
299 static int last_file_format = 0;
300 struct preloaded_file *fp;
304 if (archsw.arch_loadaddr != NULL)
305 dest = archsw.arch_loadaddr(LOAD_RAW, filename, dest);
308 for (i = last_file_format, fp = NULL;
309 file_formats[i] && fp == NULL; i++) {
310 error = (file_formats[i]->l_load)(filename, dest, &fp);
312 fp->f_loader = last_file_format = i; /* remember the loader */
315 } else if (last_file_format == i && i != 0) {
316 /* Restart from the beginning */
318 last_file_format = 0;
323 continue; /* Unknown to this handler? */
325 sprintf(command_errbuf, "can't load file '%s': %s",
326 filename, strerror(error));
334 file_load_dependencies(struct preloaded_file *base_file)
336 struct file_metadata *md;
337 struct preloaded_file *fp;
338 struct mod_depend *verinfo;
339 struct kernel_module *mp;
343 md = file_findmetadata(base_file, MODINFOMD_DEPLIST);
348 verinfo = (struct mod_depend*)md->md_data;
349 dmodname = (char *)(verinfo + 1);
350 if (file_findmodule(NULL, dmodname, verinfo) == NULL) {
351 printf("loading required module '%s'\n", dmodname);
352 error = mod_load(dmodname, verinfo, 0, NULL);
356 * If module loaded via kld name which isn't listed
357 * in the linker.hints file, we should check if it have
360 mp = file_findmodule(NULL, dmodname, verinfo);
362 sprintf(command_errbuf, "module '%s' exists but with wrong version",
368 md = metadata_next(md, MODINFOMD_DEPLIST);
372 /* Load failed; discard everything */
373 while (base_file != NULL) {
375 base_file = base_file->f_next;
382 * We've been asked to load (fname) as (type), so just suck it in,
383 * no arguments or anything.
385 struct preloaded_file *
386 file_loadraw(const char *fname, char *type, int insert)
388 struct preloaded_file *fp;
393 /* We can't load first */
394 if ((file_findfile(NULL, NULL)) == NULL) {
395 command_errmsg = "can't load file before kernel";
399 /* locate the file on the load path */
400 name = file_search(fname, NULL);
402 sprintf(command_errbuf, "can't find '%s'", fname);
406 if ((fd = open(name, O_RDONLY)) < 0) {
407 sprintf(command_errbuf, "can't open '%s': %s", name, strerror(errno));
412 if (archsw.arch_loadaddr != NULL)
413 loadaddr = archsw.arch_loadaddr(LOAD_RAW, name, loadaddr);
417 /* read in 4k chunks; size is not really important */
418 got = archsw.arch_readin(fd, laddr, 4096);
419 if (got == 0) /* end of file */
421 if (got < 0) { /* error */
422 sprintf(command_errbuf, "error reading '%s': %s", name, strerror(errno));
430 /* Looks OK so far; create & populate control structure */
432 fp->f_name = strdup(name);
433 fp->f_type = strdup(type);
435 fp->f_metadata = NULL;
437 fp->f_addr = loadaddr;
438 fp->f_size = laddr - loadaddr;
440 /* recognise space consumption */
443 /* Add to the list of loaded files */
445 file_insert_tail(fp);
451 * Load the module (name), pass it (argc),(argv), add container file
452 * to the list of loaded files.
453 * If module is already loaded just assign new argc/argv.
456 mod_load(char *modname, struct mod_depend *verinfo, int argc, char *argv[])
458 struct kernel_module *mp;
462 if (file_havepath(modname)) {
463 printf("Warning: mod_load() called instead of mod_loadkld() for module '%s'\n", modname);
464 return (mod_loadkld(modname, argc, argv));
466 /* see if module is already loaded */
467 mp = file_findmodule(NULL, modname, verinfo);
472 mp->m_args = unargv(argc, argv);
474 sprintf(command_errbuf, "warning: module '%s' already loaded", mp->m_name);
477 /* locate file with the module on the search path */
478 filename = mod_searchmodule(modname, verinfo);
479 if (filename == NULL) {
480 sprintf(command_errbuf, "can't find '%s'", modname);
483 err = mod_loadkld(filename, argc, argv);
488 * Load specified KLD. If path is omitted, then try to locate it via
492 mod_loadkld(const char *kldname, int argc, char *argv[])
494 struct preloaded_file *fp, *last_file;
499 * Get fully qualified KLD name
501 filename = file_search(kldname, kld_ext_list);
502 if (filename == NULL) {
503 sprintf(command_errbuf, "can't find '%s'", kldname);
507 * Check if KLD already loaded
509 fp = file_findfile(filename, NULL);
511 sprintf(command_errbuf, "warning: KLD '%s' already loaded", filename);
515 for (last_file = preloaded_files;
516 last_file != NULL && last_file->f_next != NULL;
517 last_file = last_file->f_next)
521 err = file_load(filename, loadaddr, &fp);
524 fp->f_args = unargv(argc, argv);
525 loadaddr = fp->f_addr + fp->f_size;
526 file_insert_tail(fp); /* Add to the list of loaded files */
527 if (file_load_dependencies(fp) != 0) {
529 last_file->f_next = NULL;
530 loadaddr = last_file->f_addr + last_file->f_size;
536 sprintf(command_errbuf, "don't know how to load module '%s'", filename);
544 * Find a file matching (name) and (type).
545 * NULL may be passed as a wildcard to either.
547 struct preloaded_file *
548 file_findfile(const char *name, const char *type)
550 struct preloaded_file *fp;
552 for (fp = preloaded_files; fp != NULL; fp = fp->f_next) {
553 if (((name == NULL) || !strcmp(name, fp->f_name)) &&
554 ((type == NULL) || !strcmp(type, fp->f_type)))
561 * Find a module matching (name) inside of given file.
562 * NULL may be passed as a wildcard.
564 struct kernel_module *
565 file_findmodule(struct preloaded_file *fp, char *modname,
566 struct mod_depend *verinfo)
568 struct kernel_module *mp, *best;
572 for (fp = preloaded_files; fp; fp = fp->f_next) {
573 mp = file_findmodule(fp, modname, verinfo);
581 for (mp = fp->f_modules; mp; mp = mp->m_next) {
582 if (strcmp(modname, mp->m_name) == 0) {
585 mver = mp->m_version;
586 if (mver == verinfo->md_ver_preferred)
588 if (mver >= verinfo->md_ver_minimum &&
589 mver <= verinfo->md_ver_maximum &&
599 * Make a copy of (size) bytes of data from (p), and associate them as
600 * metadata of (type) to the module (mp).
603 file_addmetadata(struct preloaded_file *fp, int type, size_t size, void *p)
605 struct file_metadata *md;
607 md = malloc(sizeof(struct file_metadata) - sizeof(md->md_data) + size);
610 bcopy(p, md->md_data, size);
611 md->md_next = fp->f_metadata;
616 * Find a metadata object of (type) associated with the file (fp)
618 struct file_metadata *
619 file_findmetadata(struct preloaded_file *fp, int type)
621 struct file_metadata *md;
623 for (md = fp->f_metadata; md != NULL; md = md->md_next)
624 if (md->md_type == type)
629 struct file_metadata *
630 metadata_next(struct file_metadata *md, int type)
634 while((md = md->md_next) != NULL)
635 if (md->md_type == type)
640 static char *emptyextlist[] = { "", NULL };
643 * Check if the given file is in place and return full path to it.
646 file_lookup(const char *path, const char *name, int namelen, char **extlist)
649 char *result, *cp, **cpp;
650 int pathlen, extlen, len;
652 pathlen = strlen(path);
655 extlist = emptyextlist;
656 for (cpp = extlist; *cpp; cpp++) {
661 result = malloc(pathlen + namelen + extlen + 2);
664 bcopy(path, result, pathlen);
665 if (pathlen > 0 && result[pathlen - 1] != '/')
666 result[pathlen++] = '/';
667 cp = result + pathlen;
668 bcopy(name, cp, namelen);
670 for (cpp = extlist; *cpp; cpp++) {
672 if (stat(result, &st) == 0 && S_ISREG(st.st_mode))
680 * Check if file name have any qualifiers
683 file_havepath(const char *name)
687 archsw.arch_getdev(NULL, name, &cp);
688 return (cp != name || strchr(name, '/') != NULL);
692 * Attempt to find the file (name) on the module searchpath.
693 * If (name) is qualified in any way, we simply check it and
694 * return it or NULL. If it is not qualified, then we attempt
695 * to construct a path using entries in the environment variable
698 * The path we return a pointer to need never be freed, as we manage
702 file_search(const char *name, char **extlist)
704 struct moduledir *mdp;
709 /* Don't look for nothing */
714 return(strdup(name));
716 if (file_havepath(name)) {
717 /* Qualified, so just see if it exists */
718 if (stat(name, &sb) == 0)
719 return(strdup(name));
724 namelen = strlen(name);
725 STAILQ_FOREACH(mdp, &moduledir_list, d_link) {
726 result = file_lookup(mdp->d_path, name, namelen, extlist);
733 #define INT_ALIGN(base, ptr) ptr = \
734 (base) + (((ptr) - (base) + sizeof(int) - 1) & ~(sizeof(int) - 1))
737 mod_search_hints(struct moduledir *mdp, const char *modname,
738 struct mod_depend *verinfo)
740 u_char *cp, *recptr, *bufend, *best;
742 int *intp, bestver, blen, clen, found, ival, modnamelen, reclen;
744 moduledir_readhints(mdp);
745 modnamelen = strlen(modname);
749 if (mdp->d_hints == NULL)
751 recptr = mdp->d_hints;
752 bufend = recptr + mdp->d_hintsz;
755 while (recptr < bufend && !found) {
763 if (clen != modnamelen || bcmp(cp, modname, clen) != 0)
766 INT_ALIGN(mdp->d_hints, cp);
770 if (verinfo == NULL || ival == verinfo->md_ver_preferred) {
774 if (ival >= verinfo->md_ver_minimum &&
775 ival <= verinfo->md_ver_maximum &&
785 recptr += reclen + sizeof(int);
788 * Finally check if KLD is in the place
791 result = file_lookup(mdp->d_path, cp, clen, NULL);
793 result = file_lookup(mdp->d_path, best, blen, NULL);
796 * If nothing found or hints is absent - fallback to the old way
797 * by using "kldname[.ko]" as module name.
799 if (!found && !bestver && result == NULL)
800 result = file_lookup(mdp->d_path, modname, modnamelen, kld_ext_list);
805 * Attempt to locate the file containing the module (name)
808 mod_searchmodule(char *name, struct mod_depend *verinfo)
810 struct moduledir *mdp;
815 * Now we ready to lookup module in the given directories
818 STAILQ_FOREACH(mdp, &moduledir_list, d_link) {
819 result = mod_search_hints(mdp, name, verinfo);
828 file_addmodule(struct preloaded_file *fp, char *modname, int version,
829 struct kernel_module **newmp)
831 struct kernel_module *mp;
832 struct mod_depend mdepend;
834 bzero(&mdepend, sizeof(mdepend));
835 mdepend.md_ver_preferred = version;
836 mp = file_findmodule(fp, modname, &mdepend);
839 mp = malloc(sizeof(struct kernel_module));
842 bzero(mp, sizeof(struct kernel_module));
843 mp->m_name = strdup(modname);
844 mp->m_version = version;
846 mp->m_next = fp->f_modules;
857 file_discard(struct preloaded_file *fp)
859 struct file_metadata *md, *md1;
860 struct kernel_module *mp, *mp1;
877 if (fp->f_name != NULL)
879 if (fp->f_type != NULL)
881 if (fp->f_args != NULL)
887 * Allocate a new file; must be used instead of malloc()
888 * to ensure safe initialisation.
890 struct preloaded_file *
893 struct preloaded_file *fp;
895 if ((fp = malloc(sizeof(struct preloaded_file))) != NULL) {
896 bzero(fp, sizeof(struct preloaded_file));
902 * Add a module to the chain
905 file_insert_tail(struct preloaded_file *fp)
907 struct preloaded_file *cm;
909 /* Append to list of loaded file */
911 if (preloaded_files == NULL) {
912 preloaded_files = fp;
914 for (cm = preloaded_files; cm->f_next != NULL; cm = cm->f_next)
921 moduledir_fullpath(struct moduledir *mdp, const char *fname)
925 cp = malloc(strlen(mdp->d_path) + strlen(fname) + 2);
928 strcpy(cp, mdp->d_path);
935 * Read linker.hints file into memory performing some sanity checks.
938 moduledir_readhints(struct moduledir *mdp)
942 int fd, size, version;
944 if (mdp->d_hints != NULL || (mdp->d_flags & MDIR_NOHINTS))
946 path = moduledir_fullpath(mdp, "linker.hints");
947 if (stat(path, &st) != 0 ||
948 st.st_size < (ssize_t)(sizeof(version) + sizeof(int)) ||
949 st.st_size > 100 * 1024 || (fd = open(path, O_RDONLY)) < 0) {
951 mdp->d_flags |= MDIR_NOHINTS;
955 size = read(fd, &version, sizeof(version));
956 if (size != sizeof(version) || version != LINKER_HINTS_VERSION)
958 size = st.st_size - size;
959 mdp->d_hints = malloc(size);
960 if (mdp->d_hints == NULL)
962 if (read(fd, mdp->d_hints, size) != size)
964 mdp->d_hintsz = size;
973 mdp->d_flags |= MDIR_NOHINTS;
978 * Extract directories from the ';' separated list, remove duplicates.
981 moduledir_rebuild(void)
983 struct moduledir *mdp, *mtmp;
984 const char *path, *cp, *ep;
987 path = getenv("module_path");
989 path = default_searchpath;
991 * Rebuild list of module directories if it changed
993 STAILQ_FOREACH(mdp, &moduledir_list, d_link)
994 mdp->d_flags |= MDIR_REMOVED;
996 for (ep = path; *ep != 0; ep++) {
998 for (; *ep != 0 && *ep != ';'; ep++)
1001 * Ignore trailing slashes
1003 for (cplen = ep - cp; cplen > 1 && cp[cplen - 1] == '/'; cplen--)
1005 STAILQ_FOREACH(mdp, &moduledir_list, d_link) {
1006 if (strlen(mdp->d_path) != cplen || bcmp(cp, mdp->d_path, cplen) != 0)
1008 mdp->d_flags &= ~MDIR_REMOVED;
1012 mdp = malloc(sizeof(*mdp) + cplen + 1);
1015 mdp->d_path = (char*)(mdp + 1);
1016 bcopy(cp, mdp->d_path, cplen);
1017 mdp->d_path[cplen] = 0;
1018 mdp->d_hints = NULL;
1020 STAILQ_INSERT_TAIL(&moduledir_list, mdp, d_link);
1026 * Delete unused directories if any
1028 mdp = STAILQ_FIRST(&moduledir_list);
1030 if ((mdp->d_flags & MDIR_REMOVED) == 0) {
1031 mdp = STAILQ_NEXT(mdp, d_link);
1036 mdp = STAILQ_NEXT(mdp, d_link);
1037 STAILQ_REMOVE(&moduledir_list, mtmp, moduledir, d_link);