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