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