xref: /netbsd/sys/arch/ofppc/stand/ofwboot/boot.c (revision 6550d01e)
1 /*	$NetBSD: boot.c,v 1.23 2011/01/22 19:19:21 joerg 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  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * Copyright (C) 1995, 1996 Wolfgang Solfrank.
34  * Copyright (C) 1995, 1996 TooLs GmbH.
35  * All rights reserved.
36  *
37  * ELF support derived from NetBSD/alpha's boot loader, written
38  * by Christopher G. Demetriou.
39  *
40  * Redistribution and use in source and binary forms, with or without
41  * modification, are permitted provided that the following conditions
42  * are met:
43  * 1. Redistributions of source code must retain the above copyright
44  *    notice, this list of conditions and the following disclaimer.
45  * 2. Redistributions in binary form must reproduce the above copyright
46  *    notice, this list of conditions and the following disclaimer in the
47  *    documentation and/or other materials provided with the distribution.
48  * 3. All advertising materials mentioning features or use of this software
49  *    must display the following acknowledgement:
50  *	This product includes software developed by TooLs GmbH.
51  * 4. The name of TooLs GmbH may not be used to endorse or promote products
52  *    derived from this software without specific prior written permission.
53  *
54  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
55  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
56  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
57  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
58  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
59  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
60  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
61  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
62  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
63  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
64  */
65 
66 /*
67  * First try for the boot code
68  *
69  * Input syntax is:
70  *	[promdev[{:|,}partition]]/[filename] [flags]
71  */
72 
73 #define	ELFSIZE		32		/* We use 32-bit ELF. */
74 
75 #include <sys/param.h>
76 #include <sys/exec.h>
77 #include <sys/exec_elf.h>
78 #include <sys/reboot.h>
79 #include <sys/disklabel.h>
80 #include <sys/boot_flag.h>
81 
82 #include <lib/libsa/stand.h>
83 #include <lib/libsa/loadfile.h>
84 #include <lib/libkern/libkern.h>
85 
86 #include <machine/cpu.h>
87 
88 #include "boot.h"
89 #include "ofdev.h"
90 #include "openfirm.h"
91 
92 #ifdef DEBUG
93 # define DPRINTF printf
94 #else
95 # define DPRINTF while (/*CONSTCOND*/0) printf
96 #endif
97 
98 char bootdev[128];
99 char bootfile[128];
100 int boothowto;
101 int debug;
102 
103 #ifdef OFWDUMP
104 void dump_ofwtree(int);
105 #endif
106 
107 static char *kernels[] = { "/netbsd.ofppc", "/netbsd",
108 			   "/netbsd.gz", "onetbsd", NULL };
109 static char *kernels64[] = { "/netbsd.ofppc64", "/netbsd64", "/netbsd64.gz",
110 			     "onetbsd64", "/netbsd.ofppc", "/netbsd",
111 			     "/netbsd.gz", "onetbsd", 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[];
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 	int chosen, cpu, cpunode, j, is64=0;
193 	char bootline[512];		/* Should check size? */
194 	char *cp;
195 	u_long marks[MARK_MAX];
196 	u_int32_t entry;
197 	void *ssym, *esym;
198 
199 	printf("\n");
200 	printf(">> %s, Revision %s\n", bootprog_name, bootprog_rev);
201 
202 #ifdef OFWDUMP
203 	chosen = OF_finddevice("/");
204 	dump_ofwtree(chosen);
205 #endif
206 	/*
207 	 * Get the boot arguments from Openfirmware
208 	 */
209 	if ((chosen = OF_finddevice("/chosen")) == -1 ||
210 	    OF_getprop(chosen, "bootpath", bootdev, sizeof bootdev) < 0 ||
211 	    OF_getprop(chosen, "bootargs", bootline, sizeof bootline) < 0) {
212 		printf("Invalid Openfirmware environment\n");
213 		OF_exit();
214 	}
215 
216 	/* lets see if we can guess the 64bittedness */
217 	if (OF_getprop(chosen, "cpu", &cpu, sizeof cpu) ==  sizeof(cpu)) {
218 		cpunode = OF_instance_to_package(cpu);
219 		if (OF_getprop(cpunode, "64-bit", &j, sizeof j) >= 0) {
220 			is64 = 1;
221 		}
222 	}
223 
224 	prom2boot(bootdev);
225 	parseargs(bootline, &boothowto);
226 	DPRINTF("bootline=%s\n", bootline);
227 
228 	for (;;) {
229 		int i;
230 
231 		if (boothowto & RB_ASKNAME) {
232 			printf("Boot: ");
233 			gets(bootline);
234 			parseargs(bootline, &boothowto);
235 		}
236 
237 		if (bootline[0]) {
238 			kernels[0] = bootline;
239 			kernels[1] = NULL;
240 		}
241 		if (!bootline[0] && is64) {
242 			for (i = 0; kernels64[i]; i++) {
243 				DPRINTF("Trying %s\n", kernels64[i]);
244 
245 				marks[MARK_START] = 0;
246 				if (loadfile(kernels64[i], marks, LOAD_KERNEL) >= 0)
247 					goto loaded;
248 			}
249 		} else {
250 			for (i = 0; kernels[i]; i++) {
251 				DPRINTF("Trying %s\n", kernels[i]);
252 
253 				marks[MARK_START] = 0;
254 				if (loadfile(kernels[i], marks, LOAD_KERNEL) >= 0)
255 					goto loaded;
256 			}
257 		}
258 
259 		boothowto |= RB_ASKNAME;
260 	}
261  loaded:
262 
263 #ifdef	__notyet__
264 	OF_setprop(chosen, "bootpath", opened_name, strlen(opened_name) + 1);
265 	cp = bootline;
266 #else
267 	strcpy(bootline, opened_name);
268 	cp = bootline + strlen(bootline);
269 	*cp++ = ' ';
270 #endif
271 	*cp = '-';
272 	if (boothowto & RB_ASKNAME)
273 		*++cp = 'a';
274 	if (boothowto & RB_SINGLE)
275 		*++cp = 's';
276 	if (boothowto & RB_KDB)
277 		*++cp = 'd';
278 	if (*cp == '-')
279 #ifdef	__notyet__
280 		*cp = 0;
281 #else
282 		*--cp = 0;
283 #endif
284 	else
285 		*++cp = 0;
286 #ifdef	__notyet__
287 	OF_setprop(chosen, "bootargs", bootline, strlen(bootline) + 1);
288 #endif
289 
290 	entry = marks[MARK_ENTRY];
291 	ssym = (void *)marks[MARK_SYM];
292 	esym = (void *)marks[MARK_END];
293 
294 	printf(" start=0x%x\n", entry);
295 	__syncicache((void *) entry, (u_int) ssym - (u_int) entry);
296 	chain((boot_entry_t) entry, bootline, ssym, esym);
297 
298 	OF_exit();
299 }
300