Home | History | Annotate | Line # | Download | only in boot
boot.c revision 1.4
      1 /*	$NetBSD: boot.c,v 1.4 1999/02/15 18:59:36 pk 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 <a.out.h>
     41 
     42 #include <lib/libsa/stand.h>
     43 
     44 #include <machine/promlib.h>
     45 #include <sparc/stand/common/promdev.h>
     46 
     47 static int	bootoptions __P((char *));
     48 void		loadfile __P((int, caddr_t));
     49 #if 0
     50 static void	promsyms __P((int, struct exec *));
     51 #endif
     52 
     53 int debug;
     54 int netif_debug;
     55 
     56 extern char bootprog_name[], bootprog_rev[], bootprog_date[], bootprog_maker[];
     57 unsigned long		esym;
     58 char			*strtab;
     59 int			strtablen;
     60 char			fbuf[80], dbuf[128];
     61 
     62 int	main __P((void));
     63 typedef void (*entry_t)__P((caddr_t, int, int, int, long, long));
     64 
     65 
     66 /*
     67  * Boot device is derived from ROM provided information, or if there is none,
     68  * this list is used in sequence, to find a kernel.
     69  */
     70 char *kernels[] = {
     71 	"netbsd",
     72 	"netbsd.gz",
     73 	"netbsd.old",
     74 	"netbsd.old.gz",
     75 	"onetbsd",
     76 	"onetbsd.gz",
     77 	"vmunix",
     78 #ifdef notyet
     79 	"netbsd.pl",
     80 	"netbsd.pl.gz",
     81 	"netbsd.el",
     82 	"netbsd.el.gz",
     83 #endif
     84 	NULL
     85 };
     86 
     87 int
     88 bootoptions(ap)
     89 	char *ap;
     90 {
     91 	int v = 0;
     92 	if (ap == NULL || *ap++ != '-')
     93 		return (0);
     94 
     95 	while (*ap != '\0' && *ap != ' ' && *ap != '\t' && *ap != '\n') {
     96 		switch (*ap) {
     97 		case 'a':
     98 			v |= RB_ASKNAME;
     99 			break;
    100 		case 's':
    101 			v |= RB_SINGLE;
    102 			break;
    103 		case 'd':
    104 			v |= RB_KDB;
    105 			debug = 1;
    106 			break;
    107 		}
    108 		ap++;
    109 	}
    110 
    111 	return (v);
    112 }
    113 
    114 int
    115 main()
    116 {
    117 	int	io, i;
    118 	char	*kernel;
    119 	int	how;
    120 
    121 	prom_init();
    122 
    123 	printf(">> %s, Revision %s\n", bootprog_name, bootprog_rev);
    124 	printf(">> (%s, %s)\n", bootprog_maker, bootprog_date);
    125 
    126 	/*
    127 	 * get default kernel.
    128 	 */
    129 	prom_bootdevice = prom_getbootpath();
    130 	kernel = prom_getbootfile();
    131 	how = bootoptions(prom_getbootargs());
    132 
    133 	if (kernel != NULL && *kernel != '\0') {
    134 		i = -1;	/* not using the kernels */
    135 	} else {
    136 		i = 0;
    137 		kernel = kernels[i];
    138 	}
    139 
    140 	for (;;) {
    141 		/*
    142 		 * ask for a kernel first ..
    143 		 */
    144 		if (how & RB_ASKNAME) {
    145 			printf("device[%s] (\"halt\" to halt): ",
    146 					prom_bootdevice);
    147 			gets(dbuf);
    148 			if (strcmp(dbuf, "halt") == 0)
    149 				_rtt();
    150 			if (dbuf[0])
    151 				prom_bootdevice = dbuf;
    152 			printf("boot (press RETURN to try default list): ");
    153 			gets(fbuf);
    154 			if (fbuf[0])
    155 				kernel = fbuf;
    156 			else {
    157 				how &= ~RB_ASKNAME;
    158 				i = 0;
    159 				kernel = kernels[i];
    160 			}
    161 		}
    162 
    163 		if ((io = open(kernel, 0)) >= 0)
    164 			break;
    165 		printf("open: %s: %s", kernel, strerror(errno));
    166 
    167 		/*
    168 		 * if we have are not in askname mode, and we aren't using the
    169 		 * prom bootfile, try the next one (if it exits).  otherwise,
    170 		 * go into askname mode.
    171 		 */
    172 		if ((how & RB_ASKNAME) == 0 &&
    173 		    i != -1 && kernels[++i]) {
    174 			kernel = kernels[i];
    175 			printf(": trying %s...\n", kernel);
    176 		} else {
    177 			printf("\n");
    178 			how |= RB_ASKNAME;
    179 		}
    180 	}
    181 
    182 	/*
    183 	 * XXX
    184 	 * make loadfile() return a value, so that if the load of the kernel
    185 	 * fails, we can jump back and try another kernel in the list.
    186 	 */
    187 	printf("Booting %s @ %p\n", kernel, PROM_LOADADDR);
    188 	loadfile(io, PROM_LOADADDR);
    189 
    190 	_rtt();
    191 }
    192 
    193 void
    194 loadfile(io, addr)
    195 	int	io;
    196 	caddr_t addr;
    197 {
    198 	entry_t entry = (entry_t)PROM_LOADADDR;
    199 	void *arg;
    200 	struct exec x;
    201 	int i;
    202 
    203 	i = read(io, (char *)&x, sizeof(x));
    204 	if (i != sizeof(x) ||
    205 	    N_BADMAG(x)) {
    206 		printf("Bad format\n");
    207 		return;
    208 	}
    209 	printf("%ld", x.a_text);
    210 	if (N_GETMAGIC(x) == ZMAGIC) {
    211 		entry = (entry_t)(addr+sizeof(struct exec));
    212 		addr += sizeof(struct exec);
    213 	}
    214 	if (read(io, (char *)addr, x.a_text) != x.a_text)
    215 		goto shread;
    216 	addr += x.a_text;
    217 	if (N_GETMAGIC(x) == ZMAGIC || N_GETMAGIC(x) == NMAGIC)
    218 		while ((int)addr & __LDPGSZ)
    219 			*addr++ = 0;
    220 	printf("+%ld", x.a_data);
    221 	if (read(io, addr, x.a_data) != x.a_data)
    222 		goto shread;
    223 	addr += x.a_data;
    224 	printf("+%ld", x.a_bss);
    225 	for (i = 0; i < x.a_bss; i++)
    226 		*addr++ = 0;
    227 	if (x.a_syms != 0) {
    228 		bcopy(&x.a_syms, addr, sizeof(x.a_syms));
    229 		addr += sizeof(x.a_syms);
    230 		printf("+[%ld", x.a_syms);
    231 		if (read(io, addr, x.a_syms) != x.a_syms)
    232 			goto shread;
    233 		addr += x.a_syms;
    234 
    235 		if (read(io, &strtablen, sizeof(int)) != sizeof(int))
    236 			goto shread;
    237 
    238 		bcopy(&strtablen, addr, sizeof(int));
    239 		if ((i = strtablen) != 0) {
    240 			i -= sizeof(int);
    241 			addr += sizeof(int);
    242 			if (read(io, addr, i) != i)
    243 			    goto shread;
    244 			addr += i;
    245 		}
    246 		printf("+%d]", i);
    247 		esym = ((u_int)x.a_entry - (u_int)PROM_LOADADDR) +
    248 			(((int)addr + sizeof(int) - 1) & ~(sizeof(int) - 1));
    249 #if 0
    250 		/*
    251 		 * The FORTH word `loadsyms' is mentioned in the
    252 		 * "Openboot command reference" book, but it seems it has
    253 		 * not been implemented on at least one machine..
    254 		 */
    255 		promsyms(io, &x);
    256 #endif
    257 	}
    258 	printf("=%p\n", addr);
    259 	close(io);
    260 
    261 	/* Note: args 2-4 not used due to conflicts with SunOS loaders */
    262 	arg = (prom_version() == PROM_OLDMON) ? PROM_LOADADDR : romp;
    263 	(*entry)(arg, 0, 0, 0, esym, DDB_MAGIC1);
    264 	return;
    265 
    266 shread:
    267 	printf("boot: short read\n");
    268 	return;
    269 }
    270 
    271 #if 0
    272 struct syms {
    273 	u_int32_t	value;
    274 	u_int32_t	index;
    275 };
    276 
    277 static void
    278 sort(syms, n)
    279 	struct syms *syms;
    280 	int n;
    281 {
    282 	register struct syms *sj;
    283 	register int i, j, k;
    284 	register u_int32_t value, index;
    285 
    286 	/* Insertion sort.  This is O(n^2), but so what? */
    287 	for (i = 1; i < n; i++) {
    288 		/* save i'th entry */
    289 		value = syms[i].value;
    290 		index = syms[i].index;
    291 		/* find j such that i'th entry goes before j'th */
    292 		for (j = 0, sj = syms; j < i; j++, sj++)
    293 			if (value < sj->value)
    294 				break;
    295 		/* slide up any additional entries */
    296 		for (k = 0; k < (i - j); k++) {
    297 			sj[k+1].value = sj[k].value;
    298 			sj[k+1].index = sj[k].index;
    299 		}
    300 		sj->value = value;
    301 		sj->index = index;
    302 	}
    303 }
    304 
    305 void
    306 promsyms(fd, hp)
    307 	int fd;
    308 	struct exec *hp;
    309 {
    310 	int i, n, strtablen;
    311 	char *str, *p, *cp, buf[128];
    312 	struct syms *syms;
    313 
    314 	lseek(fd, sizeof(*hp)+hp->a_text+hp->a_data, SEEK_SET);
    315 	n = hp->a_syms/sizeof(struct nlist);
    316 	if (n == 0)
    317 		return;
    318 	syms = (struct syms *)alloc(n * sizeof(struct syms));
    319 
    320 	printf("+[%x+", hp->a_syms);
    321 	for (i = 0; i < n; i++) {
    322 		struct nlist nlist;
    323 
    324 		if (read(fd, &nlist, sizeof(nlist)) != sizeof(nlist)) {
    325 			printf("promsyms: read failed\n");
    326 			return;
    327 		}
    328 		syms[i].value = nlist.n_value;
    329 		syms[i].index = nlist.n_un.n_strx - sizeof(strtablen);
    330 	}
    331 
    332 	sort(syms, n);
    333 
    334 	if (read(fd, &strtablen, sizeof(strtablen)) != sizeof(strtablen)) {
    335 		printf("promsym: read failed (strtablen)\n");
    336 		return;
    337 	}
    338 	if (strtablen < sizeof(strtablen)) {
    339 		printf("promsym: string table corrupted\n");
    340 		return;
    341 	}
    342 	strtablen -= sizeof(strtablen);
    343 	str = (char *)alloc(strtablen);
    344 
    345 	printf("%x]", strtablen);
    346 	if (read(fd, str, strtablen) != strtablen) {
    347 		printf("promsym: read failed (strtab)\n");
    348 		return;
    349 	}
    350 
    351 	sprintf(buf, "%x %d %x loadsyms", syms, n, str);
    352 	prom_interpret(buf);
    353 }
    354 #endif
    355