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