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 | |
| 10 | enum tpm_duration { |
| 11 | TPM_SHORT = 0, |
| 12 | TPM_MEDIUM = 1, |
| 13 | TPM_LONG = 2, |
| 14 | TPM_UNDEFINED, |
| 15 | |
| 16 | TPM_DURATION_COUNT, |
| 17 | }; |
| 18 | |
| 19 | /* |
| 20 | * Here is a partial implementation of TPM commands. Please consult TCG Main |
| 21 | * Specification for definitions of TPM commands. |
| 22 | */ |
| 23 | |
| 24 | #define TPM_HEADER_SIZE 10 |
| 25 | |
| 26 | /* Max buffer size supported by our tpm */ |
| 27 | #define TPM_DEV_BUFSIZE 1260 |
| 28 | |
Simon Glass | 04a3d6e | 2018-11-18 14:22:25 -0700 | [diff] [blame] | 29 | #define TPM_PCR_MINIMUM_DIGEST_SIZE 20 |
| 30 | |
Miquel Raynal | 4c6759e | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 31 | /** |
Miquel Raynal | 79920d1 | 2018-07-19 22:35:09 +0200 | [diff] [blame] | 32 | * enum tpm_version - The version of the TPM stack to be used |
| 33 | * @TPM_V1: Use TPM v1.x stack |
| 34 | * @TPM_V2: Use TPM v2.x stack |
| 35 | */ |
| 36 | enum tpm_version { |
| 37 | TPM_V1 = 0, |
| 38 | TPM_V2, |
| 39 | }; |
| 40 | |
| 41 | /** |
Miquel Raynal | 4c6759e | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 42 | * struct tpm_chip_priv - Information about a TPM, stored by the uclass |
| 43 | * |
| 44 | * These values must be set up by the device's probe() method before |
| 45 | * communcation is attempted. If the device has an xfer() method, this is |
| 46 | * not needed. There is no need to set up @buf. |
| 47 | * |
Miquel Raynal | 79920d1 | 2018-07-19 22:35:09 +0200 | [diff] [blame] | 48 | * @version: TPM stack to be used |
Miquel Raynal | 4c6759e | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 49 | * @duration_ms: Length of each duration type in milliseconds |
| 50 | * @retry_time_ms: Time to wait before retrying receive |
Miquel Raynal | 79920d1 | 2018-07-19 22:35:09 +0200 | [diff] [blame] | 51 | * @buf: Buffer used during the exchanges with the chip |
Miquel Raynal | f3b4350 | 2018-05-15 11:57:08 +0200 | [diff] [blame] | 52 | * @pcr_count: Number of PCR per bank |
| 53 | * @pcr_select_min: Minimum size in bytes of the pcrSelect array |
Miquel Raynal | 4c6759e | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 54 | */ |
| 55 | struct tpm_chip_priv { |
Miquel Raynal | 79920d1 | 2018-07-19 22:35:09 +0200 | [diff] [blame] | 56 | enum tpm_version version; |
| 57 | |
Miquel Raynal | 4c6759e | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 58 | uint duration_ms[TPM_DURATION_COUNT]; |
| 59 | uint retry_time_ms; |
Miquel Raynal | 79920d1 | 2018-07-19 22:35:09 +0200 | [diff] [blame] | 60 | u8 buf[TPM_DEV_BUFSIZE + sizeof(u8)]; /* Max buffer size + addr */ |
| 61 | |
| 62 | /* TPM v2 specific data */ |
Miquel Raynal | f3b4350 | 2018-05-15 11:57:08 +0200 | [diff] [blame] | 63 | uint pcr_count; |
| 64 | uint pcr_select_min; |
Miquel Raynal | 4c6759e | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 65 | }; |
| 66 | |
| 67 | /** |
| 68 | * struct tpm_ops - low-level TPM operations |
| 69 | * |
| 70 | * These are designed to avoid loops and delays in the driver itself. These |
| 71 | * should be handled in the uclass. |
| 72 | * |
| 73 | * In gneral you should implement everything except xfer(). Where you need |
| 74 | * complete control of the transfer, then xfer() can be provided and will |
| 75 | * override the other methods. |
| 76 | * |
| 77 | * This interface is for low-level TPM access. It does not understand the |
| 78 | * concept of localities or the various TPM messages. That interface is |
| 79 | * defined in the functions later on in this file, but they all translate |
| 80 | * to bytes which are sent and received. |
| 81 | */ |
| 82 | struct tpm_ops { |
| 83 | /** |
| 84 | * open() - Request access to locality 0 for the caller |
| 85 | * |
| 86 | * After all commands have been completed the caller should call |
| 87 | * close(). |
| 88 | * |
Miquel Raynal | c39d1b0 | 2018-07-19 22:35:06 +0200 | [diff] [blame] | 89 | * @dev: Device to open |
Miquel Raynal | 4c6759e | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 90 | * @return 0 ok OK, -ve on error |
| 91 | */ |
| 92 | int (*open)(struct udevice *dev); |
| 93 | |
| 94 | /** |
| 95 | * close() - Close the current session |
| 96 | * |
| 97 | * Releasing the locked locality. Returns 0 on success, -ve 1 on |
| 98 | * failure (in case lock removal did not succeed). |
| 99 | * |
| 100 | * @dev: Device to close |
| 101 | * @return 0 ok OK, -ve on error |
| 102 | */ |
| 103 | int (*close)(struct udevice *dev); |
| 104 | |
| 105 | /** |
| 106 | * get_desc() - Get a text description of the TPM |
| 107 | * |
| 108 | * @dev: Device to check |
| 109 | * @buf: Buffer to put the string |
| 110 | * @size: Maximum size of buffer |
| 111 | * @return length of string, or -ENOSPC it no space |
| 112 | */ |
| 113 | int (*get_desc)(struct udevice *dev, char *buf, int size); |
| 114 | |
| 115 | /** |
| 116 | * send() - send data to the TPM |
| 117 | * |
| 118 | * @dev: Device to talk to |
| 119 | * @sendbuf: Buffer of the data to send |
| 120 | * @send_size: Size of the data to send |
| 121 | * |
| 122 | * Returns 0 on success or -ve on failure. |
| 123 | */ |
| 124 | int (*send)(struct udevice *dev, const u8 *sendbuf, size_t send_size); |
| 125 | |
| 126 | /** |
| 127 | * recv() - receive a response from the TPM |
| 128 | * |
| 129 | * @dev: Device to talk to |
| 130 | * @recvbuf: Buffer to save the response to |
| 131 | * @max_size: Maximum number of bytes to receive |
| 132 | * |
| 133 | * Returns number of bytes received on success, -EAGAIN if the TPM |
| 134 | * response is not ready, -EINTR if cancelled, or other -ve value on |
| 135 | * failure. |
| 136 | */ |
| 137 | int (*recv)(struct udevice *dev, u8 *recvbuf, size_t max_size); |
| 138 | |
| 139 | /** |
| 140 | * cleanup() - clean up after an operation in progress |
| 141 | * |
| 142 | * This is called if receiving times out. The TPM may need to abort |
| 143 | * the current transaction if it did not complete, and make itself |
| 144 | * ready for another. |
| 145 | * |
| 146 | * @dev: Device to talk to |
| 147 | */ |
| 148 | int (*cleanup)(struct udevice *dev); |
| 149 | |
| 150 | /** |
| 151 | * xfer() - send data to the TPM and get response |
| 152 | * |
| 153 | * This method is optional. If it exists it is used in preference |
| 154 | * to send(), recv() and cleanup(). It should handle all aspects of |
| 155 | * TPM communication for a single transfer. |
| 156 | * |
| 157 | * @dev: Device to talk to |
| 158 | * @sendbuf: Buffer of the data to send |
| 159 | * @send_size: Size of the data to send |
| 160 | * @recvbuf: Buffer to save the response to |
| 161 | * @recv_size: Pointer to the size of the response buffer |
| 162 | * |
| 163 | * Returns 0 on success (and places the number of response bytes at |
| 164 | * recv_size) or -ve on failure. |
| 165 | */ |
| 166 | int (*xfer)(struct udevice *dev, const u8 *sendbuf, size_t send_size, |
| 167 | u8 *recvbuf, size_t *recv_size); |
| 168 | }; |
| 169 | |
| 170 | #define tpm_get_ops(dev) ((struct tpm_ops *)device_get_ops(dev)) |
| 171 | |
| 172 | #define MAKE_TPM_CMD_ENTRY(cmd) \ |
| 173 | U_BOOT_CMD_MKENT(cmd, 0, 1, do_tpm_ ## cmd, "", "") |
| 174 | |
| 175 | #define TPM_COMMAND_NO_ARG(cmd) \ |
| 176 | int do_##cmd(cmd_tbl_t *cmdtp, int flag, \ |
| 177 | int argc, char * const argv[]) \ |
| 178 | { \ |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 179 | struct udevice *dev; \ |
| 180 | int rc; \ |
| 181 | \ |
| 182 | rc = get_tpm(&dev); \ |
| 183 | if (rc) \ |
| 184 | return rc; \ |
Miquel Raynal | 4c6759e | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 185 | if (argc != 1) \ |
| 186 | return CMD_RET_USAGE; \ |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 187 | return report_return_code(cmd(dev)); \ |
Miquel Raynal | 4c6759e | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | /** |
Simon Glass | 5e7941c | 2018-11-18 14:22:26 -0700 | [diff] [blame] | 191 | * tpm_open() - Request access to locality 0 for the caller |
| 192 | * |
| 193 | * After all commands have been completed the caller is supposed to |
| 194 | * call tpm_close(). |
| 195 | * |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 196 | * @dev - TPM device |
Simon Glass | 5e7941c | 2018-11-18 14:22:26 -0700 | [diff] [blame] | 197 | * Returns 0 on success, -ve on failure. |
| 198 | */ |
| 199 | int tpm_open(struct udevice *dev); |
| 200 | |
| 201 | /** |
| 202 | * tpm_close() - Close the current session |
| 203 | * |
| 204 | * Releasing the locked locality. Returns 0 on success, -ve 1 on |
| 205 | * failure (in case lock removal did not succeed). |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 206 | * |
| 207 | * @dev - TPM device |
| 208 | * Returns 0 on success, -ve on failure. |
Simon Glass | 5e7941c | 2018-11-18 14:22:26 -0700 | [diff] [blame] | 209 | */ |
| 210 | int tpm_close(struct udevice *dev); |
| 211 | |
| 212 | /** |
Simon Glass | 36e2a04 | 2018-11-23 21:29:33 -0700 | [diff] [blame] | 213 | * tpm_clear_and_reenable() - Force clear the TPM and reenable it |
| 214 | * |
| 215 | * @dev: TPM device |
| 216 | * @return 0 on success, -ve on failure |
| 217 | */ |
| 218 | u32 tpm_clear_and_reenable(struct udevice *dev); |
| 219 | |
| 220 | /** |
Miquel Raynal | 4c6759e | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 221 | * tpm_get_desc() - Get a text description of the TPM |
| 222 | * |
| 223 | * @dev: Device to check |
| 224 | * @buf: Buffer to put the string |
| 225 | * @size: Maximum size of buffer |
| 226 | * @return length of string, or -ENOSPC it no space |
| 227 | */ |
| 228 | int tpm_get_desc(struct udevice *dev, char *buf, int size); |
| 229 | |
| 230 | /** |
| 231 | * tpm_xfer() - send data to the TPM and get response |
| 232 | * |
| 233 | * This first uses the device's send() method to send the bytes. Then it calls |
| 234 | * recv() to get the reply. If recv() returns -EAGAIN then it will delay a |
| 235 | * short time and then call recv() again. |
| 236 | * |
| 237 | * Regardless of whether recv() completes successfully, it will then call |
| 238 | * cleanup() to finish the transaction. |
| 239 | * |
| 240 | * Note that the outgoing data is inspected to determine command type |
| 241 | * (ordinal) and a timeout is used for that command type. |
| 242 | * |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 243 | * @dev - TPM device |
Miquel Raynal | 4c6759e | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 244 | * @sendbuf - buffer of the data to send |
| 245 | * @send_size size of the data to send |
| 246 | * @recvbuf - memory to save the response to |
| 247 | * @recv_len - pointer to the size of the response buffer |
| 248 | * |
| 249 | * Returns 0 on success (and places the number of response bytes at |
| 250 | * recv_len) or -ve on failure. |
| 251 | */ |
| 252 | int tpm_xfer(struct udevice *dev, const u8 *sendbuf, size_t send_size, |
| 253 | u8 *recvbuf, size_t *recv_size); |
| 254 | |
| 255 | /** |
| 256 | * Initialize TPM device. It must be called before any TPM commands. |
| 257 | * |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 258 | * @dev - TPM device |
Miquel Raynal | 4c6759e | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 259 | * @return 0 on success, non-0 on error. |
| 260 | */ |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 261 | int tpm_init(struct udevice *dev); |
Miquel Raynal | 4c6759e | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 262 | |
| 263 | /** |
Miquel Raynal | 79920d1 | 2018-07-19 22:35:09 +0200 | [diff] [blame] | 264 | * Retrieve the array containing all the v1 (resp. v2) commands. |
Miquel Raynal | 4c6759e | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 265 | * |
| 266 | * @return a cmd_tbl_t array. |
| 267 | */ |
Miquel Raynal | 79920d1 | 2018-07-19 22:35:09 +0200 | [diff] [blame] | 268 | #if defined(CONFIG_TPM_V1) |
| 269 | cmd_tbl_t *get_tpm1_commands(unsigned int *size); |
| 270 | #else |
| 271 | static inline cmd_tbl_t *get_tpm1_commands(unsigned int *size) |
| 272 | { |
| 273 | return NULL; |
| 274 | } |
| 275 | #endif |
| 276 | #if defined(CONFIG_TPM_V2) |
| 277 | cmd_tbl_t *get_tpm2_commands(unsigned int *size); |
| 278 | #else |
| 279 | static inline cmd_tbl_t *get_tpm2_commands(unsigned int *size) |
| 280 | { |
| 281 | return NULL; |
| 282 | } |
| 283 | #endif |
Miquel Raynal | 4c6759e | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 284 | |
Simon Glass | e03dbde | 2018-11-23 21:29:32 -0700 | [diff] [blame] | 285 | /** |
| 286 | * tpm_get_version() - Find the version of a TPM |
| 287 | * |
| 288 | * This checks the uclass data for a TPM device and returns the version number |
| 289 | * it supports. |
| 290 | * |
| 291 | * @dev: TPM device |
| 292 | * @return version number (TPM_V1 or TPMV2) |
| 293 | */ |
| 294 | enum tpm_version tpm_get_version(struct udevice *dev); |
| 295 | |
Miquel Raynal | 4c6759e | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 296 | #endif /* __TPM_COMMON_H */ |