vax.c revision 1.8 1 /* $NetBSD: vax.c,v 1.8 2002/05/15 02:18:24 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.8 2002/05/15 02:18:24 lukem Exp $");
75 #endif /* !__lint */
76
77 #if HAVE_CONFIG_H
78 #include "config.h"
79 #endif
80
81 #include <sys/param.h>
82
83 #include <assert.h>
84 #include <err.h>
85 #include <stddef.h>
86 #include <stdio.h>
87 #include <stdlib.h>
88 #include <unistd.h>
89
90 #include "installboot.h"
91
92 static int load_bootstrap(ib_params *, char **,
93 uint32_t *, uint32_t *, size_t *);
94
95
96 int
97 vax_parseopt(ib_params *params, const char *option)
98 {
99
100 if (parseoptionflag(params, option, IB_APPEND | IB_SUNSUM))
101 return (1);
102
103 warnx("Unknown -o option `%s'", option);
104 return (0);
105 }
106
107 int
108 vax_clearboot(ib_params *params)
109 {
110 struct vax_boot_block bb;
111 ssize_t rv;
112
113 assert(params != NULL);
114 assert(params->fsfd != -1);
115 assert(params->filesystem != NULL);
116 assert(sizeof(struct vax_boot_block) == VAX_BOOT_BLOCK_BLOCKSIZE);
117
118 if (params->flags & (IB_STAGE1START | IB_APPEND)) {
119 warnx("Can't use `-b bno' or `-o append' with `-c'");
120 return (0);
121 }
122 if (params->flags & IB_STAGE2START) {
123 warnx("`-B bno' is not supported for %s",
124 params->machine->name);
125 return (0);
126 }
127
128 rv = pread(params->fsfd, &bb, sizeof(bb), VAX_BOOT_BLOCK_OFFSET);
129 if (rv == -1) {
130 warn("Reading `%s'", params->filesystem);
131 return (0);
132 } else if (rv != sizeof(bb)) {
133 warnx("Reading `%s': short read", params->filesystem);
134 return (0);
135 }
136
137 if (bb.bb_id_offset * 2 != offsetof(struct vax_boot_block, bb_magic1)
138 || bb.bb_magic1 != VAX_BOOT_MAGIC1) {
139 warnx(
140 "Old boot block magic number invalid; boot block invalid");
141 return (0);
142 }
143
144 bb.bb_id_offset = 1;
145 bb.bb_mbone = 0;
146 bb.bb_lbn_hi = 0;
147 bb.bb_lbn_low = 0;
148
149 if (params->flags & IB_SUNSUM) {
150 uint16_t sum;
151
152 sum = compute_sunsum((uint16_t *)&bb);
153 if (! set_sunsum(params, (uint16_t *)&bb, sum))
154 return (0);
155 }
156
157 if (params->flags & IB_VERBOSE)
158 printf("%slearing boot block\n",
159 (params->flags & IB_NOWRITE) ? "Not c" : "C");
160 if (params->flags & IB_NOWRITE)
161 return (1);
162
163 rv = pwrite(params->fsfd, &bb, sizeof(bb), VAX_BOOT_BLOCK_OFFSET);
164 if (rv == -1) {
165 warn("Writing `%s'", params->filesystem);
166 return (0);
167 } else if (rv != sizeof(bb)) {
168 warnx("Writing `%s': short write", params->filesystem);
169 return (0);
170 }
171
172 return (1);
173 }
174
175 int
176 vax_setboot(ib_params *params)
177 {
178 struct stat bootstrapsb;
179 struct vax_boot_block bb;
180 uint32_t startblock;
181 int retval;
182 char *bootstrapbuf;
183 size_t bootstrapsize;
184 uint32_t bootstrapload, bootstrapexec;
185 ssize_t rv;
186
187 assert(params != NULL);
188 assert(params->fsfd != -1);
189 assert(params->filesystem != NULL);
190 assert(params->s1fd != -1);
191 assert(params->stage1 != NULL);
192 assert(sizeof(struct vax_boot_block) == VAX_BOOT_BLOCK_BLOCKSIZE);
193
194 retval = 0;
195 bootstrapbuf = NULL;
196
197 if ((params->flags & IB_STAGE1START) &&
198 (params->flags & IB_APPEND)) {
199 warnx("Can't use `-b bno' with `-o append'");
200 goto done;
201 }
202 if (params->flags & IB_STAGE2START) {
203 warnx("`-B bno' is not supported for %s",
204 params->machine->name);
205 goto done;
206 }
207
208 if (fstat(params->s1fd, &bootstrapsb) == -1) {
209 warn("Examining `%s'", params->stage1);
210 goto done;
211 }
212 if (!S_ISREG(bootstrapsb.st_mode)) {
213 warnx("`%s' must be a regular file", params->stage1);
214 goto done;
215 }
216 if (! load_bootstrap(params, &bootstrapbuf, &bootstrapload,
217 &bootstrapexec, &bootstrapsize))
218 goto done;
219
220 rv = pread(params->fsfd, &bb, sizeof(bb), VAX_BOOT_BLOCK_OFFSET);
221 if (rv == -1) {
222 warn("Reading `%s'", params->filesystem);
223 goto done;
224 } else if (rv != sizeof(bb)) {
225 warnx("Reading `%s': short read", params->filesystem);
226 goto done;
227 }
228
229 /* fill in the updated boot block fields */
230 if (params->flags & IB_APPEND) {
231 struct stat filesyssb;
232
233 if (fstat(params->fsfd, &filesyssb) == -1) {
234 warn("Examining `%s'", params->filesystem);
235 goto done;
236 }
237 if (!S_ISREG(filesyssb.st_mode)) {
238 warnx(
239 "`%s' must be a regular file to append a bootstrap",
240 params->filesystem);
241 goto done;
242 }
243 startblock = howmany(filesyssb.st_size,
244 VAX_BOOT_BLOCK_BLOCKSIZE);
245 } else if (params->flags & IB_STAGE1START) {
246 startblock = params->s1start;
247 } else {
248 startblock = VAX_BOOT_BLOCK_OFFSET / VAX_BOOT_BLOCK_BLOCKSIZE
249 + 1;
250 }
251
252 bb.bb_id_offset = offsetof(struct vax_boot_block, bb_magic1) / 2;
253 bb.bb_mbone = 1;
254 bb.bb_lbn_hi = htole16((uint16_t) (startblock >> 16));
255 bb.bb_lbn_low = htole16((uint16_t) (startblock >> 0));
256 /*
257 * Now the identification block
258 */
259 bb.bb_magic1 = VAX_BOOT_MAGIC1;
260 bb.bb_mbz1 = 0;
261 bb.bb_sum1 = ~(bb.bb_magic1 + bb.bb_mbz1 + bb.bb_pad1);
262
263 bb.bb_mbz2 = 0;
264 bb.bb_volinfo = VAX_BOOT_VOLINFO_NONE;
265 bb.bb_pad2a = 0;
266 bb.bb_pad2b = 0;
267
268 bb.bb_size = htole32(bootstrapsize / VAX_BOOT_BLOCK_BLOCKSIZE);
269 bb.bb_load = htole32(VAX_BOOT_LOAD);
270 bb.bb_entry = htole32(VAX_BOOT_ENTRY);
271 bb.bb_sum3 = htole32(le32toh(bb.bb_size) + le32toh(bb.bb_load) \
272 + le32toh(bb.bb_entry));
273
274 if (params->flags & IB_SUNSUM) {
275 uint16_t sum;
276
277 sum = compute_sunsum((uint16_t *)&bb);
278 if (! set_sunsum(params, (uint16_t *)&bb, sum))
279 goto done;
280 }
281
282 if (params->flags & IB_VERBOSE) {
283 printf("Bootstrap start sector: %u\n", startblock);
284 printf("Bootstrap sector count: %u\n", le32toh(bb.bb_size));
285 printf("%sriting bootstrap\n",
286 (params->flags & IB_NOWRITE) ? "Not w" : "W");
287 }
288 if (params->flags & IB_NOWRITE) {
289 retval = 1;
290 goto done;
291 }
292 rv = pwrite(params->fsfd, bootstrapbuf, bootstrapsize,
293 startblock * VAX_BOOT_BLOCK_BLOCKSIZE);
294 if (rv == -1) {
295 warn("Writing `%s'", params->filesystem);
296 goto done;
297 } else if (rv != bootstrapsize) {
298 warnx("Writing `%s': short write", params->filesystem);
299 goto done;
300 }
301
302 if (params->flags & IB_VERBOSE)
303 printf("Writing boot block\n");
304 rv = pwrite(params->fsfd, &bb, sizeof(bb), VAX_BOOT_BLOCK_OFFSET);
305 if (rv == -1) {
306 warn("Writing `%s'", params->filesystem);
307 goto done;
308 } else if (rv != sizeof(bb)) {
309 warnx("Writing `%s': short write", params->filesystem);
310 goto done;
311 } else {
312 retval = 1;
313 }
314
315 done:
316 if (bootstrapbuf)
317 free(bootstrapbuf);
318 return (retval);
319 }
320
321 static int
322 load_bootstrap(ib_params *params, char **data,
323 uint32_t *loadaddr, uint32_t *execaddr, size_t *len)
324 {
325 ssize_t cc;
326 size_t buflen;
327
328 buflen = 512 * (VAX_BOOT_SIZE + 1);
329 *data = malloc(buflen);
330 if (*data == NULL) {
331 warn("Allocating %lu bytes", (unsigned long) buflen);
332 return (0);
333 }
334
335 cc = pread(params->s1fd, *data, buflen, 0);
336 if (cc <= 0) {
337 warn("Reading `%s'", params->stage1);
338 return (0);
339 }
340 if (cc > 512 * VAX_BOOT_SIZE) {
341 warnx("`%s': too large", params->stage1);
342 return (0);
343 }
344
345 *len = roundup(cc, VAX_BOOT_BLOCK_BLOCKSIZE);
346 *loadaddr = VAX_BOOT_LOAD;
347 *execaddr = VAX_BOOT_ENTRY;
348 return (1);
349 }
350