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>
40 #include <sys/stdint.h>
42 #include "bootstrap.h"
44 #define MDIR_REMOVED 0x0001
45 #define MDIR_NOHINTS 0x0002
48 char *d_path; /* path of modules directory */
49 u_char *d_hints; /* content of linker.hints file */
50 int d_hintsz; /* size of hints data */
52 STAILQ_ENTRY(moduledir) d_link;
55 static int file_load(char *filename, vm_offset_t dest, struct preloaded_file **result);
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 #if defined(LOADER_FDT_SUPPORT)
70 static const char *default_searchpath =
71 "/boot/kernel;/boot/modules;/boot/dtb";
73 static const char *default_searchpath ="/boot/kernel;/boot/modules";
76 static STAILQ_HEAD(, moduledir) moduledir_list = STAILQ_HEAD_INITIALIZER(moduledir_list);
78 struct preloaded_file *preloaded_files = NULL;
80 static char *kld_ext_list[] = {
89 * load an object, either a disk file or code module.
91 * To load a file, the syntax is:
93 * load -t <type> <path>
95 * code modules are loaded as:
97 * load <path> <options>
100 COMMAND_SET(load, "load", "load a kernel or module", command_load);
103 command_load(int argc, char *argv[])
105 struct preloaded_file *fp;
107 int dofile, dokld, ch, error;
114 command_errmsg = "no filename specified";
117 while ((ch = getopt(argc, argv, "kt:")) != -1) {
128 /* getopt has already reported an error */
132 argv += (optind - 1);
133 argc -= (optind - 1);
136 * Request to load a raw file?
139 if ((argc != 2) || (typestr == NULL) || (*typestr == 0)) {
140 command_errmsg = "invalid load type";
144 fp = file_findfile(argv[1], typestr);
146 sprintf(command_errbuf, "warning: file '%s' already loaded", argv[1]);
150 if (file_loadraw(argv[1], typestr, 1) != NULL)
153 /* Failing to load mfs_root is never going to end well! */
154 if (strcmp("mfs_root", typestr) == 0)
160 * Do we have explicit KLD load ?
162 if (dokld || file_havepath(argv[1])) {
163 error = mod_loadkld(argv[1], argc - 2, argv + 2);
164 if (error == EEXIST) {
165 sprintf(command_errbuf, "warning: KLD '%s' already loaded", argv[1]);
169 return (error == 0 ? CMD_OK : CMD_CRIT);
172 * Looks like a request for a module.
174 error = mod_load(argv[1], NULL, argc - 2, argv + 2);
175 if (error == EEXIST) {
176 sprintf(command_errbuf, "warning: module '%s' already loaded", argv[1]);
180 return (error == 0 ? CMD_OK : CMD_CRIT);
183 COMMAND_SET(load_geli, "load_geli", "load a geli key", command_load_geli);
186 command_load_geli(int argc, char *argv[])
193 command_errmsg = "usage is [-n key#] <prov> <file>";
200 while ((ch = getopt(argc, argv, "n:")) != -1) {
203 num = strtol(optarg, &cp, 0);
205 sprintf(command_errbuf, "bad key index '%s'", optarg);
211 /* getopt has already reported an error */
215 argv += (optind - 1);
216 argc -= (optind - 1);
217 sprintf(typestr, "%s:geli_keyfile%d", argv[1], num);
218 return (file_loadraw(argv[2], typestr, 1) ? CMD_OK : CMD_ERROR);
224 struct preloaded_file *fp;
226 while (preloaded_files != NULL) {
227 fp = preloaded_files;
228 preloaded_files = preloaded_files->f_next;
232 unsetenv("kernelname");
235 COMMAND_SET(unload, "unload", "unload all modules", command_unload);
238 command_unload(int argc, char *argv[])
244 COMMAND_SET(lsmod, "lsmod", "list loaded modules", command_lsmod);
247 command_lsmod(int argc, char *argv[])
249 struct preloaded_file *fp;
250 struct kernel_module *mp;
251 struct file_metadata *md;
258 while ((ch = getopt(argc, argv, "v")) != -1) {
265 /* getopt has already reported an error */
271 for (fp = preloaded_files; fp; fp = fp->f_next) {
272 sprintf(lbuf, " %p: ", (void *) fp->f_addr);
274 pager_output(fp->f_name);
275 sprintf(lbuf, " (%s, 0x%lx)\n", fp->f_type, (long)fp->f_size);
277 if (fp->f_args != NULL) {
278 pager_output(" args: ");
279 pager_output(fp->f_args);
280 if (pager_output("\n"))
284 pager_output(" modules: ");
285 for (mp = fp->f_modules; mp; mp = mp->m_next) {
286 sprintf(lbuf, "%s.%d ", mp->m_name, mp->m_version);
289 if (pager_output("\n"))
293 /* XXX could add some formatting smarts here to display some better */
294 for (md = fp->f_metadata; md != NULL; md = md->md_next) {
295 sprintf(lbuf, " 0x%04x, 0x%lx\n", md->md_type, (long) md->md_size);
296 if (pager_output(lbuf))
306 * File level interface, functions file_*
309 file_load(char *filename, vm_offset_t dest, struct preloaded_file **result)
311 static int last_file_format = 0;
312 struct preloaded_file *fp;
316 if (archsw.arch_loadaddr != NULL)
317 dest = archsw.arch_loadaddr(LOAD_RAW, filename, dest);
320 for (i = last_file_format, fp = NULL;
321 file_formats[i] && fp == NULL; i++) {
322 error = (file_formats[i]->l_load)(filename, dest, &fp);
324 fp->f_loader = last_file_format = i; /* remember the loader */
327 } else if (last_file_format == i && i != 0) {
328 /* Restart from the beginning */
330 last_file_format = 0;
335 continue; /* Unknown to this handler? */
337 sprintf(command_errbuf, "can't load file '%s': %s",
338 filename, strerror(error));
346 file_load_dependencies(struct preloaded_file *base_file)
348 struct file_metadata *md;
349 struct preloaded_file *fp;
350 struct mod_depend *verinfo;
351 struct kernel_module *mp;
355 md = file_findmetadata(base_file, MODINFOMD_DEPLIST);
360 verinfo = (struct mod_depend*)md->md_data;
361 dmodname = (char *)(verinfo + 1);
362 if (file_findmodule(NULL, dmodname, verinfo) == NULL) {
363 printf("loading required module '%s'\n", dmodname);
364 error = mod_load(dmodname, verinfo, 0, NULL);
368 * If module loaded via kld name which isn't listed
369 * in the linker.hints file, we should check if it have
372 mp = file_findmodule(NULL, dmodname, verinfo);
374 sprintf(command_errbuf, "module '%s' exists but with wrong version",
380 md = metadata_next(md, MODINFOMD_DEPLIST);
384 /* Load failed; discard everything */
385 while (base_file != NULL) {
387 base_file = base_file->f_next;
394 * We've been asked to load (fname) as (type), so just suck it in,
395 * no arguments or anything.
397 struct preloaded_file *
398 file_loadraw(const char *fname, char *type, int insert)
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 name = file_search(fname, NULL);
414 sprintf(command_errbuf, "can't find '%s'", fname);
418 if ((fd = open(name, O_RDONLY)) < 0) {
419 sprintf(command_errbuf, "can't open '%s': %s", name, strerror(errno));
424 if (archsw.arch_loadaddr != NULL)
425 loadaddr = archsw.arch_loadaddr(LOAD_RAW, name, loadaddr);
431 /* read in 4k chunks; size is not really important */
432 got = archsw.arch_readin(fd, laddr, 4096);
433 if (got == 0) /* end of file */
435 if (got < 0) { /* error */
436 sprintf(command_errbuf, "error reading '%s': %s", name, strerror(errno));
444 printf("size=%#jx\n", (uintmax_t)(laddr - loadaddr));
446 /* Looks OK so far; create & populate control structure */
448 fp->f_name = strdup(name);
449 fp->f_type = strdup(type);
451 fp->f_metadata = NULL;
453 fp->f_addr = loadaddr;
454 fp->f_size = laddr - loadaddr;
456 /* recognise space consumption */
459 /* Add to the list of loaded files */
461 file_insert_tail(fp);
467 * Load the module (name), pass it (argc),(argv), add container file
468 * to the list of loaded files.
469 * If module is already loaded just assign new argc/argv.
472 mod_load(char *modname, struct mod_depend *verinfo, int argc, char *argv[])
474 struct kernel_module *mp;
478 if (file_havepath(modname)) {
479 printf("Warning: mod_load() called instead of mod_loadkld() for module '%s'\n", modname);
480 return (mod_loadkld(modname, argc, argv));
482 /* see if module is already loaded */
483 mp = file_findmodule(NULL, modname, verinfo);
488 mp->m_args = unargv(argc, argv);
490 sprintf(command_errbuf, "warning: module '%s' already loaded", mp->m_name);
493 /* locate file with the module on the search path */
494 filename = mod_searchmodule(modname, verinfo);
495 if (filename == NULL) {
496 sprintf(command_errbuf, "can't find '%s'", modname);
499 err = mod_loadkld(filename, argc, argv);
504 * Load specified KLD. If path is omitted, then try to locate it via
508 mod_loadkld(const char *kldname, int argc, char *argv[])
510 struct preloaded_file *fp, *last_file;
515 * Get fully qualified KLD name
517 filename = file_search(kldname, kld_ext_list);
518 if (filename == NULL) {
519 sprintf(command_errbuf, "can't find '%s'", kldname);
523 * Check if KLD already loaded
525 fp = file_findfile(filename, NULL);
527 sprintf(command_errbuf, "warning: KLD '%s' already loaded", filename);
531 for (last_file = preloaded_files;
532 last_file != NULL && last_file->f_next != NULL;
533 last_file = last_file->f_next)
537 err = file_load(filename, loadaddr, &fp);
540 fp->f_args = unargv(argc, argv);
541 loadaddr = fp->f_addr + fp->f_size;
542 file_insert_tail(fp); /* Add to the list of loaded files */
543 if (file_load_dependencies(fp) != 0) {
545 last_file->f_next = NULL;
546 loadaddr = last_file->f_addr + last_file->f_size;
552 sprintf(command_errbuf, "don't know how to load module '%s'", filename);
560 * Find a file matching (name) and (type).
561 * NULL may be passed as a wildcard to either.
563 struct preloaded_file *
564 file_findfile(const char *name, const char *type)
566 struct preloaded_file *fp;
568 for (fp = preloaded_files; fp != NULL; fp = fp->f_next) {
569 if (((name == NULL) || !strcmp(name, fp->f_name)) &&
570 ((type == NULL) || !strcmp(type, fp->f_type)))
577 * Find a module matching (name) inside of given file.
578 * NULL may be passed as a wildcard.
580 struct kernel_module *
581 file_findmodule(struct preloaded_file *fp, char *modname,
582 struct mod_depend *verinfo)
584 struct kernel_module *mp, *best;
588 for (fp = preloaded_files; fp; fp = fp->f_next) {
589 mp = file_findmodule(fp, modname, verinfo);
597 for (mp = fp->f_modules; mp; mp = mp->m_next) {
598 if (strcmp(modname, mp->m_name) == 0) {
601 mver = mp->m_version;
602 if (mver == verinfo->md_ver_preferred)
604 if (mver >= verinfo->md_ver_minimum &&
605 mver <= verinfo->md_ver_maximum &&
615 * Make a copy of (size) bytes of data from (p), and associate them as
616 * metadata of (type) to the module (mp).
619 file_addmetadata(struct preloaded_file *fp, int type, size_t size, void *p)
621 struct file_metadata *md;
623 md = malloc(sizeof(struct file_metadata) - sizeof(md->md_data) + size);
626 bcopy(p, md->md_data, size);
627 md->md_next = fp->f_metadata;
632 * Find a metadata object of (type) associated with the file (fp)
634 struct file_metadata *
635 file_findmetadata(struct preloaded_file *fp, int type)
637 struct file_metadata *md;
639 for (md = fp->f_metadata; md != NULL; md = md->md_next)
640 if (md->md_type == type)
645 struct file_metadata *
646 metadata_next(struct file_metadata *md, int type)
650 while((md = md->md_next) != NULL)
651 if (md->md_type == type)
656 static char *emptyextlist[] = { "", NULL };
659 * Check if the given file is in place and return full path to it.
662 file_lookup(const char *path, const char *name, int namelen, char **extlist)
665 char *result, *cp, **cpp;
666 int pathlen, extlen, len;
668 pathlen = strlen(path);
671 extlist = emptyextlist;
672 for (cpp = extlist; *cpp; cpp++) {
677 result = malloc(pathlen + namelen + extlen + 2);
680 bcopy(path, result, pathlen);
681 if (pathlen > 0 && result[pathlen - 1] != '/')
682 result[pathlen++] = '/';
683 cp = result + pathlen;
684 bcopy(name, cp, namelen);
686 for (cpp = extlist; *cpp; cpp++) {
688 if (stat(result, &st) == 0 && S_ISREG(st.st_mode))
696 * Check if file name have any qualifiers
699 file_havepath(const char *name)
703 archsw.arch_getdev(NULL, name, &cp);
704 return (cp != name || strchr(name, '/') != NULL);
708 * Attempt to find the file (name) on the module searchpath.
709 * If (name) is qualified in any way, we simply check it and
710 * return it or NULL. If it is not qualified, then we attempt
711 * to construct a path using entries in the environment variable
714 * The path we return a pointer to need never be freed, as we manage
718 file_search(const char *name, char **extlist)
720 struct moduledir *mdp;
725 /* Don't look for nothing */
730 return(strdup(name));
732 if (file_havepath(name)) {
733 /* Qualified, so just see if it exists */
734 if (stat(name, &sb) == 0)
735 return(strdup(name));
740 namelen = strlen(name);
741 STAILQ_FOREACH(mdp, &moduledir_list, d_link) {
742 result = file_lookup(mdp->d_path, name, namelen, extlist);
749 #define INT_ALIGN(base, ptr) ptr = \
750 (base) + roundup2((ptr) - (base), sizeof(int))
753 mod_search_hints(struct moduledir *mdp, const char *modname,
754 struct mod_depend *verinfo)
756 u_char *cp, *recptr, *bufend, *best;
758 int *intp, bestver, blen, clen, found, ival, modnamelen, reclen;
760 moduledir_readhints(mdp);
761 modnamelen = strlen(modname);
765 if (mdp->d_hints == NULL)
767 recptr = mdp->d_hints;
768 bufend = recptr + mdp->d_hintsz;
771 while (recptr < bufend && !found) {
779 if (clen != modnamelen || bcmp(cp, modname, clen) != 0)
782 INT_ALIGN(mdp->d_hints, cp);
786 if (verinfo == NULL || ival == verinfo->md_ver_preferred) {
790 if (ival >= verinfo->md_ver_minimum &&
791 ival <= verinfo->md_ver_maximum &&
801 recptr += reclen + sizeof(int);
804 * Finally check if KLD is in the place
807 result = file_lookup(mdp->d_path, (const char *)cp, clen, NULL);
809 result = file_lookup(mdp->d_path, (const char *)best, blen, NULL);
812 * If nothing found or hints is absent - fallback to the old way
813 * by using "kldname[.ko]" as module name.
815 if (!found && !bestver && result == NULL)
816 result = file_lookup(mdp->d_path, modname, modnamelen, kld_ext_list);
821 * Attempt to locate the file containing the module (name)
824 mod_searchmodule(char *name, struct mod_depend *verinfo)
826 struct moduledir *mdp;
831 * Now we ready to lookup module in the given directories
834 STAILQ_FOREACH(mdp, &moduledir_list, d_link) {
835 result = mod_search_hints(mdp, name, verinfo);
844 file_addmodule(struct preloaded_file *fp, char *modname, int version,
845 struct kernel_module **newmp)
847 struct kernel_module *mp;
848 struct mod_depend mdepend;
850 bzero(&mdepend, sizeof(mdepend));
851 mdepend.md_ver_preferred = version;
852 mp = file_findmodule(fp, modname, &mdepend);
855 mp = malloc(sizeof(struct kernel_module));
858 bzero(mp, sizeof(struct kernel_module));
859 mp->m_name = strdup(modname);
860 mp->m_version = version;
862 mp->m_next = fp->f_modules;
873 file_discard(struct preloaded_file *fp)
875 struct file_metadata *md, *md1;
876 struct kernel_module *mp, *mp1;
893 if (fp->f_name != NULL)
895 if (fp->f_type != NULL)
897 if (fp->f_args != NULL)
903 * Allocate a new file; must be used instead of malloc()
904 * to ensure safe initialisation.
906 struct preloaded_file *
909 struct preloaded_file *fp;
911 if ((fp = malloc(sizeof(struct preloaded_file))) != NULL) {
912 bzero(fp, sizeof(struct preloaded_file));
918 * Add a module to the chain
921 file_insert_tail(struct preloaded_file *fp)
923 struct preloaded_file *cm;
925 /* Append to list of loaded file */
927 if (preloaded_files == NULL) {
928 preloaded_files = fp;
930 for (cm = preloaded_files; cm->f_next != NULL; cm = cm->f_next)
937 moduledir_fullpath(struct moduledir *mdp, const char *fname)
941 cp = malloc(strlen(mdp->d_path) + strlen(fname) + 2);
944 strcpy(cp, mdp->d_path);
951 * Read linker.hints file into memory performing some sanity checks.
954 moduledir_readhints(struct moduledir *mdp)
958 int fd, size, version;
960 if (mdp->d_hints != NULL || (mdp->d_flags & MDIR_NOHINTS))
962 path = moduledir_fullpath(mdp, "linker.hints");
963 if (stat(path, &st) != 0 ||
964 st.st_size < (ssize_t)(sizeof(version) + sizeof(int)) ||
965 st.st_size > LINKER_HINTS_MAX || (fd = open(path, O_RDONLY)) < 0) {
967 mdp->d_flags |= MDIR_NOHINTS;
971 size = read(fd, &version, sizeof(version));
972 if (size != sizeof(version) || version != LINKER_HINTS_VERSION)
974 size = st.st_size - size;
975 mdp->d_hints = malloc(size);
976 if (mdp->d_hints == NULL)
978 if (read(fd, mdp->d_hints, size) != size)
980 mdp->d_hintsz = size;
989 mdp->d_flags |= MDIR_NOHINTS;
994 * Extract directories from the ';' separated list, remove duplicates.
997 moduledir_rebuild(void)
999 struct moduledir *mdp, *mtmp;
1000 const char *path, *cp, *ep;
1003 path = getenv("module_path");
1005 path = default_searchpath;
1007 * Rebuild list of module directories if it changed
1009 STAILQ_FOREACH(mdp, &moduledir_list, d_link)
1010 mdp->d_flags |= MDIR_REMOVED;
1012 for (ep = path; *ep != 0; ep++) {
1014 for (; *ep != 0 && *ep != ';'; ep++)
1017 * Ignore trailing slashes
1019 for (cplen = ep - cp; cplen > 1 && cp[cplen - 1] == '/'; cplen--)
1021 STAILQ_FOREACH(mdp, &moduledir_list, d_link) {
1022 if (strlen(mdp->d_path) != cplen || bcmp(cp, mdp->d_path, cplen) != 0)
1024 mdp->d_flags &= ~MDIR_REMOVED;
1028 mdp = malloc(sizeof(*mdp) + cplen + 1);
1031 mdp->d_path = (char*)(mdp + 1);
1032 bcopy(cp, mdp->d_path, cplen);
1033 mdp->d_path[cplen] = 0;
1034 mdp->d_hints = NULL;
1036 STAILQ_INSERT_TAIL(&moduledir_list, mdp, d_link);
1042 * Delete unused directories if any
1044 mdp = STAILQ_FIRST(&moduledir_list);
1046 if ((mdp->d_flags & MDIR_REMOVED) == 0) {
1047 mdp = STAILQ_NEXT(mdp, d_link);
1052 mdp = STAILQ_NEXT(mdp, d_link);
1053 STAILQ_REMOVE(&moduledir_list, mtmp, moduledir, d_link);