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