blob: 1ba81386ce1040c0b5c2eef6ffa7e86e786fc260 [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
Simon Glass401d1c42020-10-30 21:38:53 -060012struct udevice;
13
Miquel Raynald677bfe2018-05-15 11:57:06 +020014enum 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
Simon Glass07e127d2018-11-18 14:22:25 -070033#define TPM_PCR_MINIMUM_DIGEST_SIZE 20
34
Miquel Raynald677bfe2018-05-15 11:57:06 +020035/**
Miquel Raynal2a2096e2018-07-19 22:35:09 +020036 * 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 */
40enum tpm_version {
41 TPM_V1 = 0,
42 TPM_V2,
43};
44
45/**
Miquel Raynald677bfe2018-05-15 11:57:06 +020046 * struct tpm_chip_priv - Information about a TPM, stored by the uclass
47 *
48 * These values must be set up by the device's probe() method before
49 * communcation is attempted. If the device has an xfer() method, this is
50 * not needed. There is no need to set up @buf.
51 *
Miquel Raynal2a2096e2018-07-19 22:35:09 +020052 * @version: TPM stack to be used
Miquel Raynald677bfe2018-05-15 11:57:06 +020053 * @duration_ms: Length of each duration type in milliseconds
54 * @retry_time_ms: Time to wait before retrying receive
Miquel Raynal2a2096e2018-07-19 22:35:09 +020055 * @buf: Buffer used during the exchanges with the chip
Miquel Raynalff322452018-05-15 11:57:08 +020056 * @pcr_count: Number of PCR per bank
57 * @pcr_select_min: Minimum size in bytes of the pcrSelect array
Simon Glass6719cbe2021-02-06 14:23:40 -070058 * @plat_hier_disabled: Platform hierarchy has been disabled (TPM is locked
59 * down until next reboot)
Miquel Raynald677bfe2018-05-15 11:57:06 +020060 */
61struct tpm_chip_priv {
Miquel Raynal2a2096e2018-07-19 22:35:09 +020062 enum tpm_version version;
63
Miquel Raynald677bfe2018-05-15 11:57:06 +020064 uint duration_ms[TPM_DURATION_COUNT];
65 uint retry_time_ms;
Miquel Raynal2a2096e2018-07-19 22:35:09 +020066 u8 buf[TPM_DEV_BUFSIZE + sizeof(u8)]; /* Max buffer size + addr */
67
68 /* TPM v2 specific data */
Miquel Raynalff322452018-05-15 11:57:08 +020069 uint pcr_count;
70 uint pcr_select_min;
Simon Glass6719cbe2021-02-06 14:23:40 -070071 bool plat_hier_disabled;
Miquel Raynald677bfe2018-05-15 11:57:06 +020072};
73
74/**
75 * struct tpm_ops - low-level TPM operations
76 *
77 * These are designed to avoid loops and delays in the driver itself. These
78 * should be handled in the uclass.
79 *
80 * In gneral you should implement everything except xfer(). Where you need
81 * complete control of the transfer, then xfer() can be provided and will
82 * override the other methods.
83 *
84 * This interface is for low-level TPM access. It does not understand the
85 * concept of localities or the various TPM messages. That interface is
86 * defined in the functions later on in this file, but they all translate
87 * to bytes which are sent and received.
88 */
89struct tpm_ops {
90 /**
91 * open() - Request access to locality 0 for the caller
92 *
93 * After all commands have been completed the caller should call
94 * close().
95 *
Miquel Raynal350988f2018-07-19 22:35:06 +020096 * @dev: Device to open
Simon Glassa11be4c2023-02-21 06:24:52 -070097 * @return 0 ok OK, -EBUSY if already opened, other -ve on other error
Miquel Raynald677bfe2018-05-15 11:57:06 +020098 */
99 int (*open)(struct udevice *dev);
100
101 /**
102 * close() - Close the current session
103 *
104 * Releasing the locked locality. Returns 0 on success, -ve 1 on
105 * failure (in case lock removal did not succeed).
106 *
107 * @dev: Device to close
108 * @return 0 ok OK, -ve on error
109 */
110 int (*close)(struct udevice *dev);
111
112 /**
113 * get_desc() - Get a text description of the TPM
114 *
115 * @dev: Device to check
116 * @buf: Buffer to put the string
117 * @size: Maximum size of buffer
118 * @return length of string, or -ENOSPC it no space
119 */
120 int (*get_desc)(struct udevice *dev, char *buf, int size);
121
122 /**
Simon Glass3bb4db42022-08-30 21:05:36 -0600123 * report_state() - Collect information about the current TPM state
124 *
125 * @dev: Device to check
126 * @buf: Buffer to put the string
127 * @size: Maximum size of buffer
128 * Return: return code of the operation (0 = success)
129 */
130 int (*report_state)(struct udevice *dev, char *buf, int size);
131
132 /**
Miquel Raynald677bfe2018-05-15 11:57:06 +0200133 * send() - send data to the TPM
134 *
135 * @dev: Device to talk to
136 * @sendbuf: Buffer of the data to send
137 * @send_size: Size of the data to send
138 *
139 * Returns 0 on success or -ve on failure.
140 */
141 int (*send)(struct udevice *dev, const u8 *sendbuf, size_t send_size);
142
143 /**
144 * recv() - receive a response from the TPM
145 *
146 * @dev: Device to talk to
147 * @recvbuf: Buffer to save the response to
148 * @max_size: Maximum number of bytes to receive
149 *
150 * Returns number of bytes received on success, -EAGAIN if the TPM
151 * response is not ready, -EINTR if cancelled, or other -ve value on
152 * failure.
153 */
154 int (*recv)(struct udevice *dev, u8 *recvbuf, size_t max_size);
155
156 /**
157 * cleanup() - clean up after an operation in progress
158 *
159 * This is called if receiving times out. The TPM may need to abort
160 * the current transaction if it did not complete, and make itself
161 * ready for another.
162 *
163 * @dev: Device to talk to
164 */
165 int (*cleanup)(struct udevice *dev);
166
167 /**
168 * xfer() - send data to the TPM and get response
169 *
170 * This method is optional. If it exists it is used in preference
171 * to send(), recv() and cleanup(). It should handle all aspects of
172 * TPM communication for a single transfer.
173 *
174 * @dev: Device to talk to
175 * @sendbuf: Buffer of the data to send
176 * @send_size: Size of the data to send
177 * @recvbuf: Buffer to save the response to
178 * @recv_size: Pointer to the size of the response buffer
179 *
180 * Returns 0 on success (and places the number of response bytes at
181 * recv_size) or -ve on failure.
182 */
183 int (*xfer)(struct udevice *dev, const u8 *sendbuf, size_t send_size,
184 u8 *recvbuf, size_t *recv_size);
185};
186
187#define tpm_get_ops(dev) ((struct tpm_ops *)device_get_ops(dev))
188
189#define MAKE_TPM_CMD_ENTRY(cmd) \
190 U_BOOT_CMD_MKENT(cmd, 0, 1, do_tpm_ ## cmd, "", "")
191
192#define TPM_COMMAND_NO_ARG(cmd) \
Simon Glass09140112020-05-10 11:40:03 -0600193int do_##cmd(struct cmd_tbl *cmdtp, int flag, \
194 int argc, char *const argv[]) \
Miquel Raynald677bfe2018-05-15 11:57:06 +0200195{ \
Simon Glassabdc7b82018-11-18 14:22:27 -0700196 struct udevice *dev; \
197 int rc; \
198 \
199 rc = get_tpm(&dev); \
200 if (rc) \
201 return rc; \
Miquel Raynald677bfe2018-05-15 11:57:06 +0200202 if (argc != 1) \
203 return CMD_RET_USAGE; \
Simon Glassabdc7b82018-11-18 14:22:27 -0700204 return report_return_code(cmd(dev)); \
Miquel Raynald677bfe2018-05-15 11:57:06 +0200205}
206
207/**
Simon Glass51f00c12018-11-18 14:22:26 -0700208 * tpm_open() - Request access to locality 0 for the caller
209 *
210 * After all commands have been completed the caller is supposed to
211 * call tpm_close().
212 *
Simon Glassabdc7b82018-11-18 14:22:27 -0700213 * @dev - TPM device
Simon Glass51f00c12018-11-18 14:22:26 -0700214 * Returns 0 on success, -ve on failure.
215 */
216int tpm_open(struct udevice *dev);
217
218/**
219 * tpm_close() - Close the current session
220 *
221 * Releasing the locked locality. Returns 0 on success, -ve 1 on
222 * failure (in case lock removal did not succeed).
Simon Glassabdc7b82018-11-18 14:22:27 -0700223 *
224 * @dev - TPM device
225 * Returns 0 on success, -ve on failure.
Simon Glass51f00c12018-11-18 14:22:26 -0700226 */
227int tpm_close(struct udevice *dev);
228
229/**
Simon Glass5e69b8b2018-11-23 21:29:33 -0700230 * tpm_clear_and_reenable() - Force clear the TPM and reenable it
231 *
232 * @dev: TPM device
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100233 * Return: 0 on success, -ve on failure
Simon Glass5e69b8b2018-11-23 21:29:33 -0700234 */
235u32 tpm_clear_and_reenable(struct udevice *dev);
236
237/**
Miquel Raynald677bfe2018-05-15 11:57:06 +0200238 * tpm_get_desc() - Get a text description of the TPM
239 *
240 * @dev: Device to check
241 * @buf: Buffer to put the string
242 * @size: Maximum size of buffer
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100243 * Return: length of string, or -ENOSPC it no space
Miquel Raynald677bfe2018-05-15 11:57:06 +0200244 */
245int tpm_get_desc(struct udevice *dev, char *buf, int size);
246
247/**
Simon Glass3bb4db42022-08-30 21:05:36 -0600248 * tpm_report_state() - Collect information about the current TPM state
249 *
250 * @dev: Device to check
251 * @buf: Buffer to put the string
252 * @size: Maximum size of buffer
253 * Return: return code of the operation (0 = success)
254 */
255int tpm_report_state(struct udevice *dev, char *buf, int size);
256
257/**
Miquel Raynald677bfe2018-05-15 11:57:06 +0200258 * tpm_xfer() - send data to the TPM and get response
259 *
260 * This first uses the device's send() method to send the bytes. Then it calls
261 * recv() to get the reply. If recv() returns -EAGAIN then it will delay a
262 * short time and then call recv() again.
263 *
264 * Regardless of whether recv() completes successfully, it will then call
265 * cleanup() to finish the transaction.
266 *
267 * Note that the outgoing data is inspected to determine command type
268 * (ordinal) and a timeout is used for that command type.
269 *
Simon Glassabdc7b82018-11-18 14:22:27 -0700270 * @dev - TPM device
Miquel Raynald677bfe2018-05-15 11:57:06 +0200271 * @sendbuf - buffer of the data to send
272 * @send_size size of the data to send
273 * @recvbuf - memory to save the response to
274 * @recv_len - pointer to the size of the response buffer
275 *
276 * Returns 0 on success (and places the number of response bytes at
277 * recv_len) or -ve on failure.
278 */
279int tpm_xfer(struct udevice *dev, const u8 *sendbuf, size_t send_size,
280 u8 *recvbuf, size_t *recv_size);
281
282/**
283 * Initialize TPM device. It must be called before any TPM commands.
284 *
Simon Glassabdc7b82018-11-18 14:22:27 -0700285 * @dev - TPM device
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100286 * Return: 0 on success, non-0 on error.
Miquel Raynald677bfe2018-05-15 11:57:06 +0200287 */
Simon Glassabdc7b82018-11-18 14:22:27 -0700288int tpm_init(struct udevice *dev);
Miquel Raynald677bfe2018-05-15 11:57:06 +0200289
290/**
Miquel Raynal2a2096e2018-07-19 22:35:09 +0200291 * Retrieve the array containing all the v1 (resp. v2) commands.
Miquel Raynald677bfe2018-05-15 11:57:06 +0200292 *
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100293 * Return: a struct cmd_tbl array.
Miquel Raynald677bfe2018-05-15 11:57:06 +0200294 */
Miquel Raynal2a2096e2018-07-19 22:35:09 +0200295#if defined(CONFIG_TPM_V1)
Simon Glass09140112020-05-10 11:40:03 -0600296struct cmd_tbl *get_tpm1_commands(unsigned int *size);
Miquel Raynal2a2096e2018-07-19 22:35:09 +0200297#else
Simon Glass09140112020-05-10 11:40:03 -0600298static inline struct cmd_tbl *get_tpm1_commands(unsigned int *size)
Miquel Raynal2a2096e2018-07-19 22:35:09 +0200299{
300 return NULL;
301}
302#endif
303#if defined(CONFIG_TPM_V2)
Simon Glass09140112020-05-10 11:40:03 -0600304struct cmd_tbl *get_tpm2_commands(unsigned int *size);
Miquel Raynal2a2096e2018-07-19 22:35:09 +0200305#else
Simon Glass09140112020-05-10 11:40:03 -0600306static inline struct cmd_tbl *get_tpm2_commands(unsigned int *size)
Miquel Raynal2a2096e2018-07-19 22:35:09 +0200307{
308 return NULL;
309}
310#endif
Miquel Raynald677bfe2018-05-15 11:57:06 +0200311
Simon Glass0a60a0a2018-11-23 21:29:32 -0700312/**
313 * tpm_get_version() - Find the version of a TPM
314 *
315 * This checks the uclass data for a TPM device and returns the version number
316 * it supports.
317 *
318 * @dev: TPM device
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100319 * Return: version number (TPM_V1 or TPMV2)
Simon Glass0a60a0a2018-11-23 21:29:32 -0700320 */
321enum tpm_version tpm_get_version(struct udevice *dev);
322
Philippe Reynesbb3f47e2020-01-09 18:45:45 +0100323/* Iterate on all TPM devices */
324#define for_each_tpm_device(dev) uclass_foreach_dev_probe(UCLASS_TPM, (dev))
325
Miquel Raynald677bfe2018-05-15 11:57:06 +0200326#endif /* __TPM_COMMON_H */