exec.c revision 1.3 1 1.3 thorpej /* $NetBSD: exec.c,v 1.3 1997/03/22 04:15:51 thorpej Exp $ */
2 1.1 perry
3 1.1 perry /*
4 1.1 perry * Copyright (c) 1982, 1986, 1990, 1993
5 1.1 perry * The Regents of the University of California. All rights reserved.
6 1.1 perry * Copyright (c) 1996
7 1.1 perry * Matthias Drochner. All rights reserved.
8 1.1 perry * Copyright (c) 1996
9 1.1 perry * Perry E. Metzger. All rights reserved.
10 1.1 perry *
11 1.1 perry * Redistribution and use in source and binary forms, with or without
12 1.1 perry * modification, are permitted provided that the following conditions
13 1.1 perry * are met:
14 1.1 perry * 1. Redistributions of source code must retain the above copyright
15 1.1 perry * notice, this list of conditions and the following disclaimer.
16 1.1 perry * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 perry * notice, this list of conditions and the following disclaimer in the
18 1.1 perry * documentation and/or other materials provided with the distribution.
19 1.1 perry * 3. All advertising materials mentioning features or use of this software
20 1.1 perry * must display the following acknowledgement:
21 1.1 perry * This product includes software developed by the University of
22 1.1 perry * California, Berkeley and its contributors.
23 1.1 perry * 4. Neither the name of the University nor the names of its contributors
24 1.1 perry * may be used to endorse or promote products derived from this software
25 1.1 perry * without specific prior written permission.
26 1.1 perry *
27 1.1 perry * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 1.1 perry * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 1.1 perry * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 1.1 perry * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 1.1 perry * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 1.1 perry * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 1.1 perry * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 1.1 perry * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 1.1 perry * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 1.1 perry * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 1.1 perry * SUCH DAMAGE.
38 1.1 perry *
39 1.1 perry * @(#)boot.c 8.1 (Berkeley) 6/10/93
40 1.1 perry */
41 1.1 perry
42 1.2 thorpej /*
43 1.2 thorpej * starts NetBSD a.out kernel needs lowlevel startup from startprog.S
44 1.1 perry */
45 1.1 perry
46 1.1 perry #include <sys/param.h>
47 1.1 perry #include <sys/exec_aout.h>
48 1.1 perry #include <sys/reboot.h>
49 1.1 perry #ifdef COMPAT_OLDBOOT
50 1.1 perry #include <sys/disklabel.h>
51 1.1 perry #endif
52 1.1 perry
53 1.1 perry #include <lib/libsa/stand.h>
54 1.1 perry #include "libi386.h"
55 1.1 perry
56 1.1 perry #ifdef COMPAT_OLDBOOT
57 1.1 perry static int
58 1.1 perry dev2major(devname, major)
59 1.2 thorpej char *devname;
60 1.2 thorpej int *major;
61 1.1 perry {
62 1.2 thorpej static char *devices[] = {"wd", "", "fd", "", "sd"};
63 1.1 perry #define NUMDEVICES (sizeof(devices)/sizeof(char *))
64 1.2 thorpej int i;
65 1.1 perry
66 1.2 thorpej for (i = 0; i < NUMDEVICES; i++)
67 1.2 thorpej if (!strcmp(devname, devices[i])) {
68 1.2 thorpej *major = i;
69 1.2 thorpej return (0);
70 1.2 thorpej }
71 1.2 thorpej return (-1);
72 1.1 perry }
73 1.1 perry #endif
74 1.1 perry
75 1.2 thorpej int
76 1.2 thorpej exec_netbsd(file, loadaddr, boothowto, bootdev, consdev)
77 1.2 thorpej const char *file;
78 1.2 thorpej physaddr_t loadaddr;
79 1.2 thorpej int boothowto;
80 1.2 thorpej char *bootdev, *consdev; /* passed to kernel */
81 1.1 perry {
82 1.2 thorpej register int io;
83 1.2 thorpej struct exec x;
84 1.2 thorpej int cc, magic;
85 1.2 thorpej physaddr_t entry;
86 1.1 perry register physaddr_t cp;
87 1.2 thorpej u_long boot_argv[6];
88 1.1 perry
89 1.1 perry #ifdef COMPAT_OLDBOOT
90 1.2 thorpej char *fsname, *devname;
91 1.2 thorpej int unit, part;
92 1.2 thorpej const char *filename;
93 1.2 thorpej int bootdevnr;
94 1.1 perry #else
95 1.2 thorpej u_long xbootinfo[2];
96 1.1 perry #endif
97 1.1 perry
98 1.1 perry #ifdef DEBUG
99 1.1 perry printf("exec: file=%s loadaddr=0x%lx\n", file, loadaddr);
100 1.1 perry #endif
101 1.1 perry io = open(file, 0);
102 1.1 perry if (io < 0)
103 1.2 thorpej return (-1);
104 1.1 perry
105 1.1 perry /*
106 1.1 perry * Read in the exec header, and validate it.
107 1.1 perry */
108 1.2 thorpej if (read(io, (char *) &x, sizeof(x)) != sizeof(x))
109 1.2 thorpej goto shread;
110 1.1 perry magic = N_GETMAGIC(x);
111 1.1 perry
112 1.2 thorpej if ((magic != ZMAGIC) || (N_GETMID(x) != MID_MACHINE)) {
113 1.1 perry #ifdef DEBUG
114 1.2 thorpej printf("invalid NetBSD kernel (%o/%ld)\n", magic, N_GETMID(x));
115 1.1 perry #endif
116 1.2 thorpej errno = EFTYPE;
117 1.2 thorpej goto closeout;
118 1.1 perry }
119 1.1 perry entry = x.a_entry & 0xffffff;
120 1.1 perry
121 1.2 thorpej if (!loadaddr)
122 1.2 thorpej loadaddr = (entry & 0x100000);
123 1.1 perry
124 1.1 perry cp = loadaddr;
125 1.1 perry
126 1.1 perry /*
127 1.1 perry * Leave a copy of the exec header before the text.
128 1.1 perry * The kernel may use this to verify that the
129 1.1 perry * symbols were loaded by this boot program.
130 1.1 perry */
131 1.1 perry vpbcopy(&x, cp, sizeof(x));
132 1.1 perry cp += sizeof(x);
133 1.1 perry /*
134 1.1 perry * Read in the text segment.
135 1.1 perry */
136 1.1 perry
137 1.1 perry printf("%ld", x.a_text);
138 1.1 perry
139 1.1 perry if (pread(io, cp, x.a_text - sizeof(x)) != x.a_text - sizeof(x))
140 1.1 perry goto shread;
141 1.2 thorpej cp += x.a_text - sizeof(x);
142 1.1 perry
143 1.1 perry /*
144 1.1 perry * Read in the data segment.
145 1.1 perry */
146 1.1 perry
147 1.1 perry printf("+%ld", x.a_data);
148 1.1 perry
149 1.1 perry if (pread(io, cp, x.a_data) != x.a_data)
150 1.1 perry goto shread;
151 1.1 perry cp += x.a_data;
152 1.1 perry
153 1.1 perry /*
154 1.1 perry * Zero out the BSS section.
155 1.1 perry * (Kernel doesn't care, but do it anyway.)
156 1.1 perry */
157 1.1 perry
158 1.1 perry
159 1.1 perry printf("+%ld", x.a_bss);
160 1.1 perry
161 1.1 perry pbzero(cp, x.a_bss);
162 1.1 perry cp += x.a_bss;
163 1.1 perry
164 1.1 perry /*
165 1.1 perry * Read in the symbol table and strings.
166 1.1 perry * (Always set the symtab size word.)
167 1.1 perry */
168 1.1 perry vpbcopy(&x.a_syms, cp, sizeof(x.a_syms));
169 1.1 perry cp += sizeof(x.a_syms);
170 1.1 perry
171 1.1 perry if (x.a_syms > 0) {
172 1.1 perry
173 1.1 perry /* Symbol table and string table length word. */
174 1.1 perry
175 1.1 perry printf("+[%ld", x.a_syms);
176 1.1 perry
177 1.1 perry if (pread(io, cp, x.a_syms) != x.a_syms)
178 1.1 perry goto shread;
179 1.1 perry cp += x.a_syms;
180 1.1 perry
181 1.1 perry read(io, &cc, sizeof(cc));
182 1.1 perry
183 1.1 perry vpbcopy(&cc, cp, sizeof(cc));
184 1.1 perry cp += sizeof(cc);
185 1.1 perry
186 1.1 perry /* String table. Length word includes itself. */
187 1.1 perry
188 1.1 perry printf("+%d]", cc);
189 1.1 perry
190 1.1 perry cc -= sizeof(int);
191 1.1 perry if (cc <= 0)
192 1.1 perry goto shread;
193 1.1 perry if (pread(io, cp, cc) != cc)
194 1.1 perry goto shread;
195 1.1 perry cp += cc;
196 1.1 perry }
197 1.2 thorpej boot_argv[3] = (((u_int) cp + sizeof(int) - 1)) & (-sizeof(int));
198 1.1 perry
199 1.1 perry printf("=0x%lx\n", cp - loadaddr);
200 1.1 perry
201 1.1 perry boot_argv[0] = boothowto;
202 1.1 perry
203 1.1 perry #ifdef COMPAT_OLDBOOT
204 1.1 perry /* prepare boot device information for kernel */
205 1.2 thorpej if (parsebootfile(file, &fsname, &devname, &unit, &part, &filename)
206 1.2 thorpej || strcmp(fsname, "ufs"))
207 1.2 thorpej bootdevnr = 0; /* XXX error out if parse error??? */
208 1.1 perry else {
209 1.2 thorpej int major;
210 1.1 perry
211 1.3 thorpej if (strcmp(devname, "hd") == 0) {
212 1.2 thorpej /* generic BIOS disk, have to guess type */
213 1.2 thorpej struct open_file *f = &files[io]; /* XXX */
214 1.2 thorpej
215 1.2 thorpej if (biosdisk_gettype(f) == DTYPE_SCSI)
216 1.2 thorpej devname = "sd";
217 1.2 thorpej else
218 1.2 thorpej devname = "wd";
219 1.3 thorpej
220 1.3 thorpej /*
221 1.3 thorpej * The old boot block performed the following
222 1.3 thorpej * conversion:
223 1.3 thorpej *
224 1.3 thorpej * hdN -> Xd0
225 1.3 thorpej *
226 1.3 thorpej * where X is the type specified by the label.
227 1.3 thorpej * We mimmick that here, for lack of any better
228 1.3 thorpej * way of doing things.
229 1.3 thorpej */
230 1.3 thorpej unit = 0;
231 1.2 thorpej }
232 1.3 thorpej
233 1.2 thorpej if (dev2major(devname, &major))
234 1.2 thorpej bootdevnr = 0; /* XXX error out??? */
235 1.1 perry else
236 1.2 thorpej bootdevnr = MAKEBOOTDEV(major, 0, 0, unit, part);
237 1.1 perry }
238 1.1 perry
239 1.1 perry boot_argv[1] = bootdevnr;
240 1.2 thorpej boot_argv[2] = 0; /* cyl offset, unused */
241 1.2 thorpej #else /* XXX to be specified */
242 1.1 perry xbootinfo[0] = vtophys(bootdev);
243 1.1 perry xbootinfo[1] = vtophys(consdev);
244 1.1 perry boot_argv[1] = 0;
245 1.2 thorpej boot_argv[2] = vtophys(xbootinfo); /* XXX cyl offset */
246 1.1 perry #endif
247 1.2 thorpej /*
248 1.2 thorpej * boot_argv[3] = end (set above)
249 1.2 thorpej */
250 1.1 perry boot_argv[4] = getextmem();
251 1.1 perry boot_argv[5] = getbasemem();
252 1.1 perry
253 1.1 perry close(io);
254 1.1 perry
255 1.1 perry #ifdef DEBUG
256 1.1 perry printf("Start @ 0x%lx ...\n", entry);
257 1.1 perry #endif
258 1.1 perry
259 1.1 perry startprog(entry, 6, boot_argv, 0x90000);
260 1.1 perry panic("exec returned");
261 1.1 perry
262 1.1 perry shread:
263 1.1 perry printf("exec: short read\n");
264 1.1 perry errno = EIO;
265 1.1 perry closeout:
266 1.1 perry close(io);
267 1.2 thorpej return (-1);
268 1.1 perry }
269