libftdi Archives

Subject: [PATCH] Fix checksum calculation for big endian systems

From: yegorslists@xxxxxxxxxxxxxx
To: libftdi@xxxxxxxxxxxxxxxxxxxxxxx
Cc: Yegor Yefremov <yegorslists@xxxxxxxxxxxxxx>
Date: Wed, 8 Jun 2016 11:52:40 +0200
From: Yegor Yefremov <yegorslists@xxxxxxxxxxxxxx>

During checksum calculation in ftdi_eeprom_build() a 16-bit value
will be read directly from EEPROM (FT230X devices only):

ftdi_read_eeprom_location(ftdi, i, &data)

'data' is little endian, so the bytes must be swapped on a big-endian
system. Introduce related macro swap_short() and add endianess detection
to CMakeLists.txt.
---
 CMakeLists.txt | 7 +++++++
 src/ftdi.c     | 2 +-
 src/ftdi_i.h   | 5 +++++
 3 files changed, 13 insertions(+), 1 deletion(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index ce122ff..3cb2f3f 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -22,6 +22,13 @@ if(${CMAKE_BUILD_TYPE} STREQUAL Debug)
    add_definitions(-DDEBUG)
 endif(${CMAKE_BUILD_TYPE} STREQUAL Debug)
 
+# check, if he system is big endian or little endian
+include(TestBigEndian)
+test_big_endian(BIGENDIAN)
+if(${BIGENDIAN})
+     add_definitions(-DBIGENDIAN)
+endif(${BIGENDIAN})
+
 # find libusb
 find_package ( USB1 REQUIRED )
 include_directories ( ${LIBUSB_INCLUDE_DIR} )
diff --git a/src/ftdi.c b/src/ftdi.c
index f5d263c..f516538 100644
--- a/src/ftdi.c
+++ b/src/ftdi.c
@@ -3239,7 +3239,7 @@ int ftdi_eeprom_build(struct ftdi_context *ftdi)
                 fprintf(stderr, "Reading Factory Configuration Data failed\n");
                 i = 0x50;
             }
-            value = data;
+            value = swap_short(data);
         }
         else {
             value = output[i*2];
diff --git a/src/ftdi_i.h b/src/ftdi_i.h
index cf2ac78..a9ab3a6 100644
--- a/src/ftdi_i.h
+++ b/src/ftdi_i.h
@@ -141,3 +141,8 @@ struct ftdi_eeprom
     int release_number;
 };
 
+#if BIGENDIAN
+#define swap_short(val) ((val >> 8) | (val << 8))
+#else
+#define swap_short(val) (val)
+#endif
-- 
2.1.4


--
libftdi - see http://www.intra2net.com/en/developer/libftdi for details.
To unsubscribe send a mail to libftdi+unsubscribe@xxxxxxxxxxxxxxxxxxxxxxx   

Current Thread