From f9427c75a57d01f791770aa6bfc2a094c1eaf371 Mon Sep 17 00:00:00 2001 From: Antonio Ospite Date: Mon, 3 Oct 2011 16:22:36 +0200 Subject: [PATCH] kinect_upload_fw: support architectures that are not little-endian Add an endian.c program which will be used at compile time to generate an endian.h file with the information about the endianness of the architecture. This way we do not depend on any particular build system to retrieve this information (libfreenect knows the endianness with the help of Cmake, for instance). This is inspired from the rdesktop project[1] and looks more portable than relying on the system endian.h as its use is relatively new and not standardized yet, even though there are proposals in that direction[2]. Finally fn_le32() implementation is ripped off from libfreenect. [1] http://www.rdesktop.org/archive/2001/msg00722.html [2] http://austingroupbugs.net/view.php?id=162 --- kinect_upload_fw/Makefile | 14 ++++++++++++-- kinect_upload_fw/endian.c | 25 +++++++++++++++++++++++++ kinect_upload_fw/kinect_upload_fw.c | 11 ++++++++++- 3 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 kinect_upload_fw/endian.c diff --git a/kinect_upload_fw/Makefile b/kinect_upload_fw/Makefile index 1c21c14..e917d59 100644 --- a/kinect_upload_fw/Makefile +++ b/kinect_upload_fw/Makefile @@ -32,11 +32,21 @@ LDFLAGS += $(shell pkg-config --libs libusb-1.0) PREFIX ?= /usr/local bindir := $(PREFIX)/sbin -kinect_upload_fw: kinect_upload_fw.o +all: kinect_upload_fw + + +endian: endian.o + +endian.h: endian + rm -f endian.h + ./endian > endian.h + +kinect_upload_fw: endian.h kinect_upload_fw.o + install: kinect_upload_fw install -d $(DESTDIR)$(bindir) install -m 755 kinect_upload_fw $(DESTDIR)$(bindir) clean: - rm -rf *~ *.o kinect_upload_fw + rm -rf *~ *.o kinect_upload_fw endian endian.h diff --git a/kinect_upload_fw/endian.c b/kinect_upload_fw/endian.c new file mode 100644 index 0000000..89120ca --- /dev/null +++ b/kinect_upload_fw/endian.c @@ -0,0 +1,25 @@ +#include +#include + +static int litend(void) { + int i = 0; + ((char *) (&i))[0] = 1; + return (i == 1); +} + +static int bigend(void) { + return !litend(); +} + +int main(void) { + printf("#ifndef __ENDIAN_H\n"); + printf("#define __ENDIAN_H\n"); + printf("\n"); + printf("#define __LITTLE_ENDIAN 1234\n"); + printf("#define __BIG_ENDIAN 4321\n"); + printf("#define __BYTE_ORDER __%s_ENDIAN\n", + litend() ? "LITTLE" : "BIG"); + printf("\n"); + printf("#endif /* __ENDIAN_H */\n"); + exit(0); +} diff --git a/kinect_upload_fw/kinect_upload_fw.c b/kinect_upload_fw/kinect_upload_fw.c index f9a1952..eb65113 100644 --- a/kinect_upload_fw/kinect_upload_fw.c +++ b/kinect_upload_fw/kinect_upload_fw.c @@ -36,6 +36,8 @@ #include #include +#include "endian.h" + static libusb_device_handle *dev; unsigned int seq; @@ -55,8 +57,15 @@ typedef struct { } status_code; #define LOG(...) printf(__VA_ARGS__) + +#if __BYTE_ORDER == __BIG_ENDIAN +static inline uint32_t fn_le32(uint32_t d) +{ + return (d<<24) | ((d<<8)&0xFF0000) | ((d>>8)&0xFF00) | (d>>24); +} +#else #define fn_le32(x) (x) -// TODO: support architectures that aren't little-endian +#endif static void dump_bl_cmd(bootloader_command cmd) { int i; -- 2.1.4