exec.c revision 1.66
1/*	$NetBSD: exec.c,v 1.66 2017/02/23 12:14:53 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#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
125extern struct btinfo_console btinfo_console;
126
127boot_module_t *boot_modules;
128bool boot_modules_enabled = true;
129bool kernel_loaded;
130
131typedef struct userconf_command {
132	char *uc_text;
133	size_t uc_len;
134	struct userconf_command *uc_next;
135} userconf_command_t;
136userconf_command_t *userconf_commands = NULL;
137
138static struct btinfo_framebuffer btinfo_framebuffer;
139
140static struct btinfo_modulelist *btinfo_modulelist;
141static size_t btinfo_modulelist_size;
142static uint32_t image_end;
143static char module_base[64] = "/";
144static int howto;
145
146static struct btinfo_userconfcommands *btinfo_userconfcommands = NULL;
147static size_t btinfo_userconfcommands_size = 0;
148
149static void	module_init(const char *);
150static void	module_add_common(const char *, uint8_t);
151
152static void	userconf_init(void);
153
154void
155framebuffer_configure(struct btinfo_framebuffer *fb)
156{
157	if (fb)
158		btinfo_framebuffer = *fb;
159	else {
160		btinfo_framebuffer.physaddr = 0;
161		btinfo_framebuffer.flags = 0;
162	}
163}
164
165void
166module_add(char *name)
167{
168	return module_add_common(name, BM_TYPE_KMOD);
169}
170
171void
172splash_add(char *name)
173{
174	return module_add_common(name, BM_TYPE_IMAGE);
175}
176
177void
178rnd_add(char *name)
179{
180	return module_add_common(name, BM_TYPE_RND);
181}
182
183void
184fs_add(char *name)
185{
186	return module_add_common(name, BM_TYPE_FS);
187}
188
189static void
190module_add_common(const char *name, uint8_t type)
191{
192	boot_module_t *bm, *bmp;
193	size_t len;
194	char *str;
195
196	while (*name == ' ' || *name == '\t')
197		++name;
198
199	bm = alloc(sizeof(boot_module_t));
200	len = strlen(name) + 1;
201	str = alloc(len);
202	if (bm == NULL || str == NULL) {
203		printf("couldn't allocate module\n");
204		return;
205	}
206	memcpy(str, name, len);
207	bm->bm_path = str;
208	bm->bm_next = NULL;
209	bm->bm_type = type;
210	if (boot_modules == NULL)
211		boot_modules = bm;
212	else {
213		for (bmp = boot_modules; bmp->bm_next;
214		    bmp = bmp->bm_next)
215			;
216		bmp->bm_next = bm;
217	}
218}
219
220void
221userconf_add(char *cmd)
222{
223	userconf_command_t *uc;
224	size_t len;
225	char *text;
226
227	while (*cmd == ' ' || *cmd == '\t')
228		++cmd;
229
230	uc = alloc(sizeof(*uc));
231	if (uc == NULL) {
232		printf("couldn't allocate command\n");
233		return;
234	}
235
236	len = strlen(cmd) + 1;
237	text = alloc(len);
238	if (text == NULL) {
239		dealloc(uc, sizeof(*uc));
240		printf("couldn't allocate command\n");
241		return;
242	}
243	memcpy(text, cmd, len);
244
245	uc->uc_text = text;
246	uc->uc_len = len;
247	uc->uc_next = NULL;
248
249	if (userconf_commands == NULL)
250		userconf_commands = uc;
251	else {
252		userconf_command_t *ucp;
253		for (ucp = userconf_commands; ucp->uc_next != NULL;
254		     ucp = ucp->uc_next)
255			;
256		ucp->uc_next = uc;
257	}
258}
259
260static int
261common_load_kernel(const char *file, u_long *basemem, u_long *extmem,
262    physaddr_t loadaddr, int floppy, u_long marks[MARK_MAX])
263{
264	int fd;
265#ifdef XMS
266	u_long xmsmem;
267	physaddr_t origaddr = loadaddr;
268#endif
269
270	*extmem = getextmem();
271	*basemem = getbasemem();
272
273#ifdef XMS
274	if ((getextmem1() == 0) && (xmsmem = checkxms())) {
275		u_long kernsize;
276
277		/*
278		 * With "CONSERVATIVE_MEMDETECT", extmem is 0 because
279		 * getextmem() is getextmem1(). Without, the "smart"
280		 * methods could fail to report all memory as well.
281		 * xmsmem is a few kB less than the actual size, but
282		 * better than nothing.
283		 */
284		if (xmsmem > *extmem)
285			*extmem = xmsmem;
286		/*
287		 * Get the size of the kernel
288		 */
289		marks[MARK_START] = loadaddr;
290		if ((fd = loadfile(file, marks, COUNT_KERNEL)) == -1)
291			return EIO;
292		close(fd);
293
294		kernsize = marks[MARK_END];
295		kernsize = (kernsize + 1023) / 1024;
296
297		loadaddr = xmsalloc(kernsize);
298		if (!loadaddr)
299			return ENOMEM;
300	}
301#endif
302#ifdef EFIBOOT
303	{
304		EFI_STATUS status;
305		EFI_PHYSICAL_ADDRESS addr;
306		UINTN kernsize;
307
308		marks[MARK_START] = loadaddr;
309		if ((fd = loadfile(file, marks, COUNT_KERNEL)) == -1)
310			return EIO;
311		close(fd);
312
313		/* Allocate temporary arena. */
314		addr = EFI_ALLOCATE_MAX_ADDRESS;
315		kernsize = marks[MARK_END] - loadaddr;
316		kernsize = EFI_SIZE_TO_PAGES(kernsize);
317		status = uefi_call_wrapper(BS->AllocatePages, 4,
318		    AllocateMaxAddress, EfiLoaderData, kernsize, &addr);
319		if (EFI_ERROR(status))
320			return ENOMEM;
321		efi_loadaddr = loadaddr = addr;
322
323		memset(marks, 0, sizeof(marks[0]) * MARK_MAX);
324	}
325#endif
326	marks[MARK_START] = loadaddr;
327	if ((fd = loadfile(file, marks,
328	    LOAD_KERNEL & ~(floppy ? LOAD_BACKWARDS : 0))) == -1)
329		return EIO;
330
331	close(fd);
332
333	/* If the root fs type is unusual, load its module. */
334	if (fsmod != NULL)
335		module_add_common(fsmod, BM_TYPE_KMOD);
336
337	/*
338	 * Gather some information for the kernel. Do this after the
339	 * "point of no return" to avoid memory leaks.
340	 * (but before DOS might be trashed in the XMS case)
341	 */
342#ifdef PASS_BIOSGEOM
343	bi_getbiosgeom();
344#endif
345#ifdef PASS_MEMMAP
346	bi_getmemmap();
347#endif
348
349#ifdef XMS
350	if (loadaddr != origaddr) {
351		/*
352		 * We now have done our last DOS IO, so we may
353		 * trash the OS. Copy the data from the temporary
354		 * buffer to its real address.
355		 */
356		marks[MARK_START] -= loadaddr;
357		marks[MARK_END] -= loadaddr;
358		marks[MARK_SYM] -= loadaddr;
359		marks[MARK_END] -= loadaddr;
360		ppbcopy(loadaddr, origaddr, marks[MARK_END]);
361	}
362#endif
363#ifdef EFIBOOT
364	marks[MARK_START] -= loadaddr;
365	marks[MARK_ENTRY] -= loadaddr;
366	marks[MARK_DATA] -= loadaddr;
367	/* MARK_NSYM */
368	marks[MARK_SYM] -= loadaddr;
369	marks[MARK_END] -= loadaddr;
370	/* Copy the kernel to original load address later. */
371#endif
372	marks[MARK_END] = (((u_long) marks[MARK_END] + sizeof(int) - 1)) &
373	    (-sizeof(int));
374	image_end = marks[MARK_END];
375	kernel_loaded = true;
376
377	return 0;
378}
379
380int
381exec_netbsd(const char *file, physaddr_t loadaddr, int boothowto, int floppy,
382    void (*callback)(void))
383{
384	uint32_t boot_argv[BOOT_NARGS];
385	u_long marks[MARK_MAX];
386	struct btinfo_symtab btinfo_symtab;
387	u_long extmem;
388	u_long basemem;
389	int error;
390#ifdef EFIBOOT
391	int i;
392#endif
393
394#ifdef	DEBUG
395	printf("exec: file=%s loadaddr=0x%lx\n", file ? file : "NULL",
396	    loadaddr);
397#endif
398
399	BI_ALLOC(BTINFO_MAX);
400
401	BI_ADD(&btinfo_console, BTINFO_CONSOLE, sizeof(struct btinfo_console));
402
403	howto = boothowto;
404
405	memset(marks, 0, sizeof(marks));
406
407	error = common_load_kernel(file, &basemem, &extmem, loadaddr, floppy,
408	    marks);
409	if (error) {
410		errno = error;
411		goto out;
412	}
413
414	boot_argv[0] = boothowto;
415	boot_argv[1] = 0;
416	boot_argv[2] = vtophys(bootinfo);	/* old cyl offset */
417	boot_argv[3] = marks[MARK_END];
418	boot_argv[4] = extmem;
419	boot_argv[5] = basemem;
420
421	/* pull in any modules if necessary */
422	if (boot_modules_enabled) {
423		module_init(file);
424		if (btinfo_modulelist) {
425			BI_ADD(btinfo_modulelist, BTINFO_MODULELIST,
426			    btinfo_modulelist_size);
427		}
428	}
429
430	userconf_init();
431	if (btinfo_userconfcommands != NULL)
432		BI_ADD(btinfo_userconfcommands, BTINFO_USERCONFCOMMANDS,
433		    btinfo_userconfcommands_size);
434
435#ifdef DEBUG
436	printf("Start @ 0x%lx [%ld=0x%lx-0x%lx]...\n", marks[MARK_ENTRY],
437	    marks[MARK_NSYM], marks[MARK_SYM], marks[MARK_END]);
438#endif
439
440	btinfo_symtab.nsym = marks[MARK_NSYM];
441	btinfo_symtab.ssym = marks[MARK_SYM];
442	btinfo_symtab.esym = marks[MARK_END];
443	BI_ADD(&btinfo_symtab, BTINFO_SYMTAB, sizeof(struct btinfo_symtab));
444
445	/* set new video mode if necessary */
446	vbe_commit();
447	BI_ADD(&btinfo_framebuffer, BTINFO_FRAMEBUFFER,
448	    sizeof(struct btinfo_framebuffer));
449
450	if (callback != NULL)
451		(*callback)();
452#ifdef EFIBOOT
453	/* Copy bootinfo to safe arena. */
454	for (i = 0; i < bootinfo->nentries; i++) {
455		struct btinfo_common *bi = (void *)(u_long)bootinfo->entry[i];
456		char *p = alloc(bi->len);
457		memcpy(p, bi, bi->len);
458		bootinfo->entry[i] = vtophys(p);
459	}
460
461	efi_kernel_start = marks[MARK_START];
462	efi_kernel_size = marks[MARK_END] - efi_kernel_start;
463#endif
464	startprog(marks[MARK_ENTRY], BOOT_NARGS, boot_argv,
465	    x86_trunc_page(basemem * 1024));
466	panic("exec returned");
467
468out:
469	BI_FREE();
470	bootinfo = NULL;
471	return -1;
472}
473
474static void
475extract_device(const char *path, char *buf, size_t buflen)
476{
477	size_t i;
478
479	if (strchr(path, ':') != NULL) {
480		for (i = 0; i < buflen - 2 && path[i] != ':'; i++)
481			buf[i] = path[i];
482		buf[i++] = ':';
483		buf[i] = '\0';
484	} else
485		buf[0] = '\0';
486}
487
488static const char *
489module_path(boot_module_t *bm, const char *kdev)
490{
491	static char buf[256];
492	char name_buf[256], dev_buf[64];
493	const char *name, *name2, *p;
494
495	name = bm->bm_path;
496	for (name2 = name; *name2; ++name2) {
497		if (*name2 == ' ' || *name2 == '\t') {
498			strlcpy(name_buf, name, sizeof(name_buf));
499			if ((uintptr_t)name2 - (uintptr_t)name < sizeof(name_buf))
500				name_buf[name2 - name] = '\0';
501			name = name_buf;
502			break;
503		}
504	}
505	if ((p = strchr(name, ':')) != NULL) {
506		/* device specified, use it */
507		if (p[1] == '/')
508			snprintf(buf, sizeof(buf), "%s", name);
509		else {
510			p++;
511			extract_device(name, dev_buf, sizeof(dev_buf));
512			snprintf(buf, sizeof(buf), "%s%s/%s/%s.kmod",
513			    dev_buf, module_base, p, p);
514		}
515	} else {
516		/* device not specified; load from kernel device if known */
517		if (name[0] == '/')
518			snprintf(buf, sizeof(buf), "%s%s", kdev, name);
519		else
520			snprintf(buf, sizeof(buf), "%s%s/%s/%s.kmod",
521			    kdev, module_base, name, name);
522	}
523
524	return buf;
525}
526
527static int
528module_open(boot_module_t *bm, int mode, const char *kdev, bool doload)
529{
530	int fd;
531	const char *path;
532
533	/* check the expanded path first */
534	path = module_path(bm, kdev);
535	fd = open(path, mode);
536	if (fd != -1) {
537		if ((howto & AB_SILENT) == 0 && doload)
538			printf("Loading %s ", path);
539	} else {
540		/* now attempt the raw path provided */
541		fd = open(bm->bm_path, mode);
542		if (fd != -1 && (howto & AB_SILENT) == 0 && doload)
543			printf("Loading %s ", bm->bm_path);
544	}
545	if (!doload && fd == -1) {
546		printf("WARNING: couldn't open %s", bm->bm_path);
547		if (strcmp(bm->bm_path, path) != 0)
548			printf(" (%s)", path);
549		printf("\n");
550	}
551	return fd;
552}
553
554static void
555module_init(const char *kernel_path)
556{
557	struct bi_modulelist_entry *bi;
558	struct stat st;
559	const char *machine;
560	char kdev[64];
561	char *buf;
562	boot_module_t *bm;
563	ssize_t len;
564	off_t off;
565	int err, fd, nfail = 0;
566
567	extract_device(kernel_path, kdev, sizeof(kdev));
568
569	switch (netbsd_elf_class) {
570	case ELFCLASS32:
571		machine = "i386";
572		break;
573	case ELFCLASS64:
574		machine = "amd64";
575		break;
576	default:
577		machine = "generic";
578		break;
579	}
580	if (netbsd_version / 1000000 % 100 == 99) {
581		/* -current */
582		snprintf(module_base, sizeof(module_base),
583		    "/stand/%s/%d.%d.%d/modules", machine,
584		    netbsd_version / 100000000,
585		    netbsd_version / 1000000 % 100,
586		    netbsd_version / 100 % 100);
587	} else if (netbsd_version != 0) {
588		/* release */
589		snprintf(module_base, sizeof(module_base),
590		    "/stand/%s/%d.%d/modules", machine,
591		    netbsd_version / 100000000,
592		    netbsd_version / 1000000 % 100);
593	}
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, 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, 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
685static void
686userconf_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
723int
724exec_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
793out:
794	dealloc(mbi, 0);
795	return -1;
796}
797
798void
799x86_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