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