1 /** @file
2   Implement TPM1.2 Get Capabilities related commands.
3 
4 Copyright (c) 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 <PiPei.h>
16 #include <Library/Tpm12CommandLib.h>
17 #include <Library/BaseLib.h>
18 #include <Library/DebugLib.h>
19 #include <Library/BaseMemoryLib.h>
20 #include <Library/Tpm12DeviceLib.h>
21 
22 #pragma pack(1)
23 
24 typedef struct {
25   TPM_RQU_COMMAND_HDR  Hdr;
26   UINT32               Capability;
27   UINT32               CapabilityFlagSize;
28   UINT32               CapabilityFlag;
29 } TPM_CMD_GET_CAPABILITY;
30 
31 typedef struct {
32   TPM_RSP_COMMAND_HDR  Hdr;
33   UINT32               ResponseSize;
34   TPM_PERMANENT_FLAGS  Flags;
35 } TPM_RSP_GET_CAPABILITY_PERMANENT_FLAGS;
36 
37 typedef struct {
38   TPM_RSP_COMMAND_HDR  Hdr;
39   UINT32               ResponseSize;
40   TPM_STCLEAR_FLAGS    Flags;
41 } TPM_RSP_GET_CAPABILITY_STCLEAR_FLAGS;
42 
43 #pragma pack()
44 
45 /**
46 Get TPM capability permanent flags.
47 
48 @param[out] TpmPermanentFlags   Pointer to the buffer for returned flag structure.
49 
50 @retval EFI_SUCCESS           Operation completed successfully.
51 @retval EFI_TIMEOUT           The register can't run into the expected status in time.
52 @retval EFI_BUFFER_TOO_SMALL  Response data buffer is too small.
53 @retval EFI_DEVICE_ERROR      Unexpected device behavior.
54 
55 **/
56 EFI_STATUS
57 EFIAPI
Tpm12GetCapabilityFlagPermanent(OUT TPM_PERMANENT_FLAGS * TpmPermanentFlags)58 Tpm12GetCapabilityFlagPermanent (
59   OUT TPM_PERMANENT_FLAGS  *TpmPermanentFlags
60   )
61 {
62   EFI_STATUS                              Status;
63   TPM_CMD_GET_CAPABILITY                  Command;
64   TPM_RSP_GET_CAPABILITY_PERMANENT_FLAGS  Response;
65   UINT32                                  Length;
66 
67   //
68   // send Tpm command TPM_ORD_GetCapability
69   //
70   Command.Hdr.tag            = SwapBytes16 (TPM_TAG_RQU_COMMAND);
71   Command.Hdr.paramSize      = SwapBytes32 (sizeof (Command));
72   Command.Hdr.ordinal        = SwapBytes32 (TPM_ORD_GetCapability);
73   Command.Capability         = SwapBytes32 (TPM_CAP_FLAG);
74   Command.CapabilityFlagSize = SwapBytes32 (sizeof (TPM_CAP_FLAG_PERMANENT));
75   Command.CapabilityFlag     = SwapBytes32 (TPM_CAP_FLAG_PERMANENT);
76   Length = sizeof (Response);
77   Status = Tpm12SubmitCommand (sizeof (Command), (UINT8 *)&Command, &Length, (UINT8 *)&Response);
78   if (EFI_ERROR (Status)) {
79     return Status;
80   }
81 
82   ZeroMem (TpmPermanentFlags, sizeof (*TpmPermanentFlags));
83   CopyMem (TpmPermanentFlags, &Response.Flags, MIN (sizeof (*TpmPermanentFlags), Response.ResponseSize));
84 
85   return Status;
86 }
87 
88 /**
89 Get TPM capability volatile flags.
90 
91 @param[out] VolatileFlags   Pointer to the buffer for returned flag structure.
92 
93 @retval EFI_SUCCESS      Operation completed successfully.
94 @retval EFI_DEVICE_ERROR The command was unsuccessful.
95 
96 **/
97 EFI_STATUS
98 EFIAPI
Tpm12GetCapabilityFlagVolatile(OUT TPM_STCLEAR_FLAGS * VolatileFlags)99 Tpm12GetCapabilityFlagVolatile (
100   OUT TPM_STCLEAR_FLAGS  *VolatileFlags
101   )
102 {
103   EFI_STATUS                            Status;
104   TPM_CMD_GET_CAPABILITY                Command;
105   TPM_RSP_GET_CAPABILITY_STCLEAR_FLAGS  Response;
106   UINT32                                Length;
107 
108   //
109   // send Tpm command TPM_ORD_GetCapability
110   //
111   Command.Hdr.tag            = SwapBytes16 (TPM_TAG_RQU_COMMAND);
112   Command.Hdr.paramSize      = SwapBytes32 (sizeof (Command));
113   Command.Hdr.ordinal        = SwapBytes32 (TPM_ORD_GetCapability);
114   Command.Capability         = SwapBytes32 (TPM_CAP_FLAG);
115   Command.CapabilityFlagSize = SwapBytes32 (sizeof (TPM_CAP_FLAG_VOLATILE));
116   Command.CapabilityFlag     = SwapBytes32 (TPM_CAP_FLAG_VOLATILE);
117   Length = sizeof (Response);
118   Status = Tpm12SubmitCommand (sizeof (Command), (UINT8 *)&Command, &Length, (UINT8 *)&Response);
119   if (EFI_ERROR (Status)) {
120     return Status;
121   }
122 
123   ZeroMem (VolatileFlags, sizeof (*VolatileFlags));
124   CopyMem (VolatileFlags, &Response.Flags, MIN (sizeof (*VolatileFlags), Response.ResponseSize));
125 
126   return Status;
127 }
128