From: Zhichao Gao Date: Mon, 10 Jun 2019 01:52:22 +0000 (+0800) Subject: MdeMoudlePkg/CapsulePei: Optimize GetScatterGatherHeadEntries X-Git-Tag: edk2-stable201908~365 X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=commitdiff_plain;h=d96f83de5e7c25579a458b4dd17c131fa96a173b MdeMoudlePkg/CapsulePei: Optimize GetScatterGatherHeadEntries REF: https://bugzilla.tianocore.org/show_bug.cgi?id=1853 Rename the MACRO from MAX_SG_LIST_HEADS to DEFAULT_SG_LIST_HEADS. GetScatterGatherHeadEntries: use allocated buffer instead of fixed array to handle the condition which the SG list is larger then the array size. Cc: Jian J Wang Cc: Hao A Wu Cc: Ray Ni Cc: Star Zeng Cc: Liming Gao Cc: Sean Brogan Cc: Michael Turner Cc: Bret Barkelew Cc: Leif Lindholm Signed-off-by: Zhichao gao Reviewed-by: Leif Lindholm Reviewed-by: Hao A Wu --- diff --git a/MdeModulePkg/Universal/CapsulePei/UefiCapsule.c b/MdeModulePkg/Universal/CapsulePei/UefiCapsule.c index ce6d95a786..3ac95b7be6 100644 --- a/MdeModulePkg/Universal/CapsulePei/UefiCapsule.c +++ b/MdeModulePkg/Universal/CapsulePei/UefiCapsule.c @@ -10,6 +10,8 @@ SPDX-License-Identifier: BSD-2-Clause-Patent #include "Capsule.h" +#define DEFAULT_SG_LIST_HEADS (20) + #ifdef MDE_CPU_IA32 // // Global Descriptor Table (GDT) @@ -841,8 +843,6 @@ AreCapsulesStaged ( return FALSE; } -#define MAX_SG_LIST_HEADS (20) - /** Check all the variables for SG list heads and get the count and addresses. @@ -861,17 +861,19 @@ GetScatterGatherHeadEntries ( OUT EFI_PHYSICAL_ADDRESS **HeadList ) { - EFI_STATUS Status; - UINTN Size; - UINTN Index; - UINTN TempIndex; - UINTN ValidIndex; - BOOLEAN Flag; - CHAR16 CapsuleVarName[30]; - CHAR16 *TempVarName; - EFI_PHYSICAL_ADDRESS CapsuleDataPtr64; - EFI_PEI_READ_ONLY_VARIABLE2_PPI *PPIVariableServices; - EFI_PHYSICAL_ADDRESS TempList[MAX_SG_LIST_HEADS]; + EFI_STATUS Status; + UINTN Size; + UINTN Index; + UINTN TempIndex; + UINTN ValidIndex; + BOOLEAN Flag; + CHAR16 CapsuleVarName[30]; + CHAR16 *TempVarName; + EFI_PHYSICAL_ADDRESS CapsuleDataPtr64; + EFI_PEI_READ_ONLY_VARIABLE2_PPI *PPIVariableServices; + EFI_PHYSICAL_ADDRESS *TempList; + EFI_PHYSICAL_ADDRESS *EnlargedTempList; + UINTN TempListLength; Index = 0; TempVarName = NULL; @@ -901,12 +903,22 @@ GetScatterGatherHeadEntries ( return Status; } + // + // Allocate memory for sg list head + // + TempListLength = DEFAULT_SG_LIST_HEADS * sizeof (EFI_PHYSICAL_ADDRESS); + TempList = AllocateZeroPool (TempListLength); + if (TempList == NULL) { + DEBUG((DEBUG_ERROR, "Failed to allocate memory\n")); + return EFI_OUT_OF_RESOURCES; + } + // // setup var name buffer for update capsules // StrCpyS (CapsuleVarName, sizeof (CapsuleVarName) / sizeof (CHAR16), EFI_CAPSULE_VARIABLE_NAME); TempVarName = CapsuleVarName + StrLen (CapsuleVarName); - while (ValidIndex < MAX_SG_LIST_HEADS) { + while (TRUE) { if (Index != 0) { UnicodeValueToStringS ( TempVarName, @@ -948,6 +960,17 @@ GetScatterGatherHeadEntries ( continue; } + // + // The TempList is full, enlarge it + // + if ((ValidIndex + 1) >= TempListLength) { + EnlargedTempList = AllocateZeroPool (TempListLength * 2); + CopyMem (EnlargedTempList, TempList, TempListLength); + FreePool (TempList); + TempList = EnlargedTempList; + TempListLength *= 2; + } + // // add it to the cached list //