2 Basic TIS (TPM Interface Specification) functions for Atmel I2C TPM.
4 Copyright (c) 2016 - 2017, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include <Library/Tpm12DeviceLib.h>
11 #include <Library/BaseLib.h>
12 #include <Library/TimerLib.h>
13 #include <Library/DebugLib.h>
14 #include <Library/I2cLib.h>
17 // Atmel I2C TPM slave address
19 #define ATMEL_I2C_TPM_SLAVE_ADDRESS 0x29
22 // Maximum I2C transfer size for Atmel I2C TPM
24 #define ATMEL_I2C_TPM_MAX_TRANSFER_SIZE 0x10
27 // Default TimeOut values in microseconds
29 #define TIS_TIMEOUT_A ( 750 * 1000) // 750ms
30 #define TIS_TIMEOUT_B (2000 * 1000) // 2s
31 #define TIS_TIMEOUT_C ( 750 * 1000) // 750ms
32 #define TIS_TIMEOUT_D ( 750 * 1000) // 750ms
35 Send command to Atmel I2c TPM breaking request up into multiple I2C transfers
38 @param[in] Buffer Pointer to TPM command data.
39 @param[in] Length Number of bytes of TPM command data.
41 @retval EFI_SUCCESS TPM command sent.
42 @retval EFI_NOT_FOUND TPM chip doesn't exit.
43 @retval EFI_TIMEOUT Can't get the TPM control in time.
46 WriteTpmBufferMultiple (
52 EFI_I2C_DEVICE_ADDRESS I2CDeviceAddr
;
58 I2CDeviceAddr
.I2CDeviceAddress
= ATMEL_I2C_TPM_SLAVE_ADDRESS
;
60 DEBUG ((EFI_D_VERBOSE
, "WriteTpmBufferMultiple: Addr=%02x Length=%02x\n", I2CDeviceAddr
.I2CDeviceAddress
, Length
));
62 for (PartialLength
= 0; Length
> 0; Length
-= PartialLength
, Buffer
+= PartialLength
) {
66 PartialLength
= MIN (Length
, ATMEL_I2C_TPM_MAX_TRANSFER_SIZE
);
67 Status
= I2cWriteMultipleByte (
69 EfiI2CSevenBitAddrMode
,
73 DEBUG ((EFI_D_VERBOSE
, " "));
74 for (Index
= 0; Index
< PartialLength
; Index
++) {
75 DEBUG ((EFI_D_VERBOSE
, "%02x ", Buffer
[Index
]));
77 DEBUG ((EFI_D_VERBOSE
, "\n"));
78 if (EFI_ERROR (Status
)) {
79 DEBUG ((EFI_D_VERBOSE
, " Status = %r\n", Status
));
84 DEBUG ((EFI_D_VERBOSE
, " Status = %r\n", Status
));
89 Receive a response to a command from Atmel I2c TPM breaking response into
90 multiple I2C transfers if required.
92 @param[out] Buffer Pointer to TPM response data.
93 @param[in] Length Maximum number of bytes to receive.
95 @retval EFI_SUCCESS TPM response received.
96 @retval EFI_NOT_FOUND TPM chip doesn't exit.
97 @retval EFI_TIMEOUT Can't get the TPM control in time.
100 ReadTpmBufferMultiple (
106 EFI_I2C_DEVICE_ADDRESS I2CDeviceAddr
;
111 Status
= EFI_SUCCESS
;
113 I2CDeviceAddr
.I2CDeviceAddress
= ATMEL_I2C_TPM_SLAVE_ADDRESS
;
116 DEBUG ((EFI_D_VERBOSE
, "ReadTpmBufferMultiple: Addr=%02x Length=%02x\n", I2CDeviceAddr
.I2CDeviceAddress
, Length
));
118 for (PartialLength
= 0; Length
> 0; Length
-= PartialLength
, Buffer
+= PartialLength
) {
120 // Read data from TPM.
122 PartialLength
= MIN (Length
, ATMEL_I2C_TPM_MAX_TRANSFER_SIZE
);
123 Status
= I2cReadMultipleByte (
125 EfiI2CSevenBitAddrMode
,
130 if (!EFI_ERROR (Status
)) {
131 DEBUG ((EFI_D_VERBOSE
, " "));
132 for (Index
= 0; Index
< PartialLength
; Index
++) {
133 DEBUG ((EFI_D_VERBOSE
, "%02x ", Buffer
[Index
]));
135 DEBUG ((EFI_D_VERBOSE
, "\n"));
137 if (EFI_ERROR (Status
)) {
138 DEBUG ((EFI_D_VERBOSE
, " Status = %r\n", Status
));
143 DEBUG ((EFI_D_VERBOSE
, " Status = %r\n", Status
));
148 This service requests use TPM12.
150 @retval EFI_SUCCESS Get the control of TPM12 chip.
151 @retval EFI_NOT_FOUND TPM12 not found.
152 @retval EFI_DEVICE_ERROR Unexpected device behavior.
172 // Get the current timer value
174 Current
= GetPerformanceCounter();
177 // Initialize local variables
184 // Retrieve the performance counter properties and compute the number of
185 // performance counter ticks required to reach the maximum TIS timeout of
186 // TIS_TIMEOUT_A. TIS_TIMEOUT_A is in microseconds.
188 Timeout
= DivU64x32 (
190 GetPerformanceCounterProperties (&Start
, &End
),
202 // Attempt to read a byte from the Atmel I2C TPM
205 Status
= ReadTpmBufferMultiple (&Data
, sizeof(Data
));
208 Current
= GetPerformanceCounter();
209 Delta
= (INT64
) (Current
- Previous
);
217 if (Total
>= Timeout
) {
218 Status
= EFI_TIMEOUT
;
219 DEBUG ((EFI_D_ERROR
, "Atmel I2C TPM failed to read: %r\n", Status
));
222 } while (EFI_ERROR (Status
));
228 This service enables the sending of commands to the TPM12.
230 @param[in] InputParameterBlockSize Size of the TPM12 input parameter block.
231 @param[in] InputParameterBlock Pointer to the TPM12 input parameter block.
232 @param[in,out] OutputParameterBlockSize Size of the TPM12 output parameter block.
233 @param[in] OutputParameterBlock Pointer to the TPM12 output parameter block.
235 @retval EFI_SUCCESS The command byte stream was successfully sent to
236 the device and a response was successfully received.
237 @retval EFI_DEVICE_ERROR The command was not successfully sent to the
238 device or a response was not successfully received
240 @retval EFI_BUFFER_TOO_SMALL The output parameter block is too small.
245 IN UINT32 InputParameterBlockSize
,
246 IN UINT8
*InputParameterBlock
,
247 IN OUT UINT32
*OutputParameterBlockSize
,
248 IN UINT8
*OutputParameterBlock
253 TPM_RSP_COMMAND_HDR
*ResponseHeader
;
264 // Initialize local variables
271 // Make sure response buffer is big enough to hold a response header
273 if (*OutputParameterBlockSize
< sizeof (TPM_RSP_COMMAND_HDR
)) {
274 Status
= EFI_BUFFER_TOO_SMALL
;
279 // Get the current timer value
281 Current
= GetPerformanceCounter();
284 // Retrieve the performance counter properties and compute the number of
285 // performance counter ticks required to reach the maximum TIS timeout of
286 // TIS_TIMEOUT_A. TIS_TIMEOUT_A is in microseconds.
288 Timeout
= DivU64x32 (
290 GetPerformanceCounterProperties (&Start
, &End
),
305 Status
= WriteTpmBufferMultiple (InputParameterBlock
, InputParameterBlockSize
);
308 Current
= GetPerformanceCounter();
309 Delta
= (INT64
) (Current
- Previous
);
317 if (Total
>= Timeout
) {
318 Status
= EFI_TIMEOUT
;
321 } while (EFI_ERROR (Status
));
324 // Receive response header
327 Status
= ReadTpmBufferMultiple (OutputParameterBlock
, sizeof (TPM_RSP_COMMAND_HDR
));
330 Current
= GetPerformanceCounter();
331 Delta
= (INT64
) (Current
- Previous
);
339 if (Total
>= Timeout
) {
340 Status
= EFI_TIMEOUT
;
343 } while (EFI_ERROR (Status
));
346 // Check the response data header (tag, parasize and returncode)
348 ResponseHeader
= (TPM_RSP_COMMAND_HDR
*)OutputParameterBlock
;
349 if (SwapBytes16 (ReadUnaligned16 (&ResponseHeader
->tag
)) != TPM_TAG_RSP_COMMAND
) {
350 Status
= EFI_DEVICE_ERROR
;
354 TpmOutSize
= SwapBytes32 (ReadUnaligned32 (&ResponseHeader
->paramSize
));
355 if (TpmOutSize
== sizeof (TPM_RSP_COMMAND_HDR
)) {
356 *OutputParameterBlockSize
= TpmOutSize
;
357 Status
= EFI_SUCCESS
;
360 if (TpmOutSize
< sizeof (TPM_RSP_COMMAND_HDR
)) {
361 Status
= EFI_DEVICE_ERROR
;
364 if (*OutputParameterBlockSize
< TpmOutSize
) {
365 Status
= EFI_BUFFER_TOO_SMALL
;
368 *OutputParameterBlockSize
= TpmOutSize
;
371 // Receive the remaining data in the response header
374 Status
= ReadTpmBufferMultiple (
375 OutputParameterBlock
+ sizeof (TPM_RSP_COMMAND_HDR
),
376 TpmOutSize
- sizeof (TPM_RSP_COMMAND_HDR
)
380 Current
= GetPerformanceCounter();
381 Delta
= (INT64
) (Current
- Previous
);
389 if (Total
>= Timeout
) {
390 Status
= EFI_TIMEOUT
;
393 } while (EFI_ERROR (Status
));
398 "Tpm12SubmitCommand() Status = %r Time = %ld ms\n",
401 MultU64x32 (Total
, 1000),
402 GetPerformanceCounterProperties (NULL
, NULL
),