Home | History | Annotate | Line # | Download | only in common
bootxx.c revision 1.1
      1  1.1  pooka /*	$NetBSD: bootxx.c,v 1.1 2011/01/26 01:18:54 pooka Exp $	*/
      2  1.1  pooka 
      3  1.1  pooka /*-
      4  1.1  pooka  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      5  1.1  pooka  * All rights reserved.
      6  1.1  pooka  *
      7  1.1  pooka  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  pooka  * by Jonathan Stone, Michael Hitch and Simon Burge.
      9  1.1  pooka  *
     10  1.1  pooka  * Redistribution and use in source and binary forms, with or without
     11  1.1  pooka  * modification, are permitted provided that the following conditions
     12  1.1  pooka  * are met:
     13  1.1  pooka  * 1. Redistributions of source code must retain the above copyright
     14  1.1  pooka  *    notice, this list of conditions and the following disclaimer.
     15  1.1  pooka  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  pooka  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  pooka  *    documentation and/or other materials provided with the distribution.
     18  1.1  pooka  * 3. All advertising materials mentioning features or use of this software
     19  1.1  pooka  *    must display the following acknowledgement:
     20  1.1  pooka  *        This product includes software developed by the NetBSD
     21  1.1  pooka  *        Foundation, Inc. and its contributors.
     22  1.1  pooka  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.1  pooka  *    contributors may be used to endorse or promote products derived
     24  1.1  pooka  *    from this software without specific prior written permission.
     25  1.1  pooka  *
     26  1.1  pooka  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.1  pooka  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.1  pooka  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1  pooka  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.1  pooka  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.1  pooka  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1  pooka  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1  pooka  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.1  pooka  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1  pooka  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.1  pooka  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1  pooka  */
     38  1.1  pooka 
     39  1.1  pooka /*
     40  1.1  pooka  * Copyright (c) 1992, 1993
     41  1.1  pooka  *	The Regents of the University of California.  All rights reserved.
     42  1.1  pooka  *
     43  1.1  pooka  * This code is derived from software contributed to Berkeley by
     44  1.1  pooka  * Ralph Campbell.
     45  1.1  pooka  *
     46  1.1  pooka  * Redistribution and use in source and binary forms, with or without
     47  1.1  pooka  * modification, are permitted provided that the following conditions
     48  1.1  pooka  * are met:
     49  1.1  pooka  * 1. Redistributions of source code must retain the above copyright
     50  1.1  pooka  *    notice, this list of conditions and the following disclaimer.
     51  1.1  pooka  * 2. Redistributions in binary form must reproduce the above copyright
     52  1.1  pooka  *    notice, this list of conditions and the following disclaimer in the
     53  1.1  pooka  *    documentation and/or other materials provided with the distribution.
     54  1.1  pooka  * 3. Neither the name of the University nor the names of its contributors
     55  1.1  pooka  *    may be used to endorse or promote products derived from this software
     56  1.1  pooka  *    without specific prior written permission.
     57  1.1  pooka  *
     58  1.1  pooka  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     59  1.1  pooka  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     60  1.1  pooka  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     61  1.1  pooka  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     62  1.1  pooka  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     63  1.1  pooka  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     64  1.1  pooka  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     65  1.1  pooka  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     66  1.1  pooka  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     67  1.1  pooka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     68  1.1  pooka  * SUCH DAMAGE.
     69  1.1  pooka  *
     70  1.1  pooka  *	@(#)boot.c	8.1 (Berkeley) 6/10/93
     71  1.1  pooka  */
     72  1.1  pooka 
     73  1.1  pooka #include <sys/param.h>
     74  1.1  pooka #include <sys/exec_elf.h>
     75  1.1  pooka #include <lib/libsa/stand.h>
     76  1.1  pooka #include <lib/libkern/libkern.h>
     77  1.1  pooka 
     78  1.1  pooka typedef void (entrypt) (int, char **, int, const void *, int, char *);
     79  1.1  pooka 
     80  1.1  pooka entrypt main;
     81  1.1  pooka entrypt *loadfile (char *path, char *name);
     82  1.1  pooka 
     83  1.1  pooka /*
     84  1.1  pooka  * This gets arguments from the PROM, calls other routines to open
     85  1.1  pooka  * and load the secondary boot loader called boot, and then transfers
     86  1.1  pooka  * execution to that program.
     87  1.1  pooka  */
     88  1.1  pooka void
     89  1.1  pooka main(int argc, char **argv, int code, const void *cv, int bim, char *bip)
     90  1.1  pooka {
     91  1.1  pooka 	char *cp;
     92  1.1  pooka 	entrypt *entry;
     93  1.1  pooka 
     94  1.1  pooka 	cp = *argv;
     95  1.1  pooka 
     96  1.1  pooka     printf("\nNetBSD/emips " NETBSD_VERS " " BOOTXX_FS_NAME " Bootstrap\n");
     97  1.1  pooka 
     98  1.1  pooka 	entry = loadfile(cp, "netbsd");
     99  1.1  pooka 	if ((int)entry != -1)
    100  1.1  pooka 		goto goodload;
    101  1.1  pooka 
    102  1.1  pooka 	/* Give old /boot a go... */
    103  1.1  pooka 	entry = loadfile(cp, "/boot");
    104  1.1  pooka 	if ((int)entry != -1)
    105  1.1  pooka 		goto goodload;
    106  1.1  pooka 
    107  1.1  pooka 	goto bad;
    108  1.1  pooka goodload:
    109  1.1  pooka 
    110  1.1  pooka     entry(argc, argv, code, cv, bim, bip);
    111  1.1  pooka bad:
    112  1.1  pooka     printf("Boot failed.\n");
    113  1.1  pooka }
    114  1.1  pooka 
    115  1.1  pooka /*
    116  1.1  pooka  * Open 'filename', read in program and return the entry point or -1 if error.
    117  1.1  pooka  */
    118  1.1  pooka entrypt *
    119  1.1  pooka loadfile(char *path, char *name)
    120  1.1  pooka {
    121  1.1  pooka 	int fd, i;
    122  1.1  pooka 	char c, *buf, bootfname[64];
    123  1.1  pooka 	Elf32_Ehdr ehdr;
    124  1.1  pooka 	Elf32_Phdr phdr;
    125  1.1  pooka 
    126  1.1  pooka 	strcpy(bootfname, path);
    127  1.1  pooka 	buf = bootfname;
    128  1.1  pooka 	while ((c = *buf++) != '\0') {
    129  1.1  pooka 		if (c == ')')
    130  1.1  pooka 			break;
    131  1.1  pooka 		if (c != '/')
    132  1.1  pooka 			continue;
    133  1.1  pooka 		while ((c = *buf++) != '\0')
    134  1.1  pooka 			if (c == '/')
    135  1.1  pooka 				break;
    136  1.1  pooka 		/*
    137  1.1  pooka 		 * Make "N/rzY" with no trailing '/' valid by adding
    138  1.1  pooka 		 * the extra '/' before appending 'bootemips' to the path.
    139  1.1  pooka 		 */
    140  1.1  pooka 		if (c != '/') {
    141  1.1  pooka 			buf--;
    142  1.1  pooka 			*buf++ = '/';
    143  1.1  pooka 			*buf = '\0';
    144  1.1  pooka 		}
    145  1.1  pooka 		break;
    146  1.1  pooka 	}
    147  1.1  pooka 	strcpy(buf, name);
    148  1.1  pooka 	if ((fd = open(bootfname, 0)) < 0) {
    149  1.1  pooka 		printf("open %s: %d\n", bootfname, errno);
    150  1.1  pooka 		goto err;
    151  1.1  pooka 	}
    152  1.1  pooka 
    153  1.1  pooka 	/* read the exec header */
    154  1.1  pooka 	i = read(fd, (char *)&ehdr, sizeof(ehdr));
    155  1.1  pooka 	if ((i != sizeof(ehdr)) ||
    156  1.1  pooka 	    (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0) ||
    157  1.1  pooka 	    (ehdr.e_ident[EI_CLASS] != ELFCLASS32)) {
    158  1.1  pooka 		printf("%s: No ELF header\n", bootfname);
    159  1.1  pooka 		goto cerr;
    160  1.1  pooka 	}
    161  1.1  pooka 
    162  1.1  pooka 	for (i = 0; i < ehdr.e_phnum; i++) {
    163  1.1  pooka 		if (lseek(fd, (off_t) ehdr.e_phoff + i * sizeof(phdr), 0) < 0)
    164  1.1  pooka 			goto cerr;
    165  1.1  pooka 		if (read(fd, &phdr, sizeof(phdr)) != sizeof(phdr))
    166  1.1  pooka 			goto cerr;
    167  1.1  pooka 		if (phdr.p_type != PT_LOAD)
    168  1.1  pooka 			continue;
    169  1.1  pooka 		if (lseek(fd, (off_t)phdr.p_offset, 0) < 0)
    170  1.1  pooka 			goto cerr;
    171  1.1  pooka 		if (read(fd, (char *)phdr.p_paddr, phdr.p_filesz) != phdr.p_filesz)
    172  1.1  pooka 			goto cerr;
    173  1.1  pooka 	}
    174  1.1  pooka 	return ((entrypt*)ehdr.e_entry);
    175  1.1  pooka 
    176  1.1  pooka cerr:
    177  1.1  pooka #ifndef LIBSA_NO_FS_CLOSE
    178  1.1  pooka 	(void) close(fd);
    179  1.1  pooka #endif
    180  1.1  pooka err:
    181  1.1  pooka 	printf("Can't load '%s'\n", bootfname);
    182  1.1  pooka 	return ((entrypt*)-1);
    183  1.1  pooka }
    184