Tpm12GetCapability.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /** @file
  2. Implement TPM1.2 Get Capabilities related commands.
  3. Copyright (c) 2016 - 2018, Intel Corporation. All rights reserved. <BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include <PiPei.h>
  7. #include <Library/Tpm12CommandLib.h>
  8. #include <Library/BaseLib.h>
  9. #include <Library/DebugLib.h>
  10. #include <Library/BaseMemoryLib.h>
  11. #include <Library/Tpm12DeviceLib.h>
  12. #pragma pack(1)
  13. typedef struct {
  14. TPM_RQU_COMMAND_HDR Hdr;
  15. UINT32 Capability;
  16. UINT32 CapabilityFlagSize;
  17. UINT32 CapabilityFlag;
  18. } TPM_CMD_GET_CAPABILITY;
  19. typedef struct {
  20. TPM_RSP_COMMAND_HDR Hdr;
  21. UINT32 ResponseSize;
  22. TPM_PERMANENT_FLAGS Flags;
  23. } TPM_RSP_GET_CAPABILITY_PERMANENT_FLAGS;
  24. typedef struct {
  25. TPM_RSP_COMMAND_HDR Hdr;
  26. UINT32 ResponseSize;
  27. TPM_STCLEAR_FLAGS Flags;
  28. } TPM_RSP_GET_CAPABILITY_STCLEAR_FLAGS;
  29. #pragma pack()
  30. /**
  31. Get TPM capability permanent flags.
  32. @param[out] TpmPermanentFlags Pointer to the buffer for returned flag structure.
  33. @retval EFI_SUCCESS Operation completed successfully.
  34. @retval EFI_TIMEOUT The register can't run into the expected status in time.
  35. @retval EFI_BUFFER_TOO_SMALL Response data buffer is too small.
  36. @retval EFI_DEVICE_ERROR Unexpected device behavior.
  37. **/
  38. EFI_STATUS
  39. EFIAPI
  40. Tpm12GetCapabilityFlagPermanent (
  41. OUT TPM_PERMANENT_FLAGS *TpmPermanentFlags
  42. )
  43. {
  44. EFI_STATUS Status;
  45. TPM_CMD_GET_CAPABILITY Command;
  46. TPM_RSP_GET_CAPABILITY_PERMANENT_FLAGS Response;
  47. UINT32 Length;
  48. //
  49. // send Tpm command TPM_ORD_GetCapability
  50. //
  51. Command.Hdr.tag = SwapBytes16 (TPM_TAG_RQU_COMMAND);
  52. Command.Hdr.paramSize = SwapBytes32 (sizeof (Command));
  53. Command.Hdr.ordinal = SwapBytes32 (TPM_ORD_GetCapability);
  54. Command.Capability = SwapBytes32 (TPM_CAP_FLAG);
  55. Command.CapabilityFlagSize = SwapBytes32 (sizeof (TPM_CAP_FLAG_PERMANENT));
  56. Command.CapabilityFlag = SwapBytes32 (TPM_CAP_FLAG_PERMANENT);
  57. Length = sizeof (Response);
  58. Status = Tpm12SubmitCommand (sizeof (Command), (UINT8 *)&Command, &Length, (UINT8 *)&Response);
  59. if (EFI_ERROR (Status)) {
  60. return Status;
  61. }
  62. if (SwapBytes32 (Response.Hdr.returnCode) != TPM_SUCCESS) {
  63. DEBUG ((DEBUG_ERROR, "Tpm12GetCapabilityFlagPermanent: Response Code error! 0x%08x\r\n", SwapBytes32 (Response.Hdr.returnCode)));
  64. return EFI_DEVICE_ERROR;
  65. }
  66. ZeroMem (TpmPermanentFlags, sizeof (*TpmPermanentFlags));
  67. CopyMem (TpmPermanentFlags, &Response.Flags, MIN (sizeof (*TpmPermanentFlags), SwapBytes32 (Response.ResponseSize)));
  68. return Status;
  69. }
  70. /**
  71. Get TPM capability volatile flags.
  72. @param[out] VolatileFlags Pointer to the buffer for returned flag structure.
  73. @retval EFI_SUCCESS Operation completed successfully.
  74. @retval EFI_DEVICE_ERROR The command was unsuccessful.
  75. **/
  76. EFI_STATUS
  77. EFIAPI
  78. Tpm12GetCapabilityFlagVolatile (
  79. OUT TPM_STCLEAR_FLAGS *VolatileFlags
  80. )
  81. {
  82. EFI_STATUS Status;
  83. TPM_CMD_GET_CAPABILITY Command;
  84. TPM_RSP_GET_CAPABILITY_STCLEAR_FLAGS Response;
  85. UINT32 Length;
  86. //
  87. // send Tpm command TPM_ORD_GetCapability
  88. //
  89. Command.Hdr.tag = SwapBytes16 (TPM_TAG_RQU_COMMAND);
  90. Command.Hdr.paramSize = SwapBytes32 (sizeof (Command));
  91. Command.Hdr.ordinal = SwapBytes32 (TPM_ORD_GetCapability);
  92. Command.Capability = SwapBytes32 (TPM_CAP_FLAG);
  93. Command.CapabilityFlagSize = SwapBytes32 (sizeof (TPM_CAP_FLAG_VOLATILE));
  94. Command.CapabilityFlag = SwapBytes32 (TPM_CAP_FLAG_VOLATILE);
  95. Length = sizeof (Response);
  96. Status = Tpm12SubmitCommand (sizeof (Command), (UINT8 *)&Command, &Length, (UINT8 *)&Response);
  97. if (EFI_ERROR (Status)) {
  98. return Status;
  99. }
  100. if (SwapBytes32 (Response.Hdr.returnCode) != TPM_SUCCESS) {
  101. DEBUG ((DEBUG_ERROR, "Tpm12GetCapabilityFlagVolatile: Response Code error! 0x%08x\r\n", SwapBytes32 (Response.Hdr.returnCode)));
  102. return EFI_DEVICE_ERROR;
  103. }
  104. ZeroMem (VolatileFlags, sizeof (*VolatileFlags));
  105. CopyMem (VolatileFlags, &Response.Flags, MIN (sizeof (*VolatileFlags), SwapBytes32 (Response.ResponseSize)));
  106. return Status;
  107. }