All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] board: turris: Initialize serial# env
@ 2022-08-27 18:06 Pali Rohár
  2022-08-28  3:48 ` Marek Behún
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Pali Rohár @ 2022-08-27 18:06 UTC (permalink / raw)
  To: Marek Behún, Stefan Roese; +Cc: u-boot

Store serial number from atsha cryptochip into the serial# env variable.
U-Boot automatically puts content of this variable into the root device
tree property serial-number when booting Linux kernel. Refactor turris
atsha code and from turris_atsha_otp_get_serial_number() function returns
directly string suitable for printing or storing into device tree. Because
during different boot stages is env storage read-only, it is not possible
to always store serial number into env storage. So introduce a new function
turris_atsha_otp_init_serial_number() which is called at later stage and
which ensures that serial number is correctly stored into env.

Signed-off-by: Pali Rohár <pali@kernel.org>
---
 board/CZ.NIC/turris_atsha_otp.c          | 33 +++++++++++++++++++++---
 board/CZ.NIC/turris_atsha_otp.h          |  3 ++-
 board/CZ.NIC/turris_omnia/turris_omnia.c | 11 +++-----
 3 files changed, 36 insertions(+), 11 deletions(-)

diff --git a/board/CZ.NIC/turris_atsha_otp.c b/board/CZ.NIC/turris_atsha_otp.c
index aa4e29b1560e..a29fe3623174 100644
--- a/board/CZ.NIC/turris_atsha_otp.c
+++ b/board/CZ.NIC/turris_atsha_otp.c
@@ -93,30 +93,57 @@ int turris_atsha_otp_init_mac_addresses(int first_idx)
 	return 0;
 }
 
-int turris_atsha_otp_get_serial_number(u32 *version_num, u32 *serial_num)
+int turris_atsha_otp_init_serial_number(void)
+{
+	char serial[17];
+	int ret;
+
+	ret = turris_atsha_otp_get_serial_number(serial);
+	if (ret)
+		return ret;
+
+	if (!env_get("serial#"))
+		return -1;
+
+	return 0;
+}
+
+int turris_atsha_otp_get_serial_number(char serial[17])
 {
 	struct udevice *dev = get_atsha204a_dev();
+	u32 version_num, serial_num;
+	const char *serial_env;
 	int ret;
 
 	if (!dev)
 		return -1;
 
+	serial_env = env_get("serial#");
+	if (serial_env && strlen(serial_env) == 16) {
+		memcpy(serial, serial_env, 17);
+		return 0;
+	}
+
 	ret = atsha204a_wakeup(dev);
 	if (ret)
 		return ret;
 
 	ret = atsha204a_read(dev, ATSHA204A_ZONE_OTP, false,
 			     TURRIS_ATSHA_OTP_VERSION,
-			     (u8 *)version_num);
+			     (u8 *)&version_num);
 	if (ret)
 		return ret;
 
 	ret = atsha204a_read(dev, ATSHA204A_ZONE_OTP, false,
 			     TURRIS_ATSHA_OTP_SERIAL,
-			     (u8 *)serial_num);
+			     (u8 *)&serial_num);
 	if (ret)
 		return ret;
 
 	atsha204a_sleep(dev);
+
+	sprintf(serial, "%08X%08X", be32_to_cpu(version_num), be32_to_cpu(serial_num));
+	env_set("serial#", serial);
+
 	return 0;
 }
diff --git a/board/CZ.NIC/turris_atsha_otp.h b/board/CZ.NIC/turris_atsha_otp.h
index bd4308fdc3ef..2cfe20bbc3a7 100644
--- a/board/CZ.NIC/turris_atsha_otp.h
+++ b/board/CZ.NIC/turris_atsha_otp.h
@@ -4,6 +4,7 @@
 #define TURRIS_ATSHA_OTP_H
 
 int turris_atsha_otp_init_mac_addresses(int first_idx);
-int turris_atsha_otp_get_serial_number(u32 *version_num, u32 *serial_num);
+int turris_atsha_otp_init_serial_number(void);
+int turris_atsha_otp_get_serial_number(char serial[17]);
 
 #endif
diff --git a/board/CZ.NIC/turris_omnia/turris_omnia.c b/board/CZ.NIC/turris_omnia/turris_omnia.c
index ab5061ef582b..cf8a6026702b 100644
--- a/board/CZ.NIC/turris_omnia/turris_omnia.c
+++ b/board/CZ.NIC/turris_omnia/turris_omnia.c
@@ -963,19 +963,15 @@ int board_late_init(void)
 
 int show_board_info(void)
 {
-	u32 version_num, serial_num;
+	char serial[17];
 	int err;
 
-	err = turris_atsha_otp_get_serial_number(&version_num, &serial_num);
+	err = turris_atsha_otp_get_serial_number(serial);
 	printf("Model: Turris Omnia\n");
 	printf("  MCU type: %s\n", omnia_get_mcu_type());
 	printf("  MCU version: %s\n", omnia_get_mcu_version());
 	printf("  RAM size: %i MiB\n", omnia_get_ram_size_gb() * 1024);
-	if (err)
-		printf("  Serial Number: unknown\n");
-	else
-		printf("  Serial Number: %08X%08X\n", be32_to_cpu(version_num),
-		       be32_to_cpu(serial_num));
+	printf("  Serial Number: %s\n", !err ? serial : "unknown");
 
 	return 0;
 }
@@ -983,6 +979,7 @@ int show_board_info(void)
 int misc_init_r(void)
 {
 	turris_atsha_otp_init_mac_addresses(1);
+	turris_atsha_otp_init_serial_number();
 	return 0;
 }
 
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] board: turris: Initialize serial# env
  2022-08-27 18:06 [PATCH] board: turris: Initialize serial# env Pali Rohár
@ 2022-08-28  3:48 ` Marek Behún
  2022-08-30 12:26 ` Stefan Roese
  2022-09-13  6:59 ` Stefan Roese
  2 siblings, 0 replies; 4+ messages in thread
