Home | History | Annotate | Line # | Download | only in ofwboot
boot.c revision 1.13
      1 /*	$NetBSD: boot.c,v 1.13 2003/06/26 20:44:51 aymeric 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 "alloc.h"
     96 #include "ofdev.h"
     97 #include "openfirm.h"
     98 
     99 #ifdef DEBUG
    100 # define DPRINTF printf
    101 #else
    102 # define DPRINTF while (/*CONSTCOND*/0) printf
    103 #endif
    104 
    105 char bootdev[128];
    106 char bootfile[128];
    107 int boothowto;
    108 int debug;
    109 
    110 static char *kernels[] = { "/netbsd.ofppc", "/netbsd", "/netbsd.gz", NULL };
    111 
    112 static void
    113 prom2boot(char *dev)
    114 {
    115 	char *cp, *ocp;
    116 
    117 	ocp = dev;
    118 	cp = dev + strlen(dev) - 1;
    119 	for (; cp >= ocp; cp--) {
    120 		if (*cp == ':') {
    121 			*cp = '\0';
    122 			return;
    123 		}
    124 	}
    125 }
    126 
    127 static void
    128 parseargs(char *str, 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 	if (strcmp(str, "halt") == 0)
    136 		OF_exit();
    137 	if (strcmp(str, "reboot") == 0)
    138 		OF_boot("");
    139 
    140 	*howtop = 0;
    141 
    142 	for (cp = str; *cp; cp++)
    143 		if (*cp == ' ' || *cp == '-')
    144 			goto found;
    145 
    146 	return;
    147 
    148  found:
    149 	*cp++ = '\0';
    150 	while (*cp)
    151 		BOOT_FLAG(*cp++, *howtop);
    152 }
    153 
    154 static void
    155 chain(void entry(), char *args, void *ssym, void *esym)
    156 {
    157 	extern char end[], *cp;
    158 	u_int l, magic = 0x19730224;
    159 
    160 	freeall();
    161 
    162 	/*
    163 	 * Stash pointer to start and end of symbol table after the argument
    164 	 * strings.
    165 	 */
    166 	l = strlen(args) + 1;
    167 	l = (l + 3) & ~3;			/* align */
    168 	DPRINTF("magic @ %p\n", args + l);
    169 	memcpy(args + l, &magic, sizeof(magic));
    170 	l += sizeof(magic);
    171 	DPRINTF("ssym @ %p\n", args + l);
    172 	memcpy(args + l, &ssym, sizeof(ssym));
    173 	l += sizeof(ssym);
    174 	DPRINTF("esym @ %p\n", args + l);
    175 	memcpy(args + l, &esym, sizeof(esym));
    176 	l += sizeof(esym);
    177 	DPRINTF("args + l -> %p\n", args + l);
    178 
    179 	OF_chain((void *)RELOC, end - (char *)RELOC, entry, args, l);
    180 	panic("chain");
    181 }
    182 
    183 __dead void
    184 _rtt(void)
    185 {
    186 
    187 	OF_exit();
    188 }
    189 
    190 void
    191 main(void)
    192 {
    193 	extern char bootprog_name[], bootprog_rev[],
    194 		    bootprog_maker[], bootprog_date[];
    195 	int chosen, options;
    196 	char bootline[512];		/* Should check size? */
    197 	char *cp;
    198 	u_long marks[MARK_MAX];
    199 	u_int32_t entry;
    200 	void *ssym, *esym;
    201 
    202 	printf("\n");
    203 	printf(">> %s, Revision %s\n", bootprog_name, bootprog_rev);
    204 	printf(">> (%s, %s)\n", bootprog_maker, bootprog_date);
    205 
    206 	/*
    207 	 * Get the boot arguments from Openfirmware
    208 	 */
    209 	if ((chosen = OF_finddevice("/chosen")) == -1 ||
    210 	    OF_getprop(chosen, "bootpath", bootdev, sizeof bootdev) < 0 ||
    211 	    OF_getprop(chosen, "bootargs", bootline, sizeof bootline) < 0) {
    212 		printf("Invalid Openfirmware environment\n");
    213 		OF_exit();
    214 	}
    215 
    216 	prom2boot(bootdev);
    217 	parseargs(bootline, &boothowto);
    218 	DPRINTF("bootline=%s\n", bootline);
    219 
    220 	for (;;) {
    221 		int i;
    222 
    223 		if (boothowto & RB_ASKNAME) {
    224 			printf("Boot: ");
    225 			gets(bootline);
    226 			parseargs(bootline, &boothowto);
    227 		}
    228 
    229 		if (bootline[0]) {
    230 			kernels[0] = bootline;
    231 			kernels[1] = NULL;
    232 		}
    233 
    234 		for (i = 0; kernels[i]; i++) {
    235 			DPRINTF("Trying %s\n", kernels[i]);
    236 
    237 			marks[MARK_START] = 0;
    238 			if (loadfile(kernels[i], marks, LOAD_KERNEL) >= 0)
    239 				goto loaded;
    240 		}
    241 
    242 		boothowto |= RB_ASKNAME;
    243 	}
    244  loaded:
    245 
    246 #ifdef	__notyet__
    247 	OF_setprop(chosen, "bootpath", opened_name, strlen(opened_name) + 1);
    248 	cp = bootline;
    249 #else
    250 	strcpy(bootline, opened_name);
    251 	cp = bootline + strlen(bootline);
    252 	*cp++ = ' ';
    253 #endif
    254 	*cp = '-';
    255 	if (boothowto & RB_ASKNAME)
    256 		*++cp = 'a';
    257 	if (boothowto & RB_SINGLE)
    258 		*++cp = 's';
    259 	if (boothowto & RB_KDB)
    260 		*++cp = 'd';
    261 	if (*cp == '-')
    262 #ifdef	__notyet__
    263 		*cp = 0;
    264 #else
    265 		*--cp = 0;
    266 #endif
    267 	else
    268 		*++cp = 0;
    269 #ifdef	__notyet__
    270 	OF_setprop(chosen, "bootargs", bootline, strlen(bootline) + 1);
    271 #endif
    272 
    273 	entry = marks[MARK_ENTRY];
    274 	ssym = (void *)marks[MARK_SYM];
    275 	esym = (void *)marks[MARK_END];
    276 
    277 	printf(" start=0x%x\n", entry);
    278 	__syncicache((void *)entry, (u_int)ssym - (u_int)entry);
    279 	chain((void *)entry, bootline, ssym, esym);
    280 
    281 	OF_exit();
    282 }
    283