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