xref: /netbsd-src/sys/arch/sparc/stand/ofwboot/boot.c (revision 1b9578b8c2c1f848eeb16dabbfd7d1f0d9fdefbd)
1 /*	$NetBSD: boot.c,v 1.28 2011/05/21 15:50:42 tsutsui 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 extern char twiddle_toggle;
99 
100 #if 0
101 static void
102 prom2boot(char *dev)
103 {
104 	char *cp, *lp = 0;
105 	int handle;
106 	char devtype[16];
107 
108 	for (cp = dev; *cp; cp++)
109 		if (*cp == ':')
110 			lp = cp;
111 	if (!lp)
112 		lp = cp;
113 	*lp = 0;
114 }
115 #endif
116 
117 static int
118 bootoptions(const char *ap, char *loaddev, char *kernel, char *options)
119 {
120 	int v = 0;
121 	const char *start1 = NULL, *end1 = NULL, *start2 = NULL, *end2 = NULL;
122 	const char *path;
123 	char partition, *pp;
124 
125 	*kernel  = '\0';
126 	*options = '\0';
127 
128 	if (ap == NULL) {
129 		return (0);
130 	}
131 
132 	while (*ap == ' ') {
133 		ap++;
134 	}
135 
136 	if (*ap != '-') {
137 		start1 = ap;
138 		while (*ap != '\0' && *ap != ' ') {
139 			ap++;
140 		}
141 		end1 = ap;
142 
143 		while (*ap != '\0' && *ap == ' ') {
144 			ap++;
145 		}
146 
147 		if (*ap != '-') {
148 			start2 = ap;
149 			while (*ap != '\0' && *ap != ' ') {
150 				ap++;
151 			}
152 			end2 = ap;
153 			while (*ap != '\0' && *ap == ' ') {
154 				ap++;
155 			}
156 		}
157 	}
158 	if (end2 == start2) {
159 		start2 = end2 = NULL;
160 	}
161 	if (end1 == start1) {
162 		start1 = end1 = NULL;
163 	}
164 
165 	if (start1 == NULL) {
166 		/* only options */
167 	} else if (start2 == NULL) {
168 		memcpy(kernel, start1, (end1 - start1));
169 		kernel[end1 - start1] = '\0';
170 		path = filename(kernel, &partition);
171 		if (path == NULL) {
172 			strcpy(loaddev, kernel);
173 			kernel[0] = '\0';
174 		} else if (path != kernel) {
175 			/* copy device part */
176 			memcpy(loaddev, kernel, path-kernel);
177 			loaddev[path-kernel] = '\0';
178 			if (partition) {
179 				pp = loaddev + strlen(loaddev);
180 				pp[0] = ':';
181 				pp[1] = partition;
182 				pp[2] = '\0';
183 			}
184 			/* and kernel path */
185 			strcpy(kernel, path);
186 		}
187 	} else {
188 		memcpy(loaddev, start1, (end1-start1));
189 		loaddev[end1-start1] = '\0';
190 		memcpy(kernel, start2, (end2 - start2));
191 		kernel[end2 - start2] = '\0';
192 	}
193 
194 	twiddle_toggle = 1;
195 	strcpy(options, ap);
196 	while (*ap != '\0' && *ap != ' ' && *ap != '\t' && *ap != '\n') {
197 		BOOT_FLAG(*ap, v);
198 		switch(*ap++) {
199 		case 'D':
200 			debug = 2;
201 			break;
202 		case 'C':
203 			compatmode = 1;
204 			break;
205 		case 'T':
206 			twiddle_toggle = 1 - twiddle_toggle;
207 			break;
208 		default:
209 			break;
210 		}
211 	}
212 
213 	if (((v & RB_KDB) != 0) && (debug == 0)) {
214 		debug = 1;
215 	}
216 
217 	DPRINTF(("bootoptions: device='%s', kernel='%s', options='%s'\n",
218 	    loaddev, kernel, options));
219 	return (v);
220 }
221 
222 /*
223  * The older (those relying on ofwboot v1.8 and earlier) kernels can't handle
224  * ksyms information unless it resides in a dedicated memory allocated from
225  * PROM and aligned on NBPG boundary. This is because the kernels calculate
226  * their ends on their own, they use address of 'end[]' reference which follows
227  * text segment. Ok, allocate some memory from PROM and copy symbol information
228  * over there.
229  */
230 static void
231 ksyms_copyout(void **ssym, void **esym)
232 {
233 	uint8_t *addr;
234 	int kssize = (int)(long)((char *)*esym - (char *)*ssym + 1);
235 
236 	DPRINTF(("ksyms_copyout(): ssym = %p, esym = %p, kssize = %d\n",
237 				*ssym, *esym, kssize));
238 
239 	if ( (addr = OF_claim(0, kssize, NBPG)) == (void *)-1) {
240 		panic("ksyms_copyout(): no space for symbol table");
241 	}
242 
243 	memcpy(addr, *ssym, kssize);
244 	*ssym = addr;
245 	*esym = addr + kssize - 1;
246 
247 	DPRINTF(("ksyms_copyout(): ssym = %p, esym = %p\n", *ssym, *esym));
248 }
249 
250 /*
251  * Prepare boot information and jump directly to the kernel.
252  */
253 static void
254 jump_to_kernel(u_long *marks, char *kernel, char *args, void *ofw)
255 {
256 	int l, machine_tag;
257 	long newargs[4];
258 	void *ssym, *esym;
259 	vaddr_t bootinfo;
260 	struct btinfo_symtab bi_sym;
261 	struct btinfo_kernend bi_kend;
262 	char *cp;
263 	char bootline[PROM_MAX_PATH * 2];
264 
265 	/* Compose kernel boot line. */
266 	strncpy(bootline, kernel, sizeof(bootline));
267 	cp = bootline + strlen(bootline);
268 	if (*args) {
269 		*cp++ = ' ';
270 		strncpy(bootline, args, sizeof(bootline) - (cp - bootline));
271 	}
272 	*cp = 0; args = bootline;
273 
274 	/* Record symbol information in the bootinfo. */
275 	bootinfo = bi_init(marks[MARK_END]);
276 	bi_sym.nsym = marks[MARK_NSYM];
277 	bi_sym.ssym = marks[MARK_SYM];
278 	bi_sym.esym = marks[MARK_END];
279 	bi_add(&bi_sym, BTINFO_SYMTAB, sizeof(bi_sym));
280 	bi_kend.addr= bootinfo + BOOTINFO_SIZE;
281 	bi_add(&bi_kend, BTINFO_KERNEND, sizeof(bi_kend));
282 	if (bootinfo_pass_bootdev) {
283 		struct {
284 			struct btinfo_common common;
285 			char name[256];
286 		} info;
287 
288 		strcpy(info.name, bootdev);
289 		bi_add(&info, BTINFO_BOOTDEV, strlen(bootdev)
290 			+sizeof(struct btinfo_bootdev));
291 	}
292 
293 	sparc64_finalize_tlb(marks[MARK_DATA]);
294 	sparc64_bi_add();
295 
296 	ssym  = (void*)(long)marks[MARK_SYM];
297 	esym  = (void*)(long)marks[MARK_END];
298 
299 	DPRINTF(("jump_to_kernel(): ssym = %p, esym = %p\n", ssym, esym));
300 
301 	/* Adjust ksyms pointers, if needed. */
302 	if (COMPAT_BOOT(marks) || compatmode) {
303 		ksyms_copyout(&ssym, &esym);
304 	}
305 
306 	freeall();
307 	/*
308 	 * When we come in args consists of a pointer to the boot
309 	 * string.  We need to fix it so it takes into account
310 	 * other params such as romp.
311 	 */
312 
313 	/*
314 	 * Stash pointer to end of symbol table after the argument
315 	 * strings.
316 	 */
317 	l = strlen(args) + 1;
318 	memcpy(args + l, &esym, sizeof(esym));
319 	l += sizeof(esym);
320 
321 	/*
322 	 * Tell the kernel we're an OpenFirmware system.
323 	 */
324 	machine_tag = SPARC_MACHINE_OPENFIRMWARE;
325 	memcpy(args + l, &machine_tag, sizeof(machine_tag));
326 	l += sizeof(machine_tag);
327 
328 	/*
329 	 * Since we don't need the boot string (we can get it from /chosen)
330 	 * we won't pass it in.  Just pass in esym and magic #
331 	 */
332 	newargs[0] = SPARC_MACHINE_OPENFIRMWARE;
333 	newargs[1] = (long)esym;
334 	newargs[2] = (long)ssym;
335 	newargs[3] = (long)(void*)bootinfo;
336 	args = (char *)newargs;
337 	l = sizeof(newargs);
338 
339 	/* if -D is set then pause in the PROM. */
340 	if (debug > 1) callrom();
341 
342 	/*
343 	 * Jump directly to the kernel. Solaris kernel and Sun PROM
344 	 * flash updates expect ROMP vector in %o0, so we do. Format
345 	 * of other parameters and their order reflect OF_chain()
346 	 * symantics since this is what older NetBSD kernels rely on.
347 	 * (see sparc64/include/bootinfo.h for specification).
348 	 */
349 	DPRINTF(("jump_to_kernel(%lx, %lx, %lx, %lx, %lx) @ %p\n", (long)ofw,
350 				(long)args, (long)l, (long)ofw, (long)ofw,
351 				(void*)marks[MARK_ENTRY]));
352 	(*(entry_t)marks[MARK_ENTRY])((long)ofw, (long)args, (long)l, (long)ofw,
353 				      (long)ofw);
354 	printf("Returned from kernel entry point!\n");
355 }
356 
357 static void
358 start_kernel(char *kernel, char *bootline, void *ofw, int isfloppy)
359 {
360 	int fd;
361 	u_long marks[MARK_MAX];
362 	int flags = LOAD_ALL;
363 	if (isfloppy)
364 		flags &= ~LOAD_BACKWARDS;
365 
366 	/*
367 	 * First, load headers using default allocator and check whether kernel
368 	 * entry address matches kernel text load address. If yes, this is the
369 	 * old kernel designed for ofwboot v1.8 and therefore it must be mapped
370 	 * by PROM. Otherwise, map the kernel with 4MB permanent pages.
371 	 */
372 	loadfile_set_allocator(LOADFILE_NOP_ALLOCATOR);
373 	if ( (fd = loadfile(kernel, marks, LOAD_HDR|COUNT_TEXT)) != -1) {
374 		if (COMPAT_BOOT(marks) || compatmode) {
375 			(void)printf("[c] ");
376 			loadfile_set_allocator(LOADFILE_OFW_ALLOCATOR);
377 		} else {
378 			loadfile_set_allocator(LOADFILE_MMU_ALLOCATOR);
379 		}
380 		(void)printf("Loading %s: ", kernel);
381 
382 		if (fdloadfile(fd, marks, flags) != -1) {
383 			close(fd);
384 			jump_to_kernel(marks, kernel, bootline, ofw);
385 		}
386 	}
387 	(void)printf("Failed to load '%s'.\n", kernel);
388 }
389 
390 static void
391 help(void)
392 {
393 	printf( "enter a special command\n"
394 		"  halt\n"
395 		"  exit\n"
396 		"    to return to OpenFirmware\n"
397 		"  ?\n"
398 		"  help\n"
399 		"    to display this message\n"
400 		"or a boot specification:\n"
401 		"  [device] [kernel] [options]\n"
402 		"\n"
403 		"for example:\n"
404 		"  disk:a netbsd -s\n");
405 }
406 
407 static void
408 do_config_command(const char *cmd, const char *arg)
409 {
410 	DPRINTF(("do_config_command: %s\n", cmd));
411 	if (strcmp(cmd, "bootpartition") == 0) {
412 		char *c;
413 
414 		DPRINTF(("switching boot partition to %s from %s\n",
415 		    arg, bootdev));
416 		c = strrchr(bootdev, ':');
417 		if (!c) return;
418 		if (c[1] == 0) return;
419 		if (strlen(arg) > strlen(c)) return;
420 		strcpy(c, arg);
421 		DPRINTF(("new boot device: %s\n", bootdev));
422 		bootinfo_pass_bootdev = true;
423 	}
424 }
425 
426 static void
427 parse_boot_config(char *cfg, size_t len)
428 {
429 	const char *cmd = NULL, *arg = NULL;
430 
431 	while (len) {
432 		if (isspace(*cfg)) {
433 			cfg++; len--; continue;
434 		}
435 		if (*cfg == ';' || *cfg == '#') {
436 			while (len && *cfg != '\r' && *cfg != '\n') {
437 				cfg++; len--;
438 			}
439 			continue;
440 		}
441 		cmd = cfg;
442 		while (len && !isspace(*cfg)) {
443 			cfg++; len--;
444 		}
445 		*cfg = 0;
446 		if (len > 0) {
447 			cfg++; len--;
448 			while (isspace(*cfg) && len) {
449 				cfg++; len--;
450 			}
451 			if (len > 0 ) {
452 				arg = cfg;
453 				while (len && !isspace(*cfg)) {
454 					cfg++; len--;
455 				}
456 				*cfg = 0;
457 			}
458 		}
459 		do_config_command(cmd, arg);
460 		if (len > 0) {
461 			cfg++; len--;
462 		}
463 	}
464 }
465 
466 static void
467 check_boot_config(void)
468 {
469 	int fd, off, len;
470 	struct stat st;
471 	char *bc;
472 
473 	if (!root_fs_quickseekable) return;
474 	DPRINTF(("checking for /boot.cfg...\n"));
475 	fd = open("/boot.cfg", 0);
476 	if (fd < 0) return;
477 	DPRINTF(("found /boot.cfg\n"));
478 	if (fstat(fd, &st) == -1 || st.st_size > 32*1024) {
479 		close(fd);
480 		return;
481 	}
482 	bc = alloc(st.st_size+1);
483 	off = 0;
484 	do {
485 		len = read(fd, bc+off, 1024);
486 		if (len <= 0)
487 			break;
488 		off += len;
489 	} while (len > 0);
490 	bc[off] = 0;
491 	close(fd);
492 
493 	parse_boot_config(bc, off);
494 }
495 
496 void
497 main(void *ofw)
498 {
499 	int boothowto, i = 0, isfloppy;
500 
501 	char kernel[PROM_MAX_PATH];
502 	char bootline[PROM_MAX_PATH];
503 
504 	/* Initialize OpenFirmware */
505 	romp = ofw;
506 	prom_init();
507 
508 	printf("\r>> %s, Revision %s\n", bootprog_name, bootprog_rev);
509 
510 	/* Figure boot arguments */
511 	strncpy(bootdev, prom_getbootpath(), sizeof(bootdev) - 1);
512 	boothowto = bootoptions(prom_getbootargs(), bootdev, kernel, bootline);
513 	isfloppy = bootdev_isfloppy(bootdev);
514 
515 	for (;; *kernel = '\0') {
516 		if (boothowto & RB_ASKNAME) {
517 			char cmdline[PROM_MAX_PATH];
518 
519 			printf("Boot: ");
520 			gets(cmdline);
521 
522 			if (!strcmp(cmdline,"exit") ||
523 			    !strcmp(cmdline,"halt")) {
524 				prom_halt();
525 			} else if (!strcmp(cmdline, "?") ||
526 				   !strcmp(cmdline, "help")) {
527 				help();
528 				continue;
529 			}
530 
531 			boothowto  = bootoptions(cmdline, bootdev, kernel,
532 			    bootline);
533 			boothowto |= RB_ASKNAME;
534 			i = 0;
535 		}
536 
537 		if (*kernel == '\0') {
538 			if (kernelnames[i] == NULL) {
539 				boothowto |= RB_ASKNAME;
540 				continue;
541 			}
542 			strncpy(kernel, kernelnames[i++], PROM_MAX_PATH);
543 		} else if (i == 0) {
544 			/*
545 			 * Kernel name was passed via command line -- ask user
546 			 * again if requested image fails to boot.
547 			 */
548 			boothowto |= RB_ASKNAME;
549 		}
550 
551 		check_boot_config();
552 		start_kernel(kernel, bootline, ofw, isfloppy);
553 
554 		/*
555 		 * Try next name from kernel name list if not in askname mode,
556 		 * enter askname on reaching list's end.
557 		 */
558 		if ((boothowto & RB_ASKNAME) == 0 && (kernelnames[i] != NULL)) {
559 			printf(": trying %s...\n", kernelnames[i]);
560 		} else {
561 			printf("\n");
562 			boothowto |= RB_ASKNAME;
563 		}
564 	}
565 
566 	(void)printf("Boot failed! Exiting to the Firmware.\n");
567 	prom_halt();
568 }
569