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>
44 #if defined(LOADER_FDT_SUPPORT)
45 #include <fdt_platform.h>
48 #include "bootstrap.h"
50 #define MDIR_REMOVED 0x0001
51 #define MDIR_NOHINTS 0x0002
54 char *d_path; /* path of modules directory */
55 u_char *d_hints; /* content of linker.hints file */
56 int d_hintsz; /* size of hints data */
58 STAILQ_ENTRY(moduledir) d_link;
61 static int file_load(char *filename, vm_offset_t dest, struct preloaded_file **result);
62 static int file_load_dependencies(struct preloaded_file *base_mod);
63 static char * file_search(const char *name, char **extlist);
64 static struct kernel_module * file_findmodule(struct preloaded_file *fp, char *modname, struct mod_depend *verinfo);
65 static int file_havepath(const char *name);
66 static char *mod_searchmodule(char *name, struct mod_depend *verinfo);
67 static char * mod_searchmodule_pnpinfo(const char *bus, const char *pnpinfo);
68 static void file_insert_tail(struct preloaded_file *mp);
69 static void file_remove(struct preloaded_file *fp);
70 struct file_metadata* metadata_next(struct file_metadata *base_mp, int type);
71 static void moduledir_readhints(struct moduledir *mdp);
72 static void moduledir_rebuild(void);
74 /* load address should be tweaked by first module loaded (kernel) */
75 static vm_offset_t loadaddr = 0;
77 #if defined(LOADER_FDT_SUPPORT)
78 static const char *default_searchpath =
79 "/boot/kernel;/boot/modules;/boot/dtb";
81 static const char *default_searchpath = "/boot/kernel;/boot/modules";
84 static STAILQ_HEAD(, moduledir) moduledir_list =
85 STAILQ_HEAD_INITIALIZER(moduledir_list);
87 struct preloaded_file *preloaded_files = NULL;
89 static char *kld_ext_list[] = {
97 * load an object, either a disk file or code module.
99 * To load a file, the syntax is:
101 * load -t <type> <path>
103 * code modules are loaded as:
105 * load <path> <options>
108 COMMAND_SET(load, "load", "load a kernel or module", command_load);
111 command_load(int argc, char *argv[])
113 struct preloaded_file *fp;
115 #ifdef LOADER_VERIEXEC
118 int dflag, dofile, dokld, ch, error;
120 dflag = dokld = dofile = 0;
125 command_errmsg = "no filename specified";
128 #ifdef LOADER_VERIEXEC
132 while ((ch = getopt(argc, argv, "dkp:s:t:")) != -1) {
140 #ifdef LOADER_VERIEXEC
154 /* getopt has already reported an error */
158 argv += (optind - 1);
159 argc -= (optind - 1);
162 * Request to load a raw file?
165 if ((argc != 2) || (typestr == NULL) || (*typestr == 0)) {
166 command_errmsg = "invalid load type";
170 #ifdef LOADER_VERIEXEC
171 if (strncmp(typestr, "manifest", 8) == 0) {
174 return (load_manifest(argv[1], prefix, skip, NULL));
176 #ifdef LOADER_VERIEXEC_PASS_MANIFEST
177 if (strncmp(typestr, "pass_manifest", 13) == 0) {
180 return (pass_manifest(argv[1], prefix));
185 fp = file_findfile(argv[1], typestr);
187 snprintf(command_errbuf, sizeof(command_errbuf),
188 "warning: file '%s' already loaded", argv[1]);
192 if (file_loadraw(argv[1], typestr, 1) != NULL)
195 /* Failing to load mfs_root is never going to end well! */
196 if (strcmp("mfs_root", typestr) == 0)
202 * Do we have explicit KLD load ?
204 if (dokld || file_havepath(argv[1])) {
205 error = mod_loadkld(argv[1], argc - 2, argv + 2);
206 if (error == EEXIST) {
207 snprintf(command_errbuf, sizeof(command_errbuf),
208 "warning: KLD '%s' already loaded", argv[1]);
212 return (error == 0 ? CMD_OK : CMD_CRIT);
215 * Looks like a request for a module.
217 error = mod_load(argv[1], NULL, argc - 2, argv + 2);
218 if (error == EEXIST) {
219 snprintf(command_errbuf, sizeof(command_errbuf),
220 "warning: module '%s' already loaded", argv[1]);
224 return (error == 0 ? CMD_OK : CMD_CRIT);
227 #ifdef LOADER_GELI_SUPPORT
228 COMMAND_SET(load_geli, "load_geli", "load a geli key", command_load_geli);
231 command_load_geli(int argc, char *argv[])
238 command_errmsg = "usage is [-n key#] <prov> <file>";
245 while ((ch = getopt(argc, argv, "n:")) != -1) {
248 num = strtol(optarg, &cp, 0);
250 snprintf(command_errbuf, sizeof(command_errbuf),
251 "bad key index '%s'", optarg);
257 /* getopt has already reported an error */
261 argv += (optind - 1);
262 argc -= (optind - 1);
263 sprintf(typestr, "%s:geli_keyfile%d", argv[1], num);
264 return (file_loadraw(argv[2], typestr, 1) ? CMD_OK : CMD_ERROR);
271 struct preloaded_file *fp;
273 while (preloaded_files != NULL) {
274 fp = preloaded_files;
275 preloaded_files = preloaded_files->f_next;
279 unsetenv("kernelname");
282 COMMAND_SET(unload, "unload", "unload all modules", command_unload);
285 command_unload(int argc, char *argv[])
291 COMMAND_SET(lsmod, "lsmod", "list loaded modules", command_lsmod);
294 command_lsmod(int argc, char *argv[])
296 struct preloaded_file *fp;
297 struct kernel_module *mp;
298 struct file_metadata *md;
300 int ch, verbose, ret = 0;
305 while ((ch = getopt(argc, argv, "v")) != -1) {
312 /* getopt has already reported an error */
318 for (fp = preloaded_files; fp; fp = fp->f_next) {
319 snprintf(lbuf, sizeof(lbuf), " %p: ", (void *) fp->f_addr);
321 pager_output(fp->f_name);
322 snprintf(lbuf, sizeof(lbuf), " (%s, 0x%lx)\n", fp->f_type,
324 if (pager_output(lbuf))
326 if (fp->f_args != NULL) {
327 pager_output(" args: ");
328 pager_output(fp->f_args);
329 if (pager_output("\n"))
333 pager_output(" modules: ");
334 for (mp = fp->f_modules; mp; mp = mp->m_next) {
335 snprintf(lbuf, sizeof(lbuf), "%s.%d ", mp->m_name,
339 if (pager_output("\n"))
343 /* XXX could add some formatting smarts here to display some better */
344 for (md = fp->f_metadata; md != NULL; md = md->md_next) {
345 snprintf(lbuf, sizeof(lbuf), " 0x%04x, 0x%lx\n",
346 md->md_type, (long) md->md_size);
347 if (pager_output(lbuf))
358 COMMAND_SET(pnpmatch, "pnpmatch", "list matched modules based on pnpinfo", command_pnpmatch);
360 static int pnp_dump_flag = 0;
361 static int pnp_unbound_flag = 0;
362 static int pnp_verbose_flag = 0;
365 command_pnpmatch(int argc, char *argv[])
370 pnp_verbose_flag = 0;
374 while ((ch = getopt(argc, argv, "vd")) != -1) {
377 pnp_verbose_flag = 1;
384 /* getopt has already reported an error */
392 command_errmsg = "Usage: pnpmatch <busname> compat=<compatdata>";
396 module = mod_searchmodule_pnpinfo(argv[0], argv[1]);
398 printf("Matched module: %s\n", module);
400 printf("No module matches %s on bus %s\n", argv[1], argv[0]);
405 COMMAND_SET(pnpload, "pnpload", "load matched modules based on pnpinfo", command_pnpload);
408 command_pnpload(int argc, char *argv[])
413 pnp_verbose_flag = 0;
417 while ((ch = getopt(argc, argv, "vd")) != -1) {
420 pnp_verbose_flag = 1;
427 /* getopt has already reported an error */
435 command_errmsg = "Usage: pnpload <busname> compat=<compatdata>";
439 module = mod_searchmodule_pnpinfo(argv[0], argv[1]);
441 error = mod_load(module, NULL, 0, NULL);
442 if (error == EEXIST) {
443 snprintf(command_errbuf, sizeof(command_errbuf),
444 "warning: module '%s' already loaded", argv[1]);
448 return (error == 0 ? CMD_OK : CMD_CRIT);
451 #if defined(LOADER_FDT_SUPPORT)
453 pnpautoload_fdt_bus(const char *busname) {
454 const char *pnpstring;
455 const char *compatstr;
456 char *pnpinfo = NULL;
458 int tag = 0, len, pnplen;
462 pnpstring = fdt_devmatch_next(&tag, &len);
463 if (pnpstring == NULL)
466 compatstr = pnpstring;
467 for (pnplen = 0; pnplen != len; compatstr = pnpstring + pnplen) {
468 pnplen += strlen(compatstr) + 1;
469 asprintf(&pnpinfo, "compat=%s", compatstr);
471 module = mod_searchmodule_pnpinfo(busname, pnpinfo);
473 error = mod_loadkld(module, 0, NULL);
475 printf("Cannot load module %s\n", module);
487 void (*load)(const char *busname);
490 struct pnp_bus pnp_buses[] = {
491 #if defined(LOADER_FDT_SUPPORT)
492 {"simplebus", pnpautoload_fdt_bus},
493 {"ofwbus", pnpautoload_fdt_bus},
494 {"iicbus", pnpautoload_fdt_bus},
495 {"spibus", pnpautoload_fdt_bus},
499 COMMAND_SET(pnpautoload, "pnpautoload", "auto load modules based on pnpinfo", command_pnpautoload);
502 command_pnpautoload(int argc, char *argv[])
508 pnp_verbose_flag = 0;
514 while ((ch = getopt(argc, argv, "v")) != -1) {
521 /* getopt has already reported an error */
525 argv += (optind - 1);
526 argc -= (optind - 1);
531 for (i = 0; i < nitems(pnp_buses); i++) {
532 if (argc == 2 && strcmp(argv[1], pnp_buses[i].name) != 0) {
534 printf("Skipping bus %s\n", pnp_buses[i].name);
538 printf("Autoloading modules for %s\n", pnp_buses[i].name);
539 pnp_buses[i].load(pnp_buses[i].name);
543 printf("Unsupported bus %s\n", argv[1]);
549 * File level interface, functions file_*
552 file_load(char *filename, vm_offset_t dest, struct preloaded_file **result)
554 static int last_file_format = 0;
555 struct preloaded_file *fp;
560 if (archsw.arch_loadaddr != NULL)
561 dest = archsw.arch_loadaddr(LOAD_RAW, filename, dest);
564 for (i = last_file_format, fp = NULL;
565 file_formats[i] && fp == NULL; i++) {
566 error = (file_formats[i]->l_load)(filename, dest, &fp);
568 fp->f_loader = last_file_format = i; /* remember the loader */
571 } else if (last_file_format == i && i != 0) {
572 /* Restart from the beginning */
574 last_file_format = 0;
579 continue; /* Unknown to this handler? */
581 snprintf(command_errbuf, sizeof(command_errbuf),
582 "can't load file '%s': %s", filename, strerror(error));
591 file_load_dependencies(struct preloaded_file *base_file)
593 struct file_metadata *md;
594 struct preloaded_file *fp;
595 struct mod_depend *verinfo;
596 struct kernel_module *mp;
600 md = file_findmetadata(base_file, MODINFOMD_DEPLIST);
605 verinfo = (struct mod_depend*)md->md_data;
606 dmodname = (char *)(verinfo + 1);
607 if (file_findmodule(NULL, dmodname, verinfo) == NULL) {
608 printf("loading required module '%s'\n", dmodname);
609 error = mod_load(dmodname, verinfo, 0, NULL);
613 * If module loaded via kld name which isn't listed
614 * in the linker.hints file, we should check if it have
617 mp = file_findmodule(NULL, dmodname, verinfo);
619 snprintf(command_errbuf, sizeof(command_errbuf),
620 "module '%s' exists but with wrong version", dmodname);
625 md = metadata_next(md, MODINFOMD_DEPLIST);
629 /* Load failed; discard everything */
630 while (base_file != NULL) {
632 base_file = base_file->f_next;
639 build_font_module(vm_offset_t addr)
641 vt_font_bitmap_data_t *bd;
643 struct preloaded_file *fp;
651 if (STAILQ_EMPTY(&fonts))
654 /* We can't load first */
655 if ((file_findfile(NULL, NULL)) == NULL) {
656 printf("Can not load font module: %s\n",
657 "the kernel is not loaded");
661 /* helper pointers */
663 STAILQ_FOREACH(fl, &fonts, font_next) {
664 if (gfx_state.tg_font.vf_width == fl->font_data->vfbd_width &&
665 gfx_state.tg_font.vf_height == fl->font_data->vfbd_height) {
667 * Kernel does have better built in font.
669 if (fl->font_flags == FONT_BUILTIN)
680 fi.fi_width = fd->vf_width;
681 checksum = fi.fi_width;
682 fi.fi_height = fd->vf_height;
683 checksum += fi.fi_height;
684 fi.fi_bitmap_size = bd->vfbd_uncompressed_size;
685 checksum += fi.fi_bitmap_size;
687 size = roundup2(sizeof (struct font_info), 8);
688 for (i = 0; i < VFNT_MAPS; i++) {
689 fi.fi_map_count[i] = fd->vf_map_count[i];
690 checksum += fi.fi_map_count[i];
691 size += fd->vf_map_count[i] * sizeof (struct vfnt_map);
692 size += roundup2(size, 8);
694 size += bd->vfbd_uncompressed_size;
696 fi.fi_checksum = -checksum;
698 fp = file_findfile(NULL, "elf kernel");
700 fp = file_findfile(NULL, "elf64 kernel");
702 panic("can't find kernel file");
705 addr += archsw.arch_copyin(&fi, addr, sizeof (struct font_info));
706 addr = roundup2(addr, 8);
709 for (i = 0; i < VFNT_MAPS; i++) {
710 if (fd->vf_map_count[i] != 0) {
711 addr += archsw.arch_copyin(fd->vf_map[i], addr,
712 fd->vf_map_count[i] * sizeof (struct vfnt_map));
713 addr = roundup2(addr, 8);
717 /* Copy the bitmap. */
718 addr += archsw.arch_copyin(fd->vf_bytes, addr, fi.fi_bitmap_size);
720 /* Looks OK so far; populate control structure */
721 file_addmetadata(fp, MODINFOMD_FONT, sizeof(fontp), &fontp);
725 #ifdef LOADER_VERIEXEC_VECTX
726 #define VECTX_HANDLE(fd) vctx
728 #define VECTX_HANDLE(fd) fd
733 * We've been asked to load (fname) as (type), so just suck it in,
734 * no arguments or anything.
736 struct preloaded_file *
737 file_loadraw(const char *fname, char *type, int insert)
739 struct preloaded_file *fp;
743 #ifdef LOADER_VERIEXEC_VECTX
749 /* We can't load first */
750 if ((file_findfile(NULL, NULL)) == NULL) {
751 command_errmsg = "can't load file before kernel";
756 /* locate the file on the load path */
757 name = file_search(fname, NULL);
759 snprintf(command_errbuf, sizeof(command_errbuf),
760 "can't find '%s'", fname);
765 if ((fd = open(name, O_RDONLY)) < 0) {
766 snprintf(command_errbuf, sizeof(command_errbuf),
767 "can't open '%s': %s", name, strerror(errno));
773 #ifdef LOADER_VERIEXEC_VECTX
774 vctx = vectx_open(fd, name, 0L, NULL, &verror, __func__);
776 sprintf(command_errbuf, "can't verify '%s': %s",
777 name, ve_error_get());
785 #ifdef LOADER_VERIEXEC
786 if (verify_file(fd, name, 0, VE_MUST, __func__) < 0) {
787 sprintf(command_errbuf, "can't verify '%s': %s",
788 name, ve_error_get());
797 if (archsw.arch_loadaddr != NULL)
798 loadaddr = archsw.arch_loadaddr(LOAD_RAW, name, loadaddr);
804 /* read in 4k chunks; size is not really important */
805 got = archsw.arch_readin(VECTX_HANDLE(fd), laddr, 4096);
806 if (got == 0) /* end of file */
808 if (got < 0) { /* error */
809 snprintf(command_errbuf, sizeof(command_errbuf),
810 "error reading '%s': %s", name, strerror(errno));
813 #ifdef LOADER_VERIEXEC_VECTX
822 printf("size=%#jx\n", (uintmax_t)(laddr - loadaddr));
823 #ifdef LOADER_VERIEXEC_VECTX
824 verror = vectx_close(vctx, VE_MUST, __func__);
834 /* Looks OK so far; create & populate control structure */
837 snprintf(command_errbuf, sizeof (command_errbuf),
838 "no memory to load %s", name);
845 fp->f_type = strdup(type);
847 fp->f_metadata = NULL;
849 fp->f_addr = loadaddr;
850 fp->f_size = laddr - loadaddr;
852 if (fp->f_type == NULL) {
853 snprintf(command_errbuf, sizeof (command_errbuf),
854 "no memory to load %s", name);
860 /* recognise space consumption */
863 /* Add to the list of loaded files */
865 file_insert_tail(fp);
872 * Load the module (name), pass it (argc),(argv), add container file
873 * to the list of loaded files.
874 * If module is already loaded just assign new argc/argv.
877 mod_load(char *modname, struct mod_depend *verinfo, int argc, char *argv[])
879 struct kernel_module *mp;
884 if (file_havepath(modname)) {
885 printf("Warning: mod_load() called instead of mod_loadkld() for module '%s'\n", modname);
887 return (mod_loadkld(modname, argc, argv));
889 /* see if module is already loaded */
890 mp = file_findmodule(NULL, modname, verinfo);
894 mp->m_args = unargv(argc, argv);
896 snprintf(command_errbuf, sizeof(command_errbuf),
897 "warning: module '%s' already loaded", mp->m_name);
901 /* locate file with the module on the search path */
902 filename = mod_searchmodule(modname, verinfo);
903 if (filename == NULL) {
904 snprintf(command_errbuf, sizeof(command_errbuf),
905 "can't find '%s'", modname);
909 err = mod_loadkld(filename, argc, argv);
916 * Load specified KLD. If path is omitted, then try to locate it via
920 mod_loadkld(const char *kldname, int argc, char *argv[])
922 struct preloaded_file *fp;
925 vm_offset_t loadaddr_saved;
929 * Get fully qualified KLD name
931 filename = file_search(kldname, kld_ext_list);
932 if (filename == NULL) {
933 snprintf(command_errbuf, sizeof(command_errbuf),
934 "can't find '%s'", kldname);
939 * Check if KLD already loaded
941 fp = file_findfile(filename, NULL);
943 snprintf(command_errbuf, sizeof(command_errbuf),
944 "warning: KLD '%s' already loaded", filename);
951 err = file_load(filename, loadaddr, &fp);
954 fp->f_args = unargv(argc, argv);
955 loadaddr_saved = loadaddr;
956 loadaddr = fp->f_addr + fp->f_size;
957 file_insert_tail(fp); /* Add to the list of loaded files */
958 if (file_load_dependencies(fp) != 0) {
961 loadaddr = loadaddr_saved;
967 snprintf(command_errbuf, sizeof(command_errbuf),
968 "don't know how to load module '%s'", filename);
978 * Find a file matching (name) and (type).
979 * NULL may be passed as a wildcard to either.
981 struct preloaded_file *
982 file_findfile(const char *name, const char *type)
984 struct preloaded_file *fp;
986 for (fp = preloaded_files; fp != NULL; fp = fp->f_next) {
987 if (((name == NULL) || !strcmp(name, fp->f_name)) &&
988 ((type == NULL) || !strcmp(type, fp->f_type)))
995 * Find a module matching (name) inside of given file.
996 * NULL may be passed as a wildcard.
998 struct kernel_module *
999 file_findmodule(struct preloaded_file *fp, char *modname,
1000 struct mod_depend *verinfo)
1002 struct kernel_module *mp, *best;
1006 for (fp = preloaded_files; fp; fp = fp->f_next) {
1007 mp = file_findmodule(fp, modname, verinfo);
1015 for (mp = fp->f_modules; mp; mp = mp->m_next) {
1016 if (strcmp(modname, mp->m_name) == 0) {
1017 if (verinfo == NULL)
1019 mver = mp->m_version;
1020 if (mver == verinfo->md_ver_preferred)
1022 if (mver >= verinfo->md_ver_minimum &&
1023 mver <= verinfo->md_ver_maximum &&
1033 * Make a copy of (size) bytes of data from (p), and associate them as
1034 * metadata of (type) to the module (mp).
1037 file_addmetadata(struct preloaded_file *fp, int type, size_t size, void *p)
1039 struct file_metadata *md;
1041 md = malloc(sizeof(struct file_metadata) - sizeof(md->md_data) + size);
1045 bcopy(p, md->md_data, size);
1046 md->md_next = fp->f_metadata;
1048 fp->f_metadata = md;
1052 * Find a metadata object of (type) associated with the file (fp)
1054 struct file_metadata *
1055 file_findmetadata(struct preloaded_file *fp, int type)
1057 struct file_metadata *md;
1059 for (md = fp->f_metadata; md != NULL; md = md->md_next)
1060 if (md->md_type == type)
1066 * Remove all metadata from the file.
1069 file_removemetadata(struct preloaded_file *fp)
1071 struct file_metadata *md, *next;
1073 for (md = fp->f_metadata; md != NULL; md = next)
1078 fp->f_metadata = NULL;
1082 * Add a buffer to the list of preloaded "files".
1085 file_addbuf(const char *name, const char *type, size_t len, void *buf)
1087 struct preloaded_file *fp;
1090 /* We can't load first */
1091 if ((file_findfile(NULL, NULL)) == NULL) {
1092 command_errmsg = "can't load file before kernel";
1096 /* Figure out where to load the data. */
1098 if (archsw.arch_loadaddr != NULL)
1099 dest = archsw.arch_loadaddr(LOAD_RAW, (void *)name, dest);
1101 /* Create & populate control structure */
1104 snprintf(command_errbuf, sizeof (command_errbuf),
1105 "no memory to load %s", name);
1108 fp->f_name = strdup(name);
1109 fp->f_type = strdup(type);
1111 fp->f_metadata = NULL;
1115 if ((fp->f_name == NULL) || (fp->f_type == NULL)) {
1116 snprintf(command_errbuf, sizeof (command_errbuf),
1117 "no memory to load %s", name);
1123 /* Copy the data in. */
1124 archsw.arch_copyin(buf, fp->f_addr, len);
1125 loadaddr = fp->f_addr + len;
1127 /* Add to the list of loaded files */
1128 file_insert_tail(fp);
1132 struct file_metadata *
1133 metadata_next(struct file_metadata *md, int type)
1138 while((md = md->md_next) != NULL)
1139 if (md->md_type == type)
1144 static char *emptyextlist[] = { "", NULL };
1147 * Check if the given file is in place and return full path to it.
1150 file_lookup(const char *path, const char *name, int namelen, char **extlist)
1153 char *result, *cp, **cpp;
1154 int pathlen, extlen, len;
1156 pathlen = strlen(path);
1158 if (extlist == NULL)
1159 extlist = emptyextlist;
1160 for (cpp = extlist; *cpp; cpp++) {
1165 result = malloc(pathlen + namelen + extlen + 2);
1168 bcopy(path, result, pathlen);
1169 if (pathlen > 0 && result[pathlen - 1] != '/')
1170 result[pathlen++] = '/';
1171 cp = result + pathlen;
1172 bcopy(name, cp, namelen);
1174 for (cpp = extlist; *cpp; cpp++) {
1176 if (stat(result, &st) == 0 && S_ISREG(st.st_mode))
1184 * Check if file name have any qualifiers
1187 file_havepath(const char *name)
1191 archsw.arch_getdev(NULL, name, &cp);
1192 return (cp != name || strchr(name, '/') != NULL);
1196 * Attempt to find the file (name) on the module searchpath.
1197 * If (name) is qualified in any way, we simply check it and
1198 * return it or NULL. If it is not qualified, then we attempt
1199 * to construct a path using entries in the environment variable
1202 * The path we return a pointer to need never be freed, as we manage
1206 file_search(const char *name, char **extlist)
1208 struct moduledir *mdp;
1213 /* Don't look for nothing */
1218 return(strdup(name));
1220 if (file_havepath(name)) {
1221 /* Qualified, so just see if it exists */
1222 if (stat(name, &sb) == 0)
1223 return(strdup(name));
1226 moduledir_rebuild();
1228 namelen = strlen(name);
1229 STAILQ_FOREACH(mdp, &moduledir_list, d_link) {
1230 result = file_lookup(mdp->d_path, name, namelen, extlist);
1237 #define INT_ALIGN(base, ptr) ptr = \
1238 (base) + roundup2((ptr) - (base), sizeof(int))
1241 mod_search_hints(struct moduledir *mdp, const char *modname,
1242 struct mod_depend *verinfo)
1244 u_char *cp, *recptr, *bufend, *best;
1246 int *intp, bestver, blen, clen, found, ival, modnamelen, reclen;
1248 moduledir_readhints(mdp);
1249 modnamelen = strlen(modname);
1253 if (mdp->d_hints == NULL)
1255 recptr = mdp->d_hints;
1256 bufend = recptr + mdp->d_hintsz;
1259 while (recptr < bufend && !found) {
1260 intp = (int*)recptr;
1267 if (clen != modnamelen || bcmp(cp, modname, clen) != 0)
1270 INT_ALIGN(mdp->d_hints, cp);
1274 if (verinfo == NULL || ival == verinfo->md_ver_preferred) {
1278 if (ival >= verinfo->md_ver_minimum &&
1279 ival <= verinfo->md_ver_maximum &&
1289 recptr += reclen + sizeof(int);
1292 * Finally check if KLD is in the place
1295 result = file_lookup(mdp->d_path, (const char *)cp, clen, NULL);
1297 result = file_lookup(mdp->d_path, (const char *)best, blen, NULL);
1300 * If nothing found or hints is absent - fallback to the old way
1301 * by using "kldname[.ko]" as module name.
1303 if (!found && !bestver && result == NULL)
1304 result = file_lookup(mdp->d_path, modname, modnamelen, kld_ext_list);
1314 p = (int *)roundup2((intptr_t)p, sizeof(int));
1321 getstr(void **ptr, char *val)
1324 char *c = (char *)p;
1325 int len = *(uint8_t *)c;
1327 memcpy(val, c + 1, len);
1334 pnpval_as_int(const char *val, const char *pnpinfo)
1340 if (pnpinfo == NULL)
1343 cp = strchr(val, ';');
1346 strlcpy(key + 1, val, sizeof(key) - 1);
1348 memcpy(key + 1, val, cp - val);
1349 key[cp - val + 1] = '\0';
1351 strlcat(key, "=", sizeof(key));
1352 if (strncmp(key + 1, pnpinfo, strlen(key + 1)) == 0)
1353 rv = strtol(pnpinfo + strlen(key + 1), NULL, 0);
1355 cp = strstr(pnpinfo, key);
1359 rv = strtol(cp + strlen(key), NULL, 0);
1365 quoted_strcpy(char *dst, const char *src)
1369 if (*src == '\'' || *src == '"')
1371 while (*src && *src != q)
1372 *dst++ = *src++; // XXX backtick quoting
1378 pnpval_as_str(const char *val, const char *pnpinfo)
1380 static char retval[256];
1384 if (pnpinfo == NULL) {
1389 cp = strchr(val, ';');
1392 strlcpy(key + 1, val, sizeof(key) - 1);
1394 memcpy(key + 1, val, cp - val);
1395 key[cp - val + 1] = '\0';
1397 strlcat(key, "=", sizeof(key));
1398 if (strncmp(key + 1, pnpinfo, strlen(key + 1)) == 0)
1399 quoted_strcpy(retval, pnpinfo + strlen(key + 1));
1401 cp = strstr(pnpinfo, key);
1403 strcpy(retval, "MISSING");
1405 quoted_strcpy(retval, cp + strlen(key));
1411 devmatch_search_hints(struct moduledir *mdp, const char *bus, const char *dev, const char *pnpinfo)
1413 char val1[256], val2[256];
1414 int ival, len, ents, i, notme, mask, bit, v, found;
1415 void *ptr, *walker, *hints_end;
1416 char *lastmod = NULL, *cp, *s;
1418 moduledir_readhints(mdp);
1420 if (mdp->d_hints == NULL)
1422 walker = mdp->d_hints;
1423 hints_end = walker + mdp->d_hintsz;
1424 while (walker < hints_end && !found) {
1425 len = getint(&walker);
1426 ival = getint(&walker);
1431 ival = getint(&ptr);
1433 if (pnp_dump_flag || pnp_verbose_flag)
1434 printf("Version: if %s.%d kmod %s\n", val1, ival, val2);
1441 lastmod = strdup(val2);
1442 if (pnp_dump_flag || pnp_verbose_flag)
1443 printf("module %s in %s\n", val1, val1);
1446 if (!pnp_dump_flag && !pnp_unbound_flag && lastmod && strcmp(lastmod, "kernel") == 0)
1450 ents = getint(&ptr);
1451 if (pnp_dump_flag || pnp_verbose_flag)
1452 printf("PNP info for bus %s format %s %d entries (%s)\n",
1453 val1, val2, ents, lastmod);
1454 if (strcmp(val1, "usb") == 0) {
1455 if (pnp_verbose_flag)
1456 printf("Treating usb as uhub -- bug in source table still?\n");
1457 strcpy(val1, "uhub");
1459 if (bus && strcmp(val1, bus) != 0) {
1460 if (pnp_verbose_flag)
1461 printf("Skipped because table for bus %s, looking for %s\n",
1465 for (i = 0; i < ents; i++) {
1466 if (pnp_verbose_flag)
1467 printf("---------- Entry %d ----------\n", i);
1476 /* All integer fields */
1482 ival = getint(&ptr);
1483 if (pnp_dump_flag) {
1484 printf("%#x:", ival);
1487 if (bit >= 0 && ((1 << bit) & mask) == 0)
1489 v = pnpval_as_int(cp + 2, pnpinfo);
1490 if (pnp_verbose_flag)
1491 printf("Matching %s (%c) table=%#x tomatch=%#x\n",
1492 cp + 2, *cp, v, ival);
1519 if (pnp_dump_flag) {
1520 printf("'%s':", val1);
1525 s = pnpval_as_str(cp + 2, pnpinfo);
1526 if (strcmp(s, val1) != 0)
1529 /* Key override fields, required to be last in the string */
1532 * This is imperfect and only does one key and will be redone
1533 * to be more general for multiple keys. Currently, nothing
1536 if (pnp_dump_flag) /* No per-row data stored */
1538 if (cp[strlen(cp) - 1] == ';') /* Skip required ; at end */
1539 cp[strlen(cp) - 1] = '\0'; /* in case it's not there */
1540 if ((s = strstr(pnpinfo, cp + 2)) == NULL)
1542 else if (s > pnpinfo && s[-1] != ' ')
1546 printf("Unknown field type %c\n:", *cp);
1550 cp = strchr(cp, ';');
1553 } while (cp && *cp);
1557 if (!pnp_unbound_flag) {
1558 if (pnp_verbose_flag)
1559 printf("Matches --- %s ---\n", lastmod);
1568 walker = (void *)(len - sizeof(int) + (intptr_t)walker);
1570 if (pnp_unbound_flag && found == 0 && *pnpinfo) {
1571 if (pnp_verbose_flag)
1572 printf("------------------------- ");
1573 printf("%s on %s pnpinfo %s", *dev ? dev : "unattached", bus, pnpinfo);
1574 if (pnp_verbose_flag)
1575 printf(" -------------------------");
1587 * Attempt to locate the file containing the module (name)
1590 mod_searchmodule(char *name, struct mod_depend *verinfo)
1592 struct moduledir *mdp;
1595 moduledir_rebuild();
1597 * Now we ready to lookup module in the given directories
1600 STAILQ_FOREACH(mdp, &moduledir_list, d_link) {
1601 result = mod_search_hints(mdp, name, verinfo);
1610 mod_searchmodule_pnpinfo(const char *bus, const char *pnpinfo)
1612 struct moduledir *mdp;
1615 moduledir_rebuild();
1617 * Now we ready to lookup module in the given directories
1620 STAILQ_FOREACH(mdp, &moduledir_list, d_link) {
1621 result = devmatch_search_hints(mdp, bus, NULL, pnpinfo);
1630 file_addmodule(struct preloaded_file *fp, char *modname, int version,
1631 struct kernel_module **newmp)
1633 struct kernel_module *mp;
1634 struct mod_depend mdepend;
1636 bzero(&mdepend, sizeof(mdepend));
1637 mdepend.md_ver_preferred = version;
1638 mp = file_findmodule(fp, modname, &mdepend);
1641 mp = calloc(1, sizeof(struct kernel_module));
1644 mp->m_name = strdup(modname);
1645 if (mp->m_name == NULL) {
1649 mp->m_version = version;
1651 mp->m_next = fp->f_modules;
1662 file_discard(struct preloaded_file *fp)
1664 struct file_metadata *md, *md1;
1665 struct kernel_module *mp, *mp1;
1668 md = fp->f_metadata;
1688 * Allocate a new file; must be used instead of malloc()
1689 * to ensure safe initialisation.
1691 struct preloaded_file *
1695 return (calloc(1, sizeof(struct preloaded_file)));
1699 * Add a module to the chain
1702 file_insert_tail(struct preloaded_file *fp)
1704 struct preloaded_file *cm;
1706 /* Append to list of loaded file */
1708 if (preloaded_files == NULL) {
1709 preloaded_files = fp;
1711 for (cm = preloaded_files; cm->f_next != NULL; cm = cm->f_next)
1718 * Remove module from the chain
1721 file_remove(struct preloaded_file *fp)
1723 struct preloaded_file *cm;
1725 if (preloaded_files == NULL)
1728 if (preloaded_files == fp) {
1729 preloaded_files = fp->f_next;
1732 for (cm = preloaded_files; cm->f_next != NULL; cm = cm->f_next) {
1733 if (cm->f_next == fp) {
1734 cm->f_next = fp->f_next;
1741 moduledir_fullpath(struct moduledir *mdp, const char *fname)
1745 cp = malloc(strlen(mdp->d_path) + strlen(fname) + 2);
1748 strcpy(cp, mdp->d_path);
1755 * Read linker.hints file into memory performing some sanity checks.
1758 moduledir_readhints(struct moduledir *mdp)
1762 int fd, size, version;
1764 if (mdp->d_hints != NULL || (mdp->d_flags & MDIR_NOHINTS))
1766 path = moduledir_fullpath(mdp, "linker.hints");
1767 if (stat(path, &st) != 0 ||
1768 st.st_size < (ssize_t)(sizeof(version) + sizeof(int)) ||
1769 st.st_size > LINKER_HINTS_MAX || (fd = open(path, O_RDONLY)) < 0) {
1771 mdp->d_flags |= MDIR_NOHINTS;
1775 size = read(fd, &version, sizeof(version));
1776 if (size != sizeof(version) || version != LINKER_HINTS_VERSION)
1778 size = st.st_size - size;
1779 mdp->d_hints = malloc(size);
1780 if (mdp->d_hints == NULL)
1782 if (read(fd, mdp->d_hints, size) != size)
1784 mdp->d_hintsz = size;
1790 mdp->d_hints = NULL;
1791 mdp->d_flags |= MDIR_NOHINTS;
1796 * Extract directories from the ';' separated list, remove duplicates.
1799 moduledir_rebuild(void)
1801 struct moduledir *mdp, *mtmp;
1802 const char *path, *cp, *ep;
1805 path = getenv("module_path");
1807 path = default_searchpath;
1809 * Rebuild list of module directories if it changed
1811 STAILQ_FOREACH(mdp, &moduledir_list, d_link)
1812 mdp->d_flags |= MDIR_REMOVED;
1814 for (ep = path; *ep != 0; ep++) {
1816 for (; *ep != 0 && *ep != ';'; ep++)
1819 * Ignore trailing slashes
1821 for (cplen = ep - cp; cplen > 1 && cp[cplen - 1] == '/'; cplen--)
1823 STAILQ_FOREACH(mdp, &moduledir_list, d_link) {
1824 if (strlen(mdp->d_path) != cplen || bcmp(cp, mdp->d_path, cplen) != 0)
1826 mdp->d_flags &= ~MDIR_REMOVED;
1830 mdp = malloc(sizeof(*mdp) + cplen + 1);
1833 mdp->d_path = (char*)(mdp + 1);
1834 bcopy(cp, mdp->d_path, cplen);
1835 mdp->d_path[cplen] = 0;
1836 mdp->d_hints = NULL;
1838 STAILQ_INSERT_TAIL(&moduledir_list, mdp, d_link);
1844 * Delete unused directories if any
1846 mdp = STAILQ_FIRST(&moduledir_list);
1848 if ((mdp->d_flags & MDIR_REMOVED) == 0) {
1849 mdp = STAILQ_NEXT(mdp, d_link);
1853 mdp = STAILQ_NEXT(mdp, d_link);
1854 STAILQ_REMOVE(&moduledir_list, mtmp, moduledir, d_link);