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