diff mbox series

[2/2] lib: utils/fdt: Use byteorder conversion functions in libfdt_env.h

Message ID 20230128074617.1917265-3-rpathak@ventanamicro.com
State Superseded
Headers show
Series Add endianness conversion support | expand

Commit Message

Rahul Pathak Jan. 28, 2023, 7:46 a.m. UTC
FDT follows big-endian and CPU can be little or big
endian as per the implementation.
libfdt_env.h defines function for conversion between
fdt and cpu byteorder according to the endianness.

Currently, libfdt_env.h defines custom byte swapping
macros and then undefines them. Instead, use the generic
endianness conversion functions

Signed-off-by: Rahul Pathak <rpathak@ventanamicro.com>
---
 lib/utils/libfdt/libfdt_env.h | 29 ++++++++++-------------------
 1 file changed, 10 insertions(+), 19 deletions(-)

Comments

Xiang W Jan. 28, 2023, 1:13 p.m. UTC | #1
在 2023-01-28星期六的 13:16 +0530,Rahul Pathak写道:
> FDT follows big-endian and CPU can be little or big
> endian as per the implementation.
> libfdt_env.h defines function for conversion between
> fdt and cpu byteorder according to the endianness.
> 
> Currently, libfdt_env.h defines custom byte swapping
> macros and then undefines them. Instead, use the generic
> endianness conversion functions
> 
> Signed-off-by: Rahul Pathak <rpathak@ventanamicro.com>
Look good to me

Reviewed-by: Xiang W <wxjstz@126.com>
> ---
>  lib/utils/libfdt/libfdt_env.h | 29 ++++++++++-------------------
>  1 file changed, 10 insertions(+), 19 deletions(-)
> 
> diff --git a/lib/utils/libfdt/libfdt_env.h b/lib/utils/libfdt/libfdt_env.h
> index f9d9c6783c52..e5ad7698efca 100644
> --- a/lib/utils/libfdt/libfdt_env.h
> +++ b/lib/utils/libfdt/libfdt_env.h
> @@ -9,6 +9,7 @@
>  
>  #include <sbi/sbi_string.h>
>  #include <sbi/sbi_types.h>
> +#include <sbi/sbi_byteorder.h>
>  
>  #define INT_MAX                ((int)(~0U >> 1))
>  #define UINT_MAX       ((unsigned int)~0U)
> @@ -41,45 +42,35 @@ typedef uint16_t FDT_BITWISE fdt16_t;
>  typedef uint32_t FDT_BITWISE fdt32_t;
>  typedef uint64_t FDT_BITWISE fdt64_t;
>  
> -#define EXTRACT_BYTE(x, n)     ((unsigned long long)((uint8_t *)&x)[n])
> -#define CPU_TO_FDT16(x) ((EXTRACT_BYTE(x, 0) << 8) | EXTRACT_BYTE(x, 1))
> -#define CPU_TO_FDT32(x) ((EXTRACT_BYTE(x, 0) << 24) | (EXTRACT_BYTE(x, 1) << 16) | \
> -                        (EXTRACT_BYTE(x, 2) << 8) | EXTRACT_BYTE(x, 3))
> -#define CPU_TO_FDT64(x) ((EXTRACT_BYTE(x, 0) << 56) | (EXTRACT_BYTE(x, 1) << 48) | \
> -                        (EXTRACT_BYTE(x, 2) << 40) | (EXTRACT_BYTE(x, 3) << 32) | \
> -                        (EXTRACT_BYTE(x, 4) << 24) | (EXTRACT_BYTE(x, 5) << 16) | \
> -                        (EXTRACT_BYTE(x, 6) << 8) | EXTRACT_BYTE(x, 7))
> -
>  static inline uint16_t fdt16_to_cpu(fdt16_t x)
>  {
> -       return (FDT_FORCE uint16_t)CPU_TO_FDT16(x);
> +       return (FDT_FORCE uint16_t)be16_to_cpu(x);
>  }
> +
>  static inline fdt16_t cpu_to_fdt16(uint16_t x)
>  {
> -       return (FDT_FORCE fdt16_t)CPU_TO_FDT16(x);
> +       return (FDT_FORCE fdt16_t)cpu_to_be16(x);
>  }
>  
>  static inline uint32_t fdt32_to_cpu(fdt32_t x)
>  {
> -       return (FDT_FORCE uint32_t)CPU_TO_FDT32(x);
> +       return (FDT_FORCE uint32_t)be32_to_cpu(x);
>  }
> +
>  static inline fdt32_t cpu_to_fdt32(uint32_t x)
>  {
> -       return (FDT_FORCE fdt32_t)CPU_TO_FDT32(x);
> +       return (FDT_FORCE fdt32_t)cpu_to_be32(x);
>  }
>  
>  static inline uint64_t fdt64_to_cpu(fdt64_t x)
>  {
> -       return (FDT_FORCE uint64_t)CPU_TO_FDT64(x);
> +       return (FDT_FORCE uint64_t)be64_to_cpu(x);
>  }
> +
>  static inline fdt64_t cpu_to_fdt64(uint64_t x)
>  {
> -       return (FDT_FORCE fdt64_t)CPU_TO_FDT64(x);
> +       return (FDT_FORCE fdt64_t)cpu_to_be64(x);
>  }
> -#undef CPU_TO_FDT64
> -#undef CPU_TO_FDT32
> -#undef CPU_TO_FDT16
> -#undef EXTRACT_BYTE
>  
>  #ifdef __APPLE__
>  #include <AvailabilityMacros.h>
> -- 
> 2.34.1
> 
>
diff mbox series

