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