Home | History | Annotate | Line # | Download | only in ofwboot
boot.c revision 1.9.2.3
      1  1.9.2.3  jdolecek /*	$NetBSD: boot.c,v 1.9.2.3 2002/10/10 18:34:51 jdolecek Exp $	*/
      2      1.2   thorpej 
      3      1.2   thorpej /*-
      4      1.2   thorpej  * Copyright (c) 1997 The NetBSD Foundation, Inc.
      5      1.2   thorpej  * All rights reserved.
      6      1.2   thorpej  *
      7      1.2   thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8      1.2   thorpej  * by Jason R. Thorpe.
      9      1.2   thorpej  *
     10      1.2   thorpej  * Redistribution and use in source and binary forms, with or without
     11      1.2   thorpej  * modification, are permitted provided that the following conditions
     12      1.2   thorpej  * are met:
     13      1.2   thorpej  * 1. Redistributions of source code must retain the above copyright
     14      1.2   thorpej  *    notice, this list of conditions and the following disclaimer.
     15      1.2   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     16      1.2   thorpej  *    notice, this list of conditions and the following disclaimer in the
     17      1.2   thorpej  *    documentation and/or other materials provided with the distribution.
     18      1.2   thorpej  * 3. All advertising materials mentioning features or use of this software
     19      1.2   thorpej  *    must display the following acknowledgement:
     20      1.2   thorpej  *	This product includes software developed by the NetBSD
     21      1.2   thorpej  *	Foundation, Inc. and its contributors.
     22      1.2   thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23      1.2   thorpej  *    contributors may be used to endorse or promote products derived
     24      1.2   thorpej  *    from this software without specific prior written permission.
     25      1.2   thorpej  *
     26      1.2   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27      1.2   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28      1.2   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29      1.2   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30      1.2   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31      1.2   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32      1.2   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33      1.2   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34      1.2   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35      1.2   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36      1.2   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     37      1.2   thorpej  */
     38      1.1   thorpej 
     39      1.1   thorpej /*
     40      1.1   thorpej  * Copyright (C) 1995, 1996 Wolfgang Solfrank.
     41      1.1   thorpej  * Copyright (C) 1995, 1996 TooLs GmbH.
     42      1.1   thorpej  * All rights reserved.
     43      1.1   thorpej  *
     44      1.1   thorpej  * ELF support derived from NetBSD/alpha's boot loader, written
     45      1.1   thorpej  * by Christopher G. Demetriou.
     46      1.1   thorpej  *
     47      1.1   thorpej  * Redistribution and use in source and binary forms, with or without
     48      1.1   thorpej  * modification, are permitted provided that the following conditions
     49      1.1   thorpej  * are met:
     50      1.1   thorpej  * 1. Redistributions of source code must retain the above copyright
     51      1.1   thorpej  *    notice, this list of conditions and the following disclaimer.
     52      1.1   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     53      1.1   thorpej  *    notice, this list of conditions and the following disclaimer in the
     54      1.1   thorpej  *    documentation and/or other materials provided with the distribution.
     55      1.1   thorpej  * 3. All advertising materials mentioning features or use of this software
     56      1.1   thorpej  *    must display the following acknowledgement:
     57      1.1   thorpej  *	This product includes software developed by TooLs GmbH.
     58      1.1   thorpej  * 4. The name of TooLs GmbH may not be used to endorse or promote products
     59      1.1   thorpej  *    derived from this software without specific prior written permission.
     60      1.1   thorpej  *
     61      1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
     62      1.1   thorpej  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     63      1.1   thorpej  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     64      1.1   thorpej  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     65      1.1   thorpej  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     66      1.1   thorpej  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     67      1.1   thorpej  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     68      1.1   thorpej  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     69      1.1   thorpej  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     70      1.1   thorpej  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     71      1.1   thorpej  */
     72      1.1   thorpej 
     73      1.1   thorpej /*
     74      1.1   thorpej  * First try for the boot code
     75      1.1   thorpej  *
     76      1.1   thorpej  * Input syntax is:
     77      1.1   thorpej  *	[promdev[{:|,}partition]]/[filename] [flags]
     78      1.1   thorpej  */
     79      1.1   thorpej 
     80      1.1   thorpej #define	ELFSIZE		32		/* We use 32-bit ELF. */
     81      1.1   thorpej 
     82      1.1   thorpej #include <sys/param.h>
     83      1.1   thorpej #include <sys/exec.h>
     84      1.1   thorpej #include <sys/exec_elf.h>
     85      1.1   thorpej #include <sys/reboot.h>
     86      1.1   thorpej #include <sys/disklabel.h>
     87      1.7  jdolecek #include <sys/boot_flag.h>
     88      1.1   thorpej 
     89      1.4   mycroft #include <lib/libsa/stand.h>
     90  1.9.2.2   thorpej #include <lib/libsa/loadfile.h>
     91      1.4   mycroft #include <lib/libkern/libkern.h>
     92      1.4   mycroft 
     93      1.1   thorpej #include <machine/cpu.h>
     94      1.1   thorpej 
     95      1.4   mycroft #include "ofdev.h"
     96      1.4   mycroft #include "openfirm.h"
     97      1.1   thorpej 
     98  1.9.2.2   thorpej #ifdef DEBUG
     99  1.9.2.2   thorpej # define DPRINTF printf
    100  1.9.2.2   thorpej #else
    101  1.9.2.2   thorpej # define DPRINTF while (/*CONSTCOND*/0) printf
    102  1.9.2.2   thorpej #endif
    103  1.9.2.2   thorpej 
    104      1.1   thorpej char bootdev[128];
    105      1.1   thorpej char bootfile[128];
    106      1.1   thorpej int boothowto;
    107      1.1   thorpej int debug;
    108      1.1   thorpej 
    109  1.9.2.3  jdolecek static char *kernels[] = { "/netbsd.ofppc", "/netbsd", "/netbsd.gz", NULL };
    110      1.1   thorpej 
    111      1.1   thorpej static void
    112      1.1   thorpej prom2boot(dev)
    113      1.1   thorpej 	char *dev;
    114      1.1   thorpej {
    115  1.9.2.2   thorpej 	char *cp, *ocp;
    116      1.1   thorpej 
    117  1.9.2.2   thorpej 	ocp = cp;
    118  1.9.2.2   thorpej 	cp = dev + strlen(dev) - 1;
    119  1.9.2.2   thorpej 	for (; cp >= ocp; cp--) {
    120      1.4   mycroft 		if (*cp == ':') {
    121      1.4   mycroft 			*cp = '\0';
    122      1.4   mycroft 			return;
    123      1.4   mycroft 		}
    124  1.9.2.2   thorpej 	}
    125      1.1   thorpej }
    126      1.1   thorpej 
    127      1.1   thorpej static void
    128      1.1   thorpej parseargs(str, howtop)
    129      1.1   thorpej 	char *str;
    130      1.1   thorpej 	int *howtop;
    131      1.1   thorpej {
    132      1.1   thorpej 	char *cp;
    133      1.1   thorpej 
    134      1.1   thorpej 	/* Allow user to drop back to the PROM. */
    135      1.1   thorpej 	if (strcmp(str, "exit") == 0)
    136      1.4   mycroft 		OF_exit();
    137  1.9.2.3  jdolecek 	if (strcmp(str, "halt") == 0)
    138  1.9.2.3  jdolecek 		OF_exit();
    139  1.9.2.3  jdolecek 	if (strcmp(str, "reboot") == 0)
    140  1.9.2.3  jdolecek 		OF_boot("");
    141      1.1   thorpej 
    142      1.1   thorpej 	*howtop = 0;
    143  1.9.2.2   thorpej 
    144      1.1   thorpej 	for (cp = str; *cp; cp++)
    145      1.1   thorpej 		if (*cp == ' ' || *cp == '-')
    146  1.9.2.2   thorpej 			goto found;
    147      1.1   thorpej 
    148  1.9.2.2   thorpej 	return;
    149  1.9.2.2   thorpej 
    150  1.9.2.2   thorpej  found:
    151      1.7  jdolecek 	*cp++ = '\0';
    152      1.7  jdolecek 	while (*cp)
    153      1.7  jdolecek 		BOOT_FLAG(*cp++, *howtop);
    154      1.1   thorpej }
    155      1.1   thorpej 
    156      1.1   thorpej static void
    157  1.9.2.2   thorpej chain(entry, args, ssym, esym)
    158      1.1   thorpej 	void (*entry)();
    159      1.1   thorpej 	char *args;
    160  1.9.2.2   thorpej 	void *ssym, *esym;
    161      1.1   thorpej {
    162  1.9.2.2   thorpej 	extern char end[], *cp;
    163  1.9.2.2   thorpej 	u_int l, magic = 0x19730224;
    164      1.1   thorpej 
    165      1.1   thorpej 	freeall();
    166      1.1   thorpej 
    167      1.1   thorpej 	/*
    168  1.9.2.2   thorpej 	 * Stash pointer to start and end of symbol table after the argument
    169      1.1   thorpej 	 * strings.
    170      1.1   thorpej 	 */
    171      1.1   thorpej 	l = strlen(args) + 1;
    172  1.9.2.2   thorpej 	l = (l + 3) & ~3;			/* align */
    173  1.9.2.2   thorpej 	DPRINTF("magic @ %p\n", args + l);
    174  1.9.2.2   thorpej 	memcpy(args + l, &magic, sizeof(magic));
    175  1.9.2.2   thorpej 	l += sizeof(magic);
    176  1.9.2.2   thorpej 	DPRINTF("ssym @ %p\n", args + l);
    177  1.9.2.2   thorpej 	memcpy(args + l, &ssym, sizeof(ssym));
    178  1.9.2.2   thorpej 	l += sizeof(ssym);
    179  1.9.2.2   thorpej 	DPRINTF("esym @ %p\n", args + l);
    180  1.9.2.1     lukem 	memcpy(args + l, &esym, sizeof(esym));
    181      1.1   thorpej 	l += sizeof(esym);
    182  1.9.2.2   thorpej 	DPRINTF("args + l -> %p\n", args + l);
    183      1.1   thorpej 
    184      1.1   thorpej 	OF_chain((void *)RELOC, end - (char *)RELOC, entry, args, l);
    185      1.1   thorpej 	panic("chain");
    186      1.1   thorpej }
    187      1.1   thorpej 
    188      1.4   mycroft __dead void
    189      1.4   mycroft _rtt()
    190      1.4   mycroft {
    191      1.4   mycroft 
    192      1.4   mycroft 	OF_exit();
    193      1.4   mycroft }
    194      1.4   mycroft 
    195      1.1   thorpej void
    196      1.1   thorpej main()
    197      1.1   thorpej {
    198      1.1   thorpej 	extern char bootprog_name[], bootprog_rev[],
    199  1.9.2.2   thorpej 		    bootprog_maker[], bootprog_date[];
    200  1.9.2.2   thorpej 	int chosen, options;
    201      1.1   thorpej 	char bootline[512];		/* Should check size? */
    202      1.1   thorpej 	char *cp;
    203  1.9.2.2   thorpej 	u_long marks[MARK_MAX];
    204  1.9.2.2   thorpej 	u_int32_t entry;
    205  1.9.2.2   thorpej 	void *ssym, *esym;
    206  1.9.2.2   thorpej 
    207  1.9.2.2   thorpej 	printf("\n");
    208      1.1   thorpej 	printf(">> %s, Revision %s\n", bootprog_name, bootprog_rev);
    209      1.1   thorpej 	printf(">> (%s, %s)\n", bootprog_maker, bootprog_date);
    210      1.1   thorpej 
    211      1.1   thorpej 	/*
    212      1.1   thorpej 	 * Get the boot arguments from Openfirmware
    213      1.1   thorpej 	 */
    214      1.4   mycroft 	if ((chosen = OF_finddevice("/chosen")) == -1 ||
    215      1.4   mycroft 	    OF_getprop(chosen, "bootpath", bootdev, sizeof bootdev) < 0 ||
    216      1.4   mycroft 	    OF_getprop(chosen, "bootargs", bootline, sizeof bootline) < 0) {
    217      1.1   thorpej 		printf("Invalid Openfirmware environment\n");
    218      1.4   mycroft 		OF_exit();
    219      1.1   thorpej 	}
    220      1.4   mycroft 
    221      1.1   thorpej 	prom2boot(bootdev);
    222      1.1   thorpej 	parseargs(bootline, &boothowto);
    223  1.9.2.2   thorpej 	DPRINTF("bootline=%s\n", bootline);
    224      1.4   mycroft 
    225      1.1   thorpej 	for (;;) {
    226  1.9.2.2   thorpej 		int i;
    227  1.9.2.2   thorpej 
    228      1.1   thorpej 		if (boothowto & RB_ASKNAME) {
    229      1.1   thorpej 			printf("Boot: ");
    230      1.1   thorpej 			gets(bootline);
    231      1.1   thorpej 			parseargs(bootline, &boothowto);
    232      1.1   thorpej 		}
    233  1.9.2.2   thorpej 
    234  1.9.2.2   thorpej 		if (bootline[0]) {
    235  1.9.2.2   thorpej 			kernels[0] = bootline;
    236  1.9.2.2   thorpej 			kernels[1] = NULL;
    237  1.9.2.2   thorpej 		}
    238  1.9.2.2   thorpej 
    239  1.9.2.2   thorpej 		for (i = 0; kernels[i]; i++) {
    240  1.9.2.2   thorpej 			DPRINTF("Trying %s\n", kernels[i]);
    241  1.9.2.2   thorpej 
    242  1.9.2.2   thorpej 			marks[MARK_START] = 0;
    243  1.9.2.2   thorpej 			if (loadfile(kernels[i], marks, LOAD_KERNEL) >= 0)
    244  1.9.2.2   thorpej 				goto loaded;
    245  1.9.2.2   thorpej 		}
    246  1.9.2.2   thorpej 
    247      1.1   thorpej 		boothowto |= RB_ASKNAME;
    248      1.1   thorpej 	}
    249  1.9.2.2   thorpej  loaded:
    250  1.9.2.2   thorpej 
    251      1.1   thorpej #ifdef	__notyet__
    252      1.1   thorpej 	OF_setprop(chosen, "bootpath", opened_name, strlen(opened_name) + 1);
    253      1.1   thorpej 	cp = bootline;
    254      1.1   thorpej #else
    255      1.1   thorpej 	strcpy(bootline, opened_name);
    256      1.1   thorpej 	cp = bootline + strlen(bootline);
    257      1.1   thorpej 	*cp++ = ' ';
    258      1.1   thorpej #endif
    259      1.1   thorpej 	*cp = '-';
    260      1.1   thorpej 	if (boothowto & RB_ASKNAME)
    261      1.1   thorpej 		*++cp = 'a';
    262      1.1   thorpej 	if (boothowto & RB_SINGLE)
    263      1.1   thorpej 		*++cp = 's';
    264      1.1   thorpej 	if (boothowto & RB_KDB)
    265      1.1   thorpej 		*++cp = 'd';
    266      1.1   thorpej 	if (*cp == '-')
    267      1.1   thorpej #ifdef	__notyet__
    268      1.1   thorpej 		*cp = 0;
    269      1.1   thorpej #else
    270      1.1   thorpej 		*--cp = 0;
    271      1.1   thorpej #endif
    272      1.1   thorpej 	else
    273      1.1   thorpej 		*++cp = 0;
    274      1.1   thorpej #ifdef	__notyet__
    275      1.1   thorpej 	OF_setprop(chosen, "bootargs", bootline, strlen(bootline) + 1);
    276      1.1   thorpej #endif
    277  1.9.2.2   thorpej 
    278  1.9.2.2   thorpej 	entry = marks[MARK_ENTRY];
    279  1.9.2.2   thorpej 	ssym = (void *)marks[MARK_SYM];
    280  1.9.2.2   thorpej 	esym = (void *)marks[MARK_END];
    281  1.9.2.2   thorpej 
    282  1.9.2.2   thorpej 	printf(" start=0x%x\n", entry);
    283  1.9.2.2   thorpej 	__syncicache((void *)entry, (u_int)ssym - (u_int)entry);
    284  1.9.2.2   thorpej 	chain((void *)entry, bootline, ssym, esym);
    285      1.1   thorpej 
    286      1.4   mycroft 	OF_exit();
    287      1.1   thorpej }
    288