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 * @buf: Buffer used during the exchanges with the chip
40 struct tpm_chip_priv {
41 uint duration_ms[TPM_DURATION_COUNT];
43 u8 buf[TPM_DEV_BUFSIZE + sizeof(u8)]; /* Max buffer size + addr */
47 * struct tpm_ops - low-level TPM operations
49 * These are designed to avoid loops and delays in the driver itself. These
50 * should be handled in the uclass.
52 * In gneral you should implement everything except xfer(). Where you need
53 * complete control of the transfer, then xfer() can be provided and will
54 * override the other methods.
56 * This interface is for low-level TPM access. It does not understand the
57 * concept of localities or the various TPM messages. That interface is
58 * defined in the functions later on in this file, but they all translate
59 * to bytes which are sent and received.
63 * open() - Request access to locality 0 for the caller
65 * After all commands have been completed the caller should call
68 * @dev: Device to close
69 * @return 0 ok OK, -ve on error
71 int (*open)(struct udevice *dev);
74 * close() - Close the current session
76 * Releasing the locked locality. Returns 0 on success, -ve 1 on
77 * failure (in case lock removal did not succeed).
79 * @dev: Device to close
80 * @return 0 ok OK, -ve on error
82 int (*close)(struct udevice *dev);
85 * get_desc() - Get a text description of the TPM
87 * @dev: Device to check
88 * @buf: Buffer to put the string
89 * @size: Maximum size of buffer
90 * @return length of string, or -ENOSPC it no space
92 int (*get_desc)(struct udevice *dev, char *buf, int size);
95 * send() - send data to the TPM
97 * @dev: Device to talk to
98 * @sendbuf: Buffer of the data to send
99 * @send_size: Size of the data to send
101 * Returns 0 on success or -ve on failure.
103 int (*send)(struct udevice *dev, const u8 *sendbuf, size_t send_size);
106 * recv() - receive a response from the TPM
108 * @dev: Device to talk to
109 * @recvbuf: Buffer to save the response to
110 * @max_size: Maximum number of bytes to receive
112 * Returns number of bytes received on success, -EAGAIN if the TPM
113 * response is not ready, -EINTR if cancelled, or other -ve value on
116 int (*recv)(struct udevice *dev, u8 *recvbuf, size_t max_size);
119 * cleanup() - clean up after an operation in progress
121 * This is called if receiving times out. The TPM may need to abort
122 * the current transaction if it did not complete, and make itself
125 * @dev: Device to talk to
127 int (*cleanup)(struct udevice *dev);
130 * xfer() - send data to the TPM and get response
132 * This method is optional. If it exists it is used in preference
133 * to send(), recv() and cleanup(). It should handle all aspects of
134 * TPM communication for a single transfer.
136 * @dev: Device to talk to
137 * @sendbuf: Buffer of the data to send
138 * @send_size: Size of the data to send
139 * @recvbuf: Buffer to save the response to
140 * @recv_size: Pointer to the size of the response buffer
142 * Returns 0 on success (and places the number of response bytes at
143 * recv_size) or -ve on failure.
145 int (*xfer)(struct udevice *dev, const u8 *sendbuf, size_t send_size,
146 u8 *recvbuf, size_t *recv_size);
149 #define tpm_get_ops(dev) ((struct tpm_ops *)device_get_ops(dev))
151 #define MAKE_TPM_CMD_ENTRY(cmd) \
152 U_BOOT_CMD_MKENT(cmd, 0, 1, do_tpm_ ## cmd, "", "")
154 #define TPM_COMMAND_NO_ARG(cmd) \
155 int do_##cmd(cmd_tbl_t *cmdtp, int flag, \
156 int argc, char * const argv[]) \
159 return CMD_RET_USAGE; \
160 return report_return_code(cmd()); \
164 * tpm_get_desc() - Get a text description of the TPM
166 * @dev: Device to check
167 * @buf: Buffer to put the string
168 * @size: Maximum size of buffer
169 * @return length of string, or -ENOSPC it no space
171 int tpm_get_desc(struct udevice *dev, char *buf, int size);
174 * tpm_xfer() - send data to the TPM and get response
176 * This first uses the device's send() method to send the bytes. Then it calls
177 * recv() to get the reply. If recv() returns -EAGAIN then it will delay a
178 * short time and then call recv() again.
180 * Regardless of whether recv() completes successfully, it will then call
181 * cleanup() to finish the transaction.
183 * Note that the outgoing data is inspected to determine command type
184 * (ordinal) and a timeout is used for that command type.
186 * @sendbuf - buffer of the data to send
187 * @send_size size of the data to send
188 * @recvbuf - memory to save the response to
189 * @recv_len - pointer to the size of the response buffer
191 * Returns 0 on success (and places the number of response bytes at
192 * recv_len) or -ve on failure.
194 int tpm_xfer(struct udevice *dev, const u8 *sendbuf, size_t send_size,
195 u8 *recvbuf, size_t *recv_size);
198 * Initialize TPM device. It must be called before any TPM commands.
200 * @return 0 on success, non-0 on error.
205 * Retrieve the array containing all the commands.
207 * @return a cmd_tbl_t array.
209 cmd_tbl_t *get_tpm_commands(unsigned int *size);
211 #endif /* __TPM_COMMON_H */