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