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