Nico Huber has submitted this change and it was merged. ( https://review.coreboot.org/28086 )
Change subject: helpers: Add reverse_byte() and reverse_bytes() ......................................................................
helpers: Add reverse_byte() and reverse_bytes()
Change-Id: I9d2e1e2856c835d22eed3b3a34bc0379773dd831 Signed-off-by: Marc Schink flashrom-dev@marcschink.de Reviewed-on: https://review.coreboot.org/28086 Tested-by: build bot (Jenkins) no-reply@coreboot.org Reviewed-by: Nico Huber nico.h@gmx.de --- M ch341a_spi.c M flash.h M helpers.c 3 files changed, 21 insertions(+), 11 deletions(-)
Approvals: build bot (Jenkins): Verified Nico Huber: Looks good to me, approved
diff --git a/ch341a_spi.c b/ch341a_spi.c index 9ebda0d..83d5ccf 100644 --- a/ch341a_spi.c +++ b/ch341a_spi.c @@ -274,15 +274,6 @@ return ret; }
-/* ch341 requires LSB first, swap the bit order before send and after receive */ -static uint8_t swap_byte(uint8_t x) -{ - x = ((x >> 1) & 0x55) | ((x << 1) & 0xaa); - x = ((x >> 2) & 0x33) | ((x << 2) & 0xcc); - x = ((x >> 4) & 0x0f) | ((x << 4) & 0xf0); - return x; -} - /* The assumed map between UIO command bits, pins on CH341A chip and pins on SPI chip: * UIO CH341A SPI CH341A SPI name * 0 D0/15 CS/1 (CS0) @@ -372,7 +363,7 @@ *ptr++ = CH341A_CMD_SPI_STREAM; unsigned int i; for (i = 0; i < write_now; ++i) - *ptr++ = swap_byte(*writearr++); + *ptr++ = reverse_byte(*writearr++); if (read_now) { memset(ptr, 0xFF, read_now); read_left -= read_now; @@ -387,7 +378,7 @@
unsigned int i; for (i = 0; i < readcnt; i++) { - *readarr++ = swap_byte(rbuf[writecnt + i]); + *readarr++ = reverse_byte(rbuf[writecnt + i]); }
return 0; diff --git a/flash.h b/flash.h index fca3775..01ff5cd 100644 --- a/flash.h +++ b/flash.h @@ -295,6 +295,8 @@ int min(int a, int b); char *strcat_realloc(char *dest, const char *src); void tolower_string(char *str); +uint8_t reverse_byte(uint8_t x); +void reverse_bytes(uint8_t *dst, const uint8_t *src, size_t length); #ifdef __MINGW32__ char* strtok_r(char *str, const char *delim, char **nextp); #endif diff --git a/helpers.c b/helpers.c index 2fcda65..a714908 100644 --- a/helpers.c +++ b/helpers.c @@ -66,6 +66,23 @@ *str = (char)tolower((unsigned char)*str); }
+uint8_t reverse_byte(uint8_t x) +{ + x = ((x >> 1) & 0x55) | ((x << 1) & 0xaa); + x = ((x >> 2) & 0x33) | ((x << 2) & 0xcc); + x = ((x >> 4) & 0x0f) | ((x << 4) & 0xf0); + + return x; +} + +void reverse_bytes(uint8_t *dst, const uint8_t *src, size_t length) +{ + size_t i; + + for (i = 0; i < length; i++) + dst[i] = reverse_byte(src[i]); +} + /* FIXME: Find a better solution for MinGW. Maybe wrap strtok_s (C11) if it becomes available */ #ifdef __MINGW32__ char* strtok_r(char *str, const char *delim, char **nextp)