public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: Star Zeng <star.zeng@intel.com>
To: edk2-devel@lists.01.org
Cc: Star Zeng <star.zeng@intel.com>, Liming Gao <liming.gao@intel.com>
Subject: [PATCH 4/4] MdeModulePkg DxeCore: Support USED_SIZE FV_EXT_TYPE
Date: Tue, 28 Nov 2017 17:52:27 +0800	[thread overview]
Message-ID: <1511862747-10220-5-git-send-email-star.zeng@intel.com> (raw)
In-Reply-To: <1511862747-10220-1-git-send-email-star.zeng@intel.com>

The USED_SIZE FV_EXT_TYPE is introduced by PI 1.6 spec.

The EFI_FIRMWARE_VOLUME_EXT_ENTRY_USED_SIZE_TYPE can be used to find
out how many EFI_FVB2_ERASE_POLARITY bytes are at the end of the FV.

When the FV gets shadowed into memory you only need to copy the used
bytes into memory and fill the rest of the memory buffer with the
erase value.

Cc: Liming Gao <liming.gao@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Star Zeng <star.zeng@intel.com>
---
 MdeModulePkg/Core/Dxe/Dispatcher/Dispatcher.c | 85 ++++++++++++++++++++++++++-
 1 file changed, 84 insertions(+), 1 deletion(-)

diff --git a/MdeModulePkg/Core/Dxe/Dispatcher/Dispatcher.c b/MdeModulePkg/Core/Dxe/Dispatcher/Dispatcher.c
index c7b9224c0e33..1a013ba6ffed 100644
--- a/MdeModulePkg/Core/Dxe/Dispatcher/Dispatcher.c
+++ b/MdeModulePkg/Core/Dxe/Dispatcher/Dispatcher.c
@@ -938,7 +938,68 @@ FvFoundInHobFv2 (
   return FALSE;
 }
 
