Skip to contentSkip to conversation

Random Number Generator fixes

PATCH Superseded Weizhao Ouyang 3 participants 11 messages Jan 25, 2024 – Jan 29, 2024 Secure monitor call(SMC) calling convention (SMCCC)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover Random Number Generator fixes

This series aim to fix smccc bind issue and add a list command for RNG devices.

Weizhao Ouyang (3):
firmware: psci: Fix bind_smccc_features psci check
driver: rng: Fix SMCCC TRNG crash
cmd: rng: Add rng list command

 cmd/rng.c                 | 15 +++++++++++++++
 drivers/firmware/psci.c   |  2 +-
 drivers/rng/smccc_trng.c  |  2 +-
 include/linux/arm-smccc.h |  1 +
 4 files changed, 18 insertions(+), 2 deletions(-)
Signature · 1 line
2.39.2
#1 lore raw

PATCH 1/3 firmware: psci: Fix bind_smccc_features psci check

According to PSCI specification DEN0022F, PSCI_FEATURES is used to check whether the SMCCC is implemented by discovering SMCCC_VERSION.

---
 drivers/firmware/psci.c   | 2 +-
 include/linux/arm-smccc.h | 1 +
 2 files changed, 2 insertions(+), 1 deletion(-)
drivers/firmware/psci.c +1 -1
index c6b9efab41..894be8128d 100644
@@ -135,7 +135,7 @@ static int bind_smccc_features(struct udevice *dev, int psci_method)
 	    PSCI_VERSION_MAJOR(psci_0_2_get_version()) == 0)
 		return 0;
 
-	if (request_psci_features(ARM_SMCCC_ARCH_FEATURES) ==
+	if (request_psci_features(ARM_SMCCC_VERSION) ==
 	    PSCI_RET_NOT_SUPPORTED)
 		return 0;
 
include/linux/arm-smccc.h +1 -0
index f44e9e8f93..453017eb5f 100644
@@ -55,6 +55,7 @@
 #define ARM_SMCCC_QUIRK_NONE		0
 #define ARM_SMCCC_QUIRK_QCOM_A6		1 /* Save/restore register a6 */
 
+#define ARM_SMCCC_VERSION		0x80000000
 #define ARM_SMCCC_ARCH_FEATURES		0x80000001
 
 #define ARM_SMCCC_RET_NOT_SUPPORTED	((unsigned long)-1)
Signature · 1 line
2.39.2
Acked-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] firmware: psci: Fix bind_smccc_features psci check

Quoted 4 lines · Weizhao Ouyang

According to PSCI specification DEN0022F, PSCI_FEATURES is used to check whether the SMCCC is implemented by discovering SMCCC_VERSION.

5.15.2 Implementation responsibilities

PSCI_FEATURES should report the presence of SMCCC_VERSION in privileged platform firmware compliant with PSCI v1.0 and later revisions.

Quoted 30 lines
---
  drivers/firmware/psci.c   | 2 +-
  include/linux/arm-smccc.h | 1 +
  2 files changed, 2 insertions(+), 1 deletion(-)
drivers/firmware/psci.c +1 -1
index c6b9efab41..894be8128d 100644
@@ -135,7 +135,7 @@ static int bind_smccc_features(struct udevice *dev, int psci_method)
  	    PSCI_VERSION_MAJOR(psci_0_2_get_version()) == 0)
  		return 0;
 
-	if (request_psci_features(ARM_SMCCC_ARCH_FEATURES) ==
+	if (request_psci_features(ARM_SMCCC_VERSION) ==
  	    PSCI_RET_NOT_SUPPORTED)
  		return 0;
 
include/linux/arm-smccc.h +1 -0
index f44e9e8f93..453017eb5f 100644
@@ -55,6 +55,7 @@
  #define ARM_SMCCC_QUIRK_NONE		0
  #define ARM_SMCCC_QUIRK_QCOM_A6		1 /* Save/restore register a6 */
 
+#define ARM_SMCCC_VERSION		0x80000000
  #define ARM_SMCCC_ARCH_FEATURES		0x80000001
 
  #define ARM_SMCCC_RET_NOT_SUPPORTED	((unsigned long)-1)
#3 lore raw

Re: [PATCH 1/3] firmware: psci: Fix bind_smccc_features psci check

In reply to Weizhao Ouyang

Hi Weizhao,

Quoted 14 lines · Weizhao Ouyang

According to PSCI specification DEN0022F, PSCI_FEATURES is used to check whether the SMCCC is implemented by discovering SMCCC_VERSION.

---
 drivers/firmware/psci.c   | 2 +-
 include/linux/arm-smccc.h | 1 +
 2 files changed, 2 insertions(+), 1 deletion(-)
drivers/firmware/psci.c +0 -0
index c6b9efab41..894be8128d 100644

drivers/firmware/psci.c static int bind_smccc_features(struct udevice *dev, int psci_method) · line 140

Quoted 7 lines
drivers/firmware/psci.c +1 -1
@@ -135,7 +135,7 @@ static int bind_smccc_features(struct udevice *dev, int psci_method)
 	    PSCI_VERSION_MAJOR(psci_0_2_get_version()) == 0)
 		return 0;
 
