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