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