]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/CapsulePei/UefiCapsule.c
MdeMoudlePkg/CapsulePei: Optimize GetScatterGatherHeadEntries
[mirror_edk2.git] / MdeModulePkg / Universal / CapsulePei / UefiCapsule.c
index ce6d95a7866e4b52f375c277be54cce8502f0928..3ac95b7be67c994c1812dec4aa1f11a2a41ceb2e 100644 (file)
@@ -10,6 +10,8 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
 \r
 #include "Capsule.h"\r
 \r
+#define DEFAULT_SG_LIST_HEADS       (20)\r
+\r
 #ifdef MDE_CPU_IA32\r
 //\r
 // Global Descriptor Table (GDT)\r
@@ -841,8 +843,6 @@ AreCapsulesStaged (
   return FALSE;\r
 }\r
 \r
-#define MAX_SG_LIST_HEADS (20)\r
-\r
 /**\r
   Check all the variables for SG list heads and get the count and addresses.\r
 \r
@@ -861,17 +861,19 @@ GetScatterGatherHeadEntries (
   OUT EFI_PHYSICAL_ADDRESS **HeadList\r
   )\r
 {\r
-  EFI_STATUS                       Status;\r
-  UINTN                            Size;\r
-  UINTN                            Index;\r
-  UINTN                            TempIndex;\r
-  UINTN                            ValidIndex;\r
-  BOOLEAN                          Flag;\r
-  CHAR16                           CapsuleVarName[30];\r
-  CHAR16                           *TempVarName;\r
-  EFI_PHYSICAL_ADDRESS             CapsuleDataPtr64;\r
-  EFI_PEI_READ_ONLY_VARIABLE2_PPI  *PPIVariableServices;\r
-  EFI_PHYSICAL_ADDRESS             TempList[MAX_SG_LIST_HEADS];\r
+  EFI_STATUS                        Status;\r
+  UINTN                             Size;\r
+  UINTN                             Index;\r
+  UINTN                             TempIndex;\r
+  UINTN                             ValidIndex;\r
+  BOOLEAN                           Flag;\r
+  CHAR16                            CapsuleVarName[30];\r
+  CHAR16                            *TempVarName;\r
+  EFI_PHYSICAL_ADDRESS              CapsuleDataPtr64;\r
+  EFI_PEI_READ_ONLY_VARIABLE2_PPI   *PPIVariableServices;\r
+  EFI_PHYSICAL_ADDRESS              *TempList;\r
+  EFI_PHYSICAL_ADDRESS              *EnlargedTempList;\r
+  UINTN                             TempListLength;\r
 \r
   Index             = 0;\r
   TempVarName       = NULL;\r
@@ -901,12 +903,22 @@ GetScatterGatherHeadEntries (
     return Status;\r
   }\r
 \r
+  //\r
+  // Allocate memory for sg list head\r
+  //\r
+  TempListLength = DEFAULT_SG_LIST_HEADS * sizeof (EFI_PHYSICAL_ADDRESS);\r
+  TempList = AllocateZeroPool (TempListLength);\r
+  if (TempList == NULL) {\r
+    DEBUG((DEBUG_ERROR, "Failed to allocate memory\n"));\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+\r
   //\r
   // setup var name buffer for update capsules\r
   //\r
   StrCpyS (CapsuleVarName, sizeof (CapsuleVarName) / sizeof (CHAR16), EFI_CAPSULE_VARIABLE_NAME);\r
   TempVarName = CapsuleVarName + StrLen (CapsuleVarName);\r
-  while (ValidIndex < MAX_SG_LIST_HEADS) {\r
+  while (TRUE) {\r
     if (Index != 0) {\r
       UnicodeValueToStringS (\r
         TempVarName,\r
@@ -948,6 +960,17 @@ GetScatterGatherHeadEntries (
       continue;\r
     }\r
 \r
+    //\r
+    // The TempList is full, enlarge it\r
+    //\r
+    if ((ValidIndex + 1) >= TempListLength) {\r
+      EnlargedTempList = AllocateZeroPool (TempListLength * 2);\r
+      CopyMem (EnlargedTempList, TempList, TempListLength);\r
+      FreePool (TempList);\r
+      TempList = EnlargedTempList;\r
+      TempListLength *= 2;\r
+    }\r
+\r
     //\r
     // add it to the cached list\r
     //\r