boot.c revision 1.4.4.1 1 /* $NetBSD: boot.c,v 1.4.4.1 1999/06/21 00:51:42 thorpej 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
88 #include <lib/libsa/stand.h>
89 #include <lib/libsa/loadfile.h>
90 #include <lib/libkern/libkern.h>
91
92 #include <machine/cpu.h>
93 #include <machine/machine_type.h>
94
95 #include "ofdev.h"
96 #include "openfirm.h"
97
98 char bootdev[128];
99 char bootfile[128];
100 int boothowto;
101 int debug;
102
103 static void
104 prom2boot(dev)
105 char *dev;
106 {
107 char *cp;
108
109 for (cp = dev; *cp != '\0'; cp++)
110 if (*cp == ':') {
111 *cp = '\0';
112 return;
113 }
114 }
115
116 static void
117 parseargs(str, howtop)
118 char *str;
119 int *howtop;
120 {
121 char *cp;
122
123 /* Allow user to drop back to the PROM. */
124 if (strcmp(str, "exit") == 0)
125 OF_exit();
126
127 *howtop = 0;
128
129 cp = str;
130 if (*cp == '-')
131 goto found;
132 for (cp = str; *cp; cp++)
133 if (*cp == ' ')
134 goto found;
135 return;
136
137 found:
138 *cp++ = 0;
139 while (*cp) {
140 switch (*cp++) {
141 case 'a':
142 *howtop |= RB_ASKNAME;
143 break;
144 case 's':
145 *howtop |= RB_SINGLE;
146 break;
147 case 'd':
148 *howtop |= RB_KDB;
149 debug = 1;
150 break;
151 }
152 }
153 }
154
155 static void
156 chain(entry, args, ssym, esym)
157 void (*entry)();
158 char *args;
159 void *ssym, *esym;
160 {
161 extern char end[];
162 int l, machine_tag;
163
164 freeall();
165
166 /*
167 * Stash pointer to end of symbol table after the argument
168 * strings.
169 */
170 l = strlen(args) + 1;
171 bcopy(&ssym, args + l, sizeof(ssym));
172 l += sizeof(ssym);
173 bcopy(&esym, args + l, sizeof(esym));
174 l += sizeof(esym);
175
176 /*
177 * Tell the kernel we're an OpenFirmware system.
178 */
179 machine_tag = POWERPC_MACHINE_OPENFIRMWARE;
180 bcopy(&machine_tag, args + l, sizeof(machine_tag));
181 l += sizeof(machine_tag);
182
183 OF_chain((void *)RELOC, end - (char *)RELOC, entry, args, l);
184 panic("chain");
185 }
186
187 __dead void
188 _rtt()
189 {
190
191 OF_exit();
192 }
193
194 void
195 main()
196 {
197 extern char bootprog_name[], bootprog_rev[],
198 bootprog_maker[], bootprog_date[];
199 int chosen, options;
200 char bootline[512]; /* Should check size? */
201 char *cp;
202 u_long marks[MARK_MAX];
203 u_int32_t entry;
204 void *ssym, *esym;
205
206 printf("\n");
207 printf(">> %s, Revision %s\n", bootprog_name, bootprog_rev);
208 printf(">> (%s, %s)\n", bootprog_maker, bootprog_date);
209
210 /*
211 * Get the boot arguments from Openfirmware
212 */
213 if ((chosen = OF_finddevice("/chosen")) == -1 ||
214 OF_getprop(chosen, "bootpath", bootdev, sizeof bootdev) < 0 ||
215 OF_getprop(chosen, "bootargs", bootline, sizeof bootline) < 0) {
216 printf("Invalid Openfirmware environment\n");
217 OF_exit();
218 }
219
220 /*
221 * Some versions of Openfirmware sets bootpath to "".
222 * We use boot-device instead if it occurs.
223 */
224 if (bootdev[0] == 0) {
225 printf("Cannot use bootpath\n");
226 if ((options = OF_finddevice("/options")) == -1 ||
227 OF_getprop(options, "boot-device", bootdev,
228 sizeof bootdev) < 0) {
229 printf("Invalid Openfirmware environment\n");
230 OF_exit();
231 }
232 printf("Using boot-device instead\n");
233 }
234
235 prom2boot(bootdev);
236 parseargs(bootline, &boothowto);
237
238 for (;;) {
239 if (boothowto & RB_ASKNAME) {
240 printf("Boot: ");
241 gets(bootline);
242 parseargs(bootline, &boothowto);
243 }
244 marks[MARK_START] = 0;
245 if (loadfile(bootline, marks, LOAD_ALL) >= 0)
246 break;
247 if (errno)
248 printf("open %s: %s\n", opened_name, strerror(errno));
249 boothowto |= RB_ASKNAME;
250 }
251 #ifdef __notyet__
252 OF_setprop(chosen, "bootpath", opened_name, strlen(opened_name) + 1);
253 cp = bootline;
254 #else
255 strcpy(bootline, opened_name);
256 cp = bootline + strlen(bootline);
257 *cp++ = ' ';
258 #endif
259 *cp = '-';
260 if (boothowto & RB_ASKNAME)
261 *++cp = 'a';
262 if (boothowto & RB_SINGLE)
263 *++cp = 's';
264 if (boothowto & RB_KDB)
265 *++cp = 'd';
266 if (*cp == '-')
267 #ifdef __notyet__
268 *cp = 0;
269 #else
270 *--cp = 0;
271 #endif
272 else
273 *++cp = 0;
274 #ifdef __notyet__
275 OF_setprop(chosen, "bootargs", bootline, strlen(bootline) + 1);
276 #endif
277
278 entry = marks[MARK_ENTRY];
279 ssym = (void *)marks[MARK_SYM];
280 esym = (void *)marks[MARK_END];
281
282 printf(" start=0x%x\n", entry);
283 __syncicache((void *)entry, (u_int)ssym - (u_int)entry);
284 chain((void *)entry, bootline, ssym, esym);
285
286 OF_exit();
287 }
288