exec_mvme.c revision 1.1 1 /* $NetBSD: exec_mvme.c,v 1.1 1996/05/17 21:00:02 chuck 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 register int io;
54 struct exec x;
55 int cc, magic;
56 void (*entry)();
57 register char *cp;
58 register 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, (char *)&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 = N_GETMAGIC(x);
93 if (magic == ZMAGIC)
94 cp += sizeof(x);
95 entry = (void (*)())cp;
96
97 /*
98 * Leave a copy of the exec header before the text.
99 * The sun3 kernel uses this to verify that the
100 * symbols were loaded by this boot program.
101 */
102 bcopy(&x, cp - sizeof(x), sizeof(x));
103
104 /*
105 * Read in the text segment.
106 */
107 printf("%d", x.a_text);
108 cc = x.a_text;
109 if (magic == ZMAGIC)
110 cc = cc - sizeof(x); /* a.out header part of text in zmagic */
111 if (read(io, cp, cc) != cc)
112 goto shread;
113 cp += cc;
114
115 /*
116 * NMAGIC may have a gap between text and data.
117 */
118 if (magic == NMAGIC) {
119 register int mask = N_PAGSIZ(x) - 1;
120 while ((int)cp & mask)
121 *cp++ = 0;
122 }
123
124 /*
125 * Read in the data segment.
126 */
127 printf("+%d", x.a_data);
128 if (read(io, cp, x.a_data) != x.a_data)
129 goto shread;
130 cp += x.a_data;
131
132 /*
133 * Zero out the BSS section.
134 * (Kernel doesn't care, but do it anyway.)
135 */
136 printf("+%d", x.a_bss);
137 cc = x.a_bss;
138 while ((int)cp & 3) {
139 *cp++ = 0;
140 --cc;
141 }
142 ip = (int*)cp;
143 cp += cc;
144 while ((char*)ip < cp)
145 *ip++ = 0;
146
147 /*
148 * Read in the symbol table and strings.
149 * (Always set the symtab size word.)
150 */
151 *ip++ = x.a_syms;
152 cp = (char*) ip;
153
154 if (x.a_syms > 0 && (flag & RB_NOSYM) == 0) {
155
156 /* Symbol table and string table length word. */
157 cc = x.a_syms;
158 printf("+[%d", cc);
159 cc += sizeof(int); /* strtab length too */
160 if (read(io, cp, cc) != cc)
161 goto shread;
162 cp += x.a_syms;
163 ip = (int*)cp; /* points to strtab length */
164 cp += sizeof(int);
165
166 /* String table. Length word includes itself. */
167 cc = *ip;
168 printf("+%d]", cc);
169 cc -= sizeof(int);
170 if (cc <= 0)
171 goto shread;
172 if (read(io, cp, cc) != cc)
173 goto shread;
174 cp += cc;
175 }
176 printf("=0x%x\n", cp - loadaddr);
177 close(io);
178
179 printf("Start @ 0x%x ...\n", (int)entry);
180 (*entry)(flag, 0, cp, 0, 0);
181 printf("exec: kernel returned!\n");
182 return;
183
184 shread:
185 printf("exec: short read\n");
186 errno = EIO;
187 closeout:
188 close(io);
189 return;
190 }
191