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