blob: bd17f49e5d894f98606ae0298a16a9dd7f0f2287 [file] [log] [blame]
Philip Tricca21fa3212018-02-19 06:43:56 -08001/*
2 * Copyright (c) 2015 - 2018 Intel Corporation
Philip Triccaa003d762017-10-17 13:25:42 -07003 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
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
25 * THE POSSIBILITY OF SUCH DAMAGE.
Philip Tricca21fa3212018-02-19 06:43:56 -080026 */
wcarthureedecd62015-11-20 16:59:45 -050027
Philip Triccaa003ae62018-02-19 10:32:46 -080028#include <inttypes.h>
wcarthureedecd62015-11-20 16:59:45 -050029#include <stdio.h>
Philip Triccaa003d762017-10-17 13:25:42 -070030#include <stdlib.h>
Philip Triccaa003ae62018-02-19 10:32:46 -080031#include <unistd.h>
wcarthureedecd62015-11-20 16:59:45 -050032
Kristen Carlson Accardi52a83ba2018-01-23 14:38:24 -080033#include "tcti/common.h"
Philip Triccadd3b03c2017-03-05 11:38:08 -080034#include "sapi/tpm20.h"
Philip Tricca3bc96032017-10-10 16:26:09 -070035#include "tcti.h"
Philip Triccaa003ae62018-02-19 10:32:46 -080036#define LOGMODULE tcti
37#include "log/log.h"
wcarthureedecd62015-11-20 16:59:45 -050038
Philip Tricca3f1828c2017-10-18 18:44:13 -070039TSS2_RC tcti_common_checks (
40 TSS2_TCTI_CONTEXT *tcti_context
wcarthureedecd62015-11-20 16:59:45 -050041 )
42{
Philip Tricca3f1828c2017-10-18 18:44:13 -070043 TSS2_TCTI_CONTEXT_INTEL *tcti_intel;
Philip Tricca24f555c2017-10-13 17:51:53 -070044
Philip Tricca3f1828c2017-10-18 18:44:13 -070045 tcti_intel = tcti_context_intel_cast (tcti_context);
46 if (tcti_context == NULL) {
Tadeusz Struk32aadee2017-06-14 13:23:42 -070047 return TSS2_TCTI_RC_BAD_REFERENCE;
Philip Triccadfa41a52016-07-20 17:43:57 -070048 }
Philip Tricca24f555c2017-10-13 17:51:53 -070049 if (tcti_intel->magic != TCTI_MAGIC ||
Philip Triccaa003d762017-10-17 13:25:42 -070050 tcti_intel->version != TCTI_VERSION) {
Tadeusz Struk32aadee2017-06-14 13:23:42 -070051 return TSS2_TCTI_RC_BAD_CONTEXT;
wcarthur1ac13972015-11-20 18:10:04 -050052 }
Philip Triccadfa41a52016-07-20 17:43:57 -070053
Tadeusz Struk32aadee2017-06-14 13:23:42 -070054 return TSS2_RC_SUCCESS;
wcarthureedecd62015-11-20 16:59:45 -050055}
56
Philip Tricca3f1828c2017-10-18 18:44:13 -070057TSS2_RC tcti_send_checks (
58 TSS2_TCTI_CONTEXT *tctiContext,
Philip Triccaddf42b22018-02-21 15:37:23 -080059 const uint8_t *command_buffer
Philip Tricca3f1828c2017-10-18 18:44:13 -070060 )
61{
62 TSS2_TCTI_CONTEXT_INTEL *tcti_intel;
63 TSS2_RC rc;
64
65 tcti_intel = tcti_context_intel_cast (tctiContext);;
66 rc = tcti_common_checks (tctiContext);
67 if (rc != TSS2_RC_SUCCESS) {
68 return rc;
69 }
70 if (command_buffer == NULL) {
71 return TSS2_TCTI_RC_BAD_REFERENCE;
72 }
73 if (tcti_intel->previousStage == TCTI_STAGE_SEND_COMMAND) {
74 return TSS2_TCTI_RC_BAD_SEQUENCE;
75 }
76
77 return TSS2_RC_SUCCESS;
78}
wcarthureedecd62015-11-20 16:59:45 -050079
Philip Triccaa003d762017-10-17 13:25:42 -070080TSS2_RC tcti_receive_checks (
81 TSS2_TCTI_CONTEXT *tctiContext,
82 size_t *response_size,
83 unsigned char *response_buffer
wcarthureedecd62015-11-20 16:59:45 -050084 )
85{
Philip Tricca3f1828c2017-10-18 18:44:13 -070086 TSS2_TCTI_CONTEXT_INTEL *tcti_intel;
87 TSS2_RC rc;
Philip Tricca24f555c2017-10-13 17:51:53 -070088
Philip Tricca3f1828c2017-10-18 18:44:13 -070089 tcti_intel = tcti_context_intel_cast (tctiContext);
90 rc = tcti_common_checks (tctiContext);
91 if (rc != TSS2_RC_SUCCESS) {
92 return rc;
93 }
94 if (response_buffer == NULL || response_size == NULL) {
Tadeusz Struk32aadee2017-06-14 13:23:42 -070095 return TSS2_TCTI_RC_BAD_REFERENCE;
Philip Triccadfa41a52016-07-20 17:43:57 -070096 }
Philip Triccaa003d762017-10-17 13:25:42 -070097 if (tcti_intel->previousStage == TCTI_STAGE_RECEIVE_RESPONSE) {
Tadeusz Struk32aadee2017-06-14 13:23:42 -070098 return TSS2_TCTI_RC_BAD_SEQUENCE;
wcarthureedecd62015-11-20 16:59:45 -050099 }
Philip Triccadfa41a52016-07-20 17:43:57 -0700100
Tadeusz Struk32aadee2017-06-14 13:23:42 -0700101 return TSS2_RC_SUCCESS;
Philip Tricca4ea417c2016-01-24 23:10:03 +0000102}
Philip Triccaa003ae62018-02-19 10:32:46 -0800103
104ssize_t write_all (
105 int fd,
106 const uint8_t *buf,
107 size_t size)
108{
109 ssize_t written = 0;
110 size_t written_total = 0;
111
112 do {
113 LOG_DEBUG("writing %zu bytes starting at 0x%" PRIxPTR " to fd %d",
114 size - written_total,
115 (uintptr_t)buf + written_total,
116 fd);
117 written = TEMP_RETRY (write (fd,
118 (const char*)&buf [written_total],
119 size - written_total));
120 if (written >= 0) {
121 LOG_DEBUG ("wrote %zd bytes to fd %d", written, fd);
122 written_total += (size_t)written;
123 } else {
124 LOG_ERROR ("failed to write to fd %d: %s", fd, strerror (errno));
125 return written_total;
126 }
127 } while (written_total < size);
128
129 return (ssize_t)written_total;
130}
Philip Triccaee411152018-02-23 19:16:04 -0800131
132TSS2_RC tcti_make_sticky_not_implemented (
133 TSS2_TCTI_CONTEXT *tctiContext,
134 TPM2_HANDLE *handle,
135 uint8_t sticky)
136{
137 return TSS2_TCTI_RC_NOT_IMPLEMENTED;
138}