Peter Huewe | d5a36f6 | 2018-06-12 00:59:26 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: BSD-2 */ |
Philip Tricca | 21fa321 | 2018-02-19 06:43:56 -0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2015 - 2018 Intel Corporation |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 4 | * All rights reserved. |
Philip Tricca | 21fa321 | 2018-02-19 06:43:56 -0800 | [diff] [blame] | 5 | */ |
Will Arthur | ca8e7f3 | 2015-08-03 15:35:19 -0400 | [diff] [blame] | 6 | |
Will Arthur | ca8e7f3 | 2015-08-03 15:35:19 -0400 | [diff] [blame] | 7 | #include <errno.h> |
Will Arthur | ca8e7f3 | 2015-08-03 15:35:19 -0400 | [diff] [blame] | 8 | #include <fcntl.h> |
Andreas Fuchs | 43a0d76 | 2017-11-30 15:41:05 +0100 | [diff] [blame] | 9 | #include <inttypes.h> |
Philip Tricca | b5fe103 | 2018-02-27 15:05:44 -0800 | [diff] [blame] | 10 | #include <stdio.h> |
| 11 | #include <stdlib.h> |
Philip Tricca | 72976fc | 2018-02-27 16:54:58 -0800 | [diff] [blame] | 12 | #include <string.h> |
Philip Tricca | b5fe103 | 2018-02-27 15:05:44 -0800 | [diff] [blame] | 13 | #include <sys/stat.h> |
| 14 | #include <sys/types.h> |
| 15 | #include <unistd.h> |
| 16 | |
Philip Tricca | 910f17c | 2018-03-15 12:38:37 -0700 | [diff] [blame] | 17 | #include "tss2_tcti.h" |
Philip Tricca | 8ffd3c4 | 2018-03-09 16:27:24 -0800 | [diff] [blame] | 18 | #include "tss2_tcti_device.h" |
Philip Tricca | 850bb59 | 2018-04-03 09:29:22 -0700 | [diff] [blame] | 19 | |
| 20 | #include "tcti-common.h" |
| 21 | #include "tcti-device.h" |
| 22 | #include "util/io.h" |
Andreas Fuchs | 43a0d76 | 2017-11-30 15:41:05 +0100 | [diff] [blame] | 23 | #define LOGMODULE tcti |
Philip Tricca | a7c51ce | 2018-03-10 18:28:25 -0800 | [diff] [blame] | 24 | #include "util/log.h" |
Will Arthur | ca8e7f3 | 2015-08-03 15:35:19 -0400 | [diff] [blame] | 25 | |
Philip Tricca | 5a23528 | 2018-02-27 14:59:49 -0800 | [diff] [blame] | 26 | #define TCTI_DEVICE_DEFAULT "/dev/tpm0" |
Philip Tricca | 850bb59 | 2018-04-03 09:29:22 -0700 | [diff] [blame] | 27 | /* |
| 28 | * This function wraps the "up-cast" of the opaque TCTI context type to the |
| 29 | * type for the device TCTI context. The only safe-guard we have to ensure |
| 30 | * this operation is possible is the magic number for the device TCTI context. |
| 31 | * If passed a NULL context, or the magic number check fails, this function |
| 32 | * will return NULL. |
| 33 | */ |
| 34 | TSS2_TCTI_DEVICE_CONTEXT* |
| 35 | tcti_device_context_cast (TSS2_TCTI_CONTEXT *tcti_ctx) |
| 36 | { |
| 37 | if (tcti_ctx != NULL && TSS2_TCTI_MAGIC (tcti_ctx) == TCTI_DEVICE_MAGIC) { |
| 38 | return (TSS2_TCTI_DEVICE_CONTEXT*)tcti_ctx; |
| 39 | } |
| 40 | return NULL; |
| 41 | } |
| 42 | /* |
| 43 | * This function down-casts the device TCTI context to the common context |
| 44 | * defined in the tcti-common module. |
| 45 | */ |
| 46 | TSS2_TCTI_COMMON_CONTEXT* |
| 47 | tcti_device_down_cast (TSS2_TCTI_DEVICE_CONTEXT *tcti_dev) |
| 48 | { |
| 49 | if (tcti_dev == NULL) { |
| 50 | return NULL; |
| 51 | } |
| 52 | return &tcti_dev->common; |
| 53 | } |
Philip Tricca | 5a23528 | 2018-02-27 14:59:49 -0800 | [diff] [blame] | 54 | |
Philip Tricca | df25eae | 2018-02-27 15:21:20 -0800 | [diff] [blame] | 55 | TSS2_RC |
| 56 | tcti_device_transmit ( |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 57 | TSS2_TCTI_CONTEXT *tctiContext, |
| 58 | size_t command_size, |
Philip Tricca | df25eae | 2018-02-27 15:21:20 -0800 | [diff] [blame] | 59 | const uint8_t *command_buffer) |
Will Arthur | ca8e7f3 | 2015-08-03 15:35:19 -0400 | [diff] [blame] | 60 | { |
Philip Tricca | 850bb59 | 2018-04-03 09:29:22 -0700 | [diff] [blame] | 61 | TSS2_TCTI_DEVICE_CONTEXT *tcti_dev = tcti_device_context_cast (tctiContext); |
| 62 | TSS2_TCTI_COMMON_CONTEXT *tcti_common = tcti_device_down_cast (tcti_dev); |
Philip Tricca | cac4c3a | 2018-03-05 18:21:41 -0800 | [diff] [blame] | 63 | TSS2_RC rc = TSS2_RC_SUCCESS; |
Will Arthur | ca8e7f3 | 2015-08-03 15:35:19 -0400 | [diff] [blame] | 64 | ssize_t size; |
| 65 | |
Philip Tricca | 850bb59 | 2018-04-03 09:29:22 -0700 | [diff] [blame] | 66 | if (tcti_dev == NULL) { |
| 67 | return TSS2_TCTI_RC_BAD_CONTEXT; |
| 68 | } |
| 69 | rc = tcti_common_transmit_checks (tcti_common, command_buffer); |
Philip Tricca | cac4c3a | 2018-03-05 18:21:41 -0800 | [diff] [blame] | 70 | if (rc != TSS2_RC_SUCCESS) { |
| 71 | return rc; |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 72 | } |
Philip Tricca | 382f40e | 2018-02-19 07:43:00 -0800 | [diff] [blame] | 73 | LOGBLOB_DEBUG (command_buffer, |
| 74 | command_size, |
Philip Tricca | efe1d06 | 2018-02-19 07:59:21 -0800 | [diff] [blame] | 75 | "sending %zu byte command buffer:", |
Philip Tricca | 382f40e | 2018-02-19 07:43:00 -0800 | [diff] [blame] | 76 | command_size); |
Philip Tricca | 850bb59 | 2018-04-03 09:29:22 -0700 | [diff] [blame] | 77 | size = write_all (tcti_dev->fd, |
Philip Tricca | e487b99 | 2018-02-19 15:42:48 -0800 | [diff] [blame] | 78 | command_buffer, |
| 79 | command_size); |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 80 | if (size < 0) { |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 81 | return TSS2_TCTI_RC_IO_ERROR; |
| 82 | } else if ((size_t)size != command_size) { |
Philip Tricca | efe1d06 | 2018-02-19 07:59:21 -0800 | [diff] [blame] | 83 | LOG_ERROR ("wrong number of bytes written. Expected %zu, wrote %zd.", |
| 84 | command_size, |
| 85 | size); |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 86 | return TSS2_TCTI_RC_IO_ERROR; |
wcarthur | eedecd6 | 2015-11-20 16:59:45 -0500 | [diff] [blame] | 87 | } |
Philip Tricca | dfa41a5 | 2016-07-20 17:43:57 -0700 | [diff] [blame] | 88 | |
Philip Tricca | 850bb59 | 2018-04-03 09:29:22 -0700 | [diff] [blame] | 89 | tcti_common->state = TCTI_STATE_RECEIVE; |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 90 | return TSS2_RC_SUCCESS; |
Will Arthur | ca8e7f3 | 2015-08-03 15:35:19 -0400 | [diff] [blame] | 91 | } |
Philip Tricca | 7ce0cc4 | 2018-04-08 18:41:05 -0700 | [diff] [blame] | 92 | /* |
| 93 | * This receive function deviates from the spec a bit. Calling this function |
| 94 | * with a NULL 'tctiContext' parameter *should* result in the required size for |
| 95 | * the response buffer being returned to the caller. We would typically do this |
| 96 | * by reading the response's header and then returning the size to the caller. |
| 97 | * We can't do that on account of the TPM2 kernel driver closing any connection |
| 98 | * that doesn't read the whole response buffer in one 'read' call. |
| 99 | * |
| 100 | * Instead, if the caller queries the size, we return 4k just to be on the |
| 101 | * safe side. We do *not* however verify that the provided buffer is large |
| 102 | * enough to hold the full response (we can't). If the caller provides us with |
| 103 | * a buffer less than 4k we'll read as much of the response as we can given |
| 104 | * the size of the buffer. If we get enough of the response to read the size |
| 105 | * field, we check to see if the buffer was large enough to get the full |
| 106 | * response. If the response header claims it's larger than the provided |
| 107 | * buffer we print a warning. This allows "expert applications" to |
| 108 | * precalculate the required response buffer size for whatever commands they |
| 109 | * may send. |
| 110 | */ |
Philip Tricca | df25eae | 2018-02-27 15:21:20 -0800 | [diff] [blame] | 111 | TSS2_RC |
| 112 | tcti_device_receive ( |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 113 | TSS2_TCTI_CONTEXT *tctiContext, |
| 114 | size_t *response_size, |
Philip Tricca | 32c4158 | 2018-02-23 05:28:23 +0000 | [diff] [blame] | 115 | uint8_t *response_buffer, |
Philip Tricca | df25eae | 2018-02-27 15:21:20 -0800 | [diff] [blame] | 116 | int32_t timeout) |
Will Arthur | ca8e7f3 | 2015-08-03 15:35:19 -0400 | [diff] [blame] | 117 | { |
Philip Tricca | 850bb59 | 2018-04-03 09:29:22 -0700 | [diff] [blame] | 118 | TSS2_TCTI_DEVICE_CONTEXT *tcti_dev = tcti_device_context_cast (tctiContext); |
| 119 | TSS2_TCTI_COMMON_CONTEXT *tcti_common = tcti_device_down_cast (tcti_dev); |
Philip Tricca | cac4c3a | 2018-03-05 18:21:41 -0800 | [diff] [blame] | 120 | TSS2_RC rc = TSS2_RC_SUCCESS; |
Will Arthur | ca8e7f3 | 2015-08-03 15:35:19 -0400 | [diff] [blame] | 121 | ssize_t size; |
Philip Tricca | dfa41a5 | 2016-07-20 17:43:57 -0700 | [diff] [blame] | 122 | |
Philip Tricca | 850bb59 | 2018-04-03 09:29:22 -0700 | [diff] [blame] | 123 | if (tcti_dev == NULL) { |
| 124 | return TSS2_TCTI_RC_BAD_CONTEXT; |
| 125 | } |
| 126 | rc = tcti_common_receive_checks (tcti_common, response_size); |
Philip Tricca | cac4c3a | 2018-03-05 18:21:41 -0800 | [diff] [blame] | 127 | if (rc != TSS2_RC_SUCCESS) { |
Philip Tricca | 5477c2a | 2018-03-07 15:39:28 -0800 | [diff] [blame] | 128 | return rc; |
Philip Tricca | dfa41a5 | 2016-07-20 17:43:57 -0700 | [diff] [blame] | 129 | } |
Philip Tricca | e9416f3 | 2018-02-27 16:08:00 -0800 | [diff] [blame] | 130 | if (timeout != TSS2_TCTI_TIMEOUT_BLOCK) { |
| 131 | LOG_WARNING ("The underlying IPC mechanism does not support " |
| 132 | "asynchronous I/O. The 'timeout' parameter must be " |
| 133 | "TSS2_TCTI_TIMEOUT_BLOCK"); |
| 134 | return TSS2_TCTI_RC_BAD_VALUE; |
| 135 | } |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 136 | if (response_buffer == NULL) { |
Philip Tricca | 7ce0cc4 | 2018-04-08 18:41:05 -0700 | [diff] [blame] | 137 | LOG_DEBUG ("Caller queried for size but linux kernel doesn't allow this. " |
| 138 | "Returning 4k which is the max size for a response buffer."); |
| 139 | *response_size = 4096; |
Philip Tricca | 5477c2a | 2018-03-07 15:39:28 -0800 | [diff] [blame] | 140 | return TSS2_RC_SUCCESS; |
Will Arthur | ca8e7f3 | 2015-08-03 15:35:19 -0400 | [diff] [blame] | 141 | } |
Philip Tricca | 7ce0cc4 | 2018-04-08 18:41:05 -0700 | [diff] [blame] | 142 | if (*response_size < 4096) { |
| 143 | LOG_INFO ("Caller provided buffer that *may* not be large enough to " |
| 144 | "hold the response buffer."); |
Philip Tricca | dfa41a5 | 2016-07-20 17:43:57 -0700 | [diff] [blame] | 145 | } |
Philip Tricca | 7ce0cc4 | 2018-04-08 18:41:05 -0700 | [diff] [blame] | 146 | /* |
| 147 | * The kernel driver will only return a response buffer in a single read |
| 148 | * operation. If we try to read again before sending another command |
| 149 | * the kernel will close the file descriptor and we'll get an EOF. |
| 150 | */ |
David J. Maria | 06db575 | 2018-06-19 14:52:37 -0700 | [diff] [blame] | 151 | TEMP_RETRY (size, read (tcti_dev->fd, response_buffer, *response_size)); |
Philip Tricca | 7a10ee3 | 2018-03-06 12:48:39 -0800 | [diff] [blame] | 152 | if (size < 0) { |
Philip Tricca | 7ce0cc4 | 2018-04-08 18:41:05 -0700 | [diff] [blame] | 153 | LOG_ERROR ("Failed to read response from fd %d, got errno %d: %s", |
| 154 | tcti_dev->fd, errno, strerror (errno)); |
| 155 | return TSS2_TCTI_RC_IO_ERROR; |
| 156 | } |
| 157 | if (size == 0) { |
| 158 | LOG_WARNING ("Got EOF instead of response."); |
| 159 | rc = TSS2_TCTI_RC_NO_CONNECTION; |
Philip Tricca | 850bb59 | 2018-04-03 09:29:22 -0700 | [diff] [blame] | 160 | goto out; |
wcarthur | 74a92a4 | 2015-12-07 16:02:12 -0500 | [diff] [blame] | 161 | } |
Philip Tricca | 7ce0cc4 | 2018-04-08 18:41:05 -0700 | [diff] [blame] | 162 | LOGBLOB_DEBUG(response_buffer, size, "Response Received"); |
| 163 | if (size < (ssize_t)TPM_HEADER_SIZE) { |
| 164 | LOG_ERROR ("Received %zu bytes, not enough to hold a TPM2 response " |
| 165 | "header.", size); |
| 166 | rc = TSS2_TCTI_RC_GENERAL_FAILURE; |
| 167 | goto out; |
| 168 | } |
| 169 | rc = header_unmarshal (response_buffer, &tcti_common->header); |
| 170 | if (rc != TSS2_RC_SUCCESS) { |
| 171 | goto out; |
| 172 | } |
| 173 | if ((size_t)size != tcti_common->header.size) { |
| 174 | LOG_WARNING ("TPM2 header size disagrees with number of bytes read " |
| 175 | "from fd %d. Header says %u but we read %zu bytes.", |
Philip Tricca | 097cae6 | 2018-04-08 13:42:29 -0700 | [diff] [blame] | 176 | tcti_dev->fd, tcti_common->header.size, size); |
Philip Tricca | 7ce0cc4 | 2018-04-08 18:41:05 -0700 | [diff] [blame] | 177 | } |
| 178 | if (*response_size < tcti_common->header.size) { |
| 179 | LOG_WARNING ("TPM2 response header size is larger than the provided " |
| 180 | "buffer: future use of this TCTI will likely fail."); |
| 181 | rc = TSS2_TCTI_RC_GENERAL_FAILURE; |
| 182 | } |
| 183 | *response_size = size; |
Philip Tricca | 5477c2a | 2018-03-07 15:39:28 -0800 | [diff] [blame] | 184 | /* |
| 185 | * Executing code beyond this point transitions the state machine to |
| 186 | * TRANSMIT. Another call to this function will not be possible until |
| 187 | * another command is sent to the TPM. |
| 188 | */ |
Philip Tricca | 850bb59 | 2018-04-03 09:29:22 -0700 | [diff] [blame] | 189 | out: |
Philip Tricca | 850bb59 | 2018-04-03 09:29:22 -0700 | [diff] [blame] | 190 | tcti_common->state = TCTI_STATE_TRANSMIT; |
Philip Tricca | dfa41a5 | 2016-07-20 17:43:57 -0700 | [diff] [blame] | 191 | |
Philip Tricca | cac4c3a | 2018-03-05 18:21:41 -0800 | [diff] [blame] | 192 | return rc; |
Will Arthur | ca8e7f3 | 2015-08-03 15:35:19 -0400 | [diff] [blame] | 193 | } |
| 194 | |
Philip Tricca | df25eae | 2018-02-27 15:21:20 -0800 | [diff] [blame] | 195 | void |
| 196 | tcti_device_finalize ( |
| 197 | TSS2_TCTI_CONTEXT *tctiContext) |
Will Arthur | ca8e7f3 | 2015-08-03 15:35:19 -0400 | [diff] [blame] | 198 | { |
Philip Tricca | 850bb59 | 2018-04-03 09:29:22 -0700 | [diff] [blame] | 199 | TSS2_TCTI_DEVICE_CONTEXT *tcti_dev = tcti_device_context_cast (tctiContext); |
| 200 | TSS2_TCTI_COMMON_CONTEXT *tcti_common = tcti_device_down_cast (tcti_dev); |
Philip Tricca | 24f555c | 2017-10-13 17:51:53 -0700 | [diff] [blame] | 201 | |
Philip Tricca | 850bb59 | 2018-04-03 09:29:22 -0700 | [diff] [blame] | 202 | if (tcti_dev == NULL) { |
Philip Tricca | 3f1828c | 2017-10-18 18:44:13 -0700 | [diff] [blame] | 203 | return; |
wcarthur | f979e35 | 2015-11-24 17:34:44 -0500 | [diff] [blame] | 204 | } |
Philip Tricca | 850bb59 | 2018-04-03 09:29:22 -0700 | [diff] [blame] | 205 | close (tcti_dev->fd); |
| 206 | tcti_common->state = TCTI_STATE_FINAL; |
Will Arthur | ca8e7f3 | 2015-08-03 15:35:19 -0400 | [diff] [blame] | 207 | } |
| 208 | |
Philip Tricca | df25eae | 2018-02-27 15:21:20 -0800 | [diff] [blame] | 209 | TSS2_RC |
| 210 | tcti_device_cancel ( |
| 211 | TSS2_TCTI_CONTEXT *tctiContext) |
Will Arthur | ca8e7f3 | 2015-08-03 15:35:19 -0400 | [diff] [blame] | 212 | { |
Philip Tricca | 5917fbf | 2017-06-29 08:24:09 -0700 | [diff] [blame] | 213 | /* Linux driver doesn't expose a mechanism to cancel commands. */ |
Andreas Fuchs | baeb2be | 2018-03-29 15:39:50 +0200 | [diff] [blame] | 214 | (void)(tctiContext); |
Philip Tricca | 5917fbf | 2017-06-29 08:24:09 -0700 | [diff] [blame] | 215 | return TSS2_TCTI_RC_NOT_IMPLEMENTED; |
| 216 | } |
Will Arthur | ca8e7f3 | 2015-08-03 15:35:19 -0400 | [diff] [blame] | 217 | |
Philip Tricca | df25eae | 2018-02-27 15:21:20 -0800 | [diff] [blame] | 218 | TSS2_RC |
| 219 | tcti_device_get_poll_handles ( |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 220 | TSS2_TCTI_CONTEXT *tctiContext, |
Philip Tricca | 5917fbf | 2017-06-29 08:24:09 -0700 | [diff] [blame] | 221 | TSS2_TCTI_POLL_HANDLE *handles, |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 222 | size_t *num_handles) |
Philip Tricca | 5917fbf | 2017-06-29 08:24:09 -0700 | [diff] [blame] | 223 | { |
| 224 | /* Linux driver doesn't support polling. */ |
Andreas Fuchs | baeb2be | 2018-03-29 15:39:50 +0200 | [diff] [blame] | 225 | (void)(tctiContext); |
| 226 | (void)(handles); |
| 227 | (void)(num_handles); |
Philip Tricca | 5917fbf | 2017-06-29 08:24:09 -0700 | [diff] [blame] | 228 | return TSS2_TCTI_RC_NOT_IMPLEMENTED; |
Will Arthur | ca8e7f3 | 2015-08-03 15:35:19 -0400 | [diff] [blame] | 229 | } |
| 230 | |
Philip Tricca | df25eae | 2018-02-27 15:21:20 -0800 | [diff] [blame] | 231 | TSS2_RC |
| 232 | tcti_device_set_locality ( |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 233 | TSS2_TCTI_CONTEXT *tctiContext, |
Philip Tricca | df25eae | 2018-02-27 15:21:20 -0800 | [diff] [blame] | 234 | uint8_t locality) |
Will Arthur | ca8e7f3 | 2015-08-03 15:35:19 -0400 | [diff] [blame] | 235 | { |
Philip Tricca | 5917fbf | 2017-06-29 08:24:09 -0700 | [diff] [blame] | 236 | /* |
| 237 | * Linux driver doesn't expose a mechanism for user space applications |
| 238 | * to set locality. |
| 239 | */ |
Andreas Fuchs | baeb2be | 2018-03-29 15:39:50 +0200 | [diff] [blame] | 240 | (void)(tctiContext); |
| 241 | (void)(locality); |
Philip Tricca | 5917fbf | 2017-06-29 08:24:09 -0700 | [diff] [blame] | 242 | return TSS2_TCTI_RC_NOT_IMPLEMENTED; |
Will Arthur | ca8e7f3 | 2015-08-03 15:35:19 -0400 | [diff] [blame] | 243 | } |
| 244 | |
Philip Tricca | df25eae | 2018-02-27 15:21:20 -0800 | [diff] [blame] | 245 | TSS2_RC |
| 246 | Tss2_Tcti_Device_Init ( |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 247 | TSS2_TCTI_CONTEXT *tctiContext, |
Philip Tricca | 5a23528 | 2018-02-27 14:59:49 -0800 | [diff] [blame] | 248 | size_t *size, |
Philip Tricca | df25eae | 2018-02-27 15:21:20 -0800 | [diff] [blame] | 249 | const char *conf) |
Will Arthur | ca8e7f3 | 2015-08-03 15:35:19 -0400 | [diff] [blame] | 250 | { |
Philip Tricca | 850bb59 | 2018-04-03 09:29:22 -0700 | [diff] [blame] | 251 | TSS2_TCTI_DEVICE_CONTEXT *tcti_dev; |
| 252 | TSS2_TCTI_COMMON_CONTEXT *tcti_common; |
Philip Tricca | 5a23528 | 2018-02-27 14:59:49 -0800 | [diff] [blame] | 253 | const char *dev_path = conf != NULL ? conf : TCTI_DEVICE_DEFAULT; |
Will Arthur | ca8e7f3 | 2015-08-03 15:35:19 -0400 | [diff] [blame] | 254 | |
Philip Tricca | 5a23528 | 2018-02-27 14:59:49 -0800 | [diff] [blame] | 255 | if (tctiContext == NULL && size == NULL) { |
Philip Tricca | 5fd02f7 | 2016-11-17 18:22:04 -0800 | [diff] [blame] | 256 | return TSS2_TCTI_RC_BAD_VALUE; |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 257 | } else if (tctiContext == NULL) { |
Philip Tricca | 850bb59 | 2018-04-03 09:29:22 -0700 | [diff] [blame] | 258 | *size = sizeof (TSS2_TCTI_DEVICE_CONTEXT); |
Will Arthur | ca8e7f3 | 2015-08-03 15:35:19 -0400 | [diff] [blame] | 259 | return TSS2_RC_SUCCESS; |
| 260 | } |
Philip Tricca | 068d563 | 2016-03-11 12:23:40 -0800 | [diff] [blame] | 261 | |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 262 | /* Init TCTI context */ |
Philip Tricca | 850bb59 | 2018-04-03 09:29:22 -0700 | [diff] [blame] | 263 | TSS2_TCTI_MAGIC (tctiContext) = TCTI_DEVICE_MAGIC; |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 264 | TSS2_TCTI_VERSION (tctiContext) = TCTI_VERSION; |
Philip Tricca | df25eae | 2018-02-27 15:21:20 -0800 | [diff] [blame] | 265 | TSS2_TCTI_TRANSMIT (tctiContext) = tcti_device_transmit; |
| 266 | TSS2_TCTI_RECEIVE (tctiContext) = tcti_device_receive; |
| 267 | TSS2_TCTI_FINALIZE (tctiContext) = tcti_device_finalize; |
| 268 | TSS2_TCTI_CANCEL (tctiContext) = tcti_device_cancel; |
| 269 | TSS2_TCTI_GET_POLL_HANDLES (tctiContext) = tcti_device_get_poll_handles; |
| 270 | TSS2_TCTI_SET_LOCALITY (tctiContext) = tcti_device_set_locality; |
Philip Tricca | ee41115 | 2018-02-23 19:16:04 -0800 | [diff] [blame] | 271 | TSS2_TCTI_MAKE_STICKY (tctiContext) = tcti_make_sticky_not_implemented; |
Philip Tricca | 850bb59 | 2018-04-03 09:29:22 -0700 | [diff] [blame] | 272 | tcti_dev = tcti_device_context_cast (tctiContext); |
| 273 | tcti_common = tcti_device_down_cast (tcti_dev); |
| 274 | tcti_common->state = TCTI_STATE_TRANSMIT; |
| 275 | memset (&tcti_common->header, 0, sizeof (tcti_common->header)); |
| 276 | tcti_common->locality = 3; |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 277 | |
Philip Tricca | 850bb59 | 2018-04-03 09:29:22 -0700 | [diff] [blame] | 278 | tcti_dev->fd = open (dev_path, O_RDWR); |
| 279 | if (tcti_dev->fd < 0) { |
Philip Tricca | 72976fc | 2018-02-27 16:54:58 -0800 | [diff] [blame] | 280 | LOG_ERROR ("Failed to open device file %s: %s", |
| 281 | dev_path, strerror (errno)); |
Philip Tricca | a003d76 | 2017-10-17 13:25:42 -0700 | [diff] [blame] | 282 | return TSS2_TCTI_RC_IO_ERROR; |
Will Arthur | ca8e7f3 | 2015-08-03 15:35:19 -0400 | [diff] [blame] | 283 | } |
| 284 | |
Philip Tricca | 5a23528 | 2018-02-27 14:59:49 -0800 | [diff] [blame] | 285 | return TSS2_RC_SUCCESS; |
Philip Tricca | 6d4ae3e | 2017-12-06 14:23:52 -0800 | [diff] [blame] | 286 | } |
| 287 | |
Andreas Fuchs | 37b45c6 | 2018-03-29 16:54:33 +0200 | [diff] [blame] | 288 | const TSS2_TCTI_INFO tss2_tcti_info = { |
Philip Tricca | df1410e | 2018-04-23 13:07:17 -0700 | [diff] [blame] | 289 | .version = TCTI_VERSION, |
Philip Tricca | 6d4ae3e | 2017-12-06 14:23:52 -0800 | [diff] [blame] | 290 | .name = "tcti-device", |
| 291 | .description = "TCTI module for communication with Linux kernel interface.", |
Philip Tricca | 001e8bf | 2018-01-08 16:52:09 -0800 | [diff] [blame] | 292 | .config_help = "Path to TPM character device. Default value is: " |
| 293 | "TCTI_DEVICE_DEFAULT", |
Philip Tricca | 6d4ae3e | 2017-12-06 14:23:52 -0800 | [diff] [blame] | 294 | .init = Tss2_Tcti_Device_Init, |
| 295 | }; |
| 296 | |
| 297 | const TSS2_TCTI_INFO* |
| 298 | Tss2_Tcti_Info (void) |
| 299 | { |
| 300 | return &tss2_tcti_info; |
| 301 | } |