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