Home | History | Annotate | Line # | Download | only in lib
exec.c revision 1.68.6.1
      1 /*	$NetBSD: exec.c,v 1.68.6.1 2019/08/01 13:22:48 martin 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 static int
    270 common_load_kernel(const char *file, u_long *basemem, u_long *extmem,
    271     physaddr_t loadaddr, int floppy, u_long marks[MARK_MAX])
    272 {
    273 	int fd;
    274 #ifdef XMS
    275 	u_long xmsmem;
    276 	physaddr_t origaddr = loadaddr;
    277 #endif
    278 
    279 	*extmem = getextmem();
    280 	*basemem = getbasemem();
    281 
    282 #ifdef XMS
    283 	if ((getextmem1() == 0) && (xmsmem = checkxms())) {
    284 		u_long kernsize;
    285 
    286 		/*
    287 		 * With "CONSERVATIVE_MEMDETECT", extmem is 0 because
    288 		 * getextmem() is getextmem1(). Without, the "smart"
    289 		 * methods could fail to report all memory as well.
    290 		 * xmsmem is a few kB less than the actual size, but
    291 		 * better than nothing.
    292 		 */
    293 		if (xmsmem > *extmem)
    294 			*extmem = xmsmem;
    295 		/*
    296 		 * Get the size of the kernel
    297 		 */
    298 		marks[MARK_START] = loadaddr;
    299 		if ((fd = loadfile(file, marks, COUNT_KERNEL)) == -1)
    300 			return EIO;
    301 		close(fd);
    302 
    303 		kernsize = marks[MARK_END];
    304 		kernsize = (kernsize + 1023) / 1024;
    305 
    306 		loadaddr = xmsalloc(kernsize);
    307 		if (!loadaddr)
    308 			return ENOMEM;
    309 	}
    310 #endif
    311 	marks[MARK_START] = loadaddr;
    312 	if ((fd = loadfile(file, marks,
    313 	    LOAD_KERNEL & ~(floppy ? LOAD_BACKWARDS : 0))) == -1)
    314 		return EIO;
    315 
    316 	close(fd);
    317 
    318 	/* If the root fs type is unusual, load its module. */
    319 	if (fsmod != NULL)
    320 		module_add_common(fsmod, BM_TYPE_KMOD);
    321 
    322 	/*
    323 	 * Gather some information for the kernel. Do this after the
    324 	 * "point of no return" to avoid memory leaks.
    325 	 * (but before DOS might be trashed in the XMS case)
    326 	 */
    327 #ifdef PASS_BIOSGEOM
    328 	bi_getbiosgeom();
    329 #endif
    330 #ifdef PASS_MEMMAP
    331 	bi_getmemmap();
    332 #endif
    333 
    334 #ifdef XMS
    335 	if (loadaddr != origaddr) {
    336 		/*
    337 		 * We now have done our last DOS IO, so we may
    338 		 * trash the OS. Copy the data from the temporary
    339 		 * buffer to its real address.
    340 		 */
    341 		marks[MARK_START] -= loadaddr;
    342 		marks[MARK_END] -= loadaddr;
    343 		marks[MARK_SYM] -= loadaddr;
    344 		marks[MARK_END] -= loadaddr;
    345 		ppbcopy(loadaddr, origaddr, marks[MARK_END]);
    346 	}
    347 #endif
    348 	marks[MARK_END] = (((u_long) marks[MARK_END] + sizeof(int) - 1)) &
    349 	    (-sizeof(int));
    350 	image_end = marks[MARK_END];
    351 	kernel_loaded = true;
    352 
    353 	return 0;
    354 }
    355 
    356 int
    357 exec_netbsd(const char *file, physaddr_t loadaddr, int boothowto, int floppy,
    358     void (*callback)(void))
    359 {
    360 	uint32_t boot_argv[BOOT_NARGS];
    361 	u_long marks[MARK_MAX];
    362 	struct btinfo_symtab btinfo_symtab;
    363 	u_long extmem;
    364 	u_long basemem;
    365 	int error;
    366 #ifdef EFIBOOT
    367 	int i;
    368 #endif
    369 
    370 #ifdef	DEBUG
    371 	printf("exec: file=%s loadaddr=0x%lx\n", file ? file : "NULL",
    372 	    loadaddr);
    373 #endif
    374 
    375 	BI_ALLOC(BTINFO_MAX);
    376 
    377 	BI_ADD(&btinfo_console, BTINFO_CONSOLE, sizeof(struct btinfo_console));
    378 
    379 	howto = boothowto;
    380 
    381 	memset(marks, 0, sizeof(marks));
    382 
    383 	error = common_load_kernel(file, &basemem, &extmem, loadaddr, floppy,
    384 	    marks);
    385 	if (error) {
    386 		errno = error;
    387 		goto out;
    388 	}
    389 #ifdef EFIBOOT
    390 	/* adjust to the real load address */
    391 	marks[MARK_START] -= efi_loadaddr;
    392 	marks[MARK_ENTRY] -= efi_loadaddr;
    393 	marks[MARK_DATA] -= efi_loadaddr;
    394 	/* MARK_NSYM */
    395 	marks[MARK_SYM] -= efi_loadaddr;
    396 	marks[MARK_END] -= efi_loadaddr;
    397 #endif
    398 
    399 	boot_argv[0] = boothowto;
    400 	boot_argv[1] = 0;
    401 	boot_argv[2] = vtophys(bootinfo);	/* old cyl offset */
    402 	boot_argv[3] = marks[MARK_END];
    403 	boot_argv[4] = extmem;
    404 	boot_argv[5] = basemem;
    405 
    406 	/* pull in any modules if necessary */
    407 	if (boot_modules_enabled) {
    408 		module_init(file);
    409 		if (btinfo_modulelist) {
    410 #ifdef EFIBOOT
    411 			/* convert module loaded address to paddr */
    412 			struct bi_modulelist_entry *bim;
    413 			bim = (void *)(btinfo_modulelist + 1);
    414 			for (i = 0; i < btinfo_modulelist->num; i++, bim++)
    415 				bim->base -= efi_loadaddr;
    416 			btinfo_modulelist->endpa -= efi_loadaddr;
    417 #endif
    418 			BI_ADD(btinfo_modulelist, BTINFO_MODULELIST,
    419 			    btinfo_modulelist_size);
    420 		}
    421 	}
    422 
    423 	userconf_init();
    424 	if (btinfo_userconfcommands != NULL)
    425 		BI_ADD(btinfo_userconfcommands, BTINFO_USERCONFCOMMANDS,
    426 		    btinfo_userconfcommands_size);
    427 
    428 #ifdef DEBUG
    429 	printf("Start @ 0x%lx [%ld=0x%lx-0x%lx]...\n", marks[MARK_ENTRY],
    430 	    marks[MARK_NSYM], marks[MARK_SYM], marks[MARK_END]);
    431 #endif
    432 
    433 	btinfo_symtab.nsym = marks[MARK_NSYM];
    434 	btinfo_symtab.ssym = marks[MARK_SYM];
    435 	btinfo_symtab.esym = marks[MARK_END];
    436 	BI_ADD(&btinfo_symtab, BTINFO_SYMTAB, sizeof(struct btinfo_symtab));
    437 
    438 	/* set new video mode if necessary */
    439 	vbe_commit();
    440 	BI_ADD(&btinfo_framebuffer, BTINFO_FRAMEBUFFER,
    441 	    sizeof(struct btinfo_framebuffer));
    442 
    443 	if (callback != NULL)
    444 		(*callback)();
    445 #ifdef EFIBOOT
    446 	/* Copy bootinfo to safe arena. */
    447 	for (i = 0; i < bootinfo->nentries; i++) {
    448 		struct btinfo_common *bi = (void *)(u_long)bootinfo->entry[i];
    449 		char *p = alloc(bi->len);
    450 		memcpy(p, bi, bi->len);
    451 		bootinfo->entry[i] = vtophys(p);
    452 	}
    453 
    454 	efi_kernel_start = marks[MARK_START];
    455 	efi_kernel_size = image_end - efi_loadaddr - efi_kernel_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, const char *base_path)
    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, base_path, 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, base_path, name, name);
    515 	}
    516 
    517 	return buf;
    518 }
    519 
    520 static int
    521 module_open(boot_module_t *bm, int mode, const char *kdev,
    522     const char *base_path, bool doload)
    523 {
    524 	int fd;
    525 	const char *path;
    526 
    527 	/* check the expanded path first */
    528 	path = module_path(bm, kdev, base_path);
    529 	fd = open(path, mode);
    530 	if (fd != -1) {
    531 		if ((howto & AB_SILENT) == 0 && doload)
    532 			printf("Loading %s ", path);
    533 	} else {
    534 		/* now attempt the raw path provided */
    535 		fd = open(bm->bm_path, mode);
    536 		if (fd != -1 && (howto & AB_SILENT) == 0 && doload)
    537 			printf("Loading %s ", bm->bm_path);
    538 	}
    539 	if (!doload && fd == -1) {
    540 		printf("WARNING: couldn't open %s", bm->bm_path);
    541 		if (strcmp(bm->bm_path, path) != 0)
    542 			printf(" (%s)", path);
    543 		printf("\n");
    544 	}
    545 	return fd;
    546 }
    547 
    548 static void
    549 module_base_path(char *buf, size_t bufsize)
    550 {
    551 	const char *machine;
    552 
    553 	switch (netbsd_elf_class) {
    554 	case ELFCLASS32:
    555 		machine = "i386";
    556 		break;
    557 	case ELFCLASS64:
    558 		machine = "amd64";
    559 		break;
    560 	default:
    561 		machine = "generic";
    562 		break;
    563 	}
    564 	if (netbsd_version / 1000000 % 100 == 99) {
    565 		/* -current */
    566 		snprintf(buf, bufsize,
    567 		    "/stand/%s/%d.%d.%d/modules", machine,
    568 		    netbsd_version / 100000000,
    569 		    netbsd_version / 1000000 % 100,
    570 		    netbsd_version / 100 % 100);
    571 	} else if (netbsd_version != 0) {
    572 		/* release */
    573 		snprintf(buf, bufsize,
    574 		    "/stand/%s/%d.%d/modules", machine,
    575 		    netbsd_version / 100000000,
    576 		    netbsd_version / 1000000 % 100);
    577 	}
    578 }
    579 
    580 static void
    581 module_init(const char *kernel_path)
    582 {
    583 	struct bi_modulelist_entry *bi;
    584 	struct stat st;
    585 	char kdev[64];
    586 	char *buf;
    587 	boot_module_t *bm;
    588 	ssize_t len;
    589 	off_t off;
    590 	int err, fd, nfail = 0;
    591 
    592 	extract_device(kernel_path, kdev, sizeof(kdev));
    593 	module_base_path(module_base, sizeof(module_base));
    594 
    595 	/* First, see which modules are valid and calculate btinfo size */
    596 	len = sizeof(struct btinfo_modulelist);
    597 	for (bm = boot_modules; bm; bm = bm->bm_next) {
    598 		fd = module_open(bm, 0, kdev, module_base, false);
    599 		if (fd == -1) {
    600 			bm->bm_len = -1;
    601 			++nfail;
    602 			continue;
    603 		}
    604 		err = fstat(fd, &st);
    605 		if (err == -1 || st.st_size == -1) {
    606 			printf("WARNING: couldn't stat %s\n", bm->bm_path);
    607 			close(fd);
    608 			bm->bm_len = -1;
    609 			++nfail;
    610 			continue;
    611 		}
    612 		bm->bm_len = st.st_size;
    613 		close(fd);
    614 		len += sizeof(struct bi_modulelist_entry);
    615 	}
    616 
    617 	/* Allocate the module list */
    618 	btinfo_modulelist = alloc(len);
    619 	if (btinfo_modulelist == NULL) {
    620 		printf("WARNING: couldn't allocate module list\n");
    621 		wait_sec(MODULE_WARNING_SEC);
    622 		return;
    623 	}
    624 	memset(btinfo_modulelist, 0, len);
    625 	btinfo_modulelist_size = len;
    626 
    627 	/* Fill in btinfo structure */
    628 	buf = (char *)btinfo_modulelist;
    629 	btinfo_modulelist->num = 0;
    630 	off = sizeof(struct btinfo_modulelist);
    631 
    632 	for (bm = boot_modules; bm; bm = bm->bm_next) {
    633 		if (bm->bm_len == -1)
    634 			continue;
    635 		fd = module_open(bm, 0, kdev, module_base, true);
    636 		if (fd == -1)
    637 			continue;
    638 		image_end = (image_end + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
    639 		len = pread(fd, (void *)(uintptr_t)image_end, SSIZE_MAX);
    640 		if (len < bm->bm_len) {
    641 			if ((howto & AB_SILENT) != 0)
    642 				printf("Loading %s ", bm->bm_path);
    643 			printf(" FAILED\n");
    644 		} else {
    645 			btinfo_modulelist->num++;
    646 			bi = (struct bi_modulelist_entry *)(buf + off);
    647 			off += sizeof(struct bi_modulelist_entry);
    648 			strncpy(bi->path, bm->bm_path, sizeof(bi->path) - 1);
    649 			bi->base = image_end;
    650 			bi->len = len;
    651 			switch (bm->bm_type) {
    652 			    case BM_TYPE_KMOD:
    653 				bi->type = BI_MODULE_ELF;
    654 				break;
    655 			    case BM_TYPE_IMAGE:
    656 				bi->type = BI_MODULE_IMAGE;
    657 				break;
    658 			    case BM_TYPE_FS:
    659 				bi->type = BI_MODULE_FS;
    660 				break;
    661 			    case BM_TYPE_RND:
    662 			    default:
    663 				/* safest -- rnd checks the sha1 */
    664 				bi->type = BI_MODULE_RND;
    665 				break;
    666 			}
    667 			if ((howto & AB_SILENT) == 0)
    668 				printf(" \n");
    669 		}
    670 		if (len > 0)
    671 			image_end += len;
    672 		close(fd);
    673 	}
    674 	btinfo_modulelist->endpa = image_end;
    675 
    676 	if (nfail > 0) {
    677 		printf("WARNING: %d module%s failed to load\n",
    678 		    nfail, nfail == 1 ? "" : "s");
    679 #if notyet
    680 		wait_sec(MODULE_WARNING_SEC);
    681 #endif
    682 	}
    683 }
    684 
    685 static void
    686 userconf_init(void)
    687 {
    688 	size_t count, len;
    689 	userconf_command_t *uc;
    690 	char *buf;
    691 	off_t off;
    692 
    693 	/* Calculate the userconf commands list size */
    694 	count = 0;
    695 	for (uc = userconf_commands; uc != NULL; uc = uc->uc_next)
    696 		count++;
    697 	len = sizeof(*btinfo_userconfcommands) +
    698 	      count * sizeof(struct bi_userconfcommand);
    699 
    700 	/* Allocate the userconf commands list */
    701 	btinfo_userconfcommands = alloc(len);
    702 	if (btinfo_userconfcommands == NULL) {
    703 		printf("WARNING: couldn't allocate userconf commands list\n");
    704 		return;
    705 	}
    706 	memset(btinfo_userconfcommands, 0, len);
    707 	btinfo_userconfcommands_size = len;
    708 
    709 	/* Fill in btinfo structure */
    710 	buf = (char *)btinfo_userconfcommands;
    711 	off = sizeof(*btinfo_userconfcommands);
    712 	btinfo_userconfcommands->num = 0;
    713 	for (uc = userconf_commands; uc != NULL; uc = uc->uc_next) {
    714 		struct bi_userconfcommand *bi;
    715 		bi = (struct bi_userconfcommand *)(buf + off);
    716 		strncpy(bi->text, uc->uc_text, sizeof(bi->text) - 1);
    717 
    718 		off += sizeof(*bi);
    719 		btinfo_userconfcommands->num++;
    720 	}
    721 }
    722 
    723 int
    724 exec_multiboot(const char *file, char *args)
    725 {
    726 	struct multiboot_info *mbi;
    727 	struct multiboot_module *mbm;
    728 	struct bi_modulelist_entry *bim;
    729 	int i, len;
    730 	u_long marks[MARK_MAX];
    731 	u_long extmem;
    732 	u_long basemem;
    733 	char *cmdline;
    734 
    735 	mbi = alloc(sizeof(struct multiboot_info));
    736 	mbi->mi_flags = MULTIBOOT_INFO_HAS_MEMORY;
    737 
    738 	if (common_load_kernel(file, &basemem, &extmem, 0, 0, marks))
    739 		goto out;
    740 
    741 	mbi->mi_mem_upper = extmem;
    742 	mbi->mi_mem_lower = basemem;
    743 
    744 	if (args) {
    745 		mbi->mi_flags |= MULTIBOOT_INFO_HAS_CMDLINE;
    746 		len = strlen(file) + 1 + strlen(args) + 1;
    747 		cmdline = alloc(len);
    748 		snprintf(cmdline, len, "%s %s", file, args);
    749 		mbi->mi_cmdline = (char *) vtophys(cmdline);
    750 	}
    751 
    752 	/* pull in any modules if necessary */
    753 	if (boot_modules_enabled) {
    754 		module_init(file);
    755 		if (btinfo_modulelist) {
    756 			mbm = alloc(sizeof(struct multiboot_module) *
    757 					   btinfo_modulelist->num);
    758 
    759 			bim = (struct bi_modulelist_entry *)
    760 			  (((char *) btinfo_modulelist) +
    761 			   sizeof(struct btinfo_modulelist));
    762 			for (i = 0; i < btinfo_modulelist->num; i++) {
    763 				mbm[i].mmo_start = bim->base;
    764 				mbm[i].mmo_end = bim->base + bim->len;
    765 				mbm[i].mmo_string = (char *)vtophys(bim->path);
    766 				mbm[i].mmo_reserved = 0;
    767 				bim++;
    768 			}
    769 			mbi->mi_flags |= MULTIBOOT_INFO_HAS_MODS;
    770 			mbi->mi_mods_count = btinfo_modulelist->num;
    771 			mbi->mi_mods_addr = vtophys(mbm);
    772 		}
    773 	}
    774 
    775 #ifdef DEBUG
    776 	printf("Start @ 0x%lx [%ld=0x%lx-0x%lx]...\n", marks[MARK_ENTRY],
    777 	    marks[MARK_NSYM], marks[MARK_SYM], marks[MARK_END]);
    778 #endif
    779 
    780 #if 0
    781 	if (btinfo_symtab.nsym) {
    782 		mbi->mi_flags |= MULTIBOOT_INFO_HAS_ELF_SYMS;
    783 		mbi->mi_elfshdr_addr = marks[MARK_SYM];
    784 	btinfo_symtab.nsym = marks[MARK_NSYM];
    785 	btinfo_symtab.ssym = marks[MARK_SYM];
    786 	btinfo_symtab.esym = marks[MARK_END];
    787 #endif
    788 
    789 	multiboot(marks[MARK_ENTRY], vtophys(mbi),
    790 	    x86_trunc_page(mbi->mi_mem_lower * 1024));
    791 	panic("exec returned");
    792 
    793 out:
    794 	dealloc(mbi, 0);
    795 	return -1;
    796 }
    797 
    798 void
    799 x86_progress(const char *fmt, ...)
    800 {
    801 	va_list ap;
    802 
    803 	if ((howto & AB_SILENT) != 0)
    804 		return;
    805 	va_start(ap, fmt);
    806 	vprintf(fmt, ap);
    807 	va_end(ap);
    808 }
    809