Merge https://source.denx.de/u-boot/custodians/u-boot-snapdragon
[u-boot.git] / include / tpm-common.h
blobbfb84a931d16525fdf1263e83ae8f62dff318fb1
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3 * Copyright (c) 2013 The Chromium OS Authors.
4 * Coypright (c) 2013 Guntermann & Drunck GmbH
5 */
7 #ifndef __TPM_COMMON_H
8 #define __TPM_COMMON_H
10 #include <command.h>
12 struct udevice;
14 enum tpm_duration {
15 TPM_SHORT = 0,
16 TPM_MEDIUM = 1,
17 TPM_LONG = 2,
18 TPM_UNDEFINED,
20 TPM_DURATION_COUNT,
24 * Here is a partial implementation of TPM commands. Please consult TCG Main
25 * Specification for definitions of TPM commands.
28 #define TPM_HEADER_SIZE 10
30 /* Max buffer size supported by our tpm */
31 #define TPM_DEV_BUFSIZE 1260
33 #define TPM_PCR_MINIMUM_DIGEST_SIZE 20
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
40 enum tpm_version {
41 TPM_V1 = 0,
42 TPM_V2,
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
50 #define TPM2_NUM_PCR_BANKS 16
52 /**
53 * struct tpm_chip_priv - Information about a TPM, stored by the uclass
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.
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)
71 struct tpm_chip_priv {
72 enum tpm_version version;
74 uint duration_ms[TPM_DURATION_COUNT];
75 uint retry_time_ms;
76 u8 buf[TPM_DEV_BUFSIZE + sizeof(u8)]; /* Max buffer size + addr */
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;
88 /**
89 * struct tpm_ops - low-level TPM operations
91 * These are designed to avoid loops and delays in the driver itself. These
92 * should be handled in the uclass.
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.
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.
103 struct tpm_ops {
105 * open() - Request access to locality 0 for the caller
107 * After all commands have been completed the caller should call
108 * close().
110 * @dev: Device to open
111 * @return 0 ok OK, -EBUSY if already opened, other -ve on other error
113 int (*open)(struct udevice *dev);
116 * close() - Close the current session
118 * Releasing the locked locality. Returns 0 on success, -ve 1 on
119 * failure (in case lock removal did not succeed).
121 * @dev: Device to close
122 * @return 0 ok OK, -ve on error
124 int (*close)(struct udevice *dev);
127 * get_desc() - Get a text description of the TPM
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
134 int (*get_desc)(struct udevice *dev, char *buf, int size);
137 * report_state() - Collect information about the current TPM state
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)
144 int (*report_state)(struct udevice *dev, char *buf, int size);
147 * send() - send data to the TPM
149 * @dev: Device to talk to
150 * @sendbuf: Buffer of the data to send
151 * @send_size: Size of the data to send
153 * Returns 0 on success or -ve on failure.
155 int (*send)(struct udevice *dev, const u8 *sendbuf, size_t send_size);
158 * recv() - receive a response from the TPM
160 * @dev: Device to talk to
161 * @recvbuf: Buffer to save the response to
162 * @max_size: Maximum number of bytes to receive
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.
168 int (*recv)(struct udevice *dev, u8 *recvbuf, size_t max_size);
171 * cleanup() - clean up after an operation in progress
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.
177 * @dev: Device to talk to
179 int (*cleanup)(struct udevice *dev);
182 * xfer() - send data to the TPM and get response
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.
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
194 * Returns 0 on success (and places the number of response bytes at
195 * recv_size) or -ve on failure.
197 int (*xfer)(struct udevice *dev, const u8 *sendbuf, size_t send_size,
198 u8 *recvbuf, size_t *recv_size);
201 #define tpm_get_ops(dev) ((struct tpm_ops *)device_get_ops(dev))
203 #define MAKE_TPM_CMD_ENTRY(cmd) \
204 U_BOOT_CMD_MKENT(cmd, 0, 1, do_tpm_ ## cmd, "", "")
206 #define TPM_COMMAND_NO_ARG(cmd) \
207 int do_##cmd(struct cmd_tbl *cmdtp, int flag, \
208 int argc, char *const argv[]) \
210 struct udevice *dev; \
211 int rc; \
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)); \
222 * tpm_open() - Request access to locality 0 for the caller
224 * After all commands have been completed the caller is supposed to
225 * call tpm_close().
227 * @dev - TPM device
228 * Returns 0 on success, -ve on failure.
230 int tpm_open(struct udevice *dev);
233 * tpm_close() - Close the current session
235 * Releasing the locked locality. Returns 0 on success, -ve 1 on
236 * failure (in case lock removal did not succeed).
238 * @dev - TPM device
239 * Returns 0 on success, -ve on failure.
241 int tpm_close(struct udevice *dev);
244 * tpm_clear_and_reenable() - Force clear the TPM and reenable it
246 * @dev: TPM device
247 * Return: 0 on success, -ve on failure
249 u32 tpm_clear_and_reenable(struct udevice *dev);
252 * tpm_get_desc() - Get a text description of the TPM
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
259 int tpm_get_desc(struct udevice *dev, char *buf, int size);
262 * tpm_report_state() - Collect information about the current TPM state
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)
269 int tpm_report_state(struct udevice *dev, char *buf, int size);
272 * tpm_xfer() - send data to the TPM and get response
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.
278 * Regardless of whether recv() completes successfully, it will then call
279 * cleanup() to finish the transaction.
281 * Note that the outgoing data is inspected to determine command type
282 * (ordinal) and a timeout is used for that command type.
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
290 * Returns 0 on success (and places the number of response bytes at
291 * recv_len) or -ve on failure.
293 int tpm_xfer(struct udevice *dev, const u8 *sendbuf, size_t send_size,
294 u8 *recvbuf, size_t *recv_size);
297 * Initialize TPM device. It must be called before any TPM commands.
299 * @dev - TPM device
300 * Return: 0 on success, non-0 on error.
302 int tpm_init(struct udevice *dev);
305 * Retrieve the array containing all the v1 (resp. v2) commands.
307 * Return: a struct cmd_tbl array.
309 #if defined(CONFIG_TPM_V1)
310 struct cmd_tbl *get_tpm1_commands(unsigned int *size);
311 #else
312 static inline struct cmd_tbl *get_tpm1_commands(unsigned int *size)
314 return NULL;
316 #endif
317 #if defined(CONFIG_TPM_V2)
318 struct cmd_tbl *get_tpm2_commands(unsigned int *size);
319 #else
320 static inline struct cmd_tbl *get_tpm2_commands(unsigned int *size)
322 return NULL;
324 #endif
327 * tpm_get_version() - Find the version of a TPM
329 * This checks the uclass data for a TPM device and returns the version number
330 * it supports.
332 * @dev: TPM device
333 * Return: version number (TPM_V1 or TPMV2)
335 enum tpm_version tpm_get_version(struct udevice *dev);
337 /* Iterate on all TPM devices */
338 #define for_each_tpm_device(dev) uclass_foreach_dev_probe(UCLASS_TPM, (dev))
340 #endif /* __TPM_COMMON_H */