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>
29 * file/module function dispatcher, support, etc.
34 #include <sys/param.h>
35 #include <sys/linker.h>
36 #include <sys/module.h>
37 #include <sys/queue.h>
38 #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[] = {
95 * load an object, either a disk file or code module.
97 * To load a file, the syntax is:
99 * load -t <type> <path>
101 * code modules are loaded as:
103 * load <path> <options>
106 COMMAND_SET(load, "load", "load a kernel or module", command_load);
109 command_load(int argc, char *argv[])
111 struct preloaded_file *fp;
113 #ifdef LOADER_VERIEXEC
116 char *args = "dkp:s:t:";
120 int dofile, dokld, ch, error;
127 command_errmsg = "no filename specified";
130 #ifdef LOADER_VERIEXEC
134 while ((ch = getopt(argc, argv, args)) != -1) {
136 #ifdef LOADER_VERIEXEC
144 #ifdef LOADER_VERIEXEC
158 /* getopt has already reported an error */
162 argv += (optind - 1);
163 argc -= (optind - 1);
166 * Request to load a raw file?
169 if ((argc != 2) || (typestr == NULL) || (*typestr == 0)) {
170 command_errmsg = "invalid load type";
174 #ifdef LOADER_VERIEXEC
175 if (strncmp(typestr, "manifest", 8) == 0) {
178 return (load_manifest(argv[1], prefix, skip, NULL));
180 #ifdef LOADER_VERIEXEC_PASS_MANIFEST
181 if (strncmp(typestr, "pass_manifest", 13) == 0) {
184 return (pass_manifest(argv[1], prefix));
189 fp = file_findfile(argv[1], typestr);
191 snprintf(command_errbuf, sizeof(command_errbuf),
192 "warning: file '%s' already loaded", argv[1]);
196 if (file_loadraw(argv[1], typestr, 1) != NULL)
199 /* Failing to load mfs_root is never going to end well! */
200 if (strcmp("mfs_root", typestr) == 0)
206 * Do we have explicit KLD load ?
208 if (dokld || file_havepath(argv[1])) {
209 error = mod_loadkld(argv[1], argc - 2, argv + 2);
210 if (error == EEXIST) {
211 snprintf(command_errbuf, sizeof(command_errbuf),
212 "warning: KLD '%s' already loaded", argv[1]);
216 return (error == 0 ? CMD_OK : CMD_CRIT);
219 * Looks like a request for a module.
221 error = mod_load(argv[1], NULL, argc - 2, argv + 2);
222 if (error == EEXIST) {
223 snprintf(command_errbuf, sizeof(command_errbuf),
224 "warning: module '%s' already loaded", argv[1]);
228 return (error == 0 ? CMD_OK : CMD_CRIT);
231 #ifdef LOADER_GELI_SUPPORT
232 COMMAND_SET(load_geli, "load_geli", "load a geli key", command_load_geli);
235 command_load_geli(int argc, char *argv[])
242 command_errmsg = "usage is [-n key#] <prov> <file>";
249 while ((ch = getopt(argc, argv, "n:")) != -1) {
252 num = strtol(optarg, &cp, 0);
254 snprintf(command_errbuf, sizeof(command_errbuf),
255 "bad key index '%s'", optarg);
261 /* getopt has already reported an error */
265 argv += (optind - 1);
266 argc -= (optind - 1);
267 sprintf(typestr, "%s:geli_keyfile%d", argv[1], num);
268 return (file_loadraw(argv[2], typestr, 1) ? CMD_OK : CMD_ERROR);
275 struct preloaded_file *fp;
277 while (preloaded_files != NULL) {
278 fp = preloaded_files;
279 preloaded_files = preloaded_files->f_next;
283 unsetenv("kernelname");
286 COMMAND_SET(unload, "unload", "unload all modules", command_unload);
289 command_unload(int argc, char *argv[])
295 COMMAND_SET(lsmod, "lsmod", "list loaded modules", command_lsmod);
298 command_lsmod(int argc, char *argv[])
300 struct preloaded_file *fp;
301 struct kernel_module *mp;
302 struct file_metadata *md;
304 int ch, verbose, ret = 0;
309 while ((ch = getopt(argc, argv, "v")) != -1) {
316 /* getopt has already reported an error */
322 for (fp = preloaded_files; fp; fp = fp->f_next) {
323 snprintf(lbuf, sizeof(lbuf), " %p: ", (void *) fp->f_addr);
325 pager_output(fp->f_name);
326 snprintf(lbuf, sizeof(lbuf), " (%s, 0x%lx)\n", fp->f_type,
328 if (pager_output(lbuf))
330 if (fp->f_args != NULL) {
331 pager_output(" args: ");
332 pager_output(fp->f_args);
333 if (pager_output("\n"))
337 pager_output(" modules: ");
338 for (mp = fp->f_modules; mp; mp = mp->m_next) {
339 snprintf(lbuf, sizeof(lbuf), "%s.%d ", mp->m_name,
343 if (pager_output("\n"))
347 /* XXX could add some formatting smarts here to display some better */
348 for (md = fp->f_metadata; md != NULL; md = md->md_next) {
349 snprintf(lbuf, sizeof(lbuf), " 0x%04x, 0x%lx\n",
350 md->md_type, (long) md->md_size);
351 if (pager_output(lbuf))
362 COMMAND_SET(pnpmatch, "pnpmatch", "list matched modules based on pnpinfo", command_pnpmatch);
364 static int pnp_dump_flag = 0;
365 static int pnp_unbound_flag = 0;
366 static int pnp_verbose_flag = 0;
369 command_pnpmatch(int argc, char *argv[])
374 pnp_verbose_flag = 0;
378 while ((ch = getopt(argc, argv, "vd")) != -1) {
381 pnp_verbose_flag = 1;
388 /* getopt has already reported an error */
396 command_errmsg = "Usage: pnpmatch <busname> compat=<compatdata>";
400 module = mod_searchmodule_pnpinfo(argv[0], argv[1]);
402 printf("Matched module: %s\n", module);
404 printf("No module matches %s on bus %s\n", argv[1], argv[0]);
409 COMMAND_SET(pnpload, "pnpload", "load matched modules based on pnpinfo", command_pnpload);
412 command_pnpload(int argc, char *argv[])
417 pnp_verbose_flag = 0;
421 while ((ch = getopt(argc, argv, "vd")) != -1) {
424 pnp_verbose_flag = 1;
431 /* getopt has already reported an error */
439 command_errmsg = "Usage: pnpload <busname> compat=<compatdata>";
443 module = mod_searchmodule_pnpinfo(argv[0], argv[1]);
445 error = mod_load(module, NULL, 0, NULL);
446 if (error == EEXIST) {
447 snprintf(command_errbuf, sizeof(command_errbuf),
448 "warning: module '%s' already loaded", argv[1]);
452 return (error == 0 ? CMD_OK : CMD_CRIT);
455 #if defined(LOADER_FDT_SUPPORT)
457 pnpautoload_fdt_bus(const char *busname) {
458 const char *pnpstring;
459 const char *compatstr;
460 char *pnpinfo = NULL;
462 int tag = 0, len, pnplen;
466 pnpstring = fdt_devmatch_next(&tag, &len);
467 if (pnpstring == NULL)
470 compatstr = pnpstring;
471 for (pnplen = 0; pnplen != len; compatstr = pnpstring + pnplen) {
472 pnplen += strlen(compatstr) + 1;
473 asprintf(&pnpinfo, "compat=%s", compatstr);
475 module = mod_searchmodule_pnpinfo(busname, pnpinfo);
477 error = mod_loadkld(module, 0, NULL);
479 printf("Cannot load module %s\n", module);
491 void (*load)(const char *busname);
494 struct pnp_bus pnp_buses[] = {
495 #if defined(LOADER_FDT_SUPPORT)
496 {"simplebus", pnpautoload_fdt_bus},
497 {"ofwbus", pnpautoload_fdt_bus},
498 {"iicbus", pnpautoload_fdt_bus},
499 {"spibus", pnpautoload_fdt_bus},
503 COMMAND_SET(pnpautoload, "pnpautoload", "auto load modules based on pnpinfo", command_pnpautoload);
506 command_pnpautoload(int argc, char *argv[])
512 pnp_verbose_flag = 0;
518 while ((ch = getopt(argc, argv, "v")) != -1) {
525 /* getopt has already reported an error */
529 argv += (optind - 1);
530 argc -= (optind - 1);
535 for (i = 0; i < nitems(pnp_buses); i++) {
536 if (argc == 2 && strcmp(argv[1], pnp_buses[i].name) != 0) {
538 printf("Skipping bus %s\n", pnp_buses[i].name);
542 printf("Autoloading modules for %s\n", pnp_buses[i].name);
543 pnp_buses[i].load(pnp_buses[i].name);
547 printf("Unsupported bus %s\n", argv[1]);
553 * File level interface, functions file_*
556 file_load(char *filename, vm_offset_t dest, struct preloaded_file **result)
558 static int last_file_format = 0;
559 struct preloaded_file *fp;
564 if (archsw.arch_loadaddr != NULL)
565 dest = archsw.arch_loadaddr(LOAD_RAW, filename, dest);
568 for (i = last_file_format, fp = NULL;
569 file_formats[i] && fp == NULL; i++) {
570 error = (file_formats[i]->l_load)(filename, dest, &fp);
572 fp->f_loader = last_file_format = i; /* remember the loader */
575 } else if (last_file_format == i && i != 0) {
576 /* Restart from the beginning */
578 last_file_format = 0;
583 continue; /* Unknown to this handler? */
585 snprintf(command_errbuf, sizeof(command_errbuf),
586 "can't load file '%s': %s", filename, strerror(error));
595 file_load_dependencies(struct preloaded_file *base_file)
597 struct file_metadata *md;
598 struct preloaded_file *fp;
599 struct mod_depend *verinfo;
600 struct kernel_module *mp;
604 md = file_findmetadata(base_file, MODINFOMD_DEPLIST);
609 verinfo = (struct mod_depend*)md->md_data;
610 dmodname = (char *)(verinfo + 1);
611 if (file_findmodule(NULL, dmodname, verinfo) == NULL) {
612 if (module_verbose > MODULE_VERBOSE_SILENT)
613 printf("loading required module '%s'\n", dmodname);
614 error = mod_load(dmodname, verinfo, 0, NULL);
618 * If module loaded via kld name which isn't listed
619 * in the linker.hints file, we should check if it have
622 mp = file_findmodule(NULL, dmodname, verinfo);
624 snprintf(command_errbuf, sizeof(command_errbuf),
625 "module '%s' exists but with wrong version", dmodname);
630 md = metadata_next(md, MODINFOMD_DEPLIST);
634 /* Load failed; discard everything */
635 while (base_file != NULL) {
637 base_file = base_file->f_next;
644 build_font_module(vm_offset_t addr)
646 vt_font_bitmap_data_t *bd;
648 struct preloaded_file *fp;
656 if (STAILQ_EMPTY(&fonts))
659 /* We can't load first */
660 if ((file_findfile(NULL, NULL)) == NULL) {
661 printf("Can not load font module: %s\n",
662 "the kernel is not loaded");
666 /* helper pointers */
668 STAILQ_FOREACH(fl, &fonts, font_next) {
669 if (gfx_state.tg_font.vf_width == fl->font_data->vfbd_width &&
670 gfx_state.tg_font.vf_height == fl->font_data->vfbd_height) {
672 * Kernel does have better built in font.
674 if (fl->font_flags == FONT_BUILTIN)
685 fi.fi_width = fd->vf_width;
686 checksum = fi.fi_width;
687 fi.fi_height = fd->vf_height;
688 checksum += fi.fi_height;
689 fi.fi_bitmap_size = bd->vfbd_uncompressed_size;
690 checksum += fi.fi_bitmap_size;
692 size = roundup2(sizeof (struct font_info), 8);
693 for (i = 0; i < VFNT_MAPS; i++) {
694 fi.fi_map_count[i] = fd->vf_map_count[i];
695 checksum += fi.fi_map_count[i];
696 size += fd->vf_map_count[i] * sizeof (struct vfnt_map);
697 size += roundup2(size, 8);
699 size += bd->vfbd_uncompressed_size;
701 fi.fi_checksum = -checksum;
703 fp = file_findfile(NULL, "elf kernel");
705 fp = file_findfile(NULL, "elf64 kernel");
707 panic("can't find kernel file");
710 addr += archsw.arch_copyin(&fi, addr, sizeof (struct font_info));
711 addr = roundup2(addr, 8);
714 for (i = 0; i < VFNT_MAPS; i++) {
715 if (fd->vf_map_count[i] != 0) {
716 addr += archsw.arch_copyin(fd->vf_map[i], addr,
717 fd->vf_map_count[i] * sizeof (struct vfnt_map));
718 addr = roundup2(addr, 8);
722 /* Copy the bitmap. */
723 addr += archsw.arch_copyin(fd->vf_bytes, addr, fi.fi_bitmap_size);
725 /* Looks OK so far; populate control structure */
726 file_addmetadata(fp, MODINFOMD_FONT, sizeof(fontp), &fontp);
730 #ifdef LOADER_VERIEXEC_VECTX
731 #define VECTX_HANDLE(fd) vctx
733 #define VECTX_HANDLE(fd) fd
738 * We've been asked to load (fname) as (type), so just suck it in,
739 * no arguments or anything.
741 struct preloaded_file *
742 file_loadraw(const char *fname, char *type, int insert)
744 struct preloaded_file *fp;
748 #ifdef LOADER_VERIEXEC_VECTX
754 /* We can't load first */
755 if ((file_findfile(NULL, NULL)) == NULL) {
756 command_errmsg = "can't load file before kernel";
761 /* locate the file on the load path */
762 name = file_search(fname, NULL);
764 snprintf(command_errbuf, sizeof(command_errbuf),
765 "can't find '%s'", fname);
770 if ((fd = open(name, O_RDONLY)) < 0) {
771 snprintf(command_errbuf, sizeof(command_errbuf),
772 "can't open '%s': %s", name, strerror(errno));
778 #ifdef LOADER_VERIEXEC_VECTX
779 vctx = vectx_open(fd, name, 0L, NULL, &verror, __func__);
781 sprintf(command_errbuf, "can't verify '%s': %s",
782 name, ve_error_get());
790 #ifdef LOADER_VERIEXEC
791 if (verify_file(fd, name, 0, VE_MUST, __func__) < 0) {
792 sprintf(command_errbuf, "can't verify '%s': %s",
793 name, ve_error_get());
802 if (archsw.arch_loadaddr != NULL)
803 loadaddr = archsw.arch_loadaddr(LOAD_RAW, name, loadaddr);
805 if (module_verbose > MODULE_VERBOSE_SILENT)
810 /* read in 4k chunks; size is not really important */
811 got = archsw.arch_readin(VECTX_HANDLE(fd), laddr, 4096);
812 if (got == 0) /* end of file */
814 if (got < 0) { /* error */
815 snprintf(command_errbuf, sizeof(command_errbuf),
816 "error reading '%s': %s", name, strerror(errno));
819 #ifdef LOADER_VERIEXEC_VECTX
828 if (module_verbose > MODULE_VERBOSE_SILENT)
829 printf("size=%#jx\n", (uintmax_t)(laddr - loadaddr));
830 #ifdef LOADER_VERIEXEC_VECTX
831 verror = vectx_close(vctx, VE_MUST, __func__);
841 /* Looks OK so far; create & populate control structure */
844 snprintf(command_errbuf, sizeof (command_errbuf),
845 "no memory to load %s", name);
852 fp->f_type = strdup(type);
854 fp->f_metadata = NULL;
856 fp->f_addr = loadaddr;
857 fp->f_size = laddr - loadaddr;
859 if (fp->f_type == NULL) {
860 snprintf(command_errbuf, sizeof (command_errbuf),
861 "no memory to load %s", name);
867 /* recognise space consumption */
870 /* Add to the list of loaded files */
872 file_insert_tail(fp);
879 * Load the module (name), pass it (argc),(argv), add container file
880 * to the list of loaded files.
881 * If module is already loaded just assign new argc/argv.
884 mod_load(char *modname, struct mod_depend *verinfo, int argc, char *argv[])
886 struct kernel_module *mp;
891 if (file_havepath(modname)) {
892 printf("Warning: mod_load() called instead of mod_loadkld() for module '%s'\n", modname);
894 return (mod_loadkld(modname, argc, argv));
896 /* see if module is already loaded */
897 mp = file_findmodule(NULL, modname, verinfo);
901 mp->m_args = unargv(argc, argv);
903 snprintf(command_errbuf, sizeof(command_errbuf),
904 "warning: module '%s' already loaded", mp->m_name);
908 /* locate file with the module on the search path */
909 filename = mod_searchmodule(modname, verinfo);
910 if (filename == NULL) {
911 snprintf(command_errbuf, sizeof(command_errbuf),
912 "can't find '%s'", modname);
916 err = mod_loadkld(filename, argc, argv);
923 * Load specified KLD. If path is omitted, then try to locate it via
927 mod_loadkld(const char *kldname, int argc, char *argv[])
929 struct preloaded_file *fp;
932 vm_offset_t loadaddr_saved;
936 * Get fully qualified KLD name
938 filename = file_search(kldname, kld_ext_list);
939 if (filename == NULL) {
940 snprintf(command_errbuf, sizeof(command_errbuf),
941 "can't find '%s'", kldname);
946 * Check if KLD already loaded
948 fp = file_findfile(filename, NULL);
950 snprintf(command_errbuf, sizeof(command_errbuf),
951 "warning: KLD '%s' already loaded", filename);
958 err = file_load(filename, loadaddr, &fp);
961 fp->f_args = unargv(argc, argv);
962 loadaddr_saved = loadaddr;
963 loadaddr = fp->f_addr + fp->f_size;
964 file_insert_tail(fp); /* Add to the list of loaded files */
965 if (file_load_dependencies(fp) != 0) {
968 loadaddr = loadaddr_saved;
974 snprintf(command_errbuf, sizeof(command_errbuf),
975 "don't know how to load module '%s'", filename);
985 * Find a file matching (name) and (type).
986 * NULL may be passed as a wildcard to either.
988 struct preloaded_file *
989 file_findfile(const char *name, const char *type)
991 struct preloaded_file *fp;
993 for (fp = preloaded_files; fp != NULL; fp = fp->f_next) {
994 if (((name == NULL) || !strcmp(name, fp->f_name)) &&
995 ((type == NULL) || !strcmp(type, fp->f_type)))
1002 * Find a module matching (name) inside of given file.
1003 * NULL may be passed as a wildcard.
1005 struct kernel_module *
1006 file_findmodule(struct preloaded_file *fp, char *modname,
1007 struct mod_depend *verinfo)
1009 struct kernel_module *mp, *best;
1013 for (fp = preloaded_files; fp; fp = fp->f_next) {
1014 mp = file_findmodule(fp, modname, verinfo);
1022 for (mp = fp->f_modules; mp; mp = mp->m_next) {
1023 if (strcmp(modname, mp->m_name) == 0) {
1024 if (verinfo == NULL)
1026 mver = mp->m_version;
1027 if (mver == verinfo->md_ver_preferred)
1029 if (mver >= verinfo->md_ver_minimum &&
1030 mver <= verinfo->md_ver_maximum &&
1040 * Make a copy of (size) bytes of data from (p), and associate them as
1041 * metadata of (type) to the module (mp).
1044 file_addmetadata(struct preloaded_file *fp, int type, size_t size, void *p)
1046 struct file_metadata *md;
1048 md = malloc(sizeof(struct file_metadata) - sizeof(md->md_data) + size);
1052 bcopy(p, md->md_data, size);
1053 md->md_next = fp->f_metadata;
1055 fp->f_metadata = md;
1059 * Find a metadata object of (type) associated with the file (fp)
1061 struct file_metadata *
1062 file_findmetadata(struct preloaded_file *fp, int type)
1064 struct file_metadata *md;
1066 for (md = fp->f_metadata; md != NULL; md = md->md_next)
1067 if (md->md_type == type)
1073 * Remove all metadata from the file.
1076 file_removemetadata(struct preloaded_file *fp)
1078 struct file_metadata *md, *next;
1080 for (md = fp->f_metadata; md != NULL; md = next)
1085 fp->f_metadata = NULL;
1089 * Add a buffer to the list of preloaded "files".
1092 file_addbuf(const char *name, const char *type, size_t len, void *buf)
1094 struct preloaded_file *fp;
1097 /* We can't load first */
1098 if ((file_findfile(NULL, NULL)) == NULL) {
1099 command_errmsg = "can't load file before kernel";
1103 /* Figure out where to load the data. */
1105 if (archsw.arch_loadaddr != NULL)
1106 dest = archsw.arch_loadaddr(LOAD_RAW, (void *)name, dest);
1108 /* Create & populate control structure */
1111 snprintf(command_errbuf, sizeof (command_errbuf),
1112 "no memory to load %s", name);
1115 fp->f_name = strdup(name);
1116 fp->f_type = strdup(type);
1118 fp->f_metadata = NULL;
1122 if ((fp->f_name == NULL) || (fp->f_type == NULL)) {
1123 snprintf(command_errbuf, sizeof (command_errbuf),
1124 "no memory to load %s", name);
1130 /* Copy the data in. */
1131 archsw.arch_copyin(buf, fp->f_addr, len);
1132 loadaddr = fp->f_addr + len;
1134 /* Add to the list of loaded files */
1135 file_insert_tail(fp);
1139 struct file_metadata *
1140 metadata_next(struct file_metadata *md, int type)
1145 while((md = md->md_next) != NULL)
1146 if (md->md_type == type)
1151 static char *emptyextlist[] = { "", NULL };
1154 * Check if the given file is in place and return full path to it.
1157 file_lookup(const char *path, const char *name, int namelen, char **extlist)
1160 char *result, *cp, **cpp;
1161 int pathlen, extlen, len;
1163 pathlen = strlen(path);
1165 if (extlist == NULL)
1166 extlist = emptyextlist;
1167 for (cpp = extlist; *cpp; cpp++) {
1172 result = malloc(pathlen + namelen + extlen + 2);
1175 bcopy(path, result, pathlen);
1176 if (pathlen > 0 && result[pathlen - 1] != '/')
1177 result[pathlen++] = '/';
1178 cp = result + pathlen;
1179 bcopy(name, cp, namelen);
1181 for (cpp = extlist; *cpp; cpp++) {
1183 if (stat(result, &st) == 0 && S_ISREG(st.st_mode))
1191 * Check if file name have any qualifiers
1194 file_havepath(const char *name)
1198 archsw.arch_getdev(NULL, name, &cp);
1199 return (cp != name || strchr(name, '/') != NULL);
1203 * Attempt to find the file (name) on the module searchpath.
1204 * If (name) is qualified in any way, we simply check it and
1205 * return it or NULL. If it is not qualified, then we attempt
1206 * to construct a path using entries in the environment variable
1209 * The path we return a pointer to need never be freed, as we manage
1213 file_search(const char *name, char **extlist)
1215 struct moduledir *mdp;
1220 /* Don't look for nothing */
1225 return(strdup(name));
1227 if (file_havepath(name)) {
1228 /* Qualified, so just see if it exists */
1229 if (stat(name, &sb) == 0)
1230 return(strdup(name));
1233 moduledir_rebuild();
1235 namelen = strlen(name);
1236 STAILQ_FOREACH(mdp, &moduledir_list, d_link) {
1237 result = file_lookup(mdp->d_path, name, namelen, extlist);
1244 #define INT_ALIGN(base, ptr) ptr = \
1245 (base) + roundup2((ptr) - (base), sizeof(int))
1248 mod_search_hints(struct moduledir *mdp, const char *modname,
1249 struct mod_depend *verinfo)
1251 u_char *cp, *recptr, *bufend, *best;
1253 int *intp, bestver, blen, clen, found, ival, modnamelen, reclen;
1255 moduledir_readhints(mdp);
1256 modnamelen = strlen(modname);
1260 if (mdp->d_hints == NULL)
1262 recptr = mdp->d_hints;
1263 bufend = recptr + mdp->d_hintsz;
1266 while (recptr < bufend && !found) {
1267 intp = (int*)recptr;
1274 if (clen != modnamelen || bcmp(cp, modname, clen) != 0)
1277 INT_ALIGN(mdp->d_hints, cp);
1281 if (verinfo == NULL || ival == verinfo->md_ver_preferred) {
1285 if (ival >= verinfo->md_ver_minimum &&
1286 ival <= verinfo->md_ver_maximum &&
1296 recptr += reclen + sizeof(int);
1299 * Finally check if KLD is in the place
1302 result = file_lookup(mdp->d_path, (const char *)cp, clen, NULL);
1304 result = file_lookup(mdp->d_path, (const char *)best, blen, NULL);
1307 * If nothing found or hints is absent - fallback to the old way
1308 * by using "kldname[.ko]" as module name.
1310 if (!found && !bestver && result == NULL)
1311 result = file_lookup(mdp->d_path, modname, modnamelen, kld_ext_list);
1321 p = (int *)roundup2((intptr_t)p, sizeof(int));
1328 getstr(void **ptr, char *val)
1331 char *c = (char *)p;
1332 int len = *(uint8_t *)c;
1334 memcpy(val, c + 1, len);
1341 pnpval_as_int(const char *val, const char *pnpinfo)
1347 if (pnpinfo == NULL)
1350 cp = strchr(val, ';');
1353 strlcpy(key + 1, val, sizeof(key) - 1);
1355 memcpy(key + 1, val, cp - val);
1356 key[cp - val + 1] = '\0';
1358 strlcat(key, "=", sizeof(key));
1359 if (strncmp(key + 1, pnpinfo, strlen(key + 1)) == 0)
1360 rv = strtol(pnpinfo + strlen(key + 1), NULL, 0);
1362 cp = strstr(pnpinfo, key);
1366 rv = strtol(cp + strlen(key), NULL, 0);
1372 quoted_strcpy(char *dst, const char *src)
1376 if (*src == '\'' || *src == '"')
1378 while (*src && *src != q)
1379 *dst++ = *src++; // XXX backtick quoting
1385 pnpval_as_str(const char *val, const char *pnpinfo)
1387 static char retval[256];
1391 if (pnpinfo == NULL) {
1396 cp = strchr(val, ';');
1399 strlcpy(key + 1, val, sizeof(key) - 1);
1401 memcpy(key + 1, val, cp - val);
1402 key[cp - val + 1] = '\0';
1404 strlcat(key, "=", sizeof(key));
1405 if (strncmp(key + 1, pnpinfo, strlen(key + 1)) == 0)
1406 quoted_strcpy(retval, pnpinfo + strlen(key + 1));
1408 cp = strstr(pnpinfo, key);
1410 strcpy(retval, "MISSING");
1412 quoted_strcpy(retval, cp + strlen(key));
1418 devmatch_search_hints(struct moduledir *mdp, const char *bus, const char *dev, const char *pnpinfo)
1420 char val1[256], val2[256];
1421 int ival, len, ents, i, notme, mask, bit, v, found;
1422 void *ptr, *walker, *hints_end;
1423 char *lastmod = NULL, *cp, *s;
1425 moduledir_readhints(mdp);
1427 if (mdp->d_hints == NULL)
1429 walker = mdp->d_hints;
1430 hints_end = walker + mdp->d_hintsz;
1431 while (walker < hints_end && !found) {
1432 len = getint(&walker);
1433 ival = getint(&walker);
1438 ival = getint(&ptr);
1440 if (pnp_dump_flag || pnp_verbose_flag)
1441 printf("Version: if %s.%d kmod %s\n", val1, ival, val2);
1448 lastmod = strdup(val2);
1449 if (pnp_dump_flag || pnp_verbose_flag)
1450 printf("module %s in %s\n", val1, val1);
1453 if (!pnp_dump_flag && !pnp_unbound_flag && lastmod && strcmp(lastmod, "kernel") == 0)
1457 ents = getint(&ptr);
1458 if (pnp_dump_flag || pnp_verbose_flag)
1459 printf("PNP info for bus %s format %s %d entries (%s)\n",
1460 val1, val2, ents, lastmod);
1461 if (strcmp(val1, "usb") == 0) {
1462 if (pnp_verbose_flag)
1463 printf("Treating usb as uhub -- bug in source table still?\n");
1464 strcpy(val1, "uhub");
1466 if (bus && strcmp(val1, bus) != 0) {
1467 if (pnp_verbose_flag)
1468 printf("Skipped because table for bus %s, looking for %s\n",
1472 for (i = 0; i < ents; i++) {
1473 if (pnp_verbose_flag)
1474 printf("---------- Entry %d ----------\n", i);
1483 /* All integer fields */
1489 ival = getint(&ptr);
1490 if (pnp_dump_flag) {
1491 printf("%#x:", ival);
1494 if (bit >= 0 && ((1 << bit) & mask) == 0)
1496 v = pnpval_as_int(cp + 2, pnpinfo);
1497 if (pnp_verbose_flag)
1498 printf("Matching %s (%c) table=%#x tomatch=%#x\n",
1499 cp + 2, *cp, v, ival);
1526 if (pnp_dump_flag) {
1527 printf("'%s':", val1);
1532 s = pnpval_as_str(cp + 2, pnpinfo);
1533 if (strcmp(s, val1) != 0)
1536 /* Key override fields, required to be last in the string */
1539 * This is imperfect and only does one key and will be redone
1540 * to be more general for multiple keys. Currently, nothing
1543 if (pnp_dump_flag) /* No per-row data stored */
1545 if (cp[strlen(cp) - 1] == ';') /* Skip required ; at end */
1546 cp[strlen(cp) - 1] = '\0'; /* in case it's not there */
1547 if ((s = strstr(pnpinfo, cp + 2)) == NULL)
1549 else if (s > pnpinfo && s[-1] != ' ')
1553 printf("Unknown field type %c\n:", *cp);
1557 cp = strchr(cp, ';');
1560 } while (cp && *cp);
1564 if (!pnp_unbound_flag) {
1565 if (pnp_verbose_flag)
1566 printf("Matches --- %s ---\n", lastmod);
1575 walker = (void *)(len - sizeof(int) + (intptr_t)walker);
1577 if (pnp_unbound_flag && found == 0 && *pnpinfo) {
1578 if (pnp_verbose_flag)
1579 printf("------------------------- ");
1580 printf("%s on %s pnpinfo %s", *dev ? dev : "unattached", bus, pnpinfo);
1581 if (pnp_verbose_flag)
1582 printf(" -------------------------");
1594 * Attempt to locate the file containing the module (name)
1597 mod_searchmodule(char *name, struct mod_depend *verinfo)
1599 struct moduledir *mdp;
1602 moduledir_rebuild();
1604 * Now we ready to lookup module in the given directories
1607 STAILQ_FOREACH(mdp, &moduledir_list, d_link) {
1608 result = mod_search_hints(mdp, name, verinfo);
1617 mod_searchmodule_pnpinfo(const char *bus, const char *pnpinfo)
1619 struct moduledir *mdp;
1622 moduledir_rebuild();
1624 * Now we ready to lookup module in the given directories
1627 STAILQ_FOREACH(mdp, &moduledir_list, d_link) {
1628 result = devmatch_search_hints(mdp, bus, NULL, pnpinfo);
1637 file_addmodule(struct preloaded_file *fp, char *modname, int version,
1638 struct kernel_module **newmp)
1640 struct kernel_module *mp;
1641 struct mod_depend mdepend;
1643 bzero(&mdepend, sizeof(mdepend));
1644 mdepend.md_ver_preferred = version;
1645 mp = file_findmodule(fp, modname, &mdepend);
1648 mp = calloc(1, sizeof(struct kernel_module));
1651 mp->m_name = strdup(modname);
1652 if (mp->m_name == NULL) {
1656 mp->m_version = version;
1658 mp->m_next = fp->f_modules;
1669 file_discard(struct preloaded_file *fp)
1671 struct file_metadata *md, *md1;
1672 struct kernel_module *mp, *mp1;
1675 md = fp->f_metadata;
1695 * Allocate a new file; must be used instead of malloc()
1696 * to ensure safe initialisation.
1698 struct preloaded_file *
1702 return (calloc(1, sizeof(struct preloaded_file)));
1706 * Add a module to the chain
1709 file_insert_tail(struct preloaded_file *fp)
1711 struct preloaded_file *cm;
1713 /* Append to list of loaded file */
1715 if (preloaded_files == NULL) {
1716 preloaded_files = fp;
1718 for (cm = preloaded_files; cm->f_next != NULL; cm = cm->f_next)
1725 * Remove module from the chain
1728 file_remove(struct preloaded_file *fp)
1730 struct preloaded_file *cm;
1732 if (preloaded_files == NULL)
1735 if (preloaded_files == fp) {
1736 preloaded_files = fp->f_next;
1739 for (cm = preloaded_files; cm->f_next != NULL; cm = cm->f_next) {
1740 if (cm->f_next == fp) {
1741 cm->f_next = fp->f_next;
1748 moduledir_fullpath(struct moduledir *mdp, const char *fname)
1752 cp = malloc(strlen(mdp->d_path) + strlen(fname) + 2);
1755 strcpy(cp, mdp->d_path);
1762 * Read linker.hints file into memory performing some sanity checks.
1765 moduledir_readhints(struct moduledir *mdp)
1769 int fd, size, version;
1771 if (mdp->d_hints != NULL || (mdp->d_flags & MDIR_NOHINTS))
1773 path = moduledir_fullpath(mdp, "linker.hints");
1774 if (stat(path, &st) != 0 ||
1775 st.st_size < (ssize_t)(sizeof(version) + sizeof(int)) ||
1776 st.st_size > LINKER_HINTS_MAX || (fd = open(path, O_RDONLY)) < 0) {
1778 mdp->d_flags |= MDIR_NOHINTS;
1782 size = read(fd, &version, sizeof(version));
1783 if (size != sizeof(version) || version != LINKER_HINTS_VERSION)
1785 size = st.st_size - size;
1786 mdp->d_hints = malloc(size);
1787 if (mdp->d_hints == NULL)
1789 if (read(fd, mdp->d_hints, size) != size)
1791 mdp->d_hintsz = size;
1797 mdp->d_hints = NULL;
1798 mdp->d_flags |= MDIR_NOHINTS;
1803 * Extract directories from the ';' separated list, remove duplicates.
1806 moduledir_rebuild(void)
1808 struct moduledir *mdp, *mtmp;
1809 const char *path, *cp, *ep;
1812 path = getenv("module_path");
1814 path = default_searchpath;
1816 * Rebuild list of module directories if it changed
1818 STAILQ_FOREACH(mdp, &moduledir_list, d_link)
1819 mdp->d_flags |= MDIR_REMOVED;
1821 for (ep = path; *ep != 0; ep++) {
1823 for (; *ep != 0 && *ep != ';'; ep++)
1826 * Ignore trailing slashes
1828 for (cplen = ep - cp; cplen > 1 && cp[cplen - 1] == '/'; cplen--)
1830 STAILQ_FOREACH(mdp, &moduledir_list, d_link) {
1831 if (strlen(mdp->d_path) != cplen || bcmp(cp, mdp->d_path, cplen) != 0)
1833 mdp->d_flags &= ~MDIR_REMOVED;
1837 mdp = malloc(sizeof(*mdp) + cplen + 1);
1840 mdp->d_path = (char*)(mdp + 1);
1841 bcopy(cp, mdp->d_path, cplen);
1842 mdp->d_path[cplen] = 0;
1843 mdp->d_hints = NULL;
1845 STAILQ_INSERT_TAIL(&moduledir_list, mdp, d_link);
1851 * Delete unused directories if any
1853 mdp = STAILQ_FIRST(&moduledir_list);
1855 if ((mdp->d_flags & MDIR_REMOVED) == 0) {
1856 mdp = STAILQ_NEXT(mdp, d_link);
1860 mdp = STAILQ_NEXT(mdp, d_link);
1861 STAILQ_REMOVE(&moduledir_list, mtmp, moduledir, d_link);