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/reponse 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)
74 MicroSecondDelay (30);
80 Get BurstCount by reading the burstCount field of a TIS regiger
81 in the time of default TIS_TIMEOUT_D.
83 @param[in] TisReg Pointer to TIS register.
84 @param[out] BurstCount Pointer to a buffer to store the got BurstConut.
86 @retval EFI_SUCCESS Get BurstCount.
87 @retval EFI_INVALID_PARAMETER TisReg is NULL or BurstCount is NULL.
88 @retval EFI_TIMEOUT BurstCount can't be got in time.
92 IN TIS_PC_REGISTERS_PTR TisReg
,
93 OUT UINT16
*BurstCount
100 if (BurstCount
== NULL
|| TisReg
== NULL
) {
101 return EFI_INVALID_PARAMETER
;
107 // TIS_PC_REGISTERS_PTR->burstCount is UINT16, but it is not 2bytes aligned,
108 // so it needs to use MmioRead8 to read two times
110 DataByte0
= MmioRead8 ((UINTN
)&TisReg
->BurstCount
);
111 DataByte1
= MmioRead8 ((UINTN
)&TisReg
->BurstCount
+ 1);
112 *BurstCount
= (UINT16
)((DataByte1
<< 8) + DataByte0
);
113 if (*BurstCount
!= 0) {
116 MicroSecondDelay (30);
118 } while (WaitTime
< TIS_TIMEOUT_D
);
124 Set TPM chip to ready state by sending ready command TIS_PC_STS_READY
125 to Status Register in time.
127 @param[in] TisReg Pointer to TIS register.
129 @retval EFI_SUCCESS TPM chip enters into ready state.
130 @retval EFI_INVALID_PARAMETER TisReg is NULL.
131 @retval EFI_TIMEOUT TPM chip can't be set to ready state in time.
134 TisPcPrepareCommand (
135 IN TIS_PC_REGISTERS_PTR TisReg
140 if (TisReg
== NULL
) {
141 return EFI_INVALID_PARAMETER
;
144 MmioWrite8((UINTN
)&TisReg
->Status
, TIS_PC_STS_READY
);
145 Status
= TisPcWaitRegisterBits (
155 Get the control of TPM chip by sending requestUse command TIS_PC_ACC_RQUUSE
156 to ACCESS Register in the time of default TIS_TIMEOUT_A.
158 @param[in] TisReg Pointer to TIS register.
160 @retval EFI_SUCCESS Get the control of TPM chip.
161 @retval EFI_INVALID_PARAMETER TisReg is NULL.
162 @retval EFI_NOT_FOUND TPM chip doesn't exit.
163 @retval EFI_TIMEOUT Can't get the TPM control in time.
167 IN TIS_PC_REGISTERS_PTR TisReg
172 if (TisReg
== NULL
) {
173 return EFI_INVALID_PARAMETER
;
176 if (!TisPcPresenceCheck (TisReg
)) {
177 return EFI_NOT_FOUND
;
180 MmioWrite8((UINTN
)&TisReg
->Access
, TIS_PC_ACC_RQUUSE
);
181 Status
= TisPcWaitRegisterBits (
183 (UINT8
)(TIS_PC_ACC_ACTIVE
|TIS_PC_VALID
),
191 Send a command to TPM for execution and return response data.
193 @param[in] TisReg TPM register space base address.
194 @param[in] BufferIn Buffer for command data.
195 @param[in] SizeIn Size of command data.
196 @param[in, out] BufferOut Buffer for response data.
197 @param[in, out] SizeOut Size of response data.
199 @retval EFI_SUCCESS Operation completed successfully.
200 @retval EFI_BUFFER_TOO_SMALL Response data buffer is too small.
201 @retval EFI_DEVICE_ERROR Unexpected device behavior.
202 @retval EFI_UNSUPPORTED Unsupported TPM version
207 IN TIS_PC_REGISTERS_PTR TisReg
,
210 IN OUT UINT8
*BufferOut
,
211 IN OUT UINT32
*SizeOut
224 DEBUG ((EFI_D_VERBOSE
, "Tpm2TisTpmCommand Send - "));
225 if (SizeIn
> 0x100) {
230 for (Index
= 0; Index
< DebugSize
; Index
++) {
231 DEBUG ((EFI_D_VERBOSE
, "%02x ", BufferIn
[Index
]));
233 if (DebugSize
!= SizeIn
) {
234 DEBUG ((EFI_D_VERBOSE
, "...... "));
235 for (Index
= SizeIn
- 0x20; Index
< SizeIn
; Index
++) {
236 DEBUG ((EFI_D_VERBOSE
, "%02x ", BufferIn
[Index
]));
239 DEBUG ((EFI_D_VERBOSE
, "\n"));
243 Status
= TisPcPrepareCommand (TisReg
);
244 if (EFI_ERROR (Status
)){
245 DEBUG ((DEBUG_ERROR
, "Tpm2 is not ready for command!\n"));
246 return EFI_DEVICE_ERROR
;
249 // Send the command data to Tpm
252 while (Index
< SizeIn
) {
253 Status
= TisPcReadBurstCount (TisReg
, &BurstCount
);
254 if (EFI_ERROR (Status
)) {
255 Status
= EFI_DEVICE_ERROR
;
258 for (; BurstCount
> 0 && Index
< SizeIn
; BurstCount
--) {
259 MmioWrite8((UINTN
)&TisReg
->DataFifo
, *(BufferIn
+ Index
));
264 // Check the Tpm status STS_EXPECT change from 1 to 0
266 Status
= TisPcWaitRegisterBits (
268 (UINT8
) TIS_PC_VALID
,
272 if (EFI_ERROR (Status
)) {
273 DEBUG ((DEBUG_ERROR
, "Tpm2 The send buffer too small!\n"));
274 Status
= EFI_BUFFER_TOO_SMALL
;
278 // Executed the TPM command and waiting for the response data ready
280 MmioWrite8((UINTN
)&TisReg
->Status
, TIS_PC_STS_GO
);
283 // NOTE: That may take many seconds to minutes for certain commands, such as key generation.
285 Status
= TisPcWaitRegisterBits (
287 (UINT8
) (TIS_PC_VALID
| TIS_PC_STS_DATA
),
291 if (EFI_ERROR (Status
)) {
293 // dataAvail check timeout. Cancel the currently executing command by writing commandCancel,
294 // Expect TPM_RC_CANCELLED or successfully completed response.
296 DEBUG ((DEBUG_ERROR
, "Wait for Tpm2 response data time out. Trying to cancel the command!!\n"));
298 MmioWrite32((UINTN
)&TisReg
->Status
, TIS_PC_STS_CANCEL
);
299 Status
= TisPcWaitRegisterBits (
301 (UINT8
) (TIS_PC_VALID
| TIS_PC_STS_DATA
),
306 // Do not clear CANCEL bit here bicoz Writes of 0 to this bit are ignored
308 if (EFI_ERROR (Status
)) {
310 // Cancel executing command fail to get any response
311 // Try to abort the command with write of a 1 to commandReady in Command Execution state
313 Status
= EFI_DEVICE_ERROR
;
319 // Get response data header
323 while (Index
< sizeof (TPM2_RESPONSE_HEADER
)) {
324 Status
= TisPcReadBurstCount (TisReg
, &BurstCount
);
325 if (EFI_ERROR (Status
)) {
326 Status
= EFI_DEVICE_ERROR
;
329 for (; BurstCount
> 0; BurstCount
--) {
330 *(BufferOut
+ Index
) = MmioRead8 ((UINTN
)&TisReg
->DataFifo
);
332 if (Index
== sizeof (TPM2_RESPONSE_HEADER
)) break;
336 DEBUG ((EFI_D_VERBOSE
, "Tpm2TisTpmCommand ReceiveHeader - "));
337 for (Index
= 0; Index
< sizeof (TPM2_RESPONSE_HEADER
); Index
++) {
338 DEBUG ((EFI_D_VERBOSE
, "%02x ", BufferOut
[Index
]));
340 DEBUG ((EFI_D_VERBOSE
, "\n"));
343 // Check the reponse data header (tag,parasize and returncode )
345 CopyMem (&Data16
, BufferOut
, sizeof (UINT16
));
346 // TPM2 should not use this RSP_COMMAND
347 if (SwapBytes16 (Data16
) == TPM_ST_RSP_COMMAND
) {
348 DEBUG ((EFI_D_ERROR
, "TPM2: TPM_ST_RSP error - %x\n", TPM_ST_RSP_COMMAND
));
349 Status
= EFI_UNSUPPORTED
;
353 CopyMem (&Data32
, (BufferOut
+ 2), sizeof (UINT32
));
354 TpmOutSize
= SwapBytes32 (Data32
);
355 if (*SizeOut
< TpmOutSize
) {
356 Status
= EFI_BUFFER_TOO_SMALL
;
359 *SizeOut
= TpmOutSize
;
361 // Continue reading the remaining data
363 while ( Index
< TpmOutSize
) {
364 for (; BurstCount
> 0; BurstCount
--) {
365 *(BufferOut
+ Index
) = MmioRead8 ((UINTN
)&TisReg
->DataFifo
);
367 if (Index
== TpmOutSize
) {
368 Status
= EFI_SUCCESS
;
372 Status
= TisPcReadBurstCount (TisReg
, &BurstCount
);
373 if (EFI_ERROR (Status
)) {
374 Status
= EFI_DEVICE_ERROR
;
380 DEBUG ((EFI_D_VERBOSE
, "Tpm2TisTpmCommand Receive - "));
381 for (Index
= 0; Index
< TpmOutSize
; Index
++) {
382 DEBUG ((EFI_D_VERBOSE
, "%02x ", BufferOut
[Index
]));
384 DEBUG ((EFI_D_VERBOSE
, "\n"));
386 MmioWrite8((UINTN
)&TisReg
->Status
, TIS_PC_STS_READY
);
391 This service enables the sending of commands to the TPM2.
393 @param[in] InputParameterBlockSize Size of the TPM2 input parameter block.
394 @param[in] InputParameterBlock Pointer to the TPM2 input parameter block.
395 @param[in,out] OutputParameterBlockSize Size of the TPM2 output parameter block.
396 @param[in] OutputParameterBlock Pointer to the TPM2 output parameter block.
398 @retval EFI_SUCCESS The command byte stream was successfully sent to the device and a response was successfully received.
399 @retval EFI_DEVICE_ERROR The command was not successfully sent to the device or a response was not successfully received from the device.
400 @retval EFI_BUFFER_TOO_SMALL The output parameter block is too small.
404 DTpm2TisSubmitCommand (
405 IN UINT32 InputParameterBlockSize
,
406 IN UINT8
*InputParameterBlock
,
407 IN OUT UINT32
*OutputParameterBlockSize
,
408 IN UINT8
*OutputParameterBlock
411 return Tpm2TisTpmCommand (
412 (TIS_PC_REGISTERS_PTR
) (UINTN
) PcdGet64 (PcdTpmBaseAddress
),
414 InputParameterBlockSize
,
415 OutputParameterBlock
,
416 OutputParameterBlockSize
421 This service requests use TPM2.
423 @retval EFI_SUCCESS Get the control of TPM2 chip.
424 @retval EFI_NOT_FOUND TPM2 not found.
425 @retval EFI_DEVICE_ERROR Unexpected device behavior.
429 DTpm2TisRequestUseTpm (
433 return TisPcRequestUseTpm ((TIS_PC_REGISTERS_PTR
) (UINTN
) PcdGet64 (PcdTpmBaseAddress
));