[LEDE-DEV] [PATCH] uqmi: Fix for big endian arch

Oskari Lemmelä oskari at lemmela.net
Fri Apr 6 23:38:11 PDT 2018


Hi,

Here is better fix for endianness issue.

Tested on both ar71xx (big) and sunxi (little).


Use memcpy instead of pointer casting

Signed-off-by: Oskari Lemmelä <oskari at lemmela.net>
---
  data/gen-code.pl | 26 +++++++++++++-------------
  1 file changed, 13 insertions(+), 13 deletions(-)

diff --git a/data/gen-code.pl b/data/gen-code.pl
index f45d28a..1af143c 100755
--- a/data/gen-code.pl
+++ b/data/gen-code.pl
@@ -13,22 +13,22 @@ my $varsize_field;
  my %tlv_get = (
  	gint8 => "*(int8_t *) get_next(1)",
  	guint8 => "*(uint8_t *) get_next(1)",
-	gint16 => "le16_to_cpu(*(uint16_t *) get_next(2))",
-	guint16 => "le16_to_cpu(*(uint16_t *) get_next(2))",
-	gint32 => "le32_to_cpu(*(uint32_t *) get_next(4))",
-	guint32 => "le32_to_cpu(*(uint32_t *) get_next(4))",
-	gint64 => "le64_to_cpu(*(uint64_t *) get_next(8))",
-	guint64 => "le64_to_cpu(*(uint64_t *) get_next(8))",
-	gfloat => "({ uint32_t data = le32_to_cpu(*(uint32_t *) get_next(4)); 
float _val; memcpy(&_val, &data, sizeof(_val)); _val; })"
+	gint16 => "({ uint16_t var; memcpy(&var, get_next(2), 2); 
le16_to_cpu(var); })",
+	guint16 => "({ uint16_t var; memcpy(&var, get_next(2), 2); 
le16_to_cpu(var); })",
+	gint32 => "({ uint32_t var; memcpy(&var, get_next(4), 4); 
le32_to_cpu(var); })",
+	guint32 => "({ uint32_t var; memcpy(&var, get_next(4), 4); 
le32_to_cpu(var); })",
+	gint64 => "({ uint64_t var; memcpy(&var, get_next(8), 8); 
le64_to_cpu(var); })",
+	guint64 => "({ uint64_t var; memcpy(&var, get_next(8), 8); 
le64_to_cpu(var); })",
+	gfloat => "({ float var; memcpy(&var, get_next(4), 4); 
le32_to_cpu(var); })"
  );

  my %tlv_get_be = (
-	gint16 => "be16_to_cpu(*(uint16_t *) get_next(2))",
-	guint16 => "be16_to_cpu(*(uint16_t *) get_next(2))",
-	gint32 => "be32_to_cpu(*(uint32_t *) get_next(4))",
-	guint32 => "be32_to_cpu(*(uint32_t *) get_next(4))",
-	gint64 => "be64_to_cpu(*(uint64_t *) get_next(8))",
-	guint64 => "be64_to_cpu(*(uint64_t *) get_next(8))",
+	gint16 => "({ uint16_t var; memcpy(&var, get_next(2), 2); 
be16_to_cpu(var); })",
+	guint16 => "({ uint16_t var; memcpy(&var, get_next(2), 2); 
be16_to_cpu(var); })",
+	gint32 => "({ uint32_t var; memcpy(&var, get_next(4), 4); 
be32_to_cpu(var); })",
+	guint32 => "({ uint32_t var; memcpy(&var, get_next(4), 4); 
be32_to_cpu(var); })",
+	gint64 => "({ uint64_t var; memcpy(&var, get_next(8), 8); 
be64_to_cpu(var); })",
+	guint64 => "({ uint64_t var; memcpy(&var, get_next(8), 8); 
be64_to_cpu(var); })",
  );

  sub gen_tlv_parse_field($$$$) {
-- 
2.7.4


On 04/01/2018 05:22 PM, Oskari Lemmela wrote:
> leXX_to_cpu function messes up get_next value in big endian arch.
> 
> Signed-off-by: Oskari Lemmelä <oskari at lemmela.net>
> ---
>   data/gen-code.pl | 12 ++++++------
>   1 file changed, 6 insertions(+), 6 deletions(-)
> 
> diff --git a/data/gen-code.pl b/data/gen-code.pl
> index f45d28a..cf46b67 100755
> --- a/data/gen-code.pl
> +++ b/data/gen-code.pl
> @@ -13,12 +13,12 @@ my $varsize_field;
>   my %tlv_get = (
>   	gint8 => "*(int8_t *) get_next(1)",
>   	guint8 => "*(uint8_t *) get_next(1)",
> -	gint16 => "le16_to_cpu(*(uint16_t *) get_next(2))",
> -	guint16 => "le16_to_cpu(*(uint16_t *) get_next(2))",
> -	gint32 => "le32_to_cpu(*(uint32_t *) get_next(4))",
> -	guint32 => "le32_to_cpu(*(uint32_t *) get_next(4))",
> -	gint64 => "le64_to_cpu(*(uint64_t *) get_next(8))",
> -	guint64 => "le64_to_cpu(*(uint64_t *) get_next(8))",
> +	gint16 => "({ int16_t value = *(int16_t *) get_next(2); int16_t _val = le16_to_cpu(value); _val;})",
> +	guint16 => "({ uint16_t value = *(uint16_t *) get_next(2); uint16_t _val = le16_to_cpu(value); _val;})",
> +	gint32 => "({ int32_t value = *(int32_t *) get_next(4); int32_t _val = le32_to_cpu(value); _val;})",
> +	guint32 => "({ uint32_t value = *(uint32_t *) get_next(4); uint32_t _val = le32_to_cpu(value); _val;})",
> +	gint64 => "({ int64_t value = *(int64_t *) get_next(8); int64_t _val = le64_to_cpu(value); _val;})",
> +	guint64 => "({ uint64_t value = *(uint64_t *) get_next(8); uint64_t _val = le64_to_cpu(value); _val;})",
>   	gfloat => "({ uint32_t data = le32_to_cpu(*(uint32_t *) get_next(4)); float _val; memcpy(&_val, &data, sizeof(_val)); _val; })"
>   );
>   
> 



More information about the Lede-dev mailing list