]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Application/VariableInfo/VariableInfo.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / MdeModulePkg / Application / VariableInfo / VariableInfo.c
index 50b67d097480605c013e132d927d60124a8ce30b..3dee41eb4b7b74dab47f40f50c01941bb8051450 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 - 2019, Intel Corporation. All rights reserved.<BR>\r
+  SPDX-License-Identifier: BSD-2-Clause-Patent\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 <Guid/PiSmmCommunicationRegionTable.h>\r
+#include <Protocol/MmCommunication2.h>\r
+#include <Protocol/SmmVariable.h>\r
 \r
+EFI_MM_COMMUNICATION2_PROTOCOL  *mMmCommunication2 = 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_MM_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_MM_COMMUNICATE_HEADER, Data);\r
+\r
+  SmmVariableFunctionHeader           = (SMM_VARIABLE_COMMUNICATE_HEADER *)&SmmCommunicateHeader->Data[0];\r
+  SmmVariableFunctionHeader->Function = SMM_VARIABLE_FUNCTION_GET_STATISTICS;\r
+\r
+  Status = mMmCommunication2->Communicate (\r
+                                mMmCommunication2,\r
+                                SmmCommunicateHeader,\r
+                                SmmCommunicateHeader,\r
+                                SmmCommunicateSize\r
+                                );\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_MM_COMMUNICATE_HEADER                *CommBuffer;\r
+  UINTN                                    RealCommSize;\r
+  UINTN                                    CommSize;\r
+  SMM_VARIABLE_COMMUNICATE_HEADER          *FunctionHeader;\r
+  EFI_SMM_VARIABLE_PROTOCOL                *Smmvariable;\r
+  EDKII_PI_SMM_COMMUNICATION_REGION_TABLE  *PiSmmCommunicationRegionTable;\r
+  UINT32                                   Index;\r
+  EFI_MEMORY_DESCRIPTOR                    *Entry;\r
+  UINTN                                    Size;\r
+  UINTN                                    MaxSize;\r
+\r
+  Status = gBS->LocateProtocol (&gEfiSmmVariableProtocolGuid, NULL, (VOID **)&Smmvariable);\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  Status = gBS->LocateProtocol (&gEfiMmCommunication2ProtocolGuid, NULL, (VOID **)&mMmCommunication2);\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  CommBuffer   = NULL;\r
+  RealCommSize = 0;\r
+  Status       = EfiGetSystemConfigurationTable (\r
+                   &gEdkiiPiSmmCommunicationRegionTableGuid,\r
+                   (VOID **)&PiSmmCommunicationRegionTable\r
+                   );\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  ASSERT (PiSmmCommunicationRegionTable != NULL);\r
+  Entry   = (EFI_MEMORY_DESCRIPTOR *)(PiSmmCommunicationRegionTable + 1);\r
+  Size    = 0;\r
+  MaxSize = 0;\r
+  for (Index = 0; Index < PiSmmCommunicationRegionTable->NumberOfEntries; Index++) {\r
+    if (Entry->Type == EfiConventionalMemory) {\r
+      Size = EFI_PAGES_TO_SIZE ((UINTN)Entry->NumberOfPages);\r
+      if (Size > (SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + sizeof (VARIABLE_INFO_ENTRY))) {\r
+        if (Size > MaxSize) {\r
+          MaxSize      = Size;\r
+          RealCommSize = MaxSize;\r
+          CommBuffer   = (EFI_MM_COMMUNICATE_HEADER *)(UINTN)Entry->PhysicalStart;\r
+        }\r
+      }\r
+    }\r
+\r
+    Entry = (EFI_MEMORY_DESCRIPTOR *)((UINT8 *)Entry + PiSmmCommunicationRegionTable->DescriptorSize);\r
+  }\r
+\r
+  ASSERT (CommBuffer != NULL);\r
+  ZeroMem (CommBuffer, RealCommSize);\r
+\r
+  Print (L"SMM Driver Non-Volatile Variables:\n");\r
+  do {\r
+    CommSize = RealCommSize;\r
+    Status   = GetVariableStatisticsData (CommBuffer, &CommSize);\r
+    if (Status == EFI_BUFFER_TOO_SMALL) {\r
+      Print (L"The generic SMM communication buffer provided by SmmCommunicationRegionTable is too small\n");\r
+      return Status;\r
+    }\r
+\r
+    if (EFI_ERROR (Status) || (CommSize <= SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE)) {\r
+      break;\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"SMM Driver Volatile Variables:\n");\r
+  ZeroMem (CommBuffer, RealCommSize);\r
+  do {\r
+    CommSize = RealCommSize;\r
+    Status   = GetVariableStatisticsData (CommBuffer, &CommSize);\r
+    if (Status == EFI_BUFFER_TOO_SMALL) {\r
+      Print (L"The generic SMM communication buffer provided by SmmCommunicationRegionTable is too small\n");\r
+      return Status;\r
+    }\r
+\r
+    if (EFI_ERROR (Status) || (CommSize <= SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE)) {\r
+      break;\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
+  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
@@ -41,19 +215,24 @@ UefiMain (
   IN EFI_SYSTEM_TABLE  *SystemTable\r
   )\r
 {\r
-  EFI_STATUS            Status;\r
-  VARIABLE_INFO_ENTRY   *VariableInfo;\r
-  VARIABLE_INFO_ENTRY   *Entry;\r
+  EFI_STATUS           RuntimeDxeStatus;\r
+  EFI_STATUS           SmmStatus;\r
+  VARIABLE_INFO_ENTRY  *VariableInfo;\r
+  VARIABLE_INFO_ENTRY  *Entry;\r
+\r
+  RuntimeDxeStatus = EfiGetSystemConfigurationTable (&gEfiVariableGuid, (VOID **)&Entry);\r
+  if (EFI_ERROR (RuntimeDxeStatus) || (Entry == NULL)) {\r
+    RuntimeDxeStatus = EfiGetSystemConfigurationTable (&gEfiAuthenticatedVariableGuid, (VOID **)&Entry);\r
+  }\r
 \r
-  Status = EfiGetSystemConfigurationTable (&gEfiVariableGuid, (VOID **)&Entry);\r
-  if (!EFI_ERROR (Status) && (Entry != NULL)) {\r
-    Print (L"Non-Volatile EFI Variables:\n");\r
+  if (!EFI_ERROR (RuntimeDxeStatus) && (Entry != NULL)) {\r
+    Print (L"Runtime DXE Driver 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
@@ -65,13 +244,13 @@ UefiMain (
       VariableInfo = VariableInfo->Next;\r
     } while (VariableInfo != NULL);\r
 \r
-    Print (L"Volatile EFI Variables:\n");\r
+    Print (L"Runtime DXE Driver 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
@@ -79,17 +258,22 @@ UefiMain (
           VariableInfo->Name\r
           );\r
       }\r
+\r
       VariableInfo = VariableInfo->Next;\r
     } while (VariableInfo != NULL);\r
+  }\r
+\r
+  SmmStatus = PrintInfoFromSmm ();\r
 \r
-  } else {\r
-    Print (L"Warning: Variable Dxe driver doesn't enable the feature of statistical information!\n");\r
+  if (EFI_ERROR (RuntimeDxeStatus) && EFI_ERROR (SmmStatus)) {\r
+    Print (L"Warning: Variable Dxe/Smm driver doesn't enable the feature of statistical information!\n");\r
     Print (L"If you want to see this info, please:\n");\r
     Print (L"  1. Set PcdVariableCollectStatistics as TRUE\n");\r
-    Print (L"  2. Rebuild Variable Dxe driver\n");\r
+    Print (L"  2. Rebuild Variable Dxe/Smm driver\n");\r
     Print (L"  3. Run \"VariableInfo\" cmd again\n");\r
 \r
+    return EFI_NOT_FOUND;\r
   }\r
 \r
-  return Status;\r
+  return EFI_SUCCESS;\r
 }\r