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