[U-Boot] [PATCH 09/21] tpm: Remove use of build-time TPM versions

Simon Glass sjg at chromium.org
Sat Nov 24 04:29:32 UTC 2018


There is only one place in the code which assumes at build-time that we
are using either a v1 or a v2 TPM. Fix this up and add a new function to
return the version of a TPM.

Supported TPM versions (v1 and v2) can be enabled independently and it is
possible to use both versions at once. This is useful for sandbox when
running tests.

Signed-off-by: Simon Glass <sjg at chromium.org>
---

 include/tpm-common.h | 11 +++++++++++
 lib/tpm-common.c     |  7 +++++++
 lib/tpm-v1.c         | 22 +++++++++++-----------
 3 files changed, 29 insertions(+), 11 deletions(-)

diff --git a/include/tpm-common.h b/include/tpm-common.h
index 3d88b44db7a..91a1484b3d6 100644
--- a/include/tpm-common.h
+++ b/include/tpm-common.h
@@ -274,4 +274,15 @@ static inline cmd_tbl_t *get_tpm2_commands(unsigned int *size)
 }
 #endif
 
+/**
+ * tpm_get_version() - Find the version of a TPM
+ *
+ * This checks the uclass data for a TPM device and returns the version number
+ * it supports.
+ *
+ * @dev: TPM device
+ * @return version number (TPM_V1 or TPMV2)
+ */
+enum tpm_version tpm_get_version(struct udevice *dev);
+
 #endif /* __TPM_COMMON_H */
diff --git a/lib/tpm-common.c b/lib/tpm-common.c
index 6afe59b1fec..2bf0b41e26f 100644
--- a/lib/tpm-common.c
+++ b/lib/tpm-common.c
@@ -12,6 +12,13 @@
 #include <tpm-common.h>
 #include "tpm-utils.h"
 
+enum tpm_version tpm_get_version(struct udevice *dev)
+{
+	struct tpm_chip_priv *priv = dev_get_uclass_priv(dev);
+
+	return priv->version;
+}
+
 int pack_byte_string(u8 *str, size_t size, const char *format, ...)
 {
 	va_list args;
diff --git a/lib/tpm-v1.c b/lib/tpm-v1.c
index f29e62ff7b2..3e89f845441 100644
--- a/lib/tpm-v1.c
+++ b/lib/tpm-v1.c
@@ -79,19 +79,19 @@ u32 tpm_clear_and_reenable(struct udevice *dev)
 		return ret;
 	}
 
-#if IS_ENABLED(CONFIG_TPM_V1)
-	ret = tpm_physical_enable(dev);
-	if (ret != TPM_SUCCESS) {
-		log_err("TPM: Can't set enabled state\n");
-		return ret;
-	}
+	if (tpm_get_version(dev) == TPM_V1) {
+		ret = tpm_physical_enable(dev);
+		if (ret != TPM_SUCCESS) {
+			log_err("TPM: Can't set enabled state\n");
+			return ret;
+		}
 
-	ret = tpm_physical_set_deactivated(dev, 0);
-	if (ret != TPM_SUCCESS) {
-		log_err("TPM: Can't set deactivated state\n");
-		return ret;
+		ret = tpm_physical_set_deactivated(dev, 0);
+		if (ret != TPM_SUCCESS) {
+			log_err("TPM: Can't set deactivated state\n");
+			return ret;
+		}
 	}
-#endif
 
 	return TPM_SUCCESS;
 }
-- 
2.20.0.rc0.387.gc7a69e6b6c-goog



More information about the U-Boot mailing list