]> git.proxmox.com Git - mirror_edk2.git/blobdiff - IntelFrameworkModulePkg/Library/LzmaCustomDecompressLib/GuidedSectionExtraction.c
IntelFrameworkModulePkg: Fix MSFT C4255 warning
[mirror_edk2.git] / IntelFrameworkModulePkg / Library / LzmaCustomDecompressLib / GuidedSectionExtraction.c
index 2444f31def3a3bea5a58db1e7bba976c3afced47..7ef9fbbfc6bf9b3fac7238f6b20a723733c48bff 100644 (file)
@@ -1,8 +1,10 @@
 /** @file\r
-  LZMA Decompress GUIDed Section Extraction Library\r
+  LZMA Decompress GUIDed Section Extraction Library.\r
+  It wraps Lzma decompress interfaces to GUIDed Section Extraction interfaces\r
+  and registers them into GUIDed handler table.\r
 \r
-  Copyright (c) 2006 - 2009, Intel Corporation<BR>\r
-  All rights reserved. This program and the accompanying materials\r
+  Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.<BR>\r
+  This program and the accompanying materials\r
   are licensed and made available under the terms and conditions of the BSD License\r
   which accompanies this distribution.  The full text of the license may be found at\r
   http://opensource.org/licenses/bsd-license.php\r
 \r
 **/\r
 \r
