]> git.proxmox.com Git - mirror_edk2.git/blob - SecurityPkg/Application/VariableInfo/VariableInfo.c
Vlv2TbltDevicePkg: Use the merged Variable driver
[mirror_edk2.git] / SecurityPkg / Application / VariableInfo / VariableInfo.c
1 /** @file
2 If the Variable services have PcdVariableCollectStatistics set to TRUE then
3 this utility will print out the statistics information. You can use console
4 redirection to capture the data.
5
6 Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.<BR>
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
11
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14
15 **/
16
17 #include <Uefi.h>
18 #include <Library/UefiLib.h>
19 #include <Library/UefiApplicationEntryPoint.h>
20 #include <Library/BaseMemoryLib.h>
21
22 #include <Library/BaseLib.h>
23 #include <Library/MemoryAllocationLib.h>
24 #include <Library/DebugLib.h>
25 #include <Library/UefiBootServicesTableLib.h>
26
27 #include <Guid/AuthenticatedVariableFormat.h>
28 #include <Guid/SmmVariableCommon.h>
29 #include <Protocol/SmmCommunication.h>
30 #include <Protocol/SmmVariable.h>
31
32 extern EFI_GUID gEfiVariableGuid;
33 EFI_SMM_COMMUNICATION_PROTOCOL *mSmmCommunication = NULL;
34
35 /**
36
37 This function get the variable statistics data from SMM variable driver.
38
39 @param[in, out] SmmCommunicateHeader In input, a pointer to a collection of data that will
40 be passed into an SMM environment. In output, a pointer
41 to a collection of data that comes from an SMM environment.
42 @param[in, out] SmmCommunicateSize The size of the SmmCommunicateHeader.
43
44 @retval EFI_SUCCESS Get the statistics data information.
45 @retval EFI_NOT_FOUND Not found.
46 @retval EFI_BUFFER_TO_SMALL DataSize is too small for the result.
47
48 **/
49 EFI_STATUS
50 EFIAPI
51 GetVariableStatisticsData (
52 IN OUT EFI_SMM_COMMUNICATE_HEADER *SmmCommunicateHeader,
53 IN OUT UINTN *SmmCommunicateSize
54 )
55 {
56 EFI_STATUS Status;
57 SMM_VARIABLE_COMMUNICATE_HEADER *SmmVariableFunctionHeader;
58
59 CopyGuid (&SmmCommunicateHeader->HeaderGuid, &gEfiSmmVariableProtocolGuid);
60 SmmCommunicateHeader->MessageLength = *SmmCommunicateSize - OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);
61
62 SmmVariableFunctionHeader = (SMM_VARIABLE_COMMUNICATE_HEADER *) &SmmCommunicateHeader->Data[0];
63 SmmVariableFunctionHeader->Function = SMM_VARIABLE_FUNCTION_GET_STATISTICS;
64
65 Status = mSmmCommunication->Communicate (mSmmCommunication, SmmCommunicateHeader, SmmCommunicateSize);
66 ASSERT_EFI_ERROR (Status);
67
68 Status = SmmVariableFunctionHeader->ReturnStatus;
69 return Status;
70 }
71
72
73 /**
74
75 This function get and print the variable statistics data from SMM variable driver.
76
77 @retval EFI_SUCCESS Print the statistics information successfully.
78 @retval EFI_NOT_FOUND Not found the statistics information.
79
80 **/
81 EFI_STATUS
82 PrintInfoFromSmm (
83 VOID
84 )
85 {
86 EFI_STATUS Status;
87 VARIABLE_INFO_ENTRY *VariableInfo;
88 EFI_SMM_COMMUNICATE_HEADER *CommBuffer;
89 UINTN RealCommSize;
90 UINTN CommSize;
91 SMM_VARIABLE_COMMUNICATE_HEADER *FunctionHeader;
92 EFI_SMM_VARIABLE_PROTOCOL *Smmvariable;
93
94
95 Status = gBS->LocateProtocol (&gEfiSmmVariableProtocolGuid, NULL, (VOID **) &Smmvariable);
96 if (EFI_ERROR (Status)) {
97 return Status;
98 }
99
100 Status = gBS->LocateProtocol (&gEfiSmmCommunicationProtocolGuid, NULL, (VOID **) &mSmmCommunication);
101 if (EFI_ERROR (Status)) {
102 return Status;
103 }
104
105 CommSize = SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE;
106 RealCommSize = CommSize;
107 CommBuffer = AllocateZeroPool (CommSize);
108 ASSERT (CommBuffer != NULL);
109
110 Print (L"Non-Volatile SMM Variables:\n");
111 do {
112 Status = GetVariableStatisticsData (CommBuffer, &CommSize);
113 if (Status == EFI_BUFFER_TOO_SMALL) {
114 FreePool (CommBuffer);
115 CommBuffer = AllocateZeroPool (CommSize);
116 ASSERT (CommBuffer != NULL);
117 RealCommSize = CommSize;
118 Status = GetVariableStatisticsData (CommBuffer, &CommSize);
119 }
120
121 if (EFI_ERROR (Status) || (CommSize <= SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE)) {
122 break;
123 }
124
125 if (CommSize < RealCommSize) {
126 CommSize = RealCommSize;
127 }
128
129 FunctionHeader = (SMM_VARIABLE_COMMUNICATE_HEADER *) CommBuffer->Data;
130 VariableInfo = (VARIABLE_INFO_ENTRY *) FunctionHeader->Data;
131
132 if (!VariableInfo->Volatile) {
133 Print (
134 L"%g R%03d(%03d) W%03d D%03d:%s\n",
135 &VariableInfo->VendorGuid,
136 VariableInfo->ReadCount,
137 VariableInfo->CacheCount,
138 VariableInfo->WriteCount,
139 VariableInfo->DeleteCount,
140 (CHAR16 *)(VariableInfo + 1)
141 );
142 }
143 } while (TRUE);
144
145 Print (L"Volatile SMM Variables:\n");
146 ZeroMem (CommBuffer, CommSize);
147 do {
148 Status = GetVariableStatisticsData (CommBuffer, &CommSize);
149 if (Status == EFI_BUFFER_TOO_SMALL) {
150 FreePool (CommBuffer);
151 CommBuffer = AllocateZeroPool (CommSize);
152 ASSERT (CommBuffer != NULL);
153 RealCommSize = CommSize;
154 Status = GetVariableStatisticsData (CommBuffer, &CommSize);
155 }
156
157 if (EFI_ERROR (Status) || (CommSize <= SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE)) {
158 break;
159 }
160
161 if (CommSize < RealCommSize) {
162 CommSize = RealCommSize;
163 }
164
165 FunctionHeader = (SMM_VARIABLE_COMMUNICATE_HEADER *) CommBuffer->Data;
166 VariableInfo = (VARIABLE_INFO_ENTRY *) FunctionHeader->Data;
167
168 if (VariableInfo->Volatile) {
169 Print (
170 L"%g R%03d(%03d) W%03d D%03d:%s\n",
171 &VariableInfo->VendorGuid,
172 VariableInfo->ReadCount,
173 VariableInfo->CacheCount,
174 VariableInfo->WriteCount,
175 VariableInfo->DeleteCount,
176 (CHAR16 *)(VariableInfo + 1)
177 );
178 }
179 } while (TRUE);
180
181 FreePool (CommBuffer);
182 return Status;
183 }
184
185 /**
186 The user Entry Point for Application. The user code starts with this function
187 as the real entry point for the image goes into a library that calls this
188 function.
189
190 @param[in] ImageHandle The firmware allocated handle for the EFI image.
191 @param[in] SystemTable A pointer to the EFI System Table.
192
193 @retval EFI_SUCCESS The entry point is executed successfully.
194 @retval other Some error occurs when executing this entry point.
195
196 **/
197 EFI_STATUS
198 EFIAPI
199 UefiMain (
200 IN EFI_HANDLE ImageHandle,
201 IN EFI_SYSTEM_TABLE *SystemTable
202 )
203 {
204 EFI_STATUS Status;
205 VARIABLE_INFO_ENTRY *VariableInfo;
206 VARIABLE_INFO_ENTRY *Entry;
207
208 Status = EfiGetSystemConfigurationTable (&gEfiVariableGuid, (VOID **)&Entry);
209 if (EFI_ERROR (Status) || (Entry == NULL)) {
210 Status = EfiGetSystemConfigurationTable (&gEfiAuthenticatedVariableGuid, (VOID **)&Entry);
211 }
212
213 if (EFI_ERROR (Status) || (Entry == NULL)) {
214 Status = PrintInfoFromSmm ();
215 if (!EFI_ERROR (Status)) {
216 return Status;
217 }
218 }
219
220 if (!EFI_ERROR (Status) && (Entry != NULL)) {
221 Print (L"Non-Volatile EFI Variables:\n");
222 VariableInfo = Entry;
223 do {
224 if (!VariableInfo->Volatile) {
225 Print (
226 L"%g R%03d(%03d) W%03d D%03d:%s\n",
227 &VariableInfo->VendorGuid,
228 VariableInfo->ReadCount,
229 VariableInfo->CacheCount,
230 VariableInfo->WriteCount,
231 VariableInfo->DeleteCount,
232 VariableInfo->Name
233 );
234 }
235
236 VariableInfo = VariableInfo->Next;
237 } while (VariableInfo != NULL);
238
239 Print (L"Volatile EFI Variables:\n");
240 VariableInfo = Entry;
241 do {
242 if (VariableInfo->Volatile) {
243 Print (
244 L"%g R%03d(%03d) W%03d D%03d:%s\n",
245 &VariableInfo->VendorGuid,
246 VariableInfo->ReadCount,
247 VariableInfo->CacheCount,
248 VariableInfo->WriteCount,
249 VariableInfo->DeleteCount,
250 VariableInfo->Name
251 );
252 }
253 VariableInfo = VariableInfo->Next;
254 } while (VariableInfo != NULL);
255
256 } else {
257 Print (L"Warning: Variable Dxe driver doesn't enable the feature of statistical information!\n");
258 Print (L"If you want to see this info, please:\n");
259 Print (L" 1. Set PcdVariableCollectStatistics as TRUE\n");
260 Print (L" 2. Rebuild Variable Dxe driver\n");
261 Print (L" 3. Run \"VariableInfo\" cmd again\n");
262 }
263
264 return Status;
265 }