Home | History | Annotate | Line # | Download | only in common
boot.c revision 1.1
      1 /*	$NetBSD: boot.c,v 1.1 2000/09/18 11:40:48 wdk 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  * 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. All advertising materials mentioning features or use of this software
     55  *    must display the following acknowledgement:
     56  *	This product includes software developed by the University of
     57  *	California, Berkeley and its contributors.
     58  * 4. Neither the name of the University nor the names of its contributors
     59  *    may be used to endorse or promote products derived from this software
     60  *    without specific prior written permission.
     61  *
     62  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     63  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     64  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     65  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     66  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     67  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     68  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     69  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     70  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     71  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     72  * SUCH DAMAGE.
     73  *
     74  *	@(#)boot.c	8.1 (Berkeley) 6/10/93
     75  */
     76 
     77 #include <lib/libsa/stand.h>
     78 #include <lib/libsa/loadfile.h>
     79 #include <lib/libkern/libkern.h>
     80 
     81 #include <sys/param.h>
     82 #include <sys/exec.h>
     83 #include <sys/exec_elf.h>
     84 
     85 #include <machine/prom.h>
     86 
     87 #include "common.h"
     88 #include "bootinfo.h"
     89 
     90 /*
     91  * We won't go overboard with gzip'd kernel names.  After all we can
     92  * still boot a gzip'd kernel called "netbsd.mipsco" - it doesn't need
     93  * the .gz suffix.
     94  */
     95 char *kernelnames[] = {
     96 	"netbsd",
     97 	"netbsd.gz",
     98 	"netbsd.bak",
     99 	"netbsd.old",
    100 	"netbsd.mipsco",
    101 	"netbsd.elf",
    102 	NULL
    103 };
    104 
    105 
    106 static char *devname __P((char *));
    107 int main __P((int, char **));
    108 
    109 /*
    110  * This gets arguments from the first stage boot lader, calls PROM routines
    111  * to open and load the program to boot, and then transfers execution to
    112  * that new program.
    113  */
    114 int
    115 main(argc, argv)
    116 	int argc;
    117 	char **argv;
    118 {
    119 	char *name, **namep, *dev, *kernel;
    120 	char bootname[PATH_MAX], bootpath[PATH_MAX];
    121 	int win;
    122 	u_long marks[MARK_MAX];
    123 	struct btinfo_symtab bi_syms;
    124 	struct btinfo_bootpath bi_bpath;
    125 	extern void prom_init __P((void));
    126 	void (*entry) __P((int, char **, char **, u_int, char *));
    127 
    128 	prom_init();
    129 
    130 	if (*(int *)argv[0] == *(int *)"boot" && argc > 1) {
    131 		argc--;
    132 		argv++;
    133 	}
    134 
    135 	/* print a banner */
    136 	printf("\n");
    137 	printf("NetBSD/mipsco " NETBSD_VERS " " BOOT_TYPE_NAME " Bootstrap, Revision %s\n",
    138 	    bootprog_rev);
    139 	printf("(%s, %s)\n", bootprog_maker, bootprog_date);
    140 	printf("\n");
    141 
    142 	/* initialise bootinfo structure early */
    143 	bi_init(BOOTINFO_ADDR);
    144 
    145 	if (strcmp(argv[0], "boot") == 0) {
    146 		argc--;
    147 		argv++;
    148 	}
    149 	name = argv[0];
    150 	printf("Boot: %s\n", name);
    151 
    152 	/* NOTE: devname() can modify bootname[]. */
    153 	strcpy(bootname, argv[0]);
    154 	if ((kernel = devname(bootname)) == NULL) {
    155 		dev = bootname;
    156 		name = NULL;
    157 	}
    158 
    159 	memset(marks, 0, sizeof marks);
    160 	if (name != NULL)
    161 		win = (loadfile(name, marks, LOAD_KERNEL) == 0);
    162 	else {
    163 		win = 0;
    164 		for (namep = kernelnames, win = 0; *namep != NULL && !win;
    165 		    namep++) {
    166 			kernel = *namep;
    167 			strcpy(bootpath, dev);
    168 			strcat(bootpath, kernel);
    169 			printf("Loading: %s\n", bootpath);
    170 			win = (loadfile(bootpath, marks, LOAD_ALL) != -1);
    171 			if (win) {
    172 				name = bootpath;
    173 			}
    174 		}
    175 	}
    176 	if (!win)
    177 		goto fail;
    178 
    179 	strncpy(bi_bpath.bootpath, kernel, BTINFO_BOOTPATH_LEN);
    180 	bi_add(&bi_bpath, BTINFO_BOOTPATH, sizeof(bi_bpath));
    181 
    182 	entry = (void *) marks[MARK_ENTRY];
    183 	bi_syms.nsym = marks[MARK_NSYM];
    184 	bi_syms.ssym = marks[MARK_SYM];
    185 	bi_syms.esym = marks[MARK_END];
    186 	bi_add(&bi_syms, BTINFO_SYMTAB, sizeof(bi_syms));
    187 
    188 	printf("Starting at 0x%x\n\n", (u_int)entry);
    189 
    190 	(*entry)(argc, argv, NULL, BOOTINFO_MAGIC, (char *)BOOTINFO_ADDR);
    191 
    192 	(void)printf("KERNEL RETURNED!\n");
    193 
    194 fail:
    195 	(void)printf("Boot failed!  Halting...\n");
    196 	return (0);
    197 }
    198 
    199 /*
    200  * Check whether or not fname is a device name only or a full
    201  * bootpath including the kernel name.  This code to do this
    202  * is copied from loadfile() in the first stage bootblocks.
    203  * Returns the kernel name, or NULL if no kernel name specified.
    204  */
    205 static char *
    206 devname(fname)
    207 	char *fname;
    208 {
    209 	char c;
    210 
    211 	while ((c = *fname++) != '\0') {
    212 		if (c == ')')
    213 			break;
    214 		if (c != '/')
    215 			continue;
    216 		while ((c = *fname++) != '\0')
    217 			if (c == '/')
    218 				break;
    219 		/*
    220 		 * Make "N/rzY" with no trailing '/' valid by adding
    221 		 * the extra '/' before appending 'boot.mipsco' to the path.
    222 		 */
    223 		if (c != '/') {
    224 			fname--;
    225 			*fname++ = '/';
    226 			*fname = '\0';
    227 		}
    228 		break;
    229 	}
    230 	return (*fname == '\0' ? NULL : fname);
    231 }
    232