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