Home | History | Annotate | Line # | Download | only in libsa
      1 /* $NetBSD: loadfile.c,v 1.33 2021/05/21 21:52:15 jmcneill 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 u_int		netbsd_elf_class;
     87 u_int		netbsd_elf_data;
     88 
     89 /*
     90  * Open 'filename', read in program and return the opened file
     91  * descriptor if ok, or -1 on error.
     92  * Fill in marks
     93  */
     94 int
     95 loadfile(const char *fname, u_long *marks, int flags)
     96 {
     97 	int fd, error;
     98 
     99 	/* Open the file. */
    100 	if ((fd = open(fname, 0)) < 0) {
    101 		WARN(("open %s", fname ? fname : "<default>"));
    102 		return -1;
    103 	}
    104 
    105 	/* Load it; save the value of errno across the close() call */
    106 	if ((error = fdloadfile(fd, marks, flags)) != 0) {
    107 		(void)close(fd);
    108 		errno = error;
    109 		return -1;
    110 	}
    111 
    112 	return fd;
    113 }
    114 
    115 /*
    116  * Read in program from the given file descriptor.
    117  * Return error code (0 on success).
    118  * Fill in marks.
    119  */
    120 int
    121 fdloadfile(int fd, u_long *marks, int flags)
    122 {
    123 	union {
    124 #ifdef BOOT_ECOFF
    125 		struct ecoff_exechdr coff;
    126 #endif
    127 #ifdef BOOT_ELF32
    128 		Elf32_Ehdr elf32;
    129 #endif
    130 #ifdef BOOT_ELF64
    131 		Elf64_Ehdr elf64;
    132 #endif
    133 #ifdef BOOT_AOUT
    134 		struct exec aout;
    135 #endif
    136 	} hdr;
    137 	ssize_t nr;
    138 	int rval;
    139 
    140 	/* Read the exec header. */
    141 	if (lseek(fd, 0, SEEK_SET) == (off_t)-1)
    142 		goto err;
    143 	nr = read(fd, &hdr, sizeof(hdr));
    144 	if (nr == -1) {
    145 		WARN(("read header failed"));
    146 		goto err;
    147 	}
    148 	if (nr != sizeof(hdr)) {
    149 		WARN(("read header short"));
    150 		errno = EFTYPE;
    151 		goto err;
    152 	}
    153 
    154 #ifdef BOOT_ECOFF
    155 	if (!ECOFF_BADMAG(&hdr.coff)) {
    156 		rval = loadfile_coff(fd, &hdr.coff, marks, flags);
    157 	} else
    158 #endif
    159 #ifdef BOOT_ELF32
    160 	if (memcmp(hdr.elf32.e_ident, ELFMAG, SELFMAG) == 0 &&
    161 	    hdr.elf32.e_ident[EI_CLASS] == ELFCLASS32) {
    162 	    	netbsd_elf_class = ELFCLASS32;
    163 		netbsd_elf_data = hdr.elf32.e_ident[EI_DATA];
    164 		rval = loadfile_elf32(fd, &hdr.elf32, marks, flags);
    165 	} else
    166 #endif
    167 #ifdef BOOT_ELF64
    168 	if (memcmp(hdr.elf64.e_ident, ELFMAG, SELFMAG) == 0 &&
    169 	    hdr.elf64.e_ident[EI_CLASS] == ELFCLASS64) {
    170 	    	netbsd_elf_class = ELFCLASS64;
    171 		netbsd_elf_data = hdr.elf64.e_ident[EI_DATA];
    172 		rval = loadfile_elf64(fd, &hdr.elf64, marks, flags);
    173 	} else
    174 #endif
    175 #ifdef BOOT_AOUT
    176 	if (OKMAGIC(N_GETMAGIC(hdr.aout))
    177 #ifndef NO_MID_CHECK
    178 	    && N_GETMID(hdr.aout) == MID_MACHINE
    179 #endif
    180 	    ) {
    181 		rval = loadfile_aout(fd, &hdr.aout, marks, flags);
    182 	} else
    183 #endif
    184 	{
    185 		rval = 1;
    186 		errno = EFTYPE;
    187 	}
    188 
    189 	if (rval == 0) {
    190 		if ((flags & LOAD_ALL) != 0)
    191 			PROGRESS(("=0x%lx\n",
    192 				  marks[MARK_END] - marks[MARK_START]));
    193 		return 0;
    194 	}
    195 err:
    196 	return errno;
    197 }
    198