xref: /netbsd-src/sys/arch/ofppc/stand/ofwboot/boot.c (revision b519c70ad771d0a55b3c2277db6b97a05fa6465d)
1 /*	$NetBSD: boot.c,v 1.11 2001/10/23 03:31:26 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 #include <sys/boot_flag.h>
88 
89 #include <lib/libsa/stand.h>
90 #include <lib/libsa/loadfile.h>
91 #include <lib/libkern/libkern.h>
92 
93 #include <machine/cpu.h>
94 
95 #include "ofdev.h"
96 #include "openfirm.h"
97 
98 #ifdef DEBUG
99 # define DPRINTF printf
100 #else
101 # define DPRINTF while (/*CONSTCOND*/0) printf
102 #endif
103 
104 char bootdev[128];
105 char bootfile[128];
106 int boothowto;
107 int debug;
108 
109 static int ofw_version = 0;
110 static char *kernels[] = { "/netbsd", "/netbsd.gz", "/netbsd.ofppc", NULL };
111 
112 static void
113 prom2boot(dev)
114 	char *dev;
115 {
116 	char *cp, *ocp;
117 
118 	ocp = cp;
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(str, howtop)
130 	char *str;
131 	int *howtop;
132 {
133 	char *cp;
134 
135 	/* Allow user to drop back to the PROM. */
136 	if (strcmp(str, "exit") == 0)
137 		OF_exit();
138 
139 	*howtop = 0;
140 
141 	for (cp = str; *cp; cp++)
142 		if (*cp == ' ' || *cp == '-')
143 			goto found;
144 
145 	return;
146 
147  found:
148 	*cp++ = '\0';
149 	while (*cp)
150 		BOOT_FLAG(*cp++, *howtop);
151 }
152 
153 static void
154 chain(entry, args, ssym, esym)
155 	void (*entry)();
156 	char *args;
157 	void *ssym, *esym;
158 {
159 	extern char end[], *cp;
160 	u_int l, magic = 0x19730224;
161 
162 	freeall();
163 
164 	/*
165 	 * Stash pointer to start and end of symbol table after the argument
166 	 * strings.
167 	 */
168 	l = strlen(args) + 1;
169 	l = (l + 3) & ~3;			/* align */
170 	DPRINTF("magic @ %p\n", args + l);
171 	memcpy(args + l, &magic, sizeof(magic));
172 	l += sizeof(magic);
173 	DPRINTF("ssym @ %p\n", args + l);
174 	memcpy(args + l, &ssym, sizeof(ssym));
175 	l += sizeof(ssym);
176 	DPRINTF("esym @ %p\n", args + l);
177 	memcpy(args + l, &esym, sizeof(esym));
178 	l += sizeof(esym);
179 	DPRINTF("args + l -> %p\n", args + l);
180 
181 	OF_chain((void *)RELOC, end - (char *)RELOC, entry, args, l);
182 	panic("chain");
183 }
184 
185 __dead void
186 _rtt()
187 {
188 
189 	OF_exit();
190 }
191 
192 void
193 main()
194 {
195 	extern char bootprog_name[], bootprog_rev[],
196 		    bootprog_maker[], bootprog_date[];
197 	int chosen, options;
198 	char bootline[512];		/* Should check size? */
199 	char *cp;
200 	u_long marks[MARK_MAX];
201 	u_int32_t entry;
202 	void *ssym, *esym;
203 
204 	printf("\n");
205 	printf(">> %s, Revision %s\n", bootprog_name, bootprog_rev);
206 	printf(">> (%s, %s)\n", bootprog_maker, bootprog_date);
207 
208 	/*
209 	 * Get the boot arguments from Openfirmware
210 	 */
211 	if ((chosen = OF_finddevice("/chosen")) == -1 ||
212 	    OF_getprop(chosen, "bootpath", bootdev, sizeof bootdev) < 0 ||
213 	    OF_getprop(chosen, "bootargs", bootline, sizeof bootline) < 0) {
214 		printf("Invalid Openfirmware environment\n");
215 		OF_exit();
216 	}
217 
218 	prom2boot(bootdev);
219 	parseargs(bootline, &boothowto);
220 	DPRINTF("bootline=%s\n", bootline);
221 
222 	for (;;) {
223 		int i;
224 
225 		if (boothowto & RB_ASKNAME) {
226 			printf("Boot: ");
227 			gets(bootline);
228 			parseargs(bootline, &boothowto);
229 		}
230 
231 		if (bootline[0]) {
232 			kernels[0] = bootline;
233 			kernels[1] = NULL;
234 		}
235 
236 		for (i = 0; kernels[i]; i++) {
237 			DPRINTF("Trying %s\n", kernels[i]);
238 
239 			marks[MARK_START] = 0;
240 			if (loadfile(kernels[i], marks, LOAD_KERNEL) >= 0)
241 				goto loaded;
242 		}
243 
244 		boothowto |= RB_ASKNAME;
245 	}
246  loaded:
247 
248 #ifdef	__notyet__
249 	OF_setprop(chosen, "bootpath", opened_name, strlen(opened_name) + 1);
250 	cp = bootline;
251 #else
252 	strcpy(bootline, opened_name);
253 	cp = bootline + strlen(bootline);
254 	*cp++ = ' ';
255 #endif
256 	*cp = '-';
257 	if (boothowto & RB_ASKNAME)
258 		*++cp = 'a';
259 	if (boothowto & RB_SINGLE)
260 		*++cp = 's';
261 	if (boothowto & RB_KDB)
262 		*++cp = 'd';
263 	if (*cp == '-')
264 #ifdef	__notyet__
265 		*cp = 0;
266 #else
267 		*--cp = 0;
268 #endif
269 	else
270 		*++cp = 0;
271 #ifdef	__notyet__
272 	OF_setprop(chosen, "bootargs", bootline, strlen(bootline) + 1);
273 #endif
274 
275 	entry = marks[MARK_ENTRY];
276 	ssym = (void *)marks[MARK_SYM];
277 	esym = (void *)marks[MARK_END];
278 
279 	printf(" start=0x%x\n", entry);
280 	__syncicache((void *)entry, (u_int)ssym - (u_int)entry);
281 	chain((void *)entry, bootline, ssym, esym);
282 
283 	OF_exit();
284 }
285