]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Application/VariableInfo/VariableInfo.c
MdeModulePkg VariableInfo: Always consider RT DXE and SMM stats
[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 - 2019, 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"SMM Driver Non-Volatile 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"SMM Driver Volatile 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 RuntimeDxeStatus;
211 EFI_STATUS SmmStatus;
212 VARIABLE_INFO_ENTRY *VariableInfo;
213 VARIABLE_INFO_ENTRY *Entry;
214
215 RuntimeDxeStatus = EfiGetSystemConfigurationTable (&gEfiVariableGuid, (VOID **) &Entry);
216 if (EFI_ERROR (RuntimeDxeStatus) || (Entry == NULL)) {
217 RuntimeDxeStatus = EfiGetSystemConfigurationTable (&gEfiAuthenticatedVariableGuid, (VOID **) &Entry);
218 }
219
220 if (!EFI_ERROR (RuntimeDxeStatus) && (Entry != NULL)) {
221 Print (L"Runtime DXE Driver 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"Runtime DXE Driver 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
257 SmmStatus = PrintInfoFromSmm ();
258
259 if (EFI_ERROR (RuntimeDxeStatus) && EFI_ERROR (SmmStatus)) {
260 Print (L"Warning: Variable Dxe/Smm driver doesn't enable the feature of statistical information!\n");
261 Print (L"If you want to see this info, please:\n");
262 Print (L" 1. Set PcdVariableCollectStatistics as TRUE\n");
263 Print (L" 2. Rebuild Variable Dxe/Smm driver\n");
264 Print (L" 3. Run \"VariableInfo\" cmd again\n");
265
266 return EFI_NOT_FOUND;
267 }
268
269 return EFI_SUCCESS;
270 }