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