3 Execute pending TPM requests from OS or BIOS and Lock TPM.
5 Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 #include <Protocol/TcgService.h>
19 #include <Library/DebugLib.h>
20 #include <Library/BaseMemoryLib.h>
21 #include <Library/UefiRuntimeServicesTableLib.h>
22 #include <Library/UefiDriverEntryPoint.h>
23 #include <Library/UefiBootServicesTableLib.h>
24 #include <Library/UefiLib.h>
25 #include <Library/MemoryAllocationLib.h>
26 #include <Library/PrintLib.h>
27 #include <Library/HiiLib.h>
28 #include <Guid/EventGroup.h>
29 #include <Guid/PhysicalPresenceData.h>
31 #define TPM_PP_USER_ABORT ((TPM_RESULT)(-0x10))
32 #define TPM_PP_BIOS_FAILURE ((TPM_RESULT)(-0x0f))
33 #define CONFIRM_BUFFER_SIZE 4096
35 EFI_HII_HANDLE mPpStringPackHandle
;
38 Get string by string id from HII Interface.
40 @param[in] Id String ID.
42 @retval CHAR16 * String from ID.
43 @retval NULL If error occurs.
47 PhysicalPresenceGetStringById (
51 return HiiGetString (mPpStringPackHandle
, Id
, NULL
);
55 Get TPM physical presence permanent flags.
57 @param[in] TcgProtocol EFI TCG Protocol instance.
58 @param[out] LifetimeLock physicalPresenceLifetimeLock permanent flag.
59 @param[out] CmdEnable physicalPresenceCMDEnable permanent flag.
61 @retval EFI_SUCCESS Flags were returns successfully.
62 @retval other Failed to locate EFI TCG Protocol.
67 IN EFI_TCG_PROTOCOL
*TcgProtocol
,
68 OUT BOOLEAN
*LifetimeLock
,
69 OUT BOOLEAN
*CmdEnable
73 TPM_RQU_COMMAND_HDR
*TpmRqu
;
74 TPM_RSP_COMMAND_HDR
*TpmRsp
;
76 UINT8 SendBuffer
[sizeof (*TpmRqu
) + sizeof (UINT32
) * 3];
77 TPM_PERMANENT_FLAGS
*TpmPermanentFlags
;
81 // Fill request header
83 TpmRsp
= (TPM_RSP_COMMAND_HDR
*)RecvBuffer
;
84 TpmRqu
= (TPM_RQU_COMMAND_HDR
*)SendBuffer
;
86 TpmRqu
->tag
= SwapBytes16 (TPM_TAG_RQU_COMMAND
);
87 TpmRqu
->paramSize
= SwapBytes32 (sizeof (SendBuffer
));
88 TpmRqu
->ordinal
= SwapBytes32 (TPM_ORD_GetCapability
);
91 // Set request parameter
93 SendBufPtr
= (UINT32
*)(TpmRqu
+ 1);
94 WriteUnaligned32 (SendBufPtr
++, SwapBytes32 (TPM_CAP_FLAG
));
95 WriteUnaligned32 (SendBufPtr
++, SwapBytes32 (sizeof (TPM_CAP_FLAG_PERMANENT
)));
96 WriteUnaligned32 (SendBufPtr
, SwapBytes32 (TPM_CAP_FLAG_PERMANENT
));
98 Status
= TcgProtocol
->PassThroughToTpm (
105 ASSERT_EFI_ERROR (Status
);
106 ASSERT (TpmRsp
->tag
== SwapBytes16 (TPM_TAG_RSP_COMMAND
));
107 ASSERT (TpmRsp
->returnCode
== 0);
109 TpmPermanentFlags
= (TPM_PERMANENT_FLAGS
*)&RecvBuffer
[sizeof (TPM_RSP_COMMAND_HDR
) + sizeof (UINT32
)];
111 if (LifetimeLock
!= NULL
) {
112 *LifetimeLock
= TpmPermanentFlags
->physicalPresenceLifetimeLock
;
115 if (CmdEnable
!= NULL
) {
116 *CmdEnable
= TpmPermanentFlags
->physicalPresenceCMDEnable
;
123 Issue TSC_PhysicalPresence command to TPM.
125 @param[in] TcgProtocol EFI TCG Protocol instance.
126 @param[in] PhysicalPresence The state to set the TPM's Physical Presence flags.
128 @retval EFI_SUCCESS TPM executed the command successfully.
129 @retval EFI_SECURITY_VIOLATION TPM returned error when executing the command.
130 @retval other Failed to locate EFI TCG Protocol.
134 TpmPhysicalPresence (
135 IN EFI_TCG_PROTOCOL
*TcgProtocol
,
136 IN TPM_PHYSICAL_PRESENCE PhysicalPresence
140 TPM_RQU_COMMAND_HDR
*TpmRqu
;
141 TPM_PHYSICAL_PRESENCE
*TpmPp
;
142 TPM_RSP_COMMAND_HDR TpmRsp
;
143 UINT8 Buffer
[sizeof (*TpmRqu
) + sizeof (*TpmPp
)];
145 TpmRqu
= (TPM_RQU_COMMAND_HDR
*)Buffer
;
146 TpmPp
= (TPM_PHYSICAL_PRESENCE
*)(TpmRqu
+ 1);
148 TpmRqu
->tag
= SwapBytes16 (TPM_TAG_RQU_COMMAND
);
149 TpmRqu
->paramSize
= SwapBytes32 (sizeof (Buffer
));
150 TpmRqu
->ordinal
= SwapBytes32 (TSC_ORD_PhysicalPresence
);
151 WriteUnaligned16 (TpmPp
, (TPM_PHYSICAL_PRESENCE
) SwapBytes16 (PhysicalPresence
));
153 Status
= TcgProtocol
->PassThroughToTpm (
160 ASSERT_EFI_ERROR (Status
);
161 ASSERT (TpmRsp
.tag
== SwapBytes16 (TPM_TAG_RSP_COMMAND
));
162 if (TpmRsp
.returnCode
!= 0) {
164 // If it fails, some requirements may be needed for this command.
166 return EFI_SECURITY_VIOLATION
;
173 Issue a TPM command for which no additional output data will be returned.
175 @param[in] TcgProtocol EFI TCG Protocol instance.
176 @param[in] Ordinal TPM command code.
177 @param[in] AdditionalParameterSize Additional parameter size.
178 @param[in] AdditionalParameters Pointer to the Additional paramaters.
180 @retval TPM_PP_BIOS_FAILURE Error occurred during sending command to TPM or
181 receiving response from TPM.
182 @retval Others Return code from the TPM device after command execution.
186 TpmCommandNoReturnData (
187 IN EFI_TCG_PROTOCOL
*TcgProtocol
,
188 IN TPM_COMMAND_CODE Ordinal
,
189 IN UINTN AdditionalParameterSize
,
190 IN VOID
*AdditionalParameters
194 TPM_RQU_COMMAND_HDR
*TpmRqu
;
195 TPM_RSP_COMMAND_HDR TpmRsp
;
198 TpmRqu
= (TPM_RQU_COMMAND_HDR
*) AllocatePool (sizeof (*TpmRqu
) + AdditionalParameterSize
);
199 if (TpmRqu
== NULL
) {
200 return TPM_PP_BIOS_FAILURE
;
203 TpmRqu
->tag
= SwapBytes16 (TPM_TAG_RQU_COMMAND
);
204 Size
= (UINT32
)(sizeof (*TpmRqu
) + AdditionalParameterSize
);
205 TpmRqu
->paramSize
= SwapBytes32 (Size
);
206 TpmRqu
->ordinal
= SwapBytes32 (Ordinal
);
207 CopyMem (TpmRqu
+ 1, AdditionalParameters
, AdditionalParameterSize
);
209 Status
= TcgProtocol
->PassThroughToTpm (
213 (UINT32
)sizeof (TpmRsp
),
217 if (EFI_ERROR (Status
) || (TpmRsp
.tag
!= SwapBytes16 (TPM_TAG_RSP_COMMAND
))) {
218 return TPM_PP_BIOS_FAILURE
;
220 return SwapBytes32 (TpmRsp
.returnCode
);
224 Execute physical presence operation requested by the OS.
226 @param[in] TcgProtocol EFI TCG Protocol instance.
227 @param[in] CommandCode Physical presence operation value.
228 @param[in, out] PpiFlags The physical presence interface flags.
230 @retval TPM_PP_BIOS_FAILURE Unknown physical presence operation.
231 @retval TPM_PP_BIOS_FAILURE Error occurred during sending command to TPM or
232 receiving response from TPM.
233 @retval Others Return code from the TPM device after command execution.
237 ExecutePhysicalPresence (
238 IN EFI_TCG_PROTOCOL
*TcgProtocol
,
239 IN UINT8 CommandCode
,
240 IN OUT UINT8
*PpiFlags
244 TPM_RESULT TpmResponse
;
247 switch (CommandCode
) {
248 case PHYSICAL_PRESENCE_ENABLE
:
249 return TpmCommandNoReturnData (
251 TPM_ORD_PhysicalEnable
,
256 case PHYSICAL_PRESENCE_DISABLE
:
257 return TpmCommandNoReturnData (
259 TPM_ORD_PhysicalDisable
,
264 case PHYSICAL_PRESENCE_ACTIVATE
:
266 return TpmCommandNoReturnData (
268 TPM_ORD_PhysicalSetDeactivated
,
273 case PHYSICAL_PRESENCE_DEACTIVATE
:
275 return TpmCommandNoReturnData (
277 TPM_ORD_PhysicalSetDeactivated
,
282 case PHYSICAL_PRESENCE_CLEAR
:
283 return TpmCommandNoReturnData (
290 case PHYSICAL_PRESENCE_ENABLE_ACTIVATE
:
291 TpmResponse
= ExecutePhysicalPresence (TcgProtocol
, PHYSICAL_PRESENCE_ENABLE
, PpiFlags
);
292 if (TpmResponse
== 0) {
293 TpmResponse
= ExecutePhysicalPresence (TcgProtocol
, PHYSICAL_PRESENCE_ACTIVATE
, PpiFlags
);
297 case PHYSICAL_PRESENCE_DEACTIVATE_DISABLE
:
298 TpmResponse
= ExecutePhysicalPresence (TcgProtocol
, PHYSICAL_PRESENCE_DEACTIVATE
, PpiFlags
);
299 if (TpmResponse
== 0) {
300 TpmResponse
= ExecutePhysicalPresence (TcgProtocol
, PHYSICAL_PRESENCE_DISABLE
, PpiFlags
);
304 case PHYSICAL_PRESENCE_SET_OWNER_INSTALL_TRUE
:
306 return TpmCommandNoReturnData (
308 TPM_ORD_SetOwnerInstall
,
313 case PHYSICAL_PRESENCE_SET_OWNER_INSTALL_FALSE
:
315 return TpmCommandNoReturnData (
317 TPM_ORD_SetOwnerInstall
,
322 case PHYSICAL_PRESENCE_ENABLE_ACTIVATE_OWNER_TRUE
:
324 // PHYSICAL_PRESENCE_ENABLE_ACTIVATE + PHYSICAL_PRESENCE_SET_OWNER_INSTALL_TRUE
325 // PHYSICAL_PRESENCE_SET_OWNER_INSTALL_TRUE will be executed after reboot
327 if ((*PpiFlags
& FLAG_RESET_TRACK
) == 0) {
328 TpmResponse
= ExecutePhysicalPresence (TcgProtocol
, PHYSICAL_PRESENCE_ENABLE_ACTIVATE
, PpiFlags
);
329 *PpiFlags
|= FLAG_RESET_TRACK
;
331 TpmResponse
= ExecutePhysicalPresence (TcgProtocol
, PHYSICAL_PRESENCE_SET_OWNER_INSTALL_TRUE
, PpiFlags
);
332 *PpiFlags
&= ~FLAG_RESET_TRACK
;
336 case PHYSICAL_PRESENCE_DEACTIVATE_DISABLE_OWNER_FALSE
:
337 TpmResponse
= ExecutePhysicalPresence (TcgProtocol
, PHYSICAL_PRESENCE_SET_OWNER_INSTALL_FALSE
, PpiFlags
);
338 if (TpmResponse
== 0) {
339 TpmResponse
= ExecutePhysicalPresence (TcgProtocol
, PHYSICAL_PRESENCE_DEACTIVATE_DISABLE
, PpiFlags
);
343 case PHYSICAL_PRESENCE_DEFERRED_PP_UNOWNERED_FIELD_UPGRADE
:
344 InData
[0] = SwapBytes32 (TPM_SET_STCLEAR_DATA
); // CapabilityArea
345 InData
[1] = SwapBytes32 (sizeof(UINT32
)); // SubCapSize
346 InData
[2] = SwapBytes32 (TPM_SD_DEFERREDPHYSICALPRESENCE
); // SubCap
347 InData
[3] = SwapBytes32 (sizeof(UINT32
)); // SetValueSize
348 InData
[4] = SwapBytes32 (1); // UnownedFieldUpgrade; bit0
349 return TpmCommandNoReturnData (
351 TPM_ORD_SetCapability
,
356 case PHYSICAL_PRESENCE_SET_OPERATOR_AUTH
:
358 // TPM_SetOperatorAuth
359 // This command requires UI to prompt user for Auth data
360 // Here it is NOT implemented
362 return TPM_PP_BIOS_FAILURE
;
364 case PHYSICAL_PRESENCE_CLEAR_ENABLE_ACTIVATE
:
365 TpmResponse
= ExecutePhysicalPresence (TcgProtocol
, PHYSICAL_PRESENCE_CLEAR
, PpiFlags
);
366 if (TpmResponse
== 0) {
367 TpmResponse
= ExecutePhysicalPresence (TcgProtocol
, PHYSICAL_PRESENCE_ENABLE_ACTIVATE
, PpiFlags
);
371 case PHYSICAL_PRESENCE_SET_NO_PPI_PROVISION_FALSE
:
372 *PpiFlags
&= ~FLAG_NO_PPI_PROVISION
;
375 case PHYSICAL_PRESENCE_SET_NO_PPI_PROVISION_TRUE
:
376 *PpiFlags
|= FLAG_NO_PPI_PROVISION
;
379 case PHYSICAL_PRESENCE_SET_NO_PPI_CLEAR_FALSE
:
380 *PpiFlags
&= ~FLAG_NO_PPI_CLEAR
;
383 case PHYSICAL_PRESENCE_SET_NO_PPI_CLEAR_TRUE
:
384 *PpiFlags
|= FLAG_NO_PPI_CLEAR
;
387 case PHYSICAL_PRESENCE_SET_NO_PPI_MAINTENANCE_FALSE
:
388 *PpiFlags
&= ~FLAG_NO_PPI_MAINTENANCE
;
391 case PHYSICAL_PRESENCE_SET_NO_PPI_MAINTENANCE_TRUE
:
392 *PpiFlags
|= FLAG_NO_PPI_MAINTENANCE
;
395 case PHYSICAL_PRESENCE_ENABLE_ACTIVATE_CLEAR
:
396 TpmResponse
= ExecutePhysicalPresence (TcgProtocol
, PHYSICAL_PRESENCE_ENABLE_ACTIVATE
, PpiFlags
);
397 if (TpmResponse
== 0) {
398 TpmResponse
= ExecutePhysicalPresence (TcgProtocol
, PHYSICAL_PRESENCE_CLEAR
, PpiFlags
);
402 case PHYSICAL_PRESENCE_ENABLE_ACTIVATE_CLEAR_ENABLE_ACTIVATE
:
404 // PHYSICAL_PRESENCE_ENABLE_ACTIVATE + PHYSICAL_PRESENCE_CLEAR_ENABLE_ACTIVATE
405 // PHYSICAL_PRESENCE_CLEAR_ENABLE_ACTIVATE will be executed atfer reboot.
407 if ((*PpiFlags
& FLAG_RESET_TRACK
) == 0) {
408 TpmResponse
= ExecutePhysicalPresence (TcgProtocol
, PHYSICAL_PRESENCE_ENABLE_ACTIVATE
, PpiFlags
);
409 *PpiFlags
|= FLAG_RESET_TRACK
;
411 TpmResponse
= ExecutePhysicalPresence (TcgProtocol
, PHYSICAL_PRESENCE_CLEAR_ENABLE_ACTIVATE
, PpiFlags
);
412 *PpiFlags
&= ~FLAG_RESET_TRACK
;
419 return TPM_PP_BIOS_FAILURE
;
424 Read the specified key for user confirmation.
426 @param[in] CautionKey If true, F12 is used as confirm key;
427 If false, F10 is used as confirm key.
429 @retval TRUE User confirmed the changes by input.
430 @retval FALSE User discarded the changes.
435 IN BOOLEAN CautionKey
444 Status
= gBS
->CheckEvent (gST
->ConIn
->WaitForKey
);
445 if (!EFI_ERROR (Status
)) {
446 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
447 if (Key
.ScanCode
== SCAN_ESC
) {
448 InputKey
= Key
.ScanCode
;
450 if ((Key
.ScanCode
== SCAN_F10
) && !CautionKey
) {
451 InputKey
= Key
.ScanCode
;
453 if ((Key
.ScanCode
== SCAN_F12
) && CautionKey
) {
454 InputKey
= Key
.ScanCode
;
457 } while (InputKey
== 0);
459 if (InputKey
!= SCAN_ESC
) {
467 The constructor function register UNI strings into imageHandle.
469 It will ASSERT() if that operation fails and it will always return EFI_SUCCESS.
471 @param ImageHandle The firmware allocated handle for the EFI image.
472 @param SystemTable A pointer to the EFI System Table.
474 @retval EFI_SUCCESS The constructor successfully added string package.
475 @retval Other value The constructor can't add string package.
480 TcgPhysicalPresenceLibConstructor (
481 IN EFI_HANDLE ImageHandle
,
482 IN EFI_SYSTEM_TABLE
*SystemTable
485 mPpStringPackHandle
= HiiAddPackages (&gEfiPhysicalPresenceGuid
, ImageHandle
, DxeTcgPhysicalPresenceLibStrings
, NULL
);
486 ASSERT (mPpStringPackHandle
!= NULL
);
492 Display the confirm text and get user confirmation.
494 @param[in] TpmPpCommand The requested TPM physical presence command.
496 @retval TRUE The user has confirmed the changes.
497 @retval FALSE The user doesn't confirm the changes.
501 IN UINT8 TpmPpCommand
514 BufSize
= CONFIRM_BUFFER_SIZE
;
515 ConfirmText
= AllocateZeroPool (BufSize
);
516 ASSERT (ConfirmText
!= NULL
);
518 switch (TpmPpCommand
) {
519 case PHYSICAL_PRESENCE_ENABLE
:
520 TmpStr2
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_ENABLE
));
522 TmpStr1
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_HEAD_STR
));
523 UnicodeSPrint (ConfirmText
, BufSize
, TmpStr1
, TmpStr2
);
526 TmpStr1
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_ACCEPT_KEY
));
527 StrnCat (ConfirmText
, TmpStr1
, (BufSize
/ sizeof (CHAR16
*)) - StrLen (ConfirmText
) - 1);
531 case PHYSICAL_PRESENCE_DISABLE
:
532 TmpStr2
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_DISABLE
));
534 TmpStr1
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_HEAD_STR
));
535 UnicodeSPrint (ConfirmText
, BufSize
, TmpStr1
, TmpStr2
);
538 TmpStr1
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_WARNING
));
539 StrnCat (ConfirmText
, TmpStr1
, (BufSize
/ sizeof (CHAR16
*)) - StrLen (ConfirmText
) - 1);
542 TmpStr1
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_ACCEPT_KEY
));
543 StrnCat (ConfirmText
, TmpStr1
, (BufSize
/ sizeof (CHAR16
*)) - StrLen (ConfirmText
) - 1);
547 case PHYSICAL_PRESENCE_ACTIVATE
:
548 TmpStr2
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_ACTIVATE
));
550 TmpStr1
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_HEAD_STR
));
551 UnicodeSPrint (ConfirmText
, BufSize
, TmpStr1
, TmpStr2
);
554 TmpStr1
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_ACCEPT_KEY
));
555 StrnCat (ConfirmText
, TmpStr1
, (BufSize
/ sizeof (CHAR16
*)) - StrLen (ConfirmText
) - 1);
559 case PHYSICAL_PRESENCE_DEACTIVATE
:
560 TmpStr2
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_DEACTIVATE
));
562 TmpStr1
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_HEAD_STR
));
563 UnicodeSPrint (ConfirmText
, BufSize
, TmpStr1
, TmpStr2
);
566 TmpStr1
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_WARNING
));
567 StrnCat (ConfirmText
, TmpStr1
, (BufSize
/ sizeof (CHAR16
*)) - StrLen (ConfirmText
) - 1);
570 TmpStr1
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_ACCEPT_KEY
));
571 StrnCat (ConfirmText
, TmpStr1
, (BufSize
/ sizeof (CHAR16
*)) - StrLen (ConfirmText
) - 1);
575 case PHYSICAL_PRESENCE_CLEAR
:
577 TmpStr2
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_CLEAR
));
579 TmpStr1
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_HEAD_STR
));
580 UnicodeSPrint (ConfirmText
, BufSize
, TmpStr1
, TmpStr2
);
583 TmpStr1
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_WARNING_CLEAR
));
584 StrnCat (ConfirmText
, TmpStr1
, (BufSize
/ sizeof (CHAR16
*)) - StrLen (ConfirmText
) - 1);
585 StrnCat (ConfirmText
, L
" \n\n", (BufSize
/ sizeof (CHAR16
*)) - StrLen (ConfirmText
) - 1);
588 TmpStr1
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_CAUTION_KEY
));
589 StrnCat (ConfirmText
, TmpStr1
, (BufSize
/ sizeof (CHAR16
*)) - StrLen (ConfirmText
) - 1);
593 case PHYSICAL_PRESENCE_ENABLE_ACTIVATE
:
594 TmpStr2
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_ENABLE_ACTIVATE
));
596 TmpStr1
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_HEAD_STR
));
597 UnicodeSPrint (ConfirmText
, BufSize
, TmpStr1
, TmpStr2
);
600 TmpStr1
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_NOTE_ON
));
601 StrnCat (ConfirmText
, TmpStr1
, (BufSize
/ sizeof (CHAR16
*)) - StrLen (ConfirmText
) - 1);
604 TmpStr1
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_ACCEPT_KEY
));
605 StrnCat (ConfirmText
, TmpStr1
, (BufSize
/ sizeof (CHAR16
*)) - StrLen (ConfirmText
) - 1);
609 case PHYSICAL_PRESENCE_DEACTIVATE_DISABLE
:
610 TmpStr2
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_DEACTIVATE_DISABLE
));
612 TmpStr1
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_HEAD_STR
));
613 UnicodeSPrint (ConfirmText
, BufSize
, TmpStr1
, TmpStr2
);
616 TmpStr1
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_NOTE_OFF
));
617 StrnCat (ConfirmText
, TmpStr1
, (BufSize
/ sizeof (CHAR16
*)) - StrLen (ConfirmText
) - 1);
620 TmpStr1
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_WARNING
));
621 StrnCat (ConfirmText
, TmpStr1
, (BufSize
/ sizeof (CHAR16
*)) - StrLen (ConfirmText
) - 1);
624 TmpStr1
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_ACCEPT_KEY
));
625 StrnCat (ConfirmText
, TmpStr1
, (BufSize
/ sizeof (CHAR16
*)) - StrLen (ConfirmText
) - 1);
629 case PHYSICAL_PRESENCE_SET_OWNER_INSTALL_TRUE
:
630 TmpStr2
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_ALLOW_TAKE_OWNERSHIP
));
632 TmpStr1
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_HEAD_STR
));
633 UnicodeSPrint (ConfirmText
, BufSize
, TmpStr1
, TmpStr2
);
636 TmpStr1
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_ACCEPT_KEY
));
637 StrnCat (ConfirmText
, TmpStr1
, (BufSize
/ sizeof (CHAR16
*)) - StrLen (ConfirmText
) - 1);
641 case PHYSICAL_PRESENCE_SET_OWNER_INSTALL_FALSE
:
642 TmpStr2
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_DISALLOW_TAKE_OWNERSHIP
));
644 TmpStr1
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_HEAD_STR
));
645 UnicodeSPrint (ConfirmText
, BufSize
, TmpStr1
, TmpStr2
);
648 TmpStr1
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_ACCEPT_KEY
));
649 StrnCat (ConfirmText
, TmpStr1
, (BufSize
/ sizeof (CHAR16
*)) - StrLen (ConfirmText
) - 1);
653 case PHYSICAL_PRESENCE_ENABLE_ACTIVATE_OWNER_TRUE
:
654 TmpStr2
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_TURN_ON
));
656 TmpStr1
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_HEAD_STR
));
657 UnicodeSPrint (ConfirmText
, BufSize
, TmpStr1
, TmpStr2
);
660 TmpStr1
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_NOTE_ON
));
661 StrnCat (ConfirmText
, TmpStr1
, (BufSize
/ sizeof (CHAR16
*)) - StrLen (ConfirmText
) - 1);
664 TmpStr1
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_ACCEPT_KEY
));
665 StrnCat (ConfirmText
, TmpStr1
, (BufSize
/ sizeof (CHAR16
*)) - StrLen (ConfirmText
) - 1);
669 case PHYSICAL_PRESENCE_DEACTIVATE_DISABLE_OWNER_FALSE
:
670 TmpStr2
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_TURN_OFF
));
672 TmpStr1
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_HEAD_STR
));
673 UnicodeSPrint (ConfirmText
, BufSize
, TmpStr1
, TmpStr2
);
676 TmpStr1
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_NOTE_OFF
));
677 StrnCat (ConfirmText
, TmpStr1
, (BufSize
/ sizeof (CHAR16
*)) - StrLen (ConfirmText
) - 1);
680 TmpStr1
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_WARNING
));
681 StrnCat (ConfirmText
, TmpStr1
, (BufSize
/ sizeof (CHAR16
*)) - StrLen (ConfirmText
) - 1);
684 TmpStr1
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_ACCEPT_KEY
));
685 StrnCat (ConfirmText
, TmpStr1
, (BufSize
/ sizeof (CHAR16
*)) - StrLen (ConfirmText
) - 1);
689 case PHYSICAL_PRESENCE_DEFERRED_PP_UNOWNERED_FIELD_UPGRADE
:
691 TmpStr2
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_UNOWNED_FIELD_UPGRADE
));
693 TmpStr1
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_UPGRADE_HEAD_STR
));
694 UnicodeSPrint (ConfirmText
, BufSize
, TmpStr1
, TmpStr2
);
697 TmpStr1
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_WARNING_MAINTAIN
));
698 StrnCat (ConfirmText
, TmpStr1
, (BufSize
/ sizeof (CHAR16
*)) - StrLen (ConfirmText
) - 1);
701 TmpStr1
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_CAUTION_KEY
));
702 StrnCat (ConfirmText
, TmpStr1
, (BufSize
/ sizeof (CHAR16
*)) - StrLen (ConfirmText
) - 1);
706 case PHYSICAL_PRESENCE_SET_OPERATOR_AUTH
:
708 // TPM_SetOperatorAuth
709 // This command requires UI to prompt user for Auth data
710 // Here it is NOT implemented
714 case PHYSICAL_PRESENCE_CLEAR_ENABLE_ACTIVATE
:
716 TmpStr2
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_CLEAR_TURN_ON
));
718 TmpStr1
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_HEAD_STR
));
719 UnicodeSPrint (ConfirmText
, BufSize
, TmpStr1
, TmpStr2
);
722 TmpStr1
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_NOTE_ON
));
723 StrnCat (ConfirmText
, TmpStr1
, (BufSize
/ sizeof (CHAR16
*)) - StrLen (ConfirmText
) - 1);
726 TmpStr1
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_WARNING_CLEAR
));
727 StrnCat (ConfirmText
, TmpStr1
, (BufSize
/ sizeof (CHAR16
*)) - StrLen (ConfirmText
) - 1);
730 TmpStr1
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_WARNING_CLEAR_CONT
));
731 StrnCat (ConfirmText
, TmpStr1
, (BufSize
/ sizeof (CHAR16
*)) - StrLen (ConfirmText
) - 1);
734 TmpStr1
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_CAUTION_KEY
));
735 StrnCat (ConfirmText
, TmpStr1
, (BufSize
/ sizeof (CHAR16
*)) - StrLen (ConfirmText
) - 1);
739 case PHYSICAL_PRESENCE_SET_NO_PPI_PROVISION_TRUE
:
740 TmpStr2
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_NO_PPI_PROVISION
));
742 TmpStr1
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_PPI_HEAD_STR
));
743 UnicodeSPrint (ConfirmText
, BufSize
, TmpStr1
, TmpStr2
);
746 TmpStr1
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_ACCEPT_KEY
));
747 StrnCat (ConfirmText
, TmpStr1
, (BufSize
/ sizeof (CHAR16
*)) - StrLen (ConfirmText
) - 1);
750 TmpStr1
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_NO_PPI_INFO
));
751 StrnCat (ConfirmText
, TmpStr1
, (BufSize
/ sizeof (CHAR16
*)) - StrLen (ConfirmText
) - 1);
755 case PHYSICAL_PRESENCE_SET_NO_PPI_CLEAR_TRUE
:
757 TmpStr2
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_CLEAR
));
759 TmpStr1
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_PPI_HEAD_STR
));
760 UnicodeSPrint (ConfirmText
, BufSize
, TmpStr1
, TmpStr2
);
763 TmpStr1
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_NOTE_CLEAR
));
764 StrnCat (ConfirmText
, TmpStr1
, (BufSize
/ sizeof (CHAR16
*)) - StrLen (ConfirmText
) - 1);
767 TmpStr1
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_WARNING_CLEAR
));
768 StrnCat (ConfirmText
, TmpStr1
, (BufSize
/ sizeof (CHAR16
*)) - StrLen (ConfirmText
) - 1);
769 StrnCat (ConfirmText
, L
" \n\n", (BufSize
/ sizeof (CHAR16
*)) - StrLen (ConfirmText
) - 1);
772 TmpStr1
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_CAUTION_KEY
));
773 StrnCat (ConfirmText
, TmpStr1
, (BufSize
/ sizeof (CHAR16
*)) - StrLen (ConfirmText
) - 1);
776 TmpStr1
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_NO_PPI_INFO
));
777 StrnCat (ConfirmText
, TmpStr1
, (BufSize
/ sizeof (CHAR16
*)) - StrLen (ConfirmText
) - 1);
781 case PHYSICAL_PRESENCE_SET_NO_PPI_MAINTENANCE_TRUE
:
783 TmpStr2
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_NO_PPI_MAINTAIN
));
785 TmpStr1
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_PPI_HEAD_STR
));
786 UnicodeSPrint (ConfirmText
, BufSize
, TmpStr1
, TmpStr2
);
789 TmpStr1
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_WARNING_MAINTAIN
));
790 StrnCat (ConfirmText
, TmpStr1
, (BufSize
/ sizeof (CHAR16
*)) - StrLen (ConfirmText
) - 1);
793 TmpStr1
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_CAUTION_KEY
));
794 StrnCat (ConfirmText
, TmpStr1
, (BufSize
/ sizeof (CHAR16
*)) - StrLen (ConfirmText
) - 1);
797 TmpStr1
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_NO_PPI_INFO
));
798 StrnCat (ConfirmText
, TmpStr1
, (BufSize
/ sizeof (CHAR16
*)) - StrLen (ConfirmText
) - 1);
802 case PHYSICAL_PRESENCE_ENABLE_ACTIVATE_CLEAR
:
804 TmpStr2
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_ENABLE_ACTIVATE_CLEAR
));
806 TmpStr1
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_HEAD_STR
));
807 UnicodeSPrint (ConfirmText
, BufSize
, TmpStr1
, TmpStr2
);
810 TmpStr1
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_WARNING_CLEAR
));
811 StrnCat (ConfirmText
, TmpStr1
, (BufSize
/ sizeof (CHAR16
*)) - StrLen (ConfirmText
) - 1);
812 StrnCat (ConfirmText
, L
" \n\n", (BufSize
/ sizeof (CHAR16
*)) - StrLen (ConfirmText
) - 1);
815 TmpStr1
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_CAUTION_KEY
));
816 StrnCat (ConfirmText
, TmpStr1
, (BufSize
/ sizeof (CHAR16
*)) - StrLen (ConfirmText
) - 1);
820 case PHYSICAL_PRESENCE_ENABLE_ACTIVATE_CLEAR_ENABLE_ACTIVATE
:
822 TmpStr2
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_ENABLE_ACTIVATE_CLEAR_ENABLE_ACTIVATE
));
824 TmpStr1
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_HEAD_STR
));
825 UnicodeSPrint (ConfirmText
, BufSize
, TmpStr1
, TmpStr2
);
828 TmpStr1
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_NOTE_ON
));
829 StrnCat (ConfirmText
, TmpStr1
, (BufSize
/ sizeof (CHAR16
*)) - StrLen (ConfirmText
) - 1);
832 TmpStr1
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_WARNING_CLEAR
));
833 StrnCat (ConfirmText
, TmpStr1
, (BufSize
/ sizeof (CHAR16
*)) - StrLen (ConfirmText
) - 1);
836 TmpStr1
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_WARNING_CLEAR_CONT
));
837 StrnCat (ConfirmText
, TmpStr1
, (BufSize
/ sizeof (CHAR16
*)) - StrLen (ConfirmText
) - 1);
840 TmpStr1
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_CAUTION_KEY
));
841 StrnCat (ConfirmText
, TmpStr1
, (BufSize
/ sizeof (CHAR16
*)) - StrLen (ConfirmText
) - 1);
849 if (TmpStr2
== NULL
) {
850 FreePool (ConfirmText
);
854 TmpStr1
= PhysicalPresenceGetStringById (STRING_TOKEN (TPM_REJECT_KEY
));
855 BufSize
-= StrSize (ConfirmText
);
856 UnicodeSPrint (ConfirmText
+ StrLen (ConfirmText
), BufSize
, TmpStr1
, TmpStr2
);
859 for (Index
= 0; Index
< StrLen (ConfirmText
); Index
+= 80) {
860 StrnCpy(DstStr
, ConfirmText
+ Index
, 80);
866 FreePool (ConfirmText
);
868 if (ReadUserKey (CautionKey
)) {
876 Check and execute the requested physical presence command.
878 @param[in] TcgProtocol EFI TCG Protocol instance.
879 @param[in] TcgPpData Point to the physical presence NV variable.
883 ExecutePendingTpmRequest (
884 IN EFI_TCG_PROTOCOL
*TcgProtocol
,
885 IN EFI_PHYSICAL_PRESENCE
*TcgPpData
891 BOOLEAN RequestConfirmed
;
893 Flags
= TcgPpData
->Flags
;
894 RequestConfirmed
= FALSE
;
895 switch (TcgPpData
->PPRequest
) {
896 case PHYSICAL_PRESENCE_NO_ACTION
:
898 case PHYSICAL_PRESENCE_ENABLE
:
899 case PHYSICAL_PRESENCE_DISABLE
:
900 case PHYSICAL_PRESENCE_ACTIVATE
:
901 case PHYSICAL_PRESENCE_DEACTIVATE
:
902 case PHYSICAL_PRESENCE_ENABLE_ACTIVATE
:
903 case PHYSICAL_PRESENCE_DEACTIVATE_DISABLE
:
904 case PHYSICAL_PRESENCE_SET_OWNER_INSTALL_TRUE
:
905 case PHYSICAL_PRESENCE_SET_OWNER_INSTALL_FALSE
:
906 case PHYSICAL_PRESENCE_ENABLE_ACTIVATE_OWNER_TRUE
:
907 case PHYSICAL_PRESENCE_DEACTIVATE_DISABLE_OWNER_FALSE
:
908 case PHYSICAL_PRESENCE_SET_OPERATOR_AUTH
:
909 if ((Flags
& FLAG_NO_PPI_PROVISION
) != 0) {
910 RequestConfirmed
= TRUE
;
914 case PHYSICAL_PRESENCE_CLEAR
:
915 case PHYSICAL_PRESENCE_ENABLE_ACTIVATE_CLEAR
:
916 if ((Flags
& FLAG_NO_PPI_CLEAR
) != 0) {
917 RequestConfirmed
= TRUE
;
921 case PHYSICAL_PRESENCE_DEFERRED_PP_UNOWNERED_FIELD_UPGRADE
:
922 if ((Flags
& FLAG_NO_PPI_MAINTENANCE
) != 0) {
923 RequestConfirmed
= TRUE
;
927 case PHYSICAL_PRESENCE_CLEAR_ENABLE_ACTIVATE
:
928 case PHYSICAL_PRESENCE_ENABLE_ACTIVATE_CLEAR_ENABLE_ACTIVATE
:
929 if ((Flags
& FLAG_NO_PPI_CLEAR
) != 0 && (Flags
& FLAG_NO_PPI_PROVISION
) != 0) {
930 RequestConfirmed
= TRUE
;
934 case PHYSICAL_PRESENCE_SET_NO_PPI_PROVISION_FALSE
:
935 case PHYSICAL_PRESENCE_SET_NO_PPI_CLEAR_FALSE
:
936 case PHYSICAL_PRESENCE_SET_NO_PPI_MAINTENANCE_FALSE
:
937 RequestConfirmed
= TRUE
;
941 if ((Flags
& FLAG_RESET_TRACK
) != 0) {
943 // It had been confirmed in last boot, it doesn't need confirm again.
945 RequestConfirmed
= TRUE
;
948 if (!RequestConfirmed
) {
950 // Print confirm text and wait for approval.
952 RequestConfirmed
= UserConfirm (TcgPpData
->PPRequest
);
956 // Execute requested physical presence command
958 TcgPpData
->PPResponse
= TPM_PP_USER_ABORT
;
959 if (RequestConfirmed
) {
960 TcgPpData
->PPResponse
= ExecutePhysicalPresence (TcgProtocol
, TcgPpData
->PPRequest
, &TcgPpData
->Flags
);
966 if ((TcgPpData
->Flags
& FLAG_RESET_TRACK
) == 0) {
967 TcgPpData
->LastPPRequest
= TcgPpData
->PPRequest
;
968 TcgPpData
->PPRequest
= 0;
974 DataSize
= sizeof (EFI_PHYSICAL_PRESENCE
);
975 Status
= gRT
->SetVariable (
976 PHYSICAL_PRESENCE_VARIABLE
,
977 &gEfiPhysicalPresenceGuid
,
978 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
982 if (EFI_ERROR (Status
)) {
986 if (TcgPpData
->PPResponse
== TPM_PP_USER_ABORT
) {
991 // Reset system to make new TPM settings in effect
993 switch (TcgPpData
->LastPPRequest
) {
994 case PHYSICAL_PRESENCE_ACTIVATE
:
995 case PHYSICAL_PRESENCE_DEACTIVATE
:
996 case PHYSICAL_PRESENCE_CLEAR
:
997 case PHYSICAL_PRESENCE_ENABLE_ACTIVATE
:
998 case PHYSICAL_PRESENCE_DEACTIVATE_DISABLE
:
999 case PHYSICAL_PRESENCE_ENABLE_ACTIVATE_OWNER_TRUE
:
1000 case PHYSICAL_PRESENCE_DEACTIVATE_DISABLE_OWNER_FALSE
:
1001 case PHYSICAL_PRESENCE_DEFERRED_PP_UNOWNERED_FIELD_UPGRADE
:
1002 case PHYSICAL_PRESENCE_CLEAR_ENABLE_ACTIVATE
:
1003 case PHYSICAL_PRESENCE_ENABLE_ACTIVATE_CLEAR
:
1004 case PHYSICAL_PRESENCE_ENABLE_ACTIVATE_CLEAR_ENABLE_ACTIVATE
:
1007 if (TcgPpData
->PPRequest
!= 0) {
1013 Print (L
"Rebooting system to make TPM settings in effect\n");
1014 gRT
->ResetSystem (EfiResetCold
, EFI_SUCCESS
, 0, NULL
);
1019 Check and execute the pending TPM request and Lock TPM.
1021 The TPM request may come from OS or BIOS. This API will display request information and wait
1022 for user confirmation if TPM request exists. The TPM request will be sent to TPM device after
1023 the TPM request is confirmed, and one or more reset may be required to make TPM request to
1024 take effect. At last, it will lock TPM to prevent TPM state change by malware.
1026 This API should be invoked after console in and console out are all ready as they are required
1027 to display request information and get user input to confirm the request. This API should also
1028 be invoked as early as possible as TPM is locked in this function.
1033 TcgPhysicalPresenceLibProcessRequest (
1038 BOOLEAN LifetimeLock
;
1041 EFI_PHYSICAL_PRESENCE TcgPpData
;
1042 EFI_TCG_PROTOCOL
*TcgProtocol
;
1044 Status
= gBS
->LocateProtocol (&gEfiTcgProtocolGuid
, NULL
, (VOID
**)&TcgProtocol
);
1045 if (EFI_ERROR (Status
)) {
1050 // Initialize physical presence variable.
1052 DataSize
= sizeof (EFI_PHYSICAL_PRESENCE
);
1053 Status
= gRT
->GetVariable (
1054 PHYSICAL_PRESENCE_VARIABLE
,
1055 &gEfiPhysicalPresenceGuid
,
1060 if (EFI_ERROR (Status
)) {
1061 if (Status
== EFI_NOT_FOUND
) {
1062 ZeroMem ((VOID
*)&TcgPpData
, sizeof (TcgPpData
));
1063 TcgPpData
.Flags
|= FLAG_NO_PPI_PROVISION
;
1064 DataSize
= sizeof (EFI_PHYSICAL_PRESENCE
);
1065 Status
= gRT
->SetVariable (
1066 PHYSICAL_PRESENCE_VARIABLE
,
1067 &gEfiPhysicalPresenceGuid
,
1068 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
1073 ASSERT_EFI_ERROR (Status
);
1076 DEBUG ((EFI_D_INFO
, "[TPM] Flags=%x, PPRequest=%x\n", TcgPpData
.Flags
, TcgPpData
.PPRequest
));
1078 Status
= GetTpmCapability (TcgProtocol
, &LifetimeLock
, &CmdEnable
);
1079 if (EFI_ERROR (Status
)) {
1086 // physicalPresenceCMDEnable is locked, can't execute physical presence command.
1090 Status
= TpmPhysicalPresence (TcgProtocol
, TPM_PHYSICAL_PRESENCE_CMD_ENABLE
);
1091 if (EFI_ERROR (Status
)) {
1097 // Set operator physical presence flags
1099 TpmPhysicalPresence (TcgProtocol
, TPM_PHYSICAL_PRESENCE_PRESENT
);
1102 // Execute pending TPM request.
1104 ExecutePendingTpmRequest (TcgProtocol
, &TcgPpData
);
1105 DEBUG ((EFI_D_INFO
, "[TPM] PPResponse = %x\n", TcgPpData
.PPResponse
));
1108 // Lock physical presence.
1110 TpmPhysicalPresence (TcgProtocol
, TPM_PHYSICAL_PRESENCE_NOTPRESENT
| TPM_PHYSICAL_PRESENCE_LOCK
);