0006-Fix-build-on-big-endian-systems.patch 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. From 8f7e9458dbfca969d3edc9cf409b3439e98f4c5a Mon Sep 17 00:00:00 2001
  2. From: Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
  3. Date: Sun, 1 Dec 2024 15:45:26 +0100
  4. Subject: [PATCH] Fix build on big endian systems
  5. The usb_linux_client.c file defines cpu_to_le16/32 by using the C
  6. library htole16/32 function calls. However, cpu_to_le16/32 are used
  7. when initializing structures, i.e in a context where a function call
  8. is not allowed.
  9. It works fine on little endian systems because htole16/32 are defined
  10. by the C library as no-ops. But on big-endian systems, they are
  11. actually doing something, which might involve calling a function,
  12. causing build failures.
  13. To solve this, we simply open-code cpu_to_le16/32 in a way that allows
  14. them to be used when initializing structures.
  15. Signed-off-by: Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
  16. ---
  17. core/adb/usb_linux_client.c | 11 +++++++++--
  18. core/adbd/usb_linux_client.c | 11 +++++++++--
  19. 2 files changed, 18 insertions(+), 4 deletions(-)
  20. diff --git a/core/adb/usb_linux_client.c b/core/adb/usb_linux_client.c
  21. index fb1dad0..a981e96 100644
  22. --- a/core/adb/usb_linux_client.c
  23. +++ b/core/adb/usb_linux_client.c
  24. @@ -34,8 +34,15 @@
  25. #define MAX_PACKET_SIZE_FS 64
  26. #define MAX_PACKET_SIZE_HS 512
  27. -#define cpu_to_le16(x) htole16(x)
  28. -#define cpu_to_le32(x) htole32(x)
  29. +#if __BYTE_ORDER == __LITTLE_ENDIAN
  30. +# define cpu_to_le16(x) (x)
  31. +# define cpu_to_le32(x) (x)
  32. +#else
  33. +# define cpu_to_le16(x) ((((x) >> 8) & 0xffu) | (((x) & 0xffu) << 8))
  34. +# define cpu_to_le32(x) \
  35. + ((((x) & 0xff000000u) >> 24) | (((x) & 0x00ff0000u) >> 8) | \
  36. + (((x) & 0x0000ff00u) << 8) | (((x) & 0x000000ffu) << 24))
  37. +#endif
  38. struct usb_handle
  39. {
  40. diff --git a/core/adbd/usb_linux_client.c b/core/adbd/usb_linux_client.c
  41. index 33875a8..0e4d200 100644
  42. --- a/core/adbd/usb_linux_client.c
  43. +++ b/core/adbd/usb_linux_client.c
  44. @@ -34,8 +34,15 @@
  45. #define MAX_PACKET_SIZE_FS 64
  46. #define MAX_PACKET_SIZE_HS 512
  47. -#define cpu_to_le16(x) htole16(x)
  48. -#define cpu_to_le32(x) htole32(x)
  49. +#if __BYTE_ORDER == __LITTLE_ENDIAN
  50. +# define cpu_to_le16(x) (x)
  51. +# define cpu_to_le32(x) (x)
  52. +#else
  53. +# define cpu_to_le16(x) ((((x) >> 8) & 0xffu) | (((x) & 0xffu) << 8))
  54. +# define cpu_to_le32(x) \
  55. + ((((x) & 0xff000000u) >> 24) | (((x) & 0x00ff0000u) >> 8) | \
  56. + (((x) & 0x0000ff00u) << 8) | (((x) & 0x000000ffu) << 24))
  57. +#endif
  58. struct usb_handle
  59. {
  60. --
  61. 2.47.0