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