Tpm12NvStorage.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /** @file
  2. Implement TPM1.2 NV storage related command.
  3. Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved. <BR>
  4. (C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>
  5. SPDX-License-Identifier: BSD-2-Clause-Patent
  6. **/
  7. #include <PiPei.h>
  8. #include <Library/Tpm12CommandLib.h>
  9. #include <Library/BaseLib.h>
  10. #include <Library/DebugLib.h>
  11. #include <Library/BaseMemoryLib.h>
  12. #include <Library/Tpm12DeviceLib.h>
  13. //
  14. // Max TPM NV value length
  15. //
  16. #define TPMNVVALUELENGTH 1024
  17. #pragma pack(1)
  18. typedef struct {
  19. TPM_RQU_COMMAND_HDR Hdr;
  20. TPM12_NV_DATA_PUBLIC PubInfo;
  21. TPM_ENCAUTH EncAuth;
  22. } TPM_CMD_NV_DEFINE_SPACE;
  23. typedef struct {
  24. TPM_RQU_COMMAND_HDR Hdr;
  25. TPM_NV_INDEX NvIndex;
  26. UINT32 Offset;
  27. UINT32 DataSize;
  28. } TPM_CMD_NV_READ_VALUE;
  29. typedef struct {
  30. TPM_RSP_COMMAND_HDR Hdr;
  31. UINT32 DataSize;
  32. UINT8 Data[TPMNVVALUELENGTH];
  33. } TPM_RSP_NV_READ_VALUE;
  34. typedef struct {
  35. TPM_RQU_COMMAND_HDR Hdr;
  36. TPM_NV_INDEX NvIndex;
  37. UINT32 Offset;
  38. UINT32 DataSize;
  39. UINT8 Data[TPMNVVALUELENGTH];
  40. } TPM_CMD_NV_WRITE_VALUE;
  41. #pragma pack()
  42. /**
  43. Send NV DefineSpace command to TPM1.2.
  44. @param PubInfo The public parameters of the NV area.
  45. @param EncAuth The encrypted AuthData, only valid if the attributes require subsequent authorization.
  46. @retval EFI_SUCCESS Operation completed successfully.
  47. @retval EFI_DEVICE_ERROR Unexpected device behavior.
  48. **/
  49. EFI_STATUS
  50. EFIAPI
  51. Tpm12NvDefineSpace (
  52. IN TPM12_NV_DATA_PUBLIC *PubInfo,
  53. IN TPM_ENCAUTH *EncAuth
  54. )
  55. {
  56. EFI_STATUS Status;
  57. TPM_CMD_NV_DEFINE_SPACE Command;
  58. TPM_RSP_COMMAND_HDR Response;
  59. UINT32 Length;
  60. //
  61. // send Tpm command TPM_ORD_NV_DefineSpace
  62. //
  63. Command.Hdr.tag = SwapBytes16 (TPM_TAG_RQU_COMMAND);
  64. Command.Hdr.paramSize = SwapBytes32 (sizeof (Command));
  65. Command.Hdr.ordinal = SwapBytes32 (TPM_ORD_NV_DefineSpace);
  66. Command.PubInfo.tag = SwapBytes16 (PubInfo->tag);
  67. Command.PubInfo.nvIndex = SwapBytes32 (PubInfo->nvIndex);
  68. Command.PubInfo.pcrInfoRead.pcrSelection.sizeOfSelect = SwapBytes16 (PubInfo->pcrInfoRead.pcrSelection.sizeOfSelect);
  69. Command.PubInfo.pcrInfoRead.pcrSelection.pcrSelect[0] = PubInfo->pcrInfoRead.pcrSelection.pcrSelect[0];
  70. Command.PubInfo.pcrInfoRead.pcrSelection.pcrSelect[1] = PubInfo->pcrInfoRead.pcrSelection.pcrSelect[1];
  71. Command.PubInfo.pcrInfoRead.pcrSelection.pcrSelect[2] = PubInfo->pcrInfoRead.pcrSelection.pcrSelect[2];
  72. Command.PubInfo.pcrInfoRead.localityAtRelease = PubInfo->pcrInfoRead.localityAtRelease;
  73. CopyMem (&Command.PubInfo.pcrInfoRead.digestAtRelease, &PubInfo->pcrInfoRead.digestAtRelease, sizeof (PubInfo->pcrInfoRead.digestAtRelease));
  74. Command.PubInfo.pcrInfoWrite.pcrSelection.sizeOfSelect = SwapBytes16 (PubInfo->pcrInfoWrite.pcrSelection.sizeOfSelect);
  75. Command.PubInfo.pcrInfoWrite.pcrSelection.pcrSelect[0] = PubInfo->pcrInfoWrite.pcrSelection.pcrSelect[0];
  76. Command.PubInfo.pcrInfoWrite.pcrSelection.pcrSelect[1] = PubInfo->pcrInfoWrite.pcrSelection.pcrSelect[1];
  77. Command.PubInfo.pcrInfoWrite.pcrSelection.pcrSelect[2] = PubInfo->pcrInfoWrite.pcrSelection.pcrSelect[2];
  78. Command.PubInfo.pcrInfoWrite.localityAtRelease = PubInfo->pcrInfoWrite.localityAtRelease;
  79. CopyMem (&Command.PubInfo.pcrInfoWrite.digestAtRelease, &PubInfo->pcrInfoWrite.digestAtRelease, sizeof (PubInfo->pcrInfoWrite.digestAtRelease));
  80. Command.PubInfo.permission.tag = SwapBytes16 (PubInfo->permission.tag);
  81. Command.PubInfo.permission.attributes = SwapBytes32 (PubInfo->permission.attributes);
  82. Command.PubInfo.bReadSTClear = PubInfo->bReadSTClear;
  83. Command.PubInfo.bWriteSTClear = PubInfo->bWriteSTClear;
  84. Command.PubInfo.bWriteDefine = PubInfo->bWriteDefine;
  85. Command.PubInfo.dataSize = SwapBytes32 (PubInfo->dataSize);
  86. CopyMem (&Command.EncAuth, EncAuth, sizeof (*EncAuth));
  87. Length = sizeof (Response);
  88. Status = Tpm12SubmitCommand (sizeof (Command), (UINT8 *)&Command, &Length, (UINT8 *)&Response);
  89. if (EFI_ERROR (Status)) {
  90. return Status;
  91. }
  92. DEBUG ((DEBUG_INFO, "Tpm12NvDefineSpace - ReturnCode = %x\n", SwapBytes32 (Response.returnCode)));
  93. switch (SwapBytes32 (Response.returnCode)) {
  94. case TPM_SUCCESS:
  95. return EFI_SUCCESS;
  96. default:
  97. return EFI_DEVICE_ERROR;
  98. }
  99. }
  100. /**
  101. Send NV ReadValue command to TPM1.2.
  102. @param NvIndex The index of the area to set.
  103. @param Offset The offset into the area.
  104. @param DataSize The size of the data area.
  105. @param Data The data to set the area to.
  106. @retval EFI_SUCCESS Operation completed successfully.
  107. @retval EFI_DEVICE_ERROR Unexpected device behavior.
  108. **/
  109. EFI_STATUS
  110. EFIAPI
  111. Tpm12NvReadValue (
  112. IN TPM_NV_INDEX NvIndex,
  113. IN UINT32 Offset,
  114. IN OUT UINT32 *DataSize,
  115. OUT UINT8 *Data
  116. )
  117. {
  118. EFI_STATUS Status;
  119. TPM_CMD_NV_READ_VALUE Command;
  120. TPM_RSP_NV_READ_VALUE Response;
  121. UINT32 Length;
  122. //
  123. // send Tpm command TPM_ORD_NV_ReadValue
  124. //
  125. Command.Hdr.tag = SwapBytes16 (TPM_TAG_RQU_COMMAND);
  126. Command.Hdr.paramSize = SwapBytes32 (sizeof (Command));
  127. Command.Hdr.ordinal = SwapBytes32 (TPM_ORD_NV_ReadValue);
  128. Command.NvIndex = SwapBytes32 (NvIndex);
  129. Command.Offset = SwapBytes32 (Offset);
  130. Command.DataSize = SwapBytes32 (*DataSize);
  131. Length = sizeof (Response);
  132. Status = Tpm12SubmitCommand (sizeof (Command), (UINT8 *)&Command, &Length, (UINT8 *)&Response);
  133. if (EFI_ERROR (Status)) {
  134. return Status;
  135. }
  136. DEBUG ((DEBUG_INFO, "Tpm12NvReadValue - ReturnCode = %x\n", SwapBytes32 (Response.Hdr.returnCode)));
  137. switch (SwapBytes32 (Response.Hdr.returnCode)) {
  138. case TPM_SUCCESS:
  139. break;
  140. default:
  141. return EFI_DEVICE_ERROR;
  142. }
  143. //
  144. // Return the response
  145. //
  146. if (SwapBytes32 (Response.DataSize) > *DataSize) {
  147. return EFI_BUFFER_TOO_SMALL;
  148. }
  149. *DataSize = SwapBytes32 (Response.DataSize);
  150. ZeroMem (Data, *DataSize);
  151. CopyMem (Data, &Response.Data, *DataSize);
  152. return EFI_SUCCESS;
  153. }
  154. /**
  155. Send NV WriteValue command to TPM1.2.
  156. @param NvIndex The index of the area to set.
  157. @param Offset The offset into the NV Area.
  158. @param DataSize The size of the data parameter.
  159. @param Data The data to set the area to.
  160. @retval EFI_SUCCESS Operation completed successfully.
  161. @retval EFI_DEVICE_ERROR Unexpected device behavior.
  162. **/
  163. EFI_STATUS
  164. EFIAPI
  165. Tpm12NvWriteValue (
  166. IN TPM_NV_INDEX NvIndex,
  167. IN UINT32 Offset,
  168. IN UINT32 DataSize,
  169. IN UINT8 *Data
  170. )
  171. {
  172. EFI_STATUS Status;
  173. TPM_CMD_NV_WRITE_VALUE Command;
  174. UINT32 CommandLength;
  175. TPM_RSP_COMMAND_HDR Response;
  176. UINT32 ResponseLength;
  177. if (DataSize > sizeof (Command.Data)) {
  178. return EFI_UNSUPPORTED;
  179. }
  180. //
  181. // send Tpm command TPM_ORD_NV_WriteValue
  182. //
  183. Command.Hdr.tag = SwapBytes16 (TPM_TAG_RQU_COMMAND);
  184. CommandLength = sizeof (Command) - sizeof (Command.Data) + DataSize;
  185. Command.Hdr.paramSize = SwapBytes32 (CommandLength);
  186. Command.Hdr.ordinal = SwapBytes32 (TPM_ORD_NV_WriteValue);
  187. Command.NvIndex = SwapBytes32 (NvIndex);
  188. Command.Offset = SwapBytes32 (Offset);
  189. Command.DataSize = SwapBytes32 (DataSize);
  190. CopyMem (Command.Data, Data, DataSize);
  191. ResponseLength = sizeof (Response);
  192. Status = Tpm12SubmitCommand (CommandLength, (UINT8 *)&Command, &ResponseLength, (UINT8 *)&Response);
  193. if (EFI_ERROR (Status)) {
  194. return Status;
  195. }
  196. DEBUG ((DEBUG_INFO, "Tpm12NvWriteValue - ReturnCode = %x\n", SwapBytes32 (Response.returnCode)));
  197. switch (SwapBytes32 (Response.returnCode)) {
  198. case TPM_SUCCESS:
  199. return EFI_SUCCESS;
  200. default:
  201. return EFI_DEVICE_ERROR;
  202. }
  203. }