TianoCore EDK2 master
Loading...
Searching...
No Matches
Tpm12NvStorage.c
Go to the documentation of this file.
1
10#include <PiPei.h>
12#include <Library/BaseLib.h>
13#include <Library/DebugLib.h>
16
17//
18// Max TPM NV value length
19//
20#define TPMNVVALUELENGTH 1024
21
22#pragma pack(1)
23
24typedef struct {
27 TPM_ENCAUTH EncAuth;
29
30typedef struct {
32 TPM_NV_INDEX NvIndex;
33 UINT32 Offset;
34 UINT32 DataSize;
36
37typedef struct {
39 UINT32 DataSize;
40 UINT8 Data[TPMNVVALUELENGTH];
42
43typedef struct {
45 TPM_NV_INDEX NvIndex;
46 UINT32 Offset;
47 UINT32 DataSize;
48 UINT8 Data[TPMNVVALUELENGTH];
50
51#pragma pack()
52
63EFIAPI
65 IN TPM12_NV_DATA_PUBLIC *PubInfo,
66 IN TPM_ENCAUTH *EncAuth
67 )
68{
69 EFI_STATUS Status;
71 TPM_RSP_COMMAND_HDR Response;
72 UINT32 Length;
73
74 //
75 // send Tpm command TPM_ORD_NV_DefineSpace
76 //
77 Command.Hdr.tag = SwapBytes16 (TPM_TAG_RQU_COMMAND);
78 Command.Hdr.paramSize = SwapBytes32 (sizeof (Command));
79 Command.Hdr.ordinal = SwapBytes32 (TPM_ORD_NV_DefineSpace);
80 Command.PubInfo.tag = SwapBytes16 (PubInfo->tag);
81 Command.PubInfo.nvIndex = SwapBytes32 (PubInfo->nvIndex);
82 Command.PubInfo.pcrInfoRead.pcrSelection.sizeOfSelect = SwapBytes16 (PubInfo->pcrInfoRead.pcrSelection.sizeOfSelect);
83 Command.PubInfo.pcrInfoRead.pcrSelection.pcrSelect[0] = PubInfo->pcrInfoRead.pcrSelection.pcrSelect[0];
84 Command.PubInfo.pcrInfoRead.pcrSelection.pcrSelect[1] = PubInfo->pcrInfoRead.pcrSelection.pcrSelect[1];
85 Command.PubInfo.pcrInfoRead.pcrSelection.pcrSelect[2] = PubInfo->pcrInfoRead.pcrSelection.pcrSelect[2];
86 Command.PubInfo.pcrInfoRead.localityAtRelease = PubInfo->pcrInfoRead.localityAtRelease;
87 CopyMem (&Command.PubInfo.pcrInfoRead.digestAtRelease, &PubInfo->pcrInfoRead.digestAtRelease, sizeof (PubInfo->pcrInfoRead.digestAtRelease));
88 Command.PubInfo.pcrInfoWrite.pcrSelection.sizeOfSelect = SwapBytes16 (PubInfo->pcrInfoWrite.pcrSelection.sizeOfSelect);
89 Command.PubInfo.pcrInfoWrite.pcrSelection.pcrSelect[0] = PubInfo->pcrInfoWrite.pcrSelection.pcrSelect[0];
90 Command.PubInfo.pcrInfoWrite.pcrSelection.pcrSelect[1] = PubInfo->pcrInfoWrite.pcrSelection.pcrSelect[1];
91 Command.PubInfo.pcrInfoWrite.pcrSelection.pcrSelect[2] = PubInfo->pcrInfoWrite.pcrSelection.pcrSelect[2];
92 Command.PubInfo.pcrInfoWrite.localityAtRelease = PubInfo->pcrInfoWrite.localityAtRelease;
93 CopyMem (&Command.PubInfo.pcrInfoWrite.digestAtRelease, &PubInfo->pcrInfoWrite.digestAtRelease, sizeof (PubInfo->pcrInfoWrite.digestAtRelease));
94 Command.PubInfo.permission.tag = SwapBytes16 (PubInfo->permission.tag);
95 Command.PubInfo.permission.attributes = SwapBytes32 (PubInfo->permission.attributes);
96 Command.PubInfo.bReadSTClear = PubInfo->bReadSTClear;
97 Command.PubInfo.bWriteSTClear = PubInfo->bWriteSTClear;
98 Command.PubInfo.bWriteDefine = PubInfo->bWriteDefine;
99 Command.PubInfo.dataSize = SwapBytes32 (PubInfo->dataSize);
100 CopyMem (&Command.EncAuth, EncAuth, sizeof (*EncAuth));
101 Length = sizeof (Response);
102 Status = Tpm12SubmitCommand (sizeof (Command), (UINT8 *)&Command, &Length, (UINT8 *)&Response);
103 if (EFI_ERROR (Status)) {
104 return Status;
105 }
106
107 DEBUG ((DEBUG_INFO, "Tpm12NvDefineSpace - ReturnCode = %x\n", SwapBytes32 (Response.returnCode)));
108 switch (SwapBytes32 (Response.returnCode)) {
109 case TPM_SUCCESS:
110 return EFI_SUCCESS;
111 default:
112 return EFI_DEVICE_ERROR;
113 }
114}
115
128EFIAPI
130 IN TPM_NV_INDEX NvIndex,
131 IN UINT32 Offset,
132 IN OUT UINT32 *DataSize,
133 OUT UINT8 *Data
134 )
135{
136 EFI_STATUS Status;
137 TPM_CMD_NV_READ_VALUE Command;
138 TPM_RSP_NV_READ_VALUE Response;
139 UINT32 Length;
140
141 //
142 // send Tpm command TPM_ORD_NV_ReadValue
143 //
144 Command.Hdr.tag = SwapBytes16 (TPM_TAG_RQU_COMMAND);
145 Command.Hdr.paramSize = SwapBytes32 (sizeof (Command));
146 Command.Hdr.ordinal = SwapBytes32 (TPM_ORD_NV_ReadValue);
147 Command.NvIndex = SwapBytes32 (NvIndex);
148 Command.Offset = SwapBytes32 (Offset);
149 Command.DataSize = SwapBytes32 (*DataSize);
150 Length = sizeof (Response);
151 Status = Tpm12SubmitCommand (sizeof (Command), (UINT8 *)&Command, &Length, (UINT8 *)&Response);
152 if (EFI_ERROR (Status)) {
153 return Status;
154 }
155
156 DEBUG ((DEBUG_INFO, "Tpm12NvReadValue - ReturnCode = %x\n", SwapBytes32 (Response.Hdr.returnCode)));
157 switch (SwapBytes32 (Response.Hdr.returnCode)) {
158 case TPM_SUCCESS:
159 break;
160 default:
161 return EFI_DEVICE_ERROR;
162 }
163
164 //
165 // Return the response
166 //
167 if (SwapBytes32 (Response.DataSize) > *DataSize) {
168 return EFI_BUFFER_TOO_SMALL;
169 }
170
171 *DataSize = SwapBytes32 (Response.DataSize);
172 ZeroMem (Data, *DataSize);
173 CopyMem (Data, &Response.Data, *DataSize);
174
175 return EFI_SUCCESS;
176}
177
190EFIAPI
192 IN TPM_NV_INDEX NvIndex,
193 IN UINT32 Offset,
194 IN UINT32 DataSize,
195 IN UINT8 *Data
196 )
197{
198 EFI_STATUS Status;
200 UINT32 CommandLength;
201 TPM_RSP_COMMAND_HDR Response;
202 UINT32 ResponseLength;
203
204 if (DataSize > sizeof (Command.Data)) {
205 return EFI_UNSUPPORTED;
206 }
207
208 //
209 // send Tpm command TPM_ORD_NV_WriteValue
210 //
211 Command.Hdr.tag = SwapBytes16 (TPM_TAG_RQU_COMMAND);
212 CommandLength = sizeof (Command) - sizeof (Command.Data) + DataSize;
213 Command.Hdr.paramSize = SwapBytes32 (CommandLength);
214 Command.Hdr.ordinal = SwapBytes32 (TPM_ORD_NV_WriteValue);
215 Command.NvIndex = SwapBytes32 (NvIndex);
216 Command.Offset = SwapBytes32 (Offset);
217 Command.DataSize = SwapBytes32 (DataSize);
218 CopyMem (Command.Data, Data, DataSize);
219 ResponseLength = sizeof (Response);
220 Status = Tpm12SubmitCommand (CommandLength, (UINT8 *)&Command, &ResponseLength, (UINT8 *)&Response);
221 if (EFI_ERROR (Status)) {
222 return Status;
223 }
224
225 DEBUG ((DEBUG_INFO, "Tpm12NvWriteValue - ReturnCode = %x\n", SwapBytes32 (Response.returnCode)));
226 switch (SwapBytes32 (Response.returnCode)) {
227 case TPM_SUCCESS:
228 return EFI_SUCCESS;
229 default:
230 return EFI_DEVICE_ERROR;
231 }
232}
UINT16 EFIAPI SwapBytes16(IN UINT16 Value)
Definition: SwapBytes16.c:25
UINT32 EFIAPI SwapBytes32(IN UINT32 Value)
Definition: SwapBytes32.c:25
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
VOID *EFIAPI ZeroMem(OUT VOID *Buffer, IN UINTN Length)
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
#define DEBUG(Expression)
Definition: DebugLib.h:434
UINT32 TPM_NV_INDEX
Definition: Tpm12.h:149
TPM_AUTHDATA TPM_ENCAUTH
Definition: Tpm12.h:500
EFI_STATUS EFIAPI Tpm12SubmitCommand(IN UINT32 InputParameterBlockSize, IN UINT8 *InputParameterBlock, IN OUT UINT32 *OutputParameterBlockSize, IN UINT8 *OutputParameterBlock)
Definition: Tpm12Tis.c:453
EFI_STATUS EFIAPI Tpm12NvWriteValue(IN TPM_NV_INDEX NvIndex, IN UINT32 Offset, IN UINT32 DataSize, IN UINT8 *Data)
EFI_STATUS EFIAPI Tpm12NvDefineSpace(IN TPM12_NV_DATA_PUBLIC *PubInfo, IN TPM_ENCAUTH *EncAuth)
EFI_STATUS EFIAPI Tpm12NvReadValue(IN TPM_NV_INDEX NvIndex, IN UINT32 Offset, IN OUT UINT32 *DataSize, OUT UINT8 *Data)
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
#define EFI_SUCCESS
Definition: UefiBaseType.h:112