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