The DstBuffer adjustment in FfsProcessSection() was added in the
BeagleBoard era (2ef2b01e07), when the decompressed section stream
placed the encapsulated section data directly after the 4/8 byte
EFI_COMMON_SECTION_HEADER. Bumping DstBuffer forward by
EFI_PAGE_SIZE - sizeof (EFI_COMMON_SECTION_HEADER) put the section
data back on a page boundary at the cost of one extra page.

For FV images this compensation is obsolete and harmful:

- GenFds aligns the FV_IMAGE section data inside the stream to the
  FvAlignment recorded in the inner FV header, by emitting a RAW pad
  section in front of the FV_IMAGE section. With a page aligned
  DstBuffer, the decompressed FV therefore already starts at a
  FvAlignment-aligned offset (e.g. DstBuffer + 0x10 for a typical
  FvAlignment = 16 build).

- The forward bump instead moves the FV to
  DstBuffer + EFI_PAGE_SIZE - sizeof (EFI_COMMON_SECTION_HEADER) + pad,
  which is 12 bytes off for FvAlignment = 16. FfsProcessFvFile() then
  takes its misalignment fallback and AllocateAlignedPages() a second
  buffer to CopyMem() the entire FV, doubling the temporary RAM
  footprint of the decompressed DXEFV.

On platforms with a modest SEC temporary RAM pool this is fatal: a
RISC-V platform with a 16 MiB pool and a ~9 MiB LZMA compressed DXEFV
fails to boot with EFI_OUT_OF_RESOURCES from DecompressFirstFv(),
because the redundant aligned copy no longer fits. The same FDF
pattern (FvAlignment = 16 DXEFV wrapped in the LZMA GUIDED section)
is used by OvmfPkg/RiscVVirt, which gets away with it only because
of its more generous memory layout, and pays a full extra copy of
the FV on every boot.

Remove the adjustment, and the extra page that was reserved for it,
so the decompressed FV keeps its natural, build-time guaranteed
alignment. Verified on the affected RISC-V platform: the FV now
lands 16-byte aligned, the fallback copy is not taken, and boot
succeeds in both RELEASE and DEBUG builds.

Cc: Leif Lindholm <[email protected]>
Cc: Ard Biesheuvel <[email protected]>
Cc: Abner Chang <[email protected]>
Signed-off-by: Ming Huang <[email protected]>
---
 EmbeddedPkg/Library/PrePiLib/FwVol.c | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)

diff --git a/EmbeddedPkg/Library/PrePiLib/FwVol.c 
b/EmbeddedPkg/Library/PrePiLib/FwVol.c
index 69c493187b..287c2b8062 100644
--- a/EmbeddedPkg/Library/PrePiLib/FwVol.c
+++ b/EmbeddedPkg/Library/PrePiLib/FwVol.c
@@ -449,19 +449,16 @@ FfsProcessSection (
       }
 
       //
-      // Allocate destination buffer, extra one page for adjustment
+      // Allocate destination buffer. The buffer is page aligned, and the
+      // decompressed section stream keeps the alignment of the embedded FV
+      // (GenFds pads the FV_IMAGE section data to the FvAlignment recorded in
+      // the inner FV header), so no further adjustment is needed here.
       //
-      DstBuffer = (VOID *)(UINTN)AllocatePages (EFI_SIZE_TO_PAGES 
(DstBufferSize) + 1);
+      DstBuffer = (VOID *)(UINTN)AllocatePages (EFI_SIZE_TO_PAGES 
(DstBufferSize));
       if (DstBuffer == NULL) {
         return EFI_OUT_OF_RESOURCES;
       }
 
-      //
-      // DstBuffer still is one section. Adjust DstBuffer offset, skip EFI 
section header
-      // to make section data at page alignment.
-      //
-      DstBuffer = (UINT8 *)DstBuffer + EFI_PAGE_SIZE - SectionHeaderSize;
-
       //
       // Call decompress function
       //
-- 
2.17.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#122202): https://edk2.groups.io/g/devel/message/122202
Mute This Topic: https://groups.io/mt/121897317/21656
Group Owner: [email protected]
Unsubscribe: https://edk2.groups.io/g/devel/unsub [[email protected]]
-=-=-=-=-=-=-=-=-=-=-=-


Reply via email to