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