Home | History | Annotate | Line # | Download | only in ofwboot
boot.c revision 1.19
      1 /*	$NetBSD: boot.c,v 1.19 2008/01/24 19:52:53 garbled 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 "boot.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",
    111 			   "/netbsd.gz", "onetbsd", NULL };
    112 static char *kernels64[] = { "/netbsd.ofppc64", "/netbsd64", "/netbsd64.gz",
    113 			     "onetbsd64", "/netbsd.ofppc", "/netbsd",
    114 			     "/netbsd.gz", "onetbsd", NULL };
    115 
    116 static void
    117 prom2boot(char *dev)
    118 {
    119 	char *cp, *ocp;
    120 
    121 	ocp = dev;
    122 	cp = dev + strlen(dev) - 1;
    123 	for (; cp >= ocp; cp--) {
    124 		if (*cp == ':') {
    125 			*cp = '\0';
    126 			return;
    127 		}
    128 	}
    129 }
    130 
    131 static void
    132 parseargs(char *str, int *howtop)
    133 {
    134 	char *cp;
    135 
    136 	/* Allow user to drop back to the PROM. */
    137 	if (strcmp(str, "exit") == 0)
    138 		OF_exit();
    139 	if (strcmp(str, "halt") == 0)
    140 		OF_exit();
    141 	if (strcmp(str, "reboot") == 0)
    142 		OF_boot("");
    143 
    144 	*howtop = 0;
    145 
    146 	for (cp = str; *cp; cp++)
    147 		if (*cp == ' ' || *cp == '-')
    148 			goto found;
    149 
    150 	return;
    151 
    152  found:
    153 	*cp++ = '\0';
    154 	while (*cp)
    155 		BOOT_FLAG(*cp++, *howtop);
    156 }
    157 
    158 static void
    159 chain(boot_entry_t entry, char *args, void *ssym, void *esym)
    160 {
    161 	extern char end[], *cp;
    162 	u_int l, magic = 0x19730224;
    163 
    164 	/*
    165 	 * Stash pointer to start and end of symbol table after the argument
    166 	 * strings.
    167 	 */
    168 	l = strlen(args) + 1;
    169 	DPRINTF("ssym @ %p\n", args + l);
    170 	memcpy(args + l, &ssym, sizeof(ssym));
    171 	l += sizeof(ssym);
    172 	DPRINTF("esym @ %p\n", args + l);
    173 	memcpy(args + l, &esym, sizeof(esym));
    174 	l += sizeof(esym);
    175 	DPRINTF("magic @ %p\n", args + l);
    176 	memcpy(args + l, &magic, sizeof(magic));
    177 	l += sizeof(magic);
    178 	DPRINTF("args + l -> %p\n", args + l);
    179 
    180 	OF_chain((void *) RELOC, end - (char *)RELOC, entry, args, l);
    181 	panic("chain");
    182 }
    183 
    184 __dead void
    185 _rtt(void)
    186 {
    187 
    188 	OF_exit();
    189 }
    190 
    191 void
    192 main(void)
    193 {
    194 	extern char bootprog_name[], bootprog_rev[],
    195 		    bootprog_maker[], bootprog_date[];
    196 	int chosen, options, cpu, cpunode, j, is64=0;
    197 	char bootline[512];		/* Should check size? */
    198 	char *cp;
    199 	u_long marks[MARK_MAX];
    200 	u_int32_t entry;
    201 	void *ssym, *esym;
    202 
    203 	printf("\n");
    204 	printf(">> %s, Revision %s\n", bootprog_name, bootprog_rev);
    205 	printf(">> (%s, %s)\n", bootprog_maker, bootprog_date);
    206 
    207 	/*
    208 	 * Get the boot arguments from Openfirmware
    209 	 */
    210 	if ((chosen = OF_finddevice("/chosen")) == -1 ||
    211 	    OF_getprop(chosen, "bootpath", bootdev, sizeof bootdev) < 0 ||
    212 	    OF_getprop(chosen, "bootargs", bootline, sizeof bootline) < 0) {
    213 		printf("Invalid Openfirmware environment\n");
    214 		OF_exit();
    215 	}
    216 
    217 	/* lets see if we can guess the 64bittedness */
    218 	if (OF_getprop(chosen, "cpu", &cpu, sizeof cpu) ==  sizeof(cpu)) {
    219 		cpunode = OF_instance_to_package(cpu);
    220 		if (OF_getprop(cpunode, "64-bit", &j, sizeof j) >= 0) {
    221 			is64 = 1;
    222 		}
    223 	}
    224 
    225 	prom2boot(bootdev);
    226 	parseargs(bootline, &boothowto);
    227 	DPRINTF("bootline=%s\n", bootline);
    228 
    229 	for (;;) {
    230 		int i;
    231 
    232 		if (boothowto & RB_ASKNAME) {
    233 			printf("Boot: ");
    234 			gets(bootline);
    235 			parseargs(bootline, &boothowto);
    236 		}
    237 
    238 		if (bootline[0]) {
    239 			kernels[0] = bootline;
    240 			kernels[1] = NULL;
    241 		}
    242 		if (!bootline[0] && is64) {
    243 			for (i = 0; kernels64[i]; i++) {
    244 				DPRINTF("Trying %s\n", kernels64[i]);
    245 
    246 				marks[MARK_START] = 0;
    247 				if (loadfile(kernels64[i], marks, LOAD_KERNEL) >= 0)
    248 					goto loaded;
    249 			}
    250 		} else {
    251 			for (i = 0; kernels[i]; i++) {
    252 				DPRINTF("Trying %s\n", kernels[i]);
    253 
    254 				marks[MARK_START] = 0;
    255 				if (loadfile(kernels[i], marks, LOAD_KERNEL) >= 0)
    256 					goto loaded;
    257 			}
    258 		}
    259 
    260 		boothowto |= RB_ASKNAME;
    261 	}
    262  loaded:
    263 
    264 #ifdef	__notyet__
    265 	OF_setprop(chosen, "bootpath", opened_name, strlen(opened_name) + 1);
    266 	cp = bootline;
    267 #else
    268 	strcpy(bootline, opened_name);
    269 	cp = bootline + strlen(bootline);
    270 	*cp++ = ' ';
    271 #endif
    272 	*cp = '-';
    273 	if (boothowto & RB_ASKNAME)
    274 		*++cp = 'a';
    275 	if (boothowto & RB_SINGLE)
    276 		*++cp = 's';
    277 	if (boothowto & RB_KDB)
    278 		*++cp = 'd';
    279 	if (*cp == '-')
    280 #ifdef	__notyet__
    281 		*cp = 0;
    282 #else
    283 		*--cp = 0;
    284 #endif
    285 	else
    286 		*++cp = 0;
    287 #ifdef	__notyet__
    288 	OF_setprop(chosen, "bootargs", bootline, strlen(bootline) + 1);
    289 #endif
    290 
    291 	entry = marks[MARK_ENTRY];
    292 	ssym = (void *)marks[MARK_SYM];
    293 	esym = (void *)marks[MARK_END];
    294 
    295 	printf(" start=0x%x\n", entry);
    296 	__syncicache((void *) entry, (u_int) ssym - (u_int) entry);
    297 	chain((boot_entry_t) entry, bootline, ssym, esym);
    298 
    299 	OF_exit();
    300 }
    301