1 /* $NetBSD: loadfile.c,v 1.30 2008/05/20 16:04:08 ad Exp $ */
4 * Copyright (c) 1997, 2008 The NetBSD Foundation, Inc.
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.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
34 * Copyright (c) 1992, 1993
35 * The Regents of the University of California. All rights reserved.
37 * This code is derived from software contributed to Berkeley by
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
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.
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
64 * @(#)boot.c 8.1 (Berkeley) 6/10/93
68 #include <lib/libsa/stand.h>
69 #include <lib/libkern/libkern.h>
80 #include <sys/param.h>
85 uint32_t netbsd_version
;
86 u_int netbsd_elf_class
;
89 * Open 'filename', read in program and return the opened file
90 * descriptor if ok, or -1 on error.
94 loadfile(const char *fname
, u_long
*marks
, int flags
)
99 if ((fd
= open(fname
, 0)) < 0) {
100 WARN(("open %s", fname
? fname
: "<default>"));
104 /* Load it; save the value of errno across the close() call */
105 if ((error
= fdloadfile(fd
, marks
, flags
)) != 0) {
115 * Read in program from the given file descriptor.
116 * Return error code (0 on success).
120 fdloadfile(int fd
, u_long
*marks
, int flags
)
124 struct ecoff_exechdr coff
;
139 /* Read the exec header. */
140 if (lseek(fd
, 0, SEEK_SET
) == (off_t
)-1)
142 nr
= read(fd
, &hdr
, sizeof(hdr
));
144 WARN(("read header failed"));
147 if (nr
!= sizeof(hdr
)) {
148 WARN(("read header short"));
154 if (!ECOFF_BADMAG(&hdr
.coff
)) {
155 rval
= loadfile_coff(fd
, &hdr
.coff
, marks
, flags
);
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
);
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
);
173 if (OKMAGIC(N_GETMAGIC(hdr
.aout
))
175 && N_GETMID(hdr
.aout
) == MID_MACHINE
178 rval
= loadfile_aout(fd
, &hdr
.aout
, marks
, flags
);
187 if ((flags
& LOAD_ALL
) != 0)
188 PROGRESS(("=0x%lx\n",
189 marks
[MARK_END
] - marks
[MARK_START
]));