Home | History | Annotate | Line # | Download | only in lib
exec.c revision 1.63
      1 /*	$NetBSD: exec.c,v 1.63 2017/01/24 11:09:14 nonaka 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 static u_long efi_loadaddr;
    116 #endif
    117 
    118 #define BOOT_NARGS	6
    119 
    120 #ifndef	PAGE_SIZE
    121 #define	PAGE_SIZE	4096
    122 #endif
    123 
    124 #define MODULE_WARNING_SEC	5
    125 
    126 extern struct btinfo_console btinfo_console;
    127 
    128 boot_module_t *boot_modules;
    129 bool boot_modules_enabled = true;
    130 bool kernel_loaded;
    131 
    132 typedef struct userconf_command {
    133 	char *uc_text;
    134 	size_t uc_len;
    135 	struct userconf_command *uc_next;
    136 } userconf_command_t;
    137 userconf_command_t *userconf_commands = NULL;
    138 
    139 static struct btinfo_framebuffer btinfo_framebuffer;
    140 
    141 static struct btinfo_modulelist *btinfo_modulelist;
    142 static size_t btinfo_modulelist_size;
    143 static uint32_t image_end;
    144 static char module_base[64] = "/";
    145 static int howto;
    146 
    147 static struct btinfo_userconfcommands *btinfo_userconfcommands = NULL;
    148 static size_t btinfo_userconfcommands_size = 0;
    149 
    150 static void	module_init(const char *);
    151 static void	module_add_common(const char *, uint8_t);
    152 
    153 static void	userconf_init(void);
    154 
    155 void
    156 framebuffer_configure(struct btinfo_framebuffer *fb)
    157 {
    158 	if (fb)
    159 		btinfo_framebuffer = *fb;
    160 	else {
    161 		btinfo_framebuffer.physaddr = 0;
    162 		btinfo_framebuffer.flags = 0;
    163 	}
    164 }
    165 
    166 void
    167 module_add(char *name)
    168 {
    169 	return module_add_common(name, BM_TYPE_KMOD);
    170 }
    171 
    172 void
    173 splash_add(char *name)
    174 {
    175 	return module_add_common(name, BM_TYPE_IMAGE);
    176 }
    177 
    178 void
    179 rnd_add(char *name)
    180 {
    181 	return module_add_common(name, BM_TYPE_RND);
    182 }
    183 
    184 void
    185 fs_add(char *name)
    186 {
    187 	return module_add_common(name, BM_TYPE_FS);
    188 }
    189 
    190 static void
    191 module_add_common(const char *name, uint8_t type)
    192 {
    193 	boot_module_t *bm, *bmp;
    194 	size_t len;
    195 	char *str;
    196 
    197 	while (*name == ' ' || *name == '\t')
    198 		++name;
    199 
    200 	bm = alloc(sizeof(boot_module_t));
    201 	len = strlen(name) + 1;
    202 	str = alloc(len);
    203 	if (bm == NULL || str == NULL) {
    204 		printf("couldn't allocate module\n");
    205 		return;
    206 	}
    207 	memcpy(str, name, len);
    208 	bm->bm_path = str;
    209 	bm->bm_next = NULL;
    210 	bm->bm_type = type;
    211 	if (boot_modules == NULL)
    212 		boot_modules = bm;
    213 	else {
    214 		for (bmp = boot_modules; bmp->bm_next;
    215 		    bmp = bmp->bm_next)
    216 			;
    217 		bmp->bm_next = bm;
    218 	}
    219 }
    220 
    221 void
    222 userconf_add(char *cmd)
    223 {
    224 	userconf_command_t *uc;
    225 	size_t len;
    226 	char *text;
    227 
    228 	while (*cmd == ' ' || *cmd == '\t')
    229 		++cmd;
    230 
    231 	uc = alloc(sizeof(*uc));
    232 	if (uc == NULL) {
    233 		printf("couldn't allocate command\n");
    234 		return;
    235 	}
    236 
    237 	len = strlen(cmd) + 1;
    238 	text = alloc(len);
    239 	if (text == NULL) {
    240 		dealloc(uc, sizeof(*uc));
    241 		printf("couldn't allocate command\n");
    242 		return;
    243 	}
    244 	memcpy(text, cmd, len);
    245 
    246 	uc->uc_text = text;
    247 	uc->uc_len = len;
    248 	uc->uc_next = NULL;
    249 
    250 	if (userconf_commands == NULL)
    251 		userconf_commands = uc;
    252 	else {
    253 		userconf_command_t *ucp;
    254 		for (ucp = userconf_commands; ucp->uc_next != NULL;
    255 		     ucp = ucp->uc_next)
    256 			;
    257 		ucp->uc_next = uc;
    258 	}
    259 }
    260 
    261 static int
    262 common_load_kernel(const char *file, u_long *basemem, u_long *extmem,
    263     physaddr_t loadaddr, int floppy, u_long marks[MARK_MAX])
    264 {
    265 	int fd;
    266 #ifdef XMS
    267 	u_long xmsmem;
    268 	physaddr_t origaddr = loadaddr;
    269 #endif
    270 
    271 	*extmem = getextmem();
    272 	*basemem = getbasemem();
    273 
    274 #ifdef XMS
    275 	if ((getextmem1() == 0) && (xmsmem = checkxms())) {
    276 		u_long kernsize;
    277 
    278 		/*
    279 		 * With "CONSERVATIVE_MEMDETECT", extmem is 0 because
    280 		 * getextmem() is getextmem1(). Without, the "smart"
    281 		 * methods could fail to report all memory as well.
    282 		 * xmsmem is a few kB less than the actual size, but
    283 		 * better than nothing.
    284 		 */
    285 		if (xmsmem > *extmem)
    286 			*extmem = xmsmem;
    287 		/*
    288 		 * Get the size of the kernel
    289 		 */
    290 		marks[MARK_START] = loadaddr;
    291 		if ((fd = loadfile(file, marks, COUNT_KERNEL)) == -1)
    292 			return EIO;
    293 		close(fd);
    294 
    295 		kernsize = marks[MARK_END];
    296 		kernsize = (kernsize + 1023) / 1024;
    297 
    298 		loadaddr = xmsalloc(kernsize);
    299 		if (!loadaddr)
    300 			return ENOMEM;
    301 	}
    302 #endif
    303 #ifdef EFIBOOT
    304 	{
    305 		EFI_STATUS status;
    306 		EFI_PHYSICAL_ADDRESS addr;
    307 		UINTN kernsize;
    308 
    309 		marks[MARK_START] = loadaddr;
    310 		if ((fd = loadfile(file, marks, COUNT_KERNEL)) == -1)
    311 			return EIO;
    312 		close(fd);
    313 
    314 		/* Allocate temporary arena. */
    315 		addr = EFI_ALLOCATE_MAX_ADDRESS;
    316 		kernsize = marks[MARK_END] - loadaddr;
    317 		kernsize += 1 * 1024 * 1024;	/* XXX: kernel size COUNT_KERNEL vs LOAD_KERNL (lacked some SYMTAB?) */
    318 		kernsize = EFI_SIZE_TO_PAGES(kernsize);
    319 		status = uefi_call_wrapper(BS->AllocatePages, 4,
    320 		    AllocateMaxAddress, EfiLoaderData, kernsize, &addr);
    321 		if (EFI_ERROR(status))
    322 			return ENOMEM;
    323 		efi_loadaddr = loadaddr = addr;
    324 
    325 		memset(marks, 0, sizeof(marks[0]) * MARK_MAX);
    326 	}
    327 #endif
    328 	marks[MARK_START] = loadaddr;
    329 	if ((fd = loadfile(file, marks,
    330 	    LOAD_KERNEL & ~(floppy ? LOAD_BACKWARDS : 0))) == -1)
    331 		return EIO;
    332 
    333 	close(fd);
    334 
    335 	/* If the root fs type is unusual, load its module. */
    336 	if (fsmod != NULL)
    337 		module_add_common(fsmod, BM_TYPE_KMOD);
    338 
    339 	/*
    340 	 * Gather some information for the kernel. Do this after the
    341 	 * "point of no return" to avoid memory leaks.
    342 	 * (but before DOS might be trashed in the XMS case)
    343 	 */
    344 #ifdef PASS_BIOSGEOM
    345 	bi_getbiosgeom();
    346 #endif
    347 #ifdef PASS_MEMMAP
    348 	bi_getmemmap();
    349 #endif
    350 
    351 #ifdef XMS
    352 	if (loadaddr != origaddr) {
    353 		/*
    354 		 * We now have done our last DOS IO, so we may
    355 		 * trash the OS. Copy the data from the temporary
    356 		 * buffer to its real address.
    357 		 */
    358 		marks[MARK_START] -= loadaddr;
    359 		marks[MARK_END] -= loadaddr;
    360 		marks[MARK_SYM] -= loadaddr;
    361 		marks[MARK_END] -= loadaddr;
    362 		ppbcopy(loadaddr, origaddr, marks[MARK_END]);
    363 	}
    364 #endif
    365 #ifdef EFIBOOT
    366 	marks[MARK_START] -= loadaddr;
    367 	marks[MARK_ENTRY] -= loadaddr;
    368 	marks[MARK_DATA] -= loadaddr;
    369 	/* MARK_NSYM */
    370 	marks[MARK_SYM] -= loadaddr;
    371 	marks[MARK_END] -= loadaddr;
    372 	/* Copy the kernel to original load address later. */
    373 #endif
    374 	marks[MARK_END] = (((u_long) marks[MARK_END] + sizeof(int) - 1)) &
    375 	    (-sizeof(int));
    376 	image_end = marks[MARK_END];
    377 	kernel_loaded = true;
    378 
    379 	return 0;
    380 }
    381 
    382 int
    383 exec_netbsd(const char *file, physaddr_t loadaddr, int boothowto, int floppy,
    384     void (*callback)(void))
    385 {
    386 	uint32_t boot_argv[BOOT_NARGS];
    387 	u_long marks[MARK_MAX];
    388 	struct btinfo_symtab btinfo_symtab;
    389 	u_long extmem;
    390 	u_long basemem;
    391 	int error;
    392 
    393 #ifdef	DEBUG
    394 	printf("exec: file=%s loadaddr=0x%lx\n", file ? file : "NULL",
    395 	    loadaddr);
    396 #endif
    397 
    398 	BI_ALLOC(BTINFO_MAX);
    399 
    400 	BI_ADD(&btinfo_console, BTINFO_CONSOLE, sizeof(struct btinfo_console));
    401 
    402 	howto = boothowto;
    403 
    404 	memset(marks, 0, sizeof(marks));
    405 
    406 	error = common_load_kernel(file, &basemem, &extmem, loadaddr, floppy,
    407 	    marks);
    408 	if (error) {
    409 		errno = error;
    410 		goto out;
    411 	}
    412 
    413 	boot_argv[0] = boothowto;
    414 	boot_argv[1] = 0;
    415 	boot_argv[2] = vtophys(bootinfo);	/* old cyl offset */
    416 	boot_argv[3] = marks[MARK_END];
    417 	boot_argv[4] = extmem;
    418 	boot_argv[5] = basemem;
    419 
    420 	/* pull in any modules if necessary */
    421 	if (boot_modules_enabled) {
    422 		module_init(file);
    423 		if (btinfo_modulelist) {
    424 			BI_ADD(btinfo_modulelist, BTINFO_MODULELIST,
    425 			    btinfo_modulelist_size);
    426 		}
    427 	}
    428 
    429 	userconf_init();
    430 	if (btinfo_userconfcommands != NULL)
    431 		BI_ADD(btinfo_userconfcommands, BTINFO_USERCONFCOMMANDS,
    432 		    btinfo_userconfcommands_size);
    433 
    434 #ifdef DEBUG
    435 	printf("Start @ 0x%lx [%ld=0x%lx-0x%lx]...\n", marks[MARK_ENTRY],
    436 	    marks[MARK_NSYM], marks[MARK_SYM], marks[MARK_END]);
    437 #endif
    438 
    439 	btinfo_symtab.nsym = marks[MARK_NSYM];
    440 	btinfo_symtab.ssym = marks[MARK_SYM];
    441 	btinfo_symtab.esym = marks[MARK_END];
    442 	BI_ADD(&btinfo_symtab, BTINFO_SYMTAB, sizeof(struct btinfo_symtab));
    443 
    444 	/* set new video mode if necessary */
    445 	vbe_commit();
    446 	BI_ADD(&btinfo_framebuffer, BTINFO_FRAMEBUFFER,
    447 	    sizeof(struct btinfo_framebuffer));
    448 
    449 	if (callback != NULL)
    450 		(*callback)();
    451 #ifdef EFIBOOT
    452 	/* Copy the kernel to original load address. */
    453 	memmove((void *)marks[MARK_START],
    454 	    (void *)(efi_loadaddr + marks[MARK_START]),
    455 	    marks[MARK_END] - marks[MARK_START]);
    456 #endif
    457 	startprog(marks[MARK_ENTRY], BOOT_NARGS, boot_argv,
    458 	    x86_trunc_page(basemem * 1024));
    459 	panic("exec returned");
    460 
    461 out:
    462 	BI_FREE();
    463 	bootinfo = NULL;
    464 	return -1;
    465 }
    466 
    467 static void
    468 extract_device(const char *path, char *buf, size_t buflen)
    469 {
    470 	size_t i;
    471 
    472 	if (strchr(path, ':') != NULL) {
    473 		for (i = 0; i < buflen - 2 && path[i] != ':'; i++)
    474 			buf[i] = path[i];
    475 		buf[i++] = ':';
    476 		buf[i] = '\0';
    477 	} else
    478 		buf[0] = '\0';
    479 }
    480 
    481 static const char *
    482 module_path(boot_module_t *bm, const char *kdev)
    483 {
    484 	static char buf[256];
    485 	char name_buf[256], dev_buf[64];
    486 	const char *name, *name2, *p;
    487 
    488 	name = bm->bm_path;
    489 	for (name2 = name; *name2; ++name2) {
    490 		if (*name2 == ' ' || *name2 == '\t') {
    491 			strlcpy(name_buf, name, sizeof(name_buf));
    492 			if ((uintptr_t)name2 - (uintptr_t)name < sizeof(name_buf))
    493 				name_buf[name2 - name] = '\0';
    494 			name = name_buf;
    495 			break;
    496 		}
    497 	}
    498 	if ((p = strchr(name, ':')) != NULL) {
    499 		/* device specified, use it */
    500 		if (p[1] == '/')
    501 			snprintf(buf, sizeof(buf), "%s", name);
    502 		else {
    503 			p++;
    504 			extract_device(name, dev_buf, sizeof(dev_buf));
    505 			snprintf(buf, sizeof(buf), "%s%s/%s/%s.kmod",
    506 			    dev_buf, module_base, p, p);
    507 		}
    508 	} else {
    509 		/* device not specified; load from kernel device if known */
    510 		if (name[0] == '/')
    511 			snprintf(buf, sizeof(buf), "%s%s", kdev, name);
    512 		else
    513 			snprintf(buf, sizeof(buf), "%s%s/%s/%s.kmod",
    514 			    kdev, module_base, name, name);
    515 	}
    516 
    517 	return buf;
    518 }
    519 
    520 static int
    521 module_open(boot_module_t *bm, int mode, const char *kdev, bool doload)
    522 {
    523 	int fd;
    524 	const char *path;
    525 
    526 	/* check the expanded path first */
    527 	path = module_path(bm, kdev);
    528 	fd = open(path, mode);
    529 	if (fd != -1) {
    530 		if ((howto & AB_SILENT) == 0 && doload)
    531 			printf("Loading %s ", path);
    532 	} else {
    533 		/* now attempt the raw path provided */
    534 		fd = open(bm->bm_path, mode);
    535 		if (fd != -1 && (howto & AB_SILENT) == 0 && doload)
    536 			printf("Loading %s ", bm->bm_path);
    537 	}
    538 	if (!doload && fd == -1) {
    539 		printf("WARNING: couldn't open %s", bm->bm_path);
    540 		if (strcmp(bm->bm_path, path) != 0)
    541 			printf(" (%s)", path);
    542 		printf("\n");
    543 	}
    544 	return fd;
    545 }
    546 
    547 static void
    548 module_init(const char *kernel_path)
    549 {
    550 	struct bi_modulelist_entry *bi;
    551 	struct stat st;
    552 	const char *machine;
    553 	char kdev[64];
    554 	char *buf;
    555 	boot_module_t *bm;
    556 	ssize_t len;
    557 	off_t off;
    558 	int err, fd, nfail = 0;
    559 
    560 	extract_device(kernel_path, kdev, sizeof(kdev));
    561 
    562 	switch (netbsd_elf_class) {
    563 	case ELFCLASS32:
    564 		machine = "i386";
    565 		break;
    566 	case ELFCLASS64:
    567 		machine = "amd64";
    568 		break;
    569 	default:
    570 		machine = "generic";
    571 		break;
    572 	}
    573 	if (netbsd_version / 1000000 % 100 == 99) {
    574 		/* -current */
    575 		snprintf(module_base, sizeof(module_base),
    576 		    "/stand/%s/%d.%d.%d/modules", machine,
    577 		    netbsd_version / 100000000,
    578 		    netbsd_version / 1000000 % 100,
    579 		    netbsd_version / 100 % 100);
    580 	} else if (netbsd_version != 0) {
    581 		/* release */
    582 		snprintf(module_base, sizeof(module_base),
    583 		    "/stand/%s/%d.%d/modules", machine,
    584 		    netbsd_version / 100000000,
    585 		    netbsd_version / 1000000 % 100);
    586 	}
    587 
    588 	/* First, see which modules are valid and calculate btinfo size */
    589 	len = sizeof(struct btinfo_modulelist);
    590 	for (bm = boot_modules; bm; bm = bm->bm_next) {
    591 		fd = module_open(bm, 0, kdev, false);
    592 		if (fd == -1) {
    593 			bm->bm_len = -1;
    594 			++nfail;
    595 			continue;
    596 		}
    597 		err = fstat(fd, &st);
    598 		if (err == -1 || st.st_size == -1) {
    599 			printf("WARNING: couldn't stat %s\n", bm->bm_path);
    600 			close(fd);
    601 			bm->bm_len = -1;
    602 			++nfail;
    603 			continue;
    604 		}
    605 		bm->bm_len = st.st_size;
    606 		close(fd);
    607 		len += sizeof(struct bi_modulelist_entry);
    608 	}
    609 
    610 	/* Allocate the module list */
    611 	btinfo_modulelist = alloc(len);
    612 	if (btinfo_modulelist == NULL) {
    613 		printf("WARNING: couldn't allocate module list\n");
    614 		wait_sec(MODULE_WARNING_SEC);
    615 		return;
    616 	}
    617 	memset(btinfo_modulelist, 0, len);
    618 	btinfo_modulelist_size = len;
    619 
    620 	/* Fill in btinfo structure */
    621 	buf = (char *)btinfo_modulelist;
    622 	btinfo_modulelist->num = 0;
    623 	off = sizeof(struct btinfo_modulelist);
    624 
    625 	for (bm = boot_modules; bm; bm = bm->bm_next) {
    626 		if (bm->bm_len == -1)
    627 			continue;
    628 		fd = module_open(bm, 0, kdev, true);
    629 		if (fd == -1)
    630 			continue;
    631 		image_end = (image_end + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
    632 		len = pread(fd, (void *)(uintptr_t)image_end, SSIZE_MAX);
    633 		if (len < bm->bm_len) {
    634 			if ((howto & AB_SILENT) != 0)
    635 				printf("Loading %s ", bm->bm_path);
    636 			printf(" FAILED\n");
    637 		} else {
    638 			btinfo_modulelist->num++;
    639 			bi = (struct bi_modulelist_entry *)(buf + off);
    640 			off += sizeof(struct bi_modulelist_entry);
    641 			strncpy(bi->path, bm->bm_path, sizeof(bi->path) - 1);
    642 			bi->base = image_end;
    643 			bi->len = len;
    644 			switch (bm->bm_type) {
    645 			    case BM_TYPE_KMOD:
    646 				bi->type = BI_MODULE_ELF;
    647 				break;
    648 			    case BM_TYPE_IMAGE:
    649 				bi->type = BI_MODULE_IMAGE;
    650 				break;
    651 			    case BM_TYPE_FS:
    652 				bi->type = BI_MODULE_FS;
    653 				break;
    654 			    case BM_TYPE_RND:
    655 			    default:
    656 				/* safest -- rnd checks the sha1 */
    657 				bi->type = BI_MODULE_RND;
    658 				break;
    659 			}
    660 			if ((howto & AB_SILENT) == 0)
    661 				printf(" \n");
    662 		}
    663 		if (len > 0)
    664 			image_end += len;
    665 		close(fd);
    666 	}
    667 	btinfo_modulelist->endpa = image_end;
    668 
    669 	if (nfail > 0) {
    670 		printf("WARNING: %d module%s failed to load\n",
    671 		    nfail, nfail == 1 ? "" : "s");
    672 #if notyet
    673 		wait_sec(MODULE_WARNING_SEC);
    674 #endif
    675 	}
    676 }
    677 
    678 static void
    679 userconf_init(void)
    680 {
    681 	size_t count, len;
    682 	userconf_command_t *uc;
    683 	char *buf;
    684 	off_t off;
    685 
    686 	/* Calculate the userconf commands list size */
    687 	count = 0;
    688 	for (uc = userconf_commands; uc != NULL; uc = uc->uc_next)
    689 		count++;
    690 	len = sizeof(*btinfo_userconfcommands) +
    691 	      count * sizeof(struct bi_userconfcommand);
    692 
    693 	/* Allocate the userconf commands list */
    694 	btinfo_userconfcommands = alloc(len);
    695 	if (btinfo_userconfcommands == NULL) {
    696 		printf("WARNING: couldn't allocate userconf commands list\n");
    697 		return;
    698 	}
    699 	memset(btinfo_userconfcommands, 0, len);
    700 	btinfo_userconfcommands_size = len;
    701 
    702 	/* Fill in btinfo structure */
    703 	buf = (char *)btinfo_userconfcommands;
    704 	off = sizeof(*btinfo_userconfcommands);
    705 	btinfo_userconfcommands->num = 0;
    706 	for (uc = userconf_commands; uc != NULL; uc = uc->uc_next) {
    707 		struct bi_userconfcommand *bi;
    708 		bi = (struct bi_userconfcommand *)(buf + off);
    709 		strncpy(bi->text, uc->uc_text, sizeof(bi->text) - 1);
    710 
    711 		off += sizeof(*bi);
    712 		btinfo_userconfcommands->num++;
    713 	}
    714 }
    715 
    716 int
    717 exec_multiboot(const char *file, char *args)
    718 {
    719 	struct multiboot_info *mbi;
    720 	struct multiboot_module *mbm;
    721 	struct bi_modulelist_entry *bim;
    722 	int i, len;
    723 	u_long marks[MARK_MAX];
    724 	u_long extmem;
    725 	u_long basemem;
    726 	char *cmdline;
    727 
    728 	mbi = alloc(sizeof(struct multiboot_info));
    729 	mbi->mi_flags = MULTIBOOT_INFO_HAS_MEMORY;
    730 
    731 	if (common_load_kernel(file, &basemem, &extmem, 0, 0, marks))
    732 		goto out;
    733 
    734 	mbi->mi_mem_upper = extmem;
    735 	mbi->mi_mem_lower = basemem;
    736 
    737 	if (args) {
    738 		mbi->mi_flags |= MULTIBOOT_INFO_HAS_CMDLINE;
    739 		len = strlen(file) + 1 + strlen(args) + 1;
    740 		cmdline = alloc(len);
    741 		snprintf(cmdline, len, "%s %s", file, args);
    742 		mbi->mi_cmdline = (char *) vtophys(cmdline);
    743 	}
    744 
    745 	/* pull in any modules if necessary */
    746 	if (boot_modules_enabled) {
    747 		module_init(file);
    748 		if (btinfo_modulelist) {
    749 			mbm = alloc(sizeof(struct multiboot_module) *
    750 					   btinfo_modulelist->num);
    751 
    752 			bim = (struct bi_modulelist_entry *)
    753 			  (((char *) btinfo_modulelist) +
    754 			   sizeof(struct btinfo_modulelist));
    755 			for (i = 0; i < btinfo_modulelist->num; i++) {
    756 				mbm[i].mmo_start = bim->base;
    757 				mbm[i].mmo_end = bim->base + bim->len;
    758 				mbm[i].mmo_string = (char *)vtophys(bim->path);
    759 				mbm[i].mmo_reserved = 0;
    760 				bim++;
    761 			}
    762 			mbi->mi_flags |= MULTIBOOT_INFO_HAS_MODS;
    763 			mbi->mi_mods_count = btinfo_modulelist->num;
    764 			mbi->mi_mods_addr = vtophys(mbm);
    765 		}
    766 	}
    767 
    768 #ifdef DEBUG
    769 	printf("Start @ 0x%lx [%ld=0x%lx-0x%lx]...\n", marks[MARK_ENTRY],
    770 	    marks[MARK_NSYM], marks[MARK_SYM], marks[MARK_END]);
    771 #endif
    772 
    773 #if 0
    774 	if (btinfo_symtab.nsym) {
    775 		mbi->mi_flags |= MULTIBOOT_INFO_HAS_ELF_SYMS;
    776 		mbi->mi_elfshdr_addr = marks[MARK_SYM];
    777 	btinfo_symtab.nsym = marks[MARK_NSYM];
    778 	btinfo_symtab.ssym = marks[MARK_SYM];
    779 	btinfo_symtab.esym = marks[MARK_END];
    780 #endif
    781 
    782 	multiboot(marks[MARK_ENTRY], vtophys(mbi),
    783 	    x86_trunc_page(mbi->mi_mem_lower * 1024));
    784 	panic("exec returned");
    785 
    786 out:
    787 	dealloc(mbi, 0);
    788 	return -1;
    789 }
    790 
    791 void
    792 x86_progress(const char *fmt, ...)
    793 {
    794 	va_list ap;
    795 
    796 	if ((howto & AB_SILENT) != 0)
    797 		return;
    798 	va_start(ap, fmt);
    799 	vprintf(fmt, ap);
    800 	va_end(ap);
    801 }
    802