Home | History | Annotate | Line # | Download | only in lib
exec.c revision 1.1
      1  1.1  perry /*	$NetBSD: exec.c,v 1.1 1997/03/14 02:40:32 perry 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.1  perry  *
     11  1.1  perry  * Redistribution and use in source and binary forms, with or without
     12  1.1  perry  * modification, are permitted provided that the following conditions
     13  1.1  perry  * are met:
     14  1.1  perry  * 1. Redistributions of source code must retain the above copyright
     15  1.1  perry  *    notice, this list of conditions and the following disclaimer.
     16  1.1  perry  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.1  perry  *    notice, this list of conditions and the following disclaimer in the
     18  1.1  perry  *    documentation and/or other materials provided with the distribution.
     19  1.1  perry  * 3. All advertising materials mentioning features or use of this software
     20  1.1  perry  *    must display the following acknowledgement:
     21  1.1  perry  *	This product includes software developed by the University of
     22  1.1  perry  *	California, Berkeley and its contributors.
     23  1.1  perry  * 4. Neither the name of the University nor the names of its contributors
     24  1.1  perry  *    may be used to endorse or promote products derived from this software
     25  1.1  perry  *    without specific prior written permission.
     26  1.1  perry  *
     27  1.1  perry  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     28  1.1  perry  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     29  1.1  perry  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     30  1.1  perry  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     31  1.1  perry  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     32  1.1  perry  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     33  1.1  perry  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     34  1.1  perry  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     35  1.1  perry  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     36  1.1  perry  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     37  1.1  perry  * SUCH DAMAGE.
     38  1.1  perry  *
     39  1.1  perry  * 	@(#)boot.c	8.1 (Berkeley) 6/10/93
     40  1.1  perry  */
     41  1.1  perry 
     42  1.1  perry /* starts NetBSD a.out kernel
     43  1.1  perry  needs lowlevel startup from startprog.S
     44  1.1  perry  */
     45  1.1  perry 
     46  1.1  perry #include <sys/param.h>
     47  1.1  perry #include <sys/exec_aout.h>
     48  1.1  perry #include <sys/reboot.h>
     49  1.1  perry #ifdef COMPAT_OLDBOOT
     50  1.1  perry #include <sys/disklabel.h>
     51  1.1  perry #endif
     52  1.1  perry 
     53  1.1  perry #include <lib/libsa/stand.h>
     54  1.1  perry #include "libi386.h"
     55  1.1  perry 
     56  1.1  perry #ifdef COMPAT_OLDBOOT
     57  1.1  perry static int
     58  1.1  perry dev2major(devname, major)
     59  1.1  perry char *devname;
     60  1.1  perry int *major;
     61  1.1  perry {
     62  1.1  perry     static char *devices[] = {"wd", "", "fd", "", "sd"};
     63  1.1  perry #define NUMDEVICES (sizeof(devices)/sizeof(char *))
     64  1.1  perry     int i;
     65  1.1  perry 
     66  1.1  perry     for(i = 0; i < NUMDEVICES; i++)
     67  1.1  perry 	if(!strcmp(devname, devices[i])) {
     68  1.1  perry 	    *major = i;
     69  1.1  perry 	    return(0);
     70  1.1  perry 	}
     71  1.1  perry 	return(-1);
     72  1.1  perry }
     73  1.1  perry #endif
     74  1.1  perry 
     75  1.1  perry int exec_netbsd(file, loadaddr, boothowto, bootdev, consdev)
     76  1.1  perry const char	*file;
     77  1.1  perry physaddr_t	loadaddr;
     78  1.1  perry int	boothowto;
     79  1.1  perry char	*bootdev, *consdev; /* passed to kernel */
     80  1.1  perry {
     81  1.1  perry 	register int io;
     82  1.1  perry 	struct exec x;
     83  1.1  perry 	int cc, magic;
     84  1.1  perry 	physaddr_t entry;
     85  1.1  perry 	register physaddr_t cp;
     86  1.1  perry 	u_long boot_argv[6];
     87  1.1  perry 
     88  1.1  perry #ifdef COMPAT_OLDBOOT
     89  1.1  perry 	char *fsname, *devname;
     90  1.1  perry 	int unit, part;
     91  1.1  perry 	const char *filename;
     92  1.1  perry 	int bootdevnr;
     93  1.1  perry #else
     94  1.1  perry 	u_long xbootinfo[2];
     95  1.1  perry #endif
     96  1.1  perry 
     97  1.1  perry #ifdef	DEBUG
     98  1.1  perry 	printf("exec: file=%s loadaddr=0x%lx\n", file, loadaddr);
     99  1.1  perry #endif
    100  1.1  perry 	io = open(file, 0);
    101  1.1  perry 	if (io < 0)
    102  1.1  perry 		return(-1);
    103  1.1  perry 
    104  1.1  perry 	/*
    105  1.1  perry 	 * Read in the exec header, and validate it.
    106  1.1  perry 	 */
    107  1.1  perry 	if (read(io, (char *)&x, sizeof(x)) != sizeof(x))
    108  1.1  perry 	  goto shread;
    109  1.1  perry 	magic = N_GETMAGIC(x);
    110  1.1  perry 
    111  1.1  perry 	if ((magic != ZMAGIC) || (N_GETMID(x) != MID_MACHINE)){
    112  1.1  perry #ifdef DEBUG
    113  1.1  perry 	  printf("invalid NetBSD kernel (%o/%ld)\n", magic, N_GETMID(x));
    114  1.1  perry #endif
    115  1.1  perry 	  errno = EFTYPE;
    116  1.1  perry 	  goto closeout;
    117  1.1  perry 	}
    118  1.1  perry 
    119  1.1  perry 	entry = x.a_entry & 0xffffff;
    120  1.1  perry 
    121  1.1  perry 	if(!loadaddr) loadaddr = (entry & 0x100000);
    122  1.1  perry 
    123  1.1  perry 	cp = loadaddr;
    124  1.1  perry 
    125  1.1  perry 	/*
    126  1.1  perry 	 * Leave a copy of the exec header before the text.
    127  1.1  perry 	 * The kernel may use this to verify that the
    128  1.1  perry 	 * symbols were loaded by this boot program.
    129  1.1  perry 	 */
    130  1.1  perry 	vpbcopy(&x, cp, sizeof(x));
    131  1.1  perry 	cp += sizeof(x);
    132  1.1  perry 	/*
    133  1.1  perry 	 * Read in the text segment.
    134  1.1  perry 	 */
    135  1.1  perry 
    136  1.1  perry 	printf("%ld", x.a_text);
    137  1.1  perry 
    138  1.1  perry 	if (pread(io, cp, x.a_text - sizeof(x)) != x.a_text - sizeof(x))
    139  1.1  perry 		goto shread;
    140  1.1  perry 	cp += x.a_text-sizeof(x);
    141  1.1  perry 
    142  1.1  perry 	/*
    143  1.1  perry 	 * Read in the data segment.
    144  1.1  perry 	 */
    145  1.1  perry 
    146  1.1  perry 	printf("+%ld", x.a_data);
    147  1.1  perry 
    148  1.1  perry 	if (pread(io, cp, x.a_data) != x.a_data)
    149  1.1  perry 		goto shread;
    150  1.1  perry 	cp += x.a_data;
    151  1.1  perry 
    152  1.1  perry 	/*
    153  1.1  perry 	 * Zero out the BSS section.
    154  1.1  perry 	 * (Kernel doesn't care, but do it anyway.)
    155  1.1  perry 	 */
    156  1.1  perry 
    157  1.1  perry 
    158  1.1  perry 	printf("+%ld", x.a_bss);
    159  1.1  perry 
    160  1.1  perry 	pbzero(cp, x.a_bss);
    161  1.1  perry 	cp += x.a_bss;
    162  1.1  perry 
    163  1.1  perry 	/*
    164  1.1  perry 	 * Read in the symbol table and strings.
    165  1.1  perry 	 * (Always set the symtab size word.)
    166  1.1  perry 	 */
    167  1.1  perry 	vpbcopy(&x.a_syms, cp, sizeof(x.a_syms));
    168  1.1  perry 	cp += sizeof(x.a_syms);
    169  1.1  perry 
    170  1.1  perry 	if (x.a_syms > 0) {
    171  1.1  perry 
    172  1.1  perry 		/* Symbol table and string table length word. */
    173  1.1  perry 
    174  1.1  perry 		printf("+[%ld", x.a_syms);
    175  1.1  perry 
    176  1.1  perry 		if (pread(io, cp, x.a_syms) != x.a_syms)
    177  1.1  perry 			goto shread;
    178  1.1  perry 		cp += x.a_syms;
    179  1.1  perry 
    180  1.1  perry 		read(io, &cc, sizeof(cc));
    181  1.1  perry 
    182  1.1  perry 		vpbcopy(&cc, cp, sizeof(cc));
    183  1.1  perry 		cp += sizeof(cc);
    184  1.1  perry 
    185  1.1  perry 		/* String table.  Length word includes itself. */
    186  1.1  perry 
    187  1.1  perry 		printf("+%d]", cc);
    188  1.1  perry 
    189  1.1  perry 		cc -= sizeof(int);
    190  1.1  perry 		if (cc <= 0)
    191  1.1  perry 			goto shread;
    192  1.1  perry 		if (pread(io, cp, cc) != cc)
    193  1.1  perry 			goto shread;
    194  1.1  perry 		cp += cc;
    195  1.1  perry 	}
    196  1.1  perry 	boot_argv[3] = (((u_int)cp + sizeof(int) - 1)) & (-sizeof(int));
    197  1.1  perry 
    198  1.1  perry 	printf("=0x%lx\n", cp - loadaddr);
    199  1.1  perry 
    200  1.1  perry 	boot_argv[0] = boothowto;
    201  1.1  perry 
    202  1.1  perry #ifdef COMPAT_OLDBOOT
    203  1.1  perry 	/* prepare boot device information for kernel */
    204  1.1  perry 	if(parsebootfile(file, &fsname, &devname, &unit, &part, &filename)
    205  1.1  perry 	   || strcmp(fsname, "ufs"))
    206  1.1  perry 	    bootdevnr = 0; /* XXX error out if parse error??? */
    207  1.1  perry 	else {
    208  1.1  perry 	    int major;
    209  1.1  perry 
    210  1.1  perry 	    if(!strcmp(devname, "hd")) {
    211  1.1  perry 		/* generic BIOS disk, have to guess type */
    212  1.1  perry 		struct open_file *f = &files[io]; /* XXX */
    213  1.1  perry 
    214  1.1  perry 		if(biosdisk_gettype(f) == DTYPE_SCSI)
    215  1.1  perry 		    devname = "sd";
    216  1.1  perry 		else
    217  1.1  perry 		    devname = "wd";
    218  1.1  perry 	    }
    219  1.1  perry 
    220  1.1  perry 	    if(dev2major(devname, &major))
    221  1.1  perry 		bootdevnr = 0; /* XXX error out??? */
    222  1.1  perry 	    else
    223  1.1  perry 		bootdevnr = MAKEBOOTDEV(major, 0, 0, unit, part);
    224  1.1  perry 	}
    225  1.1  perry 
    226  1.1  perry 	boot_argv[1] = bootdevnr;
    227  1.1  perry 	boot_argv[2] = 0; /* cyl offset, unused */
    228  1.1  perry #else /* XXX to be specified */
    229  1.1  perry 	xbootinfo[0] = vtophys(bootdev);
    230  1.1  perry 	xbootinfo[1] = vtophys(consdev);
    231  1.1  perry 	boot_argv[1] = 0;
    232  1.1  perry 	boot_argv[2] = vtophys(xbootinfo); /* XXX cyl offset */
    233  1.1  perry #endif
    234  1.1  perry /*
    235  1.1  perry 	boot_argv[3] = end (set above)
    236  1.1  perry */
    237  1.1  perry 	boot_argv[4] = getextmem();
    238  1.1  perry 	boot_argv[5] = getbasemem();
    239  1.1  perry 
    240  1.1  perry 	close(io);
    241  1.1  perry 
    242  1.1  perry #ifdef DEBUG
    243  1.1  perry 	printf("Start @ 0x%lx ...\n", entry);
    244  1.1  perry #endif
    245  1.1  perry 
    246  1.1  perry 	startprog(entry, 6, boot_argv, 0x90000);
    247  1.1  perry 	panic("exec returned");
    248  1.1  perry 
    249  1.1  perry shread:
    250  1.1  perry 	printf("exec: short read\n");
    251  1.1  perry 	errno = EIO;
    252  1.1  perry closeout:
    253  1.1  perry 	close(io);
    254  1.1  perry 	return(-1);
    255  1.1  perry }
    256