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