2 PTP (Platform TPM Profile) CRB (Command Response Buffer) interface used by dTPM2.0 library.
4 Copyright (c) 2015 - 2018, 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.
15 #include <IndustryStandard/Tpm20.h>
17 #include <Library/BaseLib.h>
18 #include <Library/BaseMemoryLib.h>
19 #include <Library/IoLib.h>
20 #include <Library/TimerLib.h>
21 #include <Library/DebugLib.h>
22 #include <Library/Tpm2DeviceLib.h>
23 #include <Library/PcdLib.h>
25 #include <IndustryStandard/TpmPtp.h>
26 #include <IndustryStandard/TpmTis.h>
29 // Execution of the command may take from several seconds to minutes for certain
30 // commands, such as key generation.
32 #define PTP_TIMEOUT_MAX (90000 * 1000) // 90s
35 // Max TPM command/reponse length
37 #define TPMCMDBUFLENGTH 0x500
40 Check whether TPM PTP register exist.
42 @param[in] Reg Pointer to PTP register.
44 @retval TRUE TPM PTP exists.
45 @retval FALSE TPM PTP is not found.
54 RegRead
= MmioRead8 ((UINTN
)Reg
);
55 if (RegRead
== 0xFF) {
65 Check whether the value of a TPM chip register satisfies the input BIT setting.
67 @param[in] Register Address port of register to be checked.
68 @param[in] BitSet Check these data bits are set.
69 @param[in] BitClear Check these data bits are clear.
70 @param[in] TimeOut The max wait time (unit MicroSecond) when checking register.
72 @retval EFI_SUCCESS The register satisfies the check bit.
73 @retval EFI_TIMEOUT The register can't run into the expected status in time.
76 PtpCrbWaitRegisterBits (
86 for (WaitTime
= 0; WaitTime
< TimeOut
; WaitTime
+= 30){
87 RegRead
= MmioRead32 ((UINTN
)Register
);
88 if ((RegRead
& BitSet
) == BitSet
&& (RegRead
& BitClear
) == 0) {
91 MicroSecondDelay (30);
97 Get the control of TPM chip.
99 @param[in] CrbReg Pointer to CRB register.
101 @retval EFI_SUCCESS Get the control of TPM chip.
102 @retval EFI_INVALID_PARAMETER CrbReg is NULL.
103 @retval EFI_NOT_FOUND TPM chip doesn't exit.
104 @retval EFI_TIMEOUT Can't get the TPM control in time.
107 PtpCrbRequestUseTpm (
108 IN PTP_CRB_REGISTERS_PTR CrbReg
113 if (!Tpm2IsPtpPresence (CrbReg
)) {
114 return EFI_NOT_FOUND
;
117 MmioWrite32((UINTN
)&CrbReg
->LocalityControl
, PTP_CRB_LOCALITY_CONTROL_REQUEST_ACCESS
);
118 Status
= PtpCrbWaitRegisterBits (
119 &CrbReg
->LocalityStatus
,
120 PTP_CRB_LOCALITY_STATUS_GRANTED
,
128 Send a command to TPM for execution and return response data.
130 @param[in] CrbReg TPM register space base address.
131 @param[in] BufferIn Buffer for command data.
132 @param[in] SizeIn Size of command data.
133 @param[in, out] BufferOut Buffer for response data.
134 @param[in, out] SizeOut Size of response data.
136 @retval EFI_SUCCESS Operation completed successfully.
137 @retval EFI_BUFFER_TOO_SMALL Response data buffer is too small.
138 @retval EFI_DEVICE_ERROR Unexpected device behavior.
139 @retval EFI_UNSUPPORTED Unsupported TPM version
144 IN PTP_CRB_REGISTERS_PTR CrbReg
,
147 IN OUT UINT8
*BufferOut
,
148 IN OUT UINT32
*SizeOut
160 DEBUG ((EFI_D_VERBOSE
, "PtpCrbTpmCommand Send - "));
161 if (SizeIn
> 0x100) {
166 for (Index
= 0; Index
< DebugSize
; Index
++) {
167 DEBUG ((EFI_D_VERBOSE
, "%02x ", BufferIn
[Index
]));
169 if (DebugSize
!= SizeIn
) {
170 DEBUG ((EFI_D_VERBOSE
, "...... "));
171 for (Index
= SizeIn
- 0x20; Index
< SizeIn
; Index
++) {
172 DEBUG ((EFI_D_VERBOSE
, "%02x ", BufferIn
[Index
]));
175 DEBUG ((EFI_D_VERBOSE
, "\n"));
181 // Ready is any time the TPM is ready to receive a command, following a write
182 // of 1 by software to Request.cmdReady, as indicated by the Status field
183 // being cleared to 0.
185 MmioWrite32((UINTN
)&CrbReg
->CrbControlRequest
, PTP_CRB_CONTROL_AREA_REQUEST_COMMAND_READY
);
186 Status
= PtpCrbWaitRegisterBits (
187 &CrbReg
->CrbControlRequest
,
189 PTP_CRB_CONTROL_AREA_REQUEST_COMMAND_READY
,
192 if (EFI_ERROR (Status
)) {
193 Status
= EFI_DEVICE_ERROR
;
196 Status
= PtpCrbWaitRegisterBits (
197 &CrbReg
->CrbControlStatus
,
199 PTP_CRB_CONTROL_AREA_STATUS_TPM_IDLE
,
202 if (EFI_ERROR (Status
)) {
203 Status
= EFI_DEVICE_ERROR
;
209 // Command Reception occurs following a Ready state between the write of the
210 // first byte of a command to the Command Buffer and the receipt of a write
213 for (Index
= 0; Index
< SizeIn
; Index
++) {
214 MmioWrite8 ((UINTN
)&CrbReg
->CrbDataBuffer
[Index
], BufferIn
[Index
]);
216 MmioWrite32 ((UINTN
)&CrbReg
->CrbControlCommandAddressHigh
, (UINT32
)RShiftU64 ((UINTN
)CrbReg
->CrbDataBuffer
, 32));
217 MmioWrite32 ((UINTN
)&CrbReg
->CrbControlCommandAddressLow
, (UINT32
)(UINTN
)CrbReg
->CrbDataBuffer
);
218 MmioWrite32 ((UINTN
)&CrbReg
->CrbControlCommandSize
, sizeof(CrbReg
->CrbDataBuffer
));
220 MmioWrite64 ((UINTN
)&CrbReg
->CrbControlResponseAddrss
, (UINT32
)(UINTN
)CrbReg
->CrbDataBuffer
);
221 MmioWrite32 ((UINTN
)&CrbReg
->CrbControlResponseSize
, sizeof(CrbReg
->CrbDataBuffer
));
225 // Command Execution occurs after receipt of a 1 to Start and the TPM
226 // clearing Start to 0.
228 MmioWrite32((UINTN
)&CrbReg
->CrbControlStart
, PTP_CRB_CONTROL_START
);
229 Status
= PtpCrbWaitRegisterBits (
230 &CrbReg
->CrbControlStart
,
232 PTP_CRB_CONTROL_START
,
235 if (EFI_ERROR (Status
)) {
237 // Command Completion check timeout. Cancel the currently executing command by writing TPM_CRB_CTRL_CANCEL,
238 // Expect TPM_RC_CANCELLED or successfully completed response.
240 MmioWrite32((UINTN
)&CrbReg
->CrbControlCancel
, PTP_CRB_CONTROL_CANCEL
);
241 Status
= PtpCrbWaitRegisterBits (
242 &CrbReg
->CrbControlStart
,
244 PTP_CRB_CONTROL_START
,
247 MmioWrite32((UINTN
)&CrbReg
->CrbControlCancel
, 0);
249 if (EFI_ERROR(Status
)) {
251 // Still in Command Execution state. Try to goIdle, the behavior is agnostic.
253 Status
= EFI_DEVICE_ERROR
;
260 // Command Completion occurs after completion of a command (indicated by the
261 // TPM clearing TPM_CRB_CTRL_Start_x to 0) and before a write of a 1 by the
262 // software to Request.goIdle.
266 // Get response data header
268 for (Index
= 0; Index
< sizeof (TPM2_RESPONSE_HEADER
); Index
++) {
269 BufferOut
[Index
] = MmioRead8 ((UINTN
)&CrbReg
->CrbDataBuffer
[Index
]);
272 DEBUG ((EFI_D_VERBOSE
, "PtpCrbTpmCommand ReceiveHeader - "));
273 for (Index
= 0; Index
< sizeof (TPM2_RESPONSE_HEADER
); Index
++) {
274 DEBUG ((EFI_D_VERBOSE
, "%02x ", BufferOut
[Index
]));
276 DEBUG ((EFI_D_VERBOSE
, "\n"));
279 // Check the reponse data header (tag, parasize and returncode)
281 CopyMem (&Data16
, BufferOut
, sizeof (UINT16
));
282 // TPM2 should not use this RSP_COMMAND
283 if (SwapBytes16 (Data16
) == TPM_ST_RSP_COMMAND
) {
284 DEBUG ((EFI_D_ERROR
, "TPM2: TPM_ST_RSP error - %x\n", TPM_ST_RSP_COMMAND
));
285 Status
= EFI_UNSUPPORTED
;
289 CopyMem (&Data32
, (BufferOut
+ 2), sizeof (UINT32
));
290 TpmOutSize
= SwapBytes32 (Data32
);
291 if (*SizeOut
< TpmOutSize
) {
292 Status
= EFI_BUFFER_TOO_SMALL
;
295 *SizeOut
= TpmOutSize
;
297 // Continue reading the remaining data
299 for (Index
= sizeof (TPM2_RESPONSE_HEADER
); Index
< TpmOutSize
; Index
++) {
300 BufferOut
[Index
] = MmioRead8 ((UINTN
)&CrbReg
->CrbDataBuffer
[Index
]);
304 DEBUG ((EFI_D_VERBOSE
, "PtpCrbTpmCommand Receive - "));
305 for (Index
= 0; Index
< TpmOutSize
; Index
++) {
306 DEBUG ((EFI_D_VERBOSE
, "%02x ", BufferOut
[Index
]));
308 DEBUG ((EFI_D_VERBOSE
, "\n"));
313 // Idle is any time TPM_CRB_CTRL_STS_x.Status.goIdle is 1.
315 MmioWrite32((UINTN
)&CrbReg
->CrbControlRequest
, PTP_CRB_CONTROL_AREA_REQUEST_GO_IDLE
);
320 Send a command to TPM for execution and return response data.
322 @param[in] TisReg TPM register space base address.
323 @param[in] BufferIn Buffer for command data.
324 @param[in] SizeIn Size of command data.
325 @param[in, out] BufferOut Buffer for response data.
326 @param[in, out] SizeOut Size of response data.
328 @retval EFI_SUCCESS Operation completed successfully.
329 @retval EFI_BUFFER_TOO_SMALL Response data buffer is too small.
330 @retval EFI_DEVICE_ERROR Unexpected device behavior.
331 @retval EFI_UNSUPPORTED Unsupported TPM version
336 IN TIS_PC_REGISTERS_PTR TisReg
,
339 IN OUT UINT8
*BufferOut
,
340 IN OUT UINT32
*SizeOut
344 Get the control of TPM chip by sending requestUse command TIS_PC_ACC_RQUUSE
345 to ACCESS Register in the time of default TIS_TIMEOUT_A.
347 @param[in] TisReg Pointer to TIS register.
349 @retval EFI_SUCCESS Get the control of TPM chip.
350 @retval EFI_INVALID_PARAMETER TisReg is NULL.
351 @retval EFI_NOT_FOUND TPM chip doesn't exit.
352 @retval EFI_TIMEOUT Can't get the TPM control in time.
356 IN TIS_PC_REGISTERS_PTR TisReg
360 Return PTP interface type.
362 @param[in] Register Pointer to PTP register.
364 @return PTP interface type.
366 TPM2_PTP_INTERFACE_TYPE
367 Tpm2GetPtpInterface (
371 PTP_CRB_INTERFACE_IDENTIFIER InterfaceId
;
372 PTP_FIFO_INTERFACE_CAPABILITY InterfaceCapability
;
374 if (!Tpm2IsPtpPresence (Register
)) {
375 return Tpm2PtpInterfaceMax
;
378 // Check interface id
380 InterfaceId
.Uint32
= MmioRead32 ((UINTN
)&((PTP_CRB_REGISTERS
*)Register
)->InterfaceId
);
381 InterfaceCapability
.Uint32
= MmioRead32 ((UINTN
)&((PTP_FIFO_REGISTERS
*)Register
)->InterfaceCapability
);
383 if ((InterfaceId
.Bits
.InterfaceType
== PTP_INTERFACE_IDENTIFIER_INTERFACE_TYPE_CRB
) &&
384 (InterfaceId
.Bits
.InterfaceVersion
== PTP_INTERFACE_IDENTIFIER_INTERFACE_VERSION_CRB
) &&
385 (InterfaceId
.Bits
.CapCRB
!= 0)) {
386 return Tpm2PtpInterfaceCrb
;
388 if ((InterfaceId
.Bits
.InterfaceType
== PTP_INTERFACE_IDENTIFIER_INTERFACE_TYPE_FIFO
) &&
389 (InterfaceId
.Bits
.InterfaceVersion
== PTP_INTERFACE_IDENTIFIER_INTERFACE_VERSION_FIFO
) &&
390 (InterfaceId
.Bits
.CapFIFO
!= 0) &&
391 (InterfaceCapability
.Bits
.InterfaceVersion
== INTERFACE_CAPABILITY_INTERFACE_VERSION_PTP
)) {
392 return Tpm2PtpInterfaceFifo
;
394 return Tpm2PtpInterfaceTis
;
398 Dump PTP register information.
400 @param[in] Register Pointer to PTP register.
407 PTP_CRB_INTERFACE_IDENTIFIER InterfaceId
;
408 PTP_FIFO_INTERFACE_CAPABILITY InterfaceCapability
;
413 TPM2_PTP_INTERFACE_TYPE PtpInterface
;
415 if (!Tpm2IsPtpPresence (Register
)) {
419 InterfaceId
.Uint32
= MmioRead32 ((UINTN
)&((PTP_CRB_REGISTERS
*)Register
)->InterfaceId
);
420 InterfaceCapability
.Uint32
= MmioRead32 ((UINTN
)&((PTP_FIFO_REGISTERS
*)Register
)->InterfaceCapability
);
421 StatusEx
= MmioRead8 ((UINTN
)&((PTP_FIFO_REGISTERS
*)Register
)->StatusEx
);
424 // Dump InterfaceId Register for PTP
426 DEBUG ((EFI_D_INFO
, "InterfaceId - 0x%08x\n", InterfaceId
.Uint32
));
427 DEBUG ((EFI_D_INFO
, " InterfaceType - 0x%02x\n", InterfaceId
.Bits
.InterfaceType
));
428 if (InterfaceId
.Bits
.InterfaceType
!= PTP_INTERFACE_IDENTIFIER_INTERFACE_TYPE_TIS
) {
429 DEBUG ((EFI_D_INFO
, " InterfaceVersion - 0x%02x\n", InterfaceId
.Bits
.InterfaceVersion
));
430 DEBUG ((EFI_D_INFO
, " CapFIFO - 0x%x\n", InterfaceId
.Bits
.CapFIFO
));
431 DEBUG ((EFI_D_INFO
, " CapCRB - 0x%x\n", InterfaceId
.Bits
.CapCRB
));
435 // Dump Capability Register for TIS and FIFO
437 DEBUG ((EFI_D_INFO
, "InterfaceCapability - 0x%08x\n", InterfaceCapability
.Uint32
));
438 if ((InterfaceId
.Bits
.InterfaceType
== PTP_INTERFACE_IDENTIFIER_INTERFACE_TYPE_TIS
) ||
439 (InterfaceId
.Bits
.InterfaceType
== PTP_INTERFACE_IDENTIFIER_INTERFACE_TYPE_FIFO
)) {
440 DEBUG ((EFI_D_INFO
, " InterfaceVersion - 0x%x\n", InterfaceCapability
.Bits
.InterfaceVersion
));
444 // Dump StatusEx Register for PTP FIFO
446 DEBUG ((EFI_D_INFO
, "StatusEx - 0x%02x\n", StatusEx
));
447 if (InterfaceCapability
.Bits
.InterfaceVersion
== INTERFACE_CAPABILITY_INTERFACE_VERSION_PTP
) {
448 DEBUG ((EFI_D_INFO
, " TpmFamily - 0x%x\n", (StatusEx
& PTP_FIFO_STS_EX_TPM_FAMILY
) >> PTP_FIFO_STS_EX_TPM_FAMILY_OFFSET
));
454 PtpInterface
= PcdGet8(PcdActiveTpmInterfaceType
);
455 DEBUG ((EFI_D_INFO
, "PtpInterface - %x\n", PtpInterface
));
456 switch (PtpInterface
) {
457 case Tpm2PtpInterfaceCrb
:
458 Vid
= MmioRead16 ((UINTN
)&((PTP_CRB_REGISTERS
*)Register
)->Vid
);
459 Did
= MmioRead16 ((UINTN
)&((PTP_CRB_REGISTERS
*)Register
)->Did
);
460 Rid
= (UINT8
)InterfaceId
.Bits
.Rid
;
462 case Tpm2PtpInterfaceFifo
:
463 case Tpm2PtpInterfaceTis
:
464 Vid
= MmioRead16 ((UINTN
)&((PTP_FIFO_REGISTERS
*)Register
)->Vid
);
465 Did
= MmioRead16 ((UINTN
)&((PTP_FIFO_REGISTERS
*)Register
)->Did
);
466 Rid
= MmioRead8 ((UINTN
)&((PTP_FIFO_REGISTERS
*)Register
)->Rid
);
471 DEBUG ((EFI_D_INFO
, "VID - 0x%04x\n", Vid
));
472 DEBUG ((EFI_D_INFO
, "DID - 0x%04x\n", Did
));
473 DEBUG ((EFI_D_INFO
, "RID - 0x%02x\n", Rid
));
477 This service enables the sending of commands to the TPM2.
479 @param[in] InputParameterBlockSize Size of the TPM2 input parameter block.
480 @param[in] InputParameterBlock Pointer to the TPM2 input parameter block.
481 @param[in,out] OutputParameterBlockSize Size of the TPM2 output parameter block.
482 @param[in] OutputParameterBlock Pointer to the TPM2 output parameter block.
484 @retval EFI_SUCCESS The command byte stream was successfully sent to the device and a response was successfully received.
485 @retval EFI_DEVICE_ERROR The command was not successfully sent to the device or a response was not successfully received from the device.
486 @retval EFI_BUFFER_TOO_SMALL The output parameter block is too small.
491 IN UINT32 InputParameterBlockSize
,
492 IN UINT8
*InputParameterBlock
,
493 IN OUT UINT32
*OutputParameterBlockSize
,
494 IN UINT8
*OutputParameterBlock
497 TPM2_PTP_INTERFACE_TYPE PtpInterface
;
499 PtpInterface
= PcdGet8(PcdActiveTpmInterfaceType
);
500 switch (PtpInterface
) {
501 case Tpm2PtpInterfaceCrb
:
502 return PtpCrbTpmCommand (
503 (PTP_CRB_REGISTERS_PTR
) (UINTN
) PcdGet64 (PcdTpmBaseAddress
),
505 InputParameterBlockSize
,
506 OutputParameterBlock
,
507 OutputParameterBlockSize
509 case Tpm2PtpInterfaceFifo
:
510 case Tpm2PtpInterfaceTis
:
511 return Tpm2TisTpmCommand (
512 (TIS_PC_REGISTERS_PTR
) (UINTN
) PcdGet64 (PcdTpmBaseAddress
),
514 InputParameterBlockSize
,
515 OutputParameterBlock
,
516 OutputParameterBlockSize
519 return EFI_NOT_FOUND
;
524 This service requests use TPM2.
526 @retval EFI_SUCCESS Get the control of TPM2 chip.
527 @retval EFI_NOT_FOUND TPM2 not found.
528 @retval EFI_DEVICE_ERROR Unexpected device behavior.
536 TPM2_PTP_INTERFACE_TYPE PtpInterface
;
538 PtpInterface
= PcdGet8(PcdActiveTpmInterfaceType
);
539 switch (PtpInterface
) {
540 case Tpm2PtpInterfaceCrb
:
541 return PtpCrbRequestUseTpm ((PTP_CRB_REGISTERS_PTR
) (UINTN
) PcdGet64 (PcdTpmBaseAddress
));
542 case Tpm2PtpInterfaceFifo
:
543 case Tpm2PtpInterfaceTis
:
544 return TisPcRequestUseTpm ((TIS_PC_REGISTERS_PTR
) (UINTN
) PcdGet64 (PcdTpmBaseAddress
));
546 return EFI_NOT_FOUND
;