]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Application/VariableInfo/VariableInfo.c
MdeModulePkg VariableInfo: Merge from VariableInfo in SecurityPkg
[mirror_edk2.git] / MdeModulePkg / Application / VariableInfo / VariableInfo.c
index 50b67d097480605c013e132d927d60124a8ce30b..727e1ce105f32d531bb1d72187b3266e60374ba5 100644 (file)
 /** @file\r
-  If the Variable services have PcdVariableCollectStatistics set to TRUE then \r
-  the EFI system table will contain statistical information about variable usage\r
-  an this utility will print out the information. You can use console redirection\r
-  to capture the data.\r
-  \r
-  Copyright (c) 2006 - 2007, Intel Corporation. All rights reserved.<BR>\r
-  This program and the accompanying materials                          \r
-  are licensed and made available under the terms and conditions of the BSD License         \r
-  which accompanies this distribution.  The full text of the license may be found at        \r
-  http://opensource.org/licenses/bsd-license.php                                            \r
-\r
-  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     \r
-  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             \r
+  If the Variable services have PcdVariableCollectStatistics set to TRUE then\r
+  this utility will print out the statistics information. You can use console\r
+  redirection to capture the data.\r
+\r
+  Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.<BR>\r
+  This program and the accompanying materials\r
+  are licensed and made available under the terms and conditions of the BSD License\r
+  which accompanies this distribution.  The full text of the license may be found at\r
+  http://opensource.org/licenses/bsd-license.php\r
+\r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
 \r
 **/\r
 \r
 #include <Uefi.h>\r
 #include <Library/UefiLib.h>\r
 #include <Library/UefiApplicationEntryPoint.h>\r
+#include <Library/BaseMemoryLib.h>\r
+#include <Library/BaseLib.h>\r
+#include <Library/MemoryAllocationLib.h>\r
+#include <Library/DebugLib.h>\r
+#include <Library/UefiBootServicesTableLib.h>\r
+\r
 #include <Guid/VariableFormat.h>\r