Patch

diff --git a/lib/utils/libfdt/libfdt_env.h b/lib/utils/libfdt/libfdt_env.h
index f9d9c6783c52..e5ad7698efca 100644
--- a/lib/utils/libfdt/libfdt_env.h
+++ b/lib/utils/libfdt/libfdt_env.h
@@ -9,6 +9,7 @@ 
 
 #include <sbi/sbi_string.h>
 #include <sbi/sbi_types.h>
+#include <sbi/sbi_byteorder.h>
 
 #define INT_MAX		((int)(~0U >> 1))
 #define UINT_MAX	((unsigned int)~0U)
@@ -41,45 +42,35 @@  typedef uint16_t FDT_BITWISE fdt16_t;
 typedef uint32_t FDT_BITWISE fdt32_t;
 typedef uint64_t FDT_BITWISE fdt64_t;
 
-#define EXTRACT_BYTE(x, n)	((unsigned long long)((uint8_t *)&x)[n])
-#define CPU_TO_FDT16(x) ((EXTRACT_BYTE(x, 0) << 8) | EXTRACT_BYTE(x, 1))
-#define CPU_TO_FDT32(x) ((EXTRACT_BYTE(x, 0) << 24) | (EXTRACT_BYTE(x, 1) << 16) | \
-			 (EXTRACT_BYTE(x, 2) << 8) | EXTRACT_BYTE(x, 3))
-#define CPU_TO_FDT64(x) ((EXTRACT_BYTE(x, 0) << 56) | (EXTRACT_BYTE(x, 1) << 48) | \
-			 (EXTRACT_BYTE(x, 2) << 40) | (EXTRACT_BYTE(x, 3) << 32) | \
-			 (EXTRACT_BYTE(x, 4) << 24) | (EXTRACT_BYTE(x, 5) << 16) | \
-			 (EXTRACT_BYTE(x, 6) << 8) | EXTRACT_BYTE(x, 7))
-
 static inline uint16_t fdt16_to_cpu(fdt16_t x)
 {
-	return (FDT_FORCE uint16_t)CPU_TO_FDT16(x);
+	return (FDT_FORCE uint16_t)be16_to_cpu(x);
 }
+
 static inline fdt16_t cpu_to_fdt16(uint16_t x)
 {
-	return (FDT_FORCE fdt16_t)CPU_TO_FDT16(x);
+	return (FDT_FORCE fdt16_t)cpu_to_be16(x);
 }
 
 static inline uint32_t fdt32_to_cpu(fdt32_t x)
 {
-	return (FDT_FORCE uint32_t)CPU_TO_FDT32(x);
+	return (FDT_FORCE uint32_t)be32_to_cpu(x);
 }
+
 static inline fdt32_t cpu_to_fdt32(uint32_t x)
 {
-	return (FDT_FORCE fdt32_t)CPU_TO_FDT32(x);
+	return (FDT_FORCE fdt32_t)cpu_to_be32(x);
 }
 
 static inline uint64_t fdt64_to_cpu(fdt64_t x)
 {
-	return (FDT_FORCE uint64_t)CPU_TO_FDT64(x);
+	return (FDT_FORCE uint64_t)be64_to_cpu(x);
 }
+
 static inline fdt64_t cpu_to_fdt64(uint64_t x)
 {
-	return (FDT_FORCE fdt64_t)CPU_TO_FDT64(x);
+	return (FDT_FORCE fdt64_t)cpu_to_be64(x);
 }
-#undef CPU_TO_FDT64
-#undef CPU_TO_FDT32
-#undef CPU_TO_FDT16
-#undef EXTRACT_BYTE
 
 #ifdef __APPLE__
 #include <AvailabilityMacros.h>