From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=134.134.136.20; helo=mga02.intel.com; envelope-from=star.zeng@intel.com; receiver=edk2-devel@lists.01.org Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) (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 1BB77203564C3 for ; Tue, 28 Nov 2017 01:48:11 -0800 (PST) Received: from orsmga005.jf.intel.com ([10.7.209.41]) by orsmga101.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 28 Nov 2017 01:52:34 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.44,467,1505804400"; d="scan'208";a="178643050" Received: from shwdeopenpsi068.ccr.corp.intel.com ([10.239.158.46]) by orsmga005.jf.intel.com with ESMTP; 28 Nov 2017 01:52:33 -0800 From: Star Zeng To: edk2-devel@lists.01.org Cc: Star Zeng , Liming Gao Date: Tue, 28 Nov 2017 17:52:26 +0800 Message-Id: <1511862747-10220-4-git-send-email-star.zeng@intel.com> X-Mailer: git-send-email 2.7.0.windows.1 In-Reply-To: <1511862747-10220-1-git-send-email-star.zeng@intel.com> References: <1511862747-10220-1-git-send-email-star.zeng@intel.com> Subject: [PATCH 3/4] MdeModulePkg DxeCore: Check FvImage alignment X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 28 Nov 2017 09:48:11 -0000 No need to allocate aligned buffer if FvImage has been at required alignment. Then the code logic will be aligned with ProcessFvFile() in MdeModulePkg/Core/Pei/FwVol/FwVol.c. Cc: Liming Gao Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Star Zeng --- MdeModulePkg/Core/Dxe/Dispatcher/Dispatcher.c | 32 ++++++++++++++++----------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/MdeModulePkg/Core/Dxe/Dispatcher/Dispatcher.c b/MdeModulePkg/Core/Dxe/Dispatcher/Dispatcher.c index 5eee71bb2cd3..c7b9224c0e33 100644 --- a/MdeModulePkg/Core/Dxe/Dispatcher/Dispatcher.c +++ b/MdeModulePkg/Core/Dxe/Dispatcher/Dispatcher.c @@ -1023,32 +1023,38 @@ CoreProcessFvImageFile ( // 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 ((FvHeader->Attributes & EFI_FVB2_WEAK_ALIGNMENT) != EFI_FVB2_WEAK_ALIGNMENT) { + if ((ReadUnaligned32 (&FvHeader->Attributes) & EFI_FVB2_WEAK_ALIGNMENT) != EFI_FVB2_WEAK_ALIGNMENT) { // // Get FvHeader alignment // - FvAlignment = 1 << ((FvHeader->Attributes & EFI_FVB2_ALIGNMENT) >> 16); + FvAlignment = 1 << ((ReadUnaligned32 (&FvHeader->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; } + // - // Allocate the aligned buffer for the FvImage. + // Check FvImage alignment. // - AlignedBuffer = AllocateAlignedPages (EFI_SIZE_TO_PAGES (BufferSize), (UINTN) FvAlignment); - if (AlignedBuffer == NULL) { - FreePool (Buffer); - return EFI_OUT_OF_RESOURCES; - } else { + if ((UINTN) FvHeader % FvAlignment != 0) { // - // Move FvImage into the aligned buffer and release the original buffer. + // Allocate the aligned buffer for the FvImage. // - CopyMem (AlignedBuffer, Buffer, BufferSize); - FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *) AlignedBuffer; - CoreFreePool (Buffer); - Buffer = NULL; + AlignedBuffer = AllocateAlignedPages (EFI_SIZE_TO_PAGES (BufferSize), (UINTN) FvAlignment); + if (AlignedBuffer == NULL) { + FreePool (Buffer); + return EFI_OUT_OF_RESOURCES; + } else { + // + // Move FvImage into the aligned buffer and release the original buffer. + // + CopyMem (AlignedBuffer, Buffer, BufferSize); + FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *) AlignedBuffer; + CoreFreePool (Buffer); + Buffer = NULL; + } } } // -- 2.7.0.windows.1