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