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