Home | History | Annotate | Line # | Download | only in boot
boot.c revision 1.17
      1  1.17       mrg /*	$NetBSD: boot.c,v 1.17 2003/07/30 15:58:39 mrg 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.17       mrg #include <lib/libkern/libkern.h>
     46   1.1       mrg 
     47   1.4        pk #include <machine/promlib.h>
     48   1.1       mrg #include <sparc/stand/common/promdev.h>
     49   1.1       mrg 
     50   1.6  christos #include "bootinfo.h"
     51   1.6  christos 
     52  1.10       uwe extern void	prom_patch __P((void));	/* prompatch.c */
     53  1.10       uwe 
     54   1.4        pk static int	bootoptions __P((char *));
     55   1.4        pk 
     56  1.14        pk int	boothowto;
     57  1.14        pk int	debug;
     58  1.14        pk int	netif_debug;
     59  1.14        pk 
     60  1.14        pk char	fbuf[80], dbuf[128];
     61  1.15        pk paddr_t bstart, bend;	/* physical start & end address of the boot program */
     62  1.15        pk 
     63  1.15        pk int	compatmode = 0;		/* For loading older kernels */
     64  1.15        pk u_long	loadaddrmask = -1UL;
     65   1.1       mrg 
     66   1.1       mrg extern char bootprog_name[], bootprog_rev[], bootprog_date[], bootprog_maker[];
     67   1.1       mrg 
     68   1.4        pk int	main __P((void));
     69   1.1       mrg typedef void (*entry_t)__P((caddr_t, int, int, int, long, long));
     70   1.1       mrg 
     71   1.3       mrg /*
     72   1.3       mrg  * Boot device is derived from ROM provided information, or if there is none,
     73   1.3       mrg  * this list is used in sequence, to find a kernel.
     74   1.3       mrg  */
     75   1.3       mrg char *kernels[] = {
     76   1.3       mrg 	"netbsd",
     77   1.3       mrg 	"netbsd.gz",
     78   1.3       mrg 	"netbsd.old",
     79   1.3       mrg 	"netbsd.old.gz",
     80   1.3       mrg 	"onetbsd",
     81   1.3       mrg 	"onetbsd.gz",
     82   1.3       mrg 	"vmunix",
     83   1.3       mrg #ifdef notyet
     84   1.3       mrg 	"netbsd.pl",
     85   1.3       mrg 	"netbsd.pl.gz",
     86   1.3       mrg 	"netbsd.el",
     87   1.3       mrg 	"netbsd.el.gz",
     88   1.3       mrg #endif
     89   1.3       mrg 	NULL
     90   1.3       mrg };
     91   1.3       mrg 
     92   1.4        pk int
     93   1.4        pk bootoptions(ap)
     94   1.4        pk 	char *ap;
     95   1.4        pk {
     96   1.4        pk 	int v = 0;
     97   1.4        pk 	if (ap == NULL || *ap++ != '-')
     98   1.4        pk 		return (0);
     99   1.4        pk 
    100   1.4        pk 	while (*ap != '\0' && *ap != ' ' && *ap != '\t' && *ap != '\n') {
    101  1.12       uwe 		BOOT_FLAG(*ap, v);
    102  1.16        pk 		if (*ap == 'C')
    103  1.16        pk 			compatmode = 1;
    104   1.4        pk 		ap++;
    105   1.4        pk 	}
    106  1.12       uwe 
    107  1.12       uwe 	if ((v & RB_KDB) != 0)
    108  1.12       uwe 		debug = 1;
    109   1.4        pk 
    110   1.4        pk 	return (v);
    111   1.4        pk }
    112   1.4        pk 
    113  1.15        pk static paddr_t getphysmem(u_long size)
    114  1.15        pk {
    115  1.15        pk 	struct	memarr *pmemarr;	/* physical memory regions */
    116  1.15        pk 	int	npmemarr;		/* number of entries in pmemarr */
    117  1.15        pk 	struct memarr *mp;
    118  1.15        pk 	int i;
    119  1.15        pk 	extern char start[];	/* top of stack (see srt0.S) */
    120  1.15        pk 
    121  1.15        pk 	/*
    122  1.15        pk 	 * Find the physical memory area that's in use by the boot loader.
    123  1.15        pk 	 * Our stack grows down from label `start'; assume we need no more
    124  1.15        pk 	 * than 16K of stack space.
    125  1.15        pk 	 * The top of the boot loader is the next 4MB boundary.
    126  1.15        pk 	 */
    127  1.15        pk 	if (pmap_extract((vaddr_t)start - (16*1024), &bstart) != 0)
    128  1.15        pk 		return ((paddr_t)-1);
    129  1.15        pk 
    130  1.15        pk 	bend = roundup(bstart, 0x400000);
    131  1.15        pk 
    132  1.15        pk 	/*
    133  1.15        pk 	 * Get available physical memory from the prom.
    134  1.15        pk 	 */
    135  1.15        pk 	npmemarr = prom_makememarr(NULL, 0, MEMARR_AVAILPHYS);
    136  1.15        pk 	pmemarr = alloc(npmemarr*sizeof(struct memarr));
    137  1.15        pk 	if (pmemarr == NULL)
    138  1.15        pk 		return ((paddr_t)-1);
    139  1.15        pk 	npmemarr = prom_makememarr(pmemarr, npmemarr, MEMARR_AVAILPHYS);
    140  1.15        pk 
    141  1.15        pk 	/*
    142  1.15        pk 	 * Find a suitable loading address.
    143  1.15        pk 	 */
    144  1.15        pk 	for (mp = pmemarr, i = npmemarr; --i >= 0; mp++) {
    145  1.15        pk 		paddr_t pa = (paddr_t)pmemarr[i].addr;
    146  1.15        pk 		u_long len = (u_long)pmemarr[i].len;
    147  1.15        pk 
    148  1.15        pk 		/* Check whether it will fit in front of us */
    149  1.15        pk 		if (pa < bstart && len >= size && (bstart - pa) >= size)
    150  1.15        pk 			return (pa);
    151  1.15        pk 
    152  1.15        pk 		/* Skip the boot program memory */
    153  1.15        pk 		if (pa < bend) {
    154  1.15        pk 			if (len < bend - pa)
    155  1.15        pk 				/* Not large enough */
    156  1.15        pk 				continue;
    157  1.15        pk 
    158  1.15        pk 			/* Shrink this segment */
    159  1.15        pk 			len -=  bend - pa;
    160  1.15        pk 			pa = bend;
    161  1.15        pk 		}
    162  1.15        pk 
    163  1.15        pk 		/* Does it fit in the remainder of this segment? */
    164  1.15        pk 		if (len >= size)
    165  1.15        pk 			return (pa);
    166  1.15        pk 	}
    167  1.15        pk 	return ((paddr_t)-1);
    168  1.15        pk }
    169  1.15        pk 
    170  1.14        pk static int
    171  1.14        pk loadk(char *kernel, u_long *marks)
    172  1.14        pk {
    173  1.14        pk 	int fd, error;
    174  1.15        pk 	vaddr_t va;
    175  1.15        pk 	paddr_t pa;
    176  1.14        pk 	u_long size;
    177  1.14        pk 
    178  1.14        pk 	if ((fd = open(kernel, 0)) < 0)
    179  1.14        pk 		return (errno ? errno : ENOENT);
    180  1.14        pk 
    181  1.14        pk 	marks[MARK_START] = 0;
    182  1.14        pk 	if ((error = fdloadfile(fd, marks, COUNT_KERNEL)) != 0)
    183  1.14        pk 		goto out;
    184  1.14        pk 
    185  1.14        pk 	size = marks[MARK_END] - marks[MARK_START];
    186  1.15        pk 
    187  1.16        pk 	/* We want that leading 16K in front of the kernel image */
    188  1.15        pk 	size += PROM_LOADADDR;
    189  1.15        pk 	va = marks[MARK_START] - PROM_LOADADDR;
    190  1.15        pk 
    191  1.16        pk 	/*
    192  1.16        pk 	 * Extra space for bootinfo and kernel bootstrap.
    193  1.16        pk 	 * In compat mode, we get to re-use the space occupied by the
    194  1.16        pk 	 * boot program. Traditionally, we've silently assumed that
    195  1.16        pk 	 * is enough for the kernel to work with.
    196  1.16        pk 	 */
    197  1.16        pk 	size += BOOTINFO_SIZE;
    198  1.16        pk 	if (!compatmode)
    199  1.16        pk 		size += 512 * 1024;
    200  1.15        pk 
    201  1.15        pk 	/* Get a physical load address */
    202  1.15        pk 	pa = getphysmem(size);
    203  1.15        pk 	if (pa == (paddr_t)-1) {
    204  1.14        pk 		error = EFBIG;
    205  1.14        pk 		goto out;
    206  1.14        pk 	}
    207  1.14        pk 
    208  1.15        pk 	if (boothowto & AB_VERBOSE)
    209  1.15        pk 		printf("Loading at physical address %lx\n", pa);
    210  1.15        pk 	if (pmap_map(va, pa, size) != 0) {
    211  1.15        pk 		error = EFAULT;
    212  1.15        pk 		goto out;
    213  1.15        pk 	}
    214  1.15        pk 
    215  1.15        pk 	/* XXX - to do: inspect kernel image and set compat mode */
    216  1.15        pk 	if (compatmode) {
    217  1.16        pk 		/* Double-map at VA 0 for compatibility */
    218  1.16        pk 		if (pa + size >= bstart) {
    219  1.16        pk 			printf("%s: too large for compat mode\n", kernel);
    220  1.16        pk 			error = EFBIG;
    221  1.16        pk 			goto out;
    222  1.16        pk 		}
    223  1.16        pk 
    224  1.16        pk 		if (pmap_map(0, pa, size) != 0) {
    225  1.16        pk 			error = EFAULT;
    226  1.16        pk 			goto out;
    227  1.16        pk 		}
    228  1.15        pk 		loadaddrmask = 0x07ffffffUL;
    229  1.15        pk 	}
    230  1.15        pk 
    231  1.14        pk 	marks[MARK_START] = 0;
    232  1.14        pk 	error = fdloadfile(fd, marks, LOAD_KERNEL);
    233  1.14        pk out:
    234  1.14        pk 	close(fd);
    235  1.14        pk 	return (error);
    236  1.14        pk }
    237  1.14        pk 
    238   1.4        pk int
    239   1.1       mrg main()
    240   1.1       mrg {
    241  1.14        pk 	int	error, i;
    242   1.3       mrg 	char	*kernel;
    243   1.6  christos 	u_long	marks[MARK_MAX], bootinfo;
    244   1.6  christos 	struct btinfo_symtab bi_sym;
    245   1.5  christos 	void	*arg;
    246   1.1       mrg 
    247   1.8        pk #ifdef HEAP_VARIABLE
    248   1.8        pk 	{
    249   1.8        pk 		extern char end[];
    250   1.8        pk 		setheap((void *)ALIGN(end), (void *)0xffffffff);
    251   1.8        pk 	}
    252   1.8        pk #endif
    253   1.1       mrg 	prom_init();
    254  1.15        pk 	mmu_init();
    255   1.1       mrg 
    256   1.1       mrg 	printf(">> %s, Revision %s\n", bootprog_name, bootprog_rev);
    257   1.1       mrg 	printf(">> (%s, %s)\n", bootprog_maker, bootprog_date);
    258  1.10       uwe 
    259  1.10       uwe 	/* massage machine prom */
    260  1.10       uwe 	prom_patch();
    261   1.1       mrg 
    262   1.3       mrg 	/*
    263   1.3       mrg 	 * get default kernel.
    264   1.3       mrg 	 */
    265   1.4        pk 	prom_bootdevice = prom_getbootpath();
    266   1.4        pk 	kernel = prom_getbootfile();
    267  1.14        pk 	boothowto = bootoptions(prom_getbootargs());
    268   1.4        pk 
    269   1.4        pk 	if (kernel != NULL && *kernel != '\0') {
    270   1.3       mrg 		i = -1;	/* not using the kernels */
    271   1.3       mrg 	} else {
    272   1.3       mrg 		i = 0;
    273   1.3       mrg 		kernel = kernels[i];
    274   1.3       mrg 	}
    275   1.1       mrg 
    276   1.1       mrg 	for (;;) {
    277   1.3       mrg 		/*
    278   1.3       mrg 		 * ask for a kernel first ..
    279   1.3       mrg 		 */
    280  1.14        pk 		if (boothowto & RB_ASKNAME) {
    281   1.3       mrg 			printf("device[%s] (\"halt\" to halt): ",
    282   1.4        pk 					prom_bootdevice);
    283   1.1       mrg 			gets(dbuf);
    284   1.3       mrg 			if (strcmp(dbuf, "halt") == 0)
    285   1.3       mrg 				_rtt();
    286   1.1       mrg 			if (dbuf[0])
    287   1.1       mrg 				prom_bootdevice = dbuf;
    288   1.3       mrg 			printf("boot (press RETURN to try default list): ");
    289   1.1       mrg 			gets(fbuf);
    290   1.1       mrg 			if (fbuf[0])
    291   1.3       mrg 				kernel = fbuf;
    292   1.3       mrg 			else {
    293  1.14        pk 				boothowto &= ~RB_ASKNAME;
    294   1.3       mrg 				i = 0;
    295   1.3       mrg 				kernel = kernels[i];
    296   1.3       mrg 			}
    297   1.1       mrg 		}
    298   1.3       mrg 
    299   1.5  christos 		printf("Booting %s\n", kernel);
    300  1.14        pk 		if ((error = loadk(kernel, marks)) == 0)
    301  1.14        pk 			break;
    302  1.14        pk 
    303  1.14        pk 		if (error != ENOENT) {
    304  1.14        pk 			printf("Cannot load %s: error=%d\n", kernel, error);
    305  1.14        pk 			boothowto |= RB_ASKNAME;
    306  1.13        pk 		}
    307  1.13        pk 
    308   1.3       mrg 		/*
    309   1.3       mrg 		 * if we have are not in askname mode, and we aren't using the
    310   1.3       mrg 		 * prom bootfile, try the next one (if it exits).  otherwise,
    311   1.3       mrg 		 * go into askname mode.
    312   1.3       mrg 		 */
    313  1.14        pk 		if ((boothowto & RB_ASKNAME) == 0 &&
    314   1.3       mrg 		    i != -1 && kernels[++i]) {
    315   1.3       mrg 			kernel = kernels[i];
    316   1.3       mrg 			printf(": trying %s...\n", kernel);
    317   1.3       mrg 		} else {
    318   1.3       mrg 			printf("\n");
    319  1.14        pk 			boothowto |= RB_ASKNAME;
    320   1.3       mrg 		}
    321   1.1       mrg 	}
    322   1.1       mrg 
    323  1.15        pk 	marks[MARK_END] = (((u_long)marks[MARK_END] + sizeof(u_long) - 1)) &
    324  1.15        pk 	    (-sizeof(u_long));
    325   1.7  christos 	arg = (prom_version() == PROM_OLDMON) ? (caddr_t)PROM_LOADADDR : romp;
    326  1.14        pk 
    327  1.15        pk 	/* Setup boot info structure at the end of the kernel image */
    328  1.15        pk 	bootinfo = bi_init(marks[MARK_END] & loadaddrmask);
    329  1.11        pk 
    330  1.15        pk 	/* Add kernel symbols to bootinfo */
    331  1.15        pk 	bi_sym.nsym = marks[MARK_NSYM] & loadaddrmask;
    332  1.15        pk 	bi_sym.ssym = marks[MARK_SYM] & loadaddrmask;
    333  1.15        pk 	bi_sym.esym = marks[MARK_END] & loadaddrmask;
    334   1.6  christos 	bi_add(&bi_sym, BTINFO_SYMTAB, sizeof(bi_sym));
    335  1.11        pk 
    336  1.15        pk 	/* Add kernel path to bootinfo */
    337  1.11        pk 	i = sizeof(struct btinfo_common) + strlen(kernel) + 1;
    338  1.11        pk 	/* Impose limit (somewhat arbitrary) */
    339  1.11        pk 	if (i < BOOTINFO_SIZE / 2) {
    340  1.11        pk 		union {
    341  1.11        pk 			struct btinfo_kernelfile bi_file;
    342  1.11        pk 			char x[i];
    343  1.11        pk 		} U;
    344  1.11        pk 		strcpy(U.bi_file.name, kernel);
    345  1.11        pk 		bi_add(&U.bi_file, BTINFO_KERNELFILE, i);
    346  1.11        pk 	}
    347  1.11        pk 
    348   1.6  christos 	(*(entry_t)marks[MARK_ENTRY])(arg, 0, 0, 0, bootinfo, DDB_MAGIC2);
    349   1.1       mrg 	_rtt();
    350   1.1       mrg }
    351