bootxx.c revision 1.2 1 1.2 tsutsui /* $NetBSD: bootxx.c,v 1.2 2019/12/14 02:58:20 tsutsui Exp $ */
2 1.1 pooka
3 1.1 pooka /*-
4 1.1 pooka * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 1.1 pooka * All rights reserved.
6 1.1 pooka *
7 1.1 pooka * This code is derived from software contributed to The NetBSD Foundation
8 1.1 pooka * by Jonathan Stone, Michael Hitch and Simon Burge.
9 1.1 pooka *
10 1.1 pooka * Redistribution and use in source and binary forms, with or without
11 1.1 pooka * modification, are permitted provided that the following conditions
12 1.1 pooka * are met:
13 1.1 pooka * 1. Redistributions of source code must retain the above copyright
14 1.1 pooka * notice, this list of conditions and the following disclaimer.
15 1.1 pooka * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 pooka * notice, this list of conditions and the following disclaimer in the
17 1.1 pooka * documentation and/or other materials provided with the distribution.
18 1.1 pooka *
19 1.1 pooka * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 pooka * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 pooka * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 pooka * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 pooka * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 pooka * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 pooka * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 pooka * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 pooka * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 pooka * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 pooka * POSSIBILITY OF SUCH DAMAGE.
30 1.1 pooka */
31 1.1 pooka
32 1.1 pooka /*
33 1.1 pooka * Copyright (c) 1992, 1993
34 1.1 pooka * The Regents of the University of California. All rights reserved.
35 1.1 pooka *
36 1.1 pooka * This code is derived from software contributed to Berkeley by
37 1.1 pooka * Ralph Campbell.
38 1.1 pooka *
39 1.1 pooka * Redistribution and use in source and binary forms, with or without
40 1.1 pooka * modification, are permitted provided that the following conditions
41 1.1 pooka * are met:
42 1.1 pooka * 1. Redistributions of source code must retain the above copyright
43 1.1 pooka * notice, this list of conditions and the following disclaimer.
44 1.1 pooka * 2. Redistributions in binary form must reproduce the above copyright
45 1.1 pooka * notice, this list of conditions and the following disclaimer in the
46 1.1 pooka * documentation and/or other materials provided with the distribution.
47 1.1 pooka * 3. Neither the name of the University nor the names of its contributors
48 1.1 pooka * may be used to endorse or promote products derived from this software
49 1.1 pooka * without specific prior written permission.
50 1.1 pooka *
51 1.1 pooka * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 1.1 pooka * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 1.1 pooka * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 1.1 pooka * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 1.1 pooka * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 1.1 pooka * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 1.1 pooka * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 1.1 pooka * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 1.1 pooka * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 1.1 pooka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 1.1 pooka * SUCH DAMAGE.
62 1.1 pooka *
63 1.1 pooka * @(#)boot.c 8.1 (Berkeley) 6/10/93
64 1.1 pooka */
65 1.1 pooka
66 1.1 pooka #include <sys/param.h>
67 1.1 pooka #include <sys/exec_elf.h>
68 1.1 pooka #include <lib/libsa/stand.h>
69 1.1 pooka #include <lib/libkern/libkern.h>
70 1.1 pooka
71 1.1 pooka typedef void (entrypt) (int, char **, int, const void *, int, char *);
72 1.1 pooka
73 1.1 pooka entrypt main;
74 1.1 pooka entrypt *loadfile (char *path, char *name);
75 1.1 pooka
76 1.1 pooka /*
77 1.1 pooka * This gets arguments from the PROM, calls other routines to open
78 1.1 pooka * and load the secondary boot loader called boot, and then transfers
79 1.1 pooka * execution to that program.
80 1.1 pooka */
81 1.1 pooka void
82 1.1 pooka main(int argc, char **argv, int code, const void *cv, int bim, char *bip)
83 1.1 pooka {
84 1.1 pooka char *cp;
85 1.1 pooka entrypt *entry;
86 1.1 pooka
87 1.1 pooka cp = *argv;
88 1.1 pooka
89 1.1 pooka printf("\nNetBSD/emips " NETBSD_VERS " " BOOTXX_FS_NAME " Bootstrap\n");
90 1.1 pooka
91 1.1 pooka entry = loadfile(cp, "netbsd");
92 1.1 pooka if ((int)entry != -1)
93 1.1 pooka goto goodload;
94 1.1 pooka
95 1.1 pooka /* Give old /boot a go... */
96 1.1 pooka entry = loadfile(cp, "/boot");
97 1.1 pooka if ((int)entry != -1)
98 1.1 pooka goto goodload;
99 1.1 pooka
100 1.1 pooka goto bad;
101 1.1 pooka goodload:
102 1.1 pooka
103 1.1 pooka entry(argc, argv, code, cv, bim, bip);
104 1.1 pooka bad:
105 1.1 pooka printf("Boot failed.\n");
106 1.1 pooka }
107 1.1 pooka
108 1.1 pooka /*
109 1.1 pooka * Open 'filename', read in program and return the entry point or -1 if error.
110 1.1 pooka */
111 1.1 pooka entrypt *
112 1.1 pooka loadfile(char *path, char *name)
113 1.1 pooka {
114 1.1 pooka int fd, i;
115 1.1 pooka char c, *buf, bootfname[64];
116 1.1 pooka Elf32_Ehdr ehdr;
117 1.1 pooka Elf32_Phdr phdr;
118 1.1 pooka
119 1.1 pooka strcpy(bootfname, path);
120 1.1 pooka buf = bootfname;
121 1.1 pooka while ((c = *buf++) != '\0') {
122 1.1 pooka if (c == ')')
123 1.1 pooka break;
124 1.1 pooka if (c != '/')
125 1.1 pooka continue;
126 1.1 pooka while ((c = *buf++) != '\0')
127 1.1 pooka if (c == '/')
128 1.1 pooka break;
129 1.1 pooka /*
130 1.1 pooka * Make "N/rzY" with no trailing '/' valid by adding
131 1.1 pooka * the extra '/' before appending 'bootemips' to the path.
132 1.1 pooka */
133 1.1 pooka if (c != '/') {
134 1.1 pooka buf--;
135 1.1 pooka *buf++ = '/';
136 1.1 pooka *buf = '\0';
137 1.1 pooka }
138 1.1 pooka break;
139 1.1 pooka }
140 1.1 pooka strcpy(buf, name);
141 1.1 pooka if ((fd = open(bootfname, 0)) < 0) {
142 1.1 pooka printf("open %s: %d\n", bootfname, errno);
143 1.1 pooka goto err;
144 1.1 pooka }
145 1.1 pooka
146 1.1 pooka /* read the exec header */
147 1.1 pooka i = read(fd, (char *)&ehdr, sizeof(ehdr));
148 1.1 pooka if ((i != sizeof(ehdr)) ||
149 1.1 pooka (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0) ||
150 1.1 pooka (ehdr.e_ident[EI_CLASS] != ELFCLASS32)) {
151 1.1 pooka printf("%s: No ELF header\n", bootfname);
152 1.1 pooka goto cerr;
153 1.1 pooka }
154 1.1 pooka
155 1.1 pooka for (i = 0; i < ehdr.e_phnum; i++) {
156 1.1 pooka if (lseek(fd, (off_t) ehdr.e_phoff + i * sizeof(phdr), 0) < 0)
157 1.1 pooka goto cerr;
158 1.1 pooka if (read(fd, &phdr, sizeof(phdr)) != sizeof(phdr))
159 1.1 pooka goto cerr;
160 1.1 pooka if (phdr.p_type != PT_LOAD)
161 1.1 pooka continue;
162 1.1 pooka if (lseek(fd, (off_t)phdr.p_offset, 0) < 0)
163 1.1 pooka goto cerr;
164 1.1 pooka if (read(fd, (char *)phdr.p_paddr, phdr.p_filesz) != phdr.p_filesz)
165 1.1 pooka goto cerr;
166 1.1 pooka }
167 1.1 pooka return ((entrypt*)ehdr.e_entry);
168 1.1 pooka
169 1.1 pooka cerr:
170 1.1 pooka #ifndef LIBSA_NO_FS_CLOSE
171 1.1 pooka (void) close(fd);
172 1.1 pooka #endif
173 1.1 pooka err:
174 1.1 pooka printf("Can't load '%s'\n", bootfname);
175 1.1 pooka return ((entrypt*)-1);
176 1.1 pooka }
177