xref: /freebsd/stand/common/module.c (revision c75ba31904e342443c89e4dfecf1aa1715c7e063)
1ca987d46SWarner Losh /*-
2ca987d46SWarner Losh  * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3ca987d46SWarner Losh  * All rights reserved.
4ca987d46SWarner Losh  *
5ca987d46SWarner Losh  * Redistribution and use in source and binary forms, with or without
6ca987d46SWarner Losh  * modification, are permitted provided that the following conditions
7ca987d46SWarner Losh  * are met:
8ca987d46SWarner Losh  * 1. Redistributions of source code must retain the above copyright
9ca987d46SWarner Losh  *    notice, this list of conditions and the following disclaimer.
10ca987d46SWarner Losh  * 2. Redistributions in binary form must reproduce the above copyright
11ca987d46SWarner Losh  *    notice, this list of conditions and the following disclaimer in the
12ca987d46SWarner Losh  *    documentation and/or other materials provided with the distribution.
13ca987d46SWarner Losh  *
14ca987d46SWarner Losh  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15ca987d46SWarner Losh  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16ca987d46SWarner Losh  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17ca987d46SWarner Losh  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18ca987d46SWarner Losh  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19ca987d46SWarner Losh  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20ca987d46SWarner Losh  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21ca987d46SWarner Losh  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22ca987d46SWarner Losh  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23ca987d46SWarner Losh  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24ca987d46SWarner Losh  * SUCH DAMAGE.
25ca987d46SWarner Losh  */
26ca987d46SWarner Losh 
27ca987d46SWarner Losh /*
28ca987d46SWarner Losh  * file/module function dispatcher, support, etc.
29ca987d46SWarner Losh  */
30ca987d46SWarner Losh 
31ca987d46SWarner Losh #include <stand.h>
32ca987d46SWarner Losh #include <string.h>
33ca987d46SWarner Losh #include <sys/param.h>
34ca987d46SWarner Losh #include <sys/linker.h>
35ca987d46SWarner Losh #include <sys/module.h>
36ca987d46SWarner Losh #include <sys/queue.h>
37ca987d46SWarner Losh #include <sys/stdint.h>
383630506bSToomas Soome #include <sys/font.h>
393630506bSToomas Soome #include <gfx_fb.h>
40ca987d46SWarner Losh 
4132199fb3SEmmanuel Vadot #if defined(LOADER_FDT_SUPPORT)
4232199fb3SEmmanuel Vadot #include <fdt_platform.h>
4332199fb3SEmmanuel Vadot #endif
4432199fb3SEmmanuel Vadot 
45ca987d46SWarner Losh #include "bootstrap.h"
46ca987d46SWarner Losh 
47ca987d46SWarner Losh #define	MDIR_REMOVED	0x0001
48ca987d46SWarner Losh #define	MDIR_NOHINTS	0x0002
49ca987d46SWarner Losh 
50ca987d46SWarner Losh struct moduledir {
51ca987d46SWarner Losh 	char	*d_path;	/* path of modules directory */
52ca987d46SWarner Losh 	u_char	*d_hints;	/* content of linker.hints file */
53ca987d46SWarner Losh 	int	d_hintsz;	/* size of hints data */
54ca987d46SWarner Losh 	int	d_flags;
55ca987d46SWarner Losh 	STAILQ_ENTRY(moduledir) d_link;
56ca987d46SWarner Losh };
57ca987d46SWarner Losh 
5832199fb3SEmmanuel Vadot static int			file_load(char *filename, vm_offset_t dest, struct preloaded_file **result);
5932199fb3SEmmanuel Vadot static int			file_load_dependencies(struct preloaded_file *base_mod);
6032199fb3SEmmanuel Vadot static char *			file_search(const char *name, char **extlist);
6132199fb3SEmmanuel Vadot static struct kernel_module *	file_findmodule(struct preloaded_file *fp, char *modname, struct mod_depend *verinfo);
6232199fb3SEmmanuel Vadot static int			file_havepath(const char *name);
6332199fb3SEmmanuel Vadot static char			*mod_searchmodule(char *name, struct mod_depend *verinfo);
6432199fb3SEmmanuel Vadot static char *			mod_searchmodule_pnpinfo(const char *bus, const char *pnpinfo);
6532199fb3SEmmanuel Vadot static void			file_insert_tail(struct preloaded_file *mp);
6632199fb3SEmmanuel Vadot static void			file_remove(struct preloaded_file *fp);
673da56871SKyle Evans static void			file_remove_tail(struct preloaded_file *fp);
6832199fb3SEmmanuel Vadot struct file_metadata*		metadata_next(struct file_metadata *base_mp, int type);
6932199fb3SEmmanuel Vadot static void			moduledir_readhints(struct moduledir *mdp);
70ca987d46SWarner Losh static void			moduledir_rebuild(void);
71ca987d46SWarner Losh 
72ca987d46SWarner Losh /* load address should be tweaked by first module loaded (kernel) */
73ca987d46SWarner Losh static vm_offset_t	loadaddr = 0;
74ca987d46SWarner Losh 
75ca987d46SWarner Losh #if defined(LOADER_FDT_SUPPORT)
76ca987d46SWarner Losh static const char	*default_searchpath =
77ca987d46SWarner Losh     "/boot/kernel;/boot/modules;/boot/dtb";
78ca987d46SWarner Losh #else
79ca987d46SWarner Losh static const char	*default_searchpath = "/boot/kernel;/boot/modules";
80ca987d46SWarner Losh #endif
81ca987d46SWarner Losh 
82ed792026SToomas Soome static STAILQ_HEAD(, moduledir) moduledir_list =
83ed792026SToomas Soome     STAILQ_HEAD_INITIALIZER(moduledir_list);
84ca987d46SWarner Losh 
85ca987d46SWarner Losh struct preloaded_file *preloaded_files = NULL;
86ca987d46SWarner Losh 
87ca987d46SWarner Losh static char *kld_ext_list[] = {
88ca987d46SWarner Losh     ".ko",
89ca987d46SWarner Losh     "",
90ca987d46SWarner Losh     ".debug",
91ca987d46SWarner Losh     NULL
92ca987d46SWarner Losh };
93ca987d46SWarner Losh 
94ca987d46SWarner Losh /*
95ca987d46SWarner Losh  * load an object, either a disk file or code module.
96ca987d46SWarner Losh  *
97ca987d46SWarner Losh  * To load a file, the syntax is:
98ca987d46SWarner Losh  *
99ca987d46SWarner Losh  * load -t <type> <path>
100ca987d46SWarner Losh  *
101ca987d46SWarner Losh  * code modules are loaded as:
102ca987d46SWarner Losh  *
103ca987d46SWarner Losh  * load <path> <options>
104ca987d46SWarner Losh  */
105ca987d46SWarner Losh 
106ca987d46SWarner Losh COMMAND_SET(load, "load", "load a kernel or module", command_load);
107ca987d46SWarner Losh 
108ca987d46SWarner Losh static int
command_load(int argc,char * argv[])109ca987d46SWarner Losh command_load(int argc, char *argv[])
110ca987d46SWarner Losh {
111ca987d46SWarner Losh 	struct preloaded_file *fp;
112ca987d46SWarner Losh 	char	*typestr;
1135c68a216SWarner Losh #ifdef LOADER_VERIEXEC
114f0377313SWarner Losh 	char	*prefix, *skip;
115cb817907SWarner Losh 	int	dflag = 0;
116cb817907SWarner Losh 	char	*args = "dkp:s:t:";
117cb817907SWarner Losh #else
118cb817907SWarner Losh 	char	*args = "kt:";
1195c68a216SWarner Losh #endif
120cb817907SWarner Losh 	int	dofile, dokld, ch, error;
121ca987d46SWarner Losh 
122cb817907SWarner Losh 	dokld = dofile = 0;
123ca987d46SWarner Losh 	optind = 1;
124ca987d46SWarner Losh 	optreset = 1;
125ca987d46SWarner Losh 	typestr = NULL;
126ca987d46SWarner Losh 	if (argc == 1) {
127ca987d46SWarner Losh 		command_errmsg = "no filename specified";
128ca987d46SWarner Losh 		return (CMD_CRIT);
129ca987d46SWarner Losh 	}
1305c68a216SWarner Losh #ifdef LOADER_VERIEXEC
1315c68a216SWarner Losh 	prefix = NULL;
1325c68a216SWarner Losh 	skip = NULL;
133f0377313SWarner Losh #endif
134cb817907SWarner Losh 	while ((ch = getopt(argc, argv, args)) != -1) {
135ca987d46SWarner Losh 		switch(ch) {
136cb817907SWarner Losh #ifdef LOADER_VERIEXEC
137fd1d7567SSimon J. Gerraty 		case 'd':
138fd1d7567SSimon J. Gerraty 			dflag++;
139fd1d7567SSimon J. Gerraty 			break;
140cb817907SWarner Losh #endif
141ca987d46SWarner Losh 		case 'k':
142ca987d46SWarner Losh 			dokld = 1;
143ca987d46SWarner Losh 			break;
1445c68a216SWarner Losh #ifdef LOADER_VERIEXEC
1458df8b2d3SSimon J. Gerraty 		case 'p':
1468df8b2d3SSimon J. Gerraty 			prefix = optarg;
1478df8b2d3SSimon J. Gerraty 			break;
1488df8b2d3SSimon J. Gerraty 		case 's':
1498df8b2d3SSimon J. Gerraty 			skip = optarg;
1508df8b2d3SSimon J. Gerraty 			break;
151f0377313SWarner Losh #endif
152ca987d46SWarner Losh 		case 't':
153ca987d46SWarner Losh 			typestr = optarg;
154ca987d46SWarner Losh 			dofile = 1;
155ca987d46SWarner Losh 			break;
156ca987d46SWarner Losh 		case '?':
157ca987d46SWarner Losh 		default:
158ca987d46SWarner Losh 			/* getopt has already reported an error */
159ca987d46SWarner Losh 			return (CMD_OK);
160ca987d46SWarner Losh 		}
161ca987d46SWarner Losh 	}
162ca987d46SWarner Losh 	argv += (optind - 1);
163ca987d46SWarner Losh 	argc -= (optind - 1);
164ca987d46SWarner Losh 
165ca987d46SWarner Losh 	/*
166ca987d46SWarner Losh 	 * Request to load a raw file?
167ca987d46SWarner Losh 	 */
168ca987d46SWarner Losh 	if (dofile) {
169ca987d46SWarner Losh 		if ((argc != 2) || (typestr == NULL) || (*typestr == 0)) {
170ca987d46SWarner Losh 			command_errmsg = "invalid load type";
171ca987d46SWarner Losh 			return (CMD_CRIT);
172ca987d46SWarner Losh 		}
173ca987d46SWarner Losh 
1748df8b2d3SSimon J. Gerraty #ifdef LOADER_VERIEXEC
1758df8b2d3SSimon J. Gerraty 		if (strncmp(typestr, "manifest", 8) == 0) {
176fd1d7567SSimon J. Gerraty 			if (dflag > 0)
177fd1d7567SSimon J. Gerraty 				ve_debug_set(dflag);
1788df8b2d3SSimon J. Gerraty 			return (load_manifest(argv[1], prefix, skip, NULL));
1798df8b2d3SSimon J. Gerraty 		}
180b0fefb25SMarcin Wojtas #ifdef LOADER_VERIEXEC_PASS_MANIFEST
181b0fefb25SMarcin Wojtas 		if (strncmp(typestr, "pass_manifest", 13) == 0) {
182b0fefb25SMarcin Wojtas 			if (dflag > 0)
183b0fefb25SMarcin Wojtas 				ve_debug_set(dflag);
184b0fefb25SMarcin Wojtas 		    return (pass_manifest(argv[1], prefix));
185b0fefb25SMarcin Wojtas 		}
186b0fefb25SMarcin Wojtas #endif
1878df8b2d3SSimon J. Gerraty #endif
1888df8b2d3SSimon J. Gerraty 
189ca987d46SWarner Losh 		fp = file_findfile(argv[1], typestr);
190ca987d46SWarner Losh 		if (fp) {
191ca987d46SWarner Losh 			snprintf(command_errbuf, sizeof(command_errbuf),
192ca987d46SWarner Losh 			  "warning: file '%s' already loaded", argv[1]);
193ca987d46SWarner Losh 			return (CMD_WARN);
194ca987d46SWarner Losh 		}
195ca987d46SWarner Losh 
196ca987d46SWarner Losh 		if (file_loadraw(argv[1], typestr, 1) != NULL)
197ca987d46SWarner Losh 			return (CMD_OK);
198ca987d46SWarner Losh 
199ca987d46SWarner Losh 		/* Failing to load mfs_root is never going to end well! */
200ca987d46SWarner Losh 		if (strcmp("mfs_root", typestr) == 0)
201ca987d46SWarner Losh 			return (CMD_FATAL);
202ca987d46SWarner Losh 
203ca987d46SWarner Losh 		return (CMD_ERROR);
204ca987d46SWarner Losh 	}
205ca987d46SWarner Losh 	/*
206ca987d46SWarner Losh 	 * Do we have explicit KLD load ?
207ca987d46SWarner Losh 	 */
208ca987d46SWarner Losh 	if (dokld || file_havepath(argv[1])) {
209ca987d46SWarner Losh 		error = mod_loadkld(argv[1], argc - 2, argv + 2);
210ca987d46SWarner Losh 		if (error == EEXIST) {
211ca987d46SWarner Losh 			snprintf(command_errbuf, sizeof(command_errbuf),
212ca987d46SWarner Losh 			  "warning: KLD '%s' already loaded", argv[1]);
213ca987d46SWarner Losh 			return (CMD_WARN);
214ca987d46SWarner Losh 		}
215ca987d46SWarner Losh 
216ca987d46SWarner Losh 		return (error == 0 ? CMD_OK : CMD_CRIT);
217ca987d46SWarner Losh 	}
218ca987d46SWarner Losh 	/*
219ca987d46SWarner Losh 	 * Looks like a request for a module.
220ca987d46SWarner Losh 	 */
221ca987d46SWarner Losh 	error = mod_load(argv[1], NULL, argc - 2, argv + 2);
222ca987d46SWarner Losh 	if (error == EEXIST) {
223ca987d46SWarner Losh 		snprintf(command_errbuf, sizeof(command_errbuf),
224ca987d46SWarner Losh 		  "warning: module '%s' already loaded", argv[1]);
225ca987d46SWarner Losh 		return (CMD_WARN);
226ca987d46SWarner Losh 	}
227ca987d46SWarner Losh 
228ca987d46SWarner Losh 	return (error == 0 ? CMD_OK : CMD_CRIT);
229ca987d46SWarner Losh }
230ca987d46SWarner Losh 
231ca987d46SWarner Losh #ifdef LOADER_GELI_SUPPORT
232ca987d46SWarner Losh COMMAND_SET(load_geli, "load_geli", "load a geli key", command_load_geli);
233ca987d46SWarner Losh 
234ca987d46SWarner Losh static int
command_load_geli(int argc,char * argv[])235ca987d46SWarner Losh command_load_geli(int argc, char *argv[])
236ca987d46SWarner Losh {
237ca987d46SWarner Losh 	char	typestr[80];
238ca987d46SWarner Losh 	char	*cp;
239ca987d46SWarner Losh 	int		ch, num;
240ca987d46SWarner Losh 
241ca987d46SWarner Losh 	if (argc < 3) {
242ca987d46SWarner Losh 		command_errmsg = "usage is [-n key#] <prov> <file>";
243ca987d46SWarner Losh 		return(CMD_ERROR);
244ca987d46SWarner Losh 	}
245ca987d46SWarner Losh 
246ca987d46SWarner Losh 	num = 0;
247ca987d46SWarner Losh 	optind = 1;
248ca987d46SWarner Losh 	optreset = 1;
249ca987d46SWarner Losh 	while ((ch = getopt(argc, argv, "n:")) != -1) {
250ca987d46SWarner Losh 		switch(ch) {
251ca987d46SWarner Losh 		case 'n':
252ca987d46SWarner Losh 			num = strtol(optarg, &cp, 0);
253ca987d46SWarner Losh 			if (cp == optarg) {
254ca987d46SWarner Losh 				snprintf(command_errbuf, sizeof(command_errbuf),
255ca987d46SWarner Losh 				  "bad key index '%s'", optarg);
256ca987d46SWarner Losh 				return(CMD_ERROR);
257ca987d46SWarner Losh 			}
258ca987d46SWarner Losh 			break;
259ca987d46SWarner Losh 		case '?':
260ca987d46SWarner Losh 		default:
261ca987d46SWarner Losh 			/* getopt has already reported an error */
262ca987d46SWarner Losh 			return(CMD_OK);
263ca987d46SWarner Losh 		}
264ca987d46SWarner Losh 	}
265ca987d46SWarner Losh 	argv += (optind - 1);
266ca987d46SWarner Losh 	argc -= (optind - 1);
267ca987d46SWarner Losh 	sprintf(typestr, "%s:geli_keyfile%d", argv[1], num);
268ca987d46SWarner Losh 	return (file_loadraw(argv[2], typestr, 1) ? CMD_OK : CMD_ERROR);
269ca987d46SWarner Losh }
270ca987d46SWarner Losh #endif
271ca987d46SWarner Losh 
272ca987d46SWarner Losh void
unload(void)273ca987d46SWarner Losh unload(void)
274ca987d46SWarner Losh {
275ca987d46SWarner Losh 	struct preloaded_file *fp;
276ca987d46SWarner Losh 
277ca987d46SWarner Losh 	while (preloaded_files != NULL) {
278ca987d46SWarner Losh 		fp = preloaded_files;
279ca987d46SWarner Losh 		preloaded_files = preloaded_files->f_next;
280ca987d46SWarner Losh 		file_discard(fp);
281ca987d46SWarner Losh 	}
282ca987d46SWarner Losh 	loadaddr = 0;
283ca987d46SWarner Losh 	unsetenv("kernelname");
284ca987d46SWarner Losh }
285ca987d46SWarner Losh 
286ca987d46SWarner Losh COMMAND_SET(unload, "unload", "unload all modules", command_unload);
287ca987d46SWarner Losh 
288ca987d46SWarner Losh static int
command_unload(int argc,char * argv[])289ca987d46SWarner Losh command_unload(int argc, char *argv[])
290ca987d46SWarner Losh {
291ca987d46SWarner Losh 	unload();
292ca987d46SWarner Losh 	return(CMD_OK);
293ca987d46SWarner Losh }
294ca987d46SWarner Losh 
295ca987d46SWarner Losh COMMAND_SET(lsmod, "lsmod", "list loaded modules", command_lsmod);
296ca987d46SWarner Losh 
297ca987d46SWarner Losh static int
command_lsmod(int argc,char * argv[])298ca987d46SWarner Losh command_lsmod(int argc, char *argv[])
299ca987d46SWarner Losh {
300ca987d46SWarner Losh 	struct preloaded_file	*fp;
301ca987d46SWarner Losh 	struct kernel_module	*mp;
302ca987d46SWarner Losh 	struct file_metadata	*md;
303ca987d46SWarner Losh 	char			lbuf[80];
304ca987d46SWarner Losh 	int				ch, verbose, ret = 0;
305ca987d46SWarner Losh 
306ca987d46SWarner Losh 	verbose = 0;
307ca987d46SWarner Losh 	optind = 1;
308ca987d46SWarner Losh 	optreset = 1;
309ca987d46SWarner Losh 	while ((ch = getopt(argc, argv, "v")) != -1) {
310ca987d46SWarner Losh 		switch(ch) {
311ca987d46SWarner Losh 		case 'v':
312ca987d46SWarner Losh 			verbose = 1;
313ca987d46SWarner Losh 			break;
314ca987d46SWarner Losh 		case '?':
315ca987d46SWarner Losh 		default:
316ca987d46SWarner Losh 			/* getopt has already reported an error */
317ca987d46SWarner Losh 			return(CMD_OK);
318ca987d46SWarner Losh 		}
319ca987d46SWarner Losh 	}
320ca987d46SWarner Losh 
321ca987d46SWarner Losh 	pager_open();
322ca987d46SWarner Losh 	for (fp = preloaded_files; fp; fp = fp->f_next) {
323ca987d46SWarner Losh 		snprintf(lbuf, sizeof(lbuf), " %p: ", (void *) fp->f_addr);
324ca987d46SWarner Losh 		pager_output(lbuf);
325ca987d46SWarner Losh 		pager_output(fp->f_name);
326ca987d46SWarner Losh 		snprintf(lbuf, sizeof(lbuf), " (%s, 0x%lx)\n", fp->f_type,
327ca987d46SWarner Losh 		  (long)fp->f_size);
328ca987d46SWarner Losh 		if (pager_output(lbuf))
329ca987d46SWarner Losh 			break;
330ca987d46SWarner Losh 		if (fp->f_args != NULL) {
331ca987d46SWarner Losh 			pager_output("    args: ");
332ca987d46SWarner Losh 			pager_output(fp->f_args);
333ca987d46SWarner Losh 			if (pager_output("\n"))
334ca987d46SWarner Losh 				break;
335ca987d46SWarner Losh 		}
336ca987d46SWarner Losh 		if (fp->f_modules) {
337ca987d46SWarner Losh 			pager_output("  modules: ");
338ca987d46SWarner Losh 			for (mp = fp->f_modules; mp; mp = mp->m_next) {
339ca987d46SWarner Losh 				snprintf(lbuf, sizeof(lbuf), "%s.%d ", mp->m_name,
340ca987d46SWarner Losh 				  mp->m_version);
341ca987d46SWarner Losh 				pager_output(lbuf);
342ca987d46SWarner Losh 			}
343ca987d46SWarner Losh 			if (pager_output("\n"))
344ca987d46SWarner Losh 				break;
345ca987d46SWarner Losh 		}
346ca987d46SWarner Losh 		if (verbose) {
347ca987d46SWarner Losh 			/* XXX could add some formatting smarts here to display some better */
348ca987d46SWarner Losh 			for (md = fp->f_metadata; md != NULL; md = md->md_next) {
349ca987d46SWarner Losh 				snprintf(lbuf, sizeof(lbuf), "      0x%04x, 0x%lx\n",
350ca987d46SWarner Losh 				  md->md_type, (long) md->md_size);
351ca987d46SWarner Losh 				if (pager_output(lbuf))
352ca987d46SWarner Losh 					break;
353ca987d46SWarner Losh 			}
354ca987d46SWarner Losh 		}
355ca987d46SWarner Losh 		if (ret)
356ca987d46SWarner Losh 			break;
357ca987d46SWarner Losh 	}
358ca987d46SWarner Losh 	pager_close();
359ca987d46SWarner Losh 	return(CMD_OK);
360ca987d46SWarner Losh }
361ca987d46SWarner Losh 
36232199fb3SEmmanuel Vadot COMMAND_SET(pnpmatch, "pnpmatch", "list matched modules based on pnpinfo", command_pnpmatch);
36332199fb3SEmmanuel Vadot 
36432199fb3SEmmanuel Vadot static int pnp_dump_flag = 0;
36532199fb3SEmmanuel Vadot static int pnp_unbound_flag = 0;
36632199fb3SEmmanuel Vadot static int pnp_verbose_flag = 0;
36732199fb3SEmmanuel Vadot 
36832199fb3SEmmanuel Vadot static int
command_pnpmatch(int argc,char * argv[])36932199fb3SEmmanuel Vadot command_pnpmatch(int argc, char *argv[])
37032199fb3SEmmanuel Vadot {
37132199fb3SEmmanuel Vadot 	char *module;
37232199fb3SEmmanuel Vadot 	int ch;
37332199fb3SEmmanuel Vadot 
37432199fb3SEmmanuel Vadot 	pnp_verbose_flag = 0;
37532199fb3SEmmanuel Vadot 	pnp_dump_flag = 0;
37632199fb3SEmmanuel Vadot 	optind = 1;
37732199fb3SEmmanuel Vadot 	optreset = 1;
37832199fb3SEmmanuel Vadot 	while ((ch = getopt(argc, argv, "vd")) != -1) {
37932199fb3SEmmanuel Vadot 		switch(ch) {
38032199fb3SEmmanuel Vadot 		case 'v':
38132199fb3SEmmanuel Vadot 			pnp_verbose_flag = 1;
38232199fb3SEmmanuel Vadot 			break;
38332199fb3SEmmanuel Vadot 		case 'd':
38432199fb3SEmmanuel Vadot 			pnp_dump_flag = 1;
38532199fb3SEmmanuel Vadot 			break;
38632199fb3SEmmanuel Vadot 		case '?':
38732199fb3SEmmanuel Vadot 		default:
38832199fb3SEmmanuel Vadot 			/* getopt has already reported an error */
38932199fb3SEmmanuel Vadot 			return(CMD_OK);
39032199fb3SEmmanuel Vadot 		}
39132199fb3SEmmanuel Vadot 	}
392544af629SEmmanuel Vadot 	argv += optind;
393544af629SEmmanuel Vadot 	argc -= optind;
39432199fb3SEmmanuel Vadot 
395544af629SEmmanuel Vadot 	if (argc != 2) {
396544af629SEmmanuel Vadot 		command_errmsg = "Usage: pnpmatch <busname> compat=<compatdata>";
397544af629SEmmanuel Vadot 		return (CMD_CRIT);
398544af629SEmmanuel Vadot 	}
399544af629SEmmanuel Vadot 
400544af629SEmmanuel Vadot 	module = mod_searchmodule_pnpinfo(argv[0], argv[1]);
40132199fb3SEmmanuel Vadot 	if (module)
40232199fb3SEmmanuel Vadot 		printf("Matched module: %s\n", module);
403544af629SEmmanuel Vadot 	else
404544af629SEmmanuel Vadot 		printf("No module matches %s on bus %s\n", argv[1], argv[0]);
40532199fb3SEmmanuel Vadot 
40632199fb3SEmmanuel Vadot 	return (CMD_OK);
40732199fb3SEmmanuel Vadot }
40832199fb3SEmmanuel Vadot 
40932199fb3SEmmanuel Vadot COMMAND_SET(pnpload, "pnpload", "load matched modules based on pnpinfo", command_pnpload);
41032199fb3SEmmanuel Vadot 
41132199fb3SEmmanuel Vadot static int
command_pnpload(int argc,char * argv[])41232199fb3SEmmanuel Vadot command_pnpload(int argc, char *argv[])
41332199fb3SEmmanuel Vadot {
41432199fb3SEmmanuel Vadot 	char *module;
41532199fb3SEmmanuel Vadot 	int ch, error;
41632199fb3SEmmanuel Vadot 
41732199fb3SEmmanuel Vadot 	pnp_verbose_flag = 0;
41832199fb3SEmmanuel Vadot 	pnp_dump_flag = 0;
41932199fb3SEmmanuel Vadot 	optind = 1;
42032199fb3SEmmanuel Vadot 	optreset = 1;
42132199fb3SEmmanuel Vadot 	while ((ch = getopt(argc, argv, "vd")) != -1) {
42232199fb3SEmmanuel Vadot 		switch(ch) {
42332199fb3SEmmanuel Vadot 		case 'v':
42432199fb3SEmmanuel Vadot 			pnp_verbose_flag = 1;
42532199fb3SEmmanuel Vadot 			break;
42632199fb3SEmmanuel Vadot 		case 'd':
42732199fb3SEmmanuel Vadot 			pnp_dump_flag = 1;
42832199fb3SEmmanuel Vadot 			break;
42932199fb3SEmmanuel Vadot 		case '?':
43032199fb3SEmmanuel Vadot 		default:
43132199fb3SEmmanuel Vadot 			/* getopt has already reported an error */
43232199fb3SEmmanuel Vadot 			return(CMD_OK);
43332199fb3SEmmanuel Vadot 		}
43432199fb3SEmmanuel Vadot 	}
435d4874307SEmmanuel Vadot 	argv += optind;
436d4874307SEmmanuel Vadot 	argc -= optind;
43732199fb3SEmmanuel Vadot 
438d4874307SEmmanuel Vadot 	if (argc != 2) {
439d4874307SEmmanuel Vadot 		command_errmsg = "Usage: pnpload <busname> compat=<compatdata>";
44032199fb3SEmmanuel Vadot 		return (CMD_ERROR);
441d4874307SEmmanuel Vadot 	}
44232199fb3SEmmanuel Vadot 
443d4874307SEmmanuel Vadot 	module = mod_searchmodule_pnpinfo(argv[0], argv[1]);
44432199fb3SEmmanuel Vadot 
44532199fb3SEmmanuel Vadot 	error = mod_load(module, NULL, 0, NULL);
44632199fb3SEmmanuel Vadot 	if (error == EEXIST) {
44732199fb3SEmmanuel Vadot 		snprintf(command_errbuf, sizeof(command_errbuf),
44832199fb3SEmmanuel Vadot 		  "warning: module '%s' already loaded", argv[1]);
44932199fb3SEmmanuel Vadot 		return (CMD_WARN);
45032199fb3SEmmanuel Vadot 	}
45132199fb3SEmmanuel Vadot 
45232199fb3SEmmanuel Vadot 	return (error == 0 ? CMD_OK : CMD_CRIT);
45332199fb3SEmmanuel Vadot }
45432199fb3SEmmanuel Vadot 
45532199fb3SEmmanuel Vadot #if defined(LOADER_FDT_SUPPORT)
45632199fb3SEmmanuel Vadot static void
pnpautoload_fdt_bus(const char * busname)4574c0c353dSEmmanuel Vadot pnpautoload_fdt_bus(const char *busname) {
45832199fb3SEmmanuel Vadot 	const char *pnpstring;
45932199fb3SEmmanuel Vadot 	const char *compatstr;
46032199fb3SEmmanuel Vadot 	char *pnpinfo = NULL;
461540a97e3SJung-uk Kim 	char *module = NULL;
46232199fb3SEmmanuel Vadot 	int tag = 0, len, pnplen;
46332199fb3SEmmanuel Vadot 	int error;
46432199fb3SEmmanuel Vadot 
46532199fb3SEmmanuel Vadot 	while (1) {
46632199fb3SEmmanuel Vadot 		pnpstring = fdt_devmatch_next(&tag, &len);
46732199fb3SEmmanuel Vadot 		if (pnpstring == NULL)
46832199fb3SEmmanuel Vadot 			return;
46932199fb3SEmmanuel Vadot 
47032199fb3SEmmanuel Vadot 		compatstr = pnpstring;
47132199fb3SEmmanuel Vadot 		for (pnplen = 0; pnplen != len; compatstr = pnpstring + pnplen) {
47232199fb3SEmmanuel Vadot 			pnplen += strlen(compatstr) + 1;
47332199fb3SEmmanuel Vadot 			asprintf(&pnpinfo, "compat=%s", compatstr);
47432199fb3SEmmanuel Vadot 
4754c0c353dSEmmanuel Vadot 			module = mod_searchmodule_pnpinfo(busname, pnpinfo);
47632199fb3SEmmanuel Vadot 			if (module) {
47732199fb3SEmmanuel Vadot 				error = mod_loadkld(module, 0, NULL);
47832199fb3SEmmanuel Vadot 				if (error)
47932199fb3SEmmanuel Vadot 					printf("Cannot load module %s\n", module);
48032199fb3SEmmanuel Vadot 				break;
48132199fb3SEmmanuel Vadot 			}
48232199fb3SEmmanuel Vadot 		}
48332199fb3SEmmanuel Vadot 		free(pnpinfo);
48432199fb3SEmmanuel Vadot 		free(module);
48532199fb3SEmmanuel Vadot 	}
48632199fb3SEmmanuel Vadot }
48732199fb3SEmmanuel Vadot #endif
48832199fb3SEmmanuel Vadot 
48932199fb3SEmmanuel Vadot struct pnp_bus {
49032199fb3SEmmanuel Vadot 	const char *name;
4914c0c353dSEmmanuel Vadot 	void (*load)(const char *busname);
49232199fb3SEmmanuel Vadot };
49332199fb3SEmmanuel Vadot 
49432199fb3SEmmanuel Vadot struct pnp_bus pnp_buses[] = {
49532199fb3SEmmanuel Vadot #if defined(LOADER_FDT_SUPPORT)
4964c0c353dSEmmanuel Vadot 	{"simplebus", pnpautoload_fdt_bus},
4974c0c353dSEmmanuel Vadot 	{"ofwbus", pnpautoload_fdt_bus},
4984c0c353dSEmmanuel Vadot 	{"iicbus", pnpautoload_fdt_bus},
4994c0c353dSEmmanuel Vadot 	{"spibus", pnpautoload_fdt_bus},
50032199fb3SEmmanuel Vadot #endif
50132199fb3SEmmanuel Vadot };
50232199fb3SEmmanuel Vadot 
50332199fb3SEmmanuel Vadot COMMAND_SET(pnpautoload, "pnpautoload", "auto load modules based on pnpinfo", command_pnpautoload);
50432199fb3SEmmanuel Vadot 
50532199fb3SEmmanuel Vadot static int
command_pnpautoload(int argc,char * argv[])50632199fb3SEmmanuel Vadot command_pnpautoload(int argc, char *argv[])
50732199fb3SEmmanuel Vadot {
50832199fb3SEmmanuel Vadot 	int i;
50932199fb3SEmmanuel Vadot 	int verbose;
5108a2ad0aeSEmmanuel Vadot 	int ch, match;
51132199fb3SEmmanuel Vadot 
51232199fb3SEmmanuel Vadot 	pnp_verbose_flag = 0;
51332199fb3SEmmanuel Vadot 	pnp_dump_flag = 0;
51432199fb3SEmmanuel Vadot 	verbose = 0;
51532199fb3SEmmanuel Vadot 	optind = 1;
51632199fb3SEmmanuel Vadot 	optreset = 1;
51732199fb3SEmmanuel Vadot 	match = 0;
51832199fb3SEmmanuel Vadot 	while ((ch = getopt(argc, argv, "v")) != -1) {
51932199fb3SEmmanuel Vadot 		switch(ch) {
52032199fb3SEmmanuel Vadot 		case 'v':
52132199fb3SEmmanuel Vadot 			verbose = 1;
52232199fb3SEmmanuel Vadot 			break;
52332199fb3SEmmanuel Vadot 		case '?':
52432199fb3SEmmanuel Vadot 		default:
52532199fb3SEmmanuel Vadot 			/* getopt has already reported an error */
52632199fb3SEmmanuel Vadot 			return(CMD_OK);
52732199fb3SEmmanuel Vadot 		}
52832199fb3SEmmanuel Vadot 	}
52932199fb3SEmmanuel Vadot 	argv += (optind - 1);
53032199fb3SEmmanuel Vadot 	argc -= (optind - 1);
53132199fb3SEmmanuel Vadot 
53232199fb3SEmmanuel Vadot 	if (argc > 2)
53332199fb3SEmmanuel Vadot 		return (CMD_ERROR);
53432199fb3SEmmanuel Vadot 
53532199fb3SEmmanuel Vadot 	for (i = 0; i < nitems(pnp_buses); i++) {
53632199fb3SEmmanuel Vadot 		if (argc == 2 && strcmp(argv[1], pnp_buses[i].name) != 0) {
53732199fb3SEmmanuel Vadot 			if (verbose)
53832199fb3SEmmanuel Vadot 				printf("Skipping bus %s\n", pnp_buses[i].name);
53932199fb3SEmmanuel Vadot 			continue;
54032199fb3SEmmanuel Vadot 		}
54132199fb3SEmmanuel Vadot 		if (verbose)
5424c0c353dSEmmanuel Vadot 			printf("Autoloading modules for %s\n", pnp_buses[i].name);
5434c0c353dSEmmanuel Vadot 		pnp_buses[i].load(pnp_buses[i].name);
54432199fb3SEmmanuel Vadot 		match = 1;
54532199fb3SEmmanuel Vadot 	}
54632199fb3SEmmanuel Vadot 	if (match == 0)
54732199fb3SEmmanuel Vadot 		printf("Unsupported bus %s\n", argv[1]);
54832199fb3SEmmanuel Vadot 
54932199fb3SEmmanuel Vadot 	return (CMD_OK);
55032199fb3SEmmanuel Vadot }
55132199fb3SEmmanuel Vadot 
552ca987d46SWarner Losh /*
553ca987d46SWarner Losh  * File level interface, functions file_*
554ca987d46SWarner Losh  */
555ca987d46SWarner Losh int
file_load(char * filename,vm_offset_t dest,struct preloaded_file ** result)556ca987d46SWarner Losh file_load(char *filename, vm_offset_t dest, struct preloaded_file **result)
557ca987d46SWarner Losh {
558ca987d46SWarner Losh 	static int last_file_format = 0;
559ca987d46SWarner Losh 	struct preloaded_file *fp;
560ca987d46SWarner Losh 	int error;
561ca987d46SWarner Losh 	int i;
562ca987d46SWarner Losh 
56301cad731SEmmanuel Vadot 	TSENTER2(filename);
564ca987d46SWarner Losh 	if (archsw.arch_loadaddr != NULL)
565ca987d46SWarner Losh 		dest = archsw.arch_loadaddr(LOAD_RAW, filename, dest);
566ca987d46SWarner Losh 
567ca987d46SWarner Losh 	error = EFTYPE;
568ca987d46SWarner Losh 	for (i = last_file_format, fp = NULL;
569ca987d46SWarner Losh 	     file_formats[i] && fp == NULL; i++) {
570ca987d46SWarner Losh 		error = (file_formats[i]->l_load)(filename, dest, &fp);
571ca987d46SWarner Losh 		if (error == 0) {
572ca987d46SWarner Losh 			fp->f_loader = last_file_format = i; /* remember the loader */
573ca987d46SWarner Losh 			*result = fp;
574ca987d46SWarner Losh 			break;
575ca987d46SWarner Losh 		} else if (last_file_format == i && i != 0) {
576ca987d46SWarner Losh 			/* Restart from the beginning */
577ca987d46SWarner Losh 			i = -1;
578ca987d46SWarner Losh 			last_file_format = 0;
579ca987d46SWarner Losh 			fp = NULL;
580ca987d46SWarner Losh 			continue;
581ca987d46SWarner Losh 		}
582ca987d46SWarner Losh 		if (error == EFTYPE)
583ca987d46SWarner Losh 			continue;		/* Unknown to this handler? */
584ca987d46SWarner Losh 		if (error) {
585ca987d46SWarner Losh 			snprintf(command_errbuf, sizeof(command_errbuf),
586ca987d46SWarner Losh 			  "can't load file '%s': %s", filename, strerror(error));
587ca987d46SWarner Losh 			break;
588ca987d46SWarner Losh 		}
589ca987d46SWarner Losh 	}
59001cad731SEmmanuel Vadot 	TSEXIT();
591ca987d46SWarner Losh 	return (error);
592ca987d46SWarner Losh }
593ca987d46SWarner Losh 
594ca987d46SWarner Losh static int
file_load_dependencies(struct preloaded_file * base_file)595ca987d46SWarner Losh file_load_dependencies(struct preloaded_file *base_file)
596ca987d46SWarner Losh {
597ca987d46SWarner Losh 	struct file_metadata *md;
598ca987d46SWarner Losh 	struct preloaded_file *fp;
599ca987d46SWarner Losh 	struct mod_depend *verinfo;
600ca987d46SWarner Losh 	struct kernel_module *mp;
601ca987d46SWarner Losh 	char *dmodname;
602ca987d46SWarner Losh 	int error;
603ca987d46SWarner Losh 
604ca987d46SWarner Losh 	md = file_findmetadata(base_file, MODINFOMD_DEPLIST);
605ca987d46SWarner Losh 	if (md == NULL)
606ca987d46SWarner Losh 		return (0);
607ca987d46SWarner Losh 	error = 0;
608ca987d46SWarner Losh 	do {
609ca987d46SWarner Losh 		verinfo = (struct mod_depend*)md->md_data;
610ca987d46SWarner Losh 		dmodname = (char *)(verinfo + 1);
611ca987d46SWarner Losh 		if (file_findmodule(NULL, dmodname, verinfo) == NULL) {
61201b0c359SSimon J. Gerraty 			if (module_verbose > MODULE_VERBOSE_SILENT)
613ca987d46SWarner Losh 				printf("loading required module '%s'\n", dmodname);
614ca987d46SWarner Losh 			error = mod_load(dmodname, verinfo, 0, NULL);
615ca987d46SWarner Losh 			if (error)
616ca987d46SWarner Losh 				break;
617ca987d46SWarner Losh 			/*
618ca987d46SWarner Losh 			 * If module loaded via kld name which isn't listed
619ca987d46SWarner Losh 			 * in the linker.hints file, we should check if it have
620ca987d46SWarner Losh 			 * required version.
621ca987d46SWarner Losh 			 */
622ca987d46SWarner Losh 			mp = file_findmodule(NULL, dmodname, verinfo);
623ca987d46SWarner Losh 			if (mp == NULL) {
624ca987d46SWarner Losh 				snprintf(command_errbuf, sizeof(command_errbuf),
625ca987d46SWarner Losh 				  "module '%s' exists but with wrong version", dmodname);
626ca987d46SWarner Losh 				error = ENOENT;
627ca987d46SWarner Losh 				break;
628ca987d46SWarner Losh 			}
629ca987d46SWarner Losh 		}
630ca987d46SWarner Losh 		md = metadata_next(md, MODINFOMD_DEPLIST);
631ca987d46SWarner Losh 	} while (md);
632ca987d46SWarner Losh 	if (!error)
633ca987d46SWarner Losh 		return (0);
634ca987d46SWarner Losh 	/* Load failed; discard everything */
635ca987d46SWarner Losh 	while (base_file != NULL) {
636ca987d46SWarner Losh 		fp = base_file;
637ca987d46SWarner Losh 		base_file = base_file->f_next;
638ca987d46SWarner Losh 		file_discard(fp);
639ca987d46SWarner Losh 	}
640ca987d46SWarner Losh 	return (error);
641ca987d46SWarner Losh }
642ca987d46SWarner Losh 
643afc571b1SSimon J. Gerraty #ifdef LOADER_VERIEXEC_VECTX
644afc571b1SSimon J. Gerraty #define VECTX_HANDLE(fd) vctx
645afc571b1SSimon J. Gerraty #else
646afc571b1SSimon J. Gerraty #define VECTX_HANDLE(fd) fd
647afc571b1SSimon J. Gerraty #endif
648afc571b1SSimon J. Gerraty 
649afc571b1SSimon J. Gerraty 
650ca987d46SWarner Losh /*
651ca987d46SWarner Losh  * We've been asked to load (fname) as (type), so just suck it in,
652ca987d46SWarner Losh  * no arguments or anything.
653ca987d46SWarner Losh  */
654ca987d46SWarner Losh struct preloaded_file *
file_loadraw(const char * fname,const char * type,int insert)655*c75ba319SAhmad Khalifa file_loadraw(const char *fname, const char *type, int insert)
656ca987d46SWarner Losh {
657ca987d46SWarner Losh 	struct preloaded_file	*fp;
658ca987d46SWarner Losh 	char			*name;
659ca987d46SWarner Losh 	int				fd, got;
660ca987d46SWarner Losh 	vm_offset_t			laddr;
661afc571b1SSimon J. Gerraty #ifdef LOADER_VERIEXEC_VECTX
662afc571b1SSimon J. Gerraty 	struct vectx		*vctx;
663afc571b1SSimon J. Gerraty 	int			verror;
664afc571b1SSimon J. Gerraty #endif
665ca987d46SWarner Losh 
66601cad731SEmmanuel Vadot 	TSENTER2(fname);
667ca987d46SWarner Losh 	/* We can't load first */
668ca987d46SWarner Losh 	if ((file_findfile(NULL, NULL)) == NULL) {
669ca987d46SWarner Losh 		command_errmsg = "can't load file before kernel";
67001cad731SEmmanuel Vadot 		TSEXIT();
671ca987d46SWarner Losh 		return(NULL);
672ca987d46SWarner Losh 	}
673ca987d46SWarner Losh 
674ca987d46SWarner Losh 	/* locate the file on the load path */
675ca987d46SWarner Losh 	name = file_search(fname, NULL);
676ca987d46SWarner Losh 	if (name == NULL) {
677ca987d46SWarner Losh 		snprintf(command_errbuf, sizeof(command_errbuf),
678ca987d46SWarner Losh 		  "can't find '%s'", fname);
67901cad731SEmmanuel Vadot 		TSEXIT();
680ca987d46SWarner Losh 		return(NULL);
681ca987d46SWarner Losh 	}
682ca987d46SWarner Losh 
683ca987d46SWarner Losh 	if ((fd = open(name, O_RDONLY)) < 0) {
684ca987d46SWarner Losh 		snprintf(command_errbuf, sizeof(command_errbuf),
685ca987d46SWarner Losh 		  "can't open '%s': %s", name, strerror(errno));
686ca987d46SWarner Losh 		free(name);
68701cad731SEmmanuel Vadot 		TSEXIT();
688ca987d46SWarner Losh 		return(NULL);
689ca987d46SWarner Losh 	}
690ca987d46SWarner Losh 
691afc571b1SSimon J. Gerraty #ifdef LOADER_VERIEXEC_VECTX
692afc571b1SSimon J. Gerraty 	vctx = vectx_open(fd, name, 0L, NULL, &verror, __func__);
693afc571b1SSimon J. Gerraty 	if (verror) {
694afc571b1SSimon J. Gerraty 		sprintf(command_errbuf, "can't verify '%s': %s",
695afc571b1SSimon J. Gerraty 		    name, ve_error_get());
696afc571b1SSimon J. Gerraty 		free(name);
697afc571b1SSimon J. Gerraty 		free(vctx);
698afc571b1SSimon J. Gerraty 		close(fd);
69901cad731SEmmanuel Vadot 		TSEXIT();
700afc571b1SSimon J. Gerraty 		return(NULL);
701afc571b1SSimon J. Gerraty 	}
702afc571b1SSimon J. Gerraty #else
7038df8b2d3SSimon J. Gerraty #ifdef LOADER_VERIEXEC
704afc571b1SSimon J. Gerraty 	if (verify_file(fd, name, 0, VE_MUST, __func__) < 0) {
705afc571b1SSimon J. Gerraty 		sprintf(command_errbuf, "can't verify '%s': %s",
706afc571b1SSimon J. Gerraty 		    name, ve_error_get());
7078df8b2d3SSimon J. Gerraty 		free(name);
7088df8b2d3SSimon J. Gerraty 		close(fd);
70901cad731SEmmanuel Vadot 		TSEXIT();
7108df8b2d3SSimon J. Gerraty 		return(NULL);
7118df8b2d3SSimon J. Gerraty 	}
7128df8b2d3SSimon J. Gerraty #endif
713afc571b1SSimon J. Gerraty #endif
7148df8b2d3SSimon J. Gerraty 
715ca987d46SWarner Losh 	if (archsw.arch_loadaddr != NULL)
716ca987d46SWarner Losh 		loadaddr = archsw.arch_loadaddr(LOAD_RAW, name, loadaddr);
717ca987d46SWarner Losh 
71801b0c359SSimon J. Gerraty 	if (module_verbose > MODULE_VERBOSE_SILENT)
719ca987d46SWarner Losh 		printf("%s ", name);
720ca987d46SWarner Losh 
721ca987d46SWarner Losh 	laddr = loadaddr;
722ca987d46SWarner Losh 	for (;;) {
723ca987d46SWarner Losh 		/* read in 4k chunks; size is not really important */
724afc571b1SSimon J. Gerraty 		got = archsw.arch_readin(VECTX_HANDLE(fd), laddr, 4096);
725ca987d46SWarner Losh 		if (got == 0)				/* end of file */
726ca987d46SWarner Losh 			break;
727ca987d46SWarner Losh 		if (got < 0) {				/* error */
728ca987d46SWarner Losh 			snprintf(command_errbuf, sizeof(command_errbuf),
729ca987d46SWarner Losh 			  "error reading '%s': %s", name, strerror(errno));
730ca987d46SWarner Losh 			free(name);
731ca987d46SWarner Losh 			close(fd);
732afc571b1SSimon J. Gerraty #ifdef LOADER_VERIEXEC_VECTX
733afc571b1SSimon J. Gerraty 			free(vctx);
734afc571b1SSimon J. Gerraty #endif
73501cad731SEmmanuel Vadot 			TSEXIT();
736ca987d46SWarner Losh 			return(NULL);
737ca987d46SWarner Losh 		}
738ca987d46SWarner Losh 		laddr += got;
739ca987d46SWarner Losh 	}
740ca987d46SWarner Losh 
74101b0c359SSimon J. Gerraty 	if (module_verbose > MODULE_VERBOSE_SILENT)
742ca987d46SWarner Losh 		printf("size=%#jx\n", (uintmax_t)(laddr - loadaddr));
743afc571b1SSimon J. Gerraty #ifdef LOADER_VERIEXEC_VECTX
744afc571b1SSimon J. Gerraty 	verror = vectx_close(vctx, VE_MUST, __func__);
745afc571b1SSimon J. Gerraty 	if (verror) {
746afc571b1SSimon J. Gerraty 		free(name);
747afc571b1SSimon J. Gerraty 		close(fd);
748afc571b1SSimon J. Gerraty 		free(vctx);
74901cad731SEmmanuel Vadot 		TSEXIT();
750afc571b1SSimon J. Gerraty 		return(NULL);
751afc571b1SSimon J. Gerraty 	}
752afc571b1SSimon J. Gerraty #endif
753ca987d46SWarner Losh 
754ca987d46SWarner Losh 	/* Looks OK so far; create & populate control structure */
755ca987d46SWarner Losh 	fp = file_alloc();
756b17868a2SToomas Soome 	if (fp == NULL) {
757b17868a2SToomas Soome 		snprintf(command_errbuf, sizeof (command_errbuf),
758b17868a2SToomas Soome 		    "no memory to load %s", name);
759b17868a2SToomas Soome 		free(name);
760b17868a2SToomas Soome 		close(fd);
76101cad731SEmmanuel Vadot 		TSEXIT();
762b17868a2SToomas Soome 		return (NULL);
763b17868a2SToomas Soome 	}
764b17868a2SToomas Soome 	fp->f_name = name;
765ca987d46SWarner Losh 	fp->f_type = strdup(type);
766ca987d46SWarner Losh 	fp->f_args = NULL;
767ca987d46SWarner Losh 	fp->f_metadata = NULL;
768ca987d46SWarner Losh 	fp->f_loader = -1;
769ca987d46SWarner Losh 	fp->f_addr = loadaddr;
770ca987d46SWarner Losh 	fp->f_size = laddr - loadaddr;
771ca987d46SWarner Losh 
772b17868a2SToomas Soome 	if (fp->f_type == NULL) {
773b17868a2SToomas Soome 		snprintf(command_errbuf, sizeof (command_errbuf),
774b17868a2SToomas Soome 		    "no memory to load %s", name);
775b17868a2SToomas Soome 		free(name);
776b17868a2SToomas Soome 		close(fd);
77701cad731SEmmanuel Vadot 		TSEXIT();
778b17868a2SToomas Soome 		return (NULL);
779b17868a2SToomas Soome 	}
780ca987d46SWarner Losh 	/* recognise space consumption */
781ca987d46SWarner Losh 	loadaddr = laddr;
782ca987d46SWarner Losh 
783ca987d46SWarner Losh 	/* Add to the list of loaded files */
784ca987d46SWarner Losh 	if (insert != 0)
785ca987d46SWarner Losh 		file_insert_tail(fp);
786ca987d46SWarner Losh 	close(fd);
78701cad731SEmmanuel Vadot 	TSEXIT();
788ca987d46SWarner Losh 	return(fp);
789ca987d46SWarner Losh }
790ca987d46SWarner Losh 
791ca987d46SWarner Losh /*
792ca987d46SWarner Losh  * Load the module (name), pass it (argc),(argv), add container file
793ca987d46SWarner Losh  * to the list of loaded files.
794ca987d46SWarner Losh  * If module is already loaded just assign new argc/argv.
795ca987d46SWarner Losh  */
796ca987d46SWarner Losh int
mod_load(char * modname,struct mod_depend * verinfo,int argc,char * argv[])797ca987d46SWarner Losh mod_load(char *modname, struct mod_depend *verinfo, int argc, char *argv[])
798ca987d46SWarner Losh {
799ca987d46SWarner Losh 	struct kernel_module	*mp;
800ca987d46SWarner Losh 	int				err;
801ca987d46SWarner Losh 	char			*filename;
802ca987d46SWarner Losh 
80301cad731SEmmanuel Vadot 	TSENTER2(modname);
804ca987d46SWarner Losh 	if (file_havepath(modname)) {
805ca987d46SWarner Losh 		printf("Warning: mod_load() called instead of mod_loadkld() for module '%s'\n", modname);
80601cad731SEmmanuel Vadot 		TSEXIT();
807ca987d46SWarner Losh 		return (mod_loadkld(modname, argc, argv));
808ca987d46SWarner Losh 	}
809ca987d46SWarner Losh 	/* see if module is already loaded */
810ca987d46SWarner Losh 	mp = file_findmodule(NULL, modname, verinfo);
811ca987d46SWarner Losh 	if (mp) {
812ca987d46SWarner Losh #ifdef moduleargs
813ca987d46SWarner Losh 		free(mp->m_args);
814ca987d46SWarner Losh 		mp->m_args = unargv(argc, argv);
815ca987d46SWarner Losh #endif
816ca987d46SWarner Losh 		snprintf(command_errbuf, sizeof(command_errbuf),
817ca987d46SWarner Losh 		  "warning: module '%s' already loaded", mp->m_name);
81801cad731SEmmanuel Vadot 		TSEXIT();
819ca987d46SWarner Losh 		return (0);
820ca987d46SWarner Losh 	}
821ca987d46SWarner Losh 	/* locate file with the module on the search path */
822ca987d46SWarner Losh 	filename = mod_searchmodule(modname, verinfo);
823ca987d46SWarner Losh 	if (filename == NULL) {
824ca987d46SWarner Losh 		snprintf(command_errbuf, sizeof(command_errbuf),
825ca987d46SWarner Losh 		  "can't find '%s'", modname);
82601cad731SEmmanuel Vadot 		TSEXIT();
827ca987d46SWarner Losh 		return (ENOENT);
828ca987d46SWarner Losh 	}
829ca987d46SWarner Losh 	err = mod_loadkld(filename, argc, argv);
830b17868a2SToomas Soome 	free(filename);
83101cad731SEmmanuel Vadot 	TSEXIT();
832ca987d46SWarner Losh 	return (err);
833ca987d46SWarner Losh }
834ca987d46SWarner Losh 
835ca987d46SWarner Losh /*
836ca987d46SWarner Losh  * Load specified KLD. If path is omitted, then try to locate it via
837ca987d46SWarner Losh  * search path.
838ca987d46SWarner Losh  */
839ca987d46SWarner Losh int
mod_loadkld(const char * kldname,int argc,char * argv[])840ca987d46SWarner Losh mod_loadkld(const char *kldname, int argc, char *argv[])
841ca987d46SWarner Losh {
842d7125bb1SToomas Soome 	struct preloaded_file	*fp;
843ca987d46SWarner Losh 	int			err;
844ca987d46SWarner Losh 	char			*filename;
845d7125bb1SToomas Soome 	vm_offset_t		loadaddr_saved;
846ca987d46SWarner Losh 
84701cad731SEmmanuel Vadot 	TSENTER2(kldname);
848ca987d46SWarner Losh 	/*
849ca987d46SWarner Losh 	 * Get fully qualified KLD name
850ca987d46SWarner Losh 	 */
851ca987d46SWarner Losh 	filename = file_search(kldname, kld_ext_list);
852ca987d46SWarner Losh 	if (filename == NULL) {
853ca987d46SWarner Losh 		snprintf(command_errbuf, sizeof(command_errbuf),
854ca987d46SWarner Losh 		  "can't find '%s'", kldname);
85501cad731SEmmanuel Vadot 		TSEXIT();
856ca987d46SWarner Losh 		return (ENOENT);
857ca987d46SWarner Losh 	}
858ca987d46SWarner Losh 	/*
859ca987d46SWarner Losh 	 * Check if KLD already loaded
860ca987d46SWarner Losh 	 */
861ca987d46SWarner Losh 	fp = file_findfile(filename, NULL);
862ca987d46SWarner Losh 	if (fp) {
863ca987d46SWarner Losh 		snprintf(command_errbuf, sizeof(command_errbuf),
864ca987d46SWarner Losh 		  "warning: KLD '%s' already loaded", filename);
865ca987d46SWarner Losh 		free(filename);
86601cad731SEmmanuel Vadot 		TSEXIT();
867ca987d46SWarner Losh 		return (0);
868ca987d46SWarner Losh 	}
869ca987d46SWarner Losh 
870ca987d46SWarner Losh 	do {
871ca987d46SWarner Losh 		err = file_load(filename, loadaddr, &fp);
872ca987d46SWarner Losh 		if (err)
873ca987d46SWarner Losh 			break;
874ca987d46SWarner Losh 		fp->f_args = unargv(argc, argv);
875d7125bb1SToomas Soome 		loadaddr_saved = loadaddr;
876ca987d46SWarner Losh 		loadaddr = fp->f_addr + fp->f_size;
877ca987d46SWarner Losh 		file_insert_tail(fp);	/* Add to the list of loaded files */
878ca987d46SWarner Losh 		if (file_load_dependencies(fp) != 0) {
879ca987d46SWarner Losh 			err = ENOENT;
8803da56871SKyle Evans 			file_remove_tail(fp);
881d7125bb1SToomas Soome 			loadaddr = loadaddr_saved;
882ca987d46SWarner Losh 			fp = NULL;
883ca987d46SWarner Losh 			break;
884ca987d46SWarner Losh 		}
885ca987d46SWarner Losh 	} while(0);
886ca987d46SWarner Losh 	if (err == EFTYPE) {
887ca987d46SWarner Losh 		snprintf(command_errbuf, sizeof(command_errbuf),
888ca987d46SWarner Losh 		  "don't know how to load module '%s'", filename);
889ca987d46SWarner Losh 	}
890b17868a2SToomas Soome 	if (err)
891ca987d46SWarner Losh 		file_discard(fp);
892ca987d46SWarner Losh 	free(filename);
89301cad731SEmmanuel Vadot 	TSEXIT();
894ca987d46SWarner Losh 	return (err);
895ca987d46SWarner Losh }
896ca987d46SWarner Losh 
897ca987d46SWarner Losh /*
898ca987d46SWarner Losh  * Find a file matching (name) and (type).
899ca987d46SWarner Losh  * NULL may be passed as a wildcard to either.
900ca987d46SWarner Losh  */
901ca987d46SWarner Losh struct preloaded_file *
file_findfile(const char * name,const char * type)902ca987d46SWarner Losh file_findfile(const char *name, const char *type)
903ca987d46SWarner Losh {
904ca987d46SWarner Losh 	struct preloaded_file *fp;
905ca987d46SWarner Losh 
906ca987d46SWarner Losh 	for (fp = preloaded_files; fp != NULL; fp = fp->f_next) {
907ca987d46SWarner Losh 		if (((name == NULL) || !strcmp(name, fp->f_name)) &&
908ca987d46SWarner Losh 		  ((type == NULL) || !strcmp(type, fp->f_type)))
909ca987d46SWarner Losh 			break;
910ca987d46SWarner Losh 	}
911ca987d46SWarner Losh 	return (fp);
912ca987d46SWarner Losh }
913ca987d46SWarner Losh 
914ca987d46SWarner Losh /*
915ca987d46SWarner Losh  * Find a module matching (name) inside of given file.
916ca987d46SWarner Losh  * NULL may be passed as a wildcard.
917ca987d46SWarner Losh  */
918ca987d46SWarner Losh struct kernel_module *
file_findmodule(struct preloaded_file * fp,char * modname,struct mod_depend * verinfo)919ca987d46SWarner Losh file_findmodule(struct preloaded_file *fp, char *modname,
920ca987d46SWarner Losh 	struct mod_depend *verinfo)
921ca987d46SWarner Losh {
922ca987d46SWarner Losh 	struct kernel_module *mp, *best;
923ca987d46SWarner Losh 	int bestver, mver;
924ca987d46SWarner Losh 
925ca987d46SWarner Losh 	if (fp == NULL) {
926ca987d46SWarner Losh 		for (fp = preloaded_files; fp; fp = fp->f_next) {
927ca987d46SWarner Losh 			mp = file_findmodule(fp, modname, verinfo);
928ca987d46SWarner Losh 			if (mp)
929ca987d46SWarner Losh 				return (mp);
930ca987d46SWarner Losh 		}
931ca987d46SWarner Losh 		return (NULL);
932ca987d46SWarner Losh 	}
933ca987d46SWarner Losh 	best = NULL;
934ca987d46SWarner Losh 	bestver = 0;
935ca987d46SWarner Losh 	for (mp = fp->f_modules; mp; mp = mp->m_next) {
936ca987d46SWarner Losh 		if (strcmp(modname, mp->m_name) == 0) {
937ca987d46SWarner Losh 			if (verinfo == NULL)
938ca987d46SWarner Losh 				return (mp);
939ca987d46SWarner Losh 			mver = mp->m_version;
940ca987d46SWarner Losh 			if (mver == verinfo->md_ver_preferred)
941ca987d46SWarner Losh 				return (mp);
942ca987d46SWarner Losh 			if (mver >= verinfo->md_ver_minimum &&
943ca987d46SWarner Losh 			  mver <= verinfo->md_ver_maximum &&
944ca987d46SWarner Losh 			  mver > bestver) {
945ca987d46SWarner Losh 				best = mp;
946ca987d46SWarner Losh 				bestver = mver;
947ca987d46SWarner Losh 			}
948ca987d46SWarner Losh 		}
949ca987d46SWarner Losh 	}
950ca987d46SWarner Losh 	return (best);
951ca987d46SWarner Losh }
952ca987d46SWarner Losh /*
953ca987d46SWarner Losh  * Make a copy of (size) bytes of data from (p), and associate them as
954ca987d46SWarner Losh  * metadata of (type) to the module (mp).
955ca987d46SWarner Losh  */
956ca987d46SWarner Losh void
file_addmetadata(struct preloaded_file * fp,int type,size_t size,void * p)957ca987d46SWarner Losh file_addmetadata(struct preloaded_file *fp, int type, size_t size, void *p)
958ca987d46SWarner Losh {
959ca987d46SWarner Losh 	struct file_metadata	*md;
960ca987d46SWarner Losh 
961ca987d46SWarner Losh 	md = malloc(sizeof(struct file_metadata) - sizeof(md->md_data) + size);
962d28c5946SToomas Soome 	if (md != NULL) {
963ca987d46SWarner Losh 		md->md_size = size;
964ca987d46SWarner Losh 		md->md_type = type;
965ca987d46SWarner Losh 		bcopy(p, md->md_data, size);
966ca987d46SWarner Losh 		md->md_next = fp->f_metadata;
967d28c5946SToomas Soome 	}
968ca987d46SWarner Losh 	fp->f_metadata = md;
969ca987d46SWarner Losh }
970ca987d46SWarner Losh 
971ca987d46SWarner Losh /*
972ca987d46SWarner Losh  * Find a metadata object of (type) associated with the file (fp)
973ca987d46SWarner Losh  */
974ca987d46SWarner Losh struct file_metadata *
file_findmetadata(struct preloaded_file * fp,int type)975ca987d46SWarner Losh file_findmetadata(struct preloaded_file *fp, int type)
976ca987d46SWarner Losh {
977ca987d46SWarner Losh 	struct file_metadata *md;
978ca987d46SWarner Losh 
979ca987d46SWarner Losh 	for (md = fp->f_metadata; md != NULL; md = md->md_next)
980ca987d46SWarner Losh 		if (md->md_type == type)
981ca987d46SWarner Losh 			break;
982ca987d46SWarner Losh 	return(md);
983ca987d46SWarner Losh }
984ca987d46SWarner Losh 
985ca987d46SWarner Losh /*
986ca987d46SWarner Losh  * Remove all metadata from the file.
987ca987d46SWarner Losh  */
988ca987d46SWarner Losh void
file_removemetadata(struct preloaded_file * fp)989ca987d46SWarner Losh file_removemetadata(struct preloaded_file *fp)
990ca987d46SWarner Losh {
991ca987d46SWarner Losh 	struct file_metadata *md, *next;
992ca987d46SWarner Losh 
993ca987d46SWarner Losh 	for (md = fp->f_metadata; md != NULL; md = next)
994ca987d46SWarner Losh 	{
995ca987d46SWarner Losh 		next = md->md_next;
996ca987d46SWarner Losh 		free(md);
997ca987d46SWarner Losh 	}
998ca987d46SWarner Losh 	fp->f_metadata = NULL;
999ca987d46SWarner Losh }
1000ca987d46SWarner Losh 
100160a978beSColin Percival /*
100260a978beSColin Percival  * Add a buffer to the list of preloaded "files".
100360a978beSColin Percival  */
100460a978beSColin Percival int
file_addbuf(const char * name,const char * type,size_t len,void * buf)100560a978beSColin Percival file_addbuf(const char *name, const char *type, size_t len, void *buf)
100660a978beSColin Percival {
100760a978beSColin Percival 	struct preloaded_file	*fp;
100860a978beSColin Percival 	vm_offset_t dest;
100960a978beSColin Percival 
101060a978beSColin Percival 	/* We can't load first */
101160a978beSColin Percival 	if ((file_findfile(NULL, NULL)) == NULL) {
101260a978beSColin Percival 		command_errmsg = "can't load file before kernel";
101360a978beSColin Percival 		return (-1);
101460a978beSColin Percival 	}
101560a978beSColin Percival 
101660a978beSColin Percival 	/* Figure out where to load the data. */
101760a978beSColin Percival 	dest = loadaddr;
101860a978beSColin Percival 	if (archsw.arch_loadaddr != NULL)
101960a978beSColin Percival 		dest = archsw.arch_loadaddr(LOAD_RAW, (void *)name, dest);
102060a978beSColin Percival 
102160a978beSColin Percival 	/* Create & populate control structure */
102260a978beSColin Percival 	fp = file_alloc();
102360a978beSColin Percival 	if (fp == NULL) {
102460a978beSColin Percival 		snprintf(command_errbuf, sizeof (command_errbuf),
102560a978beSColin Percival 		    "no memory to load %s", name);
102660a978beSColin Percival 		return (-1);
102760a978beSColin Percival 	}
102860a978beSColin Percival 	fp->f_name = strdup(name);
102960a978beSColin Percival 	fp->f_type = strdup(type);
103060a978beSColin Percival 	fp->f_args = NULL;
103160a978beSColin Percival 	fp->f_metadata = NULL;
103260a978beSColin Percival 	fp->f_loader = -1;
103360a978beSColin Percival 	fp->f_addr = dest;
103460a978beSColin Percival 	fp->f_size = len;
103560a978beSColin Percival 	if ((fp->f_name == NULL) || (fp->f_type == NULL)) {
103660a978beSColin Percival 		snprintf(command_errbuf, sizeof (command_errbuf),
103760a978beSColin Percival 		    "no memory to load %s", name);
103860a978beSColin Percival 		free(fp->f_name);
103960a978beSColin Percival 		free(fp->f_type);
104060a978beSColin Percival 		return (-1);
104160a978beSColin Percival 	}
104260a978beSColin Percival 
104360a978beSColin Percival 	/* Copy the data in. */
104460a978beSColin Percival 	archsw.arch_copyin(buf, fp->f_addr, len);
104560a978beSColin Percival 	loadaddr = fp->f_addr + len;
104660a978beSColin Percival 
104760a978beSColin Percival 	/* Add to the list of loaded files */
104860a978beSColin Percival 	file_insert_tail(fp);
104960a978beSColin Percival 	return(0);
105060a978beSColin Percival }
105160a978beSColin Percival 
1052ca987d46SWarner Losh struct file_metadata *
metadata_next(struct file_metadata * md,int type)1053ca987d46SWarner Losh metadata_next(struct file_metadata *md, int type)
1054ca987d46SWarner Losh {
105573a41ff8SEmmanuel Vadot 
1056ca987d46SWarner Losh 	if (md == NULL)
1057ca987d46SWarner Losh 		return (NULL);
1058ca987d46SWarner Losh 	while((md = md->md_next) != NULL)
1059ca987d46SWarner Losh 		if (md->md_type == type)
1060ca987d46SWarner Losh 			break;
1061ca987d46SWarner Losh 	return (md);
1062ca987d46SWarner Losh }
1063ca987d46SWarner Losh 
1064ca987d46SWarner Losh static char *emptyextlist[] = { "", NULL };
1065ca987d46SWarner Losh 
1066ca987d46SWarner Losh /*
1067ca987d46SWarner Losh  * Check if the given file is in place and return full path to it.
1068ca987d46SWarner Losh  */
1069ca987d46SWarner Losh static char *
file_lookup(const char * path,const char * name,int namelen,char ** extlist)1070ca987d46SWarner Losh file_lookup(const char *path, const char *name, int namelen, char **extlist)
1071ca987d46SWarner Losh {
1072ca987d46SWarner Losh 	struct stat	st;
1073ca987d46SWarner Losh 	char	*result, *cp, **cpp;
1074ca987d46SWarner Losh 	int		pathlen, extlen, len;
1075ca987d46SWarner Losh 
1076ca987d46SWarner Losh 	pathlen = strlen(path);
1077ca987d46SWarner Losh 	extlen = 0;
1078ca987d46SWarner Losh 	if (extlist == NULL)
1079ca987d46SWarner Losh 		extlist = emptyextlist;
1080ca987d46SWarner Losh 	for (cpp = extlist; *cpp; cpp++) {
1081ca987d46SWarner Losh 		len = strlen(*cpp);
1082ca987d46SWarner Losh 		if (len > extlen)
1083ca987d46SWarner Losh 			extlen = len;
1084ca987d46SWarner Losh 	}
1085ca987d46SWarner Losh 	result = malloc(pathlen + namelen + extlen + 2);
1086ca987d46SWarner Losh 	if (result == NULL)
1087ca987d46SWarner Losh 		return (NULL);
1088ca987d46SWarner Losh 	bcopy(path, result, pathlen);
1089ca987d46SWarner Losh 	if (pathlen > 0 && result[pathlen - 1] != '/')
1090ca987d46SWarner Losh 		result[pathlen++] = '/';
1091ca987d46SWarner Losh 	cp = result + pathlen;
1092ca987d46SWarner Losh 	bcopy(name, cp, namelen);
1093ca987d46SWarner Losh 	cp += namelen;
1094ca987d46SWarner Losh 	for (cpp = extlist; *cpp; cpp++) {
1095ca987d46SWarner Losh 		strcpy(cp, *cpp);
1096ca987d46SWarner Losh 		if (stat(result, &st) == 0 && S_ISREG(st.st_mode))
1097ca987d46SWarner Losh 			return result;
1098ca987d46SWarner Losh 	}
1099ca987d46SWarner Losh 	free(result);
1100ca987d46SWarner Losh 	return NULL;
1101ca987d46SWarner Losh }
1102ca987d46SWarner Losh 
1103ca987d46SWarner Losh /*
1104ca987d46SWarner Losh  * Check if file name have any qualifiers
1105ca987d46SWarner Losh  */
1106ca987d46SWarner Losh static int
file_havepath(const char * name)1107ca987d46SWarner Losh file_havepath(const char *name)
1108ca987d46SWarner Losh {
1109ca987d46SWarner Losh 	const char		*cp;
1110ca987d46SWarner Losh 
1111ca987d46SWarner Losh 	archsw.arch_getdev(NULL, name, &cp);
1112ca987d46SWarner Losh 	return (cp != name || strchr(name, '/') != NULL);
1113ca987d46SWarner Losh }
1114ca987d46SWarner Losh 
1115ca987d46SWarner Losh /*
1116ca987d46SWarner Losh  * Attempt to find the file (name) on the module searchpath.
1117ca987d46SWarner Losh  * If (name) is qualified in any way, we simply check it and
1118ca987d46SWarner Losh  * return it or NULL.  If it is not qualified, then we attempt
1119ca987d46SWarner Losh  * to construct a path using entries in the environment variable
1120ca987d46SWarner Losh  * module_path.
1121ca987d46SWarner Losh  *
1122ca987d46SWarner Losh  * The path we return a pointer to need never be freed, as we manage
1123ca987d46SWarner Losh  * it internally.
1124ca987d46SWarner Losh  */
1125ca987d46SWarner Losh static char *
file_search(const char * name,char ** extlist)1126ca987d46SWarner Losh file_search(const char *name, char **extlist)
1127ca987d46SWarner Losh {
1128ca987d46SWarner Losh 	struct moduledir	*mdp;
1129ca987d46SWarner Losh 	struct stat		sb;
1130ca987d46SWarner Losh 	char		*result;
1131ca987d46SWarner Losh 	int			namelen;
1132ca987d46SWarner Losh 
1133ca987d46SWarner Losh 	/* Don't look for nothing */
1134ca987d46SWarner Losh 	if (name == NULL)
1135ca987d46SWarner Losh 		return(NULL);
1136ca987d46SWarner Losh 
1137ca987d46SWarner Losh 	if (*name == 0)
1138ca987d46SWarner Losh 		return(strdup(name));
1139ca987d46SWarner Losh 
1140ca987d46SWarner Losh 	if (file_havepath(name)) {
1141ca987d46SWarner Losh 		/* Qualified, so just see if it exists */
1142ca987d46SWarner Losh 		if (stat(name, &sb) == 0)
1143ca987d46SWarner Losh 			return(strdup(name));
1144ca987d46SWarner Losh 		return(NULL);
1145ca987d46SWarner Losh 	}
1146ca987d46SWarner Losh 	moduledir_rebuild();
1147ca987d46SWarner Losh 	result = NULL;
1148ca987d46SWarner Losh 	namelen = strlen(name);
1149ca987d46SWarner Losh 	STAILQ_FOREACH(mdp, &moduledir_list, d_link) {
1150ca987d46SWarner Losh 		result = file_lookup(mdp->d_path, name, namelen, extlist);
1151ca987d46SWarner Losh 		if (result)
1152ca987d46SWarner Losh 			break;
1153ca987d46SWarner Losh 	}
1154ca987d46SWarner Losh 	return(result);
1155ca987d46SWarner Losh }
1156ca987d46SWarner Losh 
1157ca987d46SWarner Losh #define	INT_ALIGN(base, ptr)	ptr = \
1158ca987d46SWarner Losh 	(base) + roundup2((ptr) - (base), sizeof(int))
1159ca987d46SWarner Losh 
1160ca987d46SWarner Losh static char *
mod_search_hints(struct moduledir * mdp,const char * modname,struct mod_depend * verinfo)1161ca987d46SWarner Losh mod_search_hints(struct moduledir *mdp, const char *modname,
1162ca987d46SWarner Losh 	struct mod_depend *verinfo)
1163ca987d46SWarner Losh {
1164ca987d46SWarner Losh 	u_char	*cp, *recptr, *bufend, *best;
1165ca987d46SWarner Losh 	char	*result;
1166ca987d46SWarner Losh 	int		*intp, bestver, blen, clen, found, ival, modnamelen, reclen;
1167ca987d46SWarner Losh 
1168ca987d46SWarner Losh 	moduledir_readhints(mdp);
1169ca987d46SWarner Losh 	modnamelen = strlen(modname);
1170ca987d46SWarner Losh 	found = 0;
1171ca987d46SWarner Losh 	result = NULL;
1172ca987d46SWarner Losh 	bestver = 0;
1173ca987d46SWarner Losh 	if (mdp->d_hints == NULL)
1174ca987d46SWarner Losh 		goto bad;
1175ca987d46SWarner Losh 	recptr = mdp->d_hints;
1176ca987d46SWarner Losh 	bufend = recptr + mdp->d_hintsz;
1177ca987d46SWarner Losh 	clen = blen = 0;
1178ca987d46SWarner Losh 	best = cp = NULL;
1179ca987d46SWarner Losh 	while (recptr < bufend && !found) {
1180ca987d46SWarner Losh 		intp = (int*)recptr;
1181ca987d46SWarner Losh 		reclen = *intp++;
1182ca987d46SWarner Losh 		ival = *intp++;
1183ca987d46SWarner Losh 		cp = (u_char*)intp;
1184ca987d46SWarner Losh 		switch (ival) {
1185ca987d46SWarner Losh 		case MDT_VERSION:
1186ca987d46SWarner Losh 			clen = *cp++;
1187ca987d46SWarner Losh 			if (clen != modnamelen || bcmp(cp, modname, clen) != 0)
1188ca987d46SWarner Losh 				break;
1189ca987d46SWarner Losh 			cp += clen;
1190ca987d46SWarner Losh 			INT_ALIGN(mdp->d_hints, cp);
1191ca987d46SWarner Losh 			ival = *(int*)cp;
1192ca987d46SWarner Losh 			cp += sizeof(int);
1193ca987d46SWarner Losh 			clen = *cp++;
1194ca987d46SWarner Losh 			if (verinfo == NULL || ival == verinfo->md_ver_preferred) {
1195ca987d46SWarner Losh 				found = 1;
1196ca987d46SWarner Losh 				break;
1197ca987d46SWarner Losh 			}
1198ca987d46SWarner Losh 			if (ival >= verinfo->md_ver_minimum &&
1199ca987d46SWarner Losh 			  ival <= verinfo->md_ver_maximum &&
1200ca987d46SWarner Losh 			  ival > bestver) {
1201ca987d46SWarner Losh 				bestver = ival;
1202ca987d46SWarner Losh 				best = cp;
1203ca987d46SWarner Losh 				blen = clen;
1204ca987d46SWarner Losh 			}
1205ca987d46SWarner Losh 			break;
1206ca987d46SWarner Losh 		default:
1207ca987d46SWarner Losh 			break;
1208ca987d46SWarner Losh 		}
1209ca987d46SWarner Losh 		recptr += reclen + sizeof(int);
1210ca987d46SWarner Losh 	}
1211ca987d46SWarner Losh 	/*
1212ca987d46SWarner Losh 	 * Finally check if KLD is in the place
1213ca987d46SWarner Losh 	 */
1214ca987d46SWarner Losh 	if (found)
1215ca987d46SWarner Losh 		result = file_lookup(mdp->d_path, (const char *)cp, clen, NULL);
1216ca987d46SWarner Losh 	else if (best)
1217ca987d46SWarner Losh 		result = file_lookup(mdp->d_path, (const char *)best, blen, NULL);
1218ca987d46SWarner Losh bad:
1219ca987d46SWarner Losh 	/*
1220ca987d46SWarner Losh 	 * If nothing found or hints is absent - fallback to the old way
1221ca987d46SWarner Losh 	 * by using "kldname[.ko]" as module name.
1222ca987d46SWarner Losh 	 */
1223ca987d46SWarner Losh 	if (!found && !bestver && result == NULL)
1224ca987d46SWarner Losh 		result = file_lookup(mdp->d_path, modname, modnamelen, kld_ext_list);
1225ca987d46SWarner Losh 	return result;
1226ca987d46SWarner Losh }
1227ca987d46SWarner Losh 
122832199fb3SEmmanuel Vadot static int
getint(void ** ptr)122932199fb3SEmmanuel Vadot getint(void **ptr)
123032199fb3SEmmanuel Vadot {
123132199fb3SEmmanuel Vadot 	int *p = *ptr;
123232199fb3SEmmanuel Vadot 	int rv;
123332199fb3SEmmanuel Vadot 
123432199fb3SEmmanuel Vadot 	p = (int *)roundup2((intptr_t)p, sizeof(int));
123532199fb3SEmmanuel Vadot 	rv = *p++;
123632199fb3SEmmanuel Vadot 	*ptr = p;
123732199fb3SEmmanuel Vadot 	return rv;
123832199fb3SEmmanuel Vadot }
123932199fb3SEmmanuel Vadot 
124032199fb3SEmmanuel Vadot static void
getstr(void ** ptr,char * val)124132199fb3SEmmanuel Vadot getstr(void **ptr, char *val)
124232199fb3SEmmanuel Vadot {
124332199fb3SEmmanuel Vadot 	int *p = *ptr;
124432199fb3SEmmanuel Vadot 	char *c = (char *)p;
124532199fb3SEmmanuel Vadot 	int len = *(uint8_t *)c;
124632199fb3SEmmanuel Vadot 
124732199fb3SEmmanuel Vadot 	memcpy(val, c + 1, len);
124832199fb3SEmmanuel Vadot 	val[len] = 0;
124932199fb3SEmmanuel Vadot 	c += len + 1;
125032199fb3SEmmanuel Vadot 	*ptr = (void *)c;
125132199fb3SEmmanuel Vadot }
125232199fb3SEmmanuel Vadot 
125332199fb3SEmmanuel Vadot static int
pnpval_as_int(const char * val,const char * pnpinfo)125432199fb3SEmmanuel Vadot pnpval_as_int(const char *val, const char *pnpinfo)
125532199fb3SEmmanuel Vadot {
125632199fb3SEmmanuel Vadot 	int rv;
125732199fb3SEmmanuel Vadot 	char key[256];
125832199fb3SEmmanuel Vadot 	char *cp;
125932199fb3SEmmanuel Vadot 
126032199fb3SEmmanuel Vadot 	if (pnpinfo == NULL)
126132199fb3SEmmanuel Vadot 		return -1;
126232199fb3SEmmanuel Vadot 
126332199fb3SEmmanuel Vadot 	cp = strchr(val, ';');
126432199fb3SEmmanuel Vadot 	key[0] = ' ';
126532199fb3SEmmanuel Vadot 	if (cp == NULL)
126632199fb3SEmmanuel Vadot 		strlcpy(key + 1, val, sizeof(key) - 1);
126732199fb3SEmmanuel Vadot 	else {
126832199fb3SEmmanuel Vadot 		memcpy(key + 1, val, cp - val);
126932199fb3SEmmanuel Vadot 		key[cp - val + 1] = '\0';
127032199fb3SEmmanuel Vadot 	}
127132199fb3SEmmanuel Vadot 	strlcat(key, "=", sizeof(key));
127232199fb3SEmmanuel Vadot 	if (strncmp(key + 1, pnpinfo, strlen(key + 1)) == 0)
127332199fb3SEmmanuel Vadot 		rv = strtol(pnpinfo + strlen(key + 1), NULL, 0);
127432199fb3SEmmanuel Vadot 	else {
127532199fb3SEmmanuel Vadot 		cp = strstr(pnpinfo, key);
127632199fb3SEmmanuel Vadot 		if (cp == NULL)
127732199fb3SEmmanuel Vadot 			rv = -1;
127832199fb3SEmmanuel Vadot 		else
127932199fb3SEmmanuel Vadot 			rv = strtol(cp + strlen(key), NULL, 0);
128032199fb3SEmmanuel Vadot 	}
128132199fb3SEmmanuel Vadot 	return rv;
128232199fb3SEmmanuel Vadot }
128332199fb3SEmmanuel Vadot 
128432199fb3SEmmanuel Vadot static void
quoted_strcpy(char * dst,const char * src)128532199fb3SEmmanuel Vadot quoted_strcpy(char *dst, const char *src)
128632199fb3SEmmanuel Vadot {
128732199fb3SEmmanuel Vadot 	char q = ' ';
128832199fb3SEmmanuel Vadot 
128932199fb3SEmmanuel Vadot 	if (*src == '\'' || *src == '"')
129032199fb3SEmmanuel Vadot 		q = *src++;
129132199fb3SEmmanuel Vadot 	while (*src && *src != q)
129232199fb3SEmmanuel Vadot 		*dst++ = *src++; // XXX backtick quoting
129332199fb3SEmmanuel Vadot 	*dst++ = '\0';
129432199fb3SEmmanuel Vadot 	// XXX overflow
129532199fb3SEmmanuel Vadot }
129632199fb3SEmmanuel Vadot 
129732199fb3SEmmanuel Vadot static char *
pnpval_as_str(const char * val,const char * pnpinfo)129832199fb3SEmmanuel Vadot pnpval_as_str(const char *val, const char *pnpinfo)
129932199fb3SEmmanuel Vadot {
130032199fb3SEmmanuel Vadot 	static char retval[256];
130132199fb3SEmmanuel Vadot 	char key[256];
130232199fb3SEmmanuel Vadot 	char *cp;
130332199fb3SEmmanuel Vadot 
130432199fb3SEmmanuel Vadot 	if (pnpinfo == NULL) {
130532199fb3SEmmanuel Vadot 		*retval = '\0';
130632199fb3SEmmanuel Vadot 		return retval;
130732199fb3SEmmanuel Vadot 	}
130832199fb3SEmmanuel Vadot 
130932199fb3SEmmanuel Vadot 	cp = strchr(val, ';');
131032199fb3SEmmanuel Vadot 	key[0] = ' ';
131132199fb3SEmmanuel Vadot 	if (cp == NULL)
131232199fb3SEmmanuel Vadot 		strlcpy(key + 1, val, sizeof(key) - 1);
131332199fb3SEmmanuel Vadot 	else {
131432199fb3SEmmanuel Vadot 		memcpy(key + 1, val, cp - val);
131532199fb3SEmmanuel Vadot 		key[cp - val + 1] = '\0';
131632199fb3SEmmanuel Vadot 	}
131732199fb3SEmmanuel Vadot 	strlcat(key, "=", sizeof(key));
131832199fb3SEmmanuel Vadot 	if (strncmp(key + 1, pnpinfo, strlen(key + 1)) == 0)
131932199fb3SEmmanuel Vadot 		quoted_strcpy(retval, pnpinfo + strlen(key + 1));
132032199fb3SEmmanuel Vadot 	else {
132132199fb3SEmmanuel Vadot 		cp = strstr(pnpinfo, key);
132232199fb3SEmmanuel Vadot 		if (cp == NULL)
132332199fb3SEmmanuel Vadot 			strcpy(retval, "MISSING");
132432199fb3SEmmanuel Vadot 		else
132532199fb3SEmmanuel Vadot 			quoted_strcpy(retval, cp + strlen(key));
132632199fb3SEmmanuel Vadot 	}
132732199fb3SEmmanuel Vadot 	return retval;
132832199fb3SEmmanuel Vadot }
132932199fb3SEmmanuel Vadot 
133032199fb3SEmmanuel Vadot static char *
devmatch_search_hints(struct moduledir * mdp,const char * bus,const char * dev,const char * pnpinfo)133132199fb3SEmmanuel Vadot devmatch_search_hints(struct moduledir *mdp, const char *bus, const char *dev, const char *pnpinfo)
133232199fb3SEmmanuel Vadot {
133332199fb3SEmmanuel Vadot 	char val1[256], val2[256];
133432199fb3SEmmanuel Vadot 	int ival, len, ents, i, notme, mask, bit, v, found;
133532199fb3SEmmanuel Vadot 	void *ptr, *walker, *hints_end;
133632199fb3SEmmanuel Vadot 	char *lastmod = NULL, *cp, *s;
133732199fb3SEmmanuel Vadot 
133832199fb3SEmmanuel Vadot 	moduledir_readhints(mdp);
133932199fb3SEmmanuel Vadot 	found = 0;
134032199fb3SEmmanuel Vadot 	if (mdp->d_hints == NULL)
134132199fb3SEmmanuel Vadot 		goto bad;
134232199fb3SEmmanuel Vadot 	walker = mdp->d_hints;
134332199fb3SEmmanuel Vadot 	hints_end = walker + mdp->d_hintsz;
134432199fb3SEmmanuel Vadot 	while (walker < hints_end && !found) {
134532199fb3SEmmanuel Vadot 		len = getint(&walker);
134632199fb3SEmmanuel Vadot 		ival = getint(&walker);
134732199fb3SEmmanuel Vadot 		ptr = walker;
134832199fb3SEmmanuel Vadot 		switch (ival) {
134932199fb3SEmmanuel Vadot 		case MDT_VERSION:
135032199fb3SEmmanuel Vadot 			getstr(&ptr, val1);
135132199fb3SEmmanuel Vadot 			ival = getint(&ptr);
135232199fb3SEmmanuel Vadot 			getstr(&ptr, val2);
135332199fb3SEmmanuel Vadot 			if (pnp_dump_flag || pnp_verbose_flag)
135432199fb3SEmmanuel Vadot 				printf("Version: if %s.%d kmod %s\n", val1, ival, val2);
135532199fb3SEmmanuel Vadot 			break;
135632199fb3SEmmanuel Vadot 		case MDT_MODULE:
135732199fb3SEmmanuel Vadot 			getstr(&ptr, val1);
135832199fb3SEmmanuel Vadot 			getstr(&ptr, val2);
135932199fb3SEmmanuel Vadot 			if (lastmod)
136032199fb3SEmmanuel Vadot 				free(lastmod);
136132199fb3SEmmanuel Vadot 			lastmod = strdup(val2);
136232199fb3SEmmanuel Vadot 			if (pnp_dump_flag || pnp_verbose_flag)
136332199fb3SEmmanuel Vadot 				printf("module %s in %s\n", val1, val1);
136432199fb3SEmmanuel Vadot 			break;
136532199fb3SEmmanuel Vadot 		case MDT_PNP_INFO:
136632199fb3SEmmanuel Vadot 			if (!pnp_dump_flag && !pnp_unbound_flag && lastmod && strcmp(lastmod, "kernel") == 0)
136732199fb3SEmmanuel Vadot 				break;
136832199fb3SEmmanuel Vadot 			getstr(&ptr, val1);
136932199fb3SEmmanuel Vadot 			getstr(&ptr, val2);
137032199fb3SEmmanuel Vadot 			ents = getint(&ptr);
137132199fb3SEmmanuel Vadot 			if (pnp_dump_flag || pnp_verbose_flag)
137232199fb3SEmmanuel Vadot 				printf("PNP info for bus %s format %s %d entries (%s)\n",
137332199fb3SEmmanuel Vadot 				    val1, val2, ents, lastmod);
137432199fb3SEmmanuel Vadot 			if (strcmp(val1, "usb") == 0) {
137532199fb3SEmmanuel Vadot 				if (pnp_verbose_flag)
137632199fb3SEmmanuel Vadot 					printf("Treating usb as uhub -- bug in source table still?\n");
137732199fb3SEmmanuel Vadot 				strcpy(val1, "uhub");
137832199fb3SEmmanuel Vadot 			}
137932199fb3SEmmanuel Vadot 			if (bus && strcmp(val1, bus) != 0) {
138032199fb3SEmmanuel Vadot 				if (pnp_verbose_flag)
138132199fb3SEmmanuel Vadot 					printf("Skipped because table for bus %s, looking for %s\n",
138232199fb3SEmmanuel Vadot 					    val1, bus);
138332199fb3SEmmanuel Vadot 				break;
138432199fb3SEmmanuel Vadot 			}
138532199fb3SEmmanuel Vadot 			for (i = 0; i < ents; i++) {
138632199fb3SEmmanuel Vadot 				if (pnp_verbose_flag)
138732199fb3SEmmanuel Vadot 					printf("---------- Entry %d ----------\n", i);
138832199fb3SEmmanuel Vadot 				if (pnp_dump_flag)
138932199fb3SEmmanuel Vadot 					printf("   ");
139032199fb3SEmmanuel Vadot 				cp = val2;
139132199fb3SEmmanuel Vadot 				notme = 0;
139232199fb3SEmmanuel Vadot 				mask = -1;
139332199fb3SEmmanuel Vadot 				bit = -1;
139432199fb3SEmmanuel Vadot 				do {
139532199fb3SEmmanuel Vadot 					switch (*cp) {
139632199fb3SEmmanuel Vadot 						/* All integer fields */
139732199fb3SEmmanuel Vadot 					case 'I':
139832199fb3SEmmanuel Vadot 					case 'J':
139932199fb3SEmmanuel Vadot 					case 'G':
140032199fb3SEmmanuel Vadot 					case 'L':
140132199fb3SEmmanuel Vadot 					case 'M':
140232199fb3SEmmanuel Vadot 						ival = getint(&ptr);
140332199fb3SEmmanuel Vadot 						if (pnp_dump_flag) {
140432199fb3SEmmanuel Vadot 							printf("%#x:", ival);
140532199fb3SEmmanuel Vadot 							break;
140632199fb3SEmmanuel Vadot 						}
140732199fb3SEmmanuel Vadot 						if (bit >= 0 && ((1 << bit) & mask) == 0)
140832199fb3SEmmanuel Vadot 							break;
140932199fb3SEmmanuel Vadot 						v = pnpval_as_int(cp + 2, pnpinfo);
141032199fb3SEmmanuel Vadot 						if (pnp_verbose_flag)
141132199fb3SEmmanuel Vadot 							printf("Matching %s (%c) table=%#x tomatch=%#x\n",
141232199fb3SEmmanuel Vadot 							    cp + 2, *cp, v, ival);
141332199fb3SEmmanuel Vadot 						switch (*cp) {
141432199fb3SEmmanuel Vadot 						case 'J':
141532199fb3SEmmanuel Vadot 							if (ival == -1)
141632199fb3SEmmanuel Vadot 								break;
141732199fb3SEmmanuel Vadot 							/*FALLTHROUGH*/
141832199fb3SEmmanuel Vadot 						case 'I':
141932199fb3SEmmanuel Vadot 							if (v != ival)
142032199fb3SEmmanuel Vadot 								notme++;
142132199fb3SEmmanuel Vadot 							break;
142232199fb3SEmmanuel Vadot 						case 'G':
142332199fb3SEmmanuel Vadot 							if (v < ival)
142432199fb3SEmmanuel Vadot 								notme++;
142532199fb3SEmmanuel Vadot 							break;
142632199fb3SEmmanuel Vadot 						case 'L':
142732199fb3SEmmanuel Vadot 							if (v > ival)
142832199fb3SEmmanuel Vadot 								notme++;
142932199fb3SEmmanuel Vadot 							break;
143032199fb3SEmmanuel Vadot 						case 'M':
143132199fb3SEmmanuel Vadot 							mask = ival;
143232199fb3SEmmanuel Vadot 							break;
143332199fb3SEmmanuel Vadot 						}
143432199fb3SEmmanuel Vadot 						break;
143532199fb3SEmmanuel Vadot 						/* String fields */
143632199fb3SEmmanuel Vadot 					case 'D':
143732199fb3SEmmanuel Vadot 					case 'Z':
143832199fb3SEmmanuel Vadot 						getstr(&ptr, val1);
143932199fb3SEmmanuel Vadot 						if (pnp_dump_flag) {
144032199fb3SEmmanuel Vadot 							printf("'%s':", val1);
144132199fb3SEmmanuel Vadot 							break;
144232199fb3SEmmanuel Vadot 						}
144332199fb3SEmmanuel Vadot 						if (*cp == 'D')
144432199fb3SEmmanuel Vadot 							break;
144532199fb3SEmmanuel Vadot 						s = pnpval_as_str(cp + 2, pnpinfo);
144632199fb3SEmmanuel Vadot 						if (strcmp(s, val1) != 0)
144732199fb3SEmmanuel Vadot 							notme++;
144832199fb3SEmmanuel Vadot 						break;
144932199fb3SEmmanuel Vadot 						/* Key override fields, required to be last in the string */
145032199fb3SEmmanuel Vadot 					case 'T':
145132199fb3SEmmanuel Vadot 						/*
145232199fb3SEmmanuel Vadot 						 * This is imperfect and only does one key and will be redone
145332199fb3SEmmanuel Vadot 						 * to be more general for multiple keys. Currently, nothing
145432199fb3SEmmanuel Vadot 						 * does that.
145532199fb3SEmmanuel Vadot 						 */
145632199fb3SEmmanuel Vadot 						if (pnp_dump_flag)				/* No per-row data stored */
145732199fb3SEmmanuel Vadot 							break;
145832199fb3SEmmanuel Vadot 						if (cp[strlen(cp) - 1] == ';')		/* Skip required ; at end */
145932199fb3SEmmanuel Vadot 							cp[strlen(cp) - 1] = '\0';	/* in case it's not there */
146032199fb3SEmmanuel Vadot 						if ((s = strstr(pnpinfo, cp + 2)) == NULL)
146132199fb3SEmmanuel Vadot 							notme++;
146232199fb3SEmmanuel Vadot 						else if (s > pnpinfo && s[-1] != ' ')
146332199fb3SEmmanuel Vadot 							notme++;
146432199fb3SEmmanuel Vadot 						break;
146532199fb3SEmmanuel Vadot 					default:
146632199fb3SEmmanuel Vadot 						printf("Unknown field type %c\n:", *cp);
146732199fb3SEmmanuel Vadot 						break;
146832199fb3SEmmanuel Vadot 					}
146932199fb3SEmmanuel Vadot 					bit++;
147032199fb3SEmmanuel Vadot 					cp = strchr(cp, ';');
147132199fb3SEmmanuel Vadot 					if (cp)
147232199fb3SEmmanuel Vadot 						cp++;
147332199fb3SEmmanuel Vadot 				} while (cp && *cp);
147432199fb3SEmmanuel Vadot 				if (pnp_dump_flag)
147532199fb3SEmmanuel Vadot 					printf("\n");
147632199fb3SEmmanuel Vadot 				else if (!notme) {
147732199fb3SEmmanuel Vadot 					if (!pnp_unbound_flag) {
147832199fb3SEmmanuel Vadot 						if (pnp_verbose_flag)
147932199fb3SEmmanuel Vadot 							printf("Matches --- %s ---\n", lastmod);
148032199fb3SEmmanuel Vadot 					}
148132199fb3SEmmanuel Vadot 					found++;
148232199fb3SEmmanuel Vadot 				}
148332199fb3SEmmanuel Vadot 			}
148432199fb3SEmmanuel Vadot 			break;
148532199fb3SEmmanuel Vadot 		default:
148632199fb3SEmmanuel Vadot 			break;
148732199fb3SEmmanuel Vadot 		}
148832199fb3SEmmanuel Vadot 		walker = (void *)(len - sizeof(int) + (intptr_t)walker);
148932199fb3SEmmanuel Vadot 	}
149032199fb3SEmmanuel Vadot 	if (pnp_unbound_flag && found == 0 && *pnpinfo) {
149132199fb3SEmmanuel Vadot 		if (pnp_verbose_flag)
149232199fb3SEmmanuel Vadot 			printf("------------------------- ");
149332199fb3SEmmanuel Vadot 		printf("%s on %s pnpinfo %s", *dev ? dev : "unattached", bus, pnpinfo);
149432199fb3SEmmanuel Vadot 		if (pnp_verbose_flag)
149532199fb3SEmmanuel Vadot 			printf(" -------------------------");
149632199fb3SEmmanuel Vadot 		printf("\n");
149732199fb3SEmmanuel Vadot 	}
149832199fb3SEmmanuel Vadot 	if (found != 0)
149932199fb3SEmmanuel Vadot 		return (lastmod);
150032199fb3SEmmanuel Vadot 	free(lastmod);
150132199fb3SEmmanuel Vadot 
150232199fb3SEmmanuel Vadot bad:
150332199fb3SEmmanuel Vadot 	return (NULL);
150432199fb3SEmmanuel Vadot }
150532199fb3SEmmanuel Vadot 
1506ca987d46SWarner Losh /*
1507ca987d46SWarner Losh  * Attempt to locate the file containing the module (name)
1508ca987d46SWarner Losh  */
1509ca987d46SWarner Losh static char *
mod_searchmodule(char * name,struct mod_depend * verinfo)1510ca987d46SWarner Losh mod_searchmodule(char *name, struct mod_depend *verinfo)
1511ca987d46SWarner Losh {
1512ca987d46SWarner Losh 	struct	moduledir *mdp;
1513ca987d46SWarner Losh 	char	*result;
1514ca987d46SWarner Losh 
1515ca987d46SWarner Losh 	moduledir_rebuild();
1516ca987d46SWarner Losh 	/*
1517ca987d46SWarner Losh 	 * Now we ready to lookup module in the given directories
1518ca987d46SWarner Losh 	 */
1519ca987d46SWarner Losh 	result = NULL;
1520ca987d46SWarner Losh 	STAILQ_FOREACH(mdp, &moduledir_list, d_link) {
1521ca987d46SWarner Losh 		result = mod_search_hints(mdp, name, verinfo);
1522ca987d46SWarner Losh 		if (result)
1523ca987d46SWarner Losh 			break;
1524ca987d46SWarner Losh 	}
1525ca987d46SWarner Losh 
1526ca987d46SWarner Losh 	return(result);
1527ca987d46SWarner Losh }
1528ca987d46SWarner Losh 
152932199fb3SEmmanuel Vadot static char *
mod_searchmodule_pnpinfo(const char * bus,const char * pnpinfo)153032199fb3SEmmanuel Vadot mod_searchmodule_pnpinfo(const char *bus, const char *pnpinfo)
153132199fb3SEmmanuel Vadot {
153232199fb3SEmmanuel Vadot 	struct	moduledir *mdp;
153332199fb3SEmmanuel Vadot 	char	*result;
153432199fb3SEmmanuel Vadot 
153532199fb3SEmmanuel Vadot 	moduledir_rebuild();
153632199fb3SEmmanuel Vadot 	/*
153732199fb3SEmmanuel Vadot 	 * Now we ready to lookup module in the given directories
153832199fb3SEmmanuel Vadot 	 */
153932199fb3SEmmanuel Vadot 	result = NULL;
154032199fb3SEmmanuel Vadot 	STAILQ_FOREACH(mdp, &moduledir_list, d_link) {
154132199fb3SEmmanuel Vadot 		result = devmatch_search_hints(mdp, bus, NULL, pnpinfo);
154232199fb3SEmmanuel Vadot 		if (result)
154332199fb3SEmmanuel Vadot 			break;
154432199fb3SEmmanuel Vadot 	}
154532199fb3SEmmanuel Vadot 
154632199fb3SEmmanuel Vadot 	return(result);
154732199fb3SEmmanuel Vadot }
154832199fb3SEmmanuel Vadot 
1549ca987d46SWarner Losh int
file_addmodule(struct preloaded_file * fp,char * modname,int version,struct kernel_module ** newmp)1550ca987d46SWarner Losh file_addmodule(struct preloaded_file *fp, char *modname, int version,
1551ca987d46SWarner Losh 	struct kernel_module **newmp)
1552ca987d46SWarner Losh {
1553ca987d46SWarner Losh 	struct kernel_module *mp;
1554ca987d46SWarner Losh 	struct mod_depend mdepend;
1555ca987d46SWarner Losh 
1556ca987d46SWarner Losh 	bzero(&mdepend, sizeof(mdepend));
1557ca987d46SWarner Losh 	mdepend.md_ver_preferred = version;
1558ca987d46SWarner Losh 	mp = file_findmodule(fp, modname, &mdepend);
1559ca987d46SWarner Losh 	if (mp)
1560ca987d46SWarner Losh 		return (EEXIST);
1561cef2742eSToomas Soome 	mp = calloc(1, sizeof(struct kernel_module));
1562ca987d46SWarner Losh 	if (mp == NULL)
1563ca987d46SWarner Losh 		return (ENOMEM);
1564ca987d46SWarner Losh 	mp->m_name = strdup(modname);
1565db7599e9SToomas Soome 	if (mp->m_name == NULL) {
1566db7599e9SToomas Soome 		free(mp);
1567db7599e9SToomas Soome 		return (ENOMEM);
1568db7599e9SToomas Soome 	}
1569ca987d46SWarner Losh 	mp->m_version = version;
1570ca987d46SWarner Losh 	mp->m_fp = fp;
1571ca987d46SWarner Losh 	mp->m_next = fp->f_modules;
1572ca987d46SWarner Losh 	fp->f_modules = mp;
1573ca987d46SWarner Losh 	if (newmp)
1574ca987d46SWarner Losh 		*newmp = mp;
1575ca987d46SWarner Losh 	return (0);
1576ca987d46SWarner Losh }
1577ca987d46SWarner Losh 
1578ca987d46SWarner Losh /*
1579ca987d46SWarner Losh  * Throw a file away
1580ca987d46SWarner Losh  */
1581ca987d46SWarner Losh void
file_discard(struct preloaded_file * fp)1582ca987d46SWarner Losh file_discard(struct preloaded_file *fp)
1583ca987d46SWarner Losh {
1584ca987d46SWarner Losh 	struct file_metadata	*md, *md1;
1585ca987d46SWarner Losh 	struct kernel_module	*mp, *mp1;
1586ca987d46SWarner Losh 	if (fp == NULL)
1587ca987d46SWarner Losh 		return;
1588ca987d46SWarner Losh 	md = fp->f_metadata;
1589ca987d46SWarner Losh 	while (md) {
1590ca987d46SWarner Losh 		md1 = md;
1591ca987d46SWarner Losh 		md = md->md_next;
1592ca987d46SWarner Losh 		free(md1);
1593ca987d46SWarner Losh 	}
1594ca987d46SWarner Losh 	mp = fp->f_modules;
1595ca987d46SWarner Losh 	while (mp) {
1596ca987d46SWarner Losh 		free(mp->m_name);
1597ca987d46SWarner Losh 		mp1 = mp;
1598ca987d46SWarner Losh 		mp = mp->m_next;
1599ca987d46SWarner Losh 		free(mp1);
1600ca987d46SWarner Losh 	}
1601ca987d46SWarner Losh 	free(fp->f_name);
1602ca987d46SWarner Losh 	free(fp->f_type);
1603ca987d46SWarner Losh 	free(fp->f_args);
1604ca987d46SWarner Losh 	free(fp);
1605ca987d46SWarner Losh }
1606ca987d46SWarner Losh 
1607ca987d46SWarner Losh /*
1608ca987d46SWarner Losh  * Allocate a new file; must be used instead of malloc()
1609ca987d46SWarner Losh  * to ensure safe initialisation.
1610ca987d46SWarner Losh  */
1611ca987d46SWarner Losh struct preloaded_file *
file_alloc(void)1612ca987d46SWarner Losh file_alloc(void)
1613ca987d46SWarner Losh {
1614ca987d46SWarner Losh 
1615cef2742eSToomas Soome 	return (calloc(1, sizeof(struct preloaded_file)));
1616ca987d46SWarner Losh }
1617ca987d46SWarner Losh 
1618ca987d46SWarner Losh /*
1619ca987d46SWarner Losh  * Add a module to the chain
1620ca987d46SWarner Losh  */
1621ca987d46SWarner Losh static void
file_insert_tail(struct preloaded_file * fp)1622ca987d46SWarner Losh file_insert_tail(struct preloaded_file *fp)
1623ca987d46SWarner Losh {
1624ca987d46SWarner Losh 	struct preloaded_file	*cm;
1625ca987d46SWarner Losh 
1626ca987d46SWarner Losh 	/* Append to list of loaded file */
1627ca987d46SWarner Losh 	fp->f_next = NULL;
1628ca987d46SWarner Losh 	if (preloaded_files == NULL) {
1629ca987d46SWarner Losh 		preloaded_files = fp;
1630ca987d46SWarner Losh 	} else {
1631ca987d46SWarner Losh 		for (cm = preloaded_files; cm->f_next != NULL; cm = cm->f_next)
1632ca987d46SWarner Losh 			;
1633ca987d46SWarner Losh 		cm->f_next = fp;
1634ca987d46SWarner Losh 	}
1635ca987d46SWarner Losh }
1636ca987d46SWarner Losh 
1637ed792026SToomas Soome /*
1638ed792026SToomas Soome  * Remove module from the chain
1639ed792026SToomas Soome  */
1640ed792026SToomas Soome static void
file_remove_impl(struct preloaded_file * fp,bool keep_tail)16413da56871SKyle Evans file_remove_impl(struct preloaded_file *fp, bool keep_tail)
1642ed792026SToomas Soome {
16433da56871SKyle Evans 	struct preloaded_file   *cm, *next;
1644ed792026SToomas Soome 
1645ed792026SToomas Soome 	if (preloaded_files == NULL)
1646ed792026SToomas Soome 		return;
1647ed792026SToomas Soome 
16483da56871SKyle Evans 	if (keep_tail)
16493da56871SKyle Evans 		next = fp->f_next;
16503da56871SKyle Evans 	else
16513da56871SKyle Evans 		next = NULL;
16523da56871SKyle Evans 
1653ed792026SToomas Soome 	if (preloaded_files == fp) {
16543da56871SKyle Evans 		preloaded_files = next;
1655ed792026SToomas Soome 		return;
1656ed792026SToomas Soome         }
16573da56871SKyle Evans 
1658ed792026SToomas Soome         for (cm = preloaded_files; cm->f_next != NULL; cm = cm->f_next) {
1659ed792026SToomas Soome 		if (cm->f_next == fp) {
16603da56871SKyle Evans 			cm->f_next = next;
1661ed792026SToomas Soome 			return;
1662ed792026SToomas Soome 		}
1663ed792026SToomas Soome 	}
1664ed792026SToomas Soome }
1665ed792026SToomas Soome 
16663da56871SKyle Evans static void
file_remove(struct preloaded_file * fp)16673da56871SKyle Evans file_remove(struct preloaded_file *fp)
16683da56871SKyle Evans {
16693da56871SKyle Evans 
16703da56871SKyle Evans 	file_remove_impl(fp, true);
16713da56871SKyle Evans }
16723da56871SKyle Evans 
16733da56871SKyle Evans static void
file_remove_tail(struct preloaded_file * fp)16743da56871SKyle Evans file_remove_tail(struct preloaded_file *fp)
16753da56871SKyle Evans {
16763da56871SKyle Evans 
16773da56871SKyle Evans 	file_remove_impl(fp, false);
16783da56871SKyle Evans }
16793da56871SKyle Evans 
1680ca987d46SWarner Losh static char *
moduledir_fullpath(struct moduledir * mdp,const char * fname)1681ca987d46SWarner Losh moduledir_fullpath(struct moduledir *mdp, const char *fname)
1682ca987d46SWarner Losh {
1683ca987d46SWarner Losh 	char *cp;
1684ca987d46SWarner Losh 
1685ca987d46SWarner Losh 	cp = malloc(strlen(mdp->d_path) + strlen(fname) + 2);
1686ca987d46SWarner Losh 	if (cp == NULL)
1687ca987d46SWarner Losh 		return NULL;
1688ca987d46SWarner Losh 	strcpy(cp, mdp->d_path);
1689ca987d46SWarner Losh 	strcat(cp, "/");
1690ca987d46SWarner Losh 	strcat(cp, fname);
1691ca987d46SWarner Losh 	return (cp);
1692ca987d46SWarner Losh }
1693ca987d46SWarner Losh 
1694ca987d46SWarner Losh /*
1695ca987d46SWarner Losh  * Read linker.hints file into memory performing some sanity checks.
1696ca987d46SWarner Losh  */
1697ca987d46SWarner Losh static void
moduledir_readhints(struct moduledir * mdp)1698ca987d46SWarner Losh moduledir_readhints(struct moduledir *mdp)
1699ca987d46SWarner Losh {
1700ca987d46SWarner Losh 	struct stat	st;
1701ca987d46SWarner Losh 	char	*path;
1702ca987d46SWarner Losh 	int		fd, size, version;
1703ca987d46SWarner Losh 
1704ca987d46SWarner Losh 	if (mdp->d_hints != NULL || (mdp->d_flags & MDIR_NOHINTS))
1705ca987d46SWarner Losh 		return;
1706ca987d46SWarner Losh 	path = moduledir_fullpath(mdp, "linker.hints");
1707ca987d46SWarner Losh 	if (stat(path, &st) != 0 ||
1708ca987d46SWarner Losh 	  st.st_size < (ssize_t)(sizeof(version) + sizeof(int)) ||
1709ca987d46SWarner Losh 	  st.st_size > LINKER_HINTS_MAX || (fd = open(path, O_RDONLY)) < 0) {
1710ca987d46SWarner Losh 		free(path);
1711ca987d46SWarner Losh 		mdp->d_flags |= MDIR_NOHINTS;
1712ca987d46SWarner Losh 		return;
1713ca987d46SWarner Losh 	}
1714ca987d46SWarner Losh 	free(path);
1715ca987d46SWarner Losh 	size = read(fd, &version, sizeof(version));
1716ca987d46SWarner Losh 	if (size != sizeof(version) || version != LINKER_HINTS_VERSION)
1717ca987d46SWarner Losh 		goto bad;
1718ca987d46SWarner Losh 	size = st.st_size - size;
1719ca987d46SWarner Losh 	mdp->d_hints = malloc(size);
1720ca987d46SWarner Losh 	if (mdp->d_hints == NULL)
1721ca987d46SWarner Losh 		goto bad;
1722ca987d46SWarner Losh 	if (read(fd, mdp->d_hints, size) != size)
1723ca987d46SWarner Losh 		goto bad;
1724ca987d46SWarner Losh 	mdp->d_hintsz = size;
1725ca987d46SWarner Losh 	close(fd);
1726ca987d46SWarner Losh 	return;
1727ca987d46SWarner Losh bad:
1728ca987d46SWarner Losh 	close(fd);
1729ca987d46SWarner Losh 	free(mdp->d_hints);
1730ca987d46SWarner Losh 	mdp->d_hints = NULL;
1731ca987d46SWarner Losh 	mdp->d_flags |= MDIR_NOHINTS;
1732ca987d46SWarner Losh 	return;
1733ca987d46SWarner Losh }
1734ca987d46SWarner Losh 
1735ca987d46SWarner Losh /*
1736ca987d46SWarner Losh  * Extract directories from the ';' separated list, remove duplicates.
1737ca987d46SWarner Losh  */
1738ca987d46SWarner Losh static void
moduledir_rebuild(void)1739ca987d46SWarner Losh moduledir_rebuild(void)
1740ca987d46SWarner Losh {
1741ca987d46SWarner Losh 	struct	moduledir *mdp, *mtmp;
1742ca987d46SWarner Losh 	const char	*path, *cp, *ep;
1743ca987d46SWarner Losh 	size_t	cplen;
1744ca987d46SWarner Losh 
1745ca987d46SWarner Losh 	path = getenv("module_path");
1746ca987d46SWarner Losh 	if (path == NULL)
1747ca987d46SWarner Losh 		path = default_searchpath;
1748ca987d46SWarner Losh 	/*
1749ca987d46SWarner Losh 	 * Rebuild list of module directories if it changed
1750ca987d46SWarner Losh 	 */
1751ca987d46SWarner Losh 	STAILQ_FOREACH(mdp, &moduledir_list, d_link)
1752ca987d46SWarner Losh 		mdp->d_flags |= MDIR_REMOVED;
1753ca987d46SWarner Losh 
1754ca987d46SWarner Losh 	for (ep = path; *ep != 0;  ep++) {
1755ca987d46SWarner Losh 		cp = ep;
1756ca987d46SWarner Losh 		for (; *ep != 0 && *ep != ';'; ep++)
1757ca987d46SWarner Losh 			;
1758ca987d46SWarner Losh 		/*
1759ca987d46SWarner Losh 		 * Ignore trailing slashes
1760ca987d46SWarner Losh 		 */
1761ca987d46SWarner Losh 		for (cplen = ep - cp; cplen > 1 && cp[cplen - 1] == '/'; cplen--)
1762ca987d46SWarner Losh 			;
1763ca987d46SWarner Losh 		STAILQ_FOREACH(mdp, &moduledir_list, d_link) {
1764ca987d46SWarner Losh 			if (strlen(mdp->d_path) != cplen ||	bcmp(cp, mdp->d_path, cplen) != 0)
1765ca987d46SWarner Losh 				continue;
1766ca987d46SWarner Losh 			mdp->d_flags &= ~MDIR_REMOVED;
1767ca987d46SWarner Losh 			break;
1768ca987d46SWarner Losh 		}
1769ca987d46SWarner Losh 		if (mdp == NULL) {
1770ca987d46SWarner Losh 			mdp = malloc(sizeof(*mdp) + cplen + 1);
1771ca987d46SWarner Losh 			if (mdp == NULL)
1772ca987d46SWarner Losh 				return;
1773ca987d46SWarner Losh 			mdp->d_path = (char*)(mdp + 1);
1774ca987d46SWarner Losh 			bcopy(cp, mdp->d_path, cplen);
1775ca987d46SWarner Losh 			mdp->d_path[cplen] = 0;
1776ca987d46SWarner Losh 			mdp->d_hints = NULL;
1777ca987d46SWarner Losh 			mdp->d_flags = 0;
1778ca987d46SWarner Losh 			STAILQ_INSERT_TAIL(&moduledir_list, mdp, d_link);
1779ca987d46SWarner Losh 		}
1780ca987d46SWarner Losh 		if (*ep == 0)
1781ca987d46SWarner Losh 			break;
1782ca987d46SWarner Losh 	}
1783ca987d46SWarner Losh 	/*
1784ca987d46SWarner Losh 	 * Delete unused directories if any
1785ca987d46SWarner Losh 	 */
1786ca987d46SWarner Losh 	mdp = STAILQ_FIRST(&moduledir_list);
1787ca987d46SWarner Losh 	while (mdp) {
1788ca987d46SWarner Losh 		if ((mdp->d_flags & MDIR_REMOVED) == 0) {
1789ca987d46SWarner Losh 			mdp = STAILQ_NEXT(mdp, d_link);
1790ca987d46SWarner Losh 		} else {
1791ca987d46SWarner Losh 			free(mdp->d_hints);
1792ca987d46SWarner Losh 			mtmp = mdp;
1793ca987d46SWarner Losh 			mdp = STAILQ_NEXT(mdp, d_link);
1794ca987d46SWarner Losh 			STAILQ_REMOVE(&moduledir_list, mtmp, moduledir, d_link);
1795ca987d46SWarner Losh 			free(mtmp);
1796ca987d46SWarner Losh 		}
1797ca987d46SWarner Losh 	}
1798ca987d46SWarner Losh 	return;
1799ca987d46SWarner Losh }
1800