blob: bfb84a931d16525fdf1263e83ae8f62dff318fb1 [file] [log] [blame]
Miquel Raynal4c6759e2018-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 Glassed38aef2020-05-10 11:40:03 -060010#include <command.h>
11
Simon Glass3ba929a2020-10-30 21:38:53 -060012struct udevice;
13
Miquel Raynal4c6759e2018-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 Glass04a3d6e2018-11-18 14:22:25 -070033#define TPM_PCR_MINIMUM_DIGEST_SIZE 20
34
Miquel Raynal4c6759e2018-05-15 11:57:06 +020035/**
Miquel Raynal79920d12018-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
Heinrich Schuchardt4fc65092024-12-27 14:25:41 +020045/**
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
Ilias Apalodimas2f7ad7e2024-12-24 08:01:09 -080049 */
50#define TPM2_NUM_PCR_BANKS 16
51
Miquel Raynal79920d12018-07-19 22:35:09 +020052/**
Miquel Raynal4c6759e2018-05-15 11:57:06 +020053 * struct tpm_chip_priv - Information about a TPM, stored by the uclass
54 *
Ilias Apalodimas2f7ad7e2024-12-24 08:01:09 -080055 * Some of hese values must be set up by the device's probe() method before
Miquel Raynal4c6759e2018-05-15 11:57:06 +020056 * communcation is attempted. If the device has an xfer() method, this is
57 * not needed. There is no need to set up @buf.
Ilias Apalodimas2f7ad7e2024-12-24 08:01:09 -080058 * The active_banks is only valid for TPMv2 after the device is initialized.
Miquel Raynal4c6759e2018-05-15 11:57:06 +020059 *
Miquel Raynal79920d12018-07-19 22:35:09 +020060 * @version: TPM stack to be used
Miquel Raynal4c6759e2018-05-15 11:57:06 +020061 * @duration_ms: Length of each duration type in milliseconds
62 * @retry_time_ms: Time to wait before retrying receive
Miquel Raynal79920d12018-07-19 22:35:09 +020063 * @buf: Buffer used during the exchanges with the chip
Miquel Raynalf3b43502018-05-15 11:57:08 +020064 * @pcr_count: Number of PCR per bank
65 * @pcr_select_min: Minimum size in bytes of the pcrSelect array
Ilias Apalodimas2f7ad7e2024-12-24 08:01:09 -080066 * @active_bank_count: Number of active PCR banks
67 * @active_banks: Array of active PCRs
Simon Glass3d930ed2021-02-06 14:23:40 -070068 * @plat_hier_disabled: Platform hierarchy has been disabled (TPM is locked
69 * down until next reboot)
Miquel Raynal4c6759e2018-05-15 11:57:06 +020070 */
71struct tpm_chip_priv {
Miquel Raynal79920d12018-07-19 22:35:09 +020072 enum tpm_version version;
73
Miquel Raynal4c6759e2018-05-15 11:57:06 +020074 uint duration_ms[TPM_DURATION_COUNT];
75 uint retry_time_ms;
Miquel Raynal79920d12018-07-19 22:35:09 +020076 u8 buf[TPM_DEV_BUFSIZE + sizeof(u8)]; /* Max buffer size + addr */
77
78 /* TPM v2 specific data */
Miquel Raynalf3b43502018-05-15 11:57:08 +020079 uint pcr_count;
80 uint pcr_select_min;
Ilias Apalodimas2f7ad7e2024-12-24 08:01:09 -080081#if IS_ENABLED(CONFIG_TPM_V2)
82 u8 active_bank_count;
83 u32 active_banks[TPM2_NUM_PCR_BANKS];
84#endif
Simon Glass3d930ed2021-02-06 14:23:40 -070085 bool plat_hier_disabled;
Miquel Raynal4c6759e2018-05-15 11:57:06 +020086};
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 */
103struct 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 *
Miquel Raynalc39d1b02018-07-19 22:35:06 +0200110 * @dev: Device to open
Simon Glass3467ed22023-02-21 06:24:52 -0700111 * @return 0 ok OK, -EBUSY if already opened, other -ve on other error
Miquel Raynal4c6759e2018-05-15 11:57:06 +0200112 */
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 /**
Simon Glass11a075c2022-08-30 21:05:36 -0600137 * 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 /**
Miquel Raynal4c6759e2018-05-15 11:57:06 +0200147 * 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) \
Simon Glassed38aef2020-05-10 11:40:03 -0600207int do_##cmd(struct cmd_tbl *cmdtp, int flag, \
208 int argc, char *const argv[]) \
Miquel Raynal4c6759e2018-05-15 11:57:06 +0200209{ \
Simon Glass8ceca1d2018-11-18 14:22:27 -0700210 struct udevice *dev; \
211 int rc; \
212 \
213 rc = get_tpm(&dev); \
214 if (rc) \
215 return rc; \
Miquel Raynal4c6759e2018-05-15 11:57:06 +0200216 if (argc != 1) \
217 return CMD_RET_USAGE; \
Simon Glass8ceca1d2018-11-18 14:22:27 -0700218 return report_return_code(cmd(dev)); \
Miquel Raynal4c6759e2018-05-15 11:57:06 +0200219}
220
221/**
Simon Glass5e7941c2018-11-18 14:22:26 -0700222 * 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 *
Simon Glass8ceca1d2018-11-18 14:22:27 -0700227 * @dev - TPM device
Simon Glass5e7941c2018-11-18 14:22:26 -0700228 * Returns 0 on success, -ve on failure.
229 */
230int 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).
Simon Glass8ceca1d2018-11-18 14:22:27 -0700237 *
238 * @dev - TPM device
239 * Returns 0 on success, -ve on failure.
Simon Glass5e7941c2018-11-18 14:22:26 -0700240 */
241int tpm_close(struct udevice *dev);
242
243/**
Simon Glass36e2a042018-11-23 21:29:33 -0700244 * tpm_clear_and_reenable() - Force clear the TPM and reenable it
245 *
246 * @dev: TPM device
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100247 * Return: 0 on success, -ve on failure
Simon Glass36e2a042018-11-23 21:29:33 -0700248 */
249u32 tpm_clear_and_reenable(struct udevice *dev);
250
251/**
Miquel Raynal4c6759e2018-05-15 11:57:06 +0200252 * 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
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100257 * Return: length of string, or -ENOSPC it no space
Miquel Raynal4c6759e2018-05-15 11:57:06 +0200258 */
259int tpm_get_desc(struct udevice *dev, char *buf, int size);
260
261/**
Simon Glass11a075c2022-08-30 21:05:36 -0600262 * 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 */
269int tpm_report_state(struct udevice *dev, char *buf, int size);
270
271/**
Miquel Raynal4c6759e2018-05-15 11:57:06 +0200272 * 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 *
Simon Glass8ceca1d2018-11-18 14:22:27 -0700284 * @dev - TPM device
Miquel Raynal4c6759e2018-05-15 11:57:06 +0200285 * @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 */
293int 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 *
Simon Glass8ceca1d2018-11-18 14:22:27 -0700299 * @dev - TPM device
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100300 * Return: 0 on success, non-0 on error.
Miquel Raynal4c6759e2018-05-15 11:57:06 +0200301 */
Simon Glass8ceca1d2018-11-18 14:22:27 -0700302int tpm_init(struct udevice *dev);
Miquel Raynal4c6759e2018-05-15 11:57:06 +0200303
304/**
Miquel Raynal79920d12018-07-19 22:35:09 +0200305 * Retrieve the array containing all the v1 (resp. v2) commands.
Miquel Raynal4c6759e2018-05-15 11:57:06 +0200306 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100307 * Return: a struct cmd_tbl array.
Miquel Raynal4c6759e2018-05-15 11:57:06 +0200308 */
Miquel Raynal79920d12018-07-19 22:35:09 +0200309#if defined(CONFIG_TPM_V1)
Simon Glassed38aef2020-05-10 11:40:03 -0600310struct cmd_tbl *get_tpm1_commands(unsigned int *size);
Miquel Raynal79920d12018-07-19 22:35:09 +0200311#else
Simon Glassed38aef2020-05-10 11:40:03 -0600312static inline struct cmd_tbl *get_tpm1_commands(unsigned int *size)
Miquel Raynal79920d12018-07-19 22:35:09 +0200313{
314 return NULL;
315}
316#endif
317#if defined(CONFIG_TPM_V2)
Simon Glassed38aef2020-05-10 11:40:03 -0600318struct cmd_tbl *get_tpm2_commands(unsigned int *size);
Miquel Raynal79920d12018-07-19 22:35:09 +0200319#else
Simon Glassed38aef2020-05-10 11:40:03 -0600320static inline struct cmd_tbl *get_tpm2_commands(unsigned int *size)
Miquel Raynal79920d12018-07-19 22:35:09 +0200321{
322 return NULL;
323}
324#endif
Miquel Raynal4c6759e2018-05-15 11:57:06 +0200325
Simon Glasse03dbde2018-11-23 21:29:32 -0700326/**
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
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100333 * Return: version number (TPM_V1 or TPMV2)
Simon Glasse03dbde2018-11-23 21:29:32 -0700334 */
335enum tpm_version tpm_get_version(struct udevice *dev);
336
Philippe Reynes7bafac22020-01-09 18:45:45 +0100337/* Iterate on all TPM devices */
338#define for_each_tpm_device(dev) uclass_foreach_dev_probe(UCLASS_TPM, (dev))
339
Miquel Raynal4c6759e2018-05-15 11:57:06 +0200340#endif /* __TPM_COMMON_H */