-	if (request_psci_features(ARM_SMCCC_ARCH_FEATURES) ==
+	if (request_psci_features(ARM_SMCCC_VERSION) ==
 	    PSCI_RET_NOT_SUPPORTED)
 		return 0;

I suggest we add checks on the SMCCC version like this:

if SMCCC_VERSION >= 1.1
assume SMCCC_ARCH_FEATURES is supported discover arch features and bind drivers/devices
else
error

For more details [1][2].

[1]: SMC Calling Convention 1.5 (https://developer.arm.com/documentation/den0028/latest/)

SMCCC_ARCH_FEATURES MANDATORY from SMCCC v1.1

The implementation of this function can be detected by checking the SMCCC version. This function is mandatory if SMCCC_VERSION indicates that version 1.1 or later is implemented.

[2]: Linux checks SMCCC v1.1: https://elixir.bootlin.com/linux/latest/source/drivers/firmware/psci/psci.c#L597

Cheers,
Abdellatif

#4 lore raw

Re: [PATCH 1/3] firmware: psci: Fix bind_smccc_features psci check

Hi Abdellatif,

Quoted 28 lines · Abdellatif El Khlifi

Hi Weizhao,

Quoted 14 lines · Weizhao Ouyang

According to PSCI specification DEN0022F, PSCI_FEATURES is used to check whether the SMCCC is implemented by discovering SMCCC_VERSION.

---
 drivers/firmware/psci.c   | 2 +-
 include/linux/arm-smccc.h | 1 +
 2 files changed, 2 insertions(+), 1 deletion(-)
drivers/firmware/psci.c +0 -0
index c6b9efab41..894be8128d 100644

drivers/firmware/psci.c static int bind_smccc_features(struct udevice *dev, int psci_method) · line 140

Quoted 7 lines
drivers/firmware/psci.c +1 -1
@@ -135,7 +135,7 @@ static int bind_smccc_features(struct udevice *dev, int psci_method)
          PSCI_VERSION_MAJOR(psci_0_2_get_version()) == 0)
              return 0;
 
-     if (request_psci_features(ARM_SMCCC_ARCH_FEATURES) ==
+     if (request_psci_features(ARM_SMCCC_VERSION) ==
          PSCI_RET_NOT_SUPPORTED)
              return 0;

I suggest we add checks on the SMCCC version like this:

Quoted 5 lines

if SMCCC_VERSION >= 1.1
assume SMCCC_ARCH_FEATURES is supported discover arch features and bind drivers/devices
else
error

Okay, I will add it.

BR,
Weizhao

Quoted 15 lines

For more details [1][2].

[1]: SMC Calling Convention 1.5 (https://developer.arm.com/documentation/den0028/latest/)

SMCCC_ARCH_FEATURES MANDATORY from SMCCC v1.1

The implementation of this function can be detected by checking the SMCCC version. This function is mandatory if SMCCC_VERSION indicates that version 1.1 or later is implemented.

[2]: Linux checks SMCCC v1.1: https://elixir.bootlin.com/linux/latest/source/drivers/firmware/psci/psci.c#L597

Cheers,
Abdellatif

#5 lore raw

PATCH 2/3 driver: rng: Fix SMCCC TRNG crash

In reply to Weizhao Ouyang

Fix a SMCCC TRNG null pointer crash due to a failed smccc feature binding.

---
 drivers/rng/smccc_trng.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/rng/smccc_trng.c +1 -1
index 3a4bb33941..3087cb991a 100644
@@ -166,7 +166,7 @@ static int smccc_trng_probe(struct udevice *dev)
 	struct smccc_trng_priv *priv = dev_get_priv(dev);
 	struct arm_smccc_res res;
 
-	if (!(smccc_trng_is_supported(smccc->invoke_fn)))
+	if (!smccc || !(smccc_trng_is_supported(smccc->invoke_fn)))
 		return -ENODEV;
 
 	/* At least one of 64bit and 32bit interfaces is available */
Signature · 1 line
2.39.2
Reviewed-by on v1 2/3
#6 lore raw

Re: [PATCH 2/3] driver: rng: Fix SMCCC TRNG crash

Quoted 4 lines · Weizhao Ouyang

Fix a SMCCC TRNG null pointer crash due to a failed smccc feature binding.

Quoted 17 lines
---
  drivers/rng/smccc_trng.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/rng/smccc_trng.c +1 -1
index 3a4bb33941..3087cb991a 100644
@@ -166,7 +166,7 @@ static int smccc_trng_probe(struct udevice *dev)
  	struct smccc_trng_priv *priv = dev_get_priv(dev);
  	struct arm_smccc_res res;
 
-	if (!(smccc_trng_is_supported(smccc->invoke_fn)))
+	if (!smccc || !(smccc_trng_is_supported(smccc->invoke_fn)))
  		return -ENODEV;
 
  	/* At least one of 64bit and 32bit interfaces is available */
Reviewed-by on v1 2/3
#7 lore raw

Re: [PATCH 2/3] driver: rng: Fix SMCCC TRNG crash

In reply to Weizhao Ouyang
Quoted 4 lines · Weizhao Ouyang

Fix a SMCCC TRNG null pointer crash due to a failed smccc feature binding.

Quoted 17 lines
---
  drivers/rng/smccc_trng.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/rng/smccc_trng.c +1 -1
index 3a4bb33941..3087cb991a 100644
@@ -166,7 +166,7 @@ static int smccc_trng_probe(struct udevice *dev)
  	struct smccc_trng_priv *priv = dev_get_priv(dev);
  	struct arm_smccc_res res;
 
-	if (!(smccc_trng_is_supported(smccc->invoke_fn)))
+	if (!smccc || !(smccc_trng_is_supported(smccc->invoke_fn)))
  		return -ENODEV;
 
  	/* At least one of 64bit and 32bit interfaces is available */
#8 lore raw

PATCH 3/3 cmd: rng: Add rng list command

In reply to Weizhao Ouyang

Add rng list command to list all probed RNG device.

---
 cmd/rng.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
cmd/rng.c +15 -0
index 52f722c7af..4818133f94 100644
@@ -18,6 +18,19 @@ static int do_rng(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
 	int devnum;
 	struct udevice *dev;
 	int ret = CMD_RET_SUCCESS;
+	int idx;
+
+	if (argc == 2 && !strcmp(argv[1], "list")) {
+		uclass_foreach_dev_probe(UCLASS_RNG, dev) {
+			printf("RNG #%d - %s\n", dev->seq_, dev->name);
+		}
+
+		if (!idx) {
+			printf("*** no RNG devices available ***\n");
+			return CMD_RET_FAILURE;
+		}
+		return CMD_RET_SUCCESS;
+	}
 
 	switch (argc) {
 	case 1:
@@ -57,6 +70,8 @@ static int do_rng(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
 }
 
 U_BOOT_LONGHELP(rng,
+	"[list]\n"
+	"  - list all the probe rng device\n"
 	"[dev [n]]\n"
 	"  - print n random bytes(max 64) read from dev\n");
 
Signature · 1 line
2.39.2
#9 lore raw

Re: [PATCH 3/3] cmd: rng: Add rng list command

Quoted 1 line · Weizhao Ouyang

Add rng list command to list all probed RNG device.

Thank you for your contribution.

Would the following be more accurate?

The 'rng list' command probes all RNG devices and list those devices that are successfully probed.

Quoted 11 lines
---
  cmd/rng.c | 15 +++++++++++++++
  1 file changed, 15 insertions(+)
cmd/rng.c +0 -0
index 52f722c7af..4818133f94 100644

cmd/rng.c static int do_rng(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) · line 21

Quoted 4 lines
cmd/rng.c +1 -0
@@ -18,6 +18,19 @@ static int do_rng(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
  	int devnum;
  	struct udevice *dev;
  	int ret = CMD_RET_SUCCESS;
+	int idx;

Please, put the definition into the code block where it is used.

cmd/rng.c static int do_rng(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) · line 23

Quoted 2 lines
cmd/rng.c +2 -0
+
+	if (argc == 2 && !strcmp(argv[1], "list")) {

int idx = 0;

cmd/rng.c static int do_rng(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) · line 25

Quoted 2 lines
cmd/rng.c +2 -0
+		uclass_foreach_dev_probe(UCLASS_RNG, dev) {
+			printf("RNG #%d - %s\n", dev->seq_, dev->name);

++idx;

cmd/rng.c static int do_rng(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) · line 29

Quoted 4 lines
cmd/rng.c +4 -0
+		}
+
+		if (!idx) {
+			printf("*** no RNG devices available ***\n");

If idx is not initialized, depending on the random value of idx on the stack you get a printout or not, e.g.

=> rng list
RNG #0 - riscv_zkr
RNG #1 - virtio-rng#8
*** no RNG devices available ***

Do we need the message to be so noisy? How about

log_err("No RNG device\n");

cmd/rng.c static int do_rng(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) · line 31

Quoted 2 lines
cmd/rng.c +2 -0
+			return CMD_RET_FAILURE;
+		}

We prefer having an empty line before return statements.

Best regards

Heinrich

Quoted 14 lines
cmd/rng.c +4 -0
+		return CMD_RET_SUCCESS;
+	}
 
  	switch (argc) {
  	case 1:
@@ -57,6 +70,8 @@ static int do_rng(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
  }
 
  U_BOOT_LONGHELP(rng,
+	"[list]\n"
+	"  - list all the probe rng device\n"
  	"[dev [n]]\n"
  	"  - print n random bytes(max 64) read from dev\n");
 
#10 lore raw

Re: [PATCH 3/3] cmd: rng: Add rng list command

Quoted 10 lines · Heinrich Schuchardt
Quoted 1 line · Weizhao Ouyang

Add rng list command to list all probed RNG device.

Thank you for your contribution.

Would the following be more accurate?

The 'rng list' command probes all RNG devices and list those devices that are successfully probed.

Yeah.

Quoted 7 lines
Quoted 7 lines
---
  cmd/rng.c | 15 +++++++++++++++
  1 file changed, 15 insertions(+)
Quoted 11 lines

cmd/rng.c static int do_rng(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) · line 21

Quoted 9 lines
cmd/rng.c +1 -0
index 52f722c7af..4818133f94 100644
@@ -18,6 +18,19 @@ static int do_rng(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
      int devnum;
      struct udevice *dev;
      int ret = CMD_RET_SUCCESS;
+     int idx;

Please, put the definition into the code block where it is used.

Sorry I imported the draft code, will remove it in the next version.

Quoted 46 lines

cmd/rng.c static int do_rng(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) · line 23

Quoted 3 lines
cmd/rng.c +2 -0
+
+     if (argc == 2 && !strcmp(argv[1], "list")) {

int idx = 0;

cmd/rng.c static int do_rng(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) · line 25

Quoted 2 lines
cmd/rng.c +2 -0
+             uclass_foreach_dev_probe(UCLASS_RNG, dev) {
+                     printf("RNG #%d - %s\n", dev->seq_, dev->name);

++idx;

cmd/rng.c static int do_rng(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) · line 29

Quoted 4 lines
cmd/rng.c +4 -0
+             }
+
+             if (!idx) {
+                     printf("*** no RNG devices available ***\n");

If idx is not initialized, depending on the random value of idx on the stack you get a printout or not, e.g.

=> rng list
RNG #0 - riscv_zkr
RNG #1 - virtio-rng#8
*** no RNG devices available ***

Do we need the message to be so noisy? How about

log_err("No RNG device\n");

cmd/rng.c static int do_rng(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) · line 31

Quoted 2 lines
cmd/rng.c +2 -0
+                     return CMD_RET_FAILURE;
+             }

We prefer having an empty line before return statements.

Best regards

Heinrich

Quoted 8 lines
cmd/rng.c +2 -0
+             return CMD_RET_SUCCESS;
+     }
 
      switch (argc) {
      case 1:
@@ -57,6 +70,8 @@ static int do_rng(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
  }
 
Quoted 7 lines

cmd/rng.c static int do_rng(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) · line 77

Quoted 6 lines
cmd/rng.c +2 -0
  U_BOOT_LONGHELP(rng,
+     "[list]\n"
+     "  - list all the probe rng device\n"
      "[dev [n]]\n"
      "  - print n random bytes(max 64) read from dev\n");
 

BR,
Weizhao