]> git.ipfire.org Git - thirdparty/u-boot.git/blobdiff - lib/tpm_api.c
net: hifemac_mdio: use log_msg_ret() correctly, report error by dev_err()
[thirdparty/u-boot.git] / lib / tpm_api.c
index 4c662640a92067f8a3b7310f3c10dfa3df45973c..39a5121e3028ce5c2b4fef9923672b0138476716 100644 (file)
@@ -3,7 +3,6 @@
  * Copyright 2019 Google LLC
  */
 
-#include <common.h>
 #include <dm.h>
 #include <log.h>
 #include <tpm_api.h>
 #include <tpm-v2.h>
 #include <tpm_api.h>
 
-static bool is_tpm1(struct udevice *dev)
-{
-       return IS_ENABLED(CONFIG_TPM_V1) && tpm_get_version(dev) == TPM_V1;
-}
-
-static bool is_tpm2(struct udevice *dev)
-{
-       return IS_ENABLED(CONFIG_TPM_V2) && tpm_get_version(dev) == TPM_V2;
-}
-
 u32 tpm_startup(struct udevice *dev, enum tpm_startup_type mode)
 {
-       if (is_tpm1(dev)) {
+       if (tpm_is_v1(dev)) {
                return tpm1_startup(dev, mode);
-       } else if (is_tpm2(dev)) {
+       } else if (tpm_is_v2(dev)) {
                enum tpm2_startup_types type;
 
                switch (mode) {
@@ -45,11 +34,32 @@ u32 tpm_startup(struct udevice *dev, enum tpm_startup_type mode)
        }
 }
 
+u32 tpm_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;
+
+       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)
 {
-       if (is_tpm1(dev))
+       if (tpm_is_v1(dev))
                return tpm1_startup(dev, TPM_ST_STATE);
-       else if (is_tpm2(dev))
+       else if (tpm_is_v2(dev))
                return tpm2_startup(dev, TPM2_SU_STATE);
        else
                return -ENOSYS;
@@ -57,9 +67,9 @@ u32 tpm_resume(struct udevice *dev)
 
 u32 tpm_self_test_full(struct udevice *dev)
 {
-       if (is_tpm1(dev))
+       if (tpm_is_v1(dev))
                return tpm1_self_test_full(dev);
-       else if (is_tpm2(dev))
+       else if (tpm_is_v2(dev))
                return tpm2_self_test(dev, TPMI_YES);
        else
                return -ENOSYS;
@@ -67,9 +77,9 @@ u32 tpm_self_test_full(struct udevice *dev)
 
 u32 tpm_continue_self_test(struct udevice *dev)
 {
-       if (is_tpm1(dev))
+       if (tpm_is_v1(dev))
                return tpm1_continue_self_test(dev);
-       else if (is_tpm2(dev))
+       else if (tpm_is_v2(dev))
                return tpm2_self_test(dev, TPMI_NO);
        else
                return -ENOSYS;
@@ -86,7 +96,7 @@ u32 tpm_clear_and_reenable(struct udevice *dev)
                return ret;
        }
 
-       if (is_tpm1(dev)) {
+       if (tpm_is_v1(dev)) {
                ret = tpm1_physical_enable(dev);
                if (ret != TPM_SUCCESS) {
                        log_err("TPM: Can't set enabled state\n");
@@ -105,9 +115,9 @@ u32 tpm_clear_and_reenable(struct udevice *dev)
 
 u32 tpm_nv_enable_locking(struct udevice *dev)
 {
-       if (is_tpm1(dev))
+       if (tpm_is_v1(dev))
                return tpm1_nv_define_space(dev, TPM_NV_INDEX_LOCK, 0, 0);
-       else if (is_tpm2(dev))
+       else if (tpm_is_v2(dev))
                return -ENOSYS;
        else
                return -ENOSYS;
@@ -115,9 +125,9 @@ u32 tpm_nv_enable_locking(struct udevice *dev)
 
 u32 tpm_nv_read_value(struct udevice *dev, u32 index, void *data, u32 count)
 {
-       if (is_tpm1(dev))
+       if (tpm_is_v1(dev))
                return tpm1_nv_read_value(dev, index, data, count);
-       else if (is_tpm2(dev))
+       else if (tpm_is_v2(dev))
                return tpm2_nv_read_value(dev, index, data, count);
        else
                return -ENOSYS;
@@ -126,9 +136,9 @@ u32 tpm_nv_read_value(struct udevice *dev, u32 index, void *data, u32 count)
 u32 tpm_nv_write_value(struct udevice *dev, u32 index, const void *data,
                       u32 count)
 {
-       if (is_tpm1(dev))
+       if (tpm_is_v1(dev))
                return tpm1_nv_write_value(dev, index, data, count);
-       else if (is_tpm2(dev))
+       else if (tpm_is_v2(dev))
                return tpm2_nv_write_value(dev, index, data, count);
        else
                return -ENOSYS;
@@ -141,31 +151,33 @@ u32 tpm_set_global_lock(struct udevice *dev)
 
 u32 tpm_write_lock(struct udevice *dev, u32 index)
 {
-       if (is_tpm1(dev))
+       if (tpm_is_v1(dev))
                return -ENOSYS;
-       else if (is_tpm2(dev))
+       else if (tpm_is_v2(dev))
                return tpm2_write_lock(dev, index);
        else
                return -ENOSYS;
 }
 
 u32 tpm_pcr_extend(struct udevice *dev, u32 index, const void *in_digest,
-                  void *out_digest)
+                  uint size, void *out_digest, const char *name)
 {
-       if (is_tpm1(dev))
+       if (tpm_is_v1(dev)) {
                return tpm1_extend(dev, index, in_digest, out_digest);
-       else if (is_tpm2(dev))
+       } else if (tpm_is_v2(dev)) {
                return tpm2_pcr_extend(dev, index, TPM2_ALG_SHA256, in_digest,
                                       TPM2_DIGEST_LEN);
-       else
+               /* @name is ignored as we do not support the TPM log here */
+       } else {
                return -ENOSYS;
+       }
 }
 
 u32 tpm_pcr_read(struct udevice *dev, u32 index, void *data, size_t count)
 {
-       if (is_tpm1(dev))
+       if (tpm_is_v1(dev))
                return tpm1_pcr_read(dev, index, data, count);
-       else if (is_tpm2(dev))
+       else if (tpm_is_v2(dev))
                return -ENOSYS;
        else
                return -ENOSYS;
@@ -173,14 +185,14 @@ u32 tpm_pcr_read(struct udevice *dev, u32 index, void *data, size_t count)
 
 u32 tpm_tsc_physical_presence(struct udevice *dev, u16 presence)
 {
-       if (is_tpm1(dev))
+       if (tpm_is_v1(dev))
                return tpm1_tsc_physical_presence(dev, presence);
 
        /*
         * Nothing to do on TPM2 for this; use platform hierarchy availability
         * instead.
         */
-       else if (is_tpm2(dev))
+       else if (tpm_is_v2(dev))
                return 0;
        else
                return -ENOSYS;
@@ -188,11 +200,11 @@ u32 tpm_tsc_physical_presence(struct udevice *dev, u16 presence)
 
 u32 tpm_finalise_physical_presence(struct udevice *dev)
 {
-       if (is_tpm1(dev))
+       if (tpm_is_v1(dev))
                return tpm1_finalise_physical_presence(dev);
 
        /* Nothing needs to be done with tpm2 */
-       else if (is_tpm2(dev))
+       else if (tpm_is_v2(dev))
                return 0;
        else
                return -ENOSYS;
@@ -200,9 +212,9 @@ u32 tpm_finalise_physical_presence(struct udevice *dev)
 
 u32 tpm_read_pubek(struct udevice *dev, void *data, size_t count)
 {
-       if (is_tpm1(dev))
+       if (tpm_is_v1(dev))
                return tpm1_read_pubek(dev, data, count);
-       else if (is_tpm2(dev))
+       else if (tpm_is_v2(dev))
                return -ENOSYS; /* not implemented yet */
        else
                return -ENOSYS;
@@ -210,9 +222,9 @@ u32 tpm_read_pubek(struct udevice *dev, void *data, size_t count)
 
 u32 tpm_force_clear(struct udevice *dev)
 {
-       if (is_tpm1(dev))
+       if (tpm_is_v1(dev))
                return tpm1_force_clear(dev);
-       else if (is_tpm2(dev))
+       else if (tpm_is_v2(dev))
                return tpm2_clear(dev, TPM2_RH_PLATFORM, NULL, 0);
        else
                return -ENOSYS;
@@ -220,11 +232,11 @@ u32 tpm_force_clear(struct udevice *dev)
 
 u32 tpm_physical_enable(struct udevice *dev)
 {
-       if (is_tpm1(dev))
+       if (tpm_is_v1(dev))
                return tpm1_physical_enable(dev);
 
        /* Nothing needs to be done with tpm2 */
-       else if (is_tpm2(dev))
+       else if (tpm_is_v2(dev))
                return 0;
        else
                return -ENOSYS;
@@ -232,11 +244,11 @@ u32 tpm_physical_enable(struct udevice *dev)
 
 u32 tpm_physical_disable(struct udevice *dev)
 {
-       if (is_tpm1(dev))
+       if (tpm_is_v1(dev))
                return tpm1_physical_disable(dev);
 
        /* Nothing needs to be done with tpm2 */
-       else if (is_tpm2(dev))
+       else if (tpm_is_v2(dev))
                return 0;
        else
                return -ENOSYS;
@@ -244,10 +256,10 @@ u32 tpm_physical_disable(struct udevice *dev)
 
 u32 tpm_physical_set_deactivated(struct udevice *dev, u8 state)
 {
-       if (is_tpm1(dev))
+       if (tpm_is_v1(dev))
                return tpm1_physical_set_deactivated(dev, state);
        /* Nothing needs to be done with tpm2 */
-       else if (is_tpm2(dev))
+       else if (tpm_is_v2(dev))
                return 0;
        else
                return -ENOSYS;
@@ -256,9 +268,9 @@ u32 tpm_physical_set_deactivated(struct udevice *dev, u8 state)
 u32 tpm_get_capability(struct udevice *dev, u32 cap_area, u32 sub_cap,
                       void *cap, size_t count)
 {
-       if (is_tpm1(dev))
+       if (tpm_is_v1(dev))
                return tpm1_get_capability(dev, cap_area, sub_cap, cap, count);
-       else if (is_tpm2(dev))
+       else if (tpm_is_v2(dev))
                return tpm2_get_capability(dev, cap_area, sub_cap, cap, count);
        else
                return -ENOSYS;
@@ -266,9 +278,9 @@ u32 tpm_get_capability(struct udevice *dev, u32 cap_area, u32 sub_cap,
 
 u32 tpm_get_permissions(struct udevice *dev, u32 index, u32 *perm)
 {
-       if (is_tpm1(dev))
+       if (tpm_is_v1(dev))
                return tpm1_get_permissions(dev, index, perm);
-       else if (is_tpm2(dev))
+       else if (tpm_is_v2(dev))
                return -ENOSYS; /* not implemented yet */
        else
                return -ENOSYS;
@@ -276,10 +288,10 @@ u32 tpm_get_permissions(struct udevice *dev, u32 index, u32 *perm)
 
 u32 tpm_get_random(struct udevice *dev, void *data, u32 count)
 {
-       if (is_tpm1(dev))
+       if (tpm_is_v1(dev))
                return tpm1_get_random(dev, data, count);
-       else if (is_tpm2(dev))
-               return -ENOSYS; /* not implemented yet */
-       else
-               return -ENOSYS;
+       else if (tpm_is_v2(dev))
+               return tpm2_get_random(dev, data, count);
+
+       return -ENOSYS;
 }