1 /* $NetBSD: vax.c,v 1.18 2014/11/13 16:02:25 christos 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.18 2014/11/13 16:02:25 christos 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 { "vax", vax_setboot, vax_clearboot, no_editboot, 105 IB_STAGE1START | IB_APPEND | IB_SUNSUM }; 106 107 static 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 __CTASSERT(sizeof(bb)==VAX_BOOT_BLOCK_BLOCKSIZE); 117 118 rv = pread(params->fsfd, &bb, sizeof(bb), VAX_BOOT_BLOCK_OFFSET); 119 if (rv == -1) { 120 warn("Reading `%s'", params->filesystem); 121 return (0); 122 } else if (rv != sizeof(bb)) { 123 warnx("Reading `%s': short read", params->filesystem); 124 return (0); 125 } 126 127 if (bb.bb_id_offset*2 >= VAX_BOOT_BLOCK_BLOCKSIZE 128 || bb.bb_magic1 != VAX_BOOT_MAGIC1) { 129 warnx( 130 "Old boot block magic number invalid; boot block invalid"); 131 return (0); 132 } 133 134 bb.bb_id_offset = 1; 135 bb.bb_mbone = 0; 136 bb.bb_lbn_hi = 0; 137 bb.bb_lbn_low = 0; 138 139 if (params->flags & IB_SUNSUM) { 140 uint16_t sum; 141 142 sum = compute_sunsum((uint16_t *)&bb); 143 if (! set_sunsum(params, (uint16_t *)&bb, sum)) 144 return (0); 145 } 146 147 if (params->flags & IB_VERBOSE) 148 printf("%slearing boot block\n", 149 (params->flags & IB_NOWRITE) ? "Not c" : "C"); 150 if (params->flags & IB_NOWRITE) 151 return (1); 152 153 rv = pwrite(params->fsfd, &bb, sizeof(bb), VAX_BOOT_BLOCK_OFFSET); 154 if (rv == -1) { 155 warn("Writing `%s'", params->filesystem); 156 return (0); 157 } else if (rv != sizeof(bb)) { 158 warnx("Writing `%s': short write", params->filesystem); 159 return (0); 160 } 161 162 return (1); 163 } 164 165 static int 166 vax_setboot(ib_params *params) 167 { 168 struct stat bootstrapsb; 169 struct vax_boot_block *bb; 170 uint32_t startblock; 171 int retval; 172 char *bootstrapbuf, oldbb[VAX_BOOT_BLOCK_BLOCKSIZE]; 173 size_t bootstrapsize; 174 uint32_t bootstrapload, bootstrapexec; 175 ssize_t rv; 176 177 assert(params != NULL); 178 assert(params->fsfd != -1); 179 assert(params->filesystem != NULL); 180 assert(params->s1fd != -1); 181 assert(params->stage1 != NULL); 182 183 /* see sys/arch/vax/boot/xxboot/start.S for explanation */ 184 __CTASSERT(offsetof(struct vax_boot_block,bb_magic1) == 0x19e); 185 __CTASSERT(sizeof(struct vax_boot_block) == VAX_BOOT_BLOCK_BLOCKSIZE); 186 187 startblock = 0; 188 retval = 0; 189 bootstrapbuf = NULL; 190 191 if (fstat(params->s1fd, &bootstrapsb) == -1) { 192 warn("Examining `%s'", params->stage1); 193 goto done; 194 } 195 if (!S_ISREG(bootstrapsb.st_mode)) { 196 warnx("`%s' must be a regular file", params->stage1); 197 goto done; 198 } 199 if (! load_bootstrap(params, &bootstrapbuf, &bootstrapload, 200 &bootstrapexec, &bootstrapsize)) 201 goto done; 202 203 /* read old boot block */ 204 rv = pread(params->fsfd, oldbb, sizeof(oldbb), VAX_BOOT_BLOCK_OFFSET); 205 if (rv == -1) { 206 warn("Reading `%s'", params->filesystem); 207 goto done; 208 } else if (rv != sizeof(oldbb)) { 209 warnx("Reading `%s': short read", params->filesystem); 210 goto done; 211 } 212 213 /* 214 * Copy disklabel from old boot block to new. 215 * Assume everything between VAX_LABELOFFSET and the start of 216 * the param block is scratch area and can be copied over. 217 */ 218 memcpy(bootstrapbuf + VAX_LABELOFFSET, 219 oldbb + VAX_LABELOFFSET, 220 offsetof(struct vax_boot_block,bb_magic1) - VAX_LABELOFFSET); 221 222 /* point to bootblock at begining of bootstrap */ 223 bb = (struct vax_boot_block*)bootstrapbuf; 224 225 /* fill in the updated boot block fields */ 226 if (params->flags & IB_APPEND) { 227 struct stat filesyssb; 228 229 if (fstat(params->fsfd, &filesyssb) == -1) { 230 warn("Examining `%s'", params->filesystem); 231 goto done; 232 } 233 if (!S_ISREG(filesyssb.st_mode)) { 234 warnx( 235 "`%s' must be a regular file to append a bootstrap", 236 params->filesystem); 237 goto done; 238 } 239 startblock = howmany(filesyssb.st_size, 240 VAX_BOOT_BLOCK_BLOCKSIZE); 241 bb->bb_lbn_hi = htole16((uint16_t) (startblock >> 16)); 242 bb->bb_lbn_low = htole16((uint16_t) (startblock >> 0)); 243 } 244 245 if (params->flags & IB_SUNSUM) { 246 uint16_t sum; 247 248 sum = compute_sunsum((uint16_t *)bb); 249 if (! set_sunsum(params, (uint16_t *)bb, sum)) 250 goto done; 251 } 252 253 if (params->flags & IB_VERBOSE) { 254 printf("Bootstrap start sector: %u\n", startblock); 255 printf("Bootstrap sector count: %u\n", le32toh(bb->bb_size)); 256 printf("%sriting bootstrap\n", 257 (params->flags & IB_NOWRITE) ? "Not w" : "W"); 258 } 259 if (params->flags & IB_NOWRITE) { 260 retval = 1; 261 goto done; 262 } 263 rv = pwrite(params->fsfd, bootstrapbuf, bootstrapsize, 0); 264 if (rv == -1) { 265 warn("Writing `%s'", params->filesystem); 266 goto done; 267 } else if ((size_t)rv != bootstrapsize) { 268 warnx("Writing `%s': short write", params->filesystem); 269 goto done; 270 } 271 retval = 1; 272 273 done: 274 if (bootstrapbuf) 275 free(bootstrapbuf); 276 return (retval); 277 } 278 279 static int 280 load_bootstrap(ib_params *params, char **data, 281 uint32_t *loadaddr, uint32_t *execaddr, size_t *len) 282 { 283 ssize_t cc; 284 size_t buflen; 285 286 buflen = 512 * (VAX_BOOT_SIZE + 1); 287 *data = malloc(buflen); 288 if (*data == NULL) { 289 warn("Allocating %lu bytes", (unsigned long) buflen); 290 return (0); 291 } 292 293 cc = pread(params->s1fd, *data, buflen, 0); 294 if (cc <= 0) { 295 warn("Reading `%s'", params->stage1); 296 return (0); 297 } 298 if (cc > 512 * VAX_BOOT_SIZE) { 299 warnx("`%s': too large", params->stage1); 300 return (0); 301 } 302 303 *len = roundup(cc, VAX_BOOT_BLOCK_BLOCKSIZE); 304 *loadaddr = VAX_BOOT_LOAD; 305 *execaddr = VAX_BOOT_ENTRY; 306 return (1); 307 } 308