Home | History | Annotate | Line # | Download | only in common
boot.c revision 1.2.26.1
      1  1.2.26.1  skrll /*	$NetBSD: boot.c,v 1.2.26.1 2004/08/03 10:38:02 skrll Exp $	*/
      2       1.1    wdk 
      3       1.1    wdk /*-
      4       1.1    wdk  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      5       1.1    wdk  * All rights reserved.
      6       1.1    wdk  *
      7       1.1    wdk  * This code is derived from software contributed to The NetBSD Foundation
      8       1.1    wdk  * by Jonathan Stone, Michael Hitch, Simon Burge and Wayne Knowles.
      9       1.1    wdk  *
     10       1.1    wdk  * Redistribution and use in source and binary forms, with or without
     11       1.1    wdk  * modification, are permitted provided that the following conditions
     12       1.1    wdk  * are met:
     13       1.1    wdk  * 1. Redistributions of source code must retain the above copyright
     14       1.1    wdk  *    notice, this list of conditions and the following disclaimer.
     15       1.1    wdk  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1    wdk  *    notice, this list of conditions and the following disclaimer in the
     17       1.1    wdk  *    documentation and/or other materials provided with the distribution.
     18       1.1    wdk  * 3. All advertising materials mentioning features or use of this software
     19       1.1    wdk  *    must display the following acknowledgement:
     20       1.1    wdk  *        This product includes software developed by the NetBSD
     21       1.1    wdk  *        Foundation, Inc. and its contributors.
     22       1.1    wdk  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23       1.1    wdk  *    contributors may be used to endorse or promote products derived
     24       1.1    wdk  *    from this software without specific prior written permission.
     25       1.1    wdk  *
     26       1.1    wdk  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27       1.1    wdk  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28       1.1    wdk  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29       1.1    wdk  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30       1.1    wdk  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31       1.1    wdk  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32       1.1    wdk  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33       1.1    wdk  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34       1.1    wdk  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35       1.1    wdk  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36       1.1    wdk  * POSSIBILITY OF SUCH DAMAGE.
     37       1.1    wdk  */
     38       1.1    wdk 
     39       1.1    wdk /*
     40       1.1    wdk  * Copyright (c) 1992, 1993
     41       1.1    wdk  *	The Regents of the University of California.  All rights reserved.
     42       1.1    wdk  *
     43       1.1    wdk  * This code is derived from software contributed to Berkeley by
     44       1.1    wdk  * Ralph Campbell.
     45       1.1    wdk  *
     46       1.1    wdk  * Redistribution and use in source and binary forms, with or without
     47       1.1    wdk  * modification, are permitted provided that the following conditions
     48       1.1    wdk  * are met:
     49       1.1    wdk  * 1. Redistributions of source code must retain the above copyright
     50       1.1    wdk  *    notice, this list of conditions and the following disclaimer.
     51       1.1    wdk  * 2. Redistributions in binary form must reproduce the above copyright
     52       1.1    wdk  *    notice, this list of conditions and the following disclaimer in the
     53       1.1    wdk  *    documentation and/or other materials provided with the distribution.
     54  1.2.26.1  skrll  * 3. Neither the name of the University nor the names of its contributors
     55       1.1    wdk  *    may be used to endorse or promote products derived from this software
     56       1.1    wdk  *    without specific prior written permission.
     57       1.1    wdk  *
     58       1.1    wdk  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     59       1.1    wdk  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     60       1.1    wdk  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     61       1.1    wdk  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     62       1.1    wdk  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     63       1.1    wdk  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     64       1.1    wdk  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     65       1.1    wdk  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     66       1.1    wdk  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     67       1.1    wdk  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     68       1.1    wdk  * SUCH DAMAGE.
     69       1.1    wdk  *
     70       1.1    wdk  *	@(#)boot.c	8.1 (Berkeley) 6/10/93
     71       1.1    wdk  */
     72       1.1    wdk 
     73       1.1    wdk #include <lib/libsa/stand.h>
     74       1.1    wdk #include <lib/libsa/loadfile.h>
     75       1.1    wdk #include <lib/libkern/libkern.h>
     76       1.1    wdk 
     77       1.1    wdk #include <sys/param.h>
     78       1.1    wdk #include <sys/exec.h>
     79       1.1    wdk #include <sys/exec_elf.h>
     80       1.1    wdk 
     81       1.1    wdk #include <machine/prom.h>
     82       1.1    wdk 
     83       1.1    wdk #include "common.h"
     84       1.1    wdk #include "bootinfo.h"
     85       1.1    wdk 
     86       1.1    wdk /*
     87       1.1    wdk  * We won't go overboard with gzip'd kernel names.  After all we can
     88       1.1    wdk  * still boot a gzip'd kernel called "netbsd.mipsco" - it doesn't need
     89       1.1    wdk  * the .gz suffix.
     90       1.1    wdk  */
     91       1.1    wdk char *kernelnames[] = {
     92       1.1    wdk 	"netbsd",
     93       1.1    wdk 	"netbsd.gz",
     94       1.1    wdk 	"netbsd.bak",
     95       1.1    wdk 	"netbsd.old",
     96       1.1    wdk 	"netbsd.mipsco",
     97       1.1    wdk 	"netbsd.elf",
     98       1.1    wdk 	NULL
     99       1.1    wdk };
    100       1.1    wdk 
    101       1.1    wdk 
    102       1.2    wdk static char *devsplit __P((char *, char *));
    103       1.1    wdk int main __P((int, char **));
    104       1.1    wdk 
    105       1.1    wdk /*
    106       1.1    wdk  * This gets arguments from the first stage boot lader, calls PROM routines
    107       1.1    wdk  * to open and load the program to boot, and then transfers execution to
    108       1.1    wdk  * that new program.
    109       1.1    wdk  */
    110       1.1    wdk int
    111       1.1    wdk main(argc, argv)
    112       1.1    wdk 	int argc;
    113       1.1    wdk 	char **argv;
    114       1.1    wdk {
    115       1.1    wdk 	char *name, **namep, *dev, *kernel;
    116       1.1    wdk 	char bootname[PATH_MAX], bootpath[PATH_MAX];
    117       1.1    wdk 	int win;
    118       1.1    wdk 	u_long marks[MARK_MAX];
    119       1.1    wdk 	struct btinfo_symtab bi_syms;
    120       1.1    wdk 	struct btinfo_bootpath bi_bpath;
    121       1.1    wdk 	extern void prom_init __P((void));
    122       1.1    wdk 	void (*entry) __P((int, char **, char **, u_int, char *));
    123       1.1    wdk 
    124       1.1    wdk 	prom_init();
    125       1.1    wdk 
    126       1.1    wdk 	/* print a banner */
    127       1.1    wdk 	printf("\n");
    128       1.2    wdk 	printf("NetBSD/mipsco " NETBSD_VERS " " BOOT_TYPE_NAME
    129       1.2    wdk 	       " Bootstrap, Revision %s\n", bootprog_rev);
    130       1.2    wdk 	printf("(%s, %s)\n\n", bootprog_maker, bootprog_date);
    131       1.1    wdk 
    132       1.1    wdk 	/* initialise bootinfo structure early */
    133       1.1    wdk 	bi_init(BOOTINFO_ADDR);
    134       1.1    wdk 
    135       1.2    wdk 	dev = name = NULL;
    136       1.2    wdk 	if (argc > 1) {
    137       1.2    wdk 		kernel = devsplit(argv[1], bootname);
    138       1.2    wdk 		if (*bootname) {
    139       1.2    wdk 			dev = bootname;
    140       1.2    wdk 			if (*kernel)
    141       1.2    wdk 				name = argv[1];
    142       1.2    wdk 			++argv;
    143       1.2    wdk 			--argc;
    144       1.2    wdk 		}
    145       1.2    wdk 
    146       1.1    wdk 	}
    147       1.2    wdk 	if (dev == NULL) {
    148       1.2    wdk 		(void) devsplit(argv[0], bootname);
    149       1.1    wdk 		dev = bootname;
    150       1.1    wdk 	}
    151       1.1    wdk 
    152       1.1    wdk 	memset(marks, 0, sizeof marks);
    153       1.1    wdk 	if (name != NULL)
    154       1.1    wdk 		win = (loadfile(name, marks, LOAD_KERNEL) == 0);
    155       1.1    wdk 	else {
    156       1.1    wdk 		win = 0;
    157       1.1    wdk 		for (namep = kernelnames, win = 0; *namep != NULL && !win;
    158       1.1    wdk 		    namep++) {
    159       1.1    wdk 			kernel = *namep;
    160       1.1    wdk 			strcpy(bootpath, dev);
    161       1.1    wdk 			strcat(bootpath, kernel);
    162       1.1    wdk 			printf("Loading: %s\n", bootpath);
    163       1.1    wdk 			win = (loadfile(bootpath, marks, LOAD_ALL) != -1);
    164       1.1    wdk 			if (win) {
    165       1.1    wdk 				name = bootpath;
    166       1.1    wdk 			}
    167       1.1    wdk 		}
    168       1.1    wdk 	}
    169       1.1    wdk 	if (!win)
    170       1.1    wdk 		goto fail;
    171       1.1    wdk 
    172       1.1    wdk 	strncpy(bi_bpath.bootpath, kernel, BTINFO_BOOTPATH_LEN);
    173       1.1    wdk 	bi_add(&bi_bpath, BTINFO_BOOTPATH, sizeof(bi_bpath));
    174       1.1    wdk 
    175       1.1    wdk 	entry = (void *) marks[MARK_ENTRY];
    176       1.1    wdk 	bi_syms.nsym = marks[MARK_NSYM];
    177       1.1    wdk 	bi_syms.ssym = marks[MARK_SYM];
    178       1.1    wdk 	bi_syms.esym = marks[MARK_END];
    179       1.1    wdk 	bi_add(&bi_syms, BTINFO_SYMTAB, sizeof(bi_syms));
    180       1.1    wdk 
    181       1.1    wdk 	printf("Starting at 0x%x\n\n", (u_int)entry);
    182       1.1    wdk 
    183       1.1    wdk 	(*entry)(argc, argv, NULL, BOOTINFO_MAGIC, (char *)BOOTINFO_ADDR);
    184       1.1    wdk 
    185       1.1    wdk 	(void)printf("KERNEL RETURNED!\n");
    186       1.1    wdk 
    187       1.1    wdk fail:
    188       1.1    wdk 	(void)printf("Boot failed!  Halting...\n");
    189       1.1    wdk 	return (0);
    190       1.1    wdk }
    191       1.1    wdk 
    192       1.1    wdk /*
    193       1.2    wdk  * strip out device name and kernel name
    194       1.1    wdk  */
    195       1.1    wdk static char *
    196       1.2    wdk devsplit(fname, devname)
    197       1.2    wdk 	char *fname, *devname;
    198       1.1    wdk {
    199       1.2    wdk 	char *src, *dst;
    200       1.1    wdk 
    201       1.2    wdk 	dst = devname;
    202       1.2    wdk 	for (src = fname; *src;/**/)
    203       1.2    wdk 		if ((*dst++ = *src++) == ')') {
    204       1.2    wdk 			*dst = (char) 0;
    205       1.2    wdk 			return src;
    206       1.1    wdk 		}
    207       1.2    wdk 	*devname = (char) 0;
    208       1.2    wdk 	return fname;
    209       1.1    wdk }
    210