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