Home | History | Annotate | Line # | Download | only in boot
boot.c revision 1.5
      1 /*	$NetBSD: boot.c,v 1.5 1999/04/28 15:22:25 christos 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 #include <lib/libsa/loadfile.h>
     44 
     45 #include <machine/promlib.h>
     46 #include <sparc/stand/common/promdev.h>
     47 
     48 static int	bootoptions __P((char *));
     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 	u_long	marks[MARK_MAX];
    121 	void	*arg;
    122 
    123 	prom_init();
    124 
    125 	printf(">> %s, Revision %s\n", bootprog_name, bootprog_rev);
    126 	printf(">> (%s, %s)\n", bootprog_maker, bootprog_date);
    127 
    128 	/*
    129 	 * get default kernel.
    130 	 */
    131 	prom_bootdevice = prom_getbootpath();
    132 	kernel = prom_getbootfile();
    133 	how = bootoptions(prom_getbootargs());
    134 
    135 	if (kernel != NULL && *kernel != '\0') {
    136 		i = -1;	/* not using the kernels */
    137 	} else {
    138 		i = 0;
    139 		kernel = kernels[i];
    140 	}
    141 
    142 	for (;;) {
    143 		/*
    144 		 * ask for a kernel first ..
    145 		 */
    146 		if (how & RB_ASKNAME) {
    147 			printf("device[%s] (\"halt\" to halt): ",
    148 					prom_bootdevice);
    149 			gets(dbuf);
    150 			if (strcmp(dbuf, "halt") == 0)
    151 				_rtt();
    152 			if (dbuf[0])
    153 				prom_bootdevice = dbuf;
    154 			printf("boot (press RETURN to try default list): ");
    155 			gets(fbuf);
    156 			if (fbuf[0])
    157 				kernel = fbuf;
    158 			else {
    159 				how &= ~RB_ASKNAME;
    160 				i = 0;
    161 				kernel = kernels[i];
    162 			}
    163 		}
    164 
    165 		marks[MARK_START] = 0;
    166 		printf("Booting %s\n", kernel);
    167 		if ((io = loadfile(kernel, marks, LOAD_KERNEL)) != -1)
    168 			break;
    169 
    170 		/*
    171 		 * if we have are not in askname mode, and we aren't using the
    172 		 * prom bootfile, try the next one (if it exits).  otherwise,
    173 		 * go into askname mode.
    174 		 */
    175 		if ((how & RB_ASKNAME) == 0 &&
    176 		    i != -1 && kernels[++i]) {
    177 			kernel = kernels[i];
    178 			printf(": trying %s...\n", kernel);
    179 		} else {
    180 			printf("\n");
    181 			how |= RB_ASKNAME;
    182 		}
    183 	}
    184 
    185 	marks[MARK_END] = (((u_long)marks[MARK_END] + sizeof(int) - 1)) &
    186 	    (-sizeof(int));
    187 	/*
    188 	 * XXX: Fix me properly (struct bootinfo)
    189 	 */
    190 	marks[MARK_END] |= 0xf0000000;
    191 	arg = (prom_version() == PROM_OLDMON) ? PROM_LOADADDR : romp;
    192 printf("entry = 0x%lx, arg = 0x%lx, end = 0x%lx\n", marks[MARK_ENTRY], (u_long)arg, marks[MARK_END]);
    193 	(*(entry_t)marks[MARK_ENTRY])(arg, 0, 0, 0, marks[MARK_END],
    194 	    DDB_MAGIC1);
    195 
    196 	_rtt();
    197 }
    198 
    199 #if 0
    200 struct syms {
    201 	u_int32_t	value;
    202 	u_int32_t	index;
    203 };
    204 
    205 static void
    206 sort(syms, n)
    207 	struct syms *syms;
    208 	int n;
    209 {
    210 	register struct syms *sj;
    211 	register int i, j, k;
    212 	register u_int32_t value, index;
    213 
    214 	/* Insertion sort.  This is O(n^2), but so what? */
    215 	for (i = 1; i < n; i++) {
    216 		/* save i'th entry */
    217 		value = syms[i].value;
    218 		index = syms[i].index;
    219 		/* find j such that i'th entry goes before j'th */
    220 		for (j = 0, sj = syms; j < i; j++, sj++)
    221 			if (value < sj->value)
    222 				break;
    223 		/* slide up any additional entries */
    224 		for (k = 0; k < (i - j); k++) {
    225 			sj[k+1].value = sj[k].value;
    226 			sj[k+1].index = sj[k].index;
    227 		}
    228 		sj->value = value;
    229 		sj->index = index;
    230 	}
    231 }
    232 
    233 void
    234 promsyms(fd, hp)
    235 	int fd;
    236 	struct exec *hp;
    237 {
    238 	int i, n, strtablen;
    239 	char *str, *p, *cp, buf[128];
    240 	struct syms *syms;
    241 
    242 	lseek(fd, sizeof(*hp)+hp->a_text+hp->a_data, SEEK_SET);
    243 	n = hp->a_syms/sizeof(struct nlist);
    244 	if (n == 0)
    245 		return;
    246 	syms = (struct syms *)alloc(n * sizeof(struct syms));
    247 
    248 	printf("+[%x+", hp->a_syms);
    249 	for (i = 0; i < n; i++) {
    250 		struct nlist nlist;
    251 
    252 		if (read(fd, &nlist, sizeof(nlist)) != sizeof(nlist)) {
    253 			printf("promsyms: read failed\n");
    254 			return;
    255 		}
    256 		syms[i].value = nlist.n_value;
    257 		syms[i].index = nlist.n_un.n_strx - sizeof(strtablen);
    258 	}
    259 
    260 	sort(syms, n);
    261 
    262 	if (read(fd, &strtablen, sizeof(strtablen)) != sizeof(strtablen)) {
    263 		printf("promsym: read failed (strtablen)\n");
    264 		return;
    265 	}
    266 	if (strtablen < sizeof(strtablen)) {
    267 		printf("promsym: string table corrupted\n");
    268 		return;
    269 	}
    270 	strtablen -= sizeof(strtablen);
    271 	str = (char *)alloc(strtablen);
    272 
    273 	printf("%x]", strtablen);
    274 	if (read(fd, str, strtablen) != strtablen) {
    275 		printf("promsym: read failed (strtab)\n");
    276 		return;
    277 	}
    278 
    279 	sprintf(buf, "%x %d %x loadsyms", syms, n, str);
    280 	prom_interpret(buf);
    281 }
    282 #endif
    283