2 PTP (Platform TPM Profile) CRB (Command Response Buffer) interface used by dTPM2.0 library.
4 Copyright (c) 2015 - 2016, 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>
36 // Execution of the command may take from several seconds to minutes for certain
37 // commands, such as key generation.
39 #define PTP_TIMEOUT_MAX (90000 * 1000) // 90s
42 // Max TPM command/reponse length
44 #define TPMCMDBUFLENGTH 0x500
47 Check whether TPM PTP register exist.
49 @param[in] Reg Pointer to PTP register.
51 @retval TRUE TPM PTP exists.
52 @retval FALSE TPM PTP is not found.
61 RegRead
= MmioRead8 ((UINTN
)Reg
);
62 if (RegRead
== 0xFF) {
72 Check whether the value of a TPM chip register satisfies the input BIT setting.
74 @param[in] Register Address port of register to be checked.
75 @param[in] BitSet Check these data bits are set.
76 @param[in] BitClear Check these data bits are clear.
77 @param[in] TimeOut The max wait time (unit MicroSecond) when checking register.
79 @retval EFI_SUCCESS The register satisfies the check bit.
80 @retval EFI_TIMEOUT The register can't run into the expected status in time.
83 PtpCrbWaitRegisterBits (
93 for (WaitTime
= 0; WaitTime
< TimeOut
; WaitTime
+= 30){
94 RegRead
= MmioRead32 ((UINTN
)Register
);
95 if ((RegRead
& BitSet
) == BitSet
&& (RegRead
& BitClear
) == 0) {
98 MicroSecondDelay (30);
104 Get the control of TPM chip.
106 @param[in] CrbReg Pointer to CRB register.
108 @retval EFI_SUCCESS Get the control of TPM chip.
109 @retval EFI_INVALID_PARAMETER CrbReg is NULL.
110 @retval EFI_NOT_FOUND TPM chip doesn't exit.
111 @retval EFI_TIMEOUT Can't get the TPM control in time.
114 PtpCrbRequestUseTpm (
115 IN PTP_CRB_REGISTERS_PTR CrbReg
120 if (!Tpm2IsPtpPresence (CrbReg
)) {
121 return EFI_NOT_FOUND
;
124 MmioWrite32((UINTN
)&CrbReg
->LocalityControl
, PTP_CRB_LOCALITY_CONTROL_REQUEST_ACCESS
);
125 Status
= PtpCrbWaitRegisterBits (
126 &CrbReg
->LocalityStatus
,
127 PTP_CRB_LOCALITY_STATUS_GRANTED
,
135 Send a command to TPM for execution and return response data.
137 @param[in] CrbReg TPM register space base address.
138 @param[in] BufferIn Buffer for command data.
139 @param[in] SizeIn Size of command data.
140 @param[in, out] BufferOut Buffer for response data.
141 @param[in, out] SizeOut Size of response data.
143 @retval EFI_SUCCESS Operation completed successfully.
144 @retval EFI_BUFFER_TOO_SMALL Response data buffer is too small.
145 @retval EFI_DEVICE_ERROR Unexpected device behavior.
146 @retval EFI_UNSUPPORTED Unsupported TPM version
151 IN PTP_CRB_REGISTERS_PTR CrbReg
,
154 IN OUT UINT8
*BufferOut
,
155 IN OUT UINT32
*SizeOut
167 DEBUG ((EFI_D_VERBOSE
, "PtpCrbTpmCommand Send - "));
168 if (SizeIn
> 0x100) {
173 for (Index
= 0; Index
< DebugSize
; Index
++) {
174 DEBUG ((EFI_D_VERBOSE
, "%02x ", BufferIn
[Index
]));
176 if (DebugSize
!= SizeIn
) {
177 DEBUG ((EFI_D_VERBOSE
, "...... "));
178 for (Index
= SizeIn
- 0x20; Index
< SizeIn
; Index
++) {
179 DEBUG ((EFI_D_VERBOSE
, "%02x ", BufferIn
[Index
]));
182 DEBUG ((EFI_D_VERBOSE
, "\n"));
188 // Ready is any time the TPM is ready to receive a command, following a write
189 // of 1 by software to Request.cmdReady, as indicated by the Status field
190 // being cleared to 0.
192 MmioWrite32((UINTN
)&CrbReg
->CrbControlRequest
, PTP_CRB_CONTROL_AREA_REQUEST_COMMAND_READY
);
193 Status
= PtpCrbWaitRegisterBits (
194 &CrbReg
->CrbControlRequest
,
196 PTP_CRB_CONTROL_AREA_REQUEST_COMMAND_READY
,
199 if (EFI_ERROR (Status
)) {
200 Status
= EFI_DEVICE_ERROR
;
203 Status
= PtpCrbWaitRegisterBits (
204 &CrbReg
->CrbControlStatus
,
206 PTP_CRB_CONTROL_AREA_STATUS_TPM_IDLE
,
209 if (EFI_ERROR (Status
)) {
210 Status
= EFI_DEVICE_ERROR
;
216 // Command Reception occurs following a Ready state between the write of the
217 // first byte of a command to the Command Buffer and the receipt of a write
220 for (Index
= 0; Index
< SizeIn
; Index
++) {
221 MmioWrite8 ((UINTN
)&CrbReg
->CrbDataBuffer
[Index
], BufferIn
[Index
]);
223 MmioWrite32 ((UINTN
)&CrbReg
->CrbControlCommandAddressHigh
, (UINT32
)RShiftU64 ((UINTN
)CrbReg
->CrbDataBuffer
, 32));
224 MmioWrite32 ((UINTN
)&CrbReg
->CrbControlCommandAddressLow
, (UINT32
)(UINTN
)CrbReg
->CrbDataBuffer
);
225 MmioWrite32 ((UINTN
)&CrbReg
->CrbControlCommandSize
, sizeof(CrbReg
->CrbDataBuffer
));
227 MmioWrite64 ((UINTN
)&CrbReg
->CrbControlResponseAddrss
, (UINT32
)(UINTN
)CrbReg
->CrbDataBuffer
);
228 MmioWrite32 ((UINTN
)&CrbReg
->CrbControlResponseSize
, sizeof(CrbReg
->CrbDataBuffer
));
232 // Command Execution occurs after receipt of a 1 to Start and the TPM
233 // clearing Start to 0.
235 MmioWrite32((UINTN
)&CrbReg
->CrbControlStart
, PTP_CRB_CONTROL_START
);
236 Status
= PtpCrbWaitRegisterBits (
237 &CrbReg
->CrbControlStart
,
239 PTP_CRB_CONTROL_START
,
242 if (EFI_ERROR (Status
)) {
243 Status
= EFI_DEVICE_ERROR
;
249 // Command Completion occurs after completion of a command (indicated by the
250 // TPM clearing TPM_CRB_CTRL_Start_x to 0) and before a write of a 1 by the
251 // software to Request.goIdle.
255 // Get response data header
257 for (Index
= 0; Index
< sizeof (TPM2_RESPONSE_HEADER
); Index
++) {
258 BufferOut
[Index
] = MmioRead8 ((UINTN
)&CrbReg
->CrbDataBuffer
[Index
]);
261 DEBUG ((EFI_D_VERBOSE
, "PtpCrbTpmCommand ReceiveHeader - "));
262 for (Index
= 0; Index
< sizeof (TPM2_RESPONSE_HEADER
); Index
++) {
263 DEBUG ((EFI_D_VERBOSE
, "%02x ", BufferOut
[Index
]));
265 DEBUG ((EFI_D_VERBOSE
, "\n"));
268 // Check the reponse data header (tag, parasize and returncode)
270 CopyMem (&Data16
, BufferOut
, sizeof (UINT16
));
271 // TPM2 should not use this RSP_COMMAND
272 if (SwapBytes16 (Data16
) == TPM_ST_RSP_COMMAND
) {
273 DEBUG ((EFI_D_ERROR
, "TPM2: TPM_ST_RSP error - %x\n", TPM_ST_RSP_COMMAND
));
274 Status
= EFI_UNSUPPORTED
;
278 CopyMem (&Data32
, (BufferOut
+ 2), sizeof (UINT32
));
279 TpmOutSize
= SwapBytes32 (Data32
);
280 if (*SizeOut
< TpmOutSize
) {
281 Status
= EFI_BUFFER_TOO_SMALL
;
284 *SizeOut
= TpmOutSize
;
286 // Continue reading the remaining data
288 for (Index
= sizeof (TPM2_RESPONSE_HEADER
); Index
< TpmOutSize
; Index
++) {
289 BufferOut
[Index
] = MmioRead8 ((UINTN
)&CrbReg
->CrbDataBuffer
[Index
]);
293 DEBUG ((EFI_D_VERBOSE
, "PtpCrbTpmCommand Receive - "));
294 for (Index
= 0; Index
< TpmOutSize
; Index
++) {
295 DEBUG ((EFI_D_VERBOSE
, "%02x ", BufferOut
[Index
]));
297 DEBUG ((EFI_D_VERBOSE
, "\n"));
302 // Idle is any time TPM_CRB_CTRL_STS_x.Status.goIdle is 1.
304 MmioWrite32((UINTN
)&CrbReg
->CrbControlRequest
, PTP_CRB_CONTROL_AREA_REQUEST_GO_IDLE
);
309 Send a command to TPM for execution and return response data.
311 @param[in] TisReg TPM register space base address.
312 @param[in] BufferIn Buffer for command data.
313 @param[in] SizeIn Size of command data.
314 @param[in, out] BufferOut Buffer for response data.
315 @param[in, out] SizeOut Size of response data.
317 @retval EFI_SUCCESS Operation completed successfully.
318 @retval EFI_BUFFER_TOO_SMALL Response data buffer is too small.
319 @retval EFI_DEVICE_ERROR Unexpected device behavior.
320 @retval EFI_UNSUPPORTED Unsupported TPM version
325 IN TIS_PC_REGISTERS_PTR TisReg
,
328 IN OUT UINT8
*BufferOut
,
329 IN OUT UINT32
*SizeOut
333 Get the control of TPM chip by sending requestUse command TIS_PC_ACC_RQUUSE
334 to ACCESS Register in the time of default TIS_TIMEOUT_A.
336 @param[in] TisReg Pointer to TIS register.
338 @retval EFI_SUCCESS Get the control of TPM chip.
339 @retval EFI_INVALID_PARAMETER TisReg is NULL.
340 @retval EFI_NOT_FOUND TPM chip doesn't exit.
341 @retval EFI_TIMEOUT Can't get the TPM control in time.
345 IN TIS_PC_REGISTERS_PTR TisReg
349 Return PTP interface type.
351 @param[in] Register Pointer to PTP register.
353 @return PTP interface type.
356 Tpm2GetPtpInterface (
360 PTP_CRB_INTERFACE_IDENTIFIER InterfaceId
;
361 PTP_FIFO_INTERFACE_CAPABILITY InterfaceCapability
;
363 if (!Tpm2IsPtpPresence (Register
)) {
364 return PtpInterfaceMax
;
367 // Check interface id
369 InterfaceId
.Uint32
= MmioRead32 ((UINTN
)&((PTP_CRB_REGISTERS
*)Register
)->InterfaceId
);
370 InterfaceCapability
.Uint32
= MmioRead32 ((UINTN
)&((PTP_FIFO_REGISTERS
*)Register
)->InterfaceCapability
);
372 if ((InterfaceId
.Bits
.InterfaceType
== PTP_INTERFACE_IDENTIFIER_INTERFACE_TYPE_CRB
) &&
373 (InterfaceId
.Bits
.InterfaceVersion
== PTP_INTERFACE_IDENTIFIER_INTERFACE_VERSION_CRB
) &&
374 (InterfaceId
.Bits
.CapCRB
!= 0)) {
375 return PtpInterfaceCrb
;
377 if ((InterfaceId
.Bits
.InterfaceType
== PTP_INTERFACE_IDENTIFIER_INTERFACE_TYPE_FIFO
) &&
378 (InterfaceId
.Bits
.InterfaceVersion
== PTP_INTERFACE_IDENTIFIER_INTERFACE_VERSION_FIFO
) &&
379 (InterfaceId
.Bits
.CapFIFO
!= 0) &&
380 (InterfaceCapability
.Bits
.InterfaceVersion
== INTERFACE_CAPABILITY_INTERFACE_VERSION_PTP
)) {
381 return PtpInterfaceFifo
;
383 return PtpInterfaceTis
;
387 Dump PTP register information.
389 @param[in] Register Pointer to PTP register.
396 PTP_CRB_INTERFACE_IDENTIFIER InterfaceId
;
397 PTP_FIFO_INTERFACE_CAPABILITY InterfaceCapability
;
402 PTP_INTERFACE_TYPE PtpInterface
;
404 if (!Tpm2IsPtpPresence (Register
)) {
408 InterfaceId
.Uint32
= MmioRead32 ((UINTN
)&((PTP_CRB_REGISTERS
*)Register
)->InterfaceId
);
409 InterfaceCapability
.Uint32
= MmioRead32 ((UINTN
)&((PTP_FIFO_REGISTERS
*)Register
)->InterfaceCapability
);
410 StatusEx
= MmioRead8 ((UINTN
)&((PTP_FIFO_REGISTERS
*)Register
)->StatusEx
);
413 // Dump InterfaceId Register for PTP
415 DEBUG ((EFI_D_INFO
, "InterfaceId - 0x%08x\n", InterfaceId
.Uint32
));
416 DEBUG ((EFI_D_INFO
, " InterfaceType - 0x%02x\n", InterfaceId
.Bits
.InterfaceType
));
417 if (InterfaceId
.Bits
.InterfaceType
!= PTP_INTERFACE_IDENTIFIER_INTERFACE_TYPE_TIS
) {
418 DEBUG ((EFI_D_INFO
, " InterfaceVersion - 0x%02x\n", InterfaceId
.Bits
.InterfaceVersion
));
419 DEBUG ((EFI_D_INFO
, " CapFIFO - 0x%x\n", InterfaceId
.Bits
.CapFIFO
));
420 DEBUG ((EFI_D_INFO
, " CapCRB - 0x%x\n", InterfaceId
.Bits
.CapCRB
));
424 // Dump Capability Register for TIS and FIFO
426 DEBUG ((EFI_D_INFO
, "InterfaceCapability - 0x%08x\n", InterfaceCapability
.Uint32
));
427 if ((InterfaceId
.Bits
.InterfaceType
== PTP_INTERFACE_IDENTIFIER_INTERFACE_TYPE_TIS
) ||
428 (InterfaceId
.Bits
.InterfaceType
== PTP_INTERFACE_IDENTIFIER_INTERFACE_TYPE_FIFO
)) {
429 DEBUG ((EFI_D_INFO
, " InterfaceVersion - 0x%x\n", InterfaceCapability
.Bits
.InterfaceVersion
));
433 // Dump StatusEx Register for PTP FIFO
435 DEBUG ((EFI_D_INFO
, "StatusEx - 0x%02x\n", StatusEx
));
436 if (InterfaceCapability
.Bits
.InterfaceVersion
== INTERFACE_CAPABILITY_INTERFACE_VERSION_PTP
) {
437 DEBUG ((EFI_D_INFO
, " TpmFamily - 0x%x\n", (StatusEx
& PTP_FIFO_STS_EX_TPM_FAMILY
) >> PTP_FIFO_STS_EX_TPM_FAMILY_OFFSET
));
443 PtpInterface
= Tpm2GetPtpInterface (Register
);
444 DEBUG ((EFI_D_INFO
, "PtpInterface - %x\n", PtpInterface
));
445 switch (PtpInterface
) {
446 case PtpInterfaceCrb
:
447 Vid
= MmioRead16 ((UINTN
)&((PTP_CRB_REGISTERS
*)Register
)->Vid
);
448 Did
= MmioRead16 ((UINTN
)&((PTP_CRB_REGISTERS
*)Register
)->Did
);
449 Rid
= (UINT8
)InterfaceId
.Bits
.Rid
;
451 case PtpInterfaceFifo
:
452 case PtpInterfaceTis
:
453 Vid
= MmioRead16 ((UINTN
)&((PTP_FIFO_REGISTERS
*)Register
)->Vid
);
454 Did
= MmioRead16 ((UINTN
)&((PTP_FIFO_REGISTERS
*)Register
)->Did
);
455 Rid
= MmioRead8 ((UINTN
)&((PTP_FIFO_REGISTERS
*)Register
)->Rid
);
460 DEBUG ((EFI_D_INFO
, "VID - 0x%04x\n", Vid
));
461 DEBUG ((EFI_D_INFO
, "DID - 0x%04x\n", Did
));
462 DEBUG ((EFI_D_INFO
, "RID - 0x%02x\n", Rid
));
466 This service enables the sending of commands to the TPM2.
468 @param[in] InputParameterBlockSize Size of the TPM2 input parameter block.
469 @param[in] InputParameterBlock Pointer to the TPM2 input parameter block.
470 @param[in,out] OutputParameterBlockSize Size of the TPM2 output parameter block.
471 @param[in] OutputParameterBlock Pointer to the TPM2 output parameter block.
473 @retval EFI_SUCCESS The command byte stream was successfully sent to the device and a response was successfully received.
474 @retval EFI_DEVICE_ERROR The command was not successfully sent to the device or a response was not successfully received from the device.
475 @retval EFI_BUFFER_TOO_SMALL The output parameter block is too small.
480 IN UINT32 InputParameterBlockSize
,
481 IN UINT8
*InputParameterBlock
,
482 IN OUT UINT32
*OutputParameterBlockSize
,
483 IN UINT8
*OutputParameterBlock
486 PTP_INTERFACE_TYPE PtpInterface
;
488 PtpInterface
= Tpm2GetPtpInterface ((VOID
*) (UINTN
) PcdGet64 (PcdTpmBaseAddress
));
489 switch (PtpInterface
) {
490 case PtpInterfaceCrb
:
491 return PtpCrbTpmCommand (
492 (PTP_CRB_REGISTERS_PTR
) (UINTN
) PcdGet64 (PcdTpmBaseAddress
),
494 InputParameterBlockSize
,
495 OutputParameterBlock
,
496 OutputParameterBlockSize
498 case PtpInterfaceFifo
:
499 case PtpInterfaceTis
:
500 return Tpm2TisTpmCommand (
501 (TIS_PC_REGISTERS_PTR
) (UINTN
) PcdGet64 (PcdTpmBaseAddress
),
503 InputParameterBlockSize
,
504 OutputParameterBlock
,
505 OutputParameterBlockSize
508 return EFI_NOT_FOUND
;
513 This service requests use TPM2.
515 @retval EFI_SUCCESS Get the control of TPM2 chip.
516 @retval EFI_NOT_FOUND TPM2 not found.
517 @retval EFI_DEVICE_ERROR Unexpected device behavior.
525 PTP_INTERFACE_TYPE PtpInterface
;
527 PtpInterface
= Tpm2GetPtpInterface ((VOID
*) (UINTN
) PcdGet64 (PcdTpmBaseAddress
));
528 switch (PtpInterface
) {
529 case PtpInterfaceCrb
:
530 return PtpCrbRequestUseTpm ((PTP_CRB_REGISTERS_PTR
) (UINTN
) PcdGet64 (PcdTpmBaseAddress
));
531 case PtpInterfaceFifo
:
532 case PtpInterfaceTis
:
533 return TisPcRequestUseTpm ((TIS_PC_REGISTERS_PTR
) (UINTN
) PcdGet64 (PcdTpmBaseAddress
));
535 return EFI_NOT_FOUND
;