1/*	$NetBSD: boot.c,v 1.31 2021/02/28 20:27:40 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 *
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
86extern 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
95char bootdev[MAXBOOTPATHLEN];
96extern char bootfile[MAXBOOTPATHLEN];
97int boothowto;
98bool floppyboot;
99int ofw_version = 0;
100
101static const char *kernels[] = { "/netbsd", "/netbsd.gz", "/netbsd.macppc", NULL };
102
103static void
104prom2boot(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
124static void
125parseargs(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
143found:
144	*cp++ = 0;
145	while (*cp)
146		BOOT_FLAG(*cp++, *howtop);
147}
148
149static bool
150is_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
178static void
179chain(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
210void
211main(void)
212{
213	extern char bootprog_name[], bootprog_rev[];
214	char bootline[512];		/* Should check size? */
215	char prop[32];
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 (ofw_openprom != -1) {
228		memset(prop, 0, sizeof prop);
229		OF_getprop(ofw_openprom, "model", prop, sizeof prop);
230		for (cp = prop; *cp; cp++)
231			if (*cp >= '0' && *cp <= '9') {
232				ofw_version = *cp - '0';
233				break;
234			}
235		printf(">> Open Firmware version %d.x\n", ofw_version);
236	}
237	printf(">> Open Firmware running in %s-mode.\n",
238	    ofw_real_mode ? "real" : "virtual");
239
240	/*
241	 * Get the boot arguments from Openfirmware
242	 */
243	if (OF_getprop(ofw_chosen, "bootpath", bootdev, sizeof bootdev) < 0 ||
244	    OF_getprop(ofw_chosen, "bootargs", bootline, sizeof bootline) < 0) {
245		printf("Invalid Openfirmware environment\n");
246		OF_exit();
247	}
248
249	/*
250	 * Some versions of Openfirmware sets bootpath to "".
251	 * We use boot-device instead if it occurs.
252	 */
253	if (bootdev[0] == 0) {
254		printf("Cannot use bootpath\n");
255		if (ofw_options == -1 ||
256		    OF_getprop(ofw_options, "boot-device", bootdev,
257			       sizeof bootdev) < 0) {
258			printf("Invalid Openfirmware environment\n");
259			OF_exit();
260		}
261		printf("Using boot-device instead\n");
262	}
263
264	prom2boot(bootdev);
265	parseargs(bootline, &boothowto);
266	DPRINTF("bootline=%s\n", bootline);
267
268	for (;;) {
269		int i, loadflag;
270
271		if (boothowto & RB_ASKNAME) {
272			printf("Boot: ");
273			kgets(bootline, sizeof(bootline));
274			parseargs(bootline, &boothowto);
275		}
276
277		if (bootline[0]) {
278			kernels[0] = bootline;
279			kernels[1] = NULL;
280		}
281
282		for (i = 0; kernels[i]; i++) {
283			floppyboot = is_floppyboot(kernels[i], bootdev);
284
285			DPRINTF("Trying %s%s\n", kernels[i],
286			    floppyboot ? " (floppyboot)" : "");
287
288			loadflag = LOAD_KERNEL;
289			if (floppyboot)
290				loadflag &= ~LOAD_BACKWARDS;
291
292			marks[MARK_START] = 0;
293			if (loadfile(kernels[i], marks, loadflag) >= 0)
294				goto loaded;
295		}
296		boothowto |= RB_ASKNAME;
297	}
298loaded:
299
300#ifdef	__notyet__
301	OF_setprop(ofw_chosen, "bootpath", opened_name,
302		   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(ofw_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
342void
343changedisk_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, NULL);
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, NULL);
357	OF_call_method("open", op->handle, 0, 0, NULL);
358}
359#endif
360