1 /** @file
2   Implement TPM2 Miscellanenous related command.
3 
4 Copyright (c) 2013 - 2016, 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_RH_HIERARCHY_AUTH    AuthHandle;
27   UINT32                    AuthSessionSize;
28   TPMS_AUTH_COMMAND         AuthSession;
29   UINT32                    AlgorithmSet;
30 } TPM2_SET_ALGORITHM_SET_COMMAND;
31 
32 typedef struct {
33   TPM2_RESPONSE_HEADER       Header;
34   UINT32                     AuthSessionSize;
35   TPMS_AUTH_RESPONSE         AuthSession;
36 } TPM2_SET_ALGORITHM_SET_RESPONSE;
37 
38 #pragma pack()
39 
40 /**
41   This command allows the platform to change the set of algorithms that are used by the TPM.
42   The algorithmSet setting is a vendor-dependent value.
43 
44   @param[in]  AuthHandle              TPM_RH_PLATFORM
45   @param[in]  AuthSession             Auth Session context
46   @param[in]  AlgorithmSet            A TPM vendor-dependent value indicating the
47                                       algorithm set selection
48 
49   @retval EFI_SUCCESS      Operation completed successfully.
50   @retval EFI_DEVICE_ERROR Unexpected device behavior.
51 **/
52 EFI_STATUS
53 EFIAPI
Tpm2SetAlgorithmSet(IN TPMI_RH_PLATFORM AuthHandle,IN TPMS_AUTH_COMMAND * AuthSession,IN UINT32 AlgorithmSet)54 Tpm2SetAlgorithmSet (
55   IN  TPMI_RH_PLATFORM          AuthHandle,
56   IN  TPMS_AUTH_COMMAND         *AuthSession,
57   IN  UINT32                    AlgorithmSet
58   )
59 {
60   EFI_STATUS                                 Status;
61   TPM2_SET_ALGORITHM_SET_COMMAND             SendBuffer;
62   TPM2_SET_ALGORITHM_SET_RESPONSE            RecvBuffer;
63   UINT32                                     SendBufferSize;
64   UINT32                                     RecvBufferSize;
65   UINT8                                      *Buffer;
66   UINT32                                     SessionInfoSize;
67 
68   //
69   // Construct command
70   //
71   SendBuffer.Header.tag = SwapBytes16(TPM_ST_SESSIONS);
72   SendBuffer.Header.commandCode = SwapBytes32(TPM_CC_SetAlgorithmSet);
73 
74   SendBuffer.AuthHandle = SwapBytes32 (AuthHandle);
75 
76   //
77   // Add in Auth session
78   //
79   Buffer = (UINT8 *)&SendBuffer.AuthSession;
80 
81   // sessionInfoSize
82   SessionInfoSize = CopyAuthSessionCommand (AuthSession, Buffer);
83   Buffer += SessionInfoSize;
84   SendBuffer.AuthSessionSize = SwapBytes32(SessionInfoSize);
85 
86   //
87   // Real data
88   //
89   WriteUnaligned32 ((UINT32 *)Buffer, SwapBytes32(AlgorithmSet));
90   Buffer += sizeof(UINT32);
91 
92   SendBufferSize = (UINT32)((UINTN)Buffer - (UINTN)&SendBuffer);
93   SendBuffer.Header.paramSize = SwapBytes32 (SendBufferSize);
94 
95   //
96   // send Tpm command
97   //
98   RecvBufferSize = sizeof (RecvBuffer);
99   Status = Tpm2SubmitCommand (SendBufferSize, (UINT8 *)&SendBuffer, &RecvBufferSize, (UINT8 *)&RecvBuffer);
100   if (EFI_ERROR (Status)) {
101     goto Done;
102   }
103 
104   if (RecvBufferSize < sizeof (TPM2_RESPONSE_HEADER)) {
105     DEBUG ((EFI_D_ERROR, "Tpm2SetAlgorithmSet - RecvBufferSize Error - %x\n", RecvBufferSize));
106     Status = EFI_DEVICE_ERROR;
107     goto Done;
108   }
109   if (SwapBytes32(RecvBuffer.Header.responseCode) != TPM_RC_SUCCESS) {
110     DEBUG ((EFI_D_ERROR, "Tpm2SetAlgorithmSet - responseCode - %x\n", SwapBytes32(RecvBuffer.Header.responseCode)));
111     Status = EFI_DEVICE_ERROR;
112     goto Done;
113   }
114 
115 Done:
116   //
117   // Clear AuthSession Content
118   //
119   ZeroMem (&SendBuffer, sizeof(SendBuffer));
120   ZeroMem (&RecvBuffer, sizeof(RecvBuffer));
121   return Status;
122 }
123