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 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include <IndustryStandard/Tpm20.h>
18 #include <Library/BaseLib.h>
19 #include <Library/BaseMemoryLib.h>
20 #include <Library/IoLib.h>
21 #include <Library/TimerLib.h>
22 #include <Library/DebugLib.h>
23 #include <Library/Tpm2DeviceLib.h>
24 #include <Library/PcdLib.h>
26 #include <IndustryStandard/TpmTis.h>
28 #define TIS_TIMEOUT_MAX (90000 * 1000) // 90s
31 // Max TPM command/reponse length
33 #define TPMCMDBUFLENGTH 0x500
36 Check whether TPM chip exist.
38 @param[in] TisReg Pointer to TIS register.
40 @retval TRUE TPM chip exists.
41 @retval FALSE TPM chip is not found.
45 IN TIS_PC_REGISTERS_PTR TisReg
50 RegRead
= MmioRead8 ((UINTN
)&TisReg
->Access
);
51 return (BOOLEAN
)(RegRead
!= (UINT8
)-1);
55 Check whether the value of a TPM chip register satisfies the input BIT setting.
57 @param[in] Register Address port of register to be checked.
58 @param[in] BitSet Check these data bits are set.
59 @param[in] BitClear Check these data bits are clear.
60 @param[in] TimeOut The max wait time (unit MicroSecond) when checking register.
62 @retval EFI_SUCCESS The register satisfies the check bit.
63 @retval EFI_TIMEOUT The register can't run into the expected status in time.
66 TisPcWaitRegisterBits (
76 for (WaitTime
= 0; WaitTime
< TimeOut
; WaitTime
+= 30){
77 RegRead
= MmioRead8 ((UINTN
)Register
);
78 if ((RegRead
& BitSet
) == BitSet
&& (RegRead
& BitClear
) == 0)
80 MicroSecondDelay (30);
86 Get BurstCount by reading the burstCount field of a TIS regiger
87 in the time of default TIS_TIMEOUT_D.
89 @param[in] TisReg Pointer to TIS register.
90 @param[out] BurstCount Pointer to a buffer to store the got BurstConut.
92 @retval EFI_SUCCESS Get BurstCount.
93 @retval EFI_INVALID_PARAMETER TisReg is NULL or BurstCount is NULL.
94 @retval EFI_TIMEOUT BurstCount can't be got in time.
98 IN TIS_PC_REGISTERS_PTR TisReg
,
99 OUT UINT16
*BurstCount
106 if (BurstCount
== NULL
|| TisReg
== NULL
) {
107 return EFI_INVALID_PARAMETER
;
113 // TIS_PC_REGISTERS_PTR->burstCount is UINT16, but it is not 2bytes aligned,
114 // so it needs to use MmioRead8 to read two times
116 DataByte0
= MmioRead8 ((UINTN
)&TisReg
->BurstCount
);
117 DataByte1
= MmioRead8 ((UINTN
)&TisReg
->BurstCount
+ 1);
118 *BurstCount
= (UINT16
)((DataByte1
<< 8) + DataByte0
);
119 if (*BurstCount
!= 0) {
122 MicroSecondDelay (30);
124 } while (WaitTime
< TIS_TIMEOUT_D
);
130 Set TPM chip to ready state by sending ready command TIS_PC_STS_READY
131 to Status Register in time.
133 @param[in] TisReg Pointer to TIS register.
135 @retval EFI_SUCCESS TPM chip enters into ready state.
136 @retval EFI_INVALID_PARAMETER TisReg is NULL.
137 @retval EFI_TIMEOUT TPM chip can't be set to ready state in time.
140 TisPcPrepareCommand (
141 IN TIS_PC_REGISTERS_PTR TisReg
146 if (TisReg
== NULL
) {
147 return EFI_INVALID_PARAMETER
;
150 MmioWrite8((UINTN
)&TisReg
->Status
, TIS_PC_STS_READY
);
151 Status
= TisPcWaitRegisterBits (
161 Get the control of TPM chip by sending requestUse command TIS_PC_ACC_RQUUSE
162 to ACCESS Register in the time of default TIS_TIMEOUT_A.
164 @param[in] TisReg Pointer to TIS register.
166 @retval EFI_SUCCESS Get the control of TPM chip.
167 @retval EFI_INVALID_PARAMETER TisReg is NULL.
168 @retval EFI_NOT_FOUND TPM chip doesn't exit.
169 @retval EFI_TIMEOUT Can't get the TPM control in time.
173 IN TIS_PC_REGISTERS_PTR TisReg
178 if (TisReg
== NULL
) {
179 return EFI_INVALID_PARAMETER
;
182 if (!TisPcPresenceCheck (TisReg
)) {
183 return EFI_NOT_FOUND
;
186 MmioWrite8((UINTN
)&TisReg
->Access
, TIS_PC_ACC_RQUUSE
);
187 Status
= TisPcWaitRegisterBits (
189 (UINT8
)(TIS_PC_ACC_ACTIVE
|TIS_PC_VALID
),
197 Send a command to TPM for execution and return response data.
199 @param[in] TisReg TPM register space base address.
200 @param[in] BufferIn Buffer for command data.
201 @param[in] SizeIn Size of command data.
202 @param[in, out] BufferOut Buffer for response data.
203 @param[in, out] SizeOut Size of response data.
205 @retval EFI_SUCCESS Operation completed successfully.
206 @retval EFI_BUFFER_TOO_SMALL Response data buffer is too small.
207 @retval EFI_DEVICE_ERROR Unexpected device behavior.
208 @retval EFI_UNSUPPORTED Unsupported TPM version
213 IN TIS_PC_REGISTERS_PTR TisReg
,
216 IN OUT UINT8
*BufferOut
,
217 IN OUT UINT32
*SizeOut
230 DEBUG ((EFI_D_VERBOSE
, "Tpm2TisTpmCommand Send - "));
231 if (SizeIn
> 0x100) {
236 for (Index
= 0; Index
< DebugSize
; Index
++) {
237 DEBUG ((EFI_D_VERBOSE
, "%02x ", BufferIn
[Index
]));
239 if (DebugSize
!= SizeIn
) {
240 DEBUG ((EFI_D_VERBOSE
, "...... "));
241 for (Index
= SizeIn
- 0x20; Index
< SizeIn
; Index
++) {
242 DEBUG ((EFI_D_VERBOSE
, "%02x ", BufferIn
[Index
]));
245 DEBUG ((EFI_D_VERBOSE
, "\n"));
249 Status
= TisPcPrepareCommand (TisReg
);
250 if (EFI_ERROR (Status
)){
251 DEBUG ((DEBUG_ERROR
, "Tpm2 is not ready for command!\n"));
252 return EFI_DEVICE_ERROR
;
255 // Send the command data to Tpm
258 while (Index
< SizeIn
) {
259 Status
= TisPcReadBurstCount (TisReg
, &BurstCount
);
260 if (EFI_ERROR (Status
)) {
261 Status
= EFI_DEVICE_ERROR
;
264 for (; BurstCount
> 0 && Index
< SizeIn
; BurstCount
--) {
265 MmioWrite8((UINTN
)&TisReg
->DataFifo
, *(BufferIn
+ Index
));
270 // Check the Tpm status STS_EXPECT change from 1 to 0
272 Status
= TisPcWaitRegisterBits (
274 (UINT8
) TIS_PC_VALID
,
278 if (EFI_ERROR (Status
)) {
279 DEBUG ((DEBUG_ERROR
, "Tpm2 The send buffer too small!\n"));
280 Status
= EFI_BUFFER_TOO_SMALL
;
284 // Executed the TPM command and waiting for the response data ready
286 MmioWrite8((UINTN
)&TisReg
->Status
, TIS_PC_STS_GO
);
289 // NOTE: That may take many seconds to minutes for certain commands, such as key generation.
291 Status
= TisPcWaitRegisterBits (
293 (UINT8
) (TIS_PC_VALID
| TIS_PC_STS_DATA
),
297 if (EFI_ERROR (Status
)) {
299 // dataAvail check timeout. Cancel the currently executing command by writing commandCancel,
300 // Expect TPM_RC_CANCELLED or successfully completed response.
302 DEBUG ((DEBUG_ERROR
, "Wait for Tpm2 response data time out. Trying to cancel the command!!\n"));
304 MmioWrite32((UINTN
)&TisReg
->Status
, TIS_PC_STS_CANCEL
);
305 Status
= TisPcWaitRegisterBits (
307 (UINT8
) (TIS_PC_VALID
| TIS_PC_STS_DATA
),
312 // Do not clear CANCEL bit here bicoz Writes of 0 to this bit are ignored
314 if (EFI_ERROR (Status
)) {
316 // Cancel executing command fail to get any response
317 // Try to abort the command with write of a 1 to commandReady in Command Execution state
319 Status
= EFI_DEVICE_ERROR
;
325 // Get response data header
329 while (Index
< sizeof (TPM2_RESPONSE_HEADER
)) {
330 Status
= TisPcReadBurstCount (TisReg
, &BurstCount
);
331 if (EFI_ERROR (Status
)) {
332 Status
= EFI_DEVICE_ERROR
;
335 for (; BurstCount
> 0; BurstCount
--) {
336 *(BufferOut
+ Index
) = MmioRead8 ((UINTN
)&TisReg
->DataFifo
);
338 if (Index
== sizeof (TPM2_RESPONSE_HEADER
)) break;
342 DEBUG ((EFI_D_VERBOSE
, "Tpm2TisTpmCommand ReceiveHeader - "));
343 for (Index
= 0; Index
< sizeof (TPM2_RESPONSE_HEADER
); Index
++) {
344 DEBUG ((EFI_D_VERBOSE
, "%02x ", BufferOut
[Index
]));
346 DEBUG ((EFI_D_VERBOSE
, "\n"));
349 // Check the reponse data header (tag,parasize and returncode )
351 CopyMem (&Data16
, BufferOut
, sizeof (UINT16
));
352 // TPM2 should not use this RSP_COMMAND
353 if (SwapBytes16 (Data16
) == TPM_ST_RSP_COMMAND
) {
354 DEBUG ((EFI_D_ERROR
, "TPM2: TPM_ST_RSP error - %x\n", TPM_ST_RSP_COMMAND
));
355 Status
= EFI_UNSUPPORTED
;
359 CopyMem (&Data32
, (BufferOut
+ 2), sizeof (UINT32
));
360 TpmOutSize
= SwapBytes32 (Data32
);
361 if (*SizeOut
< TpmOutSize
) {
362 Status
= EFI_BUFFER_TOO_SMALL
;
365 *SizeOut
= TpmOutSize
;
367 // Continue reading the remaining data
369 while ( Index
< TpmOutSize
) {
370 for (; BurstCount
> 0; BurstCount
--) {
371 *(BufferOut
+ Index
) = MmioRead8 ((UINTN
)&TisReg
->DataFifo
);
373 if (Index
== TpmOutSize
) {
374 Status
= EFI_SUCCESS
;
378 Status
= TisPcReadBurstCount (TisReg
, &BurstCount
);
379 if (EFI_ERROR (Status
)) {
380 Status
= EFI_DEVICE_ERROR
;
386 DEBUG ((EFI_D_VERBOSE
, "Tpm2TisTpmCommand Receive - "));
387 for (Index
= 0; Index
< TpmOutSize
; Index
++) {
388 DEBUG ((EFI_D_VERBOSE
, "%02x ", BufferOut
[Index
]));
390 DEBUG ((EFI_D_VERBOSE
, "\n"));
392 MmioWrite8((UINTN
)&TisReg
->Status
, TIS_PC_STS_READY
);
397 This service enables the sending of commands to the TPM2.
399 @param[in] InputParameterBlockSize Size of the TPM2 input parameter block.
400 @param[in] InputParameterBlock Pointer to the TPM2 input parameter block.
401 @param[in,out] OutputParameterBlockSize Size of the TPM2 output parameter block.
402 @param[in] OutputParameterBlock Pointer to the TPM2 output parameter block.
404 @retval EFI_SUCCESS The command byte stream was successfully sent to the device and a response was successfully received.
405 @retval EFI_DEVICE_ERROR The command was not successfully sent to the device or a response was not successfully received from the device.
406 @retval EFI_BUFFER_TOO_SMALL The output parameter block is too small.
410 DTpm2TisSubmitCommand (
411 IN UINT32 InputParameterBlockSize
,
412 IN UINT8
*InputParameterBlock
,
413 IN OUT UINT32
*OutputParameterBlockSize
,
414 IN UINT8
*OutputParameterBlock
417 return Tpm2TisTpmCommand (
418 (TIS_PC_REGISTERS_PTR
) (UINTN
) PcdGet64 (PcdTpmBaseAddress
),
420 InputParameterBlockSize
,
421 OutputParameterBlock
,
422 OutputParameterBlockSize
427 This service requests use TPM2.
429 @retval EFI_SUCCESS Get the control of TPM2 chip.
430 @retval EFI_NOT_FOUND TPM2 not found.
431 @retval EFI_DEVICE_ERROR Unexpected device behavior.
435 DTpm2TisRequestUseTpm (
439 return TisPcRequestUseTpm ((TIS_PC_REGISTERS_PTR
) (UINTN
) PcdGet64 (PcdTpmBaseAddress
));