[edk2-devel] [RFC 3/5] EmbeddedPkg/PrePiLib: refactor IS_SECTION2() handling

Leif Lindholm leif at nuviainc.com
Wed Jul 1 20:01:16 UTC 2020


There are a bunch of IS_SECTION2() conditional statements in
FfsProcessSection, really breaking up the readability.
Add a set of static helper functions instead.

Signed-off-by: Leif Lindholm <leif at nuviainc.com>
---
 EmbeddedPkg/Library/PrePiLib/FwVol.c | 101 ++++++++++++++++-----------
 1 file changed, 61 insertions(+), 40 deletions(-)

diff --git a/EmbeddedPkg/Library/PrePiLib/FwVol.c b/EmbeddedPkg/Library/PrePiLib/FwVol.c
index fc40d8650be1..a0672c084471 100644
--- a/EmbeddedPkg/Library/PrePiLib/FwVol.c
+++ b/EmbeddedPkg/Library/PrePiLib/FwVol.c
@@ -266,6 +266,57 @@ FindFileEx (
   return EFI_NOT_FOUND;
 }
 
+STATIC
+UINTN
+FfsSectionHeaderSize (
+  IN EFI_COMMON_SECTION_HEADER                *Section
+  )
+{
+  if (IS_SECTION2 (Section)) {
+    return sizeof (EFI_COMMON_SECTION_HEADER2);
+  }
+
+  return sizeof (EFI_COMMON_SECTION_HEADER);
+}
+
+STATIC
+UINTN
+FfsSectionLength (
+  IN EFI_COMMON_SECTION_HEADER                *Section
+  )
+{
+  if (IS_SECTION2 (Section)) {
+    return SECTION2_SIZE (Section);
+  }
+
+  return SECTION_SIZE (Section);
+}
+
+STATIC
+UINTN
+FfsSectionCompressionType (
+  IN  EFI_COMMON_SECTION_HEADER               *Section
+  )
+{
+  if (IS_SECTION2 (Section)) {
+    return ((EFI_COMPRESSION_SECTION2 *)Section)->CompressionType;
+  }
+
+  return ((EFI_COMPRESSION_SECTION *)Section)->CompressionType;
+}
+
+STATIC
+UINTN
+FfsCompressionSectionHeaderSize (
+  IN  EFI_COMMON_SECTION_HEADER               *Section
+  )
+{
+  if (IS_SECTION2 (Section)) {
+    return sizeof (EFI_COMPRESSION_SECTION2);
+  }
+
+  return sizeof (EFI_COMPRESSION_SECTION);
+}
 
 /**
   Go through the file to search SectionType section,
@@ -289,8 +340,6 @@ FfsProcessSection (
   EFI_STATUS                              Status;
   UINT32                                  SectionLength;
   UINT32                                  ParsedLength;
-  EFI_COMPRESSION_SECTION                 *CompressionSection;
-  EFI_COMPRESSION_SECTION2                *CompressionSection2;
   UINT32                                  DstBufferSize;
   VOID                                    *ScratchBuffer;
   UINT32                                  ScratchBufferSize;
@@ -310,39 +359,22 @@ FfsProcessSection (
     }
 
     if (Section->Type == SectionType) {
-      if (IS_SECTION2 (Section)) {
-        *OutputBuffer = (VOID *)((UINT8 *) Section + sizeof (EFI_COMMON_SECTION_HEADER2));
-      } else {
-        *OutputBuffer = (VOID *)((UINT8 *) Section + sizeof (EFI_COMMON_SECTION_HEADER));
-      }
+      *OutputBuffer = (VOID *)((UINT8 *)Section + FfsSectionHeaderSize (Section));
 
       return EFI_SUCCESS;
     }
 
     if ((Section->Type == EFI_SECTION_COMPRESSION) || (Section->Type == EFI_SECTION_GUID_DEFINED)) {
       if (Section->Type == EFI_SECTION_COMPRESSION) {
-        if (IS_SECTION2 (Section)) {
-          CompressionSection2 = (EFI_COMPRESSION_SECTION2 *) Section;
-          SectionLength       = SECTION2_SIZE (Section);
+        SectionLength = FfsSectionLength (Section);
 
-          if (CompressionSection2->CompressionType != EFI_STANDARD_COMPRESSION) {
-            return EFI_UNSUPPORTED;
-          }
-
-          CompressedData = (CHAR8 *) ((EFI_COMPRESSION_SECTION2 *) Section + 1);
-          CompressedDataLength = (UINT32) SectionLength - sizeof (EFI_COMPRESSION_SECTION2);
-        } else {
-          CompressionSection  = (EFI_COMPRESSION_SECTION *) Section;
-          SectionLength       = SECTION_SIZE (Section);
-
-          if (CompressionSection->CompressionType != EFI_STANDARD_COMPRESSION) {
-            return EFI_UNSUPPORTED;
-          }
-
-          CompressedData = (CHAR8 *) ((EFI_COMPRESSION_SECTION *) Section + 1);
-          CompressedDataLength = (UINT32) SectionLength - sizeof (EFI_COMPRESSION_SECTION);
+        if (FfsSectionCompressionType (Section) != EFI_STANDARD_COMPRESSION) {
+          return EFI_UNSUPPORTED;
         }
 
+        CompressedData = (VOID *)((UINTN)Section + FfsCompressionSectionHeaderSize (Section));
+        CompressedDataLength = SectionLength - FfsCompressionSectionHeaderSize (Section);
+
         Status = UefiDecompressGetInfo (
                    CompressedData,
                    CompressedDataLength,
@@ -383,19 +415,12 @@ FfsProcessSection (
       // DstBuffer still is one section. Adjust DstBuffer offset, skip EFI section header
       // to make section data at page alignment.
       //
-      if (IS_SECTION2 (Section))
-        DstBuffer = (UINT8 *)DstBuffer + EFI_PAGE_SIZE - sizeof (EFI_COMMON_SECTION_HEADER2);
-      else
-        DstBuffer = (UINT8 *)DstBuffer + EFI_PAGE_SIZE - sizeof (EFI_COMMON_SECTION_HEADER);
+      DstBuffer = (UINT8 *)DstBuffer + EFI_PAGE_SIZE - FfsSectionHeaderSize (Section);
       //
       // Call decompress function
       //
       if (Section->Type == EFI_SECTION_COMPRESSION) {
-        if (IS_SECTION2 (Section)) {
-          CompressedData = (CHAR8 *) ((EFI_COMPRESSION_SECTION2 *) Section + 1);
-        } else {
-          CompressedData = (CHAR8 *) ((EFI_COMPRESSION_SECTION *) Section + 1);
-        }
+        CompressedData = (VOID *)((UINTN)Section + FfsCompressionSectionHeaderSize (Section));
 
         Status = UefiDecompress (
                    CompressedData,
@@ -427,11 +452,7 @@ FfsProcessSection (
        }
     }
 
-    if (IS_SECTION2 (Section)) {
-      SectionLength = SECTION2_SIZE (Section);
-    } else {
-      SectionLength = SECTION_SIZE (Section);
-    }
+    SectionLength = FfsSectionLength (Section);
     //
     // SectionLength is adjusted it is 4 byte aligned.
     // Go to the next section
-- 
2.20.1


-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.

View/Reply Online (#61926): https://edk2.groups.io/g/devel/message/61926
Mute This Topic: https://groups.io/mt/75244270/1813853
Group Owner: devel+owner at edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub  [edk2-devel-archive at redhat.com]
-=-=-=-=-=-=-=-=-=-=-=-




More information about the edk2-devel-archive mailing list