-#include <Uefi.h>\r
-#include <Library/BaseMemoryLib.h>\r
-#include <Library/MemoryAllocationLib.h>\r
-#include <Library/ExtractGuidedSectionLib.h>\r
-#include <Pi/PiFirmwareFile.h>\r
-#include <Guid/LzmaDecompress.h>\r
-\r
-#include "LzmaDecompress.h"\r
-\r
-\r
-STATIC\r
-RETURN_STATUS\r
-EFIAPI\r
-LzmaGuidedSectionGetCompressedLocation (\r
-  IN  CONST VOID  *InputSection,\r
-  OUT VOID        **LmzaCompressedData,\r
-  OUT UINT32      *LmzaCompressedDataSize   OPTIONAL\r
-  )\r
-{\r
-  if (!CompareGuid (\r
-        &gLzmaCustomDecompressGuid, \r
-        &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) {\r
-    return RETURN_INVALID_PARAMETER;\r
-  }\r
-\r
-  //\r
-  // Retrieve the size and attribute of the input section data.\r
-  //\r
-  *LmzaCompressedData =\r
-    (VOID*) (\r
-        (UINT8 *) InputSection +\r
-        ((EFI_GUID_DEFINED_SECTION *) InputSection)->DataOffset\r
-      );\r
-  if (LmzaCompressedDataSize != NULL) {\r
-    *LmzaCompressedDataSize =\r
-      (UINT32)(\r
-        (\r
-          (*(UINT32 *) (((EFI_COMMON_SECTION_HEADER *) InputSection)->Size)) &\r
-          0x00ffffff\r
-        ) -\r
-        ((EFI_GUID_DEFINED_SECTION *) InputSection)->DataOffset\r
-      );\r
-  }\r
-\r
-  return RETURN_SUCCESS;\r
-}\r
-\r
+#include "LzmaDecompressLibInternal.h"\r
 \r
 /**\r
-  The implementation of 'GetInfo' for Guided Section\r
-  Extraction of LZMA compression.\r
-\r
-  @param  InputSection          Buffer containing the input GUIDed section to be processed. \r
-  @param  OutputBufferSize      The size of OutputBuffer.\r
-  @param  ScratchBufferSize     The size of ScratchBuffer.\r
-  @param  SectionAttribute      The attribute of the input guided section.\r
-\r
-  @retval RETURN_SUCCESS            The size of destination buffer and the size of scratch buffer are successull retrieved.\r
-  @retval RETURN_INVALID_PARAMETER  The source data is corrupted, or\r
-                                    The GUID in InputSection does not match this instance guid.\r
+  Examines a GUIDed section and returns the size of the decoded buffer and the\r
+  size of an scratch buffer required to actually decode the data in a GUIDed section.\r
+\r
+  Examines a GUIDed section specified by InputSection.  \r
+  If GUID for InputSection does not match the GUID that this handler supports,\r
+  then RETURN_UNSUPPORTED is returned.  \r
+  If the required information can not be retrieved from InputSection,\r
+  then RETURN_INVALID_PARAMETER is returned.\r
+  If the GUID of InputSection does match the GUID that this handler supports,\r
+  then the size required to hold the decoded buffer is returned in OututBufferSize,\r
+  the size of an optional scratch buffer is returned in ScratchSize, and the Attributes field\r
+  from EFI_GUID_DEFINED_SECTION header of InputSection is returned in SectionAttribute.\r
+  \r
+  If InputSection is NULL, then ASSERT().\r
+  If OutputBufferSize is NULL, then ASSERT().\r
+  If ScratchBufferSize is NULL, then ASSERT().\r
+  If SectionAttribute is NULL, then ASSERT().\r
+\r
+\r
+  @param[in]  InputSection       A pointer to a GUIDed section of an FFS formatted file.\r
+  @param[out] OutputBufferSize   A pointer to the size, in bytes, of an output buffer required\r
+                                 if the buffer specified by InputSection were decoded.\r
+  @param[out] ScratchBufferSize  A pointer to the size, in bytes, required as scratch space\r
+                                 if the buffer specified by InputSection were decoded.\r
+  @param[out] SectionAttribute   A pointer to the attributes of the GUIDed section. See the Attributes\r
+                                 field of EFI_GUID_DEFINED_SECTION in the PI Specification.\r
+\r
+  @retval  RETURN_SUCCESS            The information about InputSection was returned.\r
+  @retval  RETURN_UNSUPPORTED        The section specified by InputSection does not match the GUID this handler supports.\r
+  @retval  RETURN_INVALID_PARAMETER  The information can not be retrieved from the section specified by InputSection.\r
 \r
 **/\r
 RETURN_STATUS\r
@@ -83,44 +58,75 @@ LzmaGuidedSectionGetInfo (
   OUT UINT16      *SectionAttribute\r
   )\r
 {\r
-  RETURN_STATUS Status;\r
-  VOID          *LzmaInput;\r
-  UINT32        LzmaInputSize;\r
-\r
-  Status = LzmaGuidedSectionGetCompressedLocation(\r
-    InputSection,\r
-    &LzmaInput,\r
-    &LzmaInputSize\r
-    );\r
-  if (RETURN_ERROR (Status)) {\r
-    return Status;\r
-  }\r
+  ASSERT (InputSection != NULL);\r
+  ASSERT (OutputBufferSize != NULL);\r
+  ASSERT (ScratchBufferSize != NULL);\r
+  ASSERT (SectionAttribute != NULL);\r
+\r
+  if (IS_SECTION2 (InputSection)) {\r
+    if (!CompareGuid (\r
+        &gLzmaCustomDecompressGuid,\r
+        &(((EFI_GUID_DEFINED_SECTION2 *) InputSection)->SectionDefinitionGuid))) {\r
+      return RETURN_INVALID_PARAMETER;\r
+    }\r
+\r
+    *SectionAttribute = ((EFI_GUID_DEFINED_SECTION2 *) InputSection)->Attributes;\r
+\r
+    return LzmaUefiDecompressGetInfo (\r
+             (UINT8 *) InputSection + ((EFI_GUID_DEFINED_SECTION2 *) InputSection)->DataOffset,\r
+             SECTION2_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION2 *) InputSection)->DataOffset,\r
+             OutputBufferSize,\r
+             ScratchBufferSize\r
+             );\r
+  } else {\r
+    if (!CompareGuid (\r
+        &gLzmaCustomDecompressGuid,\r
+        &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) {\r
+      return RETURN_INVALID_PARAMETER;\r
+    }\r
 \r
-  *SectionAttribute = ((EFI_GUID_DEFINED_SECTION *) InputSection)->Attributes;\r
+    *SectionAttribute = ((EFI_GUID_DEFINED_SECTION *) InputSection)->Attributes;\r
 \r
-  return LzmaUefiDecompressGetInfo (\r
-    LzmaInput,\r
-    LzmaInputSize,\r
-    OutputBufferSize,\r
-    ScratchBufferSize\r
-    );\r
+    return LzmaUefiDecompressGetInfo (\r
+             (UINT8 *) InputSection + ((EFI_GUID_DEFINED_SECTION *) InputSection)->DataOffset,\r
+             SECTION_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION *) InputSection)->DataOffset,\r
+             OutputBufferSize,\r
+             ScratchBufferSize\r
+             );\r
+  }\r
 }\r
 \r
 /**\r
-  The implementation of Guided Section Extraction\r
-  for LZMA compression.\r
-\r
-  @param  InputSection          Buffer containing the input GUIDed section to be processed. \r
-  @param  OutputBuffer          OutputBuffer to point to the start of the section's contents.\r
-                                if guided data is not prcessed. Otherwise,\r
-                                OutputBuffer to contain the output data, which is allocated by the caller.\r
-  @param  ScratchBuffer         A pointer to a caller-allocated buffer for function internal use. \r
-  @param  AuthenticationStatus  A pointer to a caller-allocated UINT32 that indicates the\r
-                                authentication status of the output buffer. \r
-\r
-  @retval RETURN_SUCCESS            Decompression is successfull\r
-  @retval RETURN_INVALID_PARAMETER  The source data is corrupted, or\r
-                                    The GUID in InputSection does not match this instance guid.\r
+  Decompress a LZAM compressed GUIDed section into a caller allocated output buffer.\r
+  \r
+  Decodes the GUIDed section specified by InputSection.  \r
+  If GUID for InputSection does not match the GUID that this handler supports, then RETURN_UNSUPPORTED is returned.  \r
+  If the data in InputSection can not be decoded, then RETURN_INVALID_PARAMETER is returned.\r
+  If the GUID of InputSection does match the GUID that this handler supports, then InputSection\r
+  is decoded into the buffer specified by OutputBuffer and the authentication status of this\r
+  decode operation is returned in AuthenticationStatus.  If the decoded buffer is identical to the\r
+  data in InputSection, then OutputBuffer is set to point at the data in InputSection.  Otherwise,\r
+  the decoded data will be placed in caller allocated buffer specified by OutputBuffer.\r
+  \r
+  If InputSection is NULL, then ASSERT().\r
+  If OutputBuffer is NULL, then ASSERT().\r
+  If ScratchBuffer is NULL and this decode operation requires a scratch buffer, then ASSERT().\r
+  If AuthenticationStatus is NULL, then ASSERT().\r
+\r
+\r
+  @param[in]  InputSection  A pointer to a GUIDed section of an FFS formatted file.\r
+  @param[out] OutputBuffer  A pointer to a buffer that contains the result of a decode operation. \r
+  @param[out] ScratchBuffer A caller allocated buffer that may be required by this function\r
+                            as a scratch buffer to perform the decode operation. \r
+  @param[out] AuthenticationStatus \r
+                            A pointer to the authentication status of the decoded output buffer.\r
+                            See the definition of authentication status in the EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI\r
+                            section of the PI Specification. EFI_AUTH_STATUS_PLATFORM_OVERRIDE must\r
+                            never be set by this handler.\r
+\r
+  @retval  RETURN_SUCCESS            The buffer specified by InputSection was decoded.\r
+  @retval  RETURN_UNSUPPORTED        The section specified by InputSection does not match the GUID this handler supports.\r
+  @retval  RETURN_INVALID_PARAMETER  The section specified by InputSection can not be decoded.\r
 \r
 **/\r
 RETURN_STATUS\r
