Skip to contentSkip to conversation

tpm: Separate out the TPM tests for v1 and v2

PATCH Superseded Simon Glass 2 participants 7 messages Feb 20, 2023 – Feb 21, 2023 Library code

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 tpm: Separate out the TPM tests for v1 and v2

Currently there is only one test and it only works on TPM v2. Update it to work on v1.2 as well, using a new function to pick up the required TPM.

Update sandbox to include both a v1.2 and v2 TPM so that this works. Split out the existing test into two pieces, one for init and one for the v2-only report_state feature.

---

 arch/sandbox/dts/test.dts |  4 +++
 test/dm/tpm.c             | 60 +++++++++++++++++++++++++++++++++------
 2 files changed, 55 insertions(+), 9 deletions(-)
arch/sandbox/dts/test.dts +4 -0
index 05e09128a38..d72d7a567a7 100644
@@ -1367,6 +1367,10 @@
 		compatible = "sandbox,tpm2";
 	};
 
+	tpm {
+		compatible = "google,sandbox-tpm";
+	};
+
 	uart0: serial {
 		compatible = "sandbox,serial";
 		bootph-all;
test/dm/tpm.c +51 -9

79 lines not on this page · raw message

Signature · 1 line
2.39.2.637.g21b0678d19-goog
#1 lore raw

PATCH 2/2 tpm: Implement tpm_auto_start() for TPMv1.2

Add an implementation of this, moving the common call to tpm_init() up into the common API implementation.

Add a test.

---

 include/tpm-common.h |  2 +-
 include/tpm-v1.h     | 11 +++++++++++
 lib/tpm-v1.c         | 16 ++++++++++++++++
 lib/tpm-v2.c         |  8 --------
 lib/tpm_api.c        | 19 ++++++++++++++++---
 test/dm/tpm.c        | 45 ++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 89 insertions(+), 12 deletions(-)
include/tpm-common.h +1 -1
index b2c5404430f..1ba81386ce1 100644
@@ -94,7 +94,7 @@ struct tpm_ops {
 	 * close().
 	 *
 	 * @dev:	Device to open
-	 * @return 0 ok OK, -ve on error
+	 * @return 0 ok OK, -EBUSY if already opened, other -ve on other error
 	 */
 	int (*open)(struct udevice *dev);
 
include/tpm-v1.h +11 -0
index 33d53fb695e..60b71e2a4b6 100644
@@ -591,4 +591,15 @@ u32 tpm_set_global_lock(struct udevice *dev);
  */
 u32 tpm1_resume(struct udevice *dev);
 
+/**
+ * tpm1_auto_start() - start up the TPM
+ *
+ * This does not do a self test.
+ *
+ * @dev		TPM device
+ * Return: TPM2_RC_SUCCESS, on success, or when the TPM returns
+ * TPM_INVALID_POSTINIT; TPM_FAILEDSELFTEST, if the TPM is in failure state
+ */
+u32 tpm1_auto_start(struct udevice *dev);
+
 #endif /* __TPM_V1_H */
lib/tpm-v1.c +16 -0
index d0e3ab1b21d..ea3833549bc 100644
@@ -69,6 +69,22 @@ u32 tpm1_continue_self_test(struct udevice *dev)
 	return tpm_sendrecv_command(dev, command, NULL, NULL);
 }
 
+u32 tpm1_auto_start(struct udevice *dev)
+{
+	u32 rc;
+
+	rc = tpm1_startup(dev, TPM_ST_CLEAR);
+	/* continue on if the TPM is already inited */
+	if (rc && rc != TPM_INVALID_POSTINIT)
+		return rc;
+
+	rc = tpm1_self_test_full(dev);
+	if (rc)
+		return rc;
+
+	return rc;
+}
+
 u32 tpm1_clear_and_reenable(struct udevice *dev)
 {
 	u32 ret;
lib/tpm-v2.c +0 -8
index 895b093bcb1..9ab5b46df17 100644
@@ -48,14 +48,6 @@ u32 tpm2_auto_start(struct udevice *dev)
 {
 	u32 rc;
 
-	/*
-	 * the tpm_init() will return -EBUSY if the init has already happened
-	 * The selftest and startup code can run multiple times with no side
-	 * effects
-	 */
-	rc = tpm_init(dev);
-	if (rc && rc != -EBUSY)
-		return rc;
 	rc = tpm2_self_test(dev, TPMI_YES);
 
 	if (rc == TPM2_RC_INITIALIZE) {
lib/tpm_api.c +16 -3
index 5b2c11a277c..3ef5e811794 100644
@@ -37,10 +37,23 @@ u32 tpm_startup(struct udevice *dev, enum tpm_startup_type mode)
 
 u32 tpm_auto_start(struct udevice *dev)
 {
-	if (tpm_is_v2(dev))
-		return tpm2_auto_start(dev);
+	u32 rc;
 
-	return -ENOSYS;
+	/*
+	 * the tpm_init() will return -EBUSY if the init has already happened
+	 * The selftest and startup code can run multiple times with no side
+	 * effects
+	 */
+	rc = tpm_init(dev);
+	if (rc && rc != -EBUSY)
+		return rc;
+
+	if (tpm_is_v1(dev))
+		return tpm1_auto_start(dev);
+	else if (tpm_is_v2(dev))
+		return tpm2_auto_start(dev);
+	else
+		return -ENOSYS;
 }
 
 u32 tpm_resume(struct udevice *dev)
test/dm/tpm.c +45 -0

48 lines not on this page · raw message

Signature · 1 line
2.39.2.637.g21b0678d19-goog
Reviewed-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] tpm: Implement tpm_auto_start() for TPMv1.2

Hi Simon,

Quoted 59 lines · Simon Glass

Add an implementation of this, moving the common call to tpm_init() up into the common API implementation.

Add a test.

---

 include/tpm-common.h |  2 +-
 include/tpm-v1.h     | 11 +++++++++++
 lib/tpm-v1.c         | 16 ++++++++++++++++
 lib/tpm-v2.c         |  8 --------
 lib/tpm_api.c        | 19 ++++++++++++++++---
 test/dm/tpm.c        | 45 ++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 89 insertions(+), 12 deletions(-)
include/tpm-common.h +1 -1
index b2c5404430f..1ba81386ce1 100644
@@ -94,7 +94,7 @@ struct tpm_ops {
 	 * close().
 	 *
 	 * @dev:	Device to open
-	 * @return 0 ok OK, -ve on error
+	 * @return 0 ok OK, -EBUSY if already opened, other -ve on other error
 	 */
 	int (*open)(struct udevice *dev);
 
include/tpm-v1.h +11 -0
index 33d53fb695e..60b71e2a4b6 100644
@@ -591,4 +591,15 @@ u32 tpm_set_global_lock(struct udevice *dev);
  */
 u32 tpm1_resume(struct udevice *dev);
 
+/**
+ * tpm1_auto_start() - start up the TPM
+ *
+ * This does not do a self test.
+ *
+ * @dev		TPM device
+ * Return: TPM2_RC_SUCCESS, on success, or when the TPM returns
+ * TPM_INVALID_POSTINIT; TPM_FAILEDSELFTEST, if the TPM is in failure state
+ */
+u32 tpm1_auto_start(struct udevice *dev);
+
 #endif /* __TPM_V1_H */
lib/tpm-v1.c +2 -0
index d0e3ab1b21d..ea3833549bc 100644
@@ -69,6 +69,22 @@ u32 tpm1_continue_self_test(struct udevice *dev)
 	return tpm_sendrecv_command(dev, command, NULL, NULL);
 }
 
+u32 tpm1_auto_start(struct udevice *dev)
+{

lib/tpm-v1.c u32 tpm1_continue_self_test(struct udevice *dev) · line 83

Quoted 10 lines
lib/tpm-v1.c +10 -0
+	u32 rc;
+
+	rc = tpm1_startup(dev, TPM_ST_CLEAR);
+	/* continue on if the TPM is already inited */
+	if (rc && rc != TPM_INVALID_POSTINIT)
+		return rc;
+
+	rc = tpm1_self_test_full(dev);
+	if (rc)
+		return rc;

Mind if I remove this if before merging?

Quoted 5 lines
lib/tpm-v1.c +4 -0
+
+	return rc;
+}
+
 {

[...]

Quoted 7 lines
lib/tpm-v1.c +4 -1
+
+	return 0;
+}
+DM_TEST(dm_test_tpm_autostart_reinit, UT_TESTF_SCAN_FDT);
--

2.39.2.637.g21b0678d19-goog

Other than that

#3 lore raw

Re: [PATCH 2/2] tpm: Implement tpm_auto_start() for TPMv1.2

Hi Ilias,

Quoted 61 lines · Ilias Apalodimas

Hi Simon,

Quoted 57 lines · Simon Glass

Add an implementation of this, moving the common call to tpm_init() up into the common API implementation.

Add a test.

---

 include/tpm-common.h |  2 +-
 include/tpm-v1.h     | 11 +++++++++++
 lib/tpm-v1.c         | 16 ++++++++++++++++
 lib/tpm-v2.c         |  8 --------
 lib/tpm_api.c        | 19 ++++++++++++++++---
 test/dm/tpm.c        | 45 ++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 89 insertions(+), 12 deletions(-)
include/tpm-common.h +1 -1
index b2c5404430f..1ba81386ce1 100644
@@ -94,7 +94,7 @@ struct tpm_ops {
       * close().
       *
       * @dev:        Device to open
-      * @return 0 ok OK, -ve on error
+      * @return 0 ok OK, -EBUSY if already opened, other -ve on other error
       */
      int (*open)(struct udevice *dev);
 
include/tpm-v1.h +11 -0
index 33d53fb695e..60b71e2a4b6 100644
@@ -591,4 +591,15 @@ u32 tpm_set_global_lock(struct udevice *dev);
  */
 u32 tpm1_resume(struct udevice *dev);
 
+/**
+ * tpm1_auto_start() - start up the TPM
+ *
+ * This does not do a self test.
+ *
+ * @dev              TPM device
+ * Return: TPM2_RC_SUCCESS, on success, or when the TPM returns
+ * TPM_INVALID_POSTINIT; TPM_FAILEDSELFTEST, if the TPM is in failure state
+ */
+u32 tpm1_auto_start(struct udevice *dev);
+
 #endif /* __TPM_V1_H */
lib/tpm-v1.c +0 -0
index d0e3ab1b21d..ea3833549bc 100644
@@ -69,6 +69,22 @@ u32 tpm1_continue_self_test(struct udevice *dev)
      return tpm_sendrecv_command(dev, command, NULL, NULL);
 }
 
Quoted 14 lines

lib/tpm-v1.c u32 tpm1_continue_self_test(struct udevice *dev) · line 83

Quoted 12 lines
lib/tpm-v1.c +12 -0
+u32 tpm1_auto_start(struct udevice *dev)
+{
+     u32 rc;
+
+     rc = tpm1_startup(dev, TPM_ST_CLEAR);
+     /* continue on if the TPM is already inited */
+     if (rc && rc != TPM_INVALID_POSTINIT)
+             return rc;
+
+     rc = tpm1_self_test_full(dev);
+     if (rc)
+             return rc;

Mind if I remove this if before merging?

Yes that's fine.

Quoted 18 lines
Quoted 6 lines
lib/tpm-v1.c +4 -0
+
+     return rc;
+}
+
 {

[...]

Quoted 7 lines
lib/tpm-v1.c +4 -1
+
+     return 0;
+}
+DM_TEST(dm_test_tpm_autostart_reinit, UT_TESTF_SCAN_FDT);
--

2.39.2.637.g21b0678d19-goog

Other than that

Quoted 2 lines

Regards,
Simon

#4 lore raw

Re: [PATCH 2/2] tpm: Implement tpm_auto_start() for TPMv1.2

Hi Simon,

Unfortunately, this doesn't apply cleanly over
https://lore.kernel.org/u-boot/[email protected]/

I'll have a look at the conflicts, if they are minor i'll fix them up. Otherwise you'll have to respin this

Thanks
/Ilias

Quoted 106 lines · Simon Glass

Hi Ilias,

Quoted 61 lines · Ilias Apalodimas

Hi Simon,

Quoted 57 lines · Simon Glass

Add an implementation of this, moving the common call to tpm_init() up into the common API implementation.

Add a test.

---

 include/tpm-common.h |  2 +-
 include/tpm-v1.h     | 11 +++++++++++
 lib/tpm-v1.c         | 16 ++++++++++++++++
 lib/tpm-v2.c         |  8 --------
 lib/tpm_api.c        | 19 ++++++++++++++++---
 test/dm/tpm.c        | 45 ++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 89 insertions(+), 12 deletions(-)
include/tpm-common.h +1 -1
index b2c5404430f..1ba81386ce1 100644
@@ -94,7 +94,7 @@ struct tpm_ops {
       * close().
       *
       * @dev:        Device to open
-      * @return 0 ok OK, -ve on error
+      * @return 0 ok OK, -EBUSY if already opened, other -ve on other error
       */
      int (*open)(struct udevice *dev);
 
include/tpm-v1.h +11 -0
index 33d53fb695e..60b71e2a4b6 100644
@@ -591,4 +591,15 @@ u32 tpm_set_global_lock(struct udevice *dev);
  */
 u32 tpm1_resume(struct udevice *dev);
 
+/**
+ * tpm1_auto_start() - start up the TPM
+ *
+ * This does not do a self test.
+ *
+ * @dev              TPM device
+ * Return: TPM2_RC_SUCCESS, on success, or when the TPM returns
+ * TPM_INVALID_POSTINIT; TPM_FAILEDSELFTEST, if the TPM is in failure state
+ */
+u32 tpm1_auto_start(struct udevice *dev);
+
 #endif /* __TPM_V1_H */
lib/tpm-v1.c +0 -0
index d0e3ab1b21d..ea3833549bc 100644
@@ -69,6 +69,22 @@ u32 tpm1_continue_self_test(struct udevice *dev)
      return tpm_sendrecv_command(dev, command, NULL, NULL);
 }
 
Quoted 14 lines

lib/tpm-v1.c u32 tpm1_continue_self_test(struct udevice *dev) · line 83

Quoted 12 lines
lib/tpm-v1.c +12 -0
+u32 tpm1_auto_start(struct udevice *dev)
+{
+     u32 rc;
+
+     rc = tpm1_startup(dev, TPM_ST_CLEAR);
+     /* continue on if the TPM is already inited */
+     if (rc && rc != TPM_INVALID_POSTINIT)
+             return rc;
+
+     rc = tpm1_self_test_full(dev);
+     if (rc)
+             return rc;

Mind if I remove this if before merging?

Yes that's fine.

Quoted 18 lines
Quoted 6 lines
lib/tpm-v1.c +4 -0
+
+     return rc;
+}
+
 {

[...]

Quoted 7 lines
lib/tpm-v1.c +4 -1
+
+     return 0;
+}
+DM_TEST(dm_test_tpm_autostart_reinit, UT_TESTF_SCAN_FDT);
--

2.39.2.637.g21b0678d19-goog

Other than that

Quoted 2 lines

Regards,
Simon

#5 lore raw

Re: [PATCH 2/2] tpm: Implement tpm_auto_start() for TPMv1.2

Hi Ilias,

Quoted 8 lines · Ilias Apalodimas

Hi Simon,

Unfortunately, this doesn't apply cleanly over
https://lore.kernel.org/u-boot/[email protected]/

I'll have a look at the conflicts, if they are minor i'll fix them up. Otherwise you'll have to respin this

That's strange. I just tried again with going to the patch [1] and downloading the series. I am using -next though...could that be why?

Regards,
Simon

[1] https://patchwork.ozlabs.org/project/uboot/patch/[email protected]/

Quoted 111 lines

Thanks
/Ilias

Quoted 106 lines · Simon Glass

Hi Ilias,

Quoted 61 lines · Ilias Apalodimas

Hi Simon,

Quoted 57 lines · Simon Glass

Add an implementation of this, moving the common call to tpm_init() up into the common API implementation.

Add a test.

---

 include/tpm-common.h |  2 +-
 include/tpm-v1.h     | 11 +++++++++++
 lib/tpm-v1.c         | 16 ++++++++++++++++
 lib/tpm-v2.c         |  8 --------
 lib/tpm_api.c        | 19 ++++++++++++++++---
 test/dm/tpm.c        | 45 ++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 89 insertions(+), 12 deletions(-)
include/tpm-common.h +1 -1
index b2c5404430f..1ba81386ce1 100644
@@ -94,7 +94,7 @@ struct tpm_ops {
       * close().
       *
       * @dev:        Device to open
-      * @return 0 ok OK, -ve on error
+      * @return 0 ok OK, -EBUSY if already opened, other -ve on other error
       */
      int (*open)(struct udevice *dev);
 
include/tpm-v1.h +11 -0
index 33d53fb695e..60b71e2a4b6 100644
@@ -591,4 +591,15 @@ u32 tpm_set_global_lock(struct udevice *dev);
  */
 u32 tpm1_resume(struct udevice *dev);
 
+/**
+ * tpm1_auto_start() - start up the TPM
+ *
+ * This does not do a self test.
+ *
+ * @dev              TPM device
+ * Return: TPM2_RC_SUCCESS, on success, or when the TPM returns
+ * TPM_INVALID_POSTINIT; TPM_FAILEDSELFTEST, if the TPM is in failure state
+ */
+u32 tpm1_auto_start(struct udevice *dev);
+
 #endif /* __TPM_V1_H */
lib/tpm-v1.c +0 -0
index d0e3ab1b21d..ea3833549bc 100644
@@ -69,6 +69,22 @@ u32 tpm1_continue_self_test(struct udevice *dev)
      return tpm_sendrecv_command(dev, command, NULL, NULL);
 }
 
Quoted 14 lines

lib/tpm-v1.c u32 tpm1_continue_self_test(struct udevice *dev) · line 83

Quoted 12 lines
lib/tpm-v1.c +12 -0
+u32 tpm1_auto_start(struct udevice *dev)
+{
+     u32 rc;
+
+     rc = tpm1_startup(dev, TPM_ST_CLEAR);
+     /* continue on if the TPM is already inited */
+     if (rc && rc != TPM_INVALID_POSTINIT)
+             return rc;
+
+     rc = tpm1_self_test_full(dev);
+     if (rc)
+             return rc;

Mind if I remove this if before merging?

Yes that's fine.

Quoted 18 lines
Quoted 6 lines
lib/tpm-v1.c +4 -0
+
+     return rc;
+}
+
 {

[...]

Quoted 7 lines
lib/tpm-v1.c +4 -1
+
+     return 0;
+}
+DM_TEST(dm_test_tpm_autostart_reinit, UT_TESTF_SCAN_FDT);
--

2.39.2.637.g21b0678d19-goog

Other than that

Quoted 2 lines

Regards,
Simon

Acked-by on v1 1/2
#6 lore raw
In reply to Simon Glass
Quoted 116 lines · Simon Glass

Currently there is only one test and it only works on TPM v2. Update it to work on v1.2 as well, using a new function to pick up the required TPM.

Update sandbox to include both a v1.2 and v2 TPM so that this works. Split out the existing test into two pieces, one for init and one for the v2-only report_state feature.

---

 arch/sandbox/dts/test.dts |  4 +++
 test/dm/tpm.c             | 60 +++++++++++++++++++++++++++++++++------
 2 files changed, 55 insertions(+), 9 deletions(-)
arch/sandbox/dts/test.dts +4 -0
index 05e09128a38..d72d7a567a7 100644
@@ -1367,6 +1367,10 @@
 		compatible = "sandbox,tpm2";
 	};
 
+	tpm {
+		compatible = "google,sandbox-tpm";
+	};
+
 	uart0: serial {
 		compatible = "sandbox,serial";
 		bootph-all;
test/dm/tpm.c +51 -10
index dca540bb561..7d880012090 100644
@@ -11,24 +11,66 @@
 #include <test/test.h>
 #include <test/ut.h>
 
-/* Basic test of the TPM uclass */
+/*
+ * get_tpm_version() - Get a TPM of the given version
+ *
+ * @version: Version to get
+ * @devp: Returns the TPM device
+ * Returns: 0 if OK, -ENODEV if not found
+ */
+static int get_tpm_version(enum tpm_version version, struct udevice **devp)
+{
+	struct udevice *dev;
+
+	/*
+	 * For now we have to probe each TPM, since the version is set up in
+	 * of_to_plat(). We could require TPMs to declare their version when
+	 * probed, to avoid this
+	 */
+	uclass_foreach_dev_probe(UCLASS_TPM, dev) {
+		if (tpm_get_version(dev) == version) {
+			*devp = dev;
+			return 0;
+		}
+	}
+
+	return -ENODEV;
+}
+
+/* Basic test of initing a TPM */
+static int test_tpm_init(struct unit_test_state *uts, enum tpm_version version)
+{
+	struct udevice *dev;
+
+	/* check probe success */
+	ut_assertok(get_tpm_version(version, &dev));
+
+	ut_assertok(tpm_init(dev));
+
+	return 0;
+}
+
 static int dm_test_tpm(struct unit_test_state *uts)
+{
+	ut_assertok(test_tpm_init(uts, TPM_V1));
+	ut_assertok(test_tpm_init(uts, TPM_V2));
+
+	return 0;
+}
+DM_TEST(dm_test_tpm, UT_TESTF_SCAN_FDT);
+
+/* Test report_state */
+static int dm_test_tpm_report_state(struct unit_test_state *uts)
 {
 	struct udevice *dev;
 	char buf[50];
 
 	/* check probe success */
-	ut_assertok(uclass_first_device_err(UCLASS_TPM, &dev));
-	ut_assert(tpm_is_v2(dev));
+	ut_assertok(get_tpm_version(TPM_V2, &dev));
 
 	ut_assert(tpm_report_state(dev, buf, sizeof(buf)));
 	ut_asserteq_str("init_done=0", buf);
 
-	ut_assertok(tpm_init(dev));
-	 /*
-	  * tpm_auto_start will rerun tpm_init, but handles the
-	  * -EBUSY return code internally.
-	  */
 	ut_assertok(tpm_auto_start(dev));
 
 	ut_assert(tpm_report_state(dev, buf, sizeof(buf)));
@@ -36,4 +78,4 @@ static int dm_test_tpm(struct unit_test_state *uts)
 
 	return 0;
 }
-DM_TEST(dm_test_tpm, UT_TESTF_SCAN_FDT);
+DM_TEST(dm_test_tpm_report_state, UT_TESTF_SCAN_FDT);
--
Quoted 2 lines

2.39.2.637.g21b0678d19-goog