1 /* $NetBSD: vax.c,v 1.20 2022/01/24 09:14:38 andvar 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.20 2022/01/24 09:14:38 andvar Exp $");
72 #endif /* !__lint */
73
74 #include <sys/param.h>
75 #ifdef HAVE_NBTOOL_CONFIG_H
76 #include <nbinclude/vax/disklabel.h>
77 #else
78 #include <sys/disklabel.h>
79 #endif
80
81 #include <assert.h>
82 #include <err.h>
83 #include <stddef.h>
84 #include <stdio.h>
85 #include <stdlib.h>
86 #include <string.h>
87 #include <unistd.h>
88
89 #include "installboot.h"
90
91 #define VAX_LABELOFFSET 64
92
93 #ifndef __CTASSERT
94 #define __CTASSERT(X)
95 #endif
96
97 static int load_bootstrap(ib_params *, char **,
98 uint32_t *, uint32_t *, size_t *);
99
100 static int vax_clearboot(ib_params *);
101 static int vax_setboot(ib_params *);
102
103 struct ib_mach ib_mach_vax = {
104 .name = "vax",
105 .setboot = vax_setboot,
106 .clearboot = vax_clearboot,
107 .editboot = no_editboot,
108 .valid_flags = IB_STAGE1START | IB_APPEND | IB_SUNSUM,
109 };
110
111 static int
vax_clearboot(ib_params * params)112 vax_clearboot(ib_params *params)
113 {
114 struct vax_boot_block bb;
115 ssize_t rv;
116
117 assert(params != NULL);
118 assert(params->fsfd != -1);
119 assert(params->filesystem != NULL);
120 __CTASSERT(sizeof(bb)==VAX_BOOT_BLOCK_BLOCKSIZE);
121
122 rv = pread(params->fsfd, &bb, sizeof(bb), VAX_BOOT_BLOCK_OFFSET);
123 if (rv == -1) {
124 warn("Reading `%s'", params->filesystem);
125 return (0);
126 } else if (rv != sizeof(bb)) {
127 warnx("Reading `%s': short read", params->filesystem);
128 return (0);
129 }
130
131 if (bb.bb_id_offset*2 >= VAX_BOOT_BLOCK_BLOCKSIZE
132 || bb.bb_magic1 != VAX_BOOT_MAGIC1) {
133 warnx(
134 "Old boot block magic number invalid; boot block invalid");
135 return (0);
136 }
137
138 bb.bb_id_offset = 1;
139 bb.bb_mbone = 0;
140 bb.bb_lbn_hi = 0;
141 bb.bb_lbn_low = 0;
142
143 if (params->flags & IB_SUNSUM) {
144 uint16_t sum;
145
146 sum = compute_sunsum((uint16_t *)&bb);
147 if (! set_sunsum(params, (uint16_t *)&bb, sum))
148 return (0);
149 }
150
151 if (params->flags & IB_VERBOSE)
152 printf("%slearing boot block\n",
153 (params->flags & IB_NOWRITE) ? "Not c" : "C");
154 if (params->flags & IB_NOWRITE)
155 return (1);
156
157 rv = pwrite(params->fsfd, &bb, sizeof(bb), VAX_BOOT_BLOCK_OFFSET);
158 if (rv == -1) {
159 warn("Writing `%s'", params->filesystem);
160 return (0);
161 } else if (rv != sizeof(bb)) {
162 warnx("Writing `%s': short write", params->filesystem);
163 return (0);
164 }
165
166 return (1);
167 }
168
169 static int
vax_setboot(ib_params * params)170 vax_setboot(ib_params *params)
171 {
172 struct stat bootstrapsb;
173 struct vax_boot_block *bb;
174 uint32_t startblock;
175 int retval;
176 char *bootstrapbuf, oldbb[VAX_BOOT_BLOCK_BLOCKSIZE];
177 size_t bootstrapsize;
178 uint32_t bootstrapload, bootstrapexec;
179 ssize_t rv;
180
181 assert(params != NULL);
182 assert(params->fsfd != -1);
183 assert(params->filesystem != NULL);
184 assert(params->s1fd != -1);
185 assert(params->stage1 != NULL);
186
187 /* see sys/arch/vax/boot/xxboot/start.S for explanation */
188 __CTASSERT(offsetof(struct vax_boot_block,bb_magic1) == 0x19e);
189 __CTASSERT(sizeof(struct vax_boot_block) == VAX_BOOT_BLOCK_BLOCKSIZE);
190
191 startblock = 0;
192 retval = 0;
193 bootstrapbuf = NULL;
194
195 if (fstat(params->s1fd, &bootstrapsb) == -1) {
196 warn("Examining `%s'", params->stage1);
197 goto done;
198 }
199 if (!S_ISREG(bootstrapsb.st_mode)) {
200 warnx("`%s' must be a regular file", params->stage1);
201 goto done;
202 }
203 if (! load_bootstrap(params, &bootstrapbuf, &bootstrapload,
204 &bootstrapexec, &bootstrapsize))
205 goto done;
206
207 /* read old boot block */
208 rv = pread(params->fsfd, oldbb, sizeof(oldbb), VAX_BOOT_BLOCK_OFFSET);
209 if (rv == -1) {
210 warn("Reading `%s'", params->filesystem);
211 goto done;
212 } else if (rv != sizeof(oldbb)) {
213 warnx("Reading `%s': short read", params->filesystem);
214 goto done;
215 }
216
217 /*
218 * Copy disklabel from old boot block to new.
219 * Assume everything between VAX_LABELOFFSET and the start of
220 * the param block is scratch area and can be copied over.
221 */
222 memcpy(bootstrapbuf + VAX_LABELOFFSET,
223 oldbb + VAX_LABELOFFSET,
224 offsetof(struct vax_boot_block,bb_magic1) - VAX_LABELOFFSET);
225
226 /* point to bootblock at beginning of bootstrap */
227 bb = (struct vax_boot_block*)bootstrapbuf;
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 bb->bb_lbn_hi = htole16((uint16_t) (startblock >> 16));
246 bb->bb_lbn_low = htole16((uint16_t) (startblock >> 0));
247 }
248
249 if (params->flags & IB_SUNSUM) {
250 uint16_t sum;
251
252 sum = compute_sunsum((uint16_t *)bb);
253 if (! set_sunsum(params, (uint16_t *)bb, sum))
254 goto done;
255 }
256
257 if (params->flags & IB_VERBOSE) {
258 printf("Bootstrap start sector: %u\n", startblock);
259 printf("Bootstrap sector count: %u\n", le32toh(bb->bb_size));
260 printf("%sriting bootstrap\n",
261 (params->flags & IB_NOWRITE) ? "Not w" : "W");
262 }
263 if (params->flags & IB_NOWRITE) {
264 retval = 1;
265 goto done;
266 }
267 rv = pwrite(params->fsfd, bootstrapbuf, bootstrapsize, 0);
268 if (rv == -1) {
269 warn("Writing `%s'", params->filesystem);
270 goto done;
271 } else if ((size_t)rv != bootstrapsize) {
272 warnx("Writing `%s': short write", params->filesystem);
273 goto done;
274 }
275 retval = 1;
276
277 done:
278 if (bootstrapbuf)
279 free(bootstrapbuf);
280 return (retval);
281 }
282
283 static int
load_bootstrap(ib_params * params,char ** data,uint32_t * loadaddr,uint32_t * execaddr,size_t * len)284 load_bootstrap(ib_params *params, char **data,
285 uint32_t *loadaddr, uint32_t *execaddr, size_t *len)
286 {
287 ssize_t cc;
288 size_t buflen;
289
290 buflen = 512 * (VAX_BOOT_SIZE + 1);
291 *data = malloc(buflen);
292 if (*data == NULL) {
293 warn("Allocating %lu bytes", (unsigned long) buflen);
294 return (0);
295 }
296
297 cc = pread(params->s1fd, *data, buflen, 0);
298 if (cc <= 0) {
299 warn("Reading `%s'", params->stage1);
300 return (0);
301 }
302 if (cc > 512 * VAX_BOOT_SIZE) {
303 warnx("`%s': too large", params->stage1);
304 return (0);
305 }
306
307 *len = roundup(cc, VAX_BOOT_BLOCK_BLOCKSIZE);
308 *loadaddr = VAX_BOOT_LOAD;
309 *execaddr = VAX_BOOT_ENTRY;
310 return (1);
311 }
312