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
;
62 I2CDeviceAddr
.I2CDeviceAddress
= ATMEL_I2C_TPM_SLAVE_ADDRESS
;
64 DEBUG ((EFI_D_VERBOSE
, "WriteTpmBufferMultiple: Addr=%02x Length=%02x\n", I2CDeviceAddr
.I2CDeviceAddress
, Length
));
66 for (PartialLength
= 0; Length
> 0; Length
-= PartialLength
, Buffer
+= PartialLength
) {
70 PartialLength
= MIN (Length
, ATMEL_I2C_TPM_MAX_TRANSFER_SIZE
);
71 Status
= I2cWriteMultipleByte (
73 EfiI2CSevenBitAddrMode
,
77 DEBUG ((EFI_D_VERBOSE
, " "));
78 for (Index
= 0; Index
< PartialLength
; Index
++) {
79 DEBUG ((EFI_D_VERBOSE
, "%02x ", Buffer
[Index
]));
81 DEBUG ((EFI_D_VERBOSE
, "\n"));
82 if (EFI_ERROR (Status
)) {
83 DEBUG ((EFI_D_VERBOSE
, " Status = %r\n", Status
));
88 DEBUG ((EFI_D_VERBOSE
, " Status = %r\n", Status
));
93 Receive a response to a command from Atmel I2c TPM breaking response into
94 multiple I2C transfers if required.
96 @param[out] Buffer Pointer to TPM response data.
97 @param[in] Length Maximum number of bytes to receive.
99 @retval EFI_SUCCESS TPM response received.
100 @retval EFI_NOT_FOUND TPM chip doesn't exit.
101 @retval EFI_TIMEOUT Can't get the TPM control in time.
104 ReadTpmBufferMultiple (
110 EFI_I2C_DEVICE_ADDRESS I2CDeviceAddr
;
115 I2CDeviceAddr
.I2CDeviceAddress
= ATMEL_I2C_TPM_SLAVE_ADDRESS
;
118 DEBUG ((EFI_D_VERBOSE
, "ReadTpmBufferMultiple: Addr=%02x Length=%02x\n", I2CDeviceAddr
.I2CDeviceAddress
, Length
));
120 for (PartialLength
= 0; Length
> 0; Length
-= PartialLength
, Buffer
+= PartialLength
) {
122 // Read data from TPM.
124 PartialLength
= MIN (Length
, ATMEL_I2C_TPM_MAX_TRANSFER_SIZE
);
125 Status
= I2cReadMultipleByte (
127 EfiI2CSevenBitAddrMode
,
132 if (!EFI_ERROR (Status
)) {
133 DEBUG ((EFI_D_VERBOSE
, " "));
134 for (Index
= 0; Index
< PartialLength
; Index
++) {
135 DEBUG ((EFI_D_VERBOSE
, "%02x ", Buffer
[Index
]));
137 DEBUG ((EFI_D_VERBOSE
, "\n"));
139 if (EFI_ERROR (Status
)) {
140 DEBUG ((EFI_D_VERBOSE
, " Status = %r\n", Status
));
145 DEBUG ((EFI_D_VERBOSE
, " Status = %r\n", Status
));
150 This service requests use TPM12.
152 @retval EFI_SUCCESS Get the control of TPM12 chip.
153 @retval EFI_NOT_FOUND TPM12 not found.
154 @retval EFI_DEVICE_ERROR Unexpected device behavior.
174 // Get the current timer value
176 Current
= GetPerformanceCounter();
179 // Initialize local variables
186 // Retrieve the performance counter properties and compute the number of
187 // performance counter ticks required to reach the maximum TIS timeout of
188 // TIS_TIMEOUT_A. TIS_TIMEOUT_A is in microseconds.
190 Timeout
= DivU64x32 (
192 GetPerformanceCounterProperties (&Start
, &End
),
204 // Attempt to read a byte from the Atmel I2C TPM
207 Status
= ReadTpmBufferMultiple (&Data
, sizeof(Data
));
210 Current
= GetPerformanceCounter();
211 Delta
= (INT64
) (Current
- Previous
);
219 if (Total
>= Timeout
) {
220 Status
= EFI_TIMEOUT
;
221 DEBUG ((EFI_D_ERROR
, "Atmel I2C TPM failed to read: %r\n", Status
));
224 } while (EFI_ERROR (Status
));
230 This service enables the sending of commands to the TPM12.
232 @param[in] InputParameterBlockSize Size of the TPM12 input parameter block.
233 @param[in] InputParameterBlock Pointer to the TPM12 input parameter block.
234 @param[in,out] OutputParameterBlockSize Size of the TPM12 output parameter block.
235 @param[in] OutputParameterBlock Pointer to the TPM12 output parameter block.
237 @retval EFI_SUCCESS The command byte stream was successfully sent to
238 the device and a response was successfully received.
239 @retval EFI_DEVICE_ERROR The command was not successfully sent to the
240 device or a response was not successfully received
242 @retval EFI_BUFFER_TOO_SMALL The output parameter block is too small.
247 IN UINT32 InputParameterBlockSize
,
248 IN UINT8
*InputParameterBlock
,
249 IN OUT UINT32
*OutputParameterBlockSize
,
250 IN UINT8
*OutputParameterBlock
255 TPM_RSP_COMMAND_HDR
*ResponseHeader
;
266 // Make sure response buffer is big enough to hold a response header
268 if (*OutputParameterBlockSize
< sizeof (TPM_RSP_COMMAND_HDR
)) {
269 Status
= EFI_BUFFER_TOO_SMALL
;
274 // Get the current timer value
276 Current
= GetPerformanceCounter();
279 // Initialize local variables
286 // Retrieve the performance counter properties and compute the number of
287 // performance counter ticks required to reach the maximum TIS timeout of
288 // TIS_TIMEOUT_A. TIS_TIMEOUT_A is in microseconds.
290 Timeout
= DivU64x32 (
292 GetPerformanceCounterProperties (&Start
, &End
),
307 Status
= WriteTpmBufferMultiple (InputParameterBlock
, InputParameterBlockSize
);
310 Current
= GetPerformanceCounter();
311 Delta
= (INT64
) (Current
- Previous
);
319 if (Total
>= Timeout
) {
320 Status
= EFI_TIMEOUT
;
323 } while (EFI_ERROR (Status
));
326 // Receive response header
329 Status
= ReadTpmBufferMultiple (OutputParameterBlock
, sizeof (TPM_RSP_COMMAND_HDR
));
332 Current
= GetPerformanceCounter();
333 Delta
= (INT64
) (Current
- Previous
);
341 if (Total
>= Timeout
) {
342 Status
= EFI_TIMEOUT
;
345 } while (EFI_ERROR (Status
));
348 // Check the response data header (tag, parasize and returncode)
350 ResponseHeader
= (TPM_RSP_COMMAND_HDR
*)OutputParameterBlock
;
351 if (SwapBytes16 (ReadUnaligned16 (&ResponseHeader
->tag
)) != TPM_TAG_RSP_COMMAND
) {
352 Status
= EFI_DEVICE_ERROR
;
356 TpmOutSize
= SwapBytes32 (ReadUnaligned32 (&ResponseHeader
->paramSize
));
357 if (TpmOutSize
== sizeof (TPM_RSP_COMMAND_HDR
)) {
358 Status
= EFI_SUCCESS
;
361 if (TpmOutSize
< sizeof (TPM_RSP_COMMAND_HDR
)) {
362 Status
= EFI_DEVICE_ERROR
;
365 if (*OutputParameterBlockSize
< TpmOutSize
) {
366 Status
= EFI_BUFFER_TOO_SMALL
;
369 *OutputParameterBlockSize
= TpmOutSize
;
372 // Receive the remaining data in the response header
375 Status
= ReadTpmBufferMultiple (
376 OutputParameterBlock
+ sizeof (TPM_RSP_COMMAND_HDR
),
377 TpmOutSize
- sizeof (TPM_RSP_COMMAND_HDR
)
381 Current
= GetPerformanceCounter();
382 Delta
= (INT64
) (Current
- Previous
);
390 if (Total
>= Timeout
) {
391 Status
= EFI_TIMEOUT
;
394 } while (EFI_ERROR (Status
));
399 "Tpm12SubmitCommand() Status = %r Time = %ld ms\n",
402 MultU64x32 (Total
, 1000),
403 GetPerformanceCounterProperties (NULL
, NULL
),