15e124724SVadim Bendebury /* 28732b070SChe-liang Chiou * Copyright (c) 2013 The Chromium OS Authors. 3be6c1529SReinhard Pfau * Coypright (c) 2013 Guntermann & Drunck GmbH 45e124724SVadim Bendebury * 51a459660SWolfgang Denk * SPDX-License-Identifier: GPL-2.0+ 65e124724SVadim Bendebury */ 75e124724SVadim Bendebury 88732b070SChe-liang Chiou #ifndef __TPM_H 98732b070SChe-liang Chiou #define __TPM_H 105e124724SVadim Bendebury 115e124724SVadim Bendebury /* 128732b070SChe-liang Chiou * Here is a partial implementation of TPM commands. Please consult TCG Main 138732b070SChe-liang Chiou * Specification for definitions of TPM commands. 145e124724SVadim Bendebury */ 155e124724SVadim Bendebury 16f255d31fSSimon Glass #define TPM_HEADER_SIZE 10 17f255d31fSSimon Glass 18f255d31fSSimon Glass enum tpm_duration { 19f255d31fSSimon Glass TPM_SHORT = 0, 20f255d31fSSimon Glass TPM_MEDIUM = 1, 21f255d31fSSimon Glass TPM_LONG = 2, 22f255d31fSSimon Glass TPM_UNDEFINED, 23f255d31fSSimon Glass 24f255d31fSSimon Glass TPM_DURATION_COUNT, 25f255d31fSSimon Glass }; 26f255d31fSSimon Glass 278732b070SChe-liang Chiou enum tpm_startup_type { 288732b070SChe-liang Chiou TPM_ST_CLEAR = 0x0001, 298732b070SChe-liang Chiou TPM_ST_STATE = 0x0002, 308732b070SChe-liang Chiou TPM_ST_DEACTIVATED = 0x0003, 318732b070SChe-liang Chiou }; 328732b070SChe-liang Chiou 338732b070SChe-liang Chiou enum tpm_physical_presence { 348732b070SChe-liang Chiou TPM_PHYSICAL_PRESENCE_HW_DISABLE = 0x0200, 358732b070SChe-liang Chiou TPM_PHYSICAL_PRESENCE_CMD_DISABLE = 0x0100, 368732b070SChe-liang Chiou TPM_PHYSICAL_PRESENCE_LIFETIME_LOCK = 0x0080, 378732b070SChe-liang Chiou TPM_PHYSICAL_PRESENCE_HW_ENABLE = 0x0040, 388732b070SChe-liang Chiou TPM_PHYSICAL_PRESENCE_CMD_ENABLE = 0x0020, 398732b070SChe-liang Chiou TPM_PHYSICAL_PRESENCE_NOTPRESENT = 0x0010, 408732b070SChe-liang Chiou TPM_PHYSICAL_PRESENCE_PRESENT = 0x0008, 418732b070SChe-liang Chiou TPM_PHYSICAL_PRESENCE_LOCK = 0x0004, 428732b070SChe-liang Chiou }; 438732b070SChe-liang Chiou 448732b070SChe-liang Chiou enum tpm_nv_index { 458732b070SChe-liang Chiou TPM_NV_INDEX_LOCK = 0xffffffff, 468732b070SChe-liang Chiou TPM_NV_INDEX_0 = 0x00000000, 478732b070SChe-liang Chiou TPM_NV_INDEX_DIR = 0x10000001, 488732b070SChe-liang Chiou }; 498732b070SChe-liang Chiou 507690be35SMario Six enum tpm_resource_type { 517690be35SMario Six TPM_RT_KEY = 0x00000001, 527690be35SMario Six TPM_RT_AUTH = 0x00000002, 537690be35SMario Six TPM_RT_HASH = 0x00000003, 547690be35SMario Six TPM_RT_TRANS = 0x00000004, 557690be35SMario Six TPM_RT_CONTEXT = 0x00000005, 567690be35SMario Six TPM_RT_COUNTER = 0x00000006, 577690be35SMario Six TPM_RT_DELEGATE = 0x00000007, 587690be35SMario Six TPM_RT_DAA_TPM = 0x00000008, 597690be35SMario Six TPM_RT_DAA_V0 = 0x00000009, 607690be35SMario Six TPM_RT_DAA_V1 = 0x0000000A, 617690be35SMario Six }; 627690be35SMario Six 637690be35SMario Six enum tpm_capability_areas { 647690be35SMario Six TPM_CAP_ORD = 0x00000001, 657690be35SMario Six TPM_CAP_ALG = 0x00000002, 667690be35SMario Six TPM_CAP_PID = 0x00000003, 677690be35SMario Six TPM_CAP_FLAG = 0x00000004, 687690be35SMario Six TPM_CAP_PROPERTY = 0x00000005, 697690be35SMario Six TPM_CAP_VERSION = 0x00000006, 707690be35SMario Six TPM_CAP_KEY_HANDLE = 0x00000007, 717690be35SMario Six TPM_CAP_CHECK_LOADED = 0x00000008, 727690be35SMario Six TPM_CAP_SYM_MODE = 0x00000009, 737690be35SMario Six TPM_CAP_KEY_STATUS = 0x0000000C, 747690be35SMario Six TPM_CAP_NV_LIST = 0x0000000D, 757690be35SMario Six TPM_CAP_MFR = 0x00000010, 767690be35SMario Six TPM_CAP_NV_INDEX = 0x00000011, 777690be35SMario Six TPM_CAP_TRANS_ALG = 0x00000012, 787690be35SMario Six TPM_CAP_HANDLE = 0x00000014, 797690be35SMario Six TPM_CAP_TRANS_ES = 0x00000015, 807690be35SMario Six TPM_CAP_AUTH_ENCRYPT = 0x00000017, 817690be35SMario Six TPM_CAP_SELECT_SIZE = 0x00000018, 827690be35SMario Six TPM_CAP_DA_LOGIC = 0x00000019, 837690be35SMario Six TPM_CAP_VERSION_VAL = 0x0000001A, 847690be35SMario Six }; 857690be35SMario Six 862132f971SSimon Glass #define TPM_NV_PER_GLOBALLOCK (1U << 15) 872132f971SSimon Glass #define TPM_NV_PER_PPWRITE (1U << 0) 882132f971SSimon Glass #define TPM_NV_PER_READ_STCLEAR (1U << 31) 892132f971SSimon Glass #define TPM_NV_PER_WRITE_STCLEAR (1U << 14) 902132f971SSimon Glass 912132f971SSimon Glass enum { 922132f971SSimon Glass TPM_PUBEK_SIZE = 256, 932132f971SSimon Glass }; 942132f971SSimon Glass 958732b070SChe-liang Chiou /** 96be6c1529SReinhard Pfau * TPM return codes as defined in the TCG Main specification 97be6c1529SReinhard Pfau * (TPM Main Part 2 Structures; Specification version 1.2) 98be6c1529SReinhard Pfau */ 99be6c1529SReinhard Pfau enum tpm_return_code { 100be6c1529SReinhard Pfau TPM_BASE = 0x00000000, 101be6c1529SReinhard Pfau TPM_NON_FATAL = 0x00000800, 102be6c1529SReinhard Pfau TPM_SUCCESS = TPM_BASE, 103be6c1529SReinhard Pfau /* TPM-defined fatal error codes */ 104be6c1529SReinhard Pfau TPM_AUTHFAIL = TPM_BASE + 1, 105be6c1529SReinhard Pfau TPM_BADINDEX = TPM_BASE + 2, 106be6c1529SReinhard Pfau TPM_BAD_PARAMETER = TPM_BASE + 3, 107be6c1529SReinhard Pfau TPM_AUDITFAILURE = TPM_BASE + 4, 108be6c1529SReinhard Pfau TPM_CLEAR_DISABLED = TPM_BASE + 5, 109be6c1529SReinhard Pfau TPM_DEACTIVATED = TPM_BASE + 6, 110be6c1529SReinhard Pfau TPM_DISABLED = TPM_BASE + 7, 111be6c1529SReinhard Pfau TPM_DISABLED_CMD = TPM_BASE + 8, 112be6c1529SReinhard Pfau TPM_FAIL = TPM_BASE + 9, 113be6c1529SReinhard Pfau TPM_BAD_ORDINAL = TPM_BASE + 10, 114be6c1529SReinhard Pfau TPM_INSTALL_DISABLED = TPM_BASE + 11, 115be6c1529SReinhard Pfau TPM_INVALID_KEYHANDLE = TPM_BASE + 12, 116be6c1529SReinhard Pfau TPM_KEYNOTFOUND = TPM_BASE + 13, 117be6c1529SReinhard Pfau TPM_INAPPROPRIATE_ENC = TPM_BASE + 14, 118be6c1529SReinhard Pfau TPM_MIGRATE_FAIL = TPM_BASE + 15, 119be6c1529SReinhard Pfau TPM_INVALID_PCR_INFO = TPM_BASE + 16, 120be6c1529SReinhard Pfau TPM_NOSPACE = TPM_BASE + 17, 121be6c1529SReinhard Pfau TPM_NOSRK = TPM_BASE + 18, 122be6c1529SReinhard Pfau TPM_NOTSEALED_BLOB = TPM_BASE + 19, 123be6c1529SReinhard Pfau TPM_OWNER_SET = TPM_BASE + 20, 124be6c1529SReinhard Pfau TPM_RESOURCES = TPM_BASE + 21, 125be6c1529SReinhard Pfau TPM_SHORTRANDOM = TPM_BASE + 22, 126be6c1529SReinhard Pfau TPM_SIZE = TPM_BASE + 23, 127be6c1529SReinhard Pfau TPM_WRONGPCRVAL = TPM_BASE + 24, 128be6c1529SReinhard Pfau TPM_BAD_PARAM_SIZE = TPM_BASE + 25, 129be6c1529SReinhard Pfau TPM_SHA_THREAD = TPM_BASE + 26, 130be6c1529SReinhard Pfau TPM_SHA_ERROR = TPM_BASE + 27, 131be6c1529SReinhard Pfau TPM_FAILEDSELFTEST = TPM_BASE + 28, 132be6c1529SReinhard Pfau TPM_AUTH2FAIL = TPM_BASE + 29, 133be6c1529SReinhard Pfau TPM_BADTAG = TPM_BASE + 30, 134be6c1529SReinhard Pfau TPM_IOERROR = TPM_BASE + 31, 135be6c1529SReinhard Pfau TPM_ENCRYPT_ERROR = TPM_BASE + 32, 136be6c1529SReinhard Pfau TPM_DECRYPT_ERROR = TPM_BASE + 33, 137be6c1529SReinhard Pfau TPM_INVALID_AUTHHANDLE = TPM_BASE + 34, 138be6c1529SReinhard Pfau TPM_NO_ENDORSEMENT = TPM_BASE + 35, 139be6c1529SReinhard Pfau TPM_INVALID_KEYUSAGE = TPM_BASE + 36, 140be6c1529SReinhard Pfau TPM_WRONG_ENTITYTYPE = TPM_BASE + 37, 141be6c1529SReinhard Pfau TPM_INVALID_POSTINIT = TPM_BASE + 38, 142be6c1529SReinhard Pfau TPM_INAPPROPRIATE_SIG = TPM_BASE + 39, 143be6c1529SReinhard Pfau TPM_BAD_KEY_PROPERTY = TPM_BASE + 40, 144be6c1529SReinhard Pfau TPM_BAD_MIGRATION = TPM_BASE + 41, 145be6c1529SReinhard Pfau TPM_BAD_SCHEME = TPM_BASE + 42, 146be6c1529SReinhard Pfau TPM_BAD_DATASIZE = TPM_BASE + 43, 147be6c1529SReinhard Pfau TPM_BAD_MODE = TPM_BASE + 44, 148be6c1529SReinhard Pfau TPM_BAD_PRESENCE = TPM_BASE + 45, 149be6c1529SReinhard Pfau TPM_BAD_VERSION = TPM_BASE + 46, 150be6c1529SReinhard Pfau TPM_NO_WRAP_TRANSPORT = TPM_BASE + 47, 151be6c1529SReinhard Pfau TPM_AUDITFAIL_UNSUCCESSFUL = TPM_BASE + 48, 152be6c1529SReinhard Pfau TPM_AUDITFAIL_SUCCESSFUL = TPM_BASE + 49, 153be6c1529SReinhard Pfau TPM_NOTRESETABLE = TPM_BASE + 50, 154be6c1529SReinhard Pfau TPM_NOTLOCAL = TPM_BASE + 51, 155be6c1529SReinhard Pfau TPM_BAD_TYPE = TPM_BASE + 52, 156be6c1529SReinhard Pfau TPM_INVALID_RESOURCE = TPM_BASE + 53, 157be6c1529SReinhard Pfau TPM_NOTFIPS = TPM_BASE + 54, 158be6c1529SReinhard Pfau TPM_INVALID_FAMILY = TPM_BASE + 55, 159be6c1529SReinhard Pfau TPM_NO_NV_PERMISSION = TPM_BASE + 56, 160be6c1529SReinhard Pfau TPM_REQUIRES_SIGN = TPM_BASE + 57, 161be6c1529SReinhard Pfau TPM_KEY_NOTSUPPORTED = TPM_BASE + 58, 162be6c1529SReinhard Pfau TPM_AUTH_CONFLICT = TPM_BASE + 59, 163be6c1529SReinhard Pfau TPM_AREA_LOCKED = TPM_BASE + 60, 164be6c1529SReinhard Pfau TPM_BAD_LOCALITY = TPM_BASE + 61, 165be6c1529SReinhard Pfau TPM_READ_ONLY = TPM_BASE + 62, 166be6c1529SReinhard Pfau TPM_PER_NOWRITE = TPM_BASE + 63, 167be6c1529SReinhard Pfau TPM_FAMILY_COUNT = TPM_BASE + 64, 168be6c1529SReinhard Pfau TPM_WRITE_LOCKED = TPM_BASE + 65, 169be6c1529SReinhard Pfau TPM_BAD_ATTRIBUTES = TPM_BASE + 66, 170be6c1529SReinhard Pfau TPM_INVALID_STRUCTURE = TPM_BASE + 67, 171be6c1529SReinhard Pfau TPM_KEY_OWNER_CONTROL = TPM_BASE + 68, 172be6c1529SReinhard Pfau TPM_BAD_COUNTER = TPM_BASE + 69, 173be6c1529SReinhard Pfau TPM_NOT_FULLWRITE = TPM_BASE + 70, 174be6c1529SReinhard Pfau TPM_CONTEXT_GAP = TPM_BASE + 71, 175be6c1529SReinhard Pfau TPM_MAXNVWRITES = TPM_BASE + 72, 176be6c1529SReinhard Pfau TPM_NOOPERATOR = TPM_BASE + 73, 177be6c1529SReinhard Pfau TPM_RESOURCEMISSING = TPM_BASE + 74, 178be6c1529SReinhard Pfau TPM_DELEGATE_LOCK = TPM_BASE + 75, 179be6c1529SReinhard Pfau TPM_DELEGATE_FAMILY = TPM_BASE + 76, 180be6c1529SReinhard Pfau TPM_DELEGATE_ADMIN = TPM_BASE + 77, 181be6c1529SReinhard Pfau TPM_TRANSPORT_NOTEXCLUSIVE = TPM_BASE + 78, 182be6c1529SReinhard Pfau TPM_OWNER_CONTROL = TPM_BASE + 79, 183be6c1529SReinhard Pfau TPM_DAA_RESOURCES = TPM_BASE + 80, 184be6c1529SReinhard Pfau TPM_DAA_INPUT_DATA0 = TPM_BASE + 81, 185be6c1529SReinhard Pfau TPM_DAA_INPUT_DATA1 = TPM_BASE + 82, 186be6c1529SReinhard Pfau TPM_DAA_ISSUER_SETTINGS = TPM_BASE + 83, 187be6c1529SReinhard Pfau TPM_DAA_TPM_SETTINGS = TPM_BASE + 84, 188be6c1529SReinhard Pfau TPM_DAA_STAGE = TPM_BASE + 85, 189be6c1529SReinhard Pfau TPM_DAA_ISSUER_VALIDITY = TPM_BASE + 86, 190be6c1529SReinhard Pfau TPM_DAA_WRONG_W = TPM_BASE + 87, 191be6c1529SReinhard Pfau TPM_BAD_HANDLE = TPM_BASE + 88, 192be6c1529SReinhard Pfau TPM_BAD_DELEGATE = TPM_BASE + 89, 193be6c1529SReinhard Pfau TPM_BADCONTEXT = TPM_BASE + 90, 194be6c1529SReinhard Pfau TPM_TOOMANYCONTEXTS = TPM_BASE + 91, 195be6c1529SReinhard Pfau TPM_MA_TICKET_SIGNATURE = TPM_BASE + 92, 196be6c1529SReinhard Pfau TPM_MA_DESTINATION = TPM_BASE + 93, 197be6c1529SReinhard Pfau TPM_MA_SOURCE = TPM_BASE + 94, 198be6c1529SReinhard Pfau TPM_MA_AUTHORITY = TPM_BASE + 95, 199be6c1529SReinhard Pfau TPM_PERMANENTEK = TPM_BASE + 97, 200be6c1529SReinhard Pfau TPM_BAD_SIGNATURE = TPM_BASE + 98, 201be6c1529SReinhard Pfau TPM_NOCONTEXTSPACE = TPM_BASE + 99, 202be6c1529SReinhard Pfau /* TPM-defined non-fatal errors */ 203be6c1529SReinhard Pfau TPM_RETRY = TPM_BASE + TPM_NON_FATAL, 204be6c1529SReinhard Pfau TPM_NEEDS_SELFTEST = TPM_BASE + TPM_NON_FATAL + 1, 205be6c1529SReinhard Pfau TPM_DOING_SELFTEST = TPM_BASE + TPM_NON_FATAL + 2, 206be6c1529SReinhard Pfau TPM_DEFEND_LOCK_RUNNING = TPM_BASE + TPM_NON_FATAL + 3, 207be6c1529SReinhard Pfau }; 208be6c1529SReinhard Pfau 2092132f971SSimon Glass struct tpm_permanent_flags { 2102132f971SSimon Glass __be16 tag; 2112132f971SSimon Glass u8 disable; 2122132f971SSimon Glass u8 ownership; 2132132f971SSimon Glass u8 deactivated; 2142132f971SSimon Glass u8 read_pubek; 2152132f971SSimon Glass u8 disable_owner_clear; 2162132f971SSimon Glass u8 allow_maintenance; 2172132f971SSimon Glass u8 physical_presence_lifetime_lock; 2182132f971SSimon Glass u8 physical_presence_hw_enable; 2192132f971SSimon Glass u8 physical_presence_cmd_enable; 2202132f971SSimon Glass u8 cekp_used; 2212132f971SSimon Glass u8 tpm_post; 2222132f971SSimon Glass u8 tpm_post_lock; 2232132f971SSimon Glass u8 fips; 2242132f971SSimon Glass u8 operator; 2252132f971SSimon Glass u8 enable_revoke_ek; 2262132f971SSimon Glass u8 nv_locked; 2272132f971SSimon Glass u8 read_srk_pub; 2282132f971SSimon Glass u8 tpm_established; 2292132f971SSimon Glass u8 maintenance_done; 2302132f971SSimon Glass u8 disable_full_da_logic_info; 2312132f971SSimon Glass } __packed; 2322132f971SSimon Glass 233f255d31fSSimon Glass /* Max buffer size supported by our tpm */ 234f255d31fSSimon Glass #define TPM_DEV_BUFSIZE 1260 235f255d31fSSimon Glass 236f255d31fSSimon Glass /** 237f255d31fSSimon Glass * struct tpm_chip_priv - Information about a TPM, stored by the uclass 238f255d31fSSimon Glass * 239f255d31fSSimon Glass * These values must be set up by the device's probe() method before 240f255d31fSSimon Glass * communcation is attempted. If the device has an xfer() method, this is 241f255d31fSSimon Glass * not needed. There is no need to set up @buf. 242f255d31fSSimon Glass * 243f255d31fSSimon Glass * @duration_ms: Length of each duration type in milliseconds 244f255d31fSSimon Glass * @retry_time_ms: Time to wait before retrying receive 245f255d31fSSimon Glass */ 246f255d31fSSimon Glass struct tpm_chip_priv { 247f255d31fSSimon Glass uint duration_ms[TPM_DURATION_COUNT]; 248f255d31fSSimon Glass uint retry_time_ms; 249f255d31fSSimon Glass u8 buf[TPM_DEV_BUFSIZE + sizeof(u8)]; /* Max buffer size + addr */ 250f255d31fSSimon Glass }; 251f255d31fSSimon Glass 252f255d31fSSimon Glass /** 253f255d31fSSimon Glass * struct tpm_ops - low-level TPM operations 254f255d31fSSimon Glass * 255f255d31fSSimon Glass * These are designed to avoid loops and delays in the driver itself. These 256f255d31fSSimon Glass * should be handled in the uclass. 257f255d31fSSimon Glass * 258f255d31fSSimon Glass * In gneral you should implement everything except xfer(). Where you need 259f255d31fSSimon Glass * complete control of the transfer, then xfer() can be provided and will 260f255d31fSSimon Glass * override the other methods. 261f255d31fSSimon Glass * 262f255d31fSSimon Glass * This interface is for low-level TPM access. It does not understand the 263f255d31fSSimon Glass * concept of localities or the various TPM messages. That interface is 264f255d31fSSimon Glass * defined in the functions later on in this file, but they all translate 265f255d31fSSimon Glass * to bytes which are sent and received. 266f255d31fSSimon Glass */ 267f255d31fSSimon Glass struct tpm_ops { 268f255d31fSSimon Glass /** 269f255d31fSSimon Glass * open() - Request access to locality 0 for the caller 270f255d31fSSimon Glass * 271f255d31fSSimon Glass * After all commands have been completed the caller should call 272f255d31fSSimon Glass * close(). 273f255d31fSSimon Glass * 274f255d31fSSimon Glass * @dev: Device to close 275f255d31fSSimon Glass * @return 0 ok OK, -ve on error 276f255d31fSSimon Glass */ 277f255d31fSSimon Glass int (*open)(struct udevice *dev); 278f255d31fSSimon Glass 279f255d31fSSimon Glass /** 280f255d31fSSimon Glass * close() - Close the current session 281f255d31fSSimon Glass * 282f255d31fSSimon Glass * Releasing the locked locality. Returns 0 on success, -ve 1 on 283f255d31fSSimon Glass * failure (in case lock removal did not succeed). 284f255d31fSSimon Glass * 285f255d31fSSimon Glass * @dev: Device to close 286f255d31fSSimon Glass * @return 0 ok OK, -ve on error 287f255d31fSSimon Glass */ 288f255d31fSSimon Glass int (*close)(struct udevice *dev); 289f255d31fSSimon Glass 290f255d31fSSimon Glass /** 291f255d31fSSimon Glass * get_desc() - Get a text description of the TPM 292f255d31fSSimon Glass * 293f255d31fSSimon Glass * @dev: Device to check 294f255d31fSSimon Glass * @buf: Buffer to put the string 295f255d31fSSimon Glass * @size: Maximum size of buffer 296f255d31fSSimon Glass * @return length of string, or -ENOSPC it no space 297f255d31fSSimon Glass */ 298f255d31fSSimon Glass int (*get_desc)(struct udevice *dev, char *buf, int size); 299f255d31fSSimon Glass 300f255d31fSSimon Glass /** 301f255d31fSSimon Glass * send() - send data to the TPM 302f255d31fSSimon Glass * 303f255d31fSSimon Glass * @dev: Device to talk to 304f255d31fSSimon Glass * @sendbuf: Buffer of the data to send 305f255d31fSSimon Glass * @send_size: Size of the data to send 306f255d31fSSimon Glass * 307f255d31fSSimon Glass * Returns 0 on success or -ve on failure. 308f255d31fSSimon Glass */ 309f255d31fSSimon Glass int (*send)(struct udevice *dev, const uint8_t *sendbuf, 310f255d31fSSimon Glass size_t send_size); 311f255d31fSSimon Glass 312f255d31fSSimon Glass /** 313f255d31fSSimon Glass * recv() - receive a response from the TPM 314f255d31fSSimon Glass * 315f255d31fSSimon Glass * @dev: Device to talk to 316f255d31fSSimon Glass * @recvbuf: Buffer to save the response to 317f255d31fSSimon Glass * @max_size: Maximum number of bytes to receive 318f255d31fSSimon Glass * 319f255d31fSSimon Glass * Returns number of bytes received on success, -EAGAIN if the TPM 320f255d31fSSimon Glass * response is not ready, -EINTR if cancelled, or other -ve value on 321f255d31fSSimon Glass * failure. 322f255d31fSSimon Glass */ 323f255d31fSSimon Glass int (*recv)(struct udevice *dev, uint8_t *recvbuf, size_t max_size); 324f255d31fSSimon Glass 325f255d31fSSimon Glass /** 326f255d31fSSimon Glass * cleanup() - clean up after an operation in progress 327f255d31fSSimon Glass * 328f255d31fSSimon Glass * This is called if receiving times out. The TPM may need to abort 329f255d31fSSimon Glass * the current transaction if it did not complete, and make itself 330f255d31fSSimon Glass * ready for another. 331f255d31fSSimon Glass * 332f255d31fSSimon Glass * @dev: Device to talk to 333f255d31fSSimon Glass */ 334f255d31fSSimon Glass int (*cleanup)(struct udevice *dev); 335f255d31fSSimon Glass 336f255d31fSSimon Glass /** 337f255d31fSSimon Glass * xfer() - send data to the TPM and get response 338f255d31fSSimon Glass * 339f255d31fSSimon Glass * This method is optional. If it exists it is used in preference 340f255d31fSSimon Glass * to send(), recv() and cleanup(). It should handle all aspects of 341f255d31fSSimon Glass * TPM communication for a single transfer. 342f255d31fSSimon Glass * 343f255d31fSSimon Glass * @dev: Device to talk to 344f255d31fSSimon Glass * @sendbuf: Buffer of the data to send 345f255d31fSSimon Glass * @send_size: Size of the data to send 346f255d31fSSimon Glass * @recvbuf: Buffer to save the response to 347f255d31fSSimon Glass * @recv_size: Pointer to the size of the response buffer 348f255d31fSSimon Glass * 349f255d31fSSimon Glass * Returns 0 on success (and places the number of response bytes at 350f255d31fSSimon Glass * recv_size) or -ve on failure. 351f255d31fSSimon Glass */ 352f255d31fSSimon Glass int (*xfer)(struct udevice *dev, const uint8_t *sendbuf, 353f255d31fSSimon Glass size_t send_size, uint8_t *recvbuf, size_t *recv_size); 354f255d31fSSimon Glass }; 355f255d31fSSimon Glass 356f255d31fSSimon Glass #define tpm_get_ops(dev) ((struct tpm_ops *)device_get_ops(dev)) 357f255d31fSSimon Glass 358f255d31fSSimon Glass /** 359f255d31fSSimon Glass * tpm_open() - Request access to locality 0 for the caller 360f255d31fSSimon Glass * 361f255d31fSSimon Glass * After all commands have been completed the caller is supposed to 362f255d31fSSimon Glass * call tpm_close(). 363f255d31fSSimon Glass * 364f255d31fSSimon Glass * Returns 0 on success, -ve on failure. 365f255d31fSSimon Glass */ 366f255d31fSSimon Glass int tpm_open(struct udevice *dev); 367f255d31fSSimon Glass 368f255d31fSSimon Glass /** 369f255d31fSSimon Glass * tpm_close() - Close the current session 370f255d31fSSimon Glass * 371f255d31fSSimon Glass * Releasing the locked locality. Returns 0 on success, -ve 1 on 372f255d31fSSimon Glass * failure (in case lock removal did not succeed). 373f255d31fSSimon Glass */ 374f255d31fSSimon Glass int tpm_close(struct udevice *dev); 375f255d31fSSimon Glass 376f255d31fSSimon Glass /** 377f255d31fSSimon Glass * tpm_get_desc() - Get a text description of the TPM 378f255d31fSSimon Glass * 379f255d31fSSimon Glass * @dev: Device to check 380f255d31fSSimon Glass * @buf: Buffer to put the string 381f255d31fSSimon Glass * @size: Maximum size of buffer 382f255d31fSSimon Glass * @return length of string, or -ENOSPC it no space 383f255d31fSSimon Glass */ 384f255d31fSSimon Glass int tpm_get_desc(struct udevice *dev, char *buf, int size); 385f255d31fSSimon Glass 386f255d31fSSimon Glass /** 387f255d31fSSimon Glass * tpm_xfer() - send data to the TPM and get response 388f255d31fSSimon Glass * 389f255d31fSSimon Glass * This first uses the device's send() method to send the bytes. Then it calls 390f255d31fSSimon Glass * recv() to get the reply. If recv() returns -EAGAIN then it will delay a 391f255d31fSSimon Glass * short time and then call recv() again. 392f255d31fSSimon Glass * 393f255d31fSSimon Glass * Regardless of whether recv() completes successfully, it will then call 394f255d31fSSimon Glass * cleanup() to finish the transaction. 395f255d31fSSimon Glass * 396f255d31fSSimon Glass * Note that the outgoing data is inspected to determine command type 397f255d31fSSimon Glass * (ordinal) and a timeout is used for that command type. 398f255d31fSSimon Glass * 399f255d31fSSimon Glass * @sendbuf - buffer of the data to send 400f255d31fSSimon Glass * @send_size size of the data to send 401f255d31fSSimon Glass * @recvbuf - memory to save the response to 402f255d31fSSimon Glass * @recv_len - pointer to the size of the response buffer 403f255d31fSSimon Glass * 404f255d31fSSimon Glass * Returns 0 on success (and places the number of response bytes at 405f255d31fSSimon Glass * recv_len) or -ve on failure. 406f255d31fSSimon Glass */ 407f255d31fSSimon Glass int tpm_xfer(struct udevice *dev, const uint8_t *sendbuf, size_t send_size, 408f255d31fSSimon Glass uint8_t *recvbuf, size_t *recv_size); 409f255d31fSSimon Glass 410be6c1529SReinhard Pfau /** 4118732b070SChe-liang Chiou * Initialize TPM device. It must be called before any TPM commands. 4125e124724SVadim Bendebury * 4138732b070SChe-liang Chiou * @return 0 on success, non-0 on error. 4145e124724SVadim Bendebury */ 415c8a8c510SSimon Glass int tpm_init(void); 4165e124724SVadim Bendebury 4178732b070SChe-liang Chiou /** 4188732b070SChe-liang Chiou * Issue a TPM_Startup command. 4195e124724SVadim Bendebury * 4208732b070SChe-liang Chiou * @param mode TPM startup mode 4218732b070SChe-liang Chiou * @return return code of the operation 4225e124724SVadim Bendebury */ 4238732b070SChe-liang Chiou uint32_t tpm_startup(enum tpm_startup_type mode); 4245e124724SVadim Bendebury 4258732b070SChe-liang Chiou /** 4268732b070SChe-liang Chiou * Issue a TPM_SelfTestFull command. 4275e124724SVadim Bendebury * 4288732b070SChe-liang Chiou * @return return code of the operation 4295e124724SVadim Bendebury */ 4308732b070SChe-liang Chiou uint32_t tpm_self_test_full(void); 4315e124724SVadim Bendebury 4328732b070SChe-liang Chiou /** 4338732b070SChe-liang Chiou * Issue a TPM_ContinueSelfTest command. 4348732b070SChe-liang Chiou * 4358732b070SChe-liang Chiou * @return return code of the operation 4368732b070SChe-liang Chiou */ 4378732b070SChe-liang Chiou uint32_t tpm_continue_self_test(void); 4388732b070SChe-liang Chiou 4398732b070SChe-liang Chiou /** 4408732b070SChe-liang Chiou * Issue a TPM_NV_DefineSpace command. The implementation is limited 4418732b070SChe-liang Chiou * to specify TPM_NV_ATTRIBUTES and size of the area. The area index 4428732b070SChe-liang Chiou * could be one of the special value listed in enum tpm_nv_index. 4438732b070SChe-liang Chiou * 4448732b070SChe-liang Chiou * @param index index of the area 4458732b070SChe-liang Chiou * @param perm TPM_NV_ATTRIBUTES of the area 4468732b070SChe-liang Chiou * @param size size of the area 4478732b070SChe-liang Chiou * @return return code of the operation 4488732b070SChe-liang Chiou */ 4498732b070SChe-liang Chiou uint32_t tpm_nv_define_space(uint32_t index, uint32_t perm, uint32_t size); 4508732b070SChe-liang Chiou 4518732b070SChe-liang Chiou /** 4528732b070SChe-liang Chiou * Issue a TPM_NV_ReadValue command. This implementation is limited 4538732b070SChe-liang Chiou * to read the area from offset 0. The area index could be one of 4548732b070SChe-liang Chiou * the special value listed in enum tpm_nv_index. 4558732b070SChe-liang Chiou * 4568732b070SChe-liang Chiou * @param index index of the area 4578732b070SChe-liang Chiou * @param data output buffer of the area contents 4588732b070SChe-liang Chiou * @param count size of output buffer 4598732b070SChe-liang Chiou * @return return code of the operation 4608732b070SChe-liang Chiou */ 4618732b070SChe-liang Chiou uint32_t tpm_nv_read_value(uint32_t index, void *data, uint32_t count); 4628732b070SChe-liang Chiou 4638732b070SChe-liang Chiou /** 4648732b070SChe-liang Chiou * Issue a TPM_NV_WriteValue command. This implementation is limited 4658732b070SChe-liang Chiou * to write the area from offset 0. The area index could be one of 4668732b070SChe-liang Chiou * the special value listed in enum tpm_nv_index. 4678732b070SChe-liang Chiou * 4688732b070SChe-liang Chiou * @param index index of the area 4698732b070SChe-liang Chiou * @param data input buffer to be wrote to the area 4708732b070SChe-liang Chiou * @param length length of data bytes of input buffer 4718732b070SChe-liang Chiou * @return return code of the operation 4728732b070SChe-liang Chiou */ 4738732b070SChe-liang Chiou uint32_t tpm_nv_write_value(uint32_t index, const void *data, uint32_t length); 4748732b070SChe-liang Chiou 4758732b070SChe-liang Chiou /** 4768732b070SChe-liang Chiou * Issue a TPM_Extend command. 4778732b070SChe-liang Chiou * 4788732b070SChe-liang Chiou * @param index index of the PCR 4798732b070SChe-liang Chiou * @param in_digest 160-bit value representing the event to be 4808732b070SChe-liang Chiou * recorded 4818732b070SChe-liang Chiou * @param out_digest 160-bit PCR value after execution of the 4828732b070SChe-liang Chiou * command 4838732b070SChe-liang Chiou * @return return code of the operation 4848732b070SChe-liang Chiou */ 4858732b070SChe-liang Chiou uint32_t tpm_extend(uint32_t index, const void *in_digest, void *out_digest); 4868732b070SChe-liang Chiou 4878732b070SChe-liang Chiou /** 4888732b070SChe-liang Chiou * Issue a TPM_PCRRead command. 4898732b070SChe-liang Chiou * 4908732b070SChe-liang Chiou * @param index index of the PCR 4918732b070SChe-liang Chiou * @param data output buffer for contents of the named PCR 4928732b070SChe-liang Chiou * @param count size of output buffer 4938732b070SChe-liang Chiou * @return return code of the operation 4948732b070SChe-liang Chiou */ 4958732b070SChe-liang Chiou uint32_t tpm_pcr_read(uint32_t index, void *data, size_t count); 4968732b070SChe-liang Chiou 4978732b070SChe-liang Chiou /** 4988732b070SChe-liang Chiou * Issue a TSC_PhysicalPresence command. TPM physical presence flag 4998732b070SChe-liang Chiou * is bit-wise OR'ed of flags listed in enum tpm_physical_presence. 5008732b070SChe-liang Chiou * 5018732b070SChe-liang Chiou * @param presence TPM physical presence flag 5028732b070SChe-liang Chiou * @return return code of the operation 5038732b070SChe-liang Chiou */ 5048732b070SChe-liang Chiou uint32_t tpm_tsc_physical_presence(uint16_t presence); 5058732b070SChe-liang Chiou 5068732b070SChe-liang Chiou /** 5078732b070SChe-liang Chiou * Issue a TPM_ReadPubek command. 5088732b070SChe-liang Chiou * 5098732b070SChe-liang Chiou * @param data output buffer for the public endorsement key 5108732b070SChe-liang Chiou * @param count size of ouput buffer 5118732b070SChe-liang Chiou * @return return code of the operation 5128732b070SChe-liang Chiou */ 5138732b070SChe-liang Chiou uint32_t tpm_read_pubek(void *data, size_t count); 5148732b070SChe-liang Chiou 5158732b070SChe-liang Chiou /** 5168732b070SChe-liang Chiou * Issue a TPM_ForceClear command. 5178732b070SChe-liang Chiou * 5188732b070SChe-liang Chiou * @return return code of the operation 5198732b070SChe-liang Chiou */ 5208732b070SChe-liang Chiou uint32_t tpm_force_clear(void); 5218732b070SChe-liang Chiou 5228732b070SChe-liang Chiou /** 5238732b070SChe-liang Chiou * Issue a TPM_PhysicalEnable command. 5248732b070SChe-liang Chiou * 5258732b070SChe-liang Chiou * @return return code of the operation 5268732b070SChe-liang Chiou */ 5278732b070SChe-liang Chiou uint32_t tpm_physical_enable(void); 5288732b070SChe-liang Chiou 5298732b070SChe-liang Chiou /** 5308732b070SChe-liang Chiou * Issue a TPM_PhysicalDisable command. 5318732b070SChe-liang Chiou * 5328732b070SChe-liang Chiou * @return return code of the operation 5338732b070SChe-liang Chiou */ 5348732b070SChe-liang Chiou uint32_t tpm_physical_disable(void); 5358732b070SChe-liang Chiou 5368732b070SChe-liang Chiou /** 5378732b070SChe-liang Chiou * Issue a TPM_PhysicalSetDeactivated command. 5388732b070SChe-liang Chiou * 5398732b070SChe-liang Chiou * @param state boolean state of the deactivated flag 5408732b070SChe-liang Chiou * @return return code of the operation 5418732b070SChe-liang Chiou */ 5428732b070SChe-liang Chiou uint32_t tpm_physical_set_deactivated(uint8_t state); 5438732b070SChe-liang Chiou 5448732b070SChe-liang Chiou /** 5458732b070SChe-liang Chiou * Issue a TPM_GetCapability command. This implementation is limited 5468732b070SChe-liang Chiou * to query sub_cap index that is 4-byte wide. 5478732b070SChe-liang Chiou * 5488732b070SChe-liang Chiou * @param cap_area partition of capabilities 5498732b070SChe-liang Chiou * @param sub_cap further definition of capability, which is 5508732b070SChe-liang Chiou * limited to be 4-byte wide 5518732b070SChe-liang Chiou * @param cap output buffer for capability information 5528732b070SChe-liang Chiou * @param count size of ouput buffer 5538732b070SChe-liang Chiou * @return return code of the operation 5548732b070SChe-liang Chiou */ 5558732b070SChe-liang Chiou uint32_t tpm_get_capability(uint32_t cap_area, uint32_t sub_cap, 5568732b070SChe-liang Chiou void *cap, size_t count); 5578732b070SChe-liang Chiou 558be6c1529SReinhard Pfau /** 559be6c1529SReinhard Pfau * Issue a TPM_FlushSpecific command for a AUTH ressource. 560be6c1529SReinhard Pfau * 561be6c1529SReinhard Pfau * @param auth_handle handle of the auth session 562be6c1529SReinhard Pfau * @return return code of the operation 563be6c1529SReinhard Pfau */ 564be6c1529SReinhard Pfau uint32_t tpm_terminate_auth_session(uint32_t auth_handle); 565be6c1529SReinhard Pfau 566be6c1529SReinhard Pfau /** 567be6c1529SReinhard Pfau * Issue a TPM_OIAP command to setup an object independant authorization 568be6c1529SReinhard Pfau * session. 569be6c1529SReinhard Pfau * Information about the session is stored internally. 570be6c1529SReinhard Pfau * If there was already an OIAP session active it is terminated and a new 571be6c1529SReinhard Pfau * session is set up. 572be6c1529SReinhard Pfau * 573be6c1529SReinhard Pfau * @param auth_handle pointer to the (new) auth handle or NULL. 574be6c1529SReinhard Pfau * @return return code of the operation 575be6c1529SReinhard Pfau */ 576be6c1529SReinhard Pfau uint32_t tpm_oiap(uint32_t *auth_handle); 577be6c1529SReinhard Pfau 578be6c1529SReinhard Pfau /** 579be6c1529SReinhard Pfau * Ends an active OIAP session. 580be6c1529SReinhard Pfau * 581be6c1529SReinhard Pfau * @return return code of the operation 582be6c1529SReinhard Pfau */ 583be6c1529SReinhard Pfau uint32_t tpm_end_oiap(void); 584be6c1529SReinhard Pfau 585be6c1529SReinhard Pfau /** 586be6c1529SReinhard Pfau * Issue a TPM_LoadKey2 (Auth1) command using an OIAP session for authenticating 587be6c1529SReinhard Pfau * the usage of the parent key. 588be6c1529SReinhard Pfau * 589be6c1529SReinhard Pfau * @param parent_handle handle of the parent key. 590be6c1529SReinhard Pfau * @param key pointer to the key structure (TPM_KEY or TPM_KEY12). 591be6c1529SReinhard Pfau * @param key_length size of the key structure 592be6c1529SReinhard Pfau * @param parent_key_usage_auth usage auth for the parent key 593be6c1529SReinhard Pfau * @param key_handle pointer to the key handle 594be6c1529SReinhard Pfau * @return return code of the operation 595be6c1529SReinhard Pfau */ 596be6c1529SReinhard Pfau uint32_t tpm_load_key2_oiap(uint32_t parent_handle, 597be6c1529SReinhard Pfau const void *key, size_t key_length, 598be6c1529SReinhard Pfau const void *parent_key_usage_auth, 599be6c1529SReinhard Pfau uint32_t *key_handle); 600be6c1529SReinhard Pfau 601be6c1529SReinhard Pfau /** 602be6c1529SReinhard Pfau * Issue a TPM_GetPubKey (Auth1) command using an OIAP session for 603be6c1529SReinhard Pfau * authenticating the usage of the key. 604be6c1529SReinhard Pfau * 605be6c1529SReinhard Pfau * @param key_handle handle of the key 606be6c1529SReinhard Pfau * @param usage_auth usage auth for the key 607be6c1529SReinhard Pfau * @param pubkey pointer to the pub key buffer; may be NULL if the pubkey 608be6c1529SReinhard Pfau * should not be stored. 609be6c1529SReinhard Pfau * @param pubkey_len pointer to the pub key buffer len. On entry: the size of 610be6c1529SReinhard Pfau * the provided pubkey buffer. On successful exit: the size 611be6c1529SReinhard Pfau * of the stored TPM_PUBKEY structure (iff pubkey != NULL). 612be6c1529SReinhard Pfau * @return return code of the operation 613be6c1529SReinhard Pfau */ 614be6c1529SReinhard Pfau uint32_t tpm_get_pub_key_oiap(uint32_t key_handle, const void *usage_auth, 615be6c1529SReinhard Pfau void *pubkey, size_t *pubkey_len); 616be6c1529SReinhard Pfau 6172132f971SSimon Glass /** 6182132f971SSimon Glass * Get the TPM permanent flags value 6192132f971SSimon Glass * 6202132f971SSimon Glass * @param pflags Place to put permanent flags 6212132f971SSimon Glass * @return return code of the operation 6222132f971SSimon Glass */ 6232132f971SSimon Glass uint32_t tpm_get_permanent_flags(struct tpm_permanent_flags *pflags); 6242132f971SSimon Glass 6252132f971SSimon Glass /** 6262132f971SSimon Glass * Get the TPM permissions 6272132f971SSimon Glass * 6282132f971SSimon Glass * @param perm Returns permissions value 6292132f971SSimon Glass * @return return code of the operation 6302132f971SSimon Glass */ 6312132f971SSimon Glass uint32_t tpm_get_permissions(uint32_t index, uint32_t *perm); 6322132f971SSimon Glass 6337690be35SMario Six /** 6347690be35SMario Six * Flush a resource with a given handle and type from the TPM 6357690be35SMario Six * 6367690be35SMario Six * @param key_handle handle of the resource 6377690be35SMario Six * @param resource_type type of the resource 6387690be35SMario Six * @return return code of the operation 6397690be35SMario Six */ 6407690be35SMario Six uint32_t tpm_flush_specific(uint32_t key_handle, uint32_t resource_type); 6417690be35SMario Six 642*0f4b2ba1Smario.six@gdsys.cc #ifdef CONFIG_TPM_LOAD_KEY_BY_SHA1 643*0f4b2ba1Smario.six@gdsys.cc /** 644*0f4b2ba1Smario.six@gdsys.cc * Search for a key by usage AuthData and the hash of the parent's pub key. 645*0f4b2ba1Smario.six@gdsys.cc * 646*0f4b2ba1Smario.six@gdsys.cc * @param auth Usage auth of the key to search for 647*0f4b2ba1Smario.six@gdsys.cc * @param pubkey_digest SHA1 hash of the pub key structure of the key 648*0f4b2ba1Smario.six@gdsys.cc * @param[out] handle The handle of the key (Non-null iff found) 649*0f4b2ba1Smario.six@gdsys.cc * @return 0 if key was found in TPM; != 0 if not. 650*0f4b2ba1Smario.six@gdsys.cc */ 651*0f4b2ba1Smario.six@gdsys.cc uint32_t tpm_find_key_sha1(const uint8_t auth[20], const uint8_t 652*0f4b2ba1Smario.six@gdsys.cc pubkey_digest[20], uint32_t *handle); 653*0f4b2ba1Smario.six@gdsys.cc #endif /* CONFIG_TPM_LOAD_KEY_BY_SHA1 */ 6548732b070SChe-liang Chiou #endif /* __TPM_H */ 655