]> git.proxmox.com Git - mirror_edk2.git/blame - SecurityPkg/Tcg/TcgConfigDxe/TcgConfigImpl.c
Fix system reboot automatically if changing the value of [TPM operation]
[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
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
0c18794e 362 return EFI_SUCCESS;\r
363}\r
364\r
365/**\r
366 This function processes the results of changes in configuration.\r
367\r
368 @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.\r
369 @param[in] Action Specifies the type of action taken by the browser.\r
370 @param[in] QuestionId A unique value which is sent to the original\r
371 exporting driver so that it can identify the type\r
372 of data to expect.\r
373 @param[in] Type The type of value for the question.\r
374 @param[in] Value A pointer to the data being sent to the original\r
375 exporting driver.\r
376 @param[out] ActionRequest On return, points to the action requested by the\r
377 callback function.\r
378\r
379 @retval EFI_SUCCESS The callback successfully handled the action.\r
380 @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the\r
381 variable and its data.\r
382 @retval EFI_DEVICE_ERROR The variable could not be saved.\r
383 @retval EFI_UNSUPPORTED The specified Action is not supported by the\r
384 callback.\r
385\r
386**/\r
387EFI_STATUS\r
388EFIAPI\r
389TcgCallback (\r
390 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL *This,\r
391 IN EFI_BROWSER_ACTION Action,\r
392 IN EFI_QUESTION_ID QuestionId,\r
393 IN UINT8 Type,\r
394 IN EFI_IFR_TYPE_VALUE *Value,\r
395 OUT EFI_BROWSER_ACTION_REQUEST *ActionRequest\r
396 )\r
397{\r
398 if ((This == NULL) || (Value == NULL) || (ActionRequest == NULL)) {\r
399 return EFI_INVALID_PARAMETER;\r
400 }\r
401\r
fab10465 402 if ((Action != EFI_BROWSER_ACTION_CHANGED) || (QuestionId != KEY_TPM_ACTION)) {\r
0c18794e 403 return EFI_UNSUPPORTED;\r
404 }\r
405\r
406 SavePpRequest (Value->u8);\r
54a26282 407 *ActionRequest = EFI_BROWSER_ACTION_REQUEST_SUBMIT;\r
408 \r
0c18794e 409 return EFI_SUCCESS;\r
410}\r
411\r
412/**\r
413 This function publish the TCG configuration Form for TPM device.\r
414\r
415 @param[in, out] PrivateData Points to TCG configuration private data.\r
416\r
417 @retval EFI_SUCCESS HII Form is installed for this network device.\r
418 @retval EFI_OUT_OF_RESOURCES Not enough resource for HII Form installation.\r
419 @retval Others Other errors as indicated.\r
420\r
421**/\r
422EFI_STATUS\r
423InstallTcgConfigForm (\r
424 IN OUT TCG_CONFIG_PRIVATE_DATA *PrivateData\r
425 )\r
426{\r
427 EFI_STATUS Status;\r
428 EFI_HII_HANDLE HiiHandle;\r
429 EFI_HANDLE DriverHandle;\r
430 VOID *StartOpCodeHandle;\r
431 VOID *EndOpCodeHandle;\r
432 EFI_IFR_GUID_LABEL *StartLabel;\r
433 EFI_IFR_GUID_LABEL *EndLabel;\r
434\r
435 EFI_HII_CONFIG_ACCESS_PROTOCOL *ConfigAccess;\r
436\r
437 DriverHandle = NULL;\r
438 ConfigAccess = &PrivateData->ConfigAccess;\r
439 Status = gBS->InstallMultipleProtocolInterfaces (\r
440 &DriverHandle,\r
441 &gEfiDevicePathProtocolGuid,\r
442 &mTcgHiiVendorDevicePath,\r
443 &gEfiHiiConfigAccessProtocolGuid,\r
444 ConfigAccess,\r
445 NULL\r
446 );\r
447 if (EFI_ERROR (Status)) {\r
448 return Status;\r
449 }\r
450\r
451 PrivateData->DriverHandle = DriverHandle;\r
452\r
453 //\r
454 // Publish the HII package list\r
455 //\r
456 HiiHandle = HiiAddPackages (\r
a0c56a82 457 &gTcgConfigFormSetGuid,\r
0c18794e 458 DriverHandle,\r
459 TcgConfigDxeStrings,\r
460 TcgConfigBin,\r
461 NULL\r
462 );\r
463 if (HiiHandle == NULL) {\r
464 gBS->UninstallMultipleProtocolInterfaces (\r
465 DriverHandle,\r
466 &gEfiDevicePathProtocolGuid,\r
467 &mTcgHiiVendorDevicePath,\r
468 &gEfiHiiConfigAccessProtocolGuid,\r
469 ConfigAccess,\r
470 NULL\r
471 ); \r
472\r
473 return EFI_OUT_OF_RESOURCES;\r
474 }\r
475 \r
476 PrivateData->HiiHandle = HiiHandle;\r
477\r
478 //\r
479 // Remove the Hide TPM question from the IFR\r
480 //\r
481 if (!PcdGetBool (PcdHideTpmSupport)) {\r
482 //\r
483 // Allocate space for creation of UpdateData Buffer\r
484 //\r
485 StartOpCodeHandle = HiiAllocateOpCodeHandle ();\r
486 ASSERT (StartOpCodeHandle != NULL);\r
487\r
488 EndOpCodeHandle = HiiAllocateOpCodeHandle ();\r
489 ASSERT (EndOpCodeHandle != NULL);\r
490\r
491 //\r
492 // Create Hii Extend Label OpCode as the start opcode\r
493 //\r
494 StartLabel = (EFI_IFR_GUID_LABEL *) HiiCreateGuidOpCode (StartOpCodeHandle, &gEfiIfrTianoGuid, NULL, sizeof (EFI_IFR_GUID_LABEL));\r
495 StartLabel->ExtendOpCode = EFI_IFR_EXTEND_OP_LABEL;\r
496 StartLabel->Number = LABEL_TCG_CONFIGURATION_HIDETPM;\r
497\r
498 //\r
499 // Create Hii Extend Label OpCode as the end opcode\r
500 //\r
501 EndLabel = (EFI_IFR_GUID_LABEL *) HiiCreateGuidOpCode (EndOpCodeHandle, &gEfiIfrTianoGuid, NULL, sizeof (EFI_IFR_GUID_LABEL));\r
502 EndLabel->ExtendOpCode = EFI_IFR_EXTEND_OP_LABEL;\r
503 EndLabel->Number = LABEL_END;\r
504 \r
505 HiiUpdateForm (HiiHandle, NULL, TCG_CONFIGURATION_FORM_ID, StartOpCodeHandle, EndOpCodeHandle);\r
506\r
507 HiiFreeOpCodeHandle (StartOpCodeHandle);\r
508 HiiFreeOpCodeHandle (EndOpCodeHandle);\r
509 }\r
510\r
511 return EFI_SUCCESS; \r
512}\r
513\r
514/**\r
515 This function removes TCG configuration Form.\r
516\r
517 @param[in, out] PrivateData Points to TCG configuration private data.\r
518\r
519**/\r
520VOID\r
521UninstallTcgConfigForm (\r
522 IN OUT TCG_CONFIG_PRIVATE_DATA *PrivateData\r
523 )\r
524{\r
525 //\r
526 // Uninstall HII package list\r
527 //\r
528 if (PrivateData->HiiHandle != NULL) {\r
529 HiiRemovePackages (PrivateData->HiiHandle);\r
530 PrivateData->HiiHandle = NULL;\r
531 }\r
532\r
533 //\r
534 // Uninstall HII Config Access Protocol\r
535 //\r
536 if (PrivateData->DriverHandle != NULL) {\r
537 gBS->UninstallMultipleProtocolInterfaces (\r
538 PrivateData->DriverHandle,\r
539 &gEfiDevicePathProtocolGuid,\r
540 &mTcgHiiVendorDevicePath,\r
541 &gEfiHiiConfigAccessProtocolGuid,\r
542 &PrivateData->ConfigAccess,\r
543 NULL\r
544 );\r
545 PrivateData->DriverHandle = NULL;\r
546 }\r
547 \r
548 FreePool (PrivateData);\r
549}\r