/*-
* Copyright (c) 2002 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Luke Mewburn of Wasabi Systems.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Copyright (c) 1999 Ross Harvey. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Ross Harvey
* for the NetBSD Project.
* 4. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Copyright (c) 1999 Christopher G. Demetriou. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Christopher G. Demetriou
* for the NetBSD Project.
* 4. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Allocate a buffer, with space to round up the input file
* to the next block size boundary, and with space for the boot
* block.
*/
bootstrapsize = roundup(params->s1stat.st_size,
ALPHA_BOOT_BLOCK_BLOCKSIZE);
done:
if (bootstrapbuf)
free(bootstrapbuf);
return (retval);
}
/*
* The Sun and alpha checksums overlay, and the Sun magic number also
* overlays the alpha checksum. If you think you are smart: stop here
* and do exercise one: figure out how to salt unimportant uint16_t
* words in mid-sector so that the alpha and sparc checksums match,
* and so the Sun magic number is embedded in the alpha checksum.
*
* The last uint64_t in the sector is the alpha arithmetic checksum.
* The last uint16_t in the sector is the sun xor checksum.
* The penultimate uint16_t in the sector is the sun magic number.
*
* A: 511 510 509 508 507 506 505 504
* S: 510 511 508 509 506 507 504 505
* 63 : : : 32:31 : : : 0
* | : : : \:| : : : |
* 7654321076543210765432107654321076543210765432107654321076543210
* |-- sparc --||-- sparc --|
* |-- checksum --||-- magic --|
* |----------------------- alpha checksum -----------------------|
* 1011111011011010
* b e d a
*/
/*
* Theory: the alpha checksum is adjusted so bits 47:32 add up
* to the Sun magic number. Then, another adjustment is computed
* so bits 63:48 add up to the Sun checksum, and applied in pieces
* so it changes the alpha checksum but not the Sun value.
*
* Note: using memcpy(3) instead of a union as a strict c89/c9x
* conformance experiment and to avoid a public interface delta.
*/
assert(sizeof(bb16) == sizeof(*bb));
memcpy(bb16, bb, sizeof(bb16));
for (i = 0; our_int16s[i]; ++i) {
j = BB_ADJUST_OFFSET + our_int16s[i];
if (bb16[j]) {
warnx("Non-zero bits %04x in bytes %d..%d",
bb16[j], j * 2, j * 2 + 1);
bb16[j] = 0;
resum(params, bb, bb16);
}
}
/*
* Make alpha checksum <47:32> come out to the sun magic.
*/
bb16[BB_ADJUST_OFFSET + 2] = htobe16(SUN_DKMAGIC) - bb16[254];
resum(params, bb, bb16);
sunsum = compute_sunsum(bb16); /* might be the final value */
if (params->flags & IB_VERBOSE)
printf("target sun checksum is %04x\n", sunsum);
/*
* Arrange to have alpha 63:48 add up to the sparc checksum.
*/
chkdelta = sunsum - bb16[255];
bb16[BB_ADJUST_OFFSET + 3] = chkdelta >> 1;
bb16[BB_ADJUST_OFFSET + 7] = chkdelta >> 1;
/*
* By placing half the correction in two different uint64_t words at
* positions 63:48, the sparc sum will not change but the alpha sum
* will have the full correction, but only if the target adjustment
* was even. If it was odd, reverse propagate the carry one place.
*/
if (chkdelta & 1) {
if (params->flags & IB_VERBOSE)
printf("target adjustment %04x was odd, correcting\n",
chkdelta);
assert(bb16[BB_ADJUST_OFFSET + 6] == 0);
assert(bb16[BB_ADJUST_OFFSET + 012] == 0);
bb16[BB_ADJUST_OFFSET + 6] += 0x8000;
bb16[BB_ADJUST_OFFSET + 012] += 0x8000;
}
resum(params, bb, bb16);
if (params->flags & IB_VERBOSE)
printf("final harmonized checksum: %016llx\n",
(unsigned long long)le64toh(bb->bb_cksum));
check_sparc(bb, "Final");
}