Home | History | Annotate | Line # | Download | only in ofwboot
boot.c revision 1.4
      1  1.4  tsubai /*	$NetBSD: boot.c,v 1.4 1999/03/05 06:18:38 tsubai 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  * 3. All advertising materials mentioning features or use of this software
     19  1.1  tsubai  *    must display the following acknowledgement:
     20  1.1  tsubai  *	This product includes software developed by the NetBSD
     21  1.1  tsubai  *	Foundation, Inc. and its contributors.
     22  1.1  tsubai  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.1  tsubai  *    contributors may be used to endorse or promote products derived
     24  1.1  tsubai  *    from this software without specific prior written permission.
     25  1.1  tsubai  *
     26  1.1  tsubai  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.1  tsubai  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.1  tsubai  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1  tsubai  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.1  tsubai  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.1  tsubai  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1  tsubai  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1  tsubai  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.1  tsubai  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1  tsubai  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.1  tsubai  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1  tsubai  */
     38  1.1  tsubai 
     39  1.1  tsubai /*
     40  1.1  tsubai  * Copyright (C) 1995, 1996 Wolfgang Solfrank.
     41  1.1  tsubai  * Copyright (C) 1995, 1996 TooLs GmbH.
     42  1.1  tsubai  * All rights reserved.
     43  1.1  tsubai  *
     44  1.1  tsubai  * ELF support derived from NetBSD/alpha's boot loader, written
     45  1.1  tsubai  * by Christopher G. Demetriou.
     46  1.1  tsubai  *
     47  1.1  tsubai  * Redistribution and use in source and binary forms, with or without
     48  1.1  tsubai  * modification, are permitted provided that the following conditions
     49  1.1  tsubai  * are met:
     50  1.1  tsubai  * 1. Redistributions of source code must retain the above copyright
     51  1.1  tsubai  *    notice, this list of conditions and the following disclaimer.
     52  1.1  tsubai  * 2. Redistributions in binary form must reproduce the above copyright
     53  1.1  tsubai  *    notice, this list of conditions and the following disclaimer in the
     54  1.1  tsubai  *    documentation and/or other materials provided with the distribution.
     55  1.1  tsubai  * 3. All advertising materials mentioning features or use of this software
     56  1.1  tsubai  *    must display the following acknowledgement:
     57  1.1  tsubai  *	This product includes software developed by TooLs GmbH.
     58  1.1  tsubai  * 4. The name of TooLs GmbH may not be used to endorse or promote products
     59  1.1  tsubai  *    derived from this software without specific prior written permission.
     60  1.1  tsubai  *
     61  1.1  tsubai  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
     62  1.1  tsubai  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     63  1.1  tsubai  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     64  1.1  tsubai  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     65  1.1  tsubai  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     66  1.1  tsubai  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     67  1.1  tsubai  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     68  1.1  tsubai  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     69  1.1  tsubai  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     70  1.1  tsubai  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     71  1.1  tsubai  */
     72  1.1  tsubai 
     73  1.1  tsubai /*
     74  1.1  tsubai  * First try for the boot code
     75  1.1  tsubai  *
     76  1.1  tsubai  * Input syntax is:
     77  1.1  tsubai  *	[promdev[{:|,}partition]]/[filename] [flags]
     78  1.1  tsubai  */
     79  1.1  tsubai 
     80  1.1  tsubai #define	ELFSIZE		32		/* We use 32-bit ELF. */
     81  1.1  tsubai 
     82  1.1  tsubai #include <sys/param.h>
     83  1.1  tsubai #include <sys/exec.h>
     84  1.1  tsubai #include <sys/exec_elf.h>
     85  1.1  tsubai #include <sys/reboot.h>
     86  1.1  tsubai #include <sys/disklabel.h>
     87  1.1  tsubai 
     88  1.1  tsubai #include <lib/libsa/stand.h>
     89  1.1  tsubai #include <lib/libkern/libkern.h>
     90  1.1  tsubai 
     91  1.1  tsubai #include <machine/cpu.h>
     92  1.1  tsubai #include <machine/machine_type.h>
     93  1.1  tsubai 
     94  1.1  tsubai #include "ofdev.h"
     95  1.1  tsubai #include "openfirm.h"
     96  1.1  tsubai 
     97  1.1  tsubai char bootdev[128];
     98  1.1  tsubai char bootfile[128];
     99  1.1  tsubai int boothowto;
    100  1.1  tsubai int debug;
    101  1.1  tsubai 
    102  1.1  tsubai #ifdef POWERPC_BOOT_ELF
    103  1.1  tsubai int	elf_exec __P((int, Elf_Ehdr *, u_int32_t *, void **));
    104  1.1  tsubai #endif
    105  1.1  tsubai 
    106  1.1  tsubai #ifdef POWERPC_BOOT_AOUT
    107  1.1  tsubai int	aout_exec __P((int, struct exec *, u_int32_t *, void **));
    108  1.1  tsubai #endif
    109  1.1  tsubai 
    110  1.1  tsubai static void
    111  1.1  tsubai prom2boot(dev)
    112  1.1  tsubai 	char *dev;
    113  1.1  tsubai {
    114  1.1  tsubai 	char *cp;
    115  1.1  tsubai 
    116  1.1  tsubai 	for (cp = dev; *cp != '\0'; cp++)
    117  1.1  tsubai 		if (*cp == ':') {
    118  1.1  tsubai 			*cp = '\0';
    119  1.1  tsubai 			return;
    120  1.1  tsubai 		}
    121  1.1  tsubai }
    122  1.1  tsubai 
    123  1.1  tsubai static void
    124  1.1  tsubai parseargs(str, howtop)
    125  1.1  tsubai 	char *str;
    126  1.1  tsubai 	int *howtop;
    127  1.1  tsubai {
    128  1.1  tsubai 	char *cp;
    129  1.1  tsubai 
    130  1.1  tsubai 	/* Allow user to drop back to the PROM. */
    131  1.1  tsubai 	if (strcmp(str, "exit") == 0)
    132  1.1  tsubai 		OF_exit();
    133  1.1  tsubai 
    134  1.1  tsubai 	*howtop = 0;
    135  1.4  tsubai 
    136  1.4  tsubai 	cp = str;
    137  1.4  tsubai 	if (*cp == '-')
    138  1.4  tsubai 		goto found;
    139  1.1  tsubai 	for (cp = str; *cp; cp++)
    140  1.4  tsubai 		if (*cp == ' ')
    141  1.4  tsubai 			goto found;
    142  1.4  tsubai 	return;
    143  1.4  tsubai 
    144  1.4  tsubai found:
    145  1.1  tsubai 	*cp++ = 0;
    146  1.1  tsubai 	while (*cp) {
    147  1.1  tsubai 		switch (*cp++) {
    148  1.1  tsubai 		case 'a':
    149  1.1  tsubai 			*howtop |= RB_ASKNAME;
    150  1.1  tsubai 			break;
    151  1.1  tsubai 		case 's':
    152  1.1  tsubai 			*howtop |= RB_SINGLE;
    153  1.1  tsubai 			break;
    154  1.1  tsubai 		case 'd':
    155  1.1  tsubai 			*howtop |= RB_KDB;
    156  1.1  tsubai 			debug = 1;
    157  1.1  tsubai 			break;
    158  1.1  tsubai 		}
    159  1.1  tsubai 	}
    160  1.1  tsubai }
    161  1.1  tsubai 
    162  1.1  tsubai static void
    163  1.1  tsubai chain(entry, args, esym)
    164  1.1  tsubai 	void (*entry)();
    165  1.1  tsubai 	char *args;
    166  1.1  tsubai 	void *esym;
    167  1.1  tsubai {
    168  1.1  tsubai 	extern char end[];
    169  1.1  tsubai 	int l, machine_tag;
    170  1.1  tsubai 
    171  1.1  tsubai 	freeall();
    172  1.1  tsubai 
    173  1.1  tsubai 	/*
    174  1.1  tsubai 	 * Stash pointer to end of symbol table after the argument
    175  1.1  tsubai 	 * strings.
    176  1.1  tsubai 	 */
    177  1.1  tsubai 	l = strlen(args) + 1;
    178  1.1  tsubai 	bcopy(&esym, args + l, sizeof(esym));
    179  1.1  tsubai 	l += sizeof(esym);
    180  1.1  tsubai 
    181  1.1  tsubai 	/*
    182  1.1  tsubai 	 * Tell the kernel we're an OpenFirmware system.
    183  1.1  tsubai 	 */
    184  1.1  tsubai 	machine_tag = POWERPC_MACHINE_OPENFIRMWARE;
    185  1.1  tsubai 	bcopy(&machine_tag, args + l, sizeof(machine_tag));
    186  1.1  tsubai 	l += sizeof(machine_tag);
    187  1.1  tsubai 
    188  1.1  tsubai 	OF_chain((void *)RELOC, end - (char *)RELOC, entry, args, l);
    189  1.1  tsubai 	panic("chain");
    190  1.1  tsubai }
    191  1.1  tsubai 
    192  1.1  tsubai int
    193  1.1  tsubai loadfile(fd, args)
    194  1.1  tsubai 	int fd;
    195  1.1  tsubai 	char *args;
    196  1.1  tsubai {
    197  1.1  tsubai 	union {
    198  1.1  tsubai #ifdef POWERPC_BOOT_AOUT
    199  1.1  tsubai 		struct exec aout;
    200  1.1  tsubai #endif
    201  1.1  tsubai #ifdef POWERPC_BOOT_ELF
    202  1.1  tsubai 		Elf_Ehdr elf;
    203  1.1  tsubai #endif
    204  1.1  tsubai 	} hdr;
    205  1.1  tsubai 	int rval;
    206  1.1  tsubai 	u_int32_t entry;
    207  1.1  tsubai 	void *esym;
    208  1.1  tsubai 
    209  1.1  tsubai 	rval = 1;
    210  1.1  tsubai 	esym = NULL;
    211  1.1  tsubai 
    212  1.1  tsubai 	/* Load the header. */
    213  1.1  tsubai 	if (read(fd, &hdr, sizeof(hdr)) != sizeof(hdr)) {
    214  1.1  tsubai 		printf("read header: %s\n", strerror(errno));
    215  1.1  tsubai 		goto err;
    216  1.1  tsubai 	}
    217  1.1  tsubai 
    218  1.1  tsubai 	/* Determine file type, load kernel. */
    219  1.1  tsubai #ifdef POWERPC_BOOT_AOUT
    220  1.1  tsubai 	if (N_BADMAG(hdr.aout) == 0 && N_GETMID(hdr.aout) == MID_POWERPC) {
    221  1.1  tsubai 		rval = aout_exec(fd, &hdr.aout, &entry, &esym);
    222  1.1  tsubai 	} else
    223  1.1  tsubai #endif
    224  1.1  tsubai #ifdef POWERPC_BOOT_ELF
    225  1.1  tsubai 	if (memcmp(Elf_e_ident, hdr.elf.e_ident, Elf_e_siz) == 0) {
    226  1.1  tsubai 		rval = elf_exec(fd, &hdr.elf, &entry, &esym);
    227  1.1  tsubai 	} else
    228  1.1  tsubai #endif
    229  1.1  tsubai 	{
    230  1.1  tsubai 		printf("unknown executable format\n");
    231  1.1  tsubai 	}
    232  1.1  tsubai 
    233  1.1  tsubai 	if (rval)
    234  1.1  tsubai 		goto err;
    235  1.1  tsubai 
    236  1.1  tsubai 	printf(" start=0x%x\n", entry);
    237  1.1  tsubai 
    238  1.1  tsubai 	close(fd);
    239  1.1  tsubai 
    240  1.3  tsubai #if 0
    241  1.1  tsubai 	/* XXX this should be replaced w/ a mountroothook. */
    242  1.1  tsubai 	if (floppyboot) {
    243  1.1  tsubai 		printf("Please insert root disk and press ENTER ");
    244  1.1  tsubai 		getchar();
    245  1.1  tsubai 		printf("\n");
    246  1.1  tsubai 	}
    247  1.3  tsubai #endif
    248  1.1  tsubai 
    249  1.1  tsubai 	chain((void *)entry, args, esym);
    250  1.1  tsubai 	/* NOTREACHED */
    251  1.1  tsubai 
    252  1.1  tsubai  err:
    253  1.1  tsubai 	close(fd);
    254  1.1  tsubai 	return (rval);
    255  1.1  tsubai }
    256  1.1  tsubai 
    257  1.1  tsubai __dead void
    258  1.1  tsubai _rtt()
    259  1.1  tsubai {
    260  1.1  tsubai 
    261  1.1  tsubai 	OF_exit();
    262  1.1  tsubai }
    263  1.1  tsubai 
    264  1.1  tsubai #ifdef POWERPC_BOOT_AOUT
    265  1.1  tsubai int
    266  1.1  tsubai aout_exec(fd, hdr, entryp, esymp)
    267  1.1  tsubai 	int fd;
    268  1.1  tsubai 	struct exec *hdr;
    269  1.1  tsubai 	u_int32_t *entryp;
    270  1.1  tsubai 	void **esymp;
    271  1.1  tsubai {
    272  1.1  tsubai 	void *addr;
    273  1.1  tsubai 	int n, *paddr;
    274  1.1  tsubai 
    275  1.1  tsubai 	/* Display the load address (entry point) for a.out. */
    276  1.1  tsubai 	printf("Booting %s @ 0x%lx\n", opened_name, hdr->a_entry);
    277  1.1  tsubai 	addr = (void *)(hdr->a_entry);
    278  1.1  tsubai 
    279  1.1  tsubai 	/*
    280  1.1  tsubai 	 * Determine memory needed for kernel and allocate it from
    281  1.1  tsubai 	 * the firmware.
    282  1.1  tsubai 	 */
    283  1.1  tsubai 	n = hdr->a_text + hdr->a_data + hdr->a_bss + hdr->a_syms + sizeof(int);
    284  1.1  tsubai 	if ((paddr = OF_claim(addr, n, 0)) == (int *)-1)
    285  1.1  tsubai 		panic("cannot claim memory");
    286  1.1  tsubai 
    287  1.1  tsubai 	/* Load text. */
    288  1.1  tsubai 	lseek(fd, N_TXTOFF(*hdr), SEEK_SET);
    289  1.1  tsubai 	printf("%lu", hdr->a_text);
    290  1.1  tsubai 	if (read(fd, paddr, hdr->a_text) != hdr->a_text) {
    291  1.1  tsubai 		printf("read text: %s\n", strerror(errno));
    292  1.1  tsubai 		return (1);
    293  1.1  tsubai 	}
    294  1.1  tsubai 	syncicache((void *)paddr, hdr->a_text);
    295  1.1  tsubai 
    296  1.1  tsubai 	/* Load data. */
    297  1.1  tsubai 	printf("+%lu", hdr->a_data);
    298  1.1  tsubai 	if (read(fd, (void *)paddr + hdr->a_text, hdr->a_data) != hdr->a_data) {
    299  1.1  tsubai 		printf("read data: %s\n", strerror(errno));
    300  1.1  tsubai 		return (1);
    301  1.1  tsubai 	}
    302  1.1  tsubai 
    303  1.1  tsubai 	/* Zero BSS. */
    304  1.1  tsubai 	printf("+%lu", hdr->a_bss);
    305  1.1  tsubai 	bzero((void *)paddr + hdr->a_text + hdr->a_data, hdr->a_bss);
    306  1.1  tsubai 
    307  1.1  tsubai 	/* Symbols. */
    308  1.1  tsubai 	*esymp = paddr;
    309  1.1  tsubai 	paddr = (int *)((void *)paddr + hdr->a_text + hdr->a_data + hdr->a_bss);
    310  1.1  tsubai 	*paddr++ = hdr->a_syms;
    311  1.1  tsubai 	if (hdr->a_syms) {
    312  1.1  tsubai 		printf(" [%lu", hdr->a_syms);
    313  1.1  tsubai 		if (read(fd, paddr, hdr->a_syms) != hdr->a_syms) {
    314  1.1  tsubai 			printf("read symbols: %s\n", strerror(errno));
    315  1.1  tsubai 			return (1);
    316  1.1  tsubai 		}
    317  1.1  tsubai 		paddr = (int *)((void *)paddr + hdr->a_syms);
    318  1.1  tsubai 		if (read(fd, &n, sizeof(int)) != sizeof(int)) {
    319  1.1  tsubai 			printf("read symbols: %s\n", strerror(errno));
    320  1.1  tsubai 			return (1);
    321  1.1  tsubai 		}
    322  1.1  tsubai 		if (OF_claim((void *)paddr, n + sizeof(int), 0) == (void *)-1)
    323  1.1  tsubai 			panic("cannot claim memory");
    324  1.1  tsubai 		*paddr++ = n;
    325  1.1  tsubai 		if (read(fd, paddr, n - sizeof(int)) != n - sizeof(int)) {
    326  1.1  tsubai 			printf("read symbols: %s\n", strerror(errno));
    327  1.1  tsubai 			return (1);
    328  1.1  tsubai 		}
    329  1.1  tsubai 		printf("+%d]", n - sizeof(int));
    330  1.1  tsubai 		*esymp = paddr + (n - sizeof(int));
    331  1.1  tsubai 	}
    332  1.1  tsubai 
    333  1.1  tsubai 	*entryp = hdr->a_entry;
    334  1.1  tsubai 	return (0);
    335  1.1  tsubai }
    336  1.1  tsubai #endif /* POWERPC_BOOT_AOUT */
    337  1.1  tsubai 
    338  1.1  tsubai #ifdef POWERPC_BOOT_ELF
    339  1.1  tsubai int
    340  1.1  tsubai elf_exec(fd, elf, entryp, esymp)
    341  1.1  tsubai 	int fd;
    342  1.1  tsubai 	Elf_Ehdr *elf;
    343  1.1  tsubai 	u_int32_t *entryp;
    344  1.1  tsubai 	void **esymp;
    345  1.1  tsubai {
    346  1.1  tsubai 	Elf32_Shdr *shp;
    347  1.1  tsubai 	Elf32_Off off;
    348  1.1  tsubai 	void *addr;
    349  1.1  tsubai 	size_t size;
    350  1.1  tsubai 	int i, first = 1;
    351  1.1  tsubai 	int n;
    352  1.1  tsubai 
    353  1.1  tsubai 	/*
    354  1.1  tsubai 	 * Don't display load address for ELF; it's encoded in
    355  1.1  tsubai 	 * each section.
    356  1.1  tsubai 	 */
    357  1.1  tsubai 	printf("Booting %s\n", opened_name);
    358  1.1  tsubai 
    359  1.1  tsubai 	for (i = 0; i < elf->e_phnum; i++) {
    360  1.1  tsubai 		Elf_Phdr phdr;
    361  1.1  tsubai 		(void)lseek(fd, elf->e_phoff + sizeof(phdr) * i, SEEK_SET);
    362  1.1  tsubai 		if (read(fd, (void *)&phdr, sizeof(phdr)) != sizeof(phdr)) {
    363  1.1  tsubai 			printf("read phdr: %s\n", strerror(errno));
    364  1.1  tsubai 			return (1);
    365  1.1  tsubai 		}
    366  1.1  tsubai 		if (phdr.p_type != Elf_pt_load ||
    367  1.1  tsubai 		    (phdr.p_flags & (Elf_pf_w|Elf_pf_x)) == 0)
    368  1.1  tsubai 			continue;
    369  1.1  tsubai 
    370  1.1  tsubai 		/* Read in segment. */
    371  1.1  tsubai 		printf("%s%lu@0x%lx", first ? "" : "+", phdr.p_filesz,
    372  1.1  tsubai 		    (u_long)phdr.p_vaddr);
    373  1.1  tsubai 		(void)lseek(fd, phdr.p_offset, SEEK_SET);
    374  1.1  tsubai 		if (OF_claim((void *)phdr.p_vaddr, phdr.p_memsz, 0) ==
    375  1.1  tsubai 		    (void *)-1)
    376  1.1  tsubai 			panic("cannot claim memory");
    377  1.1  tsubai 		if (read(fd, (void *)phdr.p_vaddr, phdr.p_filesz) !=
    378  1.1  tsubai 		    phdr.p_filesz) {
    379  1.1  tsubai 			printf("read segment: %s\n", strerror(errno));
    380  1.1  tsubai 			return (1);
    381  1.1  tsubai 		}
    382  1.1  tsubai 		syncicache((void *)phdr.p_vaddr, phdr.p_filesz);
    383  1.1  tsubai 
    384  1.1  tsubai 		/* Zero BSS. */
    385  1.1  tsubai 		if (phdr.p_filesz < phdr.p_memsz) {
    386  1.1  tsubai 			printf("+%lu@0x%lx", phdr.p_memsz - phdr.p_filesz,
    387  1.1  tsubai 			    (u_long)(phdr.p_vaddr + phdr.p_filesz));
    388  1.1  tsubai 			bzero((void *)phdr.p_vaddr + phdr.p_filesz,
    389  1.1  tsubai 			    phdr.p_memsz - phdr.p_filesz);
    390  1.1  tsubai 		}
    391  1.1  tsubai 		first = 0;
    392  1.1  tsubai 	}
    393  1.1  tsubai 
    394  1.1  tsubai 	printf(" \n");
    395  1.1  tsubai 
    396  1.1  tsubai #if 0 /* I want to rethink this... --thorpej (at) netbsd.org */
    397  1.1  tsubai 	/*
    398  1.1  tsubai 	 * Compute the size of the symbol table.
    399  1.1  tsubai 	 */
    400  1.1  tsubai 	size = sizeof(Elf_Ehdr) + (elf->e_shnum * sizeof(Elf32_Shdr));
    401  1.1  tsubai 	shp = addr = alloc(elf->e_shnum * sizeof(Elf32_Shdr));
    402  1.1  tsubai 	(void)lseek(fd, elf->e_shoff, SEEK_SET);
    403  1.1  tsubai 	if (read(fd, addr, elf->e_shnum * sizeof(Elf32_Shdr)) !=
    404  1.1  tsubai 	    elf->e_shnum * sizeof(Elf32_Shdr)) {
    405  1.1  tsubai 		printf("read section headers: %s\n", strerror(errno));
    406  1.1  tsubai 		return (1);
    407  1.1  tsubai 	}
    408  1.1  tsubai 	for (i = 0; i < elf->e_shnum; i++, shp++) {
    409  1.1  tsubai 		if (shp->sh_type == Elf_sht_null)
    410  1.1  tsubai 			continue;
    411  1.1  tsubai 		if (shp->sh_type != Elf_sht_symtab
    412  1.1  tsubai 		    && shp->sh_type != Elf_sht_strtab) {
    413  1.1  tsubai 			shp->sh_offset = 0;
    414  1.1  tsubai 			shp->sh_type = Elf_sht_nobits;
    415  1.1  tsubai 			continue;
    416  1.1  tsubai 		}
    417  1.1  tsubai 		size += shp->sh_size;
    418  1.1  tsubai 	}
    419  1.1  tsubai 	shp = addr;
    420  1.1  tsubai 
    421  1.1  tsubai 	/*
    422  1.1  tsubai 	 * Reserve memory for the symbols.
    423  1.1  tsubai 	 */
    424  1.1  tsubai 	if ((addr = OF_claim(0, size, NBPG)) == (void *)-1)
    425  1.1  tsubai 		panic("no space for symbol table");
    426  1.1  tsubai 
    427  1.1  tsubai 	/*
    428  1.1  tsubai 	 * Copy the headers.
    429  1.1  tsubai 	 */
    430  1.1  tsubai 	elf->e_phoff = 0;
    431  1.1  tsubai 	elf->e_shoff = sizeof(Elf_Ehdr);
    432  1.1  tsubai 	elf->e_phentsize = 0;
    433  1.1  tsubai 	elf->e_phnum = 0;
    434  1.1  tsubai 	bcopy(elf, addr, sizeof(Elf_Ehdr));
    435  1.1  tsubai 	bcopy(shp, addr + sizeof(Elf_Ehdr), elf->e_shnum * sizeof(Elf32_Shdr));
    436  1.1  tsubai 	free(shp, elf->e_shnum * sizeof(Elf32_Shdr));
    437  1.1  tsubai 	*ssymp = addr;
    438  1.1  tsubai 
    439  1.1  tsubai 	/*
    440  1.1  tsubai 	 * Now load the symbol sections themselves.
    441  1.1  tsubai 	 */
    442  1.1  tsubai 	shp = addr + sizeof(Elf_Ehdr);
    443  1.1  tsubai 	addr += sizeof(Elf_Ehdr) + (elf->e_shnum * sizeof(Elf32_Shdr));
    444  1.1  tsubai 	off = sizeof(Elf_Ehdr) + (elf->e_shnum * sizeof(Elf32_Shdr));
    445  1.1  tsubai 	for (first = 1, i = 0; i < elf->e_shnum; i++, shp++) {
    446  1.1  tsubai 		if (shp->sh_type == Elf_sht_symtab
    447  1.1  tsubai 		    || shp->sh_type == Elf_sht_strtab) {
    448  1.1  tsubai 			if (first)
    449  1.1  tsubai 				printf("symbols @ 0x%lx ", (u_long)addr);
    450  1.1  tsubai 			printf("%s%d", first ? "" : "+", shp->sh_size);
    451  1.1  tsubai 			(void)lseek(fd, shp->sh_offset, SEEK_SET);
    452  1.1  tsubai 			if (read(fd, addr, shp->sh_size) != shp->sh_size) {
    453  1.1  tsubai 				printf("read symbols: %s\n", strerror(errno));
    454  1.1  tsubai 				return (1);
    455  1.1  tsubai 			}
    456  1.1  tsubai 			addr += shp->sh_size;
    457  1.1  tsubai 			shp->sh_offset = off;
    458  1.1  tsubai 			off += shp->sh_size;
    459  1.1  tsubai 			first = 0;
    460  1.1  tsubai 		}
    461  1.1  tsubai 	}
    462  1.1  tsubai 	*esymp = addr;
    463  1.1  tsubai #endif /* 0 */
    464  1.1  tsubai 
    465  1.1  tsubai 	*entryp = elf->e_entry;
    466  1.1  tsubai 	return (0);
    467  1.1  tsubai }
    468  1.1  tsubai #endif /* POWERPC_BOOT_ELF */
    469  1.1  tsubai 
    470  1.1  tsubai void
    471  1.1  tsubai main()
    472  1.1  tsubai {
    473  1.1  tsubai 	extern char bootprog_name[], bootprog_rev[],
    474  1.2  tsubai 		    bootprog_maker[], bootprog_date[];
    475  1.2  tsubai 	int chosen, options;
    476  1.1  tsubai 	char bootline[512];		/* Should check size? */
    477  1.1  tsubai 	char *cp;
    478  1.1  tsubai 	int fd;
    479  1.1  tsubai 
    480  1.1  tsubai 	printf("\n");
    481  1.1  tsubai 	printf(">> %s, Revision %s\n", bootprog_name, bootprog_rev);
    482  1.1  tsubai 	printf(">> (%s, %s)\n", bootprog_maker, bootprog_date);
    483  1.1  tsubai 
    484  1.1  tsubai 	/*
    485  1.1  tsubai 	 * Get the boot arguments from Openfirmware
    486  1.1  tsubai 	 */
    487  1.1  tsubai 	if ((chosen = OF_finddevice("/chosen")) == -1 ||
    488  1.1  tsubai 	    OF_getprop(chosen, "bootpath", bootdev, sizeof bootdev) < 0 ||
    489  1.1  tsubai 	    OF_getprop(chosen, "bootargs", bootline, sizeof bootline) < 0) {
    490  1.1  tsubai 		printf("Invalid Openfirmware environment\n");
    491  1.1  tsubai 		OF_exit();
    492  1.2  tsubai 	}
    493  1.2  tsubai 
    494  1.2  tsubai 	/*
    495  1.2  tsubai 	 * Some versions of Openfirmware sets bootpath to "".
    496  1.2  tsubai 	 * We use boot-device instead if it occurs.
    497  1.2  tsubai 	 */
    498  1.2  tsubai 	if (bootdev[0] == 0) {
    499  1.2  tsubai 		printf("Cannot use bootpath\n");
    500  1.2  tsubai 		if ((options = OF_finddevice("/options")) == -1 ||
    501  1.2  tsubai 		    OF_getprop(options, "boot-device", bootdev,
    502  1.2  tsubai 			       sizeof bootdev) < 0) {
    503  1.2  tsubai 			printf("Invalid Openfirmware environment\n");
    504  1.2  tsubai 			OF_exit();
    505  1.2  tsubai 		}
    506  1.2  tsubai 		printf("Using boot-device instead\n");
    507  1.1  tsubai 	}
    508  1.1  tsubai 
    509  1.1  tsubai 	prom2boot(bootdev);
    510  1.1  tsubai 	parseargs(bootline, &boothowto);
    511  1.1  tsubai 
    512  1.1  tsubai 	for (;;) {
    513  1.1  tsubai 		if (boothowto & RB_ASKNAME) {
    514  1.1  tsubai 			printf("Boot: ");
    515  1.1  tsubai 			gets(bootline);
    516  1.1  tsubai 			parseargs(bootline, &boothowto);
    517  1.1  tsubai 		}
    518  1.1  tsubai 		if ((fd = open(bootline, 0)) >= 0)
    519  1.1  tsubai 			break;
    520  1.1  tsubai 		if (errno)
    521  1.1  tsubai 			printf("open %s: %s\n", opened_name, strerror(errno));
    522  1.1  tsubai 		boothowto |= RB_ASKNAME;
    523  1.1  tsubai 	}
    524  1.1  tsubai #ifdef	__notyet__
    525  1.1  tsubai 	OF_setprop(chosen, "bootpath", opened_name, strlen(opened_name) + 1);
    526  1.1  tsubai 	cp = bootline;
    527  1.1  tsubai #else
    528  1.1  tsubai 	strcpy(bootline, opened_name);
    529  1.1  tsubai 	cp = bootline + strlen(bootline);
    530  1.1  tsubai 	*cp++ = ' ';
    531  1.1  tsubai #endif
    532  1.1  tsubai 	*cp = '-';
    533  1.1  tsubai 	if (boothowto & RB_ASKNAME)
    534  1.1  tsubai 		*++cp = 'a';
    535  1.1  tsubai 	if (boothowto & RB_SINGLE)
    536  1.1  tsubai 		*++cp = 's';
    537  1.1  tsubai 	if (boothowto & RB_KDB)
    538  1.1  tsubai 		*++cp = 'd';
    539  1.1  tsubai 	if (*cp == '-')
    540  1.1  tsubai #ifdef	__notyet__
    541  1.1  tsubai 		*cp = 0;
    542  1.1  tsubai #else
    543  1.1  tsubai 		*--cp = 0;
    544  1.1  tsubai #endif
    545  1.1  tsubai 	else
    546  1.1  tsubai 		*++cp = 0;
    547  1.1  tsubai #ifdef	__notyet__
    548  1.1  tsubai 	OF_setprop(chosen, "bootargs", bootline, strlen(bootline) + 1);
    549  1.1  tsubai #endif
    550  1.1  tsubai 	/* XXX void, for now */
    551  1.1  tsubai 	(void)loadfile(fd, bootline);
    552  1.1  tsubai 
    553  1.1  tsubai 	OF_exit();
    554  1.1  tsubai }
    555