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