Skip to contentSkip to conversation

tpm: Attempt to return value not in enum

PATCH In review Andrew Goodbody 2 participants 4 messages Jun 30, 2025 Library code

lore
Share & tools
lore
#0 lore raw

v3 cover tpm: Attempt to return value not in enum

The function tpm2_name_to_algorithm attempts to return -EINVAL for an error but that is not included in the enum tpm2_algorithms which is defined as the return type for this function. Add -EINVAL as TPM2_ALG_INVAL to the enum so that it can be returned as desired. Update the code to use this enum value.

---
Changes in v3:
- Rework first patch to have the header file include errno.h instead of the individual files
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Add patch to make use of new enum value TPM2_ALG_INVAL
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Andrew Goodbody (2):
cmd: tpm: Fix attempt to return value not in enum
tpm: Make use of TPM2_ALG_INVAL from enum

 cmd/tpm-v2.c     | 6 +++---
 include/tpm-v2.h | 2 ++
 lib/tpm-v2.c     | 2 +-
 3 files changed, 6 insertions(+), 4 deletions(-)

---
base-commit: 903eb123236ccbd8ef05d43507a2a910b785bd56
change-id: 20250625-tpm_enum-098ee9a5eb6b

Best regards,

Signature · 1 line
Andrew Goodbody <[email protected]>
#1 lore raw

PATCH v3 1/2 cmd: tpm: Fix attempt to return value not in enum

The function tpm2_name_to_algorithm is defined as returning an enum for the algorithm specified but it also attempts to return an error on failure, but that error is not included in the enum. Add the error to the enum so that it can be returned.

This issue was reported by Smatch.

---
 include/tpm-v2.h | 2 ++
 1 file changed, 2 insertions(+)
include/tpm-v2.h +2 -0
index ece422df0c7..f3eb2ef5643 100644
@@ -20,6 +20,7 @@
 #define __TPM_V2_H
 
 #include <tpm-common.h>
+#include <linux/errno.h>
 
 struct udevice;
 
@@ -266,6 +267,7 @@ enum tpm2_return_codes {
  * TPM2 algorithms.
  */
 enum tpm2_algorithms {
+	TPM2_ALG_INVAL		= -EINVAL,
 	TPM2_ALG_SHA1		= 0x04,
 	TPM2_ALG_XOR		= 0x0A,
 	TPM2_ALG_SHA256		= 0x0B,
Signature · 1 line
2.39.5
Reviewed-by on v3 1/2
#2 lore raw

Re: [PATCH v3 1/2] cmd: tpm: Fix attempt to return value not in enum

Thanks Andrew

Quoted 9 lines · Andrew Goodbody

The function tpm2_name_to_algorithm is defined as returning an enum for the algorithm specified but it also attempts to return an error on failure, but that error is not included in the enum. Add the error to the enum so that it can be returned.

This issue was reported by Smatch.

Quoted 28 lines
---
 include/tpm-v2.h | 2 ++
 1 file changed, 2 insertions(+)
include/tpm-v2.h +2 -1
index ece422df0c7..f3eb2ef5643 100644
@@ -20,6 +20,7 @@
 #define __TPM_V2_H
 
 #include <tpm-common.h>
+#include <linux/errno.h>
 
 struct udevice;
 
@@ -266,6 +267,7 @@ enum tpm2_return_codes {
  * TPM2 algorithms.
  */
 enum tpm2_algorithms {
+       TPM2_ALG_INVAL          = -EINVAL,
        TPM2_ALG_SHA1           = 0x04,
        TPM2_ALG_XOR            = 0x0A,
        TPM2_ALG_SHA256         = 0x0B,
 
--

2.39.5

#3 lore raw

PATCH v3 2/2 tpm: Make use of TPM2_ALG_INVAL from enum

In reply to Andrew Goodbody

Now that the enum includes TPM2_ALG_INVAL, use that name in the code.

---
 cmd/tpm-v2.c | 6 +++---
 lib/tpm-v2.c | 2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)
cmd/tpm-v2.c +3 -3
index a62862e94f9..346e21d27bb 100644
@@ -113,7 +113,7 @@ static int do_tpm2_pcr_extend(struct cmd_tbl *cmdtp, int flag, int argc,
 		return CMD_RET_USAGE;
 	if (argc == 4) {
 		algo = tpm2_name_to_algorithm(argv[3]);
-		if (algo < 0)
+		if (algo == TPM2_ALG_INVAL)
 			return CMD_RET_FAILURE;
 	}
 	algo_len = tpm2_algorithm_to_len(algo);
@@ -157,7 +157,7 @@ static int do_tpm_pcr_read(struct cmd_tbl *cmdtp, int flag, int argc,
 		return CMD_RET_USAGE;
 	if (argc == 4) {
 		algo = tpm2_name_to_algorithm(argv[3]);
-		if (algo < 0)
+		if (algo == TPM2_ALG_INVAL)
 			return CMD_RET_FAILURE;
 	}
 	algo_len = tpm2_algorithm_to_len(algo);
@@ -288,7 +288,7 @@ static int do_tpm2_pcrallocate(struct cmd_tbl *cmdtp, int flag, int argc,
 		return CMD_RET_USAGE;
 
 	algo = tpm2_name_to_algorithm(argv[1]);
-	if (algo == -EINVAL)
+	if (algo == TPM2_ALG_INVAL)
 		return CMD_RET_USAGE;
 
 	ret = get_tpm(&dev);
lib/tpm-v2.c +1 -1
index 9ca7933c094..5b21c57ae42 100644
@@ -1141,7 +1141,7 @@ enum tpm2_algorithms tpm2_name_to_algorithm(const char *name)
 	}
 	printf("%s: unsupported algorithm %s\n", __func__, name);
 
-	return -EINVAL;
+	return TPM2_ALG_INVAL;
 }
 
 const char *tpm2_algorithm_name(enum tpm2_algorithms algo)
Signature · 1 line
2.39.5