1// SPDX-License-Identifier: GPL-2.0+ 2/* 3 * Copyright 2019 Google LLC 4 */ 5 6#include <dm.h> 7#include <log.h> 8#include <tpm_api.h> 9#include <tpm-v1.h> 10#include <tpm-v2.h> 11#include <tpm_api.h> 12 13u32 tpm_startup(struct udevice *dev, enum tpm_startup_type mode) 14{ 15 if (tpm_is_v1(dev)) { 16 return tpm1_startup(dev, mode); 17 } else if (tpm_is_v2(dev)) { 18 enum tpm2_startup_types type; 19 20 switch (mode) { 21 case TPM_ST_CLEAR: 22 type = TPM2_SU_CLEAR; 23 break; 24 case TPM_ST_STATE: 25 type = TPM2_SU_STATE; 26 break; 27 default: 28 case TPM_ST_DEACTIVATED: 29 return -EINVAL; 30 } 31 return tpm2_startup(dev, type); 32 } else { 33 return -ENOSYS; 34 } 35} 36 37u32 tpm_auto_start(struct udevice *dev) 38{ 39 u32 rc; 40 41 /* 42 * the tpm_init() will return -EBUSY if the init has already happened 43 * The selftest and startup code can run multiple times with no side 44 * effects 45 */ 46 rc = tpm_init(dev); 47 if (rc && rc != -EBUSY) 48 return rc; 49 50 if (tpm_is_v1(dev)) 51 return tpm1_auto_start(dev); 52 else if (tpm_is_v2(dev)) 53 return tpm2_auto_start(dev); 54 else 55 return -ENOSYS; 56} 57 58u32 tpm_resume(struct udevice *dev) 59{ 60 if (tpm_is_v1(dev)) 61 return tpm1_startup(dev, TPM_ST_STATE); 62 else if (tpm_is_v2(dev)) 63 return tpm2_startup(dev, TPM2_SU_STATE); 64 else 65 return -ENOSYS; 66} 67 68u32 tpm_self_test_full(struct udevice *dev) 69{ 70 if (tpm_is_v1(dev)) 71 return tpm1_self_test_full(dev); 72 else if (tpm_is_v2(dev)) 73 return tpm2_self_test(dev, TPMI_YES); 74 else 75 return -ENOSYS; 76} 77 78u32 tpm_continue_self_test(struct udevice *dev) 79{ 80 if (tpm_is_v1(dev)) 81 return tpm1_continue_self_test(dev); 82 else if (tpm_is_v2(dev)) 83 return tpm2_self_test(dev, TPMI_NO); 84 else 85 return -ENOSYS; 86} 87 88u32 tpm_clear_and_reenable(struct udevice *dev) 89{ 90 u32 ret; 91 92 log_info("TPM: Clear and re-enable\n"); 93 ret = tpm_force_clear(dev); 94 if (ret != TPM_SUCCESS) { 95 log_err("Can't initiate a force clear\n"); 96 return ret; 97 } 98 99 if (tpm_is_v1(dev)) { 100 ret = tpm1_physical_enable(dev); 101 if (ret != TPM_SUCCESS) { 102 log_err("TPM: Can't set enabled state\n"); 103 return ret; 104 } 105 106 ret = tpm1_physical_set_deactivated(dev, 0); 107 if (ret != TPM_SUCCESS) { 108 log_err("TPM: Can't set deactivated state\n"); 109 return ret; 110 } 111 } 112 113 return TPM_SUCCESS; 114} 115 116u32 tpm_nv_enable_locking(struct udevice *dev) 117{ 118 if (tpm_is_v1(dev)) 119 return tpm1_nv_define_space(dev, TPM_NV_INDEX_LOCK, 0, 0); 120 else if (tpm_is_v2(dev)) 121 return -ENOSYS; 122 else 123 return -ENOSYS; 124} 125 126u32 tpm_nv_read_value(struct udevice *dev, u32 index, void *data, u32 count) 127{ 128 if (tpm_is_v1(dev)) 129 return tpm1_nv_read_value(dev, index, data, count); 130 else if (tpm_is_v2(dev)) 131 return tpm2_nv_read_value(dev, index, data, count); 132 else 133 return -ENOSYS; 134} 135 136u32 tpm_nv_write_value(struct udevice *dev, u32 index, const void *data, 137 u32 count) 138{ 139 if (tpm_is_v1(dev)) 140 return tpm1_nv_write_value(dev, index, data, count); 141 else if (tpm_is_v2(dev)) 142 return tpm2_nv_write_value(dev, index, data, count); 143 else 144 return -ENOSYS; 145} 146 147u32 tpm_set_global_lock(struct udevice *dev) 148{ 149 return tpm_nv_write_value(dev, TPM_NV_INDEX_0, NULL, 0); 150} 151 152u32 tpm_write_lock(struct udevice *dev, u32 index) 153{ 154 if (tpm_is_v1(dev)) 155 return -ENOSYS; 156 else if (tpm_is_v2(dev)) 157 return tpm2_write_lock(dev, index); 158 else 159 return -ENOSYS; 160} 161 162u32 tpm_pcr_extend(struct udevice *dev, u32 index, const void *in_digest, 163 uint size, void *out_digest, const char *name) 164{ 165 if (tpm_is_v1(dev)) { 166 return tpm1_extend(dev, index, in_digest, out_digest); 167 } else if (tpm_is_v2(dev)) { 168 return tpm2_pcr_extend(dev, index, TPM2_ALG_SHA256, in_digest, 169 TPM2_DIGEST_LEN); 170 /* @name is ignored as we do not support the TPM log here */ 171 } else { 172 return -ENOSYS; 173 } 174} 175 176u32 tpm_pcr_read(struct udevice *dev, u32 index, void *data, size_t count) 177{ 178 if (tpm_is_v1(dev)) 179 return tpm1_pcr_read(dev, index, data, count); 180 else if (tpm_is_v2(dev)) 181 return -ENOSYS; 182 else 183 return -ENOSYS; 184} 185 186u32 tpm_tsc_physical_presence(struct udevice *dev, u16 presence) 187{ 188 if (tpm_is_v1(dev)) 189 return tpm1_tsc_physical_presence(dev, presence); 190 191 /* 192 * Nothing to do on TPM2 for this; use platform hierarchy availability 193 * instead. 194 */ 195 else if (tpm_is_v2(dev)) 196 return 0; 197 else 198 return -ENOSYS; 199} 200 201u32 tpm_finalise_physical_presence(struct udevice *dev) 202{ 203 if (tpm_is_v1(dev)) 204 return tpm1_finalise_physical_presence(dev); 205 206 /* Nothing needs to be done with tpm2 */ 207 else if (tpm_is_v2(dev)) 208 return 0; 209 else 210 return -ENOSYS; 211} 212 213u32 tpm_read_pubek(struct udevice *dev, void *data, size_t count) 214{ 215 if (tpm_is_v1(dev)) 216 return tpm1_read_pubek(dev, data, count); 217 else if (tpm_is_v2(dev)) 218 return -ENOSYS; /* not implemented yet */ 219 else 220 return -ENOSYS; 221} 222 223u32 tpm_force_clear(struct udevice *dev) 224{ 225 if (tpm_is_v1(dev)) 226 return tpm1_force_clear(dev); 227 else if (tpm_is_v2(dev)) 228 return tpm2_clear(dev, TPM2_RH_PLATFORM, NULL, 0); 229 else 230 return -ENOSYS; 231} 232 233u32 tpm_physical_enable(struct udevice *dev) 234{ 235 if (tpm_is_v1(dev)) 236 return tpm1_physical_enable(dev); 237 238 /* Nothing needs to be done with tpm2 */ 239 else if (tpm_is_v2(dev)) 240 return 0; 241 else 242 return -ENOSYS; 243} 244 245u32 tpm_physical_disable(struct udevice *dev) 246{ 247 if (tpm_is_v1(dev)) 248 return tpm1_physical_disable(dev); 249 250 /* Nothing needs to be done with tpm2 */ 251 else if (tpm_is_v2(dev)) 252 return 0; 253 else 254 return -ENOSYS; 255} 256 257u32 tpm_physical_set_deactivated(struct udevice *dev, u8 state) 258{ 259 if (tpm_is_v1(dev)) 260 return tpm1_physical_set_deactivated(dev, state); 261 /* Nothing needs to be done with tpm2 */ 262 else if (tpm_is_v2(dev)) 263 return 0; 264 else 265 return -ENOSYS; 266} 267 268u32 tpm_get_capability(struct udevice *dev, u32 cap_area, u32 sub_cap, 269 void *cap, size_t count) 270{ 271 if (tpm_is_v1(dev)) 272 return tpm1_get_capability(dev, cap_area, sub_cap, cap, count); 273 else if (tpm_is_v2(dev)) 274 return tpm2_get_capability(dev, cap_area, sub_cap, cap, count); 275 else 276 return -ENOSYS; 277} 278 279u32 tpm_get_permissions(struct udevice *dev, u32 index, u32 *perm) 280{ 281 if (tpm_is_v1(dev)) 282 return tpm1_get_permissions(dev, index, perm); 283 else if (tpm_is_v2(dev)) 284 return -ENOSYS; /* not implemented yet */ 285 else 286 return -ENOSYS; 287} 288 289u32 tpm_get_random(struct udevice *dev, void *data, u32 count) 290{ 291 if (tpm_is_v1(dev)) 292 return tpm1_get_random(dev, data, count); 293 else if (tpm_is_v2(dev)) 294 return tpm2_get_random(dev, data, count); 295 296 return -ENOSYS; 297} 298