Home | History | Annotate | Line # | Download | only in common
boot.c revision 1.5
      1 /*	$NetBSD: boot.c,v 1.5 2003/11/11 06:47:00 sekiya 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 	char           *kernel = NULL;
    131 	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 OSLoaderPartition is not set, we're probably doing an install
    165 	 * from removable media.  Therefore, we'll fake up the bootpath from
    166 	 * argv[0].
    167 	 */
    168 
    169 	bootpath = ARCBIOS->GetEnvironmentVariable("OSLoadPartition");
    170 
    171 	if (bootpath == NULL) {
    172 		/* XXX need to actually do the fixup */
    173 		printf("\nPlease set the OSLoadPartition environment variable.\n");
    174 		return 0;
    175 	}
    176 
    177 	/*
    178 	 * Grab OSLoadFilename from ARCS.
    179 	 */
    180 
    181 	kernel = ARCBIOS->GetEnvironmentVariable("OSLoadFilename");
    182 
    183 	/*
    184 	 * argv[1] is assumed to contain the name of the kernel to boot,
    185 	 * if it a) does not start with a hyphen and b) does not contain
    186 	 * an equals sign.
    187 	 */
    188 
    189 	if (((strchr(argv[1], '=')) == NULL) && (argv[1][0] != '-'))
    190 		kernel = argv[1];
    191 
    192 	if (kernel != NULL) {
    193 		/*
    194 		 * if the name contains parenthesis, we assume that it
    195 		 * contains the bootpath and ignore anything passed through
    196 		 * the environment
    197 		 */
    198 		if (strchr(kernel, '('))
    199 			win = loadfile(kernel, marks, LOAD_KERNEL);
    200 		else {
    201 			strcpy(bootfile, bootpath);
    202 			strcat(bootfile, kernel);
    203 			win = loadfile(bootfile, marks, LOAD_KERNEL);
    204 		}
    205 
    206 	} else {
    207 		i = 1;
    208 		while (kernelnames[i] != NULL) {
    209 			strcpy(bootfile, bootpath);
    210 			strcat(bootfile, kernelnames[i]);
    211 			kernel = kernelnames[i];
    212 			win = loadfile(bootfile, marks, LOAD_KERNEL);
    213 			if (win != -1)
    214 				break;
    215 			i++;
    216 		}
    217 
    218 	}
    219 
    220 	if (win < 0) {
    221 		printf("Boot failed!  Halting...\n");
    222 		return 0;
    223 	}
    224 	strncpy(bi_bpath.bootpath, kernel, BTINFO_BOOTPATH_LEN);
    225 	bi_add(&bi_bpath, BTINFO_BOOTPATH);
    226 
    227 	entry = (void *) marks[MARK_ENTRY];
    228 	bi_syms.nsym = marks[MARK_NSYM];
    229 	bi_syms.ssym = marks[MARK_SYM];
    230 	bi_syms.esym = marks[MARK_END];
    231 	bi_add(&bi_syms, BTINFO_SYMTAB);
    232 
    233 	if (debug) {
    234 		printf("Starting at %p\n\n", entry);
    235 		printf("nsym 0x%lx ssym 0x%lx esym 0x%lx\n", marks[MARK_NSYM],
    236 		       marks[MARK_SYM], marks[MARK_END]);
    237 	}
    238 	ARCBIOS->FlushAllCaches();
    239 	(*entry) (argc, argv, BOOTINFO_MAGIC, bootinfo);
    240 
    241 	printf("Kernel returned!  Halting...\n");
    242 	return (0);
    243 }
    244