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