1*e5f39b5eStsutsui /* $NetBSD: sum.c,v 1.5 2010/01/14 16:27:49 tsutsui Exp $ */
207e46180Slukem
307e46180Slukem /*-
407e46180Slukem * Copyright (c) 2002 The NetBSD Foundation, Inc.
507e46180Slukem * All rights reserved.
607e46180Slukem *
707e46180Slukem * This code is derived from software contributed to The NetBSD Foundation
807e46180Slukem * by Luke Mewburn of Wasabi Systems.
907e46180Slukem *
1007e46180Slukem * Redistribution and use in source and binary forms, with or without
1107e46180Slukem * modification, are permitted provided that the following conditions
1207e46180Slukem * are met:
1307e46180Slukem * 1. Redistributions of source code must retain the above copyright
1407e46180Slukem * notice, this list of conditions and the following disclaimer.
1507e46180Slukem * 2. Redistributions in binary form must reproduce the above copyright
1607e46180Slukem * notice, this list of conditions and the following disclaimer in the
1707e46180Slukem * documentation and/or other materials provided with the distribution.
1807e46180Slukem *
1907e46180Slukem * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2007e46180Slukem * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2107e46180Slukem * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2207e46180Slukem * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2307e46180Slukem * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2407e46180Slukem * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2507e46180Slukem * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2607e46180Slukem * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2707e46180Slukem * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2807e46180Slukem * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2907e46180Slukem * POSSIBILITY OF SUCH DAMAGE.
3007e46180Slukem */
3107e46180Slukem
3207e46180Slukem /*
3307e46180Slukem * Copyright (c) 1999 Ross Harvey. All rights reserved.
3407e46180Slukem *
3507e46180Slukem * Redistribution and use in source and binary forms, with or without
3607e46180Slukem * modification, are permitted provided that the following conditions
3707e46180Slukem * are met:
3807e46180Slukem * 1. Redistributions of source code must retain the above copyright
3907e46180Slukem * notice, this list of conditions and the following disclaimer.
4007e46180Slukem * 2. Redistributions in binary form must reproduce the above copyright
4107e46180Slukem * notice, this list of conditions and the following disclaimer in the
4207e46180Slukem * documentation and/or other materials provided with the distribution.
4307e46180Slukem * 3. All advertising materials mentioning features or use of this software
4407e46180Slukem * must display the following acknowledgement:
4507e46180Slukem * This product includes software developed by Ross Harvey
4607e46180Slukem * for the NetBSD Project.
4707e46180Slukem * 4. The name of the author may not be used to endorse or promote products
4807e46180Slukem * derived from this software without specific prior written permission
4907e46180Slukem *
5007e46180Slukem * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
5107e46180Slukem * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
5207e46180Slukem * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
5307e46180Slukem * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
5407e46180Slukem * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
5507e46180Slukem * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
5607e46180Slukem * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
5707e46180Slukem * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
5807e46180Slukem * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
5907e46180Slukem * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
6007e46180Slukem */
6107e46180Slukem
62b2f78261Sjmc #if HAVE_NBTOOL_CONFIG_H
63b2f78261Sjmc #include "nbtool_config.h"
64b2f78261Sjmc #endif
65b2f78261Sjmc
6607e46180Slukem #include <sys/cdefs.h>
67*e5f39b5eStsutsui #if !defined(__lint)
68*e5f39b5eStsutsui __RCSID("$NetBSD: sum.c,v 1.5 2010/01/14 16:27:49 tsutsui Exp $");
6907e46180Slukem #endif /* !__lint */
7007e46180Slukem
7107e46180Slukem #include <sys/types.h>
7207e46180Slukem
7307e46180Slukem #include <assert.h>
7407e46180Slukem #include <stdio.h>
7507e46180Slukem
7607e46180Slukem #include "installboot.h"
7707e46180Slukem
7807e46180Slukem
798eb8919eSlukem uint16_t
compute_sunsum(const uint16_t * bb16)808eb8919eSlukem compute_sunsum(const uint16_t *bb16)
8107e46180Slukem {
828eb8919eSlukem uint16_t i, s;
8307e46180Slukem
8407e46180Slukem assert(bb16 != NULL);
8507e46180Slukem
8607e46180Slukem s = 0;
8707e46180Slukem for (i = 0; i < 255; ++i)
8807e46180Slukem s ^= bb16[i];
8907e46180Slukem return (s);
9007e46180Slukem }
9107e46180Slukem
9207e46180Slukem int
set_sunsum(ib_params * params,uint16_t * bb16,uint16_t sum)938eb8919eSlukem set_sunsum(ib_params *params, uint16_t *bb16, uint16_t sum)
9407e46180Slukem {
9507e46180Slukem
9607e46180Slukem assert(params != NULL);
9707e46180Slukem assert(bb16 != NULL);
9807e46180Slukem
9907e46180Slukem #define SUNSUM_OFFSET 255
10007e46180Slukem if (params->flags & IB_VERBOSE) {
10107e46180Slukem printf("Old Sun checksum: 0x%04x\n",
10207e46180Slukem be16toh(bb16[SUNSUM_OFFSET]));
10307e46180Slukem printf("Recalculated Sun checksum: 0x%04x\n", be16toh(sum));
10407e46180Slukem }
10507e46180Slukem // XXX: does this need to be big endian?
10607e46180Slukem bb16[SUNSUM_OFFSET] = sum;
10707e46180Slukem return (1);
10807e46180Slukem }
109