bootxx.c revision 1.29 1 1.29 he /* $NetBSD: bootxx.c,v 1.29 2008/03/30 16:24:50 he 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.27 agc * 3. Neither the name of the University nor the names of its contributors
55 1.1 deraadt * may be used to endorse or promote products derived from this software
56 1.1 deraadt * without specific prior written permission.
57 1.1 deraadt *
58 1.1 deraadt * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59 1.1 deraadt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 1.1 deraadt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 1.1 deraadt * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 1.1 deraadt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 1.1 deraadt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 1.1 deraadt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 1.1 deraadt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 1.1 deraadt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 1.1 deraadt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 1.1 deraadt * SUCH DAMAGE.
69 1.1 deraadt *
70 1.4 cgd * @(#)boot.c 8.1 (Berkeley) 6/10/93
71 1.1 deraadt */
72 1.1 deraadt
73 1.1 deraadt #include <sys/param.h>
74 1.14 simonb #include <sys/exec_elf.h>
75 1.23 simonb #include <lib/libsa/stand.h>
76 1.25 jdolecek #include <lib/libkern/libkern.h>
77 1.9 simonb #include <machine/dec_prom.h>
78 1.1 deraadt
79 1.18 simonb typedef void (*entrypt) __P((int, char **, int, const void *));
80 1.18 simonb
81 1.18 simonb int main __P((int, char **));
82 1.23 simonb entrypt loadfile __P((char *path, char *name));
83 1.18 simonb
84 1.13 simonb extern int clear_cache __P((char *addr, int len));
85 1.1 deraadt
86 1.1 deraadt /*
87 1.1 deraadt * This gets arguments from the PROM, calls other routines to open
88 1.18 simonb * and load the secondary boot loader called boot, and then transfers
89 1.18 simonb * execution to that program.
90 1.18 simonb *
91 1.18 simonb * Argv[0] should be something like "rz(0,0,0)netbsd" on a DECstation 3100.
92 1.18 simonb * Argv[0,1] should be something like "boot 5/rz0/netbsd" on a DECstation 5000.
93 1.18 simonb * The argument "-a" means netbsd should do an automatic reboot.
94 1.1 deraadt */
95 1.5 mellon int
96 1.13 simonb main(argc, argv)
97 1.1 deraadt int argc;
98 1.1 deraadt char **argv;
99 1.1 deraadt {
100 1.18 simonb char *cp;
101 1.18 simonb entrypt entry;
102 1.6 jonathan
103 1.2 glass /* check for DS5000 boot */
104 1.2 glass if (strcmp(argv[0], "boot") == 0) {
105 1.1 deraadt argc--;
106 1.1 deraadt argv++;
107 1.1 deraadt }
108 1.2 glass cp = *argv;
109 1.13 simonb
110 1.23 simonb printf("\nNetBSD/pmax " NETBSD_VERS " " BOOTXX_FS_NAME " Primary Bootstrap\n");
111 1.23 simonb
112 1.23 simonb entry = loadfile(cp, "/boot.pmax");
113 1.23 simonb if ((int)entry != -1)
114 1.23 simonb goto goodload;
115 1.23 simonb
116 1.23 simonb /* Give old /boot a go... */
117 1.23 simonb entry = loadfile(cp, "/boot");
118 1.23 simonb if ((int)entry != -1)
119 1.23 simonb goto goodload;
120 1.23 simonb
121 1.23 simonb /* Booting off an 8.3 filesystem? */
122 1.23 simonb entry = loadfile(cp, "/boot.pma");
123 1.23 simonb if ((int)entry != -1)
124 1.23 simonb goto goodload;
125 1.23 simonb
126 1.23 simonb goto bad;
127 1.23 simonb goodload:
128 1.5 mellon
129 1.23 simonb clear_cache((char *)PRIMARY_LOAD_ADDRESS, 1024 * 1024);
130 1.2 glass if (callv == &callvec)
131 1.18 simonb entry(argc, argv, 0, 0);
132 1.2 glass else
133 1.18 simonb entry(argc, argv, DEC_PROM_MAGIC, callv);
134 1.23 simonb bad:
135 1.23 simonb /* XXX would calling prom_halt here be cleaner? */
136 1.13 simonb return (1);
137 1.1 deraadt }
138 1.1 deraadt
139 1.1 deraadt /*
140 1.1 deraadt * Open 'filename', read in program and return the entry point or -1 if error.
141 1.1 deraadt */
142 1.18 simonb entrypt
143 1.23 simonb loadfile(path, name)
144 1.23 simonb char *path, *name;
145 1.1 deraadt {
146 1.19 simonb int fd, i;
147 1.19 simonb char c, *buf, bootfname[64];
148 1.13 simonb Elf32_Ehdr ehdr;
149 1.13 simonb Elf32_Phdr phdr;
150 1.13 simonb
151 1.23 simonb strcpy(bootfname, path);
152 1.13 simonb buf = bootfname;
153 1.19 simonb while ((c = *buf++) != '\0') {
154 1.19 simonb if (c == ')')
155 1.13 simonb break;
156 1.19 simonb if (c != '/')
157 1.13 simonb continue;
158 1.19 simonb while ((c = *buf++) != '\0')
159 1.19 simonb if (c == '/')
160 1.13 simonb break;
161 1.19 simonb /*
162 1.19 simonb * Make "N/rzY" with no trailing '/' valid by adding
163 1.23 simonb * the extra '/' before appending 'bootpmax' to the path.
164 1.19 simonb */
165 1.19 simonb if (c != '/') {
166 1.19 simonb buf--;
167 1.19 simonb *buf++ = '/';
168 1.19 simonb *buf = '\0';
169 1.19 simonb }
170 1.13 simonb break;
171 1.13 simonb }
172 1.23 simonb strcpy(buf, name);
173 1.13 simonb if ((fd = open(bootfname, 0)) < 0) {
174 1.13 simonb printf("open %s: %d\n", bootfname, errno);
175 1.1 deraadt goto err;
176 1.1 deraadt }
177 1.1 deraadt
178 1.1 deraadt /* read the exec header */
179 1.13 simonb i = read(fd, (char *)&ehdr, sizeof(ehdr));
180 1.13 simonb if ((i != sizeof(ehdr)) ||
181 1.29 he (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0) ||
182 1.21 simonb (ehdr.e_ident[EI_CLASS] != ELFCLASS32)) {
183 1.13 simonb printf("%s: No ELF header\n", bootfname);
184 1.1 deraadt goto cerr;
185 1.1 deraadt }
186 1.1 deraadt
187 1.13 simonb for (i = 0; i < ehdr.e_phnum; i++) {
188 1.13 simonb if (lseek(fd, (off_t) ehdr.e_phoff + i * sizeof(phdr), 0) < 0)
189 1.13 simonb goto cerr;
190 1.13 simonb if (read(fd, &phdr, sizeof(phdr)) != sizeof(phdr))
191 1.13 simonb goto cerr;
192 1.20 kleink if (phdr.p_type != PT_LOAD)
193 1.13 simonb continue;
194 1.13 simonb if (lseek(fd, (off_t)phdr.p_offset, 0) < 0)
195 1.13 simonb goto cerr;
196 1.13 simonb if (read(fd, (char *)phdr.p_paddr, phdr.p_filesz) != phdr.p_filesz)
197 1.13 simonb goto cerr;
198 1.1 deraadt }
199 1.18 simonb return ((entrypt)ehdr.e_entry);
200 1.1 deraadt
201 1.1 deraadt cerr:
202 1.17 simonb #ifndef LIBSA_NO_FS_CLOSE
203 1.1 deraadt (void) close(fd);
204 1.2 glass #endif
205 1.1 deraadt err:
206 1.13 simonb printf("Can't load '%s'\n", bootfname);
207 1.18 simonb return ((entrypt)-1);
208 1.1 deraadt }
209