From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: mx.groups.io; dkim=missing; spf=pass (domain: intel.com, ip: 134.134.136.126, mailfrom: dandan.bi@intel.com) Received: from mga18.intel.com (mga18.intel.com [134.134.136.126]) by groups.io with SMTP; Tue, 14 May 2019 19:20:17 -0700 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga106.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 14 May 2019 19:20:16 -0700 X-ExtLoop1: 1 Received: from shwdeopenpsi114.ccr.corp.intel.com ([10.239.157.147]) by orsmga008.jf.intel.com with ESMTP; 14 May 2019 19:20:15 -0700 From: "Dandan Bi" To: devel@edk2.groups.io Cc: Michael D Kinney , Liming Gao Subject: [patch v2 1/3] MdePkg: Merge TianoCustomDecompress algorithm into Date: Wed, 15 May 2019 10:19:58 +0800 Message-Id: <20190515022000.27644-2-dandan.bi@intel.com> X-Mailer: git-send-email 2.18.0.windows.1 In-Reply-To: <20190515022000.27644-1-dandan.bi@intel.com> References: <20190515022000.27644-1-dandan.bi@intel.com> REF: https://bugzilla.tianocore.org/show_bug.cgi?id=1722 We plan to merge the BaseUefiTianoCustomDecompressLib in MdeModulePkg into the BaseUefDecompressLib in MdePkg. In order to reduce the duplicated codes and maintain easily. This patch adds a new fdf file in BaseUefDecompressLib (BaseUefiTianoCustomDecompressLib.inf) to keep the same functionality and usage model with the one in MdeModulePkg, and then update consumer to use this new one one and remove the one in MdeModulePkg finally. Cc: Michael D Kinney Cc: Liming Gao Signed-off-by: Dandan Bi Reviewed-by: Liming Gao --- .../BaseUefiDecompressLib.c | 72 ++++-- .../BaseUefiDecompressLib.uni | 6 +- .../BaseUefiDecompressLibInternals.h | 44 +++- .../BaseUefiTianoCustomDecompressLib.c | 213 ++++++++++++++++++ .../BaseUefiTianoCustomDecompressLib.inf | 42 ++++ MdePkg/MdePkg.dec | 5 + MdePkg/MdePkg.dsc | 1 + 7 files changed, 366 insertions(+), 17 deletions(-) create mode 100644 MdePkg/Library/BaseUefiDecompressLib/BaseUefiTianoCustomDecompressLib.c create mode 100644 MdePkg/Library/BaseUefiDecompressLib/BaseUefiTianoCustomDecompressLib.inf diff --git a/MdePkg/Library/BaseUefiDecompressLib/BaseUefiDecompressLib.c b/MdePkg/Library/BaseUefiDecompressLib/BaseUefiDecompressLib.c index 8e502b0fdb..28b4bf9d8b 100644 --- a/MdePkg/Library/BaseUefiDecompressLib/BaseUefiDecompressLib.c +++ b/MdePkg/Library/BaseUefiDecompressLib/BaseUefiDecompressLib.c @@ -1,21 +1,14 @@ /** @file UEFI Decompress Library implementation refer to UEFI specification. - Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.
+ Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.
Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.
SPDX-License-Identifier: BSD-2-Clause-Patent **/ - -#include -#include -#include -#include -#include - #include "BaseUefiDecompressLibInternals.h" /** Read NumOfBit of bits from source into mBitBuf. @@ -732,29 +725,31 @@ UefiDecompressGetInfo ( then RETURN_INVALID_PARAMETER is returned. If Source is NULL, then ASSERT(). If Destination is NULL, then ASSERT(). If the required scratch buffer size > 0 and Scratch is NULL, then ASSERT(). + If the Version is not 1 or 2, then ASSERT(). @param Source The source buffer containing the compressed data. @param Destination The destination buffer to store the decompressed data. @param Scratch A temporary scratch buffer that is used to perform the decompression. This is an optional parameter that may be NULL if the required scratch buffer size is 0. + @param Version 1 for UEFI Decompress algoruthm, 2 for Tiano Decompess algorithm. @retval RETURN_SUCCESS Decompression completed successfully, and the uncompressed buffer is returned in Destination. @retval RETURN_INVALID_PARAMETER The source buffer specified by Source is corrupted (not in a valid compressed format). **/ RETURN_STATUS -EFIAPI -UefiDecompress ( +UefiTianoDecompress ( IN CONST VOID *Source, IN OUT VOID *Destination, - IN OUT VOID *Scratch OPTIONAL + IN OUT VOID *Scratch, + IN UINT32 Version ) { UINT32 CompSize; UINT32 OrigSize; SCRATCH_DATA *Sd; @@ -762,10 +757,11 @@ UefiDecompress ( UINT8 *Dst; ASSERT (Source != NULL); ASSERT (Destination != NULL); ASSERT (Scratch != NULL); + ASSERT (Version == 1 || Version == 2); Src = Source; Dst = Destination; Sd = (SCRATCH_DATA *) Scratch; @@ -784,12 +780,22 @@ UefiDecompress ( SetMem (Sd, sizeof (SCRATCH_DATA), 0); // // The length of the field 'Position Set Code Length Array Size' in Block Header. // For UEFI 2.0 de/compression algorithm(Version 1), mPBit = 4 - // - Sd->mPBit = 4; + // For Tiano de/compression algorithm(Version 2), mPBit = 5 + // + switch (Version) { + case 1 : + Sd->mPBit = 4; + break; + case 2 : + Sd->mPBit = 5; + break; + default: + ASSERT (FALSE); + } Sd->mSrcBase = (UINT8 *)Src; Sd->mDstBase = Dst; // // CompSize and OrigSize are calculated in bytes // @@ -813,5 +819,45 @@ UefiDecompress ( return RETURN_INVALID_PARAMETER; } return RETURN_SUCCESS; } + +/** + Decompresses a UEFI compressed source buffer. + + Extracts decompressed data to its original form. + This function is designed so that the decompression algorithm can be implemented + without using any memory services. As a result, this function is not allowed to + call any memory allocation services in its implementation. It is the caller's + responsibility to allocate and free the Destination and Scratch buffers. + If the compressed source data specified by Source is successfully decompressed + into Destination, then RETURN_SUCCESS is returned. If the compressed source data + specified by Source is not in a valid compressed data format, + then RETURN_INVALID_PARAMETER is returned. + + If Source is NULL, then ASSERT(). + If Destination is NULL, then ASSERT(). + If the required scratch buffer size > 0 and Scratch is NULL, then ASSERT(). + + @param Source The source buffer containing the compressed data. + @param Destination The destination buffer to store the decompressed data + @param Scratch A temporary scratch buffer that is used to perform the decompression. + This is an optional parameter that may be NULL if the + required scratch buffer size is 0. + + @retval RETURN_SUCCESS Decompression completed successfully, and + the uncompressed buffer is returned in Destination. + @retval RETURN_INVALID_PARAMETER + The source buffer specified by Source is corrupted + (not in a valid compressed format). +**/ +RETURN_STATUS +EFIAPI +UefiDecompress ( + IN CONST VOID *Source, + IN OUT VOID *Destination, + IN OUT VOID *Scratch OPTIONAL + ) +{ + return UefiTianoDecompress (Source, Destination, Scratch, 1); +} diff --git a/MdePkg/Library/BaseUefiDecompressLib/BaseUefiDecompressLib.uni b/MdePkg/Library/BaseUefiDecompressLib/BaseUefiDecompressLib.uni index 1cec6bd965..25f379d047 100644 --- a/MdePkg/Library/BaseUefiDecompressLib/BaseUefiDecompressLib.uni +++ b/MdePkg/Library/BaseUefiDecompressLib/BaseUefiDecompressLib.uni @@ -1,16 +1,16 @@ // /** @file // UEFI Decompress Library implementation. // // UEFI Decompress Library implementation. // -// Copyright (c) 2007 - 2014, Intel Corporation. All rights reserved.
+// Copyright (c) 2007 - 2019, Intel Corporation. All rights reserved.
// // SPDX-License-Identifier: BSD-2-Clause-Patent // // **/ -#string STR_MODULE_ABSTRACT #language en-US "UEFI Decompress Library implementation" +#string STR_MODULE_ABSTRACT #language en-US "UEFI Decompress Library and Tiano Custom Decompress Library implementation." -#string STR_MODULE_DESCRIPTION #language en-US "UEFI Decompress Library implementation." +#string STR_MODULE_DESCRIPTION #language en-US "Tiano custom decompression algorithm shares most of the code with the UEFI Decompress algorithm." diff --git a/MdePkg/Library/BaseUefiDecompressLib/BaseUefiDecompressLibInternals.h b/MdePkg/Library/BaseUefiDecompressLib/BaseUefiDecompressLibInternals.h index 9821c19c7b..0bfb503337 100644 --- a/MdePkg/Library/BaseUefiDecompressLib/BaseUefiDecompressLibInternals.h +++ b/MdePkg/Library/BaseUefiDecompressLib/BaseUefiDecompressLibInternals.h @@ -1,16 +1,21 @@ /** @file Internal data structure defintions for Base UEFI Decompress Library. - Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.
+ Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.
SPDX-License-Identifier: BSD-2-Clause-Patent **/ #ifndef __BASE_UEFI_DECOMPRESS_LIB_INTERNALS_H__ #define __BASE_UEFI_DECOMPRESS_LIB_INTERNALS_H__ +#include +#include +#include +#include +#include // // Decompression algorithm begins here // #define BITBUFSIZ 32 #define MAXMATCH 256 @@ -56,10 +61,11 @@ typedef struct { UINT16 mPTTable[256]; /// /// The length of the field 'Position Set Code Length Array Size' in Block Header. /// For UEFI 2.0 de/compression algorithm, mPBit = 4. + /// For Tiano de/compression algorithm, mPBit = 5. /// UINT8 mPBit; } SCRATCH_DATA; /** @@ -200,6 +206,42 @@ DecodeC ( VOID Decode ( SCRATCH_DATA *Sd ); +/** + Decompresses a compressed source buffer. + + Extracts decompressed data to its original form. + This function is designed so that the decompression algorithm can be implemented + without using any memory services. As a result, this function is not allowed to + call any memory allocation services in its implementation. It is the caller's + responsibility to allocate and free the Destination and Scratch buffers. + If the compressed source data specified by Source is successfully decompressed + into Destination, then RETURN_SUCCESS is returned. If the compressed source data + specified by Source is not in a valid compressed data format, + then RETURN_INVALID_PARAMETER is returned. + + If Source is NULL, then ASSERT(). + If Destination is NULL, then ASSERT(). + If the required scratch buffer size > 0 and Scratch is NULL, then ASSERT(). + + @param Source The source buffer containing the compressed data. + @param Destination The destination buffer to store the decompressed data. + @param Scratch A temporary scratch buffer that is used to perform the decompression. + This is an optional parameter that may be NULL if the + required scratch buffer size is 0. + + @retval RETURN_SUCCESS Decompression completed successfully, and + the uncompressed buffer is returned in Destination. + @retval RETURN_INVALID_PARAMETER + The source buffer specified by Source is corrupted + (not in a valid compressed format). +**/ +RETURN_STATUS +UefiTianoDecompress ( + IN CONST VOID *Source, + IN OUT VOID *Destination, + IN OUT VOID *Scratch, + IN UINT32 Version + ); #endif diff --git a/MdePkg/Library/BaseUefiDecompressLib/BaseUefiTianoCustomDecompressLib.c b/MdePkg/Library/BaseUefiDecompressLib/BaseUefiTianoCustomDecompressLib.c new file mode 100644 index 0000000000..5a73933d2a --- /dev/null +++ b/MdePkg/Library/BaseUefiDecompressLib/BaseUefiTianoCustomDecompressLib.c @@ -0,0 +1,213 @@ +/** @file + UEFI Decompress Library implementation refer to UEFI specification. + + Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.
+ Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.
+ SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include +#include +#include "BaseUefiDecompressLibInternals.h" + +/** + Examines a GUIDed section and returns the size of the decoded buffer and the + size of an optional scratch buffer required to actually decode the data in a GUIDed section. + + Examines a GUIDed section specified by InputSection. + If GUID for InputSection does not match the GUID that this handler supports, + then RETURN_UNSUPPORTED is returned. + If the required information can not be retrieved from InputSection, + then RETURN_INVALID_PARAMETER is returned. + If the GUID of InputSection does match the GUID that this handler supports, + then the size required to hold the decoded buffer is returned in OututBufferSize, + the size of an optional scratch buffer is returned in ScratchSize, and the Attributes field + from EFI_GUID_DEFINED_SECTION header of InputSection is returned in SectionAttribute. + + If InputSection is NULL, then ASSERT(). + If OutputBufferSize is NULL, then ASSERT(). + If ScratchBufferSize is NULL, then ASSERT(). + If SectionAttribute is NULL, then ASSERT(). + + + @param[in] InputSection A pointer to a GUIDed section of an FFS formatted file. + @param[out] OutputBufferSize A pointer to the size, in bytes, of an output buffer required + if the buffer specified by InputSection were decoded. + @param[out] ScratchBufferSize A pointer to the size, in bytes, required as scratch space + if the buffer specified by InputSection were decoded. + @param[out] SectionAttribute A pointer to the attributes of the GUIDed section. See the Attributes + field of EFI_GUID_DEFINED_SECTION in the PI Specification. + + @retval RETURN_SUCCESS The information about InputSection was returned. + @retval RETURN_UNSUPPORTED The section specified by InputSection does not match the GUID this handler supports. + @retval RETURN_INVALID_PARAMETER The information can not be retrieved from the section specified by InputSection. + +**/ +RETURN_STATUS +EFIAPI +TianoDecompressGetInfo ( + IN CONST VOID *InputSection, + OUT UINT32 *OutputBufferSize, + OUT UINT32 *ScratchBufferSize, + OUT UINT16 *SectionAttribute + ) + +{ + ASSERT (SectionAttribute != NULL); + + if (InputSection == NULL) { + return RETURN_INVALID_PARAMETER; + } + + if (IS_SECTION2 (InputSection)) { + if (!CompareGuid ( + &gTianoCustomDecompressGuid, + &(((EFI_GUID_DEFINED_SECTION2 *) InputSection)->SectionDefinitionGuid))) { + return RETURN_INVALID_PARAMETER; + } + // + // Get guid attribute of guid section. + // + *SectionAttribute = ((EFI_GUID_DEFINED_SECTION2 *) InputSection)->Attributes; + + // + // Call Tiano GetInfo to get the required size info. + // + return UefiDecompressGetInfo ( + (UINT8 *) InputSection + ((EFI_GUID_DEFINED_SECTION2 *) InputSection)->DataOffset, + SECTION2_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION2 *) InputSection)->DataOffset, + OutputBufferSize, + ScratchBufferSize + ); + } else { + if (!CompareGuid ( + &gTianoCustomDecompressGuid, + &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) { + return RETURN_INVALID_PARAMETER; + } + // + // Get guid attribute of guid section. + // + *SectionAttribute = ((EFI_GUID_DEFINED_SECTION *) InputSection)->Attributes; + + // + // Call Tiano GetInfo to get the required size info. + // + return UefiDecompressGetInfo ( + (UINT8 *) InputSection + ((EFI_GUID_DEFINED_SECTION *) InputSection)->DataOffset, + SECTION_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION *) InputSection)->DataOffset, + OutputBufferSize, + ScratchBufferSize + ); + } +} + +/** + Decompress a Tiano compressed GUIDed section into a caller allocated output buffer. + + Decodes the GUIDed section specified by InputSection. + If GUID for InputSection does not match the GUID that this handler supports, then RETURN_UNSUPPORTED is returned. + If the data in InputSection can not be decoded, then RETURN_INVALID_PARAMETER is returned. + If the GUID of InputSection does match the GUID that this handler supports, then InputSection + is decoded into the buffer specified by OutputBuffer and the authentication status of this + decode operation is returned in AuthenticationStatus. If the decoded buffer is identical to the + data in InputSection, then OutputBuffer is set to point at the data in InputSection. Otherwise, + the decoded data will be placed in caller allocated buffer specified by OutputBuffer. + + If InputSection is NULL, then ASSERT(). + If OutputBuffer is NULL, then ASSERT(). + If ScratchBuffer is NULL and this decode operation requires a scratch buffer, then ASSERT(). + If AuthenticationStatus is NULL, then ASSERT(). + + + @param[in] InputSection A pointer to a GUIDed section of an FFS formatted file. + @param[out] OutputBuffer A pointer to a buffer that contains the result of a decode operation. + @param[in] ScratchBuffer A caller allocated buffer that may be required by this function + as a scratch buffer to perform the decode operation. + @param[out] AuthenticationStatus + A pointer to the authentication status of the decoded output buffer. + See the definition of authentication status in the EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI + section of the PI Specification. EFI_AUTH_STATUS_PLATFORM_OVERRIDE must + never be set by this handler. + + @retval RETURN_SUCCESS The buffer specified by InputSection was decoded. + @retval RETURN_UNSUPPORTED The section specified by InputSection does not match the GUID this handler supports. + @retval RETURN_INVALID_PARAMETER The section specified by InputSection can not be decoded. + +**/ +RETURN_STATUS +EFIAPI +TianoDecompress ( + IN CONST VOID *InputSection, + OUT VOID **OutputBuffer, + IN VOID *ScratchBuffer, OPTIONAL + OUT UINT32 *AuthenticationStatus + ) +{ + ASSERT (OutputBuffer != NULL); + ASSERT (InputSection != NULL); + + if (IS_SECTION2 (InputSection)) { + if (!CompareGuid ( + &gTianoCustomDecompressGuid, + &(((EFI_GUID_DEFINED_SECTION2 *) InputSection)->SectionDefinitionGuid))) { + return RETURN_INVALID_PARAMETER; + } + + // + // Set Authentication to Zero. + // + *AuthenticationStatus = 0; + + // + // Call Tiano Decompress to get the raw data + // + return UefiTianoDecompress ( + (UINT8 *) InputSection + ((EFI_GUID_DEFINED_SECTION2 *) InputSection)->DataOffset, + *OutputBuffer, + ScratchBuffer, + 2 + ); + } else { + if (!CompareGuid ( + &gTianoCustomDecompressGuid, + &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) { + return RETURN_INVALID_PARAMETER; + } + + // + // Set Authentication to Zero. + // + *AuthenticationStatus = 0; + + // + // Call Tiano Decompress to get the raw data + // + return UefiTianoDecompress ( + (UINT8 *) InputSection + ((EFI_GUID_DEFINED_SECTION *) InputSection)->DataOffset, + *OutputBuffer, + ScratchBuffer, + 2 + ); + } +} + +/** + Registers TianoDecompress and TianoDecompressGetInfo handlers with TianoCustomerDecompressGuid + + @retval RETURN_SUCCESS Register successfully. + @retval RETURN_OUT_OF_RESOURCES No enough memory to store this handler. +**/ +RETURN_STATUS +EFIAPI +TianoDecompressLibConstructor ( + VOID +) +{ + return ExtractGuidedSectionRegisterHandlers ( + &gTianoCustomDecompressGuid, + TianoDecompressGetInfo, + TianoDecompress + ); +} diff --git a/MdePkg/Library/BaseUefiDecompressLib/BaseUefiTianoCustomDecompressLib.inf b/MdePkg/Library/BaseUefiDecompressLib/BaseUefiTianoCustomDecompressLib.inf new file mode 100644 index 0000000000..280a5f307d --- /dev/null +++ b/MdePkg/Library/BaseUefiDecompressLib/BaseUefiTianoCustomDecompressLib.inf @@ -0,0 +1,42 @@ +## @file +# This library instance produces UefiDecompressLib and Tiano Custom decompression algorithm. +# Tiano custom decompression algorithm shares most of code with Uefi Decompress algorithm. +# +# Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.
+# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + INF_VERSION = 0x00010005 + BASE_NAME = BaseUefiTianoCustomDecompressLib + MODULE_UNI_FILE = BaseUefiDecompressLib.uni + FILE_GUID = d774c4d9-c121-4da3-a5e2-0f317e3c630c + MODULE_TYPE = BASE + VERSION_STRING = 1.0 + LIBRARY_CLASS = UefiDecompressLib + CONSTRUCTOR = TianoDecompressLibConstructor + +# +# The following information is for reference only and not required by the build tools. +# +# VALID_ARCHITECTURES = IA32 X64 EBC +# + +[Sources] + BaseUefiDecompressLibInternals.h + BaseUefiDecompressLib.c + BaseUefiTianoCustomDecompressLib.c + +[Packages] + MdePkg/MdePkg.dec + +[LibraryClasses] + BaseLib + DebugLib + BaseMemoryLib + ExtractGuidedSectionLib + +[Guids] + gTianoCustomDecompressGuid ## PRODUCES ## UNDEFINED # specifies tiano custom decompress algorithm. diff --git a/MdePkg/MdePkg.dec b/MdePkg/MdePkg.dec index 28d4a966c2..6c563375ee 100644 --- a/MdePkg/MdePkg.dec +++ b/MdePkg/MdePkg.dec @@ -759,10 +759,15 @@ [Guids] # GUID defined in Windows UEFI Firmware Update Platform doc # ## Include/IndustryStandard/WindowsUxCapsule.h gWindowsUxCapsuleGuid = { 0x3b8c8162, 0x188c, 0x46a4, { 0xae, 0xc9, 0xbe, 0x43, 0xf1, 0xd6, 0x56, 0x97}} + # + # GUID indicates the tiano custom compress/decompress algorithm. + # + gTianoCustomDecompressGuid = { 0xA31280AD, 0x481E, 0x41B6, { 0x95, 0xE8, 0x12, 0x7F, 0x4C, 0x98, 0x47, 0x79 }} + [Guids.IA32, Guids.X64] ## Include/Guid/Cper.h gEfiIa32X64ErrorTypeCacheCheckGuid = { 0xA55701F5, 0xE3EF, 0x43de, { 0xAC, 0x72, 0x24, 0x9B, 0x57, 0x3F, 0xAD, 0x2C }} ## Include/Guid/Cper.h diff --git a/MdePkg/MdePkg.dsc b/MdePkg/MdePkg.dsc index 21743e384c..a2ff12fe75 100644 --- a/MdePkg/MdePkg.dsc +++ b/MdePkg/MdePkg.dsc @@ -55,10 +55,11 @@ [Components] MdePkg/Library/BaseReportStatusCodeLibNull/BaseReportStatusCodeLibNull.inf MdePkg/Library/BaseSerialPortLibNull/BaseSerialPortLibNull.inf MdePkg/Library/BaseSynchronizationLib/BaseSynchronizationLib.inf MdePkg/Library/BaseTimerLibNullTemplate/BaseTimerLibNullTemplate.inf MdePkg/Library/BaseUefiDecompressLib/BaseUefiDecompressLib.inf + MdePkg/Library/BaseUefiDecompressLib/BaseUefiTianoCustomDecompressLib.inf MdePkg/Library/BaseSmbusLibNull/BaseSmbusLibNull.inf MdePkg/Library/BaseSafeIntLib/BaseSafeIntLib.inf MdePkg/Library/DxeCoreEntryPoint/DxeCoreEntryPoint.inf MdePkg/Library/DxeCoreHobLib/DxeCoreHobLib.inf -- 2.18.0.windows.1