Home | History | Annotate | Line # | Download | only in common
boot.c revision 1.4
      1 /*	$NetBSD: boot.c,v 1.4 2003/08/07 16:29:27 agc 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 
     81 #include <dev/arcbios/arcbios.h>
     82 
     83 #include "common.h"
     84 #include "bootinfo.h"
     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.sgimips" - it doesn't need
     89  * the .gz suffix.
     90  */
     91 char *kernelnames[] = {
     92 	"netbsd.sgimips",
     93 	"netbsd",	"netbsd.gz",
     94 	"netbsd.bak",
     95 	"netbsd.old",
     96 	"onetbsd",
     97 	"gennetbsd",
     98 #ifdef notyet
     99 	"netbsd.el",
    100 #endif /*notyet*/
    101 	NULL
    102 };
    103 
    104 extern const struct arcbios_fv *ARCBIOS;
    105 
    106 int main __P((int, char *[]));
    107 
    108 #ifdef HEAP_VARIABLE
    109 void setheap(void *, void*);
    110 #endif
    111 
    112 /* Storage must be static. */
    113 struct btinfo_symtab bi_syms;
    114 struct btinfo_bootpath bi_bpath;
    115 
    116 /*
    117  * This gets arguments from the first stage boot lader, calls PROM routines
    118  * to open and load the program to boot, and then transfers execution to
    119  * that new program.
    120  *
    121  * Argv[0] should be something like "rz(0,0,0)netbsd" on a DECstation 3100.
    122  * Argv[0,1] should be something like "boot 5/rz0/netbsd" on a DECstation 5000.
    123  * The argument "-a" means netbsd should do an automatic reboot.
    124  */
    125 int
    126 main(argc, argv)
    127 	int argc;
    128 	char **argv;
    129 {
    130 	char *name/*, **namep, *dev, *kernel*/;
    131 	char /*bootname[PATH_MAX],*/ bootpath[PATH_MAX];
    132 	void (*entry)(int, char *[], int, void *);
    133 	u_long marks[MARK_MAX];
    134 	struct arcbios_mem *mem;
    135 	int win;
    136 
    137 	/* print a banner */
    138 	printf("\n");
    139 	printf("NetBSD/sgimips " NETBSD_VERS " Bootstrap, Revision %s\n",
    140 	    bootprog_rev);
    141 	printf("(%s, %s)\n", bootprog_maker, bootprog_date);
    142 	printf("\n");
    143 
    144 	/* Initialize heap from free memory descriptors */
    145 	mem = 0;
    146 	do {
    147 		mem = ARCBIOS->GetMemoryDescriptor(mem);
    148 		if (mem != 0)
    149 			printf("Mem block: type %d base 0x%x size 0x%x\n",
    150 			    mem->Type, mem->BasePage * 4096, mem->PageCount * 4096);
    151 	} while (mem != 0);
    152 #ifdef HEAP_VARIABLE
    153 	setheap((void *)0x88200000, (void *)0x88300000);	/* XXXX */
    154 #endif
    155 	printf("Local storage %x\n", (int)&mem);
    156 	for (win = 0; win < argc; ++win)
    157 		printf("argv[%d]: %s\n", win, argv[win]);
    158 
    159 	/* initialise bootinfo structure early */
    160 	bi_init();
    161 
    162 	/*
    163 	 * How to find partition and file to load?
    164 	 * OSLoaderPartition=scsi(n)disk(n)rdisk(n)partition(n)
    165 	 * OSLoadFilename=netbsd
    166 	 * path=???
    167 	 * argument passed to boot program
    168 	 */
    169 	bootpath[0] = 0;
    170 	name = ARCBIOS->GetEnvironmentVariable("OSLoadPartition");
    171 	if (name) {
    172 		strcpy(bootpath, name);
    173 		name = ARCBIOS->GetEnvironmentVariable("OSLoadFilename");
    174 		if (name)
    175 			strcat(bootpath, name);
    176 		else
    177 			strcat(bootpath, "netbsd");
    178 	}
    179 	if (strchr(argv[1], '=') == NULL) {
    180 		strcpy(bootpath, argv[1]);
    181 		if (strchr(argv[1], '(') == NULL) {
    182 			strcpy(bootpath,
    183 			    ARCBIOS->GetEnvironmentVariable("OSLoadPartition"));
    184 			strcat(bootpath, argv[1]);
    185 		}
    186 	}
    187 	printf("Boot: %s\n", bootpath);
    188 
    189 	memset(marks, 0, sizeof marks);
    190 #if 0
    191 	if (name != NULL)
    192 		win = (loadfile(name, marks, LOAD_KERNEL) == 0);
    193 	else {
    194 		win = 0;
    195 		for (namep = kernelnames, win = 0; *namep != NULL && !win;
    196 		    namep++) {
    197 			kernel = *namep;
    198 			strcpy(bootpath, dev);
    199 			strcat(bootpath, kernel);
    200 			printf("Loading: %s\n", bootpath);
    201 			win = (loadfile(bootpath, marks, LOAD_ALL) != -1);
    202 			if (win) {
    203 				name = bootpath;
    204 			}
    205 		}
    206 	}
    207 #else
    208 	win = loadfile(bootpath, marks, LOAD_KERNEL) == 0;
    209 #endif
    210 	if (!win)
    211 		goto fail;
    212 
    213 #if 0
    214 	strncpy(bi_bpath.bootpath, kernel, BTINFO_BOOTPATH_LEN);
    215 	bi_add(&bi_bpath, BTINFO_BOOTPATH, sizeof(bi_bpath));
    216 #endif
    217 
    218 	entry = (void *) marks[MARK_ENTRY];
    219 	bi_syms.nsym = marks[MARK_NSYM];
    220 	bi_syms.ssym = marks[MARK_SYM];
    221 	bi_syms.esym = marks[MARK_END];
    222 	bi_add(&bi_syms, BTINFO_SYMTAB);
    223 
    224 	printf("Starting at %p\n\n", entry);
    225 	printf("nsym 0x%lx ssym 0x%lx esym 0x%lx\n", marks[MARK_NSYM],
    226 	    marks[MARK_SYM], marks[MARK_END]);
    227 	ARCBIOS->FlushAllCaches();
    228 	(*entry)(argc, argv, BOOTINFO_MAGIC, bootinfo);
    229 
    230 	printf("Kernel returned!  Halting...\n");
    231 	return (0);
    232 
    233  fail:
    234 	(void)printf("Boot failed!  Halting...\n");
    235 	return (0);
    236 }
    237