Miquel Raynal | 4c6759e | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 1 | /* 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 Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 10 | #include <command.h> |
| 11 | |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 12 | struct udevice; |
| 13 | |
Miquel Raynal | 4c6759e | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 14 | enum 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 Glass | 04a3d6e | 2018-11-18 14:22:25 -0700 | [diff] [blame] | 33 | #define TPM_PCR_MINIMUM_DIGEST_SIZE 20 |
| 34 | |
Miquel Raynal | 4c6759e | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 35 | /** |
Miquel Raynal | 79920d1 | 2018-07-19 22:35:09 +0200 | [diff] [blame] | 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 |
| 39 | */ |
| 40 | enum tpm_version { |
| 41 | TPM_V1 = 0, |
| 42 | TPM_V2, |
| 43 | }; |
| 44 | |
| 45 | /** |
Miquel Raynal | 4c6759e | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 46 | * 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 Raynal | 79920d1 | 2018-07-19 22:35:09 +0200 | [diff] [blame] | 52 | * @version: TPM stack to be used |
Miquel Raynal | 4c6759e | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 53 | * @duration_ms: Length of each duration type in milliseconds |
| 54 | * @retry_time_ms: Time to wait before retrying receive |
Miquel Raynal | 79920d1 | 2018-07-19 22:35:09 +0200 | [diff] [blame] | 55 | * @buf: Buffer used during the exchanges with the chip |
Miquel Raynal | f3b4350 | 2018-05-15 11:57:08 +0200 | [diff] [blame] | 56 | * @pcr_count: Number of PCR per bank |
| 57 | * @pcr_select_min: Minimum size in bytes of the pcrSelect array |
Simon Glass | 3d930ed | 2021-02-06 14:23:40 -0700 | [diff] [blame] | 58 | * @plat_hier_disabled: Platform hierarchy has been disabled (TPM is locked |
| 59 | * down until next reboot) |
Miquel Raynal | 4c6759e | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 60 | */ |
| 61 | struct tpm_chip_priv { |
Miquel Raynal | 79920d1 | 2018-07-19 22:35:09 +0200 | [diff] [blame] | 62 | enum tpm_version version; |
| 63 | |
Miquel Raynal | 4c6759e | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 64 | uint duration_ms[TPM_DURATION_COUNT]; |
| 65 | uint retry_time_ms; |
Miquel Raynal | 79920d1 | 2018-07-19 22:35:09 +0200 | [diff] [blame] | 66 | u8 buf[TPM_DEV_BUFSIZE + sizeof(u8)]; /* Max buffer size + addr */ |
| 67 | |
| 68 | /* TPM v2 specific data */ |
Miquel Raynal | f3b4350 | 2018-05-15 11:57:08 +0200 | [diff] [blame] | 69 | uint pcr_count; |
| 70 | uint pcr_select_min; |
Simon Glass | 3d930ed | 2021-02-06 14:23:40 -0700 | [diff] [blame] | 71 | bool plat_hier_disabled; |
Miquel Raynal | 4c6759e | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 72 | }; |
| 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 | */ |
| 89 | struct 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 Raynal | c39d1b0 | 2018-07-19 22:35:06 +0200 | [diff] [blame] | 96 | * @dev: Device to open |
Miquel Raynal | 4c6759e | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 97 | * @return 0 ok OK, -ve on error |
| 98 | */ |
| 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 | /** |
| 123 | * send() - send data to the TPM |
| 124 | * |
| 125 | * @dev: Device to talk to |
| 126 | * @sendbuf: Buffer of the data to send |
| 127 | * @send_size: Size of the data to send |
| 128 | * |
| 129 | * Returns 0 on success or -ve on failure. |
| 130 | */ |
| 131 | int (*send)(struct udevice *dev, const u8 *sendbuf, size_t send_size); |
| 132 | |
| 133 | /** |
| 134 | * recv() - receive a response from the TPM |
| 135 | * |
| 136 | * @dev: Device to talk to |
| 137 | * @recvbuf: Buffer to save the response to |
| 138 | * @max_size: Maximum number of bytes to receive |
| 139 | * |
| 140 | * Returns number of bytes received on success, -EAGAIN if the TPM |
| 141 | * response is not ready, -EINTR if cancelled, or other -ve value on |
| 142 | * failure. |
| 143 | */ |
| 144 | int (*recv)(struct udevice *dev, u8 *recvbuf, size_t max_size); |
| 145 | |
| 146 | /** |
| 147 | * cleanup() - clean up after an operation in progress |
| 148 | * |
| 149 | * This is called if receiving times out. The TPM may need to abort |
| 150 | * the current transaction if it did not complete, and make itself |
| 151 | * ready for another. |
| 152 | * |
| 153 | * @dev: Device to talk to |
| 154 | */ |
| 155 | int (*cleanup)(struct udevice *dev); |
| 156 | |
| 157 | /** |
| 158 | * xfer() - send data to the TPM and get response |
| 159 | * |
| 160 | * This method is optional. If it exists it is used in preference |
| 161 | * to send(), recv() and cleanup(). It should handle all aspects of |
| 162 | * TPM communication for a single transfer. |
| 163 | * |
| 164 | * @dev: Device to talk to |
| 165 | * @sendbuf: Buffer of the data to send |
| 166 | * @send_size: Size of the data to send |
| 167 | * @recvbuf: Buffer to save the response to |
| 168 | * @recv_size: Pointer to the size of the response buffer |
| 169 | * |
| 170 | * Returns 0 on success (and places the number of response bytes at |
| 171 | * recv_size) or -ve on failure. |
| 172 | */ |
| 173 | int (*xfer)(struct udevice *dev, const u8 *sendbuf, size_t send_size, |
| 174 | u8 *recvbuf, size_t *recv_size); |
| 175 | }; |
| 176 | |
| 177 | #define tpm_get_ops(dev) ((struct tpm_ops *)device_get_ops(dev)) |
| 178 | |
| 179 | #define MAKE_TPM_CMD_ENTRY(cmd) \ |
| 180 | U_BOOT_CMD_MKENT(cmd, 0, 1, do_tpm_ ## cmd, "", "") |
| 181 | |
| 182 | #define TPM_COMMAND_NO_ARG(cmd) \ |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 183 | int do_##cmd(struct cmd_tbl *cmdtp, int flag, \ |
| 184 | int argc, char *const argv[]) \ |
Miquel Raynal | 4c6759e | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 185 | { \ |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 186 | struct udevice *dev; \ |
| 187 | int rc; \ |
| 188 | \ |
| 189 | rc = get_tpm(&dev); \ |
| 190 | if (rc) \ |
| 191 | return rc; \ |
Miquel Raynal | 4c6759e | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 192 | if (argc != 1) \ |
| 193 | return CMD_RET_USAGE; \ |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 194 | return report_return_code(cmd(dev)); \ |
Miquel Raynal | 4c6759e | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | /** |
Simon Glass | 5e7941c | 2018-11-18 14:22:26 -0700 | [diff] [blame] | 198 | * tpm_open() - Request access to locality 0 for the caller |
| 199 | * |
| 200 | * After all commands have been completed the caller is supposed to |
| 201 | * call tpm_close(). |
| 202 | * |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 203 | * @dev - TPM device |
Simon Glass | 5e7941c | 2018-11-18 14:22:26 -0700 | [diff] [blame] | 204 | * Returns 0 on success, -ve on failure. |
| 205 | */ |
| 206 | int tpm_open(struct udevice *dev); |
| 207 | |
| 208 | /** |
| 209 | * tpm_close() - Close the current session |
| 210 | * |
| 211 | * Releasing the locked locality. Returns 0 on success, -ve 1 on |
| 212 | * failure (in case lock removal did not succeed). |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 213 | * |
| 214 | * @dev - TPM device |
| 215 | * Returns 0 on success, -ve on failure. |
Simon Glass | 5e7941c | 2018-11-18 14:22:26 -0700 | [diff] [blame] | 216 | */ |
| 217 | int tpm_close(struct udevice *dev); |
| 218 | |
| 219 | /** |
Simon Glass | 36e2a04 | 2018-11-23 21:29:33 -0700 | [diff] [blame] | 220 | * tpm_clear_and_reenable() - Force clear the TPM and reenable it |
| 221 | * |
| 222 | * @dev: TPM device |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 223 | * Return: 0 on success, -ve on failure |
Simon Glass | 36e2a04 | 2018-11-23 21:29:33 -0700 | [diff] [blame] | 224 | */ |
| 225 | u32 tpm_clear_and_reenable(struct udevice *dev); |
| 226 | |
| 227 | /** |
Miquel Raynal | 4c6759e | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 228 | * tpm_get_desc() - Get a text description of the TPM |
| 229 | * |
| 230 | * @dev: Device to check |
| 231 | * @buf: Buffer to put the string |
| 232 | * @size: Maximum size of buffer |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 233 | * Return: length of string, or -ENOSPC it no space |
Miquel Raynal | 4c6759e | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 234 | */ |
| 235 | int tpm_get_desc(struct udevice *dev, char *buf, int size); |
| 236 | |
| 237 | /** |
| 238 | * tpm_xfer() - send data to the TPM and get response |
| 239 | * |
| 240 | * This first uses the device's send() method to send the bytes. Then it calls |
| 241 | * recv() to get the reply. If recv() returns -EAGAIN then it will delay a |
| 242 | * short time and then call recv() again. |
| 243 | * |
| 244 | * Regardless of whether recv() completes successfully, it will then call |
| 245 | * cleanup() to finish the transaction. |
| 246 | * |
| 247 | * Note that the outgoing data is inspected to determine command type |
| 248 | * (ordinal) and a timeout is used for that command type. |
| 249 | * |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 250 | * @dev - TPM device |
Miquel Raynal | 4c6759e | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 251 | * @sendbuf - buffer of the data to send |
| 252 | * @send_size size of the data to send |
| 253 | * @recvbuf - memory to save the response to |
| 254 | * @recv_len - pointer to the size of the response buffer |
| 255 | * |
| 256 | * Returns 0 on success (and places the number of response bytes at |
| 257 | * recv_len) or -ve on failure. |
| 258 | */ |
| 259 | int tpm_xfer(struct udevice *dev, const u8 *sendbuf, size_t send_size, |
| 260 | u8 *recvbuf, size_t *recv_size); |
| 261 | |
| 262 | /** |
| 263 | * Initialize TPM device. It must be called before any TPM commands. |
| 264 | * |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 265 | * @dev - TPM device |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 266 | * Return: 0 on success, non-0 on error. |
Miquel Raynal | 4c6759e | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 267 | */ |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 268 | int tpm_init(struct udevice *dev); |
Miquel Raynal | 4c6759e | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 269 | |
| 270 | /** |
Miquel Raynal | 79920d1 | 2018-07-19 22:35:09 +0200 | [diff] [blame] | 271 | * Retrieve the array containing all the v1 (resp. v2) commands. |
Miquel Raynal | 4c6759e | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 272 | * |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 273 | * Return: a struct cmd_tbl array. |
Miquel Raynal | 4c6759e | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 274 | */ |
Miquel Raynal | 79920d1 | 2018-07-19 22:35:09 +0200 | [diff] [blame] | 275 | #if defined(CONFIG_TPM_V1) |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 276 | struct cmd_tbl *get_tpm1_commands(unsigned int *size); |
Miquel Raynal | 79920d1 | 2018-07-19 22:35:09 +0200 | [diff] [blame] | 277 | #else |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 278 | static inline struct cmd_tbl *get_tpm1_commands(unsigned int *size) |
Miquel Raynal | 79920d1 | 2018-07-19 22:35:09 +0200 | [diff] [blame] | 279 | { |
| 280 | return NULL; |
| 281 | } |
| 282 | #endif |
| 283 | #if defined(CONFIG_TPM_V2) |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 284 | struct cmd_tbl *get_tpm2_commands(unsigned int *size); |
Miquel Raynal | 79920d1 | 2018-07-19 22:35:09 +0200 | [diff] [blame] | 285 | #else |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 286 | static inline struct cmd_tbl *get_tpm2_commands(unsigned int *size) |
Miquel Raynal | 79920d1 | 2018-07-19 22:35:09 +0200 | [diff] [blame] | 287 | { |
| 288 | return NULL; |
| 289 | } |
| 290 | #endif |
Miquel Raynal | 4c6759e | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 291 | |
Simon Glass | e03dbde | 2018-11-23 21:29:32 -0700 | [diff] [blame] | 292 | /** |
| 293 | * tpm_get_version() - Find the version of a TPM |
| 294 | * |
| 295 | * This checks the uclass data for a TPM device and returns the version number |
| 296 | * it supports. |
| 297 | * |
| 298 | * @dev: TPM device |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 299 | * Return: version number (TPM_V1 or TPMV2) |
Simon Glass | e03dbde | 2018-11-23 21:29:32 -0700 | [diff] [blame] | 300 | */ |
| 301 | enum tpm_version tpm_get_version(struct udevice *dev); |
| 302 | |
Philippe Reynes | 7bafac2 | 2020-01-09 18:45:45 +0100 | [diff] [blame] | 303 | /* Iterate on all TPM devices */ |
| 304 | #define for_each_tpm_device(dev) uclass_foreach_dev_probe(UCLASS_TPM, (dev)) |
| 305 | |
Miquel Raynal | 4c6759e | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 306 | #endif /* __TPM_COMMON_H */ |