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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
|
/** @file
Basic TIS (TPM Interface Specification) functions for Atmel I2C TPM.
Copyright (c) 2016, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
#include <PiPei.h>
#include <Library/Tpm12DeviceLib.h>
#include <Library/BaseLib.h>
#include <Library/TimerLib.h>
#include <Library/DebugLib.h>
#include <Library/I2cLib.h>
#include <Library/Tpm12CommandLib.h>
//
// Atmel I2C TPM slave address
//
#define ATMEL_I2C_TPM_SLAVE_ADDRESS 0x29
//
// Maximum I2C transfer size for Atmel I2C TPM
//
#define ATMEL_I2C_TPM_MAX_TRANSFER_SIZE 0x10
//
// Default TimeOut values in microseconds
//
#define TIS_TIMEOUT_A ( 750 * 1000) // 750ms
#define TIS_TIMEOUT_B (2000 * 1000) // 2s
#define TIS_TIMEOUT_C ( 750 * 1000) // 750ms
#define TIS_TIMEOUT_D ( 750 * 1000) // 750ms
/**
Send command to Atmel I2c TPM breaking request up into multiple I2C transfers
if required.
@param[in] Buffer Pointer to TPM command data.
@param[in] Length Number of bytes of TPM command data.
@retval EFI_SUCCESS TPM command sent.
@retval EFI_NOT_FOUND TPM chip doesn't exit.
@retval EFI_TIMEOUT Can't get the TPM control in time.
**/
EFI_STATUS
WriteTpmBufferMultiple (
IN UINT8 *Buffer,
IN UINTN Length
)
{
EFI_STATUS Status;
EFI_I2C_DEVICE_ADDRESS I2CDeviceAddr;
UINTN Index;
UINTN PartialLength;
I2CDeviceAddr.I2CDeviceAddress = ATMEL_I2C_TPM_SLAVE_ADDRESS;
DEBUG ((EFI_D_VERBOSE, "WriteTpmBufferMultiple: Addr=%02x Length=%02x\n", I2CDeviceAddr.I2CDeviceAddress, Length));
for (PartialLength = 0; Length > 0; Length -= PartialLength, Buffer += PartialLength) {
//
// Write data to TPM.
//
PartialLength = MIN (Length, ATMEL_I2C_TPM_MAX_TRANSFER_SIZE);
Status = I2cWriteMultipleByte (
I2CDeviceAddr,
EfiI2CSevenBitAddrMode,
&PartialLength,
Buffer
);
DEBUG ((EFI_D_VERBOSE, " "));
for (Index = 0; Index < PartialLength; Index++) {
DEBUG ((EFI_D_VERBOSE, "%02x ", Buffer[Index]));
}
DEBUG ((EFI_D_VERBOSE, "\n"));
if (EFI_ERROR (Status)) {
DEBUG ((EFI_D_VERBOSE, " Status = %r\n", Status));
return Status;
}
}
DEBUG ((EFI_D_VERBOSE, " Status = %r\n", Status));
return Status;
}
/**
Receive a response to a command from Atmel I2c TPM breaking response into
multiple I2C transfers if required.
@param[out] Buffer Pointer to TPM response data.
@param[in] Length Maximum number of bytes to receive.
@retval EFI_SUCCESS TPM response received.
@retval EFI_NOT_FOUND TPM chip doesn't exit.
@retval EFI_TIMEOUT Can't get the TPM control in time.
**/
EFI_STATUS
ReadTpmBufferMultiple (
OUT UINT8 *Buffer,
IN UINTN Length
)
{
EFI_STATUS Status;
EFI_I2C_DEVICE_ADDRESS I2CDeviceAddr;
UINTN WriteLength;
UINTN Index;
UINTN PartialLength;
I2CDeviceAddr.I2CDeviceAddress = ATMEL_I2C_TPM_SLAVE_ADDRESS;
WriteLength = 0;
DEBUG ((EFI_D_VERBOSE, "ReadTpmBufferMultiple: Addr=%02x Length=%02x\n", I2CDeviceAddr.I2CDeviceAddress, Length));
for (PartialLength = 0; Length > 0; Length -= PartialLength, Buffer += PartialLength) {
//
// Read data from TPM.
//
PartialLength = MIN (Length, ATMEL_I2C_TPM_MAX_TRANSFER_SIZE);
Status = I2cReadMultipleByte (
I2CDeviceAddr,
EfiI2CSevenBitAddrMode,
&WriteLength,
&PartialLength,
Buffer
);
if (!EFI_ERROR (Status)) {
DEBUG ((EFI_D_VERBOSE, " "));
for (Index = 0; Index < PartialLength; Index++) {
DEBUG ((EFI_D_VERBOSE, "%02x ", Buffer[Index]));
}
DEBUG ((EFI_D_VERBOSE, "\n"));
}
if (EFI_ERROR (Status)) {
DEBUG ((EFI_D_VERBOSE, " Status = %r\n", Status));
return Status;
}
}
DEBUG ((EFI_D_VERBOSE, " Status = %r\n", Status));
return Status;
}
/**
This service requests use TPM12.
@retval EFI_SUCCESS Get the control of TPM12 chip.
@retval EFI_NOT_FOUND TPM12 not found.
@retval EFI_DEVICE_ERROR Unexpected device behavior.
**/
EFI_STATUS
EFIAPI
Tpm12RequestUseTpm (
VOID
)
{
EFI_STATUS Status;
UINT8 Data;
UINT64 Current;
UINT64 Previous;
UINT64 Total;
UINT64 Start;
UINT64 End;
UINT64 Timeout;
INT64 Cycle;
INT64 Delta;
//
// Get the current timer value
//
Current = GetPerformanceCounter();
//
// Initialize local variables
//
Start = 0;
End = 0;
Total = 0;
//
// Retrieve the performance counter properties and compute the number of
// performance counter ticks required to reach the maximum TIS timeout of
// TIS_TIMEOUT_A. TIS_TIMEOUT_A is in microseconds.
//
Timeout = DivU64x32 (
MultU64x32 (
GetPerformanceCounterProperties (&Start, &End),
TIS_TIMEOUT_A
),
1000000
);
Cycle = End - Start;
if (Cycle < 0) {
Cycle = -Cycle;
}
Cycle++;
//
// Attempt to read a byte from the Atmel I2C TPM
//
do {
Status = ReadTpmBufferMultiple (&Data, sizeof(Data));
Previous = Current;
Current = GetPerformanceCounter();
Delta = (INT64) (Current - Previous);
if (Start > End) {
Delta = -Delta;
}
if (Delta < 0) {
Delta += Cycle;
}
Total += Delta;
if (Total >= Timeout) {
Status = EFI_TIMEOUT;
DEBUG ((EFI_D_ERROR, "Atmel I2C TPM failed to read: %r\n", Status));
return Status;
}
} while (EFI_ERROR (Status));
//
// Send Physical Presence Command to Atmel I2C TPM
//
Status = Tpm12PhysicalPresence (TPM_PHYSICAL_PRESENCE_PRESENT);
if (EFI_ERROR (Status)) {
DEBUG ((EFI_D_ERROR, "Atmel I2C TPM failed to submit physical presence command: %r\n", Status));
return Status;
}
return EFI_SUCCESS;
}
/**
This service enables the sending of commands to the TPM12.
@param[in] InputParameterBlockSize Size of the TPM12 input parameter block.
@param[in] InputParameterBlock Pointer to the TPM12 input parameter block.
@param[in,out] OutputParameterBlockSize Size of the TPM12 output parameter block.
@param[in] OutputParameterBlock Pointer to the TPM12 output parameter block.
@retval EFI_SUCCESS The command byte stream was successfully sent to
the device and a response was successfully received.
@retval EFI_DEVICE_ERROR The command was not successfully sent to the
device or a response was not successfully received
from the device.
@retval EFI_BUFFER_TOO_SMALL The output parameter block is too small.
**/
EFI_STATUS
EFIAPI
Tpm12SubmitCommand (
IN UINT32 InputParameterBlockSize,
IN UINT8 *InputParameterBlock,
IN OUT UINT32 *OutputParameterBlockSize,
IN UINT8 *OutputParameterBlock
)
{
EFI_STATUS Status;
UINT32 TpmOutSize;
TPM_RSP_COMMAND_HDR *ResponseHeader;
UINT64 Current;
UINT64 Previous;
UINT64 Total;
UINT64 Start;
UINT64 End;
UINT64 Timeout;
INT64 Cycle;
INT64 Delta;
//
// Make sure response buffer is big enough to hold a response header
//
if (*OutputParameterBlockSize < sizeof (TPM_RSP_COMMAND_HDR)) {
Status = EFI_BUFFER_TOO_SMALL;
goto Done;
}
//
// Get the current timer value
//
Current = GetPerformanceCounter();
//
// Initialize local variables
//
Start = 0;
End = 0;
Total = 0;
//
// Retrieve the performance counter properties and compute the number of
// performance counter ticks required to reach the maximum TIS timeout of
// TIS_TIMEOUT_A. TIS_TIMEOUT_A is in microseconds.
//
Timeout = DivU64x32 (
MultU64x32 (
GetPerformanceCounterProperties (&Start, &End),
TIS_TIMEOUT_A
),
1000000
);
Cycle = End - Start;
if (Cycle < 0) {
Cycle = -Cycle;
}
Cycle++;
//
// Send command
//
do {
Status = WriteTpmBufferMultiple (InputParameterBlock, InputParameterBlockSize);
Previous = Current;
Current = GetPerformanceCounter();
Delta = (INT64) (Current - Previous);
if (Start > End) {
Delta = -Delta;
}
if (Delta < 0) {
Delta += Cycle;
}
Total += Delta;
if (Total >= Timeout) {
Status = EFI_TIMEOUT;
goto Done;
}
} while (EFI_ERROR (Status));
//
// Receive response header
//
do {
Status = ReadTpmBufferMultiple (OutputParameterBlock, sizeof (TPM_RSP_COMMAND_HDR));
Previous = Current;
Current = GetPerformanceCounter();
Delta = (INT64) (Current - Previous);
if (Start > End) {
Delta = -Delta;
}
if (Delta < 0) {
Delta += Cycle;
}
Total += Delta;
if (Total >= Timeout) {
Status = EFI_TIMEOUT;
goto Done;
}
} while (EFI_ERROR (Status));
//
// Check the response data header (tag, parasize and returncode)
//
ResponseHeader = (TPM_RSP_COMMAND_HDR *)OutputParameterBlock;
if (SwapBytes16 (ReadUnaligned16 (&ResponseHeader->tag)) != TPM_TAG_RSP_COMMAND) {
Status = EFI_DEVICE_ERROR;
goto Done;
}
TpmOutSize = SwapBytes32 (ReadUnaligned32 (&ResponseHeader->paramSize));
if (TpmOutSize == sizeof (TPM_RSP_COMMAND_HDR)) {
Status = EFI_SUCCESS;
goto Done;
}
if (TpmOutSize < sizeof (TPM_RSP_COMMAND_HDR)) {
Status = EFI_DEVICE_ERROR;
goto Done;
}
if (*OutputParameterBlockSize < TpmOutSize) {
Status = EFI_BUFFER_TOO_SMALL;
goto Done;
}
*OutputParameterBlockSize = TpmOutSize;
//
// Receive the remaining data in the response header
//
do {
Status = ReadTpmBufferMultiple (
OutputParameterBlock + sizeof (TPM_RSP_COMMAND_HDR),
TpmOutSize - sizeof (TPM_RSP_COMMAND_HDR)
);
Previous = Current;
Current = GetPerformanceCounter();
Delta = (INT64) (Current - Previous);
if (Start > End) {
Delta = -Delta;
}
if (Delta < 0) {
Delta += Cycle;
}
Total += Delta;
if (Total >= Timeout) {
Status = EFI_TIMEOUT;
goto Done;
}
} while (EFI_ERROR (Status));
Done:
DEBUG ((
EFI_D_VERBOSE,
"Tpm12SubmitCommand() Status = %r Time = %ld ms\n",
Status,
DivU64x64Remainder (
MultU64x32 (Total, 1000),
GetPerformanceCounterProperties (NULL, NULL),
NULL
)
));
return Status;
}
|