2 Basic TIS (TPM Interface Specification) functions for Atmel I2C TPM.
4 Copyright (c) 2016, 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>
21 #include <Library/Tpm12CommandLib.h>
24 // Atmel I2C TPM slave address
26 #define ATMEL_I2C_TPM_SLAVE_ADDRESS 0x29
29 // Maximum I2C transfer size for Atmel I2C TPM
31 #define ATMEL_I2C_TPM_MAX_TRANSFER_SIZE 0x10
34 // Default TimeOut values in microseconds
36 #define TIS_TIMEOUT_A ( 750 * 1000) // 750ms
37 #define TIS_TIMEOUT_B (2000 * 1000) // 2s
38 #define TIS_TIMEOUT_C ( 750 * 1000) // 750ms
39 #define TIS_TIMEOUT_D ( 750 * 1000) // 750ms
42 Send command to Atmel I2c TPM breaking request up into multiple I2C transfers
45 @param[in] Buffer Pointer to TPM command data.
46 @param[in] Length Number of bytes of TPM command data.
48 @retval EFI_SUCCESS TPM command sent.
49 @retval EFI_NOT_FOUND TPM chip doesn't exit.
50 @retval EFI_TIMEOUT Can't get the TPM control in time.
53 WriteTpmBufferMultiple (
59 EFI_I2C_DEVICE_ADDRESS I2CDeviceAddr
;
63 I2CDeviceAddr
.I2CDeviceAddress
= ATMEL_I2C_TPM_SLAVE_ADDRESS
;
65 DEBUG ((EFI_D_VERBOSE
, "WriteTpmBufferMultiple: Addr=%02x Length=%02x\n", I2CDeviceAddr
.I2CDeviceAddress
, Length
));
67 for (PartialLength
= 0; Length
> 0; Length
-= PartialLength
, Buffer
+= PartialLength
) {
71 PartialLength
= MIN (Length
, ATMEL_I2C_TPM_MAX_TRANSFER_SIZE
);
72 Status
= I2cWriteMultipleByte (
74 EfiI2CSevenBitAddrMode
,
78 DEBUG ((EFI_D_VERBOSE
, " "));
79 for (Index
= 0; Index
< PartialLength
; Index
++) {
80 DEBUG ((EFI_D_VERBOSE
, "%02x ", Buffer
[Index
]));
82 DEBUG ((EFI_D_VERBOSE
, "\n"));
83 if (EFI_ERROR (Status
)) {
84 DEBUG ((EFI_D_VERBOSE
, " Status = %r\n", Status
));
89 DEBUG ((EFI_D_VERBOSE
, " Status = %r\n", Status
));
94 Receive a response to a command from Atmel I2c TPM breaking response into
95 multiple I2C transfers if required.
97 @param[out] Buffer Pointer to TPM response data.
98 @param[in] Length Maximum number of bytes to receive.
100 @retval EFI_SUCCESS TPM response received.
101 @retval EFI_NOT_FOUND TPM chip doesn't exit.
102 @retval EFI_TIMEOUT Can't get the TPM control in time.
105 ReadTpmBufferMultiple (
111 EFI_I2C_DEVICE_ADDRESS I2CDeviceAddr
;
116 I2CDeviceAddr
.I2CDeviceAddress
= ATMEL_I2C_TPM_SLAVE_ADDRESS
;
119 DEBUG ((EFI_D_VERBOSE
, "ReadTpmBufferMultiple: Addr=%02x Length=%02x\n", I2CDeviceAddr
.I2CDeviceAddress
, Length
));
121 for (PartialLength
= 0; Length
> 0; Length
-= PartialLength
, Buffer
+= PartialLength
) {
123 // Read data from TPM.
125 PartialLength
= MIN (Length
, ATMEL_I2C_TPM_MAX_TRANSFER_SIZE
);
126 Status
= I2cReadMultipleByte (
128 EfiI2CSevenBitAddrMode
,
133 if (!EFI_ERROR (Status
)) {
134 DEBUG ((EFI_D_VERBOSE
, " "));
135 for (Index
= 0; Index
< PartialLength
; Index
++) {
136 DEBUG ((EFI_D_VERBOSE
, "%02x ", Buffer
[Index
]));
138 DEBUG ((EFI_D_VERBOSE
, "\n"));
140 if (EFI_ERROR (Status
)) {
141 DEBUG ((EFI_D_VERBOSE
, " Status = %r\n", Status
));
146 DEBUG ((EFI_D_VERBOSE
, " Status = %r\n", Status
));
151 This service requests use TPM12.
153 @retval EFI_SUCCESS Get the control of TPM12 chip.
154 @retval EFI_NOT_FOUND TPM12 not found.
155 @retval EFI_DEVICE_ERROR Unexpected device behavior.
175 // Get the current timer value
177 Current
= GetPerformanceCounter();
180 // Initialize local variables
187 // Retrieve the performance counter properties and compute the number of
188 // performance counter ticks required to reach the maximum TIS timeout of
189 // TIS_TIMEOUT_A. TIS_TIMEOUT_A is in microseconds.
191 Timeout
= DivU64x32 (
193 GetPerformanceCounterProperties (&Start
, &End
),
205 // Attempt to read a byte from the Atmel I2C TPM
208 Status
= ReadTpmBufferMultiple (&Data
, sizeof(Data
));
211 Current
= GetPerformanceCounter();
212 Delta
= (INT64
) (Current
- Previous
);
220 if (Total
>= Timeout
) {
221 Status
= EFI_TIMEOUT
;
222 DEBUG ((EFI_D_ERROR
, "Atmel I2C TPM failed to read: %r\n", Status
));
225 } while (EFI_ERROR (Status
));
228 // Send Physical Presence Command to Atmel I2C TPM
230 Status
= Tpm12PhysicalPresence (TPM_PHYSICAL_PRESENCE_PRESENT
);
231 if (EFI_ERROR (Status
)) {
232 DEBUG ((EFI_D_ERROR
, "Atmel I2C TPM failed to submit physical presence command: %r\n", Status
));
240 This service enables the sending of commands to the TPM12.
242 @param[in] InputParameterBlockSize Size of the TPM12 input parameter block.
243 @param[in] InputParameterBlock Pointer to the TPM12 input parameter block.
244 @param[in,out] OutputParameterBlockSize Size of the TPM12 output parameter block.
245 @param[in] OutputParameterBlock Pointer to the TPM12 output parameter block.
247 @retval EFI_SUCCESS The command byte stream was successfully sent to
248 the device and a response was successfully received.
249 @retval EFI_DEVICE_ERROR The command was not successfully sent to the
250 device or a response was not successfully received
252 @retval EFI_BUFFER_TOO_SMALL The output parameter block is too small.
257 IN UINT32 InputParameterBlockSize
,
258 IN UINT8
*InputParameterBlock
,
259 IN OUT UINT32
*OutputParameterBlockSize
,
260 IN UINT8
*OutputParameterBlock
265 TPM_RSP_COMMAND_HDR
*ResponseHeader
;
276 // Make sure response buffer is big enough to hold a response header
278 if (*OutputParameterBlockSize
< sizeof (TPM_RSP_COMMAND_HDR
)) {
279 Status
= EFI_BUFFER_TOO_SMALL
;
284 // Get the current timer value
286 Current
= GetPerformanceCounter();
289 // Initialize local variables
296 // Retrieve the performance counter properties and compute the number of
297 // performance counter ticks required to reach the maximum TIS timeout of
298 // TIS_TIMEOUT_A. TIS_TIMEOUT_A is in microseconds.
300 Timeout
= DivU64x32 (
302 GetPerformanceCounterProperties (&Start
, &End
),
317 Status
= WriteTpmBufferMultiple (InputParameterBlock
, InputParameterBlockSize
);
320 Current
= GetPerformanceCounter();
321 Delta
= (INT64
) (Current
- Previous
);
329 if (Total
>= Timeout
) {
330 Status
= EFI_TIMEOUT
;
333 } while (EFI_ERROR (Status
));
336 // Receive response header
339 Status
= ReadTpmBufferMultiple (OutputParameterBlock
, sizeof (TPM_RSP_COMMAND_HDR
));
342 Current
= GetPerformanceCounter();
343 Delta
= (INT64
) (Current
- Previous
);
351 if (Total
>= Timeout
) {
352 Status
= EFI_TIMEOUT
;
355 } while (EFI_ERROR (Status
));
358 // Check the response data header (tag, parasize and returncode)
360 ResponseHeader
= (TPM_RSP_COMMAND_HDR
*)OutputParameterBlock
;
361 if (SwapBytes16 (ReadUnaligned16 (&ResponseHeader
->tag
)) != TPM_TAG_RSP_COMMAND
) {
362 Status
= EFI_DEVICE_ERROR
;
366 TpmOutSize
= SwapBytes32 (ReadUnaligned32 (&ResponseHeader
->paramSize
));
367 if (TpmOutSize
== sizeof (TPM_RSP_COMMAND_HDR
)) {
368 Status
= EFI_SUCCESS
;
371 if (TpmOutSize
< sizeof (TPM_RSP_COMMAND_HDR
)) {
372 Status
= EFI_DEVICE_ERROR
;
375 if (*OutputParameterBlockSize
< TpmOutSize
) {
376 Status
= EFI_BUFFER_TOO_SMALL
;
379 *OutputParameterBlockSize
= TpmOutSize
;
382 // Receive the remaining data in the response header
385 Status
= ReadTpmBufferMultiple (
386 OutputParameterBlock
+ sizeof (TPM_RSP_COMMAND_HDR
),
387 TpmOutSize
- sizeof (TPM_RSP_COMMAND_HDR
)
391 Current
= GetPerformanceCounter();
392 Delta
= (INT64
) (Current
- Previous
);
400 if (Total
>= Timeout
) {
401 Status
= EFI_TIMEOUT
;
404 } while (EFI_ERROR (Status
));
409 "Tpm12SubmitCommand() Status = %r Time = %ld ms\n",
412 MultU64x32 (Total
, 1000),
413 GetPerformanceCounterProperties (NULL
, NULL
),