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