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