xref: /netbsd-src/sys/arch/i386/stand/lib/exec.c (revision 901e7e84758515fbf39dfc064cb0b45ab146d8b0)
1 /*	$NetBSD: exec.c,v 1.79 2023/04/20 00:42:24 manu Exp $	 */
2 
3 /*
4  * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /*
30  * Copyright (c) 1982, 1986, 1990, 1993
31  *	The Regents of the University of California.  All rights reserved.
32  *
33  * Redistribution and use in source and binary forms, with or without
34  * modification, are permitted provided that the following conditions
35  * are met:
36  * 1. Redistributions of source code must retain the above copyright
37  *    notice, this list of conditions and the following disclaimer.
38  * 2. Redistributions in binary form must reproduce the above copyright
39  *    notice, this list of conditions and the following disclaimer in the
40  *    documentation and/or other materials provided with the distribution.
41  * 3. Neither the name of the University nor the names of its contributors
42  *    may be used to endorse or promote products derived from this software
43  *    without specific prior written permission.
44  *
45  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55  * SUCH DAMAGE.
56  *
57  * 	@(#)boot.c	8.1 (Berkeley) 6/10/93
58  */
59 
60 /*
61  * Copyright (c) 1996
62  *	Matthias Drochner.  All rights reserved.
63  * Copyright (c) 1996
64  * 	Perry E. Metzger.  All rights reserved.
65  *
66  * Redistribution and use in source and binary forms, with or without
67  * modification, are permitted provided that the following conditions
68  * are met:
69  * 1. Redistributions of source code must retain the above copyright
70  *    notice, this list of conditions and the following disclaimer.
71  * 2. Redistributions in binary form must reproduce the above copyright
72  *    notice, this list of conditions and the following disclaimer in the
73  *    documentation and/or other materials provided with the distribution.
74  *
75  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
76  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
77  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
78  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
79  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
80  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
81  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
82  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
83  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
84  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
85  * SUCH DAMAGE.
86  *
87  * 	@(#)boot.c	8.1 (Berkeley) 6/10/93
88  */
89 
90 /*
91  * Starts a NetBSD ELF kernel. The low level startup is done in startprog.S.
92  * This is a special version of exec.c to support use of XMS.
93  */
94 
95 #include <sys/param.h>
96 #include <sys/reboot.h>
97 
98 #include <lib/libsa/stand.h>
99 #include <lib/libkern/libkern.h>
100 
101 #include "loadfile.h"
102 #include "libi386.h"
103 #include "bootinfo.h"
104 #include "bootmod.h"
105 #include "vbe.h"
106 #ifdef SUPPORT_PS2
107 #include "biosmca.h"
108 #endif
109 #ifdef EFIBOOT
110 #include "efiboot.h"
111 #undef DEBUG	/* XXX */
112 #endif
113 
114 #define BOOT_NARGS	6
115 
116 #ifndef	PAGE_SIZE
117 #define	PAGE_SIZE	4096
118 #endif
119 
120 #define MODULE_WARNING_SEC	5
121 
122 #define MAXMODNAME	32	/* from <sys/module.h> */
123 
124 extern struct btinfo_console btinfo_console;
125 extern struct btinfo_rootdevice bi_root;
126 
127 boot_module_t *boot_modules;
128 bool boot_modules_enabled = true;
129 bool kernel_loaded;
130 
131 typedef struct userconf_command {
132 	char *uc_text;
133 	size_t uc_len;
134 	struct userconf_command *uc_next;
135 } userconf_command_t;
136 userconf_command_t *userconf_commands = NULL;
137 
138 struct btinfo_framebuffer btinfo_framebuffer;
139 
140 struct btinfo_modulelist *btinfo_modulelist;
141 static size_t btinfo_modulelist_size;
142 static uint32_t image_end;
143 static char module_base[64] = "/";
144 static int howto;
145 
146 static struct btinfo_userconfcommands *btinfo_userconfcommands = NULL;
147 static size_t btinfo_userconfcommands_size = 0;
148 
149 static void	module_init(const char *);
150 static void	module_add_common(const char *, uint8_t);
151 
152 static void	userconf_init(void);
153 
154 static void	extract_device(const char *, char *, size_t);
155 static void	module_base_path(char *, size_t, const char *);
156 static int	module_open(boot_module_t *, int, const char *, const char *,
157 		    bool);
158 
159 void
160 framebuffer_configure(struct btinfo_framebuffer *fb)
161 {
162 	if (fb)
163 		btinfo_framebuffer = *fb;
164 	else {
165 		btinfo_framebuffer.physaddr = 0;
166 		btinfo_framebuffer.flags = 0;
167 	}
168 }
169 
170 void
171 module_add(char *name)
172 {
173 	return module_add_common(name, BM_TYPE_KMOD);
174 }
175 
176 void
177 splash_add(char *name)
178 {
179 	return module_add_common(name, BM_TYPE_IMAGE);
180 }
181 
182 void
183 rnd_add(char *name)
184 {
185 	return module_add_common(name, BM_TYPE_RND);
186 }
187 
188 void
189 fs_add(char *name)
190 {
191 	return module_add_common(name, BM_TYPE_FS);
192 }
193 
194 /*
195  * Add a /-separated list of module names to the boot list
196  */
197 void
198 module_add_split(const char *name, uint8_t type)
199 {
200 	char mod_name[MAXMODNAME];
201 	int i;
202 	const char *mp = name;
203 	char *ep;
204 
205 	while (*mp) {				/* scan list of module names */
206 		i = MAXMODNAME;
207 		ep = mod_name;
208 		while (--i) {			/* scan for end of first name */
209 			*ep = *mp;
210 			if (*ep == '/')		/* NUL-terminate the name */
211 				*ep = '\0';
212 
213 			if (*ep == 0 ) {	/* add non-empty name */
214 				if (ep != mod_name)
215 					module_add_common(mod_name, type);
216 				break;
217 			}
218 			ep++; mp++;
219 		}
220 		if (*ep != 0) {
221 			printf("module name too long\n");
222 			return;
223 		}
224 		if  (*mp == '/') {		/* skip separator if more */
225 			mp++;
226 		}
227 	}
228 }
229 
230 static void
231 module_add_common(const char *name, uint8_t type)
232 {
233 	boot_module_t *bm, *bmp;
234 	size_t len;
235 	char *str;
236 
237 	while (*name == ' ' || *name == '\t')
238 		++name;
239 
240 	for (bm = boot_modules; bm != NULL; bm = bm->bm_next)
241 		if (bm->bm_type == type && strcmp(bm->bm_path, name) == 0)
242 			return;
243 
244 	bm = alloc(sizeof(boot_module_t));
245 	len = strlen(name) + 1;
246 	str = alloc(len);
247 	if (bm == NULL || str == NULL) {
248 		printf("couldn't allocate module\n");
249 		return;
250 	}
251 	memcpy(str, name, len);
252 	bm->bm_path = str;
253 	bm->bm_next = NULL;
254 	bm->bm_type = type;
255 	if (boot_modules == NULL)
256 		boot_modules = bm;
257 	else {
258 		for (bmp = boot_modules; bmp->bm_next;
259 		    bmp = bmp->bm_next)
260 			;
261 		bmp->bm_next = bm;
262 	}
263 }
264 
265 void
266 userconf_add(char *cmd)
267 {
268 	userconf_command_t *uc;
269 	size_t len;
270 	char *text;
271 
272 	while (*cmd == ' ' || *cmd == '\t')
273 		++cmd;
274 
275 	uc = alloc(sizeof(*uc));
276 	if (uc == NULL) {
277 		printf("couldn't allocate command\n");
278 		return;
279 	}
280 
281 	len = strlen(cmd) + 1;
282 	text = alloc(len);
283 	if (text == NULL) {
284 		dealloc(uc, sizeof(*uc));
285 		printf("couldn't allocate command\n");
286 		return;
287 	}
288 	memcpy(text, cmd, len);
289 
290 	uc->uc_text = text;
291 	uc->uc_len = len;
292 	uc->uc_next = NULL;
293 
294 	if (userconf_commands == NULL)
295 		userconf_commands = uc;
296 	else {
297 		userconf_command_t *ucp;
298 		for (ucp = userconf_commands; ucp->uc_next != NULL;
299 		     ucp = ucp->uc_next)
300 			;
301 		ucp->uc_next = uc;
302 	}
303 }
304 
305 struct btinfo_prekern bi_prekern;
306 int has_prekern = 0;
307 
308 static int
309 common_load_prekern(const char *file, u_long *basemem, u_long *extmem,
310     physaddr_t loadaddr, int floppy, u_long marks[MARK_MAX])
311 {
312 	paddr_t kernpa_start, kernpa_end;
313 	char prekernpath[] = "/prekern";
314 	u_long prekern_start;
315 	int fd, flags;
316 
317 	*extmem = getextmem();
318 	*basemem = getbasemem();
319 
320 	marks[MARK_START] = loadaddr;
321 
322 	/* Load the prekern (static) */
323 	flags = LOAD_KERNEL & ~(LOAD_HDR|LOAD_SYM);
324 	if ((fd = loadfile(prekernpath, marks, flags)) == -1)
325 		return errno;
326 	close(fd);
327 
328 	prekern_start = marks[MARK_START];
329 
330 	/* The kernel starts at 2MB. */
331 	marks[MARK_START] = loadaddr;
332 	marks[MARK_END] = loadaddr + (1UL << 21);
333 	kernpa_start = (1UL << 21);
334 
335 	/* Load the kernel (dynamic) */
336 	flags = (LOAD_KERNEL | LOAD_DYN) & ~(floppy ? LOAD_BACKWARDS : 0);
337 	if ((fd = loadfile(file, marks, flags)) == -1)
338 		return errno;
339 	close(fd);
340 
341 	kernpa_end = marks[MARK_END] - loadaddr;
342 
343 	/* If the root fs type is unusual, load its module. */
344 	if (fsmod != NULL)
345 		module_add_split(fsmod, BM_TYPE_KMOD);
346 
347 	bi_prekern.kernpa_start = kernpa_start;
348 	bi_prekern.kernpa_end = kernpa_end;
349 	BI_ADD(&bi_prekern, BTINFO_PREKERN, sizeof(struct btinfo_prekern));
350 
351 	/*
352 	 * Gather some information for the kernel. Do this after the
353 	 * "point of no return" to avoid memory leaks.
354 	 * (but before DOS might be trashed in the XMS case)
355 	 */
356 #ifdef PASS_BIOSGEOM
357 	bi_getbiosgeom();
358 #endif
359 #ifdef PASS_MEMMAP
360 	bi_getmemmap();
361 #endif
362 
363 	marks[MARK_START] = prekern_start;
364 	marks[MARK_END] = (((u_long)marks[MARK_END] + sizeof(int) - 1)) &
365 	    (-sizeof(int));
366 	image_end = marks[MARK_END];
367 	kernel_loaded = true;
368 
369 	return 0;
370 }
371 
372 static int
373 common_load_kernel(const char *file, u_long *basemem, u_long *extmem,
374     physaddr_t loadaddr, int floppy, u_long marks[MARK_MAX])
375 {
376 	int fd;
377 #ifdef XMS
378 	u_long xmsmem;
379 	physaddr_t origaddr = loadaddr;
380 #endif
381 
382 	*extmem = getextmem();
383 	*basemem = getbasemem();
384 
385 #ifdef XMS
386 	if ((getextmem1() == 0) && (xmsmem = checkxms())) {
387 		u_long kernsize;
388 
389 		/*
390 		 * With "CONSERVATIVE_MEMDETECT", extmem is 0 because
391 		 * getextmem() is getextmem1(). Without, the "smart"
392 		 * methods could fail to report all memory as well.
393 		 * xmsmem is a few kB less than the actual size, but
394 		 * better than nothing.
395 		 */
396 		if (xmsmem > *extmem)
397 			*extmem = xmsmem;
398 		/*
399 		 * Get the size of the kernel
400 		 */
401 		marks[MARK_START] = loadaddr;
402 		if ((fd = loadfile(file, marks, COUNT_KERNEL)) == -1)
403 			return errno;
404 		close(fd);
405 
406 		kernsize = marks[MARK_END];
407 		kernsize = (kernsize + 1023) / 1024;
408 
409 		loadaddr = xmsalloc(kernsize);
410 		if (!loadaddr)
411 			return ENOMEM;
412 	}
413 #endif
414 	marks[MARK_START] = loadaddr;
415 	if ((fd = loadfile(file, marks,
416 	    LOAD_KERNEL & ~(floppy ? LOAD_BACKWARDS : 0))) == -1)
417 		return errno;
418 
419 	close(fd);
420 
421 	/* If the root fs type is unusual, load its module. */
422 	if (fsmod != NULL)
423 		module_add_split(fsmod, BM_TYPE_KMOD);
424 
425 	/*
426 	 * Gather some information for the kernel. Do this after the
427 	 * "point of no return" to avoid memory leaks.
428 	 * (but before DOS might be trashed in the XMS case)
429 	 */
430 #ifdef PASS_BIOSGEOM
431 	bi_getbiosgeom();
432 #endif
433 #ifdef PASS_MEMMAP
434 	bi_getmemmap();
435 #endif
436 
437 #ifdef XMS
438 	if (loadaddr != origaddr) {
439 		/*
440 		 * We now have done our last DOS IO, so we may
441 		 * trash the OS. Copy the data from the temporary
442 		 * buffer to its real address.
443 		 */
444 		marks[MARK_START] -= loadaddr;
445 		marks[MARK_END] -= loadaddr;
446 		marks[MARK_SYM] -= loadaddr;
447 		marks[MARK_END] -= loadaddr;
448 		ppbcopy(loadaddr, origaddr, marks[MARK_END]);
449 	}
450 #endif
451 	marks[MARK_END] = (((u_long) marks[MARK_END] + sizeof(int) - 1)) &
452 	    (-sizeof(int));
453 	image_end = marks[MARK_END];
454 	kernel_loaded = true;
455 
456 	return 0;
457 }
458 
459 int
460 exec_netbsd(const char *file, physaddr_t loadaddr, int boothowto, int floppy,
461     void (*callback)(void))
462 {
463 	uint32_t boot_argv[BOOT_NARGS];
464 	u_long marks[MARK_MAX];
465 	struct btinfo_symtab btinfo_symtab;
466 	u_long extmem;
467 	u_long basemem;
468 	u_long entry;
469 	int error;
470 #ifdef EFIBOOT
471 	int i;
472 #endif
473 
474 #ifdef	DEBUG
475 	printf("exec: file=%s loadaddr=0x%lx\n", file ? file : "NULL",
476 	    loadaddr);
477 #endif
478 
479 	BI_ALLOC(BTINFO_MAX);
480 
481 	BI_ADD(&btinfo_console, BTINFO_CONSOLE, sizeof(struct btinfo_console));
482 	if (bi_root.devname[0])
483 		BI_ADD(&bi_root, BTINFO_ROOTDEVICE, sizeof(struct btinfo_rootdevice));
484 
485 	howto = boothowto;
486 
487 	memset(marks, 0, sizeof(marks));
488 
489 	if (has_prekern) {
490 		error = common_load_prekern(file, &basemem, &extmem, loadaddr,
491 		    floppy, marks);
492 	} else {
493 		error = common_load_kernel(file, &basemem, &extmem, loadaddr,
494 		    floppy, marks);
495 	}
496 	if (error) {
497 		errno = error;
498 		goto out;
499 	}
500 #ifdef EFIBOOT
501 	efi_load_start = marks[MARK_START];
502 
503 	/* adjust to the real load address */
504 	marks[MARK_START] -= efi_loadaddr;
505 	marks[MARK_ENTRY] -= efi_loadaddr;
506 	marks[MARK_DATA] -= efi_loadaddr;
507 	/* MARK_NSYM */
508 	marks[MARK_SYM] -= efi_loadaddr;
509 	marks[MARK_END] -= efi_loadaddr;
510 #endif
511 
512 	boot_argv[0] = boothowto;
513 	boot_argv[1] = 0;
514 	boot_argv[2] = vtophys(bootinfo);	/* old cyl offset */
515 	boot_argv[3] = marks[MARK_END];
516 	boot_argv[4] = extmem;
517 	boot_argv[5] = basemem;
518 
519 	/* pull in any modules if necessary */
520 	if (boot_modules_enabled) {
521 		module_init(file);
522 		if (btinfo_modulelist) {
523 #ifdef EFIBOOT
524 			/* convert module loaded address to paddr */
525 			struct bi_modulelist_entry *bim;
526 			bim = (void *)(btinfo_modulelist + 1);
527 			for (i = 0; i < btinfo_modulelist->num; i++, bim++)
528 				bim->base -= efi_loadaddr;
529 			btinfo_modulelist->endpa -= efi_loadaddr;
530 #endif
531 			BI_ADD(btinfo_modulelist, BTINFO_MODULELIST,
532 			    btinfo_modulelist_size);
533 		}
534 	}
535 
536 	userconf_init();
537 	if (btinfo_userconfcommands != NULL)
538 		BI_ADD(btinfo_userconfcommands, BTINFO_USERCONFCOMMANDS,
539 		    btinfo_userconfcommands_size);
540 
541 #ifdef DEBUG
542 	printf("Start @ 0x%lx [%ld=0x%lx-0x%lx]...\n", marks[MARK_ENTRY],
543 	    marks[MARK_NSYM], marks[MARK_SYM], marks[MARK_END]);
544 #endif
545 
546 	btinfo_symtab.nsym = marks[MARK_NSYM];
547 	btinfo_symtab.ssym = marks[MARK_SYM];
548 	btinfo_symtab.esym = marks[MARK_END];
549 	BI_ADD(&btinfo_symtab, BTINFO_SYMTAB, sizeof(struct btinfo_symtab));
550 
551 	/* set new video mode if necessary */
552 	vbe_commit();
553 	BI_ADD(&btinfo_framebuffer, BTINFO_FRAMEBUFFER,
554 	    sizeof(struct btinfo_framebuffer));
555 
556 	if (callback != NULL)
557 		(*callback)();
558 
559 	entry = marks[MARK_ENTRY];
560 #ifdef EFIBOOT
561 	/* Copy bootinfo to safe arena. */
562 	for (i = 0; i < bootinfo->nentries; i++) {
563 		struct btinfo_common *bi = (void *)(u_long)bootinfo->entry[i];
564 		char *p = alloc(bi->len);
565 		memcpy(p, bi, bi->len);
566 		bootinfo->entry[i] = vtophys(p);
567 	}
568 
569 	efi_kernel_start = marks[MARK_START];
570 	efi_kernel_size = image_end - (efi_loadaddr + efi_kernel_start);
571 
572 	switch (efi_reloc_type) {
573 	case RELOC_NONE:
574 		entry += (efi_load_start - efi_kernel_start);
575 		efi_kernel_start = efi_load_start;
576 		break;
577 	case RELOC_ADDR:
578 		entry += (efi_kernel_reloc - efi_kernel_start);
579 		efi_kernel_start = efi_kernel_reloc;
580 		break;
581 	case RELOC_DEFAULT:
582 	default:
583 		break;
584 	}
585 #endif
586 	startprog(entry, BOOT_NARGS, boot_argv,
587 	    x86_trunc_page(basemem * 1024));
588 	panic("exec returned");
589 
590 out:
591 	BI_FREE();
592 	bootinfo = NULL;
593 	return -1;
594 }
595 
596 static void
597 extract_device(const char *path, char *buf, size_t buflen)
598 {
599 	size_t i;
600 
601 	if (strchr(path, ':') != NULL) {
602 		for (i = 0; i < buflen - 2 && path[i] != ':'; i++)
603 			buf[i] = path[i];
604 		buf[i++] = ':';
605 		buf[i] = '\0';
606 	} else
607 		buf[0] = '\0';
608 }
609 
610 static const char *
611 module_path(boot_module_t *bm, const char *kdev, const char *base_path)
612 {
613 	static char buf[256];
614 	char name_buf[256], dev_buf[64];
615 	const char *name, *name2, *p;
616 
617 	name = bm->bm_path;
618 	for (name2 = name; *name2; ++name2) {
619 		if (*name2 == ' ' || *name2 == '\t') {
620 			strlcpy(name_buf, name, sizeof(name_buf));
621 			if ((uintptr_t)name2 - (uintptr_t)name < sizeof(name_buf))
622 				name_buf[name2 - name] = '\0';
623 			name = name_buf;
624 			break;
625 		}
626 	}
627 	if ((p = strchr(name, ':')) != NULL) {
628 		/* device specified, use it */
629 		if (p[1] == '/')
630 			snprintf(buf, sizeof(buf), "%s", name);
631 		else {
632 			p++;
633 			extract_device(name, dev_buf, sizeof(dev_buf));
634 			snprintf(buf, sizeof(buf), "%s%s/%s/%s.kmod",
635 			    dev_buf, base_path, p, p);
636 		}
637 	} else {
638 		/* device not specified; load from kernel device if known */
639 		if (name[0] == '/')
640 			snprintf(buf, sizeof(buf), "%s%s", kdev, name);
641 		else
642 			snprintf(buf, sizeof(buf), "%s%s/%s/%s.kmod",
643 			    kdev, base_path, name, name);
644 	}
645 
646 	return buf;
647 }
648 
649 static int
650 module_open(boot_module_t *bm, int mode, const char *kdev,
651     const char *base_path, bool doload)
652 {
653 	int fd;
654 	const char *path;
655 
656 	/* check the expanded path first */
657 	path = module_path(bm, kdev, base_path);
658 	fd = open(path, mode);
659 	if (fd != -1) {
660 		if ((howto & AB_SILENT) == 0 && doload)
661 			printf("Loading %s ", path);
662 	} else {
663 		/* now attempt the raw path provided */
664 		fd = open(bm->bm_path, mode);
665 		if (fd != -1 && (howto & AB_SILENT) == 0 && doload)
666 			printf("Loading %s ", bm->bm_path);
667 	}
668 	if (!doload && fd == -1) {
669 		printf("WARNING: couldn't open %s", bm->bm_path);
670 		if (strcmp(bm->bm_path, path) != 0)
671 			printf(" (%s)", path);
672 		printf("\n");
673 	}
674 	return fd;
675 }
676 
677 static void
678 module_base_path(char *buf, size_t bufsize, const char *kernel_path)
679 {
680 #ifdef KERNEL_DIR
681 	/* we cheat here, because %.* does not work with the mini printf */
682 	char *ptr = strrchr(kernel_path, '/');
683 	if (ptr) *ptr = '\0';
684 	snprintf(buf, bufsize, "%s/modules", kernel_path);
685 	if (ptr) *ptr = '/';
686 #else
687 	const char *machine;
688 
689 	switch (netbsd_elf_class) {
690 	case ELFCLASS32:
691 		machine = "i386";
692 		break;
693 	case ELFCLASS64:
694 		machine = "amd64";
695 		break;
696 	default:
697 		machine = "generic";
698 		break;
699 	}
700 	if (netbsd_version / 1000000 % 100 == 99) {
701 		/* -current */
702 		snprintf(buf, bufsize,
703 		    "/stand/%s/%d.%d.%d/modules", machine,
704 		    netbsd_version / 100000000,
705 		    netbsd_version / 1000000 % 100,
706 		    netbsd_version / 100 % 10000);
707 	} else if (netbsd_version != 0) {
708 		/* release */
709 		snprintf(buf, bufsize,
710 		    "/stand/%s/%d.%d/modules", machine,
711 		    netbsd_version / 100000000,
712 		    netbsd_version / 1000000 % 100);
713 	}
714 #endif
715 }
716 
717 static void
718 module_init(const char *kernel_path)
719 {
720 	struct bi_modulelist_entry *bi;
721 	struct stat st;
722 	char kdev[64];
723 	char *buf;
724 	boot_module_t *bm;
725 	ssize_t len;
726 	off_t off;
727 	int err, fd, nfail = 0;
728 
729 	extract_device(kernel_path, kdev, sizeof(kdev));
730 	module_base_path(module_base, sizeof(module_base), kernel_path);
731 
732 	/* First, see which modules are valid and calculate btinfo size */
733 	len = sizeof(struct btinfo_modulelist);
734 	for (bm = boot_modules; bm; bm = bm->bm_next) {
735 		fd = module_open(bm, 0, kdev, module_base, false);
736 		if (fd == -1) {
737 			bm->bm_len = -1;
738 			++nfail;
739 			continue;
740 		}
741 		err = fstat(fd, &st);
742 		if (err == -1 || st.st_size == -1) {
743 			printf("WARNING: couldn't stat %s\n", bm->bm_path);
744 			close(fd);
745 			bm->bm_len = -1;
746 			++nfail;
747 			continue;
748 		}
749 		bm->bm_len = st.st_size;
750 		close(fd);
751 		len += sizeof(struct bi_modulelist_entry);
752 	}
753 
754 	/* Allocate the module list */
755 	btinfo_modulelist = alloc(len);
756 	if (btinfo_modulelist == NULL) {
757 		printf("WARNING: couldn't allocate module list\n");
758 		wait_sec(MODULE_WARNING_SEC);
759 		return;
760 	}
761 	memset(btinfo_modulelist, 0, len);
762 	btinfo_modulelist_size = len;
763 
764 	/* Fill in btinfo structure */
765 	buf = (char *)btinfo_modulelist;
766 	btinfo_modulelist->num = 0;
767 	off = sizeof(struct btinfo_modulelist);
768 
769 	for (bm = boot_modules; bm; bm = bm->bm_next) {
770 		if (bm->bm_len == -1)
771 			continue;
772 		fd = module_open(bm, 0, kdev, module_base, true);
773 		if (fd == -1)
774 			continue;
775 		image_end = (image_end + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
776 		len = pread(fd, (void *)(uintptr_t)image_end, SSIZE_MAX);
777 		if (len < bm->bm_len) {
778 			if ((howto & AB_SILENT) != 0)
779 				printf("Loading %s ", bm->bm_path);
780 			printf(" FAILED\n");
781 		} else {
782 			btinfo_modulelist->num++;
783 			bi = (struct bi_modulelist_entry *)(buf + off);
784 			off += sizeof(struct bi_modulelist_entry);
785 			strncpy(bi->path, bm->bm_path, sizeof(bi->path) - 1);
786 			bi->base = image_end;
787 			bi->len = len;
788 			switch (bm->bm_type) {
789 			    case BM_TYPE_KMOD:
790 				bi->type = BI_MODULE_ELF;
791 				break;
792 			    case BM_TYPE_IMAGE:
793 				bi->type = BI_MODULE_IMAGE;
794 				break;
795 			    case BM_TYPE_FS:
796 				bi->type = BI_MODULE_FS;
797 				break;
798 			    case BM_TYPE_RND:
799 			    default:
800 				/* safest -- rnd checks the sha1 */
801 				bi->type = BI_MODULE_RND;
802 				break;
803 			}
804 			if ((howto & AB_SILENT) == 0)
805 				printf(" \n");
806 		}
807 		if (len > 0)
808 			image_end += len;
809 		close(fd);
810 	}
811 	btinfo_modulelist->endpa = image_end;
812 
813 	if (nfail > 0) {
814 		printf("WARNING: %d module%s failed to load\n",
815 		    nfail, nfail == 1 ? "" : "s");
816 #if notyet
817 		wait_sec(MODULE_WARNING_SEC);
818 #endif
819 	}
820 }
821 
822 static void
823 userconf_init(void)
824 {
825 	size_t count, len;
826 	userconf_command_t *uc;
827 	char *buf;
828 	off_t off;
829 
830 	/* Calculate the userconf commands list size */
831 	count = 0;
832 	for (uc = userconf_commands; uc != NULL; uc = uc->uc_next)
833 		count++;
834 	len = sizeof(*btinfo_userconfcommands) +
835 	      count * sizeof(struct bi_userconfcommand);
836 
837 	/* Allocate the userconf commands list */
838 	btinfo_userconfcommands = alloc(len);
839 	if (btinfo_userconfcommands == NULL) {
840 		printf("WARNING: couldn't allocate userconf commands list\n");
841 		return;
842 	}
843 	memset(btinfo_userconfcommands, 0, len);
844 	btinfo_userconfcommands_size = len;
845 
846 	/* Fill in btinfo structure */
847 	buf = (char *)btinfo_userconfcommands;
848 	off = sizeof(*btinfo_userconfcommands);
849 	btinfo_userconfcommands->num = 0;
850 	for (uc = userconf_commands; uc != NULL; uc = uc->uc_next) {
851 		struct bi_userconfcommand *bi;
852 		bi = (struct bi_userconfcommand *)(buf + off);
853 		strncpy(bi->text, uc->uc_text, sizeof(bi->text) - 1);
854 
855 		off += sizeof(*bi);
856 		btinfo_userconfcommands->num++;
857 	}
858 }
859 
860 int
861 exec_multiboot(const char *file, char *args)
862 {
863 	physaddr_t loadaddr = 0;
864 	u_long marks[MARK_MAX];
865 	u_long extmem;
866 	u_long basemem;
867 	struct multiboot_package *mbp = NULL;
868 
869 #ifndef NO_MULTIBOOT2
870 	if ((mbp = probe_multiboot2(file)) != NULL)
871 		goto is_multiboot;
872 #endif
873 
874 	if ((mbp = probe_multiboot1(file)) != NULL) {
875 #ifdef EFIBOOT
876 		printf("EFI boot requires multiboot 2 kernel\n");
877 		goto out;
878 #else
879 		goto is_multiboot;
880 #endif
881 	}
882 
883 #ifndef NO_MULTIBOOT2
884 	printf("%s is not a multiboot kernel\n", file);
885 #else
886 	printf("%s is not a multiboot 1 kernel "
887 	    "(multiboot 2 support is not built in)\n", file);
888 #endif
889 	goto out;
890 
891 is_multiboot:
892 #ifdef EFIBOOT
893 	loadaddr = efi_loadaddr;
894 #endif
895 	if (common_load_kernel(file, &basemem, &extmem, loadaddr, 0, marks))
896 		goto out;
897 
898 	if (boot_modules_enabled)
899 		module_init(file);
900 
901 	mbp->mbp_args = args;
902 	mbp->mbp_basemem = basemem;
903 	mbp->mbp_extmem = extmem;
904 	mbp->mbp_loadaddr = loadaddr;
905 	mbp->mbp_marks = marks;
906 
907 	/* Only returns on error */
908 	(void)mbp->mbp_exec(mbp);
909 
910 out:
911 	if (mbp != NULL)
912 		mbp->mbp_cleanup(mbp);
913 
914 	return -1;
915 }
916 
917 void
918 x86_progress(const char *fmt, ...)
919 {
920 	va_list ap;
921 
922 	if ((howto & AB_SILENT) != 0)
923 		return;
924 	va_start(ap, fmt);
925 	vprintf(fmt, ap);
926 	va_end(ap);
927 }
928