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