public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
* [edk2-devel][Patch] StandaloneMmPkg/FvLib: Support large file with EFI_FFS_FILE_HEADER2.
@ 2021-12-09  6:51 Xu, Wei6
  2021-12-13 11:50 ` Ard Biesheuvel
  2021-12-15  5:45 ` 回复: " gaoliming
  0 siblings, 2 replies; 4+ messages in thread
From: Xu, Wei6 @ 2021-12-09  6:51 UTC (permalink / raw)
  To: devel
  Cc: Ard Biesheuvel, Sami Mujawar, Jiewen Yao, Supreeth Venkatesh,
	Liming Gao

REF: https://bugzilla.tianocore.org/show_bug.cgi?id=3769

Current FvLib will hit parse issue when encountering LARGE file, then
ignore latter ffs/section, thus causing required drivers not being
dispatched. Therefore, need to add support for EFI_FFS_FILE_HEADER2
and EFI_COMMON_SECTION_HEADER2 in FvLib to fix this issue.

Cc: Ard Biesheuvel <ardb+tianocore@kernel.org>
Cc: Sami Mujawar <sami.mujawar@arm.com>
Cc: Jiewen Yao <jiewen.yao@intel.com>
Cc: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
Cc: Liming Gao <gaoliming@byosoft.com.cn>
Signed-off-by: Wei6 Xu <wei6.xu@intel.com>
---
 StandaloneMmPkg/Library/FvLib/FvLib.c | 65 ++++++++++++++++++++++++-----------
 1 file changed, 45 insertions(+), 20 deletions(-)

diff --git a/StandaloneMmPkg/Library/FvLib/FvLib.c b/StandaloneMmPkg/Library/FvLib/FvLib.c
index aa36a35eff..89504b9ee9 100644
--- a/StandaloneMmPkg/Library/FvLib/FvLib.c
+++ b/StandaloneMmPkg/Library/FvLib/FvLib.c
@@ -1,8 +1,8 @@
 /** @file
 
-Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
+Copyright (c) 2015 - 2021, Intel Corporation. All rights reserved.<BR>
 Copyright (c) 2016 - 2018, ARM Limited. All rights reserved.<BR>
 
 SPDX-License-Identifier: BSD-2-Clause-Patent
 
 **/
