xref: /netbsd-src/sys/arch/sparc/stand/ofwboot/boot.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: boot.c,v 1.25 2010/08/25 20:16:49 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 1997, 1999 Eduardo E. Horvath.  All rights reserved.
5  * Copyright (c) 1997 Jason R. Thorpe.  All rights reserved.
6  * Copyright (C) 1995, 1996 Wolfgang Solfrank.
7  * Copyright (C) 1995, 1996 TooLs GmbH.
8  * All rights reserved.
9  *
10  * ELF support derived from NetBSD/alpha's boot loader, written
11  * by Christopher G. Demetriou.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. All advertising materials mentioning features or use of this software
22  *    must display the following acknowledgement:
23  *	This product includes software developed by TooLs GmbH.
24  * 4. The name of TooLs GmbH may not be used to endorse or promote products
25  *    derived from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
28  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
29  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
30  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
33  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
34  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
35  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
36  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /*
40  * First try for the boot code
41  *
42  * Input syntax is:
43  *	[promdev[{:|,}partition]]/[filename] [flags]
44  */
45 
46 #include <lib/libsa/stand.h>
47 #include <lib/libsa/loadfile.h>
48 #include <lib/libkern/libkern.h>
49 
50 #include <sys/param.h>
51 #include <sys/reboot.h>
52 #include <sys/disklabel.h>
53 #include <sys/boot_flag.h>
54 
55 #include <machine/cpu.h>
56 #include <machine/promlib.h>
57 #include <machine/bootinfo.h>
58 #include <sparc/stand/common/isfloppy.h>
59 
60 #include "boot.h"
61 #include "ofdev.h"
62 #include "openfirm.h"
63 
64 
65 #define COMPAT_BOOT(marks)	(marks[MARK_START] == marks[MARK_ENTRY])
66 
67 
68 typedef void (*entry_t)(long o0, long bootargs, long bootsize, long o3,
69                         long ofw);
70 
71 /*
72  * Boot device is derived from ROM provided information, or if there is none,
73  * this list is used in sequence, to find a kernel.
74  */
75 const char *kernelnames[] = {
76 	"netbsd",
77 	"netbsd.gz",
78 	"netbsd.old",
79 	"netbsd.old.gz",
80 	"onetbsd",
81 	"onetbsd.gz",
82 	"vmunix ",
83 #ifdef notyet
84 	"netbsd.pl ",
85 	"netbsd.pl.gz ",
86 	"netbsd.el ",
87 	"netbsd.el.gz ",
88 #endif
89 	NULL
90 };
91 
92 char bootdev[PROM_MAX_PATH];
93 bool root_fs_quickseekable = true;	/* unset for tftp boots */
94 static bool bootinfo_pass_bootdev = false;
95 
96 int debug  = 0;
97 int compatmode = 0;
98 
99 #if 0
100 static void
101 prom2boot(char *dev)
102 {
103 	char *cp, *lp = 0;
104 	int handle;
105 	char devtype[16];
106 
107 	for (cp = dev; *cp; cp++)
108 		if (*cp == ':')
109 			lp = cp;
110 	if (!lp)
111 		lp = cp;
112 	*lp = 0;
113 }
114 #endif
115 
116 static int
117 bootoptions(const char *ap, char *loaddev, char *kernel, char *options)
118 {
119 	int v = 0;
120 	const char *start1 = NULL, *end1 = NULL, *start2 = NULL, *end2 = NULL;
121 	const char *path;
122 	char partition, *pp;
123 
124 	*kernel  = '\0';
125 	*options = '\0';
126 
127 	if (ap == NULL) {
128 		return (0);
129 	}
130 
131 	while (*ap == ' ') {
132 		ap++;
133 	}
134 
135 	if (*ap != '-') {
136 		start1 = ap;
137 		while (*ap != '\0' && *ap != ' ') {
138 			ap++;
139 		}
140 		end1 = ap;
141 
142 		while (*ap != '\0' && *ap == ' ') {
143 			ap++;
144 		}
145 
146 		if (*ap != '-') {
147 			start2 = ap;
148 			while (*ap != '\0' && *ap != ' ') {
149 				ap++;
150 			}
151 			end2 = ap;
152 			while (*ap != '\0' && *ap == ' ') {
153 				ap++;
154 			}
155 		}
156 	}
157 	if (end2 == start2) {
158 		start2 = end2 = NULL;
159 	}
160 	if (end1 == start1) {
161 		start1 = end1 = NULL;
162 	}
163 
164 	if (start1 == NULL) {
165 		/* only options */
166 	} else if (start2 == NULL) {
167 		memcpy(kernel, start1, (end1 - start1));
168 		kernel[end1 - start1] = '\0';
169 		path = filename(kernel, &partition);
170 		if (path == NULL) {
171 			strcpy(loaddev, kernel);
172 			kernel[0] = '\0';
173 		} else if (path != kernel) {
174 			/* copy device part */
175 			memcpy(loaddev, kernel, path-kernel);
176 			loaddev[path-kernel] = '\0';
177 			if (partition) {
178 				pp = loaddev + strlen(loaddev);
179 				pp[0] = ':';
180 				pp[1] = partition;
181 				pp[2] = '\0';
182 			}
183 			/* and kernel path */
184 			strcpy(kernel, path);
185 		}
186 	} else {
187 		memcpy(loaddev, start1, (end1-start1));
188 		loaddev[end1-start1] = '\0';
189 		memcpy(kernel, start2, (end2 - start2));
190 		kernel[end2 - start2] = '\0';
191 	}
192 
193 	strcpy(options, ap);
194 	while (*ap != '\0' && *ap != ' ' && *ap != '\t' && *ap != '\n') {
195 		BOOT_FLAG(*ap, v);
196 		switch(*ap++) {
197 		case 'D':
198 			debug = 2;
199 			break;
200 		case 'C':
201 			compatmode = 1;
202 			break;
203 		default:
204 			break;
205 		}
206 	}
207 
208 	if (((v & RB_KDB) != 0) && (debug == 0)) {
209 		debug = 1;
210 	}
211 
212 	DPRINTF(("bootoptions: device='%s', kernel='%s', options='%s'\n",
213 	    loaddev, kernel, options));
214 	return (v);
215 }
216 
217 /*
218  * The older (those relying on ofwboot v1.8 and earlier) kernels can't handle
219  * ksyms information unless it resides in a dedicated memory allocated from
220  * PROM and aligned on NBPG boundary. This is because the kernels calculate
221  * their ends on their own, they use address of 'end[]' reference which follows
222  * text segment. Ok, allocate some memory from PROM and copy symbol information
223  * over there.
224  */
225 static void
226 ksyms_copyout(void **ssym, void **esym)
227 {
228 	void *addr;
229 	int kssize = (int)(long)(*esym - *ssym + 1);
230 
231 	DPRINTF(("ksyms_copyout(): ssym = %p, esym = %p, kssize = %d\n",
232 				*ssym, *esym, kssize));
233 
234 	if ( (addr = OF_claim(0, kssize, NBPG)) == (void *)-1) {
235 		panic("ksyms_copyout(): no space for symbol table");
236 	}
237 
238 	memcpy(addr, *ssym, kssize);
239 	*ssym = addr;
240 	*esym = addr + kssize - 1;
241 
242 	DPRINTF(("ksyms_copyout(): ssym = %p, esym = %p\n", *ssym, *esym));
243 }
244 
245 /*
246  * Prepare boot information and jump directly to the kernel.
247  */
248 static void
249 jump_to_kernel(u_long *marks, char *kernel, char *args, void *ofw)
250 {
251 	extern char end[];
252 	int l, machine_tag;
253 	long newargs[4];
254 	void *ssym, *esym;
255 	vaddr_t bootinfo;
256 	struct btinfo_symtab bi_sym;
257 	struct btinfo_kernend bi_kend;
258 	char *cp;
259 	char bootline[PROM_MAX_PATH * 2];
260 
261 	/* Compose kernel boot line. */
262 	strncpy(bootline, kernel, sizeof(bootline));
263 	cp = bootline + strlen(bootline);
264 	if (*args) {
265 		*cp++ = ' ';
266 		strncpy(bootline, args, sizeof(bootline) - (cp - bootline));
267 	}
268 	*cp = 0; args = bootline;
269 
270 	/* Record symbol information in the bootinfo. */
271 	bootinfo = bi_init(marks[MARK_END]);
272 	bi_sym.nsym = marks[MARK_NSYM];
273 	bi_sym.ssym = marks[MARK_SYM];
274 	bi_sym.esym = marks[MARK_END];
275 	bi_add(&bi_sym, BTINFO_SYMTAB, sizeof(bi_sym));
276 	bi_kend.addr= bootinfo + BOOTINFO_SIZE;
277 	bi_add(&bi_kend, BTINFO_KERNEND, sizeof(bi_kend));
278 	if (bootinfo_pass_bootdev) {
279 		struct {
280 			struct btinfo_common common;
281 			char name[256];
282 		} info;
283 
284 		strcpy(info.name, bootdev);
285 		bi_add(&info, BTINFO_BOOTDEV, strlen(bootdev)
286 			+sizeof(struct btinfo_bootdev));
287 	}
288 
289 	sparc64_finalize_tlb(marks[MARK_DATA]);
290 	sparc64_bi_add();
291 
292 	ssym  = (void*)(long)marks[MARK_SYM];
293 	esym  = (void*)(long)marks[MARK_END];
294 
295 	DPRINTF(("jump_to_kernel(): ssym = %p, esym = %p\n", ssym, esym));
296 
297 	/* Adjust ksyms pointers, if needed. */
298 	if (COMPAT_BOOT(marks) || compatmode) {
299 		ksyms_copyout(&ssym, &esym);
300 	}
301 
302 	freeall();
303 	/*
304 	 * When we come in args consists of a pointer to the boot
305 	 * string.  We need to fix it so it takes into account
306 	 * other params such as romp.
307 	 */
308 
309 	/*
310 	 * Stash pointer to end of symbol table after the argument
311 	 * strings.
312 	 */
313 	l = strlen(args) + 1;
314 	memcpy(args + l, &esym, sizeof(esym));
315 	l += sizeof(esym);
316 
317 	/*
318 	 * Tell the kernel we're an OpenFirmware system.
319 	 */
320 	machine_tag = SPARC_MACHINE_OPENFIRMWARE;
321 	memcpy(args + l, &machine_tag, sizeof(machine_tag));
322 	l += sizeof(machine_tag);
323 
324 	/*
325 	 * Since we don't need the boot string (we can get it from /chosen)
326 	 * we won't pass it in.  Just pass in esym and magic #
327 	 */
328 	newargs[0] = SPARC_MACHINE_OPENFIRMWARE;
329 	newargs[1] = (long)esym;
330 	newargs[2] = (long)ssym;
331 	newargs[3] = (long)(void*)bootinfo;
332 	args = (char *)newargs;
333 	l = sizeof(newargs);
334 
335 	/* if -D is set then pause in the PROM. */
336 	if (debug > 1) callrom();
337 
338 	/*
339 	 * Jump directly to the kernel. Solaris kernel and Sun PROM
340 	 * flash updates expect ROMP vector in %o0, so we do. Format
341 	 * of other parameters and their order reflect OF_chain()
342 	 * symantics since this is what older NetBSD kernels rely on.
343 	 * (see sparc64/include/bootinfo.h for specification).
344 	 */
345 	DPRINTF(("jump_to_kernel(%lx, %lx, %lx, %lx, %lx) @ %p\n", (long)ofw,
346 				(long)args, (long)l, (long)ofw, (long)ofw,
347 				(void*)marks[MARK_ENTRY]));
348 	(*(entry_t)marks[MARK_ENTRY])((long)ofw, (long)args, (long)l, (long)ofw,
349 				      (long)ofw);
350 	printf("Returned from kernel entry point!\n");
351 }
352 
353 static void
354 start_kernel(char *kernel, char *bootline, void *ofw, int isfloppy)
355 {
356 	int fd;
357 	u_long marks[MARK_MAX];
358 	int flags = LOAD_ALL;
359 	if (isfloppy)
360 		flags &= ~LOAD_BACKWARDS;
361 
362 	/*
363 	 * First, load headers using default allocator and check whether kernel
364 	 * entry address matches kernel text load address. If yes, this is the
365 	 * old kernel designed for ofwboot v1.8 and therefore it must be mapped
366 	 * by PROM. Otherwise, map the kernel with 4MB permanent pages.
367 	 */
368 	loadfile_set_allocator(LOADFILE_NOP_ALLOCATOR);
369 	if ( (fd = loadfile(kernel, marks, LOAD_HDR|COUNT_TEXT)) != -1) {
370 		if (COMPAT_BOOT(marks) || compatmode) {
371 			(void)printf("[c] ");
372 			loadfile_set_allocator(LOADFILE_OFW_ALLOCATOR);
373 		} else {
374 			loadfile_set_allocator(LOADFILE_MMU_ALLOCATOR);
375 		}
376 		(void)printf("Loading %s: ", kernel);
377 
378 		if (fdloadfile(fd, marks, flags) != -1) {
379 			close(fd);
380 			jump_to_kernel(marks, kernel, bootline, ofw);
381 		}
382 	}
383 	(void)printf("Failed to load '%s'.\n", kernel);
384 }
385 
386 static void
387 help(void)
388 {
389 	printf( "enter a special command\n"
390 		"  halt\n"
391 		"  exit\n"
392 		"    to return to OpenFirmware\n"
393 		"  ?\n"
394 		"  help\n"
395 		"    to display this message\n"
396 		"or a boot specification:\n"
397 		"  [device] [kernel] [options]\n"
398 		"\n"
399 		"for example:\n"
400 		"  disk:a netbsd -s\n");
401 }
402 
403 static void
404 do_config_command(const char *cmd, const char *arg)
405 {
406 	DPRINTF(("do_config_command: %s\n", cmd));
407 	if (strcmp(cmd, "bootpartition") == 0) {
408 		char *c;
409 
410 		DPRINTF(("switching boot partition to %s from %s\n",
411 		    arg, bootdev));
412 		c = strrchr(bootdev, ':');
413 		if (!c) return;
414 		if (c[1] == 0) return;
415 		if (strlen(arg) > strlen(c)) return;
416 		strcpy(c, arg);
417 		DPRINTF(("new boot device: %s\n", bootdev));
418 		bootinfo_pass_bootdev = true;
419 	}
420 }
421 
422 static void
423 parse_boot_config(char *cfg, size_t len)
424 {
425 	const char *cmd = NULL, *arg = NULL;
426 
427 	while (len) {
428 		if (isspace(*cfg)) {
429 			cfg++; len--; continue;
430 		}
431 		if (*cfg == ';' || *cfg == '#') {
432 			while (len && *cfg != '\r' && *cfg != '\n') {
433 				cfg++; len--;
434 			}
435 			continue;
436 		}
437 		cmd = cfg;
438 		while (len && !isspace(*cfg)) {
439 			cfg++; len--;
440 		}
441 		*cfg = 0;
442 		if (len > 0) {
443 			cfg++; len--;
444 			while (isspace(*cfg) && len) {
445 				cfg++; len--;
446 			}
447 			if (len > 0 ) {
448 				arg = cfg;
449 				while (len && !isspace(*cfg)) {
450 					cfg++; len--;
451 				}
452 				*cfg = 0;
453 			}
454 		}
455 		do_config_command(cmd, arg);
456 		if (len > 0) {
457 			cfg++; len--;
458 		}
459 	}
460 }
461 
462 static void
463 check_boot_config(void)
464 {
465 	int fd, err, off, len;
466 	struct stat st;
467 	char *bc;
468 
469 	if (!root_fs_quickseekable) return;
470 	DPRINTF(("checking for /boot.cfg...\n"));
471 	fd = open("/boot.cfg", 0);
472 	if (fd < 0) return;
473 	DPRINTF(("found /boot.cfg\n"));
474 	if (fstat(fd, &st) == -1 || st.st_size > 32*1024) {
475 		close(fd);
476 		return;
477 	}
478 	bc = alloc(st.st_size+1);
479 	off = 0;
480 	do {
481 		len = read(fd, bc+off, 1024);
482 		if (len <= 0)
483 			break;
484 		off += len;
485 	} while (len > 0);
486 	bc[off] = 0;
487 	close(fd);
488 
489 	parse_boot_config(bc, off);
490 }
491 
492 void
493 main(void *ofw)
494 {
495 	int boothowto, i = 0, isfloppy;
496 
497 	char kernel[PROM_MAX_PATH];
498 	char bootline[PROM_MAX_PATH];
499 
500 	/* Initialize OpenFirmware */
501 	romp = ofw;
502 	prom_init();
503 
504 	printf("\r>> %s, Revision %s\n", bootprog_name, bootprog_rev);
505 	DPRINTF((">> (%s, %s)\n", bootprog_maker, bootprog_date));
506 
507 	/* Figure boot arguments */
508 	strncpy(bootdev, prom_getbootpath(), sizeof(bootdev) - 1);
509 	boothowto = bootoptions(prom_getbootargs(), bootdev, kernel, bootline);
510 	isfloppy = bootdev_isfloppy(bootdev);
511 
512 	for (;; *kernel = '\0') {
513 		if (boothowto & RB_ASKNAME) {
514 			char *cp, cmdline[PROM_MAX_PATH];
515 
516 			printf("Boot: ");
517 			gets(cmdline);
518 
519 			if (!strcmp(cmdline,"exit") ||
520 			    !strcmp(cmdline,"halt")) {
521 				prom_halt();
522 			} else if (!strcmp(cmdline, "?") ||
523 				   !strcmp(cmdline, "help")) {
524 				help();
525 				continue;
526 			}
527 
528 			boothowto  = bootoptions(cmdline, bootdev, kernel,
529 			    bootline);
530 			boothowto |= RB_ASKNAME;
531 			i = 0;
532 		}
533 
534 		if (*kernel == '\0') {
535 			if (kernelnames[i] == NULL) {
536 				boothowto |= RB_ASKNAME;
537 				continue;
538 			}
539 			strncpy(kernel, kernelnames[i++], PROM_MAX_PATH);
540 		} else if (i == 0) {
541 			/*
542 			 * Kernel name was passed via command line -- ask user
543 			 * again if requested image fails to boot.
544 			 */
545 			boothowto |= RB_ASKNAME;
546 		}
547 
548 		check_boot_config();
549 		start_kernel(kernel, bootline, ofw, isfloppy);
550 
551 		/*
552 		 * Try next name from kernel name list if not in askname mode,
553 		 * enter askname on reaching list's end.
554 		 */
555 		if ((boothowto & RB_ASKNAME) == 0 && (kernelnames[i] != NULL)) {
556 			printf(": trying %s...\n", kernelnames[i]);
557 		} else {
558 			printf("\n");
559 			boothowto |= RB_ASKNAME;
560 		}
561 	}
562 
563 	(void)printf("Boot failed! Exiting to the Firmware.\n");
564 	prom_halt();
565 }
566