xref: /netbsd-src/sys/lib/libkern/dkcksum.c (revision 7de9d97fa44ba024876248cc6b39a5a7c7958194)
1*7de9d97fSmrg /*	$NetBSD: dkcksum.c,v 1.1 2021/05/17 08:50:36 mrg Exp $	*/
2*7de9d97fSmrg 
3*7de9d97fSmrg /*-
4*7de9d97fSmrg  * Copyright (c) 1991, 1993
5*7de9d97fSmrg  *	The Regents of the University of California.  All rights reserved.
6*7de9d97fSmrg  *
7*7de9d97fSmrg  * Redistribution and use in source and binary forms, with or without
8*7de9d97fSmrg  * modification, are permitted provided that the following conditions
9*7de9d97fSmrg  * are met:
10*7de9d97fSmrg  * 1. Redistributions of source code must retain the above copyright
11*7de9d97fSmrg  *    notice, this list of conditions and the following disclaimer.
12*7de9d97fSmrg  * 2. Redistributions in binary form must reproduce the above copyright
13*7de9d97fSmrg  *    notice, this list of conditions and the following disclaimer in the
14*7de9d97fSmrg  *    documentation and/or other materials provided with the distribution.
15*7de9d97fSmrg  * 3. Neither the name of the University nor the names of its contributors
16*7de9d97fSmrg  *    may be used to endorse or promote products derived from this software
17*7de9d97fSmrg  *    without specific prior written permission.
18*7de9d97fSmrg  *
19*7de9d97fSmrg  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20*7de9d97fSmrg  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21*7de9d97fSmrg  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22*7de9d97fSmrg  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23*7de9d97fSmrg  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24*7de9d97fSmrg  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25*7de9d97fSmrg  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26*7de9d97fSmrg  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27*7de9d97fSmrg  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28*7de9d97fSmrg  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29*7de9d97fSmrg  * SUCH DAMAGE.
30*7de9d97fSmrg  *
31*7de9d97fSmrg  * from: NetBSD: dkcksum.c,v 1.14 2013/05/03 16:05:12 matt Exp
32*7de9d97fSmrg  */
33*7de9d97fSmrg 
34*7de9d97fSmrg #include <sys/cdefs.h>
35*7de9d97fSmrg #ifndef lint
36*7de9d97fSmrg #if 0
37*7de9d97fSmrg static char sccsid[] = "@(#)dkcksum.c	8.1 (Berkeley) 6/5/93";
38*7de9d97fSmrg #else
39*7de9d97fSmrg __RCSID("$NetBSD: dkcksum.c,v 1.1 2021/05/17 08:50:36 mrg Exp $");
40*7de9d97fSmrg #endif
41*7de9d97fSmrg #endif /* not lint */
42*7de9d97fSmrg 
43*7de9d97fSmrg #include <sys/types.h>
44*7de9d97fSmrg #include <sys/disklabel.h>
45*7de9d97fSmrg #include <lib/libkern/libkern.h>
46*7de9d97fSmrg 
47*7de9d97fSmrg uint16_t
dkcksum(const struct disklabel * lp)48*7de9d97fSmrg dkcksum(const struct disklabel *lp)
49*7de9d97fSmrg {
50*7de9d97fSmrg 
51*7de9d97fSmrg 	return dkcksum_sized(lp, lp->d_npartitions);
52*7de9d97fSmrg }
53*7de9d97fSmrg 
54*7de9d97fSmrg uint16_t
dkcksum_sized(const struct disklabel * lp,size_t npartitions)55*7de9d97fSmrg dkcksum_sized(const struct disklabel *lp, size_t npartitions)
56*7de9d97fSmrg {
57*7de9d97fSmrg 	const uint16_t *start, *end;
58*7de9d97fSmrg 	uint16_t sum;
59*7de9d97fSmrg 
60*7de9d97fSmrg 	sum = 0;
61*7de9d97fSmrg 	start = (const uint16_t *)lp;
62*7de9d97fSmrg 	end = (const uint16_t *)&lp->d_partitions[npartitions];
63*7de9d97fSmrg 	while (start < end) {
64*7de9d97fSmrg 		sum ^= *start++;
65*7de9d97fSmrg 	}
66*7de9d97fSmrg 	return sum;
67*7de9d97fSmrg }
68