2 TIS (TPM Interface Specification) functions used by dTPM2.0 library.
4 Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.<BR>
5 (C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include <IndustryStandard/Tpm20.h>
12 #include <Library/BaseLib.h>
13 #include <Library/BaseMemoryLib.h>
14 #include <Library/IoLib.h>
15 #include <Library/TimerLib.h>
16 #include <Library/DebugLib.h>
17 #include <Library/Tpm2DeviceLib.h>
18 #include <Library/PcdLib.h>
20 #include <IndustryStandard/TpmTis.h>
22 #define TIS_TIMEOUT_MAX (90000 * 1000) // 90s
25 // Max TPM command/response length
27 #define TPMCMDBUFLENGTH 0x500
30 Check whether TPM chip exist.
32 @param[in] TisReg Pointer to TIS register.
34 @retval TRUE TPM chip exists.
35 @retval FALSE TPM chip is not found.
39 IN TIS_PC_REGISTERS_PTR TisReg
44 RegRead
= MmioRead8 ((UINTN
)&TisReg
->Access
);
45 return (BOOLEAN
)(RegRead
!= (UINT8
)-1);
49 Check whether the value of a TPM chip register satisfies the input BIT setting.
51 @param[in] Register Address port of register to be checked.
52 @param[in] BitSet Check these data bits are set.
53 @param[in] BitClear Check these data bits are clear.
54 @param[in] TimeOut The max wait time (unit MicroSecond) when checking register.
56 @retval EFI_SUCCESS The register satisfies the check bit.
57 @retval EFI_TIMEOUT The register can't run into the expected status in time.
60 TisPcWaitRegisterBits (
70 for (WaitTime
= 0; WaitTime
< TimeOut
; WaitTime
+= 30) {
71 RegRead
= MmioRead8 ((UINTN
)Register
);
72 if (((RegRead
& BitSet
) == BitSet
) && ((RegRead
& BitClear
) == 0)) {
76 MicroSecondDelay (30);
83 Get BurstCount by reading the burstCount field of a TIS register
84 in the time of default TIS_TIMEOUT_D.
86 @param[in] TisReg Pointer to TIS register.
87 @param[out] BurstCount Pointer to a buffer to store the got BurstCount.
89 @retval EFI_SUCCESS Get BurstCount.
90 @retval EFI_INVALID_PARAMETER TisReg is NULL or BurstCount is NULL.
91 @retval EFI_TIMEOUT BurstCount can't be got in time.
95 IN TIS_PC_REGISTERS_PTR TisReg
,
96 OUT UINT16
*BurstCount
103 if ((BurstCount
== NULL
) || (TisReg
== NULL
)) {
104 return EFI_INVALID_PARAMETER
;
110 // TIS_PC_REGISTERS_PTR->burstCount is UINT16, but it is not 2bytes aligned,
111 // so it needs to use MmioRead8 to read two times
113 DataByte0
= MmioRead8 ((UINTN
)&TisReg
->BurstCount
);
114 DataByte1
= MmioRead8 ((UINTN
)&TisReg
->BurstCount
+ 1);
115 *BurstCount
= (UINT16
)((DataByte1
<< 8) + DataByte0
);
116 if (*BurstCount
!= 0) {
120 MicroSecondDelay (30);
122 } while (WaitTime
< TIS_TIMEOUT_D
);
128 Set TPM chip to ready state by sending ready command TIS_PC_STS_READY
129 to Status Register in time.
131 @param[in] TisReg Pointer to TIS register.
133 @retval EFI_SUCCESS TPM chip enters into ready state.
134 @retval EFI_INVALID_PARAMETER TisReg is NULL.
135 @retval EFI_TIMEOUT TPM chip can't be set to ready state in time.
138 TisPcPrepareCommand (
139 IN TIS_PC_REGISTERS_PTR TisReg
144 if (TisReg
== NULL
) {
145 return EFI_INVALID_PARAMETER
;
148 MmioWrite8 ((UINTN
)&TisReg
->Status
, TIS_PC_STS_READY
);
149 Status
= TisPcWaitRegisterBits (
159 Get the control of TPM chip by sending requestUse command TIS_PC_ACC_RQUUSE
160 to ACCESS Register in the time of default TIS_TIMEOUT_A.
162 @param[in] TisReg Pointer to TIS register.
164 @retval EFI_SUCCESS Get the control of TPM chip.
165 @retval EFI_INVALID_PARAMETER TisReg is NULL.
166 @retval EFI_NOT_FOUND TPM chip doesn't exit.
167 @retval EFI_TIMEOUT Can't get the TPM control in time.
171 IN TIS_PC_REGISTERS_PTR TisReg
176 if (TisReg
== NULL
) {
177 return EFI_INVALID_PARAMETER
;
180 if (!TisPcPresenceCheck (TisReg
)) {
181 return EFI_NOT_FOUND
;
184 MmioWrite8 ((UINTN
)&TisReg
->Access
, TIS_PC_ACC_RQUUSE
);
185 Status
= TisPcWaitRegisterBits (
187 (UINT8
)(TIS_PC_ACC_ACTIVE
|TIS_PC_VALID
),
195 Send a command to TPM for execution and return response data.
197 @param[in] TisReg TPM register space base address.
198 @param[in] BufferIn Buffer for command data.
199 @param[in] SizeIn Size of command data.
200 @param[in, out] BufferOut Buffer for response data.
201 @param[in, out] SizeOut Size of response data.
203 @retval EFI_SUCCESS Operation completed successfully.
204 @retval EFI_BUFFER_TOO_SMALL Response data buffer is too small.
205 @retval EFI_DEVICE_ERROR Unexpected device behavior.
206 @retval EFI_UNSUPPORTED Unsupported TPM version
211 IN TIS_PC_REGISTERS_PTR TisReg
,
214 IN OUT UINT8
*BufferOut
,
215 IN OUT UINT32
*SizeOut
228 DEBUG ((DEBUG_VERBOSE
, "Tpm2TisTpmCommand Send - "));
229 if (SizeIn
> 0x100) {
235 for (Index
= 0; Index
< DebugSize
; Index
++) {
236 DEBUG ((DEBUG_VERBOSE
, "%02x ", BufferIn
[Index
]));
239 if (DebugSize
!= SizeIn
) {
240 DEBUG ((DEBUG_VERBOSE
, "...... "));
241 for (Index
= SizeIn
- 0x20; Index
< SizeIn
; Index
++) {
242 DEBUG ((DEBUG_VERBOSE
, "%02x ", BufferIn
[Index
]));
246 DEBUG ((DEBUG_VERBOSE
, "\n"));
250 Status
= TisPcPrepareCommand (TisReg
);
251 if (EFI_ERROR (Status
)) {
252 DEBUG ((DEBUG_ERROR
, "Tpm2 is not ready for command!\n"));
253 return EFI_DEVICE_ERROR
;
257 // Send the command data to Tpm
260 while (Index
< SizeIn
) {
261 Status
= TisPcReadBurstCount (TisReg
, &BurstCount
);
262 if (EFI_ERROR (Status
)) {
263 Status
= EFI_DEVICE_ERROR
;
267 for ( ; BurstCount
> 0 && Index
< SizeIn
; BurstCount
--) {
268 MmioWrite8 ((UINTN
)&TisReg
->DataFifo
, *(BufferIn
+ Index
));
274 // Check the Tpm status STS_EXPECT change from 1 to 0
276 Status
= TisPcWaitRegisterBits (
282 if (EFI_ERROR (Status
)) {
283 DEBUG ((DEBUG_ERROR
, "Tpm2 The send buffer too small!\n"));
284 Status
= EFI_BUFFER_TOO_SMALL
;
289 // Executed the TPM command and waiting for the response data ready
291 MmioWrite8 ((UINTN
)&TisReg
->Status
, TIS_PC_STS_GO
);
294 // NOTE: That may take many seconds to minutes for certain commands, such as key generation.
296 Status
= TisPcWaitRegisterBits (
298 (UINT8
)(TIS_PC_VALID
| TIS_PC_STS_DATA
),
302 if (EFI_ERROR (Status
)) {
304 // dataAvail check timeout. Cancel the currently executing command by writing commandCancel,
305 // Expect TPM_RC_CANCELLED or successfully completed response.
307 DEBUG ((DEBUG_ERROR
, "Wait for Tpm2 response data time out. Trying to cancel the command!!\n"));
309 MmioWrite32 ((UINTN
)&TisReg
->Status
, TIS_PC_STS_CANCEL
);
310 Status
= TisPcWaitRegisterBits (
312 (UINT8
)(TIS_PC_VALID
| TIS_PC_STS_DATA
),
317 // Do not clear CANCEL bit here because Writes of 0 to this bit are ignored
319 if (EFI_ERROR (Status
)) {
321 // Cancel executing command fail to get any response
322 // Try to abort the command with write of a 1 to commandReady in Command Execution state
324 Status
= EFI_DEVICE_ERROR
;
330 // Get response data header
334 while (Index
< sizeof (TPM2_RESPONSE_HEADER
)) {
335 Status
= TisPcReadBurstCount (TisReg
, &BurstCount
);
336 if (EFI_ERROR (Status
)) {
337 Status
= EFI_DEVICE_ERROR
;
341 for ( ; BurstCount
> 0; BurstCount
--) {
342 *(BufferOut
+ Index
) = MmioRead8 ((UINTN
)&TisReg
->DataFifo
);
344 if (Index
== sizeof (TPM2_RESPONSE_HEADER
)) {
351 DEBUG ((DEBUG_VERBOSE
, "Tpm2TisTpmCommand ReceiveHeader - "));
352 for (Index
= 0; Index
< sizeof (TPM2_RESPONSE_HEADER
); Index
++) {
353 DEBUG ((DEBUG_VERBOSE
, "%02x ", BufferOut
[Index
]));
356 DEBUG ((DEBUG_VERBOSE
, "\n"));
359 // Check the response data header (tag,parasize and returncode )
361 CopyMem (&Data16
, BufferOut
, sizeof (UINT16
));
362 // TPM2 should not use this RSP_COMMAND
363 if (SwapBytes16 (Data16
) == TPM_ST_RSP_COMMAND
) {
364 DEBUG ((DEBUG_ERROR
, "TPM2: TPM_ST_RSP error - %x\n", TPM_ST_RSP_COMMAND
));
365 Status
= EFI_UNSUPPORTED
;
369 CopyMem (&Data32
, (BufferOut
+ 2), sizeof (UINT32
));
370 TpmOutSize
= SwapBytes32 (Data32
);
371 if (*SizeOut
< TpmOutSize
) {
372 Status
= EFI_BUFFER_TOO_SMALL
;
376 *SizeOut
= TpmOutSize
;
378 // Continue reading the remaining data
380 while ( Index
< TpmOutSize
) {
381 for ( ; BurstCount
> 0; BurstCount
--) {
382 *(BufferOut
+ Index
) = MmioRead8 ((UINTN
)&TisReg
->DataFifo
);
384 if (Index
== TpmOutSize
) {
385 Status
= EFI_SUCCESS
;
390 Status
= TisPcReadBurstCount (TisReg
, &BurstCount
);
391 if (EFI_ERROR (Status
)) {
392 Status
= EFI_DEVICE_ERROR
;
399 DEBUG ((DEBUG_VERBOSE
, "Tpm2TisTpmCommand Receive - "));
400 for (Index
= 0; Index
< TpmOutSize
; Index
++) {
401 DEBUG ((DEBUG_VERBOSE
, "%02x ", BufferOut
[Index
]));
404 DEBUG ((DEBUG_VERBOSE
, "\n"));
406 MmioWrite8 ((UINTN
)&TisReg
->Status
, TIS_PC_STS_READY
);
411 This service enables the sending of commands to the TPM2.
413 @param[in] InputParameterBlockSize Size of the TPM2 input parameter block.
414 @param[in] InputParameterBlock Pointer to the TPM2 input parameter block.
415 @param[in,out] OutputParameterBlockSize Size of the TPM2 output parameter block.
416 @param[in] OutputParameterBlock Pointer to the TPM2 output parameter block.
418 @retval EFI_SUCCESS The command byte stream was successfully sent to the device and a response was successfully received.
419 @retval EFI_DEVICE_ERROR The command was not successfully sent to the device or a response was not successfully received from the device.
420 @retval EFI_BUFFER_TOO_SMALL The output parameter block is too small.
424 DTpm2TisSubmitCommand (
425 IN UINT32 InputParameterBlockSize
,
426 IN UINT8
*InputParameterBlock
,
427 IN OUT UINT32
*OutputParameterBlockSize
,
428 IN UINT8
*OutputParameterBlock
431 return Tpm2TisTpmCommand (
432 (TIS_PC_REGISTERS_PTR
)(UINTN
)PcdGet64 (PcdTpmBaseAddress
),
434 InputParameterBlockSize
,
435 OutputParameterBlock
,
436 OutputParameterBlockSize
441 This service requests use TPM2.
443 @retval EFI_SUCCESS Get the control of TPM2 chip.
444 @retval EFI_NOT_FOUND TPM2 not found.
445 @retval EFI_DEVICE_ERROR Unexpected device behavior.
449 DTpm2TisRequestUseTpm (
453 return TisPcRequestUseTpm ((TIS_PC_REGISTERS_PTR
)(UINTN
)PcdGet64 (PcdTpmBaseAddress
));