Home | History | Annotate | Line # | Download | only in boot
boot.c revision 1.10.8.4
      1  1.10.8.4  thorpej /*	$NetBSD: boot.c,v 1.10.8.4 2002/12/11 15:53:53 thorpej Exp $ */
      2  1.10.8.2  nathanw 
      3  1.10.8.2  nathanw /*-
      4  1.10.8.2  nathanw  * Copyright (c) 1982, 1986, 1990, 1993
      5  1.10.8.2  nathanw  *	The Regents of the University of California.  All rights reserved.
      6  1.10.8.2  nathanw  *
      7  1.10.8.2  nathanw  * Redistribution and use in source and binary forms, with or without
      8  1.10.8.2  nathanw  * modification, are permitted provided that the following conditions
      9  1.10.8.2  nathanw  * are met:
     10  1.10.8.2  nathanw  * 1. Redistributions of source code must retain the above copyright
     11  1.10.8.2  nathanw  *    notice, this list of conditions and the following disclaimer.
     12  1.10.8.2  nathanw  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.10.8.2  nathanw  *    notice, this list of conditions and the following disclaimer in the
     14  1.10.8.2  nathanw  *    documentation and/or other materials provided with the distribution.
     15  1.10.8.2  nathanw  * 3. All advertising materials mentioning features or use of this software
     16  1.10.8.2  nathanw  *    must display the following acknowledgement:
     17  1.10.8.2  nathanw  *	This product includes software developed by the University of
     18  1.10.8.2  nathanw  *	California, Berkeley and its contributors.
     19  1.10.8.2  nathanw  * 4. Neither the name of the University nor the names of its contributors
     20  1.10.8.2  nathanw  *    may be used to endorse or promote products derived from this software
     21  1.10.8.2  nathanw  *    without specific prior written permission.
     22  1.10.8.2  nathanw  *
     23  1.10.8.2  nathanw  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  1.10.8.2  nathanw  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  1.10.8.2  nathanw  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  1.10.8.2  nathanw  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  1.10.8.2  nathanw  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  1.10.8.2  nathanw  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  1.10.8.2  nathanw  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  1.10.8.2  nathanw  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  1.10.8.2  nathanw  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  1.10.8.2  nathanw  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  1.10.8.2  nathanw  * SUCH DAMAGE.
     34  1.10.8.2  nathanw  *
     35  1.10.8.2  nathanw  * 	@(#)boot.c	8.1 (Berkeley) 6/10/93
     36  1.10.8.2  nathanw  */
     37  1.10.8.2  nathanw 
     38  1.10.8.2  nathanw #include <sys/param.h>
     39  1.10.8.2  nathanw #include <sys/reboot.h>
     40  1.10.8.3  nathanw #include <sys/boot_flag.h>
     41  1.10.8.2  nathanw #include <sys/exec.h>
     42  1.10.8.2  nathanw 
     43  1.10.8.2  nathanw #include <lib/libsa/stand.h>
     44  1.10.8.2  nathanw #include <lib/libsa/loadfile.h>
     45  1.10.8.2  nathanw 
     46  1.10.8.2  nathanw #include <machine/promlib.h>
     47  1.10.8.2  nathanw #include <sparc/stand/common/promdev.h>
     48  1.10.8.2  nathanw 
     49  1.10.8.2  nathanw #include "bootinfo.h"
     50  1.10.8.2  nathanw 
     51  1.10.8.2  nathanw extern void	prom_patch __P((void));	/* prompatch.c */
     52  1.10.8.2  nathanw 
     53  1.10.8.2  nathanw static int	bootoptions __P((char *));
     54  1.10.8.2  nathanw #if 0
     55  1.10.8.2  nathanw static void	promsyms __P((int, struct exec *));
     56  1.10.8.2  nathanw #endif
     57  1.10.8.2  nathanw 
     58  1.10.8.2  nathanw int debug;
     59  1.10.8.2  nathanw int netif_debug;
     60  1.10.8.2  nathanw 
     61  1.10.8.2  nathanw extern char bootprog_name[], bootprog_rev[], bootprog_date[], bootprog_maker[];
     62  1.10.8.2  nathanw unsigned long		esym;
     63  1.10.8.2  nathanw char			*strtab;
     64  1.10.8.2  nathanw int			strtablen;
     65  1.10.8.2  nathanw char			fbuf[80], dbuf[128];
     66  1.10.8.2  nathanw 
     67  1.10.8.2  nathanw int	main __P((void));
     68  1.10.8.2  nathanw typedef void (*entry_t)__P((caddr_t, int, int, int, long, long));
     69  1.10.8.2  nathanw 
     70  1.10.8.2  nathanw /*
     71  1.10.8.2  nathanw  * Boot device is derived from ROM provided information, or if there is none,
     72  1.10.8.2  nathanw  * this list is used in sequence, to find a kernel.
     73  1.10.8.2  nathanw  */
     74  1.10.8.2  nathanw char *kernels[] = {
     75  1.10.8.2  nathanw 	"netbsd",
     76  1.10.8.2  nathanw 	"netbsd.gz",
     77  1.10.8.2  nathanw 	"netbsd.old",
     78  1.10.8.2  nathanw 	"netbsd.old.gz",
     79  1.10.8.2  nathanw 	"onetbsd",
     80  1.10.8.2  nathanw 	"onetbsd.gz",
     81  1.10.8.2  nathanw 	"vmunix",
     82  1.10.8.2  nathanw #ifdef notyet
     83  1.10.8.2  nathanw 	"netbsd.pl",
     84  1.10.8.2  nathanw 	"netbsd.pl.gz",
     85  1.10.8.2  nathanw 	"netbsd.el",
     86  1.10.8.2  nathanw 	"netbsd.el.gz",
     87  1.10.8.2  nathanw #endif
     88  1.10.8.2  nathanw 	NULL
     89  1.10.8.2  nathanw };
     90  1.10.8.2  nathanw 
     91  1.10.8.2  nathanw int
     92  1.10.8.2  nathanw bootoptions(ap)
     93  1.10.8.2  nathanw 	char *ap;
     94  1.10.8.2  nathanw {
     95  1.10.8.2  nathanw 	int v = 0;
     96  1.10.8.2  nathanw 	if (ap == NULL || *ap++ != '-')
     97  1.10.8.2  nathanw 		return (0);
     98  1.10.8.2  nathanw 
     99  1.10.8.2  nathanw 	while (*ap != '\0' && *ap != ' ' && *ap != '\t' && *ap != '\n') {
    100  1.10.8.3  nathanw 		BOOT_FLAG(*ap, v);
    101  1.10.8.2  nathanw 		ap++;
    102  1.10.8.2  nathanw 	}
    103  1.10.8.2  nathanw 
    104  1.10.8.3  nathanw 	if ((v & RB_KDB) != 0)
    105  1.10.8.3  nathanw 		debug = 1;
    106  1.10.8.3  nathanw 
    107  1.10.8.2  nathanw 	return (v);
    108  1.10.8.2  nathanw }
    109  1.10.8.2  nathanw 
    110  1.10.8.2  nathanw int
    111  1.10.8.2  nathanw main()
    112  1.10.8.2  nathanw {
    113  1.10.8.2  nathanw 	int	io, i;
    114  1.10.8.2  nathanw 	char	*kernel;
    115  1.10.8.2  nathanw 	int	how;
    116  1.10.8.2  nathanw 	u_long	marks[MARK_MAX], bootinfo;
    117  1.10.8.2  nathanw 	struct btinfo_symtab bi_sym;
    118  1.10.8.2  nathanw 	void	*arg;
    119  1.10.8.4  thorpej 	u_long	maxkernsize;
    120  1.10.8.2  nathanw 
    121  1.10.8.2  nathanw #ifdef HEAP_VARIABLE
    122  1.10.8.2  nathanw 	{
    123  1.10.8.2  nathanw 		extern char end[];
    124  1.10.8.2  nathanw 		setheap((void *)ALIGN(end), (void *)0xffffffff);
    125  1.10.8.2  nathanw 	}
    126  1.10.8.2  nathanw #endif
    127  1.10.8.4  thorpej 	{
    128  1.10.8.4  thorpej 		/*
    129  1.10.8.4  thorpej 		 * Find maximum the kernel size that we can handle.
    130  1.10.8.4  thorpej 		 * Our stack grows down from label `start'; assume
    131  1.10.8.4  thorpej 		 * we need no more that 16K of stack space.
    132  1.10.8.4  thorpej 		 */
    133  1.10.8.4  thorpej 		extern char start[];	/* top of stack (see srt0.S) */
    134  1.10.8.4  thorpej 		maxkernsize = (u_long)start - (16*1024) - PROM_LOADADDR;
    135  1.10.8.4  thorpej 
    136  1.10.8.4  thorpej 	}
    137  1.10.8.2  nathanw 	prom_init();
    138  1.10.8.2  nathanw 
    139  1.10.8.2  nathanw 	printf(">> %s, Revision %s\n", bootprog_name, bootprog_rev);
    140  1.10.8.2  nathanw 	printf(">> (%s, %s)\n", bootprog_maker, bootprog_date);
    141  1.10.8.2  nathanw 
    142  1.10.8.2  nathanw 	/* massage machine prom */
    143  1.10.8.2  nathanw 	prom_patch();
    144  1.10.8.2  nathanw 
    145  1.10.8.2  nathanw 	/*
    146  1.10.8.2  nathanw 	 * get default kernel.
    147  1.10.8.2  nathanw 	 */
    148  1.10.8.2  nathanw 	prom_bootdevice = prom_getbootpath();
    149  1.10.8.2  nathanw 	kernel = prom_getbootfile();
    150  1.10.8.2  nathanw 	how = bootoptions(prom_getbootargs());
    151  1.10.8.2  nathanw 
    152  1.10.8.2  nathanw 	if (kernel != NULL && *kernel != '\0') {
    153  1.10.8.2  nathanw 		i = -1;	/* not using the kernels */
    154  1.10.8.2  nathanw 	} else {
    155  1.10.8.2  nathanw 		i = 0;
    156  1.10.8.2  nathanw 		kernel = kernels[i];
    157  1.10.8.2  nathanw 	}
    158  1.10.8.2  nathanw 
    159  1.10.8.2  nathanw 	for (;;) {
    160  1.10.8.2  nathanw 		/*
    161  1.10.8.2  nathanw 		 * ask for a kernel first ..
    162  1.10.8.2  nathanw 		 */
    163  1.10.8.2  nathanw 		if (how & RB_ASKNAME) {
    164  1.10.8.2  nathanw 			printf("device[%s] (\"halt\" to halt): ",
    165  1.10.8.2  nathanw 					prom_bootdevice);
    166  1.10.8.2  nathanw 			gets(dbuf);
    167  1.10.8.2  nathanw 			if (strcmp(dbuf, "halt") == 0)
    168  1.10.8.2  nathanw 				_rtt();
    169  1.10.8.2  nathanw 			if (dbuf[0])
    170  1.10.8.2  nathanw 				prom_bootdevice = dbuf;
    171  1.10.8.2  nathanw 			printf("boot (press RETURN to try default list): ");
    172  1.10.8.2  nathanw 			gets(fbuf);
    173  1.10.8.2  nathanw 			if (fbuf[0])
    174  1.10.8.2  nathanw 				kernel = fbuf;
    175  1.10.8.2  nathanw 			else {
    176  1.10.8.2  nathanw 				how &= ~RB_ASKNAME;
    177  1.10.8.2  nathanw 				i = 0;
    178  1.10.8.2  nathanw 				kernel = kernels[i];
    179  1.10.8.2  nathanw 			}
    180  1.10.8.2  nathanw 		}
    181  1.10.8.2  nathanw 
    182  1.10.8.2  nathanw 		marks[MARK_START] = 0;
    183  1.10.8.2  nathanw 		printf("Booting %s\n", kernel);
    184  1.10.8.4  thorpej 		if ((io = loadfile(kernel, marks, COUNT_KERNEL)) != -1) {
    185  1.10.8.4  thorpej 			close(io);
    186  1.10.8.4  thorpej 			if (marks[MARK_END] - marks[MARK_START] > maxkernsize) {
    187  1.10.8.4  thorpej 				printf("kernel too large: %lu"
    188  1.10.8.4  thorpej 					" (maximum kernel size is %lu)\n",
    189  1.10.8.4  thorpej 					marks[MARK_END] - marks[MARK_START],
    190  1.10.8.4  thorpej 					maxkernsize);
    191  1.10.8.4  thorpej 				how |= RB_ASKNAME;
    192  1.10.8.4  thorpej 				continue;
    193  1.10.8.4  thorpej 			}
    194  1.10.8.4  thorpej 			marks[MARK_START] = 0;
    195  1.10.8.4  thorpej 			if ((io = loadfile(kernel, marks, LOAD_KERNEL)) != -1) {
    196  1.10.8.4  thorpej 				close(io);
    197  1.10.8.4  thorpej 				break;
    198  1.10.8.4  thorpej 			}
    199  1.10.8.4  thorpej 		}
    200  1.10.8.4  thorpej 
    201  1.10.8.2  nathanw 		/*
    202  1.10.8.2  nathanw 		 * if we have are not in askname mode, and we aren't using the
    203  1.10.8.2  nathanw 		 * prom bootfile, try the next one (if it exits).  otherwise,
    204  1.10.8.2  nathanw 		 * go into askname mode.
    205  1.10.8.2  nathanw 		 */
    206  1.10.8.2  nathanw 		if ((how & RB_ASKNAME) == 0 &&
    207  1.10.8.2  nathanw 		    i != -1 && kernels[++i]) {
    208  1.10.8.2  nathanw 			kernel = kernels[i];
    209  1.10.8.2  nathanw 			printf(": trying %s...\n", kernel);
    210  1.10.8.2  nathanw 		} else {
    211  1.10.8.2  nathanw 			printf("\n");
    212  1.10.8.2  nathanw 			how |= RB_ASKNAME;
    213  1.10.8.2  nathanw 		}
    214  1.10.8.2  nathanw 	}
    215  1.10.8.2  nathanw 
    216  1.10.8.2  nathanw 	marks[MARK_END] = (((u_long)marks[MARK_END] + sizeof(int) - 1)) &
    217  1.10.8.2  nathanw 	    (-sizeof(int));
    218  1.10.8.2  nathanw 	arg = (prom_version() == PROM_OLDMON) ? (caddr_t)PROM_LOADADDR : romp;
    219  1.10.8.2  nathanw #if 0
    220  1.10.8.2  nathanw 	/* Old style cruft; works only with a.out */
    221  1.10.8.2  nathanw 	marks[MARK_END] |= 0xf0000000;
    222  1.10.8.2  nathanw 	(*(entry_t)marks[MARK_ENTRY])(arg, 0, 0, 0, marks[MARK_END],
    223  1.10.8.2  nathanw 	    DDB_MAGIC1);
    224  1.10.8.2  nathanw #else
    225  1.10.8.2  nathanw 	/* Should work with both a.out and ELF, but somehow ELF is busted */
    226  1.10.8.2  nathanw 	bootinfo = bi_init(marks[MARK_END]);
    227  1.10.8.2  nathanw 
    228  1.10.8.2  nathanw 	bi_sym.nsym = marks[MARK_NSYM];
    229  1.10.8.2  nathanw 	bi_sym.ssym = marks[MARK_SYM];
    230  1.10.8.2  nathanw 	bi_sym.esym = marks[MARK_END];
    231  1.10.8.2  nathanw 	bi_add(&bi_sym, BTINFO_SYMTAB, sizeof(bi_sym));
    232  1.10.8.2  nathanw 
    233  1.10.8.2  nathanw 	/*
    234  1.10.8.2  nathanw 	 * Add kernel path to bootinfo
    235  1.10.8.2  nathanw 	 */
    236  1.10.8.2  nathanw 	i = sizeof(struct btinfo_common) + strlen(kernel) + 1;
    237  1.10.8.2  nathanw 	/* Impose limit (somewhat arbitrary) */
    238  1.10.8.2  nathanw 	if (i < BOOTINFO_SIZE / 2) {
    239  1.10.8.2  nathanw 		union {
    240  1.10.8.2  nathanw 			struct btinfo_kernelfile bi_file;
    241  1.10.8.2  nathanw 			char x[i];
    242  1.10.8.2  nathanw 		} U;
    243  1.10.8.2  nathanw 		strcpy(U.bi_file.name, kernel);
    244  1.10.8.2  nathanw 		bi_add(&U.bi_file, BTINFO_KERNELFILE, i);
    245  1.10.8.2  nathanw 	}
    246  1.10.8.2  nathanw 
    247  1.10.8.2  nathanw 	(*(entry_t)marks[MARK_ENTRY])(arg, 0, 0, 0, bootinfo, DDB_MAGIC2);
    248  1.10.8.2  nathanw #endif
    249  1.10.8.2  nathanw 
    250  1.10.8.2  nathanw 	_rtt();
    251  1.10.8.2  nathanw }
    252