Tpm2InstanceLibDTpm.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /** @file
  2. This library is TPM2 DTPM instance.
  3. It can be registered to Tpm2 Device router, to be active TPM2 engine,
  4. based on platform setting.
  5. Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved. <BR>
  6. SPDX-License-Identifier: BSD-2-Clause-Patent
  7. **/
  8. #include <Library/BaseLib.h>
  9. #include <Library/BaseMemoryLib.h>
  10. #include <Library/DebugLib.h>
  11. #include <Library/Tpm2DeviceLib.h>
  12. #include <Library/PcdLib.h>
  13. #include <Guid/TpmInstance.h>
  14. /**
  15. Return PTP interface type.
  16. @param[in] Register Pointer to PTP register.
  17. @return PTP interface type.
  18. **/
  19. TPM2_PTP_INTERFACE_TYPE
  20. Tpm2GetPtpInterface (
  21. IN VOID *Register
  22. );
  23. /**
  24. Return PTP CRB interface IdleByPass state.
  25. @param[in] Register Pointer to PTP register.
  26. @return PTP CRB interface IdleByPass state.
  27. **/
  28. UINT8
  29. Tpm2GetIdleByPass (
  30. IN VOID *Register
  31. );
  32. /**
  33. Dump PTP register information.
  34. @param[in] Register Pointer to PTP register.
  35. **/
  36. VOID
  37. DumpPtpInfo (
  38. IN VOID *Register
  39. );
  40. /**
  41. This service enables the sending of commands to the TPM2.
  42. @param[in] InputParameterBlockSize Size of the TPM2 input parameter block.
  43. @param[in] InputParameterBlock Pointer to the TPM2 input parameter block.
  44. @param[in,out] OutputParameterBlockSize Size of the TPM2 output parameter block.
  45. @param[in] OutputParameterBlock Pointer to the TPM2 output parameter block.
  46. @retval EFI_SUCCESS The command byte stream was successfully sent to the device and a response was successfully received.
  47. @retval EFI_DEVICE_ERROR The command was not successfully sent to the device or a response was not successfully received from the device.
  48. @retval EFI_BUFFER_TOO_SMALL The output parameter block is too small.
  49. **/
  50. EFI_STATUS
  51. EFIAPI
  52. DTpm2SubmitCommand (
  53. IN UINT32 InputParameterBlockSize,
  54. IN UINT8 *InputParameterBlock,
  55. IN OUT UINT32 *OutputParameterBlockSize,
  56. IN UINT8 *OutputParameterBlock
  57. );
  58. /**
  59. This service requests use TPM2.
  60. @retval EFI_SUCCESS Get the control of TPM2 chip.
  61. @retval EFI_NOT_FOUND TPM2 not found.
  62. @retval EFI_DEVICE_ERROR Unexpected device behavior.
  63. **/
  64. EFI_STATUS
  65. EFIAPI
  66. DTpm2RequestUseTpm (
  67. VOID
  68. );
  69. TPM2_DEVICE_INTERFACE mDTpm2InternalTpm2Device = {
  70. TPM_DEVICE_INTERFACE_TPM20_DTPM,
  71. DTpm2SubmitCommand,
  72. DTpm2RequestUseTpm,
  73. };
  74. /**
  75. The function register DTPM2.0 instance and caches current active TPM interface type.
  76. @retval EFI_SUCCESS DTPM2.0 instance is registered, or system does not support register DTPM2.0 instance
  77. **/
  78. EFI_STATUS
  79. EFIAPI
  80. Tpm2InstanceLibDTpmConstructor (
  81. VOID
  82. )
  83. {
  84. EFI_STATUS Status;
  85. TPM2_PTP_INTERFACE_TYPE PtpInterface;
  86. UINT8 IdleByPass;
  87. Status = Tpm2RegisterTpm2DeviceLib (&mDTpm2InternalTpm2Device);
  88. if ((Status == EFI_SUCCESS) || (Status == EFI_UNSUPPORTED)) {
  89. //
  90. // Unsupported means platform policy does not need this instance enabled.
  91. //
  92. if (Status == EFI_SUCCESS) {
  93. //
  94. // Cache current active TpmInterfaceType only when needed
  95. //
  96. if (PcdGet8(PcdActiveTpmInterfaceType) == 0xFF) {
  97. PtpInterface = Tpm2GetPtpInterface ((VOID *) (UINTN) PcdGet64 (PcdTpmBaseAddress));
  98. PcdSet8S(PcdActiveTpmInterfaceType, PtpInterface);
  99. }
  100. if (PcdGet8(PcdActiveTpmInterfaceType) == Tpm2PtpInterfaceCrb && PcdGet8(PcdCRBIdleByPass) == 0xFF) {
  101. IdleByPass = Tpm2GetIdleByPass((VOID *) (UINTN) PcdGet64 (PcdTpmBaseAddress));
  102. PcdSet8S(PcdCRBIdleByPass, IdleByPass);
  103. }
  104. DumpPtpInfo ((VOID *) (UINTN) PcdGet64 (PcdTpmBaseAddress));
  105. }
  106. return EFI_SUCCESS;
  107. }
  108. return Status;
  109. }