TianoCore EDK2 master
Loading...
Searching...
No Matches
TcgConfigImpl.c
Go to the documentation of this file.
1
9#include "TcgConfigImpl.h"
10
11CHAR16 mTcgStorageName[] = L"TCG_CONFIGURATION";
12
13TCG_CONFIG_PRIVATE_DATA mTcgConfigPrivateDateTemplate = {
14 TCG_CONFIG_PRIVATE_DATA_SIGNATURE,
15 {
19 }
20};
21
22HII_VENDOR_DEVICE_PATH mTcgHiiVendorDevicePath = {
23 {
24 {
27 {
28 (UINT8)(sizeof (VENDOR_DEVICE_PATH)),
29 (UINT8)((sizeof (VENDOR_DEVICE_PATH)) >> 8)
30 }
31 },
32 TCG_CONFIG_FORM_SET_GUID
33 },
34 {
35 END_DEVICE_PATH_TYPE,
36 END_ENTIRE_DEVICE_PATH_SUBTYPE,
37 {
38 (UINT8)(END_DEVICE_PATH_LENGTH),
39 (UINT8)((END_DEVICE_PATH_LENGTH) >> 8)
40 }
41 }
42};
43
58 IN EFI_TCG_PROTOCOL *TcgProtocol,
59 OUT BOOLEAN *TpmEnable OPTIONAL,
60 OUT BOOLEAN *TpmActivate OPTIONAL
61 )
62{
63 EFI_STATUS Status;
64 TPM_RSP_COMMAND_HDR *TpmRsp;
65 UINT32 TpmSendSize;
66 TPM_PERMANENT_FLAGS *TpmPermanentFlags;
67 UINT8 CmdBuf[64];
68
69 ASSERT (TcgProtocol != NULL);
70
71 //
72 // Get TPM Permanent flags (TpmEnable, TpmActivate)
73 //
74 if ((TpmEnable != NULL) || (TpmActivate != NULL)) {
75 TpmSendSize = sizeof (TPM_RQU_COMMAND_HDR) + sizeof (UINT32) * 3;
76 *(UINT16 *)&CmdBuf[0] = SwapBytes16 (TPM_TAG_RQU_COMMAND);
77 *(UINT32 *)&CmdBuf[2] = SwapBytes32 (TpmSendSize);
78 *(UINT32 *)&CmdBuf[6] = SwapBytes32 (TPM_ORD_GetCapability);
79
80 *(UINT32 *)&CmdBuf[10] = SwapBytes32 (TPM_CAP_FLAG);
81 *(UINT32 *)&CmdBuf[14] = SwapBytes32 (sizeof (TPM_CAP_FLAG_PERMANENT));
82 *(UINT32 *)&CmdBuf[18] = SwapBytes32 (TPM_CAP_FLAG_PERMANENT);
83
84 Status = TcgProtocol->PassThroughToTpm (
85 TcgProtocol,
86 TpmSendSize,
87 CmdBuf,
88 sizeof (CmdBuf),
89 CmdBuf
90 );
91 TpmRsp = (TPM_RSP_COMMAND_HDR *)&CmdBuf[0];
92 if (EFI_ERROR (Status) || (TpmRsp->tag != SwapBytes16 (TPM_TAG_RSP_COMMAND)) || (TpmRsp->returnCode != 0)) {
93 return EFI_DEVICE_ERROR;
94 }
95
96 TpmPermanentFlags = (TPM_PERMANENT_FLAGS *)&CmdBuf[sizeof (TPM_RSP_COMMAND_HDR) + sizeof (UINT32)];
97
98 if (TpmEnable != NULL) {
99 *TpmEnable = (BOOLEAN) !TpmPermanentFlags->disable;
100 }
101
102 if (TpmActivate != NULL) {
103 *TpmActivate = (BOOLEAN) !TpmPermanentFlags->deactivated;
104 }
105 }
106
107 return EFI_SUCCESS;
108}
109
137EFIAPI
140 IN CONST EFI_STRING Request,
141 OUT EFI_STRING *Progress,
142 OUT EFI_STRING *Results
143 )
144{
145 EFI_STATUS Status;
146 TCG_CONFIG_PRIVATE_DATA *PrivateData;
147 EFI_STRING ConfigRequestHdr;
148 EFI_STRING ConfigRequest;
149 BOOLEAN AllocatedRequest;
150 UINTN Size;
151 BOOLEAN TpmEnable;
152 BOOLEAN TpmActivate;
153
154 if ((Progress == NULL) || (Results == NULL)) {
155 return EFI_INVALID_PARAMETER;
156 }
157
158 *Progress = Request;
159 if ((Request != NULL) && !HiiIsConfigHdrMatch (Request, &gTcgConfigFormSetGuid, mTcgStorageName)) {
160 return EFI_NOT_FOUND;
161 }
162
163 ConfigRequestHdr = NULL;
164 ConfigRequest = NULL;
165 AllocatedRequest = FALSE;
166 Size = 0;
167
168 PrivateData = TCG_CONFIG_PRIVATE_DATA_FROM_THIS (This);
169
170 //
171 // Convert buffer data to <ConfigResp> by helper function BlockToConfig()
172 //
173 PrivateData->Configuration->TpmOperation = PHYSICAL_PRESENCE_NO_ACTION;
174
175 //
176 // Get current TPM state.
177 //
178 if (PrivateData->TcgProtocol != NULL) {
179 Status = GetTpmState (PrivateData->TcgProtocol, &TpmEnable, &TpmActivate);
180 if (EFI_ERROR (Status)) {
181 return Status;
182 }
183
184 PrivateData->Configuration->TpmEnable = TpmEnable;
185 PrivateData->Configuration->TpmActivate = TpmActivate;
186 }
187
188 ConfigRequest = Request;
189 if ((Request == NULL) || (StrStr (Request, L"OFFSET") == NULL)) {
190 //
191 // Request has no request element, construct full request string.
192 // Allocate and fill a buffer large enough to hold the <ConfigHdr> template
193 // followed by "&OFFSET=0&WIDTH=WWWWWWWWWWWWWWWW" followed by a Null-terminator
194 //
195 ConfigRequestHdr = HiiConstructConfigHdr (&gTcgConfigFormSetGuid, mTcgStorageName, PrivateData->DriverHandle);
196 Size = (StrLen (ConfigRequestHdr) + 32 + 1) * sizeof (CHAR16);
197 ConfigRequest = AllocateZeroPool (Size);
198 ASSERT (ConfigRequest != NULL);
199 AllocatedRequest = TRUE;
200 UnicodeSPrint (ConfigRequest, Size, L"%s&OFFSET=0&WIDTH=%016LX", ConfigRequestHdr, sizeof (TCG_CONFIGURATION));
201 FreePool (ConfigRequestHdr);
202 }
203
204 Status = gHiiConfigRouting->BlockToConfig (
206 ConfigRequest,
207 (UINT8 *)PrivateData->Configuration,
208 sizeof (TCG_CONFIGURATION),
209 Results,
210 Progress
211 );
212 //
213 // Free the allocated config request string.
214 //
215 if (AllocatedRequest) {
216 FreePool (ConfigRequest);
217 }
218
219 //
220 // Set Progress string to the original request string.
221 //
222 if (Request == NULL) {
223 *Progress = NULL;
224 } else if (StrStr (Request, L"OFFSET") == NULL) {
225 *Progress = Request + StrLen (Request);
226 }
227
228 return Status;
229}
230
250EFIAPI
253 IN CONST EFI_STRING Configuration,
254 OUT EFI_STRING *Progress
255 )
256{
257 EFI_STATUS Status;
258 UINTN BufferSize;
259 TCG_CONFIGURATION TcgConfiguration;
260
261 if ((Configuration == NULL) || (Progress == NULL)) {
262 return EFI_INVALID_PARAMETER;
263 }
264
265 *Progress = Configuration;
266 if (!HiiIsConfigHdrMatch (Configuration, &gTcgConfigFormSetGuid, mTcgStorageName)) {
267 return EFI_NOT_FOUND;
268 }
269
270 //
271 // Convert <ConfigResp> to buffer data by helper function ConfigToBlock()
272 //
273 BufferSize = sizeof (TCG_CONFIGURATION);
274 Status = gHiiConfigRouting->ConfigToBlock (
276 Configuration,
277 (UINT8 *)&TcgConfiguration,
278 &BufferSize,
279 Progress
280 );
281 if (EFI_ERROR (Status)) {
282 return Status;
283 }
284
285 return EFI_SUCCESS;
286}
287
299 IN UINT8 PpRequest
300 )
301{
302 EFI_STATUS Status;
303 UINTN DataSize;
305
306 //
307 // Save TPM command to variable.
308 //
309 DataSize = sizeof (EFI_PHYSICAL_PRESENCE);
310 Status = gRT->GetVariable (
311 PHYSICAL_PRESENCE_VARIABLE,
312 &gEfiPhysicalPresenceGuid,
313 NULL,
314 &DataSize,
315 &PpData
316 );
317 if (EFI_ERROR (Status)) {
318 return Status;
319 }
320
321 PpData.PPRequest = PpRequest;
322 Status = gRT->SetVariable (
323 PHYSICAL_PRESENCE_VARIABLE,
324 &gEfiPhysicalPresenceGuid,
325 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
326 DataSize,
327 &PpData
328 );
329 if (EFI_ERROR (Status)) {
330 return Status;
331 }
332
333 return EFI_SUCCESS;
334}
335
359EFIAPI
362 IN EFI_BROWSER_ACTION Action,
363 IN EFI_QUESTION_ID QuestionId,
364 IN UINT8 Type,
365 IN EFI_IFR_TYPE_VALUE *Value,
366 OUT EFI_BROWSER_ACTION_REQUEST *ActionRequest
367 )
368{
369 TCG_CONFIG_PRIVATE_DATA *PrivateData;
370 CHAR16 State[32];
371
372 if ((This == NULL) || (Value == NULL) || (ActionRequest == NULL)) {
373 return EFI_INVALID_PARAMETER;
374 }
375
376 if (Action == EFI_BROWSER_ACTION_FORM_OPEN) {
377 if (QuestionId == KEY_TPM_ACTION) {
378 PrivateData = TCG_CONFIG_PRIVATE_DATA_FROM_THIS (This);
380 State,
381 sizeof (State),
382 L"%s, and %s",
383 PrivateData->Configuration->TpmEnable ? L"Enabled" : L"Disabled",
384 PrivateData->Configuration->TpmActivate ? L"Activated" : L"Deactivated"
385 );
386 HiiSetString (PrivateData->HiiHandle, STRING_TOKEN (STR_TPM_STATE_CONTENT), State, NULL);
387 }
388
389 return EFI_SUCCESS;
390 }
391
392 if ((Action != EFI_BROWSER_ACTION_CHANGED) || (QuestionId != KEY_TPM_ACTION)) {
393 return EFI_UNSUPPORTED;
394 }
395
396 SavePpRequest (Value->u8);
397 *ActionRequest = EFI_BROWSER_ACTION_REQUEST_SUBMIT;
398
399 return EFI_SUCCESS;
400}
401
414 IN OUT TCG_CONFIG_PRIVATE_DATA *PrivateData
415 )
416{
417 EFI_STATUS Status;
418 EFI_HII_HANDLE HiiHandle;
419 EFI_HANDLE DriverHandle;
420 EFI_HII_CONFIG_ACCESS_PROTOCOL *ConfigAccess;
421
422 DriverHandle = NULL;
423 ConfigAccess = &PrivateData->ConfigAccess;
424 Status = gBS->InstallMultipleProtocolInterfaces (
425 &DriverHandle,
426 &gEfiDevicePathProtocolGuid,
427 &mTcgHiiVendorDevicePath,
428 &gEfiHiiConfigAccessProtocolGuid,
429 ConfigAccess,
430 NULL
431 );
432 if (EFI_ERROR (Status)) {
433 return Status;
434 }
435
436 PrivateData->DriverHandle = DriverHandle;
437
438 //
439 // Publish the HII package list
440 //
441 HiiHandle = HiiAddPackages (
442 &gTcgConfigFormSetGuid,
443 DriverHandle,
444 TcgConfigDxeStrings,
445 TcgConfigBin,
446 NULL
447 );
448 if (HiiHandle == NULL) {
449 gBS->UninstallMultipleProtocolInterfaces (
450 DriverHandle,
451 &gEfiDevicePathProtocolGuid,
452 &mTcgHiiVendorDevicePath,
453 &gEfiHiiConfigAccessProtocolGuid,
454 ConfigAccess,
455 NULL
456 );
457
458 return EFI_OUT_OF_RESOURCES;
459 }
460
461 PrivateData->HiiHandle = HiiHandle;
462
463 return EFI_SUCCESS;
464}
465
472VOID
474 IN OUT TCG_CONFIG_PRIVATE_DATA *PrivateData
475 )
476{
477 //
478 // Uninstall HII package list
479 //
480 if (PrivateData->HiiHandle != NULL) {
481 HiiRemovePackages (PrivateData->HiiHandle);
482 PrivateData->HiiHandle = NULL;
483 }
484
485 //
486 // Uninstall HII Config Access Protocol
487 //
488 if (PrivateData->DriverHandle != NULL) {
489 gBS->UninstallMultipleProtocolInterfaces (
490 PrivateData->DriverHandle,
491 &gEfiDevicePathProtocolGuid,
492 &mTcgHiiVendorDevicePath,
493 &gEfiHiiConfigAccessProtocolGuid,
494 &PrivateData->ConfigAccess,
495 NULL
496 );
497 PrivateData->DriverHandle = NULL;
498 }
499
500 if (PrivateData->Configuration != NULL) {
501 FreePool (PrivateData->Configuration);
502 }
503
504 FreePool (PrivateData);
505}
UINT64 UINTN
UINT16 EFIAPI SwapBytes16(IN UINT16 Value)
Definition: SwapBytes16.c:25
UINT32 EFIAPI SwapBytes32(IN UINT32 Value)
Definition: SwapBytes32.c:25
UINTN EFIAPI StrLen(IN CONST CHAR16 *String)
Definition: String.c:30
CHAR16 *EFIAPI StrStr(IN CONST CHAR16 *String, IN CONST CHAR16 *SearchString)
Definition: String.c:224
#define HARDWARE_DEVICE_PATH
Definition: DevicePath.h:68
#define HW_VENDOR_DP
Definition: DevicePath.h:133
VOID *EFIAPI AllocateZeroPool(IN UINTN AllocationSize)
VOID EFIAPI FreePool(IN VOID *Buffer)
EFI_STRING EFIAPI HiiConstructConfigHdr(IN CONST EFI_GUID *Guid OPTIONAL, IN CONST CHAR16 *Name OPTIONAL, IN EFI_HANDLE DriverHandle)
Definition: HiiLib.c:723
EFI_HII_HANDLE EFIAPI HiiAddPackages(IN CONST EFI_GUID *PackageListGuid, IN EFI_HANDLE DeviceHandle OPTIONAL,...)
Definition: HiiLib.c:141
BOOLEAN EFIAPI HiiIsConfigHdrMatch(IN CONST EFI_STRING ConfigHdr, IN CONST EFI_GUID *Guid OPTIONAL, IN CONST CHAR16 *Name OPTIONAL)
Definition: HiiLib.c:2813
EFI_STRING_ID EFIAPI HiiSetString(IN EFI_HII_HANDLE HiiHandle, IN EFI_STRING_ID StringId OPTIONAL, IN CONST EFI_STRING String, IN CONST CHAR8 *SupportedLanguages OPTIONAL)
Definition: HiiString.c:52
VOID EFIAPI HiiRemovePackages(IN EFI_HII_HANDLE HiiHandle)
Definition: HiiLib.c:253
UINTN EFIAPI UnicodeSPrint(OUT CHAR16 *StartOfBuffer, IN UINTN BufferSize, IN CONST CHAR16 *FormatString,...)
Definition: PrintLib.c:408
EFI_RUNTIME_SERVICES * gRT
#define NULL
Definition: Base.h:319
#define CONST
Definition: Base.h:259
#define TRUE
Definition: Base.h:301
#define FALSE
Definition: Base.h:307
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
EFI_STATUS EFIAPI TcgCallback(IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This, IN EFI_BROWSER_ACTION Action, IN EFI_QUESTION_ID QuestionId, IN UINT8 Type, IN EFI_IFR_TYPE_VALUE *Value, OUT EFI_BROWSER_ACTION_REQUEST *ActionRequest)
EFI_STATUS SavePpRequest(IN UINT8 PpRequest)
EFI_STATUS EFIAPI TcgExtractConfig(IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This, IN CONST EFI_STRING Request, OUT EFI_STRING *Progress, OUT EFI_STRING *Results)
EFI_STATUS InstallTcgConfigForm(IN OUT TCG_CONFIG_PRIVATE_DATA *PrivateData)
VOID UninstallTcgConfigForm(IN OUT TCG_CONFIG_PRIVATE_DATA *PrivateData)
EFI_STATUS EFIAPI TcgRouteConfig(IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This, IN CONST EFI_STRING Configuration, OUT EFI_STRING *Progress)
EFI_STATUS GetTpmState(IN EFI_TCG_PROTOCOL *TcgProtocol, OUT BOOLEAN *TpmEnable OPTIONAL, OUT BOOLEAN *TpmActivate OPTIONAL)
Definition: TcgConfigImpl.c:57
struct tdTPM_RQU_COMMAND_HDR TPM_RQU_COMMAND_HDR
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
VOID * EFI_HANDLE
Definition: UefiBaseType.h:33
#define EFI_SUCCESS
Definition: UefiBaseType.h:112
EFI_BOOT_SERVICES * gBS
EFI_HII_CONFIG_ROUTING_PROTOCOL * gHiiConfigRouting
#define STRING_TOKEN(t)
VOID * EFI_HII_HANDLE
#define EFI_VARIABLE_NON_VOLATILE
UINT8 PPRequest
Physical Presence request command.