1f0e1b5deSdrahn /* $NetBSD: loadfile.c,v 1.10 2000/12/03 02:53:04 tsutsui Exp $ */
2*d6da11dcSderaadt /* $OpenBSD: loadfile.c,v 1.21 2021/10/24 17:49:19 deraadt Exp $ */
3f0e1b5deSdrahn
4f0e1b5deSdrahn /*-
5f0e1b5deSdrahn * Copyright (c) 1997 The NetBSD Foundation, Inc.
6f0e1b5deSdrahn * All rights reserved.
7f0e1b5deSdrahn *
8f0e1b5deSdrahn * This code is derived from software contributed to The NetBSD Foundation
9f0e1b5deSdrahn * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
10f0e1b5deSdrahn * NASA Ames Research Center and by Christos Zoulas.
11f0e1b5deSdrahn *
12f0e1b5deSdrahn * Redistribution and use in source and binary forms, with or without
13f0e1b5deSdrahn * modification, are permitted provided that the following conditions
14f0e1b5deSdrahn * are met:
15f0e1b5deSdrahn * 1. Redistributions of source code must retain the above copyright
16f0e1b5deSdrahn * notice, this list of conditions and the following disclaimer.
17f0e1b5deSdrahn * 2. Redistributions in binary form must reproduce the above copyright
18f0e1b5deSdrahn * notice, this list of conditions and the following disclaimer in the
19f0e1b5deSdrahn * documentation and/or other materials provided with the distribution.
20f0e1b5deSdrahn *
21f0e1b5deSdrahn * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22f0e1b5deSdrahn * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23f0e1b5deSdrahn * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24f0e1b5deSdrahn * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25f0e1b5deSdrahn * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26f0e1b5deSdrahn * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27f0e1b5deSdrahn * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28f0e1b5deSdrahn * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29f0e1b5deSdrahn * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30f0e1b5deSdrahn * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31f0e1b5deSdrahn * POSSIBILITY OF SUCH DAMAGE.
32f0e1b5deSdrahn */
33f0e1b5deSdrahn
34f0e1b5deSdrahn /*
35f0e1b5deSdrahn * Copyright (c) 1992, 1993
36f0e1b5deSdrahn * The Regents of the University of California. All rights reserved.
37f0e1b5deSdrahn *
38f0e1b5deSdrahn * This code is derived from software contributed to Berkeley by
39f0e1b5deSdrahn * Ralph Campbell.
40f0e1b5deSdrahn *
41f0e1b5deSdrahn * Redistribution and use in source and binary forms, with or without
42f0e1b5deSdrahn * modification, are permitted provided that the following conditions
43f0e1b5deSdrahn * are met:
44f0e1b5deSdrahn * 1. Redistributions of source code must retain the above copyright
45f0e1b5deSdrahn * notice, this list of conditions and the following disclaimer.
46f0e1b5deSdrahn * 2. Redistributions in binary form must reproduce the above copyright
47f0e1b5deSdrahn * notice, this list of conditions and the following disclaimer in the
48f0e1b5deSdrahn * documentation and/or other materials provided with the distribution.
4929295d1cSmillert * 3. Neither the name of the University nor the names of its contributors
50f0e1b5deSdrahn * may be used to endorse or promote products derived from this software
51f0e1b5deSdrahn * without specific prior written permission.
52f0e1b5deSdrahn *
53f0e1b5deSdrahn * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54f0e1b5deSdrahn * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55f0e1b5deSdrahn * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56f0e1b5deSdrahn * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57f0e1b5deSdrahn * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58f0e1b5deSdrahn * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59f0e1b5deSdrahn * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60f0e1b5deSdrahn * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61f0e1b5deSdrahn * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62f0e1b5deSdrahn * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63f0e1b5deSdrahn * SUCH DAMAGE.
64f0e1b5deSdrahn *
65f0e1b5deSdrahn * @(#)boot.c 8.1 (Berkeley) 6/10/93
66f0e1b5deSdrahn */
67f0e1b5deSdrahn
68f0e1b5deSdrahn #ifdef _STANDALONE
69f0e1b5deSdrahn #include <lib/libkern/libkern.h>
70f0e1b5deSdrahn #include <lib/libsa/stand.h>
71f0e1b5deSdrahn #else
72f0e1b5deSdrahn #include <stdio.h>
73f0e1b5deSdrahn #include <string.h>
74f0e1b5deSdrahn #include <errno.h>
75f0e1b5deSdrahn #include <stdlib.h>
76f0e1b5deSdrahn #include <unistd.h>
77f0e1b5deSdrahn #include <fcntl.h>
78f0e1b5deSdrahn #include <err.h>
79f0e1b5deSdrahn #endif
80f0e1b5deSdrahn
81f0e1b5deSdrahn #include <sys/param.h>
82f0e1b5deSdrahn #include <sys/exec.h>
83f0e1b5deSdrahn
84f0e1b5deSdrahn #include "loadfile.h"
85f0e1b5deSdrahn
86d64c9d94Stom #ifdef BOOT_ELF
87d64c9d94Stom #include <sys/exec_elf.h>
88d64c9d94Stom #if defined(BOOT_ELF32) && defined(BOOT_ELF64)
89d64c9d94Stom /*
90d64c9d94Stom * Both defined, so elf32_exec() and elf64_exec() need to be separately
91d64c9d94Stom * created (can't do it by including loadfile_elf.c here).
92d64c9d94Stom */
932340cfa5Sderaadt int elf32_exec(int, Elf32_Ehdr *, uint64_t *, int);
942340cfa5Sderaadt int elf64_exec(int, Elf64_Ehdr *, uint64_t *, int);
95d64c9d94Stom #else
96d64c9d94Stom #include "loadfile_elf.c"
97d64c9d94Stom #endif
98d64c9d94Stom #endif
99d64c9d94Stom
100f0e1b5deSdrahn /*
1010c1ce41cStom * Open 'filename', read in program and return -1 on error otherwise fd,
1020c1ce41cStom * with file still open.
1030c1ce41cStom * Also fills in marks.
104f0e1b5deSdrahn */
105f0e1b5deSdrahn int
loadfile(const char * fname,uint64_t * marks,int flags)1062340cfa5Sderaadt loadfile(const char *fname, uint64_t *marks, int flags)
107f0e1b5deSdrahn {
108f0e1b5deSdrahn union {
109d64c9d94Stom #if defined(BOOT_ELF32) || (defined(BOOT_ELF) && ELFSIZE == 32)
110d64c9d94Stom Elf32_Ehdr elf32;
111d64c9d94Stom #endif
112d64c9d94Stom #if defined(BOOT_ELF64) || (defined(BOOT_ELF) && ELFSIZE == 64)
113d64c9d94Stom Elf64_Ehdr elf64;
114f0e1b5deSdrahn #endif
115f0e1b5deSdrahn
116f0e1b5deSdrahn } hdr;
117f0e1b5deSdrahn ssize_t nr;
118f0e1b5deSdrahn int fd, rval;
119f0e1b5deSdrahn
120f0e1b5deSdrahn /* Open the file. */
121*d6da11dcSderaadt if ((fd = open(fname, O_RDONLY)) < 0) {
122f0e1b5deSdrahn WARN(("open %s", fname ? fname : "<default>"));
123f0e1b5deSdrahn return -1;
124f0e1b5deSdrahn }
125f0e1b5deSdrahn
126f0e1b5deSdrahn /* Read the exec header. */
127f0e1b5deSdrahn if ((nr = read(fd, &hdr, sizeof(hdr))) != sizeof(hdr)) {
128f0e1b5deSdrahn WARN(("read header"));
129f0e1b5deSdrahn goto err;
130f0e1b5deSdrahn }
131f0e1b5deSdrahn
132d64c9d94Stom #if defined(BOOT_ELF32) || (defined(BOOT_ELF) && ELFSIZE == 32)
133d64c9d94Stom if (memcmp(hdr.elf32.e_ident, ELFMAG, SELFMAG) == 0 &&
134d64c9d94Stom hdr.elf32.e_ident[EI_CLASS] == ELFCLASS32) {
135d64c9d94Stom rval = elf32_exec(fd, &hdr.elf32, marks, flags);
136d64c9d94Stom } else
137d64c9d94Stom #endif
138d64c9d94Stom #if defined(BOOT_ELF64) || (defined(BOOT_ELF) && ELFSIZE == 64)
139d64c9d94Stom if (memcmp(hdr.elf64.e_ident, ELFMAG, SELFMAG) == 0 &&
140d64c9d94Stom hdr.elf64.e_ident[EI_CLASS] == ELFCLASS64) {
141d64c9d94Stom rval = elf64_exec(fd, &hdr.elf64, marks, flags);
142f0e1b5deSdrahn } else
143f0e1b5deSdrahn #endif
144f0e1b5deSdrahn {
145f0e1b5deSdrahn rval = 1;
146f0e1b5deSdrahn errno = EFTYPE;
147f0e1b5deSdrahn WARN(("%s", fname ? fname : "<default>"));
148f0e1b5deSdrahn }
149f0e1b5deSdrahn
150f0e1b5deSdrahn if (rval == 0) {
151f0e1b5deSdrahn PROGRESS(("=0x%lx\n", marks[MARK_END] - marks[MARK_START]));
152f0e1b5deSdrahn return fd;
153f0e1b5deSdrahn }
154f0e1b5deSdrahn err:
155f0e1b5deSdrahn (void)close(fd);
156f0e1b5deSdrahn return -1;
157f0e1b5deSdrahn }
158