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