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(unload, "unload", "unload all modules", command_unload);
160 command_unload(int argc, char *argv[])
162 struct preloaded_file *fp;
164 while (preloaded_files != NULL) {
165 fp = preloaded_files;
166 preloaded_files = preloaded_files->f_next;
170 unsetenv("kernelname");
174 COMMAND_SET(lsmod, "lsmod", "list loaded modules", command_lsmod);
177 command_lsmod(int argc, char *argv[])
179 struct preloaded_file *fp;
180 struct kernel_module *mp;
181 struct file_metadata *md;
188 while ((ch = getopt(argc, argv, "v")) != -1) {
195 /* getopt has already reported an error */
201 for (fp = preloaded_files; fp; fp = fp->f_next) {
202 sprintf(lbuf, " %p: %s (%s, 0x%lx)\n",
203 (void *) fp->f_addr, fp->f_name, fp->f_type, (long) fp->f_size);
205 if (fp->f_args != NULL) {
206 pager_output(" args: ");
207 pager_output(fp->f_args);
211 pager_output(" modules: ");
212 for (mp = fp->f_modules; mp; mp = mp->m_next) {
213 sprintf(lbuf, "%s.%d ", mp->m_name, mp->m_version);
219 /* XXX could add some formatting smarts here to display some better */
220 for (md = fp->f_metadata; md != NULL; md = md->md_next) {
221 sprintf(lbuf, " 0x%04x, 0x%lx\n", md->md_type, (long) md->md_size);
231 * File level interface, functions file_*
234 file_load(char *filename, vm_offset_t dest, struct preloaded_file **result)
236 struct preloaded_file *fp;
241 for (i = 0, fp = NULL; file_formats[i] && fp == NULL; i++) {
242 error = (file_formats[i]->l_load)(filename, loadaddr, &fp);
244 fp->f_loader = i; /* remember the loader */
249 continue; /* Unknown to this handler? */
251 sprintf(command_errbuf, "can't load file '%s': %s",
252 filename, strerror(error));
260 file_load_dependencies(struct preloaded_file *base_file) {
261 struct file_metadata *md;
262 struct preloaded_file *fp;
263 struct mod_depend *verinfo;
264 struct kernel_module *mp;
268 md = file_findmetadata(base_file, MODINFOMD_DEPLIST);
273 verinfo = (struct mod_depend*)md->md_data;
274 dmodname = (char *)(verinfo + 1);
275 if (file_findmodule(NULL, dmodname, verinfo) == NULL) {
276 printf("loading required module '%s'\n", dmodname);
277 error = mod_load(dmodname, verinfo, 0, NULL);
281 * If module loaded via kld name which isn't listed
282 * in the linker.hints file, we should check if it have
285 mp = file_findmodule(NULL, dmodname, verinfo);
287 sprintf(command_errbuf, "module '%s' exists but with wrong version",
293 md = metadata_next(md, MODINFOMD_DEPLIST);
297 /* Load failed; discard everything */
298 while (base_file != NULL) {
300 base_file = base_file->f_next;
306 * We've been asked to load (name) as (type), so just suck it in,
307 * no arguments or anything.
310 file_loadraw(char *type, char *name)
312 struct preloaded_file *fp;
317 /* We can't load first */
318 if ((file_findfile(NULL, NULL)) == NULL) {
319 command_errmsg = "can't load file before kernel";
323 /* locate the file on the load path */
324 cp = file_search(name, NULL);
326 sprintf(command_errbuf, "can't find '%s'", name);
331 if ((fd = open(name, O_RDONLY)) < 0) {
332 sprintf(command_errbuf, "can't open '%s': %s", name, strerror(errno));
339 /* read in 4k chunks; size is not really important */
340 got = archsw.arch_readin(fd, laddr, 4096);
341 if (got == 0) /* end of file */
343 if (got < 0) { /* error */
344 sprintf(command_errbuf, "error reading '%s': %s", name, strerror(errno));
352 /* Looks OK so far; create & populate control structure */
355 fp->f_type = strdup(type);
357 fp->f_metadata = NULL;
359 fp->f_addr = loadaddr;
360 fp->f_size = laddr - loadaddr;
362 /* recognise space consumption */
365 /* Add to the list of loaded files */
366 file_insert_tail(fp);
372 * Load the module (name), pass it (argc),(argv), add container file
373 * to the list of loaded files.
374 * If module is already loaded just assign new argc/argv.
377 mod_load(char *modname, struct mod_depend *verinfo, int argc, char *argv[])
379 struct kernel_module *mp;
383 if (file_havepath(modname)) {
384 printf("Warning: mod_load() called instead of mod_loadkld() for module '%s'\n", modname);
385 return (mod_loadkld(modname, argc, argv));
387 /* see if module is already loaded */
388 mp = file_findmodule(NULL, modname, verinfo);
393 mp->m_args = unargv(argc, argv);
395 sprintf(command_errbuf, "warning: module '%s' already loaded", mp->m_name);
398 /* locate file with the module on the search path */
399 filename = mod_searchmodule(modname, verinfo);
400 if (filename == NULL) {
401 sprintf(command_errbuf, "can't find '%s'", modname);
404 err = mod_loadkld(filename, argc, argv);
409 * Load specified KLD. If path is omitted, then try to locate it via
413 mod_loadkld(const char *kldname, int argc, char *argv[])
415 struct preloaded_file *fp, *last_file;
420 * Get fully qualified KLD name
422 filename = file_search(kldname, kld_ext_list);
423 if (filename == NULL) {
424 sprintf(command_errbuf, "can't find '%s'", kldname);
428 * Check if KLD already loaded
430 fp = file_findfile(filename, NULL);
432 sprintf(command_errbuf, "warning: KLD '%s' already loaded", filename);
436 for (last_file = preloaded_files;
437 last_file != NULL && last_file->f_next != NULL;
438 last_file = last_file->f_next)
442 err = file_load(filename, loadaddr, &fp);
445 fp->f_args = unargv(argc, argv);
446 loadaddr = fp->f_addr + fp->f_size;
447 file_insert_tail(fp); /* Add to the list of loaded files */
448 if (file_load_dependencies(fp) != 0) {
450 last_file->f_next = NULL;
451 loadaddr = last_file->f_addr + last_file->f_size;
457 sprintf(command_errbuf, "don't know how to load module '%s'", filename);
465 * Find a file matching (name) and (type).
466 * NULL may be passed as a wildcard to either.
468 struct preloaded_file *
469 file_findfile(char *name, char *type)
471 struct preloaded_file *fp;
473 for (fp = preloaded_files; fp != NULL; fp = fp->f_next) {
474 if (((name == NULL) || !strcmp(name, fp->f_name)) &&
475 ((type == NULL) || !strcmp(type, fp->f_type)))
482 * Find a module matching (name) inside of given file.
483 * NULL may be passed as a wildcard.
485 struct kernel_module *
486 file_findmodule(struct preloaded_file *fp, char *modname,
487 struct mod_depend *verinfo)
489 struct kernel_module *mp, *best;
493 for (fp = preloaded_files; fp; fp = fp->f_next) {
494 mp = file_findmodule(fp, modname, verinfo);
502 for (mp = fp->f_modules; mp; mp = mp->m_next) {
503 if (strcmp(modname, mp->m_name) == 0) {
506 mver = mp->m_version;
507 if (mver == verinfo->md_ver_preferred)
509 if (mver >= verinfo->md_ver_minimum &&
510 mver <= verinfo->md_ver_maximum &&
520 * Make a copy of (size) bytes of data from (p), and associate them as
521 * metadata of (type) to the module (mp).
524 file_addmetadata(struct preloaded_file *fp, int type, size_t size, void *p)
526 struct file_metadata *md;
528 md = malloc(sizeof(struct file_metadata) - sizeof(md->md_data) + size);
531 bcopy(p, md->md_data, size);
532 md->md_next = fp->f_metadata;
537 * Find a metadata object of (type) associated with the file (fp)
539 struct file_metadata *
540 file_findmetadata(struct preloaded_file *fp, int type)
542 struct file_metadata *md;
544 for (md = fp->f_metadata; md != NULL; md = md->md_next)
545 if (md->md_type == type)
550 struct file_metadata *
551 metadata_next(struct file_metadata *md, int type)
555 while((md = md->md_next) != NULL)
556 if (md->md_type == type)
561 static char *emptyextlist[] = { "", NULL };
564 * Check if the given file is in place and return full path to it.
567 file_lookup(const char *path, const char *name, int namelen, char **extlist)
570 char *result, *cp, **cpp;
571 int pathlen, extlen, len;
573 pathlen = strlen(path);
576 extlist = emptyextlist;
577 for (cpp = extlist; *cpp; cpp++) {
582 result = malloc(pathlen + namelen + extlen + 2);
585 bcopy(path, result, pathlen);
586 if (pathlen > 0 && result[pathlen - 1] != '/')
587 result[pathlen++] = '/';
588 cp = result + pathlen;
589 bcopy(name, cp, namelen);
591 for (cpp = extlist; *cpp; cpp++) {
593 if (stat(result, &st) == 0 && S_ISREG(st.st_mode))
601 * Check if file name have any qualifiers
604 file_havepath(const char *name)
608 archsw.arch_getdev(NULL, name, &cp);
609 return (cp != name || strchr(name, '/') != NULL);
613 * Attempt to find the file (name) on the module searchpath.
614 * If (name) is qualified in any way, we simply check it and
615 * return it or NULL. If it is not qualified, then we attempt
616 * to construct a path using entries in the environment variable
619 * The path we return a pointer to need never be freed, as we manage
623 file_search(const char *name, char **extlist)
625 struct moduledir *mdp;
630 /* Don't look for nothing */
635 return(strdup(name));
637 if (file_havepath(name)) {
638 /* Qualified, so just see if it exists */
639 if (stat(name, &sb) == 0)
640 return(strdup(name));
645 namelen = strlen(name);
646 STAILQ_FOREACH(mdp, &moduledir_list, d_link) {
647 result = file_lookup(mdp->d_path, name, namelen, extlist);
654 #define INT_ALIGN(base, ptr) ptr = \
655 (base) + (((ptr) - (base) + sizeof(int) - 1) & ~(sizeof(int) - 1))
658 mod_search_hints(struct moduledir *mdp, const char *modname,
659 struct mod_depend *verinfo)
661 u_char *cp, *recptr, *bufend, *best;
663 int *intp, bestver, blen, clen, found, ival, modnamelen, reclen;
665 moduledir_readhints(mdp);
666 modnamelen = strlen(modname);
670 if (mdp->d_hints == NULL)
672 recptr = mdp->d_hints;
673 bufend = recptr + mdp->d_hintsz;
676 while (recptr < bufend && !found) {
684 if (clen != modnamelen || bcmp(cp, modname, clen) != 0)
687 INT_ALIGN(mdp->d_hints, cp);
691 if (verinfo == NULL || ival == verinfo->md_ver_preferred) {
695 if (ival >= verinfo->md_ver_minimum &&
696 ival <= verinfo->md_ver_maximum &&
706 recptr += reclen + sizeof(int);
709 * Finally check if KLD is in the place
712 result = file_lookup(mdp->d_path, cp, clen, NULL);
714 result = file_lookup(mdp->d_path, best, blen, NULL);
717 * If nothing found or hints is absent - fallback to the old way
718 * by using "kldname[.ko]" as module name.
720 if (!found && !bestver && result == NULL)
721 result = file_lookup(mdp->d_path, modname, modnamelen, kld_ext_list);
726 * Attempt to locate the file containing the module (name)
729 mod_searchmodule(char *name, struct mod_depend *verinfo)
731 struct moduledir *mdp;
736 * Now we ready to lookup module in the given directories
739 STAILQ_FOREACH(mdp, &moduledir_list, d_link) {
740 result = mod_search_hints(mdp, name, verinfo);
749 file_addmodule(struct preloaded_file *fp, char *modname, int version,
750 struct kernel_module **newmp)
752 struct kernel_module *mp;
753 struct mod_depend mdepend;
755 bzero(&mdepend, sizeof(mdepend));
756 mdepend.md_ver_preferred = version;
757 mp = file_findmodule(fp, modname, &mdepend);
760 mp = malloc(sizeof(struct kernel_module));
763 bzero(mp, sizeof(struct kernel_module));
764 mp->m_name = strdup(modname);
765 mp->m_version = version;
767 mp->m_next = fp->f_modules;
778 file_discard(struct preloaded_file *fp)
780 struct file_metadata *md, *md1;
781 struct kernel_module *mp, *mp1;
798 if (fp->f_name != NULL)
800 if (fp->f_type != NULL)
802 if (fp->f_args != NULL)
808 * Allocate a new file; must be used instead of malloc()
809 * to ensure safe initialisation.
811 struct preloaded_file *
814 struct preloaded_file *fp;
816 if ((fp = malloc(sizeof(struct preloaded_file))) != NULL) {
817 bzero(fp, sizeof(struct preloaded_file));
823 * Add a module to the chain
826 file_insert_tail(struct preloaded_file *fp)
828 struct preloaded_file *cm;
830 /* Append to list of loaded file */
832 if (preloaded_files == NULL) {
833 preloaded_files = fp;
835 for (cm = preloaded_files; cm->f_next != NULL; cm = cm->f_next)
842 moduledir_fullpath(struct moduledir *mdp, const char *fname)
846 cp = malloc(strlen(mdp->d_path) + strlen(fname) + 2);
849 strcpy(cp, mdp->d_path);
856 * Read linker.hints file into memory performing some sanity checks.
859 moduledir_readhints(struct moduledir *mdp)
863 int fd, size, version;
865 if (mdp->d_hints != NULL || (mdp->d_flags & MDIR_NOHINTS))
867 path = moduledir_fullpath(mdp, "linker.hints");
868 if (stat(path, &st) != 0 ||
869 st.st_size < (ssize_t)(sizeof(version) + sizeof(int)) ||
870 st.st_size > 100 * 1024 || (fd = open(path, O_RDONLY)) < 0) {
872 mdp->d_flags |= MDIR_NOHINTS;
876 size = read(fd, &version, sizeof(version));
877 if (size != sizeof(version) || version != LINKER_HINTS_VERSION)
879 size = st.st_size - size;
880 mdp->d_hints = malloc(size);
881 if (mdp->d_hints == NULL)
883 if (read(fd, mdp->d_hints, size) != size)
885 mdp->d_hintsz = size;
894 mdp->d_flags |= MDIR_NOHINTS;
899 * Extract directories from the ';' separated list, remove duplicates.
902 moduledir_rebuild(void)
904 struct moduledir *mdp, *mtmp;
905 const char *path, *cp, *ep;
908 path = getenv("module_path");
910 path = default_searchpath;
912 * Rebuild list of module directories if it changed
914 STAILQ_FOREACH(mdp, &moduledir_list, d_link)
915 mdp->d_flags |= MDIR_REMOVED;
917 for (ep = path; *ep != 0; ep++) {
919 for (; *ep != 0 && *ep != ';'; ep++)
922 * Ignore trailing slashes
924 for (cplen = ep - cp; cplen > 1 && cp[cplen - 1] == '/'; cplen--)
926 STAILQ_FOREACH(mdp, &moduledir_list, d_link) {
927 if (strlen(mdp->d_path) != cplen || bcmp(cp, mdp->d_path, cplen) != 0)
929 mdp->d_flags &= ~MDIR_REMOVED;
933 mdp = malloc(sizeof(*mdp) + cplen + 1);
936 mdp->d_path = (char*)(mdp + 1);
937 bcopy(cp, mdp->d_path, cplen);
938 mdp->d_path[cplen] = 0;
941 STAILQ_INSERT_TAIL(&moduledir_list, mdp, d_link);
947 * Delete unused directories if any
949 mdp = STAILQ_FIRST(&moduledir_list);
951 if ((mdp->d_flags & MDIR_REMOVED) == 0) {
952 mdp = STAILQ_NEXT(mdp, d_link);
957 mdp = STAILQ_NEXT(mdp, d_link);
958 STAILQ_REMOVE(&moduledir_list, mtmp, moduledir, d_link);