xref: /netbsd-src/usr.sbin/installboot/sum.c (revision e5f39b5e902b26e459bbc8b4d080b39f031e2fa9)
1 /*	$NetBSD: sum.c,v 1.5 2010/01/14 16:27:49 tsutsui 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  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * Copyright (c) 1999 Ross Harvey.  All rights reserved.
34  *
35  * Redistribution and use in source and binary forms, with or without
36  * modification, are permitted provided that the following conditions
37  * are met:
38  * 1. Redistributions of source code must retain the above copyright
39  *    notice, this list of conditions and the following disclaimer.
40  * 2. Redistributions in binary form must reproduce the above copyright
41  *    notice, this list of conditions and the following disclaimer in the
42  *    documentation and/or other materials provided with the distribution.
43  * 3. All advertising materials mentioning features or use of this software
44  *    must display the following acknowledgement:
45  *      This product includes software developed by Ross Harvey
46  *	for the NetBSD Project.
47  * 4. The name of the author may not be used to endorse or promote products
48  *    derived from this software without specific prior written permission
49  *
50  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
51  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
52  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
53  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
54  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
55  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
56  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
57  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
58  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
59  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
60  */
61 
62 #if HAVE_NBTOOL_CONFIG_H
63 #include "nbtool_config.h"
64 #endif
65 
66 #include <sys/cdefs.h>
67 #if !defined(__lint)
68 __RCSID("$NetBSD: sum.c,v 1.5 2010/01/14 16:27:49 tsutsui Exp $");
69 #endif	/* !__lint */
70 
71 #include <sys/types.h>
72 
73 #include <assert.h>
74 #include <stdio.h>
75 
76 #include "installboot.h"
77 
78 
79 uint16_t
compute_sunsum(const uint16_t * bb16)80 compute_sunsum(const uint16_t *bb16)
81 {
82 	uint16_t	i, s;
83 
84 	assert(bb16 != NULL);
85 
86 	s = 0;
87 	for (i = 0; i < 255; ++i)
88 		s ^= bb16[i];
89 	return (s);
90 }
91 
92 int
set_sunsum(ib_params * params,uint16_t * bb16,uint16_t sum)93 set_sunsum(ib_params *params, uint16_t *bb16, uint16_t sum)
94 {
95 
96 	assert(params != NULL);
97 	assert(bb16 != NULL);
98 
99 #define	SUNSUM_OFFSET	255
100 	if (params->flags & IB_VERBOSE) {
101 		printf("Old Sun checksum:          0x%04x\n",
102 		    be16toh(bb16[SUNSUM_OFFSET]));
103 		printf("Recalculated Sun checksum: 0x%04x\n", be16toh(sum));
104 	}
105 		// XXX: does this need to be big endian?
106 	bb16[SUNSUM_OFFSET] = sum;
107 	return (1);
108 }
109