boot.c revision 1.20 1 /* $NetBSD: boot.c,v 1.20 2006/01/27 03:11:33 uwe 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 #include "boot.h"
81
82 #include <sys/param.h>
83 #include <sys/boot_flag.h>
84
85 #include <lib/libsa/stand.h>
86 #include <lib/libsa/loadfile.h>
87 #include <lib/libkern/libkern.h>
88
89 #include "ofdev.h"
90 #include "openfirm.h"
91
92 extern void __syncicache(void *, size_t); /* in libkern */
93
94
95 #ifdef DEBUG
96 # define DPRINTF printf
97 #else
98 # define DPRINTF while (0) printf
99 #endif
100
101 char bootdev[128];
102 char bootfile[128];
103 int boothowto;
104
105 static int ofw_version = 0;
106 static const char *kernels[] = { "/netbsd", "/netbsd.gz", "/netbsd.macppc", NULL };
107
108 static void
109 prom2boot(char *dev)
110 {
111 char *cp;
112
113 cp = dev + strlen(dev) - 1;
114 for (; *cp; cp--) {
115 if (*cp == ':') {
116 if (ofw_version < 3) {
117 /* sd@0:0 -> sd@0 */
118 *cp = 0;
119 break;
120 } else {
121 /* disk@0:5,boot -> disk@0:0 */
122 strcpy(cp, ":0");
123 break;
124 }
125 }
126 }
127 }
128
129 static void
130 parseargs(char *str, int *howtop)
131 {
132 char *cp;
133
134 /* Allow user to drop back to the PROM. */
135 if (strcmp(str, "exit") == 0)
136 OF_exit();
137
138 *howtop = 0;
139
140 cp = str;
141 if (*cp == '-')
142 goto found;
143 for (cp = str; *cp; cp++)
144 if (*cp == ' ')
145 goto found;
146 return;
147
148 found:
149 *cp++ = 0;
150 while (*cp)
151 BOOT_FLAG(*cp++, *howtop);
152 }
153
154 static void
155 chain(boot_entry_t entry, char *args, void *ssym, void *esym)
156 {
157 extern char end[];
158 int l;
159
160 /*
161 * Stash pointer to end of symbol table after the argument
162 * strings.
163 */
164 l = strlen(args) + 1;
165 memcpy(args + l, &ssym, sizeof(ssym));
166 l += sizeof(ssym);
167 memcpy(args + l, &esym, sizeof(esym));
168 l += sizeof(esym);
169 l += sizeof(int); /* XXX */
170
171 OF_chain((void *) RELOC, end - (char *) RELOC, entry, args, l);
172 panic("chain");
173 }
174
175 __dead void
176 _rtt(void)
177 {
178
179 OF_exit();
180 }
181
182 void
183 main(void)
184 {
185 extern char bootprog_name[], bootprog_rev[],
186 bootprog_maker[], bootprog_date[];
187 int chosen, options, openprom;
188 char bootline[512]; /* Should check size? */
189 char *cp;
190 u_long marks[MARK_MAX];
191 u_int32_t entry;
192 void *ssym, *esym;
193
194 printf("\n");
195 printf(">> %s, Revision %s\n", bootprog_name, bootprog_rev);
196 printf(">> (%s, %s)\n", bootprog_maker, bootprog_date);
197
198 /*
199 * Figure out what version of Open Firmware...
200 */
201 if ((openprom = OF_finddevice("/openprom")) != -1) {
202 char model[32];
203
204 memset(model, 0, sizeof model);
205 OF_getprop(openprom, "model", model, sizeof model);
206 for (cp = model; *cp; cp++)
207 if (*cp >= '0' && *cp <= '9') {
208 ofw_version = *cp - '0';
209 break;
210 }
211 DPRINTF(">> Open Firmware version %d.x\n", ofw_version);
212 }
213
214 /*
215 * Get the boot arguments from Openfirmware
216 */
217 if ((chosen = OF_finddevice("/chosen")) == -1 ||
218 OF_getprop(chosen, "bootpath", bootdev, sizeof bootdev) < 0 ||
219 OF_getprop(chosen, "bootargs", bootline, sizeof bootline) < 0) {
220 printf("Invalid Openfirmware environment\n");
221 OF_exit();
222 }
223
224 /*
225 * Some versions of Openfirmware sets bootpath to "".
226 * We use boot-device instead if it occurs.
227 */
228 if (bootdev[0] == 0) {
229 printf("Cannot use bootpath\n");
230 if ((options = OF_finddevice("/options")) == -1 ||
231 OF_getprop(options, "boot-device", bootdev,
232 sizeof bootdev) < 0) {
233 printf("Invalid Openfirmware environment\n");
234 OF_exit();
235 }
236 printf("Using boot-device instead\n");
237 }
238
239 prom2boot(bootdev);
240 parseargs(bootline, &boothowto);
241 DPRINTF("bootline=%s\n", bootline);
242
243 for (;;) {
244 int i;
245
246 if (boothowto & RB_ASKNAME) {
247 printf("Boot: ");
248 gets(bootline);
249 parseargs(bootline, &boothowto);
250 }
251
252 if (bootline[0]) {
253 kernels[0] = bootline;
254 kernels[1] = NULL;
255 }
256
257 for (i = 0; kernels[i]; i++) {
258 DPRINTF("Trying %s\n", kernels[i]);
259
260 marks[MARK_START] = 0;
261 if (loadfile(kernels[i], marks, LOAD_KERNEL) >= 0)
262 goto loaded;
263 }
264 boothowto |= RB_ASKNAME;
265 }
266 loaded:
267
268 #ifdef __notyet__
269 OF_setprop(chosen, "bootpath", opened_name, strlen(opened_name) + 1);
270 cp = bootline;
271 #else
272 strcpy(bootline, opened_name);
273 cp = bootline + strlen(bootline);
274 *cp++ = ' ';
275 #endif
276 *cp = '-';
277 if (boothowto & RB_ASKNAME)
278 *++cp = 'a';
279 if (boothowto & RB_USERCONF)
280 *++cp = 'c';
281 if (boothowto & RB_SINGLE)
282 *++cp = 's';
283 if (boothowto & RB_KDB)
284 *++cp = 'd';
285 if (*cp == '-')
286 #ifdef __notyet__
287 *cp = 0;
288 #else
289 *--cp = 0;
290 #endif
291 else
292 *++cp = 0;
293 #ifdef __notyet__
294 OF_setprop(chosen, "bootargs", bootline, strlen(bootline) + 1);
295 #endif
296
297 entry = marks[MARK_ENTRY];
298 ssym = (void *)marks[MARK_SYM];
299 esym = (void *)marks[MARK_END];
300
301 printf(" start=0x%x\n", entry);
302 __syncicache((void *) entry, (u_int) ssym - (u_int) entry);
303 chain((boot_entry_t) entry, bootline, ssym, esym);
304
305 OF_exit();
306 }
307
308 #ifdef HAVE_CHANGEDISK_HOOK
309 void
310 changedisk_hook(struct open_file *of)
311 {
312 struct of_dev *op = of->f_devdata;
313 int c;
314
315 OF_call_method("eject", op->handle, 0, 0);
316
317 c = getchar();
318 if (c == 'q') {
319 printf("quit\n");
320 OF_exit();
321 }
322
323 OF_call_method("close", op->handle, 0, 0);
324 OF_call_method("open", op->handle, 0, 0);
325 }
326 #endif
327