From: Marek Behún @ 2022-08-28  3:48 UTC (permalink / raw)
  To: Pali Rohár; +Cc: Stefan Roese, u-boot

On Sat, 27 Aug 2022 20:06:30 +0200
Pali Rohár <pali@kernel.org> wrote:

> Store serial number from atsha cryptochip into the serial# env variable.
> U-Boot automatically puts content of this variable into the root device
> tree property serial-number when booting Linux kernel. Refactor turris
> atsha code and from turris_atsha_otp_get_serial_number() function returns
> directly string suitable for printing or storing into device tree. Because
> during different boot stages is env storage read-only, it is not possible
> to always store serial number into env storage. So introduce a new function
> turris_atsha_otp_init_serial_number() which is called at later stage and
> which ensures that serial number is correctly stored into env.
> 
> Signed-off-by: Pali Rohár <pali@kernel.org>

Reviewed-by: Marek Behún <kabel@kernel.org>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] board: turris: Initialize serial# env
  2022-08-27 18:06 [PATCH] board: turris: Initialize serial# env Pali Rohár
  2022-08-28  3:48 ` Marek Behún
@ 2022-08-30 12:26 ` Stefan Roese
  2022-09-13  6:59 ` Stefan Roese
  2 siblings, 0 replies; 4+ messages in thread
From: Stefan Roese @ 2022-08-30 12:26 UTC (permalink / raw)
  To: Pali Rohár, Marek Behún; +Cc: u-boot

On 27.08.22 20:06, Pali Rohár wrote:
> Store serial number from atsha cryptochip into the serial# env variable.
> U-Boot automatically puts content of this variable into the root device
> tree property serial-number when booting Linux kernel. Refactor turris
> atsha code and from turris_atsha_otp_get_serial_number() function returns
> directly string suitable for printing or storing into device tree. Because
> during different boot stages is env storage read-only, it is not possible
> to always store serial number into env storage. So introduce a new function
> turris_atsha_otp_init_serial_number() which is called at later stage and
> which ensures that serial number is correctly stored into env.
> 
> Signed-off-by: Pali Rohár <pali@kernel.org>

Reviewed-by: Stefan Roese <sr@denx.de>

Thanks,
Stefan

> ---
>   board/CZ.NIC/turris_atsha_otp.c          | 33 +++++++++++++++++++++---
>   board/CZ.NIC/turris_atsha_otp.h          |  3 ++-
>   board/CZ.NIC/turris_omnia/turris_omnia.c | 11 +++-----
>   3 files changed, 36 insertions(+), 11 deletions(-)
> 
> diff --git a/board/CZ.NIC/turris_atsha_otp.c b/board/CZ.NIC/turris_atsha_otp.c
> index aa4e29b1560e..a29fe3623174 100644
> --- a/board/CZ.NIC/turris_atsha_otp.c
> +++ b/board/CZ.NIC/turris_atsha_otp.c
> @@ -93,30 +93,57 @@ int turris_atsha_otp_init_mac_addresses(int first_idx)
>   	return 0;
>   }
>   
> -int turris_atsha_otp_get_serial_number(u32 *version_num, u32 *serial_num)
> +int turris_atsha_otp_init_serial_number(void)
> +{
> +	char serial[17];
> +	int ret;
> +
> +	ret = turris_atsha_otp_get_serial_number(serial);
> +	if (ret)
> +		return ret;
> +
> +	if (!env_get("serial#"))
> +		return -1;
> +
> +	return 0;
> +}
> +
> +int turris_atsha_otp_get_serial_number(char serial[17])
>   {
>   	struct udevice *dev = get_atsha204a_dev();
> +	u32 version_num, serial_num;
> +	const char *serial_env;
>   	int ret;
>   
>   	if (!dev)
>   		return -1;
>   
> +	serial_env = env_get("serial#");
> +	if (serial_env && strlen(serial_env) == 16) {
> +		memcpy(serial, serial_env, 17);
> +		return 0;
> +	}
> +
>   	ret = atsha204a_wakeup(dev);
>   	if (ret)
>   		return ret;
>   
>   	ret = atsha204a_read(dev, ATSHA204A_ZONE_OTP, false,
>   			     TURRIS_ATSHA_OTP_VERSION,
> -			     (u8 *)version_num);
> +			     (u8 *)&version_num);
>   	if (ret)
>   		return ret;
>   
>   	ret = atsha204a_read(dev, ATSHA204A_ZONE_OTP, false,
>   			     TURRIS_ATSHA_OTP_SERIAL,
> -			     (u8 *)serial_num);
> +			     (u8 *)&serial_num);
>   	if (ret)
>   		return ret;
>   
>   	atsha204a_sleep(dev);
> +
> +	sprintf(serial, "%08X%08X", be32_to_cpu(version_num), be32_to_cpu(serial_num));
> +	env_set("serial#", serial);
> +
>   	return 0;
>   }
> diff --git a/board/CZ.NIC/turris_atsha_otp.h b/board/CZ.NIC/turris_atsha_otp.h
> index bd4308fdc3ef..2cfe20bbc3a7 100644
> --- a/board/CZ.NIC/turris_atsha_otp.h
> +++ b/board/CZ.NIC/turris_atsha_otp.h
> @@ -4,6 +4,7 @@
>   #define TURRIS_ATSHA_OTP_H
>   
>   int turris_atsha_otp_init_mac_addresses(int first_idx);
> -int turris_atsha_otp_get_serial_number(u32 *version_num, u32 *serial_num);
> +int turris_atsha_otp_init_serial_number(void);
> +int turris_atsha_otp_get_serial_number(char serial[17]);
>   
>   #endif
> diff --git a/board/CZ.NIC/turris_omnia/turris_omnia.c b/board/CZ.NIC/turris_omnia/turris_omnia.c
> index ab5061ef582b..cf8a6026702b 100644
> --- a/board/CZ.NIC/turris_omnia/turris_omnia.c
> +++ b/board/CZ.NIC/turris_omnia/turris_omnia.c
> @@ -963,19 +963,15 @@ int board_late_init(void)
>   
>   int show_board_info(void)
>   {
> -	u32 version_num, serial_num;
> +	char serial[17];
>   	int err;
>   
> -	err = turris_atsha_otp_get_serial_number(&version_num, &serial_num);
> +	err = turris_atsha_otp_get_serial_number(serial);
>   	printf("Model: Turris Omnia\n");
>   	printf("  MCU type: %s\n", omnia_get_mcu_type());
>   	printf("  MCU version: %s\n", omnia_get_mcu_version());
>   	printf("  RAM size: %i MiB\n", omnia_get_ram_size_gb() * 1024);
> -	if (err)
> -		printf("  Serial Number: unknown\n");
> -	else
> -		printf("  Serial Number: %08X%08X\n", be32_to_cpu(version_num),
> -		       be32_to_cpu(serial_num));
> +	printf("  Serial Number: %s\n", !err ? serial : "unknown");
>   
>   	return 0;
>   }
> @@ -983,6 +979,7 @@ int show_board_info(void)
>   int misc_init_r(void)
>   {
>   	turris_atsha_otp_init_mac_addresses(1);
> +	turris_atsha_otp_init_serial_number();
>   	return 0;
>   }
>   

Viele Grüße,
Stefan Roese

-- 
DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-51 Fax: (+49)-8142-66989-80 Email: sr@denx.de

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] board: turris: Initialize serial# env
  2022-08-27 18:06 [PATCH] board: turris: Initialize serial# env Pali Rohár
  2022-08-28  3:48 ` Marek Behún
  2022-08-30 12:26 ` Stefan Roese
@ 2022-09-13  6:59 ` Stefan Roese
  2 siblings, 0 replies; 4+ messages in thread
