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