From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id F1DDE81DC7 for ; Sun, 13 Nov 2016 19:23:06 -0800 (PST) Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga103.fm.intel.com with ESMTP; 13 Nov 2016 19:23:11 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.31,488,1473145200"; d="scan'208";a="1084766024" Received: from shwdeopenpsi068.ccr.corp.intel.com ([10.239.9.9]) by fmsmga002.fm.intel.com with ESMTP; 13 Nov 2016 19:23:09 -0800 From: Star Zeng To: edk2-devel@lists.01.org Cc: Star Zeng , Jiewen Yao , Liming Gao , Michael D Kinney Date: Mon, 14 Nov 2016 11:23:07 +0800 Message-Id: <1479093787-40688-1-git-send-email-star.zeng@intel.com> X-Mailer: git-send-email 2.7.0.windows.1 Subject: [PATCH] MdePkg PeiHobLib: Check FV alignment when building FV HOB X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 14 Nov 2016 03:23:07 -0000 Ref: https://bugzilla.tianocore.org/show_bug.cgi?id=205 Cc: Jiewen Yao Cc: Liming Gao Cc: Michael D Kinney Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Star Zeng --- MdePkg/Library/PeiHobLib/HobLib.c | 66 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 65 insertions(+), 1 deletion(-) diff --git a/MdePkg/Library/PeiHobLib/HobLib.c b/MdePkg/Library/PeiHobLib/HobLib.c index f3ce93afc63e..f2e44016e71c 100644 --- a/MdePkg/Library/PeiHobLib/HobLib.c +++ b/MdePkg/Library/PeiHobLib/HobLib.c @@ -1,7 +1,7 @@ /** @file Provide Hob Library functions for Pei phase. -Copyright (c) 2007 - 2014, Intel Corporation. All rights reserved.
+Copyright (c) 2007 - 2016, Intel Corporation. All rights reserved.
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 @@ -474,6 +474,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. @@ -495,6 +551,10 @@ BuildFvHob ( { EFI_HOB_FIRMWARE_VOLUME *Hob; + if (!InternalCheckFvAlignment (BaseAddress, Length)) { + return; + } + Hob = InternalPeiCreateHob (EFI_HOB_TYPE_FV, (UINT16) sizeof (EFI_HOB_FIRMWARE_VOLUME)); if (Hob == NULL) { return; @@ -530,6 +590,10 @@ BuildFv2Hob ( { EFI_HOB_FIRMWARE_VOLUME2 *Hob; + if (!InternalCheckFvAlignment (BaseAddress, Length)) { + return; + } + Hob = InternalPeiCreateHob (EFI_HOB_TYPE_FV2, (UINT16) sizeof (EFI_HOB_FIRMWARE_VOLUME2)); if (Hob == NULL) { return; -- 2.7.0.windows.1