xref: /netbsd-src/sys/arch/macppc/stand/ofwboot/boot.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: boot.c,v 1.29 2018/06/06 22:56:25 uwe 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 #include "boot.h"
74 
75 #include <sys/param.h>
76 #include <sys/boot_flag.h>
77 #include <sys/disklabel.h>
78 
79 #include <lib/libsa/stand.h>
80 #include <lib/libsa/loadfile.h>
81 #include <lib/libkern/libkern.h>
82 
83 #include "ofdev.h"
84 #include "openfirm.h"
85 
86 extern void __syncicache(void *, size_t); /* in libkern */
87 
88 
89 #ifdef DEBUG
90 # define DPRINTF printf
91 #else
92 # define DPRINTF while (0) printf
93 #endif
94 
95 char bootdev[MAXBOOTPATHLEN];
96 char bootfile[MAXBOOTPATHLEN];
97 int boothowto;
98 bool floppyboot;
99 int ofw_version = 0;
100 
101 static const char *kernels[] = { "/netbsd", "/netbsd.gz", "/netbsd.macppc", NULL };
102 
103 static void
104 prom2boot(char *dev)
105 {
106 	char *cp;
107 
108 	cp = dev + strlen(dev) - 1;
109 	for (; *cp; cp--) {
110 		if (*cp == ':') {
111 			if (ofw_version < 3) {
112 				/* sd@0:0 -> sd@0 */
113 				*cp = 0;
114 				break;
115 			} else {
116 				/* disk@0:5,boot -> disk@0:0 */
117 				strcpy(cp, ":0");
118 				break;
119 			}
120 		}
121 	}
122 }
123 
124 static void
125 parseargs(char *str, int *howtop)
126 {
127 	char *cp;
128 
129 	/* Allow user to drop back to the PROM. */
130 	if (strcmp(str, "exit") == 0)
131 		OF_exit();
132 
133 	*howtop = 0;
134 
135 	cp = str;
136 	if (*cp == '-')
137 		goto found;
138 	for (cp = str; *cp; cp++)
139 		if (*cp == ' ')
140 			goto found;
141 	return;
142 
143 found:
144 	*cp++ = 0;
145 	while (*cp)
146 		BOOT_FLAG(*cp++, *howtop);
147 }
148 
149 static bool
150 is_floppyboot(const char *path, const char *defaultdev)
151 {
152 	char dev[MAXBOOTPATHLEN];
153 	char nam[16];
154 	int handle, rv;
155 
156 	if (parsefilepath(path, dev, NULL, NULL)) {
157 		if (dev[0] == '\0' && defaultdev != NULL)
158 			strlcpy(dev, defaultdev, sizeof(dev));
159 
160 		/* check properties */
161 		handle = OF_finddevice(dev);
162 		if (handle != -1) {
163 			rv = OF_getprop(handle, "name", nam, sizeof(nam));
164 			if (rv >= 0 &&
165 			    (strcmp(nam, "swim3") == 0 ||
166 			     strcmp(nam, "floppy") == 0))
167 				return true;
168 		}
169 
170 		/* also check devalias */
171 		if (strcmp(dev, "fd") == 0)
172 			return true;
173 	}
174 
175 	return false;
176 }
177 
178 static void
179 chain(boot_entry_t entry, char *args, void *ssym, void *esym)
180 {
181 	extern char end[];
182 	int l;
183 
184 #if !defined(HEAP_VARIABLE)
185 	freeall();
186 #endif
187 
188 	/*
189 	 * Stash pointer to end of symbol table after the argument
190 	 * strings.
191 	 */
192 	l = strlen(args) + 1;
193 	memcpy(args + l, &ssym, sizeof(ssym));
194 	l += sizeof(ssym);
195 	memcpy(args + l, &esym, sizeof(esym));
196 	l += sizeof(esym);
197 	l += sizeof(int);	/* XXX */
198 
199 	OF_chain((void *) RELOC, end - (char *) RELOC, entry, args, l);
200 	panic("chain");
201 }
202 
203 __dead void
204 _rtt(void)
205 {
206 
207 	OF_exit();
208 }
209 
210 void
211 main(void)
212 {
213 	extern char bootprog_name[], bootprog_rev[];
214 	int chosen, options, openprom;
215 	char bootline[512];		/* Should check size? */
216 	char *cp;
217 	u_long marks[MARK_MAX];
218 	u_int32_t entry;
219 	void *ssym, *esym;
220 
221 	printf("\n");
222 	printf(">> %s, Revision %s\n", bootprog_name, bootprog_rev);
223 
224 	/*
225 	 * Figure out what version of Open Firmware...
226 	 */
227 	if ((openprom = OF_finddevice("/openprom")) != -1) {
228 		char model[32];
229 
230 		memset(model, 0, sizeof model);
231 		OF_getprop(openprom, "model", model, sizeof model);
232 		for (cp = model; *cp; cp++)
233 			if (*cp >= '0' && *cp <= '9') {
234 				ofw_version = *cp - '0';
235 				break;
236 			}
237 		DPRINTF(">> Open Firmware version %d.x\n", ofw_version);
238 	}
239 
240 	/*
241 	 * Get the boot arguments from Openfirmware
242 	 */
243 	if ((chosen = OF_finddevice("/chosen")) == -1 ||
244 	    OF_getprop(chosen, "bootpath", bootdev, sizeof bootdev) < 0 ||
245 	    OF_getprop(chosen, "bootargs", bootline, sizeof bootline) < 0) {
246 		printf("Invalid Openfirmware environment\n");
247 		OF_exit();
248 	}
249 
250 	/*
251 	 * Some versions of Openfirmware sets bootpath to "".
252 	 * We use boot-device instead if it occurs.
253 	 */
254 	if (bootdev[0] == 0) {
255 		printf("Cannot use bootpath\n");
256 		if ((options = OF_finddevice("/options")) == -1 ||
257 		    OF_getprop(options, "boot-device", bootdev,
258 			       sizeof bootdev) < 0) {
259 			printf("Invalid Openfirmware environment\n");
260 			OF_exit();
261 		}
262 		printf("Using boot-device instead\n");
263 	}
264 
265 	prom2boot(bootdev);
266 	parseargs(bootline, &boothowto);
267 	DPRINTF("bootline=%s\n", bootline);
268 
269 	for (;;) {
270 		int i, loadflag;
271 
272 		if (boothowto & RB_ASKNAME) {
273 			printf("Boot: ");
274 			kgets(bootline, sizeof(bootline));
275 			parseargs(bootline, &boothowto);
276 		}
277 
278 		if (bootline[0]) {
279 			kernels[0] = bootline;
280 			kernels[1] = NULL;
281 		}
282 
283 		for (i = 0; kernels[i]; i++) {
284 			floppyboot = is_floppyboot(kernels[i], bootdev);
285 
286 			DPRINTF("Trying %s%s\n", kernels[i],
287 			    floppyboot ? " (floppyboot)" : "");
288 
289 			loadflag = LOAD_KERNEL;
290 			if (floppyboot)
291 				loadflag &= ~LOAD_BACKWARDS;
292 
293 			marks[MARK_START] = 0;
294 			if (loadfile(kernels[i], marks, loadflag) >= 0)
295 				goto loaded;
296 		}
297 		boothowto |= RB_ASKNAME;
298 	}
299 loaded:
300 
301 #ifdef	__notyet__
302 	OF_setprop(chosen, "bootpath", opened_name, strlen(opened_name) + 1);
303 	cp = bootline;
304 #else
305 	strcpy(bootline, opened_name);
306 	cp = bootline + strlen(bootline);
307 	*cp++ = ' ';
308 #endif
309 	*cp = '-';
310 	if (boothowto & RB_ASKNAME)
311 		*++cp = 'a';
312 	if (boothowto & RB_USERCONF)
313 		*++cp = 'c';
314 	if (boothowto & RB_SINGLE)
315 		*++cp = 's';
316 	if (boothowto & RB_KDB)
317 		*++cp = 'd';
318 	if (*cp == '-')
319 #ifdef	__notyet__
320 		*cp = 0;
321 #else
322 		*--cp = 0;
323 #endif
324 	else
325 		*++cp = 0;
326 #ifdef	__notyet__
327 	OF_setprop(chosen, "bootargs", bootline, strlen(bootline) + 1);
328 #endif
329 
330 	entry = marks[MARK_ENTRY];
331 	ssym = (void *)marks[MARK_SYM];
332 	esym = (void *)marks[MARK_END];
333 
334 	printf(" start=0x%x\n", entry);
335 	__syncicache((void *)(uintptr_t)entry, (size_t)ssym - entry);
336 	chain((boot_entry_t)(uintptr_t)entry, bootline, ssym, esym);
337 
338 	OF_exit();
339 }
340 
341 #ifdef HAVE_CHANGEDISK_HOOK
342 void
343 changedisk_hook(struct open_file *of)
344 {
345 	struct of_dev *op = of->f_devdata;
346 	int c;
347 
348 	OF_call_method("eject", op->handle, 0, 0);
349 
350 	c = getchar();
351 	if (c == 'q') {
352 		printf("quit\n");
353 		OF_exit();
354 	}
355 
356 	OF_call_method("close", op->handle, 0, 0);
357 	OF_call_method("open", op->handle, 0, 0);
358 }
359 #endif
360