Home | History | Annotate | Line # | Download | only in libsa
loadfile.c revision 1.18
      1  1.18   thorpej /* $NetBSD: loadfile.c,v 1.18 2001/10/30 23:51:03 thorpej Exp $ */
      2   1.1  christos 
      3   1.1  christos /*-
      4   1.1  christos  * Copyright (c) 1997 The NetBSD Foundation, Inc.
      5   1.1  christos  * All rights reserved.
      6   1.1  christos  *
      7   1.1  christos  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1  christos  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9   1.1  christos  * NASA Ames Research Center and by Christos Zoulas.
     10   1.1  christos  *
     11   1.1  christos  * Redistribution and use in source and binary forms, with or without
     12   1.1  christos  * modification, are permitted provided that the following conditions
     13   1.1  christos  * are met:
     14   1.1  christos  * 1. Redistributions of source code must retain the above copyright
     15   1.1  christos  *    notice, this list of conditions and the following disclaimer.
     16   1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     17   1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     18   1.1  christos  *    documentation and/or other materials provided with the distribution.
     19   1.1  christos  * 3. All advertising materials mentioning features or use of this software
     20   1.1  christos  *    must display the following acknowledgement:
     21   1.1  christos  *	This product includes software developed by the NetBSD
     22   1.1  christos  *	Foundation, Inc. and its contributors.
     23   1.1  christos  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24   1.1  christos  *    contributors may be used to endorse or promote products derived
     25   1.1  christos  *    from this software without specific prior written permission.
     26   1.1  christos  *
     27   1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28   1.1  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29   1.1  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30   1.1  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31   1.1  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32   1.1  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33   1.1  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34   1.1  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35   1.1  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36   1.1  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37   1.1  christos  * POSSIBILITY OF SUCH DAMAGE.
     38   1.1  christos  */
     39   1.1  christos 
     40   1.1  christos /*
     41   1.1  christos  * Copyright (c) 1992, 1993
     42   1.1  christos  *	The Regents of the University of California.  All rights reserved.
     43   1.1  christos  *
     44   1.1  christos  * This code is derived from software contributed to Berkeley by
     45   1.1  christos  * Ralph Campbell.
     46   1.1  christos  *
     47   1.1  christos  * Redistribution and use in source and binary forms, with or without
     48   1.1  christos  * modification, are permitted provided that the following conditions
     49   1.1  christos  * are met:
     50   1.1  christos  * 1. Redistributions of source code must retain the above copyright
     51   1.1  christos  *    notice, this list of conditions and the following disclaimer.
     52   1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     53   1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     54   1.1  christos  *    documentation and/or other materials provided with the distribution.
     55   1.1  christos  * 3. All advertising materials mentioning features or use of this software
     56   1.1  christos  *    must display the following acknowledgement:
     57   1.1  christos  *	This product includes software developed by the University of
     58   1.1  christos  *	California, Berkeley and its contributors.
     59   1.1  christos  * 4. Neither the name of the University nor the names of its contributors
     60   1.1  christos  *    may be used to endorse or promote products derived from this software
     61   1.1  christos  *    without specific prior written permission.
     62   1.1  christos  *
     63   1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     64   1.1  christos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     65   1.1  christos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     66   1.1  christos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     67   1.1  christos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     68   1.1  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     69   1.1  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     70   1.1  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     71   1.1  christos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     72   1.1  christos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     73   1.1  christos  * SUCH DAMAGE.
     74   1.1  christos  *
     75   1.1  christos  *	@(#)boot.c	8.1 (Berkeley) 6/10/93
     76   1.1  christos  */
     77   1.1  christos 
     78   1.1  christos #ifdef _STANDALONE
     79   1.1  christos #include <lib/libsa/stand.h>
     80   1.6   thorpej #include <lib/libkern/libkern.h>
     81   1.1  christos #else
     82   1.1  christos #include <stdio.h>
     83   1.1  christos #include <string.h>
     84   1.1  christos #include <errno.h>
     85   1.1  christos #include <stdlib.h>
     86   1.1  christos #include <unistd.h>
     87   1.1  christos #include <fcntl.h>
     88   1.1  christos #include <err.h>
     89   1.1  christos #endif
     90   1.1  christos 
     91   1.1  christos #include <sys/param.h>
     92   1.1  christos #include <sys/exec.h>
     93   1.1  christos 
     94   1.1  christos #include "loadfile.h"
     95   1.1  christos 
     96   1.1  christos #ifdef BOOT_ECOFF
     97   1.1  christos #include <sys/exec_ecoff.h>
     98   1.1  christos static int coff_exec __P((int, struct ecoff_exechdr *, u_long *, int));
     99   1.1  christos #endif
    100  1.18   thorpej 
    101   1.1  christos #ifdef BOOT_ELF
    102   1.1  christos #include <sys/exec_elf.h>
    103   1.1  christos static int elf_exec __P((int, Elf_Ehdr *, u_long *, int));
    104   1.1  christos #endif
    105  1.18   thorpej 
    106   1.1  christos #ifdef BOOT_AOUT
    107   1.1  christos #include <sys/exec_aout.h>
    108   1.1  christos static int aout_exec __P((int, struct exec *, u_long *, int));
    109   1.1  christos #endif
    110   1.1  christos 
    111   1.1  christos /*
    112   1.1  christos  * Open 'filename', read in program and and return 0 if ok 1 on error.
    113   1.1  christos  * Fill in marks
    114   1.1  christos  */
    115   1.1  christos int
    116   1.1  christos loadfile(fname, marks, flags)
    117   1.1  christos 	const char *fname;
    118   1.1  christos 	u_long *marks;
    119   1.1  christos 	int flags;
    120   1.1  christos {
    121   1.1  christos 	union {
    122   1.1  christos #ifdef BOOT_ECOFF
    123   1.1  christos 		struct ecoff_exechdr coff;
    124   1.1  christos #endif
    125   1.1  christos #ifdef BOOT_ELF
    126   1.1  christos 		Elf_Ehdr elf;
    127   1.1  christos #endif
    128   1.1  christos #ifdef BOOT_AOUT
    129   1.1  christos 		struct exec aout;
    130   1.1  christos #endif
    131   1.1  christos 
    132   1.1  christos 	} hdr;
    133   1.1  christos 	ssize_t nr;
    134   1.1  christos 	int fd, rval;
    135   1.1  christos 
    136   1.1  christos 	/* Open the file. */
    137   1.1  christos 	if ((fd = open(fname, 0)) < 0) {
    138   1.1  christos 		WARN(("open %s", fname ? fname : "<default>"));
    139   1.1  christos 		return -1;
    140   1.1  christos 	}
    141   1.1  christos 
    142   1.1  christos 	/* Read the exec header. */
    143   1.1  christos 	if ((nr = read(fd, &hdr, sizeof(hdr))) != sizeof(hdr)) {
    144   1.1  christos 		WARN(("read header"));
    145   1.1  christos 		goto err;
    146   1.1  christos 	}
    147   1.1  christos 
    148   1.1  christos #ifdef BOOT_ECOFF
    149   1.1  christos 	if (!ECOFF_BADMAG(&hdr.coff)) {
    150   1.1  christos 		rval = coff_exec(fd, &hdr.coff, marks, flags);
    151   1.1  christos 	} else
    152   1.1  christos #endif
    153   1.1  christos #ifdef BOOT_ELF
    154   1.4    kleink 	if (memcmp(hdr.elf.e_ident, ELFMAG, SELFMAG) == 0 &&
    155   1.4    kleink 	    hdr.elf.e_ident[EI_CLASS] == ELFCLASS) {
    156   1.1  christos 		rval = elf_exec(fd, &hdr.elf, marks, flags);
    157   1.1  christos 	} else
    158   1.1  christos #endif
    159   1.1  christos #ifdef BOOT_AOUT
    160   1.8     ragge 	if (OKMAGIC(N_GETMAGIC(hdr.aout))
    161   1.8     ragge #ifndef NO_MID_CHECK
    162   1.8     ragge 	    && N_GETMID(hdr.aout) == MID_MACHINE
    163   1.8     ragge #endif
    164   1.8     ragge 	    ) {
    165   1.1  christos 		rval = aout_exec(fd, &hdr.aout, marks, flags);
    166   1.1  christos 	} else
    167   1.1  christos #endif
    168   1.1  christos 	{
    169   1.1  christos 		rval = 1;
    170   1.1  christos 		errno = EFTYPE;
    171   1.1  christos 		WARN(("%s", fname ? fname : "<default>"));
    172   1.1  christos 	}
    173   1.1  christos 
    174   1.1  christos 	if (rval == 0) {
    175   1.1  christos 		PROGRESS(("=0x%lx\n", marks[MARK_END] - marks[MARK_START]));
    176   1.1  christos 		return fd;
    177   1.1  christos 	}
    178   1.1  christos err:
    179   1.1  christos 	(void)close(fd);
    180   1.1  christos 	return -1;
    181   1.1  christos }
    182   1.1  christos 
    183   1.1  christos #ifdef BOOT_ECOFF
    184  1.18   thorpej #include "loadfile_ecoff.c"
    185  1.18   thorpej #endif
    186   1.1  christos 
    187   1.1  christos #ifdef BOOT_ELF
    188  1.18   thorpej #include "loadfile_elf32.c"
    189  1.18   thorpej #endif
    190   1.1  christos 
    191   1.1  christos #ifdef BOOT_AOUT
    192  1.18   thorpej #include "loadfile_aout.c"
    193  1.18   thorpej #endif
    194