Skip to contentSkip to conversation

WIP: TPM additions

PATCH In review Simon Glass 2 participants 3 messages Feb 20, 2023 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 WIP: TPM additions

---

 arch/sandbox/dts/test.dts |   4 ++
 include/tpm-v1.h          |  11 ++++
 lib/tpm-v1.c              |  11 ++++
 lib/tpm-v2.c              |   8 ---
 lib/tpm_api.c             |  19 +++++--
 test/dm/tpm.c             | 105 ++++++++++++++++++++++++++++++++++----
 6 files changed, 138 insertions(+), 20 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;
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 +11 -0
index d0e3ab1b21d..884b8ddbac9 100644
@@ -69,6 +69,17 @@ 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);
+	if (rc == TPM_INVALID_POSTINIT)
+		rc = 0;
+
+	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 +96 -9

124 lines not on this page · raw message

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

Hi Simon,

Quoted 12 lines

+ * @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
+ */

include/tpm-v1.h +2 -0
+u32 tpm1_auto_start(struct udevice *dev);
+
 #endif /* __TPM_V1_H */
lib/tpm-v1.c +0 -0
index d0e3ab1b21d..884b8ddbac9 100644

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

Quoted 10 lines
lib/tpm-v1.c +7 -0
@@ -69,6 +69,17 @@ 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);
+	if (rc == TPM_INVALID_POSTINIT)
+		rc = 0;

I am not an expert in 1.2 devices by any means, but don't you need to start auto tests here as well similarly to what 2.0 does?
Looking at the kernel code it does something along the lines of

rc= tpm1_self_test_full(dev);
if (rc == TPM_FAILEDSELFTEST) {
.....
}

Quoted 3 lines
lib/tpm-v1.c +2 -1
+
+	return rc;
-	  * -EBUSY return code internally.

[...]

Quoted 11 lines
lib/tpm-v1.c +1 -3
-	  */
 	ut_assertok(tpm_auto_start(dev));
 
 	ut_assert(tpm_report_state(dev, buf, sizeof(buf)));
@@ -36,4 +123,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

Other than that, this looks ok to me

Cheers
/Ilias

#2 lore raw

Hi Ilias,

Quoted 30 lines · Ilias Apalodimas

Hi Simon,

Quoted 12 lines

+ * @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
+ */

include/tpm-v1.h +2 -0
+u32 tpm1_auto_start(struct udevice *dev);
+
 #endif /* __TPM_V1_H */
lib/tpm-v1.c +0 -0
index d0e3ab1b21d..884b8ddbac9 100644

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

Quoted 10 lines
lib/tpm-v1.c +7 -0
@@ -69,6 +69,17 @@ 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);
+     if (rc == TPM_INVALID_POSTINIT)
+             rc = 0;

I am not an expert in 1.2 devices by any means, but don't you need to start auto tests here as well similarly to what 2.0 does?
Looking at the kernel code it does something along the lines of

Quoted 5 lines

rc= tpm1_self_test_full(dev);
if (rc == TPM_FAILEDSELFTEST) {
.....
}

Yes, if it has never been done before, e.g. in the factory. I didn't understand that you were trying to do that. I will update it. Note that this will add time.

Quoted 13 lines
Quoted 4 lines
lib/tpm-v1.c +2 -1
+
+     return rc;
-       * -EBUSY return code internally.

[...]

Quoted 6 lines
lib/tpm-v1.c +0 -1
-       */
      ut_assertok(tpm_auto_start(dev));
 
      ut_assert(tpm_report_state(dev, buf, sizeof(buf)));
@@ -36,4 +123,4 @@ static int dm_test_tpm(struct unit_test_state *uts)
 
Quoted 11 lines
Quoted 7 lines
lib/tpm-v1.c +1 -2
      return 0;
 }
-DM_TEST(dm_test_tpm, UT_TESTF_SCAN_FDT);
+DM_TEST(dm_test_tpm_report_state, UT_TESTF_SCAN_FDT);
--

2.39.2.637.g21b0678d19-goog

Other than that, this looks ok to me

OK I will clean it up and send.

Regards,
Simon