blob: e29b10b1766e1cc14c977f7f298def14f354981a [file] [log] [blame]
Miquel Raynald677bfe2018-05-15 11:57:06 +02001/* 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
Simon Glass09140112020-05-10 11:40:03 -060010#include <command.h>
11
Miquel Raynald677bfe2018-05-15 11:57:06 +020012enum 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
Simon Glass07e127d2018-11-18 14:22:25 -070031#define TPM_PCR_MINIMUM_DIGEST_SIZE 20
32
Miquel Raynald677bfe2018-05-15 11:57:06 +020033/**
Miquel Raynal2a2096e2018-07-19 22:35:09 +020034 * 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 */
38enum tpm_version {
39 TPM_V1 = 0,
40 TPM_V2,
41};
42
43/**
Miquel Raynald677bfe2018-05-15 11:57:06 +020044 * 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 *
Miquel Raynal2a2096e2018-07-19 22:35:09 +020050 * @version: TPM stack to be used
Miquel Raynald677bfe2018-05-15 11:57:06 +020051 * @duration_ms: Length of each duration type in milliseconds
52 * @retry_time_ms: Time to wait before retrying receive
Miquel Raynal2a2096e2018-07-19 22:35:09 +020053 * @buf: Buffer used during the exchanges with the chip
Miquel Raynalff322452018-05-15 11:57:08 +020054 * @pcr_count: Number of PCR per bank
55 * @pcr_select_min: Minimum size in bytes of the pcrSelect array
Miquel Raynald677bfe2018-05-15 11:57:06 +020056 */
57struct tpm_chip_priv {
Miquel Raynal2a2096e2018-07-19 22:35:09 +020058 enum tpm_version version;
59
Miquel Raynald677bfe2018-05-15 11:57:06 +020060 uint duration_ms[TPM_DURATION_COUNT];
61 uint retry_time_ms;
Miquel Raynal2a2096e2018-07-19 22:35:09 +020062 u8 buf[TPM_DEV_BUFSIZE + sizeof(u8)]; /* Max buffer size + addr */
63
64 /* TPM v2 specific data */
Miquel Raynalff322452018-05-15 11:57:08 +020065 uint pcr_count;
66 uint pcr_select_min;
Miquel Raynald677bfe2018-05-15 11:57:06 +020067};
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 */
84struct 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 *
Miquel Raynal350988f2018-07-19 22:35:06 +020091 * @dev: Device to open
Miquel Raynald677bfe2018-05-15 11:57:06 +020092 * @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) \
Simon Glass09140112020-05-10 11:40:03 -0600178int do_##cmd(struct cmd_tbl *cmdtp, int flag, \
179 int argc, char *const argv[]) \
Miquel Raynald677bfe2018-05-15 11:57:06 +0200180{ \
Simon Glassabdc7b82018-11-18 14:22:27 -0700181 struct udevice *dev; \
182 int rc; \
183 \
184 rc = get_tpm(&dev); \
185 if (rc) \
186 return rc; \
Miquel Raynald677bfe2018-05-15 11:57:06 +0200187 if (argc != 1) \
188 return CMD_RET_USAGE; \
Simon Glassabdc7b82018-11-18 14:22:27 -0700189 return report_return_code(cmd(dev)); \
Miquel Raynald677bfe2018-05-15 11:57:06 +0200190}
191
192/**
Simon Glass51f00c12018-11-18 14:22:26 -0700193 * 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 *
Simon Glassabdc7b82018-11-18 14:22:27 -0700198 * @dev - TPM device
Simon Glass51f00c12018-11-18 14:22:26 -0700199 * Returns 0 on success, -ve on failure.
200 */
201int 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).
Simon Glassabdc7b82018-11-18 14:22:27 -0700208 *
209 * @dev - TPM device
210 * Returns 0 on success, -ve on failure.
Simon Glass51f00c12018-11-18 14:22:26 -0700211 */
212int tpm_close(struct udevice *dev);
213
214/**
Simon Glass5e69b8b2018-11-23 21:29:33 -0700215 * 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 */
220u32 tpm_clear_and_reenable(struct udevice *dev);
221
222/**
Miquel Raynald677bfe2018-05-15 11:57:06 +0200223 * 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 */
230int 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 *
Simon Glassabdc7b82018-11-18 14:22:27 -0700245 * @dev - TPM device
Miquel Raynald677bfe2018-05-15 11:57:06 +0200246 * @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 */
254int 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 *
Simon Glassabdc7b82018-11-18 14:22:27 -0700260 * @dev - TPM device
Miquel Raynald677bfe2018-05-15 11:57:06 +0200261 * @return 0 on success, non-0 on error.
262 */
Simon Glassabdc7b82018-11-18 14:22:27 -0700263int tpm_init(struct udevice *dev);
Miquel Raynald677bfe2018-05-15 11:57:06 +0200264
265/**
Miquel Raynal2a2096e2018-07-19 22:35:09 +0200266 * Retrieve the array containing all the v1 (resp. v2) commands.
Miquel Raynald677bfe2018-05-15 11:57:06 +0200267 *
Simon Glass09140112020-05-10 11:40:03 -0600268 * @return a struct cmd_tbl array.
Miquel Raynald677bfe2018-05-15 11:57:06 +0200269 */
Miquel Raynal2a2096e2018-07-19 22:35:09 +0200270#if defined(CONFIG_TPM_V1)
Simon Glass09140112020-05-10 11:40:03 -0600271struct cmd_tbl *get_tpm1_commands(unsigned int *size);
Miquel Raynal2a2096e2018-07-19 22:35:09 +0200272#else
Simon Glass09140112020-05-10 11:40:03 -0600273static inline struct cmd_tbl *get_tpm1_commands(unsigned int *size)
Miquel Raynal2a2096e2018-07-19 22:35:09 +0200274{
275 return NULL;
276}
277#endif
278#if defined(CONFIG_TPM_V2)
Simon Glass09140112020-05-10 11:40:03 -0600279struct cmd_tbl *get_tpm2_commands(unsigned int *size);
Miquel Raynal2a2096e2018-07-19 22:35:09 +0200280#else
Simon Glass09140112020-05-10 11:40:03 -0600281static inline struct cmd_tbl *get_tpm2_commands(unsigned int *size)
Miquel Raynal2a2096e2018-07-19 22:35:09 +0200282{
283 return NULL;
284}
285#endif
Miquel Raynald677bfe2018-05-15 11:57:06 +0200286
Simon Glass0a60a0a2018-11-23 21:29:32 -0700287/**
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 */
296enum tpm_version tpm_get_version(struct udevice *dev);
297
Philippe Reynesbb3f47e2020-01-09 18:45:45 +0100298/* Iterate on all TPM devices */
299#define for_each_tpm_device(dev) uclass_foreach_dev_probe(UCLASS_TPM, (dev))
300
Miquel Raynald677bfe2018-05-15 11:57:06 +0200301#endif /* __TPM_COMMON_H */