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