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