Home | History | Annotate | Line # | Download | only in ofwboot
boot.c revision 1.27.16.1
      1 /*	$NetBSD: boot.c,v 1.27.16.1 2016/07/09 20:24:53 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  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Copyright (C) 1995, 1996 Wolfgang Solfrank.
     34  * Copyright (C) 1995, 1996 TooLs GmbH.
     35  * All rights reserved.
     36  *
     37  * ELF support derived from NetBSD/alpha's boot loader, written
     38  * by Christopher G. Demetriou.
     39  *
     40  * Redistribution and use in source and binary forms, with or without
     41  * modification, are permitted provided that the following conditions
     42  * are met:
     43  * 1. Redistributions of source code must retain the above copyright
     44  *    notice, this list of conditions and the following disclaimer.
     45  * 2. Redistributions in binary form must reproduce the above copyright
     46  *    notice, this list of conditions and the following disclaimer in the
     47  *    documentation and/or other materials provided with the distribution.
     48  * 3. All advertising materials mentioning features or use of this software
     49  *    must display the following acknowledgement:
     50  *	This product includes software developed by TooLs GmbH.
     51  * 4. The name of TooLs GmbH may not be used to endorse or promote products
     52  *    derived from this software without specific prior written permission.
     53  *
     54  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
     55  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     56  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     57  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     58  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     59  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     60  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     61  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     62  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     63  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     64  */
     65 
     66 /*
     67  * First try for the boot code
     68  *
     69  * Input syntax is:
     70  *	[promdev[{:|,}partition]]/[filename] [flags]
     71  */
     72 
     73 #include "boot.h"
     74 
     75 #include <sys/param.h>
     76 #include <sys/boot_flag.h>
     77 #include <sys/disklabel.h>
     78 
     79 #include <lib/libsa/stand.h>
     80 #include <lib/libsa/loadfile.h>
     81 #include <lib/libkern/libkern.h>
     82 
     83 #include "ofdev.h"
     84 #include "openfirm.h"
     85 
     86 extern void __syncicache(void *, size_t); /* in libkern */
     87 
     88 
     89 #ifdef DEBUG
     90 # define DPRINTF printf
     91 #else
     92 # define DPRINTF while (0) printf
     93 #endif
     94 
     95 char bootdev[MAXBOOTPATHLEN];
     96 char bootfile[MAXBOOTPATHLEN];
     97 int boothowto;
     98 bool floppyboot;
     99 int ofw_version = 0;
    100 
    101 static const char *kernels[] = { "/netbsd", "/netbsd.gz", "/netbsd.macppc", NULL };
    102 
    103 static void
    104 prom2boot(char *dev)
    105 {
    106 	char *cp;
    107 
    108 	cp = dev + strlen(dev) - 1;
    109 	for (; *cp; cp--) {
    110 		if (*cp == ':') {
    111 			if (ofw_version < 3) {
    112 				/* sd@0:0 -> sd@0 */
    113 				*cp = 0;
    114 				break;
    115 			} else {
    116 				/* disk@0:5,boot -> disk@0:0 */
    117 				strcpy(cp, ":0");
    118 				break;
    119 			}
    120 		}
    121 	}
    122 }
    123 
    124 static void
    125 parseargs(char *str, int *howtop)
    126 {
    127 	char *cp;
    128 
    129 	/* Allow user to drop back to the PROM. */
    130 	if (strcmp(str, "exit") == 0)
    131 		OF_exit();
    132 
    133 	*howtop = 0;
    134 
    135 	cp = str;
    136 	if (*cp == '-')
    137 		goto found;
    138 	for (cp = str; *cp; cp++)
    139 		if (*cp == ' ')
    140 			goto found;
    141 	return;
    142 
    143 found:
    144 	*cp++ = 0;
    145 	while (*cp)
    146 		BOOT_FLAG(*cp++, *howtop);
    147 }
    148 
    149 static bool
    150 is_floppyboot(const char *path, const char *defaultdev)
    151 {
    152 	char dev[MAXBOOTPATHLEN];
    153 	char nam[16];
    154 	int handle, rv;
    155 
    156 	if (parsefilepath(path, dev, NULL, NULL)) {
    157 		if (dev[0] == '\0' && defaultdev != NULL)
    158 			strlcpy(dev, defaultdev, sizeof(dev));
    159 
    160 		/* check properties */
    161 		handle = OF_finddevice(dev);
    162 		if (handle != -1) {
    163 			rv = OF_getprop(handle, "name", nam, sizeof(nam));
    164 			if (rv >= 0 &&
    165 			    (strcmp(nam, "swim3") == 0 ||
    166 			     strcmp(nam, "floppy") == 0))
    167 				return true;
    168 		}
    169 
    170 		/* also check devalias */
    171 		if (strcmp(dev, "fd") == 0)
    172 			return true;
    173 	}
    174 
    175 	return false;
    176 }
    177 
    178 static void
    179 chain(boot_entry_t entry, char *args, void *ssym, void *esym)
    180 {
    181 	extern char end[];
    182 	int l;
    183 
    184 	freeall();
    185 
    186 	/*
    187 	 * Stash pointer to end of symbol table after the argument
    188 	 * strings.
    189 	 */
    190 	l = strlen(args) + 1;
    191 	memcpy(args + l, &ssym, sizeof(ssym));
    192 	l += sizeof(ssym);
    193 	memcpy(args + l, &esym, sizeof(esym));
    194 	l += sizeof(esym);
    195 	l += sizeof(int);	/* XXX */
    196 
    197 	OF_chain((void *) RELOC, end - (char *) RELOC, entry, args, l);
    198 	panic("chain");
    199 }
    200 
    201 __dead void
    202 _rtt(void)
    203 {
    204 
    205 	OF_exit();
    206 }
    207 
    208 void
    209 main(void)
    210 {
    211 	extern char bootprog_name[], bootprog_rev[];
    212 	int chosen, options, openprom;
    213 	char bootline[512];		/* Should check size? */
    214 	char *cp;
    215 	u_long marks[MARK_MAX];
    216 	u_int32_t entry;
    217 	void *ssym, *esym;
    218 
    219 	printf("\n");
    220 	printf(">> %s, Revision %s\n", bootprog_name, bootprog_rev);
    221 
    222 	/*
    223 	 * Figure out what version of Open Firmware...
    224 	 */
    225 	if ((openprom = OF_finddevice("/openprom")) != -1) {
    226 		char model[32];
    227 
    228 		memset(model, 0, sizeof model);
    229 		OF_getprop(openprom, "model", model, sizeof model);
    230 		for (cp = model; *cp; cp++)
    231 			if (*cp >= '0' && *cp <= '9') {
    232 				ofw_version = *cp - '0';
    233 				break;
    234 			}
    235 		DPRINTF(">> Open Firmware version %d.x\n", ofw_version);
    236 	}
    237 
    238 	/*
    239 	 * Get the boot arguments from Openfirmware
    240 	 */
    241 	if ((chosen = OF_finddevice("/chosen")) == -1 ||
    242 	    OF_getprop(chosen, "bootpath", bootdev, sizeof bootdev) < 0 ||
    243 	    OF_getprop(chosen, "bootargs", bootline, sizeof bootline) < 0) {
    244 		printf("Invalid Openfirmware environment\n");
    245 		OF_exit();
    246 	}
    247 
    248 	/*
    249 	 * Some versions of Openfirmware sets bootpath to "".
    250 	 * We use boot-device instead if it occurs.
    251 	 */
    252 	if (bootdev[0] == 0) {
    253 		printf("Cannot use bootpath\n");
    254 		if ((options = OF_finddevice("/options")) == -1 ||
    255 		    OF_getprop(options, "boot-device", bootdev,
    256 			       sizeof bootdev) < 0) {
    257 			printf("Invalid Openfirmware environment\n");
    258 			OF_exit();
    259 		}
    260 		printf("Using boot-device instead\n");
    261 	}
    262 
    263 	prom2boot(bootdev);
    264 	parseargs(bootline, &boothowto);
    265 	DPRINTF("bootline=%s\n", bootline);
    266 
    267 	for (;;) {
    268 		int i, loadflag;
    269 
    270 		if (boothowto & RB_ASKNAME) {
    271 			printf("Boot: ");
    272 			kgets(bootline, sizeof(bootline));
    273 			parseargs(bootline, &boothowto);
    274 		}
    275 
    276 		if (bootline[0]) {
    277 			kernels[0] = bootline;
    278 			kernels[1] = NULL;
    279 		}
    280 
    281 		for (i = 0; kernels[i]; i++) {
    282 			floppyboot = is_floppyboot(kernels[i], bootdev);
    283 
    284 			DPRINTF("Trying %s%s\n", kernels[i],
    285 			    floppyboot ? " (floppyboot)" : "");
    286 
    287 			loadflag = LOAD_KERNEL;
    288 			if (floppyboot)
    289 				loadflag &= ~LOAD_BACKWARDS;
    290 
    291 			marks[MARK_START] = 0;
    292 			if (loadfile(kernels[i], marks, loadflag) >= 0)
    293 				goto loaded;
    294 		}
    295 		boothowto |= RB_ASKNAME;
    296 	}
    297 loaded:
    298 
    299 #ifdef	__notyet__
    300 	OF_setprop(chosen, "bootpath", opened_name, strlen(opened_name) + 1);
    301 	cp = bootline;
    302 #else
    303 	strcpy(bootline, opened_name);
    304 	cp = bootline + strlen(bootline);
    305 	*cp++ = ' ';
    306 #endif
    307 	*cp = '-';
    308 	if (boothowto & RB_ASKNAME)
    309 		*++cp = 'a';
    310 	if (boothowto & RB_USERCONF)
    311 		*++cp = 'c';
    312 	if (boothowto & RB_SINGLE)
    313 		*++cp = 's';
    314 	if (boothowto & RB_KDB)
    315 		*++cp = 'd';
    316 	if (*cp == '-')
    317 #ifdef	__notyet__
    318 		*cp = 0;
    319 #else
    320 		*--cp = 0;
    321 #endif
    322 	else
    323 		*++cp = 0;
    324 #ifdef	__notyet__
    325 	OF_setprop(chosen, "bootargs", bootline, strlen(bootline) + 1);
    326 #endif
    327 
    328 	entry = marks[MARK_ENTRY];
    329 	ssym = (void *)marks[MARK_SYM];
    330 	esym = (void *)marks[MARK_END];
    331 
    332 	printf(" start=0x%x\n", entry);
    333 	__syncicache((void *)(uintptr_t)entry, (size_t)ssym - entry);
    334 	chain((boot_entry_t)(uintptr_t)entry, bootline, ssym, esym);
    335 
    336 	OF_exit();
    337 }
    338 
    339 #ifdef HAVE_CHANGEDISK_HOOK
    340 void
    341 changedisk_hook(struct open_file *of)
    342 {
    343 	struct of_dev *op = of->f_devdata;
    344 	int c;
    345 
    346 	OF_call_method("eject", op->handle, 0, 0);
    347 
    348 	c = getchar();
    349 	if (c == 'q') {
    350 		printf("quit\n");
    351 		OF_exit();
    352 	}
    353 
    354 	OF_call_method("close", op->handle, 0, 0);
    355 	OF_call_method("open", op->handle, 0, 0);
    356 }
    357 #endif
    358