hamming.c revision 1.1.10.2 1 1.1.10.2 matt /* $NetBSD: hamming.c,v 1.1.10.2 2011/07/26 03:22:27 matt Exp $ */
2 1.1.10.2 matt
3 1.1.10.2 matt /*
4 1.1.10.2 matt * Copyright (c) 2008, Atmel Corporation
5 1.1.10.2 matt *
6 1.1.10.2 matt * All rights reserved.
7 1.1.10.2 matt *
8 1.1.10.2 matt * Redistribution and use in source and binary forms, with or without
9 1.1.10.2 matt * modification, are permitted provided that the following conditions are met:
10 1.1.10.2 matt *
11 1.1.10.2 matt * - Redistributions of source code must retain the above copyright notice,
12 1.1.10.2 matt * this list of conditions and the disclaimer below.
13 1.1.10.2 matt *
14 1.1.10.2 matt * Atmel's name may not be used to endorse or promote products derived from
15 1.1.10.2 matt * this software without specific prior written permission.
16 1.1.10.2 matt *
17 1.1.10.2 matt * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
18 1.1.10.2 matt * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 1.1.10.2 matt * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
20 1.1.10.2 matt * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
21 1.1.10.2 matt * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 1.1.10.2 matt * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
23 1.1.10.2 matt * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24 1.1.10.2 matt * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25 1.1.10.2 matt * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
26 1.1.10.2 matt * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 1.1.10.2 matt */
28 1.1.10.2 matt
29 1.1.10.2 matt #include <sys/cdefs.h>
30 1.1.10.2 matt __KERNEL_RCSID(0, "$NetBSD: hamming.c,v 1.1.10.2 2011/07/26 03:22:27 matt Exp $");
31 1.1.10.2 matt
32 1.1.10.2 matt #include <sys/param.h>
33 1.1.10.2 matt #include <lib/libkern/libkern.h>
34 1.1.10.2 matt #include "hamming.h"
35 1.1.10.2 matt
36 1.1.10.2 matt /**
37 1.1.10.2 matt * Calculates the 22-bit hamming code for a 256-bytes block of data.
38 1.1.10.2 matt * \param data Data buffer to calculate code for.
39 1.1.10.2 matt * \param code Pointer to a buffer where the code should be stored.
40 1.1.10.2 matt */
41 1.1.10.2 matt void
42 1.1.10.2 matt hamming_compute_256(const uint8_t *data, uint8_t *code)
43 1.1.10.2 matt {
44 1.1.10.2 matt unsigned int i;
45 1.1.10.2 matt uint8_t column_sum = 0;
46 1.1.10.2 matt uint8_t even_line_code = 0;
47 1.1.10.2 matt uint8_t odd_line_code = 0;
48 1.1.10.2 matt uint8_t even_column_code = 0;
49 1.1.10.2 matt uint8_t odd_column_code = 0;
50 1.1.10.2 matt
51 1.1.10.2 matt /*-
52 1.1.10.2 matt * Xor all bytes together to get the column sum;
53 1.1.10.2 matt * At the same time, calculate the even and odd line codes
54 1.1.10.2 matt */
55 1.1.10.2 matt for (i = 0; i < 256; i++) {
56 1.1.10.2 matt column_sum ^= data[i];
57 1.1.10.2 matt
58 1.1.10.2 matt /*-
59 1.1.10.2 matt * If the xor sum of the byte is 0, then this byte has no
60 1.1.10.2 matt * incidence on the computed code; so check if the sum is 1.
61 1.1.10.2 matt */
62 1.1.10.2 matt if ((popcount(data[i]) & 1) == 1) {
63 1.1.10.2 matt /*-
64 1.1.10.2 matt * Parity groups are formed by forcing a particular
65 1.1.10.2 matt * index bit to 0 (even) or 1 (odd).
66 1.1.10.2 matt * Example on one byte:
67 1.1.10.2 matt *
68 1.1.10.2 matt * bits (dec) 7 6 5 4 3 2 1 0
69 1.1.10.2 matt * (bin) 111 110 101 100 011 010 001 000
70 1.1.10.2 matt * '---'---'---'----------.
71 1.1.10.2 matt * |
72 1.1.10.2 matt * groups P4' ooooooooooooooo eeeeeeeeeeeeeee P4 |
73 1.1.10.2 matt * P2' ooooooo eeeeeee ooooooo eeeeeee P2 |
74 1.1.10.2 matt * P1' ooo eee ooo eee ooo eee ooo eee P1 |
75 1.1.10.2 matt * |
76 1.1.10.2 matt * We can see that: |
77 1.1.10.2 matt * - P4 -> bit 2 of index is 0 --------------------'
78 1.1.10.2 matt * - P4' -> bit 2 of index is 1.
79 1.1.10.2 matt * - P2 -> bit 1 of index if 0.
80 1.1.10.2 matt * - etc...
81 1.1.10.2 matt * We deduce that a bit position has an impact on all
82 1.1.10.2 matt * even Px if the log2(x)nth bit of its index is 0
83 1.1.10.2 matt * ex: log2(4) = 2,
84 1.1.10.2 matt * bit2 of the index must be 0 (-> 0 1 2 3)
85 1.1.10.2 matt * and on all odd Px' if the log2(x)nth bit
86 1.1.10.2 matt * of its index is 1
87 1.1.10.2 matt * ex: log2(2) = 1,
88 1.1.10.2 matt * bit1 of the index must be 1 (-> 0 1 4 5)
89 1.1.10.2 matt *
90 1.1.10.2 matt * As such, we calculate all the possible Px and Px'
91 1.1.10.2 matt * values at the same time in two variables,
92 1.1.10.2 matt * even_line_code and odd_line_code, such as
93 1.1.10.2 matt * even_line_code bits: P128 P64 P32
94 1.1.10.2 matt * P16 P8 P4 P2 P1
95 1.1.10.2 matt * odd_line_code bits: P128' P64' P32' P16'
96 1.1.10.2 matt * P8' P4' P2' P1'
97 1.1.10.2 matt */
98 1.1.10.2 matt even_line_code ^= (255 - i);
99 1.1.10.2 matt odd_line_code ^= i;
100 1.1.10.2 matt }
101 1.1.10.2 matt }
102 1.1.10.2 matt
103 1.1.10.2 matt /*-
104 1.1.10.2 matt * At this point, we have the line parities, and the column sum.
105 1.1.10.2 matt * First, We must caculate the parity group values on the column sum.
106 1.1.10.2 matt */
107 1.1.10.2 matt for (i = 0; i < 8; i++) {
108 1.1.10.2 matt if (column_sum & 1) {
109 1.1.10.2 matt even_column_code ^= (7 - i);
110 1.1.10.2 matt odd_column_code ^= i;
111 1.1.10.2 matt }
112 1.1.10.2 matt column_sum >>= 1;
113 1.1.10.2 matt }
114 1.1.10.2 matt
115 1.1.10.2 matt /*-
116 1.1.10.2 matt * Now, we must interleave the parity values,
117 1.1.10.2 matt * to obtain the following layout:
118 1.1.10.2 matt * Code[0] = Line1
119 1.1.10.2 matt * Code[1] = Line2
120 1.1.10.2 matt * Code[2] = Column
121 1.1.10.2 matt * Line = Px' Px P(x-1)- P(x-1) ...
122 1.1.10.2 matt * Column = P4' P4 P2' P2 P1' P1 PadBit PadBit
123 1.1.10.2 matt */
124 1.1.10.2 matt code[0] = 0;
125 1.1.10.2 matt code[1] = 0;
126 1.1.10.2 matt code[2] = 0;
127 1.1.10.2 matt
128 1.1.10.2 matt for (i = 0; i < 4; i++) {
129 1.1.10.2 matt code[0] <<= 2;
130 1.1.10.2 matt code[1] <<= 2;
131 1.1.10.2 matt code[2] <<= 2;
132 1.1.10.2 matt
133 1.1.10.2 matt /* Line 1 */
134 1.1.10.2 matt if ((odd_line_code & 0x80) != 0) {
135 1.1.10.2 matt
136 1.1.10.2 matt code[0] |= 2;
137 1.1.10.2 matt }
138 1.1.10.2 matt if ((even_line_code & 0x80) != 0) {
139 1.1.10.2 matt
140 1.1.10.2 matt code[0] |= 1;
141 1.1.10.2 matt }
142 1.1.10.2 matt
143 1.1.10.2 matt /* Line 2 */
144 1.1.10.2 matt if ((odd_line_code & 0x08) != 0) {
145 1.1.10.2 matt
146 1.1.10.2 matt code[1] |= 2;
147 1.1.10.2 matt }
148 1.1.10.2 matt if ((even_line_code & 0x08) != 0) {
149 1.1.10.2 matt
150 1.1.10.2 matt code[1] |= 1;
151 1.1.10.2 matt }
152 1.1.10.2 matt
153 1.1.10.2 matt /* Column */
154 1.1.10.2 matt if ((odd_column_code & 0x04) != 0) {
155 1.1.10.2 matt
156 1.1.10.2 matt code[2] |= 2;
157 1.1.10.2 matt }
158 1.1.10.2 matt if ((even_column_code & 0x04) != 0) {
159 1.1.10.2 matt
160 1.1.10.2 matt code[2] |= 1;
161 1.1.10.2 matt }
162 1.1.10.2 matt
163 1.1.10.2 matt odd_line_code <<= 1;
164 1.1.10.2 matt even_line_code <<= 1;
165 1.1.10.2 matt odd_column_code <<= 1;
166 1.1.10.2 matt even_column_code <<= 1;
167 1.1.10.2 matt }
168 1.1.10.2 matt
169 1.1.10.2 matt /* Invert codes (linux compatibility) */
170 1.1.10.2 matt code[0] = ~code[0];
171 1.1.10.2 matt code[1] = ~code[1];
172 1.1.10.2 matt code[2] = ~code[2];
173 1.1.10.2 matt }
174 1.1.10.2 matt
175 1.1.10.2 matt /**
176 1.1.10.2 matt * Verifies and corrects a 256-bytes block of data using the given 22-bits
177 1.1.10.2 matt * hamming code.
178 1.1.10.2 matt * Returns 0 if there is no error, otherwise returns a HAMMING_ERROR code.
179 1.1.10.2 matt * param data Data buffer to check.
180 1.1.10.2 matt * \param original_code Hamming code to use for verifying the data.
181 1.1.10.2 matt */
182 1.1.10.2 matt uint8_t
183 1.1.10.2 matt hamming_correct_256(uint8_t *data, const uint8_t *original_code,
184 1.1.10.2 matt const uint8_t *computed_code)
185 1.1.10.2 matt {
186 1.1.10.2 matt /* Calculate new code */
187 1.1.10.2 matt /* we allocate 4 bytes so we can use popcount32 in one step */
188 1.1.10.2 matt uint8_t correction_code[4];
189 1.1.10.2 matt
190 1.1.10.2 matt /* this byte should remain zero all the time */
191 1.1.10.2 matt correction_code[3] = 0;
192 1.1.10.2 matt
193 1.1.10.2 matt /* Xor both codes together */
194 1.1.10.2 matt correction_code[0] = computed_code[0] ^ original_code[0];
195 1.1.10.2 matt correction_code[1] = computed_code[1] ^ original_code[1];
196 1.1.10.2 matt correction_code[2] = computed_code[2] ^ original_code[2];
197 1.1.10.2 matt
198 1.1.10.2 matt /* If all bytes are 0, there is no error */
199 1.1.10.2 matt if (*(uint32_t *)correction_code == 0) {
200 1.1.10.2 matt return 0;
201 1.1.10.2 matt }
202 1.1.10.2 matt /* If there is a single bit error, there are 11 bits set to 1 */
203 1.1.10.2 matt if (popcount32(*(uint32_t *)correction_code) == 11) {
204 1.1.10.2 matt /* Get byte and bit indexes */
205 1.1.10.2 matt uint8_t byte = correction_code[0] & 0x80;
206 1.1.10.2 matt byte |= (correction_code[0] << 1) & 0x40;
207 1.1.10.2 matt byte |= (correction_code[0] << 2) & 0x20;
208 1.1.10.2 matt byte |= (correction_code[0] << 3) & 0x10;
209 1.1.10.2 matt
210 1.1.10.2 matt byte |= (correction_code[1] >> 4) & 0x08;
211 1.1.10.2 matt byte |= (correction_code[1] >> 3) & 0x04;
212 1.1.10.2 matt byte |= (correction_code[1] >> 2) & 0x02;
213 1.1.10.2 matt byte |= (correction_code[1] >> 1) & 0x01;
214 1.1.10.2 matt
215 1.1.10.2 matt uint8_t bit = (correction_code[2] >> 5) & 0x04;
216 1.1.10.2 matt bit |= (correction_code[2] >> 4) & 0x02;
217 1.1.10.2 matt bit |= (correction_code[2] >> 3) & 0x01;
218 1.1.10.2 matt
219 1.1.10.2 matt /* Correct bit */
220 1.1.10.2 matt data[byte] ^= (1 << bit);
221 1.1.10.2 matt
222 1.1.10.2 matt return HAMMING_ERROR_SINGLEBIT;
223 1.1.10.2 matt }
224 1.1.10.2 matt /* Check if ECC has been corrupted */
225 1.1.10.2 matt if (popcount32(*(uint32_t *)correction_code) == 1) {
226 1.1.10.2 matt return HAMMING_ERROR_ECC;
227 1.1.10.2 matt } else {
228 1.1.10.2 matt /* Otherwise, this is a multi-bit error */
229 1.1.10.2 matt return HAMMING_ERROR_MULTIPLEBITS;
230 1.1.10.2 matt }
231 1.1.10.2 matt }
232 1.1.10.2 matt
233