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