1 /** @file
2   Implement TPM2 Context related command.
3 
4 Copyright (c) 2014, Intel Corporation. All rights reserved. <BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution.  The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9 
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12 
13 **/
14 
15 #include <IndustryStandard/UefiTcgPlatform.h>
16 #include <Library/Tpm2CommandLib.h>
17 #include <Library/Tpm2DeviceLib.h>
18 #include <Library/BaseMemoryLib.h>
19 #include <Library/BaseLib.h>
20 #include <Library/DebugLib.h>
21 
22 #pragma pack(1)
23 
24 typedef struct {
25   TPM2_COMMAND_HEADER       Header;
26   TPMI_DH_CONTEXT           FlushHandle;
27 } TPM2_FLUSH_CONTEXT_COMMAND;
28 
29 typedef struct {
30   TPM2_RESPONSE_HEADER      Header;
31 } TPM2_FLUSH_CONTEXT_RESPONSE;
32 
33 #pragma pack()
34 
35 /**
36   This command causes all context associated with a loaded object or session to be removed from TPM memory.
37 
38   @param[in]  FlushHandle        The handle of the item to flush.
39 
40   @retval EFI_SUCCESS            Operation completed successfully.
41   @retval EFI_DEVICE_ERROR       The command was unsuccessful.
42 **/
43 EFI_STATUS
44 EFIAPI
Tpm2FlushContext(IN TPMI_DH_CONTEXT FlushHandle)45 Tpm2FlushContext (
46   IN      TPMI_DH_CONTEXT           FlushHandle
47   )
48 {
49   EFI_STATUS                        Status;
50   TPM2_FLUSH_CONTEXT_COMMAND        SendBuffer;
51   TPM2_FLUSH_CONTEXT_RESPONSE       RecvBuffer;
52   UINT32                            SendBufferSize;
53   UINT32                            RecvBufferSize;
54 
55   //
56   // Construct command
57   //
58   SendBuffer.Header.tag = SwapBytes16(TPM_ST_NO_SESSIONS);
59   SendBuffer.Header.commandCode = SwapBytes32(TPM_CC_FlushContext);
60 
61   SendBuffer.FlushHandle = SwapBytes32 (FlushHandle);
62 
63   SendBufferSize = (UINT32) sizeof (SendBuffer);
64   SendBuffer.Header.paramSize = SwapBytes32 (SendBufferSize);
65 
66   //
67   // send Tpm command
68   //
69   RecvBufferSize = sizeof (RecvBuffer);
70   Status = Tpm2SubmitCommand (SendBufferSize, (UINT8 *)&SendBuffer, &RecvBufferSize, (UINT8 *)&RecvBuffer);
71   if (EFI_ERROR (Status)) {
72     return Status;
73   }
74 
75   if (RecvBufferSize < sizeof (TPM2_RESPONSE_HEADER)) {
76     DEBUG ((EFI_D_ERROR, "Tpm2FlushContext - RecvBufferSize Error - %x\n", RecvBufferSize));
77     return EFI_DEVICE_ERROR;
78   }
79   if (SwapBytes32(RecvBuffer.Header.responseCode) != TPM_RC_SUCCESS) {
80     DEBUG ((EFI_D_ERROR, "Tpm2FlushContext - responseCode - %x\n", SwapBytes32(RecvBuffer.Header.responseCode)));
81     return EFI_DEVICE_ERROR;
82   }
83 
84   return EFI_SUCCESS;
85 }
86 
87