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