-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtis.c
226 lines (170 loc) · 4.31 KB
/
tis.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
* Copyright (c) 2019 Apertus Solutions, LLC
*
* Author(s):
* Daniel P. Smith <dpsmith@apertussolutions.com>
*
* The code in this file is based on the article "Writing a TPM Device Driver"
* published on http://ptgmedia.pearsoncmg.com.
*
*/
#ifdef LINUX_KERNEL
#include <linux/types.h>
#include <asm/byteorder.h>
#elif defined LINUX_USERSPACE
#include <endian.h>
#define be32_to_cpu be32toh
#define be16_to_cpu be16toh
#endif
#include "tpm.h"
#include "tpmbuff.h"
#include "tpm_common.h"
#include "tis.h"
#define TPM_BURST_MIN_DELAY 100 /* 100us */
static u8 locality = TPM_NO_LOCALITY;
static u32 burst_wait(void)
{
u32 count = 0;
while (count == 0) {
count = tpm_read8(STS(locality) + 1);
count += tpm_read8(STS(locality) + 2) << 8;
/* Wait for FIFO to drain */
if (count == 0)
tpm_udelay(TPM_BURST_MIN_DELAY);
}
return count;
}
void tis_relinquish_locality(void)
{
if (locality < TPM_MAX_LOCALITY)
tpm_write8(ACCESS_RELINQUISH_LOCALITY, ACCESS(locality));
locality = TPM_NO_LOCALITY;
}
u8 tis_request_locality(u8 l)
{
if (l > TPM_MAX_LOCALITY)
return TPM_NO_LOCALITY;
if (l == locality)
return locality;
tis_relinquish_locality();
tpm_write8(ACCESS_REQUEST_USE, ACCESS(l));
/* wait for locality to be granted */
if (tpm_read8(ACCESS(l)) & ACCESS_ACTIVE_LOCALITY)
locality = l;
return locality;
}
size_t tis_send(struct tpmbuff *buf)
{
u8 status, *buf_ptr;
u32 burstcnt = 0;
u32 count = 0;
if (locality > TPM_MAX_LOCALITY)
return 0;
for (status = 0; (status & STS_COMMAND_READY) == 0; ) {
tpm_write8(STS_COMMAND_READY, STS(locality));
status = tpm_read8(STS(locality));
}
buf_ptr = buf->head;
/* send all but the last byte */
while (count < (buf->len - 1)) {
burstcnt = burst_wait();
for (; burstcnt > 0 && count < (buf->len - 1); burstcnt--) {
tpm_write8(buf_ptr[count], DATA_FIFO(locality));
count++;
}
/* check for overflow */
for (status = 0; (status & STS_VALID) == 0; )
status = tpm_read8(STS(locality));
if ((status & STS_DATA_EXPECT) == 0)
return 0;
}
/* write last byte */
tpm_write8(buf_ptr[buf->len - 1], DATA_FIFO(locality));
count++;
/* make sure it stuck */
for (status = 0; (status & STS_VALID) == 0; )
status = tpm_read8(STS(locality));
if ((status & STS_DATA_EXPECT) != 0)
return 0;
/* go and do it */
tpm_write8(STS_GO, STS(locality));
return (size_t)count;
}
static size_t recv_data(unsigned char *buf, size_t len)
{
size_t size = 0;
u8 *bufptr;
u32 burstcnt = 0;
bufptr = (u8 *)buf;
while (tis_data_available(locality) && size < len) {
burstcnt = burst_wait();
for (; burstcnt > 0 && size < len; burstcnt--) {
*bufptr = tpm_read8(DATA_FIFO(locality));
bufptr++;
size++;
}
}
return size;
}
size_t tis_recv(enum tpm_family f, struct tpmbuff *buf)
{
u32 expected;
u8 *buf_ptr;
struct tpm_header *hdr;
if (locality > TPM_MAX_LOCALITY)
return 0;
/* ensure that there is data available */
if (!tis_data_available(locality)) {
if (f == TPM12)
tpm1_timeout_d();
else
tpm2_timeout_d();
if (!tis_data_available(locality))
return 0;
}
/* read header */
hdr = (struct tpm_header *)buf->head;
expected = sizeof(struct tpm_header);
if (recv_data(buf->head, expected) < expected)
return 0;
/* convert header */
hdr->tag = be16_to_cpu(hdr->tag);
hdr->size = be32_to_cpu(hdr->size);
hdr->code = be32_to_cpu(hdr->code);
/* protect against integer underflow */
if (hdr->size <= expected)
return 0;
/* hdr->size = header + data */
expected = hdr->size - expected;
buf_ptr = tpmb_put(buf, expected);
if (!buf_ptr)
return 0;
/* read all data, except last byte */
if (recv_data(buf_ptr, expected - 1) < (expected - 1))
return 0;
/* check for receive underflow */
if (!tis_data_available(locality))
return 0;
/* read last byte */
if (recv_data(buf_ptr, 1) != 1)
return 0;
/* make sure we read everything */
if (tis_data_available(locality))
return 0;
tpm_write8(STS_COMMAND_READY, STS(locality));
return hdr->size;
}
u8 tis_init(struct tpm *t)
{
locality = TPM_NO_LOCALITY;
if (tis_request_locality(0) != 0)
return 0;
t->vendor = tpm_read32(DID_VID(0));
if ((t->vendor & 0xFFFF) == 0xFFFF)
return 0;
t->ops.request_locality = tis_request_locality;
t->ops.relinquish_locality = tis_relinquish_locality;
t->ops.send = tis_send;
t->ops.recv = tis_recv;
return 1;
}