@@ -128,37 +134,55 @@ EFIAPI
 LzmaGuidedSectionExtraction (\r
   IN CONST  VOID    *InputSection,\r
   OUT       VOID    **OutputBuffer,\r
-  IN        VOID    *ScratchBuffer,        OPTIONAL\r
+  OUT       VOID    *ScratchBuffer,        OPTIONAL\r
   OUT       UINT32  *AuthenticationStatus\r
   )\r
 {\r
-  RETURN_STATUS Status;\r
-  VOID          *LzmaInput;\r
-\r
-  Status = LzmaGuidedSectionGetCompressedLocation(\r
-    InputSection,\r
-    &LzmaInput,\r
-    NULL\r
+  ASSERT (OutputBuffer != NULL);\r
+  ASSERT (InputSection != NULL);\r
+\r
+  if (IS_SECTION2 (InputSection)) {\r
+    if (!CompareGuid (\r
+        &gLzmaCustomDecompressGuid,\r
+        &(((EFI_GUID_DEFINED_SECTION2 *) InputSection)->SectionDefinitionGuid))) {\r
+      return RETURN_INVALID_PARAMETER;\r
+    }\r
+\r
+    //\r
+    // Authentication is set to Zero, which may be ignored.\r
+    //\r
+    *AuthenticationStatus = 0;\r
+\r
+    return LzmaUefiDecompress (\r
+             (UINT8 *) InputSection + ((EFI_GUID_DEFINED_SECTION2 *) InputSection)->DataOffset,\r
+             SECTION2_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION2 *) InputSection)->DataOffset,\r
+             *OutputBuffer,\r
+             ScratchBuffer\r
+             );\r
+  } else {\r
+    if (!CompareGuid (\r
+        &gLzmaCustomDecompressGuid,\r
+        &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) {\r
+      return RETURN_INVALID_PARAMETER;\r
+    }\r
+\r
+    //\r
+    // Authentication is set to Zero, which may be ignored.\r
+    //\r
+    *AuthenticationStatus = 0;\r
+\r
+    return LzmaUefiDecompress (\r
+             (UINT8 *) InputSection + ((EFI_GUID_DEFINED_SECTION *) InputSection)->DataOffset,\r
+             SECTION_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION *) InputSection)->DataOffset,\r
+             *OutputBuffer,\r
+             ScratchBuffer\r
     );\r
-  if (RETURN_ERROR (Status)) {\r
-    return Status;\r
   }\r
-\r
-  //\r
-  // Authentication is set to Zero, which may be ignored.\r
-  //\r
-  *AuthenticationStatus = 0;\r
-\r
-  return LzmaUefiDecompress (\r
-    LzmaInput,\r
-    *OutputBuffer,\r
-    ScratchBuffer\r
-    );\r
 }\r
 \r
 \r
 /**\r
-  Register LzmaDecompress handler.\r
+  Register LzmaDecompress and LzmaDecompressGetInfo handlers with LzmaCustomerDecompressGuid.\r
 \r
   @retval  RETURN_SUCCESS            Register successfully.\r
   @retval  RETURN_OUT_OF_RESOURCES   No enough memory to store this handler.\r
@@ -166,6 +190,7 @@ LzmaGuidedSectionExtraction (
 EFI_STATUS\r
 EFIAPI\r
 LzmaDecompressLibConstructor (\r
+  VOID\r
   )\r
 {\r
   return ExtractGuidedSectionRegisterHandlers (\r