Skip to content

Commit 8975a68

Browse files
petersennagregkh
authored andcommitted
tools: usb: ffs-test: Fix build on big endian systems
The tools/usb/ffs-test.c file defines cpu_to_le16/32 by using the C library htole16/32 function calls. However, cpu_to_le16/32 are used when initializing structures, i.e in a context where a function call is not allowed. It works fine on little endian systems because htole16/32 are defined by the C library as no-ops. But on big-endian systems, they are actually doing something, which might involve calling a function, causing build failures, such as: ffs-test.c:48:25: error: initializer element is not constant #define cpu_to_le32(x) htole32(x) ^~~~~~~ ffs-test.c:128:12: note: in expansion of macro ‘cpu_to_le32’ .magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC_V2), ^~~~~~~~~~~ To solve this, we code cpu_to_le16/32 in a way that allows them to be used when initializing structures. This fix was imported from meta-openembedded/android-tools/fix-big-endian-build.patch written by Thomas Petazzoni <thomas.petazzoni@free-electrons.com>. CC: Thomas Petazzoni <thomas.petazzoni@free-electrons.com> Signed-off-by: Peter Senna Tschudin <peter.senna@gmail.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent db9fc50 commit 8975a68

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

tools/usb/ffs-test.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,25 @@
4444

4545
/******************** Little Endian Handling ********************************/
4646

47-
#define cpu_to_le16(x) htole16(x)
48-
#define cpu_to_le32(x) htole32(x)
47+
/*
48+
* cpu_to_le16/32 are used when initializing structures, a context where a
49+
* function call is not allowed. To solve this, we code cpu_to_le16/32 in a way
50+
* that allows them to be used when initializing structures.
51+
*/
52+
53+
#if __BYTE_ORDER == __LITTLE_ENDIAN
54+
#define cpu_to_le16(x) (x)
55+
#define cpu_to_le32(x) (x)
56+
#else
57+
#define cpu_to_le16(x) ((((x) >> 8) & 0xffu) | (((x) & 0xffu) << 8))
58+
#define cpu_to_le32(x) \
59+
((((x) & 0xff000000u) >> 24) | (((x) & 0x00ff0000u) >> 8) | \
60+
(((x) & 0x0000ff00u) << 8) | (((x) & 0x000000ffu) << 24))
61+
#endif
62+
4963
#define le32_to_cpu(x) le32toh(x)
5064
#define le16_to_cpu(x) le16toh(x)
5165

52-
5366
/******************** Messages and Errors ***********************************/
5467

5568
static const char argv0[] = "ffs-test";

0 commit comments

Comments
 (0)