1*4882a593Smuzhiyun /* 2*4882a593Smuzhiyun * Copyright (c) 2013 The Chromium OS Authors. 3*4882a593Smuzhiyun * Coypright (c) 2013 Guntermann & Drunck GmbH 4*4882a593Smuzhiyun * 5*4882a593Smuzhiyun * SPDX-License-Identifier: GPL-2.0+ 6*4882a593Smuzhiyun */ 7*4882a593Smuzhiyun 8*4882a593Smuzhiyun #ifndef __TPM_H 9*4882a593Smuzhiyun #define __TPM_H 10*4882a593Smuzhiyun 11*4882a593Smuzhiyun /* 12*4882a593Smuzhiyun * Here is a partial implementation of TPM commands. Please consult TCG Main 13*4882a593Smuzhiyun * Specification for definitions of TPM commands. 14*4882a593Smuzhiyun */ 15*4882a593Smuzhiyun 16*4882a593Smuzhiyun #define TPM_HEADER_SIZE 10 17*4882a593Smuzhiyun 18*4882a593Smuzhiyun enum tpm_duration { 19*4882a593Smuzhiyun TPM_SHORT = 0, 20*4882a593Smuzhiyun TPM_MEDIUM = 1, 21*4882a593Smuzhiyun TPM_LONG = 2, 22*4882a593Smuzhiyun TPM_UNDEFINED, 23*4882a593Smuzhiyun 24*4882a593Smuzhiyun TPM_DURATION_COUNT, 25*4882a593Smuzhiyun }; 26*4882a593Smuzhiyun 27*4882a593Smuzhiyun enum tpm_startup_type { 28*4882a593Smuzhiyun TPM_ST_CLEAR = 0x0001, 29*4882a593Smuzhiyun TPM_ST_STATE = 0x0002, 30*4882a593Smuzhiyun TPM_ST_DEACTIVATED = 0x0003, 31*4882a593Smuzhiyun }; 32*4882a593Smuzhiyun 33*4882a593Smuzhiyun enum tpm_physical_presence { 34*4882a593Smuzhiyun TPM_PHYSICAL_PRESENCE_HW_DISABLE = 0x0200, 35*4882a593Smuzhiyun TPM_PHYSICAL_PRESENCE_CMD_DISABLE = 0x0100, 36*4882a593Smuzhiyun TPM_PHYSICAL_PRESENCE_LIFETIME_LOCK = 0x0080, 37*4882a593Smuzhiyun TPM_PHYSICAL_PRESENCE_HW_ENABLE = 0x0040, 38*4882a593Smuzhiyun TPM_PHYSICAL_PRESENCE_CMD_ENABLE = 0x0020, 39*4882a593Smuzhiyun TPM_PHYSICAL_PRESENCE_NOTPRESENT = 0x0010, 40*4882a593Smuzhiyun TPM_PHYSICAL_PRESENCE_PRESENT = 0x0008, 41*4882a593Smuzhiyun TPM_PHYSICAL_PRESENCE_LOCK = 0x0004, 42*4882a593Smuzhiyun }; 43*4882a593Smuzhiyun 44*4882a593Smuzhiyun enum tpm_nv_index { 45*4882a593Smuzhiyun TPM_NV_INDEX_LOCK = 0xffffffff, 46*4882a593Smuzhiyun TPM_NV_INDEX_0 = 0x00000000, 47*4882a593Smuzhiyun TPM_NV_INDEX_DIR = 0x10000001, 48*4882a593Smuzhiyun }; 49*4882a593Smuzhiyun 50*4882a593Smuzhiyun enum tpm_resource_type { 51*4882a593Smuzhiyun TPM_RT_KEY = 0x00000001, 52*4882a593Smuzhiyun TPM_RT_AUTH = 0x00000002, 53*4882a593Smuzhiyun TPM_RT_HASH = 0x00000003, 54*4882a593Smuzhiyun TPM_RT_TRANS = 0x00000004, 55*4882a593Smuzhiyun TPM_RT_CONTEXT = 0x00000005, 56*4882a593Smuzhiyun TPM_RT_COUNTER = 0x00000006, 57*4882a593Smuzhiyun TPM_RT_DELEGATE = 0x00000007, 58*4882a593Smuzhiyun TPM_RT_DAA_TPM = 0x00000008, 59*4882a593Smuzhiyun TPM_RT_DAA_V0 = 0x00000009, 60*4882a593Smuzhiyun TPM_RT_DAA_V1 = 0x0000000A, 61*4882a593Smuzhiyun }; 62*4882a593Smuzhiyun 63*4882a593Smuzhiyun enum tpm_capability_areas { 64*4882a593Smuzhiyun TPM_CAP_ORD = 0x00000001, 65*4882a593Smuzhiyun TPM_CAP_ALG = 0x00000002, 66*4882a593Smuzhiyun TPM_CAP_PID = 0x00000003, 67*4882a593Smuzhiyun TPM_CAP_FLAG = 0x00000004, 68*4882a593Smuzhiyun TPM_CAP_PROPERTY = 0x00000005, 69*4882a593Smuzhiyun TPM_CAP_VERSION = 0x00000006, 70*4882a593Smuzhiyun TPM_CAP_KEY_HANDLE = 0x00000007, 71*4882a593Smuzhiyun TPM_CAP_CHECK_LOADED = 0x00000008, 72*4882a593Smuzhiyun TPM_CAP_SYM_MODE = 0x00000009, 73*4882a593Smuzhiyun TPM_CAP_KEY_STATUS = 0x0000000C, 74*4882a593Smuzhiyun TPM_CAP_NV_LIST = 0x0000000D, 75*4882a593Smuzhiyun TPM_CAP_MFR = 0x00000010, 76*4882a593Smuzhiyun TPM_CAP_NV_INDEX = 0x00000011, 77*4882a593Smuzhiyun TPM_CAP_TRANS_ALG = 0x00000012, 78*4882a593Smuzhiyun TPM_CAP_HANDLE = 0x00000014, 79*4882a593Smuzhiyun TPM_CAP_TRANS_ES = 0x00000015, 80*4882a593Smuzhiyun TPM_CAP_AUTH_ENCRYPT = 0x00000017, 81*4882a593Smuzhiyun TPM_CAP_SELECT_SIZE = 0x00000018, 82*4882a593Smuzhiyun TPM_CAP_DA_LOGIC = 0x00000019, 83*4882a593Smuzhiyun TPM_CAP_VERSION_VAL = 0x0000001A, 84*4882a593Smuzhiyun }; 85*4882a593Smuzhiyun 86*4882a593Smuzhiyun #define TPM_NV_PER_GLOBALLOCK (1U << 15) 87*4882a593Smuzhiyun #define TPM_NV_PER_PPWRITE (1U << 0) 88*4882a593Smuzhiyun #define TPM_NV_PER_READ_STCLEAR (1U << 31) 89*4882a593Smuzhiyun #define TPM_NV_PER_WRITE_STCLEAR (1U << 14) 90*4882a593Smuzhiyun 91*4882a593Smuzhiyun enum { 92*4882a593Smuzhiyun TPM_PUBEK_SIZE = 256, 93*4882a593Smuzhiyun }; 94*4882a593Smuzhiyun 95*4882a593Smuzhiyun /** 96*4882a593Smuzhiyun * TPM return codes as defined in the TCG Main specification 97*4882a593Smuzhiyun * (TPM Main Part 2 Structures; Specification version 1.2) 98*4882a593Smuzhiyun */ 99*4882a593Smuzhiyun enum tpm_return_code { 100*4882a593Smuzhiyun TPM_BASE = 0x00000000, 101*4882a593Smuzhiyun TPM_NON_FATAL = 0x00000800, 102*4882a593Smuzhiyun TPM_SUCCESS = TPM_BASE, 103*4882a593Smuzhiyun /* TPM-defined fatal error codes */ 104*4882a593Smuzhiyun TPM_AUTHFAIL = TPM_BASE + 1, 105*4882a593Smuzhiyun TPM_BADINDEX = TPM_BASE + 2, 106*4882a593Smuzhiyun TPM_BAD_PARAMETER = TPM_BASE + 3, 107*4882a593Smuzhiyun TPM_AUDITFAILURE = TPM_BASE + 4, 108*4882a593Smuzhiyun TPM_CLEAR_DISABLED = TPM_BASE + 5, 109*4882a593Smuzhiyun TPM_DEACTIVATED = TPM_BASE + 6, 110*4882a593Smuzhiyun TPM_DISABLED = TPM_BASE + 7, 111*4882a593Smuzhiyun TPM_DISABLED_CMD = TPM_BASE + 8, 112*4882a593Smuzhiyun TPM_FAIL = TPM_BASE + 9, 113*4882a593Smuzhiyun TPM_BAD_ORDINAL = TPM_BASE + 10, 114*4882a593Smuzhiyun TPM_INSTALL_DISABLED = TPM_BASE + 11, 115*4882a593Smuzhiyun TPM_INVALID_KEYHANDLE = TPM_BASE + 12, 116*4882a593Smuzhiyun TPM_KEYNOTFOUND = TPM_BASE + 13, 117*4882a593Smuzhiyun TPM_INAPPROPRIATE_ENC = TPM_BASE + 14, 118*4882a593Smuzhiyun TPM_MIGRATE_FAIL = TPM_BASE + 15, 119*4882a593Smuzhiyun TPM_INVALID_PCR_INFO = TPM_BASE + 16, 120*4882a593Smuzhiyun TPM_NOSPACE = TPM_BASE + 17, 121*4882a593Smuzhiyun TPM_NOSRK = TPM_BASE + 18, 122*4882a593Smuzhiyun TPM_NOTSEALED_BLOB = TPM_BASE + 19, 123*4882a593Smuzhiyun TPM_OWNER_SET = TPM_BASE + 20, 124*4882a593Smuzhiyun TPM_RESOURCES = TPM_BASE + 21, 125*4882a593Smuzhiyun TPM_SHORTRANDOM = TPM_BASE + 22, 126*4882a593Smuzhiyun TPM_SIZE = TPM_BASE + 23, 127*4882a593Smuzhiyun TPM_WRONGPCRVAL = TPM_BASE + 24, 128*4882a593Smuzhiyun TPM_BAD_PARAM_SIZE = TPM_BASE + 25, 129*4882a593Smuzhiyun TPM_SHA_THREAD = TPM_BASE + 26, 130*4882a593Smuzhiyun TPM_SHA_ERROR = TPM_BASE + 27, 131*4882a593Smuzhiyun TPM_FAILEDSELFTEST = TPM_BASE + 28, 132*4882a593Smuzhiyun TPM_AUTH2FAIL = TPM_BASE + 29, 133*4882a593Smuzhiyun TPM_BADTAG = TPM_BASE + 30, 134*4882a593Smuzhiyun TPM_IOERROR = TPM_BASE + 31, 135*4882a593Smuzhiyun TPM_ENCRYPT_ERROR = TPM_BASE + 32, 136*4882a593Smuzhiyun TPM_DECRYPT_ERROR = TPM_BASE + 33, 137*4882a593Smuzhiyun TPM_INVALID_AUTHHANDLE = TPM_BASE + 34, 138*4882a593Smuzhiyun TPM_NO_ENDORSEMENT = TPM_BASE + 35, 139*4882a593Smuzhiyun TPM_INVALID_KEYUSAGE = TPM_BASE + 36, 140*4882a593Smuzhiyun TPM_WRONG_ENTITYTYPE = TPM_BASE + 37, 141*4882a593Smuzhiyun TPM_INVALID_POSTINIT = TPM_BASE + 38, 142*4882a593Smuzhiyun TPM_INAPPROPRIATE_SIG = TPM_BASE + 39, 143*4882a593Smuzhiyun TPM_BAD_KEY_PROPERTY = TPM_BASE + 40, 144*4882a593Smuzhiyun TPM_BAD_MIGRATION = TPM_BASE + 41, 145*4882a593Smuzhiyun TPM_BAD_SCHEME = TPM_BASE + 42, 146*4882a593Smuzhiyun TPM_BAD_DATASIZE = TPM_BASE + 43, 147*4882a593Smuzhiyun TPM_BAD_MODE = TPM_BASE + 44, 148*4882a593Smuzhiyun TPM_BAD_PRESENCE = TPM_BASE + 45, 149*4882a593Smuzhiyun TPM_BAD_VERSION = TPM_BASE + 46, 150*4882a593Smuzhiyun TPM_NO_WRAP_TRANSPORT = TPM_BASE + 47, 151*4882a593Smuzhiyun TPM_AUDITFAIL_UNSUCCESSFUL = TPM_BASE + 48, 152*4882a593Smuzhiyun TPM_AUDITFAIL_SUCCESSFUL = TPM_BASE + 49, 153*4882a593Smuzhiyun TPM_NOTRESETABLE = TPM_BASE + 50, 154*4882a593Smuzhiyun TPM_NOTLOCAL = TPM_BASE + 51, 155*4882a593Smuzhiyun TPM_BAD_TYPE = TPM_BASE + 52, 156*4882a593Smuzhiyun TPM_INVALID_RESOURCE = TPM_BASE + 53, 157*4882a593Smuzhiyun TPM_NOTFIPS = TPM_BASE + 54, 158*4882a593Smuzhiyun TPM_INVALID_FAMILY = TPM_BASE + 55, 159*4882a593Smuzhiyun TPM_NO_NV_PERMISSION = TPM_BASE + 56, 160*4882a593Smuzhiyun TPM_REQUIRES_SIGN = TPM_BASE + 57, 161*4882a593Smuzhiyun TPM_KEY_NOTSUPPORTED = TPM_BASE + 58, 162*4882a593Smuzhiyun TPM_AUTH_CONFLICT = TPM_BASE + 59, 163*4882a593Smuzhiyun TPM_AREA_LOCKED = TPM_BASE + 60, 164*4882a593Smuzhiyun TPM_BAD_LOCALITY = TPM_BASE + 61, 165*4882a593Smuzhiyun TPM_READ_ONLY = TPM_BASE + 62, 166*4882a593Smuzhiyun TPM_PER_NOWRITE = TPM_BASE + 63, 167*4882a593Smuzhiyun TPM_FAMILY_COUNT = TPM_BASE + 64, 168*4882a593Smuzhiyun TPM_WRITE_LOCKED = TPM_BASE + 65, 169*4882a593Smuzhiyun TPM_BAD_ATTRIBUTES = TPM_BASE + 66, 170*4882a593Smuzhiyun TPM_INVALID_STRUCTURE = TPM_BASE + 67, 171*4882a593Smuzhiyun TPM_KEY_OWNER_CONTROL = TPM_BASE + 68, 172*4882a593Smuzhiyun TPM_BAD_COUNTER = TPM_BASE + 69, 173*4882a593Smuzhiyun TPM_NOT_FULLWRITE = TPM_BASE + 70, 174*4882a593Smuzhiyun TPM_CONTEXT_GAP = TPM_BASE + 71, 175*4882a593Smuzhiyun TPM_MAXNVWRITES = TPM_BASE + 72, 176*4882a593Smuzhiyun TPM_NOOPERATOR = TPM_BASE + 73, 177*4882a593Smuzhiyun TPM_RESOURCEMISSING = TPM_BASE + 74, 178*4882a593Smuzhiyun TPM_DELEGATE_LOCK = TPM_BASE + 75, 179*4882a593Smuzhiyun TPM_DELEGATE_FAMILY = TPM_BASE + 76, 180*4882a593Smuzhiyun TPM_DELEGATE_ADMIN = TPM_BASE + 77, 181*4882a593Smuzhiyun TPM_TRANSPORT_NOTEXCLUSIVE = TPM_BASE + 78, 182*4882a593Smuzhiyun TPM_OWNER_CONTROL = TPM_BASE + 79, 183*4882a593Smuzhiyun TPM_DAA_RESOURCES = TPM_BASE + 80, 184*4882a593Smuzhiyun TPM_DAA_INPUT_DATA0 = TPM_BASE + 81, 185*4882a593Smuzhiyun TPM_DAA_INPUT_DATA1 = TPM_BASE + 82, 186*4882a593Smuzhiyun TPM_DAA_ISSUER_SETTINGS = TPM_BASE + 83, 187*4882a593Smuzhiyun TPM_DAA_TPM_SETTINGS = TPM_BASE + 84, 188*4882a593Smuzhiyun TPM_DAA_STAGE = TPM_BASE + 85, 189*4882a593Smuzhiyun TPM_DAA_ISSUER_VALIDITY = TPM_BASE + 86, 190*4882a593Smuzhiyun TPM_DAA_WRONG_W = TPM_BASE + 87, 191*4882a593Smuzhiyun TPM_BAD_HANDLE = TPM_BASE + 88, 192*4882a593Smuzhiyun TPM_BAD_DELEGATE = TPM_BASE + 89, 193*4882a593Smuzhiyun TPM_BADCONTEXT = TPM_BASE + 90, 194*4882a593Smuzhiyun TPM_TOOMANYCONTEXTS = TPM_BASE + 91, 195*4882a593Smuzhiyun TPM_MA_TICKET_SIGNATURE = TPM_BASE + 92, 196*4882a593Smuzhiyun TPM_MA_DESTINATION = TPM_BASE + 93, 197*4882a593Smuzhiyun TPM_MA_SOURCE = TPM_BASE + 94, 198*4882a593Smuzhiyun TPM_MA_AUTHORITY = TPM_BASE + 95, 199*4882a593Smuzhiyun TPM_PERMANENTEK = TPM_BASE + 97, 200*4882a593Smuzhiyun TPM_BAD_SIGNATURE = TPM_BASE + 98, 201*4882a593Smuzhiyun TPM_NOCONTEXTSPACE = TPM_BASE + 99, 202*4882a593Smuzhiyun /* TPM-defined non-fatal errors */ 203*4882a593Smuzhiyun TPM_RETRY = TPM_BASE + TPM_NON_FATAL, 204*4882a593Smuzhiyun TPM_NEEDS_SELFTEST = TPM_BASE + TPM_NON_FATAL + 1, 205*4882a593Smuzhiyun TPM_DOING_SELFTEST = TPM_BASE + TPM_NON_FATAL + 2, 206*4882a593Smuzhiyun TPM_DEFEND_LOCK_RUNNING = TPM_BASE + TPM_NON_FATAL + 3, 207*4882a593Smuzhiyun }; 208*4882a593Smuzhiyun 209*4882a593Smuzhiyun struct tpm_permanent_flags { 210*4882a593Smuzhiyun __be16 tag; 211*4882a593Smuzhiyun u8 disable; 212*4882a593Smuzhiyun u8 ownership; 213*4882a593Smuzhiyun u8 deactivated; 214*4882a593Smuzhiyun u8 read_pubek; 215*4882a593Smuzhiyun u8 disable_owner_clear; 216*4882a593Smuzhiyun u8 allow_maintenance; 217*4882a593Smuzhiyun u8 physical_presence_lifetime_lock; 218*4882a593Smuzhiyun u8 physical_presence_hw_enable; 219*4882a593Smuzhiyun u8 physical_presence_cmd_enable; 220*4882a593Smuzhiyun u8 cekp_used; 221*4882a593Smuzhiyun u8 tpm_post; 222*4882a593Smuzhiyun u8 tpm_post_lock; 223*4882a593Smuzhiyun u8 fips; 224*4882a593Smuzhiyun u8 operator; 225*4882a593Smuzhiyun u8 enable_revoke_ek; 226*4882a593Smuzhiyun u8 nv_locked; 227*4882a593Smuzhiyun u8 read_srk_pub; 228*4882a593Smuzhiyun u8 tpm_established; 229*4882a593Smuzhiyun u8 maintenance_done; 230*4882a593Smuzhiyun u8 disable_full_da_logic_info; 231*4882a593Smuzhiyun } __packed; 232*4882a593Smuzhiyun 233*4882a593Smuzhiyun /* Max buffer size supported by our tpm */ 234*4882a593Smuzhiyun #define TPM_DEV_BUFSIZE 1260 235*4882a593Smuzhiyun 236*4882a593Smuzhiyun /** 237*4882a593Smuzhiyun * struct tpm_chip_priv - Information about a TPM, stored by the uclass 238*4882a593Smuzhiyun * 239*4882a593Smuzhiyun * These values must be set up by the device's probe() method before 240*4882a593Smuzhiyun * communcation is attempted. If the device has an xfer() method, this is 241*4882a593Smuzhiyun * not needed. There is no need to set up @buf. 242*4882a593Smuzhiyun * 243*4882a593Smuzhiyun * @duration_ms: Length of each duration type in milliseconds 244*4882a593Smuzhiyun * @retry_time_ms: Time to wait before retrying receive 245*4882a593Smuzhiyun */ 246*4882a593Smuzhiyun struct tpm_chip_priv { 247*4882a593Smuzhiyun uint duration_ms[TPM_DURATION_COUNT]; 248*4882a593Smuzhiyun uint retry_time_ms; 249*4882a593Smuzhiyun u8 buf[TPM_DEV_BUFSIZE + sizeof(u8)]; /* Max buffer size + addr */ 250*4882a593Smuzhiyun }; 251*4882a593Smuzhiyun 252*4882a593Smuzhiyun /** 253*4882a593Smuzhiyun * struct tpm_ops - low-level TPM operations 254*4882a593Smuzhiyun * 255*4882a593Smuzhiyun * These are designed to avoid loops and delays in the driver itself. These 256*4882a593Smuzhiyun * should be handled in the uclass. 257*4882a593Smuzhiyun * 258*4882a593Smuzhiyun * In gneral you should implement everything except xfer(). Where you need 259*4882a593Smuzhiyun * complete control of the transfer, then xfer() can be provided and will 260*4882a593Smuzhiyun * override the other methods. 261*4882a593Smuzhiyun * 262*4882a593Smuzhiyun * This interface is for low-level TPM access. It does not understand the 263*4882a593Smuzhiyun * concept of localities or the various TPM messages. That interface is 264*4882a593Smuzhiyun * defined in the functions later on in this file, but they all translate 265*4882a593Smuzhiyun * to bytes which are sent and received. 266*4882a593Smuzhiyun */ 267*4882a593Smuzhiyun struct tpm_ops { 268*4882a593Smuzhiyun /** 269*4882a593Smuzhiyun * open() - Request access to locality 0 for the caller 270*4882a593Smuzhiyun * 271*4882a593Smuzhiyun * After all commands have been completed the caller should call 272*4882a593Smuzhiyun * close(). 273*4882a593Smuzhiyun * 274*4882a593Smuzhiyun * @dev: Device to close 275*4882a593Smuzhiyun * @return 0 ok OK, -ve on error 276*4882a593Smuzhiyun */ 277*4882a593Smuzhiyun int (*open)(struct udevice *dev); 278*4882a593Smuzhiyun 279*4882a593Smuzhiyun /** 280*4882a593Smuzhiyun * close() - Close the current session 281*4882a593Smuzhiyun * 282*4882a593Smuzhiyun * Releasing the locked locality. Returns 0 on success, -ve 1 on 283*4882a593Smuzhiyun * failure (in case lock removal did not succeed). 284*4882a593Smuzhiyun * 285*4882a593Smuzhiyun * @dev: Device to close 286*4882a593Smuzhiyun * @return 0 ok OK, -ve on error 287*4882a593Smuzhiyun */ 288*4882a593Smuzhiyun int (*close)(struct udevice *dev); 289*4882a593Smuzhiyun 290*4882a593Smuzhiyun /** 291*4882a593Smuzhiyun * get_desc() - Get a text description of the TPM 292*4882a593Smuzhiyun * 293*4882a593Smuzhiyun * @dev: Device to check 294*4882a593Smuzhiyun * @buf: Buffer to put the string 295*4882a593Smuzhiyun * @size: Maximum size of buffer 296*4882a593Smuzhiyun * @return length of string, or -ENOSPC it no space 297*4882a593Smuzhiyun */ 298*4882a593Smuzhiyun int (*get_desc)(struct udevice *dev, char *buf, int size); 299*4882a593Smuzhiyun 300*4882a593Smuzhiyun /** 301*4882a593Smuzhiyun * send() - send data to the TPM 302*4882a593Smuzhiyun * 303*4882a593Smuzhiyun * @dev: Device to talk to 304*4882a593Smuzhiyun * @sendbuf: Buffer of the data to send 305*4882a593Smuzhiyun * @send_size: Size of the data to send 306*4882a593Smuzhiyun * 307*4882a593Smuzhiyun * Returns 0 on success or -ve on failure. 308*4882a593Smuzhiyun */ 309*4882a593Smuzhiyun int (*send)(struct udevice *dev, const uint8_t *sendbuf, 310*4882a593Smuzhiyun size_t send_size); 311*4882a593Smuzhiyun 312*4882a593Smuzhiyun /** 313*4882a593Smuzhiyun * recv() - receive a response from the TPM 314*4882a593Smuzhiyun * 315*4882a593Smuzhiyun * @dev: Device to talk to 316*4882a593Smuzhiyun * @recvbuf: Buffer to save the response to 317*4882a593Smuzhiyun * @max_size: Maximum number of bytes to receive 318*4882a593Smuzhiyun * 319*4882a593Smuzhiyun * Returns number of bytes received on success, -EAGAIN if the TPM 320*4882a593Smuzhiyun * response is not ready, -EINTR if cancelled, or other -ve value on 321*4882a593Smuzhiyun * failure. 322*4882a593Smuzhiyun */ 323*4882a593Smuzhiyun int (*recv)(struct udevice *dev, uint8_t *recvbuf, size_t max_size); 324*4882a593Smuzhiyun 325*4882a593Smuzhiyun /** 326*4882a593Smuzhiyun * cleanup() - clean up after an operation in progress 327*4882a593Smuzhiyun * 328*4882a593Smuzhiyun * This is called if receiving times out. The TPM may need to abort 329*4882a593Smuzhiyun * the current transaction if it did not complete, and make itself 330*4882a593Smuzhiyun * ready for another. 331*4882a593Smuzhiyun * 332*4882a593Smuzhiyun * @dev: Device to talk to 333*4882a593Smuzhiyun */ 334*4882a593Smuzhiyun int (*cleanup)(struct udevice *dev); 335*4882a593Smuzhiyun 336*4882a593Smuzhiyun /** 337*4882a593Smuzhiyun * xfer() - send data to the TPM and get response 338*4882a593Smuzhiyun * 339*4882a593Smuzhiyun * This method is optional. If it exists it is used in preference 340*4882a593Smuzhiyun * to send(), recv() and cleanup(). It should handle all aspects of 341*4882a593Smuzhiyun * TPM communication for a single transfer. 342*4882a593Smuzhiyun * 343*4882a593Smuzhiyun * @dev: Device to talk to 344*4882a593Smuzhiyun * @sendbuf: Buffer of the data to send 345*4882a593Smuzhiyun * @send_size: Size of the data to send 346*4882a593Smuzhiyun * @recvbuf: Buffer to save the response to 347*4882a593Smuzhiyun * @recv_size: Pointer to the size of the response buffer 348*4882a593Smuzhiyun * 349*4882a593Smuzhiyun * Returns 0 on success (and places the number of response bytes at 350*4882a593Smuzhiyun * recv_size) or -ve on failure. 351*4882a593Smuzhiyun */ 352*4882a593Smuzhiyun int (*xfer)(struct udevice *dev, const uint8_t *sendbuf, 353*4882a593Smuzhiyun size_t send_size, uint8_t *recvbuf, size_t *recv_size); 354*4882a593Smuzhiyun }; 355*4882a593Smuzhiyun 356*4882a593Smuzhiyun #define tpm_get_ops(dev) ((struct tpm_ops *)device_get_ops(dev)) 357*4882a593Smuzhiyun 358*4882a593Smuzhiyun /** 359*4882a593Smuzhiyun * tpm_open() - Request access to locality 0 for the caller 360*4882a593Smuzhiyun * 361*4882a593Smuzhiyun * After all commands have been completed the caller is supposed to 362*4882a593Smuzhiyun * call tpm_close(). 363*4882a593Smuzhiyun * 364*4882a593Smuzhiyun * Returns 0 on success, -ve on failure. 365*4882a593Smuzhiyun */ 366*4882a593Smuzhiyun int tpm_open(struct udevice *dev); 367*4882a593Smuzhiyun 368*4882a593Smuzhiyun /** 369*4882a593Smuzhiyun * tpm_close() - Close the current session 370*4882a593Smuzhiyun * 371*4882a593Smuzhiyun * Releasing the locked locality. Returns 0 on success, -ve 1 on 372*4882a593Smuzhiyun * failure (in case lock removal did not succeed). 373*4882a593Smuzhiyun */ 374*4882a593Smuzhiyun int tpm_close(struct udevice *dev); 375*4882a593Smuzhiyun 376*4882a593Smuzhiyun /** 377*4882a593Smuzhiyun * tpm_get_desc() - Get a text description of the TPM 378*4882a593Smuzhiyun * 379*4882a593Smuzhiyun * @dev: Device to check 380*4882a593Smuzhiyun * @buf: Buffer to put the string 381*4882a593Smuzhiyun * @size: Maximum size of buffer 382*4882a593Smuzhiyun * @return length of string, or -ENOSPC it no space 383*4882a593Smuzhiyun */ 384*4882a593Smuzhiyun int tpm_get_desc(struct udevice *dev, char *buf, int size); 385*4882a593Smuzhiyun 386*4882a593Smuzhiyun /** 387*4882a593Smuzhiyun * tpm_xfer() - send data to the TPM and get response 388*4882a593Smuzhiyun * 389*4882a593Smuzhiyun * This first uses the device's send() method to send the bytes. Then it calls 390*4882a593Smuzhiyun * recv() to get the reply. If recv() returns -EAGAIN then it will delay a 391*4882a593Smuzhiyun * short time and then call recv() again. 392*4882a593Smuzhiyun * 393*4882a593Smuzhiyun * Regardless of whether recv() completes successfully, it will then call 394*4882a593Smuzhiyun * cleanup() to finish the transaction. 395*4882a593Smuzhiyun * 396*4882a593Smuzhiyun * Note that the outgoing data is inspected to determine command type 397*4882a593Smuzhiyun * (ordinal) and a timeout is used for that command type. 398*4882a593Smuzhiyun * 399*4882a593Smuzhiyun * @sendbuf - buffer of the data to send 400*4882a593Smuzhiyun * @send_size size of the data to send 401*4882a593Smuzhiyun * @recvbuf - memory to save the response to 402*4882a593Smuzhiyun * @recv_len - pointer to the size of the response buffer 403*4882a593Smuzhiyun * 404*4882a593Smuzhiyun * Returns 0 on success (and places the number of response bytes at 405*4882a593Smuzhiyun * recv_len) or -ve on failure. 406*4882a593Smuzhiyun */ 407*4882a593Smuzhiyun int tpm_xfer(struct udevice *dev, const uint8_t *sendbuf, size_t send_size, 408*4882a593Smuzhiyun uint8_t *recvbuf, size_t *recv_size); 409*4882a593Smuzhiyun 410*4882a593Smuzhiyun /** 411*4882a593Smuzhiyun * Initialize TPM device. It must be called before any TPM commands. 412*4882a593Smuzhiyun * 413*4882a593Smuzhiyun * @return 0 on success, non-0 on error. 414*4882a593Smuzhiyun */ 415*4882a593Smuzhiyun int tpm_init(void); 416*4882a593Smuzhiyun 417*4882a593Smuzhiyun /** 418*4882a593Smuzhiyun * Issue a TPM_Startup command. 419*4882a593Smuzhiyun * 420*4882a593Smuzhiyun * @param mode TPM startup mode 421*4882a593Smuzhiyun * @return return code of the operation 422*4882a593Smuzhiyun */ 423*4882a593Smuzhiyun uint32_t tpm_startup(enum tpm_startup_type mode); 424*4882a593Smuzhiyun 425*4882a593Smuzhiyun /** 426*4882a593Smuzhiyun * Issue a TPM_SelfTestFull command. 427*4882a593Smuzhiyun * 428*4882a593Smuzhiyun * @return return code of the operation 429*4882a593Smuzhiyun */ 430*4882a593Smuzhiyun uint32_t tpm_self_test_full(void); 431*4882a593Smuzhiyun 432*4882a593Smuzhiyun /** 433*4882a593Smuzhiyun * Issue a TPM_ContinueSelfTest command. 434*4882a593Smuzhiyun * 435*4882a593Smuzhiyun * @return return code of the operation 436*4882a593Smuzhiyun */ 437*4882a593Smuzhiyun uint32_t tpm_continue_self_test(void); 438*4882a593Smuzhiyun 439*4882a593Smuzhiyun /** 440*4882a593Smuzhiyun * Issue a TPM_NV_DefineSpace command. The implementation is limited 441*4882a593Smuzhiyun * to specify TPM_NV_ATTRIBUTES and size of the area. The area index 442*4882a593Smuzhiyun * could be one of the special value listed in enum tpm_nv_index. 443*4882a593Smuzhiyun * 444*4882a593Smuzhiyun * @param index index of the area 445*4882a593Smuzhiyun * @param perm TPM_NV_ATTRIBUTES of the area 446*4882a593Smuzhiyun * @param size size of the area 447*4882a593Smuzhiyun * @return return code of the operation 448*4882a593Smuzhiyun */ 449*4882a593Smuzhiyun uint32_t tpm_nv_define_space(uint32_t index, uint32_t perm, uint32_t size); 450*4882a593Smuzhiyun 451*4882a593Smuzhiyun /** 452*4882a593Smuzhiyun * Issue a TPM_NV_ReadValue command. This implementation is limited 453*4882a593Smuzhiyun * to read the area from offset 0. The area index could be one of 454*4882a593Smuzhiyun * the special value listed in enum tpm_nv_index. 455*4882a593Smuzhiyun * 456*4882a593Smuzhiyun * @param index index of the area 457*4882a593Smuzhiyun * @param data output buffer of the area contents 458*4882a593Smuzhiyun * @param count size of output buffer 459*4882a593Smuzhiyun * @return return code of the operation 460*4882a593Smuzhiyun */ 461*4882a593Smuzhiyun uint32_t tpm_nv_read_value(uint32_t index, void *data, uint32_t count); 462*4882a593Smuzhiyun 463*4882a593Smuzhiyun /** 464*4882a593Smuzhiyun * Issue a TPM_NV_WriteValue command. This implementation is limited 465*4882a593Smuzhiyun * to write the area from offset 0. The area index could be one of 466*4882a593Smuzhiyun * the special value listed in enum tpm_nv_index. 467*4882a593Smuzhiyun * 468*4882a593Smuzhiyun * @param index index of the area 469*4882a593Smuzhiyun * @param data input buffer to be wrote to the area 470*4882a593Smuzhiyun * @param length length of data bytes of input buffer 471*4882a593Smuzhiyun * @return return code of the operation 472*4882a593Smuzhiyun */ 473*4882a593Smuzhiyun uint32_t tpm_nv_write_value(uint32_t index, const void *data, uint32_t length); 474*4882a593Smuzhiyun 475*4882a593Smuzhiyun /** 476*4882a593Smuzhiyun * Issue a TPM_Extend command. 477*4882a593Smuzhiyun * 478*4882a593Smuzhiyun * @param index index of the PCR 479*4882a593Smuzhiyun * @param in_digest 160-bit value representing the event to be 480*4882a593Smuzhiyun * recorded 481*4882a593Smuzhiyun * @param out_digest 160-bit PCR value after execution of the 482*4882a593Smuzhiyun * command 483*4882a593Smuzhiyun * @return return code of the operation 484*4882a593Smuzhiyun */ 485*4882a593Smuzhiyun uint32_t tpm_extend(uint32_t index, const void *in_digest, void *out_digest); 486*4882a593Smuzhiyun 487*4882a593Smuzhiyun /** 488*4882a593Smuzhiyun * Issue a TPM_PCRRead command. 489*4882a593Smuzhiyun * 490*4882a593Smuzhiyun * @param index index of the PCR 491*4882a593Smuzhiyun * @param data output buffer for contents of the named PCR 492*4882a593Smuzhiyun * @param count size of output buffer 493*4882a593Smuzhiyun * @return return code of the operation 494*4882a593Smuzhiyun */ 495*4882a593Smuzhiyun uint32_t tpm_pcr_read(uint32_t index, void *data, size_t count); 496*4882a593Smuzhiyun 497*4882a593Smuzhiyun /** 498*4882a593Smuzhiyun * Issue a TSC_PhysicalPresence command. TPM physical presence flag 499*4882a593Smuzhiyun * is bit-wise OR'ed of flags listed in enum tpm_physical_presence. 500*4882a593Smuzhiyun * 501*4882a593Smuzhiyun * @param presence TPM physical presence flag 502*4882a593Smuzhiyun * @return return code of the operation 503*4882a593Smuzhiyun */ 504*4882a593Smuzhiyun uint32_t tpm_tsc_physical_presence(uint16_t presence); 505*4882a593Smuzhiyun 506*4882a593Smuzhiyun /** 507*4882a593Smuzhiyun * Issue a TPM_ReadPubek command. 508*4882a593Smuzhiyun * 509*4882a593Smuzhiyun * @param data output buffer for the public endorsement key 510*4882a593Smuzhiyun * @param count size of ouput buffer 511*4882a593Smuzhiyun * @return return code of the operation 512*4882a593Smuzhiyun */ 513*4882a593Smuzhiyun uint32_t tpm_read_pubek(void *data, size_t count); 514*4882a593Smuzhiyun 515*4882a593Smuzhiyun /** 516*4882a593Smuzhiyun * Issue a TPM_ForceClear command. 517*4882a593Smuzhiyun * 518*4882a593Smuzhiyun * @return return code of the operation 519*4882a593Smuzhiyun */ 520*4882a593Smuzhiyun uint32_t tpm_force_clear(void); 521*4882a593Smuzhiyun 522*4882a593Smuzhiyun /** 523*4882a593Smuzhiyun * Issue a TPM_PhysicalEnable command. 524*4882a593Smuzhiyun * 525*4882a593Smuzhiyun * @return return code of the operation 526*4882a593Smuzhiyun */ 527*4882a593Smuzhiyun uint32_t tpm_physical_enable(void); 528*4882a593Smuzhiyun 529*4882a593Smuzhiyun /** 530*4882a593Smuzhiyun * Issue a TPM_PhysicalDisable command. 531*4882a593Smuzhiyun * 532*4882a593Smuzhiyun * @return return code of the operation 533*4882a593Smuzhiyun */ 534*4882a593Smuzhiyun uint32_t tpm_physical_disable(void); 535*4882a593Smuzhiyun 536*4882a593Smuzhiyun /** 537*4882a593Smuzhiyun * Issue a TPM_PhysicalSetDeactivated command. 538*4882a593Smuzhiyun * 539*4882a593Smuzhiyun * @param state boolean state of the deactivated flag 540*4882a593Smuzhiyun * @return return code of the operation 541*4882a593Smuzhiyun */ 542*4882a593Smuzhiyun uint32_t tpm_physical_set_deactivated(uint8_t state); 543*4882a593Smuzhiyun 544*4882a593Smuzhiyun /** 545*4882a593Smuzhiyun * Issue a TPM_GetCapability command. This implementation is limited 546*4882a593Smuzhiyun * to query sub_cap index that is 4-byte wide. 547*4882a593Smuzhiyun * 548*4882a593Smuzhiyun * @param cap_area partition of capabilities 549*4882a593Smuzhiyun * @param sub_cap further definition of capability, which is 550*4882a593Smuzhiyun * limited to be 4-byte wide 551*4882a593Smuzhiyun * @param cap output buffer for capability information 552*4882a593Smuzhiyun * @param count size of ouput buffer 553*4882a593Smuzhiyun * @return return code of the operation 554*4882a593Smuzhiyun */ 555*4882a593Smuzhiyun uint32_t tpm_get_capability(uint32_t cap_area, uint32_t sub_cap, 556*4882a593Smuzhiyun void *cap, size_t count); 557*4882a593Smuzhiyun 558*4882a593Smuzhiyun /** 559*4882a593Smuzhiyun * Issue a TPM_FlushSpecific command for a AUTH ressource. 560*4882a593Smuzhiyun * 561*4882a593Smuzhiyun * @param auth_handle handle of the auth session 562*4882a593Smuzhiyun * @return return code of the operation 563*4882a593Smuzhiyun */ 564*4882a593Smuzhiyun uint32_t tpm_terminate_auth_session(uint32_t auth_handle); 565*4882a593Smuzhiyun 566*4882a593Smuzhiyun /** 567*4882a593Smuzhiyun * Issue a TPM_OIAP command to setup an object independant authorization 568*4882a593Smuzhiyun * session. 569*4882a593Smuzhiyun * Information about the session is stored internally. 570*4882a593Smuzhiyun * If there was already an OIAP session active it is terminated and a new 571*4882a593Smuzhiyun * session is set up. 572*4882a593Smuzhiyun * 573*4882a593Smuzhiyun * @param auth_handle pointer to the (new) auth handle or NULL. 574*4882a593Smuzhiyun * @return return code of the operation 575*4882a593Smuzhiyun */ 576*4882a593Smuzhiyun uint32_t tpm_oiap(uint32_t *auth_handle); 577*4882a593Smuzhiyun 578*4882a593Smuzhiyun /** 579*4882a593Smuzhiyun * Ends an active OIAP session. 580*4882a593Smuzhiyun * 581*4882a593Smuzhiyun * @return return code of the operation 582*4882a593Smuzhiyun */ 583*4882a593Smuzhiyun uint32_t tpm_end_oiap(void); 584*4882a593Smuzhiyun 585*4882a593Smuzhiyun /** 586*4882a593Smuzhiyun * Issue a TPM_LoadKey2 (Auth1) command using an OIAP session for authenticating 587*4882a593Smuzhiyun * the usage of the parent key. 588*4882a593Smuzhiyun * 589*4882a593Smuzhiyun * @param parent_handle handle of the parent key. 590*4882a593Smuzhiyun * @param key pointer to the key structure (TPM_KEY or TPM_KEY12). 591*4882a593Smuzhiyun * @param key_length size of the key structure 592*4882a593Smuzhiyun * @param parent_key_usage_auth usage auth for the parent key 593*4882a593Smuzhiyun * @param key_handle pointer to the key handle 594*4882a593Smuzhiyun * @return return code of the operation 595*4882a593Smuzhiyun */ 596*4882a593Smuzhiyun uint32_t tpm_load_key2_oiap(uint32_t parent_handle, 597*4882a593Smuzhiyun const void *key, size_t key_length, 598*4882a593Smuzhiyun const void *parent_key_usage_auth, 599*4882a593Smuzhiyun uint32_t *key_handle); 600*4882a593Smuzhiyun 601*4882a593Smuzhiyun /** 602*4882a593Smuzhiyun * Issue a TPM_GetPubKey (Auth1) command using an OIAP session for 603*4882a593Smuzhiyun * authenticating the usage of the key. 604*4882a593Smuzhiyun * 605*4882a593Smuzhiyun * @param key_handle handle of the key 606*4882a593Smuzhiyun * @param usage_auth usage auth for the key 607*4882a593Smuzhiyun * @param pubkey pointer to the pub key buffer; may be NULL if the pubkey 608*4882a593Smuzhiyun * should not be stored. 609*4882a593Smuzhiyun * @param pubkey_len pointer to the pub key buffer len. On entry: the size of 610*4882a593Smuzhiyun * the provided pubkey buffer. On successful exit: the size 611*4882a593Smuzhiyun * of the stored TPM_PUBKEY structure (iff pubkey != NULL). 612*4882a593Smuzhiyun * @return return code of the operation 613*4882a593Smuzhiyun */ 614*4882a593Smuzhiyun uint32_t tpm_get_pub_key_oiap(uint32_t key_handle, const void *usage_auth, 615*4882a593Smuzhiyun void *pubkey, size_t *pubkey_len); 616*4882a593Smuzhiyun 617*4882a593Smuzhiyun /** 618*4882a593Smuzhiyun * Get the TPM permanent flags value 619*4882a593Smuzhiyun * 620*4882a593Smuzhiyun * @param pflags Place to put permanent flags 621*4882a593Smuzhiyun * @return return code of the operation 622*4882a593Smuzhiyun */ 623*4882a593Smuzhiyun uint32_t tpm_get_permanent_flags(struct tpm_permanent_flags *pflags); 624*4882a593Smuzhiyun 625*4882a593Smuzhiyun /** 626*4882a593Smuzhiyun * Get the TPM permissions 627*4882a593Smuzhiyun * 628*4882a593Smuzhiyun * @param perm Returns permissions value 629*4882a593Smuzhiyun * @return return code of the operation 630*4882a593Smuzhiyun */ 631*4882a593Smuzhiyun uint32_t tpm_get_permissions(uint32_t index, uint32_t *perm); 632*4882a593Smuzhiyun 633*4882a593Smuzhiyun /** 634*4882a593Smuzhiyun * Flush a resource with a given handle and type from the TPM 635*4882a593Smuzhiyun * 636*4882a593Smuzhiyun * @param key_handle handle of the resource 637*4882a593Smuzhiyun * @param resource_type type of the resource 638*4882a593Smuzhiyun * @return return code of the operation 639*4882a593Smuzhiyun */ 640*4882a593Smuzhiyun uint32_t tpm_flush_specific(uint32_t key_handle, uint32_t resource_type); 641*4882a593Smuzhiyun 642*4882a593Smuzhiyun #ifdef CONFIG_TPM_LOAD_KEY_BY_SHA1 643*4882a593Smuzhiyun /** 644*4882a593Smuzhiyun * Search for a key by usage AuthData and the hash of the parent's pub key. 645*4882a593Smuzhiyun * 646*4882a593Smuzhiyun * @param auth Usage auth of the key to search for 647*4882a593Smuzhiyun * @param pubkey_digest SHA1 hash of the pub key structure of the key 648*4882a593Smuzhiyun * @param[out] handle The handle of the key (Non-null iff found) 649*4882a593Smuzhiyun * @return 0 if key was found in TPM; != 0 if not. 650*4882a593Smuzhiyun */ 651*4882a593Smuzhiyun uint32_t tpm_find_key_sha1(const uint8_t auth[20], const uint8_t 652*4882a593Smuzhiyun pubkey_digest[20], uint32_t *handle); 653*4882a593Smuzhiyun #endif /* CONFIG_TPM_LOAD_KEY_BY_SHA1 */ 654*4882a593Smuzhiyun #endif /* __TPM_H */ 655