Home | History | Annotate | Line # | Download | only in lib
exec.c revision 1.42.4.1
      1  1.42.4.1     rmind /*	$NetBSD: exec.c,v 1.42.4.1 2010/07/03 01:19:20 rmind Exp $	 */
      2      1.23        ad 
      3      1.23        ad /*-
      4      1.40        ad  * 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  * 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.40        ad #include <sys/reboot.h>
    106       1.1     perry 
    107      1.33     joerg #include <machine/multiboot.h>
    108      1.40        ad #include <machine/stdarg.h>
    109      1.33     joerg 
    110       1.1     perry #include <lib/libsa/stand.h>
    111      1.23        ad #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.23        ad #include "bootmod.h"
    117      1.42  jmcneill #include "vbe.h"
    118      1.16  jdolecek #ifdef SUPPORT_PS2
    119      1.16  jdolecek #include "biosmca.h"
    120      1.16  jdolecek #endif
    121       1.1     perry 
    122       1.6  christos #define BOOT_NARGS	6
    123       1.1     perry 
    124      1.23        ad #ifndef	PAGE_SIZE
    125      1.23        ad #define	PAGE_SIZE	4096
    126      1.23        ad #endif
    127      1.23        ad 
    128  1.42.4.1     rmind #define MODULE_WARNING_SEC	5
    129      1.41  jmcneill 
    130       1.4  drochner extern struct btinfo_console btinfo_console;
    131       1.4  drochner 
    132      1.23        ad boot_module_t *boot_modules;
    133      1.23        ad bool boot_modules_enabled = true;
    134      1.23        ad bool kernel_loaded;
    135      1.23        ad 
    136      1.39  jmcneill static struct btinfo_framebuffer btinfo_framebuffer;
    137      1.39  jmcneill 
    138      1.23        ad static struct btinfo_modulelist *btinfo_modulelist;
    139      1.23        ad static size_t btinfo_modulelist_size;
    140      1.23        ad static uint32_t image_end;
    141      1.26        ad static char module_base[64] = "/";
    142      1.40        ad static int howto;
    143      1.23        ad 
    144      1.41  jmcneill static void	module_init(const char *);
    145      1.23        ad 
    146      1.34        ad void
    147      1.39  jmcneill framebuffer_configure(struct btinfo_framebuffer *fb)
    148      1.39  jmcneill {
    149      1.39  jmcneill 	if (fb)
    150      1.39  jmcneill 		btinfo_framebuffer = *fb;
    151      1.39  jmcneill 	else {
    152      1.39  jmcneill 		btinfo_framebuffer.physaddr = 0;
    153      1.39  jmcneill 		btinfo_framebuffer.flags = 0;
    154      1.39  jmcneill 	}
    155      1.39  jmcneill }
    156      1.39  jmcneill 
    157      1.39  jmcneill void
    158      1.34        ad module_add(char *name)
    159      1.34        ad {
    160      1.34        ad 	boot_module_t *bm, *bmp;
    161      1.34        ad 	size_t len;
    162      1.34        ad 	char *str;
    163      1.34        ad 
    164      1.35        ad 	while (*name == ' ' || *name == '\t')
    165      1.35        ad 		++name;
    166      1.35        ad 
    167      1.34        ad 	bm = alloc(sizeof(boot_module_t));
    168      1.34        ad 	len = strlen(name) + 1;
    169      1.34        ad 	str = alloc(len);
    170      1.34        ad 	if (bm == NULL || str == NULL) {
    171      1.34        ad 		printf("couldn't allocate module\n");
    172      1.34        ad 		return;
    173      1.34        ad 	}
    174      1.34        ad 	memcpy(str, name, len);
    175      1.34        ad 	bm->bm_path = str;
    176      1.34        ad 	bm->bm_next = NULL;
    177      1.34        ad 	if (boot_modules == NULL)
    178      1.34        ad 		boot_modules = bm;
    179      1.34        ad 	else {
    180      1.34        ad 		for (bmp = boot_modules; bmp->bm_next;
    181      1.34        ad 		    bmp = bmp->bm_next)
    182      1.34        ad 			;
    183      1.34        ad 		bmp->bm_next = bm;
    184      1.34        ad 	}
    185      1.34        ad }
    186      1.34        ad 
    187      1.32     joerg static int
    188      1.32     joerg common_load_kernel(const char *file, u_long *basemem, u_long *extmem,
    189      1.32     joerg     physaddr_t loadaddr, int floppy, u_long marks[MARK_MAX])
    190       1.1     perry {
    191      1.32     joerg 	int fd;
    192       1.8  christos #ifdef XMS
    193       1.8  christos 	u_long		xmsmem;
    194       1.8  christos 	physaddr_t	origaddr = loadaddr;
    195       1.8  christos #endif
    196       1.8  christos 
    197      1.32     joerg 	*extmem = getextmem();
    198      1.32     joerg 	*basemem = getbasemem();
    199       1.8  christos 
    200       1.8  christos #ifdef XMS
    201       1.8  christos 	if ((getextmem1() == 0) && (xmsmem = checkxms())) {
    202       1.8  christos 	        u_long kernsize;
    203       1.8  christos 
    204       1.8  christos 		/*
    205       1.8  christos 		 * With "CONSERVATIVE_MEMDETECT", extmem is 0 because
    206       1.8  christos 		 *  getextmem() is getextmem1(). Without, the "smart"
    207       1.8  christos 		 *  methods could fail to report all memory as well.
    208       1.8  christos 		 * xmsmem is a few kB less than the actual size, but
    209       1.8  christos 		 *  better than nothing.
    210       1.8  christos 		 */
    211      1.32     joerg 		if (xmsmem > *extmem)
    212      1.32     joerg 			*extmem = xmsmem;
    213      1.21  junyoung 		/*
    214       1.8  christos 		 * Get the size of the kernel
    215       1.8  christos 		 */
    216       1.8  christos 		marks[MARK_START] = loadaddr;
    217      1.12  christos 		if ((fd = loadfile(file, marks, COUNT_KERNEL)) == -1)
    218      1.32     joerg 			return EIO;
    219       1.8  christos 		close(fd);
    220       1.8  christos 
    221       1.8  christos 		kernsize = marks[MARK_END];
    222       1.8  christos 		kernsize = (kernsize + 1023) / 1024;
    223       1.8  christos 
    224       1.8  christos 		loadaddr = xmsalloc(kernsize);
    225       1.8  christos 		if (!loadaddr)
    226      1.21  junyoung 			return ENOMEM;
    227       1.8  christos 	}
    228       1.8  christos #endif
    229       1.7  christos 	marks[MARK_START] = loadaddr;
    230      1.29  christos 	if ((fd = loadfile(file, marks,
    231      1.29  christos 	    LOAD_KERNEL & ~(floppy ? LOAD_NOTE : 0))) == -1)
    232      1.32     joerg 		return EIO;
    233       1.1     perry 
    234       1.8  christos 	close(fd);
    235      1.10  drochner 
    236      1.34        ad 	/* Now we know the root fs type, load modules for it. */
    237      1.34        ad 	module_add(fsmod);
    238      1.34        ad 	if (fsmod2 != NULL && strcmp(fsmod, fsmod2) != 0)
    239      1.34        ad 		module_add(fsmod2);
    240      1.34        ad 
    241      1.10  drochner 	/*
    242      1.10  drochner 	 * Gather some information for the kernel. Do this after the
    243      1.10  drochner 	 * "point of no return" to avoid memory leaks.
    244      1.10  drochner 	 * (but before DOS might be trashed in the XMS case)
    245      1.10  drochner 	 */
    246      1.10  drochner #ifdef PASS_BIOSGEOM
    247      1.10  drochner 	bi_getbiosgeom();
    248      1.10  drochner #endif
    249      1.10  drochner #ifdef PASS_MEMMAP
    250      1.10  drochner 	bi_getmemmap();
    251      1.10  drochner #endif
    252       1.8  christos 
    253       1.8  christos #ifdef XMS
    254       1.8  christos 	if (loadaddr != origaddr) {
    255       1.8  christos 		/*
    256      1.14      ross 		 * We now have done our last DOS IO, so we may
    257       1.8  christos 		 * trash the OS. Copy the data from the temporary
    258      1.20       wiz 		 * buffer to its real address.
    259       1.8  christos 		 */
    260       1.8  christos 		marks[MARK_START] -= loadaddr;
    261       1.8  christos 		marks[MARK_END] -= loadaddr;
    262       1.8  christos 		marks[MARK_SYM] -= loadaddr;
    263       1.8  christos 		marks[MARK_END] -= loadaddr;
    264       1.8  christos 		ppbcopy(loadaddr, origaddr, marks[MARK_END]);
    265       1.8  christos 	}
    266       1.8  christos #endif
    267       1.8  christos 	marks[MARK_END] = (((u_long) marks[MARK_END] + sizeof(int) - 1)) &
    268       1.8  christos 	    (-sizeof(int));
    269      1.23        ad 	image_end = marks[MARK_END];
    270      1.23        ad 	kernel_loaded = true;
    271       1.1     perry 
    272      1.32     joerg 	return 0;
    273      1.32     joerg }
    274      1.32     joerg 
    275      1.32     joerg int
    276      1.40        ad exec_netbsd(const char *file, physaddr_t loadaddr, int boothowto, int floppy,
    277      1.40        ad 	    void (*callback)(void))
    278      1.32     joerg {
    279      1.32     joerg 	u_long          boot_argv[BOOT_NARGS];
    280      1.32     joerg 	u_long		marks[MARK_MAX];
    281      1.32     joerg 	struct btinfo_symtab btinfo_symtab;
    282      1.32     joerg 	u_long		extmem;
    283      1.32     joerg 	u_long		basemem;
    284      1.32     joerg 
    285      1.32     joerg #ifdef	DEBUG
    286      1.32     joerg 	printf("exec: file=%s loadaddr=0x%lx\n",
    287      1.32     joerg 	       file ? file : "NULL", loadaddr);
    288      1.32     joerg #endif
    289      1.32     joerg 
    290      1.32     joerg 	BI_ALLOC(32); /* ??? */
    291      1.32     joerg 
    292      1.32     joerg 	BI_ADD(&btinfo_console, BTINFO_CONSOLE, sizeof(struct btinfo_console));
    293      1.32     joerg 
    294      1.40        ad 	howto = boothowto;
    295      1.40        ad 
    296      1.32     joerg 	if (common_load_kernel(file, &basemem, &extmem, loadaddr, floppy, marks))
    297      1.32     joerg 		goto out;
    298      1.32     joerg 
    299      1.31     joerg 	boot_argv[0] = boothowto;
    300      1.31     joerg 	boot_argv[1] = 0;
    301      1.31     joerg 	boot_argv[2] = vtophys(bootinfo);	/* old cyl offset */
    302      1.31     joerg 	boot_argv[3] = marks[MARK_END];
    303      1.31     joerg 	boot_argv[4] = extmem;
    304      1.31     joerg 	boot_argv[5] = basemem;
    305      1.31     joerg 
    306      1.23        ad 	/* pull in any modules if necessary */
    307      1.23        ad 	if (boot_modules_enabled) {
    308      1.41  jmcneill 		module_init(file);
    309      1.23        ad 		if (btinfo_modulelist) {
    310      1.23        ad 			BI_ADD(btinfo_modulelist, BTINFO_MODULELIST,
    311      1.23        ad 			    btinfo_modulelist_size);
    312      1.23        ad 		}
    313      1.23        ad 	}
    314       1.1     perry 
    315       1.1     perry #ifdef DEBUG
    316       1.8  christos 	printf("Start @ 0x%lx [%ld=0x%lx-0x%lx]...\n", marks[MARK_ENTRY],
    317       1.7  christos 	    marks[MARK_NSYM], marks[MARK_SYM], marks[MARK_END]);
    318       1.1     perry #endif
    319       1.1     perry 
    320       1.8  christos 	btinfo_symtab.nsym = marks[MARK_NSYM];
    321       1.8  christos 	btinfo_symtab.ssym = marks[MARK_SYM];
    322       1.8  christos 	btinfo_symtab.esym = marks[MARK_END];
    323       1.8  christos 	BI_ADD(&btinfo_symtab, BTINFO_SYMTAB, sizeof(struct btinfo_symtab));
    324       1.8  christos 
    325      1.42  jmcneill 	/* set new video mode if necessary */
    326      1.42  jmcneill 	vbe_commit();
    327      1.42  jmcneill 	BI_ADD(&btinfo_framebuffer, BTINFO_FRAMEBUFFER,
    328      1.42  jmcneill 	    sizeof(struct btinfo_framebuffer));
    329      1.42  jmcneill 
    330      1.40        ad 	if (callback != NULL)
    331      1.40        ad 		(*callback)();
    332      1.15       dbj 	startprog(marks[MARK_ENTRY], BOOT_NARGS, boot_argv,
    333      1.21  junyoung 		  x86_trunc_page(basemem*1024));
    334       1.1     perry 	panic("exec returned");
    335       1.1     perry 
    336       1.4  drochner out:
    337       1.4  drochner 	BI_FREE();
    338       1.4  drochner 	bootinfo = 0;
    339      1.21  junyoung 	return -1;
    340       1.1     perry }
    341      1.23        ad 
    342      1.41  jmcneill static void
    343      1.41  jmcneill extract_device(const char *path, char *buf, size_t buflen)
    344      1.41  jmcneill {
    345      1.41  jmcneill 	int i;
    346      1.41  jmcneill 
    347      1.41  jmcneill 	if (strchr(path, ':') != NULL) {
    348      1.41  jmcneill 		for (i = 0; i < buflen - 2 && path[i] != ':'; i++)
    349      1.41  jmcneill 			buf[i] = path[i];
    350      1.41  jmcneill 		buf[i++] = ':';
    351      1.41  jmcneill 		buf[i] = '\0';
    352      1.41  jmcneill 	} else
    353      1.41  jmcneill 		buf[0] = '\0';
    354      1.41  jmcneill }
    355      1.41  jmcneill 
    356      1.26        ad static const char *
    357      1.41  jmcneill module_path(boot_module_t *bm, const char *kdev)
    358      1.26        ad {
    359      1.26        ad 	static char buf[256];
    360      1.41  jmcneill 	char name_buf[256], dev_buf[64];
    361      1.41  jmcneill 	const char *name, *name2, *p;
    362      1.26        ad 
    363      1.26        ad 	name = bm->bm_path;
    364      1.33     joerg 	for (name2 = name; *name2; ++name2) {
    365      1.33     joerg 		if (*name2 == ' ' || *name2 == '\t') {
    366      1.33     joerg 			strlcpy(name_buf, name, sizeof(name_buf));
    367      1.33     joerg 			if (name2 - name < sizeof(name_buf))
    368      1.33     joerg 				name_buf[name2 - name] = '\0';
    369      1.33     joerg 			name = name_buf;
    370      1.33     joerg 			break;
    371      1.33     joerg 		}
    372      1.33     joerg 	}
    373      1.41  jmcneill 	if ((p = strchr(name, ':')) != NULL) {
    374      1.41  jmcneill 		/* device specified, use it */
    375      1.41  jmcneill 		if (p[1] == '/')
    376      1.41  jmcneill 			snprintf(buf, sizeof(buf), "%s", name);
    377      1.41  jmcneill 		else {
    378      1.41  jmcneill 			p++;
    379      1.41  jmcneill 			extract_device(name, dev_buf, sizeof(dev_buf));
    380      1.41  jmcneill 			snprintf(buf, sizeof(buf), "%s%s/%s/%s.kmod",
    381      1.41  jmcneill 			    dev_buf, module_base, p, p);
    382      1.41  jmcneill 		}
    383      1.41  jmcneill 	} else {
    384      1.41  jmcneill 		/* device not specified; load from kernel device if known */
    385      1.41  jmcneill  		if (name[0] == '/')
    386      1.41  jmcneill 			snprintf(buf, sizeof(buf), "%s%s", kdev, name);
    387      1.41  jmcneill 		else
    388      1.41  jmcneill 			snprintf(buf, sizeof(buf), "%s%s/%s/%s.kmod",
    389      1.41  jmcneill 			    kdev, module_base, name, name);
    390      1.41  jmcneill 	}
    391      1.33     joerg 
    392      1.26        ad 	return buf;
    393      1.26        ad }
    394      1.26        ad 
    395      1.36  christos static int
    396      1.41  jmcneill module_open(boot_module_t *bm, int mode, const char *kdev, bool doload)
    397      1.28     chris {
    398      1.28     chris 	int fd;
    399      1.28     chris 	const char *path;
    400      1.36  christos 
    401      1.28     chris 	/* check the expanded path first */
    402      1.41  jmcneill 	path = module_path(bm, kdev);
    403      1.28     chris 	fd = open(path, mode);
    404      1.41  jmcneill 	if (fd != -1) {
    405      1.41  jmcneill 		if ((howto & AB_SILENT) == 0 && doload)
    406      1.41  jmcneill 			printf("Loading %s ", path);
    407      1.41  jmcneill 	} else {
    408      1.28     chris 		/* now attempt the raw path provided */
    409      1.28     chris 		fd = open(bm->bm_path, mode);
    410      1.41  jmcneill 		if (fd != -1 && (howto & AB_SILENT) == 0 && doload)
    411      1.41  jmcneill 			printf("Loading %s ", bm->bm_path);
    412      1.41  jmcneill 	}
    413      1.41  jmcneill 	if (!doload && fd == -1) {
    414      1.41  jmcneill 		printf("WARNING: couldn't open %s", bm->bm_path);
    415      1.41  jmcneill 		if (strcmp(bm->bm_path, path) != 0)
    416      1.41  jmcneill 			printf(" (%s)", path);
    417      1.41  jmcneill 		printf("\n");
    418      1.28     chris 	}
    419      1.28     chris 	return fd;
    420      1.28     chris }
    421      1.28     chris 
    422      1.23        ad static void
    423      1.41  jmcneill module_init(const char *kernel_path)
    424      1.23        ad {
    425      1.23        ad 	struct bi_modulelist_entry *bi;
    426      1.23        ad 	struct stat st;
    427      1.30     joerg 	const char *machine;
    428      1.41  jmcneill 	char kdev[64];
    429      1.23        ad 	char *buf;
    430      1.23        ad 	boot_module_t *bm;
    431      1.23        ad 	size_t len;
    432      1.23        ad 	off_t off;
    433      1.41  jmcneill 	int err, fd, nfail = 0;
    434      1.41  jmcneill 
    435      1.41  jmcneill 	extract_device(kernel_path, kdev, sizeof(kdev));
    436      1.23        ad 
    437      1.30     joerg 	switch (netbsd_elf_class) {
    438      1.30     joerg 	case ELFCLASS32:
    439      1.30     joerg 		machine = "i386";
    440      1.30     joerg 		break;
    441      1.30     joerg 	case ELFCLASS64:
    442      1.30     joerg 		machine = "amd64";
    443      1.30     joerg 		break;
    444      1.30     joerg 	default:
    445      1.30     joerg 		machine = "generic";
    446      1.30     joerg 		break;
    447      1.30     joerg 	}
    448      1.30     joerg 	if (netbsd_version / 1000000 % 100 == 99) {
    449      1.30     joerg 		/* -current */
    450      1.30     joerg 		snprintf(module_base, sizeof(module_base),
    451      1.38     rmind 		    "/stand/%s/%d.%d.%d/modules", machine,
    452      1.30     joerg 		    netbsd_version / 100000000,
    453      1.30     joerg 		    netbsd_version / 1000000 % 100,
    454      1.30     joerg 		    netbsd_version / 100 % 100);
    455      1.30     joerg 	} else if (netbsd_version != 0) {
    456      1.30     joerg 		/* release */
    457      1.30     joerg 		snprintf(module_base, sizeof(module_base),
    458      1.38     rmind 		    "/stand/%s/%d.%d/modules", machine,
    459      1.30     joerg 		    netbsd_version / 100000000,
    460      1.30     joerg 		    netbsd_version / 1000000 % 100);
    461      1.30     joerg 	}
    462      1.30     joerg 
    463      1.23        ad 	/* First, see which modules are valid and calculate btinfo size */
    464      1.23        ad 	len = sizeof(struct btinfo_modulelist);
    465      1.23        ad 	for (bm = boot_modules; bm; bm = bm->bm_next) {
    466      1.41  jmcneill 		fd = module_open(bm, 0, kdev, false);
    467      1.23        ad 		if (fd == -1) {
    468      1.23        ad 			bm->bm_len = -1;
    469      1.41  jmcneill 			++nfail;
    470      1.23        ad 			continue;
    471      1.23        ad 		}
    472      1.23        ad 		err = fstat(fd, &st);
    473      1.24        ad 		if (err == -1 || st.st_size == -1) {
    474      1.28     chris 			printf("WARNING: couldn't stat %s\n", bm->bm_path);
    475      1.23        ad 			close(fd);
    476      1.23        ad 			bm->bm_len = -1;
    477      1.41  jmcneill 			++nfail;
    478      1.23        ad 			continue;
    479      1.23        ad 		}
    480      1.23        ad 		bm->bm_len = st.st_size;
    481      1.23        ad 		close(fd);
    482      1.23        ad 		len += sizeof(struct bi_modulelist_entry);
    483      1.23        ad 	}
    484      1.23        ad 
    485      1.23        ad 	/* Allocate the module list */
    486      1.23        ad 	btinfo_modulelist = alloc(len);
    487      1.23        ad 	if (btinfo_modulelist == NULL) {
    488      1.23        ad 		printf("WARNING: couldn't allocate module list\n");
    489  1.42.4.1     rmind 		wait_sec(MODULE_WARNING_SEC);
    490      1.23        ad 		return;
    491      1.23        ad 	}
    492      1.23        ad 	memset(btinfo_modulelist, 0, len);
    493      1.23        ad 	btinfo_modulelist_size = len;
    494      1.23        ad 
    495      1.23        ad 	/* Fill in btinfo structure */
    496      1.23        ad 	buf = (char *)btinfo_modulelist;
    497      1.23        ad 	btinfo_modulelist->num = 0;
    498      1.23        ad 	off = sizeof(struct btinfo_modulelist);
    499      1.23        ad 
    500      1.23        ad 	for (bm = boot_modules; bm; bm = bm->bm_next) {
    501      1.23        ad 		if (bm->bm_len == -1)
    502      1.23        ad 			continue;
    503      1.41  jmcneill 		fd = module_open(bm, 0, kdev, true);
    504      1.41  jmcneill 		if (fd == -1)
    505      1.23        ad 			continue;
    506      1.24        ad 		image_end = (image_end + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
    507      1.24        ad 		len = pread(fd, (void *)image_end, SSIZE_MAX);
    508      1.24        ad 		if (len < bm->bm_len) {
    509      1.40        ad 			if ((howto & AB_SILENT) != 0)
    510      1.40        ad 				printf("Loading %s ", bm->bm_path);
    511      1.23        ad 			printf(" FAILED\n");
    512      1.23        ad 		} else {
    513      1.23        ad 			btinfo_modulelist->num++;
    514      1.23        ad 			bi = (struct bi_modulelist_entry *)(buf + off);
    515      1.23        ad 			off += sizeof(struct bi_modulelist_entry);
    516      1.23        ad 			strncpy(bi->path, bm->bm_path, sizeof(bi->path) - 1);
    517      1.24        ad 			bi->base = image_end;
    518      1.24        ad 			bi->len = len;
    519      1.23        ad 			bi->type = BI_MODULE_ELF;
    520      1.40        ad 			if ((howto & AB_SILENT) == 0)
    521      1.40        ad 				printf(" \n");
    522      1.23        ad 		}
    523      1.24        ad 		if (len > 0)
    524      1.24        ad 			image_end += len;
    525      1.23        ad 		close(fd);
    526      1.23        ad 	}
    527      1.23        ad 	btinfo_modulelist->endpa = image_end;
    528      1.41  jmcneill 
    529      1.41  jmcneill 	if (nfail > 0) {
    530      1.41  jmcneill 		printf("WARNING: %d module%s failed to load\n",
    531      1.41  jmcneill 		    nfail, nfail == 1 ? "" : "s");
    532      1.41  jmcneill #if notyet
    533  1.42.4.1     rmind 		wait_sec(MODULE_WARNING_SEC);
    534      1.41  jmcneill #endif
    535      1.41  jmcneill 	}
    536      1.23        ad }
    537      1.33     joerg 
    538      1.33     joerg int
    539      1.33     joerg exec_multiboot(const char *file, char *args)
    540      1.33     joerg {
    541      1.33     joerg 	struct multiboot_info *mbi;
    542      1.33     joerg 	struct multiboot_module *mbm;
    543      1.33     joerg 	struct bi_modulelist_entry *bim;
    544      1.33     joerg 	int		i, len;
    545      1.33     joerg 	u_long		marks[MARK_MAX];
    546      1.33     joerg 	u_long		extmem;
    547      1.33     joerg 	u_long		basemem;
    548      1.33     joerg 	char		*cmdline;
    549      1.33     joerg 
    550      1.33     joerg 	mbi = alloc(sizeof(struct multiboot_info));
    551      1.33     joerg 	mbi->mi_flags = MULTIBOOT_INFO_HAS_MEMORY;
    552      1.33     joerg 
    553      1.33     joerg 	if (common_load_kernel(file, &basemem, &extmem, 0, 0, marks))
    554      1.33     joerg 		goto out;
    555      1.33     joerg 
    556      1.33     joerg 	mbi->mi_mem_upper = extmem;
    557      1.33     joerg 	mbi->mi_mem_lower = basemem;
    558      1.33     joerg 
    559      1.33     joerg 	if (args) {
    560      1.33     joerg 		mbi->mi_flags |= MULTIBOOT_INFO_HAS_CMDLINE;
    561      1.33     joerg 		len = strlen(file) + 1 + strlen(args) + 1;
    562      1.33     joerg 		cmdline = alloc(len);
    563      1.33     joerg 		snprintf(cmdline, len, "%s %s", file, args);
    564      1.33     joerg 		mbi->mi_cmdline = (char *) vtophys(cmdline);
    565      1.33     joerg 	}
    566      1.33     joerg 
    567      1.33     joerg 	/* pull in any modules if necessary */
    568      1.33     joerg 	if (boot_modules_enabled) {
    569      1.41  jmcneill 		module_init(file);
    570      1.33     joerg 		if (btinfo_modulelist) {
    571      1.33     joerg 			mbm = alloc(sizeof(struct multiboot_module) *
    572      1.33     joerg 					   btinfo_modulelist->num);
    573      1.33     joerg 
    574      1.33     joerg 			bim = (struct bi_modulelist_entry *)
    575      1.33     joerg 			  (((char *) btinfo_modulelist) +
    576      1.33     joerg 			   sizeof(struct btinfo_modulelist));
    577      1.33     joerg 			for (i = 0; i < btinfo_modulelist->num; i++) {
    578      1.33     joerg 				mbm[i].mmo_start = bim->base;
    579      1.33     joerg 				mbm[i].mmo_end = bim->base + bim->len;
    580      1.33     joerg 				mbm[i].mmo_string = (char *)vtophys(bim->path);
    581      1.33     joerg 				mbm[i].mmo_reserved = 0;
    582      1.33     joerg 				bim++;
    583      1.33     joerg 			}
    584      1.33     joerg 			mbi->mi_flags |= MULTIBOOT_INFO_HAS_MODS;
    585      1.33     joerg 			mbi->mi_mods_count = btinfo_modulelist->num;
    586      1.33     joerg 			mbi->mi_mods_addr = vtophys(mbm);
    587      1.33     joerg 		}
    588      1.33     joerg 	}
    589      1.33     joerg 
    590      1.33     joerg #ifdef DEBUG
    591      1.33     joerg 	printf("Start @ 0x%lx [%ld=0x%lx-0x%lx]...\n", marks[MARK_ENTRY],
    592      1.33     joerg 	    marks[MARK_NSYM], marks[MARK_SYM], marks[MARK_END]);
    593      1.33     joerg #endif
    594      1.33     joerg 
    595      1.33     joerg 
    596      1.33     joerg #if 0
    597      1.33     joerg 	if (btinfo_symtab.nsym) {
    598      1.33     joerg 		mbi->mi_flags |= MULTIBOOT_INFO_HAS_ELF_SYMS;
    599      1.33     joerg 		mbi->mi_elfshdr_addr = marks[MARK_SYM];
    600      1.33     joerg 	btinfo_symtab.nsym = marks[MARK_NSYM];
    601      1.33     joerg 	btinfo_symtab.ssym = marks[MARK_SYM];
    602      1.33     joerg 	btinfo_symtab.esym = marks[MARK_END];
    603      1.33     joerg #endif
    604      1.33     joerg 
    605      1.33     joerg 	multiboot(marks[MARK_ENTRY], vtophys(mbi),
    606      1.33     joerg 		  x86_trunc_page(mbi->mi_mem_lower*1024));
    607      1.33     joerg 	panic("exec returned");
    608      1.33     joerg 
    609      1.33     joerg out:
    610      1.33     joerg         dealloc(mbi, 0);
    611      1.33     joerg 	return -1;
    612      1.33     joerg }
    613      1.40        ad 
    614      1.40        ad void
    615      1.40        ad x86_progress(const char *fmt, ...)
    616      1.40        ad {
    617      1.40        ad 	va_list ap;
    618      1.40        ad 
    619      1.40        ad 	if ((howto & AB_SILENT) != 0)
    620      1.40        ad 		return;
    621      1.40        ad 	va_start(ap, fmt);
    622      1.40        ad 	vprintf(fmt, ap);
    623      1.40        ad 	va_end(ap);
    624      1.40        ad }
    625