Home | History | Annotate | Line # | Download | only in libsa
exec_mvme.c revision 1.9
      1 /*	$NetBSD: exec_mvme.c,v 1.9 2000/07/24 18:39:46 jdolecek 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 <sys/exec_aout.h>
     42 
     43 #include "stand.h"
     44 #include "libsa.h"
     45 
     46 /* This must agree with what locore.s expects */
     47 typedef void (*kentry_t)(int, u_int, u_int, u_int, int, char *);
     48 
     49 
     50 /*ARGSUSED*/
     51 void
     52 exec_mvme(file, flag, part)
     53 	char	*file;
     54 	int	flag;
     55 	int	part;
     56 {
     57 	char *loadaddr;
     58 	int io;
     59 	struct exec x;
     60 	int cc, magic;
     61 	kentry_t *entry;
     62 	char *cp;
     63 	int *ip;
     64 
     65 #ifdef	DEBUG
     66 	printf("exec_mvme: partition=%d, file=%s flag=0x%x\n", part, file, flag);
     67 #endif
     68 
     69 	io = open(file, 0);
     70 	if (io < 0)
     71 		return;
     72 
     73 	/*
     74 	 * Read in the exec header, and validate it.
     75 	 */
     76 	if (read(io, (void *)&x, sizeof(x)) != sizeof(x))
     77 		goto shread;
     78 	if (N_BADMAG(x)) {
     79 		errno = EFTYPE;
     80 		goto closeout;
     81 	}
     82 
     83 	/*
     84 	 * note: on the mvme ports, the kernel is linked in such a way that
     85 	 * its entry point is the first item in .text, and thus a_entry can
     86 	 * be used to determine both the load address and the entry point.
     87 	 * (also note that we make use of the fact that the kernel will live
     88 	 *  in a VA == PA range of memory ... otherwise we would take
     89 	 *  loadaddr as a parameter and let the kernel relocate itself!)
     90 	 *
     91 	 * note that ZMAGIC files included the a.out header in the text area
     92 	 * so we must mask that off (has no effect on the other formats
     93 	 */
     94 	loadaddr = (void *)(x.a_entry & ~sizeof(x));
     95 
     96 	cp = loadaddr;
     97 	magic = (int)N_GETMAGIC(x);
     98 	if (magic == ZMAGIC)
     99 		cp += sizeof(x);
    100 	/*LINTED*/
    101 	entry = (kentry_t *) cp;
    102 
    103 	/*
    104 	 * Leave a copy of the exec header before the text.
    105 	 * The sun3 kernel uses this to verify that the
    106 	 * symbols were loaded by this boot program.
    107 	 */
    108 	bcopy(&x, cp - sizeof(x), sizeof(x));
    109 
    110 	/*
    111 	 * Read in the text segment.
    112 	 */
    113 	printf("%ld", x.a_text);
    114 	cc = (int)x.a_text;
    115 	if (magic == ZMAGIC)
    116 		cc = cc - sizeof(x); /* a.out header part of text in zmagic */
    117 	if (read(io, cp, (size_t)cc) != (size_t)cc)
    118 		goto shread;
    119 	cp += cc;
    120 
    121 	/*
    122 	 * NMAGIC may have a gap between text and data.
    123 	 */
    124 	if (magic == NMAGIC) {
    125 		int mask = N_PAGSIZ(x) - 1;
    126 		/*LINTED*/
    127 		while ((int)cp & mask)
    128 			*cp++ = 0;
    129 	}
    130 
    131 	/*
    132 	 * Read in the data segment.
    133 	 */
    134 	printf("+%ld", x.a_data);
    135 	if (read(io, cp, (size_t)x.a_data) != (size_t)x.a_data)
    136 		goto shread;
    137 	cp += (int)x.a_data;
    138 
    139 	/*
    140 	 * Zero out the BSS section.
    141 	 * (Kernel doesn't care, but do it anyway.)
    142 	 */
    143 	printf("+%ld", x.a_bss);
    144 	cc = (int)x.a_bss;
    145 	/*LINTED*/
    146 	while ((int)cp & 3) {
    147 		*cp++ = 0;
    148 		--cc;
    149 	}
    150 	/*LINTED*/
    151 	ip = (int*)cp;
    152 	cp += cc;
    153 	/*LINTED*/
    154 	while ((char*)ip < cp)
    155 		*ip++ = 0;
    156 
    157 	/*
    158 	 * Read in the symbol table and strings.
    159 	 * (Always set the symtab size word.)
    160 	 */
    161 	*ip++ = (int)x.a_syms;
    162 	/*LINTED*/
    163 	cp = (char*) ip;
    164 
    165 	if (x.a_syms > 0 && (flag & RB_NOSYM) == 0) {
    166 
    167 		/* Symbol table and string table length word. */
    168 		cc = (int)x.a_syms;
    169 		printf("+[%d", cc);
    170 		cc += sizeof(int);	/* strtab length too */
    171 		if (read(io, cp, (size_t)cc) != (size_t)cc)
    172 			goto shread;
    173 		cp += (int)x.a_syms;
    174 		/*LINTED*/
    175 		ip = (int*)cp;  	/* points to strtab length */
    176 		cp += sizeof(int);
    177 
    178 		/* String table.  Length word includes itself. */
    179 		cc = *ip;
    180 		printf("+%d]", cc);
    181 		cc -= sizeof(int);
    182 		if (cc <= 0)
    183 			goto shread;
    184 		if (read(io, cp, (size_t)cc) != (size_t)cc)
    185 			goto shread;
    186 		cp += cc;
    187 	}
    188 	printf("=0x%x\n", cp - loadaddr);
    189 	close(io);
    190 
    191 	printf("Start @ 0x%p ...\n", entry);
    192 	(*entry)(flag, bugargs.ctrl_addr,
    193 				bugargs.ctrl_lun, bugargs.dev_lun, part, cp);
    194 	printf("exec: kernel returned!\n");
    195 	return;
    196 
    197 shread:
    198 	printf("exec: short read\n");
    199 	errno = EIO;
    200 closeout:
    201 	close(io);
    202 	return;
    203 }
    204