Home | History | Annotate | Line # | Download | only in libsa
loadfile.c revision 1.22
      1 /* $NetBSD: loadfile.c,v 1.22 2003/02/24 10:51:05 pk 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. All advertising materials mentioning features or use of this software
     56  *    must display the following acknowledgement:
     57  *	This product includes software developed by the University of
     58  *	California, Berkeley and its contributors.
     59  * 4. Neither the name of the University nor the names of its contributors
     60  *    may be used to endorse or promote products derived from this software
     61  *    without specific prior written permission.
     62  *
     63  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     64  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     65  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     66  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     67  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     68  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     69  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     70  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     71  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     72  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     73  * SUCH DAMAGE.
     74  *
     75  *	@(#)boot.c	8.1 (Berkeley) 6/10/93
     76  */
     77 
     78 #ifdef _STANDALONE
     79 #include <lib/libsa/stand.h>
     80 #include <lib/libkern/libkern.h>
     81 #else
     82 #include <stdio.h>
     83 #include <string.h>
     84 #include <errno.h>
     85 #include <stdlib.h>
     86 #include <unistd.h>
     87 #include <fcntl.h>
     88 #include <err.h>
     89 #endif
     90 
     91 #include <sys/param.h>
     92 #include <sys/exec.h>
     93 
     94 #include "loadfile.h"
     95 
     96 /*
     97  * Open 'filename', read in program and return the opened file
     98  * descriptor if ok, or -1 on error.
     99  * Fill in marks
    100  */
    101 int
    102 loadfile(fname, marks, flags)
    103 	const char *fname;
    104 	u_long *marks;
    105 	int flags;
    106 {
    107 	int fd, error;
    108 
    109 	/* Open the file. */
    110 	if ((fd = open(fname, 0)) < 0) {
    111 		WARN(("open %s", fname ? fname : "<default>"));
    112 		return (-1);
    113 	}
    114 
    115 	/* Load it; save the value of errno across the close() call */
    116 	if ((error = fdloadfile(fd, marks, flags)) != 0) {
    117 		(void)close(fd);
    118 		errno = error;
    119 		return (-1);
    120 	}
    121 
    122 	return (fd);
    123 }
    124 
    125 /*
    126  * Read in program from the given file descriptor.
    127  * Return error code (0 on success).
    128  * Fill in marks.
    129  */
    130 int
    131 fdloadfile(fd, marks, flags)
    132 	int fd;
    133 	u_long *marks;
    134 	int flags;
    135 {
    136 	union {
    137 #ifdef BOOT_ECOFF
    138 		struct ecoff_exechdr coff;
    139 #endif
    140 #ifdef BOOT_ELF32
    141 		Elf32_Ehdr elf32;
    142 #endif
    143 #ifdef BOOT_ELF64
    144 		Elf64_Ehdr elf64;
    145 #endif
    146 #ifdef BOOT_AOUT
    147 		struct exec aout;
    148 #endif
    149 	} hdr;
    150 	ssize_t nr;
    151 	int rval;
    152 
    153 	/* Read the exec header. */
    154 	if (lseek(fd, 0, SEEK_SET) == (off_t)-1)
    155 		goto err;
    156 	if ((nr = read(fd, &hdr, sizeof(hdr))) != sizeof(hdr)) {
    157 		WARN(("read header"));
    158 		goto err;
    159 	}
    160 
    161 #ifdef BOOT_ECOFF
    162 	if (!ECOFF_BADMAG(&hdr.coff)) {
    163 		rval = loadfile_coff(fd, &hdr.coff, marks, flags);
    164 	} else
    165 #endif
    166 #ifdef BOOT_ELF32
    167 	if (memcmp(hdr.elf32.e_ident, ELFMAG, SELFMAG) == 0 &&
    168 	    hdr.elf32.e_ident[EI_CLASS] == ELFCLASS32) {
    169 		rval = loadfile_elf32(fd, &hdr.elf32, marks, flags);
    170 	} else
    171 #endif
    172 #ifdef BOOT_ELF64
    173 	if (memcmp(hdr.elf64.e_ident, ELFMAG, SELFMAG) == 0 &&
    174 	    hdr.elf64.e_ident[EI_CLASS] == ELFCLASS64) {
    175 		rval = loadfile_elf64(fd, &hdr.elf64, marks, flags);
    176 	} else
    177 #endif
    178 #ifdef BOOT_AOUT
    179 	if (OKMAGIC(N_GETMAGIC(hdr.aout))
    180 #ifndef NO_MID_CHECK
    181 	    && N_GETMID(hdr.aout) == MID_MACHINE
    182 #endif
    183 	    ) {
    184 		rval = loadfile_aout(fd, &hdr.aout, marks, flags);
    185 	} else
    186 #endif
    187 	{
    188 		rval = 1;
    189 		errno = EFTYPE;
    190 	}
    191 
    192 	if (rval == 0) {
    193 		if ((flags & LOAD_ALL) != 0)
    194 			PROGRESS(("=0x%lx\n",
    195 				  marks[MARK_END] - marks[MARK_START]));
    196 		return (0);
    197 	}
    198 err:
    199 	return (errno);
    200 }
    201