xref: /illumos-gate/usr/src/boot/common/module.c (revision b70bf3ee79d81df3ccc36e553e0ff11049a2b51a)
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 
29 /*
30  * file/module function dispatcher, support, etc.
31  */
32 
33 #include <stand.h>
34 #include <string.h>
35 #include <sys/param.h>
36 #include <sys/linker.h>
37 #include <sys/module.h>
38 #include <sys/queue.h>
39 #include <sys/stdint.h>
40 #include <sys/tem_impl.h>
41 #include <sys/font.h>
42 #include <sys/sha1.h>
43 #include <libcrypto.h>
44 
45 #include "bootstrap.h"
46 
47 #if defined(EFI)
48 #define	PTOV(pa)	((void *)pa)
49 #else
50 #include "../i386/btx/lib/btxv86.h"
51 #endif
52 
53 #define	MDIR_REMOVED	0x0001
54 #define	MDIR_NOHINTS	0x0002
55 
56 struct moduledir {
57 	char	*d_path;	/* path of modules directory */
58 	uchar_t	*d_hints;	/* content of linker.hints file */
59 	int	d_hintsz;	/* size of hints data */
60 	int	d_flags;
61 	STAILQ_ENTRY(moduledir) d_link;
62 };
63 
64 static int file_load(char *, vm_offset_t, struct preloaded_file **);
65 static int file_load_dependencies(struct preloaded_file *);
66 static char *file_search(const char *, const char **);
67 static struct kernel_module *file_findmodule(struct preloaded_file *, char *,
68     struct mod_depend *);
69 static int file_havepath(const char *);
70 static char *mod_searchmodule(char *, struct mod_depend *);
71 static void file_insert_tail(struct preloaded_file *);
72 static void file_remove(struct preloaded_file *);
73 struct file_metadata *metadata_next(struct file_metadata *, int);
74 static void moduledir_readhints(struct moduledir *);
75 static void moduledir_rebuild(void);
76 
77 /* load address should be tweaked by first module loaded (kernel) */
78 static vm_offset_t loadaddr = 0;
79 
80 #if defined(LOADER_FDT_SUPPORT)
81 static const char *default_searchpath = "/boot/kernel;/boot/modules;/boot/dtb";
82 #else
83 static const char *default_searchpath = "/platform/i86pc";
84 #endif
85 
86 static STAILQ_HEAD(, moduledir) moduledir_list =
87     STAILQ_HEAD_INITIALIZER(moduledir_list);
88 
89 struct preloaded_file *preloaded_files = NULL;
90 
91 static const char *kld_ext_list[] = {
92 	".ko",
93 	"",
94 	".debug",
95 	NULL
96 };
97 
98 
99 /*
100  * load an object, either a disk file or code module.
101  *
102  * To load a file, the syntax is:
103  *
104  * load -t <type> <path>
105  *
106  * code modules are loaded as:
107  *
108  * load <path> <options>
109  */
110 
111 COMMAND_SET(load, "load", "load a kernel or module", command_load);
112 
113 static int
114 command_load(int argc, char *argv[])
115 {
116 	char *typestr;
117 	int dofile, dokld, ch, error;
118 
119 	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 	while ((ch = getopt(argc, argv, "kt:")) != -1) {
128 		switch (ch) {
129 		case 'k':
130 			dokld = 1;
131 			break;
132 		case 't':
133 			typestr = optarg;
134 			dofile = 1;
135 			break;
136 		case '?':
137 		default:
138 			/* getopt has already reported an error */
139 			return (CMD_OK);
140 		}
141 	}
142 	argv += (optind - 1);
143 	argc -= (optind - 1);
144 
145 	printf("Loading %s...\n", argv[1]);
146 	/*
147 	 * Request to load a raw file?
148 	 */
149 	if (dofile) {
150 		struct preloaded_file *fp;
151 
152 		if ((typestr == NULL) || (*typestr == 0)) {
153 			command_errmsg = "invalid load type";
154 			return (CMD_CRIT);
155 		}
156 
157 		if (file_findfile(argv[1], typestr) != NULL) {
158 			(void) snprintf(command_errbuf, sizeof (command_errbuf),
159 			    "warning: file '%s' already loaded", argv[1]);
160 			return (CMD_WARN);
161 		}
162 
163 		fp = file_loadraw(argv[1], typestr, argc - 2, argv + 2, 1);
164 		if (fp != NULL)
165 			return (CMD_OK);
166 
167 		/* Failing to load mfs_root is never going to end well! */
168 		if (strcmp("mfs_root", typestr) == 0)
169 			return (CMD_FATAL);
170 
171 		return (CMD_ERROR);
172 	}
173 	/*
174 	 * Do we have explicit KLD load ?
175 	 */
176 	if (dokld || file_havepath(argv[1])) {
177 		error = mod_loadkld(argv[1], argc - 2, argv + 2);
178 		if (error == EEXIST) {
179 			(void) snprintf(command_errbuf, sizeof (command_errbuf),
180 			    "warning: KLD '%s' already loaded", argv[1]);
181 			return (CMD_WARN);
182 		}
183 
184 		return (error == 0 ? CMD_OK : CMD_CRIT);
185 	}
186 	/*
187 	 * Looks like a request for a module.
188 	 */
189 	error = mod_load(argv[1], NULL, argc - 2, argv + 2);
190 	if (error == EEXIST) {
191 		(void) snprintf(command_errbuf, sizeof (command_errbuf),
192 		    "warning: module '%s' already loaded", argv[1]);
193 		return (CMD_WARN);
194 	}
195 
196 	return (error == 0 ? CMD_OK : CMD_CRIT);
197 }
198 
199 void
200 unload(void)
201 {
202 	struct preloaded_file *fp;
203 
204 	while (preloaded_files != NULL) {
205 		fp = preloaded_files;
206 		preloaded_files = preloaded_files->f_next;
207 		file_discard(fp);
208 	}
209 	loadaddr = 0;
210 	(void) unsetenv("kernelname");
211 }
212 
213 COMMAND_SET(unload, "unload", "unload all modules", command_unload);
214 
215 static int
216 command_unload(int argc __unused, char *argv[] __unused)
217 {
218 	unload();
219 	return (CMD_OK);
220 }
221 
222 COMMAND_SET(lsmod, "lsmod", "list loaded modules", command_lsmod);
223 
224 static int
225 command_lsmod(int argc, char *argv[])
226 {
227 	struct preloaded_file *fp;
228 	struct kernel_module *mp;
229 	struct file_metadata *md;
230 	char lbuf[80];
231 	int ch, verbose, hash, ret = 0;
232 
233 	verbose = 0;
234 	hash = 0;
235 	optind = 1;
236 	optreset = 1;
237 	while ((ch = getopt(argc, argv, "vs")) != -1) {
238 		switch (ch) {
239 		case 'v':
240 			verbose = 1;
241 			break;
242 		case 's':
243 			hash = 1;
244 			break;
245 		case '?':
246 		default:
247 			/* getopt has already reported an error */
248 			return (CMD_OK);
249 		}
250 	}
251 
252 	pager_open();
253 	for (fp = preloaded_files; fp; fp = fp->f_next) {
254 		(void) snprintf(lbuf, sizeof (lbuf), " %p: ",
255 		    (void *) fp->f_addr);
256 		(void) pager_output(lbuf);
257 		(void) pager_output(fp->f_name);
258 		(void) snprintf(lbuf, sizeof (lbuf), " (%s, 0x%lx)\n",
259 		    fp->f_type, (long)fp->f_size);
260 		if (pager_output(lbuf))
261 			break;
262 		if (fp->f_args != NULL) {
263 			(void) pager_output("    args: ");
264 			(void) pager_output(fp->f_args);
265 			if (pager_output("\n"))
266 				break;
267 			if (strcmp(fp->f_type, "hash") == 0) {
268 				size_t dsize;
269 
270 				(void) pager_output("    contents: ");
271 				dsize = fp->f_size + 1;
272 				if (sizeof (lbuf) < dsize)
273 					dsize = sizeof (lbuf);
274 				(void) strlcpy(lbuf, ptov(fp->f_addr), dsize);
275 				if (pager_output(lbuf))
276 					break;
277 			}
278 		}
279 
280 		if (hash == 1) {
281 			void *ptr = PTOV(fp->f_addr);
282 
283 			(void) pager_output("  hash: ");
284 			sha1(ptr, fp->f_size, (uint8_t *)lbuf);
285 			for (int i = 0; i < SHA1_DIGEST_LENGTH; i++)
286 				printf("%02x", (int)(lbuf[i] & 0xff));
287 			if (pager_output("\n"))
288 				break;
289 		}
290 
291 		if (fp->f_modules) {
292 			(void) pager_output("  modules: ");
293 			for (mp = fp->f_modules; mp; mp = mp->m_next) {
294 				(void) snprintf(lbuf, sizeof (lbuf), "%s.%d ",
295 				    mp->m_name, mp->m_version);
296 				(void) pager_output(lbuf);
297 			}
298 			if (pager_output("\n"))
299 				break;
300 		}
301 		if (verbose) {
302 			/*
303 			 * XXX could add some formatting smarts here to
304 			 * display some better
305 			 */
306 			for (md = fp->f_metadata; md != NULL;
307 			    md = md->md_next) {
308 				(void) snprintf(lbuf, sizeof (lbuf),
309 				    "      0x%04x, 0x%lx\n",
310 				    md->md_type, (long)md->md_size);
311 				if ((ret = pager_output(lbuf)))
312 					break;
313 			}
314 		}
315 		if (ret != 0)
316 			break;
317 	}
318 	pager_close();
319 	return (CMD_OK);
320 }
321 
322 /*
323  * File level interface, functions file_*
324  */
325 int
326 file_load(char *filename, vm_offset_t dest, struct preloaded_file **result)
327 {
328 	static int last_file_format = 0;
329 	struct preloaded_file *fp;
330 	int error;
331 	int i;
332 
333 	if (preloaded_files == NULL)
334 		last_file_format = 0;
335 
336 	if (archsw.arch_loadaddr != NULL)
337 		dest = archsw.arch_loadaddr(LOAD_RAW, filename, dest);
338 
339 	error = EFTYPE;
340 	for (i = last_file_format, fp = NULL;
341 	    file_formats[i] && fp == NULL; i++) {
342 		error = (file_formats[i]->l_load)(filename, dest, &fp);
343 		if (error == 0) {
344 			/* remember the loader */
345 			fp->f_loader = last_file_format = i;
346 			*result = fp;
347 			break;
348 		} else if (last_file_format == i && i != 0) {
349 			/* Restart from the beginning */
350 			i = -1;
351 			last_file_format = 0;
352 			fp = NULL;
353 			continue;
354 		}
355 		if (error == EFTYPE)
356 			continue;	/* Unknown to this handler? */
357 		if (error) {
358 			(void) snprintf(command_errbuf, sizeof (command_errbuf),
359 			    "can't load file '%s': %s", filename,
360 			    strerror(error));
361 			break;
362 		}
363 	}
364 	return (error);
365 }
366 
367 static int
368 file_load_dependencies(struct preloaded_file *base_file)
369 {
370 	struct file_metadata *md;
371 	struct preloaded_file *fp;
372 	struct mod_depend *verinfo;
373 	struct kernel_module *mp;
374 	char *dmodname;
375 	int error;
376 
377 	md = file_findmetadata(base_file, MODINFOMD_DEPLIST);
378 	if (md == NULL)
379 		return (0);
380 	error = 0;
381 	do {
382 		verinfo = (struct mod_depend *)md->md_data;
383 		dmodname = (char *)(verinfo + 1);
384 		if (file_findmodule(NULL, dmodname, verinfo) == NULL) {
385 			printf("loading required module '%s'\n", dmodname);
386 			error = mod_load(dmodname, verinfo, 0, NULL);
387 			if (error)
388 				break;
389 			/*
390 			 * If module loaded via kld name which isn't listed
391 			 * in the linker.hints file, we should check if it have
392 			 * required version.
393 			 */
394 			mp = file_findmodule(NULL, dmodname, verinfo);
395 			if (mp == NULL) {
396 				(void) snprintf(command_errbuf,
397 				    sizeof (command_errbuf),
398 				    "module '%s' exists but with wrong version",
399 				    dmodname);
400 				error = ENOENT;
401 				break;
402 			}
403 		}
404 		md = metadata_next(md, MODINFOMD_DEPLIST);
405 	} while (md);
406 	if (!error)
407 		return (0);
408 	/* Load failed; discard everything */
409 	while (base_file != NULL) {
410 		fp = base_file;
411 		base_file = base_file->f_next;
412 		file_discard(fp);
413 	}
414 	return (error);
415 }
416 
417 /*
418  * Calculate the size of the environment module.
419  * The environment is list of name=value C strings, ending with a '\0' byte.
420  */
421 static size_t
422 env_get_size(void)
423 {
424 	size_t size = 0;
425 	struct env_var *ep;
426 
427 	/* Traverse the environment. */
428 	for (ep = environ; ep != NULL; ep = ep->ev_next) {
429 		size += strlen(ep->ev_name);
430 		size++;		/* "=" */
431 		if (ep->ev_value != NULL)
432 			size += strlen(ep->ev_value);
433 		size++;		/* nul byte */
434 	}
435 	size++;			/* nul byte */
436 	return (size);
437 }
438 
439 static void
440 module_hash(struct preloaded_file *fp, void *addr, size_t size)
441 {
442 	uint8_t hash[SHA1_DIGEST_LENGTH];
443 	char ascii[2 * SHA1_DIGEST_LENGTH + 1];
444 	int i;
445 
446 	sha1(addr, size, hash);
447 	for (i = 0; i < SHA1_DIGEST_LENGTH; i++) {
448 		(void) snprintf(ascii + 2 * i, sizeof (ascii) - 2 * i, "%02x",
449 		    hash[i] & 0xff);
450 	}
451 	/* Out of memory here is not fatal issue. */
452 	(void) asprintf(&fp->f_args, "hash=%s", ascii);
453 }
454 
455 /*
456  * Create virtual module for environment variables.
457  * This module should be created as late as possible before executing
458  * the OS kernel, or we may miss some environment variable updates.
459  */
460 void
461 build_environment_module(void)
462 {
463 	struct preloaded_file *fp;
464 	size_t size;
465 	char *name = "environment";
466 	vm_offset_t laddr;
467 
468 	/* We can't load first */
469 	if (file_findfile(NULL, NULL) == NULL) {
470 		printf("Can not load environment module: %s\n",
471 		    "the kernel is not loaded");
472 		return;
473 	}
474 
475 	if (file_findfile(name, name) != NULL) {
476 		printf("warning: '%s' is already loaded\n", name);
477 		return;
478 	}
479 
480 	tem_save_state();	/* Ask tem to save it's state in env. */
481 	size = env_get_size();
482 
483 	fp = file_alloc();
484 	if (fp != NULL) {
485 		fp->f_name = strdup(name);
486 		fp->f_type = strdup(name);
487 	}
488 
489 	if (fp == NULL || fp->f_name == NULL || fp->f_type == NULL) {
490 		printf("Can not load environment module: %s\n",
491 		    "out of memory");
492 		file_discard(fp);
493 		return;
494 	}
495 
496 
497 	if (archsw.arch_loadaddr != NULL)
498 		loadaddr = archsw.arch_loadaddr(LOAD_MEM, &size, loadaddr);
499 
500 	if (loadaddr == 0) {
501 		printf("Can not load environment module: %s\n",
502 		    "out of memory");
503 		file_discard(fp);
504 		return;
505 	}
506 
507 	laddr = bi_copyenv(loadaddr);
508 	/* Looks OK so far; populate control structure */
509 	module_hash(fp, PTOV(loadaddr), laddr - loadaddr);
510 	fp->f_loader = -1;
511 	fp->f_addr = loadaddr;
512 	fp->f_size = laddr - loadaddr;
513 
514 	/* recognise space consumption */
515 	loadaddr = laddr;
516 
517 	file_insert_tail(fp);
518 }
519 
520 void
521 build_font_module(void)
522 {
523 	bitmap_data_t *bd;
524 	struct font *fd;
525 	struct preloaded_file *fp;
526 	size_t size;
527 	uint32_t checksum;
528 	int i;
529 	char *name = "console-font";
530 	vm_offset_t laddr;
531 	struct font_info fi;
532 	struct fontlist *fl;
533 
534 	if (STAILQ_EMPTY(&fonts))
535 		return;
536 
537 	/* We can't load first */
538 	if (file_findfile(NULL, NULL) == NULL) {
539 		printf("Can not load font module: %s\n",
540 		    "the kernel is not loaded");
541 		return;
542 	}
543 
544 	if (file_findfile(name, name) != NULL) {
545 		printf("warning: '%s' is already loaded\n", name);
546 		return;
547 	}
548 
549 	/* helper pointers */
550 	bd = NULL;
551 	STAILQ_FOREACH(fl, &fonts, font_next) {
552 		if (tems.ts_font.vf_width == fl->font_data->width &&
553 		    tems.ts_font.vf_height == fl->font_data->height) {
554 			/*
555 			 * Kernel does have better built in font.
556 			 */
557 			if (fl->font_flags == FONT_BUILTIN)
558 				return;
559 
560 			bd = fl->font_data;
561 			break;
562 		}
563 	}
564 	if (bd == NULL)
565 		return;
566 	fd = bd->font;
567 
568 	fi.fi_width = fd->vf_width;
569 	checksum = fi.fi_width;
570 	fi.fi_height = fd->vf_height;
571 	checksum += fi.fi_height;
572 	fi.fi_bitmap_size = bd->uncompressed_size;
573 	checksum += fi.fi_bitmap_size;
574 
575 	size = roundup2(sizeof (struct font_info), 8);
576 	for (i = 0; i < VFNT_MAPS; i++) {
577 		fi.fi_map_count[i] = fd->vf_map_count[i];
578 		checksum += fi.fi_map_count[i];
579 		size += fd->vf_map_count[i] * sizeof (struct font_map);
580 		size += roundup2(size, 8);
581 	}
582 	size += bd->uncompressed_size;
583 
584 	fi.fi_checksum = -checksum;
585 
586 	fp = file_alloc();
587 	if (fp != NULL) {
588 		fp->f_name = strdup(name);
589 		fp->f_type = strdup(name);
590 	}
591 
592 	if (fp == NULL || fp->f_name == NULL || fp->f_type == NULL) {
593 		printf("Can not load font module: %s\n",
594 		    "out of memory");
595 		file_discard(fp);
596 		return;
597 	}
598 
599 	if (archsw.arch_loadaddr != NULL)
600 		loadaddr = archsw.arch_loadaddr(LOAD_MEM, &size, loadaddr);
601 
602 	if (loadaddr == 0) {
603 		printf("Can not load font module: %s\n",
604 		    "out of memory");
605 		file_discard(fp);
606 		return;
607 	}
608 
609 	laddr = loadaddr;
610 	laddr += archsw.arch_copyin(&fi, laddr, sizeof (struct font_info));
611 	laddr = roundup2(laddr, 8);
612 
613 	/* Copy maps. */
614 	for (i = 0; i < VFNT_MAPS; i++) {
615 		if (fd->vf_map_count[i] != 0) {
616 			laddr += archsw.arch_copyin(fd->vf_map[i], laddr,
617 			    fd->vf_map_count[i] * sizeof (struct font_map));
618 			laddr = roundup2(laddr, 8);
619 		}
620 	}
621 
622 	/* Copy the bitmap. */
623 	laddr += archsw.arch_copyin(fd->vf_bytes, laddr, fi.fi_bitmap_size);
624 
625 	/* Looks OK so far; populate control structure */
626 	module_hash(fp, PTOV(loadaddr), laddr - loadaddr);
627 	fp->f_loader = -1;
628 	fp->f_addr = loadaddr;
629 	fp->f_size = laddr - loadaddr;
630 
631 	/* recognise space consumption */
632 	loadaddr = laddr;
633 
634 	file_insert_tail(fp);
635 }
636 
637 /*
638  * We've been asked to load (fname) as (type), so just suck it in,
639  * no arguments or anything.
640  */
641 struct preloaded_file *
642 file_loadraw(const char *fname, char *type, int argc, char **argv, int insert)
643 {
644 	struct preloaded_file *fp;
645 	char *name;
646 	int fd;
647 	ssize_t got;
648 	struct stat st;
649 
650 	/* We can't load first */
651 	if ((file_findfile(NULL, NULL)) == NULL) {
652 		command_errmsg = "can't load file before kernel";
653 		return (NULL);
654 	}
655 
656 	/* locate the file on the load path */
657 	name = file_search(fname, NULL);
658 	if (name == NULL) {
659 		(void) snprintf(command_errbuf, sizeof (command_errbuf),
660 		    "can't find '%s'", fname);
661 		return (NULL);
662 	}
663 
664 	if ((fd = open(name, O_RDONLY)) < 0) {
665 		(void) snprintf(command_errbuf, sizeof (command_errbuf),
666 		    "can't open '%s': %s", name, strerror(errno));
667 		free(name);
668 		return (NULL);
669 	}
670 	if (fstat(fd, &st) < 0) {
671 		(void) close(fd);
672 		(void) snprintf(command_errbuf, sizeof (command_errbuf),
673 		    "stat error '%s': %s", name, strerror(errno));
674 		free(name);
675 		return (NULL);
676 	}
677 
678 	if (archsw.arch_loadaddr != NULL)
679 		loadaddr = archsw.arch_loadaddr(LOAD_RAW, name, loadaddr);
680 	if (loadaddr == 0) {
681 		(void) close(fd);
682 		(void) snprintf(command_errbuf, sizeof (command_errbuf),
683 		    "no memory to load %s", name);
684 		free(name);
685 		return (NULL);
686 	}
687 
688 	got = archsw.arch_readin(fd, loadaddr, st.st_size);
689 	if ((size_t)got != st.st_size) {
690 		(void) snprintf(command_errbuf, sizeof (command_errbuf),
691 		    "error reading '%s': %s", name, strerror(errno));
692 		free(name);
693 		(void) close(fd);
694 		if (archsw.arch_free_loadaddr != NULL && st.st_size != 0) {
695 			archsw.arch_free_loadaddr(loadaddr,
696 			    (uint64_t)(roundup2(st.st_size, PAGE_SIZE) >> 12));
697 		}
698 		return (NULL);
699 	}
700 
701 	/* Looks OK so far; create & populate control structure */
702 	fp = file_alloc();
703 	if (fp == NULL) {
704 		if (archsw.arch_free_loadaddr != NULL && st.st_size != 0)
705 			archsw.arch_free_loadaddr(loadaddr,
706 			    (uint64_t)(roundup2(st.st_size, PAGE_SIZE) >> 12));
707 		(void) snprintf(command_errbuf, sizeof (command_errbuf),
708 		    "no memory to load %s", name);
709 		free(name);
710 		(void) close(fd);
711 		return (NULL);
712 	}
713 
714 	fp->f_name = name;
715 	fp->f_args = unargv(argc, argv);
716 	fp->f_type = strdup(type);
717 	fp->f_metadata = NULL;
718 	fp->f_loader = -1;
719 	fp->f_addr = loadaddr;
720 	fp->f_size = st.st_size;
721 
722 	if (fp->f_type == NULL ||
723 	    (argc != 0 && fp->f_args == NULL)) {
724 		(void) close(fd);
725 		(void) snprintf(command_errbuf, sizeof (command_errbuf),
726 		    "no memory to load %s", name);
727 		file_discard(fp);
728 		return (NULL);
729 	}
730 	/* recognise space consumption */
731 	loadaddr += st.st_size;
732 
733 	/* Add to the list of loaded files */
734 	if (insert != 0)
735 		file_insert_tail(fp);
736 	(void) close(fd);
737 	return (fp);
738 }
739 
740 /*
741  * Load the module (name), pass it (argc),(argv), add container file
742  * to the list of loaded files.
743  * If module is already loaded just assign new argc/argv.
744  */
745 int
746 mod_load(char *modname, struct mod_depend *verinfo, int argc, char *argv[])
747 {
748 	struct kernel_module *mp;
749 	int err;
750 	char *filename;
751 
752 	if (file_havepath(modname)) {
753 		printf("Warning: mod_load() called instead of mod_loadkld() "
754 		    "for module '%s'\n", modname);
755 		return (mod_loadkld(modname, argc, argv));
756 	}
757 	/* see if module is already loaded */
758 	mp = file_findmodule(NULL, modname, verinfo);
759 	if (mp != NULL) {
760 		free(mp->m_args);
761 		mp->m_args = unargv(argc, argv);
762 		(void) snprintf(command_errbuf, sizeof (command_errbuf),
763 		    "warning: module '%s' already loaded", mp->m_name);
764 		return (0);
765 	}
766 	/* locate file with the module on the search path */
767 	filename = mod_searchmodule(modname, verinfo);
768 	if (filename == NULL) {
769 		(void) snprintf(command_errbuf, sizeof (command_errbuf),
770 		    "can't find '%s'", modname);
771 		return (ENOENT);
772 	}
773 	err = mod_loadkld(filename, argc, argv);
774 	free(filename);
775 	return (err);
776 }
777 
778 /*
779  * Load specified KLD. If path is omitted, then try to locate it via
780  * search path.
781  */
782 int
783 mod_loadkld(const char *kldname, int argc, char *argv[])
784 {
785 	struct preloaded_file *fp;
786 	int err;
787 	char *filename;
788 	vm_offset_t loadaddr_saved;
789 
790 	/*
791 	 * Get fully qualified KLD name
792 	 */
793 	filename = file_search(kldname, kld_ext_list);
794 	if (filename == NULL) {
795 		(void) snprintf(command_errbuf, sizeof (command_errbuf),
796 		    "can't find '%s'", kldname);
797 		return (ENOENT);
798 	}
799 	/*
800 	 * Check if KLD already loaded
801 	 */
802 	fp = file_findfile(filename, NULL);
803 	if (fp != NULL) {
804 		(void) snprintf(command_errbuf, sizeof (command_errbuf),
805 		    "warning: KLD '%s' already loaded", filename);
806 		free(filename);
807 		return (0);
808 	}
809 
810 	do {
811 		err = file_load(filename, loadaddr, &fp);
812 		if (err)
813 			break;
814 		fp->f_args = unargv(argc, argv);
815 		loadaddr_saved = loadaddr;
816 		loadaddr = fp->f_addr + fp->f_size;
817 		file_insert_tail(fp);	/* Add to the list of loaded files */
818 		if (file_load_dependencies(fp) != 0) {
819 			err = ENOENT;
820 			file_remove(fp);
821 			loadaddr = loadaddr_saved;
822 			fp = NULL;
823 			break;
824 		}
825 	} while (0);
826 	if (err == EFTYPE) {
827 		(void) snprintf(command_errbuf, sizeof (command_errbuf),
828 		    "don't know how to load module '%s'", filename);
829 	}
830 	if (err)
831 		file_discard(fp);
832 	free(filename);
833 	return (err);
834 }
835 
836 /*
837  * Find a file matching (name) and (type).
838  * NULL may be passed as a wildcard to either.
839  */
840 struct preloaded_file *
841 file_findfile(const char *name, const char *type)
842 {
843 	struct preloaded_file *fp;
844 
845 	for (fp = preloaded_files; fp != NULL; fp = fp->f_next) {
846 		if (((name == NULL) || strcmp(name, fp->f_name) == 0) &&
847 		    ((type == NULL) || strcmp(type, fp->f_type) == 0))
848 			break;
849 	}
850 	return (fp);
851 }
852 
853 /*
854  * Find a module matching (name) inside of given file.
855  * NULL may be passed as a wildcard.
856  */
857 struct kernel_module *
858 file_findmodule(struct preloaded_file *fp, char *modname,
859     struct mod_depend *verinfo)
860 {
861 	struct kernel_module *mp, *best;
862 	int bestver, mver;
863 
864 	if (fp == NULL) {
865 		for (fp = preloaded_files; fp; fp = fp->f_next) {
866 			mp = file_findmodule(fp, modname, verinfo);
867 			if (mp != NULL)
868 				return (mp);
869 		}
870 		return (NULL);
871 	}
872 	best = NULL;
873 	bestver = 0;
874 	for (mp = fp->f_modules; mp; mp = mp->m_next) {
875 		if (strcmp(modname, mp->m_name) == 0) {
876 			if (verinfo == NULL)
877 				return (mp);
878 			mver = mp->m_version;
879 			if (mver == verinfo->md_ver_preferred)
880 				return (mp);
881 			if (mver >= verinfo->md_ver_minimum &&
882 			    mver <= verinfo->md_ver_maximum &&
883 			    mver > bestver) {
884 				best = mp;
885 				bestver = mver;
886 			}
887 		}
888 	}
889 	return (best);
890 }
891 /*
892  * Make a copy of (size) bytes of data from (p), and associate them as
893  * metadata of (type) to the module (mp).
894  */
895 void
896 file_addmetadata(struct preloaded_file *fp, int type, size_t size, void *p)
897 {
898 	struct file_metadata	*md;
899 
900 	md = malloc(sizeof (struct file_metadata) - sizeof (md->md_data) +
901 	    size);
902 	if (md != NULL) {
903 		md->md_size = size;
904 		md->md_type = type;
905 		bcopy(p, md->md_data, size);
906 		md->md_next = fp->f_metadata;
907 	}
908 	fp->f_metadata = md;
909 }
910 
911 /*
912  * Find a metadata object of (type) associated with the file (fp)
913  */
914 struct file_metadata *
915 file_findmetadata(struct preloaded_file *fp, int type)
916 {
917 	struct file_metadata *md;
918 
919 	for (md = fp->f_metadata; md != NULL; md = md->md_next)
920 		if (md->md_type == type)
921 			break;
922 	return (md);
923 }
924 
925 struct file_metadata *
926 metadata_next(struct file_metadata *md, int type)
927 {
928 
929 	if (md == NULL)
930 		return (NULL);
931 	while ((md = md->md_next) != NULL)
932 		if (md->md_type == type)
933 			break;
934 	return (md);
935 }
936 
937 static const char *emptyextlist[] = { "", NULL };
938 
939 /*
940  * Check if the given file is in place and return full path to it.
941  */
942 static char *
943 file_lookup(const char *path, const char *name, int namelen,
944     const char **extlist)
945 {
946 	struct stat st;
947 	char *result, *cp;
948 	const char **cpp;
949 	int pathlen, extlen, len;
950 
951 	pathlen = strlen(path);
952 	extlen = 0;
953 	if (extlist == NULL)
954 		extlist = emptyextlist;
955 	for (cpp = extlist; *cpp; cpp++) {
956 		len = strlen(*cpp);
957 		if (len > extlen)
958 			extlen = len;
959 	}
960 	result = malloc(pathlen + namelen + extlen + 2);
961 	if (result == NULL)
962 		return (NULL);
963 	bcopy(path, result, pathlen);
964 	if (pathlen > 0 && result[pathlen - 1] != '/')
965 		result[pathlen++] = '/';
966 	cp = result + pathlen;
967 	bcopy(name, cp, namelen);
968 	cp += namelen;
969 	for (cpp = extlist; *cpp; cpp++) {
970 		(void) strcpy(cp, *cpp);
971 		if (stat(result, &st) == 0 && S_ISREG(st.st_mode))
972 			return (result);
973 	}
974 	free(result);
975 	return (NULL);
976 }
977 
978 /*
979  * Check if file name have any qualifiers
980  */
981 static int
982 file_havepath(const char *name)
983 {
984 	const char *cp;
985 
986 	(void) archsw.arch_getdev(NULL, name, &cp);
987 	return (cp != name || strchr(name, '/') != NULL);
988 }
989 
990 /*
991  * Attempt to find the file (name) on the module searchpath.
992  * If (name) is qualified in any way, we simply check it and
993  * return it or NULL.  If it is not qualified, then we attempt
994  * to construct a path using entries in the environment variable
995  * module_path.
996  *
997  * The path we return a pointer to need never be freed, as we manage
998  * it internally.
999  */
1000 static char *
1001 file_search(const char *name, const char **extlist)
1002 {
1003 	struct moduledir *mdp;
1004 	struct stat sb;
1005 	char *result;
1006 	int namelen;
1007 
1008 	/* Don't look for nothing */
1009 	if (name == NULL)
1010 		return (NULL);
1011 
1012 	if (*name == '\0')
1013 		return (strdup(name));
1014 
1015 	if (file_havepath(name)) {
1016 		/* Qualified, so just see if it exists */
1017 		if (stat(name, &sb) == 0)
1018 			return (strdup(name));
1019 		return (NULL);
1020 	}
1021 	moduledir_rebuild();
1022 	result = NULL;
1023 	namelen = strlen(name);
1024 	STAILQ_FOREACH(mdp, &moduledir_list, d_link) {
1025 		result = file_lookup(mdp->d_path, name, namelen, extlist);
1026 		if (result != NULL)
1027 			break;
1028 	}
1029 	return (result);
1030 }
1031 
1032 #define	INT_ALIGN(base, ptr)	ptr = \
1033 	(base) + (((ptr) - (base) + sizeof (int) - 1) & ~(sizeof (int) - 1))
1034 
1035 static char *
1036 mod_search_hints(struct moduledir *mdp, const char *modname,
1037     struct mod_depend *verinfo)
1038 {
1039 	uchar_t *cp, *recptr, *bufend, *best;
1040 	char *result;
1041 	int *intp, bestver, blen, clen, ival, modnamelen, reclen;
1042 	bool found;
1043 
1044 	moduledir_readhints(mdp);
1045 	modnamelen = strlen(modname);
1046 	found = false;
1047 	result = NULL;
1048 	bestver = 0;
1049 	if (mdp->d_hints == NULL)
1050 		goto bad;
1051 	recptr = mdp->d_hints;
1052 	bufend = recptr + mdp->d_hintsz;
1053 	clen = blen = 0;
1054 	best = cp = NULL;
1055 	while (recptr < bufend && !found) {
1056 		intp = (int *)recptr;
1057 		reclen = *intp++;
1058 		ival = *intp++;
1059 		cp = (uchar_t *)intp;
1060 		switch (ival) {
1061 		case MDT_VERSION:
1062 			clen = *cp++;
1063 			if (clen != modnamelen || bcmp(cp, modname, clen) != 0)
1064 				break;
1065 			cp += clen;
1066 			INT_ALIGN(mdp->d_hints, cp);
1067 			ival = *(int *)cp;
1068 			cp += sizeof (int);
1069 			clen = *cp++;
1070 			if (verinfo == NULL ||
1071 			    ival == verinfo->md_ver_preferred) {
1072 				found = true;
1073 				break;
1074 			}
1075 			if (ival >= verinfo->md_ver_minimum &&
1076 			    ival <= verinfo->md_ver_maximum &&
1077 			    ival > bestver) {
1078 				bestver = ival;
1079 				best = cp;
1080 				blen = clen;
1081 			}
1082 			break;
1083 		default:
1084 			break;
1085 		}
1086 		recptr += reclen + sizeof (int);
1087 	}
1088 	/*
1089 	 * Finally check if KLD is in the place
1090 	 */
1091 	if (found)
1092 		result = file_lookup(mdp->d_path, (char *)cp, clen, NULL);
1093 	else if (best)
1094 		result = file_lookup(mdp->d_path, (char *)best, blen, NULL);
1095 bad:
1096 	/*
1097 	 * If nothing found or hints is absent - fallback to the old way
1098 	 * by using "kldname[.ko]" as module name.
1099 	 */
1100 	if (!found && bestver == 0 && result == NULL) {
1101 		result = file_lookup(mdp->d_path, modname, modnamelen,
1102 		    kld_ext_list);
1103 	}
1104 	return (result);
1105 }
1106 
1107 /*
1108  * Attempt to locate the file containing the module (name)
1109  */
1110 static char *
1111 mod_searchmodule(char *name, struct mod_depend *verinfo)
1112 {
1113 	struct moduledir *mdp;
1114 	char *result;
1115 
1116 	moduledir_rebuild();
1117 	/*
1118 	 * Now we ready to lookup module in the given directories
1119 	 */
1120 	result = NULL;
1121 	STAILQ_FOREACH(mdp, &moduledir_list, d_link) {
1122 		result = mod_search_hints(mdp, name, verinfo);
1123 		if (result != NULL)
1124 			break;
1125 	}
1126 
1127 	return (result);
1128 }
1129 
1130 int
1131 file_addmodule(struct preloaded_file *fp, char *modname, int version,
1132     struct kernel_module **newmp)
1133 {
1134 	struct kernel_module *mp;
1135 	struct mod_depend mdepend;
1136 
1137 	bzero(&mdepend, sizeof (mdepend));
1138 	mdepend.md_ver_preferred = version;
1139 	mp = file_findmodule(fp, modname, &mdepend);
1140 	if (mp != NULL)
1141 		return (EEXIST);
1142 	mp = calloc(1, sizeof (struct kernel_module));
1143 	if (mp == NULL)
1144 		return (ENOMEM);
1145 	mp->m_name = strdup(modname);
1146 	if (mp->m_name == NULL) {
1147 		free(mp);
1148 		return (ENOMEM);
1149 	}
1150 	mp->m_version = version;
1151 	mp->m_fp = fp;
1152 	mp->m_next = fp->f_modules;
1153 	fp->f_modules = mp;
1154 	if (newmp)
1155 		*newmp = mp;
1156 	return (0);
1157 }
1158 
1159 /*
1160  * Throw a file away
1161  */
1162 void
1163 file_discard(struct preloaded_file *fp)
1164 {
1165 	struct file_metadata *md, *md1;
1166 	struct kernel_module *mp, *mp1;
1167 
1168 	if (fp == NULL)
1169 		return;
1170 
1171 	if (archsw.arch_free_loadaddr != NULL && fp->f_addr &&
1172 	    fp->f_size != 0) {
1173 		archsw.arch_free_loadaddr(fp->f_addr,
1174 		    (uint64_t)(roundup2(fp->f_size, PAGE_SIZE) >> 12));
1175 	}
1176 
1177 	md = fp->f_metadata;
1178 	while (md != NULL) {
1179 		md1 = md;
1180 		md = md->md_next;
1181 		free(md1);
1182 	}
1183 	mp = fp->f_modules;
1184 	while (mp != NULL) {
1185 		free(mp->m_name);
1186 		mp1 = mp;
1187 		mp = mp->m_next;
1188 		free(mp1);
1189 	}
1190 	free(fp->f_name);
1191 	free(fp->f_type);
1192 	free(fp->f_args);
1193 	free(fp);
1194 }
1195 
1196 /*
1197  * Allocate a new file; must be used instead of malloc()
1198  * to ensure safe initialisation.
1199  */
1200 struct preloaded_file *
1201 file_alloc(void)
1202 {
1203 
1204 	return (calloc(1, sizeof (struct preloaded_file)));
1205 }
1206 
1207 /*
1208  * Add a module to the chain
1209  */
1210 static void
1211 file_insert_tail(struct preloaded_file *fp)
1212 {
1213 	struct preloaded_file *cm;
1214 
1215 	/* Append to list of loaded file */
1216 	fp->f_next = NULL;
1217 	if (preloaded_files == NULL) {
1218 		preloaded_files = fp;
1219 	} else {
1220 		for (cm = preloaded_files; cm->f_next != NULL; cm = cm->f_next)
1221 			;
1222 		cm->f_next = fp;
1223 	}
1224 }
1225 
1226 /*
1227  * Remove module from the chain
1228  */
1229 static void
1230 file_remove(struct preloaded_file *fp)
1231 {
1232 	struct preloaded_file   *cm;
1233 
1234 	if (preloaded_files == NULL)
1235 		return;
1236 
1237 	if (preloaded_files == fp) {
1238 		preloaded_files = fp->f_next;
1239 		return;
1240 	}
1241 	for (cm = preloaded_files; cm->f_next != NULL; cm = cm->f_next) {
1242 		if (cm->f_next == fp) {
1243 			cm->f_next = fp->f_next;
1244 			return;
1245 		}
1246 	}
1247 }
1248 
1249 static char *
1250 moduledir_fullpath(struct moduledir *mdp, const char *fname)
1251 {
1252 	char *cp;
1253 
1254 	cp = malloc(strlen(mdp->d_path) + strlen(fname) + 2);
1255 	if (cp == NULL)
1256 		return (NULL);
1257 	strcpy(cp, mdp->d_path);
1258 	strcat(cp, "/");
1259 	strcat(cp, fname);
1260 	return (cp);
1261 }
1262 
1263 /*
1264  * Read linker.hints file into memory performing some sanity checks.
1265  */
1266 static void
1267 moduledir_readhints(struct moduledir *mdp)
1268 {
1269 	struct stat st;
1270 	char *path;
1271 	int fd, size, version;
1272 
1273 	if (mdp->d_hints != NULL || (mdp->d_flags & MDIR_NOHINTS))
1274 		return;
1275 	path = moduledir_fullpath(mdp, "linker.hints");
1276 	if (stat(path, &st) != 0 ||
1277 	    st.st_size < (ssize_t)(sizeof (version) + sizeof (int)) ||
1278 	    st.st_size > LINKER_HINTS_MAX ||
1279 	    (fd = open(path, O_RDONLY)) < 0) {
1280 		free(path);
1281 		mdp->d_flags |= MDIR_NOHINTS;
1282 		return;
1283 	}
1284 	free(path);
1285 	size = read(fd, &version, sizeof (version));
1286 	if (size != sizeof (version) || version != LINKER_HINTS_VERSION)
1287 		goto bad;
1288 	size = st.st_size - size;
1289 	mdp->d_hints = malloc(size);
1290 	if (mdp->d_hints == NULL)
1291 		goto bad;
1292 	if (read(fd, mdp->d_hints, size) != size)
1293 		goto bad;
1294 	mdp->d_hintsz = size;
1295 	(void) close(fd);
1296 	return;
1297 bad:
1298 	(void) close(fd);
1299 	free(mdp->d_hints);
1300 	mdp->d_hints = NULL;
1301 	mdp->d_flags |= MDIR_NOHINTS;
1302 }
1303 
1304 /*
1305  * Extract directories from the ';' separated list, remove duplicates.
1306  */
1307 static void
1308 moduledir_rebuild(void)
1309 {
1310 	struct moduledir *mdp, *mtmp;
1311 	const char *path, *cp, *ep;
1312 	size_t cplen;
1313 
1314 	path = getenv("module_path");
1315 	if (path == NULL)
1316 		path = default_searchpath;
1317 	/*
1318 	 * Rebuild list of module directories if it changed
1319 	 */
1320 	STAILQ_FOREACH(mdp, &moduledir_list, d_link)
1321 		mdp->d_flags |= MDIR_REMOVED;
1322 
1323 	for (ep = path; *ep != 0; ep++) {
1324 		cp = ep;
1325 		for (; *ep != 0 && *ep != ';'; ep++)
1326 			;
1327 		/*
1328 		 * Ignore trailing slashes
1329 		 */
1330 		for (cplen = ep - cp; cplen > 1 && cp[cplen - 1] == '/';
1331 		    cplen--)
1332 			;
1333 		STAILQ_FOREACH(mdp, &moduledir_list, d_link) {
1334 			if (strlen(mdp->d_path) != cplen ||
1335 			    bcmp(cp, mdp->d_path, cplen) != 0)
1336 				continue;
1337 			mdp->d_flags &= ~MDIR_REMOVED;
1338 			break;
1339 		}
1340 		if (mdp == NULL) {
1341 			mdp = malloc(sizeof (*mdp) + cplen + 1);
1342 			if (mdp == NULL)
1343 				return;
1344 			mdp->d_path = (char *)(mdp + 1);
1345 			bcopy(cp, mdp->d_path, cplen);
1346 			mdp->d_path[cplen] = 0;
1347 			mdp->d_hints = NULL;
1348 			mdp->d_flags = 0;
1349 			STAILQ_INSERT_TAIL(&moduledir_list, mdp, d_link);
1350 		}
1351 		if (*ep == '\0')
1352 			break;
1353 	}
1354 	/*
1355 	 * Delete unused directories if any
1356 	 */
1357 	mdp = STAILQ_FIRST(&moduledir_list);
1358 	while (mdp) {
1359 		if ((mdp->d_flags & MDIR_REMOVED) == 0) {
1360 			mdp = STAILQ_NEXT(mdp, d_link);
1361 		} else {
1362 			free(mdp->d_hints);
1363 			mtmp = mdp;
1364 			mdp = STAILQ_NEXT(mdp, d_link);
1365 			STAILQ_REMOVE(&moduledir_list, mtmp, moduledir, d_link);
1366 			free(mtmp);
1367 		}
1368 	}
1369 }
1370