1 /** @file
2   LZMA Decompress GUIDed Section Extraction Library.
3   It wraps Lzma decompress interfaces to GUIDed Section Extraction interfaces
4   and registers them into GUIDed handler table.
5 
6   Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.<BR>
7   This program and the accompanying materials
8   are licensed and made available under the terms and conditions of the BSD License
9   which accompanies this distribution.  The full text of the license may be found at
10   http://opensource.org/licenses/bsd-license.php
11 
12   THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13   WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14 
15 **/
16 
17 #include "LzmaDecompressLibInternal.h"
18 
19 /**
20   Examines a GUIDed section and returns the size of the decoded buffer and the
21   size of an scratch buffer required to actually decode the data in a GUIDed section.
22 
23   Examines a GUIDed section specified by InputSection.
24   If GUID for InputSection does not match the GUID that this handler supports,
25   then RETURN_UNSUPPORTED is returned.
26   If the required information can not be retrieved from InputSection,
27   then RETURN_INVALID_PARAMETER is returned.
28   If the GUID of InputSection does match the GUID that this handler supports,
29   then the size required to hold the decoded buffer is returned in OututBufferSize,
30   the size of an optional scratch buffer is returned in ScratchSize, and the Attributes field
31   from EFI_GUID_DEFINED_SECTION header of InputSection is returned in SectionAttribute.
32 
33   If InputSection is NULL, then ASSERT().
34   If OutputBufferSize is NULL, then ASSERT().
35   If ScratchBufferSize is NULL, then ASSERT().
36   If SectionAttribute is NULL, then ASSERT().
37 
38 
39   @param[in]  InputSection       A pointer to a GUIDed section of an FFS formatted file.
40   @param[out] OutputBufferSize   A pointer to the size, in bytes, of an output buffer required
41                                  if the buffer specified by InputSection were decoded.
42   @param[out] ScratchBufferSize  A pointer to the size, in bytes, required as scratch space
43                                  if the buffer specified by InputSection were decoded.
44   @param[out] SectionAttribute   A pointer to the attributes of the GUIDed section. See the Attributes
45                                  field of EFI_GUID_DEFINED_SECTION in the PI Specification.
46 
47   @retval  RETURN_SUCCESS            The information about InputSection was returned.
48   @retval  RETURN_UNSUPPORTED        The section specified by InputSection does not match the GUID this handler supports.
49   @retval  RETURN_INVALID_PARAMETER  The information can not be retrieved from the section specified by InputSection.
50 
51 **/
52 RETURN_STATUS
53 EFIAPI
LzmaGuidedSectionGetInfo(IN CONST VOID * InputSection,OUT UINT32 * OutputBufferSize,OUT UINT32 * ScratchBufferSize,OUT UINT16 * SectionAttribute)54 LzmaGuidedSectionGetInfo (
55   IN  CONST VOID  *InputSection,
56   OUT UINT32      *OutputBufferSize,
57   OUT UINT32      *ScratchBufferSize,
58   OUT UINT16      *SectionAttribute
59   )
60 {
61   ASSERT (InputSection != NULL);
62   ASSERT (OutputBufferSize != NULL);
63   ASSERT (ScratchBufferSize != NULL);
64   ASSERT (SectionAttribute != NULL);
65 
66   if (IS_SECTION2 (InputSection)) {
67     if (!CompareGuid (
68         &gLzmaCustomDecompressGuid,
69         &(((EFI_GUID_DEFINED_SECTION2 *) InputSection)->SectionDefinitionGuid))) {
70       return RETURN_INVALID_PARAMETER;
71     }
72 
73     *SectionAttribute = ((EFI_GUID_DEFINED_SECTION2 *) InputSection)->Attributes;
74 
75     return LzmaUefiDecompressGetInfo (
76              (UINT8 *) InputSection + ((EFI_GUID_DEFINED_SECTION2 *) InputSection)->DataOffset,
77              SECTION2_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION2 *) InputSection)->DataOffset,
78              OutputBufferSize,
79              ScratchBufferSize
80              );
81   } else {
82     if (!CompareGuid (
83         &gLzmaCustomDecompressGuid,
84         &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) {
85       return RETURN_INVALID_PARAMETER;
86     }
87 
88     *SectionAttribute = ((EFI_GUID_DEFINED_SECTION *) InputSection)->Attributes;
89 
90     return LzmaUefiDecompressGetInfo (
91              (UINT8 *) InputSection + ((EFI_GUID_DEFINED_SECTION *) InputSection)->DataOffset,
92              SECTION_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION *) InputSection)->DataOffset,
93              OutputBufferSize,
94              ScratchBufferSize
95              );
96   }
97 }
98 
99 /**
100   Decompress a LZAM compressed GUIDed section into a caller allocated output buffer.
101 
102   Decodes the GUIDed section specified by InputSection.
103   If GUID for InputSection does not match the GUID that this handler supports, then RETURN_UNSUPPORTED is returned.
104   If the data in InputSection can not be decoded, then RETURN_INVALID_PARAMETER is returned.
105   If the GUID of InputSection does match the GUID that this handler supports, then InputSection
106   is decoded into the buffer specified by OutputBuffer and the authentication status of this
107   decode operation is returned in AuthenticationStatus.  If the decoded buffer is identical to the
108   data in InputSection, then OutputBuffer is set to point at the data in InputSection.  Otherwise,
109   the decoded data will be placed in caller allocated buffer specified by OutputBuffer.
110 
111   If InputSection is NULL, then ASSERT().
112   If OutputBuffer is NULL, then ASSERT().
113   If ScratchBuffer is NULL and this decode operation requires a scratch buffer, then ASSERT().
114   If AuthenticationStatus is NULL, then ASSERT().
115 
116 
117   @param[in]  InputSection  A pointer to a GUIDed section of an FFS formatted file.
118   @param[out] OutputBuffer  A pointer to a buffer that contains the result of a decode operation.
119   @param[out] ScratchBuffer A caller allocated buffer that may be required by this function
120                             as a scratch buffer to perform the decode operation.
121   @param[out] AuthenticationStatus
122                             A pointer to the authentication status of the decoded output buffer.
123                             See the definition of authentication status in the EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI
124                             section of the PI Specification. EFI_AUTH_STATUS_PLATFORM_OVERRIDE must
125                             never be set by this handler.
126 
127   @retval  RETURN_SUCCESS            The buffer specified by InputSection was decoded.
128   @retval  RETURN_UNSUPPORTED        The section specified by InputSection does not match the GUID this handler supports.
129   @retval  RETURN_INVALID_PARAMETER  The section specified by InputSection can not be decoded.
130 
131 **/
132 RETURN_STATUS
133 EFIAPI
LzmaGuidedSectionExtraction(IN CONST VOID * InputSection,OUT VOID ** OutputBuffer,OUT VOID * ScratchBuffer,OPTIONAL OUT UINT32 * AuthenticationStatus)134 LzmaGuidedSectionExtraction (
135   IN CONST  VOID    *InputSection,
136   OUT       VOID    **OutputBuffer,
137   OUT       VOID    *ScratchBuffer,        OPTIONAL
138   OUT       UINT32  *AuthenticationStatus
139   )
140 {
141   ASSERT (OutputBuffer != NULL);
142   ASSERT (InputSection != NULL);
143 
144   if (IS_SECTION2 (InputSection)) {
145     if (!CompareGuid (
146         &gLzmaCustomDecompressGuid,
147         &(((EFI_GUID_DEFINED_SECTION2 *) InputSection)->SectionDefinitionGuid))) {
148       return RETURN_INVALID_PARAMETER;
149     }
150 
151     //
152     // Authentication is set to Zero, which may be ignored.
153     //
154     *AuthenticationStatus = 0;
155 
156     return LzmaUefiDecompress (
157              (UINT8 *) InputSection + ((EFI_GUID_DEFINED_SECTION2 *) InputSection)->DataOffset,
158              SECTION2_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION2 *) InputSection)->DataOffset,
159              *OutputBuffer,
160              ScratchBuffer
161              );
162   } else {
163     if (!CompareGuid (
164         &gLzmaCustomDecompressGuid,
165         &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) {
166       return RETURN_INVALID_PARAMETER;
167     }
168 
169     //
170     // Authentication is set to Zero, which may be ignored.
171     //
172     *AuthenticationStatus = 0;
173 
174     return LzmaUefiDecompress (
175              (UINT8 *) InputSection + ((EFI_GUID_DEFINED_SECTION *) InputSection)->DataOffset,
176              SECTION_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION *) InputSection)->DataOffset,
177              *OutputBuffer,
178              ScratchBuffer
179     );
180   }
181 }
182 
183 
184 /**
185   Register LzmaDecompress and LzmaDecompressGetInfo handlers with LzmaCustomerDecompressGuid.
186 
187   @retval  RETURN_SUCCESS            Register successfully.
188   @retval  RETURN_OUT_OF_RESOURCES   No enough memory to store this handler.
189 **/
190 EFI_STATUS
191 EFIAPI
LzmaDecompressLibConstructor()192 LzmaDecompressLibConstructor (
193   )
194 {
195   return ExtractGuidedSectionRegisterHandlers (
196           &gLzmaCustomDecompressGuid,
197           LzmaGuidedSectionGetInfo,
198           LzmaGuidedSectionExtraction
199           );
200 }
201 
202