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