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_all_flag = 0;
354 static int pnp_dump_flag = 0;
355 static int pnp_unbound_flag = 0;
356 static int pnp_verbose_flag = 0;
359 command_pnpmatch(int argc, char *argv[])
364 pnp_verbose_flag = 0;
368 while ((ch = getopt(argc, argv, "vd")) != -1) {
371 pnp_verbose_flag = 1;
378 /* getopt has already reported an error */
382 argv += (optind - 1);
383 argc -= (optind - 1);
385 module = mod_searchmodule_pnpinfo(argv[1], argv[2]);
387 printf("Matched module: %s\n", module);
389 printf("No module matches %s\n", argv[1]);
394 COMMAND_SET(pnpload, "pnpload", "load matched modules based on pnpinfo", command_pnpload);
397 command_pnpload(int argc, char *argv[])
402 pnp_verbose_flag = 0;
406 while ((ch = getopt(argc, argv, "vd")) != -1) {
409 pnp_verbose_flag = 1;
416 /* getopt has already reported an error */
420 argv += (optind - 1);
421 argc -= (optind - 1);
426 module = mod_searchmodule_pnpinfo(argv[1], argv[2]);
428 error = mod_load(module, NULL, 0, NULL);
429 if (error == EEXIST) {
430 snprintf(command_errbuf, sizeof(command_errbuf),
431 "warning: module '%s' already loaded", argv[1]);
435 return (error == 0 ? CMD_OK : CMD_CRIT);
438 #if defined(LOADER_FDT_SUPPORT)
440 pnpautoload_simplebus(void) {
441 const char *pnpstring;
442 const char *compatstr;
443 char *pnpinfo = NULL;
445 int tag = 0, len, pnplen;
449 pnpstring = fdt_devmatch_next(&tag, &len);
450 if (pnpstring == NULL)
453 compatstr = pnpstring;
454 for (pnplen = 0; pnplen != len; compatstr = pnpstring + pnplen) {
455 pnplen += strlen(compatstr) + 1;
456 asprintf(&pnpinfo, "compat=%s", compatstr);
458 module = mod_searchmodule_pnpinfo("simplebus", pnpinfo);
460 error = mod_loadkld(module, 0, NULL);
462 printf("Cannot load module %s\n", module);
477 struct pnp_bus pnp_buses[] = {
478 #if defined(LOADER_FDT_SUPPORT)
479 {"simplebus", pnpautoload_simplebus},
483 COMMAND_SET(pnpautoload, "pnpautoload", "auto load modules based on pnpinfo", command_pnpautoload);
486 command_pnpautoload(int argc, char *argv[])
490 int ch, error, match;
492 pnp_verbose_flag = 0;
498 while ((ch = getopt(argc, argv, "v")) != -1) {
505 /* getopt has already reported an error */
509 argv += (optind - 1);
510 argc -= (optind - 1);
515 for (i = 0; i < nitems(pnp_buses); i++) {
516 if (argc == 2 && strcmp(argv[1], pnp_buses[i].name) != 0) {
518 printf("Skipping bus %s\n", pnp_buses[i].name);
522 printf("Autoloading modules for simplebus\n");
527 printf("Unsupported bus %s\n", argv[1]);
533 * File level interface, functions file_*
536 file_load(char *filename, vm_offset_t dest, struct preloaded_file **result)
538 static int last_file_format = 0;
539 struct preloaded_file *fp;
543 if (archsw.arch_loadaddr != NULL)
544 dest = archsw.arch_loadaddr(LOAD_RAW, filename, dest);
547 for (i = last_file_format, fp = NULL;
548 file_formats[i] && fp == NULL; i++) {
549 error = (file_formats[i]->l_load)(filename, dest, &fp);
551 fp->f_loader = last_file_format = i; /* remember the loader */
554 } else if (last_file_format == i && i != 0) {
555 /* Restart from the beginning */
557 last_file_format = 0;
562 continue; /* Unknown to this handler? */
564 snprintf(command_errbuf, sizeof(command_errbuf),
565 "can't load file '%s': %s", filename, strerror(error));
573 file_load_dependencies(struct preloaded_file *base_file)
575 struct file_metadata *md;
576 struct preloaded_file *fp;
577 struct mod_depend *verinfo;
578 struct kernel_module *mp;
582 md = file_findmetadata(base_file, MODINFOMD_DEPLIST);
587 verinfo = (struct mod_depend*)md->md_data;
588 dmodname = (char *)(verinfo + 1);
589 if (file_findmodule(NULL, dmodname, verinfo) == NULL) {
590 printf("loading required module '%s'\n", dmodname);
591 error = mod_load(dmodname, verinfo, 0, NULL);
595 * If module loaded via kld name which isn't listed
596 * in the linker.hints file, we should check if it have
599 mp = file_findmodule(NULL, dmodname, verinfo);
601 snprintf(command_errbuf, sizeof(command_errbuf),
602 "module '%s' exists but with wrong version", dmodname);
607 md = metadata_next(md, MODINFOMD_DEPLIST);
611 /* Load failed; discard everything */
612 while (base_file != NULL) {
614 base_file = base_file->f_next;
621 * We've been asked to load (fname) as (type), so just suck it in,
622 * no arguments or anything.
624 struct preloaded_file *
625 file_loadraw(const char *fname, char *type, int insert)
627 struct preloaded_file *fp;
632 /* We can't load first */
633 if ((file_findfile(NULL, NULL)) == NULL) {
634 command_errmsg = "can't load file before kernel";
638 /* locate the file on the load path */
639 name = file_search(fname, NULL);
641 snprintf(command_errbuf, sizeof(command_errbuf),
642 "can't find '%s'", fname);
646 if ((fd = open(name, O_RDONLY)) < 0) {
647 snprintf(command_errbuf, sizeof(command_errbuf),
648 "can't open '%s': %s", name, strerror(errno));
653 #ifdef LOADER_VERIEXEC
654 if (verify_file(fd, name, 0, VE_MUST) < 0) {
655 sprintf(command_errbuf, "can't verify '%s'", name);
662 if (archsw.arch_loadaddr != NULL)
663 loadaddr = archsw.arch_loadaddr(LOAD_RAW, name, loadaddr);
669 /* read in 4k chunks; size is not really important */
670 got = archsw.arch_readin(fd, laddr, 4096);
671 if (got == 0) /* end of file */
673 if (got < 0) { /* error */
674 snprintf(command_errbuf, sizeof(command_errbuf),
675 "error reading '%s': %s", name, strerror(errno));
683 printf("size=%#jx\n", (uintmax_t)(laddr - loadaddr));
685 /* Looks OK so far; create & populate control structure */
688 snprintf(command_errbuf, sizeof (command_errbuf),
689 "no memory to load %s", name);
695 fp->f_type = strdup(type);
697 fp->f_metadata = NULL;
699 fp->f_addr = loadaddr;
700 fp->f_size = laddr - loadaddr;
702 if (fp->f_type == NULL) {
703 snprintf(command_errbuf, sizeof (command_errbuf),
704 "no memory to load %s", name);
709 /* recognise space consumption */
712 /* Add to the list of loaded files */
714 file_insert_tail(fp);
720 * Load the module (name), pass it (argc),(argv), add container file
721 * to the list of loaded files.
722 * If module is already loaded just assign new argc/argv.
725 mod_load(char *modname, struct mod_depend *verinfo, int argc, char *argv[])
727 struct kernel_module *mp;
731 if (file_havepath(modname)) {
732 printf("Warning: mod_load() called instead of mod_loadkld() for module '%s'\n", modname);
733 return (mod_loadkld(modname, argc, argv));
735 /* see if module is already loaded */
736 mp = file_findmodule(NULL, modname, verinfo);
740 mp->m_args = unargv(argc, argv);
742 snprintf(command_errbuf, sizeof(command_errbuf),
743 "warning: module '%s' already loaded", mp->m_name);
746 /* locate file with the module on the search path */
747 filename = mod_searchmodule(modname, verinfo);
748 if (filename == NULL) {
749 snprintf(command_errbuf, sizeof(command_errbuf),
750 "can't find '%s'", modname);
753 err = mod_loadkld(filename, argc, argv);
759 * Load specified KLD. If path is omitted, then try to locate it via
763 mod_loadkld(const char *kldname, int argc, char *argv[])
765 struct preloaded_file *fp;
768 vm_offset_t loadaddr_saved;
771 * Get fully qualified KLD name
773 filename = file_search(kldname, kld_ext_list);
774 if (filename == NULL) {
775 snprintf(command_errbuf, sizeof(command_errbuf),
776 "can't find '%s'", kldname);
780 * Check if KLD already loaded
782 fp = file_findfile(filename, NULL);
784 snprintf(command_errbuf, sizeof(command_errbuf),
785 "warning: KLD '%s' already loaded", filename);
791 err = file_load(filename, loadaddr, &fp);
794 fp->f_args = unargv(argc, argv);
795 loadaddr_saved = loadaddr;
796 loadaddr = fp->f_addr + fp->f_size;
797 file_insert_tail(fp); /* Add to the list of loaded files */
798 if (file_load_dependencies(fp) != 0) {
801 loadaddr = loadaddr_saved;
807 snprintf(command_errbuf, sizeof(command_errbuf),
808 "don't know how to load module '%s'", filename);
817 * Find a file matching (name) and (type).
818 * NULL may be passed as a wildcard to either.
820 struct preloaded_file *
821 file_findfile(const char *name, const char *type)
823 struct preloaded_file *fp;
825 for (fp = preloaded_files; fp != NULL; fp = fp->f_next) {
826 if (((name == NULL) || !strcmp(name, fp->f_name)) &&
827 ((type == NULL) || !strcmp(type, fp->f_type)))
834 * Find a module matching (name) inside of given file.
835 * NULL may be passed as a wildcard.
837 struct kernel_module *
838 file_findmodule(struct preloaded_file *fp, char *modname,
839 struct mod_depend *verinfo)
841 struct kernel_module *mp, *best;
845 for (fp = preloaded_files; fp; fp = fp->f_next) {
846 mp = file_findmodule(fp, modname, verinfo);
854 for (mp = fp->f_modules; mp; mp = mp->m_next) {
855 if (strcmp(modname, mp->m_name) == 0) {
858 mver = mp->m_version;
859 if (mver == verinfo->md_ver_preferred)
861 if (mver >= verinfo->md_ver_minimum &&
862 mver <= verinfo->md_ver_maximum &&
872 * Make a copy of (size) bytes of data from (p), and associate them as
873 * metadata of (type) to the module (mp).
876 file_addmetadata(struct preloaded_file *fp, int type, size_t size, void *p)
878 struct file_metadata *md;
880 md = malloc(sizeof(struct file_metadata) - sizeof(md->md_data) + size);
884 bcopy(p, md->md_data, size);
885 md->md_next = fp->f_metadata;
891 * Find a metadata object of (type) associated with the file (fp)
893 struct file_metadata *
894 file_findmetadata(struct preloaded_file *fp, int type)
896 struct file_metadata *md;
898 for (md = fp->f_metadata; md != NULL; md = md->md_next)
899 if (md->md_type == type)
905 * Remove all metadata from the file.
908 file_removemetadata(struct preloaded_file *fp)
910 struct file_metadata *md, *next;
912 for (md = fp->f_metadata; md != NULL; md = next)
917 fp->f_metadata = NULL;
920 struct file_metadata *
921 metadata_next(struct file_metadata *md, int type)
926 while((md = md->md_next) != NULL)
927 if (md->md_type == type)
932 static char *emptyextlist[] = { "", NULL };
935 * Check if the given file is in place and return full path to it.
938 file_lookup(const char *path, const char *name, int namelen, char **extlist)
941 char *result, *cp, **cpp;
942 int pathlen, extlen, len;
944 pathlen = strlen(path);
947 extlist = emptyextlist;
948 for (cpp = extlist; *cpp; cpp++) {
953 result = malloc(pathlen + namelen + extlen + 2);
956 bcopy(path, result, pathlen);
957 if (pathlen > 0 && result[pathlen - 1] != '/')
958 result[pathlen++] = '/';
959 cp = result + pathlen;
960 bcopy(name, cp, namelen);
962 for (cpp = extlist; *cpp; cpp++) {
964 if (stat(result, &st) == 0 && S_ISREG(st.st_mode))
972 * Check if file name have any qualifiers
975 file_havepath(const char *name)
979 archsw.arch_getdev(NULL, name, &cp);
980 return (cp != name || strchr(name, '/') != NULL);
984 * Attempt to find the file (name) on the module searchpath.
985 * If (name) is qualified in any way, we simply check it and
986 * return it or NULL. If it is not qualified, then we attempt
987 * to construct a path using entries in the environment variable
990 * The path we return a pointer to need never be freed, as we manage
994 file_search(const char *name, char **extlist)
996 struct moduledir *mdp;
1001 /* Don't look for nothing */
1006 return(strdup(name));
1008 if (file_havepath(name)) {
1009 /* Qualified, so just see if it exists */
1010 if (stat(name, &sb) == 0)
1011 return(strdup(name));
1014 moduledir_rebuild();
1016 namelen = strlen(name);
1017 STAILQ_FOREACH(mdp, &moduledir_list, d_link) {
1018 result = file_lookup(mdp->d_path, name, namelen, extlist);
1025 #define INT_ALIGN(base, ptr) ptr = \
1026 (base) + roundup2((ptr) - (base), sizeof(int))
1029 mod_search_hints(struct moduledir *mdp, const char *modname,
1030 struct mod_depend *verinfo)
1032 u_char *cp, *recptr, *bufend, *best;
1034 int *intp, bestver, blen, clen, found, ival, modnamelen, reclen;
1036 moduledir_readhints(mdp);
1037 modnamelen = strlen(modname);
1041 if (mdp->d_hints == NULL)
1043 recptr = mdp->d_hints;
1044 bufend = recptr + mdp->d_hintsz;
1047 while (recptr < bufend && !found) {
1048 intp = (int*)recptr;
1055 if (clen != modnamelen || bcmp(cp, modname, clen) != 0)
1058 INT_ALIGN(mdp->d_hints, cp);
1062 if (verinfo == NULL || ival == verinfo->md_ver_preferred) {
1066 if (ival >= verinfo->md_ver_minimum &&
1067 ival <= verinfo->md_ver_maximum &&
1077 recptr += reclen + sizeof(int);
1080 * Finally check if KLD is in the place
1083 result = file_lookup(mdp->d_path, (const char *)cp, clen, NULL);
1085 result = file_lookup(mdp->d_path, (const char *)best, blen, NULL);
1088 * If nothing found or hints is absent - fallback to the old way
1089 * by using "kldname[.ko]" as module name.
1091 if (!found && !bestver && result == NULL)
1092 result = file_lookup(mdp->d_path, modname, modnamelen, kld_ext_list);
1102 p = (int *)roundup2((intptr_t)p, sizeof(int));
1109 getstr(void **ptr, char *val)
1112 char *c = (char *)p;
1113 int len = *(uint8_t *)c;
1115 memcpy(val, c + 1, len);
1122 pnpval_as_int(const char *val, const char *pnpinfo)
1128 if (pnpinfo == NULL)
1131 cp = strchr(val, ';');
1134 strlcpy(key + 1, val, sizeof(key) - 1);
1136 memcpy(key + 1, val, cp - val);
1137 key[cp - val + 1] = '\0';
1139 strlcat(key, "=", sizeof(key));
1140 if (strncmp(key + 1, pnpinfo, strlen(key + 1)) == 0)
1141 rv = strtol(pnpinfo + strlen(key + 1), NULL, 0);
1143 cp = strstr(pnpinfo, key);
1147 rv = strtol(cp + strlen(key), NULL, 0);
1153 quoted_strcpy(char *dst, const char *src)
1157 if (*src == '\'' || *src == '"')
1159 while (*src && *src != q)
1160 *dst++ = *src++; // XXX backtick quoting
1166 pnpval_as_str(const char *val, const char *pnpinfo)
1168 static char retval[256];
1172 if (pnpinfo == NULL) {
1177 cp = strchr(val, ';');
1180 strlcpy(key + 1, val, sizeof(key) - 1);
1182 memcpy(key + 1, val, cp - val);
1183 key[cp - val + 1] = '\0';
1185 strlcat(key, "=", sizeof(key));
1186 if (strncmp(key + 1, pnpinfo, strlen(key + 1)) == 0)
1187 quoted_strcpy(retval, pnpinfo + strlen(key + 1));
1189 cp = strstr(pnpinfo, key);
1191 strcpy(retval, "MISSING");
1193 quoted_strcpy(retval, cp + strlen(key));
1199 devmatch_search_hints(struct moduledir *mdp, const char *bus, const char *dev, const char *pnpinfo)
1201 char val1[256], val2[256];
1202 int ival, len, ents, i, notme, mask, bit, v, found;
1203 void *ptr, *walker, *hints_end;
1204 char *lastmod = NULL, *cp, *s;
1206 moduledir_readhints(mdp);
1208 if (mdp->d_hints == NULL)
1210 walker = mdp->d_hints;
1211 hints_end = walker + mdp->d_hintsz;
1212 while (walker < hints_end && !found) {
1213 len = getint(&walker);
1214 ival = getint(&walker);
1219 ival = getint(&ptr);
1221 if (pnp_dump_flag || pnp_verbose_flag)
1222 printf("Version: if %s.%d kmod %s\n", val1, ival, val2);
1229 lastmod = strdup(val2);
1230 if (pnp_dump_flag || pnp_verbose_flag)
1231 printf("module %s in %s\n", val1, val1);
1234 if (!pnp_dump_flag && !pnp_unbound_flag && lastmod && strcmp(lastmod, "kernel") == 0)
1238 ents = getint(&ptr);
1239 if (pnp_dump_flag || pnp_verbose_flag)
1240 printf("PNP info for bus %s format %s %d entries (%s)\n",
1241 val1, val2, ents, lastmod);
1242 if (strcmp(val1, "usb") == 0) {
1243 if (pnp_verbose_flag)
1244 printf("Treating usb as uhub -- bug in source table still?\n");
1245 strcpy(val1, "uhub");
1247 if (bus && strcmp(val1, bus) != 0) {
1248 if (pnp_verbose_flag)
1249 printf("Skipped because table for bus %s, looking for %s\n",
1253 for (i = 0; i < ents; i++) {
1254 if (pnp_verbose_flag)
1255 printf("---------- Entry %d ----------\n", i);
1264 /* All integer fields */
1270 ival = getint(&ptr);
1271 if (pnp_dump_flag) {
1272 printf("%#x:", ival);
1275 if (bit >= 0 && ((1 << bit) & mask) == 0)
1277 v = pnpval_as_int(cp + 2, pnpinfo);
1278 if (pnp_verbose_flag)
1279 printf("Matching %s (%c) table=%#x tomatch=%#x\n",
1280 cp + 2, *cp, v, ival);
1307 if (pnp_dump_flag) {
1308 printf("'%s':", val1);
1313 s = pnpval_as_str(cp + 2, pnpinfo);
1314 if (strcmp(s, val1) != 0)
1317 /* Key override fields, required to be last in the string */
1320 * This is imperfect and only does one key and will be redone
1321 * to be more general for multiple keys. Currently, nothing
1324 if (pnp_dump_flag) /* No per-row data stored */
1326 if (cp[strlen(cp) - 1] == ';') /* Skip required ; at end */
1327 cp[strlen(cp) - 1] = '\0'; /* in case it's not there */
1328 if ((s = strstr(pnpinfo, cp + 2)) == NULL)
1330 else if (s > pnpinfo && s[-1] != ' ')
1334 printf("Unknown field type %c\n:", *cp);
1338 cp = strchr(cp, ';');
1341 } while (cp && *cp);
1345 if (!pnp_unbound_flag) {
1346 if (pnp_verbose_flag)
1347 printf("Matches --- %s ---\n", lastmod);
1356 walker = (void *)(len - sizeof(int) + (intptr_t)walker);
1358 if (pnp_unbound_flag && found == 0 && *pnpinfo) {
1359 if (pnp_verbose_flag)
1360 printf("------------------------- ");
1361 printf("%s on %s pnpinfo %s", *dev ? dev : "unattached", bus, pnpinfo);
1362 if (pnp_verbose_flag)
1363 printf(" -------------------------");
1375 * Attempt to locate the file containing the module (name)
1378 mod_searchmodule(char *name, struct mod_depend *verinfo)
1380 struct moduledir *mdp;
1383 moduledir_rebuild();
1385 * Now we ready to lookup module in the given directories
1388 STAILQ_FOREACH(mdp, &moduledir_list, d_link) {
1389 result = mod_search_hints(mdp, name, verinfo);
1398 mod_searchmodule_pnpinfo(const char *bus, const char *pnpinfo)
1400 struct moduledir *mdp;
1403 moduledir_rebuild();
1405 * Now we ready to lookup module in the given directories
1408 STAILQ_FOREACH(mdp, &moduledir_list, d_link) {
1409 result = devmatch_search_hints(mdp, bus, NULL, pnpinfo);
1418 file_addmodule(struct preloaded_file *fp, char *modname, int version,
1419 struct kernel_module **newmp)
1421 struct kernel_module *mp;
1422 struct mod_depend mdepend;
1424 bzero(&mdepend, sizeof(mdepend));
1425 mdepend.md_ver_preferred = version;
1426 mp = file_findmodule(fp, modname, &mdepend);
1429 mp = calloc(1, sizeof(struct kernel_module));
1432 mp->m_name = strdup(modname);
1433 if (mp->m_name == NULL) {
1437 mp->m_version = version;
1439 mp->m_next = fp->f_modules;
1450 file_discard(struct preloaded_file *fp)
1452 struct file_metadata *md, *md1;
1453 struct kernel_module *mp, *mp1;
1456 md = fp->f_metadata;
1476 * Allocate a new file; must be used instead of malloc()
1477 * to ensure safe initialisation.
1479 struct preloaded_file *
1483 return (calloc(1, sizeof(struct preloaded_file)));
1487 * Add a module to the chain
1490 file_insert_tail(struct preloaded_file *fp)
1492 struct preloaded_file *cm;
1494 /* Append to list of loaded file */
1496 if (preloaded_files == NULL) {
1497 preloaded_files = fp;
1499 for (cm = preloaded_files; cm->f_next != NULL; cm = cm->f_next)
1506 * Remove module from the chain
1509 file_remove(struct preloaded_file *fp)
1511 struct preloaded_file *cm;
1513 if (preloaded_files == NULL)
1516 if (preloaded_files == fp) {
1517 preloaded_files = fp->f_next;
1520 for (cm = preloaded_files; cm->f_next != NULL; cm = cm->f_next) {
1521 if (cm->f_next == fp) {
1522 cm->f_next = fp->f_next;
1529 moduledir_fullpath(struct moduledir *mdp, const char *fname)
1533 cp = malloc(strlen(mdp->d_path) + strlen(fname) + 2);
1536 strcpy(cp, mdp->d_path);
1543 * Read linker.hints file into memory performing some sanity checks.
1546 moduledir_readhints(struct moduledir *mdp)
1550 int fd, size, version;
1552 if (mdp->d_hints != NULL || (mdp->d_flags & MDIR_NOHINTS))
1554 path = moduledir_fullpath(mdp, "linker.hints");
1555 if (stat(path, &st) != 0 ||
1556 st.st_size < (ssize_t)(sizeof(version) + sizeof(int)) ||
1557 st.st_size > LINKER_HINTS_MAX || (fd = open(path, O_RDONLY)) < 0) {
1559 mdp->d_flags |= MDIR_NOHINTS;
1563 size = read(fd, &version, sizeof(version));
1564 if (size != sizeof(version) || version != LINKER_HINTS_VERSION)
1566 size = st.st_size - size;
1567 mdp->d_hints = malloc(size);
1568 if (mdp->d_hints == NULL)
1570 if (read(fd, mdp->d_hints, size) != size)
1572 mdp->d_hintsz = size;
1578 mdp->d_hints = NULL;
1579 mdp->d_flags |= MDIR_NOHINTS;
1584 * Extract directories from the ';' separated list, remove duplicates.
1587 moduledir_rebuild(void)
1589 struct moduledir *mdp, *mtmp;
1590 const char *path, *cp, *ep;
1593 path = getenv("module_path");
1595 path = default_searchpath;
1597 * Rebuild list of module directories if it changed
1599 STAILQ_FOREACH(mdp, &moduledir_list, d_link)
1600 mdp->d_flags |= MDIR_REMOVED;
1602 for (ep = path; *ep != 0; ep++) {
1604 for (; *ep != 0 && *ep != ';'; ep++)
1607 * Ignore trailing slashes
1609 for (cplen = ep - cp; cplen > 1 && cp[cplen - 1] == '/'; cplen--)
1611 STAILQ_FOREACH(mdp, &moduledir_list, d_link) {
1612 if (strlen(mdp->d_path) != cplen || bcmp(cp, mdp->d_path, cplen) != 0)
1614 mdp->d_flags &= ~MDIR_REMOVED;
1618 mdp = malloc(sizeof(*mdp) + cplen + 1);
1621 mdp->d_path = (char*)(mdp + 1);
1622 bcopy(cp, mdp->d_path, cplen);
1623 mdp->d_path[cplen] = 0;
1624 mdp->d_hints = NULL;
1626 STAILQ_INSERT_TAIL(&moduledir_list, mdp, d_link);
1632 * Delete unused directories if any
1634 mdp = STAILQ_FIRST(&moduledir_list);
1636 if ((mdp->d_flags & MDIR_REMOVED) == 0) {
1637 mdp = STAILQ_NEXT(mdp, d_link);
1641 mdp = STAILQ_NEXT(mdp, d_link);
1642 STAILQ_REMOVE(&moduledir_list, mtmp, moduledir, d_link);