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