]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Application/MemoryProfileInfo/MemoryProfileInfo.c
MdeModulePkg:Full support F10 hot key in UiApp.
[mirror_edk2.git] / MdeModulePkg / Application / MemoryProfileInfo / MemoryProfileInfo.c
index 4896ebd1ff0bedd1fda747a046a2a48e9cc8b8f5..f69a3e77486a82368f127e42b5280fb2c3608865 100644 (file)
@@ -1,6 +1,6 @@
 /** @file\r
   \r
-  Copyright (c) 2014, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2014 - 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
@@ -54,7 +54,9 @@ CHAR16 *mMemoryTypeString[] = {
   L"EfiMemoryMappedIO",\r
   L"EfiMemoryMappedIOPortSpace",\r
   L"EfiPalCode",\r
+  L"EfiPersistentMemory",\r
   L"EfiOSReserved",\r
+  L"EfiOemReserved",\r
 };\r
 \r
 CHAR16 *mSubsystemString[] = {\r
@@ -119,10 +121,10 @@ GetShortPdbFileName (
   UINTN StartIndex;\r
   UINTN EndIndex;\r
 \r
-  ZeroMem (UnicodeBuffer, PROFILE_NAME_STRING_LENGTH * sizeof (CHAR16));\r
+  ZeroMem (UnicodeBuffer, (PROFILE_NAME_STRING_LENGTH + 1) * sizeof (CHAR16));\r
 \r
   if (PdbFileName == NULL) {\r
-    StrnCpy (UnicodeBuffer, L" ", 1);\r
+    StrnCpyS (UnicodeBuffer, PROFILE_NAME_STRING_LENGTH + 1, L" ", 1);\r
   } else {\r
     StartIndex = 0;\r
     for (EndIndex = 0; PdbFileName[EndIndex] != 0; EndIndex++);\r
@@ -199,7 +201,7 @@ GetDriverNameString (
       //\r
       // Method 2: Get the name string from FFS UI section\r
       //\r
-      StrnCpy (mNameString, NameString, PROFILE_NAME_STRING_LENGTH);\r
+      StrnCpyS (mNameString, PROFILE_NAME_STRING_LENGTH + 1, NameString, PROFILE_NAME_STRING_LENGTH);\r
       mNameString[PROFILE_NAME_STRING_LENGTH] = 0;\r
       FreePool (NameString);\r
       return;\r
@@ -212,6 +214,38 @@ GetDriverNameString (
   UnicodeSPrint (mNameString, sizeof (mNameString), L"%g", &DriverInfo->FileName);\r
 }\r
 \r
+/**\r
+  Memory type to string.\r
+\r
+  @param[in] MemoryType Memory type.\r
+\r
+  @return Pointer to string.\r
+\r
+**/\r
+CHAR16 *\r
+ProfileMemoryTypeToStr (\r
+  IN EFI_MEMORY_TYPE    MemoryType\r
+  )\r
+{\r
+  UINTN     Index;\r
+\r
+  if ((UINT32) MemoryType >= 0x80000000) {\r
+    //\r
+    // OS reserved memory type.\r
+    //\r
+    Index = EfiMaxMemoryType;\r
+  } else if ((UINT32) MemoryType >= 0x70000000) {\r
+    //\r
+    // OEM reserved memory type.\r
+    //\r
+    Index = EfiMaxMemoryType + 1;\r
+  } else {\r
+    Index = MemoryType;\r
+  }\r
+\r
+  return mMemoryTypeString[Index];\r
+}\r
+\r
 /**\r
   Dump memory profile allocate information.\r
 \r
@@ -239,7 +273,7 @@ DumpMemoryProfileAllocInfo (
   Print (L"      CallerAddress - 0x%016lx (Offset: 0x%08x)\n", AllocInfo->CallerAddress, (UINTN) (AllocInfo->CallerAddress - DriverInfo->ImageBase));\r
   Print (L"      SequenceId    - 0x%08x\n", AllocInfo->SequenceId);\r
   Print (L"      Action        - 0x%08x (%s)\n", AllocInfo->Action, mActionString[(AllocInfo->Action < sizeof(mActionString)/sizeof(mActionString[0])) ? AllocInfo->Action : 0]);\r
-  Print (L"      MemoryType    - 0x%08x (%s)\n", AllocInfo->MemoryType, mMemoryTypeString[(AllocInfo->MemoryType < sizeof(mMemoryTypeString)/sizeof(mMemoryTypeString[0])) ? AllocInfo->MemoryType : (sizeof(mMemoryTypeString)/sizeof(mMemoryTypeString[0]) - 1)]);\r
+  Print (L"      MemoryType    - 0x%08x (%s)\n", AllocInfo->MemoryType, ProfileMemoryTypeToStr (AllocInfo->MemoryType));\r
   Print (L"      Buffer        - 0x%016lx\n", AllocInfo->Buffer);\r
   Print (L"      Size          - 0x%016lx\n", AllocInfo->Size);\r
 \r
@@ -281,7 +315,7 @@ DumpMemoryProfileDriverInfo (
   Print (L"    FileType                - 0x%02x (%s)\n", DriverInfo->FileType, mFileTypeString[(DriverInfo->FileType < sizeof(mFileTypeString)/sizeof(mFileTypeString[0])) ? DriverInfo->FileType : 0]);\r
   Print (L"    CurrentUsage            - 0x%016lx\n", DriverInfo->CurrentUsage);\r
   Print (L"    PeakUsage               - 0x%016lx\n", DriverInfo->PeakUsage);\r
-  for (TypeIndex = 0; TypeIndex <= EfiMaxMemoryType; TypeIndex++) {\r
+  for (TypeIndex = 0; TypeIndex < sizeof (DriverInfo->CurrentUsageByType) / sizeof (DriverInfo->CurrentUsageByType[0]); TypeIndex++) {\r
     if ((DriverInfo->CurrentUsageByType[TypeIndex] != 0) ||\r
         (DriverInfo->PeakUsageByType[TypeIndex] != 0)) {\r
       Print (L"    CurrentUsage[0x%02x]      - 0x%016lx (%s)\n", TypeIndex, DriverInfo->CurrentUsageByType[TypeIndex], mMemoryTypeString[TypeIndex]);\r
@@ -326,7 +360,7 @@ DumpMemoryProfileContext (
   Print (L"  Revision                      - 0x%04x\n", Context->Header.Revision);  \r
   Print (L"  CurrentTotalUsage             - 0x%016lx\n", Context->CurrentTotalUsage);\r
   Print (L"  PeakTotalUsage                - 0x%016lx\n", Context->PeakTotalUsage);\r
-  for (TypeIndex = 0; TypeIndex <= EfiMaxMemoryType; TypeIndex++) {\r
+  for (TypeIndex = 0; TypeIndex < sizeof (Context->CurrentTotalUsageByType) / sizeof (Context->CurrentTotalUsageByType[0]); TypeIndex++) {\r
     if ((Context->CurrentTotalUsageByType[TypeIndex] != 0) ||\r
         (Context->PeakTotalUsageByType[TypeIndex] != 0)) {\r
       Print (L"  CurrentTotalUsage[0x%02x]       - 0x%016lx (%s)\n", TypeIndex, Context->CurrentTotalUsageByType[TypeIndex], mMemoryTypeString[TypeIndex]);\r
@@ -557,6 +591,7 @@ GetUefiMemoryProfileData (
   Size = Size + sizeof (MEMORY_PROFILE_ALLOC_INFO);\r
   Data = AllocateZeroPool ((UINTN) Size);\r
   if (Data == NULL) {\r
+    Status = EFI_OUT_OF_RESOURCES;\r
     Print (L"UefiMemoryProfile: AllocateZeroPool (0x%x) - %r\n", Size, Status);\r
     return Status;\r
   }\r
@@ -597,7 +632,7 @@ GetSmramProfileData (
 {\r
   EFI_STATUS                                    Status;\r
   UINTN                                         CommSize;\r
-  UINT8                                         CommBuffer[sizeof (EFI_GUID) + sizeof (UINTN) + sizeof (SMRAM_PROFILE_PARAMETER_GET_PROFILE_DATA)];\r
+  UINT8                                         *CommBuffer;\r
   EFI_SMM_COMMUNICATE_HEADER                    *CommHeader;\r
   SMRAM_PROFILE_PARAMETER_GET_PROFILE_INFO      *CommGetProfileInfo;\r
   SMRAM_PROFILE_PARAMETER_GET_PROFILE_DATA      *CommGetProfileData;\r
@@ -611,6 +646,14 @@ GetSmramProfileData (
     return Status;\r
   }\r
 \r
+  CommSize = sizeof (EFI_GUID) + sizeof (UINTN) + sizeof (SMRAM_PROFILE_PARAMETER_GET_PROFILE_DATA);\r
+  CommBuffer = AllocateZeroPool (CommSize);\r
+  if (CommBuffer == NULL) {\r
+    Status = EFI_OUT_OF_RESOURCES;\r
+    Print (L"SmramProfile: AllocateZeroPool (0x%x) for comm buffer - %r\n", CommSize, Status);\r
+    return Status;\r
+  }\r
+\r
   //\r
   // Get Size\r
   //\r
@@ -627,6 +670,7 @@ GetSmramProfileData (
   CommSize = sizeof (EFI_GUID) + sizeof (UINTN) + CommHeader->MessageLength;\r
   Status = SmmCommunication->Communicate (SmmCommunication, CommBuffer, &CommSize);\r
   if (EFI_ERROR (Status)) {\r
+    FreePool (CommBuffer);\r
     DEBUG ((EFI_D_ERROR, "SmramProfile: SmmCommunication - %r\n", Status));\r
     return Status;\r
   }\r
@@ -643,15 +687,17 @@ GetSmramProfileData (
   //\r
   ProfileBuffer = (PHYSICAL_ADDRESS) (UINTN) AllocateZeroPool ((UINTN) ProfileSize);\r
   if (ProfileBuffer == 0) {\r
-    Print (L"UefiMemoryProfile: AllocateZeroPool (0x%x) - %r\n", (UINTN) ProfileSize, Status);\r
-    return EFI_SUCCESS;\r
+    FreePool (CommBuffer);\r
+    Status = EFI_OUT_OF_RESOURCES;\r
+    Print (L"SmramProfile: AllocateZeroPool (0x%x) for profile buffer - %r\n", (UINTN) ProfileSize, Status);\r
+    return Status;\r
   }\r
 \r
   CommHeader = (EFI_SMM_COMMUNICATE_HEADER *) &CommBuffer[0];\r
   CopyMem (&CommHeader->HeaderGuid, &gEdkiiMemoryProfileGuid, sizeof(gEdkiiMemoryProfileGuid));\r
   CommHeader->MessageLength = sizeof (SMRAM_PROFILE_PARAMETER_GET_PROFILE_DATA);\r
 \r
-  CommGetProfileData = (SMRAM_PROFILE_PARAMETER_GET_PROFILE_DATA *)&CommBuffer[OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data)];\r
+  CommGetProfileData = (SMRAM_PROFILE_PARAMETER_GET_PROFILE_DATA *) &CommBuffer[OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data)];\r
   CommGetProfileData->Header.Command      = SMRAM_PROFILE_COMMAND_GET_PROFILE_DATA;\r
   CommGetProfileData->Header.DataLength   = sizeof (*CommGetProfileData);\r
   CommGetProfileData->Header.ReturnStatus = (UINT64)-1;\r
@@ -663,6 +709,8 @@ GetSmramProfileData (
   ASSERT_EFI_ERROR (Status);\r
 \r
   if (CommGetProfileData->Header.ReturnStatus != 0) {\r
+    FreePool ((VOID *) (UINTN) CommGetProfileData->ProfileBuffer);\r
+    FreePool (CommBuffer);\r
     Print (L"GetProfileData - 0x%x\n", CommGetProfileData->Header.ReturnStatus);\r
     return EFI_SUCCESS;\r
   }\r
@@ -674,6 +722,7 @@ GetSmramProfileData (
   Print (L"======= SmramProfile end =======\n\n\n");\r
 \r
   FreePool ((VOID *) (UINTN) CommGetProfileData->ProfileBuffer);\r
+  FreePool (CommBuffer);\r
 \r
   return EFI_SUCCESS;\r
 }\r