xref: /freebsd/stand/common/module.c (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
1 /*-
2  * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  */
26 
27 /*
28  * file/module function dispatcher, support, etc.
29  */
30 
31 #include <stand.h>
32 #include <string.h>
33 #include <sys/param.h>
34 #include <sys/linker.h>
35 #include <sys/module.h>
36 #include <sys/queue.h>
37 #include <sys/stdint.h>
38 #include <sys/font.h>
39 #include <gfx_fb.h>
40 
41 #if defined(LOADER_FDT_SUPPORT)
42 #include <fdt_platform.h>
43 #endif
44 
45 #include "bootstrap.h"
46 
47 #define	MDIR_REMOVED	0x0001
48 #define	MDIR_NOHINTS	0x0002
49 
50 struct moduledir {
51 	char	*d_path;	/* path of modules directory */
52 	u_char	*d_hints;	/* content of linker.hints file */
53 	int	d_hintsz;	/* size of hints data */
54 	int	d_flags;
55 	STAILQ_ENTRY(moduledir) d_link;
56 };
57 
58 static int			file_load(char *filename, vm_offset_t dest, struct preloaded_file **result);
59 static int			file_load_dependencies(struct preloaded_file *base_mod);
60 static char *			file_search(const char *name, char **extlist);
61 static struct kernel_module *	file_findmodule(struct preloaded_file *fp, char *modname, struct mod_depend *verinfo);
62 static int			file_havepath(const char *name);
63 static char			*mod_searchmodule(char *name, struct mod_depend *verinfo);
64 static char *			mod_searchmodule_pnpinfo(const char *bus, const char *pnpinfo);
65 static void			file_insert_tail(struct preloaded_file *mp);
66 static void			file_remove(struct preloaded_file *fp);
67 struct file_metadata*		metadata_next(struct file_metadata *base_mp, int type);
68 static void			moduledir_readhints(struct moduledir *mdp);
69 static void			moduledir_rebuild(void);
70 
71 /* load address should be tweaked by first module loaded (kernel) */
72 static vm_offset_t	loadaddr = 0;
73 
74 #if defined(LOADER_FDT_SUPPORT)
75 static const char	*default_searchpath =
76     "/boot/kernel;/boot/modules;/boot/dtb";
77 #else
78 static const char	*default_searchpath = "/boot/kernel;/boot/modules";
79 #endif
80 
81 static STAILQ_HEAD(, moduledir) moduledir_list =
82     STAILQ_HEAD_INITIALIZER(moduledir_list);
83 
84 struct preloaded_file *preloaded_files = NULL;
85 
86 static char *kld_ext_list[] = {
87     ".ko",
88     "",
89     ".debug",
90     NULL
91 };
92 
93 /*
94  * load an object, either a disk file or code module.
95  *
96  * To load a file, the syntax is:
97  *
98  * load -t <type> <path>
99  *
100  * code modules are loaded as:
101  *
102  * load <path> <options>
103  */
104 
105 COMMAND_SET(load, "load", "load a kernel or module", command_load);
106 
107 static int
108 command_load(int argc, char *argv[])
109 {
110 	struct preloaded_file *fp;
111 	char	*typestr;
112 #ifdef LOADER_VERIEXEC
113 	char	*prefix, *skip;
114 	int	dflag = 0;
115 	char	*args = "dkp:s:t:";
116 #else
117 	char	*args = "kt:";
118 #endif
119 	int	dofile, dokld, ch, error;
120 
121 	dokld = dofile = 0;
122 	optind = 1;
123 	optreset = 1;
124 	typestr = NULL;
125 	if (argc == 1) {
126 		command_errmsg = "no filename specified";
127 		return (CMD_CRIT);
128 	}
129 #ifdef LOADER_VERIEXEC
130 	prefix = NULL;
131 	skip = NULL;
132 #endif
133 	while ((ch = getopt(argc, argv, args)) != -1) {
134 		switch(ch) {
135 #ifdef LOADER_VERIEXEC
136 		case 'd':
137 			dflag++;
138 			break;
139 #endif
140 		case 'k':
141 			dokld = 1;
142 			break;
143 #ifdef LOADER_VERIEXEC
144 		case 'p':
145 			prefix = optarg;
146 			break;
147 		case 's':
148 			skip = optarg;
149 			break;
150 #endif
151 		case 't':
152 			typestr = optarg;
153 			dofile = 1;
154 			break;
155 		case '?':
156 		default:
157 			/* getopt has already reported an error */
158 			return (CMD_OK);
159 		}
160 	}
161 	argv += (optind - 1);
162 	argc -= (optind - 1);
163 
164 	/*
165 	 * Request to load a raw file?
166 	 */
167 	if (dofile) {
168 		if ((argc != 2) || (typestr == NULL) || (*typestr == 0)) {
169 			command_errmsg = "invalid load type";
170 			return (CMD_CRIT);
171 		}
172 
173 #ifdef LOADER_VERIEXEC
174 		if (strncmp(typestr, "manifest", 8) == 0) {
175 			if (dflag > 0)
176 				ve_debug_set(dflag);
177 			return (load_manifest(argv[1], prefix, skip, NULL));
178 		}
179 #ifdef LOADER_VERIEXEC_PASS_MANIFEST
180 		if (strncmp(typestr, "pass_manifest", 13) == 0) {
181 			if (dflag > 0)
182 				ve_debug_set(dflag);
183 		    return (pass_manifest(argv[1], prefix));
184 		}
185 #endif
186 #endif
187 
188 		fp = file_findfile(argv[1], typestr);
189 		if (fp) {
190 			snprintf(command_errbuf, sizeof(command_errbuf),
191 			  "warning: file '%s' already loaded", argv[1]);
192 			return (CMD_WARN);
193 		}
194 
195 		if (file_loadraw(argv[1], typestr, 1) != NULL)
196 			return (CMD_OK);
197 
198 		/* Failing to load mfs_root is never going to end well! */
199 		if (strcmp("mfs_root", typestr) == 0)
200 			return (CMD_FATAL);
201 
202 		return (CMD_ERROR);
203 	}
204 	/*
205 	 * Do we have explicit KLD load ?
206 	 */
207 	if (dokld || file_havepath(argv[1])) {
208 		error = mod_loadkld(argv[1], argc - 2, argv + 2);
209 		if (error == EEXIST) {
210 			snprintf(command_errbuf, sizeof(command_errbuf),
211 			  "warning: KLD '%s' already loaded", argv[1]);
212 			return (CMD_WARN);
213 		}
214 
215 		return (error == 0 ? CMD_OK : CMD_CRIT);
216 	}
217 	/*
218 	 * Looks like a request for a module.
219 	 */
220 	error = mod_load(argv[1], NULL, argc - 2, argv + 2);
221 	if (error == EEXIST) {
222 		snprintf(command_errbuf, sizeof(command_errbuf),
223 		  "warning: module '%s' already loaded", argv[1]);
224 		return (CMD_WARN);
225 	}
226 
227 	return (error == 0 ? CMD_OK : CMD_CRIT);
228 }
229 
230 #ifdef LOADER_GELI_SUPPORT
231 COMMAND_SET(load_geli, "load_geli", "load a geli key", command_load_geli);
232 
233 static int
234 command_load_geli(int argc, char *argv[])
235 {
236 	char	typestr[80];
237 	char	*cp;
238 	int		ch, num;
239 
240 	if (argc < 3) {
241 		command_errmsg = "usage is [-n key#] <prov> <file>";
242 		return(CMD_ERROR);
243 	}
244 
245 	num = 0;
246 	optind = 1;
247 	optreset = 1;
248 	while ((ch = getopt(argc, argv, "n:")) != -1) {
249 		switch(ch) {
250 		case 'n':
251 			num = strtol(optarg, &cp, 0);
252 			if (cp == optarg) {
253 				snprintf(command_errbuf, sizeof(command_errbuf),
254 				  "bad key index '%s'", optarg);
255 				return(CMD_ERROR);
256 			}
257 			break;
258 		case '?':
259 		default:
260 			/* getopt has already reported an error */
261 			return(CMD_OK);
262 		}
263 	}
264 	argv += (optind - 1);
265 	argc -= (optind - 1);
266 	sprintf(typestr, "%s:geli_keyfile%d", argv[1], num);
267 	return (file_loadraw(argv[2], typestr, 1) ? CMD_OK : CMD_ERROR);
268 }
269 #endif
270 
271 void
272 unload(void)
273 {
274 	struct preloaded_file *fp;
275 
276 	while (preloaded_files != NULL) {
277 		fp = preloaded_files;
278 		preloaded_files = preloaded_files->f_next;
279 		file_discard(fp);
280 	}
281 	loadaddr = 0;
282 	unsetenv("kernelname");
283 }
284 
285 COMMAND_SET(unload, "unload", "unload all modules", command_unload);
286 
287 static int
288 command_unload(int argc, char *argv[])
289 {
290 	unload();
291 	return(CMD_OK);
292 }
293 
294 COMMAND_SET(lsmod, "lsmod", "list loaded modules", command_lsmod);
295 
296 static int
297 command_lsmod(int argc, char *argv[])
298 {
299 	struct preloaded_file	*fp;
300 	struct kernel_module	*mp;
301 	struct file_metadata	*md;
302 	char			lbuf[80];
303 	int				ch, verbose, ret = 0;
304 
305 	verbose = 0;
306 	optind = 1;
307 	optreset = 1;
308 	while ((ch = getopt(argc, argv, "v")) != -1) {
309 		switch(ch) {
310 		case 'v':
311 			verbose = 1;
312 			break;
313 		case '?':
314 		default:
315 			/* getopt has already reported an error */
316 			return(CMD_OK);
317 		}
318 	}
319 
320 	pager_open();
321 	for (fp = preloaded_files; fp; fp = fp->f_next) {
322 		snprintf(lbuf, sizeof(lbuf), " %p: ", (void *) fp->f_addr);
323 		pager_output(lbuf);
324 		pager_output(fp->f_name);
325 		snprintf(lbuf, sizeof(lbuf), " (%s, 0x%lx)\n", fp->f_type,
326 		  (long)fp->f_size);
327 		if (pager_output(lbuf))
328 			break;
329 		if (fp->f_args != NULL) {
330 			pager_output("    args: ");
331 			pager_output(fp->f_args);
332 			if (pager_output("\n"))
333 				break;
334 		}
335 		if (fp->f_modules) {
336 			pager_output("  modules: ");
337 			for (mp = fp->f_modules; mp; mp = mp->m_next) {
338 				snprintf(lbuf, sizeof(lbuf), "%s.%d ", mp->m_name,
339 				  mp->m_version);
340 				pager_output(lbuf);
341 			}
342 			if (pager_output("\n"))
343 				break;
344 		}
345 		if (verbose) {
346 			/* XXX could add some formatting smarts here to display some better */
347 			for (md = fp->f_metadata; md != NULL; md = md->md_next) {
348 				snprintf(lbuf, sizeof(lbuf), "      0x%04x, 0x%lx\n",
349 				  md->md_type, (long) md->md_size);
350 				if (pager_output(lbuf))
351 					break;
352 			}
353 		}
354 		if (ret)
355 			break;
356 	}
357 	pager_close();
358 	return(CMD_OK);
359 }
360 
361 COMMAND_SET(pnpmatch, "pnpmatch", "list matched modules based on pnpinfo", command_pnpmatch);
362 
363 static int pnp_dump_flag = 0;
364 static int pnp_unbound_flag = 0;
365 static int pnp_verbose_flag = 0;
366 
367 static int
368 command_pnpmatch(int argc, char *argv[])
369 {
370 	char *module;
371 	int ch;
372 
373 	pnp_verbose_flag = 0;
374 	pnp_dump_flag = 0;
375 	optind = 1;
376 	optreset = 1;
377 	while ((ch = getopt(argc, argv, "vd")) != -1) {
378 		switch(ch) {
379 		case 'v':
380 			pnp_verbose_flag = 1;
381 			break;
382 		case 'd':
383 			pnp_dump_flag = 1;
384 			break;
385 		case '?':
386 		default:
387 			/* getopt has already reported an error */
388 			return(CMD_OK);
389 		}
390 	}
391 	argv += optind;
392 	argc -= optind;
393 
394 	if (argc != 2) {
395 		command_errmsg = "Usage: pnpmatch <busname> compat=<compatdata>";
396 		return (CMD_CRIT);
397 	}
398 
399 	module = mod_searchmodule_pnpinfo(argv[0], argv[1]);
400 	if (module)
401 		printf("Matched module: %s\n", module);
402 	else
403 		printf("No module matches %s on bus %s\n", argv[1], argv[0]);
404 
405 	return (CMD_OK);
406 }
407 
408 COMMAND_SET(pnpload, "pnpload", "load matched modules based on pnpinfo", command_pnpload);
409 
410 static int
411 command_pnpload(int argc, char *argv[])
412 {
413 	char *module;
414 	int ch, error;
415 
416 	pnp_verbose_flag = 0;
417 	pnp_dump_flag = 0;
418 	optind = 1;
419 	optreset = 1;
420 	while ((ch = getopt(argc, argv, "vd")) != -1) {
421 		switch(ch) {
422 		case 'v':
423 			pnp_verbose_flag = 1;
424 			break;
425 		case 'd':
426 			pnp_dump_flag = 1;
427 			break;
428 		case '?':
429 		default:
430 			/* getopt has already reported an error */
431 			return(CMD_OK);
432 		}
433 	}
434 	argv += optind;
435 	argc -= optind;
436 
437 	if (argc != 2) {
438 		command_errmsg = "Usage: pnpload <busname> compat=<compatdata>";
439 		return (CMD_ERROR);
440 	}
441 
442 	module = mod_searchmodule_pnpinfo(argv[0], argv[1]);
443 
444 	error = mod_load(module, NULL, 0, NULL);
445 	if (error == EEXIST) {
446 		snprintf(command_errbuf, sizeof(command_errbuf),
447 		  "warning: module '%s' already loaded", argv[1]);
448 		return (CMD_WARN);
449 	}
450 
451 	return (error == 0 ? CMD_OK : CMD_CRIT);
452 }
453 
454 #if defined(LOADER_FDT_SUPPORT)
455 static void
456 pnpautoload_fdt_bus(const char *busname) {
457 	const char *pnpstring;
458 	const char *compatstr;
459 	char *pnpinfo = NULL;
460 	char *module = NULL;
461 	int tag = 0, len, pnplen;
462 	int error;
463 
464 	while (1) {
465 		pnpstring = fdt_devmatch_next(&tag, &len);
466 		if (pnpstring == NULL)
467 			return;
468 
469 		compatstr = pnpstring;
470 		for (pnplen = 0; pnplen != len; compatstr = pnpstring + pnplen) {
471 			pnplen += strlen(compatstr) + 1;
472 			asprintf(&pnpinfo, "compat=%s", compatstr);
473 
474 			module = mod_searchmodule_pnpinfo(busname, pnpinfo);
475 			if (module) {
476 				error = mod_loadkld(module, 0, NULL);
477 				if (error)
478 					printf("Cannot load module %s\n", module);
479 				break;
480 			}
481 		}
482 		free(pnpinfo);
483 		free(module);
484 	}
485 }
486 #endif
487 
488 struct pnp_bus {
489 	const char *name;
490 	void (*load)(const char *busname);
491 };
492 
493 struct pnp_bus pnp_buses[] = {
494 #if defined(LOADER_FDT_SUPPORT)
495 	{"simplebus", pnpautoload_fdt_bus},
496 	{"ofwbus", pnpautoload_fdt_bus},
497 	{"iicbus", pnpautoload_fdt_bus},
498 	{"spibus", pnpautoload_fdt_bus},
499 #endif
500 };
501 
502 COMMAND_SET(pnpautoload, "pnpautoload", "auto load modules based on pnpinfo", command_pnpautoload);
503 
504 static int
505 command_pnpautoload(int argc, char *argv[])
506 {
507 	int i;
508 	int verbose;
509 	int ch, match;
510 
511 	pnp_verbose_flag = 0;
512 	pnp_dump_flag = 0;
513 	verbose = 0;
514 	optind = 1;
515 	optreset = 1;
516 	match = 0;
517 	while ((ch = getopt(argc, argv, "v")) != -1) {
518 		switch(ch) {
519 		case 'v':
520 			verbose = 1;
521 			break;
522 		case '?':
523 		default:
524 			/* getopt has already reported an error */
525 			return(CMD_OK);
526 		}
527 	}
528 	argv += (optind - 1);
529 	argc -= (optind - 1);
530 
531 	if (argc > 2)
532 		return (CMD_ERROR);
533 
534 	for (i = 0; i < nitems(pnp_buses); i++) {
535 		if (argc == 2 && strcmp(argv[1], pnp_buses[i].name) != 0) {
536 			if (verbose)
537 				printf("Skipping bus %s\n", pnp_buses[i].name);
538 			continue;
539 		}
540 		if (verbose)
541 			printf("Autoloading modules for %s\n", pnp_buses[i].name);
542 		pnp_buses[i].load(pnp_buses[i].name);
543 		match = 1;
544 	}
545 	if (match == 0)
546 		printf("Unsupported bus %s\n", argv[1]);
547 
548 	return (CMD_OK);
549 }
550 
551 /*
552  * File level interface, functions file_*
553  */
554 int
555 file_load(char *filename, vm_offset_t dest, struct preloaded_file **result)
556 {
557 	static int last_file_format = 0;
558 	struct preloaded_file *fp;
559 	int error;
560 	int i;
561 
562 	TSENTER2(filename);
563 	if (archsw.arch_loadaddr != NULL)
564 		dest = archsw.arch_loadaddr(LOAD_RAW, filename, dest);
565 
566 	error = EFTYPE;
567 	for (i = last_file_format, fp = NULL;
568 	     file_formats[i] && fp == NULL; i++) {
569 		error = (file_formats[i]->l_load)(filename, dest, &fp);
570 		if (error == 0) {
571 			fp->f_loader = last_file_format = i; /* remember the loader */
572 			*result = fp;
573 			break;
574 		} else if (last_file_format == i && i != 0) {
575 			/* Restart from the beginning */
576 			i = -1;
577 			last_file_format = 0;
578 			fp = NULL;
579 			continue;
580 		}
581 		if (error == EFTYPE)
582 			continue;		/* Unknown to this handler? */
583 		if (error) {
584 			snprintf(command_errbuf, sizeof(command_errbuf),
585 			  "can't load file '%s': %s", filename, strerror(error));
586 			break;
587 		}
588 	}
589 	TSEXIT();
590 	return (error);
591 }
592 
593 static int
594 file_load_dependencies(struct preloaded_file *base_file)
595 {
596 	struct file_metadata *md;
597 	struct preloaded_file *fp;
598 	struct mod_depend *verinfo;
599 	struct kernel_module *mp;
600 	char *dmodname;
601 	int error;
602 
603 	md = file_findmetadata(base_file, MODINFOMD_DEPLIST);
604 	if (md == NULL)
605 		return (0);
606 	error = 0;
607 	do {
608 		verinfo = (struct mod_depend*)md->md_data;
609 		dmodname = (char *)(verinfo + 1);
610 		if (file_findmodule(NULL, dmodname, verinfo) == NULL) {
611 			if (module_verbose > MODULE_VERBOSE_SILENT)
612 				printf("loading required module '%s'\n", dmodname);
613 			error = mod_load(dmodname, verinfo, 0, NULL);
614 			if (error)
615 				break;
616 			/*
617 			 * If module loaded via kld name which isn't listed
618 			 * in the linker.hints file, we should check if it have
619 			 * required version.
620 			 */
621 			mp = file_findmodule(NULL, dmodname, verinfo);
622 			if (mp == NULL) {
623 				snprintf(command_errbuf, sizeof(command_errbuf),
624 				  "module '%s' exists but with wrong version", dmodname);
625 				error = ENOENT;
626 				break;
627 			}
628 		}
629 		md = metadata_next(md, MODINFOMD_DEPLIST);
630 	} while (md);
631 	if (!error)
632 		return (0);
633 	/* Load failed; discard everything */
634 	while (base_file != NULL) {
635 		fp = base_file;
636 		base_file = base_file->f_next;
637 		file_discard(fp);
638 	}
639 	return (error);
640 }
641 
642 #ifdef LOADER_VERIEXEC_VECTX
643 #define VECTX_HANDLE(fd) vctx
644 #else
645 #define VECTX_HANDLE(fd) fd
646 #endif
647 
648 
649 /*
650  * We've been asked to load (fname) as (type), so just suck it in,
651  * no arguments or anything.
652  */
653 struct preloaded_file *
654 file_loadraw(const char *fname, char *type, int insert)
655 {
656 	struct preloaded_file	*fp;
657 	char			*name;
658 	int				fd, got;
659 	vm_offset_t			laddr;
660 #ifdef LOADER_VERIEXEC_VECTX
661 	struct vectx		*vctx;
662 	int			verror;
663 #endif
664 
665 	TSENTER2(fname);
666 	/* We can't load first */
667 	if ((file_findfile(NULL, NULL)) == NULL) {
668 		command_errmsg = "can't load file before kernel";
669 		TSEXIT();
670 		return(NULL);
671 	}
672 
673 	/* locate the file on the load path */
674 	name = file_search(fname, NULL);
675 	if (name == NULL) {
676 		snprintf(command_errbuf, sizeof(command_errbuf),
677 		  "can't find '%s'", fname);
678 		TSEXIT();
679 		return(NULL);
680 	}
681 
682 	if ((fd = open(name, O_RDONLY)) < 0) {
683 		snprintf(command_errbuf, sizeof(command_errbuf),
684 		  "can't open '%s': %s", name, strerror(errno));
685 		free(name);
686 		TSEXIT();
687 		return(NULL);
688 	}
689 
690 #ifdef LOADER_VERIEXEC_VECTX
691 	vctx = vectx_open(fd, name, 0L, NULL, &verror, __func__);
692 	if (verror) {
693 		sprintf(command_errbuf, "can't verify '%s': %s",
694 		    name, ve_error_get());
695 		free(name);
696 		free(vctx);
697 		close(fd);
698 		TSEXIT();
699 		return(NULL);
700 	}
701 #else
702 #ifdef LOADER_VERIEXEC
703 	if (verify_file(fd, name, 0, VE_MUST, __func__) < 0) {
704 		sprintf(command_errbuf, "can't verify '%s': %s",
705 		    name, ve_error_get());
706 		free(name);
707 		close(fd);
708 		TSEXIT();
709 		return(NULL);
710 	}
711 #endif
712 #endif
713 
714 	if (archsw.arch_loadaddr != NULL)
715 		loadaddr = archsw.arch_loadaddr(LOAD_RAW, name, loadaddr);
716 
717 	if (module_verbose > MODULE_VERBOSE_SILENT)
718 		printf("%s ", name);
719 
720 	laddr = loadaddr;
721 	for (;;) {
722 		/* read in 4k chunks; size is not really important */
723 		got = archsw.arch_readin(VECTX_HANDLE(fd), laddr, 4096);
724 		if (got == 0)				/* end of file */
725 			break;
726 		if (got < 0) {				/* error */
727 			snprintf(command_errbuf, sizeof(command_errbuf),
728 			  "error reading '%s': %s", name, strerror(errno));
729 			free(name);
730 			close(fd);
731 #ifdef LOADER_VERIEXEC_VECTX
732 			free(vctx);
733 #endif
734 			TSEXIT();
735 			return(NULL);
736 		}
737 		laddr += got;
738 	}
739 
740 	if (module_verbose > MODULE_VERBOSE_SILENT)
741 		printf("size=%#jx\n", (uintmax_t)(laddr - loadaddr));
742 #ifdef LOADER_VERIEXEC_VECTX
743 	verror = vectx_close(vctx, VE_MUST, __func__);
744 	if (verror) {
745 		free(name);
746 		close(fd);
747 		free(vctx);
748 		TSEXIT();
749 		return(NULL);
750 	}
751 #endif
752 
753 	/* Looks OK so far; create & populate control structure */
754 	fp = file_alloc();
755 	if (fp == NULL) {
756 		snprintf(command_errbuf, sizeof (command_errbuf),
757 		    "no memory to load %s", name);
758 		free(name);
759 		close(fd);
760 		TSEXIT();
761 		return (NULL);
762 	}
763 	fp->f_name = name;
764 	fp->f_type = strdup(type);
765 	fp->f_args = NULL;
766 	fp->f_metadata = NULL;
767 	fp->f_loader = -1;
768 	fp->f_addr = loadaddr;
769 	fp->f_size = laddr - loadaddr;
770 
771 	if (fp->f_type == NULL) {
772 		snprintf(command_errbuf, sizeof (command_errbuf),
773 		    "no memory to load %s", name);
774 		free(name);
775 		close(fd);
776 		TSEXIT();
777 		return (NULL);
778 	}
779 	/* recognise space consumption */
780 	loadaddr = laddr;
781 
782 	/* Add to the list of loaded files */
783 	if (insert != 0)
784 		file_insert_tail(fp);
785 	close(fd);
786 	TSEXIT();
787 	return(fp);
788 }
789 
790 /*
791  * Load the module (name), pass it (argc),(argv), add container file
792  * to the list of loaded files.
793  * If module is already loaded just assign new argc/argv.
794  */
795 int
796 mod_load(char *modname, struct mod_depend *verinfo, int argc, char *argv[])
797 {
798 	struct kernel_module	*mp;
799 	int				err;
800 	char			*filename;
801 
802 	TSENTER2(modname);
803 	if (file_havepath(modname)) {
804 		printf("Warning: mod_load() called instead of mod_loadkld() for module '%s'\n", modname);
805 		TSEXIT();
806 		return (mod_loadkld(modname, argc, argv));
807 	}
808 	/* see if module is already loaded */
809 	mp = file_findmodule(NULL, modname, verinfo);
810 	if (mp) {
811 #ifdef moduleargs
812 		free(mp->m_args);
813 		mp->m_args = unargv(argc, argv);
814 #endif
815 		snprintf(command_errbuf, sizeof(command_errbuf),
816 		  "warning: module '%s' already loaded", mp->m_name);
817 		TSEXIT();
818 		return (0);
819 	}
820 	/* locate file with the module on the search path */
821 	filename = mod_searchmodule(modname, verinfo);
822 	if (filename == NULL) {
823 		snprintf(command_errbuf, sizeof(command_errbuf),
824 		  "can't find '%s'", modname);
825 		TSEXIT();
826 		return (ENOENT);
827 	}
828 	err = mod_loadkld(filename, argc, argv);
829 	free(filename);
830 	TSEXIT();
831 	return (err);
832 }
833 
834 /*
835  * Load specified KLD. If path is omitted, then try to locate it via
836  * search path.
837  */
838 int
839 mod_loadkld(const char *kldname, int argc, char *argv[])
840 {
841 	struct preloaded_file	*fp;
842 	int			err;
843 	char			*filename;
844 	vm_offset_t		loadaddr_saved;
845 
846 	TSENTER2(kldname);
847 	/*
848 	 * Get fully qualified KLD name
849 	 */
850 	filename = file_search(kldname, kld_ext_list);
851 	if (filename == NULL) {
852 		snprintf(command_errbuf, sizeof(command_errbuf),
853 		  "can't find '%s'", kldname);
854 		TSEXIT();
855 		return (ENOENT);
856 	}
857 	/*
858 	 * Check if KLD already loaded
859 	 */
860 	fp = file_findfile(filename, NULL);
861 	if (fp) {
862 		snprintf(command_errbuf, sizeof(command_errbuf),
863 		  "warning: KLD '%s' already loaded", filename);
864 		free(filename);
865 		TSEXIT();
866 		return (0);
867 	}
868 
869 	do {
870 		err = file_load(filename, loadaddr, &fp);
871 		if (err)
872 			break;
873 		fp->f_args = unargv(argc, argv);
874 		loadaddr_saved = loadaddr;
875 		loadaddr = fp->f_addr + fp->f_size;
876 		file_insert_tail(fp);	/* Add to the list of loaded files */
877 		if (file_load_dependencies(fp) != 0) {
878 			err = ENOENT;
879 			file_remove(fp);
880 			loadaddr = loadaddr_saved;
881 			fp = NULL;
882 			break;
883 		}
884 	} while(0);
885 	if (err == EFTYPE) {
886 		snprintf(command_errbuf, sizeof(command_errbuf),
887 		  "don't know how to load module '%s'", filename);
888 	}
889 	if (err)
890 		file_discard(fp);
891 	free(filename);
892 	TSEXIT();
893 	return (err);
894 }
895 
896 /*
897  * Find a file matching (name) and (type).
898  * NULL may be passed as a wildcard to either.
899  */
900 struct preloaded_file *
901 file_findfile(const char *name, const char *type)
902 {
903 	struct preloaded_file *fp;
904 
905 	for (fp = preloaded_files; fp != NULL; fp = fp->f_next) {
906 		if (((name == NULL) || !strcmp(name, fp->f_name)) &&
907 		  ((type == NULL) || !strcmp(type, fp->f_type)))
908 			break;
909 	}
910 	return (fp);
911 }
912 
913 /*
914  * Find a module matching (name) inside of given file.
915  * NULL may be passed as a wildcard.
916  */
917 struct kernel_module *
918 file_findmodule(struct preloaded_file *fp, char *modname,
919 	struct mod_depend *verinfo)
920 {
921 	struct kernel_module *mp, *best;
922 	int bestver, mver;
923 
924 	if (fp == NULL) {
925 		for (fp = preloaded_files; fp; fp = fp->f_next) {
926 			mp = file_findmodule(fp, modname, verinfo);
927 			if (mp)
928 				return (mp);
929 		}
930 		return (NULL);
931 	}
932 	best = NULL;
933 	bestver = 0;
934 	for (mp = fp->f_modules; mp; mp = mp->m_next) {
935 		if (strcmp(modname, mp->m_name) == 0) {
936 			if (verinfo == NULL)
937 				return (mp);
938 			mver = mp->m_version;
939 			if (mver == verinfo->md_ver_preferred)
940 				return (mp);
941 			if (mver >= verinfo->md_ver_minimum &&
942 			  mver <= verinfo->md_ver_maximum &&
943 			  mver > bestver) {
944 				best = mp;
945 				bestver = mver;
946 			}
947 		}
948 	}
949 	return (best);
950 }
951 /*
952  * Make a copy of (size) bytes of data from (p), and associate them as
953  * metadata of (type) to the module (mp).
954  */
955 void
956 file_addmetadata(struct preloaded_file *fp, int type, size_t size, void *p)
957 {
958 	struct file_metadata	*md;
959 
960 	md = malloc(sizeof(struct file_metadata) - sizeof(md->md_data) + size);
961 	if (md != NULL) {
962 		md->md_size = size;
963 		md->md_type = type;
964 		bcopy(p, md->md_data, size);
965 		md->md_next = fp->f_metadata;
966 	}
967 	fp->f_metadata = md;
968 }
969 
970 /*
971  * Find a metadata object of (type) associated with the file (fp)
972  */
973 struct file_metadata *
974 file_findmetadata(struct preloaded_file *fp, int type)
975 {
976 	struct file_metadata *md;
977 
978 	for (md = fp->f_metadata; md != NULL; md = md->md_next)
979 		if (md->md_type == type)
980 			break;
981 	return(md);
982 }
983 
984 /*
985  * Remove all metadata from the file.
986  */
987 void
988 file_removemetadata(struct preloaded_file *fp)
989 {
990 	struct file_metadata *md, *next;
991 
992 	for (md = fp->f_metadata; md != NULL; md = next)
993 	{
994 		next = md->md_next;
995 		free(md);
996 	}
997 	fp->f_metadata = NULL;
998 }
999 
1000 /*
1001  * Add a buffer to the list of preloaded "files".
1002  */
1003 int
1004 file_addbuf(const char *name, const char *type, size_t len, void *buf)
1005 {
1006 	struct preloaded_file	*fp;
1007 	vm_offset_t dest;
1008 
1009 	/* We can't load first */
1010 	if ((file_findfile(NULL, NULL)) == NULL) {
1011 		command_errmsg = "can't load file before kernel";
1012 		return (-1);
1013 	}
1014 
1015 	/* Figure out where to load the data. */
1016 	dest = loadaddr;
1017 	if (archsw.arch_loadaddr != NULL)
1018 		dest = archsw.arch_loadaddr(LOAD_RAW, (void *)name, dest);
1019 
1020 	/* Create & populate control structure */
1021 	fp = file_alloc();
1022 	if (fp == NULL) {
1023 		snprintf(command_errbuf, sizeof (command_errbuf),
1024 		    "no memory to load %s", name);
1025 		return (-1);
1026 	}
1027 	fp->f_name = strdup(name);
1028 	fp->f_type = strdup(type);
1029 	fp->f_args = NULL;
1030 	fp->f_metadata = NULL;
1031 	fp->f_loader = -1;
1032 	fp->f_addr = dest;
1033 	fp->f_size = len;
1034 	if ((fp->f_name == NULL) || (fp->f_type == NULL)) {
1035 		snprintf(command_errbuf, sizeof (command_errbuf),
1036 		    "no memory to load %s", name);
1037 		free(fp->f_name);
1038 		free(fp->f_type);
1039 		return (-1);
1040 	}
1041 
1042 	/* Copy the data in. */
1043 	archsw.arch_copyin(buf, fp->f_addr, len);
1044 	loadaddr = fp->f_addr + len;
1045 
1046 	/* Add to the list of loaded files */
1047 	file_insert_tail(fp);
1048 	return(0);
1049 }
1050 
1051 struct file_metadata *
1052 metadata_next(struct file_metadata *md, int type)
1053 {
1054 
1055 	if (md == NULL)
1056 		return (NULL);
1057 	while((md = md->md_next) != NULL)
1058 		if (md->md_type == type)
1059 			break;
1060 	return (md);
1061 }
1062 
1063 static char *emptyextlist[] = { "", NULL };
1064 
1065 /*
1066  * Check if the given file is in place and return full path to it.
1067  */
1068 static char *
1069 file_lookup(const char *path, const char *name, int namelen, char **extlist)
1070 {
1071 	struct stat	st;
1072 	char	*result, *cp, **cpp;
1073 	int		pathlen, extlen, len;
1074 
1075 	pathlen = strlen(path);
1076 	extlen = 0;
1077 	if (extlist == NULL)
1078 		extlist = emptyextlist;
1079 	for (cpp = extlist; *cpp; cpp++) {
1080 		len = strlen(*cpp);
1081 		if (len > extlen)
1082 			extlen = len;
1083 	}
1084 	result = malloc(pathlen + namelen + extlen + 2);
1085 	if (result == NULL)
1086 		return (NULL);
1087 	bcopy(path, result, pathlen);
1088 	if (pathlen > 0 && result[pathlen - 1] != '/')
1089 		result[pathlen++] = '/';
1090 	cp = result + pathlen;
1091 	bcopy(name, cp, namelen);
1092 	cp += namelen;
1093 	for (cpp = extlist; *cpp; cpp++) {
1094 		strcpy(cp, *cpp);
1095 		if (stat(result, &st) == 0 && S_ISREG(st.st_mode))
1096 			return result;
1097 	}
1098 	free(result);
1099 	return NULL;
1100 }
1101 
1102 /*
1103  * Check if file name have any qualifiers
1104  */
1105 static int
1106 file_havepath(const char *name)
1107 {
1108 	const char		*cp;
1109 
1110 	archsw.arch_getdev(NULL, name, &cp);
1111 	return (cp != name || strchr(name, '/') != NULL);
1112 }
1113 
1114 /*
1115  * Attempt to find the file (name) on the module searchpath.
1116  * If (name) is qualified in any way, we simply check it and
1117  * return it or NULL.  If it is not qualified, then we attempt
1118  * to construct a path using entries in the environment variable
1119  * module_path.
1120  *
1121  * The path we return a pointer to need never be freed, as we manage
1122  * it internally.
1123  */
1124 static char *
1125 file_search(const char *name, char **extlist)
1126 {
1127 	struct moduledir	*mdp;
1128 	struct stat		sb;
1129 	char		*result;
1130 	int			namelen;
1131 
1132 	/* Don't look for nothing */
1133 	if (name == NULL)
1134 		return(NULL);
1135 
1136 	if (*name == 0)
1137 		return(strdup(name));
1138 
1139 	if (file_havepath(name)) {
1140 		/* Qualified, so just see if it exists */
1141 		if (stat(name, &sb) == 0)
1142 			return(strdup(name));
1143 		return(NULL);
1144 	}
1145 	moduledir_rebuild();
1146 	result = NULL;
1147 	namelen = strlen(name);
1148 	STAILQ_FOREACH(mdp, &moduledir_list, d_link) {
1149 		result = file_lookup(mdp->d_path, name, namelen, extlist);
1150 		if (result)
1151 			break;
1152 	}
1153 	return(result);
1154 }
1155 
1156 #define	INT_ALIGN(base, ptr)	ptr = \
1157 	(base) + roundup2((ptr) - (base), sizeof(int))
1158 
1159 static char *
1160 mod_search_hints(struct moduledir *mdp, const char *modname,
1161 	struct mod_depend *verinfo)
1162 {
1163 	u_char	*cp, *recptr, *bufend, *best;
1164 	char	*result;
1165 	int		*intp, bestver, blen, clen, found, ival, modnamelen, reclen;
1166 
1167 	moduledir_readhints(mdp);
1168 	modnamelen = strlen(modname);
1169 	found = 0;
1170 	result = NULL;
1171 	bestver = 0;
1172 	if (mdp->d_hints == NULL)
1173 		goto bad;
1174 	recptr = mdp->d_hints;
1175 	bufend = recptr + mdp->d_hintsz;
1176 	clen = blen = 0;
1177 	best = cp = NULL;
1178 	while (recptr < bufend && !found) {
1179 		intp = (int*)recptr;
1180 		reclen = *intp++;
1181 		ival = *intp++;
1182 		cp = (u_char*)intp;
1183 		switch (ival) {
1184 		case MDT_VERSION:
1185 			clen = *cp++;
1186 			if (clen != modnamelen || bcmp(cp, modname, clen) != 0)
1187 				break;
1188 			cp += clen;
1189 			INT_ALIGN(mdp->d_hints, cp);
1190 			ival = *(int*)cp;
1191 			cp += sizeof(int);
1192 			clen = *cp++;
1193 			if (verinfo == NULL || ival == verinfo->md_ver_preferred) {
1194 				found = 1;
1195 				break;
1196 			}
1197 			if (ival >= verinfo->md_ver_minimum &&
1198 			  ival <= verinfo->md_ver_maximum &&
1199 			  ival > bestver) {
1200 				bestver = ival;
1201 				best = cp;
1202 				blen = clen;
1203 			}
1204 			break;
1205 		default:
1206 			break;
1207 		}
1208 		recptr += reclen + sizeof(int);
1209 	}
1210 	/*
1211 	 * Finally check if KLD is in the place
1212 	 */
1213 	if (found)
1214 		result = file_lookup(mdp->d_path, (const char *)cp, clen, NULL);
1215 	else if (best)
1216 		result = file_lookup(mdp->d_path, (const char *)best, blen, NULL);
1217 bad:
1218 	/*
1219 	 * If nothing found or hints is absent - fallback to the old way
1220 	 * by using "kldname[.ko]" as module name.
1221 	 */
1222 	if (!found && !bestver && result == NULL)
1223 		result = file_lookup(mdp->d_path, modname, modnamelen, kld_ext_list);
1224 	return result;
1225 }
1226 
1227 static int
1228 getint(void **ptr)
1229 {
1230 	int *p = *ptr;
1231 	int rv;
1232 
1233 	p = (int *)roundup2((intptr_t)p, sizeof(int));
1234 	rv = *p++;
1235 	*ptr = p;
1236 	return rv;
1237 }
1238 
1239 static void
1240 getstr(void **ptr, char *val)
1241 {
1242 	int *p = *ptr;
1243 	char *c = (char *)p;
1244 	int len = *(uint8_t *)c;
1245 
1246 	memcpy(val, c + 1, len);
1247 	val[len] = 0;
1248 	c += len + 1;
1249 	*ptr = (void *)c;
1250 }
1251 
1252 static int
1253 pnpval_as_int(const char *val, const char *pnpinfo)
1254 {
1255 	int rv;
1256 	char key[256];
1257 	char *cp;
1258 
1259 	if (pnpinfo == NULL)
1260 		return -1;
1261 
1262 	cp = strchr(val, ';');
1263 	key[0] = ' ';
1264 	if (cp == NULL)
1265 		strlcpy(key + 1, val, sizeof(key) - 1);
1266 	else {
1267 		memcpy(key + 1, val, cp - val);
1268 		key[cp - val + 1] = '\0';
1269 	}
1270 	strlcat(key, "=", sizeof(key));
1271 	if (strncmp(key + 1, pnpinfo, strlen(key + 1)) == 0)
1272 		rv = strtol(pnpinfo + strlen(key + 1), NULL, 0);
1273 	else {
1274 		cp = strstr(pnpinfo, key);
1275 		if (cp == NULL)
1276 			rv = -1;
1277 		else
1278 			rv = strtol(cp + strlen(key), NULL, 0);
1279 	}
1280 	return rv;
1281 }
1282 
1283 static void
1284 quoted_strcpy(char *dst, const char *src)
1285 {
1286 	char q = ' ';
1287 
1288 	if (*src == '\'' || *src == '"')
1289 		q = *src++;
1290 	while (*src && *src != q)
1291 		*dst++ = *src++; // XXX backtick quoting
1292 	*dst++ = '\0';
1293 	// XXX overflow
1294 }
1295 
1296 static char *
1297 pnpval_as_str(const char *val, const char *pnpinfo)
1298 {
1299 	static char retval[256];
1300 	char key[256];
1301 	char *cp;
1302 
1303 	if (pnpinfo == NULL) {
1304 		*retval = '\0';
1305 		return retval;
1306 	}
1307 
1308 	cp = strchr(val, ';');
1309 	key[0] = ' ';
1310 	if (cp == NULL)
1311 		strlcpy(key + 1, val, sizeof(key) - 1);
1312 	else {
1313 		memcpy(key + 1, val, cp - val);
1314 		key[cp - val + 1] = '\0';
1315 	}
1316 	strlcat(key, "=", sizeof(key));
1317 	if (strncmp(key + 1, pnpinfo, strlen(key + 1)) == 0)
1318 		quoted_strcpy(retval, pnpinfo + strlen(key + 1));
1319 	else {
1320 		cp = strstr(pnpinfo, key);
1321 		if (cp == NULL)
1322 			strcpy(retval, "MISSING");
1323 		else
1324 			quoted_strcpy(retval, cp + strlen(key));
1325 	}
1326 	return retval;
1327 }
1328 
1329 static char *
1330 devmatch_search_hints(struct moduledir *mdp, const char *bus, const char *dev, const char *pnpinfo)
1331 {
1332 	char val1[256], val2[256];
1333 	int ival, len, ents, i, notme, mask, bit, v, found;
1334 	void *ptr, *walker, *hints_end;
1335 	char *lastmod = NULL, *cp, *s;
1336 
1337 	moduledir_readhints(mdp);
1338 	found = 0;
1339 	if (mdp->d_hints == NULL)
1340 		goto bad;
1341 	walker = mdp->d_hints;
1342 	hints_end = walker + mdp->d_hintsz;
1343 	while (walker < hints_end && !found) {
1344 		len = getint(&walker);
1345 		ival = getint(&walker);
1346 		ptr = walker;
1347 		switch (ival) {
1348 		case MDT_VERSION:
1349 			getstr(&ptr, val1);
1350 			ival = getint(&ptr);
1351 			getstr(&ptr, val2);
1352 			if (pnp_dump_flag || pnp_verbose_flag)
1353 				printf("Version: if %s.%d kmod %s\n", val1, ival, val2);
1354 			break;
1355 		case MDT_MODULE:
1356 			getstr(&ptr, val1);
1357 			getstr(&ptr, val2);
1358 			if (lastmod)
1359 				free(lastmod);
1360 			lastmod = strdup(val2);
1361 			if (pnp_dump_flag || pnp_verbose_flag)
1362 				printf("module %s in %s\n", val1, val1);
1363 			break;
1364 		case MDT_PNP_INFO:
1365 			if (!pnp_dump_flag && !pnp_unbound_flag && lastmod && strcmp(lastmod, "kernel") == 0)
1366 				break;
1367 			getstr(&ptr, val1);
1368 			getstr(&ptr, val2);
1369 			ents = getint(&ptr);
1370 			if (pnp_dump_flag || pnp_verbose_flag)
1371 				printf("PNP info for bus %s format %s %d entries (%s)\n",
1372 				    val1, val2, ents, lastmod);
1373 			if (strcmp(val1, "usb") == 0) {
1374 				if (pnp_verbose_flag)
1375 					printf("Treating usb as uhub -- bug in source table still?\n");
1376 				strcpy(val1, "uhub");
1377 			}
1378 			if (bus && strcmp(val1, bus) != 0) {
1379 				if (pnp_verbose_flag)
1380 					printf("Skipped because table for bus %s, looking for %s\n",
1381 					    val1, bus);
1382 				break;
1383 			}
1384 			for (i = 0; i < ents; i++) {
1385 				if (pnp_verbose_flag)
1386 					printf("---------- Entry %d ----------\n", i);
1387 				if (pnp_dump_flag)
1388 					printf("   ");
1389 				cp = val2;
1390 				notme = 0;
1391 				mask = -1;
1392 				bit = -1;
1393 				do {
1394 					switch (*cp) {
1395 						/* All integer fields */
1396 					case 'I':
1397 					case 'J':
1398 					case 'G':
1399 					case 'L':
1400 					case 'M':
1401 						ival = getint(&ptr);
1402 						if (pnp_dump_flag) {
1403 							printf("%#x:", ival);
1404 							break;
1405 						}
1406 						if (bit >= 0 && ((1 << bit) & mask) == 0)
1407 							break;
1408 						v = pnpval_as_int(cp + 2, pnpinfo);
1409 						if (pnp_verbose_flag)
1410 							printf("Matching %s (%c) table=%#x tomatch=%#x\n",
1411 							    cp + 2, *cp, v, ival);
1412 						switch (*cp) {
1413 						case 'J':
1414 							if (ival == -1)
1415 								break;
1416 							/*FALLTHROUGH*/
1417 						case 'I':
1418 							if (v != ival)
1419 								notme++;
1420 							break;
1421 						case 'G':
1422 							if (v < ival)
1423 								notme++;
1424 							break;
1425 						case 'L':
1426 							if (v > ival)
1427 								notme++;
1428 							break;
1429 						case 'M':
1430 							mask = ival;
1431 							break;
1432 						}
1433 						break;
1434 						/* String fields */
1435 					case 'D':
1436 					case 'Z':
1437 						getstr(&ptr, val1);
1438 						if (pnp_dump_flag) {
1439 							printf("'%s':", val1);
1440 							break;
1441 						}
1442 						if (*cp == 'D')
1443 							break;
1444 						s = pnpval_as_str(cp + 2, pnpinfo);
1445 						if (strcmp(s, val1) != 0)
1446 							notme++;
1447 						break;
1448 						/* Key override fields, required to be last in the string */
1449 					case 'T':
1450 						/*
1451 						 * This is imperfect and only does one key and will be redone
1452 						 * to be more general for multiple keys. Currently, nothing
1453 						 * does that.
1454 						 */
1455 						if (pnp_dump_flag)				/* No per-row data stored */
1456 							break;
1457 						if (cp[strlen(cp) - 1] == ';')		/* Skip required ; at end */
1458 							cp[strlen(cp) - 1] = '\0';	/* in case it's not there */
1459 						if ((s = strstr(pnpinfo, cp + 2)) == NULL)
1460 							notme++;
1461 						else if (s > pnpinfo && s[-1] != ' ')
1462 							notme++;
1463 						break;
1464 					default:
1465 						printf("Unknown field type %c\n:", *cp);
1466 						break;
1467 					}
1468 					bit++;
1469 					cp = strchr(cp, ';');
1470 					if (cp)
1471 						cp++;
1472 				} while (cp && *cp);
1473 				if (pnp_dump_flag)
1474 					printf("\n");
1475 				else if (!notme) {
1476 					if (!pnp_unbound_flag) {
1477 						if (pnp_verbose_flag)
1478 							printf("Matches --- %s ---\n", lastmod);
1479 					}
1480 					found++;
1481 				}
1482 			}
1483 			break;
1484 		default:
1485 			break;
1486 		}
1487 		walker = (void *)(len - sizeof(int) + (intptr_t)walker);
1488 	}
1489 	if (pnp_unbound_flag && found == 0 && *pnpinfo) {
1490 		if (pnp_verbose_flag)
1491 			printf("------------------------- ");
1492 		printf("%s on %s pnpinfo %s", *dev ? dev : "unattached", bus, pnpinfo);
1493 		if (pnp_verbose_flag)
1494 			printf(" -------------------------");
1495 		printf("\n");
1496 	}
1497 	if (found != 0)
1498 		return (lastmod);
1499 	free(lastmod);
1500 
1501 bad:
1502 	return (NULL);
1503 }
1504 
1505 /*
1506  * Attempt to locate the file containing the module (name)
1507  */
1508 static char *
1509 mod_searchmodule(char *name, struct mod_depend *verinfo)
1510 {
1511 	struct	moduledir *mdp;
1512 	char	*result;
1513 
1514 	moduledir_rebuild();
1515 	/*
1516 	 * Now we ready to lookup module in the given directories
1517 	 */
1518 	result = NULL;
1519 	STAILQ_FOREACH(mdp, &moduledir_list, d_link) {
1520 		result = mod_search_hints(mdp, name, verinfo);
1521 		if (result)
1522 			break;
1523 	}
1524 
1525 	return(result);
1526 }
1527 
1528 static char *
1529 mod_searchmodule_pnpinfo(const char *bus, const char *pnpinfo)
1530 {
1531 	struct	moduledir *mdp;
1532 	char	*result;
1533 
1534 	moduledir_rebuild();
1535 	/*
1536 	 * Now we ready to lookup module in the given directories
1537 	 */
1538 	result = NULL;
1539 	STAILQ_FOREACH(mdp, &moduledir_list, d_link) {
1540 		result = devmatch_search_hints(mdp, bus, NULL, pnpinfo);
1541 		if (result)
1542 			break;
1543 	}
1544 
1545 	return(result);
1546 }
1547 
1548 int
1549 file_addmodule(struct preloaded_file *fp, char *modname, int version,
1550 	struct kernel_module **newmp)
1551 {
1552 	struct kernel_module *mp;
1553 	struct mod_depend mdepend;
1554 
1555 	bzero(&mdepend, sizeof(mdepend));
1556 	mdepend.md_ver_preferred = version;
1557 	mp = file_findmodule(fp, modname, &mdepend);
1558 	if (mp)
1559 		return (EEXIST);
1560 	mp = calloc(1, sizeof(struct kernel_module));
1561 	if (mp == NULL)
1562 		return (ENOMEM);
1563 	mp->m_name = strdup(modname);
1564 	if (mp->m_name == NULL) {
1565 		free(mp);
1566 		return (ENOMEM);
1567 	}
1568 	mp->m_version = version;
1569 	mp->m_fp = fp;
1570 	mp->m_next = fp->f_modules;
1571 	fp->f_modules = mp;
1572 	if (newmp)
1573 		*newmp = mp;
1574 	return (0);
1575 }
1576 
1577 /*
1578  * Throw a file away
1579  */
1580 void
1581 file_discard(struct preloaded_file *fp)
1582 {
1583 	struct file_metadata	*md, *md1;
1584 	struct kernel_module	*mp, *mp1;
1585 	if (fp == NULL)
1586 		return;
1587 	md = fp->f_metadata;
1588 	while (md) {
1589 		md1 = md;
1590 		md = md->md_next;
1591 		free(md1);
1592 	}
1593 	mp = fp->f_modules;
1594 	while (mp) {
1595 		free(mp->m_name);
1596 		mp1 = mp;
1597 		mp = mp->m_next;
1598 		free(mp1);
1599 	}
1600 	free(fp->f_name);
1601 	free(fp->f_type);
1602 	free(fp->f_args);
1603 	free(fp);
1604 }
1605 
1606 /*
1607  * Allocate a new file; must be used instead of malloc()
1608  * to ensure safe initialisation.
1609  */
1610 struct preloaded_file *
1611 file_alloc(void)
1612 {
1613 
1614 	return (calloc(1, sizeof(struct preloaded_file)));
1615 }
1616 
1617 /*
1618  * Add a module to the chain
1619  */
1620 static void
1621 file_insert_tail(struct preloaded_file *fp)
1622 {
1623 	struct preloaded_file	*cm;
1624 
1625 	/* Append to list of loaded file */
1626 	fp->f_next = NULL;
1627 	if (preloaded_files == NULL) {
1628 		preloaded_files = fp;
1629 	} else {
1630 		for (cm = preloaded_files; cm->f_next != NULL; cm = cm->f_next)
1631 			;
1632 		cm->f_next = fp;
1633 	}
1634 }
1635 
1636 /*
1637  * Remove module from the chain
1638  */
1639 static void
1640 file_remove(struct preloaded_file *fp)
1641 {
1642 	struct preloaded_file   *cm;
1643 
1644 	if (preloaded_files == NULL)
1645 		return;
1646 
1647 	if (preloaded_files == fp) {
1648 		preloaded_files = fp->f_next;
1649 		return;
1650         }
1651         for (cm = preloaded_files; cm->f_next != NULL; cm = cm->f_next) {
1652 		if (cm->f_next == fp) {
1653 			cm->f_next = fp->f_next;
1654 			return;
1655 		}
1656 	}
1657 }
1658 
1659 static char *
1660 moduledir_fullpath(struct moduledir *mdp, const char *fname)
1661 {
1662 	char *cp;
1663 
1664 	cp = malloc(strlen(mdp->d_path) + strlen(fname) + 2);
1665 	if (cp == NULL)
1666 		return NULL;
1667 	strcpy(cp, mdp->d_path);
1668 	strcat(cp, "/");
1669 	strcat(cp, fname);
1670 	return (cp);
1671 }
1672 
1673 /*
1674  * Read linker.hints file into memory performing some sanity checks.
1675  */
1676 static void
1677 moduledir_readhints(struct moduledir *mdp)
1678 {
1679 	struct stat	st;
1680 	char	*path;
1681 	int		fd, size, version;
1682 
1683 	if (mdp->d_hints != NULL || (mdp->d_flags & MDIR_NOHINTS))
1684 		return;
1685 	path = moduledir_fullpath(mdp, "linker.hints");
1686 	if (stat(path, &st) != 0 ||
1687 	  st.st_size < (ssize_t)(sizeof(version) + sizeof(int)) ||
1688 	  st.st_size > LINKER_HINTS_MAX || (fd = open(path, O_RDONLY)) < 0) {
1689 		free(path);
1690 		mdp->d_flags |= MDIR_NOHINTS;
1691 		return;
1692 	}
1693 	free(path);
1694 	size = read(fd, &version, sizeof(version));
1695 	if (size != sizeof(version) || version != LINKER_HINTS_VERSION)
1696 		goto bad;
1697 	size = st.st_size - size;
1698 	mdp->d_hints = malloc(size);
1699 	if (mdp->d_hints == NULL)
1700 		goto bad;
1701 	if (read(fd, mdp->d_hints, size) != size)
1702 		goto bad;
1703 	mdp->d_hintsz = size;
1704 	close(fd);
1705 	return;
1706 bad:
1707 	close(fd);
1708 	free(mdp->d_hints);
1709 	mdp->d_hints = NULL;
1710 	mdp->d_flags |= MDIR_NOHINTS;
1711 	return;
1712 }
1713 
1714 /*
1715  * Extract directories from the ';' separated list, remove duplicates.
1716  */
1717 static void
1718 moduledir_rebuild(void)
1719 {
1720 	struct	moduledir *mdp, *mtmp;
1721 	const char	*path, *cp, *ep;
1722 	size_t	cplen;
1723 
1724 	path = getenv("module_path");
1725 	if (path == NULL)
1726 		path = default_searchpath;
1727 	/*
1728 	 * Rebuild list of module directories if it changed
1729 	 */
1730 	STAILQ_FOREACH(mdp, &moduledir_list, d_link)
1731 		mdp->d_flags |= MDIR_REMOVED;
1732 
1733 	for (ep = path; *ep != 0;  ep++) {
1734 		cp = ep;
1735 		for (; *ep != 0 && *ep != ';'; ep++)
1736 			;
1737 		/*
1738 		 * Ignore trailing slashes
1739 		 */
1740 		for (cplen = ep - cp; cplen > 1 && cp[cplen - 1] == '/'; cplen--)
1741 			;
1742 		STAILQ_FOREACH(mdp, &moduledir_list, d_link) {
1743 			if (strlen(mdp->d_path) != cplen ||	bcmp(cp, mdp->d_path, cplen) != 0)
1744 				continue;
1745 			mdp->d_flags &= ~MDIR_REMOVED;
1746 			break;
1747 		}
1748 		if (mdp == NULL) {
1749 			mdp = malloc(sizeof(*mdp) + cplen + 1);
1750 			if (mdp == NULL)
1751 				return;
1752 			mdp->d_path = (char*)(mdp + 1);
1753 			bcopy(cp, mdp->d_path, cplen);
1754 			mdp->d_path[cplen] = 0;
1755 			mdp->d_hints = NULL;
1756 			mdp->d_flags = 0;
1757 			STAILQ_INSERT_TAIL(&moduledir_list, mdp, d_link);
1758 		}
1759 		if (*ep == 0)
1760 			break;
1761 	}
1762 	/*
1763 	 * Delete unused directories if any
1764 	 */
1765 	mdp = STAILQ_FIRST(&moduledir_list);
1766 	while (mdp) {
1767 		if ((mdp->d_flags & MDIR_REMOVED) == 0) {
1768 			mdp = STAILQ_NEXT(mdp, d_link);
1769 		} else {
1770 			free(mdp->d_hints);
1771 			mtmp = mdp;
1772 			mdp = STAILQ_NEXT(mdp, d_link);
1773 			STAILQ_REMOVE(&moduledir_list, mtmp, moduledir, d_link);
1774 			free(mtmp);
1775 		}
1776 	}
1777 	return;
1778 }
1779