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