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