Home | History | Annotate | Line # | Download | only in boot
boot.c revision 1.2.2.1
      1  1.2.2.1  cgd /*	$NetBSD: boot.c,v 1.2.2.1 1999/01/19 07:46:15 cgd 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.1  mrg  * 3. All advertising materials mentioning features or use of this software
     16      1.1  mrg  *    must display the following acknowledgement:
     17      1.1  mrg  *	This product includes software developed by the University of
     18      1.1  mrg  *	California, Berkeley and its contributors.
     19      1.1  mrg  * 4. Neither the name of the University nor the names of its contributors
     20      1.1  mrg  *    may be used to endorse or promote products derived from this software
     21      1.1  mrg  *    without specific prior written permission.
     22      1.1  mrg  *
     23      1.1  mrg  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24      1.1  mrg  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25      1.1  mrg  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26      1.1  mrg  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27      1.1  mrg  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28      1.1  mrg  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29      1.1  mrg  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30      1.1  mrg  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31      1.1  mrg  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32      1.1  mrg  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33      1.1  mrg  * SUCH DAMAGE.
     34      1.1  mrg  *
     35      1.1  mrg  * 	@(#)boot.c	8.1 (Berkeley) 6/10/93
     36      1.1  mrg  */
     37      1.1  mrg 
     38      1.1  mrg #include <sys/param.h>
     39      1.1  mrg #include <sys/reboot.h>
     40      1.1  mrg #include <a.out.h>
     41      1.1  mrg 
     42      1.1  mrg #include <lib/libsa/stand.h>
     43      1.1  mrg 
     44      1.1  mrg #include <sparc/stand/common/promdev.h>
     45      1.1  mrg 
     46      1.1  mrg static void copyunix __P((int, char *));
     47      1.1  mrg static void promsyms __P((int, struct exec *));
     48      1.1  mrg int debug;
     49      1.1  mrg int netif_debug;
     50      1.1  mrg 
     51      1.1  mrg extern char bootprog_name[], bootprog_rev[], bootprog_date[], bootprog_maker[];
     52      1.1  mrg unsigned long		esym;
     53      1.1  mrg char			*strtab;
     54      1.1  mrg int			strtablen;
     55      1.1  mrg char			fbuf[80], dbuf[128];
     56      1.1  mrg 
     57      1.1  mrg typedef void (*entry_t)__P((caddr_t, int, int, int, long, long));
     58      1.1  mrg 
     59      1.1  mrg void	loadfile __P((int, caddr_t));
     60      1.1  mrg 
     61  1.2.2.1  cgd /*
     62  1.2.2.1  cgd  * Boot device is derived from ROM provided information, or if there is none,
     63  1.2.2.1  cgd  * this list is used in sequence, to find a kernel.
     64  1.2.2.1  cgd  */
     65  1.2.2.1  cgd char *kernels[] = {
     66  1.2.2.1  cgd 	"netbsd",
     67  1.2.2.1  cgd 	"netbsd.gz",
     68  1.2.2.1  cgd 	"netbsd.old",
     69  1.2.2.1  cgd 	"netbsd.old.gz",
     70  1.2.2.1  cgd 	"onetbsd",
     71  1.2.2.1  cgd 	"onetbsd.gz",
     72  1.2.2.1  cgd 	"vmunix",
     73  1.2.2.1  cgd #ifdef notyet
     74  1.2.2.1  cgd 	"netbsd.pl",
     75  1.2.2.1  cgd 	"netbsd.pl.gz",
     76  1.2.2.1  cgd 	"netbsd.el",
     77  1.2.2.1  cgd 	"netbsd.el.gz",
     78  1.2.2.1  cgd #endif
     79  1.2.2.1  cgd 	NULL
     80  1.2.2.1  cgd };
     81  1.2.2.1  cgd 
     82      1.1  mrg main()
     83      1.1  mrg {
     84  1.2.2.1  cgd 	int	io, i;
     85  1.2.2.1  cgd 	char	*kernel;
     86      1.1  mrg 
     87      1.1  mrg 	prom_init();
     88      1.1  mrg 
     89      1.1  mrg 	printf(">> %s, Revision %s\n", bootprog_name, bootprog_rev);
     90      1.1  mrg 	printf(">> (%s, %s)\n", bootprog_maker, bootprog_date);
     91      1.1  mrg 
     92  1.2.2.1  cgd 	/*
     93  1.2.2.1  cgd 	 * get default kernel.
     94  1.2.2.1  cgd 	 */
     95  1.2.2.1  cgd 	if (prom_bootfile && *prom_bootfile) {
     96  1.2.2.1  cgd 		i = -1;	/* not using the kernels */
     97  1.2.2.1  cgd 		kernel = prom_bootfile;
     98  1.2.2.1  cgd 	} else {
     99  1.2.2.1  cgd 		i = 0;
    100  1.2.2.1  cgd 		kernel = kernels[i];
    101  1.2.2.1  cgd 	}
    102      1.1  mrg 
    103      1.1  mrg 	for (;;) {
    104  1.2.2.1  cgd 		/*
    105  1.2.2.1  cgd 		 * ask for a kernel first ..
    106  1.2.2.1  cgd 		 */
    107      1.1  mrg 		if (prom_boothow & RB_ASKNAME) {
    108  1.2.2.1  cgd 			printf("device[%s] (\"halt\" to halt): ",
    109  1.2.2.1  cgd 			   prom_bootdevice);
    110      1.1  mrg 			gets(dbuf);
    111  1.2.2.1  cgd 			if (strcmp(dbuf, "halt") == 0)
    112  1.2.2.1  cgd 				_rtt();
    113      1.1  mrg 			if (dbuf[0])
    114      1.1  mrg 				prom_bootdevice = dbuf;
    115  1.2.2.1  cgd 			printf("boot (press RETURN to try default list): ");
    116      1.1  mrg 			gets(fbuf);
    117      1.1  mrg 			if (fbuf[0])
    118  1.2.2.1  cgd 				kernel = fbuf;
    119  1.2.2.1  cgd 			else {
    120  1.2.2.1  cgd 				prom_boothow &= ~RB_ASKNAME;
    121  1.2.2.1  cgd 				i = 0;
    122  1.2.2.1  cgd 				kernel = kernels[i];
    123  1.2.2.1  cgd 			}
    124      1.1  mrg 		}
    125  1.2.2.1  cgd 
    126  1.2.2.1  cgd 		if ((io = open(kernel, 0)) >= 0)
    127      1.1  mrg 			break;
    128  1.2.2.1  cgd 		printf("open: %s: %s", kernel, strerror(errno));
    129  1.2.2.1  cgd 
    130  1.2.2.1  cgd 		/*
    131  1.2.2.1  cgd 		 * if we have are not in askname mode, and we aren't using the
    132  1.2.2.1  cgd 		 * prom bootfile, try the next one (if it exits).  otherwise,
    133  1.2.2.1  cgd 		 * go into askname mode.
    134  1.2.2.1  cgd 		 */
    135  1.2.2.1  cgd 		if ((prom_boothow & RB_ASKNAME) == 0 &&
    136  1.2.2.1  cgd 		    i != -1 && kernels[++i]) {
    137  1.2.2.1  cgd 			kernel = kernels[i];
    138  1.2.2.1  cgd 			printf(": trying %s...\n", kernel);
    139  1.2.2.1  cgd 		} else {
    140  1.2.2.1  cgd 			printf("\n");
    141  1.2.2.1  cgd 			prom_boothow |= RB_ASKNAME;
    142  1.2.2.1  cgd 		}
    143      1.1  mrg 	}
    144      1.1  mrg 
    145  1.2.2.1  cgd 	/*
    146  1.2.2.1  cgd 	 * XXX
    147  1.2.2.1  cgd 	 * make loadfile() return a value, so that if the load of the kernel
    148  1.2.2.1  cgd 	 * fails, we can jump back and try another kernel in the list.
    149  1.2.2.1  cgd 	 */
    150  1.2.2.1  cgd 	printf("Booting %s @ 0x%x\n", kernel, LOADADDR);
    151      1.1  mrg 	loadfile(io, LOADADDR);
    152      1.1  mrg 
    153      1.1  mrg 	_rtt();
    154      1.1  mrg }
    155      1.1  mrg 
    156      1.1  mrg void
    157      1.1  mrg loadfile(io, addr)
    158      1.1  mrg 	register int	io;
    159      1.1  mrg 	register caddr_t addr;
    160      1.1  mrg {
    161      1.1  mrg 	register entry_t entry = (entry_t)LOADADDR;
    162      1.1  mrg 	struct exec x;
    163      1.1  mrg 	int i;
    164      1.1  mrg 
    165      1.1  mrg 	i = read(io, (char *)&x, sizeof(x));
    166      1.1  mrg 	if (i != sizeof(x) ||
    167      1.1  mrg 	    N_BADMAG(x)) {
    168      1.1  mrg 		printf("Bad format\n");
    169      1.1  mrg 		return;
    170      1.1  mrg 	}
    171      1.1  mrg 	printf("%d", x.a_text);
    172      1.1  mrg 	if (N_GETMAGIC(x) == ZMAGIC) {
    173      1.1  mrg 		entry = (entry_t)(addr+sizeof(struct exec));
    174      1.1  mrg 		addr += sizeof(struct exec);
    175      1.1  mrg 	}
    176      1.1  mrg 	if (read(io, (char *)addr, x.a_text) != x.a_text)
    177      1.1  mrg 		goto shread;
    178      1.1  mrg 	addr += x.a_text;
    179      1.1  mrg 	if (N_GETMAGIC(x) == ZMAGIC || N_GETMAGIC(x) == NMAGIC)
    180      1.1  mrg 		while ((int)addr & __LDPGSZ)
    181      1.1  mrg 			*addr++ = 0;
    182      1.1  mrg 	printf("+%d", x.a_data);
    183      1.1  mrg 	if (read(io, addr, x.a_data) != x.a_data)
    184      1.1  mrg 		goto shread;
    185      1.1  mrg 	addr += x.a_data;
    186      1.1  mrg 	printf("+%d", x.a_bss);
    187      1.1  mrg 	for (i = 0; i < x.a_bss; i++)
    188      1.1  mrg 		*addr++ = 0;
    189      1.1  mrg 	if (x.a_syms != 0) {
    190      1.1  mrg 		bcopy(&x.a_syms, addr, sizeof(x.a_syms));
    191      1.1  mrg 		addr += sizeof(x.a_syms);
    192      1.1  mrg 		printf("+[%d", x.a_syms);
    193      1.1  mrg 		if (read(io, addr, x.a_syms) != x.a_syms)
    194      1.1  mrg 			goto shread;
    195      1.1  mrg 		addr += x.a_syms;
    196      1.1  mrg 
    197      1.1  mrg 		if (read(io, &strtablen, sizeof(int)) != sizeof(int))
    198      1.1  mrg 			goto shread;
    199      1.1  mrg 
    200      1.1  mrg 		bcopy(&strtablen, addr, sizeof(int));
    201      1.1  mrg 		if (i = strtablen) {
    202      1.1  mrg 			i -= sizeof(int);
    203      1.1  mrg 			addr += sizeof(int);
    204      1.1  mrg 			if (read(io, addr, i) != i)
    205      1.1  mrg 			    goto shread;
    206      1.1  mrg 			addr += i;
    207      1.1  mrg 		}
    208      1.1  mrg 		printf("+%d]", i);
    209      1.2   pk 		esym = ((u_int)x.a_entry - (u_int)LOADADDR) +
    210      1.1  mrg 			(((int)addr + sizeof(int) - 1) & ~(sizeof(int) - 1));
    211      1.1  mrg #if 0
    212      1.1  mrg 		/*
    213      1.1  mrg 		 * The FORTH word `loadsyms' is mentioned in the
    214      1.1  mrg 		 * "Openboot command reference" book, but it seems it has
    215      1.1  mrg 		 * not been implemented on at least one machine..
    216      1.1  mrg 		 */
    217      1.1  mrg 		promsyms(io, &x);
    218      1.1  mrg #endif
    219      1.1  mrg 	}
    220      1.1  mrg 	printf("=0x%x\n", addr);
    221      1.1  mrg 	close(io);
    222      1.1  mrg 
    223      1.1  mrg 	/* Note: args 2-4 not used due to conflicts with SunOS loaders */
    224      1.1  mrg 	(*entry)(cputyp == CPU_SUN4 ? LOADADDR : (caddr_t)promvec,
    225      1.2   pk 		 0, 0, 0, esym, DDB_MAGIC1);
    226      1.1  mrg 	return;
    227      1.1  mrg 
    228      1.1  mrg shread:
    229      1.1  mrg 	printf("boot: short read\n");
    230      1.1  mrg 	return;
    231      1.1  mrg }
    232      1.1  mrg 
    233      1.1  mrg #if 0
    234      1.1  mrg struct syms {
    235      1.1  mrg 	u_int32_t	value;
    236      1.1  mrg 	u_int32_t	index;
    237      1.1  mrg };
    238      1.1  mrg 
    239      1.1  mrg static void
    240      1.1  mrg sort(syms, n)
    241      1.1  mrg 	struct syms *syms;
    242      1.1  mrg 	int n;
    243      1.1  mrg {
    244      1.1  mrg 	register struct syms *sj;
    245      1.1  mrg 	register int i, j, k;
    246      1.1  mrg 	register u_int32_t value, index;
    247      1.1  mrg 
    248      1.1  mrg 	/* Insertion sort.  This is O(n^2), but so what? */
    249      1.1  mrg 	for (i = 1; i < n; i++) {
    250      1.1  mrg 		/* save i'th entry */
    251      1.1  mrg 		value = syms[i].value;
    252      1.1  mrg 		index = syms[i].index;
    253      1.1  mrg 		/* find j such that i'th entry goes before j'th */
    254      1.1  mrg 		for (j = 0, sj = syms; j < i; j++, sj++)
    255      1.1  mrg 			if (value < sj->value)
    256      1.1  mrg 				break;
    257      1.1  mrg 		/* slide up any additional entries */
    258      1.1  mrg 		for (k = 0; k < (i - j); k++) {
    259      1.1  mrg 			sj[k+1].value = sj[k].value;
    260      1.1  mrg 			sj[k+1].index = sj[k].index;
    261      1.1  mrg 		}
    262      1.1  mrg 		sj->value = value;
    263      1.1  mrg 		sj->index = index;
    264      1.1  mrg 	}
    265      1.1  mrg }
    266      1.1  mrg 
    267      1.1  mrg void
    268      1.1  mrg promsyms(fd, hp)
    269      1.1  mrg 	int fd;
    270      1.1  mrg 	struct exec *hp;
    271      1.1  mrg {
    272      1.1  mrg 	int i, n, strtablen;
    273      1.1  mrg 	char *str, *p, *cp, buf[128];
    274      1.1  mrg 	struct syms *syms;
    275      1.1  mrg 
    276      1.1  mrg 	lseek(fd, sizeof(*hp)+hp->a_text+hp->a_data, SEEK_SET);
    277      1.1  mrg 	n = hp->a_syms/sizeof(struct nlist);
    278      1.1  mrg 	if (n == 0)
    279      1.1  mrg 		return;
    280      1.1  mrg 	syms = (struct syms *)alloc(n * sizeof(struct syms));
    281      1.1  mrg 
    282      1.1  mrg 	printf("+[%x+", hp->a_syms);
    283      1.1  mrg 	for (i = 0; i < n; i++) {
    284      1.1  mrg 		struct nlist nlist;
    285      1.1  mrg 
    286      1.1  mrg 		if (read(fd, &nlist, sizeof(nlist)) != sizeof(nlist)) {
    287      1.1  mrg 			printf("promsyms: read failed\n");
    288      1.1  mrg 			return;
    289      1.1  mrg 		}
    290      1.1  mrg 		syms[i].value = nlist.n_value;
    291      1.1  mrg 		syms[i].index = nlist.n_un.n_strx - sizeof(strtablen);
    292      1.1  mrg 	}
    293      1.1  mrg 
    294      1.1  mrg 	sort(syms, n);
    295      1.1  mrg 
    296      1.1  mrg 	if (read(fd, &strtablen, sizeof(strtablen)) != sizeof(strtablen)) {
    297      1.1  mrg 		printf("promsym: read failed (strtablen)\n");
    298      1.1  mrg 		return;
    299      1.1  mrg 	}
    300      1.1  mrg 	if (strtablen < sizeof(strtablen)) {
    301      1.1  mrg 		printf("promsym: string table corrupted\n");
    302      1.1  mrg 		return;
    303      1.1  mrg 	}
    304      1.1  mrg 	strtablen -= sizeof(strtablen);
    305      1.1  mrg 	str = (char *)alloc(strtablen);
    306      1.1  mrg 
    307      1.1  mrg 	printf("%x]", strtablen);
    308      1.1  mrg 	if (read(fd, str, strtablen) != strtablen) {
    309      1.1  mrg 		printf("promsym: read failed (strtab)\n");
    310      1.1  mrg 		return;
    311      1.1  mrg 	}
    312      1.1  mrg 
    313      1.1  mrg 	sprintf(buf, "%x %d %x loadsyms", syms, n, str);
    314      1.1  mrg 	(promvec->pv_fortheval.v2_eval)(buf);
    315      1.1  mrg }
    316      1.1  mrg #endif
    317