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