VariableInfo.c 9.2 KB

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