boot.c revision 1.18.12.1 1 /* $NetBSD: boot.c,v 1.18.12.1 2006/06/21 14:53:28 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 * 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 freeall();
161
162 /*
163 * Stash pointer to end of symbol table after the argument
164 * strings.
165 */
166 l = strlen(args) + 1;
167 memcpy(args + l, &ssym, sizeof(ssym));
168 l += sizeof(ssym);
169 memcpy(args + l, &esym, sizeof(esym));
170 l += sizeof(esym);
171 l += sizeof(int); /* XXX */
172
173 OF_chain((void *) RELOC, end - (char *) RELOC, entry, args, l);
174 panic("chain");
175 }
176
177 __dead void
178 _rtt(void)
179 {
180
181 OF_exit();
182 }
183
184 void
185 main(void)
186 {
187 extern char bootprog_name[], bootprog_rev[],
188 bootprog_maker[], bootprog_date[];
189 int chosen, options, openprom;
190 char bootline[512]; /* Should check size? */
191 char *cp;
192 u_long marks[MARK_MAX];
193 u_int32_t entry;
194 void *ssym, *esym;
195
196 printf("\n");
197 printf(">> %s, Revision %s\n", bootprog_name, bootprog_rev);
198 printf(">> (%s, %s)\n", bootprog_maker, bootprog_date);
199
200 /*
201 * Figure out what version of Open Firmware...
202 */
203 if ((openprom = OF_finddevice("/openprom")) != -1) {
204 char model[32];
205
206 memset(model, 0, sizeof model);
207 OF_getprop(openprom, "model", model, sizeof model);
208 for (cp = model; *cp; cp++)
209 if (*cp >= '0' && *cp <= '9') {
210 ofw_version = *cp - '0';
211 break;
212 }
213 DPRINTF(">> Open Firmware version %d.x\n", ofw_version);
214 }
215
216 /*
217 * Get the boot arguments from Openfirmware
218 */
219 if ((chosen = OF_finddevice("/chosen")) == -1 ||
220 OF_getprop(chosen, "bootpath", bootdev, sizeof bootdev) < 0 ||
221 OF_getprop(chosen, "bootargs", bootline, sizeof bootline) < 0) {
222 printf("Invalid Openfirmware environment\n");
223 OF_exit();
224 }
225
226 /*
227 * Some versions of Openfirmware sets bootpath to "".
228 * We use boot-device instead if it occurs.
229 */
230 if (bootdev[0] == 0) {
231 printf("Cannot use bootpath\n");
232 if ((options = OF_finddevice("/options")) == -1 ||
233 OF_getprop(options, "boot-device", bootdev,
234 sizeof bootdev) < 0) {
235 printf("Invalid Openfirmware environment\n");
236 OF_exit();
237 }
238 printf("Using boot-device instead\n");
239 }
240
241 prom2boot(bootdev);
242 parseargs(bootline, &boothowto);
243 DPRINTF("bootline=%s\n", bootline);
244
245 for (;;) {
246 int i;
247
248 if (boothowto & RB_ASKNAME) {
249 printf("Boot: ");
250 gets(bootline);
251 parseargs(bootline, &boothowto);
252 }
253
254 if (bootline[0]) {
255 kernels[0] = bootline;
256 kernels[1] = NULL;
257 }
258
259 for (i = 0; kernels[i]; i++) {
260 DPRINTF("Trying %s\n", kernels[i]);
261
262 marks[MARK_START] = 0;
263 if (loadfile(kernels[i], marks, LOAD_KERNEL) >= 0)
264 goto loaded;
265 }
266 boothowto |= RB_ASKNAME;
267 }
268 loaded:
269
270 #ifdef __notyet__
271 OF_setprop(chosen, "bootpath", opened_name, strlen(opened_name) + 1);
272 cp = bootline;
273 #else
274 strcpy(bootline, opened_name);
275 cp = bootline + strlen(bootline);
276 *cp++ = ' ';
277 #endif
278 *cp = '-';
279 if (boothowto & RB_ASKNAME)
280 *++cp = 'a';
281 if (boothowto & RB_USERCONF)
282 *++cp = 'c';
283 if (boothowto & RB_SINGLE)
284 *++cp = 's';
285 if (boothowto & RB_KDB)
286 *++cp = 'd';
287 if (*cp == '-')
288 #ifdef __notyet__
289 *cp = 0;
290 #else
291 *--cp = 0;
292 #endif
293 else
294 *++cp = 0;
295 #ifdef __notyet__
296 OF_setprop(chosen, "bootargs", bootline, strlen(bootline) + 1);
297 #endif
298
299 entry = marks[MARK_ENTRY];
300 ssym = (void *)marks[MARK_SYM];
301 esym = (void *)marks[MARK_END];
302
303 printf(" start=0x%x\n", entry);
304 __syncicache((void *) entry, (u_int) ssym - (u_int) entry);
305 chain((boot_entry_t) entry, bootline, ssym, esym);
306
307 OF_exit();
308 }
309
310 #ifdef HAVE_CHANGEDISK_HOOK
311 void
312 changedisk_hook(struct open_file *of)
313 {
314 struct of_dev *op = of->f_devdata;
315 int c;
316
317 OF_call_method("eject", op->handle, 0, 0);
318
319 c = getchar();
320 if (c == 'q') {
321 printf("quit\n");
322 OF_exit();
323 }
324
325 OF_call_method("close", op->handle, 0, 0);
326 OF_call_method("open", op->handle, 0, 0);
327 }
328 #endif
329