Home | History | Annotate | Line # | Download | only in boot
boot.c revision 1.15.2.1
      1  1.15.2.1     skrll /*	$NetBSD: boot.c,v 1.15.2.1 2004/08/03 10:41:11 skrll 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.15.2.1     skrll  * 3. Neither the name of the University nor the names of its contributors
     16       1.1       mrg  *    may be used to endorse or promote products derived from this software
     17       1.1       mrg  *    without specific prior written permission.
     18       1.1       mrg  *
     19       1.1       mrg  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20       1.1       mrg  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21       1.1       mrg  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22       1.1       mrg  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23       1.1       mrg  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24       1.1       mrg  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25       1.1       mrg  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26       1.1       mrg  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27       1.1       mrg  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28       1.1       mrg  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29       1.1       mrg  * SUCH DAMAGE.
     30       1.1       mrg  *
     31       1.1       mrg  * 	@(#)boot.c	8.1 (Berkeley) 6/10/93
     32       1.1       mrg  */
     33       1.1       mrg 
     34       1.1       mrg #include <sys/param.h>
     35       1.1       mrg #include <sys/reboot.h>
     36      1.12       uwe #include <sys/boot_flag.h>
     37       1.9        pk #include <sys/exec.h>
     38       1.1       mrg 
     39       1.1       mrg #include <lib/libsa/stand.h>
     40       1.5  christos #include <lib/libsa/loadfile.h>
     41  1.15.2.1     skrll #include <lib/libkern/libkern.h>
     42       1.1       mrg 
     43       1.4        pk #include <machine/promlib.h>
     44       1.1       mrg #include <sparc/stand/common/promdev.h>
     45       1.1       mrg 
     46       1.6  christos #include "bootinfo.h"
     47       1.6  christos 
     48      1.10       uwe extern void	prom_patch __P((void));	/* prompatch.c */
     49      1.10       uwe 
     50       1.4        pk static int	bootoptions __P((char *));
     51       1.4        pk 
     52      1.14        pk int	boothowto;
     53      1.14        pk int	debug;
     54      1.14        pk int	netif_debug;
     55      1.14        pk 
     56      1.14        pk char	fbuf[80], dbuf[128];
     57      1.15        pk paddr_t bstart, bend;	/* physical start & end address of the boot program */
     58      1.15        pk 
     59      1.15        pk int	compatmode = 0;		/* For loading older kernels */
     60      1.15        pk u_long	loadaddrmask = -1UL;
     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.15.2.1     skrll 		if (*ap == 'C')
     99  1.15.2.1     skrll 			compatmode = 1;
    100       1.4        pk 		ap++;
    101       1.4        pk 	}
    102      1.12       uwe 
    103      1.12       uwe 	if ((v & RB_KDB) != 0)
    104      1.12       uwe 		debug = 1;
    105       1.4        pk 
    106       1.4        pk 	return (v);
    107       1.4        pk }
    108       1.4        pk 
    109      1.15        pk static paddr_t getphysmem(u_long size)
    110      1.15        pk {
    111      1.15        pk 	struct	memarr *pmemarr;	/* physical memory regions */
    112      1.15        pk 	int	npmemarr;		/* number of entries in pmemarr */
    113      1.15        pk 	struct memarr *mp;
    114      1.15        pk 	int i;
    115      1.15        pk 	extern char start[];	/* top of stack (see srt0.S) */
    116      1.15        pk 
    117      1.15        pk 	/*
    118      1.15        pk 	 * Find the physical memory area that's in use by the boot loader.
    119      1.15        pk 	 * Our stack grows down from label `start'; assume we need no more
    120      1.15        pk 	 * than 16K of stack space.
    121      1.15        pk 	 * The top of the boot loader is the next 4MB boundary.
    122      1.15        pk 	 */
    123      1.15        pk 	if (pmap_extract((vaddr_t)start - (16*1024), &bstart) != 0)
    124      1.15        pk 		return ((paddr_t)-1);
    125      1.15        pk 
    126      1.15        pk 	bend = roundup(bstart, 0x400000);
    127      1.15        pk 
    128      1.15        pk 	/*
    129      1.15        pk 	 * Get available physical memory from the prom.
    130      1.15        pk 	 */
    131      1.15        pk 	npmemarr = prom_makememarr(NULL, 0, MEMARR_AVAILPHYS);
    132      1.15        pk 	pmemarr = alloc(npmemarr*sizeof(struct memarr));
    133      1.15        pk 	if (pmemarr == NULL)
    134      1.15        pk 		return ((paddr_t)-1);
    135      1.15        pk 	npmemarr = prom_makememarr(pmemarr, npmemarr, MEMARR_AVAILPHYS);
    136      1.15        pk 
    137      1.15        pk 	/*
    138      1.15        pk 	 * Find a suitable loading address.
    139      1.15        pk 	 */
    140      1.15        pk 	for (mp = pmemarr, i = npmemarr; --i >= 0; mp++) {
    141      1.15        pk 		paddr_t pa = (paddr_t)pmemarr[i].addr;
    142      1.15        pk 		u_long len = (u_long)pmemarr[i].len;
    143      1.15        pk 
    144      1.15        pk 		/* Check whether it will fit in front of us */
    145      1.15        pk 		if (pa < bstart && len >= size && (bstart - pa) >= size)
    146      1.15        pk 			return (pa);
    147      1.15        pk 
    148      1.15        pk 		/* Skip the boot program memory */
    149      1.15        pk 		if (pa < bend) {
    150      1.15        pk 			if (len < bend - pa)
    151      1.15        pk 				/* Not large enough */
    152      1.15        pk 				continue;
    153      1.15        pk 
    154      1.15        pk 			/* Shrink this segment */
    155      1.15        pk 			len -=  bend - pa;
    156      1.15        pk 			pa = bend;
    157      1.15        pk 		}
    158      1.15        pk 
    159      1.15        pk 		/* Does it fit in the remainder of this segment? */
    160      1.15        pk 		if (len >= size)
    161      1.15        pk 			return (pa);
    162      1.15        pk 	}
    163      1.15        pk 	return ((paddr_t)-1);
    164      1.15        pk }
    165      1.15        pk 
    166      1.14        pk static int
    167      1.14        pk loadk(char *kernel, u_long *marks)
    168      1.14        pk {
    169      1.14        pk 	int fd, error;
    170      1.15        pk 	vaddr_t va;
    171      1.15        pk 	paddr_t pa;
    172      1.14        pk 	u_long size;
    173      1.14        pk 
    174      1.14        pk 	if ((fd = open(kernel, 0)) < 0)
    175      1.14        pk 		return (errno ? errno : ENOENT);
    176      1.14        pk 
    177      1.14        pk 	marks[MARK_START] = 0;
    178      1.14        pk 	if ((error = fdloadfile(fd, marks, COUNT_KERNEL)) != 0)
    179      1.14        pk 		goto out;
    180      1.14        pk 
    181      1.14        pk 	size = marks[MARK_END] - marks[MARK_START];
    182      1.15        pk 
    183  1.15.2.1     skrll 	/* We want that leading 16K in front of the kernel image */
    184      1.15        pk 	size += PROM_LOADADDR;
    185      1.15        pk 	va = marks[MARK_START] - PROM_LOADADDR;
    186      1.15        pk 
    187  1.15.2.1     skrll 	/*
    188  1.15.2.1     skrll 	 * Extra space for bootinfo and kernel bootstrap.
    189  1.15.2.1     skrll 	 * In compat mode, we get to re-use the space occupied by the
    190  1.15.2.1     skrll 	 * boot program. Traditionally, we've silently assumed that
    191  1.15.2.1     skrll 	 * is enough for the kernel to work with.
    192  1.15.2.1     skrll 	 */
    193  1.15.2.1     skrll 	size += BOOTINFO_SIZE;
    194  1.15.2.1     skrll 	if (!compatmode)
    195  1.15.2.1     skrll 		size += 512 * 1024;
    196      1.15        pk 
    197      1.15        pk 	/* Get a physical load address */
    198      1.15        pk 	pa = getphysmem(size);
    199      1.15        pk 	if (pa == (paddr_t)-1) {
    200      1.14        pk 		error = EFBIG;
    201      1.14        pk 		goto out;
    202      1.14        pk 	}
    203      1.14        pk 
    204      1.15        pk 	if (boothowto & AB_VERBOSE)
    205      1.15        pk 		printf("Loading at physical address %lx\n", pa);
    206      1.15        pk 	if (pmap_map(va, pa, size) != 0) {
    207      1.15        pk 		error = EFAULT;
    208      1.15        pk 		goto out;
    209      1.15        pk 	}
    210      1.15        pk 
    211      1.15        pk 	/* XXX - to do: inspect kernel image and set compat mode */
    212      1.15        pk 	if (compatmode) {
    213  1.15.2.1     skrll 		/* Double-map at VA 0 for compatibility */
    214  1.15.2.1     skrll 		if (pa + size >= bstart) {
    215  1.15.2.1     skrll 			printf("%s: too large for compat mode\n", kernel);
    216  1.15.2.1     skrll 			error = EFBIG;
    217  1.15.2.1     skrll 			goto out;
    218  1.15.2.1     skrll 		}
    219  1.15.2.1     skrll 
    220  1.15.2.1     skrll 		if (pa != 0 && pmap_map(0, pa, size) != 0) {
    221  1.15.2.1     skrll 			error = EFAULT;
    222  1.15.2.1     skrll 			goto out;
    223  1.15.2.1     skrll 		}
    224      1.15        pk 		loadaddrmask = 0x07ffffffUL;
    225      1.15        pk 	}
    226      1.15        pk 
    227      1.14        pk 	marks[MARK_START] = 0;
    228      1.14        pk 	error = fdloadfile(fd, marks, LOAD_KERNEL);
    229      1.14        pk out:
    230      1.14        pk 	close(fd);
    231      1.14        pk 	return (error);
    232      1.14        pk }
    233      1.14        pk 
    234       1.4        pk int
    235       1.1       mrg main()
    236       1.1       mrg {
    237      1.14        pk 	int	error, i;
    238       1.3       mrg 	char	*kernel;
    239       1.6  christos 	u_long	marks[MARK_MAX], bootinfo;
    240       1.6  christos 	struct btinfo_symtab bi_sym;
    241       1.5  christos 	void	*arg;
    242       1.1       mrg 
    243       1.8        pk #ifdef HEAP_VARIABLE
    244       1.8        pk 	{
    245       1.8        pk 		extern char end[];
    246       1.8        pk 		setheap((void *)ALIGN(end), (void *)0xffffffff);
    247       1.8        pk 	}
    248       1.8        pk #endif
    249       1.1       mrg 	prom_init();
    250      1.15        pk 	mmu_init();
    251       1.1       mrg 
    252       1.1       mrg 	printf(">> %s, Revision %s\n", bootprog_name, bootprog_rev);
    253       1.1       mrg 	printf(">> (%s, %s)\n", bootprog_maker, bootprog_date);
    254      1.10       uwe 
    255      1.10       uwe 	/* massage machine prom */
    256      1.10       uwe 	prom_patch();
    257       1.1       mrg 
    258       1.3       mrg 	/*
    259       1.3       mrg 	 * get default kernel.
    260       1.3       mrg 	 */
    261       1.4        pk 	prom_bootdevice = prom_getbootpath();
    262       1.4        pk 	kernel = prom_getbootfile();
    263      1.14        pk 	boothowto = bootoptions(prom_getbootargs());
    264       1.4        pk 
    265       1.4        pk 	if (kernel != NULL && *kernel != '\0') {
    266       1.3       mrg 		i = -1;	/* not using the kernels */
    267       1.3       mrg 	} else {
    268       1.3       mrg 		i = 0;
    269       1.3       mrg 		kernel = kernels[i];
    270       1.3       mrg 	}
    271       1.1       mrg 
    272       1.1       mrg 	for (;;) {
    273       1.3       mrg 		/*
    274       1.3       mrg 		 * ask for a kernel first ..
    275       1.3       mrg 		 */
    276      1.14        pk 		if (boothowto & RB_ASKNAME) {
    277       1.3       mrg 			printf("device[%s] (\"halt\" to halt): ",
    278       1.4        pk 					prom_bootdevice);
    279       1.1       mrg 			gets(dbuf);
    280       1.3       mrg 			if (strcmp(dbuf, "halt") == 0)
    281       1.3       mrg 				_rtt();
    282       1.1       mrg 			if (dbuf[0])
    283       1.1       mrg 				prom_bootdevice = dbuf;
    284       1.3       mrg 			printf("boot (press RETURN to try default list): ");
    285       1.1       mrg 			gets(fbuf);
    286       1.1       mrg 			if (fbuf[0])
    287       1.3       mrg 				kernel = fbuf;
    288       1.3       mrg 			else {
    289      1.14        pk 				boothowto &= ~RB_ASKNAME;
    290       1.3       mrg 				i = 0;
    291       1.3       mrg 				kernel = kernels[i];
    292       1.3       mrg 			}
    293       1.1       mrg 		}
    294       1.3       mrg 
    295       1.5  christos 		printf("Booting %s\n", kernel);
    296      1.14        pk 		if ((error = loadk(kernel, marks)) == 0)
    297      1.14        pk 			break;
    298      1.14        pk 
    299      1.14        pk 		if (error != ENOENT) {
    300      1.14        pk 			printf("Cannot load %s: error=%d\n", kernel, error);
    301      1.14        pk 			boothowto |= RB_ASKNAME;
    302      1.13        pk 		}
    303      1.13        pk 
    304       1.3       mrg 		/*
    305       1.3       mrg 		 * if we have are not in askname mode, and we aren't using the
    306       1.3       mrg 		 * prom bootfile, try the next one (if it exits).  otherwise,
    307       1.3       mrg 		 * go into askname mode.
    308       1.3       mrg 		 */
    309      1.14        pk 		if ((boothowto & RB_ASKNAME) == 0 &&
    310       1.3       mrg 		    i != -1 && kernels[++i]) {
    311       1.3       mrg 			kernel = kernels[i];
    312       1.3       mrg 			printf(": trying %s...\n", kernel);
    313       1.3       mrg 		} else {
    314       1.3       mrg 			printf("\n");
    315      1.14        pk 			boothowto |= RB_ASKNAME;
    316       1.3       mrg 		}
    317       1.1       mrg 	}
    318       1.1       mrg 
    319      1.15        pk 	marks[MARK_END] = (((u_long)marks[MARK_END] + sizeof(u_long) - 1)) &
    320      1.15        pk 	    (-sizeof(u_long));
    321       1.7  christos 	arg = (prom_version() == PROM_OLDMON) ? (caddr_t)PROM_LOADADDR : romp;
    322      1.14        pk 
    323      1.15        pk 	/* Setup boot info structure at the end of the kernel image */
    324      1.15        pk 	bootinfo = bi_init(marks[MARK_END] & loadaddrmask);
    325      1.11        pk 
    326      1.15        pk 	/* Add kernel symbols to bootinfo */
    327      1.15        pk 	bi_sym.nsym = marks[MARK_NSYM] & loadaddrmask;
    328      1.15        pk 	bi_sym.ssym = marks[MARK_SYM] & loadaddrmask;
    329      1.15        pk 	bi_sym.esym = marks[MARK_END] & loadaddrmask;
    330       1.6  christos 	bi_add(&bi_sym, BTINFO_SYMTAB, sizeof(bi_sym));
    331      1.11        pk 
    332      1.15        pk 	/* Add kernel path to bootinfo */
    333      1.11        pk 	i = sizeof(struct btinfo_common) + strlen(kernel) + 1;
    334      1.11        pk 	/* Impose limit (somewhat arbitrary) */
    335      1.11        pk 	if (i < BOOTINFO_SIZE / 2) {
    336      1.11        pk 		union {
    337      1.11        pk 			struct btinfo_kernelfile bi_file;
    338      1.11        pk 			char x[i];
    339      1.11        pk 		} U;
    340      1.11        pk 		strcpy(U.bi_file.name, kernel);
    341      1.11        pk 		bi_add(&U.bi_file, BTINFO_KERNELFILE, i);
    342      1.11        pk 	}
    343      1.11        pk 
    344       1.6  christos 	(*(entry_t)marks[MARK_ENTRY])(arg, 0, 0, 0, bootinfo, DDB_MAGIC2);
    345       1.1       mrg 	_rtt();
    346       1.1       mrg }
    347