Home | History | Annotate | Line # | Download | only in lib
exec.c revision 1.11
      1 /*	$NetBSD: exec.c,v 1.11 1999/04/14 11:19:23 drochner Exp $	 */
      2 
      3 /*
      4  * Copyright (c) 1982, 1986, 1990, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  * Copyright (c) 1996
      7  *	Matthias Drochner.  All rights reserved.
      8  * Copyright (c) 1996
      9  * 	Perry E. Metzger.  All rights reserved.
     10  * Copyright (c) 1997
     11  * 	Martin Husemann.  All rights reserved.
     12  *
     13  * Redistribution and use in source and binary forms, with or without
     14  * modification, are permitted provided that the following conditions
     15  * are met:
     16  * 1. Redistributions of source code must retain the above copyright
     17  *    notice, this list of conditions and the following disclaimer.
     18  * 2. Redistributions in binary form must reproduce the above copyright
     19  *    notice, this list of conditions and the following disclaimer in the
     20  *    documentation and/or other materials provided with the distribution.
     21  * 3. All advertising materials mentioning features or use of this software
     22  *    must display the following acknowledgement:
     23  *	This product includes software developed by the University of
     24  *	California, Berkeley and its contributors.
     25  * 4. Neither the name of the University nor the names of its contributors
     26  *    may be used to endorse or promote products derived from this software
     27  *    without specific prior written permission.
     28  *
     29  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     30  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     31  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     32  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     33  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     34  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     35  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     38  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     39  * SUCH DAMAGE.
     40  *
     41  * 	@(#)boot.c	8.1 (Berkeley) 6/10/93
     42  */
     43 
     44 /*
     45  * starts NetBSD a.out kernel
     46  * needs lowlevel startup from startprog.S
     47  * This is a special version of exec.c to support use of XMS.
     48  */
     49 
     50 #include <sys/param.h>
     51 #include <sys/reboot.h>
     52 #ifdef COMPAT_OLDBOOT
     53 #include <sys/disklabel.h>
     54 #endif
     55 
     56 #include <lib/libsa/stand.h>
     57 
     58 #include "loadfile.h"
     59 #include "libi386.h"
     60 #include "bootinfo.h"
     61 
     62 #ifdef COMPAT_OLDBOOT
     63 static int dev2major __P((char *, int *));
     64 
     65 static int
     66 dev2major(devname, major)
     67 	char           *devname;
     68 	int            *major;
     69 {
     70 	static char    *devices[] = {"wd", "", "fd", "", "sd"};
     71 #define NUMDEVICES (sizeof(devices)/sizeof(char *))
     72 	int             i;
     73 
     74 	for (i = 0; i < NUMDEVICES; i++)
     75 		if (!strcmp(devname, devices[i])) {
     76 			*major = i;
     77 			return (0);
     78 		}
     79 	return (-1);
     80 }
     81 #endif
     82 #define BOOT_NARGS	6
     83 
     84 extern struct btinfo_console btinfo_console;
     85 
     86 int
     87 exec_netbsd(file, loadaddr, boothowto)
     88 	const char     *file;
     89 	physaddr_t      loadaddr;
     90 	int             boothowto;
     91 {
     92 	u_long          boot_argv[BOOT_NARGS];
     93 	int		fd;
     94 	u_long		marks[MARK_MAX];
     95 	struct btinfo_symtab btinfo_symtab;
     96 	u_long		extmem;
     97 #ifdef XMS
     98 	u_long		xmsmem;
     99 	physaddr_t	origaddr = loadaddr;
    100 #endif
    101 
    102 #ifdef COMPAT_OLDBOOT
    103 	char           *fsname, *devname;
    104 	int             unit, part;
    105 	const char     *filename;
    106 	int             bootdevnr;
    107 #endif
    108 
    109 #ifdef	DEBUG
    110 	printf("exec: file=%s loadaddr=0x%lx\n", file, loadaddr);
    111 #endif
    112 
    113 	BI_ALLOC(6); /* ??? */
    114 
    115 	BI_ADD(&btinfo_console, BTINFO_CONSOLE, sizeof(struct btinfo_console));
    116 
    117 	extmem = getextmem();
    118 
    119 #ifdef XMS
    120 	if ((getextmem1() == 0) && (xmsmem = checkxms())) {
    121 	        u_long kernsize;
    122 
    123 		/*
    124 		 * With "CONSERVATIVE_MEMDETECT", extmem is 0 because
    125 		 *  getextmem() is getextmem1(). Without, the "smart"
    126 		 *  methods could fail to report all memory as well.
    127 		 * xmsmem is a few kB less than the actual size, but
    128 		 *  better than nothing.
    129 		 */
    130 		if (xmsmem > extmem)
    131 			extmem = xmsmem;
    132 		/*
    133 		 * Get the size of the kernel
    134 		 */
    135 		marks[MARK_START] = loadaddr;
    136 		if ((fd = loadfile(file, marks, COUNT_ALL)) == -1)
    137 			goto out;
    138 		close(fd);
    139 
    140 		kernsize = marks[MARK_END];
    141 		kernsize = (kernsize + 1023) / 1024;
    142 
    143 		loadaddr = xmsalloc(kernsize);
    144 		if (!loadaddr)
    145 			return(ENOMEM);
    146 	}
    147 #endif
    148 	marks[MARK_START] = loadaddr;
    149 	if ((fd = loadfile(file, marks, LOAD_ALL)) == -1)
    150 		goto out;
    151 
    152 	boot_argv[0] = boothowto;
    153 
    154 #ifdef COMPAT_OLDBOOT
    155 	/* prepare boot device information for kernel */
    156 	if (parsebootfile(file, &fsname, &devname, &unit, &part, &filename)
    157 	    || strcmp(fsname, "ufs"))
    158 		bootdevnr = 0;	/* XXX error out if parse error??? */
    159 	else {
    160 		int             major;
    161 
    162 		if (strcmp(devname, "hd") == 0) {
    163 			/* generic BIOS disk, have to guess type */
    164 			struct open_file *f = &files[fd];	/* XXX */
    165 
    166 			if (biosdisk_gettype(f) == DTYPE_SCSI)
    167 				devname = "sd";
    168 			else
    169 				devname = "wd";
    170 
    171 			/*
    172 			 * The old boot block performed the following
    173 			 * conversion:
    174 			 *
    175 			 *	hdN -> Xd0
    176 			 *
    177 			 * where X is the type specified by the label.
    178 			 * We mimmick that here, for lack of any better
    179 			 * way of doing things.
    180 			 */
    181 			unit = 0;
    182 		}
    183 
    184 		if (dev2major(devname, &major))
    185 			bootdevnr = 0;	/* XXX error out??? */
    186 		else
    187 			bootdevnr = MAKEBOOTDEV(major, 0, 0, unit, part);
    188 	}
    189 
    190 	boot_argv[1] = bootdevnr;
    191 #else
    192 	boot_argv[1] = 0;
    193 #endif
    194 	boot_argv[2] = vtophys(bootinfo);	/* old cyl offset */
    195 	/* argv[3] below */
    196 	boot_argv[4] = extmem;
    197 	boot_argv[5] = getbasemem();
    198 
    199 	close(fd);
    200 
    201 	/*
    202 	 * Gather some information for the kernel. Do this after the
    203 	 * "point of no return" to avoid memory leaks.
    204 	 * (but before DOS might be trashed in the XMS case)
    205 	 */
    206 #ifdef PASS_BIOSGEOM
    207 	bi_getbiosgeom();
    208 #endif
    209 #ifdef PASS_MEMMAP
    210 	bi_getmemmap();
    211 #endif
    212 
    213 #ifdef XMS
    214 	if (loadaddr != origaddr) {
    215 		/*
    216 		 * We know have done our last DOS IO, so we may
    217 		 * trash the OS. Copy the data from the temporary
    218 		 * buffer to its real adress.
    219 		 */
    220 		marks[MARK_START] -= loadaddr;
    221 		marks[MARK_END] -= loadaddr;
    222 		marks[MARK_SYM] -= loadaddr;
    223 		marks[MARK_END] -= loadaddr;
    224 		ppbcopy(loadaddr, origaddr, marks[MARK_END]);
    225 	}
    226 #endif
    227 	marks[MARK_END] = (((u_long) marks[MARK_END] + sizeof(int) - 1)) &
    228 	    (-sizeof(int));
    229 
    230 	boot_argv[3] = marks[MARK_END];
    231 
    232 
    233 #ifdef DEBUG
    234 	printf("Start @ 0x%lx [%ld=0x%lx-0x%lx]...\n", marks[MARK_ENTRY],
    235 	    marks[MARK_NSYM], marks[MARK_SYM], marks[MARK_END]);
    236 #endif
    237 
    238 	btinfo_symtab.nsym = marks[MARK_NSYM];
    239 	btinfo_symtab.ssym = marks[MARK_SYM];
    240 	btinfo_symtab.esym = marks[MARK_END];
    241 	BI_ADD(&btinfo_symtab, BTINFO_SYMTAB, sizeof(struct btinfo_symtab));
    242 
    243 	startprog(marks[MARK_ENTRY], BOOT_NARGS, boot_argv, 0x90000);
    244 	panic("exec returned");
    245 
    246 out:
    247 	BI_FREE();
    248 	bootinfo = 0;
    249 	return (-1);
    250 }
    251