]> git.proxmox.com Git - mirror_edk2.git/blame - SecurityPkg/Tcg/TcgConfigDxe/TcgConfigImpl.c
Remove MOR check box feature. should not have a switch to turn off MOR.
[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
54a26282 4Copyright (c) 2011 - 2012, Intel Corporation. All rights reserved.<BR>\r
0c18794e 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
607599bf 184 Configuration.TpmOperation = PHYSICAL_PRESENCE_ENABLE;\r
81b7a609 185 Configuration.HideTpm = (BOOLEAN) (PcdGetBool (PcdHideTpmSupport) && PcdGetBool (PcdHideTpm));\r
0c18794e 186 //\r
187 // Read the original value of HideTpm from PrivateData which won't be changed by Setup in this boot.\r
188 //\r
189 Configuration.OriginalHideTpm = PrivateData->HideTpm;\r
190\r
191 //\r
192 // Display current TPM state.\r
193 //\r
194 if (PrivateData->TcgProtocol != NULL) {\r
195 Status = GetTpmState (PrivateData->TcgProtocol, &TpmEnable, &TpmActivate);\r
196 if (EFI_ERROR (Status)) {\r
197 return Status;\r
198 }\r
199\r
200 UnicodeSPrint (\r
201 State,\r
202 sizeof (State),\r
203 L"%s, and %s",\r
204 TpmEnable ? L"Enabled" : L"Disabled",\r
205 TpmActivate ? L"Activated" : L"Deactivated"\r
206 );\r
207 Configuration.TpmEnable = TpmEnable;\r
208 Configuration.TpmActivate = TpmActivate;\r
209\r
210 HiiSetString (PrivateData->HiiHandle, STRING_TOKEN (STR_TPM_STATE_CONTENT), State, NULL);\r
211 }\r
212\r
213 BufferSize = sizeof (Configuration);\r
214 ConfigRequest = Request;\r
215 if ((Request == NULL) || (StrStr (Request, L"OFFSET") == NULL)) {\r
216 //\r
217 // Request has no request element, construct full request string.\r
218 // Allocate and fill a buffer large enough to hold the <ConfigHdr> template\r
219 // followed by "&OFFSET=0&WIDTH=WWWWWWWWWWWWWWWW" followed by a Null-terminator\r
220 //\r
a0c56a82 221 ConfigRequestHdr = HiiConstructConfigHdr (&gTcgConfigFormSetGuid, mTcgStorageName, PrivateData->DriverHandle);\r
0c18794e 222 Size = (StrLen (ConfigRequestHdr) + 32 + 1) * sizeof (CHAR16);\r
223 ConfigRequest = AllocateZeroPool (Size);\r
224 ASSERT (ConfigRequest != NULL);\r
225 AllocatedRequest = TRUE;\r
226 UnicodeSPrint (ConfigRequest, Size, L"%s&OFFSET=0&WIDTH=%016LX", ConfigRequestHdr, (UINT64) BufferSize);\r
227 FreePool (ConfigRequestHdr);\r
228 }\r
229\r
230 Status = gHiiConfigRouting->BlockToConfig (\r
231 gHiiConfigRouting,\r
232 ConfigRequest,\r
233 (UINT8 *) &Configuration,\r
234 BufferSize,\r
235 Results,\r
236 Progress\r
237 );\r
238 //\r
239 // Free the allocated config request string.\r
240 //\r
241 if (AllocatedRequest) {\r
242 FreePool (ConfigRequest);\r
243 }\r
244 //\r
245 // Set Progress string to the original request string.\r
246 //\r
247 if (Request == NULL) {\r
248 *Progress = NULL;\r
249 } else if (StrStr (Request, L"OFFSET") == NULL) {\r
250 *Progress = Request + StrLen (Request);\r
251 }\r
252\r
253 return Status;\r
254}\r
255\r
256/**\r
257 This function processes the results of changes in configuration.\r
258\r
259 @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.\r
260 @param[in] Configuration A null-terminated Unicode string in <ConfigResp>\r
261 format.\r
262 @param[out] Progress A pointer to a string filled in with the offset of\r
263 the most recent '&' before the first failing\r
264 name/value pair (or the beginning of the string if\r
265 the failure is in the first name/value pair) or\r
266 the terminating NULL if all was successful.\r
267\r
268 @retval EFI_SUCCESS The Results is processed successfully.\r
269 @retval EFI_INVALID_PARAMETER Configuration is NULL.\r
270 @retval EFI_NOT_FOUND Routing data doesn't match any storage in this\r
271 driver.\r
272\r
273**/\r
274EFI_STATUS\r
275EFIAPI\r
276TcgRouteConfig (\r
277 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This,\r
278 IN CONST EFI_STRING Configuration,\r
279 OUT EFI_STRING *Progress\r
280 )\r
281{\r
282 EFI_STATUS Status;\r
283 UINTN BufferSize;\r
284 TCG_CONFIGURATION TcgConfiguration;\r
285\r
286 if (Configuration == NULL || Progress == NULL) {\r
287 return EFI_INVALID_PARAMETER;\r
288 }\r
289\r
290 *Progress = Configuration;\r
a0c56a82 291 if (!HiiIsConfigHdrMatch (Configuration, &gTcgConfigFormSetGuid, mTcgStorageName)) {\r
0c18794e 292 return EFI_NOT_FOUND;\r
293 }\r
294\r
295 //\r
296 // Convert <ConfigResp> to buffer data by helper function ConfigToBlock()\r
297 //\r
298 BufferSize = sizeof (TCG_CONFIGURATION);\r
299 Status = gHiiConfigRouting->ConfigToBlock (\r
300 gHiiConfigRouting,\r
301 Configuration,\r
302 (UINT8 *) &TcgConfiguration,\r
303 &BufferSize,\r
304 Progress\r
305 );\r
306 if (EFI_ERROR (Status)) {\r
307 return Status;\r
308 }\r
309\r
0c18794e 310 PcdSetBool (PcdHideTpm, TcgConfiguration.HideTpm);\r
311\r
312 return EFI_SUCCESS;\r
313}\r
314\r
315/**\r
316 Save TPM request to variable space.\r
317\r
318 @param[in] PpRequest Physical Presence request command.\r
319\r
320 @retval EFI_SUCCESS The operation is finished successfully.\r
321 @retval Others Other errors as indicated.\r
322\r
323**/\r
324EFI_STATUS\r
325SavePpRequest (\r
326 IN UINT8 PpRequest\r
327 )\r
328{\r
329 EFI_STATUS Status;\r
330 UINTN DataSize;\r
331 EFI_PHYSICAL_PRESENCE PpData;\r
332\r
333 //\r
334 // Save TPM command to variable.\r
335 //\r
336 DataSize = sizeof (EFI_PHYSICAL_PRESENCE);\r
337 Status = gRT->GetVariable (\r
338 PHYSICAL_PRESENCE_VARIABLE,\r
339 &gEfiPhysicalPresenceGuid,\r
340 NULL,\r
341 &DataSize,\r
342 &PpData\r
343 );\r
344 if (EFI_ERROR (Status)) {\r
345 return Status;\r
346 } \r
347 \r
348 PpData.PPRequest = PpRequest;\r
349 Status = gRT->SetVariable (\r
350 PHYSICAL_PRESENCE_VARIABLE,\r
351 &gEfiPhysicalPresenceGuid,\r
352 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r
353 DataSize,\r
354 &PpData\r
355 );\r
356 if (EFI_ERROR(Status)) {\r
357 return Status;\r
358 }\r
359\r
0c18794e 360 return EFI_SUCCESS;\r
361}\r
362\r
363/**\r
364 This function processes the results of changes in configuration.\r
365\r
366 @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.\r
367 @param[in] Action Specifies the type of action taken by the browser.\r
368 @param[in] QuestionId A unique value which is sent to the original\r
369 exporting driver so that it can identify the type\r
370 of data to expect.\r
371 @param[in] Type The type of value for the question.\r
372 @param[in] Value A pointer to the data being sent to the original\r
373 exporting driver.\r
374 @param[out] ActionRequest On return, points to the action requested by the\r
375 callback function.\r
376\r
377 @retval EFI_SUCCESS The callback successfully handled the action.\r
378 @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the\r
379 variable and its data.\r
380 @retval EFI_DEVICE_ERROR The variable could not be saved.\r
381 @retval EFI_UNSUPPORTED The specified Action is not supported by the\r
382 callback.\r
383\r
384**/\r
385EFI_STATUS\r
386EFIAPI\r
387TcgCallback (\r
388 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This,\r
389 IN EFI_BROWSER_ACTION Action,\r
390 IN EFI_QUESTION_ID QuestionId,\r
391 IN UINT8 Type,\r
392 IN EFI_IFR_TYPE_VALUE *Value,\r
393 OUT EFI_BROWSER_ACTION_REQUEST *ActionRequest\r
394 )\r
395{\r
396 if ((This == NULL) || (Value == NULL) || (ActionRequest == NULL)) {\r
397 return EFI_INVALID_PARAMETER;\r
398 }\r
399\r
fab10465 400 if ((Action != EFI_BROWSER_ACTION_CHANGED) || (QuestionId != KEY_TPM_ACTION)) {\r
0c18794e 401 return EFI_UNSUPPORTED;\r
402 }\r
403\r
404 SavePpRequest (Value->u8);\r
54a26282 405 *ActionRequest = EFI_BROWSER_ACTION_REQUEST_SUBMIT;\r
406 \r
0c18794e 407 return EFI_SUCCESS;\r
408}\r
409\r
410/**\r
411 This function publish the TCG configuration Form for TPM device.\r
412\r
413 @param[in, out] PrivateData Points to TCG configuration private data.\r
414\r
415 @retval EFI_SUCCESS HII Form is installed for this network device.\r
416 @retval EFI_OUT_OF_RESOURCES Not enough resource for HII Form installation.\r
417 @retval Others Other errors as indicated.\r
418\r
419**/\r
420EFI_STATUS\r
421InstallTcgConfigForm (\r
422 IN OUT TCG_CONFIG_PRIVATE_DATA *PrivateData\r
423 )\r
424{\r
425 EFI_STATUS Status;\r
426 EFI_HII_HANDLE HiiHandle;\r
427 EFI_HANDLE DriverHandle;\r
428 VOID *StartOpCodeHandle;\r
429 VOID *EndOpCodeHandle;\r
430 EFI_IFR_GUID_LABEL *StartLabel;\r
431 EFI_IFR_GUID_LABEL *EndLabel;\r
432\r
433 EFI_HII_CONFIG_ACCESS_PROTOCOL *ConfigAccess;\r
434\r
435 DriverHandle = NULL;\r
436 ConfigAccess = &PrivateData->ConfigAccess;\r
437 Status = gBS->InstallMultipleProtocolInterfaces (\r
438 &DriverHandle,\r
439 &gEfiDevicePathProtocolGuid,\r
440 &mTcgHiiVendorDevicePath,\r
441 &gEfiHiiConfigAccessProtocolGuid,\r
442 ConfigAccess,\r
443 NULL\r
444 );\r
445 if (EFI_ERROR (Status)) {\r
446 return Status;\r
447 }\r
448\r
449 PrivateData->DriverHandle = DriverHandle;\r
450\r
451 //\r
452 // Publish the HII package list\r
453 //\r
454 HiiHandle = HiiAddPackages (\r
a0c56a82 455 &gTcgConfigFormSetGuid,\r
0c18794e 456 DriverHandle,\r
457 TcgConfigDxeStrings,\r
458 TcgConfigBin,\r
459 NULL\r
460 );\r
461 if (HiiHandle == NULL) {\r
462 gBS->UninstallMultipleProtocolInterfaces (\r
463 DriverHandle,\r
464 &gEfiDevicePathProtocolGuid,\r
465 &mTcgHiiVendorDevicePath,\r
466 &gEfiHiiConfigAccessProtocolGuid,\r
467 ConfigAccess,\r
468 NULL\r
469 ); \r
470\r
471 return EFI_OUT_OF_RESOURCES;\r
472 }\r
473 \r
474 PrivateData->HiiHandle = HiiHandle;\r
475\r
476 //\r
477 // Remove the Hide TPM question from the IFR\r
478 //\r
479 if (!PcdGetBool (PcdHideTpmSupport)) {\r
480 //\r
481 // Allocate space for creation of UpdateData Buffer\r
482 //\r
483 StartOpCodeHandle = HiiAllocateOpCodeHandle ();\r
484 ASSERT (StartOpCodeHandle != NULL);\r
485\r
486 EndOpCodeHandle = HiiAllocateOpCodeHandle ();\r
487 ASSERT (EndOpCodeHandle != NULL);\r
488\r
489 //\r
490 // Create Hii Extend Label OpCode as the start opcode\r
491 //\r
492 StartLabel = (EFI_IFR_GUID_LABEL *) HiiCreateGuidOpCode (StartOpCodeHandle, &gEfiIfrTianoGuid, NULL, sizeof (EFI_IFR_GUID_LABEL));\r
493 StartLabel->ExtendOpCode = EFI_IFR_EXTEND_OP_LABEL;\r
494 StartLabel->Number = LABEL_TCG_CONFIGURATION_HIDETPM;\r
495\r
496 //\r
497 // Create Hii Extend Label OpCode as the end opcode\r
498 //\r
499 EndLabel = (EFI_IFR_GUID_LABEL *) HiiCreateGuidOpCode (EndOpCodeHandle, &gEfiIfrTianoGuid, NULL, sizeof (EFI_IFR_GUID_LABEL));\r
500 EndLabel->ExtendOpCode = EFI_IFR_EXTEND_OP_LABEL;\r
501 EndLabel->Number = LABEL_END;\r
502 \r
503 HiiUpdateForm (HiiHandle, NULL, TCG_CONFIGURATION_FORM_ID, StartOpCodeHandle, EndOpCodeHandle);\r
504\r
505 HiiFreeOpCodeHandle (StartOpCodeHandle);\r
506 HiiFreeOpCodeHandle (EndOpCodeHandle);\r
507 }\r
508\r
509 return EFI_SUCCESS; \r
510}\r
511\r
512/**\r
513 This function removes TCG configuration Form.\r
514\r
515 @param[in, out] PrivateData Points to TCG configuration private data.\r
516\r
517**/\r
518VOID\r
519UninstallTcgConfigForm (\r
520 IN OUT TCG_CONFIG_PRIVATE_DATA *PrivateData\r
521 )\r
522{\r
523 //\r
524 // Uninstall HII package list\r
525 //\r
526 if (PrivateData->HiiHandle != NULL) {\r
527 HiiRemovePackages (PrivateData->HiiHandle);\r
528 PrivateData->HiiHandle = NULL;\r
529 }\r
530\r
531 //\r
532 // Uninstall HII Config Access Protocol\r
533 //\r
534 if (PrivateData->DriverHandle != NULL) {\r
535 gBS->UninstallMultipleProtocolInterfaces (\r
536 PrivateData->DriverHandle,\r
537 &gEfiDevicePathProtocolGuid,\r
538 &mTcgHiiVendorDevicePath,\r
539 &gEfiHiiConfigAccessProtocolGuid,\r
540 &PrivateData->ConfigAccess,\r
541 NULL\r
542 );\r
543 PrivateData->DriverHandle = NULL;\r
544 }\r
545 \r
546 FreePool (PrivateData);\r
547}\r