1d677bfe2SMiquel Raynal /* SPDX-License-Identifier: GPL-2.0+ */
2d677bfe2SMiquel Raynal /*
3d677bfe2SMiquel Raynal * Copyright (c) 2013 The Chromium OS Authors.
4d677bfe2SMiquel Raynal * Coypright (c) 2013 Guntermann & Drunck GmbH
5d677bfe2SMiquel Raynal */
6d677bfe2SMiquel Raynal
7d677bfe2SMiquel Raynal #ifndef __TPM_COMMON_H
8d677bfe2SMiquel Raynal #define __TPM_COMMON_H
9d677bfe2SMiquel Raynal
10*d7869cecSEddie James #include <command.h>
11*d7869cecSEddie James
12*d7869cecSEddie James struct udevice;
13*d7869cecSEddie James
14d677bfe2SMiquel Raynal enum tpm_duration {
15d677bfe2SMiquel Raynal TPM_SHORT = 0,
16d677bfe2SMiquel Raynal TPM_MEDIUM = 1,
17d677bfe2SMiquel Raynal TPM_LONG = 2,
18d677bfe2SMiquel Raynal TPM_UNDEFINED,
19d677bfe2SMiquel Raynal
20d677bfe2SMiquel Raynal TPM_DURATION_COUNT,
21d677bfe2SMiquel Raynal };
22d677bfe2SMiquel Raynal
23d677bfe2SMiquel Raynal /*
24d677bfe2SMiquel Raynal * Here is a partial implementation of TPM commands. Please consult TCG Main
25d677bfe2SMiquel Raynal * Specification for definitions of TPM commands.
26d677bfe2SMiquel Raynal */
27d677bfe2SMiquel Raynal
28d677bfe2SMiquel Raynal #define TPM_HEADER_SIZE 10
29d677bfe2SMiquel Raynal
30d677bfe2SMiquel Raynal /* Max buffer size supported by our tpm */
31d677bfe2SMiquel Raynal #define TPM_DEV_BUFSIZE 1260
32d677bfe2SMiquel Raynal
3307e127d8SSimon Glass #define TPM_PCR_MINIMUM_DIGEST_SIZE 20
3407e127d8SSimon Glass
35d677bfe2SMiquel Raynal /**
362a2096eaSMiquel Raynal * enum tpm_version - The version of the TPM stack to be used
372a2096eaSMiquel Raynal * @TPM_V1: Use TPM v1.x stack
382a2096eaSMiquel Raynal * @TPM_V2: Use TPM v2.x stack
392a2096eaSMiquel Raynal */
402a2096eaSMiquel Raynal enum tpm_version {
412a2096eaSMiquel Raynal TPM_V1 = 0,
422a2096eaSMiquel Raynal TPM_V2,
432a2096eaSMiquel Raynal };
442a2096eaSMiquel Raynal
452a2096eaSMiquel Raynal /**
46d677bfe2SMiquel Raynal * struct tpm_chip_priv - Information about a TPM, stored by the uclass
47d677bfe2SMiquel Raynal *
48d677bfe2SMiquel Raynal * These values must be set up by the device's probe() method before
49d677bfe2SMiquel Raynal * communcation is attempted. If the device has an xfer() method, this is
50d677bfe2SMiquel Raynal * not needed. There is no need to set up @buf.
51d677bfe2SMiquel Raynal *
522a2096eaSMiquel Raynal * @version: TPM stack to be used
53d677bfe2SMiquel Raynal * @duration_ms: Length of each duration type in milliseconds
54d677bfe2SMiquel Raynal * @retry_time_ms: Time to wait before retrying receive
552a2096eaSMiquel Raynal * @buf: Buffer used during the exchanges with the chip
56ff32245bSMiquel Raynal * @pcr_count: Number of PCR per bank
57ff32245bSMiquel Raynal * @pcr_select_min: Minimum size in bytes of the pcrSelect array
58*d7869cecSEddie James * @plat_hier_disabled: Platform hierarchy has been disabled (TPM is locked
59*d7869cecSEddie James * down until next reboot)
60d677bfe2SMiquel Raynal */
61d677bfe2SMiquel Raynal struct tpm_chip_priv {
622a2096eaSMiquel Raynal enum tpm_version version;
632a2096eaSMiquel Raynal
64d677bfe2SMiquel Raynal uint duration_ms[TPM_DURATION_COUNT];
65d677bfe2SMiquel Raynal uint retry_time_ms;
662a2096eaSMiquel Raynal u8 buf[TPM_DEV_BUFSIZE + sizeof(u8)]; /* Max buffer size + addr */
672a2096eaSMiquel Raynal
682a2096eaSMiquel Raynal /* TPM v2 specific data */
69ff32245bSMiquel Raynal uint pcr_count;
70ff32245bSMiquel Raynal uint pcr_select_min;
71*d7869cecSEddie James bool plat_hier_disabled;
72d677bfe2SMiquel Raynal };
73d677bfe2SMiquel Raynal
74d677bfe2SMiquel Raynal /**
75d677bfe2SMiquel Raynal * struct tpm_ops - low-level TPM operations
76d677bfe2SMiquel Raynal *
77d677bfe2SMiquel Raynal * These are designed to avoid loops and delays in the driver itself. These
78d677bfe2SMiquel Raynal * should be handled in the uclass.
79d677bfe2SMiquel Raynal *
80d677bfe2SMiquel Raynal * In gneral you should implement everything except xfer(). Where you need
81d677bfe2SMiquel Raynal * complete control of the transfer, then xfer() can be provided and will
82d677bfe2SMiquel Raynal * override the other methods.
83d677bfe2SMiquel Raynal *
84d677bfe2SMiquel Raynal * This interface is for low-level TPM access. It does not understand the
85d677bfe2SMiquel Raynal * concept of localities or the various TPM messages. That interface is
86d677bfe2SMiquel Raynal * defined in the functions later on in this file, but they all translate
87d677bfe2SMiquel Raynal * to bytes which are sent and received.
88d677bfe2SMiquel Raynal */
89d677bfe2SMiquel Raynal struct tpm_ops {
90d677bfe2SMiquel Raynal /**
91d677bfe2SMiquel Raynal * open() - Request access to locality 0 for the caller
92d677bfe2SMiquel Raynal *
93d677bfe2SMiquel Raynal * After all commands have been completed the caller should call
94d677bfe2SMiquel Raynal * close().
95d677bfe2SMiquel Raynal *
96350988ffSMiquel Raynal * @dev: Device to open
97d677bfe2SMiquel Raynal * @return 0 ok OK, -ve on error
98d677bfe2SMiquel Raynal */
99d677bfe2SMiquel Raynal int (*open)(struct udevice *dev);
100d677bfe2SMiquel Raynal
101d677bfe2SMiquel Raynal /**
102d677bfe2SMiquel Raynal * close() - Close the current session
103d677bfe2SMiquel Raynal *
104d677bfe2SMiquel Raynal * Releasing the locked locality. Returns 0 on success, -ve 1 on
105d677bfe2SMiquel Raynal * failure (in case lock removal did not succeed).
106d677bfe2SMiquel Raynal *
107d677bfe2SMiquel Raynal * @dev: Device to close
108d677bfe2SMiquel Raynal * @return 0 ok OK, -ve on error
109d677bfe2SMiquel Raynal */
110d677bfe2SMiquel Raynal int (*close)(struct udevice *dev);
111d677bfe2SMiquel Raynal
112d677bfe2SMiquel Raynal /**
113d677bfe2SMiquel Raynal * get_desc() - Get a text description of the TPM
114d677bfe2SMiquel Raynal *
115d677bfe2SMiquel Raynal * @dev: Device to check
116d677bfe2SMiquel Raynal * @buf: Buffer to put the string
117d677bfe2SMiquel Raynal * @size: Maximum size of buffer
118d677bfe2SMiquel Raynal * @return length of string, or -ENOSPC it no space
119d677bfe2SMiquel Raynal */
120d677bfe2SMiquel Raynal int (*get_desc)(struct udevice *dev, char *buf, int size);
121d677bfe2SMiquel Raynal
122d677bfe2SMiquel Raynal /**
123*d7869cecSEddie James * report_state() - Collect information about the current TPM state
124*d7869cecSEddie James *
125*d7869cecSEddie James * @dev: Device to check
126*d7869cecSEddie James * @buf: Buffer to put the string
127*d7869cecSEddie James * @size: Maximum size of buffer
128*d7869cecSEddie James * Return: return code of the operation (0 = success)
129*d7869cecSEddie James */
130*d7869cecSEddie James int (*report_state)(struct udevice *dev, char *buf, int size);
131*d7869cecSEddie James
132*d7869cecSEddie James /**
133d677bfe2SMiquel Raynal * send() - send data to the TPM
134d677bfe2SMiquel Raynal *
135d677bfe2SMiquel Raynal * @dev: Device to talk to
136d677bfe2SMiquel Raynal * @sendbuf: Buffer of the data to send
137d677bfe2SMiquel Raynal * @send_size: Size of the data to send
138d677bfe2SMiquel Raynal *
139d677bfe2SMiquel Raynal * Returns 0 on success or -ve on failure.
140d677bfe2SMiquel Raynal */
141d677bfe2SMiquel Raynal int (*send)(struct udevice *dev, const u8 *sendbuf, size_t send_size);
142d677bfe2SMiquel Raynal
143d677bfe2SMiquel Raynal /**
144d677bfe2SMiquel Raynal * recv() - receive a response from the TPM
145d677bfe2SMiquel Raynal *
146d677bfe2SMiquel Raynal * @dev: Device to talk to
147d677bfe2SMiquel Raynal * @recvbuf: Buffer to save the response to
148d677bfe2SMiquel Raynal * @max_size: Maximum number of bytes to receive
149d677bfe2SMiquel Raynal *
150d677bfe2SMiquel Raynal * Returns number of bytes received on success, -EAGAIN if the TPM
151d677bfe2SMiquel Raynal * response is not ready, -EINTR if cancelled, or other -ve value on
152d677bfe2SMiquel Raynal * failure.
153d677bfe2SMiquel Raynal */
154d677bfe2SMiquel Raynal int (*recv)(struct udevice *dev, u8 *recvbuf, size_t max_size);
155d677bfe2SMiquel Raynal
156d677bfe2SMiquel Raynal /**
157d677bfe2SMiquel Raynal * cleanup() - clean up after an operation in progress
158d677bfe2SMiquel Raynal *
159d677bfe2SMiquel Raynal * This is called if receiving times out. The TPM may need to abort
160d677bfe2SMiquel Raynal * the current transaction if it did not complete, and make itself
161d677bfe2SMiquel Raynal * ready for another.
162d677bfe2SMiquel Raynal *
163d677bfe2SMiquel Raynal * @dev: Device to talk to
164d677bfe2SMiquel Raynal */
165d677bfe2SMiquel Raynal int (*cleanup)(struct udevice *dev);
166d677bfe2SMiquel Raynal
167d677bfe2SMiquel Raynal /**
168d677bfe2SMiquel Raynal * xfer() - send data to the TPM and get response
169d677bfe2SMiquel Raynal *
170d677bfe2SMiquel Raynal * This method is optional. If it exists it is used in preference
171d677bfe2SMiquel Raynal * to send(), recv() and cleanup(). It should handle all aspects of
172d677bfe2SMiquel Raynal * TPM communication for a single transfer.
173d677bfe2SMiquel Raynal *
174d677bfe2SMiquel Raynal * @dev: Device to talk to
175d677bfe2SMiquel Raynal * @sendbuf: Buffer of the data to send
176d677bfe2SMiquel Raynal * @send_size: Size of the data to send
177d677bfe2SMiquel Raynal * @recvbuf: Buffer to save the response to
178d677bfe2SMiquel Raynal * @recv_size: Pointer to the size of the response buffer
179d677bfe2SMiquel Raynal *
180d677bfe2SMiquel Raynal * Returns 0 on success (and places the number of response bytes at
181d677bfe2SMiquel Raynal * recv_size) or -ve on failure.
182d677bfe2SMiquel Raynal */
183d677bfe2SMiquel Raynal int (*xfer)(struct udevice *dev, const u8 *sendbuf, size_t send_size,
184d677bfe2SMiquel Raynal u8 *recvbuf, size_t *recv_size);
185d677bfe2SMiquel Raynal };
186d677bfe2SMiquel Raynal
187d677bfe2SMiquel Raynal #define tpm_get_ops(dev) ((struct tpm_ops *)device_get_ops(dev))
188d677bfe2SMiquel Raynal
189d677bfe2SMiquel Raynal #define MAKE_TPM_CMD_ENTRY(cmd) \
190d677bfe2SMiquel Raynal U_BOOT_CMD_MKENT(cmd, 0, 1, do_tpm_ ## cmd, "", "")
191d677bfe2SMiquel Raynal
192d677bfe2SMiquel Raynal #define TPM_COMMAND_NO_ARG(cmd) \
193d677bfe2SMiquel Raynal int do_##cmd(cmd_tbl_t *cmdtp, int flag, \
194d677bfe2SMiquel Raynal int argc, char *const argv[]) \
195d677bfe2SMiquel Raynal { \
196abdc7b8aSSimon Glass struct udevice *dev; \
197abdc7b8aSSimon Glass int rc; \
198abdc7b8aSSimon Glass \
199abdc7b8aSSimon Glass rc = get_tpm(&dev); \
200abdc7b8aSSimon Glass if (rc) \
201abdc7b8aSSimon Glass return rc; \
202d677bfe2SMiquel Raynal if (argc != 1) \
203d677bfe2SMiquel Raynal return CMD_RET_USAGE; \
204abdc7b8aSSimon Glass return report_return_code(cmd(dev)); \
205d677bfe2SMiquel Raynal }
206d677bfe2SMiquel Raynal
207d677bfe2SMiquel Raynal /**
20851f00c17SSimon Glass * tpm_open() - Request access to locality 0 for the caller
20951f00c17SSimon Glass *
21051f00c17SSimon Glass * After all commands have been completed the caller is supposed to
21151f00c17SSimon Glass * call tpm_close().
21251f00c17SSimon Glass *
213abdc7b8aSSimon Glass * @dev - TPM device
21451f00c17SSimon Glass * Returns 0 on success, -ve on failure.
21551f00c17SSimon Glass */
21651f00c17SSimon Glass int tpm_open(struct udevice *dev);
21751f00c17SSimon Glass
21851f00c17SSimon Glass /**
21951f00c17SSimon Glass * tpm_close() - Close the current session
22051f00c17SSimon Glass *
22151f00c17SSimon Glass * Releasing the locked locality. Returns 0 on success, -ve 1 on
22251f00c17SSimon Glass * failure (in case lock removal did not succeed).
223abdc7b8aSSimon Glass *
224abdc7b8aSSimon Glass * @dev - TPM device
225abdc7b8aSSimon Glass * Returns 0 on success, -ve on failure.
22651f00c17SSimon Glass */
22751f00c17SSimon Glass int tpm_close(struct udevice *dev);
22851f00c17SSimon Glass
22951f00c17SSimon Glass /**
2305e69b8bcSSimon Glass * tpm_clear_and_reenable() - Force clear the TPM and reenable it
2315e69b8bcSSimon Glass *
2325e69b8bcSSimon Glass * @dev: TPM device
233*d7869cecSEddie James * Return: 0 on success, -ve on failure
2345e69b8bcSSimon Glass */
2355e69b8bcSSimon Glass u32 tpm_clear_and_reenable(struct udevice *dev);
2365e69b8bcSSimon Glass
2375e69b8bcSSimon Glass /**
238d677bfe2SMiquel Raynal * tpm_get_desc() - Get a text description of the TPM
239d677bfe2SMiquel Raynal *
240d677bfe2SMiquel Raynal * @dev: Device to check
241d677bfe2SMiquel Raynal * @buf: Buffer to put the string
242d677bfe2SMiquel Raynal * @size: Maximum size of buffer
243*d7869cecSEddie James * Return: length of string, or -ENOSPC it no space
244d677bfe2SMiquel Raynal */
245d677bfe2SMiquel Raynal int tpm_get_desc(struct udevice *dev, char *buf, int size);
246d677bfe2SMiquel Raynal
247d677bfe2SMiquel Raynal /**
248*d7869cecSEddie James * tpm_report_state() - Collect information about the current TPM state
249*d7869cecSEddie James *
250*d7869cecSEddie James * @dev: Device to check
251*d7869cecSEddie James * @buf: Buffer to put the string
252*d7869cecSEddie James * @size: Maximum size of buffer
253*d7869cecSEddie James * Return: return code of the operation (0 = success)
254*d7869cecSEddie James */
255*d7869cecSEddie James int tpm_report_state(struct udevice *dev, char *buf, int size);
256*d7869cecSEddie James
257*d7869cecSEddie James /**
258d677bfe2SMiquel Raynal * tpm_xfer() - send data to the TPM and get response
259d677bfe2SMiquel Raynal *
260d677bfe2SMiquel Raynal * This first uses the device's send() method to send the bytes. Then it calls
261d677bfe2SMiquel Raynal * recv() to get the reply. If recv() returns -EAGAIN then it will delay a
262d677bfe2SMiquel Raynal * short time and then call recv() again.
263d677bfe2SMiquel Raynal *
264d677bfe2SMiquel Raynal * Regardless of whether recv() completes successfully, it will then call
265d677bfe2SMiquel Raynal * cleanup() to finish the transaction.
266d677bfe2SMiquel Raynal *
267d677bfe2SMiquel Raynal * Note that the outgoing data is inspected to determine command type
268d677bfe2SMiquel Raynal * (ordinal) and a timeout is used for that command type.
269d677bfe2SMiquel Raynal *
270abdc7b8aSSimon Glass * @dev - TPM device
271d677bfe2SMiquel Raynal * @sendbuf - buffer of the data to send
272d677bfe2SMiquel Raynal * @send_size size of the data to send
273d677bfe2SMiquel Raynal * @recvbuf - memory to save the response to
274d677bfe2SMiquel Raynal * @recv_len - pointer to the size of the response buffer
275d677bfe2SMiquel Raynal *
276d677bfe2SMiquel Raynal * Returns 0 on success (and places the number of response bytes at
277d677bfe2SMiquel Raynal * recv_len) or -ve on failure.
278d677bfe2SMiquel Raynal */
279d677bfe2SMiquel Raynal int tpm_xfer(struct udevice *dev, const u8 *sendbuf, size_t send_size,
280d677bfe2SMiquel Raynal u8 *recvbuf, size_t *recv_size);
281d677bfe2SMiquel Raynal
282d677bfe2SMiquel Raynal /**
283d677bfe2SMiquel Raynal * Initialize TPM device. It must be called before any TPM commands.
284d677bfe2SMiquel Raynal *
285abdc7b8aSSimon Glass * @dev - TPM device
286*d7869cecSEddie James * Return: 0 on success, non-0 on error.
287d677bfe2SMiquel Raynal */
288abdc7b8aSSimon Glass int tpm_init(struct udevice *dev);
289d677bfe2SMiquel Raynal
290d677bfe2SMiquel Raynal /**
2912a2096eaSMiquel Raynal * Retrieve the array containing all the v1 (resp. v2) commands.
292d677bfe2SMiquel Raynal *
293*d7869cecSEddie James * Return: a cmd_tbl_t array.
294d677bfe2SMiquel Raynal */
2952a2096eaSMiquel Raynal #if defined(CONFIG_TPM_V1)
2962a2096eaSMiquel Raynal cmd_tbl_t *get_tpm1_commands(unsigned int *size);
2972a2096eaSMiquel Raynal #else
get_tpm1_commands(unsigned int * size)2982a2096eaSMiquel Raynal static inline cmd_tbl_t *get_tpm1_commands(unsigned int *size)
2992a2096eaSMiquel Raynal {
3002a2096eaSMiquel Raynal return NULL;
3012a2096eaSMiquel Raynal }
3022a2096eaSMiquel Raynal #endif
3032a2096eaSMiquel Raynal #if defined(CONFIG_TPM_V2)
3042a2096eaSMiquel Raynal cmd_tbl_t *get_tpm2_commands(unsigned int *size);
3052a2096eaSMiquel Raynal #else
get_tpm2_commands(unsigned int * size)3062a2096eaSMiquel Raynal static inline cmd_tbl_t *get_tpm2_commands(unsigned int *size)
3072a2096eaSMiquel Raynal {
3082a2096eaSMiquel Raynal return NULL;
3092a2096eaSMiquel Raynal }
3102a2096eaSMiquel Raynal #endif
311d677bfe2SMiquel Raynal
3120a60a0a6SSimon Glass /**
3130a60a0a6SSimon Glass * tpm_get_version() - Find the version of a TPM
3140a60a0a6SSimon Glass *
3150a60a0a6SSimon Glass * This checks the uclass data for a TPM device and returns the version number
3160a60a0a6SSimon Glass * it supports.
3170a60a0a6SSimon Glass *
3180a60a0a6SSimon Glass * @dev: TPM device
319*d7869cecSEddie James * Return: version number (TPM_V1 or TPMV2)
3200a60a0a6SSimon Glass */
3210a60a0a6SSimon Glass enum tpm_version tpm_get_version(struct udevice *dev);
3220a60a0a6SSimon Glass
323*d7869cecSEddie James /* Iterate on all TPM devices */
324*d7869cecSEddie James #define for_each_tpm_device(dev) uclass_foreach_dev_probe(UCLASS_TPM, (dev))
325*d7869cecSEddie James
326d677bfe2SMiquel Raynal #endif /* __TPM_COMMON_H */
327