Home | History | Annotate | Line # | Download | only in arch
alpha.c revision 1.2
      1 /*	$NetBSD: alpha.c,v 1.2 2002/04/04 00:32:06 lukem Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2002 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Luke Mewburn of Wasabi Systems.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Copyright (c) 1999 Ross Harvey.  All rights reserved.
     41  *
     42  * Redistribution and use in source and binary forms, with or without
     43  * modification, are permitted provided that the following conditions
     44  * are met:
     45  * 1. Redistributions of source code must retain the above copyright
     46  *    notice, this list of conditions and the following disclaimer.
     47  * 2. Redistributions in binary form must reproduce the above copyright
     48  *    notice, this list of conditions and the following disclaimer in the
     49  *    documentation and/or other materials provided with the distribution.
     50  * 3. All advertising materials mentioning features or use of this software
     51  *    must display the following acknowledgement:
     52  *      This product includes software developed by Ross Harvey
     53  *	for the NetBSD Project.
     54  * 4. The name of the author may not be used to endorse or promote products
     55  *    derived from this software without specific prior written permission
     56  *
     57  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     58  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     59  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     60  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     61  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     62  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     63  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     64  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     65  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     66  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     67  */
     68 
     69 /*
     70  * Copyright (c) 1999 Christopher G. Demetriou.  All rights reserved.
     71  *
     72  * Redistribution and use in source and binary forms, with or without
     73  * modification, are permitted provided that the following conditions
     74  * are met:
     75  * 1. Redistributions of source code must retain the above copyright
     76  *    notice, this list of conditions and the following disclaimer.
     77  * 2. Redistributions in binary form must reproduce the above copyright
     78  *    notice, this list of conditions and the following disclaimer in the
     79  *    documentation and/or other materials provided with the distribution.
     80  * 3. All advertising materials mentioning features or use of this software
     81  *    must display the following acknowledgement:
     82  *      This product includes software developed by Christopher G. Demetriou
     83  *	for the NetBSD Project.
     84  * 4. The name of the author may not be used to endorse or promote products
     85  *    derived from this software without specific prior written permission
     86  *
     87  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     88  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     89  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     90  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     91  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     92  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     93  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     94  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     95  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     96  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     97  */
     98 
     99 #include <sys/cdefs.h>
    100 #if defined(__RCSID) && !defined(__lint)
    101 __RCSID("$NetBSD: alpha.c,v 1.2 2002/04/04 00:32:06 lukem Exp $");
    102 #endif	/* !__lint */
    103 
    104 #include <sys/param.h>
    105 #include <sys/stat.h>
    106 
    107 #include <assert.h>
    108 #include <err.h>
    109 #include <stddef.h>
    110 #include <stdio.h>
    111 #include <stdlib.h>
    112 #include <unistd.h>
    113 
    114 #include <dev/dec/dec_boot.h>
    115 
    116 #include "installboot.h"
    117 
    118 #define	SUN_DKMAGIC	55998		/* XXX: from <dev/sun/disklabel.h> */
    119 
    120 int		alpha_parseopt(ib_params *, const char *);
    121 int		alpha_setboot(ib_params *);
    122 int		alpha_clearboot(ib_params *);
    123 static void	resum(ib_params *, struct alpha_boot_block * const bb,
    124 			u_int16_t *bb16);
    125 static void	sun_bootstrap(ib_params *, struct alpha_boot_block * const);
    126 static void	check_sparc(const struct alpha_boot_block * const,
    127 			    const char *);
    128 
    129 
    130 int
    131 alpha_parseopt(ib_params *params, const char *option)
    132 {
    133 
    134 	if (parseoptionflag(params, option,
    135 	    IB_ALPHASUM | IB_APPEND | IB_SUNSUM))
    136 		return (1);
    137 
    138 	warnx("Unknown -o option `%s'", option);
    139 	return (0);
    140 }
    141 
    142 int
    143 alpha_clearboot(ib_params *params)
    144 {
    145 	struct alpha_boot_block	bb;
    146 	u_int64_t		cksum;
    147 	ssize_t			rv;
    148 
    149 	assert(params != NULL);
    150 	assert(params->fsfd != -1);
    151 	assert(params->filesystem != NULL);
    152 	assert(sizeof(struct alpha_boot_block) == ALPHA_BOOT_BLOCK_BLOCKSIZE);
    153 
    154 	if (params->flags & (IB_STARTBLOCK | IB_APPEND)) {
    155 		warnx("Can't use `-b bno' or `-o append' with `-c'");
    156 		return (0);
    157 	}
    158 	rv = pread(params->fsfd, &bb, sizeof(bb), ALPHA_BOOT_BLOCK_OFFSET);
    159 	if (rv == -1) {
    160 		warn("Reading `%s'", params->filesystem);
    161 		return (0);
    162 	} else if (rv != sizeof(bb)) {
    163 		warnx("Reading `%s': short read", params->filesystem);
    164 		return (0);
    165 	}
    166 	ALPHA_BOOT_BLOCK_CKSUM(&bb, &cksum);
    167 	if (cksum != bb.bb_cksum) {		// XXX check bb_cksum endian?
    168 		warnx(
    169 	    "Old boot block checksum invalid (was %#llx, calculated %#llx)",
    170 		    (unsigned long long)bb.bb_cksum,
    171 		    (unsigned long long)cksum);
    172 		warnx("Boot block invalid\n");
    173 		return (0);
    174 	}
    175 
    176 	if (params->flags & IB_VERBOSE) {
    177 		printf("Old boot block start sector: %#llx\n",
    178 		    (unsigned long long)le64toh(bb.bb_secstart));
    179 		printf("Old boot block size:         %#llx\n",
    180 		    (unsigned long long)le64toh(bb.bb_secsize));
    181 		printf("Old boot block checksum:     %#llx\n",
    182 		    (unsigned long long)bb.bb_cksum);
    183 	}
    184 
    185 	bb.bb_secstart = bb.bb_secsize = bb.bb_flags = 0;
    186 
    187 	ALPHA_BOOT_BLOCK_CKSUM(&bb, &bb.bb_cksum);
    188 	if (params->flags & IB_SUNSUM)
    189 		sun_bootstrap(params, &bb);
    190 
    191 	printf("New boot block start sector: %#llx\n",
    192 	    (unsigned long long)le64toh(bb.bb_secstart));
    193 	printf("New boot block size:         %#llx\n",
    194 	    (unsigned long long)le64toh(bb.bb_secsize));
    195 	printf("New boot block checksum:     %#llx\n",
    196 	    (unsigned long long)bb.bb_cksum);
    197 
    198 	if (params->flags & IB_VERBOSE)
    199 		printf("%slearing boot block\n",
    200 		    (params->flags & IB_NOWRITE) ? "Not c" : "C");
    201 	if (params->flags & IB_NOWRITE)
    202 		return (1);
    203 
    204 	rv = pwrite(params->fsfd, &bb, sizeof(bb), ALPHA_BOOT_BLOCK_OFFSET);
    205 	if (rv == -1) {
    206 		warn("Writing `%s'", params->filesystem);
    207 		return (0);
    208 	} else if (rv != sizeof(bb)) {
    209 		warnx("Writing `%s': short write", params->filesystem);
    210 		return (0);
    211 	}
    212 
    213 	return (1);
    214 }
    215 
    216 int
    217 alpha_setboot(ib_params *params)
    218 {
    219 	struct stat		bootstrapsb;
    220 	struct alpha_boot_block	bb;
    221 	u_int64_t		startblock;
    222 	int			retval;
    223 	char			*bootstrapbuf;
    224 	size_t			bootstrapsize;
    225 	ssize_t			rv;
    226 
    227 	assert(params != NULL);
    228 	assert(params->fsfd != -1);
    229 	assert(params->filesystem != NULL);
    230 	assert(params->bbfd != -1);
    231 	assert(params->bootblock != NULL);
    232 	assert(sizeof(struct alpha_boot_block) == ALPHA_BOOT_BLOCK_BLOCKSIZE);
    233 
    234 	retval = 0;
    235 	bootstrapbuf = NULL;
    236 
    237 	if ((params->flags & IB_STARTBLOCK) &&
    238 	    (params->flags & IB_APPEND)) {
    239 		warnx("Can't use `-b bno' with `-o append'");
    240 		goto done;
    241 	}
    242 
    243 	if (fstat(params->bbfd, &bootstrapsb) == -1) {
    244 		warn("Examining `%s'", params->bootblock);
    245 		goto done;
    246 	}
    247 	if (!S_ISREG(bootstrapsb.st_mode)) {
    248 		warnx("`%s' must be a regular file", params->bootblock);
    249 		goto done;
    250 	}
    251 	/*
    252 	 * Allocate a buffer, with space to round up the input file
    253 	 * to the next block size boundary, and with space for the boot
    254 	 * block.
    255 	 */
    256 	bootstrapsize = roundup(bootstrapsb.st_size,
    257 	    ALPHA_BOOT_BLOCK_BLOCKSIZE);
    258 
    259 	bootstrapbuf = malloc(bootstrapsize);
    260 	if (bootstrapbuf == NULL) {
    261 		warn("Allocating %lu bytes", (unsigned long) bootstrapsize);
    262 		goto done;
    263 	}
    264 	memset(bootstrapbuf, 0, bootstrapsize);
    265 
    266 	/* read the file into the buffer */
    267 	rv = pread(params->bbfd, bootstrapbuf, bootstrapsb.st_size, 0);
    268 	if (rv == -1) {
    269 		warn("Reading `%s'", params->bootblock);
    270 		return (0);
    271 	} else if (rv != sizeof(bb)) {
    272 		warnx("Reading `%s': short read", params->bootblock);
    273 		return (0);
    274 	}
    275 
    276 	rv = pread(params->fsfd, &bb, sizeof(bb), ALPHA_BOOT_BLOCK_OFFSET);
    277 	if (rv == -1) {
    278 		warn("Reading `%s'", params->filesystem);
    279 		goto done;
    280 	} else if (rv != sizeof(bb)) {
    281 		warnx("Reading `%s': short read", params->filesystem);
    282 		goto done;
    283 	}
    284 
    285 	if (params->flags & IB_SUNSUM)
    286 		check_sparc(&bb, "Initial");
    287 
    288 		/* fill in the updated boot block fields */
    289 	if (params->flags & IB_APPEND) {
    290 		struct stat	filesyssb;
    291 
    292 		if (fstat(params->fsfd, &filesyssb) == -1) {
    293 			warn("Examining `%s'", params->filesystem);
    294 			goto done;
    295 		}
    296 		if (!S_ISREG(filesyssb.st_mode)) {
    297 			warnx(
    298 		    "`%s' must be a regular file to append a boot block",
    299 			    params->filesystem);
    300 			goto done;
    301 		}
    302 		startblock = howmany(filesyssb.st_size,
    303 		    ALPHA_BOOT_BLOCK_BLOCKSIZE);
    304 	} else if (params->flags & IB_STARTBLOCK) {
    305 		startblock = params->startblock;
    306 	} else {
    307 		startblock = ALPHA_BOOT_BLOCK_OFFSET /
    308 		    ALPHA_BOOT_BLOCK_BLOCKSIZE + 1;
    309 	}
    310 
    311 	bb.bb_secsize =
    312 	    htole64(howmany(bootstrapsb.st_size, ALPHA_BOOT_BLOCK_BLOCKSIZE));
    313 	bb.bb_secstart = htole64(startblock);
    314 	bb.bb_flags = 0;
    315 
    316 	ALPHA_BOOT_BLOCK_CKSUM(&bb, &bb.bb_cksum);
    317 	if (params->flags & IB_SUNSUM)
    318 		sun_bootstrap(params, &bb);
    319 
    320 	if (params->flags & IB_VERBOSE) {
    321 		printf("Bootstrap start sector:  %#llx\n",
    322 		    startblock);
    323 		printf("Bootstrap sector count:  %#llx\n",
    324 		    le64toh(bb.bb_secsize));
    325 		printf("New boot block checksum: %#llx\n",
    326 		    (unsigned long long)bb.bb_cksum);
    327 		printf("%sriting bootstrap\n",
    328 		    (params->flags & IB_NOWRITE) ? "Not w" : "W");
    329 	}
    330 	if (params->flags & IB_NOWRITE) {
    331 		retval = 1;
    332 		goto done;
    333 	}
    334 	rv = pwrite(params->fsfd, bootstrapbuf, bootstrapsize,
    335 	     startblock * ALPHA_BOOT_BLOCK_BLOCKSIZE);
    336 	if (rv == -1) {
    337 		warn("Writing `%s'", params->filesystem);
    338 		goto done;
    339 	} else if (rv != bootstrapsize) {
    340 		warnx("Writing `%s': short write", params->filesystem);
    341 		goto done;
    342 	}
    343 
    344 	if (params->flags & IB_VERBOSE)
    345 		printf("Writing boot block\n");
    346 	rv = pwrite(params->fsfd, &bb, sizeof(bb), ALPHA_BOOT_BLOCK_OFFSET);
    347 	if (rv == -1) {
    348 		warn("Writing `%s'", params->filesystem);
    349 		goto done;
    350 	} else if (rv != sizeof(bb)) {
    351 		warnx("Writing `%s': short write", params->filesystem);
    352 		goto done;
    353 	} else {
    354 		retval = 1;
    355 	}
    356 
    357  done:
    358 	if (bootstrapbuf)
    359 		free(bootstrapbuf);
    360 	return (retval);
    361 }
    362 
    363 
    364 /*
    365  * The Sun and alpha checksums overlay, and the Sun magic number also
    366  * overlays the alpha checksum. If you think you are smart: stop here
    367  * and do exercise one: figure out how to salt unimportant u_int16_t
    368  * words in mid-sector so that the alpha and sparc checksums match,
    369  * and so the Sun magic number is embedded in the alpha checksum.
    370  *
    371  * The last u_int64_t in the sector is the alpha arithmetic checksum.
    372  * The last u_int16_t in the sector is the sun xor checksum.
    373  * The penultimate u_int16_t in the sector is the sun magic number.
    374  *
    375  *	A:   511     510     509     508     507     506     505     504
    376  *	S:   510     511     508     509     506     507     504     505
    377  *	63     :       :       :     32:31     :       :       :       0
    378  *	|      :       :       :      \:|      :       :       :       |
    379  *	7654321076543210765432107654321076543210765432107654321076543210
    380  *	|--  sparc   --||--  sparc   --|
    381  *	|-- checksum --||--  magic   --|
    382  *	|----------------------- alpha checksum -----------------------|
    383  *			1011111011011010
    384  *			   b   e   d   a
    385  */
    386 
    387 static void
    388 resum(ib_params *params, struct alpha_boot_block * const bb, u_int16_t *bb16)
    389 {
    390 	static u_int64_t lastsum;
    391 
    392 	if (bb16 != NULL)
    393 		memcpy(bb, bb16, sizeof(*bb));
    394 	ALPHA_BOOT_BLOCK_CKSUM(bb, &bb->bb_cksum);
    395 	if (bb16 != NULL)
    396 		memcpy(bb16, bb, sizeof(*bb));
    397 	if ((params->flags & IB_VERBOSE) && lastsum != bb->bb_cksum)
    398 		printf("alpha checksum now %016llx\n", (long long)bb->bb_cksum);
    399 	lastsum = bb->bb_cksum;
    400 }
    401 
    402 static void
    403 sun_bootstrap(ib_params *params, struct alpha_boot_block * const bb)
    404 {
    405 #	define BB_ADJUST_OFFSET 64
    406 	static char our_int16s[] = "\2\3\6\7\12";
    407 	u_int16_t i, j, chkdelta, sunsum, bb16[256];
    408 
    409 	/*
    410 	 * Theory: the alpha checksum is adjusted so bits 47:32 add up
    411 	 * to the Sun magic number. Then, another adjustment is computed
    412 	 * so bits 63:48 add up to the Sun checksum, and applied in pieces
    413 	 * so it changes the alpha checksum but not the Sun value.
    414 	 *
    415   	 * Note: using memcpy(3) instead of a union as a strict c89/c9x
    416   	 * conformance experiment and to avoid a public interface delta.
    417 	 */
    418 	assert(sizeof(bb16) == sizeof(*bb));
    419 	memcpy(bb16, bb, sizeof(bb16));
    420 	for (i = 0; our_int16s[i]; ++i) {
    421 		j = BB_ADJUST_OFFSET + our_int16s[i];
    422 		if (bb16[j]) {
    423 			warnx("Non-zero bits %04x in bytes %d..%d",
    424 			    bb16[j], j * 2, j * 2 + 1);
    425 			bb16[j] = 0;
    426 			resum(params, bb, bb16);
    427 		}
    428 	}
    429 	/*
    430 	 * Make alpha checksum <47:32> come out to the sun magic.
    431 	 */
    432 	bb16[BB_ADJUST_OFFSET + 2] = htons(SUN_DKMAGIC) - bb16[254];
    433 	resum(params, bb, bb16);
    434 	sunsum = compute_sunsum(bb16);		/* might be the final value */
    435 	if (params->flags & IB_VERBOSE)
    436 		printf("target sun checksum is %04x\n", sunsum);
    437 	/*
    438 	 * Arrange to have alpha 63:48 add up to the sparc checksum.
    439 	 */
    440 	chkdelta = sunsum - bb16[255];
    441 	bb16[BB_ADJUST_OFFSET + 3] = chkdelta >> 1;
    442 	bb16[BB_ADJUST_OFFSET + 7] = chkdelta >> 1;
    443 	/*
    444 	 * By placing half the correction in two different u_int64_t words at
    445 	 * positions 63:48, the sparc sum will not change but the alpha sum
    446 	 * will have the full correction, but only if the target adjustment
    447 	 * was even. If it was odd, reverse propagate the carry one place.
    448 	 */
    449 	if (chkdelta & 1) {
    450 		if (params->flags & IB_VERBOSE)
    451 			printf("target adjustment %04x was odd, correcting\n",
    452 			    chkdelta);
    453 		assert(bb16[BB_ADJUST_OFFSET + 6] == 0);
    454 		assert(bb16[BB_ADJUST_OFFSET + 012] == 0);
    455 		bb16[BB_ADJUST_OFFSET + 6] += 0x8000;
    456 		bb16[BB_ADJUST_OFFSET + 012] += 0x8000;
    457 	}
    458 	resum(params, bb, bb16);
    459 	if (params->flags & IB_VERBOSE)
    460 		printf("final harmonized checksum: %016llx\n",
    461 		    (long long)bb->bb_cksum);
    462 	check_sparc(bb, "Final");
    463 }
    464 
    465 static void
    466 check_sparc(const struct alpha_boot_block * const bb, const char *when)
    467 {
    468 	u_int16_t bb16[256];
    469 	const char * const wmsg =
    470 	    "%s sparc %s 0x%04x invalid, expected 0x%04x";
    471 
    472 	memcpy(bb16, bb, sizeof(bb16));
    473 	if (compute_sunsum(bb16) != bb16[255])
    474 		warnx(wmsg, when, "checksum", bb16[255], compute_sunsum(bb16));
    475 	if (bb16[254] != htons(SUN_DKMAGIC))
    476 		warnx(wmsg, when, "magic number", bb16[254],
    477 		    htons(SUN_DKMAGIC));
    478 }
    479