Home | History | Annotate | Line # | Download | only in lib
exec.c revision 1.22.74.3
      1  1.22.74.1       mjf /*	$NetBSD: exec.c,v 1.22.74.3 2009/01/17 13:28:05 mjf Exp $	 */
      2  1.22.74.1       mjf 
      3  1.22.74.1       mjf /*-
      4  1.22.74.1       mjf  * Copyright (c) 2008 The NetBSD Foundation, Inc.
      5  1.22.74.1       mjf  * All rights reserved.
      6  1.22.74.1       mjf  *
      7  1.22.74.1       mjf  * Redistribution and use in source and binary forms, with or without
      8  1.22.74.1       mjf  * modification, are permitted provided that the following conditions
      9  1.22.74.1       mjf  * are met:
     10  1.22.74.1       mjf  * 1. Redistributions of source code must retain the above copyright
     11  1.22.74.1       mjf  *    notice, this list of conditions and the following disclaimer.
     12  1.22.74.1       mjf  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.22.74.1       mjf  *    notice, this list of conditions and the following disclaimer in the
     14  1.22.74.1       mjf  *    documentation and/or other materials provided with the distribution.
     15  1.22.74.1       mjf  *
     16  1.22.74.1       mjf  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.22.74.1       mjf  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.22.74.1       mjf  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.22.74.1       mjf  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.22.74.1       mjf  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.22.74.1       mjf  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.22.74.1       mjf  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.22.74.1       mjf  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.22.74.1       mjf  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.22.74.1       mjf  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.22.74.1       mjf  * POSSIBILITY OF SUCH DAMAGE.
     27  1.22.74.1       mjf  */
     28        1.1     perry 
     29        1.1     perry /*
     30        1.1     perry  * Copyright (c) 1982, 1986, 1990, 1993
     31        1.1     perry  *	The Regents of the University of California.  All rights reserved.
     32       1.19       agc  *
     33       1.19       agc  * Redistribution and use in source and binary forms, with or without
     34       1.19       agc  * modification, are permitted provided that the following conditions
     35       1.19       agc  * are met:
     36       1.19       agc  * 1. Redistributions of source code must retain the above copyright
     37       1.19       agc  *    notice, this list of conditions and the following disclaimer.
     38       1.19       agc  * 2. Redistributions in binary form must reproduce the above copyright
     39       1.19       agc  *    notice, this list of conditions and the following disclaimer in the
     40       1.19       agc  *    documentation and/or other materials provided with the distribution.
     41       1.19       agc  * 3. Neither the name of the University nor the names of its contributors
     42       1.19       agc  *    may be used to endorse or promote products derived from this software
     43       1.19       agc  *    without specific prior written permission.
     44       1.19       agc  *
     45       1.19       agc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     46       1.19       agc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     47       1.19       agc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     48       1.19       agc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     49       1.19       agc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     50       1.19       agc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     51       1.19       agc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     52       1.19       agc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     53       1.19       agc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     54       1.19       agc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     55       1.19       agc  * SUCH DAMAGE.
     56       1.19       agc  *
     57       1.19       agc  * 	@(#)boot.c	8.1 (Berkeley) 6/10/93
     58       1.19       agc  */
     59       1.19       agc 
     60       1.19       agc /*
     61        1.1     perry  * Copyright (c) 1996
     62        1.1     perry  *	Matthias Drochner.  All rights reserved.
     63        1.1     perry  * Copyright (c) 1996
     64        1.1     perry  * 	Perry E. Metzger.  All rights reserved.
     65        1.1     perry  *
     66        1.1     perry  * Redistribution and use in source and binary forms, with or without
     67        1.1     perry  * modification, are permitted provided that the following conditions
     68        1.1     perry  * are met:
     69        1.1     perry  * 1. Redistributions of source code must retain the above copyright
     70        1.1     perry  *    notice, this list of conditions and the following disclaimer.
     71        1.1     perry  * 2. Redistributions in binary form must reproduce the above copyright
     72        1.1     perry  *    notice, this list of conditions and the following disclaimer in the
     73        1.1     perry  *    documentation and/or other materials provided with the distribution.
     74        1.1     perry  * 3. All advertising materials mentioning features or use of this software
     75        1.1     perry  *    must display the following acknowledgement:
     76        1.1     perry  *	This product includes software developed by the University of
     77        1.1     perry  *	California, Berkeley and its contributors.
     78        1.1     perry  * 4. Neither the name of the University nor the names of its contributors
     79        1.1     perry  *    may be used to endorse or promote products derived from this software
     80        1.1     perry  *    without specific prior written permission.
     81        1.1     perry  *
     82        1.1     perry  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     83        1.1     perry  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     84        1.1     perry  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     85        1.1     perry  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     86        1.1     perry  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     87        1.1     perry  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     88        1.1     perry  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     89        1.1     perry  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     90        1.1     perry  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     91        1.1     perry  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     92        1.1     perry  * SUCH DAMAGE.
     93        1.1     perry  *
     94        1.1     perry  * 	@(#)boot.c	8.1 (Berkeley) 6/10/93
     95        1.1     perry  */
     96        1.1     perry 
     97        1.2   thorpej /*
     98        1.8  christos  * starts NetBSD a.out kernel
     99        1.8  christos  * needs lowlevel startup from startprog.S
    100        1.8  christos  * This is a special version of exec.c to support use of XMS.
    101        1.1     perry  */
    102        1.8  christos 
    103        1.1     perry #include <sys/param.h>
    104        1.1     perry #include <sys/reboot.h>
    105        1.1     perry 
    106  1.22.74.3       mjf #include <machine/multiboot.h>
    107  1.22.74.3       mjf 
    108        1.1     perry #include <lib/libsa/stand.h>
    109  1.22.74.1       mjf #include <lib/libkern/libkern.h>
    110        1.4  drochner 
    111        1.6  christos #include "loadfile.h"
    112        1.1     perry #include "libi386.h"
    113        1.4  drochner #include "bootinfo.h"
    114  1.22.74.1       mjf #include "bootmod.h"
    115       1.16  jdolecek #ifdef SUPPORT_PS2
    116       1.16  jdolecek #include "biosmca.h"
    117       1.16  jdolecek #endif
    118        1.1     perry 
    119        1.6  christos #define BOOT_NARGS	6
    120        1.1     perry 
    121  1.22.74.1       mjf #ifndef	PAGE_SIZE
    122  1.22.74.1       mjf #define	PAGE_SIZE	4096
    123  1.22.74.1       mjf #endif
    124  1.22.74.1       mjf 
    125        1.4  drochner extern struct btinfo_console btinfo_console;
    126        1.4  drochner 
    127  1.22.74.1       mjf boot_module_t *boot_modules;
    128  1.22.74.1       mjf bool boot_modules_enabled = true;
    129  1.22.74.1       mjf bool kernel_loaded;
    130  1.22.74.1       mjf 
    131  1.22.74.1       mjf static struct btinfo_modulelist *btinfo_modulelist;
    132  1.22.74.1       mjf static size_t btinfo_modulelist_size;
    133  1.22.74.1       mjf static uint32_t image_end;
    134  1.22.74.1       mjf static char module_base[64] = "/";
    135  1.22.74.1       mjf 
    136  1.22.74.1       mjf static void	module_init(void);
    137  1.22.74.1       mjf 
    138  1.22.74.3       mjf void
    139  1.22.74.3       mjf module_add(char *name)
    140        1.1     perry {
    141  1.22.74.3       mjf 	boot_module_t *bm, *bmp;
    142  1.22.74.3       mjf 	size_t len;
    143  1.22.74.3       mjf 	char *str;
    144  1.22.74.3       mjf 
    145  1.22.74.3       mjf 	while (*name == ' ' || *name == '\t')
    146  1.22.74.3       mjf 		++name;
    147  1.22.74.3       mjf 
    148  1.22.74.3       mjf 	bm = alloc(sizeof(boot_module_t));
    149  1.22.74.3       mjf 	len = strlen(name) + 1;
    150  1.22.74.3       mjf 	str = alloc(len);
    151  1.22.74.3       mjf 	if (bm == NULL || str == NULL) {
    152  1.22.74.3       mjf 		printf("couldn't allocate module\n");
    153  1.22.74.3       mjf 		return;
    154  1.22.74.3       mjf 	}
    155  1.22.74.3       mjf 	memcpy(str, name, len);
    156  1.22.74.3       mjf 	bm->bm_path = str;
    157  1.22.74.3       mjf 	bm->bm_next = NULL;
    158  1.22.74.3       mjf 	if (boot_modules == NULL)
    159  1.22.74.3       mjf 		boot_modules = bm;
    160  1.22.74.3       mjf 	else {
    161  1.22.74.3       mjf 		for (bmp = boot_modules; bmp->bm_next;
    162  1.22.74.3       mjf 		    bmp = bmp->bm_next)
    163  1.22.74.3       mjf 			;
    164  1.22.74.3       mjf 		bmp->bm_next = bm;
    165  1.22.74.3       mjf 	}
    166  1.22.74.3       mjf }
    167  1.22.74.3       mjf 
    168  1.22.74.3       mjf static int
    169  1.22.74.3       mjf common_load_kernel(const char *file, u_long *basemem, u_long *extmem,
    170  1.22.74.3       mjf     physaddr_t loadaddr, int floppy, u_long marks[MARK_MAX])
    171  1.22.74.3       mjf {
    172  1.22.74.3       mjf 	int fd;
    173        1.8  christos #ifdef XMS
    174        1.8  christos 	u_long		xmsmem;
    175        1.8  christos 	physaddr_t	origaddr = loadaddr;
    176        1.8  christos #endif
    177        1.4  drochner 
    178  1.22.74.3       mjf 	*extmem = getextmem();
    179  1.22.74.3       mjf 	*basemem = getbasemem();
    180        1.8  christos 
    181        1.8  christos #ifdef XMS
    182        1.8  christos 	if ((getextmem1() == 0) && (xmsmem = checkxms())) {
    183        1.8  christos 	        u_long kernsize;
    184        1.8  christos 
    185        1.8  christos 		/*
    186        1.8  christos 		 * With "CONSERVATIVE_MEMDETECT", extmem is 0 because
    187        1.8  christos 		 *  getextmem() is getextmem1(). Without, the "smart"
    188        1.8  christos 		 *  methods could fail to report all memory as well.
    189        1.8  christos 		 * xmsmem is a few kB less than the actual size, but
    190        1.8  christos 		 *  better than nothing.
    191        1.8  christos 		 */
    192  1.22.74.3       mjf 		if (xmsmem > *extmem)
    193  1.22.74.3       mjf 			*extmem = xmsmem;
    194       1.21  junyoung 		/*
    195        1.8  christos 		 * Get the size of the kernel
    196        1.8  christos 		 */
    197        1.8  christos 		marks[MARK_START] = loadaddr;
    198       1.12  christos 		if ((fd = loadfile(file, marks, COUNT_KERNEL)) == -1)
    199  1.22.74.3       mjf 			return EIO;
    200        1.8  christos 		close(fd);
    201        1.8  christos 
    202        1.8  christos 		kernsize = marks[MARK_END];
    203        1.8  christos 		kernsize = (kernsize + 1023) / 1024;
    204        1.8  christos 
    205        1.8  christos 		loadaddr = xmsalloc(kernsize);
    206        1.8  christos 		if (!loadaddr)
    207       1.21  junyoung 			return ENOMEM;
    208        1.8  christos 	}
    209        1.8  christos #endif
    210        1.7  christos 	marks[MARK_START] = loadaddr;
    211  1.22.74.2       mjf 	if ((fd = loadfile(file, marks,
    212  1.22.74.2       mjf 	    LOAD_KERNEL & ~(floppy ? LOAD_NOTE : 0))) == -1)
    213  1.22.74.3       mjf 		return EIO;
    214        1.8  christos 
    215        1.8  christos 	close(fd);
    216       1.10  drochner 
    217  1.22.74.3       mjf 	/* Now we know the root fs type, load modules for it. */
    218  1.22.74.3       mjf 	module_add(fsmod);
    219  1.22.74.3       mjf 	if (fsmod2 != NULL && strcmp(fsmod, fsmod2) != 0)
    220  1.22.74.3       mjf 		module_add(fsmod2);
    221  1.22.74.3       mjf 
    222       1.10  drochner 	/*
    223       1.10  drochner 	 * Gather some information for the kernel. Do this after the
    224       1.10  drochner 	 * "point of no return" to avoid memory leaks.
    225       1.10  drochner 	 * (but before DOS might be trashed in the XMS case)
    226       1.10  drochner 	 */
    227       1.10  drochner #ifdef PASS_BIOSGEOM
    228       1.10  drochner 	bi_getbiosgeom();
    229       1.10  drochner #endif
    230       1.10  drochner #ifdef PASS_MEMMAP
    231       1.10  drochner 	bi_getmemmap();
    232       1.10  drochner #endif
    233        1.8  christos 
    234        1.8  christos #ifdef XMS
    235        1.8  christos 	if (loadaddr != origaddr) {
    236        1.8  christos 		/*
    237       1.14      ross 		 * We now have done our last DOS IO, so we may
    238        1.8  christos 		 * trash the OS. Copy the data from the temporary
    239       1.20       wiz 		 * buffer to its real address.
    240        1.8  christos 		 */
    241        1.8  christos 		marks[MARK_START] -= loadaddr;
    242        1.8  christos 		marks[MARK_END] -= loadaddr;
    243        1.8  christos 		marks[MARK_SYM] -= loadaddr;
    244        1.8  christos 		marks[MARK_END] -= loadaddr;
    245        1.8  christos 		ppbcopy(loadaddr, origaddr, marks[MARK_END]);
    246        1.8  christos 	}
    247        1.8  christos #endif
    248        1.8  christos 	marks[MARK_END] = (((u_long) marks[MARK_END] + sizeof(int) - 1)) &
    249        1.8  christos 	    (-sizeof(int));
    250  1.22.74.1       mjf 	image_end = marks[MARK_END];
    251  1.22.74.1       mjf 	kernel_loaded = true;
    252        1.1     perry 
    253  1.22.74.3       mjf 	return 0;
    254  1.22.74.3       mjf }
    255  1.22.74.3       mjf 
    256  1.22.74.3       mjf int
    257  1.22.74.3       mjf exec_netbsd(const char *file, physaddr_t loadaddr, int boothowto, int floppy)
    258  1.22.74.3       mjf {
    259  1.22.74.3       mjf 	u_long          boot_argv[BOOT_NARGS];
    260  1.22.74.3       mjf 	u_long		marks[MARK_MAX];
    261  1.22.74.3       mjf 	struct btinfo_symtab btinfo_symtab;
    262  1.22.74.3       mjf 	u_long		extmem;
    263  1.22.74.3       mjf 	u_long		basemem;
    264  1.22.74.3       mjf 
    265  1.22.74.3       mjf #ifdef	DEBUG
    266  1.22.74.3       mjf 	printf("exec: file=%s loadaddr=0x%lx\n",
    267  1.22.74.3       mjf 	       file ? file : "NULL", loadaddr);
    268  1.22.74.3       mjf #endif
    269  1.22.74.3       mjf 
    270  1.22.74.3       mjf 	BI_ALLOC(32); /* ??? */
    271  1.22.74.3       mjf 
    272  1.22.74.3       mjf 	BI_ADD(&btinfo_console, BTINFO_CONSOLE, sizeof(struct btinfo_console));
    273  1.22.74.3       mjf 
    274  1.22.74.3       mjf 	if (common_load_kernel(file, &basemem, &extmem, loadaddr, floppy, marks))
    275  1.22.74.3       mjf 		goto out;
    276  1.22.74.3       mjf 
    277  1.22.74.3       mjf 	boot_argv[0] = boothowto;
    278  1.22.74.3       mjf 	boot_argv[1] = 0;
    279  1.22.74.3       mjf 	boot_argv[2] = vtophys(bootinfo);	/* old cyl offset */
    280  1.22.74.3       mjf 	boot_argv[3] = marks[MARK_END];
    281  1.22.74.3       mjf 	boot_argv[4] = extmem;
    282  1.22.74.3       mjf 	boot_argv[5] = basemem;
    283  1.22.74.3       mjf 
    284  1.22.74.1       mjf 	/* pull in any modules if necessary */
    285  1.22.74.1       mjf 	if (boot_modules_enabled) {
    286  1.22.74.1       mjf 		module_init();
    287  1.22.74.1       mjf 		if (btinfo_modulelist) {
    288  1.22.74.1       mjf 			BI_ADD(btinfo_modulelist, BTINFO_MODULELIST,
    289  1.22.74.1       mjf 			    btinfo_modulelist_size);
    290  1.22.74.1       mjf 		}
    291  1.22.74.1       mjf 	}
    292        1.1     perry 
    293        1.1     perry #ifdef DEBUG
    294        1.8  christos 	printf("Start @ 0x%lx [%ld=0x%lx-0x%lx]...\n", marks[MARK_ENTRY],
    295        1.7  christos 	    marks[MARK_NSYM], marks[MARK_SYM], marks[MARK_END]);
    296        1.1     perry #endif
    297        1.1     perry 
    298        1.8  christos 	btinfo_symtab.nsym = marks[MARK_NSYM];
    299        1.8  christos 	btinfo_symtab.ssym = marks[MARK_SYM];
    300        1.8  christos 	btinfo_symtab.esym = marks[MARK_END];
    301        1.8  christos 	BI_ADD(&btinfo_symtab, BTINFO_SYMTAB, sizeof(struct btinfo_symtab));
    302        1.8  christos 
    303       1.15       dbj 	startprog(marks[MARK_ENTRY], BOOT_NARGS, boot_argv,
    304       1.21  junyoung 		  x86_trunc_page(basemem*1024));
    305        1.1     perry 	panic("exec returned");
    306        1.1     perry 
    307        1.4  drochner out:
    308        1.4  drochner 	BI_FREE();
    309        1.4  drochner 	bootinfo = 0;
    310       1.21  junyoung 	return -1;
    311        1.1     perry }
    312  1.22.74.1       mjf 
    313  1.22.74.1       mjf static const char *
    314  1.22.74.1       mjf module_path(boot_module_t *bm)
    315  1.22.74.1       mjf {
    316  1.22.74.1       mjf 	static char buf[256];
    317  1.22.74.3       mjf 	char name_buf[256];
    318  1.22.74.3       mjf 	const char *name, *name2;
    319  1.22.74.1       mjf 
    320  1.22.74.1       mjf 	name = bm->bm_path;
    321  1.22.74.3       mjf 	for (name2 = name; *name2; ++name2) {
    322  1.22.74.3       mjf 		if (*name2 == ' ' || *name2 == '\t') {
    323  1.22.74.3       mjf 			strlcpy(name_buf, name, sizeof(name_buf));
    324  1.22.74.3       mjf 			if (name2 - name < sizeof(name_buf))
    325  1.22.74.3       mjf 				name_buf[name2 - name] = '\0';
    326  1.22.74.3       mjf 			name = name_buf;
    327  1.22.74.3       mjf 			break;
    328  1.22.74.3       mjf 		}
    329  1.22.74.3       mjf 	}
    330  1.22.74.3       mjf  	if (name[0] == '/')
    331  1.22.74.3       mjf 		snprintf(buf, sizeof(buf), "%s", name);
    332  1.22.74.3       mjf 	else
    333  1.22.74.3       mjf 		snprintf(buf, sizeof(buf), "%s/%s/%s.kmod",
    334  1.22.74.3       mjf 		    module_base, name, name);
    335  1.22.74.3       mjf 
    336  1.22.74.1       mjf 	return buf;
    337  1.22.74.1       mjf }
    338  1.22.74.1       mjf 
    339  1.22.74.3       mjf static int
    340  1.22.74.1       mjf module_open(boot_module_t *bm, int mode)
    341  1.22.74.1       mjf {
    342  1.22.74.1       mjf 	int fd;
    343  1.22.74.1       mjf 	const char *path;
    344  1.22.74.3       mjf 
    345  1.22.74.1       mjf 	/* check the expanded path first */
    346  1.22.74.1       mjf 	path = module_path(bm);
    347  1.22.74.1       mjf 	fd = open(path, mode);
    348  1.22.74.1       mjf 	if (fd == -1) {
    349  1.22.74.1       mjf 		printf("WARNING: couldn't open %s\n", path);
    350  1.22.74.1       mjf 		/* now attempt the raw path provided */
    351  1.22.74.1       mjf 		fd = open(bm->bm_path, mode);
    352  1.22.74.1       mjf 		if (fd == -1)
    353  1.22.74.1       mjf 			printf("WARNING: couldn't open %s\n", bm->bm_path);
    354  1.22.74.1       mjf 	}
    355  1.22.74.1       mjf 	return fd;
    356  1.22.74.1       mjf }
    357  1.22.74.1       mjf 
    358  1.22.74.1       mjf static void
    359  1.22.74.1       mjf module_init(void)
    360  1.22.74.1       mjf {
    361  1.22.74.1       mjf 	struct bi_modulelist_entry *bi;
    362  1.22.74.1       mjf 	struct stat st;
    363  1.22.74.3       mjf 	const char *machine;
    364  1.22.74.1       mjf 	char *buf;
    365  1.22.74.1       mjf 	boot_module_t *bm;
    366  1.22.74.1       mjf 	size_t len;
    367  1.22.74.1       mjf 	off_t off;
    368  1.22.74.1       mjf 	int err, fd;
    369  1.22.74.1       mjf 
    370  1.22.74.3       mjf 	switch (netbsd_elf_class) {
    371  1.22.74.3       mjf 	case ELFCLASS32:
    372  1.22.74.3       mjf 		machine = "i386";
    373  1.22.74.3       mjf 		break;
    374  1.22.74.3       mjf 	case ELFCLASS64:
    375  1.22.74.3       mjf 		machine = "amd64";
    376  1.22.74.3       mjf 		break;
    377  1.22.74.3       mjf 	default:
    378  1.22.74.3       mjf 		machine = "generic";
    379  1.22.74.3       mjf 		break;
    380  1.22.74.3       mjf 	}
    381  1.22.74.3       mjf 	if (netbsd_version / 1000000 % 100 == 99) {
    382  1.22.74.3       mjf 		/* -current */
    383  1.22.74.3       mjf 		snprintf(module_base, sizeof(module_base),
    384  1.22.74.3       mjf 		    "/stand/%s/%d.%d.%d/modules", machine,
    385  1.22.74.3       mjf 		    netbsd_version / 100000000,
    386  1.22.74.3       mjf 		    netbsd_version / 1000000 % 100,
    387  1.22.74.3       mjf 		    netbsd_version / 100 % 100);
    388  1.22.74.3       mjf 	} else if (netbsd_version != 0) {
    389  1.22.74.3       mjf 		/* release */
    390  1.22.74.3       mjf 		snprintf(module_base, sizeof(module_base),
    391  1.22.74.3       mjf 		    "/stand/%s/%d.%d/modules", machine,
    392  1.22.74.3       mjf 		    netbsd_version / 100000000,
    393  1.22.74.3       mjf 		    netbsd_version / 1000000 % 100);
    394  1.22.74.3       mjf 	}
    395  1.22.74.3       mjf 
    396  1.22.74.1       mjf 	/* First, see which modules are valid and calculate btinfo size */
    397  1.22.74.1       mjf 	len = sizeof(struct btinfo_modulelist);
    398  1.22.74.1       mjf 	for (bm = boot_modules; bm; bm = bm->bm_next) {
    399  1.22.74.1       mjf 		fd = module_open(bm, 0);
    400  1.22.74.1       mjf 		if (fd == -1) {
    401  1.22.74.1       mjf 			bm->bm_len = -1;
    402  1.22.74.1       mjf 			continue;
    403  1.22.74.1       mjf 		}
    404  1.22.74.1       mjf 		err = fstat(fd, &st);
    405  1.22.74.1       mjf 		if (err == -1 || st.st_size == -1) {
    406  1.22.74.1       mjf 			printf("WARNING: couldn't stat %s\n", bm->bm_path);
    407  1.22.74.1       mjf 			close(fd);
    408  1.22.74.1       mjf 			bm->bm_len = -1;
    409  1.22.74.1       mjf 			continue;
    410  1.22.74.1       mjf 		}
    411  1.22.74.1       mjf 		bm->bm_len = st.st_size;
    412  1.22.74.1       mjf 		close(fd);
    413  1.22.74.1       mjf 		len += sizeof(struct bi_modulelist_entry);
    414  1.22.74.1       mjf 	}
    415  1.22.74.1       mjf 
    416  1.22.74.1       mjf 	/* Allocate the module list */
    417  1.22.74.1       mjf 	btinfo_modulelist = alloc(len);
    418  1.22.74.1       mjf 	if (btinfo_modulelist == NULL) {
    419  1.22.74.1       mjf 		printf("WARNING: couldn't allocate module list\n");
    420  1.22.74.1       mjf 		return;
    421  1.22.74.1       mjf 	}
    422  1.22.74.1       mjf 	memset(btinfo_modulelist, 0, len);
    423  1.22.74.1       mjf 	btinfo_modulelist_size = len;
    424  1.22.74.1       mjf 
    425  1.22.74.1       mjf 	/* Fill in btinfo structure */
    426  1.22.74.1       mjf 	buf = (char *)btinfo_modulelist;
    427  1.22.74.1       mjf 	btinfo_modulelist->num = 0;
    428  1.22.74.1       mjf 	off = sizeof(struct btinfo_modulelist);
    429  1.22.74.1       mjf 
    430  1.22.74.1       mjf 	for (bm = boot_modules; bm; bm = bm->bm_next) {
    431  1.22.74.1       mjf 		if (bm->bm_len == -1)
    432  1.22.74.1       mjf 			continue;
    433  1.22.74.1       mjf 		printf("Loading %s ", bm->bm_path);
    434  1.22.74.1       mjf 		fd = module_open(bm, 0);
    435  1.22.74.1       mjf 		if (fd == -1) {
    436  1.22.74.1       mjf 			printf("ERROR: couldn't open %s\n", bm->bm_path);
    437  1.22.74.1       mjf 			continue;
    438  1.22.74.1       mjf 		}
    439  1.22.74.1       mjf 		image_end = (image_end + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
    440  1.22.74.1       mjf 		len = pread(fd, (void *)image_end, SSIZE_MAX);
    441  1.22.74.1       mjf 		if (len < bm->bm_len) {
    442  1.22.74.1       mjf 			printf(" FAILED\n");
    443  1.22.74.1       mjf 		} else {
    444  1.22.74.1       mjf 			btinfo_modulelist->num++;
    445  1.22.74.1       mjf 			bi = (struct bi_modulelist_entry *)(buf + off);
    446  1.22.74.1       mjf 			off += sizeof(struct bi_modulelist_entry);
    447  1.22.74.1       mjf 			strncpy(bi->path, bm->bm_path, sizeof(bi->path) - 1);
    448  1.22.74.1       mjf 			bi->base = image_end;
    449  1.22.74.1       mjf 			bi->len = len;
    450  1.22.74.1       mjf 			bi->type = BI_MODULE_ELF;
    451  1.22.74.1       mjf 			printf(" \n");
    452  1.22.74.1       mjf 		}
    453  1.22.74.1       mjf 		if (len > 0)
    454  1.22.74.1       mjf 			image_end += len;
    455  1.22.74.1       mjf 		close(fd);
    456  1.22.74.1       mjf 	}
    457  1.22.74.1       mjf 	btinfo_modulelist->endpa = image_end;
    458  1.22.74.1       mjf }
    459  1.22.74.3       mjf 
    460  1.22.74.3       mjf int
    461  1.22.74.3       mjf exec_multiboot(const char *file, char *args)
    462  1.22.74.3       mjf {
    463  1.22.74.3       mjf 	struct multiboot_info *mbi;
    464  1.22.74.3       mjf 	struct multiboot_module *mbm;
    465  1.22.74.3       mjf 	struct bi_modulelist_entry *bim;
    466  1.22.74.3       mjf 	int		i, len;
    467  1.22.74.3       mjf 	u_long		marks[MARK_MAX];
    468  1.22.74.3       mjf 	u_long		extmem;
    469  1.22.74.3       mjf 	u_long		basemem;
    470  1.22.74.3       mjf 	char		*cmdline;
    471  1.22.74.3       mjf 
    472  1.22.74.3       mjf 	mbi = alloc(sizeof(struct multiboot_info));
    473  1.22.74.3       mjf 	mbi->mi_flags = MULTIBOOT_INFO_HAS_MEMORY;
    474  1.22.74.3       mjf 
    475  1.22.74.3       mjf 	if (common_load_kernel(file, &basemem, &extmem, 0, 0, marks))
    476  1.22.74.3       mjf 		goto out;
    477  1.22.74.3       mjf 
    478  1.22.74.3       mjf 	mbi->mi_mem_upper = extmem;
    479  1.22.74.3       mjf 	mbi->mi_mem_lower = basemem;
    480  1.22.74.3       mjf 
    481  1.22.74.3       mjf 	if (args) {
    482  1.22.74.3       mjf 		mbi->mi_flags |= MULTIBOOT_INFO_HAS_CMDLINE;
    483  1.22.74.3       mjf 		len = strlen(file) + 1 + strlen(args) + 1;
    484  1.22.74.3       mjf 		cmdline = alloc(len);
    485  1.22.74.3       mjf 		snprintf(cmdline, len, "%s %s", file, args);
    486  1.22.74.3       mjf 		mbi->mi_cmdline = (char *) vtophys(cmdline);
    487  1.22.74.3       mjf 	}
    488  1.22.74.3       mjf 
    489  1.22.74.3       mjf 	/* pull in any modules if necessary */
    490  1.22.74.3       mjf 	if (boot_modules_enabled) {
    491  1.22.74.3       mjf 		module_init();
    492  1.22.74.3       mjf 		if (btinfo_modulelist) {
    493  1.22.74.3       mjf 			mbm = alloc(sizeof(struct multiboot_module) *
    494  1.22.74.3       mjf 					   btinfo_modulelist->num);
    495  1.22.74.3       mjf 
    496  1.22.74.3       mjf 			bim = (struct bi_modulelist_entry *)
    497  1.22.74.3       mjf 			  (((char *) btinfo_modulelist) +
    498  1.22.74.3       mjf 			   sizeof(struct btinfo_modulelist));
    499  1.22.74.3       mjf 			for (i = 0; i < btinfo_modulelist->num; i++) {
    500  1.22.74.3       mjf 				mbm[i].mmo_start = bim->base;
    501  1.22.74.3       mjf 				mbm[i].mmo_end = bim->base + bim->len;
    502  1.22.74.3       mjf 				mbm[i].mmo_string = (char *)vtophys(bim->path);
    503  1.22.74.3       mjf 				mbm[i].mmo_reserved = 0;
    504  1.22.74.3       mjf 				bim++;
    505  1.22.74.3       mjf 			}
    506  1.22.74.3       mjf 			mbi->mi_flags |= MULTIBOOT_INFO_HAS_MODS;
    507  1.22.74.3       mjf 			mbi->mi_mods_count = btinfo_modulelist->num;
    508  1.22.74.3       mjf 			mbi->mi_mods_addr = vtophys(mbm);
    509  1.22.74.3       mjf 		}
    510  1.22.74.3       mjf 	}
    511  1.22.74.3       mjf 
    512  1.22.74.3       mjf #ifdef DEBUG
    513  1.22.74.3       mjf 	printf("Start @ 0x%lx [%ld=0x%lx-0x%lx]...\n", marks[MARK_ENTRY],
    514  1.22.74.3       mjf 	    marks[MARK_NSYM], marks[MARK_SYM], marks[MARK_END]);
    515  1.22.74.3       mjf #endif
    516  1.22.74.3       mjf 
    517  1.22.74.3       mjf 
    518  1.22.74.3       mjf #if 0
    519  1.22.74.3       mjf 	if (btinfo_symtab.nsym) {
    520  1.22.74.3       mjf 		mbi->mi_flags |= MULTIBOOT_INFO_HAS_ELF_SYMS;
    521  1.22.74.3       mjf 		mbi->mi_elfshdr_addr = marks[MARK_SYM];
    522  1.22.74.3       mjf 	btinfo_symtab.nsym = marks[MARK_NSYM];
    523  1.22.74.3       mjf 	btinfo_symtab.ssym = marks[MARK_SYM];
    524  1.22.74.3       mjf 	btinfo_symtab.esym = marks[MARK_END];
    525  1.22.74.3       mjf #endif
    526  1.22.74.3       mjf 
    527  1.22.74.3       mjf 	multiboot(marks[MARK_ENTRY], vtophys(mbi),
    528  1.22.74.3       mjf 		  x86_trunc_page(mbi->mi_mem_lower*1024));
    529  1.22.74.3       mjf 	panic("exec returned");
    530  1.22.74.3       mjf 
    531  1.22.74.3       mjf out:
    532  1.22.74.3       mjf         dealloc(mbi, 0);
    533  1.22.74.3       mjf 	return -1;
    534  1.22.74.3       mjf }
    535