Home | History | Annotate | Line # | Download | only in lib
exec.c revision 1.38.2.5
      1  1.38.2.5       jym /*	$NetBSD: exec.c,v 1.38.2.5 2011/08/27 15:37:28 jym Exp $	 */
      2      1.23        ad 
      3      1.23        ad /*-
      4  1.38.2.1       jym  * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
      5      1.23        ad  * All rights reserved.
      6      1.23        ad  *
      7      1.23        ad  * Redistribution and use in source and binary forms, with or without
      8      1.23        ad  * modification, are permitted provided that the following conditions
      9      1.23        ad  * are met:
     10      1.23        ad  * 1. Redistributions of source code must retain the above copyright
     11      1.23        ad  *    notice, this list of conditions and the following disclaimer.
     12      1.23        ad  * 2. Redistributions in binary form must reproduce the above copyright
     13      1.23        ad  *    notice, this list of conditions and the following disclaimer in the
     14      1.23        ad  *    documentation and/or other materials provided with the distribution.
     15      1.23        ad  *
     16      1.23        ad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17      1.23        ad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18      1.23        ad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19      1.23        ad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20      1.23        ad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21      1.23        ad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22      1.23        ad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23      1.23        ad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24      1.23        ad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25      1.23        ad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26      1.23        ad  * POSSIBILITY OF SUCH DAMAGE.
     27      1.23        ad  */
     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  *
     75       1.1     perry  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     76       1.1     perry  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     77       1.1     perry  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     78       1.1     perry  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     79       1.1     perry  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     80       1.1     perry  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     81       1.1     perry  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     82       1.1     perry  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     83       1.1     perry  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     84       1.1     perry  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     85       1.1     perry  * SUCH DAMAGE.
     86       1.1     perry  *
     87       1.1     perry  * 	@(#)boot.c	8.1 (Berkeley) 6/10/93
     88       1.1     perry  */
     89       1.1     perry 
     90       1.2   thorpej /*
     91       1.8  christos  * starts NetBSD a.out kernel
     92       1.8  christos  * needs lowlevel startup from startprog.S
     93       1.8  christos  * This is a special version of exec.c to support use of XMS.
     94       1.1     perry  */
     95       1.8  christos 
     96       1.1     perry #include <sys/param.h>
     97       1.1     perry #include <sys/reboot.h>
     98  1.38.2.1       jym #include <sys/reboot.h>
     99       1.1     perry 
    100      1.33     joerg #include <machine/multiboot.h>
    101      1.33     joerg 
    102       1.1     perry #include <lib/libsa/stand.h>
    103      1.23        ad #include <lib/libkern/libkern.h>
    104       1.4  drochner 
    105       1.6  christos #include "loadfile.h"
    106       1.1     perry #include "libi386.h"
    107       1.4  drochner #include "bootinfo.h"
    108      1.23        ad #include "bootmod.h"
    109  1.38.2.2       jym #include "vbe.h"
    110      1.16  jdolecek #ifdef SUPPORT_PS2
    111      1.16  jdolecek #include "biosmca.h"
    112      1.16  jdolecek #endif
    113       1.1     perry 
    114       1.6  christos #define BOOT_NARGS	6
    115       1.1     perry 
    116      1.23        ad #ifndef	PAGE_SIZE
    117      1.23        ad #define	PAGE_SIZE	4096
    118      1.23        ad #endif
    119      1.23        ad 
    120  1.38.2.4       jym #define MODULE_WARNING_SEC	5
    121  1.38.2.2       jym 
    122       1.4  drochner extern struct btinfo_console btinfo_console;
    123       1.4  drochner 
    124      1.23        ad boot_module_t *boot_modules;
    125      1.23        ad bool boot_modules_enabled = true;
    126      1.23        ad bool kernel_loaded;
    127      1.23        ad 
    128  1.38.2.5       jym typedef struct userconf_command {
    129  1.38.2.5       jym 	char *uc_text;
    130  1.38.2.5       jym 	size_t uc_len;
    131  1.38.2.5       jym 	struct userconf_command *uc_next;
    132  1.38.2.5       jym } userconf_command_t;
    133  1.38.2.5       jym userconf_command_t *userconf_commands = NULL;
    134  1.38.2.5       jym 
    135  1.38.2.1       jym static struct btinfo_framebuffer btinfo_framebuffer;
    136  1.38.2.1       jym 
    137      1.23        ad static struct btinfo_modulelist *btinfo_modulelist;
    138      1.23        ad static size_t btinfo_modulelist_size;
    139      1.23        ad static uint32_t image_end;
    140      1.26        ad static char module_base[64] = "/";
    141  1.38.2.1       jym static int howto;
    142      1.23        ad 
    143  1.38.2.5       jym static struct btinfo_userconfcommands *btinfo_userconfcommands = NULL;
    144  1.38.2.5       jym static size_t btinfo_userconfcommands_size = 0;
    145  1.38.2.5       jym 
    146  1.38.2.2       jym static void	module_init(const char *);
    147  1.38.2.3       jym static void	module_add_common(char *, uint8_t);
    148      1.23        ad 
    149  1.38.2.5       jym static void	userconf_init(void);
    150  1.38.2.5       jym 
    151      1.34        ad void
    152  1.38.2.1       jym framebuffer_configure(struct btinfo_framebuffer *fb)
    153  1.38.2.1       jym {
    154  1.38.2.1       jym 	if (fb)
    155  1.38.2.1       jym 		btinfo_framebuffer = *fb;
    156  1.38.2.1       jym 	else {
    157  1.38.2.1       jym 		btinfo_framebuffer.physaddr = 0;
    158  1.38.2.1       jym 		btinfo_framebuffer.flags = 0;
    159  1.38.2.1       jym 	}
    160  1.38.2.1       jym }
    161  1.38.2.1       jym 
    162  1.38.2.1       jym void
    163      1.34        ad module_add(char *name)
    164      1.34        ad {
    165  1.38.2.3       jym 	return module_add_common(name, BM_TYPE_KMOD);
    166  1.38.2.3       jym }
    167  1.38.2.3       jym 
    168  1.38.2.3       jym void
    169  1.38.2.3       jym splash_add(char *name)
    170  1.38.2.3       jym {
    171  1.38.2.3       jym 	return module_add_common(name, BM_TYPE_IMAGE);
    172  1.38.2.3       jym }
    173  1.38.2.3       jym 
    174  1.38.2.3       jym static void
    175  1.38.2.3       jym module_add_common(char *name, uint8_t type)
    176  1.38.2.3       jym {
    177      1.34        ad 	boot_module_t *bm, *bmp;
    178      1.34        ad 	size_t len;
    179      1.34        ad 	char *str;
    180      1.34        ad 
    181      1.35        ad 	while (*name == ' ' || *name == '\t')
    182      1.35        ad 		++name;
    183      1.35        ad 
    184      1.34        ad 	bm = alloc(sizeof(boot_module_t));
    185      1.34        ad 	len = strlen(name) + 1;
    186      1.34        ad 	str = alloc(len);
    187      1.34        ad 	if (bm == NULL || str == NULL) {
    188      1.34        ad 		printf("couldn't allocate module\n");
    189      1.34        ad 		return;
    190      1.34        ad 	}
    191      1.34        ad 	memcpy(str, name, len);
    192      1.34        ad 	bm->bm_path = str;
    193      1.34        ad 	bm->bm_next = NULL;
    194  1.38.2.3       jym 	bm->bm_type = type;
    195      1.34        ad 	if (boot_modules == NULL)
    196      1.34        ad 		boot_modules = bm;
    197      1.34        ad 	else {
    198      1.34        ad 		for (bmp = boot_modules; bmp->bm_next;
    199      1.34        ad 		    bmp = bmp->bm_next)
    200      1.34        ad 			;
    201      1.34        ad 		bmp->bm_next = bm;
    202      1.34        ad 	}
    203      1.34        ad }
    204      1.34        ad 
    205  1.38.2.5       jym void
    206  1.38.2.5       jym userconf_add(char *cmd)
    207  1.38.2.5       jym {
    208  1.38.2.5       jym 	userconf_command_t *uc;
    209  1.38.2.5       jym 	size_t len;
    210  1.38.2.5       jym 	char *text;
    211  1.38.2.5       jym 
    212  1.38.2.5       jym 	while (*cmd == ' ' || *cmd == '\t')
    213  1.38.2.5       jym 		++cmd;
    214  1.38.2.5       jym 
    215  1.38.2.5       jym 	uc = alloc(sizeof(*uc));
    216  1.38.2.5       jym 	if (uc == NULL) {
    217  1.38.2.5       jym 		printf("couldn't allocate command\n");
    218  1.38.2.5       jym 		return;
    219  1.38.2.5       jym 	}
    220  1.38.2.5       jym 
    221  1.38.2.5       jym 	len = strlen(cmd) + 1;
    222  1.38.2.5       jym 	text = alloc(len);
    223  1.38.2.5       jym 	if (text == NULL) {
    224  1.38.2.5       jym 		dealloc(uc, sizeof(*uc));
    225  1.38.2.5       jym 		printf("couldn't allocate command\n");
    226  1.38.2.5       jym 		return;
    227  1.38.2.5       jym 	}
    228  1.38.2.5       jym 	memcpy(text, cmd, len);
    229  1.38.2.5       jym 
    230  1.38.2.5       jym 	uc->uc_text = text;
    231  1.38.2.5       jym 	uc->uc_len = len;
    232  1.38.2.5       jym 	uc->uc_next = NULL;
    233  1.38.2.5       jym 
    234  1.38.2.5       jym 	if (userconf_commands == NULL)
    235  1.38.2.5       jym 		userconf_commands = uc;
    236  1.38.2.5       jym 	else {
    237  1.38.2.5       jym 		userconf_command_t *ucp;
    238  1.38.2.5       jym 		for (ucp = userconf_commands; ucp->uc_next != NULL;
    239  1.38.2.5       jym 		     ucp = ucp->uc_next)
    240  1.38.2.5       jym 			;
    241  1.38.2.5       jym 		ucp->uc_next = uc;
    242  1.38.2.5       jym 	}
    243  1.38.2.5       jym }
    244  1.38.2.5       jym 
    245      1.32     joerg static int
    246      1.32     joerg common_load_kernel(const char *file, u_long *basemem, u_long *extmem,
    247      1.32     joerg     physaddr_t loadaddr, int floppy, u_long marks[MARK_MAX])
    248       1.1     perry {
    249      1.32     joerg 	int fd;
    250       1.8  christos #ifdef XMS
    251       1.8  christos 	u_long		xmsmem;
    252       1.8  christos 	physaddr_t	origaddr = loadaddr;
    253       1.8  christos #endif
    254       1.8  christos 
    255      1.32     joerg 	*extmem = getextmem();
    256      1.32     joerg 	*basemem = getbasemem();
    257       1.8  christos 
    258       1.8  christos #ifdef XMS
    259       1.8  christos 	if ((getextmem1() == 0) && (xmsmem = checkxms())) {
    260       1.8  christos 	        u_long kernsize;
    261       1.8  christos 
    262       1.8  christos 		/*
    263       1.8  christos 		 * With "CONSERVATIVE_MEMDETECT", extmem is 0 because
    264       1.8  christos 		 *  getextmem() is getextmem1(). Without, the "smart"
    265       1.8  christos 		 *  methods could fail to report all memory as well.
    266       1.8  christos 		 * xmsmem is a few kB less than the actual size, but
    267       1.8  christos 		 *  better than nothing.
    268       1.8  christos 		 */
    269      1.32     joerg 		if (xmsmem > *extmem)
    270      1.32     joerg 			*extmem = xmsmem;
    271      1.21  junyoung 		/*
    272       1.8  christos 		 * Get the size of the kernel
    273       1.8  christos 		 */
    274       1.8  christos 		marks[MARK_START] = loadaddr;
    275      1.12  christos 		if ((fd = loadfile(file, marks, COUNT_KERNEL)) == -1)
    276      1.32     joerg 			return EIO;
    277       1.8  christos 		close(fd);
    278       1.8  christos 
    279       1.8  christos 		kernsize = marks[MARK_END];
    280       1.8  christos 		kernsize = (kernsize + 1023) / 1024;
    281       1.8  christos 
    282       1.8  christos 		loadaddr = xmsalloc(kernsize);
    283       1.8  christos 		if (!loadaddr)
    284      1.21  junyoung 			return ENOMEM;
    285       1.8  christos 	}
    286       1.8  christos #endif
    287       1.7  christos 	marks[MARK_START] = loadaddr;
    288      1.29  christos 	if ((fd = loadfile(file, marks,
    289  1.38.2.4       jym 	    LOAD_KERNEL & ~(floppy ? LOAD_BACKWARDS : 0))) == -1)
    290      1.32     joerg 		return EIO;
    291       1.1     perry 
    292       1.8  christos 	close(fd);
    293      1.10  drochner 
    294      1.34        ad 	/* Now we know the root fs type, load modules for it. */
    295      1.34        ad 	module_add(fsmod);
    296      1.34        ad 	if (fsmod2 != NULL && strcmp(fsmod, fsmod2) != 0)
    297      1.34        ad 		module_add(fsmod2);
    298      1.34        ad 
    299      1.10  drochner 	/*
    300      1.10  drochner 	 * Gather some information for the kernel. Do this after the
    301      1.10  drochner 	 * "point of no return" to avoid memory leaks.
    302      1.10  drochner 	 * (but before DOS might be trashed in the XMS case)
    303      1.10  drochner 	 */
    304      1.10  drochner #ifdef PASS_BIOSGEOM
    305      1.10  drochner 	bi_getbiosgeom();
    306      1.10  drochner #endif
    307      1.10  drochner #ifdef PASS_MEMMAP
    308      1.10  drochner 	bi_getmemmap();
    309      1.10  drochner #endif
    310       1.8  christos 
    311       1.8  christos #ifdef XMS
    312       1.8  christos 	if (loadaddr != origaddr) {
    313       1.8  christos 		/*
    314      1.14      ross 		 * We now have done our last DOS IO, so we may
    315       1.8  christos 		 * trash the OS. Copy the data from the temporary
    316      1.20       wiz 		 * buffer to its real address.
    317       1.8  christos 		 */
    318       1.8  christos 		marks[MARK_START] -= loadaddr;
    319       1.8  christos 		marks[MARK_END] -= loadaddr;
    320       1.8  christos 		marks[MARK_SYM] -= loadaddr;
    321       1.8  christos 		marks[MARK_END] -= loadaddr;
    322       1.8  christos 		ppbcopy(loadaddr, origaddr, marks[MARK_END]);
    323       1.8  christos 	}
    324       1.8  christos #endif
    325       1.8  christos 	marks[MARK_END] = (((u_long) marks[MARK_END] + sizeof(int) - 1)) &
    326       1.8  christos 	    (-sizeof(int));
    327      1.23        ad 	image_end = marks[MARK_END];
    328      1.23        ad 	kernel_loaded = true;
    329       1.1     perry 
    330      1.32     joerg 	return 0;
    331      1.32     joerg }
    332      1.32     joerg 
    333      1.32     joerg int
    334  1.38.2.1       jym exec_netbsd(const char *file, physaddr_t loadaddr, int boothowto, int floppy,
    335  1.38.2.1       jym 	    void (*callback)(void))
    336      1.32     joerg {
    337      1.32     joerg 	u_long          boot_argv[BOOT_NARGS];
    338      1.32     joerg 	u_long		marks[MARK_MAX];
    339      1.32     joerg 	struct btinfo_symtab btinfo_symtab;
    340      1.32     joerg 	u_long		extmem;
    341      1.32     joerg 	u_long		basemem;
    342      1.32     joerg 
    343      1.32     joerg #ifdef	DEBUG
    344      1.32     joerg 	printf("exec: file=%s loadaddr=0x%lx\n",
    345      1.32     joerg 	       file ? file : "NULL", loadaddr);
    346      1.32     joerg #endif
    347      1.32     joerg 
    348      1.32     joerg 	BI_ALLOC(32); /* ??? */
    349      1.32     joerg 
    350      1.32     joerg 	BI_ADD(&btinfo_console, BTINFO_CONSOLE, sizeof(struct btinfo_console));
    351  1.38.2.1       jym 
    352  1.38.2.1       jym 	howto = boothowto;
    353      1.32     joerg 
    354      1.32     joerg 	if (common_load_kernel(file, &basemem, &extmem, loadaddr, floppy, marks))
    355      1.32     joerg 		goto out;
    356      1.32     joerg 
    357      1.31     joerg 	boot_argv[0] = boothowto;
    358      1.31     joerg 	boot_argv[1] = 0;
    359      1.31     joerg 	boot_argv[2] = vtophys(bootinfo);	/* old cyl offset */
    360      1.31     joerg 	boot_argv[3] = marks[MARK_END];
    361      1.31     joerg 	boot_argv[4] = extmem;
    362      1.31     joerg 	boot_argv[5] = basemem;
    363      1.31     joerg 
    364      1.23        ad 	/* pull in any modules if necessary */
    365      1.23        ad 	if (boot_modules_enabled) {
    366  1.38.2.2       jym 		module_init(file);
    367      1.23        ad 		if (btinfo_modulelist) {
    368      1.23        ad 			BI_ADD(btinfo_modulelist, BTINFO_MODULELIST,
    369      1.23        ad 			    btinfo_modulelist_size);
    370      1.23        ad 		}
    371      1.23        ad 	}
    372       1.1     perry 
    373  1.38.2.5       jym 	userconf_init();
    374  1.38.2.5       jym 	if (btinfo_userconfcommands != NULL)
    375  1.38.2.5       jym 		BI_ADD(btinfo_userconfcommands, BTINFO_USERCONFCOMMANDS,
    376  1.38.2.5       jym 	btinfo_userconfcommands_size);
    377  1.38.2.5       jym 
    378       1.1     perry #ifdef DEBUG
    379       1.8  christos 	printf("Start @ 0x%lx [%ld=0x%lx-0x%lx]...\n", marks[MARK_ENTRY],
    380       1.7  christos 	    marks[MARK_NSYM], marks[MARK_SYM], marks[MARK_END]);
    381       1.1     perry #endif
    382       1.1     perry 
    383       1.8  christos 	btinfo_symtab.nsym = marks[MARK_NSYM];
    384       1.8  christos 	btinfo_symtab.ssym = marks[MARK_SYM];
    385       1.8  christos 	btinfo_symtab.esym = marks[MARK_END];
    386       1.8  christos 	BI_ADD(&btinfo_symtab, BTINFO_SYMTAB, sizeof(struct btinfo_symtab));
    387       1.8  christos 
    388  1.38.2.2       jym 	/* set new video mode if necessary */
    389  1.38.2.2       jym 	vbe_commit();
    390  1.38.2.2       jym 	BI_ADD(&btinfo_framebuffer, BTINFO_FRAMEBUFFER,
    391  1.38.2.2       jym 	    sizeof(struct btinfo_framebuffer));
    392  1.38.2.2       jym 
    393  1.38.2.1       jym 	if (callback != NULL)
    394  1.38.2.1       jym 		(*callback)();
    395      1.15       dbj 	startprog(marks[MARK_ENTRY], BOOT_NARGS, boot_argv,
    396      1.21  junyoung 		  x86_trunc_page(basemem*1024));
    397       1.1     perry 	panic("exec returned");
    398       1.1     perry 
    399       1.4  drochner out:
    400       1.4  drochner 	BI_FREE();
    401       1.4  drochner 	bootinfo = 0;
    402      1.21  junyoung 	return -1;
    403       1.1     perry }
    404      1.23        ad 
    405  1.38.2.2       jym static void
    406  1.38.2.2       jym extract_device(const char *path, char *buf, size_t buflen)
    407  1.38.2.2       jym {
    408  1.38.2.2       jym 	int i;
    409  1.38.2.2       jym 
    410  1.38.2.2       jym 	if (strchr(path, ':') != NULL) {
    411  1.38.2.2       jym 		for (i = 0; i < buflen - 2 && path[i] != ':'; i++)
    412  1.38.2.2       jym 			buf[i] = path[i];
    413  1.38.2.2       jym 		buf[i++] = ':';
    414  1.38.2.2       jym 		buf[i] = '\0';
    415  1.38.2.2       jym 	} else
    416  1.38.2.2       jym 		buf[0] = '\0';
    417  1.38.2.2       jym }
    418  1.38.2.2       jym 
    419      1.26        ad static const char *
    420  1.38.2.2       jym module_path(boot_module_t *bm, const char *kdev)
    421      1.26        ad {
    422      1.26        ad 	static char buf[256];
    423  1.38.2.2       jym 	char name_buf[256], dev_buf[64];
    424  1.38.2.2       jym 	const char *name, *name2, *p;
    425      1.26        ad 
    426      1.26        ad 	name = bm->bm_path;
    427      1.33     joerg 	for (name2 = name; *name2; ++name2) {
    428      1.33     joerg 		if (*name2 == ' ' || *name2 == '\t') {
    429      1.33     joerg 			strlcpy(name_buf, name, sizeof(name_buf));
    430      1.33     joerg 			if (name2 - name < sizeof(name_buf))
    431      1.33     joerg 				name_buf[name2 - name] = '\0';
    432      1.33     joerg 			name = name_buf;
    433      1.33     joerg 			break;
    434      1.33     joerg 		}
    435      1.33     joerg 	}
    436  1.38.2.2       jym 	if ((p = strchr(name, ':')) != NULL) {
    437  1.38.2.2       jym 		/* device specified, use it */
    438  1.38.2.2       jym 		if (p[1] == '/')
    439  1.38.2.2       jym 			snprintf(buf, sizeof(buf), "%s", name);
    440  1.38.2.2       jym 		else {
    441  1.38.2.2       jym 			p++;
    442  1.38.2.2       jym 			extract_device(name, dev_buf, sizeof(dev_buf));
    443  1.38.2.2       jym 			snprintf(buf, sizeof(buf), "%s%s/%s/%s.kmod",
    444  1.38.2.2       jym 			    dev_buf, module_base, p, p);
    445  1.38.2.2       jym 		}
    446  1.38.2.2       jym 	} else {
    447  1.38.2.2       jym 		/* device not specified; load from kernel device if known */
    448  1.38.2.2       jym  		if (name[0] == '/')
    449  1.38.2.2       jym 			snprintf(buf, sizeof(buf), "%s%s", kdev, name);
    450  1.38.2.2       jym 		else
    451  1.38.2.2       jym 			snprintf(buf, sizeof(buf), "%s%s/%s/%s.kmod",
    452  1.38.2.2       jym 			    kdev, module_base, name, name);
    453  1.38.2.2       jym 	}
    454      1.33     joerg 
    455      1.26        ad 	return buf;
    456      1.26        ad }
    457      1.26        ad 
    458      1.36  christos static int
    459  1.38.2.2       jym module_open(boot_module_t *bm, int mode, const char *kdev, bool doload)
    460      1.28     chris {
    461      1.28     chris 	int fd;
    462      1.28     chris 	const char *path;
    463      1.36  christos 
    464      1.28     chris 	/* check the expanded path first */
    465  1.38.2.2       jym 	path = module_path(bm, kdev);
    466      1.28     chris 	fd = open(path, mode);
    467  1.38.2.2       jym 	if (fd != -1) {
    468  1.38.2.2       jym 		if ((howto & AB_SILENT) == 0 && doload)
    469  1.38.2.2       jym 			printf("Loading %s ", path);
    470  1.38.2.2       jym 	} else {
    471      1.28     chris 		/* now attempt the raw path provided */
    472      1.28     chris 		fd = open(bm->bm_path, mode);
    473  1.38.2.2       jym 		if (fd != -1 && (howto & AB_SILENT) == 0 && doload)
    474  1.38.2.2       jym 			printf("Loading %s ", bm->bm_path);
    475  1.38.2.2       jym 	}
    476  1.38.2.2       jym 	if (!doload && fd == -1) {
    477  1.38.2.2       jym 		printf("WARNING: couldn't open %s", bm->bm_path);
    478  1.38.2.2       jym 		if (strcmp(bm->bm_path, path) != 0)
    479  1.38.2.2       jym 			printf(" (%s)", path);
    480  1.38.2.2       jym 		printf("\n");
    481      1.28     chris 	}
    482      1.28     chris 	return fd;
    483      1.28     chris }
    484      1.28     chris 
    485      1.23        ad static void
    486  1.38.2.2       jym module_init(const char *kernel_path)
    487      1.23        ad {
    488      1.23        ad 	struct bi_modulelist_entry *bi;
    489      1.23        ad 	struct stat st;
    490      1.30     joerg 	const char *machine;
    491  1.38.2.2       jym 	char kdev[64];
    492      1.23        ad 	char *buf;
    493      1.23        ad 	boot_module_t *bm;
    494      1.23        ad 	size_t len;
    495      1.23        ad 	off_t off;
    496  1.38.2.2       jym 	int err, fd, nfail = 0;
    497  1.38.2.2       jym 
    498  1.38.2.2       jym 	extract_device(kernel_path, kdev, sizeof(kdev));
    499      1.23        ad 
    500      1.30     joerg 	switch (netbsd_elf_class) {
    501      1.30     joerg 	case ELFCLASS32:
    502      1.30     joerg 		machine = "i386";
    503      1.30     joerg 		break;
    504      1.30     joerg 	case ELFCLASS64:
    505      1.30     joerg 		machine = "amd64";
    506      1.30     joerg 		break;
    507      1.30     joerg 	default:
    508      1.30     joerg 		machine = "generic";
    509      1.30     joerg 		break;
    510      1.30     joerg 	}
    511      1.30     joerg 	if (netbsd_version / 1000000 % 100 == 99) {
    512      1.30     joerg 		/* -current */
    513      1.30     joerg 		snprintf(module_base, sizeof(module_base),
    514      1.38     rmind 		    "/stand/%s/%d.%d.%d/modules", machine,
    515      1.30     joerg 		    netbsd_version / 100000000,
    516      1.30     joerg 		    netbsd_version / 1000000 % 100,
    517      1.30     joerg 		    netbsd_version / 100 % 100);
    518      1.30     joerg 	} else if (netbsd_version != 0) {
    519      1.30     joerg 		/* release */
    520      1.30     joerg 		snprintf(module_base, sizeof(module_base),
    521      1.38     rmind 		    "/stand/%s/%d.%d/modules", machine,
    522      1.30     joerg 		    netbsd_version / 100000000,
    523      1.30     joerg 		    netbsd_version / 1000000 % 100);
    524      1.30     joerg 	}
    525      1.30     joerg 
    526      1.23        ad 	/* First, see which modules are valid and calculate btinfo size */
    527      1.23        ad 	len = sizeof(struct btinfo_modulelist);
    528      1.23        ad 	for (bm = boot_modules; bm; bm = bm->bm_next) {
    529  1.38.2.2       jym 		fd = module_open(bm, 0, kdev, false);
    530      1.23        ad 		if (fd == -1) {
    531      1.23        ad 			bm->bm_len = -1;
    532  1.38.2.2       jym 			++nfail;
    533      1.23        ad 			continue;
    534      1.23        ad 		}
    535      1.23        ad 		err = fstat(fd, &st);
    536      1.24        ad 		if (err == -1 || st.st_size == -1) {
    537      1.28     chris 			printf("WARNING: couldn't stat %s\n", bm->bm_path);
    538      1.23        ad 			close(fd);
    539      1.23        ad 			bm->bm_len = -1;
    540  1.38.2.2       jym 			++nfail;
    541      1.23        ad 			continue;
    542      1.23        ad 		}
    543      1.23        ad 		bm->bm_len = st.st_size;
    544      1.23        ad 		close(fd);
    545      1.23        ad 		len += sizeof(struct bi_modulelist_entry);
    546      1.23        ad 	}
    547      1.23        ad 
    548      1.23        ad 	/* Allocate the module list */
    549      1.23        ad 	btinfo_modulelist = alloc(len);
    550      1.23        ad 	if (btinfo_modulelist == NULL) {
    551      1.23        ad 		printf("WARNING: couldn't allocate module list\n");
    552  1.38.2.4       jym 		wait_sec(MODULE_WARNING_SEC);
    553      1.23        ad 		return;
    554      1.23        ad 	}
    555      1.23        ad 	memset(btinfo_modulelist, 0, len);
    556      1.23        ad 	btinfo_modulelist_size = len;
    557      1.23        ad 
    558      1.23        ad 	/* Fill in btinfo structure */
    559      1.23        ad 	buf = (char *)btinfo_modulelist;
    560      1.23        ad 	btinfo_modulelist->num = 0;
    561      1.23        ad 	off = sizeof(struct btinfo_modulelist);
    562      1.23        ad 
    563      1.23        ad 	for (bm = boot_modules; bm; bm = bm->bm_next) {
    564      1.23        ad 		if (bm->bm_len == -1)
    565      1.23        ad 			continue;
    566  1.38.2.2       jym 		fd = module_open(bm, 0, kdev, true);
    567  1.38.2.2       jym 		if (fd == -1)
    568      1.23        ad 			continue;
    569      1.24        ad 		image_end = (image_end + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
    570      1.24        ad 		len = pread(fd, (void *)image_end, SSIZE_MAX);
    571      1.24        ad 		if (len < bm->bm_len) {
    572  1.38.2.1       jym 			if ((howto & AB_SILENT) != 0)
    573  1.38.2.1       jym 				printf("Loading %s ", bm->bm_path);
    574      1.23        ad 			printf(" FAILED\n");
    575      1.23        ad 		} else {
    576      1.23        ad 			btinfo_modulelist->num++;
    577      1.23        ad 			bi = (struct bi_modulelist_entry *)(buf + off);
    578      1.23        ad 			off += sizeof(struct bi_modulelist_entry);
    579      1.23        ad 			strncpy(bi->path, bm->bm_path, sizeof(bi->path) - 1);
    580      1.24        ad 			bi->base = image_end;
    581      1.24        ad 			bi->len = len;
    582  1.38.2.3       jym 			bi->type = bm->bm_type == BM_TYPE_KMOD ?
    583  1.38.2.3       jym 			    BI_MODULE_ELF : BI_MODULE_IMAGE;
    584  1.38.2.1       jym 			if ((howto & AB_SILENT) == 0)
    585  1.38.2.1       jym 				printf(" \n");
    586      1.23        ad 		}
    587      1.24        ad 		if (len > 0)
    588      1.24        ad 			image_end += len;
    589      1.23        ad 		close(fd);
    590      1.23        ad 	}
    591      1.23        ad 	btinfo_modulelist->endpa = image_end;
    592  1.38.2.2       jym 
    593  1.38.2.2       jym 	if (nfail > 0) {
    594  1.38.2.2       jym 		printf("WARNING: %d module%s failed to load\n",
    595  1.38.2.2       jym 		    nfail, nfail == 1 ? "" : "s");
    596  1.38.2.2       jym #if notyet
    597  1.38.2.4       jym 		wait_sec(MODULE_WARNING_SEC);
    598  1.38.2.2       jym #endif
    599  1.38.2.2       jym 	}
    600      1.23        ad }
    601      1.33     joerg 
    602  1.38.2.5       jym static void
    603  1.38.2.5       jym userconf_init(void)
    604  1.38.2.5       jym {
    605  1.38.2.5       jym 	size_t count, len;
    606  1.38.2.5       jym 	userconf_command_t *uc;
    607  1.38.2.5       jym 	char *buf;
    608  1.38.2.5       jym 	off_t off;
    609  1.38.2.5       jym 
    610  1.38.2.5       jym 	/* Calculate the userconf commands list size */
    611  1.38.2.5       jym 	count = 0;
    612  1.38.2.5       jym 	for (uc = userconf_commands; uc != NULL; uc = uc->uc_next)
    613  1.38.2.5       jym 		count++;
    614  1.38.2.5       jym 	len = sizeof(btinfo_userconfcommands) +
    615  1.38.2.5       jym 	      count * sizeof(struct bi_userconfcommand);
    616  1.38.2.5       jym 
    617  1.38.2.5       jym 	/* Allocate the userconf commands list */
    618  1.38.2.5       jym 	btinfo_userconfcommands = alloc(len);
    619  1.38.2.5       jym 	if (btinfo_userconfcommands == NULL) {
    620  1.38.2.5       jym 		printf("WARNING: couldn't allocate userconf commands list\n");
    621  1.38.2.5       jym 		return;
    622  1.38.2.5       jym 	}
    623  1.38.2.5       jym 	memset(btinfo_userconfcommands, 0, len);
    624  1.38.2.5       jym 	btinfo_userconfcommands_size = len;
    625  1.38.2.5       jym 
    626  1.38.2.5       jym 	/* Fill in btinfo structure */
    627  1.38.2.5       jym 	buf = (char *)btinfo_userconfcommands;
    628  1.38.2.5       jym 	off = sizeof(*btinfo_userconfcommands);
    629  1.38.2.5       jym 	btinfo_userconfcommands->num = 0;
    630  1.38.2.5       jym 	for (uc = userconf_commands; uc != NULL; uc = uc->uc_next) {
    631  1.38.2.5       jym 		struct bi_userconfcommand *bi;
    632  1.38.2.5       jym 		bi = (struct bi_userconfcommand *)(buf + off);
    633  1.38.2.5       jym 		strncpy(bi->text, uc->uc_text, sizeof(bi->text) - 1);
    634  1.38.2.5       jym 
    635  1.38.2.5       jym 		off += sizeof(*bi);
    636  1.38.2.5       jym 		btinfo_userconfcommands->num++;
    637  1.38.2.5       jym 	}
    638  1.38.2.5       jym }
    639  1.38.2.5       jym 
    640      1.33     joerg int
    641      1.33     joerg exec_multiboot(const char *file, char *args)
    642      1.33     joerg {
    643      1.33     joerg 	struct multiboot_info *mbi;
    644      1.33     joerg 	struct multiboot_module *mbm;
    645      1.33     joerg 	struct bi_modulelist_entry *bim;
    646      1.33     joerg 	int		i, len;
    647      1.33     joerg 	u_long		marks[MARK_MAX];
    648      1.33     joerg 	u_long		extmem;
    649      1.33     joerg 	u_long		basemem;
    650      1.33     joerg 	char		*cmdline;
    651      1.33     joerg 
    652      1.33     joerg 	mbi = alloc(sizeof(struct multiboot_info));
    653      1.33     joerg 	mbi->mi_flags = MULTIBOOT_INFO_HAS_MEMORY;
    654      1.33     joerg 
    655      1.33     joerg 	if (common_load_kernel(file, &basemem, &extmem, 0, 0, marks))
    656      1.33     joerg 		goto out;
    657      1.33     joerg 
    658      1.33     joerg 	mbi->mi_mem_upper = extmem;
    659      1.33     joerg 	mbi->mi_mem_lower = basemem;
    660      1.33     joerg 
    661      1.33     joerg 	if (args) {
    662      1.33     joerg 		mbi->mi_flags |= MULTIBOOT_INFO_HAS_CMDLINE;
    663      1.33     joerg 		len = strlen(file) + 1 + strlen(args) + 1;
    664      1.33     joerg 		cmdline = alloc(len);
    665      1.33     joerg 		snprintf(cmdline, len, "%s %s", file, args);
    666      1.33     joerg 		mbi->mi_cmdline = (char *) vtophys(cmdline);
    667      1.33     joerg 	}
    668      1.33     joerg 
    669      1.33     joerg 	/* pull in any modules if necessary */
    670      1.33     joerg 	if (boot_modules_enabled) {
    671  1.38.2.2       jym 		module_init(file);
    672      1.33     joerg 		if (btinfo_modulelist) {
    673      1.33     joerg 			mbm = alloc(sizeof(struct multiboot_module) *
    674      1.33     joerg 					   btinfo_modulelist->num);
    675      1.33     joerg 
    676      1.33     joerg 			bim = (struct bi_modulelist_entry *)
    677      1.33     joerg 			  (((char *) btinfo_modulelist) +
    678      1.33     joerg 			   sizeof(struct btinfo_modulelist));
    679      1.33     joerg 			for (i = 0; i < btinfo_modulelist->num; i++) {
    680      1.33     joerg 				mbm[i].mmo_start = bim->base;
    681      1.33     joerg 				mbm[i].mmo_end = bim->base + bim->len;
    682      1.33     joerg 				mbm[i].mmo_string = (char *)vtophys(bim->path);
    683      1.33     joerg 				mbm[i].mmo_reserved = 0;
    684      1.33     joerg 				bim++;
    685      1.33     joerg 			}
    686      1.33     joerg 			mbi->mi_flags |= MULTIBOOT_INFO_HAS_MODS;
    687      1.33     joerg 			mbi->mi_mods_count = btinfo_modulelist->num;
    688      1.33     joerg 			mbi->mi_mods_addr = vtophys(mbm);
    689      1.33     joerg 		}
    690      1.33     joerg 	}
    691      1.33     joerg 
    692      1.33     joerg #ifdef DEBUG
    693      1.33     joerg 	printf("Start @ 0x%lx [%ld=0x%lx-0x%lx]...\n", marks[MARK_ENTRY],
    694      1.33     joerg 	    marks[MARK_NSYM], marks[MARK_SYM], marks[MARK_END]);
    695      1.33     joerg #endif
    696      1.33     joerg 
    697      1.33     joerg 
    698      1.33     joerg #if 0
    699      1.33     joerg 	if (btinfo_symtab.nsym) {
    700      1.33     joerg 		mbi->mi_flags |= MULTIBOOT_INFO_HAS_ELF_SYMS;
    701      1.33     joerg 		mbi->mi_elfshdr_addr = marks[MARK_SYM];
    702      1.33     joerg 	btinfo_symtab.nsym = marks[MARK_NSYM];
    703      1.33     joerg 	btinfo_symtab.ssym = marks[MARK_SYM];
    704      1.33     joerg 	btinfo_symtab.esym = marks[MARK_END];
    705      1.33     joerg #endif
    706      1.33     joerg 
    707      1.33     joerg 	multiboot(marks[MARK_ENTRY], vtophys(mbi),
    708      1.33     joerg 		  x86_trunc_page(mbi->mi_mem_lower*1024));
    709      1.33     joerg 	panic("exec returned");
    710      1.33     joerg 
    711      1.33     joerg out:
    712      1.33     joerg         dealloc(mbi, 0);
    713      1.33     joerg 	return -1;
    714      1.33     joerg }
    715  1.38.2.1       jym 
    716  1.38.2.1       jym void
    717  1.38.2.1       jym x86_progress(const char *fmt, ...)
    718  1.38.2.1       jym {
    719  1.38.2.1       jym 	va_list ap;
    720  1.38.2.1       jym 
    721  1.38.2.1       jym 	if ((howto & AB_SILENT) != 0)
    722  1.38.2.1       jym 		return;
    723  1.38.2.1       jym 	va_start(ap, fmt);
    724  1.38.2.1       jym 	vprintf(fmt, ap);
    725  1.38.2.1       jym 	va_end(ap);
    726  1.38.2.1       jym }
    727