boot.c revision 1.20 1 /* $NetBSD: boot.c,v 1.20 2008/02/12 04:27:46 garbled 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 "boot.h"
96 #include "ofdev.h"
97 #include "openfirm.h"
98
99 #ifdef DEBUG
100 # define DPRINTF printf
101 #else
102 # define DPRINTF while (/*CONSTCOND*/0) printf
103 #endif
104
105 char bootdev[128];
106 char bootfile[128];
107 int boothowto;
108 int debug;
109
110 #ifdef OFWDUMP
111 void dump_ofwtree(int);
112 #endif
113
114 static char *kernels[] = { "/netbsd.ofppc", "/netbsd",
115 "/netbsd.gz", "onetbsd", NULL };
116 static char *kernels64[] = { "/netbsd.ofppc64", "/netbsd64", "/netbsd64.gz",
117 "onetbsd64", "/netbsd.ofppc", "/netbsd",
118 "/netbsd.gz", "onetbsd", NULL };
119
120 static void
121 prom2boot(char *dev)
122 {
123 char *cp, *ocp;
124
125 ocp = dev;
126 cp = dev + strlen(dev) - 1;
127 for (; cp >= ocp; cp--) {
128 if (*cp == ':') {
129 *cp = '\0';
130 return;
131 }
132 }
133 }
134
135 static void
136 parseargs(char *str, int *howtop)
137 {
138 char *cp;
139
140 /* Allow user to drop back to the PROM. */
141 if (strcmp(str, "exit") == 0)
142 OF_exit();
143 if (strcmp(str, "halt") == 0)
144 OF_exit();
145 if (strcmp(str, "reboot") == 0)
146 OF_boot("");
147
148 *howtop = 0;
149
150 for (cp = str; *cp; cp++)
151 if (*cp == ' ' || *cp == '-')
152 goto found;
153
154 return;
155
156 found:
157 *cp++ = '\0';
158 while (*cp)
159 BOOT_FLAG(*cp++, *howtop);
160 }
161
162 static void
163 chain(boot_entry_t entry, char *args, void *ssym, void *esym)
164 {
165 extern char end[], *cp;
166 u_int l, magic = 0x19730224;
167
168 /*
169 * Stash pointer to start and end of symbol table after the argument
170 * strings.
171 */
172 l = strlen(args) + 1;
173 DPRINTF("ssym @ %p\n", args + l);
174 memcpy(args + l, &ssym, sizeof(ssym));
175 l += sizeof(ssym);
176 DPRINTF("esym @ %p\n", args + l);
177 memcpy(args + l, &esym, sizeof(esym));
178 l += sizeof(esym);
179 DPRINTF("magic @ %p\n", args + l);
180 memcpy(args + l, &magic, sizeof(magic));
181 l += sizeof(magic);
182 DPRINTF("args + l -> %p\n", args + l);
183
184 OF_chain((void *) RELOC, end - (char *)RELOC, entry, args, l);
185 panic("chain");
186 }
187
188 __dead void
189 _rtt(void)
190 {
191
192 OF_exit();
193 }
194
195 void
196 main(void)
197 {
198 extern char bootprog_name[], bootprog_rev[],
199 bootprog_maker[], bootprog_date[];
200 int chosen, options, cpu, cpunode, j, is64=0;
201 char bootline[512]; /* Should check size? */
202 char *cp;
203 u_long marks[MARK_MAX];
204 u_int32_t entry;
205 void *ssym, *esym;
206
207 printf("\n");
208 printf(">> %s, Revision %s\n", bootprog_name, bootprog_rev);
209 printf(">> (%s, %s)\n", bootprog_maker, bootprog_date);
210
211 #ifdef OFWDUMP
212 chosen = OF_finddevice("/");
213 dump_ofwtree(chosen);
214 #endif
215 /*
216 * Get the boot arguments from Openfirmware
217 */
218 if ((chosen = OF_finddevice("/chosen")) == -1 ||
219 OF_getprop(chosen, "bootpath", bootdev, sizeof bootdev) < 0 ||
220 OF_getprop(chosen, "bootargs", bootline, sizeof bootline) < 0) {
221 printf("Invalid Openfirmware environment\n");
222 OF_exit();
223 }
224
225 /* lets see if we can guess the 64bittedness */
226 if (OF_getprop(chosen, "cpu", &cpu, sizeof cpu) == sizeof(cpu)) {
227 cpunode = OF_instance_to_package(cpu);
228 if (OF_getprop(cpunode, "64-bit", &j, sizeof j) >= 0) {
229 is64 = 1;
230 }
231 }
232
233 prom2boot(bootdev);
234 parseargs(bootline, &boothowto);
235 DPRINTF("bootline=%s\n", bootline);
236
237 for (;;) {
238 int i;
239
240 if (boothowto & RB_ASKNAME) {
241 printf("Boot: ");
242 gets(bootline);
243 parseargs(bootline, &boothowto);
244 }
245
246 if (bootline[0]) {
247 kernels[0] = bootline;
248 kernels[1] = NULL;
249 }
250 if (!bootline[0] && is64) {
251 for (i = 0; kernels64[i]; i++) {
252 DPRINTF("Trying %s\n", kernels64[i]);
253
254 marks[MARK_START] = 0;
255 if (loadfile(kernels64[i], marks, LOAD_KERNEL) >= 0)
256 goto loaded;
257 }
258 } else {
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 }
267
268 boothowto |= RB_ASKNAME;
269 }
270 loaded:
271
272 #ifdef __notyet__
273 OF_setprop(chosen, "bootpath", opened_name, strlen(opened_name) + 1);
274 cp = bootline;
275 #else
276 strcpy(bootline, opened_name);
277 cp = bootline + strlen(bootline);
278 *cp++ = ' ';
279 #endif
280 *cp = '-';
281 if (boothowto & RB_ASKNAME)
282 *++cp = 'a';
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