]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Library/LzmaCustomDecompressLib/F86GuidedSectionExtraction.c
MdeModulePkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / MdeModulePkg / Library / LzmaCustomDecompressLib / F86GuidedSectionExtraction.c
1 /** @file
2 LZMA Decompress GUIDed Section Extraction Library, which produces LZMA custom
3 decompression algorithm with the converter for the different arch code.
4 It wraps Lzma decompress interfaces to GUIDed Section Extraction interfaces
5 and registers them into GUIDed handler table.
6
7 Copyright (c) 2012 - 2018, Intel Corporation. All rights reserved.<BR>
8 SPDX-License-Identifier: BSD-2-Clause-Patent
9
10 **/
11
12 #include "LzmaDecompressLibInternal.h"
13 #include "Sdk/C/Bra.h"
14
15 /**
16 Examines a GUIDed section and returns the size of the decoded buffer and the
17 size of an scratch buffer required to actually decode the data in a GUIDed section.
18
19 Examines a GUIDed section specified by InputSection.
20 If GUID for InputSection does not match the GUID that this handler supports,
21 then RETURN_UNSUPPORTED is returned.
22 If the required information can not be retrieved from InputSection,
23 then RETURN_INVALID_PARAMETER is returned.
24 If the GUID of InputSection does match the GUID that this handler supports,
25 then the size required to hold the decoded buffer is returned in OututBufferSize,
26 the size of an optional scratch buffer is returned in ScratchSize, and the Attributes field
27 from EFI_GUID_DEFINED_SECTION header of InputSection is returned in SectionAttribute.
28
29 If InputSection is NULL, then ASSERT().
30 If OutputBufferSize is NULL, then ASSERT().
31 If ScratchBufferSize is NULL, then ASSERT().
32 If SectionAttribute is NULL, then ASSERT().
33
34
35 @param[in] InputSection A pointer to a GUIDed section of an FFS formatted file.
36 @param[out] OutputBufferSize A pointer to the size, in bytes, of an output buffer required
37 if the buffer specified by InputSection were decoded.
38 @param[out] ScratchBufferSize A pointer to the size, in bytes, required as scratch space
39 if the buffer specified by InputSection were decoded.
40 @param[out] SectionAttribute A pointer to the attributes of the GUIDed section. See the Attributes
41 field of EFI_GUID_DEFINED_SECTION in the PI Specification.
42
43 @retval RETURN_SUCCESS The information about InputSection was returned.
44 @retval RETURN_UNSUPPORTED The section specified by InputSection does not match the GUID this handler supports.
45 @retval RETURN_INVALID_PARAMETER The information can not be retrieved from the section specified by InputSection.
46
47 **/
48 RETURN_STATUS
49 EFIAPI
50 LzmaArchGuidedSectionGetInfo (
51 IN CONST VOID *InputSection,
52 OUT UINT32 *OutputBufferSize,
53 OUT UINT32 *ScratchBufferSize,
54 OUT UINT16 *SectionAttribute
55 )
56 {
57 ASSERT (InputSection != NULL);
58 ASSERT (OutputBufferSize != NULL);
59 ASSERT (ScratchBufferSize != NULL);
60 ASSERT (SectionAttribute != NULL);
61
62 if (IS_SECTION2 (InputSection)) {
63 if (!CompareGuid (
64 &gLzmaF86CustomDecompressGuid,
65 &(((EFI_GUID_DEFINED_SECTION2 *) InputSection)->SectionDefinitionGuid))) {
66 return RETURN_INVALID_PARAMETER;
67 }
68
69 *SectionAttribute = ((EFI_GUID_DEFINED_SECTION2 *) InputSection)->Attributes;
70
71 return LzmaUefiDecompressGetInfo (
72 (UINT8 *) InputSection + ((EFI_GUID_DEFINED_SECTION2 *) InputSection)->DataOffset,
73 SECTION2_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION2 *) InputSection)->DataOffset,
74 OutputBufferSize,
75 ScratchBufferSize
76 );
77 } else {
78 if (!CompareGuid (
79 &gLzmaF86CustomDecompressGuid,
80 &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) {
81 return RETURN_INVALID_PARAMETER;
82 }
83
84 *SectionAttribute = ((EFI_GUID_DEFINED_SECTION *) InputSection)->Attributes;
85
86 return LzmaUefiDecompressGetInfo (
87 (UINT8 *) InputSection + ((EFI_GUID_DEFINED_SECTION *) InputSection)->DataOffset,
88 SECTION_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION *) InputSection)->DataOffset,
89 OutputBufferSize,
90 ScratchBufferSize
91 );
92 }
93 }
94
95 /**
96 Decompress a LZAM compressed GUIDed section into a caller allocated output buffer.
97
98 Decodes the GUIDed section specified by InputSection.
99 If GUID for InputSection does not match the GUID that this handler supports, then RETURN_UNSUPPORTED is returned.
100 If the data in InputSection can not be decoded, then RETURN_INVALID_PARAMETER is returned.
101 If the GUID of InputSection does match the GUID that this handler supports, then InputSection
102 is decoded into the buffer specified by OutputBuffer and the authentication status of this
103 decode operation is returned in AuthenticationStatus. If the decoded buffer is identical to the
104 data in InputSection, then OutputBuffer is set to point at the data in InputSection. Otherwise,
105 the decoded data will be placed in caller allocated buffer specified by OutputBuffer.
106
107 If InputSection is NULL, then ASSERT().
108 If OutputBuffer is NULL, then ASSERT().
109 If ScratchBuffer is NULL and this decode operation requires a scratch buffer, then ASSERT().
110 If AuthenticationStatus is NULL, then ASSERT().
111
112
113 @param[in] InputSection A pointer to a GUIDed section of an FFS formatted file.
114 @param[out] OutputBuffer A pointer to a buffer that contains the result of a decode operation.
115 @param[out] ScratchBuffer A caller allocated buffer that may be required by this function
116 as a scratch buffer to perform the decode operation.
117 @param[out] AuthenticationStatus
118 A pointer to the authentication status of the decoded output buffer.
119 See the definition of authentication status in the EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI
120 section of the PI Specification. EFI_AUTH_STATUS_PLATFORM_OVERRIDE must
121 never be set by this handler.
122
123 @retval RETURN_SUCCESS The buffer specified by InputSection was decoded.
124 @retval RETURN_UNSUPPORTED The section specified by InputSection does not match the GUID this handler supports.
125 @retval RETURN_INVALID_PARAMETER The section specified by InputSection can not be decoded.
126
127 **/
128 RETURN_STATUS
129 EFIAPI
130 LzmaArchGuidedSectionExtraction (
131 IN CONST VOID *InputSection,
132 OUT VOID **OutputBuffer,
133 OUT VOID *ScratchBuffer, OPTIONAL
134 OUT UINT32 *AuthenticationStatus
135 )
136 {
137 EFI_GUID *InputGuid;
138 VOID *Source;
139 UINTN SourceSize;
140 EFI_STATUS Status;
141 UINT32 X86State;
142 UINT32 OutputBufferSize;
143 UINT32 ScratchBufferSize;
144
145 ASSERT (OutputBuffer != NULL);
146 ASSERT (InputSection != NULL);
147
148 if (IS_SECTION2 (InputSection)) {
149 InputGuid = &(((EFI_GUID_DEFINED_SECTION2 *) InputSection)->SectionDefinitionGuid);
150 Source = (UINT8 *) InputSection + ((EFI_GUID_DEFINED_SECTION2 *) InputSection)->DataOffset;
151 SourceSize = SECTION2_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION2 *) InputSection)->DataOffset;
152 } else {
153 InputGuid = &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid);
154 Source = (UINT8 *) InputSection + ((EFI_GUID_DEFINED_SECTION *) InputSection)->DataOffset;
155 SourceSize = SECTION_SIZE (InputSection) - ((EFI_GUID_DEFINED_SECTION *) InputSection)->DataOffset;
156 }
157
158 if (!CompareGuid (&gLzmaF86CustomDecompressGuid, InputGuid)) {
159 return RETURN_INVALID_PARAMETER;
160 }
161
162 //
163 // Authentication is set to Zero, which may be ignored.
164 //
165 *AuthenticationStatus = 0;
166
167 Status = LzmaUefiDecompress (
168 Source,
169 SourceSize,
170 *OutputBuffer,
171 ScratchBuffer
172 );
173
174 //
175 // After decompress, the data need to be converted to the raw data.
176 //
177 if (!EFI_ERROR (Status)) {
178 Status = LzmaUefiDecompressGetInfo (
179 Source,
180 (UINT32) SourceSize,
181 &OutputBufferSize,
182 &ScratchBufferSize
183 );
184
185 if (!EFI_ERROR (Status)) {
186 x86_Convert_Init(X86State);
187 x86_Convert(*OutputBuffer, OutputBufferSize, 0, &X86State, 0);
188 }
189 }
190
191 return Status;
192 }
193
194
195 /**
196 Register LzmaArchDecompress and LzmaArchDecompressGetInfo handlers with LzmaF86CustomDecompressGuid.
197
198 @retval RETURN_SUCCESS Register successfully.
199 @retval RETURN_OUT_OF_RESOURCES No enough memory to store this handler.
200 **/
201 EFI_STATUS
202 EFIAPI
203 LzmaArchDecompressLibConstructor (
204 VOID
205 )
206 {
207 return ExtractGuidedSectionRegisterHandlers (
208 &gLzmaF86CustomDecompressGuid,
209 LzmaArchGuidedSectionGetInfo,
210 LzmaArchGuidedSectionExtraction
211 );
212 }
213