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 * enum tpm_version - The version of the TPM stack to be used
31 * @TPM_V1: Use TPM v1.x stack
32 * @TPM_V2: Use TPM v2.x stack
40 * struct tpm_chip_priv - Information about a TPM, stored by the uclass
42 * These values must be set up by the device's probe() method before
43 * communcation is attempted. If the device has an xfer() method, this is
44 * not needed. There is no need to set up @buf.
46 * @version: TPM stack to be used
47 * @duration_ms: Length of each duration type in milliseconds
48 * @retry_time_ms: Time to wait before retrying receive
49 * @buf: Buffer used during the exchanges with the chip
50 * @pcr_count: Number of PCR per bank
51 * @pcr_select_min: Minimum size in bytes of the pcrSelect array
53 struct tpm_chip_priv {
54 enum tpm_version version;
56 uint duration_ms[TPM_DURATION_COUNT];
58 u8 buf[TPM_DEV_BUFSIZE + sizeof(u8)]; /* Max buffer size + addr */
60 /* TPM v2 specific data */
66 * struct tpm_ops - low-level TPM operations
68 * These are designed to avoid loops and delays in the driver itself. These
69 * should be handled in the uclass.
71 * In gneral you should implement everything except xfer(). Where you need
72 * complete control of the transfer, then xfer() can be provided and will
73 * override the other methods.
75 * This interface is for low-level TPM access. It does not understand the
76 * concept of localities or the various TPM messages. That interface is
77 * defined in the functions later on in this file, but they all translate
78 * to bytes which are sent and received.
82 * open() - Request access to locality 0 for the caller
84 * After all commands have been completed the caller should call
87 * @dev: Device to open
88 * @return 0 ok OK, -ve on error
90 int (*open)(struct udevice *dev);
93 * close() - Close the current session
95 * Releasing the locked locality. Returns 0 on success, -ve 1 on
96 * failure (in case lock removal did not succeed).
98 * @dev: Device to close
99 * @return 0 ok OK, -ve on error
101 int (*close)(struct udevice *dev);
104 * get_desc() - Get a text description of the TPM
106 * @dev: Device to check
107 * @buf: Buffer to put the string
108 * @size: Maximum size of buffer
109 * @return length of string, or -ENOSPC it no space
111 int (*get_desc)(struct udevice *dev, char *buf, int size);
114 * send() - send data to the TPM
116 * @dev: Device to talk to
117 * @sendbuf: Buffer of the data to send
118 * @send_size: Size of the data to send
120 * Returns 0 on success or -ve on failure.
122 int (*send)(struct udevice *dev, const u8 *sendbuf, size_t send_size);
125 * recv() - receive a response from the TPM
127 * @dev: Device to talk to
128 * @recvbuf: Buffer to save the response to
129 * @max_size: Maximum number of bytes to receive
131 * Returns number of bytes received on success, -EAGAIN if the TPM
132 * response is not ready, -EINTR if cancelled, or other -ve value on
135 int (*recv)(struct udevice *dev, u8 *recvbuf, size_t max_size);
138 * cleanup() - clean up after an operation in progress
140 * This is called if receiving times out. The TPM may need to abort
141 * the current transaction if it did not complete, and make itself
144 * @dev: Device to talk to
146 int (*cleanup)(struct udevice *dev);
149 * xfer() - send data to the TPM and get response
151 * This method is optional. If it exists it is used in preference
152 * to send(), recv() and cleanup(). It should handle all aspects of
153 * TPM communication for a single transfer.
155 * @dev: Device to talk to
156 * @sendbuf: Buffer of the data to send
157 * @send_size: Size of the data to send
158 * @recvbuf: Buffer to save the response to
159 * @recv_size: Pointer to the size of the response buffer
161 * Returns 0 on success (and places the number of response bytes at
162 * recv_size) or -ve on failure.
164 int (*xfer)(struct udevice *dev, const u8 *sendbuf, size_t send_size,
165 u8 *recvbuf, size_t *recv_size);
168 #define tpm_get_ops(dev) ((struct tpm_ops *)device_get_ops(dev))
170 #define MAKE_TPM_CMD_ENTRY(cmd) \
171 U_BOOT_CMD_MKENT(cmd, 0, 1, do_tpm_ ## cmd, "", "")
173 #define TPM_COMMAND_NO_ARG(cmd) \
174 int do_##cmd(cmd_tbl_t *cmdtp, int flag, \
175 int argc, char * const argv[]) \
178 return CMD_RET_USAGE; \
179 return report_return_code(cmd()); \
183 * tpm_get_desc() - Get a text description of the TPM
185 * @dev: Device to check
186 * @buf: Buffer to put the string
187 * @size: Maximum size of buffer
188 * @return length of string, or -ENOSPC it no space
190 int tpm_get_desc(struct udevice *dev, char *buf, int size);
193 * tpm_xfer() - send data to the TPM and get response
195 * This first uses the device's send() method to send the bytes. Then it calls
196 * recv() to get the reply. If recv() returns -EAGAIN then it will delay a
197 * short time and then call recv() again.
199 * Regardless of whether recv() completes successfully, it will then call
200 * cleanup() to finish the transaction.
202 * Note that the outgoing data is inspected to determine command type
203 * (ordinal) and a timeout is used for that command type.
205 * @sendbuf - buffer of the data to send
206 * @send_size size of the data to send
207 * @recvbuf - memory to save the response to
208 * @recv_len - pointer to the size of the response buffer
210 * Returns 0 on success (and places the number of response bytes at
211 * recv_len) or -ve on failure.
213 int tpm_xfer(struct udevice *dev, const u8 *sendbuf, size_t send_size,
214 u8 *recvbuf, size_t *recv_size);
217 * Initialize TPM device. It must be called before any TPM commands.
219 * @return 0 on success, non-0 on error.
224 * Retrieve the array containing all the v1 (resp. v2) commands.
226 * @return a cmd_tbl_t array.
228 #if defined(CONFIG_TPM_V1)
229 cmd_tbl_t *get_tpm1_commands(unsigned int *size);
231 static inline cmd_tbl_t *get_tpm1_commands(unsigned int *size)
236 #if defined(CONFIG_TPM_V2)
237 cmd_tbl_t *get_tpm2_commands(unsigned int *size);
239 static inline cmd_tbl_t *get_tpm2_commands(unsigned int *size)
245 #endif /* __TPM_COMMON_H */