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