Home | History | Annotate | Line # | Download | only in common
boot.c revision 1.11
      1 /*	$NetBSD: boot.c,v 1.11 2005/06/03 18:55:12 martin Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jonathan Stone, Michael Hitch and Simon Burge.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Copyright (c) 1992, 1993
     41  *	The Regents of the University of California.  All rights reserved.
     42  *
     43  * This code is derived from software contributed to Berkeley by
     44  * Ralph Campbell.
     45  *
     46  * Redistribution and use in source and binary forms, with or without
     47  * modification, are permitted provided that the following conditions
     48  * are met:
     49  * 1. Redistributions of source code must retain the above copyright
     50  *    notice, this list of conditions and the following disclaimer.
     51  * 2. Redistributions in binary form must reproduce the above copyright
     52  *    notice, this list of conditions and the following disclaimer in the
     53  *    documentation and/or other materials provided with the distribution.
     54  * 3. Neither the name of the University nor the names of its contributors
     55  *    may be used to endorse or promote products derived from this software
     56  *    without specific prior written permission.
     57  *
     58  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     59  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     60  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     61  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     62  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     63  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     64  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     65  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     66  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     67  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     68  * SUCH DAMAGE.
     69  *
     70  *	@(#)boot.c	8.1 (Berkeley) 6/10/93
     71  */
     72 
     73 #include <lib/libsa/stand.h>
     74 #include <lib/libsa/loadfile.h>
     75 #include <lib/libkern/libkern.h>
     76 
     77 #include <sys/param.h>
     78 #include <sys/exec.h>
     79 #include <sys/exec_elf.h>
     80 #include <sys/boot_flag.h>
     81 
     82 #include <dev/arcbios/arcbios.h>
     83 
     84 #include "common.h"
     85 #include "bootinfo.h"
     86 
     87 /*
     88  * We won't go overboard with gzip'd kernel names.  After all we can
     89  * still boot a gzip'd kernel called "netbsd.sgimips" - it doesn't need
     90  * the .gz suffix.
     91  *
     92  * For arcane reasons, the first byte of the first element of this struct will
     93  * contain a zero.  We therefore start from one.
     94  */
     95 
     96 char           *kernelnames[] = {
     97 	"placekeeper",
     98 	"netbsd.sgimips",
     99 	"netbsd",
    100 	"netbsd.gz",
    101 	"netbsd.bak",
    102 	"netbsd.old",
    103 	"onetbsd",
    104 	"gennetbsd",
    105 	NULL
    106 };
    107 
    108 extern const struct arcbios_fv *ARCBIOS;
    109 static int      debug = 0;
    110 
    111 int             main(int, char **);
    112 
    113 /* Storage must be static. */
    114 struct btinfo_symtab bi_syms;
    115 struct btinfo_bootpath bi_bpath;
    116 
    117 /*
    118  * This gets arguments from the ARCS monitor, calls ARCS routines to open
    119  * and load the program to boot, then transfers execution to the new program.
    120  *
    121  * argv[0] will be the ARCS path to the bootloader (i.e.,
    122  * "pci(0)scsi(0)disk(2)rdisk(0)partition(8)/boot.ip3").
    123  *
    124  * argv[1] through argv[n] will contain arguments passed from the PROM, if any.
    125  */
    126 
    127 int
    128 main(int argc, char **argv)
    129 {
    130 	const char      *kernel = NULL;
    131 	const char      *bootpath = NULL;
    132 	char            bootfile[PATH_MAX];
    133 	void            (*entry) (int, char *[], int, void *);
    134 	u_long          marks[MARK_MAX];
    135 	int             win = 0;
    136 	int             i;
    137 	int             ch;
    138 
    139 	/* print a banner */
    140 	printf("\n");
    141 	printf("NetBSD/sgimips " NETBSD_VERS " Bootstrap, Revision %s\n",
    142 	       bootprog_rev);
    143 	printf("(%s, %s)\n", bootprog_maker, bootprog_date);
    144 	printf("\n");
    145 
    146 	memset(marks, 0, sizeof marks);
    147 
    148 	/* initialise bootinfo structure early */
    149 	bi_init();
    150 
    151 	/* Parse arguments, if present.  */
    152 
    153 	while ((ch = getopt(argc, argv, "v")) != -1) {
    154 		switch (ch) {
    155 		case 'v':
    156 			debug = 1;
    157 			break;
    158 		}
    159 	}
    160 
    161 	/*
    162 	 * How to find partition and file to load?
    163 	 *
    164 	 * If argv[0] contains the string "cdrom(", we're probably doing an
    165 	 * install.  The bootpath will therefore be partition 0 of whatever
    166 	 * device we've booted from.  Derive the install kernel name from
    167 	 * the bootloader name ("ip32boot", "ip22boot", or "aoutboot").
    168 	 */
    169 
    170 	if (strstr(argv[0], "cdrom("))
    171 	{
    172 		strcpy(bootfile, argv[0]);
    173 		i = (strrchr(bootfile, ')') - bootfile);
    174 		bootfile[i-1] = '0';
    175 		if (strstr(bootfile, "ip3x"))
    176 			sprintf( (strrchr(bootfile, ')') + 1), "ip3x");
    177 		else
    178 			sprintf( (strrchr(bootfile, ')') + 1), "ip2x");
    179 		if ( (loadfile(bootfile, marks, LOAD_KERNEL)) >= 0 )
    180 			goto finish;
    181 	}
    182 
    183 	bootpath = ARCBIOS->GetEnvironmentVariable("OSLoadPartition");
    184 
    185 	if (bootpath == NULL) {
    186 		/* XXX need to actually do the fixup */
    187 		printf("\nPlease set the OSLoadPartition environment variable.\n");
    188 		return 0;
    189 	}
    190 
    191 	/*
    192 	 * Grab OSLoadFilename from ARCS.
    193 	 */
    194 
    195 	kernel = ARCBIOS->GetEnvironmentVariable("OSLoadFilename");
    196 
    197 	/*
    198 	 * argv[1] is assumed to contain the name of the kernel to boot,
    199 	 * if it a) does not start with a hyphen and b) does not contain
    200 	 * an equals sign.
    201 	 */
    202 
    203 	if (((strchr(argv[1], '=')) == NULL) && (argv[1][0] != '-'))
    204 		kernel = argv[1];
    205 
    206 	if (kernel != NULL) {
    207 		/*
    208 		 * if the name contains parenthesis, we assume that it
    209 		 * contains the bootpath and ignore anything passed through
    210 		 * the environment
    211 		 */
    212 		if (strchr(kernel, '('))
    213 			win = loadfile(kernel, marks, LOAD_KERNEL);
    214 		else {
    215 			strcpy(bootfile, bootpath);
    216 			strcat(bootfile, kernel);
    217 			win = loadfile(bootfile, marks, LOAD_KERNEL);
    218 		}
    219 
    220 	} else {
    221 		i = 1;
    222 		while (kernelnames[i] != NULL) {
    223 			strcpy(bootfile, bootpath);
    224 			strcat(bootfile, kernelnames[i]);
    225 			kernel = kernelnames[i];
    226 			win = loadfile(bootfile, marks, LOAD_KERNEL);
    227 			if (win != -1)
    228 				break;
    229 			i++;
    230 		}
    231 
    232 	}
    233 
    234 	if (win < 0) {
    235 		printf("Boot failed!  Halting...\n");
    236 		return 0;
    237 	}
    238 
    239 finish:
    240 #if 0
    241 	strncpy(bi_bpath.bootpath, kernel, BTINFO_BOOTPATH_LEN);
    242 	bi_add(&bi_bpath, BTINFO_BOOTPATH);
    243 
    244 	bi_syms.nsym = marks[MARK_NSYM];
    245 	bi_syms.ssym = marks[MARK_SYM];
    246 	bi_syms.esym = marks[MARK_END];
    247 	bi_add(&bi_syms, BTINFO_SYMTAB);
    248 #endif
    249 	entry = (void *) marks[MARK_ENTRY];
    250 
    251 	if (debug) {
    252 		printf("Starting at %p\n\n", entry);
    253 		printf("nsym 0x%lx ssym 0x%lx esym 0x%lx\n", marks[MARK_NSYM],
    254 		       marks[MARK_SYM], marks[MARK_END]);
    255 	}
    256 	(*entry) (argc, argv, 0 /* BOOTINFO_MAGIC */, NULL);
    257 
    258 	printf("Kernel returned!  Halting...\n");
    259 	return (0);
    260 }
    261