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