2013-11-18 16:43:44 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/mtd/hamming.c
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013 Gregory Nutt. All rights reserved.
|
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
|
|
|
*
|
|
|
|
* This logic was taken directly from Atmel sample code with only
|
|
|
|
* modifications for better integration with NuttX. The Atmel sample
|
2013-12-21 16:45:27 +01:00
|
|
|
* code has a BSD compatible license that requires this copyright notice:
|
2013-11-18 16:43:44 +01:00
|
|
|
*
|
|
|
|
* Copyright (c) 2011, Atmel Corporation
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* 3. Neither the names NuttX nor Atmel nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
#include <nuttx/mtd/nand_config.h>
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <nuttx/mtd/hamming.h>
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2013-12-01 14:39:20 +01:00
|
|
|
* Name: hamming_bitsinbyte
|
2013-11-18 16:43:44 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Counts the number of bits set to '1' in the given byte.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* bytes - The byte to use.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2013-11-18 16:43:44 +01:00
|
|
|
* Returns the number of bits set to '1' in the given byte.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2013-12-01 14:39:20 +01:00
|
|
|
static unsigned int hamming_bitsinbyte(uint8_t byte)
|
2013-11-18 16:43:44 +01:00
|
|
|
{
|
|
|
|
unsigned int count = 0;
|
|
|
|
|
|
|
|
while (byte != 0)
|
|
|
|
{
|
|
|
|
if ((byte & 1) != 0)
|
|
|
|
{
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
byte >>= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2013-12-01 14:39:20 +01:00
|
|
|
* Name: hamming_bitsincode256
|
2013-11-18 16:43:44 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Counts the number of bits set to '1' in the given hamming code.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* code - Hamming code
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2013-11-18 16:43:44 +01:00
|
|
|
* Returns the number of bits set to '1' in the given hamming code.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2013-12-01 14:39:20 +01:00
|
|
|
static uint8_t hamming_bitsincode256(FAR uint8_t *code)
|
2013-11-18 16:43:44 +01:00
|
|
|
{
|
2013-12-01 14:39:20 +01:00
|
|
|
return hamming_bitsinbyte(code[0]) +
|
|
|
|
hamming_bitsinbyte(code[1]) +
|
|
|
|
hamming_bitsinbyte(code[2]);
|
2013-11-18 16:43:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2013-12-01 14:39:20 +01:00
|
|
|
* Name: hamming_compute256
|
2013-11-18 16:43:44 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Calculates the 22-bit hamming code for a 256-bytes block of data.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* data - Data buffer to calculate code
|
|
|
|
* code - Pointer to a buffer where the code should be stored
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2013-11-18 16:43:44 +01:00
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2013-12-01 14:39:20 +01:00
|
|
|
static void hamming_compute256(FAR const uint8_t *data, FAR uint8_t *code)
|
2013-11-18 16:43:44 +01:00
|
|
|
{
|
|
|
|
uint8_t colsum = 0;
|
|
|
|
uint8_t evenline = 0;
|
|
|
|
uint8_t oddline = 0;
|
|
|
|
uint8_t evencol = 0;
|
|
|
|
uint8_t oddcol = 0;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Xor all bytes together to get the column sum;
|
|
|
|
* At the same time, calculate the even and odd line codes
|
|
|
|
*/
|
|
|
|
|
|
|
|
for (i = 0; i < 256; i++)
|
|
|
|
{
|
|
|
|
colsum ^= data[i];
|
|
|
|
|
|
|
|
/* If the xor sum of the byte is 0, then this byte has no incidence on
|
|
|
|
* the computed code; so check if the sum is 1.
|
|
|
|
*/
|
|
|
|
|
2013-12-01 14:39:20 +01:00
|
|
|
if ((hamming_bitsinbyte(data[i]) & 1) == 1)
|
2013-11-18 16:43:44 +01:00
|
|
|
{
|
|
|
|
/* Parity groups are formed by forcing a particular index bit to 0
|
|
|
|
* (even) or 1 (odd).
|
|
|
|
* Example on one byte:
|
|
|
|
*
|
|
|
|
* bits (dec) 7 6 5 4 3 2 1 0
|
|
|
|
* (bin) 111 110 101 100 011 010 001 000
|
|
|
|
* '---'---'---'----------.
|
|
|
|
* |
|
|
|
|
* groups P4' ooooooooooooooo eeeeeeeeeeeeeee P4 |
|
|
|
|
* P2' ooooooo eeeeeee ooooooo eeeeeee P2 |
|
|
|
|
* P1' ooo eee ooo eee ooo eee ooo eee P1 |
|
|
|
|
* |
|
|
|
|
* We can see that: |
|
|
|
|
* - P4 -> bit 2 of index is 0 --------------------'
|
|
|
|
* - P4' -> bit 2 of index is 1.
|
|
|
|
* - P2 -> bit 1 of index if 0.
|
|
|
|
* - etc...
|
|
|
|
* We deduce that a bit position has an impact on all even Px if
|
|
|
|
* the log2(x)nth bit of its index is 0
|
|
|
|
* ex: log2(4) = 2, bit2 of the index must be 0 (-> 0 1 2 3)
|
|
|
|
* and on all odd Px' if the log2(x)nth bit of its index is 1
|
|
|
|
* ex: log2(2) = 1, bit1 of the index must be 1 (-> 0 1 4 5)
|
|
|
|
*
|
|
|
|
* As such, we calculate all the possible Px and Px' values at the
|
|
|
|
* same time in two variables, evenline and oddline, such as
|
|
|
|
* evenline bits: P128 P64 P32 P16 P8 P4 P2 P1
|
|
|
|
* oddline bits: P128' P64' P32' P16' P8' P4' P2' P1'
|
|
|
|
*/
|
|
|
|
|
|
|
|
evenline ^= (255 - i);
|
|
|
|
oddline ^= i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* At this point, we have the line parities, and the column sum. First, We
|
2020-02-23 09:50:23 +01:00
|
|
|
* must calculate the parity group values on the column sum.
|
2013-11-18 16:43:44 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
for (i = 0; i < 8; i++)
|
|
|
|
{
|
|
|
|
if (colsum & 1)
|
|
|
|
{
|
|
|
|
evencol ^= (7 - i);
|
|
|
|
oddcol ^= i;
|
|
|
|
}
|
|
|
|
|
|
|
|
colsum >>= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now, we must interleave the parity values, to obtain the following layout:
|
|
|
|
* Code[0] = Line1
|
|
|
|
* Code[1] = Line2
|
|
|
|
* Code[2] = Column
|
|
|
|
* Line = Px' Px P(x-1)- P(x-1) ...
|
|
|
|
* Column = P4' P4 P2' P2 P1' P1 PadBit PadBit
|
|
|
|
*/
|
|
|
|
|
|
|
|
code[0] = 0;
|
|
|
|
code[1] = 0;
|
|
|
|
code[2] = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < 4; i++)
|
|
|
|
{
|
|
|
|
code[0] <<= 2;
|
|
|
|
code[1] <<= 2;
|
|
|
|
code[2] <<= 2;
|
|
|
|
|
|
|
|
/* Line 1 */
|
|
|
|
|
|
|
|
if ((oddline & 0x80) != 0)
|
|
|
|
{
|
|
|
|
code[0] |= 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((evenline & 0x80) != 0)
|
|
|
|
{
|
|
|
|
code[0] |= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Line 2 */
|
|
|
|
|
|
|
|
if ((oddline & 0x08) != 0)
|
|
|
|
{
|
|
|
|
code[1] |= 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((evenline & 0x08) != 0)
|
|
|
|
{
|
|
|
|
code[1] |= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Column */
|
|
|
|
|
|
|
|
if ((oddcol & 0x04) != 0)
|
|
|
|
{
|
|
|
|
code[2] |= 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((evencol & 0x04) != 0)
|
|
|
|
{
|
|
|
|
code[2] |= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
oddline <<= 1;
|
|
|
|
evenline <<= 1;
|
|
|
|
oddcol <<= 1;
|
|
|
|
evencol <<= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Invert codes (linux compatibility) */
|
|
|
|
|
|
|
|
code[0] = (~(uint32_t)code[0]);
|
|
|
|
code[1] = (~(uint32_t)code[1]);
|
|
|
|
code[2] = (~(uint32_t)code[2]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2013-12-01 14:39:20 +01:00
|
|
|
* Name: hamming_verify256
|
2013-11-18 16:43:44 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Verifies and corrects a 256-bytes block of data using the given 22-bits
|
|
|
|
* hamming code.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* data - Data buffer to check
|
|
|
|
* original - Hamming code to use for verifying the data
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2013-11-18 16:43:44 +01:00
|
|
|
* Zero on success, otherwise returns a HAMMING_ERROR_ code.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2013-12-01 14:39:20 +01:00
|
|
|
static int hamming_verify256(FAR uint8_t *data, FAR const uint8_t *original)
|
2013-11-18 16:43:44 +01:00
|
|
|
{
|
|
|
|
/* Calculate new code */
|
|
|
|
|
|
|
|
uint8_t computed[3];
|
|
|
|
uint8_t correction[3];
|
|
|
|
|
2013-12-01 14:39:20 +01:00
|
|
|
hamming_compute256(data, computed);
|
2013-11-18 16:43:44 +01:00
|
|
|
|
|
|
|
/* Xor both codes together */
|
|
|
|
|
|
|
|
correction[0] = computed[0] ^ original[0];
|
|
|
|
correction[1] = computed[1] ^ original[1];
|
|
|
|
correction[2] = computed[2] ^ original[2];
|
|
|
|
|
|
|
|
/* If all bytes are 0, there is no error */
|
|
|
|
|
|
|
|
if ((correction[0] == 0) && (correction[1] == 0) && (correction[2] == 0))
|
2013-12-02 04:06:07 +01:00
|
|
|
{
|
2013-11-18 16:43:44 +01:00
|
|
|
return 0;
|
2013-12-02 04:06:07 +01:00
|
|
|
}
|
2013-11-18 16:43:44 +01:00
|
|
|
|
2013-12-03 00:21:09 +01:00
|
|
|
/* There are bit errors */
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Read: %02x %02x %02x\n",
|
2013-12-03 00:21:09 +01:00
|
|
|
original[0], original[1], original[2]);
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Computed: %02x %02x %02x\n",
|
2013-12-03 00:21:09 +01:00
|
|
|
computed[0], computed[1], computed[2]);
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Correction: %02x %02x %02x\n",
|
2013-12-03 00:21:09 +01:00
|
|
|
correction[0], correction[1], correction[2]);
|
|
|
|
|
2013-11-18 16:43:44 +01:00
|
|
|
/* If there is a single bit error, there are 11 bits set to 1 */
|
|
|
|
|
2013-12-01 14:39:20 +01:00
|
|
|
if (hamming_bitsincode256(correction) == 11)
|
2013-11-18 16:43:44 +01:00
|
|
|
{
|
|
|
|
uint8_t byte;
|
|
|
|
uint8_t bit;
|
|
|
|
|
|
|
|
/* Get byte and bit indexes */
|
|
|
|
|
|
|
|
byte = correction[0] & 0x80;
|
|
|
|
byte |= (correction[0] << 1) & 0x40;
|
|
|
|
byte |= (correction[0] << 2) & 0x20;
|
|
|
|
byte |= (correction[0] << 3) & 0x10;
|
|
|
|
|
|
|
|
byte |= (correction[1] >> 4) & 0x08;
|
|
|
|
byte |= (correction[1] >> 3) & 0x04;
|
|
|
|
byte |= (correction[1] >> 2) & 0x02;
|
|
|
|
byte |= (correction[1] >> 1) & 0x01;
|
|
|
|
|
|
|
|
bit = (correction[2] >> 5) & 0x04;
|
|
|
|
bit |= (correction[2] >> 4) & 0x02;
|
|
|
|
bit |= (correction[2] >> 3) & 0x01;
|
|
|
|
|
|
|
|
/* Correct bit */
|
|
|
|
|
2016-06-12 16:31:22 +02:00
|
|
|
finfo("Correcting byte %d at bit %d\n", byte, bit);
|
2013-11-18 16:43:44 +01:00
|
|
|
data[byte] ^= (1 << bit);
|
|
|
|
|
|
|
|
return HAMMING_ERROR_SINGLEBIT;
|
2013-12-02 04:06:07 +01:00
|
|
|
}
|
2013-11-18 16:43:44 +01:00
|
|
|
|
|
|
|
/* Check if ECC has been corrupted */
|
|
|
|
|
2013-12-01 14:39:20 +01:00
|
|
|
if (hamming_bitsincode256(correction) == 1)
|
2013-11-18 16:43:44 +01:00
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: ECC has been correupted\n");
|
2013-11-18 16:43:44 +01:00
|
|
|
return HAMMING_ERROR_ECC;
|
|
|
|
}
|
|
|
|
|
2013-12-03 00:21:09 +01:00
|
|
|
/* Otherwise, there are multiple bit errors */
|
2013-11-18 16:43:44 +01:00
|
|
|
|
|
|
|
else
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: Multiple bit errors\n");
|
2013-11-18 16:43:44 +01:00
|
|
|
return HAMMING_ERROR_MULTIPLEBITS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: hamming_compute256x
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Computes 3-bytes hamming codes for a data block whose size is multiple
|
|
|
|
* of 256 bytes. Each 256 bytes block gets its own code.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* data - Data to compute code for
|
|
|
|
* size - Data size in bytes
|
|
|
|
* code - Codes buffer
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2013-11-18 16:43:44 +01:00
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void hamming_compute256x(FAR const uint8_t *data, size_t size, uint8_t *code)
|
|
|
|
{
|
|
|
|
ssize_t remaining = (ssize_t)size;
|
|
|
|
DEBUGASSERT((size & 0xff) == 0);
|
|
|
|
|
|
|
|
/* Loop, computing the Hamming code on each 256 byte chunk of data */
|
|
|
|
|
|
|
|
while (remaining > 0)
|
|
|
|
{
|
2013-12-01 14:39:20 +01:00
|
|
|
hamming_compute256(data, code);
|
2013-11-18 16:43:44 +01:00
|
|
|
|
|
|
|
/* Setup for the next 256 byte chunk */
|
|
|
|
|
|
|
|
data += 256;
|
|
|
|
code += 3;
|
|
|
|
remaining -= 256;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: hamming_verify256x
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Verifies 3-bytes hamming codes for a data block whose size is multiple
|
|
|
|
* of 256 bytes. Each 256-bytes block is verified with its own code.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* data - Data buffer to verify
|
|
|
|
* size - Size of the data in bytes
|
|
|
|
* code - Original codes
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2013-11-18 16:43:44 +01:00
|
|
|
* Return 0 if the data is correct, HAMMING_ERROR_SINGLEBIT if one or more
|
|
|
|
* block(s) have had a single bit corrected, or either HAMMING_ERROR_ECC
|
|
|
|
* or HAMMING_ERROR_MULTIPLEBITS.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2013-11-18 18:42:17 +01:00
|
|
|
int hamming_verify256x(FAR uint8_t *data, size_t size, FAR const uint8_t *code)
|
2013-11-18 16:43:44 +01:00
|
|
|
{
|
|
|
|
ssize_t remaining = (ssize_t)size;
|
|
|
|
int result = HAMMING_SUCCESS;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
DEBUGASSERT((size & 0xff) == 0);
|
|
|
|
|
|
|
|
/* Loop, verifying each 256 byte chunk of data */
|
|
|
|
|
|
|
|
while (remaining > 0)
|
|
|
|
{
|
2013-12-01 14:39:20 +01:00
|
|
|
result = hamming_verify256(data, code);
|
2013-11-18 16:43:44 +01:00
|
|
|
if (result != HAMMING_SUCCESS)
|
|
|
|
{
|
|
|
|
/* Check for the case of a single bit error that was corrected */
|
|
|
|
|
|
|
|
if (result == HAMMING_ERROR_SINGLEBIT)
|
|
|
|
{
|
|
|
|
/* Report the error, but continue verifying */
|
|
|
|
|
|
|
|
ret = HAMMING_ERROR_SINGLEBIT;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* A bad error occurred, abort the verification and return the
|
|
|
|
* error code
|
|
|
|
*/
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Setup for the next 256 byte chunk */
|
|
|
|
|
|
|
|
data += 256;
|
|
|
|
code += 3;
|
|
|
|
remaining -= 256;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|