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