Home | History | Annotate | Line # | Download | only in common
boot.c revision 1.14
      1 /*	$NetBSD: boot.c,v 1.14 2008/02/23 05:35:20 tsutsui 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 static uint8_t bootinfo[BOOTINFO_SIZE];
    118 
    119 /*
    120  * This gets arguments from the ARCS monitor, calls ARCS routines to open
    121  * and load the program to boot, then transfers execution to the new program.
    122  *
    123  * argv[0] will be the ARCS path to the bootloader (i.e.,
    124  * "pci(0)scsi(0)disk(2)rdisk(0)partition(8)/boot.ip3").
    125  *
    126  * argv[1] through argv[n] will contain arguments passed from the PROM, if any.
    127  */
    128 
    129 int
    130 main(int argc, char **argv)
    131 {
    132 	const char      *kernel = NULL;
    133 	const char      *bootpath = NULL;
    134 	char            bootfile[PATH_MAX];
    135 	void            (*entry) (int, char *[], int, void *);
    136 	u_long          marks[MARK_MAX];
    137 	int             win = 0;
    138 	int             i;
    139 	int             ch;
    140 
    141 	/* print a banner */
    142 	printf("\n");
    143 	printf("NetBSD/sgimips " NETBSD_VERS " Bootstrap, Revision %s\n",
    144 	       bootprog_rev);
    145 	printf("(%s, %s)\n", bootprog_maker, bootprog_date);
    146 	printf("\n");
    147 
    148 	memset(marks, 0, sizeof marks);
    149 
    150 	/* initialise bootinfo structure early */
    151 	bi_init(bootinfo);
    152 
    153 	/* Parse arguments, if present.  */
    154 
    155 	while ((ch = getopt(argc, argv, "v")) != -1) {
    156 		switch (ch) {
    157 		case 'v':
    158 			debug = 1;
    159 			break;
    160 		}
    161 	}
    162 
    163 	/*
    164 	 * How to find partition and file to load?
    165 	 *
    166 	 * If argv[0] contains the string "cdrom(", we're probably doing an
    167 	 * install.  The bootpath will therefore be partition 0 of whatever
    168 	 * device we've booted from.  Derive the install kernel name from
    169 	 * the bootloader name ("ip32boot", "ip22boot", or "aoutboot").
    170 	 */
    171 
    172 	if (strstr(argv[0], "cdrom("))
    173 	{
    174 		strcpy(bootfile, argv[0]);
    175 		i = (strrchr(bootfile, ')') - bootfile);
    176 		bootfile[i-1] = '0';
    177 		if (strstr(bootfile, "ip3x"))
    178 			kernel = "ip3x";
    179 		else
    180 			kernel = "ip2x";
    181 		sprintf((strrchr(bootfile, ')') + 1), kernel);
    182 		if ( (loadfile(bootfile, marks, LOAD_KERNEL)) >= 0 )
    183 			goto finish;
    184 	}
    185 
    186 	bootpath = ARCBIOS->GetEnvironmentVariable("OSLoadPartition");
    187 
    188 	if (bootpath == NULL) {
    189 		/* XXX need to actually do the fixup */
    190 		printf("\nPlease set the OSLoadPartition environment variable.\n");
    191 		return 0;
    192 	}
    193 
    194 	/*
    195 	 * Grab OSLoadFilename from ARCS.
    196 	 */
    197 
    198 	kernel = ARCBIOS->GetEnvironmentVariable("OSLoadFilename");
    199 
    200 	/*
    201 	 * argv[1] is assumed to contain the name of the kernel to boot,
    202 	 * if it a) does not start with a hyphen and b) does not contain
    203 	 * an equals sign.
    204 	 */
    205 
    206 	if (((strchr(argv[1], '=')) == NULL) && (argv[1][0] != '-'))
    207 		kernel = argv[1];
    208 
    209 	if (kernel != NULL) {
    210 		/*
    211 		 * if the name contains parenthesis, we assume that it
    212 		 * contains the bootpath and ignore anything passed through
    213 		 * the environment
    214 		 */
    215 		if (strchr(kernel, '('))
    216 			win = loadfile(kernel, marks, LOAD_KERNEL);
    217 		else {
    218 			strcpy(bootfile, bootpath);
    219 			strcat(bootfile, kernel);
    220 			win = loadfile(bootfile, marks, LOAD_KERNEL);
    221 		}
    222 
    223 	} else {
    224 		i = 1;
    225 		while (kernelnames[i] != NULL) {
    226 			strcpy(bootfile, bootpath);
    227 			strcat(bootfile, kernelnames[i]);
    228 			kernel = kernelnames[i];
    229 			win = loadfile(bootfile, marks, LOAD_KERNEL);
    230 			if (win != -1)
    231 				break;
    232 			i++;
    233 		}
    234 
    235 	}
    236 
    237 	if (win < 0) {
    238 		printf("Boot failed!  Halting...\n");
    239 		return 0;
    240 	}
    241 
    242 finish:
    243 	strlcpy(bi_bpath.bootpath, kernel, BTINFO_BOOTPATH_LEN);
    244 	bi_add(&bi_bpath, BTINFO_BOOTPATH, sizeof(bi_bpath));
    245 
    246 	bi_syms.nsym = marks[MARK_NSYM];
    247 	bi_syms.ssym = marks[MARK_SYM];
    248 	bi_syms.esym = marks[MARK_END];
    249 	bi_add(&bi_syms, BTINFO_SYMTAB, sizeof(bi_syms));
    250 	entry = (void *)marks[MARK_ENTRY];
    251 
    252 	if (debug) {
    253 		printf("Starting at %p\n\n", entry);
    254 		printf("nsym 0x%lx ssym 0x%lx esym 0x%lx\n", marks[MARK_NSYM],
    255 		       marks[MARK_SYM], marks[MARK_END]);
    256 	}
    257 	(*entry)(argc, argv, BOOTINFO_MAGIC, bootinfo);
    258 
    259 	printf("Kernel returned!  Halting...\n");
    260 	return (0);
    261 }
    262