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