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