vax.c revision 1.2 1 1.2 lukem /* $NetBSD: vax.c,v 1.2 2002/04/12 06:50:41 lukem Exp $ */
2 1.1 lukem
3 1.1 lukem /*-
4 1.1 lukem * Copyright (c) 1999, 2002 The NetBSD Foundation, Inc.
5 1.1 lukem * All rights reserved.
6 1.1 lukem *
7 1.1 lukem * This code is derived from software contributed to The NetBSD Foundation
8 1.1 lukem * by Simon Burge.
9 1.1 lukem *
10 1.1 lukem * This code is derived from software contributed to The NetBSD Foundation
11 1.1 lukem * by Luke Mewburn of Wasabi Systems.
12 1.1 lukem *
13 1.1 lukem * Redistribution and use in source and binary forms, with or without
14 1.1 lukem * modification, are permitted provided that the following conditions
15 1.1 lukem * are met:
16 1.1 lukem * 1. Redistributions of source code must retain the above copyright
17 1.1 lukem * notice, this list of conditions and the following disclaimer.
18 1.1 lukem * 2. Redistributions in binary form must reproduce the above copyright
19 1.1 lukem * notice, this list of conditions and the following disclaimer in the
20 1.1 lukem * documentation and/or other materials provided with the distribution.
21 1.1 lukem * 3. All advertising materials mentioning features or use of this software
22 1.1 lukem * must display the following acknowledgement:
23 1.1 lukem * This product includes software developed by the NetBSD
24 1.1 lukem * Foundation, Inc. and its contributors.
25 1.1 lukem * 4. Neither the name of The NetBSD Foundation nor the names of its
26 1.1 lukem * contributors may be used to endorse or promote products derived
27 1.1 lukem * from this software without specific prior written permission.
28 1.1 lukem *
29 1.1 lukem * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
30 1.1 lukem * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
31 1.1 lukem * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
32 1.1 lukem * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
33 1.1 lukem * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34 1.1 lukem * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35 1.1 lukem * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36 1.1 lukem * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37 1.1 lukem * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 1.1 lukem * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39 1.1 lukem * POSSIBILITY OF SUCH DAMAGE.
40 1.1 lukem */
41 1.1 lukem
42 1.1 lukem /*
43 1.1 lukem * Copyright (c) 1999 Christopher G. Demetriou. All rights reserved.
44 1.1 lukem *
45 1.1 lukem * Redistribution and use in source and binary forms, with or without
46 1.1 lukem * modification, are permitted provided that the following conditions
47 1.1 lukem * are met:
48 1.1 lukem * 1. Redistributions of source code must retain the above copyright
49 1.1 lukem * notice, this list of conditions and the following disclaimer.
50 1.1 lukem * 2. Redistributions in binary form must reproduce the above copyright
51 1.1 lukem * notice, this list of conditions and the following disclaimer in the
52 1.1 lukem * documentation and/or other materials provided with the distribution.
53 1.1 lukem * 3. All advertising materials mentioning features or use of this software
54 1.1 lukem * must display the following acknowledgement:
55 1.1 lukem * This product includes software developed by Christopher G. Demetriou
56 1.1 lukem * for the NetBSD Project.
57 1.1 lukem * 4. The name of the author may not be used to endorse or promote products
58 1.1 lukem * derived from this software without specific prior written permission
59 1.1 lukem *
60 1.1 lukem * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
61 1.1 lukem * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
62 1.1 lukem * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
63 1.1 lukem * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
64 1.1 lukem * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
65 1.1 lukem * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
66 1.1 lukem * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
67 1.1 lukem * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
68 1.1 lukem * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
69 1.1 lukem * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
70 1.1 lukem */
71 1.1 lukem
72 1.1 lukem #include <sys/cdefs.h>
73 1.1 lukem #if defined(__RCSID) && !defined(__lint)
74 1.2 lukem __RCSID("$NetBSD: vax.c,v 1.2 2002/04/12 06:50:41 lukem Exp $");
75 1.1 lukem #endif /* !__lint */
76 1.1 lukem
77 1.1 lukem #include <sys/param.h>
78 1.1 lukem #include <sys/stat.h>
79 1.1 lukem
80 1.1 lukem #include <assert.h>
81 1.1 lukem #include <err.h>
82 1.1 lukem #include <stddef.h>
83 1.1 lukem #include <stdio.h>
84 1.1 lukem #include <stdlib.h>
85 1.1 lukem #include <unistd.h>
86 1.1 lukem
87 1.1 lukem #include <dev/dec/dec_boot.h>
88 1.1 lukem
89 1.1 lukem #include "installboot.h"
90 1.1 lukem
91 1.1 lukem int vax_parseopt(ib_params *, const char *);
92 1.1 lukem int vax_setboot(ib_params *);
93 1.1 lukem int vax_clearboot(ib_params *);
94 1.1 lukem static int load_bootstrap(ib_params *, char **,
95 1.1 lukem u_int32_t *, u_int32_t *, size_t *);
96 1.1 lukem
97 1.1 lukem
98 1.1 lukem int
99 1.1 lukem vax_parseopt(ib_params *params, const char *option)
100 1.1 lukem {
101 1.1 lukem
102 1.1 lukem if (parseoptionflag(params, option, IB_APPEND | IB_SUNSUM))
103 1.1 lukem return (1);
104 1.1 lukem
105 1.1 lukem warnx("Unknown -o option `%s'", option);
106 1.1 lukem return (0);
107 1.1 lukem }
108 1.1 lukem
109 1.1 lukem int
110 1.1 lukem vax_clearboot(ib_params *params)
111 1.1 lukem {
112 1.1 lukem struct vax_boot_block bb;
113 1.1 lukem ssize_t rv;
114 1.1 lukem
115 1.1 lukem assert(params != NULL);
116 1.1 lukem assert(params->fsfd != -1);
117 1.1 lukem assert(params->filesystem != NULL);
118 1.1 lukem assert(sizeof(struct vax_boot_block) == VAX_BOOT_BLOCK_BLOCKSIZE);
119 1.1 lukem
120 1.1 lukem if (params->flags & (IB_STARTBLOCK | IB_APPEND)) {
121 1.1 lukem warnx("Can't use `-b bno' or `-o append' with `-c'");
122 1.1 lukem return (0);
123 1.1 lukem }
124 1.1 lukem rv = pread(params->fsfd, &bb, sizeof(bb), VAX_BOOT_BLOCK_OFFSET);
125 1.1 lukem if (rv == -1) {
126 1.1 lukem warn("Reading `%s'", params->filesystem);
127 1.1 lukem return (0);
128 1.1 lukem } else if (rv != sizeof(bb)) {
129 1.1 lukem warnx("Reading `%s': short read", params->filesystem);
130 1.1 lukem return (0);
131 1.1 lukem }
132 1.1 lukem
133 1.1 lukem if (bb.bb_id_offset * 2 != offsetof(struct vax_boot_block, bb_magic1)
134 1.1 lukem || bb.bb_magic1 != VAX_BOOT_MAGIC1) {
135 1.1 lukem warnx(
136 1.1 lukem "Old boot block magic number invalid; boot block invalid");
137 1.1 lukem return (0);
138 1.1 lukem }
139 1.1 lukem
140 1.1 lukem bb.bb_id_offset = 1;
141 1.1 lukem bb.bb_mbone = 0;
142 1.1 lukem bb.bb_lbn_hi = 0;
143 1.1 lukem bb.bb_lbn_low = 0;
144 1.1 lukem
145 1.1 lukem if (params->flags & IB_SUNSUM) {
146 1.1 lukem u_int16_t sum;
147 1.1 lukem
148 1.1 lukem sum = compute_sunsum((u_int16_t *)&bb);
149 1.1 lukem if (! set_sunsum(params, (u_int16_t *)&bb, sum))
150 1.1 lukem return (0);
151 1.1 lukem }
152 1.1 lukem
153 1.1 lukem if (params->flags & IB_VERBOSE)
154 1.1 lukem printf("%slearing boot block\n",
155 1.1 lukem (params->flags & IB_NOWRITE) ? "Not c" : "C");
156 1.1 lukem if (params->flags & IB_NOWRITE)
157 1.1 lukem return (1);
158 1.1 lukem
159 1.1 lukem rv = pwrite(params->fsfd, &bb, sizeof(bb), VAX_BOOT_BLOCK_OFFSET);
160 1.1 lukem if (rv == -1) {
161 1.1 lukem warn("Writing `%s'", params->filesystem);
162 1.1 lukem return (0);
163 1.1 lukem } else if (rv != sizeof(bb)) {
164 1.1 lukem warnx("Writing `%s': short write", params->filesystem);
165 1.1 lukem return (0);
166 1.1 lukem }
167 1.1 lukem
168 1.1 lukem return (1);
169 1.1 lukem }
170 1.1 lukem
171 1.1 lukem int
172 1.1 lukem vax_setboot(ib_params *params)
173 1.1 lukem {
174 1.1 lukem struct stat bootstrapsb;
175 1.1 lukem struct vax_boot_block bb;
176 1.1 lukem int startblock, retval;
177 1.1 lukem char *bootstrapbuf;
178 1.1 lukem size_t bootstrapsize;
179 1.1 lukem u_int32_t bootstrapload, bootstrapexec;
180 1.1 lukem ssize_t rv;
181 1.1 lukem
182 1.1 lukem assert(params != NULL);
183 1.1 lukem assert(params->fsfd != -1);
184 1.1 lukem assert(params->filesystem != NULL);
185 1.2 lukem assert(params->s1fd != -1);
186 1.2 lukem assert(params->stage1 != NULL);
187 1.1 lukem assert(sizeof(struct vax_boot_block) == VAX_BOOT_BLOCK_BLOCKSIZE);
188 1.1 lukem
189 1.1 lukem retval = 0;
190 1.1 lukem bootstrapbuf = NULL;
191 1.1 lukem
192 1.1 lukem if ((params->flags & IB_STARTBLOCK) &&
193 1.1 lukem (params->flags & IB_APPEND)) {
194 1.1 lukem warnx("Can't use `-b bno' with `-o append'");
195 1.1 lukem goto done;
196 1.1 lukem }
197 1.1 lukem
198 1.2 lukem if (fstat(params->s1fd, &bootstrapsb) == -1) {
199 1.2 lukem warn("Examining `%s'", params->stage1);
200 1.1 lukem goto done;
201 1.1 lukem }
202 1.1 lukem if (!S_ISREG(bootstrapsb.st_mode)) {
203 1.2 lukem warnx("`%s' must be a regular file", params->stage1);
204 1.1 lukem goto done;
205 1.1 lukem }
206 1.1 lukem if (! load_bootstrap(params, &bootstrapbuf, &bootstrapload,
207 1.1 lukem &bootstrapexec, &bootstrapsize))
208 1.1 lukem goto done;
209 1.1 lukem
210 1.1 lukem rv = pread(params->fsfd, &bb, sizeof(bb), VAX_BOOT_BLOCK_OFFSET);
211 1.1 lukem if (rv == -1) {
212 1.1 lukem warn("Reading `%s'", params->filesystem);
213 1.1 lukem goto done;
214 1.1 lukem } else if (rv != sizeof(bb)) {
215 1.1 lukem warnx("Reading `%s': short read", params->filesystem);
216 1.1 lukem goto done;
217 1.1 lukem }
218 1.1 lukem
219 1.1 lukem /* fill in the updated boot block fields */
220 1.1 lukem if (params->flags & IB_APPEND) {
221 1.1 lukem struct stat filesyssb;
222 1.1 lukem
223 1.1 lukem if (fstat(params->fsfd, &filesyssb) == -1) {
224 1.1 lukem warn("Examining `%s'", params->filesystem);
225 1.1 lukem goto done;
226 1.1 lukem }
227 1.1 lukem if (!S_ISREG(filesyssb.st_mode)) {
228 1.1 lukem warnx(
229 1.2 lukem "`%s' must be a regular file to append a bootstrap",
230 1.1 lukem params->filesystem);
231 1.1 lukem goto done;
232 1.1 lukem }
233 1.1 lukem startblock = howmany(filesyssb.st_size,
234 1.1 lukem VAX_BOOT_BLOCK_BLOCKSIZE);
235 1.1 lukem } else if (params->flags & IB_STARTBLOCK) {
236 1.1 lukem startblock = params->startblock;
237 1.1 lukem } else {
238 1.1 lukem startblock = VAX_BOOT_BLOCK_OFFSET / VAX_BOOT_BLOCK_BLOCKSIZE
239 1.1 lukem + 1;
240 1.1 lukem }
241 1.1 lukem
242 1.1 lukem bb.bb_id_offset = offsetof(struct vax_boot_block, bb_magic1) / 2;
243 1.1 lukem bb.bb_mbone = 1;
244 1.1 lukem bb.bb_lbn_hi = htole16((u_int16_t) (startblock >> 16));
245 1.1 lukem bb.bb_lbn_low = htole16((u_int16_t) (startblock >> 0));
246 1.1 lukem /*
247 1.1 lukem * Now the identification block
248 1.1 lukem */
249 1.1 lukem bb.bb_magic1 = VAX_BOOT_MAGIC1;
250 1.1 lukem bb.bb_mbz1 = 0;
251 1.1 lukem bb.bb_sum1 = ~(bb.bb_magic1 + bb.bb_mbz1 + bb.bb_pad1);
252 1.1 lukem
253 1.1 lukem bb.bb_mbz2 = 0;
254 1.1 lukem bb.bb_volinfo = VAX_BOOT_VOLINFO_NONE;
255 1.1 lukem bb.bb_pad2a = 0;
256 1.1 lukem bb.bb_pad2b = 0;
257 1.1 lukem
258 1.1 lukem bb.bb_size = htole32(bootstrapsize / VAX_BOOT_BLOCK_BLOCKSIZE);
259 1.1 lukem bb.bb_load = htole32(VAX_BOOT_LOAD);
260 1.1 lukem bb.bb_entry = htole32(VAX_BOOT_ENTRY);
261 1.1 lukem bb.bb_sum3 = htole32(le32toh(bb.bb_size) + le32toh(bb.bb_load) \
262 1.1 lukem + le32toh(bb.bb_entry));
263 1.1 lukem
264 1.1 lukem if (params->flags & IB_SUNSUM) {
265 1.1 lukem u_int16_t sum;
266 1.1 lukem
267 1.1 lukem sum = compute_sunsum((u_int16_t *)&bb);
268 1.1 lukem if (! set_sunsum(params, (u_int16_t *)&bb, sum))
269 1.1 lukem goto done;
270 1.1 lukem }
271 1.1 lukem
272 1.1 lukem if (params->flags & IB_VERBOSE) {
273 1.1 lukem printf("Bootstrap start sector: %#x\n", startblock);
274 1.1 lukem printf("Bootstrap sector count: %#x\n", le32toh(bb.bb_size));
275 1.1 lukem printf("%sriting bootstrap\n",
276 1.1 lukem (params->flags & IB_NOWRITE) ? "Not w" : "W");
277 1.1 lukem }
278 1.1 lukem if (params->flags & IB_NOWRITE) {
279 1.1 lukem retval = 1;
280 1.1 lukem goto done;
281 1.1 lukem }
282 1.1 lukem rv = pwrite(params->fsfd, bootstrapbuf, bootstrapsize,
283 1.1 lukem startblock * VAX_BOOT_BLOCK_BLOCKSIZE);
284 1.1 lukem if (rv == -1) {
285 1.1 lukem warn("Writing `%s'", params->filesystem);
286 1.1 lukem goto done;
287 1.1 lukem } else if (rv != bootstrapsize) {
288 1.1 lukem warnx("Writing `%s': short write", params->filesystem);
289 1.1 lukem goto done;
290 1.1 lukem }
291 1.1 lukem
292 1.1 lukem if (params->flags & IB_VERBOSE)
293 1.1 lukem printf("Writing boot block\n");
294 1.1 lukem rv = pwrite(params->fsfd, &bb, sizeof(bb), VAX_BOOT_BLOCK_OFFSET);
295 1.1 lukem if (rv == -1) {
296 1.1 lukem warn("Writing `%s'", params->filesystem);
297 1.1 lukem goto done;
298 1.1 lukem } else if (rv != sizeof(bb)) {
299 1.1 lukem warnx("Writing `%s': short write", params->filesystem);
300 1.1 lukem goto done;
301 1.1 lukem } else {
302 1.1 lukem retval = 1;
303 1.1 lukem }
304 1.1 lukem
305 1.1 lukem done:
306 1.1 lukem if (bootstrapbuf)
307 1.1 lukem free(bootstrapbuf);
308 1.1 lukem return (retval);
309 1.1 lukem }
310 1.1 lukem
311 1.1 lukem static int
312 1.1 lukem load_bootstrap(ib_params *params, char **data,
313 1.1 lukem u_int32_t *loadaddr, u_int32_t *execaddr, size_t *len)
314 1.1 lukem {
315 1.1 lukem ssize_t cc;
316 1.1 lukem size_t buflen;
317 1.1 lukem
318 1.1 lukem buflen = 512 * (VAX_BOOT_SIZE + 1);
319 1.1 lukem *data = malloc(buflen);
320 1.1 lukem if (*data == NULL) {
321 1.1 lukem warn("Allocating %lu bytes", (unsigned long) buflen);
322 1.1 lukem return (0);
323 1.1 lukem }
324 1.1 lukem
325 1.2 lukem cc = pread(params->s1fd, *data, buflen, 0);
326 1.1 lukem if (cc <= 0) {
327 1.2 lukem warn("Reading `%s'", params->stage1);
328 1.1 lukem return (0);
329 1.1 lukem }
330 1.1 lukem if (cc > 512 * VAX_BOOT_SIZE) {
331 1.2 lukem warnx("`%s': too large", params->stage1);
332 1.1 lukem return (0);
333 1.1 lukem }
334 1.1 lukem
335 1.1 lukem *len = roundup(cc, VAX_BOOT_BLOCK_BLOCKSIZE);
336 1.1 lukem *loadaddr = VAX_BOOT_LOAD;
337 1.1 lukem *execaddr = VAX_BOOT_ENTRY;
338 1.1 lukem return (1);
339 1.1 lukem }
340