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>,
	Jiewen Yao <jiewen.yao@intel.com>,
	Liming Gao <liming.gao@intel.com>,
	Michael D Kinney <michael.d.kinney@intel.com>,
	Jeff Fan <jeff.fan@intel.com>
Subject: [PATCH V2 2/3] IntelFrameworkPkg PeiHobLib: Check FV alignment when building FV HOB
Date: Wed, 16 Nov 2016 10:17:39 +0800	[thread overview]
Message-ID: <1479262660-54096-3-git-send-email-star.zeng@intel.com> (raw)
In-Reply-To: <1479262660-54096-1-git-send-email-star.zeng@intel.com>

If the FvImage buffer is not at its required alignment, then ASSERT().
Also update the function header description of BuildFv(2)Hob()
correspondingly.

Ref: https://bugzilla.tianocore.org/show_bug.cgi?id=205

Cc: Jiewen Yao <jiewen.yao@intel.com>
Cc: Liming Gao <liming.gao@intel.com>
Cc: Michael D Kinney <michael.d.kinney@intel.com>
Cc: Jeff Fan <jeff.fan@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Star Zeng <star.zeng@intel.com>
---
 .../Library/PeiHobLibFramework/HobLib.c            | 70 +++++++++++++++++++++-
 1 file changed, 69 insertions(+), 1 deletion(-)

diff --git a/IntelFrameworkPkg/Library/PeiHobLibFramework/HobLib.c b/IntelFrameworkPkg/Library/PeiHobLibFramework/HobLib.c
index b41536e2a15a..223e56a9db12 100644
--- a/IntelFrameworkPkg/Library/PeiHobLibFramework/HobLib.c
+++ b/IntelFrameworkPkg/Library/PeiHobLibFramework/HobLib.c
@@ -5,7 +5,7 @@
  This library instance uses EFI_HOB_TYPE_CV defined in Intel framework HOB specification v0.9
  to implement HobLib BuildCvHob() API. 
 
-Copyright (c) 2007 - 2014, Intel Corporation. All rights reserved.<BR>
+Copyright (c) 2007 - 2016, Intel Corporation. All rights reserved.<BR>
 This program and the accompanying materials
 are licensed and made available under the terms and conditions of the BSD License
 which accompanies this distribution.  The full text of the license may be found at
@@ -476,6 +476,62 @@ BuildGuidDataHob (
 }
 
 /**
+  Check FV alignment.
+
+  @param  BaseAddress   The base address of the Firmware Volume.
+  @param  Length        The size of the Firmware Volume in bytes.
+
+  @retval TRUE          FvImage buffer is at its required alignment.
+  @retval FALSE         FvImage buffer is not at its required alignment.
+
+**/
+BOOLEAN
+InternalCheckFvAlignment (
+  IN EFI_PHYSICAL_ADDRESS       BaseAddress,
+  IN UINT64                     Length
+  )
+{
+  EFI_FIRMWARE_VOLUME_HEADER    *FwVolHeader;
+  UINT32                        FvAlignment;
+
+  FvAlignment = 0;
+  FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) BaseAddress;
+
+  //
+  // If EFI_FVB2_WEAK_ALIGNMENT is set in the volume header then the first byte of the volume
+  // can be aligned on any power-of-two boundary. A weakly aligned volume can not be moved from
+  // its initial linked location and maintain its alignment.
+  //
+  if ((FwVolHeader->Attributes & EFI_FVB2_WEAK_ALIGNMENT) != EFI_FVB2_WEAK_ALIGNMENT) {
+    //
+    // Get FvHeader alignment
+    //
+    FvAlignment = 1 << ((FwVolHeader->Attributes & EFI_FVB2_ALIGNMENT) >> 16);
+    //
+    // FvAlignment must be greater than or equal to 8 bytes of the minimum FFS alignment value.
+    //
+    if (FvAlignment < 8) {
+      FvAlignment = 8;
+    }
+    if ((UINTN)BaseAddress % FvAlignment != 0) {
+      //
+      // FvImage buffer is not at its required alignment.
+      //
+      DEBUG ((
+        DEBUG_ERROR,
+        "Unaligned FvImage found at 0x%lx:0x%lx, the required alignment is 0x%x\n",
+        BaseAddress,
+        Length,
+        FvAlignment
+        ));
+      return FALSE;
+    }
+  }
+
+  return TRUE;
+}
+
+/**
   Builds a Firmware Volume HOB.
 
   This function builds a Firmware Volume HOB.
@@ -483,6 +539,7 @@ BuildGuidDataHob (
   for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
   
   If there is no additional space for HOB creation, then ASSERT().
+  If the FvImage buffer is not at its required alignment, then ASSERT().
 
   @param  BaseAddress   The base address of the Firmware Volume.
   @param  Length        The size of the Firmware Volume in bytes.
@@ -497,6 +554,11 @@ BuildFvHob (
 {
   EFI_HOB_FIRMWARE_VOLUME  *Hob;
 
+  if (!InternalCheckFvAlignment (BaseAddress, Length)) {
+    ASSERT (FALSE);
+    return;
+  }
+
   Hob = InternalPeiCreateHob (EFI_HOB_TYPE_FV, (UINT16) sizeof (EFI_HOB_FIRMWARE_VOLUME));
   if (Hob == NULL) {
     return;
@@ -514,6 +576,7 @@ BuildFvHob (
   for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
   
   If there is no additional space for HOB creation, then ASSERT().
+  If the FvImage buffer is not at its required alignment, then ASSERT().
 
   @param  BaseAddress   The base address of the Firmware Volume.
   @param  Length        The size of the Firmware Volume in bytes.
@@ -532,6 +595,11 @@ BuildFv2Hob (
 {
   EFI_HOB_FIRMWARE_VOLUME2  *Hob;
 
+  if (!InternalCheckFvAlignment (BaseAddress, Length)) {
+    ASSERT (FALSE);
+    return;
+  }
+
   Hob = InternalPeiCreateHob (EFI_HOB_TYPE_FV2, (UINT16) sizeof (EFI_HOB_FIRMWARE_VOLUME2));
   if (Hob == NULL) {
     return;
-- 
2.7.0.windows.1



  parent reply	other threads:[~2016-11-16  2:17 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-11-16  2:17 [PATCH V2 0/3] HobLib: Check FV alignment when building FV HOB Star Zeng
2016-11-16  2:17 ` [PATCH V2 1/3] MdePkg " Star Zeng
2016-11-16  2:17 ` Star Zeng [this message]
2016-11-16  2:17 ` [PATCH V2 3/3] ArmVirtPkg DxeHobLib: Update func header description of BuildFv(2)Hob() Star Zeng
2016-11-16 11:26   ` Ard Biesheuvel
2016-11-16  3:51 ` [PATCH V2 0/3] HobLib: Check FV alignment when building FV HOB Yao, Jiewen
2016-11-16  4:53 ` Gao, Liming
2016-11-16 17:04 ` Laszlo Ersek

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=1479262660-54096-3-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