Home | History | Annotate | Line # | Download | only in lib
exec.c revision 1.3.4.1
      1  1.3.4.1  thorpej /*	$NetBSD: exec.c,v 1.3.4.1 1997/09/22 06:31:30 thorpej 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.2  thorpej /*
     43      1.2  thorpej  * starts NetBSD a.out kernel 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.3.4.1  thorpej 
     55      1.1    perry #include "libi386.h"
     56  1.3.4.1  thorpej #include "bootinfo.h"
     57      1.1    perry 
     58      1.1    perry #ifdef COMPAT_OLDBOOT
     59      1.1    perry static int
     60      1.1    perry dev2major(devname, major)
     61      1.2  thorpej 	char           *devname;
     62      1.2  thorpej 	int            *major;
     63      1.1    perry {
     64      1.2  thorpej 	static char    *devices[] = {"wd", "", "fd", "", "sd"};
     65      1.1    perry #define NUMDEVICES (sizeof(devices)/sizeof(char *))
     66      1.2  thorpej 	int             i;
     67      1.1    perry 
     68      1.2  thorpej 	for (i = 0; i < NUMDEVICES; i++)
     69      1.2  thorpej 		if (!strcmp(devname, devices[i])) {
     70      1.2  thorpej 			*major = i;
     71      1.2  thorpej 			return (0);
     72      1.2  thorpej 		}
     73      1.2  thorpej 	return (-1);
     74      1.1    perry }
     75      1.1    perry #endif
     76      1.1    perry 
     77  1.3.4.1  thorpej extern struct btinfo_console btinfo_console;
     78  1.3.4.1  thorpej 
     79      1.2  thorpej int
     80  1.3.4.1  thorpej exec_netbsd(file, loadaddr, boothowto)
     81      1.2  thorpej 	const char     *file;
     82      1.2  thorpej 	physaddr_t      loadaddr;
     83      1.2  thorpej 	int             boothowto;
     84      1.1    perry {
     85      1.2  thorpej 	register int    io;
     86      1.2  thorpej 	struct exec     x;
     87      1.2  thorpej 	int             cc, magic;
     88      1.2  thorpej 	physaddr_t      entry;
     89      1.1    perry 	register physaddr_t cp;
     90      1.2  thorpej 	u_long          boot_argv[6];
     91      1.1    perry 
     92      1.1    perry #ifdef COMPAT_OLDBOOT
     93      1.2  thorpej 	char           *fsname, *devname;
     94      1.2  thorpej 	int             unit, part;
     95      1.2  thorpej 	const char     *filename;
     96      1.2  thorpej 	int             bootdevnr;
     97      1.1    perry #endif
     98      1.1    perry 
     99      1.1    perry #ifdef	DEBUG
    100      1.1    perry 	printf("exec: file=%s loadaddr=0x%lx\n", file, loadaddr);
    101      1.1    perry #endif
    102  1.3.4.1  thorpej 
    103  1.3.4.1  thorpej 	BI_ALLOC(5); /* ??? */
    104  1.3.4.1  thorpej 
    105  1.3.4.1  thorpej 	BI_ADD(&btinfo_console, BTINFO_CONSOLE, sizeof(struct btinfo_console));
    106  1.3.4.1  thorpej 
    107      1.1    perry 	io = open(file, 0);
    108      1.1    perry 	if (io < 0)
    109  1.3.4.1  thorpej 		goto out;
    110      1.1    perry 
    111      1.1    perry 	/*
    112      1.1    perry 	 * Read in the exec header, and validate it.
    113      1.1    perry 	 */
    114      1.2  thorpej 	if (read(io, (char *) &x, sizeof(x)) != sizeof(x))
    115      1.2  thorpej 		goto shread;
    116      1.1    perry 	magic = N_GETMAGIC(x);
    117      1.1    perry 
    118      1.2  thorpej 	if ((magic != ZMAGIC) || (N_GETMID(x) != MID_MACHINE)) {
    119      1.1    perry #ifdef DEBUG
    120  1.3.4.1  thorpej 		printf("invalid NetBSD kernel (%o/%d)\n", magic, N_GETMID(x));
    121      1.1    perry #endif
    122      1.2  thorpej 		errno = EFTYPE;
    123      1.2  thorpej 		goto closeout;
    124      1.1    perry 	}
    125      1.1    perry 	entry = x.a_entry & 0xffffff;
    126      1.1    perry 
    127      1.2  thorpej 	if (!loadaddr)
    128      1.2  thorpej 		loadaddr = (entry & 0x100000);
    129      1.1    perry 
    130      1.1    perry 	cp = loadaddr;
    131      1.1    perry 
    132      1.1    perry 	/*
    133      1.1    perry 	 * Leave a copy of the exec header before the text.
    134      1.1    perry 	 * The kernel may use this to verify that the
    135      1.1    perry 	 * symbols were loaded by this boot program.
    136      1.1    perry 	 */
    137      1.1    perry 	vpbcopy(&x, cp, sizeof(x));
    138      1.1    perry 	cp += sizeof(x);
    139      1.1    perry 	/*
    140      1.1    perry 	 * Read in the text segment.
    141      1.1    perry 	 */
    142      1.1    perry 
    143      1.1    perry 	printf("%ld", x.a_text);
    144      1.1    perry 
    145      1.1    perry 	if (pread(io, cp, x.a_text - sizeof(x)) != x.a_text - sizeof(x))
    146      1.1    perry 		goto shread;
    147      1.2  thorpej 	cp += x.a_text - sizeof(x);
    148      1.1    perry 
    149      1.1    perry 	/*
    150      1.1    perry 	 * Read in the data segment.
    151      1.1    perry 	 */
    152      1.1    perry 
    153      1.1    perry 	printf("+%ld", x.a_data);
    154      1.1    perry 
    155      1.1    perry 	if (pread(io, cp, x.a_data) != x.a_data)
    156      1.1    perry 		goto shread;
    157      1.1    perry 	cp += x.a_data;
    158      1.1    perry 
    159      1.1    perry 	/*
    160      1.1    perry 	 * Zero out the BSS section.
    161      1.1    perry 	 * (Kernel doesn't care, but do it anyway.)
    162      1.1    perry 	 */
    163      1.1    perry 
    164      1.1    perry 
    165      1.1    perry 	printf("+%ld", x.a_bss);
    166      1.1    perry 
    167      1.1    perry 	pbzero(cp, x.a_bss);
    168      1.1    perry 	cp += x.a_bss;
    169      1.1    perry 
    170      1.1    perry 	/*
    171      1.1    perry 	 * Read in the symbol table and strings.
    172      1.1    perry 	 * (Always set the symtab size word.)
    173      1.1    perry 	 */
    174      1.1    perry 	vpbcopy(&x.a_syms, cp, sizeof(x.a_syms));
    175      1.1    perry 	cp += sizeof(x.a_syms);
    176      1.1    perry 
    177      1.1    perry 	if (x.a_syms > 0) {
    178      1.1    perry 
    179      1.1    perry 		/* Symbol table and string table length word. */
    180      1.1    perry 
    181      1.1    perry 		printf("+[%ld", x.a_syms);
    182      1.1    perry 
    183      1.1    perry 		if (pread(io, cp, x.a_syms) != x.a_syms)
    184      1.1    perry 			goto shread;
    185      1.1    perry 		cp += x.a_syms;
    186      1.1    perry 
    187      1.1    perry 		read(io, &cc, sizeof(cc));
    188      1.1    perry 
    189      1.1    perry 		vpbcopy(&cc, cp, sizeof(cc));
    190      1.1    perry 		cp += sizeof(cc);
    191      1.1    perry 
    192      1.1    perry 		/* String table.  Length word includes itself. */
    193      1.1    perry 
    194      1.1    perry 		printf("+%d]", cc);
    195      1.1    perry 
    196      1.1    perry 		cc -= sizeof(int);
    197      1.1    perry 		if (cc <= 0)
    198      1.1    perry 			goto shread;
    199      1.1    perry 		if (pread(io, cp, cc) != cc)
    200      1.1    perry 			goto shread;
    201      1.1    perry 		cp += cc;
    202      1.1    perry 	}
    203      1.2  thorpej 	boot_argv[3] = (((u_int) cp + sizeof(int) - 1)) & (-sizeof(int));
    204      1.1    perry 
    205      1.1    perry 	printf("=0x%lx\n", cp - loadaddr);
    206      1.1    perry 
    207      1.1    perry 	boot_argv[0] = boothowto;
    208      1.1    perry 
    209      1.1    perry #ifdef COMPAT_OLDBOOT
    210      1.1    perry 	/* prepare boot device information for kernel */
    211      1.2  thorpej 	if (parsebootfile(file, &fsname, &devname, &unit, &part, &filename)
    212      1.2  thorpej 	    || strcmp(fsname, "ufs"))
    213      1.2  thorpej 		bootdevnr = 0;	/* XXX error out if parse error??? */
    214      1.1    perry 	else {
    215      1.2  thorpej 		int             major;
    216      1.1    perry 
    217      1.3  thorpej 		if (strcmp(devname, "hd") == 0) {
    218      1.2  thorpej 			/* generic BIOS disk, have to guess type */
    219      1.2  thorpej 			struct open_file *f = &files[io];	/* XXX */
    220      1.2  thorpej 
    221      1.2  thorpej 			if (biosdisk_gettype(f) == DTYPE_SCSI)
    222      1.2  thorpej 				devname = "sd";
    223      1.2  thorpej 			else
    224      1.2  thorpej 				devname = "wd";
    225      1.3  thorpej 
    226      1.3  thorpej 			/*
    227      1.3  thorpej 			 * The old boot block performed the following
    228      1.3  thorpej 			 * conversion:
    229      1.3  thorpej 			 *
    230      1.3  thorpej 			 *	hdN -> Xd0
    231      1.3  thorpej 			 *
    232      1.3  thorpej 			 * where X is the type specified by the label.
    233      1.3  thorpej 			 * We mimmick that here, for lack of any better
    234      1.3  thorpej 			 * way of doing things.
    235      1.3  thorpej 			 */
    236      1.3  thorpej 			unit = 0;
    237      1.2  thorpej 		}
    238      1.3  thorpej 
    239      1.2  thorpej 		if (dev2major(devname, &major))
    240      1.2  thorpej 			bootdevnr = 0;	/* XXX error out??? */
    241      1.1    perry 		else
    242      1.2  thorpej 			bootdevnr = MAKEBOOTDEV(major, 0, 0, unit, part);
    243      1.1    perry 	}
    244      1.1    perry 
    245      1.1    perry 	boot_argv[1] = bootdevnr;
    246      1.2  thorpej 	boot_argv[2] = 0;	/* cyl offset, unused */
    247      1.2  thorpej #else				/* XXX to be specified */
    248  1.3.4.1  thorpej #ifdef PASS_BIOSGEOM
    249  1.3.4.1  thorpej 	bi_getbiosgeom();
    250  1.3.4.1  thorpej #endif
    251      1.1    perry 	boot_argv[1] = 0;
    252  1.3.4.1  thorpej 	boot_argv[2] = vtophys(bootinfo);	/* cyl offset */
    253      1.1    perry #endif
    254      1.2  thorpej 	/*
    255      1.2  thorpej 	 * boot_argv[3] = end (set above)
    256      1.2  thorpej 	 */
    257      1.1    perry 	boot_argv[4] = getextmem();
    258      1.1    perry 	boot_argv[5] = getbasemem();
    259      1.1    perry 
    260      1.1    perry 	close(io);
    261      1.1    perry 
    262      1.1    perry #ifdef DEBUG
    263      1.1    perry 	printf("Start @ 0x%lx ...\n", entry);
    264      1.1    perry #endif
    265      1.1    perry 
    266      1.1    perry 	startprog(entry, 6, boot_argv, 0x90000);
    267      1.1    perry 	panic("exec returned");
    268      1.1    perry 
    269      1.1    perry shread:
    270      1.1    perry 	printf("exec: short read\n");
    271      1.1    perry 	errno = EIO;
    272      1.1    perry closeout:
    273      1.1    perry 	close(io);
    274  1.3.4.1  thorpej out:
    275  1.3.4.1  thorpej 	BI_FREE();
    276  1.3.4.1  thorpej 	bootinfo = 0;
    277      1.2  thorpej 	return (-1);
    278      1.1    perry }
    279