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