Home | History | Annotate | Line # | Download | only in boot
boot.c revision 1.5
      1 /*	$NetBSD: boot.c,v 1.5 2006/07/22 18:15:06 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 #ifdef BOOT_DEBUG
     88 #define DPRINTF if (debug) printf
     89 #else
     90 #define DPRINTF while (/*CONSTCOND*/0) printf
     91 #endif
     92 
     93 /*
     94  * We won't go overboard with gzip'd kernel names.  After all we can
     95  * still boot a gzip'd kernel called "netbsd.arc" - it doesn't need
     96  * the .gz suffix.
     97  *
     98  * For arcane reasons, the first byte of the first element of this struct will
     99  * contain a zero.  We therefore start from one.
    100  */
    101 
    102 char *kernelnames[] = {
    103 	"placekeeper",
    104 	"netbsd.arc",
    105 	"netbsd",
    106 	"netbsd.gz",
    107 	"netbsd.bak",
    108 	"netbsd.old",
    109 	"onetbsd",
    110 	"gennetbsd",
    111 	NULL
    112 };
    113 
    114 static int debug = 0;
    115 static char **environment;
    116 
    117 /* Storage must be static. */
    118 struct btinfo_symtab bi_syms;
    119 struct btinfo_bootpath bi_bpath;
    120 
    121 static char bootinfo[BOOTINFO_SIZE];
    122 
    123 extern const struct arcbios_fv *ARCBIOS;
    124 
    125 int main(int, char **);
    126 static char *firmware_getenv(char *);
    127 
    128 /*
    129  * This gets arguments from the ARCS monitor, calls ARCS routines to open
    130  * and load the program to boot, then transfers execution to the new program.
    131  *
    132  * argv[0] will be the ARCS path to the bootloader (i.e.,
    133  * "scsi(0)disk(2)rdisk(0)partition(1)\boot").
    134  *
    135  * argv[1] through argv[n] will contain arguments passed from the PROM, if any.
    136  */
    137 
    138 int
    139 main(int argc, char **argv)
    140 {
    141 	const char     *kernel = NULL;
    142 	const char     *bootpath = NULL;
    143 	char            bootfile[PATH_MAX];
    144 	void            (*entry)(int, char *[], u_int, void *);
    145 	u_long          marks[MARK_MAX];
    146 	int             win = 0;
    147 	int             i;
    148 	int             ch;
    149 
    150 	/* print a banner */
    151 	printf("\n");
    152 	printf("%s Bootstrap, Revision %s\n", bootprog_name, bootprog_rev);
    153 	printf("(%s, %s)\n", bootprog_maker, bootprog_date);
    154 
    155 	memset(marks, 0, sizeof marks);
    156 
    157 	/* initialise bootinfo structure early */
    158 	bi_init(bootinfo);
    159 
    160 #ifdef BOOT_DEBUG
    161 	for (i = 0; i < argc; i++)
    162 		printf("argv[%d] = %s\n", i, argv[i]);
    163 #endif
    164 
    165 	/* Parse arguments, if present.  */
    166 	while ((ch = getopt(argc, argv, "v")) != -1) {
    167 		switch (ch) {
    168 		case 'v':
    169 			debug = 1;
    170 			break;
    171 		}
    172 	}
    173 
    174 	environment = &argv[1];
    175 
    176 	bootpath = firmware_getenv("OSLoadPartition");
    177 	if (bootpath == NULL)
    178 		bootpath =
    179 		    (*ARCBIOS->GetEnvironmentVariable)("OSLoadPartition");
    180 
    181 	if (bootpath == NULL) {
    182 		/* XXX need to actually do the fixup */
    183 		printf("OSLoadPartition is not specified.\n");
    184 		return 0;
    185 	}
    186 	DPRINTF("bootpath = %s\n", bootpath);
    187 
    188 	/*
    189 	 * Grab OSLoadFilename from ARCS.
    190 	 */
    191 
    192 	kernel = firmware_getenv("OSLoadFilename");
    193 	if (kernel == NULL)
    194 		kernel = (*ARCBIOS->GetEnvironmentVariable)("OSLoadFilename");
    195 
    196 	DPRINTF("kernel = %s\n", kernel ? kernel : "<null>");
    197 
    198 	/*
    199 	 * The first arg is assumed to contain the name of the kernel to boot,
    200 	 * if it a) does not start with a hyphen and b) does not contain
    201 	 * an equals sign.
    202 	 */
    203 
    204 	for (i = 1; i < argc; i++) {
    205 		if (((strchr(argv[i], '=')) == NULL) && (argv[i][0] != '-')) {
    206 			kernel = argv[i];
    207 			break;
    208 		}
    209 	}
    210 
    211 	if (kernel != NULL) {
    212 		/*
    213 		 * if the name contains parenthesis, we assume that it
    214 		 * contains the bootpath and ignore anything passed through
    215 		 * the environment
    216 		 */
    217 		if (strchr(kernel, '('))
    218 			win = loadfile(kernel, marks, LOAD_KERNEL);
    219 		else {
    220 			strcpy(bootfile, bootpath);
    221 			strcat(bootfile, kernel);
    222 			win = loadfile(bootfile, marks, LOAD_KERNEL);
    223 		}
    224 
    225 	} else {
    226 		i = 1;
    227 		while (kernelnames[i] != NULL) {
    228 			strcpy(bootfile, bootpath);
    229 			strcat(bootfile, kernelnames[i]);
    230 			kernel = kernelnames[i];
    231 			win = loadfile(bootfile, marks, LOAD_KERNEL);
    232 			if (win != -1)
    233 				break;
    234 			i++;
    235 		}
    236 
    237 	}
    238 
    239 	if (win < 0) {
    240 		printf("Boot failed!  Halting...\n");
    241 		(void)getchar();
    242 		return 0;
    243 	}
    244 
    245 	strlcpy(bi_bpath.bootpath, kernel, BTINFO_BOOTPATH_LEN);
    246 	bi_add(&bi_bpath, BTINFO_BOOTPATH, sizeof(bi_bpath));
    247 
    248 	bi_syms.nsym = marks[MARK_NSYM];
    249 	bi_syms.ssym = marks[MARK_SYM];
    250 	bi_syms.esym = marks[MARK_END];
    251 	bi_add(&bi_syms, BTINFO_SYMTAB, sizeof(bi_syms));
    252 
    253 	entry = (void *)marks[MARK_ENTRY];
    254 
    255 	if (debug) {
    256 		printf("Starting at %p\n\n", entry);
    257 		printf("nsym 0x%lx ssym 0x%lx esym 0x%lx\n", marks[MARK_NSYM],
    258 		       marks[MARK_SYM], marks[MARK_END]);
    259 	}
    260 	(*entry)(argc, argv, BOOTINFO_MAGIC, bootinfo);
    261 
    262 	printf("Kernel returned!  Halting...\n");
    263 	return 0;
    264 }
    265 
    266 char *
    267 firmware_getenv(char *envname)
    268 {
    269 	char **env;
    270 	int len;
    271 
    272 	len = strlen(envname);
    273 
    274 	for (env = environment; env[0]; env++) {
    275 		if (strncasecmp(envname, env[0], len) == 0 &&
    276 		    env[0][len] == '=') {
    277 			return &env[0][len + 1];
    278 		}
    279 	}
    280 	return NULL;
    281 }
    282 
    283 void
    284 _rtt(void)
    285 {
    286 
    287 	(*ARCBIOS->Halt)();
    288 }
    289