Tcg2ConfigPeim.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /** @file
  2. The module entry point for Tcg2 configuration module.
  3. Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include <PiPei.h>
  7. #include <Guid/TpmInstance.h>
  8. #include <Library/BaseLib.h>
  9. #include <Library/BaseMemoryLib.h>
  10. #include <Library/DebugLib.h>
  11. #include <Library/MemoryAllocationLib.h>
  12. #include <Library/PeiServicesLib.h>
  13. #include <Library/PcdLib.h>
  14. #include <Ppi/ReadOnlyVariable2.h>
  15. #include <Ppi/TpmInitialized.h>
  16. #include <Protocol/Tcg2Protocol.h>
  17. #include "Tcg2ConfigNvData.h"
  18. #include "Tcg2Internal.h"
  19. TPM_INSTANCE_ID mTpmInstanceId[] = TPM_INSTANCE_ID_LIST;
  20. CONST EFI_PEI_PPI_DESCRIPTOR gTpmSelectedPpi = {
  21. (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
  22. &gEfiTpmDeviceSelectedGuid,
  23. NULL
  24. };
  25. EFI_PEI_PPI_DESCRIPTOR mTpmInitializationDonePpiList = {
  26. EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST,
  27. &gPeiTpmInitializationDonePpiGuid,
  28. NULL
  29. };
  30. /**
  31. This routine check both SetupVariable and real TPM device, and return final TpmDevice configuration.
  32. @param SetupTpmDevice TpmDevice configuration in setup driver
  33. @return TpmDevice configuration
  34. **/
  35. UINT8
  36. DetectTpmDevice (
  37. IN UINT8 SetupTpmDevice
  38. );
  39. /**
  40. The entry point for Tcg2 configuration driver.
  41. @param FileHandle Handle of the file being invoked.
  42. @param PeiServices Describes the list of possible PEI Services.
  43. @retval EFI_SUCCESS Convert variable to PCD successfully.
  44. @retval Others Fail to convert variable to PCD.
  45. **/
  46. EFI_STATUS
  47. EFIAPI
  48. Tcg2ConfigPeimEntryPoint (
  49. IN EFI_PEI_FILE_HANDLE FileHandle,
  50. IN CONST EFI_PEI_SERVICES **PeiServices
  51. )
  52. {
  53. UINTN Size;
  54. EFI_STATUS Status;
  55. EFI_STATUS Status2;
  56. EFI_PEI_READ_ONLY_VARIABLE2_PPI *VariablePpi;
  57. TCG2_CONFIGURATION Tcg2Configuration;
  58. UINTN Index;
  59. UINT8 TpmDevice;
  60. Status = PeiServicesLocatePpi (&gEfiPeiReadOnlyVariable2PpiGuid, 0, NULL, (VOID **) &VariablePpi);
  61. ASSERT_EFI_ERROR (Status);
  62. Size = sizeof(Tcg2Configuration);
  63. Status = VariablePpi->GetVariable (
  64. VariablePpi,
  65. TCG2_STORAGE_NAME,
  66. &gTcg2ConfigFormSetGuid,
  67. NULL,
  68. &Size,
  69. &Tcg2Configuration
  70. );
  71. if (EFI_ERROR (Status)) {
  72. //
  73. // Variable not ready, set default value
  74. //
  75. Tcg2Configuration.TpmDevice = TPM_DEVICE_DEFAULT;
  76. }
  77. //
  78. // Validation
  79. //
  80. if ((Tcg2Configuration.TpmDevice > TPM_DEVICE_MAX) || (Tcg2Configuration.TpmDevice < TPM_DEVICE_MIN)) {
  81. Tcg2Configuration.TpmDevice = TPM_DEVICE_DEFAULT;
  82. }
  83. //
  84. // Although we have SetupVariable info, we still need detect TPM device manually.
  85. //
  86. DEBUG ((EFI_D_INFO, "Tcg2Configuration.TpmDevice from Setup: %x\n", Tcg2Configuration.TpmDevice));
  87. if (PcdGetBool (PcdTpmAutoDetection)) {
  88. TpmDevice = DetectTpmDevice (Tcg2Configuration.TpmDevice);
  89. DEBUG ((EFI_D_INFO, "TpmDevice final: %x\n", TpmDevice));
  90. if (TpmDevice != TPM_DEVICE_NULL) {
  91. Tcg2Configuration.TpmDevice = TpmDevice;
  92. }
  93. } else {
  94. TpmDevice = Tcg2Configuration.TpmDevice;
  95. }
  96. //
  97. // Convert variable to PCD.
  98. // This is work-around because there is no guarantee DynamicHiiPcd can return correct value in DXE phase.
  99. // Using DynamicPcd instead.
  100. //
  101. // NOTE: Tcg2Configuration variable contains the desired TpmDevice type,
  102. // while PcdTpmInstanceGuid PCD contains the real detected TpmDevice type
  103. //
  104. for (Index = 0; Index < sizeof(mTpmInstanceId)/sizeof(mTpmInstanceId[0]); Index++) {
  105. if (TpmDevice == mTpmInstanceId[Index].TpmDevice) {
  106. Size = sizeof(mTpmInstanceId[Index].TpmInstanceGuid);
  107. Status = PcdSetPtrS (PcdTpmInstanceGuid, &Size, &mTpmInstanceId[Index].TpmInstanceGuid);
  108. ASSERT_EFI_ERROR (Status);
  109. DEBUG ((EFI_D_INFO, "TpmDevice PCD: %g\n", &mTpmInstanceId[Index].TpmInstanceGuid));
  110. break;
  111. }
  112. }
  113. //
  114. // Selection done
  115. //
  116. Status = PeiServicesInstallPpi (&gTpmSelectedPpi);
  117. ASSERT_EFI_ERROR (Status);
  118. //
  119. // Even if no TPM is selected or detected, we still need intall TpmInitializationDonePpi.
  120. // Because TcgPei or Tcg2Pei will not run, but we still need a way to notify other driver.
  121. // Other driver can know TPM initialization state by TpmInitializedPpi.
  122. //
  123. if (CompareGuid (PcdGetPtr(PcdTpmInstanceGuid), &gEfiTpmDeviceInstanceNoneGuid)) {
  124. Status2 = PeiServicesInstallPpi (&mTpmInitializationDonePpiList);
  125. ASSERT_EFI_ERROR (Status2);
  126. }
  127. return Status;
  128. }