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