diff mbox

[2/2] libflash: endian fixups for ecc.c

Message ID 1425363689-22815-3-git-send-email-cyril.bur@au1.ibm.com
State Accepted
Headers show

Commit Message

Cyril Bur March 3, 2015, 6:21 a.m. UTC
The flash is in big endian and the ecc code must be aware of this when
performing ecc checks on a little endian cpu.

Signed-off-by: Cyril Bur <cyril.bur@au1.ibm.com>
---
 libflash/ecc.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

Comments

Michael Neuling March 6, 2015, 4:49 a.m. UTC | #1
On Tue, 2015-03-03 at 17:21 +1100, Cyril Bur wrote:
> The flash is in big endian and the ecc code must be aware of this when
> performing ecc checks on a little endian cpu.
> 
> Signed-off-by: Cyril Bur <cyril.bur@au1.ibm.com>
> ---
>  libflash/ecc.c | 13 ++++++++-----
>  1 file changed, 8 insertions(+), 5 deletions(-)
> 
> diff --git a/libflash/ecc.c b/libflash/ecc.c
> index 2fca0fa..61084cc 100644
> --- a/libflash/ecc.c
> +++ b/libflash/ecc.c
> @@ -18,6 +18,8 @@
>  
>  #include <stdint.h>
>  
> +#include <ccan/endian/endian.h>
> +
>  #include "libflash.h"
>  #include "ecc.h"
>  
> @@ -141,7 +143,7 @@ static uint8_t eccverify(uint64_t data, uint8_t ecc)
>   */
>  uint8_t eccmemcpy(uint64_t *dst, uint64_t *src, uint32_t len)
>  {
> -	uint64_t *data;
> +	beint64_t *data;
>  	uint8_t *ecc;
>  	uint32_t i;
>  	uint8_t badbit;
> @@ -157,20 +159,21 @@ uint8_t eccmemcpy(uint64_t *dst, uint64_t *src, uint32_t len)
>  	len >>= 3;
>  
>  	for (i = 0; i < len; i++) {
> -		data = (uint64_t *)((uint8_t *)src + i*9);
> +		data = (beint64_t *)((uint8_t *)src + i * 9);
>  		ecc = (uint8_t *)data + 8;
>  
> -		badbit = eccverify(*data, *ecc);
> +		badbit = eccverify(be64_to_cpu(*data), *ecc);
>  		if (badbit == UE) {
>  			FL_ERR("ECC: uncorrectable error: %016lx %02x\n",
> -				(long unsigned int)*data, *ecc);
> +				(long unsigned int)be64_to_cpu(*data), *ecc);
>  			return badbit;
>  		}
>  		*dst = *data;
>  		if (badbit <= UE)
>  			FL_INF("ECC: correctable error: %i\n", badbit);
>  		if (badbit < 64)
> -			*dst = *data ^ (1ul << (63 - badbit));
> +			*dst = (uint64_t)cpu_to_be64(be64_to_cpu(*data) ^
> +					(1ul << (63 - badbit)));

This line makes me wanna vomit.  Can you stick it a function or
something?

Mikey


>  		dst++;
>  	}
>  	return GD;
diff mbox

Patch

diff --git a/libflash/ecc.c b/libflash/ecc.c
index 2fca0fa..61084cc 100644
--- a/libflash/ecc.c
+++ b/libflash/ecc.c
@@ -18,6 +18,8 @@ 
 
 #include <stdint.h>
 
+#include <ccan/endian/endian.h>
+
 #include "libflash.h"
 #include "ecc.h"
 
@@ -141,7 +143,7 @@  static uint8_t eccverify(uint64_t data, uint8_t ecc)
  */
 uint8_t eccmemcpy(uint64_t *dst, uint64_t *src, uint32_t len)
 {
-	uint64_t *data;
+	beint64_t *data;
 	uint8_t *ecc;
 	uint32_t i;
 	uint8_t badbit;
@@ -157,20 +159,21 @@  uint8_t eccmemcpy(uint64_t *dst, uint64_t *src, uint32_t len)
 	len >>= 3;
 
 	for (i = 0; i < len; i++) {
-		data = (uint64_t *)((uint8_t *)src + i*9);
+		data = (beint64_t *)((uint8_t *)src + i * 9);
 		ecc = (uint8_t *)data + 8;
 
-		badbit = eccverify(*data, *ecc);
+		badbit = eccverify(be64_to_cpu(*data), *ecc);
 		if (badbit == UE) {
 			FL_ERR("ECC: uncorrectable error: %016lx %02x\n",
-				(long unsigned int)*data, *ecc);
+				(long unsigned int)be64_to_cpu(*data), *ecc);
 			return badbit;
 		}
 		*dst = *data;
 		if (badbit <= UE)
 			FL_INF("ECC: correctable error: %i\n", badbit);
 		if (badbit < 64)
-			*dst = *data ^ (1ul << (63 - badbit));
+			*dst = (uint64_t)cpu_to_be64(be64_to_cpu(*data) ^
+					(1ul << (63 - badbit)));
 		dst++;
 	}
 	return GD;