New option: -s

.
Makes the 64-bit alpha checksum magically contain an embedded 16-bit
Sun (NetBSD/sparc) compatible magic number and checksum. This is the
last piece in the "boot one disk from alpha, sun, and i386" problem.
.
And for those who told me this had been done before, :-P.  It turns out,
it was only done in the collective sense by multi-disk distributions.
This commit is contained in:
ross 1999-09-19 03:19:00 +00:00
parent 33630db000
commit 9ff31ad0f5
1 changed files with 147 additions and 3 deletions

View File

@ -1,4 +1,34 @@
/* $NetBSD: installboot.c,v 1.16 1999/06/14 23:55:29 cgd Exp $ */
/* $NetBSD: installboot.c,v 1.17 1999/09/19 03:19:00 ross Exp $ */
/*
* 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.
@ -33,6 +63,7 @@
#include <sys/param.h> /* XXX for roundup, howmany */
#include <sys/stat.h>
#include <sys/disklabel.h>
#include <dev/sun/disklabel.h>
#include <assert.h>
#include <err.h>
#include <fcntl.h>
@ -43,11 +74,14 @@
static void usage(void);
static void clr_bootstrap(const char *disk);
void check_sparc(const struct boot_block * const);
static u_int16_t compute_sparc(const u_int16_t *);
static void sun_bootstrap(struct boot_block * const);
static void set_bootstrap(const char *disk, const char *bootstrap);
extern char *__progname;
int verbose, nowrite;
int sunflag, verbose, nowrite;
static void
usage()
@ -66,16 +100,24 @@ main(int argc, char **argv)
clearflag = verbose = nowrite = 0;
while ((c = getopt(argc, argv, "cnv")) != -1) {
while ((c = getopt(argc, argv, "cnsv")) != -1) {
switch (c) {
case 'c':
/* Clear any existing boot block */
if (sunflag)
goto argdeath;
clearflag = 1;
break;
case 'n':
/* Do not actually write the boot file */
nowrite = 1;
break;
case 's':
/* Magically arrange for the Sun checksum overlay */
if (clearflag)
goto argdeath;
sunflag = 1;
break;
case 'v':
/* Chat */
verbose = 1;
@ -109,6 +151,8 @@ main(int argc, char **argv)
set_bootstrap(disk, bootstrap);
exit(EXIT_SUCCESS);
argdeath:
errx(EXIT_FAILURE, "incompatible options");
}
static void
@ -221,6 +265,9 @@ set_bootstrap(const char *disk, const char *bootstrap)
else if (rv != sizeof bb)
errx(EXIT_FAILURE, "read %s: short read", disk);
if (sunflag)
check_sparc(&bb);
/* fill in the updated boot block fields, and checksum boot block */
bb.bb_secsize = howmany(bootstrapsb.st_size, BOOT_BLOCK_BLOCKSIZE);
bb.bb_secstart = 1;
@ -236,6 +283,9 @@ set_bootstrap(const char *disk, const char *bootstrap)
(unsigned long long)bb.bb_cksum);
}
if (sunflag)
sun_bootstrap(&bb);
if (nowrite) {
if (verbose)
fprintf(stderr, "not writing\n");
@ -264,3 +314,97 @@ set_bootstrap(const char *disk, const char *bootstrap)
done:
(void)close(diskfd);
}
/*
* The Sun and alpha checksums overlay, and the Sun magic number also
* overlays the alpha checksum. Fortunately, there are a set of identity
* transformations for the Sun checksum that can fit within bits of the
* alpha checksum that are not overlayed. Add to the xor and algebra
* magic a bit of carry-blocking magic, and everything Just Works.
*
* 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
*/
static void
sun_bootstrap(struct boot_block * const bb)
{
# define BB_ADJUST_OFFSET 64
u_int16_t sunsum, old_3, bb16[256];
/*
* 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. This changes the Sun
* checksum, however, so compensating entries are made in bits
* 15:0 to restore the original Sun value. This changes the alpha
* value, but only in bits that don't overlay.
*
* A new carry (or lost carry) out of bit 31 would break things,
* so prearrange for the alpha 31:16 bits to be 01010101...
*
* 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);
bb16[BB_ADJUST_OFFSET + 1] = 0x5555 - bb16[253]; /* carry trap */
memcpy(bb, bb16, sizeof bb16);
CHECKSUM_BOOT_BLOCK(bb, &bb->bb_cksum);
memcpy(bb16, bb, sizeof bb16);
/*
* Make alpha checksum <47:32> come out to the sun magic.
*/
bb16[BB_ADJUST_OFFSET + 2] = htons(SUN_DKMAGIC) - bb16[254];
memcpy(bb, bb16, sizeof bb16);
CHECKSUM_BOOT_BLOCK(bb, &bb->bb_cksum);
memcpy(bb16, bb, sizeof bb16);
sunsum = compute_sparc(bb16); /* this is the final value */
/*
* Arrange to have alpha 63:48 add up to the sparc checksum.
*/
old_3 = bb16[BB_ADJUST_OFFSET + 3];
bb16[BB_ADJUST_OFFSET + 3] = sunsum - bb16[255];
/*
* The sparc checksum has changed again to a third value. Modify
* the alpha 15:0 bits to restore the previous sparc checksum
* that we went to all the trouble to generate.
*/
bb16[BB_ADJUST_OFFSET + 0] ^= (old_3 ^ bb16[BB_ADJUST_OFFSET + 3]);
memcpy(bb, bb16, sizeof bb16);
CHECKSUM_BOOT_BLOCK(bb, &bb->bb_cksum);
if (verbose)
printf("final harmonized checksum: %016lx\n", bb->bb_cksum);
}
void
check_sparc(const struct boot_block * const bb)
{
u_int16_t bb16[256];
const char * const wmsg = "warning, initial sparc %s 0x%04x invalid,"
" expected 0x%04x";
memcpy(bb16, bb, sizeof bb16);
if (compute_sparc(bb16) != bb16[255])
warnx(wmsg, "checksum", bb16[255], compute_sparc(bb16));
if (bb16[254] != htons(SUN_DKMAGIC))
warnx(wmsg, "magic number", bb16[254], htons(SUN_DKMAGIC));
}
static u_int16_t
compute_sparc(const u_int16_t *bb16)
{
u_int16_t i, s = 0;
for (i = 0; i < 255; ++i)
s ^= bb16[i];
return s;
}