]> git.proxmox.com Git - mirror_edk2.git/blame - SecurityPkg/Tcg/TcgConfigDxe/TcgConfigImpl.c
Implement Tcg physical presence as a library instead of DXE driver in order that...
[mirror_edk2.git] / SecurityPkg / Tcg / TcgConfigDxe / TcgConfigImpl.c
CommitLineData
0c18794e 1/** @file\r
2 HII Config Access protocol implementation of TCG configuration module.\r
3\r
4Copyright (c) 2011, Intel Corporation. All rights reserved.<BR>\r
5This program and the accompanying materials \r
6are licensed and made available under the terms and conditions of the BSD License \r
7which accompanies this distribution. The full text of the license may be found at \r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#include "TcgConfigImpl.h"\r
16\r
0c18794e 17CHAR16 mTcgStorageName[] = L"TCG_CONFIGURATION";\r
18\r
19TCG_CONFIG_PRIVATE_DATA mTcgConfigPrivateDateTemplate = {\r
20 TCG_CONFIG_PRIVATE_DATA_SIGNATURE,\r
21 {\r
22 TcgExtractConfig,\r
23 TcgRouteConfig,\r
24 TcgCallback\r
25 }\r
26};\r
27\r
28HII_VENDOR_DEVICE_PATH mTcgHiiVendorDevicePath = {\r
29 {\r
30 {\r
31 HARDWARE_DEVICE_PATH,\r
32 HW_VENDOR_DP,\r
33 {\r
34 (UINT8) (sizeof (VENDOR_DEVICE_PATH)),\r
35 (UINT8) ((sizeof (VENDOR_DEVICE_PATH)) >> 8)\r
36 }\r
37 },\r
a0c56a82 38 TCG_CONFIG_FORM_SET_GUID\r
0c18794e 39 },\r
40 {\r
41 END_DEVICE_PATH_TYPE,\r
42 END_ENTIRE_DEVICE_PATH_SUBTYPE,\r
43 { \r
44 (UINT8) (END_DEVICE_PATH_LENGTH),\r
45 (UINT8) ((END_DEVICE_PATH_LENGTH) >> 8)\r
46 }\r
47 }\r
48};\r
49\r
50/**\r
51 Get current state of TPM device.\r
52\r
53 @param[in] TcgProtocol Point to EFI_TCG_PROTOCOL instance.\r
54 @param[out] TpmEnable Flag to indicate TPM is enabled or not.\r
55 @param[out] TpmActivate Flag to indicate TPM is activated or not.\r
56\r
57 @retval EFI_SUCCESS State is successfully returned.\r
58 @retval EFI_DEVICE_ERROR Failed to get TPM response.\r
59 @retval Others Other errors as indicated.\r
60\r
61**/\r
62EFI_STATUS\r
63GetTpmState (\r
64 IN EFI_TCG_PROTOCOL *TcgProtocol,\r
65 OUT BOOLEAN *TpmEnable, OPTIONAL\r
66 OUT BOOLEAN *TpmActivate OPTIONAL\r
67 )\r
68{\r
69 EFI_STATUS Status;\r
70 TPM_RSP_COMMAND_HDR *TpmRsp;\r
71 UINT32 TpmSendSize;\r
72 TPM_PERMANENT_FLAGS *TpmPermanentFlags;\r
73 UINT8 CmdBuf[64];\r
74\r
75 ASSERT (TcgProtocol != NULL);\r
76 \r
77 //\r
78 // Get TPM Permanent flags (TpmEnable, TpmActivate)\r
79 //\r
80 if ((TpmEnable != NULL) || (TpmActivate != NULL)) {\r
81 TpmSendSize = sizeof (TPM_RQU_COMMAND_HDR) + sizeof (UINT32) * 3;\r
607599bf 82 *(UINT16*)&CmdBuf[0] = SwapBytes16 (TPM_TAG_RQU_COMMAND);\r
83 *(UINT32*)&CmdBuf[2] = SwapBytes32 (TpmSendSize);\r
84 *(UINT32*)&CmdBuf[6] = SwapBytes32 (TPM_ORD_GetCapability);\r
0c18794e 85 \r
607599bf 86 *(UINT32*)&CmdBuf[10] = SwapBytes32 (TPM_CAP_FLAG);\r
87 *(UINT32*)&CmdBuf[14] = SwapBytes32 (sizeof (TPM_CAP_FLAG_PERMANENT));\r
88 *(UINT32*)&CmdBuf[18] = SwapBytes32 (TPM_CAP_FLAG_PERMANENT);\r
0c18794e 89\r
90 Status = TcgProtocol->PassThroughToTpm (\r
91 TcgProtocol,\r
92 TpmSendSize,\r
93 CmdBuf,\r
94 sizeof (CmdBuf),\r
95 CmdBuf\r
96 ); \r
97 TpmRsp = (TPM_RSP_COMMAND_HDR *) &CmdBuf[0];\r
607599bf 98 if (EFI_ERROR (Status) || (TpmRsp->tag != SwapBytes16 (TPM_TAG_RSP_COMMAND)) || (TpmRsp->returnCode != 0)) {\r
0c18794e 99 return EFI_DEVICE_ERROR;\r
100 }\r
101 \r
102 TpmPermanentFlags = (TPM_PERMANENT_FLAGS *) &CmdBuf[sizeof (TPM_RSP_COMMAND_HDR) + sizeof (UINT32)];\r
103\r
104 if (TpmEnable != NULL) {\r
105 *TpmEnable = (BOOLEAN) !TpmPermanentFlags->disable;\r
106 }\r
107\r
108 if (TpmActivate != NULL) {\r
109 *TpmActivate = (BOOLEAN) !TpmPermanentFlags->deactivated;\r
110 }\r
111 }\r
112 \r
113 return EFI_SUCCESS; \r
114}\r
115\r
116/**\r
117 This function allows a caller to extract the current configuration for one\r
118 or more named elements from the target driver.\r
119\r
120 @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.\r
121 @param[in] Request A null-terminated Unicode string in\r
122 <ConfigRequest> format.\r
123 @param[out] Progress On return, points to a character in the Request\r
124 string. Points to the string's null terminator if\r
125 request was successful. Points to the most recent\r
126 '&' before the first failing name/value pair (or\r
127 the beginning of the string if the failure is in\r
128 the first name/value pair) if the request was not\r
129 successful.\r
130 @param[out] Results A null-terminated Unicode string in\r
131 <ConfigAltResp> format which has all values filled\r
132 in for the names in the Request string. String to\r
133 be allocated by the called function.\r
134\r
135 @retval EFI_SUCCESS The Results is filled with the requested values.\r
136 @retval EFI_OUT_OF_RESOURCES Not enough memory to store the results.\r
137 @retval EFI_INVALID_PARAMETER Request is illegal syntax, or unknown name.\r
138 @retval EFI_NOT_FOUND Routing data doesn't match any storage in this\r
139 driver.\r
140\r
141**/\r
142EFI_STATUS\r
143EFIAPI\r
144TcgExtractConfig (\r
145 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This,\r
146 IN CONST EFI_STRING Request,\r
147 OUT EFI_STRING *Progress,\r
148 OUT EFI_STRING *Results\r
149 )\r
150{\r
151 EFI_STATUS Status;\r
152 UINTN BufferSize;\r
153 TCG_CONFIGURATION Configuration;\r
154 TCG_CONFIG_PRIVATE_DATA *PrivateData;\r
155 EFI_STRING ConfigRequestHdr;\r
156 EFI_STRING ConfigRequest;\r
157 BOOLEAN AllocatedRequest;\r
158 UINTN Size;\r
159 BOOLEAN TpmEnable;\r
160 BOOLEAN TpmActivate;\r
161 CHAR16 State[32];\r
162\r
163 if (Progress == NULL || Results == NULL) {\r
164 return EFI_INVALID_PARAMETER;\r
165 }\r
166\r
167 *Progress = Request;\r
a0c56a82 168 if ((Request != NULL) && !HiiIsConfigHdrMatch (Request, &gTcgConfigFormSetGuid, mTcgStorageName)) {\r
0c18794e 169 return EFI_NOT_FOUND;\r
170 }\r
171\r
172 ConfigRequestHdr = NULL;\r
173 ConfigRequest = NULL;\r
174 AllocatedRequest = FALSE;\r
175 Size = 0;\r
176\r
177 PrivateData = TCG_CONFIG_PRIVATE_DATA_FROM_THIS (This);\r
178\r
179 //\r
180 // Convert buffer data to <ConfigResp> by helper function BlockToConfig()\r
181 // \r
182 ZeroMem (&Configuration, sizeof (TCG_CONFIGURATION));\r
183\r
184 Configuration.MorState = PcdGetBool (PcdMorEnable);\r
607599bf 185 Configuration.TpmOperation = PHYSICAL_PRESENCE_ENABLE;\r
81b7a609 186 Configuration.HideTpm = (BOOLEAN) (PcdGetBool (PcdHideTpmSupport) && PcdGetBool (PcdHideTpm));\r
0c18794e 187 //\r
188 // Read the original value of HideTpm from PrivateData which won't be changed by Setup in this boot.\r
189 //\r
190 Configuration.OriginalHideTpm = PrivateData->HideTpm;\r
191\r
192 //\r
193 // Display current TPM state.\r
194 //\r
195 if (PrivateData->TcgProtocol != NULL) {\r
196 Status = GetTpmState (PrivateData->TcgProtocol, &TpmEnable, &TpmActivate);\r
197 if (EFI_ERROR (Status)) {\r
198 return Status;\r
199 }\r
200\r
201 UnicodeSPrint (\r
202 State,\r
203 sizeof (State),\r
204 L"%s, and %s",\r
205 TpmEnable ? L"Enabled" : L"Disabled",\r
206 TpmActivate ? L"Activated" : L"Deactivated"\r
207 );\r
208 Configuration.TpmEnable = TpmEnable;\r
209 Configuration.TpmActivate = TpmActivate;\r
210\r
211 HiiSetString (PrivateData->HiiHandle, STRING_TOKEN (STR_TPM_STATE_CONTENT), State, NULL);\r
212 }\r
213\r
214 BufferSize = sizeof (Configuration);\r
215 ConfigRequest = Request;\r
216 if ((Request == NULL) || (StrStr (Request, L"OFFSET") == NULL)) {\r
217 //\r
218 // Request has no request element, construct full request string.\r
219 // Allocate and fill a buffer large enough to hold the <ConfigHdr> template\r
220 // followed by "&OFFSET=0&WIDTH=WWWWWWWWWWWWWWWW" followed by a Null-terminator\r
221 //\r
a0c56a82 222 ConfigRequestHdr = HiiConstructConfigHdr (&gTcgConfigFormSetGuid, mTcgStorageName, PrivateData->DriverHandle);\r
0c18794e 223 Size = (StrLen (ConfigRequestHdr) + 32 + 1) * sizeof (CHAR16);\r
224 ConfigRequest = AllocateZeroPool (Size);\r
225 ASSERT (ConfigRequest != NULL);\r
226 AllocatedRequest = TRUE;\r
227 UnicodeSPrint (ConfigRequest, Size, L"%s&OFFSET=0&WIDTH=%016LX", ConfigRequestHdr, (UINT64) BufferSize);\r
228 FreePool (ConfigRequestHdr);\r
229 }\r
230\r
231 Status = gHiiConfigRouting->BlockToConfig (\r
232 gHiiConfigRouting,\r
233 ConfigRequest,\r
234 (UINT8 *) &Configuration,\r
235 BufferSize,\r
236 Results,\r
237 Progress\r
238 );\r
239 //\r
240 // Free the allocated config request string.\r
241 //\r
242 if (AllocatedRequest) {\r
243 FreePool (ConfigRequest);\r
244 }\r
245 //\r
246 // Set Progress string to the original request string.\r
247 //\r
248 if (Request == NULL) {\r
249 *Progress = NULL;\r
250 } else if (StrStr (Request, L"OFFSET") == NULL) {\r
251 *Progress = Request + StrLen (Request);\r
252 }\r
253\r
254 return Status;\r
255}\r
256\r
257/**\r
258 This function processes the results of changes in configuration.\r
259\r
260 @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.\r
261 @param[in] Configuration A null-terminated Unicode string in <ConfigResp>\r
262 format.\r
263 @param[out] Progress A pointer to a string filled in with the offset of\r
264 the most recent '&' before the first failing\r
265 name/value pair (or the beginning of the string if\r
266 the failure is in the first name/value pair) or\r
267 the terminating NULL if all was successful.\r
268\r
269 @retval EFI_SUCCESS The Results is processed successfully.\r
270 @retval EFI_INVALID_PARAMETER Configuration is NULL.\r
271 @retval EFI_NOT_FOUND Routing data doesn't match any storage in this\r
272 driver.\r
273\r
274**/\r
275EFI_STATUS\r
276EFIAPI\r
277TcgRouteConfig (\r
278 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This,\r
279 IN CONST EFI_STRING Configuration,\r
280 OUT EFI_STRING *Progress\r
281 )\r
282{\r
283 EFI_STATUS Status;\r
284 UINTN BufferSize;\r
285 TCG_CONFIGURATION TcgConfiguration;\r
286\r
287 if (Configuration == NULL || Progress == NULL) {\r
288 return EFI_INVALID_PARAMETER;\r
289 }\r
290\r
291 *Progress = Configuration;\r
a0c56a82 292 if (!HiiIsConfigHdrMatch (Configuration, &gTcgConfigFormSetGuid, mTcgStorageName)) {\r
0c18794e 293 return EFI_NOT_FOUND;\r
294 }\r
295\r
296 //\r
297 // Convert <ConfigResp> to buffer data by helper function ConfigToBlock()\r
298 //\r
299 BufferSize = sizeof (TCG_CONFIGURATION);\r
300 Status = gHiiConfigRouting->ConfigToBlock (\r
301 gHiiConfigRouting,\r
302 Configuration,\r
303 (UINT8 *) &TcgConfiguration,\r
304 &BufferSize,\r
305 Progress\r
306 );\r
307 if (EFI_ERROR (Status)) {\r
308 return Status;\r
309 }\r
310\r
311 PcdSetBool (PcdMorEnable, TcgConfiguration.MorState);\r
312 PcdSetBool (PcdHideTpm, TcgConfiguration.HideTpm);\r
313\r
314 return EFI_SUCCESS;\r
315}\r
316\r
317/**\r
318 Save TPM request to variable space.\r
319\r
320 @param[in] PpRequest Physical Presence request command.\r
321\r
322 @retval EFI_SUCCESS The operation is finished successfully.\r
323 @retval Others Other errors as indicated.\r
324\r
325**/\r
326EFI_STATUS\r
327SavePpRequest (\r
328 IN UINT8 PpRequest\r
329 )\r
330{\r
331 EFI_STATUS Status;\r
332 UINTN DataSize;\r
333 EFI_PHYSICAL_PRESENCE PpData;\r
334\r
335 //\r
336 // Save TPM command to variable.\r
337 //\r
338 DataSize = sizeof (EFI_PHYSICAL_PRESENCE);\r
339 Status = gRT->GetVariable (\r
340 PHYSICAL_PRESENCE_VARIABLE,\r
341 &gEfiPhysicalPresenceGuid,\r
342 NULL,\r
343 &DataSize,\r
344 &PpData\r
345 );\r
346 if (EFI_ERROR (Status)) {\r
347 return Status;\r
348 } \r
349 \r
350 PpData.PPRequest = PpRequest;\r
351 Status = gRT->SetVariable (\r
352 PHYSICAL_PRESENCE_VARIABLE,\r
353 &gEfiPhysicalPresenceGuid,\r
354 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r
355 DataSize,\r
356 &PpData\r
357 );\r
358 if (EFI_ERROR(Status)) {\r
359 return Status;\r
360 }\r
361\r
362 //\r
363 // Reset system.\r
364 //\r
365 gRT->ResetSystem (EfiResetCold, EFI_SUCCESS, 0, NULL);\r
366\r
367 return EFI_SUCCESS;\r
368}\r
369\r
370/**\r
371 This function processes the results of changes in configuration.\r
372\r
373 @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.\r
374 @param[in] Action Specifies the type of action taken by the browser.\r
375 @param[in] QuestionId A unique value which is sent to the original\r
376 exporting driver so that it can identify the type\r
377 of data to expect.\r
378 @param[in] Type The type of value for the question.\r
379 @param[in] Value A pointer to the data being sent to the original\r
380 exporting driver.\r
381 @param[out] ActionRequest On return, points to the action requested by the\r
382 callback function.\r
383\r
384 @retval EFI_SUCCESS The callback successfully handled the action.\r
385 @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the\r
386 variable and its data.\r
387 @retval EFI_DEVICE_ERROR The variable could not be saved.\r
388 @retval EFI_UNSUPPORTED The specified Action is not supported by the\r
389 callback.\r
390\r
391**/\r
392EFI_STATUS\r
393EFIAPI\r
394TcgCallback (\r
395 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This,\r
396 IN EFI_BROWSER_ACTION Action,\r
397 IN EFI_QUESTION_ID QuestionId,\r
398 IN UINT8 Type,\r
399 IN EFI_IFR_TYPE_VALUE *Value,\r
400 OUT EFI_BROWSER_ACTION_REQUEST *ActionRequest\r
401 )\r
402{\r
403 if ((This == NULL) || (Value == NULL) || (ActionRequest == NULL)) {\r
404 return EFI_INVALID_PARAMETER;\r
405 }\r
406\r
407 if ((Action != EFI_BROWSER_ACTION_CHANGING) || (QuestionId != KEY_TPM_ACTION)) {\r
408 return EFI_UNSUPPORTED;\r
409 }\r
410\r
411 SavePpRequest (Value->u8);\r
412 ASSERT (FALSE);\r
413\r
414 return EFI_SUCCESS;\r
415}\r
416\r
417/**\r
418 This function publish the TCG configuration Form for TPM device.\r
419\r
420 @param[in, out] PrivateData Points to TCG configuration private data.\r
421\r
422 @retval EFI_SUCCESS HII Form is installed for this network device.\r
423 @retval EFI_OUT_OF_RESOURCES Not enough resource for HII Form installation.\r
424 @retval Others Other errors as indicated.\r
425\r
426**/\r
427EFI_STATUS\r
428InstallTcgConfigForm (\r
429 IN OUT TCG_CONFIG_PRIVATE_DATA *PrivateData\r
430 )\r
431{\r
432 EFI_STATUS Status;\r
433 EFI_HII_HANDLE HiiHandle;\r
434 EFI_HANDLE DriverHandle;\r
435 VOID *StartOpCodeHandle;\r
436 VOID *EndOpCodeHandle;\r
437 EFI_IFR_GUID_LABEL *StartLabel;\r
438 EFI_IFR_GUID_LABEL *EndLabel;\r
439\r
440 EFI_HII_CONFIG_ACCESS_PROTOCOL *ConfigAccess;\r
441\r
442 DriverHandle = NULL;\r
443 ConfigAccess = &PrivateData->ConfigAccess;\r
444 Status = gBS->InstallMultipleProtocolInterfaces (\r
445 &DriverHandle,\r
446 &gEfiDevicePathProtocolGuid,\r
447 &mTcgHiiVendorDevicePath,\r
448 &gEfiHiiConfigAccessProtocolGuid,\r
449 ConfigAccess,\r
450 NULL\r
451 );\r
452 if (EFI_ERROR (Status)) {\r
453 return Status;\r
454 }\r
455\r
456 PrivateData->DriverHandle = DriverHandle;\r
457\r
458 //\r
459 // Publish the HII package list\r
460 //\r
461 HiiHandle = HiiAddPackages (\r
a0c56a82 462 &gTcgConfigFormSetGuid,\r
0c18794e 463 DriverHandle,\r
464 TcgConfigDxeStrings,\r
465 TcgConfigBin,\r
466 NULL\r
467 );\r
468 if (HiiHandle == NULL) {\r
469 gBS->UninstallMultipleProtocolInterfaces (\r
470 DriverHandle,\r
471 &gEfiDevicePathProtocolGuid,\r
472 &mTcgHiiVendorDevicePath,\r
473 &gEfiHiiConfigAccessProtocolGuid,\r
474 ConfigAccess,\r
475 NULL\r
476 ); \r
477\r
478 return EFI_OUT_OF_RESOURCES;\r
479 }\r
480 \r
481 PrivateData->HiiHandle = HiiHandle;\r
482\r
483 //\r
484 // Remove the Hide TPM question from the IFR\r
485 //\r
486 if (!PcdGetBool (PcdHideTpmSupport)) {\r
487 //\r
488 // Allocate space for creation of UpdateData Buffer\r
489 //\r
490 StartOpCodeHandle = HiiAllocateOpCodeHandle ();\r
491 ASSERT (StartOpCodeHandle != NULL);\r
492\r
493 EndOpCodeHandle = HiiAllocateOpCodeHandle ();\r
494 ASSERT (EndOpCodeHandle != NULL);\r
495\r
496 //\r
497 // Create Hii Extend Label OpCode as the start opcode\r
498 //\r
499 StartLabel = (EFI_IFR_GUID_LABEL *) HiiCreateGuidOpCode (StartOpCodeHandle, &gEfiIfrTianoGuid, NULL, sizeof (EFI_IFR_GUID_LABEL));\r
500 StartLabel->ExtendOpCode = EFI_IFR_EXTEND_OP_LABEL;\r
501 StartLabel->Number = LABEL_TCG_CONFIGURATION_HIDETPM;\r
502\r
503 //\r
504 // Create Hii Extend Label OpCode as the end opcode\r
505 //\r
506 EndLabel = (EFI_IFR_GUID_LABEL *) HiiCreateGuidOpCode (EndOpCodeHandle, &gEfiIfrTianoGuid, NULL, sizeof (EFI_IFR_GUID_LABEL));\r
507 EndLabel->ExtendOpCode = EFI_IFR_EXTEND_OP_LABEL;\r
508 EndLabel->Number = LABEL_END;\r
509 \r
510 HiiUpdateForm (HiiHandle, NULL, TCG_CONFIGURATION_FORM_ID, StartOpCodeHandle, EndOpCodeHandle);\r
511\r
512 HiiFreeOpCodeHandle (StartOpCodeHandle);\r
513 HiiFreeOpCodeHandle (EndOpCodeHandle);\r
514 }\r
515\r
516 return EFI_SUCCESS; \r
517}\r
518\r
519/**\r
520 This function removes TCG configuration Form.\r
521\r
522 @param[in, out] PrivateData Points to TCG configuration private data.\r
523\r
524**/\r
525VOID\r
526UninstallTcgConfigForm (\r
527 IN OUT TCG_CONFIG_PRIVATE_DATA *PrivateData\r
528 )\r
529{\r
530 //\r
531 // Uninstall HII package list\r
532 //\r
533 if (PrivateData->HiiHandle != NULL) {\r
534 HiiRemovePackages (PrivateData->HiiHandle);\r
535 PrivateData->HiiHandle = NULL;\r
536 }\r
537\r
538 //\r
539 // Uninstall HII Config Access Protocol\r
540 //\r
541 if (PrivateData->DriverHandle != NULL) {\r
542 gBS->UninstallMultipleProtocolInterfaces (\r
543 PrivateData->DriverHandle,\r
544 &gEfiDevicePathProtocolGuid,\r
545 &mTcgHiiVendorDevicePath,\r
546 &gEfiHiiConfigAccessProtocolGuid,\r
547 &PrivateData->ConfigAccess,\r
548 NULL\r
549 );\r
550 PrivateData->DriverHandle = NULL;\r
551 }\r
552 \r
553 FreePool (PrivateData);\r
554}\r