Home | History | Annotate | Line # | Download | only in lib
exec.c revision 1.24
      1 /*	$NetBSD: exec.c,v 1.24 2008/05/03 18:49:13 ad 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  * Copyright (c) 1997
     66  * 	Martin Husemann.  All rights reserved.
     67  *
     68  * Redistribution and use in source and binary forms, with or without
     69  * modification, are permitted provided that the following conditions
     70  * are met:
     71  * 1. Redistributions of source code must retain the above copyright
     72  *    notice, this list of conditions and the following disclaimer.
     73  * 2. Redistributions in binary form must reproduce the above copyright
     74  *    notice, this list of conditions and the following disclaimer in the
     75  *    documentation and/or other materials provided with the distribution.
     76  * 3. All advertising materials mentioning features or use of this software
     77  *    must display the following acknowledgement:
     78  *	This product includes software developed by the University of
     79  *	California, Berkeley and its contributors.
     80  * 4. Neither the name of the University nor the names of its contributors
     81  *    may be used to endorse or promote products derived from this software
     82  *    without specific prior written permission.
     83  *
     84  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     85  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     86  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     87  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     88  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     89  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     90  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     91  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     92  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     93  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     94  * SUCH DAMAGE.
     95  *
     96  * 	@(#)boot.c	8.1 (Berkeley) 6/10/93
     97  */
     98 
     99 /*
    100  * starts NetBSD a.out kernel
    101  * needs lowlevel startup from startprog.S
    102  * This is a special version of exec.c to support use of XMS.
    103  */
    104 
    105 #include <sys/param.h>
    106 #include <sys/reboot.h>
    107 
    108 #include <lib/libsa/stand.h>
    109 #include <lib/libkern/libkern.h>
    110 
    111 #include "loadfile.h"
    112 #include "libi386.h"
    113 #include "bootinfo.h"
    114 #include "bootmod.h"
    115 #ifdef SUPPORT_PS2
    116 #include "biosmca.h"
    117 #endif
    118 
    119 #define BOOT_NARGS	6
    120 
    121 #ifndef	PAGE_SIZE
    122 #define	PAGE_SIZE	4096
    123 #endif
    124 
    125 extern struct btinfo_console btinfo_console;
    126 
    127 boot_module_t *boot_modules;
    128 bool boot_modules_enabled = true;
    129 bool kernel_loaded;
    130 
    131 static struct btinfo_modulelist *btinfo_modulelist;
    132 static size_t btinfo_modulelist_size;
    133 static uint32_t image_end;
    134 
    135 static void	module_init(void);
    136 
    137 int
    138 exec_netbsd(const char *file, physaddr_t loadaddr, int boothowto)
    139 {
    140 	u_long          boot_argv[BOOT_NARGS];
    141 	int		fd;
    142 	u_long		marks[MARK_MAX];
    143 	struct btinfo_symtab btinfo_symtab;
    144 	u_long		extmem;
    145 	u_long		basemem;
    146 #ifdef XMS
    147 	u_long		xmsmem;
    148 	physaddr_t	origaddr = loadaddr;
    149 #endif
    150 
    151 #ifdef	DEBUG
    152 	printf("exec: file=%s loadaddr=0x%lx\n",
    153 	       file ? file : "NULL", loadaddr);
    154 #endif
    155 
    156 	BI_ALLOC(32); /* ??? */
    157 
    158 	BI_ADD(&btinfo_console, BTINFO_CONSOLE, sizeof(struct btinfo_console));
    159 
    160 	extmem = getextmem();
    161 	basemem = getbasemem();
    162 
    163 #ifdef XMS
    164 	if ((getextmem1() == 0) && (xmsmem = checkxms())) {
    165 	        u_long kernsize;
    166 
    167 		/*
    168 		 * With "CONSERVATIVE_MEMDETECT", extmem is 0 because
    169 		 *  getextmem() is getextmem1(). Without, the "smart"
    170 		 *  methods could fail to report all memory as well.
    171 		 * xmsmem is a few kB less than the actual size, but
    172 		 *  better than nothing.
    173 		 */
    174 		if (xmsmem > extmem)
    175 			extmem = xmsmem;
    176 		/*
    177 		 * Get the size of the kernel
    178 		 */
    179 		marks[MARK_START] = loadaddr;
    180 		if ((fd = loadfile(file, marks, COUNT_KERNEL)) == -1)
    181 			goto out;
    182 		close(fd);
    183 
    184 		kernsize = marks[MARK_END];
    185 		kernsize = (kernsize + 1023) / 1024;
    186 
    187 		loadaddr = xmsalloc(kernsize);
    188 		if (!loadaddr)
    189 			return ENOMEM;
    190 	}
    191 #endif
    192 	marks[MARK_START] = loadaddr;
    193 	if ((fd = loadfile(file, marks, LOAD_KERNEL)) == -1)
    194 		goto out;
    195 
    196 	boot_argv[0] = boothowto;
    197 	boot_argv[1] = 0;
    198 	boot_argv[2] = vtophys(bootinfo);	/* old cyl offset */
    199 	/* argv[3] below */
    200 	boot_argv[4] = extmem;
    201 	boot_argv[5] = basemem;
    202 
    203 	close(fd);
    204 
    205 	/*
    206 	 * Gather some information for the kernel. Do this after the
    207 	 * "point of no return" to avoid memory leaks.
    208 	 * (but before DOS might be trashed in the XMS case)
    209 	 */
    210 #ifdef PASS_BIOSGEOM
    211 	bi_getbiosgeom();
    212 #endif
    213 #ifdef PASS_MEMMAP
    214 	bi_getmemmap();
    215 #endif
    216 
    217 #ifdef XMS
    218 	if (loadaddr != origaddr) {
    219 		/*
    220 		 * We now have done our last DOS IO, so we may
    221 		 * trash the OS. Copy the data from the temporary
    222 		 * buffer to its real address.
    223 		 */
    224 		marks[MARK_START] -= loadaddr;
    225 		marks[MARK_END] -= loadaddr;
    226 		marks[MARK_SYM] -= loadaddr;
    227 		marks[MARK_END] -= loadaddr;
    228 		ppbcopy(loadaddr, origaddr, marks[MARK_END]);
    229 	}
    230 #endif
    231 	marks[MARK_END] = (((u_long) marks[MARK_END] + sizeof(int) - 1)) &
    232 	    (-sizeof(int));
    233 	boot_argv[3] = marks[MARK_END];
    234 	image_end = marks[MARK_END];
    235 	kernel_loaded = true;
    236 
    237 	/* pull in any modules if necessary */
    238 	if (boot_modules_enabled) {
    239 		module_init();
    240 		if (btinfo_modulelist) {
    241 			BI_ADD(btinfo_modulelist, BTINFO_MODULELIST,
    242 			    btinfo_modulelist_size);
    243 		}
    244 	}
    245 
    246 #ifdef DEBUG
    247 	printf("Start @ 0x%lx [%ld=0x%lx-0x%lx]...\n", marks[MARK_ENTRY],
    248 	    marks[MARK_NSYM], marks[MARK_SYM], marks[MARK_END]);
    249 #endif
    250 
    251 	btinfo_symtab.nsym = marks[MARK_NSYM];
    252 	btinfo_symtab.ssym = marks[MARK_SYM];
    253 	btinfo_symtab.esym = marks[MARK_END];
    254 	BI_ADD(&btinfo_symtab, BTINFO_SYMTAB, sizeof(struct btinfo_symtab));
    255 
    256 	startprog(marks[MARK_ENTRY], BOOT_NARGS, boot_argv,
    257 		  x86_trunc_page(basemem*1024));
    258 	panic("exec returned");
    259 
    260 out:
    261 	BI_FREE();
    262 	bootinfo = 0;
    263 	return -1;
    264 }
    265 
    266 static void
    267 module_init(void)
    268 {
    269 	struct bi_modulelist_entry *bi;
    270 	struct stat st;
    271 	char *buf;
    272 	boot_module_t *bm;
    273 	size_t len;
    274 	off_t off;
    275 	int err, fd;
    276 
    277 	/* First, see which modules are valid and calculate btinfo size */
    278 	len = sizeof(struct btinfo_modulelist);
    279 	for (bm = boot_modules; bm; bm = bm->bm_next) {
    280 		fd = open(bm->bm_path, 0);
    281 		if (fd == -1) {
    282 			printf("WARNING: couldn't open %s\n", bm->bm_path);
    283 			bm->bm_len = -1;
    284 			continue;
    285 		}
    286 		err = fstat(fd, &st);
    287 		if (err == -1 || st.st_size == -1) {
    288 			printf("WARNING: couldn't stat %s\n", bm->bm_path);
    289 			close(fd);
    290 			bm->bm_len = -1;
    291 			continue;
    292 		}
    293 		bm->bm_len = st.st_size;
    294 		close(fd);
    295 		len += sizeof(struct bi_modulelist_entry);
    296 	}
    297 
    298 	/* Allocate the module list */
    299 	btinfo_modulelist = alloc(len);
    300 	if (btinfo_modulelist == NULL) {
    301 		printf("WARNING: couldn't allocate module list\n");
    302 		return;
    303 	}
    304 	memset(btinfo_modulelist, 0, len);
    305 	btinfo_modulelist_size = len;
    306 
    307 	/* Fill in btinfo structure */
    308 	buf = (char *)btinfo_modulelist;
    309 	btinfo_modulelist->num = 0;
    310 	off = sizeof(struct btinfo_modulelist);
    311 
    312 	for (bm = boot_modules; bm; bm = bm->bm_next) {
    313 		if (bm->bm_len == -1)
    314 			continue;
    315 		printf("Loading %s ", bm->bm_path);
    316 		fd = open(bm->bm_path, 0);
    317 		if (fd == -1) {
    318 			printf("ERROR: couldn't open %s\n", bm->bm_path);
    319 			continue;
    320 		}
    321 		image_end = (image_end + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
    322 		len = pread(fd, (void *)image_end, SSIZE_MAX);
    323 		if (len < bm->bm_len) {
    324 			printf(" FAILED\n");
    325 		} else {
    326 			btinfo_modulelist->num++;
    327 			bi = (struct bi_modulelist_entry *)(buf + off);
    328 			off += sizeof(struct bi_modulelist_entry);
    329 			strncpy(bi->path, bm->bm_path, sizeof(bi->path) - 1);
    330 			bi->base = image_end;
    331 			bi->len = len;
    332 			bi->type = BI_MODULE_ELF;
    333 			printf(" \n");
    334 		}
    335 		if (len > 0)
    336 			image_end += len;
    337 		close(fd);
    338 	}
    339 	btinfo_modulelist->endpa = image_end;
    340 }
    341