From: Stefan Roese @ 2022-09-13  6:59 UTC (permalink / raw)
  To: Pali Rohár, Marek Behún; +Cc: u-boot

On 27.08.22 20:06, Pali Rohár wrote:
> Store serial number from atsha cryptochip into the serial# env variable.
> U-Boot automatically puts content of this variable into the root device
> tree property serial-number when booting Linux kernel. Refactor turris
> atsha code and from turris_atsha_otp_get_serial_number() function returns
> directly string suitable for printing or storing into device tree. Because
> during different boot stages is env storage read-only, it is not possible
> to always store serial number into env storage. So introduce a new function
> turris_atsha_otp_init_serial_number() which is called at later stage and
> which ensures that serial number is correctly stored into env.
> 
> Signed-off-by: Pali Rohár <pali@kernel.org>

Applied to u-boot-marvell/master

Thanks,
Stefan

> ---
>   board/CZ.NIC/turris_atsha_otp.c          | 33 +++++++++++++++++++++---
>   board/CZ.NIC/turris_atsha_otp.h          |  3 ++-
>   board/CZ.NIC/turris_omnia/turris_omnia.c | 11 +++-----
>   3 files changed, 36 insertions(+), 11 deletions(-)
> 
> diff --git a/board/CZ.NIC/turris_atsha_otp.c b/board/CZ.NIC/turris_atsha_otp.c
> index aa4e29b1560e..a29fe3623174 100644
> --- a/board/CZ.NIC/turris_atsha_otp.c
> +++ b/board/CZ.NIC/turris_atsha_otp.c
> @@ -93,30 +93,57 @@ int turris_atsha_otp_init_mac_addresses(int first_idx)
>   	return 0;
>   }
>   
> -int turris_atsha_otp_get_serial_number(u32 *version_num, u32 *serial_num)
> +int turris_atsha_otp_init_serial_number(void)
> +{
> +	char serial[17];
> +	int ret;
> +
> +	ret = turris_atsha_otp_get_serial_number(serial);
> +	if (ret)
> +		return ret;
> +
> +	if (!env_get("serial#"))
> +		return -1;
> +
> +	return 0;
> +}
> +
> +int turris_atsha_otp_get_serial_number(char serial[17])
>   {
>   	struct udevice *dev = get_atsha204a_dev();
> +	u32 version_num, serial_num;
> +	const char *serial_env;
>   	int ret;
>   
>   	if (!dev)
>   		return -1;
>   
> +	serial_env = env_get("serial#");
> +	if (serial_env && strlen(serial_env) == 16) {
> +		memcpy(serial, serial_env, 17);
> +		return 0;
> +	}
> +
>   	ret = atsha204a_wakeup(dev);
>   	if (ret)
>   		return ret;
>   
>   	ret = atsha204a_read(dev, ATSHA204A_ZONE_OTP, false,
>   			     TURRIS_ATSHA_OTP_VERSION,
> -			     (u8 *)version_num);
> +			     (u8 *)&version_num);
>   	if (ret)
>   		return ret;
>   
>   	ret = atsha204a_read(dev, ATSHA204A_ZONE_OTP, false,
>   			     TURRIS_ATSHA_OTP_SERIAL,
> -			     (u8 *)serial_num);
> +			     (u8 *)&serial_num);
>   	if (ret)
>   		return ret;
>   
>   	atsha204a_sleep(dev);
> +
> +	sprintf(serial, "%08X%08X", be32_to_cpu(version_num), be32_to_cpu(serial_num));
> +	env_set("serial#", serial);
> +
>   	return 0;
>   }
> diff --git a/board/CZ.NIC/turris_atsha_otp.h b/board/CZ.NIC/turris_atsha_otp.h
> index bd4308fdc3ef..2cfe20bbc3a7 100644
> --- a/board/CZ.NIC/turris_atsha_otp.h
> +++ b/board/CZ.NIC/turris_atsha_otp.h
> @@ -4,6 +4,7 @@
>   #define TURRIS_ATSHA_OTP_H
>   
>   int turris_atsha_otp_init_mac_addresses(int first_idx);
> -int turris_atsha_otp_get_serial_number(u32 *version_num, u32 *serial_num);
> +int turris_atsha_otp_init_serial_number(void);
> +int turris_atsha_otp_get_serial_number(char serial[17]);
>   
>   #endif
> diff --git a/board/CZ.NIC/turris_omnia/turris_omnia.c b/board/CZ.NIC/turris_omnia/turris_omnia.c
> index ab5061ef582b..cf8a6026702b 100644
> --- a/board/CZ.NIC/turris_omnia/turris_omnia.c
> +++ b/board/CZ.NIC/turris_omnia/turris_omnia.c
> @@ -963,19 +963,15 @@ int board_late_init(void)
>   
>   int show_board_info(void)
>   {
> -	u32 version_num, serial_num;
> +	char serial[17];
>   	int err;
>   
> -	err = turris_atsha_otp_get_serial_number(&version_num, &serial_num);
> +	err = turris_atsha_otp_get_serial_number(serial);
>   	printf("Model: Turris Omnia\n");
>   	printf("  MCU type: %s\n", omnia_get_mcu_type());
>   	printf("  MCU version: %s\n", omnia_get_mcu_version());
>   	printf("  RAM size: %i MiB\n", omnia_get_ram_size_gb() * 1024);
> -	if (err)
> -		printf("  Serial Number: unknown\n");
> -	else
> -		printf("  Serial Number: %08X%08X\n", be32_to_cpu(version_num),
> -		       be32_to_cpu(serial_num));
> +	printf("  Serial Number: %s\n", !err ? serial : "unknown");
>   
>   	return 0;
>   }
> @@ -983,6 +979,7 @@ int show_board_info(void)
>   int misc_init_r(void)
>   {
>   	turris_atsha_otp_init_mac_addresses(1);
> +	turris_atsha_otp_init_serial_number();
>   	return 0;
>   }
>   

Viele Grüße,
Stefan Roese

-- 
DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-51 Fax: (+49)-8142-66989-80 Email: sr@denx.de

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-09-13  6:59 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-27 18:06 [PATCH] board: turris: Initialize serial# env Pali Rohár
2022-08-28  3:48 ` Marek Behún
2022-08-30 12:26 ` Stefan Roese
2022-09-13  6:59 ` Stefan Roese

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.