diff mbox series

[2/2] arm: mach-snapdrgon: misc: Simplify msm_generate_mac_addr()

Message ID 20210802145231.85305-2-stephan@gerhold.net
State Superseded
Delegated to: Tom Rini
Headers show
Series [1/2] arm: mach-snapdragon: misc: Initialize eMMC if necessary | expand

Commit Message

Stephan Gerhold Aug. 2, 2021, 2:52 p.m. UTC
The logic in msm_generate_mac_addr() was originally taken from the LK
bootloader where the serial number is a string and must be parsed first.
However, in U-Boot msm_board_serial() returns an u32 and
msm_generate_mac_addr() has quite complicated code that will first
print it as a hex string and then immediately parse it again.

What this function actually does at the end is to put the serial number
encoded as big endian (the order used for the hex string) into the u8 *mac.
Use put_unaligned_be32() to do that with bit shifts instead of going
through the string format.

This should be slightly more efficient and cleaner but does not result
in any functional difference.

Cc: Ramon Fried <rfried.dev@gmail.com>
Signed-off-by: Stephan Gerhold <stephan@gerhold.net>
---

 arch/arm/mach-snapdragon/misc.c | 13 +++----------
 1 file changed, 3 insertions(+), 10 deletions(-)

Comments

Ramon Fried Aug. 2, 2021, 11:20 p.m. UTC | #1
On Mon, Aug 2, 2021 at 5:52 PM Stephan Gerhold <stephan@gerhold.net> wrote:
>
> The logic in msm_generate_mac_addr() was originally taken from the LK
> bootloader where the serial number is a string and must be parsed first.
> However, in U-Boot msm_board_serial() returns an u32 and
> msm_generate_mac_addr() has quite complicated code that will first
> print it as a hex string and then immediately parse it again.
>
> What this function actually does at the end is to put the serial number
> encoded as big endian (the order used for the hex string) into the u8 *mac.
> Use put_unaligned_be32() to do that with bit shifts instead of going
> through the string format.
>
> This should be slightly more efficient and cleaner but does not result
> in any functional difference.
>
> Cc: Ramon Fried <rfried.dev@gmail.com>
> Signed-off-by: Stephan Gerhold <stephan@gerhold.net>
> ---
>
>  arch/arm/mach-snapdragon/misc.c | 13 +++----------
>  1 file changed, 3 insertions(+), 10 deletions(-)
>
> diff --git a/arch/arm/mach-snapdragon/misc.c b/arch/arm/mach-snapdragon/misc.c
> index 5c062e9636..02edfda5d6 100644
> --- a/arch/arm/mach-snapdragon/misc.c
> +++ b/arch/arm/mach-snapdragon/misc.c
> @@ -9,6 +9,7 @@
>  #include <common.h>
>  #include <mmc.h>
>  #include <asm/arch/misc.h>
> +#include <asm/unaligned.h>
>
>  /* UNSTUFF_BITS macro taken from Linux Kernel: drivers/mmc/core/sd.c */
>  #define UNSTUFF_BITS(resp, start, size) \
> @@ -41,16 +42,8 @@ u32 msm_board_serial(void)
>
>  void msm_generate_mac_addr(u8 *mac)
>  {
> -       int i;
> -       char sn[9];
> -
> -       snprintf(sn, 9, "%08x", msm_board_serial());
> -
>         /* fill in the mac with serialno, use locally adminstrated pool */
>         mac[0] = 0x02;
> -       mac[1] = 00;
> -       for (i = 3; i >= 0; i--) {
> -               mac[i + 2] = simple_strtoul(&sn[2 * i], NULL, 16);
> -               sn[2 * i] = 0;
> -       }
> +       mac[1] = 0x00;
> +       put_unaligned_be32(msm_board_serial(), &mac[2]);
>  }
> --
> 2.32.0
>
Please add a comment explaining the logic in the code. This is not
understandable without explanation.
Thanks,
Ramon.
diff mbox series

Patch

diff --git a/arch/arm/mach-snapdragon/misc.c b/arch/arm/mach-snapdragon/misc.c
index 5c062e9636..02edfda5d6 100644
--- a/arch/arm/mach-snapdragon/misc.c
+++ b/arch/arm/mach-snapdragon/misc.c
@@ -9,6 +9,7 @@ 
 #include <common.h>
 #include <mmc.h>
 #include <asm/arch/misc.h>
+#include <asm/unaligned.h>
 
 /* UNSTUFF_BITS macro taken from Linux Kernel: drivers/mmc/core/sd.c */
 #define UNSTUFF_BITS(resp, start, size) \
@@ -41,16 +42,8 @@  u32 msm_board_serial(void)
 
 void msm_generate_mac_addr(u8 *mac)
 {
-	int i;
-	char sn[9];
-
-	snprintf(sn, 9, "%08x", msm_board_serial());
-
 	/* fill in the mac with serialno, use locally adminstrated pool */
 	mac[0] = 0x02;
-	mac[1] = 00;
-	for (i = 3; i >= 0; i--) {
-		mac[i + 2] = simple_strtoul(&sn[2 * i], NULL, 16);
-		sn[2 * i] = 0;
-	}
+	mac[1] = 0x00;
+	put_unaligned_be32(msm_board_serial(), &mac[2]);
 }