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