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;
109 int dflag, dofile, dokld, ch, error;
111 dflag = dokld = dofile = 0;
116 command_errmsg = "no filename specified";
119 prefix = skip = NULL;
120 while ((ch = getopt(argc, argv, "dkp:s:t:")) != -1) {
140 /* getopt has already reported an error */
144 argv += (optind - 1);
145 argc -= (optind - 1);
148 * Request to load a raw file?
151 if ((argc != 2) || (typestr == NULL) || (*typestr == 0)) {
152 command_errmsg = "invalid load type";
156 #ifdef LOADER_VERIEXEC
157 if (strncmp(typestr, "manifest", 8) == 0) {
160 return (load_manifest(argv[1], prefix, skip, NULL));
162 #ifdef LOADER_VERIEXEC_PASS_MANIFEST
163 if (strncmp(typestr, "pass_manifest", 13) == 0) {
166 return (pass_manifest(argv[1], prefix));
171 fp = file_findfile(argv[1], typestr);
173 snprintf(command_errbuf, sizeof(command_errbuf),
174 "warning: file '%s' already loaded", argv[1]);
178 if (file_loadraw(argv[1], typestr, 1) != NULL)
181 /* Failing to load mfs_root is never going to end well! */
182 if (strcmp("mfs_root", typestr) == 0)
188 * Do we have explicit KLD load ?
190 if (dokld || file_havepath(argv[1])) {
191 error = mod_loadkld(argv[1], argc - 2, argv + 2);
192 if (error == EEXIST) {
193 snprintf(command_errbuf, sizeof(command_errbuf),
194 "warning: KLD '%s' already loaded", argv[1]);
198 return (error == 0 ? CMD_OK : CMD_CRIT);
201 * Looks like a request for a module.
203 error = mod_load(argv[1], NULL, argc - 2, argv + 2);
204 if (error == EEXIST) {
205 snprintf(command_errbuf, sizeof(command_errbuf),
206 "warning: module '%s' already loaded", argv[1]);
210 return (error == 0 ? CMD_OK : CMD_CRIT);
213 #ifdef LOADER_GELI_SUPPORT
214 COMMAND_SET(load_geli, "load_geli", "load a geli key", command_load_geli);
217 command_load_geli(int argc, char *argv[])
224 command_errmsg = "usage is [-n key#] <prov> <file>";
231 while ((ch = getopt(argc, argv, "n:")) != -1) {
234 num = strtol(optarg, &cp, 0);
236 snprintf(command_errbuf, sizeof(command_errbuf),
237 "bad key index '%s'", optarg);
243 /* getopt has already reported an error */
247 argv += (optind - 1);
248 argc -= (optind - 1);
249 sprintf(typestr, "%s:geli_keyfile%d", argv[1], num);
250 return (file_loadraw(argv[2], typestr, 1) ? CMD_OK : CMD_ERROR);
257 struct preloaded_file *fp;
259 while (preloaded_files != NULL) {
260 fp = preloaded_files;
261 preloaded_files = preloaded_files->f_next;
265 unsetenv("kernelname");
268 COMMAND_SET(unload, "unload", "unload all modules", command_unload);
271 command_unload(int argc, char *argv[])
277 COMMAND_SET(lsmod, "lsmod", "list loaded modules", command_lsmod);
280 command_lsmod(int argc, char *argv[])
282 struct preloaded_file *fp;
283 struct kernel_module *mp;
284 struct file_metadata *md;
286 int ch, verbose, ret = 0;
291 while ((ch = getopt(argc, argv, "v")) != -1) {
298 /* getopt has already reported an error */
304 for (fp = preloaded_files; fp; fp = fp->f_next) {
305 snprintf(lbuf, sizeof(lbuf), " %p: ", (void *) fp->f_addr);
307 pager_output(fp->f_name);
308 snprintf(lbuf, sizeof(lbuf), " (%s, 0x%lx)\n", fp->f_type,
310 if (pager_output(lbuf))
312 if (fp->f_args != NULL) {
313 pager_output(" args: ");
314 pager_output(fp->f_args);
315 if (pager_output("\n"))
319 pager_output(" modules: ");
320 for (mp = fp->f_modules; mp; mp = mp->m_next) {
321 snprintf(lbuf, sizeof(lbuf), "%s.%d ", mp->m_name,
325 if (pager_output("\n"))
329 /* XXX could add some formatting smarts here to display some better */
330 for (md = fp->f_metadata; md != NULL; md = md->md_next) {
331 snprintf(lbuf, sizeof(lbuf), " 0x%04x, 0x%lx\n",
332 md->md_type, (long) md->md_size);
333 if (pager_output(lbuf))
345 * File level interface, functions file_*
348 file_load(char *filename, vm_offset_t dest, struct preloaded_file **result)
350 static int last_file_format = 0;
351 struct preloaded_file *fp;
355 if (archsw.arch_loadaddr != NULL)
356 dest = archsw.arch_loadaddr(LOAD_RAW, filename, dest);
359 for (i = last_file_format, fp = NULL;
360 file_formats[i] && fp == NULL; i++) {
361 error = (file_formats[i]->l_load)(filename, dest, &fp);
363 fp->f_loader = last_file_format = i; /* remember the loader */
366 } else if (last_file_format == i && i != 0) {
367 /* Restart from the beginning */
369 last_file_format = 0;
374 continue; /* Unknown to this handler? */
376 snprintf(command_errbuf, sizeof(command_errbuf),
377 "can't load file '%s': %s", filename, strerror(error));
385 file_load_dependencies(struct preloaded_file *base_file)
387 struct file_metadata *md;
388 struct preloaded_file *fp;
389 struct mod_depend *verinfo;
390 struct kernel_module *mp;
394 md = file_findmetadata(base_file, MODINFOMD_DEPLIST);
399 verinfo = (struct mod_depend*)md->md_data;
400 dmodname = (char *)(verinfo + 1);
401 if (file_findmodule(NULL, dmodname, verinfo) == NULL) {
402 printf("loading required module '%s'\n", dmodname);
403 error = mod_load(dmodname, verinfo, 0, NULL);
407 * If module loaded via kld name which isn't listed
408 * in the linker.hints file, we should check if it have
411 mp = file_findmodule(NULL, dmodname, verinfo);
413 snprintf(command_errbuf, sizeof(command_errbuf),
414 "module '%s' exists but with wrong version", dmodname);
419 md = metadata_next(md, MODINFOMD_DEPLIST);
423 /* Load failed; discard everything */
424 while (base_file != NULL) {
426 base_file = base_file->f_next;
433 * We've been asked to load (fname) as (type), so just suck it in,
434 * no arguments or anything.
436 struct preloaded_file *
437 file_loadraw(const char *fname, char *type, int insert)
439 struct preloaded_file *fp;
444 /* We can't load first */
445 if ((file_findfile(NULL, NULL)) == NULL) {
446 command_errmsg = "can't load file before kernel";
450 /* locate the file on the load path */
451 name = file_search(fname, NULL);
453 snprintf(command_errbuf, sizeof(command_errbuf),
454 "can't find '%s'", fname);
458 if ((fd = open(name, O_RDONLY)) < 0) {
459 snprintf(command_errbuf, sizeof(command_errbuf),
460 "can't open '%s': %s", name, strerror(errno));
465 #ifdef LOADER_VERIEXEC
466 if (verify_file(fd, name, 0, VE_MUST) < 0) {
467 sprintf(command_errbuf, "can't verify '%s'", name);
474 if (archsw.arch_loadaddr != NULL)
475 loadaddr = archsw.arch_loadaddr(LOAD_RAW, name, loadaddr);
481 /* read in 4k chunks; size is not really important */
482 got = archsw.arch_readin(fd, laddr, 4096);
483 if (got == 0) /* end of file */
485 if (got < 0) { /* error */
486 snprintf(command_errbuf, sizeof(command_errbuf),
487 "error reading '%s': %s", name, strerror(errno));
495 printf("size=%#jx\n", (uintmax_t)(laddr - loadaddr));
497 /* Looks OK so far; create & populate control structure */
499 fp->f_name = strdup(name);
500 fp->f_type = strdup(type);
502 fp->f_metadata = NULL;
504 fp->f_addr = loadaddr;
505 fp->f_size = laddr - loadaddr;
507 /* recognise space consumption */
510 /* Add to the list of loaded files */
512 file_insert_tail(fp);
518 * Load the module (name), pass it (argc),(argv), add container file
519 * to the list of loaded files.
520 * If module is already loaded just assign new argc/argv.
523 mod_load(char *modname, struct mod_depend *verinfo, int argc, char *argv[])
525 struct kernel_module *mp;
529 if (file_havepath(modname)) {
530 printf("Warning: mod_load() called instead of mod_loadkld() for module '%s'\n", modname);
531 return (mod_loadkld(modname, argc, argv));
533 /* see if module is already loaded */
534 mp = file_findmodule(NULL, modname, verinfo);
538 mp->m_args = unargv(argc, argv);
540 snprintf(command_errbuf, sizeof(command_errbuf),
541 "warning: module '%s' already loaded", mp->m_name);
544 /* locate file with the module on the search path */
545 filename = mod_searchmodule(modname, verinfo);
546 if (filename == NULL) {
547 snprintf(command_errbuf, sizeof(command_errbuf),
548 "can't find '%s'", modname);
551 err = mod_loadkld(filename, argc, argv);
556 * Load specified KLD. If path is omitted, then try to locate it via
560 mod_loadkld(const char *kldname, int argc, char *argv[])
562 struct preloaded_file *fp, *last_file;
567 * Get fully qualified KLD name
569 filename = file_search(kldname, kld_ext_list);
570 if (filename == NULL) {
571 snprintf(command_errbuf, sizeof(command_errbuf),
572 "can't find '%s'", kldname);
576 * Check if KLD already loaded
578 fp = file_findfile(filename, NULL);
580 snprintf(command_errbuf, sizeof(command_errbuf),
581 "warning: KLD '%s' already loaded", filename);
585 for (last_file = preloaded_files;
586 last_file != NULL && last_file->f_next != NULL;
587 last_file = last_file->f_next)
591 err = file_load(filename, loadaddr, &fp);
594 fp->f_args = unargv(argc, argv);
595 loadaddr = fp->f_addr + fp->f_size;
596 file_insert_tail(fp); /* Add to the list of loaded files */
597 if (file_load_dependencies(fp) != 0) {
599 last_file->f_next = NULL;
600 loadaddr = last_file->f_addr + last_file->f_size;
606 snprintf(command_errbuf, sizeof(command_errbuf),
607 "don't know how to load module '%s'", filename);
616 * Find a file matching (name) and (type).
617 * NULL may be passed as a wildcard to either.
619 struct preloaded_file *
620 file_findfile(const char *name, const char *type)
622 struct preloaded_file *fp;
624 for (fp = preloaded_files; fp != NULL; fp = fp->f_next) {
625 if (((name == NULL) || !strcmp(name, fp->f_name)) &&
626 ((type == NULL) || !strcmp(type, fp->f_type)))
633 * Find a module matching (name) inside of given file.
634 * NULL may be passed as a wildcard.
636 struct kernel_module *
637 file_findmodule(struct preloaded_file *fp, char *modname,
638 struct mod_depend *verinfo)
640 struct kernel_module *mp, *best;
644 for (fp = preloaded_files; fp; fp = fp->f_next) {
645 mp = file_findmodule(fp, modname, verinfo);
653 for (mp = fp->f_modules; mp; mp = mp->m_next) {
654 if (strcmp(modname, mp->m_name) == 0) {
657 mver = mp->m_version;
658 if (mver == verinfo->md_ver_preferred)
660 if (mver >= verinfo->md_ver_minimum &&
661 mver <= verinfo->md_ver_maximum &&
671 * Make a copy of (size) bytes of data from (p), and associate them as
672 * metadata of (type) to the module (mp).
675 file_addmetadata(struct preloaded_file *fp, int type, size_t size, void *p)
677 struct file_metadata *md;
679 md = malloc(sizeof(struct file_metadata) - sizeof(md->md_data) + size);
683 bcopy(p, md->md_data, size);
684 md->md_next = fp->f_metadata;
690 * Find a metadata object of (type) associated with the file (fp)
692 struct file_metadata *
693 file_findmetadata(struct preloaded_file *fp, int type)
695 struct file_metadata *md;
697 for (md = fp->f_metadata; md != NULL; md = md->md_next)
698 if (md->md_type == type)
704 * Remove all metadata from the file.
707 file_removemetadata(struct preloaded_file *fp)
709 struct file_metadata *md, *next;
711 for (md = fp->f_metadata; md != NULL; md = next)
716 fp->f_metadata = NULL;
719 struct file_metadata *
720 metadata_next(struct file_metadata *md, int type)
725 while((md = md->md_next) != NULL)
726 if (md->md_type == type)
731 static char *emptyextlist[] = { "", NULL };
734 * Check if the given file is in place and return full path to it.
737 file_lookup(const char *path, const char *name, int namelen, char **extlist)
740 char *result, *cp, **cpp;
741 int pathlen, extlen, len;
743 pathlen = strlen(path);
746 extlist = emptyextlist;
747 for (cpp = extlist; *cpp; cpp++) {
752 result = malloc(pathlen + namelen + extlen + 2);
755 bcopy(path, result, pathlen);
756 if (pathlen > 0 && result[pathlen - 1] != '/')
757 result[pathlen++] = '/';
758 cp = result + pathlen;
759 bcopy(name, cp, namelen);
761 for (cpp = extlist; *cpp; cpp++) {
763 if (stat(result, &st) == 0 && S_ISREG(st.st_mode))
771 * Check if file name have any qualifiers
774 file_havepath(const char *name)
778 archsw.arch_getdev(NULL, name, &cp);
779 return (cp != name || strchr(name, '/') != NULL);
783 * Attempt to find the file (name) on the module searchpath.
784 * If (name) is qualified in any way, we simply check it and
785 * return it or NULL. If it is not qualified, then we attempt
786 * to construct a path using entries in the environment variable
789 * The path we return a pointer to need never be freed, as we manage
793 file_search(const char *name, char **extlist)
795 struct moduledir *mdp;
800 /* Don't look for nothing */
805 return(strdup(name));
807 if (file_havepath(name)) {
808 /* Qualified, so just see if it exists */
809 if (stat(name, &sb) == 0)
810 return(strdup(name));
815 namelen = strlen(name);
816 STAILQ_FOREACH(mdp, &moduledir_list, d_link) {
817 result = file_lookup(mdp->d_path, name, namelen, extlist);
824 #define INT_ALIGN(base, ptr) ptr = \
825 (base) + roundup2((ptr) - (base), sizeof(int))
828 mod_search_hints(struct moduledir *mdp, const char *modname,
829 struct mod_depend *verinfo)
831 u_char *cp, *recptr, *bufend, *best;
833 int *intp, bestver, blen, clen, found, ival, modnamelen, reclen;
835 moduledir_readhints(mdp);
836 modnamelen = strlen(modname);
840 if (mdp->d_hints == NULL)
842 recptr = mdp->d_hints;
843 bufend = recptr + mdp->d_hintsz;
846 while (recptr < bufend && !found) {
854 if (clen != modnamelen || bcmp(cp, modname, clen) != 0)
857 INT_ALIGN(mdp->d_hints, cp);
861 if (verinfo == NULL || ival == verinfo->md_ver_preferred) {
865 if (ival >= verinfo->md_ver_minimum &&
866 ival <= verinfo->md_ver_maximum &&
876 recptr += reclen + sizeof(int);
879 * Finally check if KLD is in the place
882 result = file_lookup(mdp->d_path, (const char *)cp, clen, NULL);
884 result = file_lookup(mdp->d_path, (const char *)best, blen, NULL);
887 * If nothing found or hints is absent - fallback to the old way
888 * by using "kldname[.ko]" as module name.
890 if (!found && !bestver && result == NULL)
891 result = file_lookup(mdp->d_path, modname, modnamelen, kld_ext_list);
896 * Attempt to locate the file containing the module (name)
899 mod_searchmodule(char *name, struct mod_depend *verinfo)
901 struct moduledir *mdp;
906 * Now we ready to lookup module in the given directories
909 STAILQ_FOREACH(mdp, &moduledir_list, d_link) {
910 result = mod_search_hints(mdp, name, verinfo);
919 file_addmodule(struct preloaded_file *fp, char *modname, int version,
920 struct kernel_module **newmp)
922 struct kernel_module *mp;
923 struct mod_depend mdepend;
925 bzero(&mdepend, sizeof(mdepend));
926 mdepend.md_ver_preferred = version;
927 mp = file_findmodule(fp, modname, &mdepend);
930 mp = calloc(1, sizeof(struct kernel_module));
933 mp->m_name = strdup(modname);
934 if (mp->m_name == NULL) {
938 mp->m_version = version;
940 mp->m_next = fp->f_modules;
951 file_discard(struct preloaded_file *fp)
953 struct file_metadata *md, *md1;
954 struct kernel_module *mp, *mp1;
977 * Allocate a new file; must be used instead of malloc()
978 * to ensure safe initialisation.
980 struct preloaded_file *
984 return (calloc(1, sizeof(struct preloaded_file)));
988 * Add a module to the chain
991 file_insert_tail(struct preloaded_file *fp)
993 struct preloaded_file *cm;
995 /* Append to list of loaded file */
997 if (preloaded_files == NULL) {
998 preloaded_files = fp;
1000 for (cm = preloaded_files; cm->f_next != NULL; cm = cm->f_next)
1007 moduledir_fullpath(struct moduledir *mdp, const char *fname)
1011 cp = malloc(strlen(mdp->d_path) + strlen(fname) + 2);
1014 strcpy(cp, mdp->d_path);
1021 * Read linker.hints file into memory performing some sanity checks.
1024 moduledir_readhints(struct moduledir *mdp)
1028 int fd, size, version;
1030 if (mdp->d_hints != NULL || (mdp->d_flags & MDIR_NOHINTS))
1032 path = moduledir_fullpath(mdp, "linker.hints");
1033 if (stat(path, &st) != 0 ||
1034 st.st_size < (ssize_t)(sizeof(version) + sizeof(int)) ||
1035 st.st_size > LINKER_HINTS_MAX || (fd = open(path, O_RDONLY)) < 0) {
1037 mdp->d_flags |= MDIR_NOHINTS;
1041 size = read(fd, &version, sizeof(version));
1042 if (size != sizeof(version) || version != LINKER_HINTS_VERSION)
1044 size = st.st_size - size;
1045 mdp->d_hints = malloc(size);
1046 if (mdp->d_hints == NULL)
1048 if (read(fd, mdp->d_hints, size) != size)
1050 mdp->d_hintsz = size;
1056 mdp->d_hints = NULL;
1057 mdp->d_flags |= MDIR_NOHINTS;
1062 * Extract directories from the ';' separated list, remove duplicates.
1065 moduledir_rebuild(void)
1067 struct moduledir *mdp, *mtmp;
1068 const char *path, *cp, *ep;
1071 path = getenv("module_path");
1073 path = default_searchpath;
1075 * Rebuild list of module directories if it changed
1077 STAILQ_FOREACH(mdp, &moduledir_list, d_link)
1078 mdp->d_flags |= MDIR_REMOVED;
1080 for (ep = path; *ep != 0; ep++) {
1082 for (; *ep != 0 && *ep != ';'; ep++)
1085 * Ignore trailing slashes
1087 for (cplen = ep - cp; cplen > 1 && cp[cplen - 1] == '/'; cplen--)
1089 STAILQ_FOREACH(mdp, &moduledir_list, d_link) {
1090 if (strlen(mdp->d_path) != cplen || bcmp(cp, mdp->d_path, cplen) != 0)
1092 mdp->d_flags &= ~MDIR_REMOVED;
1096 mdp = malloc(sizeof(*mdp) + cplen + 1);
1099 mdp->d_path = (char*)(mdp + 1);
1100 bcopy(cp, mdp->d_path, cplen);
1101 mdp->d_path[cplen] = 0;
1102 mdp->d_hints = NULL;
1104 STAILQ_INSERT_TAIL(&moduledir_list, mdp, d_link);
1110 * Delete unused directories if any
1112 mdp = STAILQ_FIRST(&moduledir_list);
1114 if ((mdp->d_flags & MDIR_REMOVED) == 0) {
1115 mdp = STAILQ_NEXT(mdp, d_link);
1119 mdp = STAILQ_NEXT(mdp, d_link);
1120 STAILQ_REMOVE(&moduledir_list, mtmp, moduledir, d_link);