Marc Schink has uploaded a new change for review. ( https://review.coreboot.org/19993 )
Change subject: helpers: Add swap_byte() and swap_bytes() ......................................................................
helpers: Add swap_byte() and swap_bytes()
Change-Id: I9d2e1e2856c835d22eed3b3a34bc0379773dd831 Signed-off-by: Marc Schink jaylink-dev@marcschink.de --- M ch341a_spi.c M flash.h M helpers.c 3 files changed, 19 insertions(+), 9 deletions(-)
git pull ssh://review.coreboot.org:29418/flashrom refs/changes/93/19993/1
diff --git a/ch341a_spi.c b/ch341a_spi.c index 6eb2804..1ffa437 100644 --- a/ch341a_spi.c +++ b/ch341a_spi.c @@ -278,15 +278,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) diff --git a/flash.h b/flash.h index da049d1..2b539a8 100644 --- a/flash.h +++ b/flash.h @@ -257,6 +257,8 @@ int min(int a, int b); char *strcat_realloc(char *dest, const char *src); void tolower_string(char *str); +uint8_t swap_byte(uint8_t x); +void swap_bytes(uint8_t *buffer, 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 f6eae46..69c14ed 100644 --- a/helpers.c +++ b/helpers.c @@ -70,6 +70,23 @@ *str = (char)tolower((unsigned char)*str); }
+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; +} + +void swap_bytes(uint8_t *buffer, size_t length) +{ + size_t i; + + for (i = 0; i < length; i++) + buffer[i] = swap_byte(buffer[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)