2 Basic TIS (TPM Interface Specification) functions for Atmel I2C TPM.
4 Copyright (c) 2016 - 2017, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include <Library/Tpm12DeviceLib.h>
17 #include <Library/BaseLib.h>
18 #include <Library/TimerLib.h>
19 #include <Library/DebugLib.h>
20 #include <Library/I2cLib.h>
23 // Atmel I2C TPM slave address
25 #define ATMEL_I2C_TPM_SLAVE_ADDRESS 0x29
28 // Maximum I2C transfer size for Atmel I2C TPM
30 #define ATMEL_I2C_TPM_MAX_TRANSFER_SIZE 0x10
33 // Default TimeOut values in microseconds
35 #define TIS_TIMEOUT_A ( 750 * 1000) // 750ms
36 #define TIS_TIMEOUT_B (2000 * 1000) // 2s
37 #define TIS_TIMEOUT_C ( 750 * 1000) // 750ms
38 #define TIS_TIMEOUT_D ( 750 * 1000) // 750ms
41 Send command to Atmel I2c TPM breaking request up into multiple I2C transfers
44 @param[in] Buffer Pointer to TPM command data.
45 @param[in] Length Number of bytes of TPM command data.
47 @retval EFI_SUCCESS TPM command sent.
48 @retval EFI_NOT_FOUND TPM chip doesn't exit.
49 @retval EFI_TIMEOUT Can't get the TPM control in time.
52 WriteTpmBufferMultiple (
58 EFI_I2C_DEVICE_ADDRESS I2CDeviceAddr
;
64 I2CDeviceAddr
.I2CDeviceAddress
= ATMEL_I2C_TPM_SLAVE_ADDRESS
;
66 DEBUG ((EFI_D_VERBOSE
, "WriteTpmBufferMultiple: Addr=%02x Length=%02x\n", I2CDeviceAddr
.I2CDeviceAddress
, Length
));
68 for (PartialLength
= 0; Length
> 0; Length
-= PartialLength
, Buffer
+= PartialLength
) {
72 PartialLength
= MIN (Length
, ATMEL_I2C_TPM_MAX_TRANSFER_SIZE
);
73 Status
= I2cWriteMultipleByte (
75 EfiI2CSevenBitAddrMode
,
79 DEBUG ((EFI_D_VERBOSE
, " "));
80 for (Index
= 0; Index
< PartialLength
; Index
++) {
81 DEBUG ((EFI_D_VERBOSE
, "%02x ", Buffer
[Index
]));
83 DEBUG ((EFI_D_VERBOSE
, "\n"));
84 if (EFI_ERROR (Status
)) {
85 DEBUG ((EFI_D_VERBOSE
, " Status = %r\n", Status
));
90 DEBUG ((EFI_D_VERBOSE
, " Status = %r\n", Status
));
95 Receive a response to a command from Atmel I2c TPM breaking response into
96 multiple I2C transfers if required.
98 @param[out] Buffer Pointer to TPM response data.
99 @param[in] Length Maximum number of bytes to receive.
101 @retval EFI_SUCCESS TPM response received.
102 @retval EFI_NOT_FOUND TPM chip doesn't exit.
103 @retval EFI_TIMEOUT Can't get the TPM control in time.
106 ReadTpmBufferMultiple (
112 EFI_I2C_DEVICE_ADDRESS I2CDeviceAddr
;
117 Status
= EFI_SUCCESS
;
119 I2CDeviceAddr
.I2CDeviceAddress
= ATMEL_I2C_TPM_SLAVE_ADDRESS
;
122 DEBUG ((EFI_D_VERBOSE
, "ReadTpmBufferMultiple: Addr=%02x Length=%02x\n", I2CDeviceAddr
.I2CDeviceAddress
, Length
));
124 for (PartialLength
= 0; Length
> 0; Length
-= PartialLength
, Buffer
+= PartialLength
) {
126 // Read data from TPM.
128 PartialLength
= MIN (Length
, ATMEL_I2C_TPM_MAX_TRANSFER_SIZE
);
129 Status
= I2cReadMultipleByte (
131 EfiI2CSevenBitAddrMode
,
136 if (!EFI_ERROR (Status
)) {
137 DEBUG ((EFI_D_VERBOSE
, " "));
138 for (Index
= 0; Index
< PartialLength
; Index
++) {
139 DEBUG ((EFI_D_VERBOSE
, "%02x ", Buffer
[Index
]));
141 DEBUG ((EFI_D_VERBOSE
, "\n"));
143 if (EFI_ERROR (Status
)) {
144 DEBUG ((EFI_D_VERBOSE
, " Status = %r\n", Status
));
149 DEBUG ((EFI_D_VERBOSE
, " Status = %r\n", Status
));
154 This service requests use TPM12.
156 @retval EFI_SUCCESS Get the control of TPM12 chip.
157 @retval EFI_NOT_FOUND TPM12 not found.
158 @retval EFI_DEVICE_ERROR Unexpected device behavior.
178 // Get the current timer value
180 Current
= GetPerformanceCounter();
183 // Initialize local variables
190 // Retrieve the performance counter properties and compute the number of
191 // performance counter ticks required to reach the maximum TIS timeout of
192 // TIS_TIMEOUT_A. TIS_TIMEOUT_A is in microseconds.
194 Timeout
= DivU64x32 (
196 GetPerformanceCounterProperties (&Start
, &End
),
208 // Attempt to read a byte from the Atmel I2C TPM
211 Status
= ReadTpmBufferMultiple (&Data
, sizeof(Data
));
214 Current
= GetPerformanceCounter();
215 Delta
= (INT64
) (Current
- Previous
);
223 if (Total
>= Timeout
) {
224 Status
= EFI_TIMEOUT
;
225 DEBUG ((EFI_D_ERROR
, "Atmel I2C TPM failed to read: %r\n", Status
));
228 } while (EFI_ERROR (Status
));
234 This service enables the sending of commands to the TPM12.
236 @param[in] InputParameterBlockSize Size of the TPM12 input parameter block.
237 @param[in] InputParameterBlock Pointer to the TPM12 input parameter block.
238 @param[in,out] OutputParameterBlockSize Size of the TPM12 output parameter block.
239 @param[in] OutputParameterBlock Pointer to the TPM12 output parameter block.
241 @retval EFI_SUCCESS The command byte stream was successfully sent to
242 the device and a response was successfully received.
243 @retval EFI_DEVICE_ERROR The command was not successfully sent to the
244 device or a response was not successfully received
246 @retval EFI_BUFFER_TOO_SMALL The output parameter block is too small.
251 IN UINT32 InputParameterBlockSize
,
252 IN UINT8
*InputParameterBlock
,
253 IN OUT UINT32
*OutputParameterBlockSize
,
254 IN UINT8
*OutputParameterBlock
259 TPM_RSP_COMMAND_HDR
*ResponseHeader
;
270 // Initialize local variables
277 // Make sure response buffer is big enough to hold a response header
279 if (*OutputParameterBlockSize
< sizeof (TPM_RSP_COMMAND_HDR
)) {
280 Status
= EFI_BUFFER_TOO_SMALL
;
285 // Get the current timer value
287 Current
= GetPerformanceCounter();
290 // Retrieve the performance counter properties and compute the number of
291 // performance counter ticks required to reach the maximum TIS timeout of
292 // TIS_TIMEOUT_A. TIS_TIMEOUT_A is in microseconds.
294 Timeout
= DivU64x32 (
296 GetPerformanceCounterProperties (&Start
, &End
),
311 Status
= WriteTpmBufferMultiple (InputParameterBlock
, InputParameterBlockSize
);
314 Current
= GetPerformanceCounter();
315 Delta
= (INT64
) (Current
- Previous
);
323 if (Total
>= Timeout
) {
324 Status
= EFI_TIMEOUT
;
327 } while (EFI_ERROR (Status
));
330 // Receive response header
333 Status
= ReadTpmBufferMultiple (OutputParameterBlock
, sizeof (TPM_RSP_COMMAND_HDR
));
336 Current
= GetPerformanceCounter();
337 Delta
= (INT64
) (Current
- Previous
);
345 if (Total
>= Timeout
) {
346 Status
= EFI_TIMEOUT
;
349 } while (EFI_ERROR (Status
));
352 // Check the response data header (tag, parasize and returncode)
354 ResponseHeader
= (TPM_RSP_COMMAND_HDR
*)OutputParameterBlock
;
355 if (SwapBytes16 (ReadUnaligned16 (&ResponseHeader
->tag
)) != TPM_TAG_RSP_COMMAND
) {
356 Status
= EFI_DEVICE_ERROR
;
360 TpmOutSize
= SwapBytes32 (ReadUnaligned32 (&ResponseHeader
->paramSize
));
361 if (TpmOutSize
== sizeof (TPM_RSP_COMMAND_HDR
)) {
362 *OutputParameterBlockSize
= TpmOutSize
;
363 Status
= EFI_SUCCESS
;
366 if (TpmOutSize
< sizeof (TPM_RSP_COMMAND_HDR
)) {
367 Status
= EFI_DEVICE_ERROR
;
370 if (*OutputParameterBlockSize
< TpmOutSize
) {
371 Status
= EFI_BUFFER_TOO_SMALL
;
374 *OutputParameterBlockSize
= TpmOutSize
;
377 // Receive the remaining data in the response header
380 Status
= ReadTpmBufferMultiple (
381 OutputParameterBlock
+ sizeof (TPM_RSP_COMMAND_HDR
),
382 TpmOutSize
- sizeof (TPM_RSP_COMMAND_HDR
)
386 Current
= GetPerformanceCounter();
387 Delta
= (INT64
) (Current
- Previous
);
395 if (Total
>= Timeout
) {
396 Status
= EFI_TIMEOUT
;
399 } while (EFI_ERROR (Status
));
404 "Tpm12SubmitCommand() Status = %r Time = %ld ms\n",
407 MultU64x32 (Total
, 1000),
408 GetPerformanceCounterProperties (NULL
, NULL
),