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_loadraw(char *type, char *name);
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 static const char *default_searchpath ="/boot/kernel;/boot/modules";
71 static STAILQ_HEAD(, moduledir) moduledir_list = STAILQ_HEAD_INITIALIZER(moduledir_list);
73 struct preloaded_file *preloaded_files = NULL;
75 static char *kld_ext_list[] = {
84 * load an object, either a disk file or code module.
86 * To load a file, the syntax is:
88 * load -t <type> <path>
90 * code modules are loaded as:
92 * load <path> <options>
95 COMMAND_SET(load, "load", "load a kernel or module", command_load);
98 command_load(int argc, char *argv[])
101 int dofile, dokld, ch, error;
108 command_errmsg = "no filename specified";
111 while ((ch = getopt(argc, argv, "kt:")) != -1) {
122 /* getopt has already reported an error */
126 argv += (optind - 1);
127 argc -= (optind - 1);
130 * Request to load a raw file?
133 if ((argc != 2) || (typestr == NULL) || (*typestr == 0)) {
134 command_errmsg = "invalid load type";
137 return(file_loadraw(typestr, argv[1]));
140 * Do we have explicit KLD load ?
142 if (dokld || file_havepath(argv[1])) {
143 error = mod_loadkld(argv[1], argc - 2, argv + 2);
145 sprintf(command_errbuf, "warning: KLD '%s' already loaded", argv[1]);
146 return (error == 0 ? CMD_OK : CMD_ERROR);
149 * Looks like a request for a module.
151 error = mod_load(argv[1], NULL, argc - 2, argv + 2);
153 sprintf(command_errbuf, "warning: module '%s' already loaded", argv[1]);
154 return (error == 0 ? CMD_OK : CMD_ERROR);
157 COMMAND_SET(load_geli, "load_geli", "load a geli key", command_load_geli);
160 command_load_geli(int argc, char *argv[])
167 command_errmsg = "usage is [-n key#] <prov> <file>";
174 while ((ch = getopt(argc, argv, "n:")) != -1) {
177 num = strtol(optarg, &cp, 0);
179 sprintf(command_errbuf, "bad key index '%s'", optarg);
185 /* getopt has already reported an error */
189 argv += (optind - 1);
190 argc -= (optind - 1);
191 sprintf(typestr, "%s:geli_keyfile%d", argv[1], num);
192 return(file_loadraw(typestr, argv[2]));
195 COMMAND_SET(unload, "unload", "unload all modules", command_unload);
198 command_unload(int argc, char *argv[])
200 struct preloaded_file *fp;
202 while (preloaded_files != NULL) {
203 fp = preloaded_files;
204 preloaded_files = preloaded_files->f_next;
208 unsetenv("kernelname");
212 COMMAND_SET(lsmod, "lsmod", "list loaded modules", command_lsmod);
215 command_lsmod(int argc, char *argv[])
217 struct preloaded_file *fp;
218 struct kernel_module *mp;
219 struct file_metadata *md;
226 while ((ch = getopt(argc, argv, "v")) != -1) {
233 /* getopt has already reported an error */
239 for (fp = preloaded_files; fp; fp = fp->f_next) {
240 sprintf(lbuf, " %p: %s (%s, 0x%lx)\n",
241 (void *) fp->f_addr, fp->f_name, fp->f_type, (long) fp->f_size);
243 if (fp->f_args != NULL) {
244 pager_output(" args: ");
245 pager_output(fp->f_args);
249 pager_output(" modules: ");
250 for (mp = fp->f_modules; mp; mp = mp->m_next) {
251 sprintf(lbuf, "%s.%d ", mp->m_name, mp->m_version);
257 /* XXX could add some formatting smarts here to display some better */
258 for (md = fp->f_metadata; md != NULL; md = md->md_next) {
259 sprintf(lbuf, " 0x%04x, 0x%lx\n", md->md_type, (long) md->md_size);
269 * File level interface, functions file_*
272 file_load(char *filename, vm_offset_t dest, struct preloaded_file **result)
274 static int last_file_format = 0;
275 struct preloaded_file *fp;
279 if (archsw.arch_loadaddr != NULL)
280 dest = archsw.arch_loadaddr(LOAD_RAW, filename, dest);
283 for (i = last_file_format, fp = NULL;
284 file_formats[i] && fp == NULL; i++) {
285 error = (file_formats[i]->l_load)(filename, dest, &fp);
287 fp->f_loader = last_file_format = i; /* remember the loader */
290 } else if (last_file_format == i && i != 0) {
291 /* Restart from the beginning */
293 last_file_format = 0;
298 continue; /* Unknown to this handler? */
300 sprintf(command_errbuf, "can't load file '%s': %s",
301 filename, strerror(error));
309 file_load_dependencies(struct preloaded_file *base_file)
311 struct file_metadata *md;
312 struct preloaded_file *fp;
313 struct mod_depend *verinfo;
314 struct kernel_module *mp;
318 md = file_findmetadata(base_file, MODINFOMD_DEPLIST);
323 verinfo = (struct mod_depend*)md->md_data;
324 dmodname = (char *)(verinfo + 1);
325 if (file_findmodule(NULL, dmodname, verinfo) == NULL) {
326 printf("loading required module '%s'\n", dmodname);
327 error = mod_load(dmodname, verinfo, 0, NULL);
331 * If module loaded via kld name which isn't listed
332 * in the linker.hints file, we should check if it have
335 mp = file_findmodule(NULL, dmodname, verinfo);
337 sprintf(command_errbuf, "module '%s' exists but with wrong version",
343 md = metadata_next(md, MODINFOMD_DEPLIST);
347 /* Load failed; discard everything */
348 while (base_file != NULL) {
350 base_file = base_file->f_next;
356 * We've been asked to load (name) as (type), so just suck it in,
357 * no arguments or anything.
360 file_loadraw(char *type, char *name)
362 struct preloaded_file *fp;
367 /* We can't load first */
368 if ((file_findfile(NULL, NULL)) == NULL) {
369 command_errmsg = "can't load file before kernel";
373 /* locate the file on the load path */
374 cp = file_search(name, NULL);
376 sprintf(command_errbuf, "can't find '%s'", name);
381 if ((fd = open(name, O_RDONLY)) < 0) {
382 sprintf(command_errbuf, "can't open '%s': %s", name, strerror(errno));
387 if (archsw.arch_loadaddr != NULL)
388 loadaddr = archsw.arch_loadaddr(LOAD_RAW, name, loadaddr);
392 /* read in 4k chunks; size is not really important */
393 got = archsw.arch_readin(fd, laddr, 4096);
394 if (got == 0) /* end of file */
396 if (got < 0) { /* error */
397 sprintf(command_errbuf, "error reading '%s': %s", name, strerror(errno));
405 /* Looks OK so far; create & populate control structure */
407 fp->f_name = strdup(name);
408 fp->f_type = strdup(type);
410 fp->f_metadata = NULL;
412 fp->f_addr = loadaddr;
413 fp->f_size = laddr - loadaddr;
415 /* recognise space consumption */
418 /* Add to the list of loaded files */
419 file_insert_tail(fp);
425 * Load the module (name), pass it (argc),(argv), add container file
426 * to the list of loaded files.
427 * If module is already loaded just assign new argc/argv.
430 mod_load(char *modname, struct mod_depend *verinfo, int argc, char *argv[])
432 struct kernel_module *mp;
436 if (file_havepath(modname)) {
437 printf("Warning: mod_load() called instead of mod_loadkld() for module '%s'\n", modname);
438 return (mod_loadkld(modname, argc, argv));
440 /* see if module is already loaded */
441 mp = file_findmodule(NULL, modname, verinfo);
446 mp->m_args = unargv(argc, argv);
448 sprintf(command_errbuf, "warning: module '%s' already loaded", mp->m_name);
451 /* locate file with the module on the search path */
452 filename = mod_searchmodule(modname, verinfo);
453 if (filename == NULL) {
454 sprintf(command_errbuf, "can't find '%s'", modname);
457 err = mod_loadkld(filename, argc, argv);
462 * Load specified KLD. If path is omitted, then try to locate it via
466 mod_loadkld(const char *kldname, int argc, char *argv[])
468 struct preloaded_file *fp, *last_file;
473 * Get fully qualified KLD name
475 filename = file_search(kldname, kld_ext_list);
476 if (filename == NULL) {
477 sprintf(command_errbuf, "can't find '%s'", kldname);
481 * Check if KLD already loaded
483 fp = file_findfile(filename, NULL);
485 sprintf(command_errbuf, "warning: KLD '%s' already loaded", filename);
489 for (last_file = preloaded_files;
490 last_file != NULL && last_file->f_next != NULL;
491 last_file = last_file->f_next)
495 err = file_load(filename, loadaddr, &fp);
498 fp->f_args = unargv(argc, argv);
499 loadaddr = fp->f_addr + fp->f_size;
500 file_insert_tail(fp); /* Add to the list of loaded files */
501 if (file_load_dependencies(fp) != 0) {
503 last_file->f_next = NULL;
504 loadaddr = last_file->f_addr + last_file->f_size;
510 sprintf(command_errbuf, "don't know how to load module '%s'", filename);
518 * Find a file matching (name) and (type).
519 * NULL may be passed as a wildcard to either.
521 struct preloaded_file *
522 file_findfile(char *name, char *type)
524 struct preloaded_file *fp;
526 for (fp = preloaded_files; fp != NULL; fp = fp->f_next) {
527 if (((name == NULL) || !strcmp(name, fp->f_name)) &&
528 ((type == NULL) || !strcmp(type, fp->f_type)))
535 * Find a module matching (name) inside of given file.
536 * NULL may be passed as a wildcard.
538 struct kernel_module *
539 file_findmodule(struct preloaded_file *fp, char *modname,
540 struct mod_depend *verinfo)
542 struct kernel_module *mp, *best;
546 for (fp = preloaded_files; fp; fp = fp->f_next) {
547 mp = file_findmodule(fp, modname, verinfo);
555 for (mp = fp->f_modules; mp; mp = mp->m_next) {
556 if (strcmp(modname, mp->m_name) == 0) {
559 mver = mp->m_version;
560 if (mver == verinfo->md_ver_preferred)
562 if (mver >= verinfo->md_ver_minimum &&
563 mver <= verinfo->md_ver_maximum &&
573 * Make a copy of (size) bytes of data from (p), and associate them as
574 * metadata of (type) to the module (mp).
577 file_addmetadata(struct preloaded_file *fp, int type, size_t size, void *p)
579 struct file_metadata *md;
581 md = malloc(sizeof(struct file_metadata) - sizeof(md->md_data) + size);
584 bcopy(p, md->md_data, size);
585 md->md_next = fp->f_metadata;
590 * Find a metadata object of (type) associated with the file (fp)
592 struct file_metadata *
593 file_findmetadata(struct preloaded_file *fp, int type)
595 struct file_metadata *md;
597 for (md = fp->f_metadata; md != NULL; md = md->md_next)
598 if (md->md_type == type)
603 struct file_metadata *
604 metadata_next(struct file_metadata *md, int type)
608 while((md = md->md_next) != NULL)
609 if (md->md_type == type)
614 static char *emptyextlist[] = { "", NULL };
617 * Check if the given file is in place and return full path to it.
620 file_lookup(const char *path, const char *name, int namelen, char **extlist)
623 char *result, *cp, **cpp;
624 int pathlen, extlen, len;
626 pathlen = strlen(path);
629 extlist = emptyextlist;
630 for (cpp = extlist; *cpp; cpp++) {
635 result = malloc(pathlen + namelen + extlen + 2);
638 bcopy(path, result, pathlen);
639 if (pathlen > 0 && result[pathlen - 1] != '/')
640 result[pathlen++] = '/';
641 cp = result + pathlen;
642 bcopy(name, cp, namelen);
644 for (cpp = extlist; *cpp; cpp++) {
646 if (stat(result, &st) == 0 && S_ISREG(st.st_mode))
654 * Check if file name have any qualifiers
657 file_havepath(const char *name)
661 archsw.arch_getdev(NULL, name, &cp);
662 return (cp != name || strchr(name, '/') != NULL);
666 * Attempt to find the file (name) on the module searchpath.
667 * If (name) is qualified in any way, we simply check it and
668 * return it or NULL. If it is not qualified, then we attempt
669 * to construct a path using entries in the environment variable
672 * The path we return a pointer to need never be freed, as we manage
676 file_search(const char *name, char **extlist)
678 struct moduledir *mdp;
683 /* Don't look for nothing */
688 return(strdup(name));
690 if (file_havepath(name)) {
691 /* Qualified, so just see if it exists */
692 if (stat(name, &sb) == 0)
693 return(strdup(name));
698 namelen = strlen(name);
699 STAILQ_FOREACH(mdp, &moduledir_list, d_link) {
700 result = file_lookup(mdp->d_path, name, namelen, extlist);
707 #define INT_ALIGN(base, ptr) ptr = \
708 (base) + (((ptr) - (base) + sizeof(int) - 1) & ~(sizeof(int) - 1))
711 mod_search_hints(struct moduledir *mdp, const char *modname,
712 struct mod_depend *verinfo)
714 u_char *cp, *recptr, *bufend, *best;
716 int *intp, bestver, blen, clen, found, ival, modnamelen, reclen;
718 moduledir_readhints(mdp);
719 modnamelen = strlen(modname);
723 if (mdp->d_hints == NULL)
725 recptr = mdp->d_hints;
726 bufend = recptr + mdp->d_hintsz;
729 while (recptr < bufend && !found) {
737 if (clen != modnamelen || bcmp(cp, modname, clen) != 0)
740 INT_ALIGN(mdp->d_hints, cp);
744 if (verinfo == NULL || ival == verinfo->md_ver_preferred) {
748 if (ival >= verinfo->md_ver_minimum &&
749 ival <= verinfo->md_ver_maximum &&
759 recptr += reclen + sizeof(int);
762 * Finally check if KLD is in the place
765 result = file_lookup(mdp->d_path, cp, clen, NULL);
767 result = file_lookup(mdp->d_path, best, blen, NULL);
770 * If nothing found or hints is absent - fallback to the old way
771 * by using "kldname[.ko]" as module name.
773 if (!found && !bestver && result == NULL)
774 result = file_lookup(mdp->d_path, modname, modnamelen, kld_ext_list);
779 * Attempt to locate the file containing the module (name)
782 mod_searchmodule(char *name, struct mod_depend *verinfo)
784 struct moduledir *mdp;
789 * Now we ready to lookup module in the given directories
792 STAILQ_FOREACH(mdp, &moduledir_list, d_link) {
793 result = mod_search_hints(mdp, name, verinfo);
802 file_addmodule(struct preloaded_file *fp, char *modname, int version,
803 struct kernel_module **newmp)
805 struct kernel_module *mp;
806 struct mod_depend mdepend;
808 bzero(&mdepend, sizeof(mdepend));
809 mdepend.md_ver_preferred = version;
810 mp = file_findmodule(fp, modname, &mdepend);
813 mp = malloc(sizeof(struct kernel_module));
816 bzero(mp, sizeof(struct kernel_module));
817 mp->m_name = strdup(modname);
818 mp->m_version = version;
820 mp->m_next = fp->f_modules;
831 file_discard(struct preloaded_file *fp)
833 struct file_metadata *md, *md1;
834 struct kernel_module *mp, *mp1;
851 if (fp->f_name != NULL)
853 if (fp->f_type != NULL)
855 if (fp->f_args != NULL)
861 * Allocate a new file; must be used instead of malloc()
862 * to ensure safe initialisation.
864 struct preloaded_file *
867 struct preloaded_file *fp;
869 if ((fp = malloc(sizeof(struct preloaded_file))) != NULL) {
870 bzero(fp, sizeof(struct preloaded_file));
876 * Add a module to the chain
879 file_insert_tail(struct preloaded_file *fp)
881 struct preloaded_file *cm;
883 /* Append to list of loaded file */
885 if (preloaded_files == NULL) {
886 preloaded_files = fp;
888 for (cm = preloaded_files; cm->f_next != NULL; cm = cm->f_next)
895 moduledir_fullpath(struct moduledir *mdp, const char *fname)
899 cp = malloc(strlen(mdp->d_path) + strlen(fname) + 2);
902 strcpy(cp, mdp->d_path);
909 * Read linker.hints file into memory performing some sanity checks.
912 moduledir_readhints(struct moduledir *mdp)
916 int fd, size, version;
918 if (mdp->d_hints != NULL || (mdp->d_flags & MDIR_NOHINTS))
920 path = moduledir_fullpath(mdp, "linker.hints");
921 if (stat(path, &st) != 0 ||
922 st.st_size < (ssize_t)(sizeof(version) + sizeof(int)) ||
923 st.st_size > 100 * 1024 || (fd = open(path, O_RDONLY)) < 0) {
925 mdp->d_flags |= MDIR_NOHINTS;
929 size = read(fd, &version, sizeof(version));
930 if (size != sizeof(version) || version != LINKER_HINTS_VERSION)
932 size = st.st_size - size;
933 mdp->d_hints = malloc(size);
934 if (mdp->d_hints == NULL)
936 if (read(fd, mdp->d_hints, size) != size)
938 mdp->d_hintsz = size;
947 mdp->d_flags |= MDIR_NOHINTS;
952 * Extract directories from the ';' separated list, remove duplicates.
955 moduledir_rebuild(void)
957 struct moduledir *mdp, *mtmp;
958 const char *path, *cp, *ep;
961 path = getenv("module_path");
963 path = default_searchpath;
965 * Rebuild list of module directories if it changed
967 STAILQ_FOREACH(mdp, &moduledir_list, d_link)
968 mdp->d_flags |= MDIR_REMOVED;
970 for (ep = path; *ep != 0; ep++) {
972 for (; *ep != 0 && *ep != ';'; ep++)
975 * Ignore trailing slashes
977 for (cplen = ep - cp; cplen > 1 && cp[cplen - 1] == '/'; cplen--)
979 STAILQ_FOREACH(mdp, &moduledir_list, d_link) {
980 if (strlen(mdp->d_path) != cplen || bcmp(cp, mdp->d_path, cplen) != 0)
982 mdp->d_flags &= ~MDIR_REMOVED;
986 mdp = malloc(sizeof(*mdp) + cplen + 1);
989 mdp->d_path = (char*)(mdp + 1);
990 bcopy(cp, mdp->d_path, cplen);
991 mdp->d_path[cplen] = 0;
994 STAILQ_INSERT_TAIL(&moduledir_list, mdp, d_link);
1000 * Delete unused directories if any
1002 mdp = STAILQ_FIRST(&moduledir_list);
1004 if ((mdp->d_flags & MDIR_REMOVED) == 0) {
1005 mdp = STAILQ_NEXT(mdp, d_link);
1010 mdp = STAILQ_NEXT(mdp, d_link);
1011 STAILQ_REMOVE(&moduledir_list, mtmp, moduledir, d_link);