]> git.ipfire.org Git - thirdparty/u-boot.git/blob - include/tpm-common.h
usb: dwc3: add dis_u2_freeclk_exists_quirk
[thirdparty/u-boot.git] / include / tpm-common.h
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3 * Copyright (c) 2013 The Chromium OS Authors.
4 * Coypright (c) 2013 Guntermann & Drunck GmbH
5 */
6
7 #ifndef __TPM_COMMON_H
8 #define __TPM_COMMON_H
9
10 #include <command.h>
11
12 enum tpm_duration {
13 TPM_SHORT = 0,
14 TPM_MEDIUM = 1,
15 TPM_LONG = 2,
16 TPM_UNDEFINED,
17
18 TPM_DURATION_COUNT,
19 };
20
21 /*
22 * Here is a partial implementation of TPM commands. Please consult TCG Main
23 * Specification for definitions of TPM commands.
24 */
25
26 #define TPM_HEADER_SIZE 10
27
28 /* Max buffer size supported by our tpm */
29 #define TPM_DEV_BUFSIZE 1260
30
31 #define TPM_PCR_MINIMUM_DIGEST_SIZE 20
32
33 /**
34 * enum tpm_version - The version of the TPM stack to be used
35 * @TPM_V1: Use TPM v1.x stack
36 * @TPM_V2: Use TPM v2.x stack
37 */
38 enum tpm_version {
39 TPM_V1 = 0,
40 TPM_V2,
41 };
42
43 /**
44 * struct tpm_chip_priv - Information about a TPM, stored by the uclass
45 *
46 * These values must be set up by the device's probe() method before
47 * communcation is attempted. If the device has an xfer() method, this is
48 * not needed. There is no need to set up @buf.
49 *
50 * @version: TPM stack to be used
51 * @duration_ms: Length of each duration type in milliseconds
52 * @retry_time_ms: Time to wait before retrying receive
53 * @buf: Buffer used during the exchanges with the chip
54 * @pcr_count: Number of PCR per bank
55 * @pcr_select_min: Minimum size in bytes of the pcrSelect array
56 */
57 struct tpm_chip_priv {
58 enum tpm_version version;
59
60 uint duration_ms[TPM_DURATION_COUNT];
61 uint retry_time_ms;
62 u8 buf[TPM_DEV_BUFSIZE + sizeof(u8)]; /* Max buffer size + addr */
63
64 /* TPM v2 specific data */
65 uint pcr_count;
66 uint pcr_select_min;
67 };
68
69 /**
70 * struct tpm_ops - low-level TPM operations
71 *
72 * These are designed to avoid loops and delays in the driver itself. These
73 * should be handled in the uclass.
74 *
75 * In gneral you should implement everything except xfer(). Where you need
76 * complete control of the transfer, then xfer() can be provided and will
77 * override the other methods.
78 *
79 * This interface is for low-level TPM access. It does not understand the
80 * concept of localities or the various TPM messages. That interface is
81 * defined in the functions later on in this file, but they all translate
82 * to bytes which are sent and received.
83 */
84 struct tpm_ops {
85 /**
86 * open() - Request access to locality 0 for the caller
87 *
88 * After all commands have been completed the caller should call
89 * close().
90 *
91 * @dev: Device to open
92 * @return 0 ok OK, -ve on error
93 */
94 int (*open)(struct udevice *dev);
95
96 /**
97 * close() - Close the current session
98 *
99 * Releasing the locked locality. Returns 0 on success, -ve 1 on
100 * failure (in case lock removal did not succeed).
101 *
102 * @dev: Device to close
103 * @return 0 ok OK, -ve on error
104 */
105 int (*close)(struct udevice *dev);
106
107 /**
108 * get_desc() - Get a text description of the TPM
109 *
110 * @dev: Device to check
111 * @buf: Buffer to put the string
112 * @size: Maximum size of buffer
113 * @return length of string, or -ENOSPC it no space
114 */
115 int (*get_desc)(struct udevice *dev, char *buf, int size);
116
117 /**
118 * send() - send data to the TPM
119 *
120 * @dev: Device to talk to
121 * @sendbuf: Buffer of the data to send
122 * @send_size: Size of the data to send
123 *
124 * Returns 0 on success or -ve on failure.
125 */
126 int (*send)(struct udevice *dev, const u8 *sendbuf, size_t send_size);
127
128 /**
129 * recv() - receive a response from the TPM
130 *
131 * @dev: Device to talk to
132 * @recvbuf: Buffer to save the response to
133 * @max_size: Maximum number of bytes to receive
134 *
135 * Returns number of bytes received on success, -EAGAIN if the TPM
136 * response is not ready, -EINTR if cancelled, or other -ve value on
137 * failure.
138 */
139 int (*recv)(struct udevice *dev, u8 *recvbuf, size_t max_size);
140
141 /**
142 * cleanup() - clean up after an operation in progress
143 *
144 * This is called if receiving times out. The TPM may need to abort
145 * the current transaction if it did not complete, and make itself
146 * ready for another.
147 *
148 * @dev: Device to talk to
149 */
150 int (*cleanup)(struct udevice *dev);
151
152 /**
153 * xfer() - send data to the TPM and get response
154 *
155 * This method is optional. If it exists it is used in preference
156 * to send(), recv() and cleanup(). It should handle all aspects of
157 * TPM communication for a single transfer.
158 *
159 * @dev: Device to talk to
160 * @sendbuf: Buffer of the data to send
161 * @send_size: Size of the data to send
162 * @recvbuf: Buffer to save the response to
163 * @recv_size: Pointer to the size of the response buffer
164 *
165 * Returns 0 on success (and places the number of response bytes at
166 * recv_size) or -ve on failure.
167 */
168 int (*xfer)(struct udevice *dev, const u8 *sendbuf, size_t send_size,
169 u8 *recvbuf, size_t *recv_size);
170 };
171
172 #define tpm_get_ops(dev) ((struct tpm_ops *)device_get_ops(dev))
173
174 #define MAKE_TPM_CMD_ENTRY(cmd) \
175 U_BOOT_CMD_MKENT(cmd, 0, 1, do_tpm_ ## cmd, "", "")
176
177 #define TPM_COMMAND_NO_ARG(cmd) \
178 int do_##cmd(struct cmd_tbl *cmdtp, int flag, \
179 int argc, char *const argv[]) \
180 { \
181 struct udevice *dev; \
182 int rc; \
183 \
184 rc = get_tpm(&dev); \
185 if (rc) \
186 return rc; \
187 if (argc != 1) \
188 return CMD_RET_USAGE; \
189 return report_return_code(cmd(dev)); \
190 }
191
192 /**
193 * tpm_open() - Request access to locality 0 for the caller
194 *
195 * After all commands have been completed the caller is supposed to
196 * call tpm_close().
197 *
198 * @dev - TPM device
199 * Returns 0 on success, -ve on failure.
200 */
201 int tpm_open(struct udevice *dev);
202
203 /**
204 * tpm_close() - Close the current session
205 *
206 * Releasing the locked locality. Returns 0 on success, -ve 1 on
207 * failure (in case lock removal did not succeed).
208 *
209 * @dev - TPM device
210 * Returns 0 on success, -ve on failure.
211 */
212 int tpm_close(struct udevice *dev);
213
214 /**
215 * tpm_clear_and_reenable() - Force clear the TPM and reenable it
216 *
217 * @dev: TPM device
218 * @return 0 on success, -ve on failure
219 */
220 u32 tpm_clear_and_reenable(struct udevice *dev);
221
222 /**
223 * tpm_get_desc() - Get a text description of the TPM
224 *
225 * @dev: Device to check
226 * @buf: Buffer to put the string
227 * @size: Maximum size of buffer
228 * @return length of string, or -ENOSPC it no space
229 */
230 int tpm_get_desc(struct udevice *dev, char *buf, int size);
231
232 /**
233 * tpm_xfer() - send data to the TPM and get response
234 *
235 * This first uses the device's send() method to send the bytes. Then it calls
236 * recv() to get the reply. If recv() returns -EAGAIN then it will delay a
237 * short time and then call recv() again.
238 *
239 * Regardless of whether recv() completes successfully, it will then call
240 * cleanup() to finish the transaction.
241 *
242 * Note that the outgoing data is inspected to determine command type
243 * (ordinal) and a timeout is used for that command type.
244 *
245 * @dev - TPM device
246 * @sendbuf - buffer of the data to send
247 * @send_size size of the data to send
248 * @recvbuf - memory to save the response to
249 * @recv_len - pointer to the size of the response buffer
250 *
251 * Returns 0 on success (and places the number of response bytes at
252 * recv_len) or -ve on failure.
253 */
254 int tpm_xfer(struct udevice *dev, const u8 *sendbuf, size_t send_size,
255 u8 *recvbuf, size_t *recv_size);
256
257 /**
258 * Initialize TPM device. It must be called before any TPM commands.
259 *
260 * @dev - TPM device
261 * @return 0 on success, non-0 on error.
262 */
263 int tpm_init(struct udevice *dev);
264
265 /**
266 * Retrieve the array containing all the v1 (resp. v2) commands.
267 *
268 * @return a struct cmd_tbl array.
269 */
270 #if defined(CONFIG_TPM_V1)
271 struct cmd_tbl *get_tpm1_commands(unsigned int *size);
272 #else
273 static inline struct cmd_tbl *get_tpm1_commands(unsigned int *size)
274 {
275 return NULL;
276 }
277 #endif
278 #if defined(CONFIG_TPM_V2)
279 struct cmd_tbl *get_tpm2_commands(unsigned int *size);
280 #else
281 static inline struct cmd_tbl *get_tpm2_commands(unsigned int *size)
282 {
283 return NULL;
284 }
285 #endif
286
287 /**
288 * tpm_get_version() - Find the version of a TPM
289 *
290 * This checks the uclass data for a TPM device and returns the version number
291 * it supports.
292 *
293 * @dev: TPM device
294 * @return version number (TPM_V1 or TPMV2)
295 */
296 enum tpm_version tpm_get_version(struct udevice *dev);
297
298 /* Iterate on all TPM devices */
299 #define for_each_tpm_device(dev) uclass_foreach_dev_probe(UCLASS_TPM, (dev))
300
301 #endif /* __TPM_COMMON_H */