exec.c revision 1.64
1/*	$NetBSD: exec.c,v 1.64 2017/02/11 10:18:10 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 */
115static 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
126extern struct btinfo_console btinfo_console;
127
128boot_module_t *boot_modules;
129bool boot_modules_enabled = true;
130bool kernel_loaded;
131
132typedef struct userconf_command {
133	char *uc_text;
134	size_t uc_len;
135	struct userconf_command *uc_next;
136} userconf_command_t;
137userconf_command_t *userconf_commands = NULL;
138
139static struct btinfo_framebuffer btinfo_framebuffer;
140
141static struct btinfo_modulelist *btinfo_modulelist;
142static size_t btinfo_modulelist_size;
143static uint32_t image_end;
144static char module_base[64] = "/";
145static int howto;
146
147static struct btinfo_userconfcommands *btinfo_userconfcommands = NULL;
148static size_t btinfo_userconfcommands_size = 0;
149
150static void	module_init(const char *);
151static void	module_add_common(const char *, uint8_t);
152
153static void	userconf_init(void);
154
155void
156framebuffer_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
166void
167module_add(char *name)
168{
169	return module_add_common(name, BM_TYPE_KMOD);
170}
171
172void
173splash_add(char *name)
174{
175	return module_add_common(name, BM_TYPE_IMAGE);
176}
177
178void
179rnd_add(char *name)
180{
181	return module_add_common(name, BM_TYPE_RND);
182}
183
184void
185fs_add(char *name)
186{
187	return module_add_common(name, BM_TYPE_FS);
188}
189
190static void
191module_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
221void
222userconf_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
261static int
262common_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
382int
383exec_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#ifdef EFIBOOT
393	int i;
394#endif
395
396#ifdef	DEBUG
397	printf("exec: file=%s loadaddr=0x%lx\n", file ? file : "NULL",
398	    loadaddr);
399#endif
400
401	BI_ALLOC(BTINFO_MAX);
402
403	BI_ADD(&btinfo_console, BTINFO_CONSOLE, sizeof(struct btinfo_console));
404
405	howto = boothowto;
406
407	memset(marks, 0, sizeof(marks));
408
409	error = common_load_kernel(file, &basemem, &extmem, loadaddr, floppy,
410	    marks);
411	if (error) {
412		errno = error;
413		goto out;
414	}
415
416	boot_argv[0] = boothowto;
417	boot_argv[1] = 0;
418	boot_argv[2] = vtophys(bootinfo);	/* old cyl offset */
419	boot_argv[3] = marks[MARK_END];
420	boot_argv[4] = extmem;
421	boot_argv[5] = basemem;
422
423	/* pull in any modules if necessary */
424	if (boot_modules_enabled) {
425		module_init(file);
426		if (btinfo_modulelist) {
427			BI_ADD(btinfo_modulelist, BTINFO_MODULELIST,
428			    btinfo_modulelist_size);
429		}
430	}
431
432	userconf_init();
433	if (btinfo_userconfcommands != NULL)
434		BI_ADD(btinfo_userconfcommands, BTINFO_USERCONFCOMMANDS,
435		    btinfo_userconfcommands_size);
436
437#ifdef DEBUG
438	printf("Start @ 0x%lx [%ld=0x%lx-0x%lx]...\n", marks[MARK_ENTRY],
439	    marks[MARK_NSYM], marks[MARK_SYM], marks[MARK_END]);
440#endif
441
442	btinfo_symtab.nsym = marks[MARK_NSYM];
443	btinfo_symtab.ssym = marks[MARK_SYM];
444	btinfo_symtab.esym = marks[MARK_END];
445	BI_ADD(&btinfo_symtab, BTINFO_SYMTAB, sizeof(struct btinfo_symtab));
446
447	/* set new video mode if necessary */
448	vbe_commit();
449	BI_ADD(&btinfo_framebuffer, BTINFO_FRAMEBUFFER,
450	    sizeof(struct btinfo_framebuffer));
451
452	if (callback != NULL)
453		(*callback)();
454#ifdef EFIBOOT
455	/* Copy bootinfo to safe arena. */
456	for (i = 0; i < bootinfo->nentries; i++) {
457		struct btinfo_common *bi = (void *)(u_long)bootinfo->entry[i];
458		char *p = alloc(bi->len);
459		memcpy(p, bi, bi->len);
460		bootinfo->entry[i] = vtophys(p);
461	}
462
463	/* Copy the kernel to original load address. */
464	memmove((void *)marks[MARK_START],
465	    (void *)(efi_loadaddr + marks[MARK_START]),
466	    marks[MARK_END] - marks[MARK_START]);
467#endif
468	startprog(marks[MARK_ENTRY], BOOT_NARGS, boot_argv,
469	    x86_trunc_page(basemem * 1024));
470	panic("exec returned");
471
472out:
473	BI_FREE();
474	bootinfo = NULL;
475	return -1;
476}
477
478static void
479extract_device(const char *path, char *buf, size_t buflen)
480{
481	size_t i;
482
483	if (strchr(path, ':') != NULL) {
484		for (i = 0; i < buflen - 2 && path[i] != ':'; i++)
485			buf[i] = path[i];
486		buf[i++] = ':';
487		buf[i] = '\0';
488	} else
489		buf[0] = '\0';
490}
491
492static const char *
493module_path(boot_module_t *bm, const char *kdev)
494{
495	static char buf[256];
496	char name_buf[256], dev_buf[64];
497	const char *name, *name2, *p;
498
499	name = bm->bm_path;
500	for (name2 = name; *name2; ++name2) {
501		if (*name2 == ' ' || *name2 == '\t') {
502			strlcpy(name_buf, name, sizeof(name_buf));
503			if ((uintptr_t)name2 - (uintptr_t)name < sizeof(name_buf))
504				name_buf[name2 - name] = '\0';
505			name = name_buf;
506			break;
507		}
508	}
509	if ((p = strchr(name, ':')) != NULL) {
510		/* device specified, use it */
511		if (p[1] == '/')
512			snprintf(buf, sizeof(buf), "%s", name);
513		else {
514			p++;
515			extract_device(name, dev_buf, sizeof(dev_buf));
516			snprintf(buf, sizeof(buf), "%s%s/%s/%s.kmod",
517			    dev_buf, module_base, p, p);
518		}
519	} else {
520		/* device not specified; load from kernel device if known */
521		if (name[0] == '/')
522			snprintf(buf, sizeof(buf), "%s%s", kdev, name);
523		else
524			snprintf(buf, sizeof(buf), "%s%s/%s/%s.kmod",
525			    kdev, module_base, name, name);
526	}
527
528	return buf;
529}
530
531static int
532module_open(boot_module_t *bm, int mode, const char *kdev, bool doload)
533{
534	int fd;
535	const char *path;
536
537	/* check the expanded path first */
538	path = module_path(bm, kdev);
539	fd = open(path, mode);
540	if (fd != -1) {
541		if ((howto & AB_SILENT) == 0 && doload)
542			printf("Loading %s ", path);
543	} else {
544		/* now attempt the raw path provided */
545		fd = open(bm->bm_path, mode);
546		if (fd != -1 && (howto & AB_SILENT) == 0 && doload)
547			printf("Loading %s ", bm->bm_path);
548	}
549	if (!doload && fd == -1) {
550		printf("WARNING: couldn't open %s", bm->bm_path);
551		if (strcmp(bm->bm_path, path) != 0)
552			printf(" (%s)", path);
553		printf("\n");
554	}
555	return fd;
556}
557
558static void
559module_init(const char *kernel_path)
560{
561	struct bi_modulelist_entry *bi;
562	struct stat st;
563	const char *machine;
564	char kdev[64];
565	char *buf;
566	boot_module_t *bm;
567	ssize_t len;
568	off_t off;
569	int err, fd, nfail = 0;
570
571	extract_device(kernel_path, kdev, sizeof(kdev));
572
573	switch (netbsd_elf_class) {
574	case ELFCLASS32:
575		machine = "i386";
576		break;
577	case ELFCLASS64:
578		machine = "amd64";
579		break;
580	default:
581		machine = "generic";
582		break;
583	}
584	if (netbsd_version / 1000000 % 100 == 99) {
585		/* -current */
586		snprintf(module_base, sizeof(module_base),
587		    "/stand/%s/%d.%d.%d/modules", machine,
588		    netbsd_version / 100000000,
589		    netbsd_version / 1000000 % 100,
590		    netbsd_version / 100 % 100);
591	} else if (netbsd_version != 0) {
592		/* release */
593		snprintf(module_base, sizeof(module_base),
594		    "/stand/%s/%d.%d/modules", machine,
595		    netbsd_version / 100000000,
596		    netbsd_version / 1000000 % 100);
597	}
598
599	/* First, see which modules are valid and calculate btinfo size */
600	len = sizeof(struct btinfo_modulelist);
601	for (bm = boot_modules; bm; bm = bm->bm_next) {
602		fd = module_open(bm, 0, kdev, false);
603		if (fd == -1) {
604			bm->bm_len = -1;
605			++nfail;
606			continue;
607		}
608		err = fstat(fd, &st);
609		if (err == -1 || st.st_size == -1) {
610			printf("WARNING: couldn't stat %s\n", bm->bm_path);
611			close(fd);
612			bm->bm_len = -1;
613			++nfail;
614			continue;
615		}
616		bm->bm_len = st.st_size;
617		close(fd);
618		len += sizeof(struct bi_modulelist_entry);
619	}
620
621	/* Allocate the module list */
622	btinfo_modulelist = alloc(len);
623	if (btinfo_modulelist == NULL) {
624		printf("WARNING: couldn't allocate module list\n");
625		wait_sec(MODULE_WARNING_SEC);
626		return;
627	}
628	memset(btinfo_modulelist, 0, len);
629	btinfo_modulelist_size = len;
630
631	/* Fill in btinfo structure */
632	buf = (char *)btinfo_modulelist;
633	btinfo_modulelist->num = 0;
634	off = sizeof(struct btinfo_modulelist);
635
636	for (bm = boot_modules; bm; bm = bm->bm_next) {
637		if (bm->bm_len == -1)
638			continue;
639		fd = module_open(bm, 0, kdev, true);
640		if (fd == -1)
641			continue;
642		image_end = (image_end + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
643		len = pread(fd, (void *)(uintptr_t)image_end, SSIZE_MAX);
644		if (len < bm->bm_len) {
645			if ((howto & AB_SILENT) != 0)
646				printf("Loading %s ", bm->bm_path);
647			printf(" FAILED\n");
648		} else {
649			btinfo_modulelist->num++;
650			bi = (struct bi_modulelist_entry *)(buf + off);
651			off += sizeof(struct bi_modulelist_entry);
652			strncpy(bi->path, bm->bm_path, sizeof(bi->path) - 1);
653			bi->base = image_end;
654			bi->len = len;
655			switch (bm->bm_type) {
656			    case BM_TYPE_KMOD:
657				bi->type = BI_MODULE_ELF;
658				break;
659			    case BM_TYPE_IMAGE:
660				bi->type = BI_MODULE_IMAGE;
661				break;
662			    case BM_TYPE_FS:
663				bi->type = BI_MODULE_FS;
664				break;
665			    case BM_TYPE_RND:
666			    default:
667				/* safest -- rnd checks the sha1 */
668				bi->type = BI_MODULE_RND;
669				break;
670			}
671			if ((howto & AB_SILENT) == 0)
672				printf(" \n");
673		}
674		if (len > 0)
675			image_end += len;
676		close(fd);
677	}
678	btinfo_modulelist->endpa = image_end;
679
680	if (nfail > 0) {
681		printf("WARNING: %d module%s failed to load\n",
682		    nfail, nfail == 1 ? "" : "s");
683#if notyet
684		wait_sec(MODULE_WARNING_SEC);
685#endif
686	}
687}
688
689static void
690userconf_init(void)
691{
692	size_t count, len;
693	userconf_command_t *uc;
694	char *buf;
695	off_t off;
696
697	/* Calculate the userconf commands list size */
698	count = 0;
699	for (uc = userconf_commands; uc != NULL; uc = uc->uc_next)
700		count++;
701	len = sizeof(*btinfo_userconfcommands) +
702	      count * sizeof(struct bi_userconfcommand);
703
704	/* Allocate the userconf commands list */
705	btinfo_userconfcommands = alloc(len);
706	if (btinfo_userconfcommands == NULL) {
707		printf("WARNING: couldn't allocate userconf commands list\n");
708		return;
709	}
710	memset(btinfo_userconfcommands, 0, len);
711	btinfo_userconfcommands_size = len;
712
713	/* Fill in btinfo structure */
714	buf = (char *)btinfo_userconfcommands;
715	off = sizeof(*btinfo_userconfcommands);
716	btinfo_userconfcommands->num = 0;
717	for (uc = userconf_commands; uc != NULL; uc = uc->uc_next) {
718		struct bi_userconfcommand *bi;
719		bi = (struct bi_userconfcommand *)(buf + off);
720		strncpy(bi->text, uc->uc_text, sizeof(bi->text) - 1);
721
722		off += sizeof(*bi);
723		btinfo_userconfcommands->num++;
724	}
725}
726
727int
728exec_multiboot(const char *file, char *args)
729{
730	struct multiboot_info *mbi;
731	struct multiboot_module *mbm;
732	struct bi_modulelist_entry *bim;
733	int i, len;
734	u_long marks[MARK_MAX];
735	u_long extmem;
736	u_long basemem;
737	char *cmdline;
738
739	mbi = alloc(sizeof(struct multiboot_info));
740	mbi->mi_flags = MULTIBOOT_INFO_HAS_MEMORY;
741
742	if (common_load_kernel(file, &basemem, &extmem, 0, 0, marks))
743		goto out;
744
745	mbi->mi_mem_upper = extmem;
746	mbi->mi_mem_lower = basemem;
747
748	if (args) {
749		mbi->mi_flags |= MULTIBOOT_INFO_HAS_CMDLINE;
750		len = strlen(file) + 1 + strlen(args) + 1;
751		cmdline = alloc(len);
752		snprintf(cmdline, len, "%s %s", file, args);
753		mbi->mi_cmdline = (char *) vtophys(cmdline);
754	}
755
756	/* pull in any modules if necessary */
757	if (boot_modules_enabled) {
758		module_init(file);
759		if (btinfo_modulelist) {
760			mbm = alloc(sizeof(struct multiboot_module) *
761					   btinfo_modulelist->num);
762
763			bim = (struct bi_modulelist_entry *)
764			  (((char *) btinfo_modulelist) +
765			   sizeof(struct btinfo_modulelist));
766			for (i = 0; i < btinfo_modulelist->num; i++) {
767				mbm[i].mmo_start = bim->base;
768				mbm[i].mmo_end = bim->base + bim->len;
769				mbm[i].mmo_string = (char *)vtophys(bim->path);
770				mbm[i].mmo_reserved = 0;
771				bim++;
772			}
773			mbi->mi_flags |= MULTIBOOT_INFO_HAS_MODS;
774			mbi->mi_mods_count = btinfo_modulelist->num;
775			mbi->mi_mods_addr = vtophys(mbm);
776		}
777	}
778
779#ifdef DEBUG
780	printf("Start @ 0x%lx [%ld=0x%lx-0x%lx]...\n", marks[MARK_ENTRY],
781	    marks[MARK_NSYM], marks[MARK_SYM], marks[MARK_END]);
782#endif
783
784#if 0
785	if (btinfo_symtab.nsym) {
786		mbi->mi_flags |= MULTIBOOT_INFO_HAS_ELF_SYMS;
787		mbi->mi_elfshdr_addr = marks[MARK_SYM];
788	btinfo_symtab.nsym = marks[MARK_NSYM];
789	btinfo_symtab.ssym = marks[MARK_SYM];
790	btinfo_symtab.esym = marks[MARK_END];
791#endif
792
793	multiboot(marks[MARK_ENTRY], vtophys(mbi),
794	    x86_trunc_page(mbi->mi_mem_lower * 1024));
795	panic("exec returned");
796
797out:
798	dealloc(mbi, 0);
799	return -1;
800}
801
802void
803x86_progress(const char *fmt, ...)
804{
805	va_list ap;
806
807	if ((howto & AB_SILENT) != 0)
808		return;
809	va_start(ap, fmt);
810	vprintf(fmt, ap);
811	va_end(ap);
812}
813