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