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