From 090d308851492a7b647fab6451fa1a1bb763d6d5 Mon Sep 17 00:00:00 2001 From: jljusten Date: Mon, 4 Jan 2010 16:17:47 +0000 Subject: [PATCH] IntelFrameworkModulePkg LZMA: Support running LZMA from flash/rom Previously the code relied upon global variables which could not be modified if the code was running from ROM (or similarly a flash memory which is not easily modified). git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@9667 6f19259b-4bc3-4df7-8a09-765794883524 --- .../GuidedSectionExtraction.c | 3 +- .../LzmaCustomDecompressLib/LzmaDecompress.c | 53 ++++++++----------- .../LzmaDecompressLibInternal.h | 1 + 3 files changed, 25 insertions(+), 32 deletions(-) diff --git a/IntelFrameworkModulePkg/Library/LzmaCustomDecompressLib/GuidedSectionExtraction.c b/IntelFrameworkModulePkg/Library/LzmaCustomDecompressLib/GuidedSectionExtraction.c index ff91600afd..37dbca0ef0 100644 --- a/IntelFrameworkModulePkg/Library/LzmaCustomDecompressLib/GuidedSectionExtraction.c +++ b/IntelFrameworkModulePkg/Library/LzmaCustomDecompressLib/GuidedSectionExtraction.c @@ -73,7 +73,7 @@ LzmaGuidedSectionGetInfo ( return LzmaUefiDecompressGetInfo ( (UINT8 *) InputSection + ((EFI_GUID_DEFINED_SECTION *) InputSection)->DataOffset, - (*(UINT32 *) (((EFI_COMMON_SECTION_HEADER *) InputSection)->Size) & 0x00ffffff) - ((EFI_GUID_DEFINED_SECTION *) InputSection)->DataOffset, + SECTION_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION *) InputSection)->DataOffset, OutputBufferSize, ScratchBufferSize ); @@ -137,6 +137,7 @@ LzmaGuidedSectionExtraction ( return LzmaUefiDecompress ( (UINT8 *) InputSection + ((EFI_GUID_DEFINED_SECTION *) InputSection)->DataOffset, + SECTION_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION *) InputSection)->DataOffset, *OutputBuffer, ScratchBuffer ); diff --git a/IntelFrameworkModulePkg/Library/LzmaCustomDecompressLib/LzmaDecompress.c b/IntelFrameworkModulePkg/Library/LzmaCustomDecompressLib/LzmaDecompress.c index b8523f4b4f..dd08008258 100644 --- a/IntelFrameworkModulePkg/Library/LzmaCustomDecompressLib/LzmaDecompress.c +++ b/IntelFrameworkModulePkg/Library/LzmaCustomDecompressLib/LzmaDecompress.c @@ -17,18 +17,15 @@ #include "Sdk/C/7zVersion.h" #include "Sdk/C/LzmaDec.h" -// -// Global data -// - -CONST VOID *mSourceLastUsedWithGetInfo; -UINT32 mSizeOfLastSource; -UINT32 mDecompressedSizeForLastSource; -VOID *mScratchBuffer; -UINTN mScratchBufferSize; - #define SCRATCH_BUFFER_REQUEST_SIZE SIZE_64KB +typedef struct +{ + ISzAlloc Functions; + VOID *Buffer; + UINTN BufferSize; +} ISzAllocWithData; + /** Allocation routine used by LZMA decompression. @@ -44,11 +41,12 @@ SzAlloc ( ) { VOID *Addr; + ISzAllocWithData *Private = (ISzAllocWithData*) P; - if (mScratchBufferSize >= Size) { - Addr = mScratchBuffer; - mScratchBuffer = (VOID*) ((UINT8*)Addr + Size); - mScratchBufferSize -= Size; + if (Private->BufferSize >= Size) { + Addr = Private->Buffer; + Private->Buffer = (VOID*) ((UINT8*)Addr + Size); + Private->BufferSize -= Size; return Addr; } else { ASSERT (FALSE); @@ -75,8 +73,6 @@ SzFree ( // } -STATIC ISzAlloc g_Alloc = { SzAlloc, SzFree }; - #define LZMA_HEADER_SIZE (LZMA_PROPS_SIZE + 8) /** @@ -151,15 +147,11 @@ LzmaUefiDecompressGetInfo ( DecodedSize = GetDecodedSizeOfBuf((UINT8*)Source); - mSourceLastUsedWithGetInfo = Source; - mSizeOfLastSource = SourceSize; - mDecompressedSizeForLastSource = (UInt32)DecodedSize; - *DestinationSize = mDecompressedSizeForLastSource; + *DestinationSize = (UINT32)DecodedSize; *ScratchSize = SCRATCH_BUFFER_REQUEST_SIZE; return RETURN_SUCCESS; } - /** Decompresses a Lzma compressed source buffer. @@ -185,6 +177,7 @@ RETURN_STATUS EFIAPI LzmaUefiDecompress ( IN CONST VOID *Source, + IN UINTN SourceSize, IN OUT VOID *Destination, IN OUT VOID *Scratch ) @@ -193,16 +186,14 @@ LzmaUefiDecompress ( ELzmaStatus Status; SizeT DecodedBufSize; SizeT EncodedDataSize; + ISzAllocWithData AllocFuncs = { + { SzAlloc, SzFree }, + Scratch, + SCRATCH_BUFFER_REQUEST_SIZE + }; - if (Source != mSourceLastUsedWithGetInfo) { - return RETURN_INVALID_PARAMETER; - } - - DecodedBufSize = (SizeT)mDecompressedSizeForLastSource; - EncodedDataSize = (SizeT)(mSizeOfLastSource - LZMA_HEADER_SIZE); - - mScratchBuffer = Scratch; - mScratchBufferSize = SCRATCH_BUFFER_REQUEST_SIZE; + DecodedBufSize = GetDecodedSizeOfBuf((UINT8*)Source); + EncodedDataSize = (SizeT) (SourceSize - LZMA_HEADER_SIZE); LzmaResult = LzmaDecode( Destination, @@ -213,7 +204,7 @@ LzmaUefiDecompress ( LZMA_PROPS_SIZE, LZMA_FINISH_END, &Status, - &g_Alloc + (ISzAlloc*) &AllocFuncs ); if (LzmaResult == SZ_OK) { diff --git a/IntelFrameworkModulePkg/Library/LzmaCustomDecompressLib/LzmaDecompressLibInternal.h b/IntelFrameworkModulePkg/Library/LzmaCustomDecompressLib/LzmaDecompressLibInternal.h index cc632a21f8..2db60fb19c 100644 --- a/IntelFrameworkModulePkg/Library/LzmaCustomDecompressLib/LzmaDecompressLibInternal.h +++ b/IntelFrameworkModulePkg/Library/LzmaCustomDecompressLib/LzmaDecompressLibInternal.h @@ -86,6 +86,7 @@ RETURN_STATUS EFIAPI LzmaUefiDecompress ( IN CONST VOID *Source, + IN UINTN SourceSize, IN OUT VOID *Destination, IN OUT VOID *Scratch ); -- 2.39.2