@@ -61,22 +61,24 @@ CalculateHeaderChecksum (
   )
 {
   UINT8  *ptr;
   UINTN  Index;
   UINT8  Sum;
+  UINTN  Size;
 
-  Sum = 0;
-  ptr = (UINT8 *)FileHeader;
+  Sum  = 0;
+  ptr  = (UINT8 *)FileHeader;
+  Size = IS_FFS_FILE2 (FileHeader) ? sizeof (EFI_FFS_FILE_HEADER2) : sizeof (EFI_FFS_FILE_HEADER);
 
-  for (Index = 0; Index < sizeof (EFI_FFS_FILE_HEADER) - 3; Index += 4) {
+  for (Index = 0; Index < Size - 3; Index += 4) {
     Sum = (UINT8)(Sum + ptr[Index]);
     Sum = (UINT8)(Sum + ptr[Index + 1]);
     Sum = (UINT8)(Sum + ptr[Index + 2]);
     Sum = (UINT8)(Sum + ptr[Index + 3]);
   }
 
-  for ( ; Index < sizeof (EFI_FFS_FILE_HEADER); Index++) {
+  for ( ; Index < Size; Index++) {
     Sum = (UINT8)(Sum + ptr[Index]);
   }
 
   //
   // State field (since this indicates the different state of file).
@@ -155,11 +157,12 @@ FfsFindNextFile (
   } else {
     //
     // Length is 24 bits wide so mask upper 8 bits
     // FileLength is adjusted to FileOccupiedSize as it is 8 byte aligned.
     //
-    FileLength       = FFS_FILE_SIZE (*FileHeader);
+    FileLength = IS_FFS_FILE2 (*FileHeader) ?
+                 FFS_FILE2_SIZE (*FileHeader) : FFS_FILE_SIZE (*FileHeader);
     FileOccupiedSize = GET_OCCUPIED_SIZE (FileLength, 8);
     FfsFileHeader    = (EFI_FFS_FILE_HEADER *)((UINT8 *)*FileHeader + FileOccupiedSize);
   }
 
   FileOffset = (UINT32)((UINT8 *)FfsFileHeader - (UINT8 *)FwVolHeader);
@@ -170,18 +173,25 @@ FfsFindNextFile (
     //
     FileState = GetFileState (ErasePolarity, FfsFileHeader);
 
     switch (FileState) {
       case EFI_FILE_HEADER_INVALID:
-        FileOffset   += sizeof (EFI_FFS_FILE_HEADER);
-        FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)FfsFileHeader + sizeof (EFI_FFS_FILE_HEADER));
+        if (IS_FFS_FILE2 (FfsFileHeader)) {
+          FileOffset   += sizeof (EFI_FFS_FILE_HEADER2);
+          FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)FfsFileHeader + sizeof (EFI_FFS_FILE_HEADER2));
+        } else {
+          FileOffset   += sizeof (EFI_FFS_FILE_HEADER);
+          FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)FfsFileHeader + sizeof (EFI_FFS_FILE_HEADER));
+        }
+
         break;
 
       case EFI_FILE_DATA_VALID:
       case EFI_FILE_MARKED_FOR_UPDATE:
         if (CalculateHeaderChecksum (FfsFileHeader) == 0) {
-          FileLength       = FFS_FILE_SIZE (FfsFileHeader);
+          FileLength = IS_FFS_FILE2 (FfsFileHeader) ?
+                       FFS_FILE2_SIZE (FfsFileHeader) : FFS_FILE_SIZE (FfsFileHeader);
           FileOccupiedSize = GET_OCCUPIED_SIZE (FileLength, 8);
 
           if ((SearchType == FfsFileHeader->Type) || (SearchType == EFI_FV_FILETYPE_ALL)) {
             *FileHeader = FfsFileHeader;
 
@@ -195,11 +205,12 @@ FfsFindNextFile (
         }
 
         break;
 
       case EFI_FILE_DELETED:
-        FileLength       = FFS_FILE_SIZE (FfsFileHeader);
+        FileLength = IS_FFS_FILE2 (FfsFileHeader) ?
+                     FFS_FILE2_SIZE (FfsFileHeader) : FFS_FILE_SIZE (FfsFileHeader);
         FileOccupiedSize = GET_OCCUPIED_SIZE (FileLength, 8);
         FileOffset      += FileOccupiedSize;
         FfsFileHeader    = (EFI_FFS_FILE_HEADER *)((UINT8 *)FfsFileHeader + FileOccupiedSize);
         break;
 
@@ -251,11 +262,11 @@ FindFfsSectionInSections (
 
     CurrentAddress = EndOfSection;
 
     Section = (EFI_COMMON_SECTION_HEADER *)(UINTN)CurrentAddress;
 
-    Size = SECTION_SIZE (Section);
+    Size = IS_SECTION2 (Section) ? SECTION2_SIZE (Section) : SECTION_SIZE (Section);
     if (Size < sizeof (*Section)) {
       return EFI_VOLUME_CORRUPTED;
     }
 
     EndOfSection = CurrentAddress + Size;
@@ -304,13 +315,17 @@ FfsFindSection (
   //
   // Size is 24 bits wide so mask upper 8 bits.
   //    Does not include FfsFileHeader header size
   // FileSize is adjusted to FileOccupiedSize as it is 8 byte aligned.
   //
-  Section   = (EFI_COMMON_SECTION_HEADER *)(FfsFileHeader + 1);
-  FileSize  = FFS_FILE_SIZE (FfsFileHeader);
-  FileSize -= sizeof (EFI_FFS_FILE_HEADER);
+  if (IS_FFS_FILE2 (FfsFileHeader)) {
+    Section  = (EFI_COMMON_SECTION_HEADER *)((EFI_FFS_FILE_HEADER2 *)FfsFileHeader + 1);
+    FileSize = FFS_FILE2_SIZE (FfsFileHeader) - sizeof (EFI_FFS_FILE_HEADER2);
+  } else {
+    Section  = (EFI_COMMON_SECTION_HEADER *)(FfsFileHeader + 1);
+    FileSize = FFS_FILE_SIZE (FfsFileHeader) - sizeof (EFI_FFS_FILE_HEADER);
+  }
 
   Status = FindFfsSectionInSections (
              Section,
              FileSize,
              SectionType,
@@ -349,29 +364,39 @@ FfsFindSectionData (
   //
   // Size is 24 bits wide so mask upper 8 bits.
   // Does not include FfsFileHeader header size
   // FileSize is adjusted to FileOccupiedSize as it is 8 byte aligned.
   //
-  Section   = (EFI_COMMON_SECTION_HEADER *)(FfsFileHeader + 1);
-  FileSize  = FFS_FILE_SIZE (FfsFileHeader);
-  FileSize -= sizeof (EFI_FFS_FILE_HEADER);
+  if (IS_FFS_FILE2 (FfsFileHeader)) {
+    Section  = (EFI_COMMON_SECTION_HEADER *)((EFI_FFS_FILE_HEADER2 *)FfsFileHeader + 1);
+    FileSize = FFS_FILE2_SIZE (FfsFileHeader) - sizeof (EFI_FFS_FILE_HEADER2);
+  } else {
+    Section  = (EFI_COMMON_SECTION_HEADER *)(FfsFileHeader + 1);
+    FileSize = FFS_FILE_SIZE (FfsFileHeader) - sizeof (EFI_FFS_FILE_HEADER);
+  }
 
   *SectionData = NULL;
   ParsedLength = 0;
   while (ParsedLength < FileSize) {
     if (Section->Type == SectionType) {
-      *SectionData     = (VOID *)(Section + 1);
-      *SectionDataSize = SECTION_SIZE (Section);
+      if (IS_SECTION2 (Section)) {
+        *SectionData     = (VOID *)((EFI_COMMON_SECTION_HEADER2 *)Section + 1);
+        *SectionDataSize = SECTION2_SIZE (Section);
+      } else {
+        *SectionData     = (VOID *)(Section + 1);
+        *SectionDataSize = SECTION_SIZE (Section);
+      }
+
       return EFI_SUCCESS;
     }
 
     //
     // Size is 24 bits wide so mask upper 8 bits.
     // SectionLength is adjusted it is 4 byte aligned.
     // Go to the next section
     //
-    SectionLength = SECTION_SIZE (Section);
+    SectionLength = IS_SECTION2 (Section) ? SECTION2_SIZE (Section) : SECTION_SIZE (Section);
     SectionLength = GET_OCCUPIED_SIZE (SectionLength, 4);
 
     ParsedLength += SectionLength;
     Section       = (EFI_COMMON_SECTION_HEADER *)((UINT8 *)Section + SectionLength);
   }
-- 
2.16.2.windows.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [edk2-devel][Patch] StandaloneMmPkg/FvLib: Support large file with EFI_FFS_FILE_HEADER2.
  2021-12-09  6:51 [edk2-devel][Patch] StandaloneMmPkg/FvLib: Support large file with EFI_FFS_FILE_HEADER2 Xu, Wei6
@ 2021-12-13 11:50 ` Ard Biesheuvel
  2021-12-15  5:45 ` 回复: " gaoliming
  1 sibling, 0 replies; 4+ messages in thread
From: Ard Biesheuvel @ 2021-12-13 11:50 UTC (permalink / raw)
  To: Wei6 Xu
  Cc: edk2-devel-groups-io, Ard Biesheuvel, Sami Mujawar, Jiewen Yao,
	Supreeth Venkatesh, Liming Gao

On Thu, 9 Dec 2021 at 07:51, Wei6 Xu <wei6.xu@intel.com> wrote:
>
> REF: https://bugzilla.tianocore.org/show_bug.cgi?id=3769
>
> Current FvLib will hit parse issue when encountering LARGE file, then
> ignore latter ffs/section, thus causing required drivers not being
> dispatched. Therefore, need to add support for EFI_FFS_FILE_HEADER2
> and EFI_COMMON_SECTION_HEADER2 in FvLib to fix this issue.
>
> Cc: Ard Biesheuvel <ardb+tianocore@kernel.org>
> Cc: Sami Mujawar <sami.mujawar@arm.com>
> Cc: Jiewen Yao <jiewen.yao@intel.com>
> Cc: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
> Cc: Liming Gao <gaoliming@byosoft.com.cn>
> Signed-off-by: Wei6 Xu <wei6.xu@intel.com>
> ---

Is this an issue in practice? How large does the FV that is being
dispatched into MM context have to be in order for this issue to
trigger?


>  StandaloneMmPkg/Library/FvLib/FvLib.c | 65 ++++++++++++++++++++++++-----------
>  1 file changed, 45 insertions(+), 20 deletions(-)
>
> diff --git a/StandaloneMmPkg/Library/FvLib/FvLib.c b/StandaloneMmPkg/Library/FvLib/FvLib.c
> index aa36a35eff..89504b9ee9 100644
> --- a/StandaloneMmPkg/Library/FvLib/FvLib.c
> +++ b/StandaloneMmPkg/Library/FvLib/FvLib.c
> @@ -1,8 +1,8 @@
>  /** @file
>
> -Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
> +Copyright (c) 2015 - 2021, Intel Corporation. All rights reserved.<BR>
>  Copyright (c) 2016 - 2018, ARM Limited. All rights reserved.<BR>
>
>  SPDX-License-Identifier: BSD-2-Clause-Patent
>
>  **/
> @@ -61,22 +61,24 @@ CalculateHeaderChecksum (
>    )
>  {
>    UINT8  *ptr;
>    UINTN  Index;
>    UINT8  Sum;
> +  UINTN  Size;
>
> -  Sum = 0;
> -  ptr = (UINT8 *)FileHeader;
> +  Sum  = 0;
> +  ptr  = (UINT8 *)FileHeader;
> +  Size = IS_FFS_FILE2 (FileHeader) ? sizeof (EFI_FFS_FILE_HEADER2) : sizeof (EFI_FFS_FILE_HEADER);
>
> -  for (Index = 0; Index < sizeof (EFI_FFS_FILE_HEADER) - 3; Index += 4) {
> +  for (Index = 0; Index < Size - 3; Index += 4) {
>      Sum = (UINT8)(Sum + ptr[Index]);
>      Sum = (UINT8)(Sum + ptr[Index + 1]);
>      Sum = (UINT8)(Sum + ptr[Index + 2]);
>      Sum = (UINT8)(Sum + ptr[Index + 3]);
>    }
>
> -  for ( ; Index < sizeof (EFI_FFS_FILE_HEADER); Index++) {
> +  for ( ; Index < Size; Index++) {
>      Sum = (UINT8)(Sum + ptr[Index]);
>    }
>
>    //
>    // State field (since this indicates the different state of file).
> @@ -155,11 +157,12 @@ FfsFindNextFile (
>    } else {
>      //
>      // Length is 24 bits wide so mask upper 8 bits
>      // FileLength is adjusted to FileOccupiedSize as it is 8 byte aligned.
>      //
> -    FileLength       = FFS_FILE_SIZE (*FileHeader);
> +    FileLength = IS_FFS_FILE2 (*FileHeader) ?
> +                 FFS_FILE2_SIZE (*FileHeader) : FFS_FILE_SIZE (*FileHeader);
>      FileOccupiedSize = GET_OCCUPIED_SIZE (FileLength, 8);
>      FfsFileHeader    = (EFI_FFS_FILE_HEADER *)((UINT8 *)*FileHeader + FileOccupiedSize);
>    }
>
>    FileOffset = (UINT32)((UINT8 *)FfsFileHeader - (UINT8 *)FwVolHeader);
> @@ -170,18 +173,25 @@ FfsFindNextFile (
>      //
>      FileState = GetFileState (ErasePolarity, FfsFileHeader);
>
>      switch (FileState) {
>        case EFI_FILE_HEADER_INVALID:
> -        FileOffset   += sizeof (EFI_FFS_FILE_HEADER);
> -        FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)FfsFileHeader + sizeof (EFI_FFS_FILE_HEADER));
> +        if (IS_FFS_FILE2 (FfsFileHeader)) {
> +          FileOffset   += sizeof (EFI_FFS_FILE_HEADER2);
> +          FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)FfsFileHeader + sizeof (EFI_FFS_FILE_HEADER2));
> +        } else {
> +          FileOffset   += sizeof (EFI_FFS_FILE_HEADER);
> +          FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)FfsFileHeader + sizeof (EFI_FFS_FILE_HEADER));
> +        }
> +
>          break;
>
>        case EFI_FILE_DATA_VALID:
>        case EFI_FILE_MARKED_FOR_UPDATE:
>          if (CalculateHeaderChecksum (FfsFileHeader) == 0) {
> -          FileLength       = FFS_FILE_SIZE (FfsFileHeader);
> +          FileLength = IS_FFS_FILE2 (FfsFileHeader) ?
> +                       FFS_FILE2_SIZE (FfsFileHeader) : FFS_FILE_SIZE (FfsFileHeader);
>            FileOccupiedSize = GET_OCCUPIED_SIZE (FileLength, 8);
>
>            if ((SearchType == FfsFileHeader->Type) || (SearchType == EFI_FV_FILETYPE_ALL)) {
>              *FileHeader = FfsFileHeader;
>
> @@ -195,11 +205,12 @@ FfsFindNextFile (
>          }
>
>          break;
>
>        case EFI_FILE_DELETED:
> -        FileLength       = FFS_FILE_SIZE (FfsFileHeader);
> +        FileLength = IS_FFS_FILE2 (FfsFileHeader) ?
> +                     FFS_FILE2_SIZE (FfsFileHeader) : FFS_FILE_SIZE (FfsFileHeader);
>          FileOccupiedSize = GET_OCCUPIED_SIZE (FileLength, 8);
>          FileOffset      += FileOccupiedSize;
>          FfsFileHeader    = (EFI_FFS_FILE_HEADER *)((UINT8 *)FfsFileHeader + FileOccupiedSize);
>          break;
>
> @@ -251,11 +262,11 @@ FindFfsSectionInSections (
>
>      CurrentAddress = EndOfSection;
>
>      Section = (EFI_COMMON_SECTION_HEADER *)(UINTN)CurrentAddress;
>
> -    Size = SECTION_SIZE (Section);
> +    Size = IS_SECTION2 (Section) ? SECTION2_SIZE (Section) : SECTION_SIZE (Section);
>      if (Size < sizeof (*Section)) {
>        return EFI_VOLUME_CORRUPTED;
>      }
>
>      EndOfSection = CurrentAddress + Size;
> @@ -304,13 +315,17 @@ FfsFindSection (
>    //
>    // Size is 24 bits wide so mask upper 8 bits.
>    //    Does not include FfsFileHeader header size
>    // FileSize is adjusted to FileOccupiedSize as it is 8 byte aligned.
>    //
> -  Section   = (EFI_COMMON_SECTION_HEADER *)(FfsFileHeader + 1);
> -  FileSize  = FFS_FILE_SIZE (FfsFileHeader);
> -  FileSize -= sizeof (EFI_FFS_FILE_HEADER);
> +  if (IS_FFS_FILE2 (FfsFileHeader)) {
> +    Section  = (EFI_COMMON_SECTION_HEADER *)((EFI_FFS_FILE_HEADER2 *)FfsFileHeader + 1);
> +    FileSize = FFS_FILE2_SIZE (FfsFileHeader) - sizeof (EFI_FFS_FILE_HEADER2);
> +  } else {
> +    Section  = (EFI_COMMON_SECTION_HEADER *)(FfsFileHeader + 1);
> +    FileSize = FFS_FILE_SIZE (FfsFileHeader) - sizeof (EFI_FFS_FILE_HEADER);
> +  }
>
>    Status = FindFfsSectionInSections (
>               Section,
>               FileSize,
>               SectionType,
> @@ -349,29 +364,39 @@ FfsFindSectionData (
>    //
>    // Size is 24 bits wide so mask upper 8 bits.
>    // Does not include FfsFileHeader header size
>    // FileSize is adjusted to FileOccupiedSize as it is 8 byte aligned.
>    //
> -  Section   = (EFI_COMMON_SECTION_HEADER *)(FfsFileHeader + 1);
> -  FileSize  = FFS_FILE_SIZE (FfsFileHeader);
> -  FileSize -= sizeof (EFI_FFS_FILE_HEADER);
> +  if (IS_FFS_FILE2 (FfsFileHeader)) {
> +    Section  = (EFI_COMMON_SECTION_HEADER *)((EFI_FFS_FILE_HEADER2 *)FfsFileHeader + 1);
> +    FileSize = FFS_FILE2_SIZE (FfsFileHeader) - sizeof (EFI_FFS_FILE_HEADER2);
> +  } else {
> +    Section  = (EFI_COMMON_SECTION_HEADER *)(FfsFileHeader + 1);
> +    FileSize = FFS_FILE_SIZE (FfsFileHeader) - sizeof (EFI_FFS_FILE_HEADER);
> +  }
>
>    *SectionData = NULL;
>    ParsedLength = 0;
>    while (ParsedLength < FileSize) {
>      if (Section->Type == SectionType) {
> -      *SectionData     = (VOID *)(Section + 1);
> -      *SectionDataSize = SECTION_SIZE (Section);
> +      if (IS_SECTION2 (Section)) {
> +        *SectionData     = (VOID *)((EFI_COMMON_SECTION_HEADER2 *)Section + 1);
> +        *SectionDataSize = SECTION2_SIZE (Section);
> +      } else {
> +        *SectionData     = (VOID *)(Section + 1);
> +        *SectionDataSize = SECTION_SIZE (Section);
> +      }
> +
>        return EFI_SUCCESS;
>      }
>
>      //
>      // Size is 24 bits wide so mask upper 8 bits.
>      // SectionLength is adjusted it is 4 byte aligned.
>      // Go to the next section
>      //
> -    SectionLength = SECTION_SIZE (Section);
> +    SectionLength = IS_SECTION2 (Section) ? SECTION2_SIZE (Section) : SECTION_SIZE (Section);
>      SectionLength = GET_OCCUPIED_SIZE (SectionLength, 4);
>
>      ParsedLength += SectionLength;
>      Section       = (EFI_COMMON_SECTION_HEADER *)((UINT8 *)Section + SectionLength);
>    }
> --
> 2.16.2.windows.1
>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* 回复: [edk2-devel][Patch] StandaloneMmPkg/FvLib: Support large file with EFI_FFS_FILE_HEADER2.
  2021-12-09  6:51 [edk2-devel][Patch] StandaloneMmPkg/FvLib: Support large file with EFI_FFS_FILE_HEADER2 Xu, Wei6
  2021-12-13 11:50 ` Ard Biesheuvel
@ 2021-12-15  5:45 ` gaoliming
  2021-12-15  7:25   ` Ard Biesheuvel
  1 sibling, 1 reply; 4+ messages in thread
From: gaoliming @ 2021-12-15  5:45 UTC (permalink / raw)
  To: devel, wei6.xu
  Cc: 'Ard Biesheuvel', 'Sami Mujawar',
	'Jiewen Yao', 'Supreeth Venkatesh'

Reviewed-by: Liming Gao <gaoliming@byosoft.com.cn>

> -----邮件原件-----
> 发件人: devel@edk2.groups.io <devel@edk2.groups.io> 代表 Xu, Wei6
> 发送时间: 2021年12月9日 14:52
> 收件人: devel@edk2.groups.io
> 抄送: Ard Biesheuvel <ardb+tianocore@kernel.org>; Sami Mujawar
> <sami.mujawar@arm.com>; Jiewen Yao <jiewen.yao@intel.com>; Supreeth
> Venkatesh <supreeth.venkatesh@arm.com>; Liming Gao
> <gaoliming@byosoft.com.cn>
> 主题: [edk2-devel][Patch] StandaloneMmPkg/FvLib: Support large file with
> EFI_FFS_FILE_HEADER2.
> 
> REF: https://bugzilla.tianocore.org/show_bug.cgi?id=3769
> 
> Current FvLib will hit parse issue when encountering LARGE file, then
> ignore latter ffs/section, thus causing required drivers not being
> dispatched. Therefore, need to add support for EFI_FFS_FILE_HEADER2
> and EFI_COMMON_SECTION_HEADER2 in FvLib to fix this issue.
> 
> Cc: Ard Biesheuvel <ardb+tianocore@kernel.org>
> Cc: Sami Mujawar <sami.mujawar@arm.com>
> Cc: Jiewen Yao <jiewen.yao@intel.com>
> Cc: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
> Cc: Liming Gao <gaoliming@byosoft.com.cn>
> Signed-off-by: Wei6 Xu <wei6.xu@intel.com>
> ---
>  StandaloneMmPkg/Library/FvLib/FvLib.c | 65
> ++++++++++++++++++++++++-----------
>  1 file changed, 45 insertions(+), 20 deletions(-)
> 
> diff --git a/StandaloneMmPkg/Library/FvLib/FvLib.c
> b/StandaloneMmPkg/Library/FvLib/FvLib.c
> index aa36a35eff..89504b9ee9 100644
> --- a/StandaloneMmPkg/Library/FvLib/FvLib.c
> +++ b/StandaloneMmPkg/Library/FvLib/FvLib.c
> @@ -1,8 +1,8 @@
>  /** @file
> 
> -Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
> +Copyright (c) 2015 - 2021, Intel Corporation. All rights reserved.<BR>
>  Copyright (c) 2016 - 2018, ARM Limited. All rights reserved.<BR>
> 
>  SPDX-License-Identifier: BSD-2-Clause-Patent
> 
>  **/
> @@ -61,22 +61,24 @@ CalculateHeaderChecksum (
>    )
>  {
>    UINT8  *ptr;
>    UINTN  Index;
>    UINT8  Sum;
> +  UINTN  Size;
> 
> -  Sum = 0;
> -  ptr = (UINT8 *)FileHeader;
> +  Sum  = 0;
> +  ptr  = (UINT8 *)FileHeader;
> +  Size = IS_FFS_FILE2 (FileHeader) ? sizeof (EFI_FFS_FILE_HEADER2) :
sizeof
> (EFI_FFS_FILE_HEADER);
> 
> -  for (Index = 0; Index < sizeof (EFI_FFS_FILE_HEADER) - 3; Index += 4) {
> +  for (Index = 0; Index < Size - 3; Index += 4) {
>      Sum = (UINT8)(Sum + ptr[Index]);
>      Sum = (UINT8)(Sum + ptr[Index + 1]);
>      Sum = (UINT8)(Sum + ptr[Index + 2]);
>      Sum = (UINT8)(Sum + ptr[Index + 3]);
>    }
> 
> -  for ( ; Index < sizeof (EFI_FFS_FILE_HEADER); Index++) {
> +  for ( ; Index < Size; Index++) {
>      Sum = (UINT8)(Sum + ptr[Index]);
>    }
> 
>    //
>    // State field (since this indicates the different state of file).
> @@ -155,11 +157,12 @@ FfsFindNextFile (
>    } else {
>      //
>      // Length is 24 bits wide so mask upper 8 bits
>      // FileLength is adjusted to FileOccupiedSize as it is 8 byte
aligned.
>      //
> -    FileLength       = FFS_FILE_SIZE (*FileHeader);
> +    FileLength = IS_FFS_FILE2 (*FileHeader) ?
> +                 FFS_FILE2_SIZE (*FileHeader) : FFS_FILE_SIZE
> (*FileHeader);
>      FileOccupiedSize = GET_OCCUPIED_SIZE (FileLength, 8);
>      FfsFileHeader    = (EFI_FFS_FILE_HEADER *)((UINT8 *)*FileHeader +
> FileOccupiedSize);
>    }
> 
>    FileOffset = (UINT32)((UINT8 *)FfsFileHeader - (UINT8 *)FwVolHeader);
> @@ -170,18 +173,25 @@ FfsFindNextFile (
>      //
>      FileState = GetFileState (ErasePolarity, FfsFileHeader);
> 
>      switch (FileState) {
>        case EFI_FILE_HEADER_INVALID:
> -        FileOffset   += sizeof (EFI_FFS_FILE_HEADER);
> -        FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)FfsFileHeader
> + sizeof (EFI_FFS_FILE_HEADER));
> +        if (IS_FFS_FILE2 (FfsFileHeader)) {
> +          FileOffset   += sizeof (EFI_FFS_FILE_HEADER2);
> +          FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8
> *)FfsFileHeader + sizeof (EFI_FFS_FILE_HEADER2));
> +        } else {
> +          FileOffset   += sizeof (EFI_FFS_FILE_HEADER);
> +          FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8
> *)FfsFileHeader + sizeof (EFI_FFS_FILE_HEADER));
> +        }
> +
>          break;
> 
>        case EFI_FILE_DATA_VALID:
>        case EFI_FILE_MARKED_FOR_UPDATE:
>          if (CalculateHeaderChecksum (FfsFileHeader) == 0) {
> -          FileLength       = FFS_FILE_SIZE (FfsFileHeader);
> +          FileLength = IS_FFS_FILE2 (FfsFileHeader) ?
> +                       FFS_FILE2_SIZE (FfsFileHeader) : FFS_FILE_SIZE
> (FfsFileHeader);
>            FileOccupiedSize = GET_OCCUPIED_SIZE (FileLength, 8);
> 
>            if ((SearchType == FfsFileHeader->Type) || (SearchType ==
> EFI_FV_FILETYPE_ALL)) {
>              *FileHeader = FfsFileHeader;
> 
> @@ -195,11 +205,12 @@ FfsFindNextFile (
>          }
> 
>          break;
> 
>        case EFI_FILE_DELETED:
> -        FileLength       = FFS_FILE_SIZE (FfsFileHeader);
> +        FileLength = IS_FFS_FILE2 (FfsFileHeader) ?
> +                     FFS_FILE2_SIZE (FfsFileHeader) : FFS_FILE_SIZE
> (FfsFileHeader);
>          FileOccupiedSize = GET_OCCUPIED_SIZE (FileLength, 8);
>          FileOffset      += FileOccupiedSize;
>          FfsFileHeader    = (EFI_FFS_FILE_HEADER *)((UINT8
> *)FfsFileHeader + FileOccupiedSize);
>          break;
> 
> @@ -251,11 +262,11 @@ FindFfsSectionInSections (
> 
>      CurrentAddress = EndOfSection;
> 
>      Section = (EFI_COMMON_SECTION_HEADER
> *)(UINTN)CurrentAddress;
> 
> -    Size = SECTION_SIZE (Section);
> +    Size = IS_SECTION2 (Section) ? SECTION2_SIZE (Section) :
> SECTION_SIZE (Section);
>      if (Size < sizeof (*Section)) {
>        return EFI_VOLUME_CORRUPTED;
>      }
> 
>      EndOfSection = CurrentAddress + Size;
> @@ -304,13 +315,17 @@ FfsFindSection (
>    //
>    // Size is 24 bits wide so mask upper 8 bits.
>    //    Does not include FfsFileHeader header size
>    // FileSize is adjusted to FileOccupiedSize as it is 8 byte aligned.
>    //
> -  Section   = (EFI_COMMON_SECTION_HEADER *)(FfsFileHeader + 1);
> -  FileSize  = FFS_FILE_SIZE (FfsFileHeader);
> -  FileSize -= sizeof (EFI_FFS_FILE_HEADER);
> +  if (IS_FFS_FILE2 (FfsFileHeader)) {
> +    Section  = (EFI_COMMON_SECTION_HEADER
> *)((EFI_FFS_FILE_HEADER2 *)FfsFileHeader + 1);
> +    FileSize = FFS_FILE2_SIZE (FfsFileHeader) - sizeof
> (EFI_FFS_FILE_HEADER2);
> +  } else {
> +    Section  = (EFI_COMMON_SECTION_HEADER *)(FfsFileHeader + 1);
> +    FileSize = FFS_FILE_SIZE (FfsFileHeader) - sizeof
> (EFI_FFS_FILE_HEADER);
> +  }
> 
>    Status = FindFfsSectionInSections (
>               Section,
>               FileSize,
>               SectionType,
> @@ -349,29 +364,39 @@ FfsFindSectionData (
>    //
>    // Size is 24 bits wide so mask upper 8 bits.
>    // Does not include FfsFileHeader header size
>    // FileSize is adjusted to FileOccupiedSize as it is 8 byte aligned.
>    //
> -  Section   = (EFI_COMMON_SECTION_HEADER *)(FfsFileHeader + 1);
> -  FileSize  = FFS_FILE_SIZE (FfsFileHeader);
> -  FileSize -= sizeof (EFI_FFS_FILE_HEADER);
> +  if (IS_FFS_FILE2 (FfsFileHeader)) {
> +    Section  = (EFI_COMMON_SECTION_HEADER
> *)((EFI_FFS_FILE_HEADER2 *)FfsFileHeader + 1);
> +    FileSize = FFS_FILE2_SIZE (FfsFileHeader) - sizeof
> (EFI_FFS_FILE_HEADER2);
> +  } else {
> +    Section  = (EFI_COMMON_SECTION_HEADER *)(FfsFileHeader + 1);
> +    FileSize = FFS_FILE_SIZE (FfsFileHeader) - sizeof
> (EFI_FFS_FILE_HEADER);
> +  }
> 
>    *SectionData = NULL;
>    ParsedLength = 0;
>    while (ParsedLength < FileSize) {
>      if (Section->Type == SectionType) {
> -      *SectionData     = (VOID *)(Section + 1);
> -      *SectionDataSize = SECTION_SIZE (Section);
> +      if (IS_SECTION2 (Section)) {
> +        *SectionData     = (VOID
> *)((EFI_COMMON_SECTION_HEADER2 *)Section + 1);
> +        *SectionDataSize = SECTION2_SIZE (Section);
> +      } else {
> +        *SectionData     = (VOID *)(Section + 1);
> +        *SectionDataSize = SECTION_SIZE (Section);
> +      }
> +
>        return EFI_SUCCESS;
>      }
> 
>      //
>      // Size is 24 bits wide so mask upper 8 bits.
>      // SectionLength is adjusted it is 4 byte aligned.
>      // Go to the next section
>      //
> -    SectionLength = SECTION_SIZE (Section);
> +    SectionLength = IS_SECTION2 (Section) ? SECTION2_SIZE (Section) :
> SECTION_SIZE (Section);
>      SectionLength = GET_OCCUPIED_SIZE (SectionLength, 4);
> 
>      ParsedLength += SectionLength;
>      Section       = (EFI_COMMON_SECTION_HEADER *)((UINT8
> *)Section + SectionLength);
>    }
> --
> 2.16.2.windows.1
> 
> 
> 
> 
> 




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [edk2-devel][Patch] StandaloneMmPkg/FvLib: Support large file with EFI_FFS_FILE_HEADER2.
  2021-12-15  5:45 ` 回复: " gaoliming
@ 2021-12-15  7:25   ` Ard Biesheuvel
  0 siblings, 0 replies; 4+ messages in thread
From: Ard Biesheuvel @ 2021-12-15  7:25 UTC (permalink / raw)
  To: edk2-devel-groups-io, Liming Gao (Byosoft address)
  Cc: Wei6 Xu, Ard Biesheuvel, Sami Mujawar, Jiewen Yao,
	Supreeth Venkatesh

On Wed, 15 Dec 2021 at 06:46, gaoliming <gaoliming@byosoft.com.cn> wrote:
>
> Reviewed-by: Liming Gao <gaoliming@byosoft.com.cn>
>

Merged as #2307

Thanks,

> > -----邮件原件-----
> > 发件人: devel@edk2.groups.io <devel@edk2.groups.io> 代表 Xu, Wei6
> > 发送时间: 2021年12月9日 14:52
> > 收件人: devel@edk2.groups.io
> > 抄送: Ard Biesheuvel <ardb+tianocore@kernel.org>; Sami Mujawar
> > <sami.mujawar@arm.com>; Jiewen Yao <jiewen.yao@intel.com>; Supreeth
> > Venkatesh <supreeth.venkatesh@arm.com>; Liming Gao
> > <gaoliming@byosoft.com.cn>
> > 主题: [edk2-devel][Patch] StandaloneMmPkg/FvLib: Support large file with
> > EFI_FFS_FILE_HEADER2.
> >
> > REF: https://bugzilla.tianocore.org/show_bug.cgi?id=3769
> >
> > Current FvLib will hit parse issue when encountering LARGE file, then
> > ignore latter ffs/section, thus causing required drivers not being
> > dispatched. Therefore, need to add support for EFI_FFS_FILE_HEADER2
> > and EFI_COMMON_SECTION_HEADER2 in FvLib to fix this issue.
> >
> > Cc: Ard Biesheuvel <ardb+tianocore@kernel.org>
> > Cc: Sami Mujawar <sami.mujawar@arm.com>
> > Cc: Jiewen Yao <jiewen.yao@intel.com>
> > Cc: Supreeth Venkatesh <supreeth.venkatesh@arm.com>
> > Cc: Liming Gao <gaoliming@byosoft.com.cn>
> > Signed-off-by: Wei6 Xu <wei6.xu@intel.com>
> > ---
> >  StandaloneMmPkg/Library/FvLib/FvLib.c | 65
> > ++++++++++++++++++++++++-----------
> >  1 file changed, 45 insertions(+), 20 deletions(-)
> >
> > diff --git a/StandaloneMmPkg/Library/FvLib/FvLib.c
> > b/StandaloneMmPkg/Library/FvLib/FvLib.c
> > index aa36a35eff..89504b9ee9 100644
> > --- a/StandaloneMmPkg/Library/FvLib/FvLib.c
> > +++ b/StandaloneMmPkg/Library/FvLib/FvLib.c
> > @@ -1,8 +1,8 @@
> >  /** @file
> >
> > -Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
> > +Copyright (c) 2015 - 2021, Intel Corporation. All rights reserved.<BR>
> >  Copyright (c) 2016 - 2018, ARM Limited. All rights reserved.<BR>
> >
> >  SPDX-License-Identifier: BSD-2-Clause-Patent
> >
> >  **/
> > @@ -61,22 +61,24 @@ CalculateHeaderChecksum (
> >    )
> >  {
> >    UINT8  *ptr;
> >    UINTN  Index;
> >    UINT8  Sum;
> > +  UINTN  Size;
> >
> > -  Sum = 0;
> > -  ptr = (UINT8 *)FileHeader;
> > +  Sum  = 0;
> > +  ptr  = (UINT8 *)FileHeader;
> > +  Size = IS_FFS_FILE2 (FileHeader) ? sizeof (EFI_FFS_FILE_HEADER2) :
> sizeof
> > (EFI_FFS_FILE_HEADER);
> >
> > -  for (Index = 0; Index < sizeof (EFI_FFS_FILE_HEADER) - 3; Index += 4) {
> > +  for (Index = 0; Index < Size - 3; Index += 4) {
> >      Sum = (UINT8)(Sum + ptr[Index]);
> >      Sum = (UINT8)(Sum + ptr[Index + 1]);
> >      Sum = (UINT8)(Sum + ptr[Index + 2]);
> >      Sum = (UINT8)(Sum + ptr[Index + 3]);
> >    }
> >
> > -  for ( ; Index < sizeof (EFI_FFS_FILE_HEADER); Index++) {
> > +  for ( ; Index < Size; Index++) {
> >      Sum = (UINT8)(Sum + ptr[Index]);
> >    }
> >
> >    //
> >    // State field (since this indicates the different state of file).
> > @@ -155,11 +157,12 @@ FfsFindNextFile (
> >    } else {
> >      //
> >      // Length is 24 bits wide so mask upper 8 bits
> >      // FileLength is adjusted to FileOccupiedSize as it is 8 byte
> aligned.
> >      //
> > -    FileLength       = FFS_FILE_SIZE (*FileHeader);
> > +    FileLength = IS_FFS_FILE2 (*FileHeader) ?
> > +                 FFS_FILE2_SIZE (*FileHeader) : FFS_FILE_SIZE
> > (*FileHeader);
> >      FileOccupiedSize = GET_OCCUPIED_SIZE (FileLength, 8);
> >      FfsFileHeader    = (EFI_FFS_FILE_HEADER *)((UINT8 *)*FileHeader +
> > FileOccupiedSize);
> >    }
> >
> >    FileOffset = (UINT32)((UINT8 *)FfsFileHeader - (UINT8 *)FwVolHeader);
> > @@ -170,18 +173,25 @@ FfsFindNextFile (
> >      //
> >      FileState = GetFileState (ErasePolarity, FfsFileHeader);
> >
> >      switch (FileState) {
> >        case EFI_FILE_HEADER_INVALID:
> > -        FileOffset   += sizeof (EFI_FFS_FILE_HEADER);
> > -        FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)FfsFileHeader
> > + sizeof (EFI_FFS_FILE_HEADER));
> > +        if (IS_FFS_FILE2 (FfsFileHeader)) {
> > +          FileOffset   += sizeof (EFI_FFS_FILE_HEADER2);
> > +          FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8
> > *)FfsFileHeader + sizeof (EFI_FFS_FILE_HEADER2));
> > +        } else {
> > +          FileOffset   += sizeof (EFI_FFS_FILE_HEADER);
> > +          FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8
> > *)FfsFileHeader + sizeof (EFI_FFS_FILE_HEADER));
> > +        }
> > +
> >          break;
> >
> >        case EFI_FILE_DATA_VALID:
> >        case EFI_FILE_MARKED_FOR_UPDATE:
> >          if (CalculateHeaderChecksum (FfsFileHeader) == 0) {
> > -          FileLength       = FFS_FILE_SIZE (FfsFileHeader);
> > +          FileLength = IS_FFS_FILE2 (FfsFileHeader) ?
> > +                       FFS_FILE2_SIZE (FfsFileHeader) : FFS_FILE_SIZE
> > (FfsFileHeader);
> >            FileOccupiedSize = GET_OCCUPIED_SIZE (FileLength, 8);
> >
> >            if ((SearchType == FfsFileHeader->Type) || (SearchType ==
> > EFI_FV_FILETYPE_ALL)) {
> >              *FileHeader = FfsFileHeader;
> >
> > @@ -195,11 +205,12 @@ FfsFindNextFile (
> >          }
> >
> >          break;
> >
> >        case EFI_FILE_DELETED:
> > -        FileLength       = FFS_FILE_SIZE (FfsFileHeader);
> > +        FileLength = IS_FFS_FILE2 (FfsFileHeader) ?
> > +                     FFS_FILE2_SIZE (FfsFileHeader) : FFS_FILE_SIZE
> > (FfsFileHeader);
> >          FileOccupiedSize = GET_OCCUPIED_SIZE (FileLength, 8);
> >          FileOffset      += FileOccupiedSize;
> >          FfsFileHeader    = (EFI_FFS_FILE_HEADER *)((UINT8
> > *)FfsFileHeader + FileOccupiedSize);
> >          break;
> >
> > @@ -251,11 +262,11 @@ FindFfsSectionInSections (
> >
> >      CurrentAddress = EndOfSection;
> >
> >      Section = (EFI_COMMON_SECTION_HEADER
> > *)(UINTN)CurrentAddress;
> >
> > -    Size = SECTION_SIZE (Section);
> > +    Size = IS_SECTION2 (Section) ? SECTION2_SIZE (Section) :
> > SECTION_SIZE (Section);
> >      if (Size < sizeof (*Section)) {
> >        return EFI_VOLUME_CORRUPTED;
> >      }
> >
> >      EndOfSection = CurrentAddress + Size;
> > @@ -304,13 +315,17 @@ FfsFindSection (
> >    //
> >    // Size is 24 bits wide so mask upper 8 bits.
> >    //    Does not include FfsFileHeader header size
> >    // FileSize is adjusted to FileOccupiedSize as it is 8 byte aligned.
> >    //
> > -  Section   = (EFI_COMMON_SECTION_HEADER *)(FfsFileHeader + 1);
> > -  FileSize  = FFS_FILE_SIZE (FfsFileHeader);
> > -  FileSize -= sizeof (EFI_FFS_FILE_HEADER);
> > +  if (IS_FFS_FILE2 (FfsFileHeader)) {
> > +    Section  = (EFI_COMMON_SECTION_HEADER
> > *)((EFI_FFS_FILE_HEADER2 *)FfsFileHeader + 1);
> > +    FileSize = FFS_FILE2_SIZE (FfsFileHeader) - sizeof
> > (EFI_FFS_FILE_HEADER2);
> > +  } else {
> > +    Section  = (EFI_COMMON_SECTION_HEADER *)(FfsFileHeader + 1);
> > +    FileSize = FFS_FILE_SIZE (FfsFileHeader) - sizeof
> > (EFI_FFS_FILE_HEADER);
> > +  }
> >
> >    Status = FindFfsSectionInSections (
> >               Section,
> >               FileSize,
> >               SectionType,
> > @@ -349,29 +364,39 @@ FfsFindSectionData (
> >    //
> >    // Size is 24 bits wide so mask upper 8 bits.
> >    // Does not include FfsFileHeader header size
> >    // FileSize is adjusted to FileOccupiedSize as it is 8 byte aligned.
> >    //
> > -  Section   = (EFI_COMMON_SECTION_HEADER *)(FfsFileHeader + 1);
> > -  FileSize  = FFS_FILE_SIZE (FfsFileHeader);
> > -  FileSize -= sizeof (EFI_FFS_FILE_HEADER);
> > +  if (IS_FFS_FILE2 (FfsFileHeader)) {
> > +    Section  = (EFI_COMMON_SECTION_HEADER
> > *)((EFI_FFS_FILE_HEADER2 *)FfsFileHeader + 1);
> > +    FileSize = FFS_FILE2_SIZE (FfsFileHeader) - sizeof
> > (EFI_FFS_FILE_HEADER2);
> > +  } else {
> > +    Section  = (EFI_COMMON_SECTION_HEADER *)(FfsFileHeader + 1);
> > +    FileSize = FFS_FILE_SIZE (FfsFileHeader) - sizeof
> > (EFI_FFS_FILE_HEADER);
> > +  }
> >
> >    *SectionData = NULL;
> >    ParsedLength = 0;
> >    while (ParsedLength < FileSize) {
> >      if (Section->Type == SectionType) {
> > -      *SectionData     = (VOID *)(Section + 1);
> > -      *SectionDataSize = SECTION_SIZE (Section);
> > +      if (IS_SECTION2 (Section)) {
> > +        *SectionData     = (VOID
> > *)((EFI_COMMON_SECTION_HEADER2 *)Section + 1);
> > +        *SectionDataSize = SECTION2_SIZE (Section);
> > +      } else {
> > +        *SectionData     = (VOID *)(Section + 1);
> > +        *SectionDataSize = SECTION_SIZE (Section);
> > +      }
> > +
> >        return EFI_SUCCESS;
> >      }
> >
> >      //
> >      // Size is 24 bits wide so mask upper 8 bits.
> >      // SectionLength is adjusted it is 4 byte aligned.
> >      // Go to the next section
> >      //
> > -    SectionLength = SECTION_SIZE (Section);
> > +    SectionLength = IS_SECTION2 (Section) ? SECTION2_SIZE (Section) :
> > SECTION_SIZE (Section);
> >      SectionLength = GET_OCCUPIED_SIZE (SectionLength, 4);
> >
> >      ParsedLength += SectionLength;
> >      Section       = (EFI_COMMON_SECTION_HEADER *)((UINT8
> > *)Section + SectionLength);
> >    }
> > --
> > 2.16.2.windows.1
> >
> >
> >
> >
> >
>
>
>
>
>
> 
>
>

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-12-15  7:25 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-12-09  6:51 [edk2-devel][Patch] StandaloneMmPkg/FvLib: Support large file with EFI_FFS_FILE_HEADER2 Xu, Wei6
2021-12-13 11:50 ` Ard Biesheuvel
2021-12-15  5:45 ` 回复: " gaoliming
2021-12-15  7:25   ` Ard Biesheuvel

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox