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 #if defined(LOADER_FDT_SUPPORT)
43 #include <fdt_platform.h>
46 #include "bootstrap.h"
48 #define MDIR_REMOVED 0x0001
49 #define MDIR_NOHINTS 0x0002
52 char *d_path; /* path of modules directory */
53 u_char *d_hints; /* content of linker.hints file */
54 int d_hintsz; /* size of hints data */
56 STAILQ_ENTRY(moduledir) d_link;
59 static int file_load(char *filename, vm_offset_t dest, struct preloaded_file **result);
60 static int file_load_dependencies(struct preloaded_file *base_mod);
61 static char * file_search(const char *name, char **extlist);
62 static struct kernel_module * file_findmodule(struct preloaded_file *fp, char *modname, struct mod_depend *verinfo);
63 static int file_havepath(const char *name);
64 static char *mod_searchmodule(char *name, struct mod_depend *verinfo);
65 static char * mod_searchmodule_pnpinfo(const char *bus, const char *pnpinfo);
66 static void file_insert_tail(struct preloaded_file *mp);
67 static void file_remove(struct preloaded_file *fp);
68 struct file_metadata* metadata_next(struct file_metadata *base_mp, int type);
69 static void moduledir_readhints(struct moduledir *mdp);
70 static void moduledir_rebuild(void);
72 /* load address should be tweaked by first module loaded (kernel) */
73 static vm_offset_t loadaddr = 0;
75 #if defined(LOADER_FDT_SUPPORT)
76 static const char *default_searchpath =
77 "/boot/kernel;/boot/modules;/boot/dtb";
79 static const char *default_searchpath = "/boot/kernel;/boot/modules";
82 static STAILQ_HEAD(, moduledir) moduledir_list =
83 STAILQ_HEAD_INITIALIZER(moduledir_list);
85 struct preloaded_file *preloaded_files = NULL;
87 static char *kld_ext_list[] = {
96 * load an object, either a disk file or code module.
98 * To load a file, the syntax is:
100 * load -t <type> <path>
102 * code modules are loaded as:
104 * load <path> <options>
107 COMMAND_SET(load, "load", "load a kernel or module", command_load);
110 command_load(int argc, char *argv[])
112 struct preloaded_file *fp;
116 int dflag, dofile, dokld, ch, error;
118 dflag = dokld = dofile = 0;
123 command_errmsg = "no filename specified";
126 prefix = skip = NULL;
127 while ((ch = getopt(argc, argv, "dkp:s:t:")) != -1) {
147 /* getopt has already reported an error */
151 argv += (optind - 1);
152 argc -= (optind - 1);
155 * Request to load a raw file?
158 if ((argc != 2) || (typestr == NULL) || (*typestr == 0)) {
159 command_errmsg = "invalid load type";
163 #ifdef LOADER_VERIEXEC
164 if (strncmp(typestr, "manifest", 8) == 0) {
167 return (load_manifest(argv[1], prefix, skip, NULL));
169 #ifdef LOADER_VERIEXEC_PASS_MANIFEST
170 if (strncmp(typestr, "pass_manifest", 13) == 0) {
173 return (pass_manifest(argv[1], prefix));
178 fp = file_findfile(argv[1], typestr);
180 snprintf(command_errbuf, sizeof(command_errbuf),
181 "warning: file '%s' already loaded", argv[1]);
185 if (file_loadraw(argv[1], typestr, 1) != NULL)
188 /* Failing to load mfs_root is never going to end well! */
189 if (strcmp("mfs_root", typestr) == 0)
195 * Do we have explicit KLD load ?
197 if (dokld || file_havepath(argv[1])) {
198 error = mod_loadkld(argv[1], argc - 2, argv + 2);
199 if (error == EEXIST) {
200 snprintf(command_errbuf, sizeof(command_errbuf),
201 "warning: KLD '%s' already loaded", argv[1]);
205 return (error == 0 ? CMD_OK : CMD_CRIT);
208 * Looks like a request for a module.
210 error = mod_load(argv[1], NULL, argc - 2, argv + 2);
211 if (error == EEXIST) {
212 snprintf(command_errbuf, sizeof(command_errbuf),
213 "warning: module '%s' already loaded", argv[1]);
217 return (error == 0 ? CMD_OK : CMD_CRIT);
220 #ifdef LOADER_GELI_SUPPORT
221 COMMAND_SET(load_geli, "load_geli", "load a geli key", command_load_geli);
224 command_load_geli(int argc, char *argv[])
231 command_errmsg = "usage is [-n key#] <prov> <file>";
238 while ((ch = getopt(argc, argv, "n:")) != -1) {
241 num = strtol(optarg, &cp, 0);
243 snprintf(command_errbuf, sizeof(command_errbuf),
244 "bad key index '%s'", optarg);
250 /* getopt has already reported an error */
254 argv += (optind - 1);
255 argc -= (optind - 1);
256 sprintf(typestr, "%s:geli_keyfile%d", argv[1], num);
257 return (file_loadraw(argv[2], typestr, 1) ? CMD_OK : CMD_ERROR);
264 struct preloaded_file *fp;
266 while (preloaded_files != NULL) {
267 fp = preloaded_files;
268 preloaded_files = preloaded_files->f_next;
272 unsetenv("kernelname");
275 COMMAND_SET(unload, "unload", "unload all modules", command_unload);
278 command_unload(int argc, char *argv[])
284 COMMAND_SET(lsmod, "lsmod", "list loaded modules", command_lsmod);
287 command_lsmod(int argc, char *argv[])
289 struct preloaded_file *fp;
290 struct kernel_module *mp;
291 struct file_metadata *md;
293 int ch, verbose, ret = 0;
298 while ((ch = getopt(argc, argv, "v")) != -1) {
305 /* getopt has already reported an error */
311 for (fp = preloaded_files; fp; fp = fp->f_next) {
312 snprintf(lbuf, sizeof(lbuf), " %p: ", (void *) fp->f_addr);
314 pager_output(fp->f_name);
315 snprintf(lbuf, sizeof(lbuf), " (%s, 0x%lx)\n", fp->f_type,
317 if (pager_output(lbuf))
319 if (fp->f_args != NULL) {
320 pager_output(" args: ");
321 pager_output(fp->f_args);
322 if (pager_output("\n"))
326 pager_output(" modules: ");
327 for (mp = fp->f_modules; mp; mp = mp->m_next) {
328 snprintf(lbuf, sizeof(lbuf), "%s.%d ", mp->m_name,
332 if (pager_output("\n"))
336 /* XXX could add some formatting smarts here to display some better */
337 for (md = fp->f_metadata; md != NULL; md = md->md_next) {
338 snprintf(lbuf, sizeof(lbuf), " 0x%04x, 0x%lx\n",
339 md->md_type, (long) md->md_size);
340 if (pager_output(lbuf))
351 COMMAND_SET(pnpmatch, "pnpmatch", "list matched modules based on pnpinfo", command_pnpmatch);
353 static int pnp_dump_flag = 0;
354 static int pnp_unbound_flag = 0;
355 static int pnp_verbose_flag = 0;
358 command_pnpmatch(int argc, char *argv[])
363 pnp_verbose_flag = 0;
367 while ((ch = getopt(argc, argv, "vd")) != -1) {
370 pnp_verbose_flag = 1;
377 /* getopt has already reported an error */
381 argv += (optind - 1);
382 argc -= (optind - 1);
384 module = mod_searchmodule_pnpinfo(argv[1], argv[2]);
386 printf("Matched module: %s\n", module);
388 printf("No module matches %s\n", argv[1]);
393 COMMAND_SET(pnpload, "pnpload", "load matched modules based on pnpinfo", command_pnpload);
396 command_pnpload(int argc, char *argv[])
401 pnp_verbose_flag = 0;
405 while ((ch = getopt(argc, argv, "vd")) != -1) {
408 pnp_verbose_flag = 1;
415 /* getopt has already reported an error */
419 argv += (optind - 1);
420 argc -= (optind - 1);
425 module = mod_searchmodule_pnpinfo(argv[1], argv[2]);
427 error = mod_load(module, NULL, 0, NULL);
428 if (error == EEXIST) {
429 snprintf(command_errbuf, sizeof(command_errbuf),
430 "warning: module '%s' already loaded", argv[1]);
434 return (error == 0 ? CMD_OK : CMD_CRIT);
437 #if defined(LOADER_FDT_SUPPORT)
439 pnpautoload_simplebus(void) {
440 const char *pnpstring;
441 const char *compatstr;
442 char *pnpinfo = NULL;
444 int tag = 0, len, pnplen;
448 pnpstring = fdt_devmatch_next(&tag, &len);
449 if (pnpstring == NULL)
452 compatstr = pnpstring;
453 for (pnplen = 0; pnplen != len; compatstr = pnpstring + pnplen) {
454 pnplen += strlen(compatstr) + 1;
455 asprintf(&pnpinfo, "compat=%s", compatstr);
457 module = mod_searchmodule_pnpinfo("simplebus", pnpinfo);
459 error = mod_loadkld(module, 0, NULL);
461 printf("Cannot load module %s\n", module);
476 struct pnp_bus pnp_buses[] = {
477 #if defined(LOADER_FDT_SUPPORT)
478 {"simplebus", pnpautoload_simplebus},
482 COMMAND_SET(pnpautoload, "pnpautoload", "auto load modules based on pnpinfo", command_pnpautoload);
485 command_pnpautoload(int argc, char *argv[])
491 pnp_verbose_flag = 0;
497 while ((ch = getopt(argc, argv, "v")) != -1) {
504 /* getopt has already reported an error */
508 argv += (optind - 1);
509 argc -= (optind - 1);
514 for (i = 0; i < nitems(pnp_buses); i++) {
515 if (argc == 2 && strcmp(argv[1], pnp_buses[i].name) != 0) {
517 printf("Skipping bus %s\n", pnp_buses[i].name);
521 printf("Autoloading modules for simplebus\n");
526 printf("Unsupported bus %s\n", argv[1]);
532 * File level interface, functions file_*
535 file_load(char *filename, vm_offset_t dest, struct preloaded_file **result)
537 static int last_file_format = 0;
538 struct preloaded_file *fp;
542 if (archsw.arch_loadaddr != NULL)
543 dest = archsw.arch_loadaddr(LOAD_RAW, filename, dest);
546 for (i = last_file_format, fp = NULL;
547 file_formats[i] && fp == NULL; i++) {
548 error = (file_formats[i]->l_load)(filename, dest, &fp);
550 fp->f_loader = last_file_format = i; /* remember the loader */
553 } else if (last_file_format == i && i != 0) {
554 /* Restart from the beginning */
556 last_file_format = 0;
561 continue; /* Unknown to this handler? */
563 snprintf(command_errbuf, sizeof(command_errbuf),
564 "can't load file '%s': %s", filename, strerror(error));
572 file_load_dependencies(struct preloaded_file *base_file)
574 struct file_metadata *md;
575 struct preloaded_file *fp;
576 struct mod_depend *verinfo;
577 struct kernel_module *mp;
581 md = file_findmetadata(base_file, MODINFOMD_DEPLIST);
586 verinfo = (struct mod_depend*)md->md_data;
587 dmodname = (char *)(verinfo + 1);
588 if (file_findmodule(NULL, dmodname, verinfo) == NULL) {
589 printf("loading required module '%s'\n", dmodname);
590 error = mod_load(dmodname, verinfo, 0, NULL);
594 * If module loaded via kld name which isn't listed
595 * in the linker.hints file, we should check if it have
598 mp = file_findmodule(NULL, dmodname, verinfo);
600 snprintf(command_errbuf, sizeof(command_errbuf),
601 "module '%s' exists but with wrong version", dmodname);
606 md = metadata_next(md, MODINFOMD_DEPLIST);
610 /* Load failed; discard everything */
611 while (base_file != NULL) {
613 base_file = base_file->f_next;
620 * We've been asked to load (fname) as (type), so just suck it in,
621 * no arguments or anything.
623 struct preloaded_file *
624 file_loadraw(const char *fname, char *type, int insert)
626 struct preloaded_file *fp;
631 /* We can't load first */
632 if ((file_findfile(NULL, NULL)) == NULL) {
633 command_errmsg = "can't load file before kernel";
637 /* locate the file on the load path */
638 name = file_search(fname, NULL);
640 snprintf(command_errbuf, sizeof(command_errbuf),
641 "can't find '%s'", fname);
645 if ((fd = open(name, O_RDONLY)) < 0) {
646 snprintf(command_errbuf, sizeof(command_errbuf),
647 "can't open '%s': %s", name, strerror(errno));
652 #ifdef LOADER_VERIEXEC
653 if (verify_file(fd, name, 0, VE_MUST) < 0) {
654 sprintf(command_errbuf, "can't verify '%s'", name);
661 if (archsw.arch_loadaddr != NULL)
662 loadaddr = archsw.arch_loadaddr(LOAD_RAW, name, loadaddr);
668 /* read in 4k chunks; size is not really important */
669 got = archsw.arch_readin(fd, laddr, 4096);
670 if (got == 0) /* end of file */
672 if (got < 0) { /* error */
673 snprintf(command_errbuf, sizeof(command_errbuf),
674 "error reading '%s': %s", name, strerror(errno));
682 printf("size=%#jx\n", (uintmax_t)(laddr - loadaddr));
684 /* Looks OK so far; create & populate control structure */
687 snprintf(command_errbuf, sizeof (command_errbuf),
688 "no memory to load %s", name);
694 fp->f_type = strdup(type);
696 fp->f_metadata = NULL;
698 fp->f_addr = loadaddr;
699 fp->f_size = laddr - loadaddr;
701 if (fp->f_type == NULL) {
702 snprintf(command_errbuf, sizeof (command_errbuf),
703 "no memory to load %s", name);
708 /* recognise space consumption */
711 /* Add to the list of loaded files */
713 file_insert_tail(fp);
719 * Load the module (name), pass it (argc),(argv), add container file
720 * to the list of loaded files.
721 * If module is already loaded just assign new argc/argv.
724 mod_load(char *modname, struct mod_depend *verinfo, int argc, char *argv[])
726 struct kernel_module *mp;
730 if (file_havepath(modname)) {
731 printf("Warning: mod_load() called instead of mod_loadkld() for module '%s'\n", modname);
732 return (mod_loadkld(modname, argc, argv));
734 /* see if module is already loaded */
735 mp = file_findmodule(NULL, modname, verinfo);
739 mp->m_args = unargv(argc, argv);
741 snprintf(command_errbuf, sizeof(command_errbuf),
742 "warning: module '%s' already loaded", mp->m_name);
745 /* locate file with the module on the search path */
746 filename = mod_searchmodule(modname, verinfo);
747 if (filename == NULL) {
748 snprintf(command_errbuf, sizeof(command_errbuf),
749 "can't find '%s'", modname);
752 err = mod_loadkld(filename, argc, argv);
758 * Load specified KLD. If path is omitted, then try to locate it via
762 mod_loadkld(const char *kldname, int argc, char *argv[])
764 struct preloaded_file *fp;
767 vm_offset_t loadaddr_saved;
770 * Get fully qualified KLD name
772 filename = file_search(kldname, kld_ext_list);
773 if (filename == NULL) {
774 snprintf(command_errbuf, sizeof(command_errbuf),
775 "can't find '%s'", kldname);
779 * Check if KLD already loaded
781 fp = file_findfile(filename, NULL);
783 snprintf(command_errbuf, sizeof(command_errbuf),
784 "warning: KLD '%s' already loaded", filename);
790 err = file_load(filename, loadaddr, &fp);
793 fp->f_args = unargv(argc, argv);
794 loadaddr_saved = loadaddr;
795 loadaddr = fp->f_addr + fp->f_size;
796 file_insert_tail(fp); /* Add to the list of loaded files */
797 if (file_load_dependencies(fp) != 0) {
800 loadaddr = loadaddr_saved;
806 snprintf(command_errbuf, sizeof(command_errbuf),
807 "don't know how to load module '%s'", filename);
816 * Find a file matching (name) and (type).
817 * NULL may be passed as a wildcard to either.
819 struct preloaded_file *
820 file_findfile(const char *name, const char *type)
822 struct preloaded_file *fp;
824 for (fp = preloaded_files; fp != NULL; fp = fp->f_next) {
825 if (((name == NULL) || !strcmp(name, fp->f_name)) &&
826 ((type == NULL) || !strcmp(type, fp->f_type)))
833 * Find a module matching (name) inside of given file.
834 * NULL may be passed as a wildcard.
836 struct kernel_module *
837 file_findmodule(struct preloaded_file *fp, char *modname,
838 struct mod_depend *verinfo)
840 struct kernel_module *mp, *best;
844 for (fp = preloaded_files; fp; fp = fp->f_next) {
845 mp = file_findmodule(fp, modname, verinfo);
853 for (mp = fp->f_modules; mp; mp = mp->m_next) {
854 if (strcmp(modname, mp->m_name) == 0) {
857 mver = mp->m_version;
858 if (mver == verinfo->md_ver_preferred)
860 if (mver >= verinfo->md_ver_minimum &&
861 mver <= verinfo->md_ver_maximum &&
871 * Make a copy of (size) bytes of data from (p), and associate them as
872 * metadata of (type) to the module (mp).
875 file_addmetadata(struct preloaded_file *fp, int type, size_t size, void *p)
877 struct file_metadata *md;
879 md = malloc(sizeof(struct file_metadata) - sizeof(md->md_data) + size);
883 bcopy(p, md->md_data, size);
884 md->md_next = fp->f_metadata;
890 * Find a metadata object of (type) associated with the file (fp)
892 struct file_metadata *
893 file_findmetadata(struct preloaded_file *fp, int type)
895 struct file_metadata *md;
897 for (md = fp->f_metadata; md != NULL; md = md->md_next)
898 if (md->md_type == type)
904 * Remove all metadata from the file.
907 file_removemetadata(struct preloaded_file *fp)
909 struct file_metadata *md, *next;
911 for (md = fp->f_metadata; md != NULL; md = next)
916 fp->f_metadata = NULL;
919 struct file_metadata *
920 metadata_next(struct file_metadata *md, int type)
925 while((md = md->md_next) != NULL)
926 if (md->md_type == type)
931 static char *emptyextlist[] = { "", NULL };
934 * Check if the given file is in place and return full path to it.
937 file_lookup(const char *path, const char *name, int namelen, char **extlist)
940 char *result, *cp, **cpp;
941 int pathlen, extlen, len;
943 pathlen = strlen(path);
946 extlist = emptyextlist;
947 for (cpp = extlist; *cpp; cpp++) {
952 result = malloc(pathlen + namelen + extlen + 2);
955 bcopy(path, result, pathlen);
956 if (pathlen > 0 && result[pathlen - 1] != '/')
957 result[pathlen++] = '/';
958 cp = result + pathlen;
959 bcopy(name, cp, namelen);
961 for (cpp = extlist; *cpp; cpp++) {
963 if (stat(result, &st) == 0 && S_ISREG(st.st_mode))
971 * Check if file name have any qualifiers
974 file_havepath(const char *name)
978 archsw.arch_getdev(NULL, name, &cp);
979 return (cp != name || strchr(name, '/') != NULL);
983 * Attempt to find the file (name) on the module searchpath.
984 * If (name) is qualified in any way, we simply check it and
985 * return it or NULL. If it is not qualified, then we attempt
986 * to construct a path using entries in the environment variable
989 * The path we return a pointer to need never be freed, as we manage
993 file_search(const char *name, char **extlist)
995 struct moduledir *mdp;
1000 /* Don't look for nothing */
1005 return(strdup(name));
1007 if (file_havepath(name)) {
1008 /* Qualified, so just see if it exists */
1009 if (stat(name, &sb) == 0)
1010 return(strdup(name));
1013 moduledir_rebuild();
1015 namelen = strlen(name);
1016 STAILQ_FOREACH(mdp, &moduledir_list, d_link) {
1017 result = file_lookup(mdp->d_path, name, namelen, extlist);
1024 #define INT_ALIGN(base, ptr) ptr = \
1025 (base) + roundup2((ptr) - (base), sizeof(int))
1028 mod_search_hints(struct moduledir *mdp, const char *modname,
1029 struct mod_depend *verinfo)
1031 u_char *cp, *recptr, *bufend, *best;
1033 int *intp, bestver, blen, clen, found, ival, modnamelen, reclen;
1035 moduledir_readhints(mdp);
1036 modnamelen = strlen(modname);
1040 if (mdp->d_hints == NULL)
1042 recptr = mdp->d_hints;
1043 bufend = recptr + mdp->d_hintsz;
1046 while (recptr < bufend && !found) {
1047 intp = (int*)recptr;
1054 if (clen != modnamelen || bcmp(cp, modname, clen) != 0)
1057 INT_ALIGN(mdp->d_hints, cp);
1061 if (verinfo == NULL || ival == verinfo->md_ver_preferred) {
1065 if (ival >= verinfo->md_ver_minimum &&
1066 ival <= verinfo->md_ver_maximum &&
1076 recptr += reclen + sizeof(int);
1079 * Finally check if KLD is in the place
1082 result = file_lookup(mdp->d_path, (const char *)cp, clen, NULL);
1084 result = file_lookup(mdp->d_path, (const char *)best, blen, NULL);
1087 * If nothing found or hints is absent - fallback to the old way
1088 * by using "kldname[.ko]" as module name.
1090 if (!found && !bestver && result == NULL)
1091 result = file_lookup(mdp->d_path, modname, modnamelen, kld_ext_list);
1101 p = (int *)roundup2((intptr_t)p, sizeof(int));
1108 getstr(void **ptr, char *val)
1111 char *c = (char *)p;
1112 int len = *(uint8_t *)c;
1114 memcpy(val, c + 1, len);
1121 pnpval_as_int(const char *val, const char *pnpinfo)
1127 if (pnpinfo == NULL)
1130 cp = strchr(val, ';');
1133 strlcpy(key + 1, val, sizeof(key) - 1);
1135 memcpy(key + 1, val, cp - val);
1136 key[cp - val + 1] = '\0';
1138 strlcat(key, "=", sizeof(key));
1139 if (strncmp(key + 1, pnpinfo, strlen(key + 1)) == 0)
1140 rv = strtol(pnpinfo + strlen(key + 1), NULL, 0);
1142 cp = strstr(pnpinfo, key);
1146 rv = strtol(cp + strlen(key), NULL, 0);
1152 quoted_strcpy(char *dst, const char *src)
1156 if (*src == '\'' || *src == '"')
1158 while (*src && *src != q)
1159 *dst++ = *src++; // XXX backtick quoting
1165 pnpval_as_str(const char *val, const char *pnpinfo)
1167 static char retval[256];
1171 if (pnpinfo == NULL) {
1176 cp = strchr(val, ';');
1179 strlcpy(key + 1, val, sizeof(key) - 1);
1181 memcpy(key + 1, val, cp - val);
1182 key[cp - val + 1] = '\0';
1184 strlcat(key, "=", sizeof(key));
1185 if (strncmp(key + 1, pnpinfo, strlen(key + 1)) == 0)
1186 quoted_strcpy(retval, pnpinfo + strlen(key + 1));
1188 cp = strstr(pnpinfo, key);
1190 strcpy(retval, "MISSING");
1192 quoted_strcpy(retval, cp + strlen(key));
1198 devmatch_search_hints(struct moduledir *mdp, const char *bus, const char *dev, const char *pnpinfo)
1200 char val1[256], val2[256];
1201 int ival, len, ents, i, notme, mask, bit, v, found;
1202 void *ptr, *walker, *hints_end;
1203 char *lastmod = NULL, *cp, *s;
1205 moduledir_readhints(mdp);
1207 if (mdp->d_hints == NULL)
1209 walker = mdp->d_hints;
1210 hints_end = walker + mdp->d_hintsz;
1211 while (walker < hints_end && !found) {
1212 len = getint(&walker);
1213 ival = getint(&walker);
1218 ival = getint(&ptr);
1220 if (pnp_dump_flag || pnp_verbose_flag)
1221 printf("Version: if %s.%d kmod %s\n", val1, ival, val2);
1228 lastmod = strdup(val2);
1229 if (pnp_dump_flag || pnp_verbose_flag)
1230 printf("module %s in %s\n", val1, val1);
1233 if (!pnp_dump_flag && !pnp_unbound_flag && lastmod && strcmp(lastmod, "kernel") == 0)
1237 ents = getint(&ptr);
1238 if (pnp_dump_flag || pnp_verbose_flag)
1239 printf("PNP info for bus %s format %s %d entries (%s)\n",
1240 val1, val2, ents, lastmod);
1241 if (strcmp(val1, "usb") == 0) {
1242 if (pnp_verbose_flag)
1243 printf("Treating usb as uhub -- bug in source table still?\n");
1244 strcpy(val1, "uhub");
1246 if (bus && strcmp(val1, bus) != 0) {
1247 if (pnp_verbose_flag)
1248 printf("Skipped because table for bus %s, looking for %s\n",
1252 for (i = 0; i < ents; i++) {
1253 if (pnp_verbose_flag)
1254 printf("---------- Entry %d ----------\n", i);
1263 /* All integer fields */
1269 ival = getint(&ptr);
1270 if (pnp_dump_flag) {
1271 printf("%#x:", ival);
1274 if (bit >= 0 && ((1 << bit) & mask) == 0)
1276 v = pnpval_as_int(cp + 2, pnpinfo);
1277 if (pnp_verbose_flag)
1278 printf("Matching %s (%c) table=%#x tomatch=%#x\n",
1279 cp + 2, *cp, v, ival);
1306 if (pnp_dump_flag) {
1307 printf("'%s':", val1);
1312 s = pnpval_as_str(cp + 2, pnpinfo);
1313 if (strcmp(s, val1) != 0)
1316 /* Key override fields, required to be last in the string */
1319 * This is imperfect and only does one key and will be redone
1320 * to be more general for multiple keys. Currently, nothing
1323 if (pnp_dump_flag) /* No per-row data stored */
1325 if (cp[strlen(cp) - 1] == ';') /* Skip required ; at end */
1326 cp[strlen(cp) - 1] = '\0'; /* in case it's not there */
1327 if ((s = strstr(pnpinfo, cp + 2)) == NULL)
1329 else if (s > pnpinfo && s[-1] != ' ')
1333 printf("Unknown field type %c\n:", *cp);
1337 cp = strchr(cp, ';');
1340 } while (cp && *cp);
1344 if (!pnp_unbound_flag) {
1345 if (pnp_verbose_flag)
1346 printf("Matches --- %s ---\n", lastmod);
1355 walker = (void *)(len - sizeof(int) + (intptr_t)walker);
1357 if (pnp_unbound_flag && found == 0 && *pnpinfo) {
1358 if (pnp_verbose_flag)
1359 printf("------------------------- ");
1360 printf("%s on %s pnpinfo %s", *dev ? dev : "unattached", bus, pnpinfo);
1361 if (pnp_verbose_flag)
1362 printf(" -------------------------");
1374 * Attempt to locate the file containing the module (name)
1377 mod_searchmodule(char *name, struct mod_depend *verinfo)
1379 struct moduledir *mdp;
1382 moduledir_rebuild();
1384 * Now we ready to lookup module in the given directories
1387 STAILQ_FOREACH(mdp, &moduledir_list, d_link) {
1388 result = mod_search_hints(mdp, name, verinfo);
1397 mod_searchmodule_pnpinfo(const char *bus, const char *pnpinfo)
1399 struct moduledir *mdp;
1402 moduledir_rebuild();
1404 * Now we ready to lookup module in the given directories
1407 STAILQ_FOREACH(mdp, &moduledir_list, d_link) {
1408 result = devmatch_search_hints(mdp, bus, NULL, pnpinfo);
1417 file_addmodule(struct preloaded_file *fp, char *modname, int version,
1418 struct kernel_module **newmp)
1420 struct kernel_module *mp;
1421 struct mod_depend mdepend;
1423 bzero(&mdepend, sizeof(mdepend));
1424 mdepend.md_ver_preferred = version;
1425 mp = file_findmodule(fp, modname, &mdepend);
1428 mp = calloc(1, sizeof(struct kernel_module));
1431 mp->m_name = strdup(modname);
1432 if (mp->m_name == NULL) {
1436 mp->m_version = version;
1438 mp->m_next = fp->f_modules;
1449 file_discard(struct preloaded_file *fp)
1451 struct file_metadata *md, *md1;
1452 struct kernel_module *mp, *mp1;
1455 md = fp->f_metadata;
1475 * Allocate a new file; must be used instead of malloc()
1476 * to ensure safe initialisation.
1478 struct preloaded_file *
1482 return (calloc(1, sizeof(struct preloaded_file)));
1486 * Add a module to the chain
1489 file_insert_tail(struct preloaded_file *fp)
1491 struct preloaded_file *cm;
1493 /* Append to list of loaded file */
1495 if (preloaded_files == NULL) {
1496 preloaded_files = fp;
1498 for (cm = preloaded_files; cm->f_next != NULL; cm = cm->f_next)
1505 * Remove module from the chain
1508 file_remove(struct preloaded_file *fp)
1510 struct preloaded_file *cm;
1512 if (preloaded_files == NULL)
1515 if (preloaded_files == fp) {
1516 preloaded_files = fp->f_next;
1519 for (cm = preloaded_files; cm->f_next != NULL; cm = cm->f_next) {
1520 if (cm->f_next == fp) {
1521 cm->f_next = fp->f_next;
1528 moduledir_fullpath(struct moduledir *mdp, const char *fname)
1532 cp = malloc(strlen(mdp->d_path) + strlen(fname) + 2);
1535 strcpy(cp, mdp->d_path);
1542 * Read linker.hints file into memory performing some sanity checks.
1545 moduledir_readhints(struct moduledir *mdp)
1549 int fd, size, version;
1551 if (mdp->d_hints != NULL || (mdp->d_flags & MDIR_NOHINTS))
1553 path = moduledir_fullpath(mdp, "linker.hints");
1554 if (stat(path, &st) != 0 ||
1555 st.st_size < (ssize_t)(sizeof(version) + sizeof(int)) ||
1556 st.st_size > LINKER_HINTS_MAX || (fd = open(path, O_RDONLY)) < 0) {
1558 mdp->d_flags |= MDIR_NOHINTS;
1562 size = read(fd, &version, sizeof(version));
1563 if (size != sizeof(version) || version != LINKER_HINTS_VERSION)
1565 size = st.st_size - size;
1566 mdp->d_hints = malloc(size);
1567 if (mdp->d_hints == NULL)
1569 if (read(fd, mdp->d_hints, size) != size)
1571 mdp->d_hintsz = size;
1577 mdp->d_hints = NULL;
1578 mdp->d_flags |= MDIR_NOHINTS;
1583 * Extract directories from the ';' separated list, remove duplicates.
1586 moduledir_rebuild(void)
1588 struct moduledir *mdp, *mtmp;
1589 const char *path, *cp, *ep;
1592 path = getenv("module_path");
1594 path = default_searchpath;
1596 * Rebuild list of module directories if it changed
1598 STAILQ_FOREACH(mdp, &moduledir_list, d_link)
1599 mdp->d_flags |= MDIR_REMOVED;
1601 for (ep = path; *ep != 0; ep++) {
1603 for (; *ep != 0 && *ep != ';'; ep++)
1606 * Ignore trailing slashes
1608 for (cplen = ep - cp; cplen > 1 && cp[cplen - 1] == '/'; cplen--)
1610 STAILQ_FOREACH(mdp, &moduledir_list, d_link) {
1611 if (strlen(mdp->d_path) != cplen || bcmp(cp, mdp->d_path, cplen) != 0)
1613 mdp->d_flags &= ~MDIR_REMOVED;
1617 mdp = malloc(sizeof(*mdp) + cplen + 1);
1620 mdp->d_path = (char*)(mdp + 1);
1621 bcopy(cp, mdp->d_path, cplen);
1622 mdp->d_path[cplen] = 0;
1623 mdp->d_hints = NULL;
1625 STAILQ_INSERT_TAIL(&moduledir_list, mdp, d_link);
1631 * Delete unused directories if any
1633 mdp = STAILQ_FIRST(&moduledir_list);
1635 if ((mdp->d_flags & MDIR_REMOVED) == 0) {
1636 mdp = STAILQ_NEXT(mdp, d_link);
1640 mdp = STAILQ_NEXT(mdp, d_link);
1641 STAILQ_REMOVE(&moduledir_list, mtmp, moduledir, d_link);