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[])
105 int dofile, dokld, ch, error;
112 command_errmsg = "no filename specified";
115 while ((ch = getopt(argc, argv, "kt:")) != -1) {
126 /* getopt has already reported an error */
130 argv += (optind - 1);
131 argc -= (optind - 1);
134 * Request to load a raw file?
137 if ((argc != 2) || (typestr == NULL) || (*typestr == 0)) {
138 command_errmsg = "invalid load type";
141 return(file_loadraw(argv[1], typestr) ? CMD_OK : CMD_ERROR);
144 * Do we have explicit KLD load ?
146 if (dokld || file_havepath(argv[1])) {
147 error = mod_loadkld(argv[1], argc - 2, argv + 2);
149 sprintf(command_errbuf, "warning: KLD '%s' already loaded", argv[1]);
150 return (error == 0 ? CMD_OK : CMD_ERROR);
153 * Looks like a request for a module.
155 error = mod_load(argv[1], NULL, argc - 2, argv + 2);
157 sprintf(command_errbuf, "warning: module '%s' already loaded", argv[1]);
158 return (error == 0 ? CMD_OK : CMD_ERROR);
161 COMMAND_SET(load_geli, "load_geli", "load a geli key", command_load_geli);
164 command_load_geli(int argc, char *argv[])
171 command_errmsg = "usage is [-n key#] <prov> <file>";
178 while ((ch = getopt(argc, argv, "n:")) != -1) {
181 num = strtol(optarg, &cp, 0);
183 sprintf(command_errbuf, "bad key index '%s'", optarg);
189 /* getopt has already reported an error */
193 argv += (optind - 1);
194 argc -= (optind - 1);
195 sprintf(typestr, "%s:geli_keyfile%d", argv[1], num);
196 return(file_loadraw(argv[2], typestr) ? CMD_OK : CMD_ERROR);
199 COMMAND_SET(unload, "unload", "unload all modules", command_unload);
202 command_unload(int argc, char *argv[])
204 struct preloaded_file *fp;
206 while (preloaded_files != NULL) {
207 fp = preloaded_files;
208 preloaded_files = preloaded_files->f_next;
212 unsetenv("kernelname");
216 COMMAND_SET(lsmod, "lsmod", "list loaded modules", command_lsmod);
219 command_lsmod(int argc, char *argv[])
221 struct preloaded_file *fp;
222 struct kernel_module *mp;
223 struct file_metadata *md;
230 while ((ch = getopt(argc, argv, "v")) != -1) {
237 /* getopt has already reported an error */
243 for (fp = preloaded_files; fp; fp = fp->f_next) {
244 sprintf(lbuf, " %p: %s (%s, 0x%lx)\n",
245 (void *) fp->f_addr, fp->f_name, fp->f_type, (long) fp->f_size);
247 if (fp->f_args != NULL) {
248 pager_output(" args: ");
249 pager_output(fp->f_args);
253 pager_output(" modules: ");
254 for (mp = fp->f_modules; mp; mp = mp->m_next) {
255 sprintf(lbuf, "%s.%d ", mp->m_name, mp->m_version);
261 /* XXX could add some formatting smarts here to display some better */
262 for (md = fp->f_metadata; md != NULL; md = md->md_next) {
263 sprintf(lbuf, " 0x%04x, 0x%lx\n", md->md_type, (long) md->md_size);
273 * File level interface, functions file_*
276 file_load(char *filename, vm_offset_t dest, struct preloaded_file **result)
278 static int last_file_format = 0;
279 struct preloaded_file *fp;
283 if (archsw.arch_loadaddr != NULL)
284 dest = archsw.arch_loadaddr(LOAD_RAW, filename, dest);
287 for (i = last_file_format, fp = NULL;
288 file_formats[i] && fp == NULL; i++) {
289 error = (file_formats[i]->l_load)(filename, dest, &fp);
291 fp->f_loader = last_file_format = i; /* remember the loader */
294 } else if (last_file_format == i && i != 0) {
295 /* Restart from the beginning */
297 last_file_format = 0;
302 continue; /* Unknown to this handler? */
304 sprintf(command_errbuf, "can't load file '%s': %s",
305 filename, strerror(error));
313 file_load_dependencies(struct preloaded_file *base_file)
315 struct file_metadata *md;
316 struct preloaded_file *fp;
317 struct mod_depend *verinfo;
318 struct kernel_module *mp;
322 md = file_findmetadata(base_file, MODINFOMD_DEPLIST);
327 verinfo = (struct mod_depend*)md->md_data;
328 dmodname = (char *)(verinfo + 1);
329 if (file_findmodule(NULL, dmodname, verinfo) == NULL) {
330 printf("loading required module '%s'\n", dmodname);
331 error = mod_load(dmodname, verinfo, 0, NULL);
335 * If module loaded via kld name which isn't listed
336 * in the linker.hints file, we should check if it have
339 mp = file_findmodule(NULL, dmodname, verinfo);
341 sprintf(command_errbuf, "module '%s' exists but with wrong version",
347 md = metadata_next(md, MODINFOMD_DEPLIST);
351 /* Load failed; discard everything */
352 while (base_file != NULL) {
354 base_file = base_file->f_next;
361 * We've been asked to load (name) as (type), so just suck it in,
362 * no arguments or anything.
364 struct preloaded_file *
365 file_loadraw(char *name, char *type)
367 struct preloaded_file *fp;
372 /* We can't load first */
373 if ((file_findfile(NULL, NULL)) == NULL) {
374 command_errmsg = "can't load file before kernel";
378 /* locate the file on the load path */
379 cp = file_search(name, NULL);
381 sprintf(command_errbuf, "can't find '%s'", name);
386 if ((fd = open(name, O_RDONLY)) < 0) {
387 sprintf(command_errbuf, "can't open '%s': %s", name, strerror(errno));
392 if (archsw.arch_loadaddr != NULL)
393 loadaddr = archsw.arch_loadaddr(LOAD_RAW, name, loadaddr);
397 /* read in 4k chunks; size is not really important */
398 got = archsw.arch_readin(fd, laddr, 4096);
399 if (got == 0) /* end of file */
401 if (got < 0) { /* error */
402 sprintf(command_errbuf, "error reading '%s': %s", name, strerror(errno));
410 /* Looks OK so far; create & populate control structure */
412 fp->f_name = strdup(name);
413 fp->f_type = strdup(type);
415 fp->f_metadata = NULL;
417 fp->f_addr = loadaddr;
418 fp->f_size = laddr - loadaddr;
420 /* recognise space consumption */
423 /* Add to the list of loaded files */
424 file_insert_tail(fp);
430 * Load the module (name), pass it (argc),(argv), add container file
431 * to the list of loaded files.
432 * If module is already loaded just assign new argc/argv.
435 mod_load(char *modname, struct mod_depend *verinfo, int argc, char *argv[])
437 struct kernel_module *mp;
441 if (file_havepath(modname)) {
442 printf("Warning: mod_load() called instead of mod_loadkld() for module '%s'\n", modname);
443 return (mod_loadkld(modname, argc, argv));
445 /* see if module is already loaded */
446 mp = file_findmodule(NULL, modname, verinfo);
451 mp->m_args = unargv(argc, argv);
453 sprintf(command_errbuf, "warning: module '%s' already loaded", mp->m_name);
456 /* locate file with the module on the search path */
457 filename = mod_searchmodule(modname, verinfo);
458 if (filename == NULL) {
459 sprintf(command_errbuf, "can't find '%s'", modname);
462 err = mod_loadkld(filename, argc, argv);
467 * Load specified KLD. If path is omitted, then try to locate it via
471 mod_loadkld(const char *kldname, int argc, char *argv[])
473 struct preloaded_file *fp, *last_file;
478 * Get fully qualified KLD name
480 filename = file_search(kldname, kld_ext_list);
481 if (filename == NULL) {
482 sprintf(command_errbuf, "can't find '%s'", kldname);
486 * Check if KLD already loaded
488 fp = file_findfile(filename, NULL);
490 sprintf(command_errbuf, "warning: KLD '%s' already loaded", filename);
494 for (last_file = preloaded_files;
495 last_file != NULL && last_file->f_next != NULL;
496 last_file = last_file->f_next)
500 err = file_load(filename, loadaddr, &fp);
503 fp->f_args = unargv(argc, argv);
504 loadaddr = fp->f_addr + fp->f_size;
505 file_insert_tail(fp); /* Add to the list of loaded files */
506 if (file_load_dependencies(fp) != 0) {
508 last_file->f_next = NULL;
509 loadaddr = last_file->f_addr + last_file->f_size;
515 sprintf(command_errbuf, "don't know how to load module '%s'", filename);
523 * Find a file matching (name) and (type).
524 * NULL may be passed as a wildcard to either.
526 struct preloaded_file *
527 file_findfile(char *name, char *type)
529 struct preloaded_file *fp;
531 for (fp = preloaded_files; fp != NULL; fp = fp->f_next) {
532 if (((name == NULL) || !strcmp(name, fp->f_name)) &&
533 ((type == NULL) || !strcmp(type, fp->f_type)))
540 * Find a module matching (name) inside of given file.
541 * NULL may be passed as a wildcard.
543 struct kernel_module *
544 file_findmodule(struct preloaded_file *fp, char *modname,
545 struct mod_depend *verinfo)
547 struct kernel_module *mp, *best;
551 for (fp = preloaded_files; fp; fp = fp->f_next) {
552 mp = file_findmodule(fp, modname, verinfo);
560 for (mp = fp->f_modules; mp; mp = mp->m_next) {
561 if (strcmp(modname, mp->m_name) == 0) {
564 mver = mp->m_version;
565 if (mver == verinfo->md_ver_preferred)
567 if (mver >= verinfo->md_ver_minimum &&
568 mver <= verinfo->md_ver_maximum &&
578 * Make a copy of (size) bytes of data from (p), and associate them as
579 * metadata of (type) to the module (mp).
582 file_addmetadata(struct preloaded_file *fp, int type, size_t size, void *p)
584 struct file_metadata *md;
586 md = malloc(sizeof(struct file_metadata) - sizeof(md->md_data) + size);
589 bcopy(p, md->md_data, size);
590 md->md_next = fp->f_metadata;
595 * Find a metadata object of (type) associated with the file (fp)
597 struct file_metadata *
598 file_findmetadata(struct preloaded_file *fp, int type)
600 struct file_metadata *md;
602 for (md = fp->f_metadata; md != NULL; md = md->md_next)
603 if (md->md_type == type)
608 struct file_metadata *
609 metadata_next(struct file_metadata *md, int type)
613 while((md = md->md_next) != NULL)
614 if (md->md_type == type)
619 static char *emptyextlist[] = { "", NULL };
622 * Check if the given file is in place and return full path to it.
625 file_lookup(const char *path, const char *name, int namelen, char **extlist)
628 char *result, *cp, **cpp;
629 int pathlen, extlen, len;
631 pathlen = strlen(path);
634 extlist = emptyextlist;
635 for (cpp = extlist; *cpp; cpp++) {
640 result = malloc(pathlen + namelen + extlen + 2);
643 bcopy(path, result, pathlen);
644 if (pathlen > 0 && result[pathlen - 1] != '/')
645 result[pathlen++] = '/';
646 cp = result + pathlen;
647 bcopy(name, cp, namelen);
649 for (cpp = extlist; *cpp; cpp++) {
651 if (stat(result, &st) == 0 && S_ISREG(st.st_mode))
659 * Check if file name have any qualifiers
662 file_havepath(const char *name)
666 archsw.arch_getdev(NULL, name, &cp);
667 return (cp != name || strchr(name, '/') != NULL);
671 * Attempt to find the file (name) on the module searchpath.
672 * If (name) is qualified in any way, we simply check it and
673 * return it or NULL. If it is not qualified, then we attempt
674 * to construct a path using entries in the environment variable
677 * The path we return a pointer to need never be freed, as we manage
681 file_search(const char *name, char **extlist)
683 struct moduledir *mdp;
688 /* Don't look for nothing */
693 return(strdup(name));
695 if (file_havepath(name)) {
696 /* Qualified, so just see if it exists */
697 if (stat(name, &sb) == 0)
698 return(strdup(name));
703 namelen = strlen(name);
704 STAILQ_FOREACH(mdp, &moduledir_list, d_link) {
705 result = file_lookup(mdp->d_path, name, namelen, extlist);
712 #define INT_ALIGN(base, ptr) ptr = \
713 (base) + (((ptr) - (base) + sizeof(int) - 1) & ~(sizeof(int) - 1))
716 mod_search_hints(struct moduledir *mdp, const char *modname,
717 struct mod_depend *verinfo)
719 u_char *cp, *recptr, *bufend, *best;
721 int *intp, bestver, blen, clen, found, ival, modnamelen, reclen;
723 moduledir_readhints(mdp);
724 modnamelen = strlen(modname);
728 if (mdp->d_hints == NULL)
730 recptr = mdp->d_hints;
731 bufend = recptr + mdp->d_hintsz;
734 while (recptr < bufend && !found) {
742 if (clen != modnamelen || bcmp(cp, modname, clen) != 0)
745 INT_ALIGN(mdp->d_hints, cp);
749 if (verinfo == NULL || ival == verinfo->md_ver_preferred) {
753 if (ival >= verinfo->md_ver_minimum &&
754 ival <= verinfo->md_ver_maximum &&
764 recptr += reclen + sizeof(int);
767 * Finally check if KLD is in the place
770 result = file_lookup(mdp->d_path, cp, clen, NULL);
772 result = file_lookup(mdp->d_path, best, blen, NULL);
775 * If nothing found or hints is absent - fallback to the old way
776 * by using "kldname[.ko]" as module name.
778 if (!found && !bestver && result == NULL)
779 result = file_lookup(mdp->d_path, modname, modnamelen, kld_ext_list);
784 * Attempt to locate the file containing the module (name)
787 mod_searchmodule(char *name, struct mod_depend *verinfo)
789 struct moduledir *mdp;
794 * Now we ready to lookup module in the given directories
797 STAILQ_FOREACH(mdp, &moduledir_list, d_link) {
798 result = mod_search_hints(mdp, name, verinfo);
807 file_addmodule(struct preloaded_file *fp, char *modname, int version,
808 struct kernel_module **newmp)
810 struct kernel_module *mp;
811 struct mod_depend mdepend;
813 bzero(&mdepend, sizeof(mdepend));
814 mdepend.md_ver_preferred = version;
815 mp = file_findmodule(fp, modname, &mdepend);
818 mp = malloc(sizeof(struct kernel_module));
821 bzero(mp, sizeof(struct kernel_module));
822 mp->m_name = strdup(modname);
823 mp->m_version = version;
825 mp->m_next = fp->f_modules;
836 file_discard(struct preloaded_file *fp)
838 struct file_metadata *md, *md1;
839 struct kernel_module *mp, *mp1;
856 if (fp->f_name != NULL)
858 if (fp->f_type != NULL)
860 if (fp->f_args != NULL)
866 * Allocate a new file; must be used instead of malloc()
867 * to ensure safe initialisation.
869 struct preloaded_file *
872 struct preloaded_file *fp;
874 if ((fp = malloc(sizeof(struct preloaded_file))) != NULL) {
875 bzero(fp, sizeof(struct preloaded_file));
881 * Add a module to the chain
884 file_insert_tail(struct preloaded_file *fp)
886 struct preloaded_file *cm;
888 /* Append to list of loaded file */
890 if (preloaded_files == NULL) {
891 preloaded_files = fp;
893 for (cm = preloaded_files; cm->f_next != NULL; cm = cm->f_next)
900 moduledir_fullpath(struct moduledir *mdp, const char *fname)
904 cp = malloc(strlen(mdp->d_path) + strlen(fname) + 2);
907 strcpy(cp, mdp->d_path);
914 * Read linker.hints file into memory performing some sanity checks.
917 moduledir_readhints(struct moduledir *mdp)
921 int fd, size, version;
923 if (mdp->d_hints != NULL || (mdp->d_flags & MDIR_NOHINTS))
925 path = moduledir_fullpath(mdp, "linker.hints");
926 if (stat(path, &st) != 0 ||
927 st.st_size < (ssize_t)(sizeof(version) + sizeof(int)) ||
928 st.st_size > 100 * 1024 || (fd = open(path, O_RDONLY)) < 0) {
930 mdp->d_flags |= MDIR_NOHINTS;
934 size = read(fd, &version, sizeof(version));
935 if (size != sizeof(version) || version != LINKER_HINTS_VERSION)
937 size = st.st_size - size;
938 mdp->d_hints = malloc(size);
939 if (mdp->d_hints == NULL)
941 if (read(fd, mdp->d_hints, size) != size)
943 mdp->d_hintsz = size;
952 mdp->d_flags |= MDIR_NOHINTS;
957 * Extract directories from the ';' separated list, remove duplicates.
960 moduledir_rebuild(void)
962 struct moduledir *mdp, *mtmp;
963 const char *path, *cp, *ep;
966 path = getenv("module_path");
968 path = default_searchpath;
970 * Rebuild list of module directories if it changed
972 STAILQ_FOREACH(mdp, &moduledir_list, d_link)
973 mdp->d_flags |= MDIR_REMOVED;
975 for (ep = path; *ep != 0; ep++) {
977 for (; *ep != 0 && *ep != ';'; ep++)
980 * Ignore trailing slashes
982 for (cplen = ep - cp; cplen > 1 && cp[cplen - 1] == '/'; cplen--)
984 STAILQ_FOREACH(mdp, &moduledir_list, d_link) {
985 if (strlen(mdp->d_path) != cplen || bcmp(cp, mdp->d_path, cplen) != 0)
987 mdp->d_flags &= ~MDIR_REMOVED;
991 mdp = malloc(sizeof(*mdp) + cplen + 1);
994 mdp->d_path = (char*)(mdp + 1);
995 bcopy(cp, mdp->d_path, cplen);
996 mdp->d_path[cplen] = 0;
999 STAILQ_INSERT_TAIL(&moduledir_list, mdp, d_link);
1005 * Delete unused directories if any
1007 mdp = STAILQ_FIRST(&moduledir_list);
1009 if ((mdp->d_flags & MDIR_REMOVED) == 0) {
1010 mdp = STAILQ_NEXT(mdp, d_link);
1015 mdp = STAILQ_NEXT(mdp, d_link);
1016 STAILQ_REMOVE(&moduledir_list, mtmp, moduledir, d_link);