+/**
+  Find USED_SIZE FV_EXT_TYPE entry in FV extension header and get the FV used size.
+
+  @param[in]  FvHeader      Pointer to FV header.
+  @param[out] FvUsedSize    Pointer to FV used size returned,
+                            only valid if USED_SIZE FV_EXT_TYPE entry is found.
+  @param[out] EraseByte     Pointer to erase byte returned,
+                            only valid if USED_SIZE FV_EXT_TYPE entry is found.
 
+  @retval TRUE              USED_SIZE FV_EXT_TYPE entry is found,
+                            FV used size and erase byte are returned.
+  @retval FALSE             No USED_SIZE FV_EXT_TYPE entry found.
+
+**/
+BOOLEAN
+GetFvUsedSize (
+  IN EFI_FIRMWARE_VOLUME_HEADER     *FvHeader,
+  OUT UINT32                        *FvUsedSize,
+  OUT UINT8                         *EraseByte
+  )
+{
+  UINT16                                        ExtHeaderOffset;
+  EFI_FIRMWARE_VOLUME_EXT_HEADER                *ExtHeader;
+  EFI_FIRMWARE_VOLUME_EXT_ENTRY                 *ExtEntryList;
+  UINTN                                         ExtEntryListSize;
+  EFI_FIRMWARE_VOLUME_EXT_ENTRY_USED_SIZE_TYPE  *ExtEntryUsedSize;
+
+  ExtHeaderOffset = ReadUnaligned16 (&FvHeader->ExtHeaderOffset);
+  if (ExtHeaderOffset != 0) {
+    ExtHeader = (EFI_FIRMWARE_VOLUME_EXT_HEADER *) ((UINT8 *) FvHeader + ExtHeaderOffset);
+    ExtEntryList = (EFI_FIRMWARE_VOLUME_EXT_ENTRY *) (ExtHeader + 1);
+    ExtEntryListSize = ReadUnaligned32 (&ExtHeader->ExtHeaderSize) - sizeof (EFI_FIRMWARE_VOLUME_EXT_HEADER);
+    while (ExtEntryListSize >= sizeof (EFI_FIRMWARE_VOLUME_EXT_ENTRY)) {
+      if (ReadUnaligned16 (&ExtEntryList->ExtEntryType) == EFI_FV_EXT_TYPE_USED_SIZE_TYPE) {
+        //
+        // USED_SIZE FV_EXT_TYPE entry is found.
+        //
+        ExtEntryUsedSize = (EFI_FIRMWARE_VOLUME_EXT_ENTRY_USED_SIZE_TYPE *) ExtEntryList;
+        *FvUsedSize = ReadUnaligned32 (&ExtEntryUsedSize->UsedSize);
+        if ((ReadUnaligned32 (&FvHeader->Attributes) & EFI_FVB2_ERASE_POLARITY) != 0) {
+          *EraseByte = 0xFF;
+        } else {
+          *EraseByte = 0;
+        }
+        DEBUG ((
+          DEBUG_INFO,
+          "FV at 0x%x has 0x%x used size, and erase byte is 0x%02x\n",
+          FvHeader,
+          *FvUsedSize,
+          *EraseByte
+          ));
+        return TRUE;
+      }
+      ExtEntryListSize -= ReadUnaligned16 (&ExtEntryList->ExtEntrySize);
+    }
+  }
+
+  //
+  // No USED_SIZE FV_EXT_TYPE entry found.
+  //
+  return FALSE;
+}
 
 /**
   Get the driver from the FV through driver name, and produce a FVB protocol on FvHandle.
@@ -968,6 +1029,8 @@ CoreProcessFvImageFile (
   EFI_FIRMWARE_VOLUME_HEADER          *FvHeader;
   UINT32                              FvAlignment;
   EFI_DEVICE_PATH_PROTOCOL            *FvFileDevicePath;
+  UINT32                              FvUsedSize;
+  UINT8                               EraseByte;
 
   //
   // Read the first (and only the first) firmware volume section
@@ -1035,6 +1098,14 @@ CoreProcessFvImageFile (
         FvAlignment = 8;
       }
 
+      DEBUG ((
+        DEBUG_INFO,
+        "%a() FV at 0x%x, FvAlignment required is 0x%x\n",
+        __FUNCTION__,
+        FvHeader,
+        FvAlignment
+        ));
+
       //
       // Check FvImage alignment.
       //
@@ -1050,7 +1121,19 @@ CoreProcessFvImageFile (
           //
           // Move FvImage into the aligned buffer and release the original buffer.
           //
-          CopyMem (AlignedBuffer, Buffer, BufferSize);
+          if (GetFvUsedSize (FvHeader, &FvUsedSize, &EraseByte)) {
+            //
+            // Copy the used bytes and fill the rest with the erase value.
+            //
+            CopyMem (AlignedBuffer, FvHeader, (UINTN) FvUsedSize);
+            SetMem (
+              (UINT8 *) AlignedBuffer + FvUsedSize,
+              (UINTN) (BufferSize - FvUsedSize),
+              EraseByte
+              );
+          } else {
+            CopyMem (AlignedBuffer, Buffer, BufferSize);
+          }
           FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *) AlignedBuffer;
           CoreFreePool (Buffer);
           Buffer = NULL;
-- 
2.7.0.windows.1



      parent reply	other threads:[~2017-11-28  9:48 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-11-28  9:52 [PATCH 0/4] Add USED_SIZE FV_EXT_TYPE support Star Zeng
2017-11-28  9:52 ` [PATCH 1/4] MdePkg PiFirmwareVolume.h: Add USED_SIZE FV_EXT_TYPE definitions Star Zeng
2017-11-28  9:52 ` [PATCH 2/4] MdeModulePkg PeiCore: Support USED_SIZE FV_EXT_TYPE Star Zeng
2017-11-28  9:52 ` [PATCH 3/4] MdeModulePkg DxeCore: Check FvImage alignment Star Zeng
2017-11-28  9:52 ` Star Zeng [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1511862747-10220-5-git-send-email-star.zeng@intel.com \
    --to=devel@edk2.groups.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox