Home | History | Annotate | Line # | Download | only in common
boot.c revision 1.9
      1 /*	$NetBSD: boot.c,v 1.9 2011/01/22 19:19:20 joerg 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, Simon Burge and Wayne Knowles.
      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 
     74 #include <machine/prom.h>
     75 
     76 #include "common.h"
     77 #include "bootinfo.h"
     78 
     79 /*
     80  * We won't go overboard with gzip'd kernel names.  After all we can
     81  * still boot a gzip'd kernel called "netbsd.mipsco" - it doesn't need
     82  * the .gz suffix.
     83  */
     84 char *kernelnames[] = {
     85 	"netbsd",
     86 	"netbsd.gz",
     87 	"netbsd.bak",
     88 	"netbsd.old",
     89 	"netbsd.mipsco",
     90 	"netbsd.elf",
     91 	NULL
     92 };
     93 
     94 
     95 static char *devsplit(char *, char *);
     96 int main(int, char **);
     97 
     98 /*
     99  * This gets arguments from the first stage boot lader, calls PROM routines
    100  * to open and load the program to boot, and then transfers execution to
    101  * that new program.
    102  */
    103 int
    104 main(int argc, char **argv)
    105 {
    106 	char *name, **namep, *dev, *kernel;
    107 	char bootname[PATH_MAX], bootpath[PATH_MAX];
    108 	int win;
    109 	u_long marks[MARK_MAX];
    110 	struct btinfo_symtab bi_syms;
    111 	struct btinfo_bootpath bi_bpath;
    112 	extern void prom_init(void);
    113 	void (*entry)(int, char **, char **, u_int, char *);
    114 
    115 	prom_init();
    116 
    117 	/* print a banner */
    118 	printf("\n");
    119 	printf("NetBSD/mipsco " NETBSD_VERS " " BOOT_TYPE_NAME
    120 	       " Bootstrap, Revision %s\n", bootprog_rev);
    121 
    122 	/* initialise bootinfo structure early */
    123 	bi_init(BOOTINFO_ADDR);
    124 
    125 	dev = name = NULL;
    126 	if (argc > 1) {
    127 		kernel = devsplit(argv[1], bootname);
    128 		if (*bootname) {
    129 			dev = bootname;
    130 			if (*kernel)
    131 				name = argv[1];
    132 			++argv;
    133 			--argc;
    134 		}
    135 
    136 	}
    137 	if (dev == NULL) {
    138 		(void) devsplit(argv[0], bootname);
    139 		dev = bootname;
    140 	}
    141 
    142 	memset(marks, 0, sizeof marks);
    143 	if (name != NULL)
    144 		win = (loadfile(name, marks, LOAD_KERNEL) == 0);
    145 	else {
    146 		win = 0;
    147 		for (namep = kernelnames, win = 0; *namep != NULL && !win;
    148 		    namep++) {
    149 			kernel = *namep;
    150 			strcpy(bootpath, dev);
    151 			strcat(bootpath, kernel);
    152 			printf("Loading: %s\n", bootpath);
    153 			win = (loadfile(bootpath, marks, LOAD_ALL) != -1);
    154 			if (win) {
    155 				name = bootpath;
    156 			}
    157 		}
    158 	}
    159 	if (!win)
    160 		goto fail;
    161 
    162 	strncpy(bi_bpath.bootpath, kernel, BTINFO_BOOTPATH_LEN);
    163 	bi_add(&bi_bpath, BTINFO_BOOTPATH, sizeof(bi_bpath));
    164 
    165 	entry = (void *) marks[MARK_ENTRY];
    166 	bi_syms.nsym = marks[MARK_NSYM];
    167 	bi_syms.ssym = marks[MARK_SYM];
    168 	bi_syms.esym = marks[MARK_END];
    169 	bi_add(&bi_syms, BTINFO_SYMTAB, sizeof(bi_syms));
    170 
    171 	printf("Starting at 0x%x\n\n", (u_int)entry);
    172 
    173 	(*entry)(argc, argv, NULL, BOOTINFO_MAGIC, (char *)BOOTINFO_ADDR);
    174 
    175 	(void)printf("KERNEL RETURNED!\n");
    176 
    177 fail:
    178 	(void)printf("Boot failed!  Halting...\n");
    179 	return (0);
    180 }
    181 
    182 /*
    183  * strip out device name and kernel name
    184  */
    185 static char *
    186 devsplit(char *fname, char *devname)
    187 {
    188 	char *src, *dst;
    189 
    190 	dst = devname;
    191 	for (src = fname; *src;/**/)
    192 		if ((*dst++ = *src++) == ')') {
    193 			*dst = (char) 0;
    194 			return src;
    195 		}
    196 	*devname = (char) 0;
    197 	return fname;
    198 }
    199