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