boot.c revision 1.12 1 /* $NetBSD: boot.c,v 1.12 2001/08/13 15:38:11 tsubai Exp $ */
2
3 /*-
4 * Copyright (c) 1997 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Copyright (C) 1995, 1996 Wolfgang Solfrank.
41 * Copyright (C) 1995, 1996 TooLs GmbH.
42 * All rights reserved.
43 *
44 * ELF support derived from NetBSD/alpha's boot loader, written
45 * by Christopher G. Demetriou.
46 *
47 * Redistribution and use in source and binary forms, with or without
48 * modification, are permitted provided that the following conditions
49 * are met:
50 * 1. Redistributions of source code must retain the above copyright
51 * notice, this list of conditions and the following disclaimer.
52 * 2. Redistributions in binary form must reproduce the above copyright
53 * notice, this list of conditions and the following disclaimer in the
54 * documentation and/or other materials provided with the distribution.
55 * 3. All advertising materials mentioning features or use of this software
56 * must display the following acknowledgement:
57 * This product includes software developed by TooLs GmbH.
58 * 4. The name of TooLs GmbH may not be used to endorse or promote products
59 * derived from this software without specific prior written permission.
60 *
61 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
62 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
63 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
64 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
65 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
66 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
67 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
68 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
69 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
70 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
71 */
72
73 /*
74 * First try for the boot code
75 *
76 * Input syntax is:
77 * [promdev[{:|,}partition]]/[filename] [flags]
78 */
79
80 #define ELFSIZE 32 /* We use 32-bit ELF. */
81
82 #include <sys/param.h>
83 #include <sys/exec.h>
84 #include <sys/exec_elf.h>
85 #include <sys/reboot.h>
86 #include <sys/disklabel.h>
87 #include <sys/boot_flag.h>
88
89 #include <lib/libsa/stand.h>
90 #include <lib/libsa/loadfile.h>
91 #include <lib/libkern/libkern.h>
92
93 #include <machine/cpu.h>
94
95 #include "ofdev.h"
96 #include "openfirm.h"
97
98 #ifdef DEBUG
99 # define DPRINTF printf
100 #else
101 # define DPRINTF while (0) printf
102 #endif
103
104 char bootdev[128];
105 char bootfile[128];
106 int boothowto;
107 int debug;
108
109 static ofw_version = 0;
110 static char *kernels[] = { "/netbsd", "/netbsd.gz", "/netbsd.macppc", NULL };
111
112 static void
113 prom2boot(dev)
114 char *dev;
115 {
116 char *cp;
117
118 cp = dev + strlen(dev) - 1;
119 for (; *cp; cp--) {
120 if (*cp == ':') {
121 if (ofw_version < 3) {
122 /* sd@0:0 -> sd@0 */
123 *cp = 0;
124 break;
125 } else {
126 /* disk@0:5,boot -> disk@0:0 */
127 strcpy(cp, ":0");
128 break;
129 }
130 }
131 }
132 }
133
134 static void
135 parseargs(str, howtop)
136 char *str;
137 int *howtop;
138 {
139 char *cp;
140
141 /* Allow user to drop back to the PROM. */
142 if (strcmp(str, "exit") == 0)
143 OF_exit();
144
145 *howtop = 0;
146
147 cp = str;
148 if (*cp == '-')
149 goto found;
150 for (cp = str; *cp; cp++)
151 if (*cp == ' ')
152 goto found;
153 return;
154
155 found:
156 *cp++ = 0;
157 while (*cp)
158 BOOT_FLAG(*cp++, *howtop);
159 }
160
161 static void
162 chain(entry, args, ssym, esym)
163 void (*entry)();
164 char *args;
165 void *ssym, *esym;
166 {
167 extern char end[];
168 int l;
169
170 /*
171 * Stash pointer to end of symbol table after the argument
172 * strings.
173 */
174 l = strlen(args) + 1;
175 memcpy(args + l, &ssym, sizeof(ssym));
176 l += sizeof(ssym);
177 memcpy(args + l, &esym, sizeof(esym));
178 l += sizeof(esym);
179
180 OF_chain((void *)RELOC, end - (char *)RELOC, entry, args, l);
181 panic("chain");
182 }
183
184 __dead void
185 _rtt()
186 {
187
188 OF_exit();
189 }
190
191 void
192 main()
193 {
194 extern char bootprog_name[], bootprog_rev[],
195 bootprog_maker[], bootprog_date[];
196 int chosen, options, openprom;
197 char bootline[512]; /* Should check size? */
198 char *cp;
199 u_long marks[MARK_MAX];
200 u_int32_t entry;
201 void *ssym, *esym;
202
203 printf("\n");
204 printf(">> %s, Revision %s\n", bootprog_name, bootprog_rev);
205 printf(">> (%s, %s)\n", bootprog_maker, bootprog_date);
206
207 /*
208 * Figure out what version of Open Firmware...
209 */
210 if ((openprom = OF_finddevice("/openprom")) != -1) {
211 char model[32];
212
213 memset(model, 0, sizeof model);
214 OF_getprop(openprom, "model", model, sizeof model);
215 for (cp = model; *cp; cp++)
216 if (*cp >= '0' && *cp <= '9') {
217 ofw_version = *cp - '0';
218 break;
219 }
220 #if 0
221 printf(">> Open Firmware version %d.x\n", ofw_version);
222 #endif
223 }
224
225 /*
226 * Get the boot arguments from Openfirmware
227 */
228 if ((chosen = OF_finddevice("/chosen")) == -1 ||
229 OF_getprop(chosen, "bootpath", bootdev, sizeof bootdev) < 0 ||
230 OF_getprop(chosen, "bootargs", bootline, sizeof bootline) < 0) {
231 printf("Invalid Openfirmware environment\n");
232 OF_exit();
233 }
234
235 /*
236 * Some versions of Openfirmware sets bootpath to "".
237 * We use boot-device instead if it occurs.
238 */
239 if (bootdev[0] == 0) {
240 printf("Cannot use bootpath\n");
241 if ((options = OF_finddevice("/options")) == -1 ||
242 OF_getprop(options, "boot-device", bootdev,
243 sizeof bootdev) < 0) {
244 printf("Invalid Openfirmware environment\n");
245 OF_exit();
246 }
247 printf("Using boot-device instead\n");
248 }
249
250 prom2boot(bootdev);
251 parseargs(bootline, &boothowto);
252 DPRINTF("bootline=%s\n", bootline);
253
254 for (;;) {
255 int i;
256
257 if (boothowto & RB_ASKNAME) {
258 printf("Boot: ");
259 gets(bootline);
260 parseargs(bootline, &boothowto);
261 }
262
263 if (bootline[0]) {
264 kernels[0] = bootline;
265 kernels[1] = NULL;
266 }
267
268 for (i = 0; kernels[i]; i++) {
269 DPRINTF("Trying %s\n", kernels[i]);
270
271 marks[MARK_START] = 0;
272 if (loadfile(kernels[i], marks, LOAD_KERNEL) >= 0)
273 goto loaded;
274 }
275 boothowto |= RB_ASKNAME;
276 }
277 loaded:
278
279 #ifdef __notyet__
280 OF_setprop(chosen, "bootpath", opened_name, strlen(opened_name) + 1);
281 cp = bootline;
282 #else
283 strcpy(bootline, opened_name);
284 cp = bootline + strlen(bootline);
285 *cp++ = ' ';
286 #endif
287 *cp = '-';
288 if (boothowto & RB_ASKNAME)
289 *++cp = 'a';
290 if (boothowto & RB_SINGLE)
291 *++cp = 's';
292 if (boothowto & RB_KDB)
293 *++cp = 'd';
294 if (*cp == '-')
295 #ifdef __notyet__
296 *cp = 0;
297 #else
298 *--cp = 0;
299 #endif
300 else
301 *++cp = 0;
302 #ifdef __notyet__
303 OF_setprop(chosen, "bootargs", bootline, strlen(bootline) + 1);
304 #endif
305
306 entry = marks[MARK_ENTRY];
307 ssym = (void *)marks[MARK_SYM];
308 esym = (void *)marks[MARK_END];
309
310 printf(" start=0x%x\n", entry);
311 __syncicache((void *)entry, (u_int)ssym - (u_int)entry);
312 chain((void *)entry, bootline, ssym, esym);
313
314 OF_exit();
315 }
316