blob: d9135e5562331e79ce90c3ac98d97d1ae8c6441b [file] [log] [blame]
Will Arthurca8e7f32015-08-03 15:35:19 -04001//**********************************************************************;
2// Copyright (c) 2015, Intel Corporation
3// All rights reserved.
Philip Tricca1ea84a52015-11-19 18:07:06 -08004//
5// Redistribution and use in source and binary forms, with or without
Will Arthurca8e7f32015-08-03 15:35:19 -04006// modification, are permitted provided that the following conditions are met:
Philip Tricca1ea84a52015-11-19 18:07:06 -08007//
8// 1. Redistributions of source code must retain the above copyright notice,
Will Arthurca8e7f32015-08-03 15:35:19 -04009// this list of conditions and the following disclaimer.
Philip Tricca1ea84a52015-11-19 18:07:06 -080010//
11// 2. Redistributions in binary form must reproduce the above copyright notice,
12// this list of conditions and the following disclaimer in the documentation
Will Arthurca8e7f32015-08-03 15:35:19 -040013// and/or other materials provided with the distribution.
Philip Tricca1ea84a52015-11-19 18:07:06 -080014//
15// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
Will Arthurca8e7f32015-08-03 15:35:19 -040025// THE POSSIBILITY OF SUCH DAMAGE.
26//**********************************************************************;
27
28#include <stdio.h>
29#include <stdlib.h> // Needed for _wtoi
30
Philip Triccae03b8462016-07-08 19:51:23 -070031#include <sapi/tpm20.h>
Will Arthurca8e7f32015-08-03 15:35:19 -040032//#include "resourcemgr.h"
33//#include <sample.h>
Philip Triccac3dedc22016-01-15 13:47:22 -080034#include "sysapi_util.h"
Will Arthurca8e7f32015-08-03 15:35:19 -040035#include <errno.h>
36#include <sys/types.h>
37#include <sys/stat.h>
38#include <fcntl.h>
Will-nuc3ebfeb92015-10-29 15:53:27 -040039#include "debug.h"
wcarthureedecd62015-11-20 16:59:45 -050040#include "commonchecks.h"
Philip Tricca785e88d2016-02-13 12:02:44 -080041#include <tcti/tcti_device.h>
Philip Tricca068d5632016-03-11 12:23:40 -080042#include "logging.h"
Will Arthurca8e7f32015-08-03 15:35:19 -040043
44#ifdef _WIN32
45#define ssize_t int
46#elif __linux
47#include <unistd.h>
48#endif
49
50#define HOSTNAME_LENGTH 200
51
Philip Tricca068d5632016-03-11 12:23:40 -080052const char *deviceTctiName = "device TCTI";
Will Arthurca8e7f32015-08-03 15:35:19 -040053
54TSS2_RC LocalTpmSendTpmCommand(
55 TSS2_TCTI_CONTEXT *tctiContext, /* in */
56 size_t command_size, /* in */
57 uint8_t *command_buffer /* in */
58 )
59{
60 TSS2_RC rval = TSS2_RC_SUCCESS;
61 ssize_t size;
62
Will-nuc3ebfeb92015-10-29 15:53:27 -040063#ifdef DEBUG
wcarthureedecd62015-11-20 16:59:45 -050064 UINT32 commandCode;
65 UINT32 cnt;
66#endif
wcarthur1c827592016-04-27 11:07:51 -040067 printf_type rmPrefix;
Philip Triccadfa41a52016-07-20 17:43:57 -070068
wcarthureedecd62015-11-20 16:59:45 -050069 rval = CommonSendChecks( tctiContext, command_buffer );
Will-nuc3ebfeb92015-10-29 15:53:27 -040070
wcarthureedecd62015-11-20 16:59:45 -050071 if( rval == TSS2_RC_SUCCESS )
Will-nuc3ebfeb92015-10-29 15:53:27 -040072 {
wcarthur1c827592016-04-27 11:07:51 -040073 if( ( ( TSS2_TCTI_CONTEXT_INTEL *)tctiContext )->status.rmDebugPrefix == 1 )
74 rmPrefix = RM_PREFIX;
75 else
76 rmPrefix = NO_PREFIX;
77
wcarthureedecd62015-11-20 16:59:45 -050078#ifdef DEBUG
79 commandCode = CHANGE_ENDIAN_DWORD( ( (TPM20_Header_In *)command_buffer )->commandCode );
80 cnt = CHANGE_ENDIAN_DWORD(((TPM20_Header_In *) command_buffer)->commandSize);
81
wcarthur1c827592016-04-27 11:07:51 -040082 if( ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext )->status.debugMsgEnabled == 1 )
wcarthureedecd62015-11-20 16:59:45 -050083 {
wcarthur1c827592016-04-27 11:07:51 -040084 TCTI_LOG( tctiContext, rmPrefix, "" );
85 TCTI_LOG( tctiContext, rmPrefix, "Cmd sent: %s\n", strTpmCommandCode( commandCode ) );
86 DEBUG_PRINT_BUFFER( rmPrefix, command_buffer, cnt );
wcarthureedecd62015-11-20 16:59:45 -050087 }
Will-nuc3ebfeb92015-10-29 15:53:27 -040088#endif
89
wcarthureedecd62015-11-20 16:59:45 -050090 size = write( ( (TSS2_TCTI_CONTEXT_INTEL *)tctiContext )->devFile, command_buffer, command_size );
Will Arthurca8e7f32015-08-03 15:35:19 -040091
wcarthureedecd62015-11-20 16:59:45 -050092 if( size < 0 )
93 {
wcarthur1c827592016-04-27 11:07:51 -040094 TCTI_LOG( tctiContext, rmPrefix, "send failed with error: %d\n", errno );
wcarthureedecd62015-11-20 16:59:45 -050095 rval = TSS2_TCTI_RC_IO_ERROR;
96 }
97 else if( (size_t)size != command_size )
Philip Triccadfa41a52016-07-20 17:43:57 -070098 {
wcarthureedecd62015-11-20 16:59:45 -050099 rval = TSS2_TCTI_RC_IO_ERROR;
100 }
Will Arthurca8e7f32015-08-03 15:35:19 -0400101
wcarthureedecd62015-11-20 16:59:45 -0500102 if( rval == TSS2_RC_SUCCESS )
103 {
104 ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->previousStage = TCTI_STAGE_SEND_COMMAND;
wcarthur74a92a42015-12-07 16:02:12 -0500105 ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.tagReceived = 0;
106 ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.responseSizeReceived = 0;
107 ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.protocolResponseSizeReceived = 0;
108
wcarthureedecd62015-11-20 16:59:45 -0500109 }
110 }
Philip Triccadfa41a52016-07-20 17:43:57 -0700111
Will Arthurca8e7f32015-08-03 15:35:19 -0400112 return rval;
113}
114
115TSS2_RC LocalTpmReceiveTpmResponse(
116 TSS2_TCTI_CONTEXT *tctiContext, /* in */
117 size_t *response_size, /* out */
118 unsigned char *response_buffer, /* in */
119 int32_t timeout
120 )
121{
122 TSS2_RC rval = TSS2_RC_SUCCESS;
123 ssize_t size;
Will-nuc7f5680b2015-12-08 18:05:03 -0500124 unsigned int i;
wcarthur1c827592016-04-27 11:07:51 -0400125 printf_type rmPrefix;
Philip Triccadfa41a52016-07-20 17:43:57 -0700126
wcarthurf979e352015-11-24 17:34:44 -0500127 rval = CommonReceiveChecks( tctiContext, response_size, response_buffer );
wcarthur74a92a42015-12-07 16:02:12 -0500128 if( rval != TSS2_RC_SUCCESS )
Will Arthurca8e7f32015-08-03 15:35:19 -0400129 {
wcarthur74a92a42015-12-07 16:02:12 -0500130 goto retLocalTpmReceive;
Philip Triccadfa41a52016-07-20 17:43:57 -0700131 }
Will-nuc9ab34612015-10-29 17:02:47 -0400132
wcarthur1c827592016-04-27 11:07:51 -0400133 if( ( ( TSS2_TCTI_CONTEXT_INTEL *)tctiContext )->status.rmDebugPrefix == 1 )
134 rmPrefix = RM_PREFIX;
135 else
136 rmPrefix = NO_PREFIX;
137
Will-nuc8619aca2015-12-10 17:20:26 -0500138 if( ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.tagReceived == 0 )
wcarthur74a92a42015-12-07 16:02:12 -0500139 {
Will-nuc8619aca2015-12-10 17:20:26 -0500140 size = read( ( (TSS2_TCTI_CONTEXT_INTEL *)tctiContext )->devFile, &((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->responseBuffer[0], 4096 );
Will-nuc9ab34612015-10-29 17:02:47 -0400141
wcarthureedecd62015-11-20 16:59:45 -0500142 if( size < 0 )
Will-nuc9ab34612015-10-29 17:02:47 -0400143 {
wcarthur1c827592016-04-27 11:07:51 -0400144 TCTI_LOG( tctiContext, rmPrefix, "read failed with error: %d\n", errno );
wcarthureedecd62015-11-20 16:59:45 -0500145 rval = TSS2_TCTI_RC_IO_ERROR;
Will-nuc8619aca2015-12-10 17:20:26 -0500146 goto retLocalTpmReceive;
Will-nuc9ab34612015-10-29 17:02:47 -0400147 }
wcarthureedecd62015-11-20 16:59:45 -0500148 else
149 {
Will-nuc8619aca2015-12-10 17:20:26 -0500150 ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.tagReceived = 1;
151 ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->responseSize = size;
wcarthureedecd62015-11-20 16:59:45 -0500152 }
Will-nuc8619aca2015-12-10 17:20:26 -0500153
Will-nuc7f5680b2015-12-08 18:05:03 -0500154 ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->responseSize = size;
Will Arthurca8e7f32015-08-03 15:35:19 -0400155 }
Philip Triccadfa41a52016-07-20 17:43:57 -0700156
wcarthur74a92a42015-12-07 16:02:12 -0500157 if( response_buffer == NULL )
158 {
159 // In this case, just return the size
Will-nuc8619aca2015-12-10 17:20:26 -0500160 *response_size = ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->responseSize;
wcarthur74a92a42015-12-07 16:02:12 -0500161 goto retLocalTpmReceive;
Will Arthurca8e7f32015-08-03 15:35:19 -0400162 }
163
wcarthur74a92a42015-12-07 16:02:12 -0500164 if( *response_size < ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->responseSize )
165 {
Philip Triccadfa41a52016-07-20 17:43:57 -0700166 rval = TSS2_TCTI_RC_INSUFFICIENT_BUFFER;
Will-nuc8619aca2015-12-10 17:20:26 -0500167 *response_size = ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->responseSize;
wcarthur74a92a42015-12-07 16:02:12 -0500168 goto retLocalTpmReceive;
Philip Triccadfa41a52016-07-20 17:43:57 -0700169 }
wcarthur74a92a42015-12-07 16:02:12 -0500170
Will-nuc7f5680b2015-12-08 18:05:03 -0500171 *response_size = ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->responseSize;
wcarthur74a92a42015-12-07 16:02:12 -0500172
Will-nuc7f5680b2015-12-08 18:05:03 -0500173 for( i = 0; i < *response_size; i++ )
wcarthur74a92a42015-12-07 16:02:12 -0500174 {
Will-nuce0d3ec22015-12-09 15:09:49 -0500175 response_buffer[i] = ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->responseBuffer[i];
wcarthur74a92a42015-12-07 16:02:12 -0500176 }
Will-nuc7f5680b2015-12-08 18:05:03 -0500177
wcarthur74a92a42015-12-07 16:02:12 -0500178#ifdef DEBUG
wcarthur1c827592016-04-27 11:07:51 -0400179 if( ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext )->status.debugMsgEnabled == 1 &&
Will-nuce0d3ec22015-12-09 15:09:49 -0500180 ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->responseSize > 0 )
wcarthur74a92a42015-12-07 16:02:12 -0500181 {
wcarthur1c827592016-04-27 11:07:51 -0400182 TCTI_LOG( tctiContext, rmPrefix, "\n" );
183 TCTI_LOG( tctiContext, rmPrefix, "Response Received: " );
184 DEBUG_PRINT_BUFFER( rmPrefix, response_buffer, ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->responseSize );
wcarthur74a92a42015-12-07 16:02:12 -0500185 }
Will-nuc7f5680b2015-12-08 18:05:03 -0500186#endif
187
wcarthur74a92a42015-12-07 16:02:12 -0500188 ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.commandSent = 0;
Philip Triccadfa41a52016-07-20 17:43:57 -0700189
wcarthur74a92a42015-12-07 16:02:12 -0500190retLocalTpmReceive:
191
Philip Triccadfa41a52016-07-20 17:43:57 -0700192 if( rval == TSS2_RC_SUCCESS &&
wcarthur74a92a42015-12-07 16:02:12 -0500193 response_buffer != NULL )
wcarthureedecd62015-11-20 16:59:45 -0500194 {
195 ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->previousStage = TCTI_STAGE_RECEIVE_RESPONSE;
196 }
Philip Triccadfa41a52016-07-20 17:43:57 -0700197
Will Arthurca8e7f32015-08-03 15:35:19 -0400198 return rval;
199}
200
wcarthur35aa5392016-03-22 16:52:44 -0400201void LocalTpmFinalize(
Will Arthurca8e7f32015-08-03 15:35:19 -0400202 TSS2_TCTI_CONTEXT *tctiContext /* in */
203 )
204{
wcarthur35aa5392016-03-22 16:52:44 -0400205 if( tctiContext != NULL )
wcarthurf979e352015-11-24 17:34:44 -0500206 {
207 close( ( (TSS2_TCTI_CONTEXT_INTEL *)tctiContext )->devFile );
208 }
Will Arthurca8e7f32015-08-03 15:35:19 -0400209}
210
211TSS2_RC LocalTpmCancel(
212 TSS2_TCTI_CONTEXT *tctiContext
213 )
214{
215 TSS2_RC rval = TSS2_RC_SUCCESS;
216
217 // TBD. Needs support from device driver.
218
219 return rval;
220}
221
222TSS2_RC LocalTpmSetLocality(
223 TSS2_TCTI_CONTEXT *tctiContext, /* in */
224 uint8_t locality /* in */
225 )
226{
227 TSS2_RC rval = TSS2_RC_SUCCESS;
228
229 // TBD: how do I do this?
Philip Triccadfa41a52016-07-20 17:43:57 -0700230
Will Arthurca8e7f32015-08-03 15:35:19 -0400231 return rval;
232}
233
Philip Tricca0dca4f12016-02-14 19:32:29 -0800234TSS2_RC InitDeviceTcti (
Will Arthurca8e7f32015-08-03 15:35:19 -0400235 TSS2_TCTI_CONTEXT *tctiContext, // OUT
236 size_t *contextSize, // IN/OUT
Philip Triccadecc21c2016-03-30 13:45:18 -0700237 const TCTI_DEVICE_CONF *config // IN
Will Arthurca8e7f32015-08-03 15:35:19 -0400238 )
239{
240 TSS2_RC rval = TSS2_RC_SUCCESS;
Will Arthurca8e7f32015-08-03 15:35:19 -0400241
242 if( tctiContext == NULL )
243 {
244 *contextSize = sizeof( TSS2_TCTI_CONTEXT_INTEL );
245 return TSS2_RC_SUCCESS;
246 }
Philip Tricca3f7201d2016-02-29 17:15:29 -0800247 else if( config == NULL )
248 {
249 return TSS2_TCTI_RC_BAD_VALUE;
250 }
Will Arthurca8e7f32015-08-03 15:35:19 -0400251 else
252 {
Will Arthurca8e7f32015-08-03 15:35:19 -0400253 // Init TCTI context.
Philip Tricca7b4948e2016-04-18 20:43:59 -0700254 TSS2_TCTI_MAGIC( tctiContext ) = TCTI_MAGIC;
255 TSS2_TCTI_VERSION( tctiContext ) = TCTI_VERSION;
256 TSS2_TCTI_TRANSMIT( tctiContext ) = LocalTpmSendTpmCommand;
257 TSS2_TCTI_RECEIVE( tctiContext ) = LocalTpmReceiveTpmResponse;
258 TSS2_TCTI_FINALIZE( tctiContext ) = LocalTpmFinalize;
259 TSS2_TCTI_CANCEL( tctiContext ) = LocalTpmCancel;
260 TSS2_TCTI_GET_POLL_HANDLES( tctiContext ) = 0;
261 TSS2_TCTI_SET_LOCALITY( tctiContext ) = LocalTpmSetLocality;
Will Arthurca8e7f32015-08-03 15:35:19 -0400262 ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.locality = 3;
263 ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.commandSent = 0;
264 ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->status.rmDebugPrefix = 0;
265 ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->currentTctiContext = 0;
wcarthureedecd62015-11-20 16:59:45 -0500266 ((TSS2_TCTI_CONTEXT_INTEL *)tctiContext)->previousStage = TCTI_STAGE_INITIALIZE;
Philip Tricca068d5632016-03-11 12:23:40 -0800267 TCTI_LOG_CALLBACK( tctiContext ) = config->logCallback;
268 TCTI_LOG_DATA( tctiContext ) = config->logData;
269
Philip Tricca3f7201d2016-02-29 17:15:29 -0800270 ( ( (TSS2_TCTI_CONTEXT_INTEL *)tctiContext )->devFile ) = open( config->device_path, O_RDWR );
Philip Triccadfa41a52016-07-20 17:43:57 -0700271 if( ( (TSS2_TCTI_CONTEXT_INTEL *)tctiContext )->devFile < 0 )
Will Arthurca8e7f32015-08-03 15:35:19 -0400272 {
273 return( TSS2_TCTI_RC_IO_ERROR );
274 }
Will Arthurca8e7f32015-08-03 15:35:19 -0400275 }
276
277 return rval;
278}