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