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