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