1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * Copyright (c) 2013 The Chromium OS Authors.
4 * Coypright (c) 2013 Guntermann & Drunck GmbH
20 * Here is a partial implementation of TPM commands. Please consult TCG Main
21 * Specification for definitions of TPM commands.
24 #define TPM_HEADER_SIZE 10
26 /* Max buffer size supported by our tpm */
27 #define TPM_DEV_BUFSIZE 1260
30 * struct tpm_chip_priv - Information about a TPM, stored by the uclass
32 * These values must be set up by the device's probe() method before
33 * communcation is attempted. If the device has an xfer() method, this is
34 * not needed. There is no need to set up @buf.
36 * @duration_ms: Length of each duration type in milliseconds
37 * @retry_time_ms: Time to wait before retrying receive
38 * @pcr_count: Number of PCR per bank
39 * @pcr_select_min: Minimum size in bytes of the pcrSelect array
40 * @buf: Buffer used during the exchanges with the chip
42 struct tpm_chip_priv {
43 uint duration_ms[TPM_DURATION_COUNT];
45 #if defined(CONFIG_TPM_V2)
49 u8 buf[TPM_DEV_BUFSIZE + sizeof(u8)]; /* Max buffer size + addr */
53 * struct tpm_ops - low-level TPM operations
55 * These are designed to avoid loops and delays in the driver itself. These
56 * should be handled in the uclass.
58 * In gneral you should implement everything except xfer(). Where you need
59 * complete control of the transfer, then xfer() can be provided and will
60 * override the other methods.
62 * This interface is for low-level TPM access. It does not understand the
63 * concept of localities or the various TPM messages. That interface is
64 * defined in the functions later on in this file, but they all translate
65 * to bytes which are sent and received.
69 * open() - Request access to locality 0 for the caller
71 * After all commands have been completed the caller should call
74 * @dev: Device to close
75 * @return 0 ok OK, -ve on error
77 int (*open)(struct udevice *dev);
80 * close() - Close the current session
82 * Releasing the locked locality. Returns 0 on success, -ve 1 on
83 * failure (in case lock removal did not succeed).
85 * @dev: Device to close
86 * @return 0 ok OK, -ve on error
88 int (*close)(struct udevice *dev);
91 * get_desc() - Get a text description of the TPM
93 * @dev: Device to check
94 * @buf: Buffer to put the string
95 * @size: Maximum size of buffer
96 * @return length of string, or -ENOSPC it no space
98 int (*get_desc)(struct udevice *dev, char *buf, int size);
101 * send() - send data to the TPM
103 * @dev: Device to talk to
104 * @sendbuf: Buffer of the data to send
105 * @send_size: Size of the data to send
107 * Returns 0 on success or -ve on failure.
109 int (*send)(struct udevice *dev, const u8 *sendbuf, size_t send_size);
112 * recv() - receive a response from the TPM
114 * @dev: Device to talk to
115 * @recvbuf: Buffer to save the response to
116 * @max_size: Maximum number of bytes to receive
118 * Returns number of bytes received on success, -EAGAIN if the TPM
119 * response is not ready, -EINTR if cancelled, or other -ve value on
122 int (*recv)(struct udevice *dev, u8 *recvbuf, size_t max_size);
125 * cleanup() - clean up after an operation in progress
127 * This is called if receiving times out. The TPM may need to abort
128 * the current transaction if it did not complete, and make itself
131 * @dev: Device to talk to
133 int (*cleanup)(struct udevice *dev);
136 * xfer() - send data to the TPM and get response
138 * This method is optional. If it exists it is used in preference
139 * to send(), recv() and cleanup(). It should handle all aspects of
140 * TPM communication for a single transfer.
142 * @dev: Device to talk to
143 * @sendbuf: Buffer of the data to send
144 * @send_size: Size of the data to send
145 * @recvbuf: Buffer to save the response to
146 * @recv_size: Pointer to the size of the response buffer
148 * Returns 0 on success (and places the number of response bytes at
149 * recv_size) or -ve on failure.
151 int (*xfer)(struct udevice *dev, const u8 *sendbuf, size_t send_size,
152 u8 *recvbuf, size_t *recv_size);
155 #define tpm_get_ops(dev) ((struct tpm_ops *)device_get_ops(dev))
157 #define MAKE_TPM_CMD_ENTRY(cmd) \
158 U_BOOT_CMD_MKENT(cmd, 0, 1, do_tpm_ ## cmd, "", "")
160 #define TPM_COMMAND_NO_ARG(cmd) \
161 int do_##cmd(cmd_tbl_t *cmdtp, int flag, \
162 int argc, char * const argv[]) \
165 return CMD_RET_USAGE; \
166 return report_return_code(cmd()); \
170 * tpm_get_desc() - Get a text description of the TPM
172 * @dev: Device to check
173 * @buf: Buffer to put the string
174 * @size: Maximum size of buffer
175 * @return length of string, or -ENOSPC it no space
177 int tpm_get_desc(struct udevice *dev, char *buf, int size);
180 * tpm_xfer() - send data to the TPM and get response
182 * This first uses the device's send() method to send the bytes. Then it calls
183 * recv() to get the reply. If recv() returns -EAGAIN then it will delay a
184 * short time and then call recv() again.
186 * Regardless of whether recv() completes successfully, it will then call
187 * cleanup() to finish the transaction.
189 * Note that the outgoing data is inspected to determine command type
190 * (ordinal) and a timeout is used for that command type.
192 * @sendbuf - buffer of the data to send
193 * @send_size size of the data to send
194 * @recvbuf - memory to save the response to
195 * @recv_len - pointer to the size of the response buffer
197 * Returns 0 on success (and places the number of response bytes at
198 * recv_len) or -ve on failure.
200 int tpm_xfer(struct udevice *dev, const u8 *sendbuf, size_t send_size,
201 u8 *recvbuf, size_t *recv_size);
204 * Initialize TPM device. It must be called before any TPM commands.
206 * @return 0 on success, non-0 on error.
211 * Retrieve the array containing all the commands.
213 * @return a cmd_tbl_t array.
215 cmd_tbl_t *get_tpm_commands(unsigned int *size);
217 #endif /* __TPM_COMMON_H */