Home | History | Annotate | Line # | Download | only in libsa
exec_mvme.c revision 1.5
      1 /*	$NetBSD: exec_mvme.c,v 1.5 1998/08/22 10:55:36 scw Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 1982, 1986, 1990, 1993
      5  *	The Regents of the University of California.  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  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  *
     35  * 	@(#)boot.c	8.1 (Berkeley) 6/10/93
     36  */
     37 
     38 #include <sys/param.h>
     39 #include <sys/reboot.h>
     40 #include <machine/prom.h>
     41 #include <a.out.h>
     42 
     43 #include "stand.h"
     44 #include "libsa.h"
     45 
     46 /*ARGSUSED*/
     47 void
     48 exec_mvme(file, flag)
     49 	char	*file;
     50 	int	flag;
     51 {
     52 	char *loadaddr;
     53 	int io;
     54 	struct exec x;
     55 	int cc, magic;
     56 	void (*entry)();
     57 	char *cp;
     58 	int *ip;
     59 
     60 #ifdef	DEBUG
     61 	printf("exec_mvme: file=%s flag=0x%x\n", file, flag);
     62 #endif
     63 
     64 	io = open(file, 0);
     65 	if (io < 0)
     66 		return;
     67 
     68 	/*
     69 	 * Read in the exec header, and validate it.
     70 	 */
     71 	if (read(io, (void *)&x, sizeof(x)) != sizeof(x))
     72 		goto shread;
     73 	if (N_BADMAG(x)) {
     74 		errno = EFTYPE;
     75 		goto closeout;
     76 	}
     77 
     78 	/*
     79 	 * note: on the mvme ports, the kernel is linked in such a way that
     80 	 * its entry point is the first item in .text, and thus a_entry can
     81 	 * be used to determine both the load address and the entry point.
     82 	 * (also note that we make use of the fact that the kernel will live
     83 	 *  in a VA == PA range of memory ... otherwise we would take
     84 	 *  loadaddr as a parameter and let the kernel relocate itself!)
     85 	 *
     86 	 * note that ZMAGIC files included the a.out header in the text area
     87 	 * so we must mask that off (has no effect on the other formats
     88 	 */
     89 	loadaddr = (void *)(x.a_entry & ~sizeof(x));
     90 
     91 	cp = loadaddr;
     92 	magic = (int)N_GETMAGIC(x);
     93 	if (magic == ZMAGIC)
     94 		cp += sizeof(x);
     95 	/*LINTED*/
     96 	entry = (void (*)())cp;
     97 
     98 	/*
     99 	 * Leave a copy of the exec header before the text.
    100 	 * The sun3 kernel uses this to verify that the
    101 	 * symbols were loaded by this boot program.
    102 	 */
    103 	bcopy(&x, cp - sizeof(x), sizeof(x));
    104 
    105 	/*
    106 	 * Read in the text segment.
    107 	 */
    108 	printf("%d", x.a_text);
    109 	cc = (int)x.a_text;
    110 	if (magic == ZMAGIC)
    111 		cc = cc - sizeof(x); /* a.out header part of text in zmagic */
    112 	if (read(io, cp, (size_t)cc) != (size_t)cc)
    113 		goto shread;
    114 	cp += cc;
    115 
    116 	/*
    117 	 * NMAGIC may have a gap between text and data.
    118 	 */
    119 	if (magic == NMAGIC) {
    120 		int mask = N_PAGSIZ(x) - 1;
    121 		/*LINTED*/
    122 		while ((int)cp & mask)
    123 			*cp++ = 0;
    124 	}
    125 
    126 	/*
    127 	 * Read in the data segment.
    128 	 */
    129 	printf("+%d", x.a_data);
    130 	if (read(io, cp, (size_t)x.a_data) != (size_t)x.a_data)
    131 		goto shread;
    132 	cp += (int)x.a_data;
    133 
    134 	/*
    135 	 * Zero out the BSS section.
    136 	 * (Kernel doesn't care, but do it anyway.)
    137 	 */
    138 	printf("+%d", x.a_bss);
    139 	cc = (int)x.a_bss;
    140 	/*LINTED*/
    141 	while ((int)cp & 3) {
    142 		*cp++ = 0;
    143 		--cc;
    144 	}
    145 	/*LINTED*/
    146 	ip = (int*)cp;
    147 	cp += cc;
    148 	/*LINTED*/
    149 	while ((char*)ip < cp)
    150 		*ip++ = 0;
    151 
    152 	/*
    153 	 * Read in the symbol table and strings.
    154 	 * (Always set the symtab size word.)
    155 	 */
    156 	*ip++ = (int)x.a_syms;
    157 	/*LINTED*/
    158 	cp = (char*) ip;
    159 
    160 	if (x.a_syms > 0 && (flag & RB_NOSYM) == 0) {
    161 
    162 		/* Symbol table and string table length word. */
    163 		cc = (int)x.a_syms;
    164 		printf("+[%d", cc);
    165 		cc += sizeof(int);	/* strtab length too */
    166 		if (read(io, cp, (size_t)cc) != (size_t)cc)
    167 			goto shread;
    168 		cp += (int)x.a_syms;
    169 		/*LINTED*/
    170 		ip = (int*)cp;  	/* points to strtab length */
    171 		cp += sizeof(int);
    172 
    173 		/* String table.  Length word includes itself. */
    174 		cc = *ip;
    175 		printf("+%d]", cc);
    176 		cc -= sizeof(int);
    177 		if (cc <= 0)
    178 			goto shread;
    179 		if (read(io, cp, (size_t)cc) != (size_t)cc)
    180 			goto shread;
    181 		cp += cc;
    182 	}
    183 	printf("=0x%x\n", cp - loadaddr);
    184 	close(io);
    185 
    186 	printf("Start @ 0x%p ...\n", entry);
    187 	(*entry)(flag, bugargs.ctrl_addr,
    188 				bugargs.ctrl_lun, bugargs.dev_lun, 0, cp);
    189 	printf("exec: kernel returned!\n");
    190 	return;
    191 
    192 shread:
    193 	printf("exec: short read\n");
    194 	errno = EIO;
    195 closeout:
    196 	close(io);
    197 	return;
    198 }
    199