Home | History | Annotate | Line # | Download | only in ofwboot
boot.c revision 1.15
      1 /*	$NetBSD: boot.c,v 1.15 2002/03/29 20:31:52 tsutsui 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 #include <sys/param.h>
     81 #include <sys/boot_flag.h>
     82 
     83 #include <lib/libsa/stand.h>
     84 #include <lib/libsa/loadfile.h>
     85 #include <lib/libkern/libkern.h>
     86 
     87 #include "ofdev.h"
     88 #include "openfirm.h"
     89 
     90 #ifdef DEBUG
     91 # define DPRINTF printf
     92 #else
     93 # define DPRINTF while (0) printf
     94 #endif
     95 
     96 char bootdev[128];
     97 char bootfile[128];
     98 int boothowto;
     99 
    100 static ofw_version = 0;
    101 static char *kernels[] = { "/netbsd", "/netbsd.gz", "/netbsd.macppc", NULL };
    102 
    103 static void
    104 prom2boot(dev)
    105 	char *dev;
    106 {
    107 	char *cp;
    108 
    109 	cp = dev + strlen(dev) - 1;
    110 	for (; *cp; cp--) {
    111 		if (*cp == ':') {
    112 			if (ofw_version < 3) {
    113 				/* sd@0:0 -> sd@0 */
    114 				*cp = 0;
    115 				break;
    116 			} else {
    117 				/* disk@0:5,boot -> disk@0:0 */
    118 				strcpy(cp, ":0");
    119 				break;
    120 			}
    121 		}
    122 	}
    123 }
    124 
    125 static void
    126 parseargs(str, howtop)
    127 	char *str;
    128 	int *howtop;
    129 {
    130 	char *cp;
    131 
    132 	/* Allow user to drop back to the PROM. */
    133 	if (strcmp(str, "exit") == 0)
    134 		OF_exit();
    135 
    136 	*howtop = 0;
    137 
    138 	cp = str;
    139 	if (*cp == '-')
    140 		goto found;
    141 	for (cp = str; *cp; cp++)
    142 		if (*cp == ' ')
    143 			goto found;
    144 	return;
    145 
    146 found:
    147 	*cp++ = 0;
    148 	while (*cp)
    149 		BOOT_FLAG(*cp++, *howtop);
    150 }
    151 
    152 static void
    153 chain(entry, args, ssym, esym)
    154 	void (*entry)();
    155 	char *args;
    156 	void *ssym, *esym;
    157 {
    158 	extern char end[];
    159 	int l;
    160 
    161 	/*
    162 	 * Stash pointer to end of symbol table after the argument
    163 	 * strings.
    164 	 */
    165 	l = strlen(args) + 1;
    166 	memcpy(args + l, &ssym, sizeof(ssym));
    167 	l += sizeof(ssym);
    168 	memcpy(args + l, &esym, sizeof(esym));
    169 	l += sizeof(esym);
    170 	l += sizeof(int);	/* XXX */
    171 
    172 	OF_chain((void *)RELOC, end - (char *)RELOC, entry, args, l);
    173 	panic("chain");
    174 }
    175 
    176 __dead void
    177 _rtt()
    178 {
    179 
    180 	OF_exit();
    181 }
    182 
    183 void
    184 main()
    185 {
    186 	extern char bootprog_name[], bootprog_rev[],
    187 		    bootprog_maker[], bootprog_date[];
    188 	int chosen, options, openprom;
    189 	char bootline[512];		/* Should check size? */
    190 	char *cp;
    191 	u_long marks[MARK_MAX];
    192 	u_int32_t entry;
    193 	void *ssym, *esym;
    194 
    195 	printf("\n");
    196 	printf(">> %s, Revision %s\n", bootprog_name, bootprog_rev);
    197 	printf(">> (%s, %s)\n", bootprog_maker, bootprog_date);
    198 
    199 	/*
    200 	 * Figure out what version of Open Firmware...
    201 	 */
    202 	if ((openprom = OF_finddevice("/openprom")) != -1) {
    203 		char model[32];
    204 
    205 		memset(model, 0, sizeof model);
    206 		OF_getprop(openprom, "model", model, sizeof model);
    207 		for (cp = model; *cp; cp++)
    208 			if (*cp >= '0' && *cp <= '9') {
    209 				ofw_version = *cp - '0';
    210 				break;
    211 			}
    212 		DPRINTF(">> Open Firmware version %d.x\n", ofw_version);
    213 	}
    214 
    215 	/*
    216 	 * Get the boot arguments from Openfirmware
    217 	 */
    218 	if ((chosen = OF_finddevice("/chosen")) == -1 ||
    219 	    OF_getprop(chosen, "bootpath", bootdev, sizeof bootdev) < 0 ||
    220 	    OF_getprop(chosen, "bootargs", bootline, sizeof bootline) < 0) {
    221 		printf("Invalid Openfirmware environment\n");
    222 		OF_exit();
    223 	}
    224 
    225 	/*
    226 	 * Some versions of Openfirmware sets bootpath to "".
    227 	 * We use boot-device instead if it occurs.
    228 	 */
    229 	if (bootdev[0] == 0) {
    230 		printf("Cannot use bootpath\n");
    231 		if ((options = OF_finddevice("/options")) == -1 ||
    232 		    OF_getprop(options, "boot-device", bootdev,
    233 			       sizeof bootdev) < 0) {
    234 			printf("Invalid Openfirmware environment\n");
    235 			OF_exit();
    236 		}
    237 		printf("Using boot-device instead\n");
    238 	}
    239 
    240 	prom2boot(bootdev);
    241 	parseargs(bootline, &boothowto);
    242 	DPRINTF("bootline=%s\n", bootline);
    243 
    244 	for (;;) {
    245 		int i;
    246 
    247 		if (boothowto & RB_ASKNAME) {
    248 			printf("Boot: ");
    249 			gets(bootline);
    250 			parseargs(bootline, &boothowto);
    251 		}
    252 
    253 		if (bootline[0]) {
    254 			kernels[0] = bootline;
    255 			kernels[1] = NULL;
    256 		}
    257 
    258 		for (i = 0; kernels[i]; i++) {
    259 			DPRINTF("Trying %s\n", kernels[i]);
    260 
    261 			marks[MARK_START] = 0;
    262 			if (loadfile(kernels[i], marks, LOAD_KERNEL) >= 0)
    263 				goto loaded;
    264 		}
    265 		boothowto |= RB_ASKNAME;
    266 	}
    267 loaded:
    268 
    269 #ifdef	__notyet__
    270 	OF_setprop(chosen, "bootpath", opened_name, strlen(opened_name) + 1);
    271 	cp = bootline;
    272 #else
    273 	strcpy(bootline, opened_name);
    274 	cp = bootline + strlen(bootline);
    275 	*cp++ = ' ';
    276 #endif
    277 	*cp = '-';
    278 	if (boothowto & RB_ASKNAME)
    279 		*++cp = 'a';
    280 	if (boothowto & RB_SINGLE)
    281 		*++cp = 's';
    282 	if (boothowto & RB_KDB)
    283 		*++cp = 'd';
    284 	if (*cp == '-')
    285 #ifdef	__notyet__
    286 		*cp = 0;
    287 #else
    288 		*--cp = 0;
    289 #endif
    290 	else
    291 		*++cp = 0;
    292 #ifdef	__notyet__
    293 	OF_setprop(chosen, "bootargs", bootline, strlen(bootline) + 1);
    294 #endif
    295 
    296 	entry = marks[MARK_ENTRY];
    297 	ssym = (void *)marks[MARK_SYM];
    298 	esym = (void *)marks[MARK_END];
    299 
    300 	printf(" start=0x%x\n", entry);
    301 	__syncicache((void *)entry, (u_int)ssym - (u_int)entry);
    302 	chain((void *)entry, bootline, ssym, esym);
    303 
    304 	OF_exit();
    305 }
    306