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