]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/PeiDxeExtractGuidedSectionLib/PeiDxeExtractGuidedSectionLib.c
1. Add ExtractGuidedSectionLib library to replace customdecompress library.
[mirror_edk2.git] / MdePkg / Library / PeiDxeExtractGuidedSectionLib / PeiDxeExtractGuidedSectionLib.c
1 /*++
2
3 Copyright (c) 2007, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12 Module Name:
13
14 PeiDxeExtractGuidedSectionLib.c
15
16 Abstract:
17
18 Provide generic extract guided section functions.
19
20 --*/
21
22 #include <PiPei.h>
23
24 #include <Library/DebugLib.h>
25 #include <Library/PcdLib.h>
26 #include <Library/BaseMemoryLib.h>
27 #include <Library/MemoryAllocationLib.h>
28 #include <Library/ExtractGuidedSectionLib.h>
29
30 STATIC GUID *mExtractHandlerGuidTable;
31 STATIC UINT32 mNumberOfExtractHandler;
32
33 STATIC EXTRACT_GUIDED_SECTION_DECODE_HANDLER *mExtractDecodeHandlerTable;
34 STATIC EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER *mExtractGetInfoHandlerTable;
35
36 /**
37 Construtor allocates the global memory to store the registered guid and Handler list.
38
39 @retval RETURN_SUCCESS Allocate the global memory space to store guid and funciton tables.
40 @retval RETURN_OUT_OF_RESOURCES No enough memory to allocated.
41 **/
42 RETURN_STATUS
43 EFIAPI
44 PeiDxeExtractGuidedSectionLibConstructor (
45 )
46 {
47 //
48 // Allocate global pool space to store the registered handler and its guid value.
49 //
50 mExtractHandlerGuidTable = (GUID *) AllocatePool (PcdGet32 (PcdMaximumGuidedExtractHandler) * sizeof (GUID));
51 if (mExtractHandlerGuidTable == NULL) {
52 return RETURN_OUT_OF_RESOURCES;
53 }
54
55 mExtractDecodeHandlerTable = (EXTRACT_GUIDED_SECTION_DECODE_HANDLER *) AllocatePool (PcdGet32 (PcdMaximumGuidedExtractHandler) * sizeof (EXTRACT_GUIDED_SECTION_DECODE_HANDLER));
56 if (mExtractDecodeHandlerTable == NULL) {
57 return RETURN_OUT_OF_RESOURCES;
58 }
59
60 mExtractGetInfoHandlerTable = (EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER *) AllocatePool (PcdGet32 (PcdMaximumGuidedExtractHandler) * sizeof (EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER));
61 if (mExtractGetInfoHandlerTable == NULL) {
62 return RETURN_OUT_OF_RESOURCES;
63 }
64
65 //
66 // the initialized number is Zero.
67 //
68 mNumberOfExtractHandler = 0;
69
70 return RETURN_SUCCESS;
71 }
72
73 /**
74 Get the supported exract guided section Handler guid list.
75 If ExtractHandlerGuidTable = NULL, then ASSERT.
76
77 @param[in, out] ExtractHandlerGuidTable The extract Handler guid pointer list.
78
79 @retval return the number of the supported extract guided Handler.
80 **/
81 UINTN
82 EFIAPI
83 ExtractGuidedSectionGetGuidList (
84 IN OUT GUID **ExtractHandlerGuidTable
85 )
86 {
87 ASSERT (ExtractHandlerGuidTable != NULL);
88
89 *ExtractHandlerGuidTable = mExtractHandlerGuidTable;
90 return mNumberOfExtractHandler;
91 }
92
93 /**
94 Register Guided Section Extract and GetInfo handler.
95
96 @param[in] SectionGuid The guid matches this Extraction function.
97 @param[in] GetInfoHandler Function to get info from guided section.
98 @param[in] DecodeHandler Function to extract guided section.
99
100 @retval RETURN_SUCCESS Register Guided Section Extract function successfully.
101 @retval RETURN_OUT_OF_RESOURCES Resource is not enough to register new function.
102 @retval RETURN_INVALID_PARAMETER Input pointer to Guid value is not valid.
103 **/
104 RETURN_STATUS
105 EFIAPI
106 ExtractGuidedSectionRegisterHandlers (
107 IN CONST GUID *SectionGuid,
108 IN EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER GetInfoHandler,
109 IN EXTRACT_GUIDED_SECTION_DECODE_HANDLER DecodeHandler
110 )
111 {
112 //
113 // Check input paramter.
114 //
115 if (SectionGuid == NULL) {
116 return RETURN_INVALID_PARAMETER;
117 }
118 //
119 // Check the global table is enough to contain new Handler.
120 //
121 if (mNumberOfExtractHandler >= PcdGet32 (PcdMaximumGuidedExtractHandler)) {
122 return RETURN_OUT_OF_RESOURCES;
123 }
124
125 //
126 // Register new Handler and guid value.
127 //
128 CopyGuid (&mExtractHandlerGuidTable [mNumberOfExtractHandler], SectionGuid);
129 mExtractDecodeHandlerTable [mNumberOfExtractHandler] = DecodeHandler;
130 mExtractGetInfoHandlerTable [mNumberOfExtractHandler++] = GetInfoHandler;
131
132 return RETURN_SUCCESS;
133 }
134
135 /**
136 Get information from the guided section. This function first gets the guid value
137 from guided section header, then match this guid in the registered extract Handler list
138 to its corresponding getinfo Handler.
139 If not found, RETURN_UNSUPPORTED will be return.
140 If found, it will call the getinfo Handler to get the required size and attribute.
141
142 It will ASSERT () if the pointer to OutputBufferSize is NULL.
143 It will ASSERT () if the pointer to ScratchBufferSize is NULL.
144 It will ASSERT () if the pointer to SectionAttribute is NULL.
145
146 @param[in] InputSection Buffer containing the input GUIDed section to be processed.
147 @param[out] OutputBufferSize The size of OutputBuffer.
148 @param[out] ScratchBufferSize The size of ScratchBuffer.
149 @param[out] SectionAttribute The attribute of the input guided section.
150
151 @retval RETURN_SUCCESS Get the required information successfully.
152 @retval RETURN_UNSUPPORTED Guided section data is not supported.
153 @retval RETURN_INVALID_PARAMETER The input data can't be parsed correctly.
154
155 **/
156 RETURN_STATUS
157 EFIAPI
158 ExtractGuidedSectionGetInfo (
159 IN CONST VOID *InputSection,
160 OUT UINT32 *OutputBufferSize,
161 OUT UINT32 *ScratchBufferSize,
162 OUT UINT16 *SectionAttribute
163 )
164 {
165 UINT32 Index;
166
167 if (InputSection == NULL) {
168 return RETURN_INVALID_PARAMETER;
169 }
170
171 ASSERT (OutputBufferSize != NULL);
172 ASSERT (ScratchBufferSize != NULL);
173 ASSERT (SectionAttribute != NULL);
174
175 //
176 // Search the match registered GetInfo handler for the input guided section.
177 //
178 for (Index = 0; Index < mNumberOfExtractHandler; Index ++) {
179 if (CompareGuid (&mExtractHandlerGuidTable[Index], &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) {
180 break;
181 }
182 }
183
184 //
185 // Not found, the input guided section is not supported.
186 //
187 if (Index == mNumberOfExtractHandler) {
188 return RETURN_UNSUPPORTED;
189 }
190
191 //
192 // Call the match handler to getinfo for the input section data.
193 //
194 return mExtractGetInfoHandlerTable [Index] (
195 InputSection,
196 OutputBufferSize,
197 ScratchBufferSize,
198 SectionAttribute
199 );
200 }
201
202 /**
203 Extract data from the guided section. This function first gets the guid value
204 from guided section header, then match this guid in the registered extract Handler list
205 to its corresponding extract Handler.
206 If not found, RETURN_UNSUPPORTED will be return.
207 If found, it will call this extract Handler to get output data and AuthenticationStatus.
208
209 It will ASSERT () if the pointer to OutputBuffer is NULL.
210 It will ASSERT () if the pointer to AuthenticationStatus is NULL.
211
212 @param[in] InputSection Buffer containing the input GUIDed section to be processed.
213 @param[out] OutputBuffer OutputBuffer to point the start of the section's contents
214 if guided data is not required prcessing. Otherwise,
215 OutputBuffer to contain the output data, which is
216 allocated by the caller.
217 @param[out] ScratchBuffer A pointer to a caller-allocated buffer for function internal use.
218 @param[out] AuthenticationStatus
219 A pointer to a caller-allocated UINT32 that indicates the
220 authentication status of the output buffer.
221
222 @retval RETURN_SUCCESS Get the output data, size and AuthenticationStatus successfully.
223 @retval RETURN_UNSUPPORTED Guided section data is not supported to be decoded.
224 @retval RETURN_INVALID_PARAMETER The input data can't be parsed correctly.
225
226 **/
227 RETURN_STATUS
228 EFIAPI
229 ExtractGuidedSectionDecode (
230 IN CONST VOID *InputSection,
231 OUT VOID **OutputBuffer,
232 OUT VOID *ScratchBuffer, OPTIONAL
233 OUT UINT32 *AuthenticationStatus
234 )
235 {
236 UINT32 Index;
237
238 if (InputSection == NULL) {
239 return RETURN_INVALID_PARAMETER;
240 }
241
242 ASSERT (OutputBuffer != NULL);
243 ASSERT (AuthenticationStatus != NULL);
244
245 //
246 // Search the match registered GetInfo handler for the input guided section.
247 //
248 for (Index = 0; Index < mNumberOfExtractHandler; Index ++) {
249 if (CompareGuid (&mExtractHandlerGuidTable[Index], &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) {
250 break;
251 }
252 }
253
254 //
255 // Not found, the input guided section is not supported.
256 //
257 if (Index == mNumberOfExtractHandler) {
258 return RETURN_UNSUPPORTED;
259 }
260
261 //
262 // Call the match handler to getinfo for the input section data.
263 //
264 return mExtractDecodeHandlerTable [Index] (
265 InputSection,
266 OutputBuffer,
267 ScratchBuffer,
268 AuthenticationStatus
269 );
270 }