]> git.dujemihanovic.xyz Git - u-boot.git/commitdiff
tpm: Implement tpm_auto_start() for TPMv1.2
authorSimon Glass <sjg@chromium.org>
Tue, 21 Feb 2023 13:24:52 +0000 (06:24 -0700)
committerIlias Apalodimas <ilias.apalodimas@linaro.org>
Tue, 28 Feb 2023 07:44:33 +0000 (09:44 +0200)
Add an implementation of this, moving the common call to tpm_init() up
into the common API implementation.

Add a test.

Reviewed-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>
Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>
include/tpm-common.h
include/tpm-v1.h
lib/tpm-v1.c
lib/tpm-v2.c
lib/tpm_api.c
test/dm/tpm.c

index b2c5404430f5ce22f4b97659b046527798cbb064..1ba81386ce1040c0b5c2eef6ffa7e86e786fc260 100644 (file)
@@ -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);
 
index 33d53fb695ee203f14f76168d5e7a3dcdc1f92c9..60b71e2a4b642adbae74ac0ada6b743515f6087f 100644 (file)
@@ -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 */
index d0e3ab1b21d1ab0109d1ddf00dadf9be83f665a9..60a18ca50400404b3a16bdbc431898f7fb211186 100644 (file)
@@ -69,6 +69,20 @@ 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);
+
+       return rc;
+}
+
 u32 tpm1_clear_and_reenable(struct udevice *dev)
 {
        u32 ret;
index 895b093bcb1a5df38fea36792dd673f090335042..9ab5b46df177eddf6df5491aa3bf1232a7efec77 100644 (file)
@@ -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) {
index 5b2c11a277cc9c4610d1f546f34d56b10dbd2128..3ef5e811794faf5762c0334c3b66594bc834feb7 100644 (file)
@@ -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)
index 7d88001209061831c15684d6e2af26494ba1046f..3defb3c3da1ff379537214fa4e9b34ae94499da6 100644 (file)
@@ -79,3 +79,48 @@ static int dm_test_tpm_report_state(struct unit_test_state *uts)
        return 0;
 }
 DM_TEST(dm_test_tpm_report_state, UT_TESTF_SCAN_FDT);
+
+/**
+ * test_tpm_autostart() - check the tpm_auto_start() call
+ *
+ * @uts: Unit test state
+ * @version: TPM version to use
+ * @reinit: true to call tpm_init() first
+ * Returns 0 if OK, non-zero on failure
+ */
+static int test_tpm_autostart(struct unit_test_state *uts,
+                             enum tpm_version version, bool reinit)
+{
+       struct udevice *dev;
+
+       /* check probe success */
+       ut_assertok(get_tpm_version(version, &dev));
+
+       if (reinit)
+               ut_assertok(tpm_init(dev));
+        /*
+         * tpm_auto_start will rerun tpm_init() if reinit, but handles the
+         * -EBUSY return code internally.
+         */
+       ut_assertok(tpm_auto_start(dev));
+
+       return 0;
+}
+
+static int dm_test_tpm_autostart(struct unit_test_state *uts)
+{
+       ut_assertok(test_tpm_autostart(uts, TPM_V1, false));
+       ut_assertok(test_tpm_autostart(uts, TPM_V2, false));
+
+       return 0;
+}
+DM_TEST(dm_test_tpm_autostart, UT_TESTF_SCAN_FDT);
+
+static int dm_test_tpm_autostart_reinit(struct unit_test_state *uts)
+{
+       ut_assertok(test_tpm_autostart(uts, TPM_V1, true));
+       ut_assertok(test_tpm_autostart(uts, TPM_V2, true));
+
+       return 0;
+}
+DM_TEST(dm_test_tpm_autostart_reinit, UT_TESTF_SCAN_FDT);