+#include <Guid/SmmVariableCommon.h>\r
+#include <Protocol/SmmCommunication.h>\r
+#include <Protocol/SmmVariable.h>\r
+\r
+EFI_SMM_COMMUNICATION_PROTOCOL  *mSmmCommunication = NULL;\r
+\r
+/**\r
+  This function get the variable statistics data from SMM variable driver.\r
+\r
+  @param[in, out] SmmCommunicateHeader In input, a pointer to a collection of data that will\r
+                                       be passed into an SMM environment. In output, a pointer\r
+                                       to a collection of data that comes from an SMM environment.\r
+  @param[in, out] SmmCommunicateSize   The size of the SmmCommunicateHeader.\r
+\r
+  @retval EFI_SUCCESS               Get the statistics data information.\r
+  @retval EFI_NOT_FOUND             Not found.\r
+  @retval EFI_BUFFER_TO_SMALL       DataSize is too small for the result.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+GetVariableStatisticsData (\r
+  IN OUT  EFI_SMM_COMMUNICATE_HEADER  *SmmCommunicateHeader,\r
+  IN OUT  UINTN                       *SmmCommunicateSize\r
+  )\r
+{\r
+  EFI_STATUS                          Status;\r
+  SMM_VARIABLE_COMMUNICATE_HEADER     *SmmVariableFunctionHeader;\r
+\r
+  CopyGuid (&SmmCommunicateHeader->HeaderGuid, &gEfiSmmVariableProtocolGuid);\r
+  SmmCommunicateHeader->MessageLength = *SmmCommunicateSize - OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);\r
+\r
+  SmmVariableFunctionHeader = (SMM_VARIABLE_COMMUNICATE_HEADER *) &SmmCommunicateHeader->Data[0];\r
+  SmmVariableFunctionHeader->Function = SMM_VARIABLE_FUNCTION_GET_STATISTICS;\r
 \r
+  Status = mSmmCommunication->Communicate (mSmmCommunication, SmmCommunicateHeader, SmmCommunicateSize);\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  Status = SmmVariableFunctionHeader->ReturnStatus;\r
+  return Status;\r
+}\r
+\r
+/**\r
+\r
+  This function get and print the variable statistics data from SMM variable driver.\r
+\r
+  @retval EFI_SUCCESS               Print the statistics information successfully.\r
+  @retval EFI_NOT_FOUND             Not found the statistics information.\r
+\r
+**/\r
+EFI_STATUS\r
+PrintInfoFromSmm (\r
+  VOID\r
+  )\r
+{\r
+  EFI_STATUS                                     Status;\r
+  VARIABLE_INFO_ENTRY                            *VariableInfo;\r
+  EFI_SMM_COMMUNICATE_HEADER                     *CommBuffer;\r
+  UINTN                                          RealCommSize;\r
+  UINTN                                          CommSize;\r
+  SMM_VARIABLE_COMMUNICATE_HEADER                *FunctionHeader;\r
+  EFI_SMM_VARIABLE_PROTOCOL                      *Smmvariable;\r
+\r
+  Status = gBS->LocateProtocol (&gEfiSmmVariableProtocolGuid, NULL, (VOID **) &Smmvariable);\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  Status = gBS->LocateProtocol (&gEfiSmmCommunicationProtocolGuid, NULL, (VOID **) &mSmmCommunication);\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  CommSize = SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE;\r
+  RealCommSize = CommSize;\r
+  CommBuffer = AllocateZeroPool (CommSize);\r
+  ASSERT (CommBuffer != NULL);\r
+\r
+  Print (L"Non-Volatile SMM Variables:\n");\r
+  do {\r
+    Status = GetVariableStatisticsData (CommBuffer, &CommSize);\r
+    if (Status == EFI_BUFFER_TOO_SMALL) {\r
+      FreePool (CommBuffer);\r
+      CommBuffer = AllocateZeroPool (CommSize);\r
+      ASSERT (CommBuffer != NULL);\r
+      RealCommSize = CommSize;\r
+      Status = GetVariableStatisticsData (CommBuffer, &CommSize);\r
+    }\r
+\r
+    if (EFI_ERROR (Status) || (CommSize <= SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE)) {\r
+      break;\r
+    }\r
+\r
+    if (CommSize < RealCommSize) {\r
+      CommSize = RealCommSize;\r
+    }\r
+\r
+    FunctionHeader = (SMM_VARIABLE_COMMUNICATE_HEADER *) CommBuffer->Data;\r
+    VariableInfo   = (VARIABLE_INFO_ENTRY *) FunctionHeader->Data;\r
+\r
+    if (!VariableInfo->Volatile) {\r
+      Print (\r
+          L"%g R%03d(%03d) W%03d D%03d:%s\n",\r
+          &VariableInfo->VendorGuid,\r
+          VariableInfo->ReadCount,\r
+          VariableInfo->CacheCount,\r
+          VariableInfo->WriteCount,\r
+          VariableInfo->DeleteCount,\r
+          (CHAR16 *)(VariableInfo + 1)\r
+          );\r
+    }\r
+  } while (TRUE);\r
+\r
+  Print (L"Volatile SMM Variables:\n");\r
+  ZeroMem (CommBuffer, CommSize);\r
+  do {\r
+    Status = GetVariableStatisticsData (CommBuffer, &CommSize);\r
+    if (Status == EFI_BUFFER_TOO_SMALL) {\r
+      FreePool (CommBuffer);\r
+      CommBuffer = AllocateZeroPool (CommSize);\r
+      ASSERT (CommBuffer != NULL);\r
+      RealCommSize = CommSize;\r
+      Status = GetVariableStatisticsData (CommBuffer, &CommSize);\r
+    }\r
+\r
+    if (EFI_ERROR (Status) || (CommSize <= SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE)) {\r
+      break;\r
+    }\r
+\r
+    if (CommSize < RealCommSize) {\r
+      CommSize = RealCommSize;\r
+    }\r
+\r
+    FunctionHeader = (SMM_VARIABLE_COMMUNICATE_HEADER *) CommBuffer->Data;\r
+    VariableInfo   = (VARIABLE_INFO_ENTRY *) FunctionHeader->Data;\r
+\r
+    if (VariableInfo->Volatile) {\r
+      Print (\r
+          L"%g R%03d(%03d) W%03d D%03d:%s\n",\r
+          &VariableInfo->VendorGuid,\r
+          VariableInfo->ReadCount,\r
+          VariableInfo->CacheCount,\r
+          VariableInfo->WriteCount,\r
+          VariableInfo->DeleteCount,\r
+          (CHAR16 *)(VariableInfo + 1)\r
+          );\r
+    }\r
+  } while (TRUE);\r
+\r
+  FreePool (CommBuffer);\r
+  return Status;\r
+}\r
 \r
 /**\r
   The user Entry Point for Application. The user code starts with this function\r
-  as the real entry point for the image goes into a library that calls this \r
+  as the real entry point for the image goes into a library that calls this\r
   function.\r
 \r
-\r
-  @param[in] ImageHandle    The firmware allocated handle for the EFI image.  \r
+  @param[in] ImageHandle    The firmware allocated handle for the EFI image.\r
   @param[in] SystemTable    A pointer to the EFI System Table.\r
-  \r
+\r
   @retval EFI_SUCCESS       The entry point is executed successfully.\r
   @retval other             Some error occurs when executing this entry point.\r
 \r
@@ -46,14 +201,25 @@ UefiMain (
   VARIABLE_INFO_ENTRY   *Entry;\r
 \r
   Status = EfiGetSystemConfigurationTable (&gEfiVariableGuid, (VOID **)&Entry);\r
+  if (EFI_ERROR (Status) || (Entry == NULL)) {\r
+    Status = EfiGetSystemConfigurationTable (&gEfiAuthenticatedVariableGuid, (VOID **)&Entry);\r
+  }\r
+\r
+  if (EFI_ERROR (Status) || (Entry == NULL)) {\r
+    Status = PrintInfoFromSmm ();\r
+    if (!EFI_ERROR (Status)) {\r
+      return Status;\r
+    }\r
+  }\r
+\r
   if (!EFI_ERROR (Status) && (Entry != NULL)) {\r
     Print (L"Non-Volatile EFI Variables:\n");\r
     VariableInfo = Entry;\r
     do {\r
       if (!VariableInfo->Volatile) {\r
         Print (\r
-          L"%g R%03d(%03d) W%03d D%03d:%s\n", \r
-          &VariableInfo->VendorGuid,  \r
+          L"%g R%03d(%03d) W%03d D%03d:%s\n",\r
+          &VariableInfo->VendorGuid,\r
           VariableInfo->ReadCount,\r
           VariableInfo->CacheCount,\r
           VariableInfo->WriteCount,\r
@@ -70,8 +236,8 @@ UefiMain (
     do {\r
       if (VariableInfo->Volatile) {\r
         Print (\r
-          L"%g R%03d(%03d) W%03d D%03d:%s\n", \r
-          &VariableInfo->VendorGuid,  \r
+          L"%g R%03d(%03d) W%03d D%03d:%s\n",\r
+          &VariableInfo->VendorGuid,\r
           VariableInfo->ReadCount,\r
           VariableInfo->CacheCount,\r
           VariableInfo->WriteCount,\r
@@ -88,7 +254,6 @@ UefiMain (
     Print (L"  1. Set PcdVariableCollectStatistics as TRUE\n");\r
     Print (L"  2. Rebuild Variable Dxe driver\n");\r
     Print (L"  3. Run \"VariableInfo\" cmd again\n");\r
-\r
   }\r
 \r
   return Status;\r