]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/DxeExtractGuidedSectionLib/DxeExtractGuidedSectionLib.c
Update ExtractGuidedSectionLib instance to cover the same handler is registered multi...
[mirror_edk2.git] / MdePkg / Library / DxeExtractGuidedSectionLib / DxeExtractGuidedSectionLib.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 DxeExtractGuidedSectionLib.c
15
16 Abstract:
17
18 Provide generic extract guided section functions.
19
20 --*/
21
22 #include <PiDxe.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 @param ImageHandle The firmware allocated handle for the EFI image.
40 @param SystemTable A pointer to the EFI System Table.
41
42 @retval RETURN_SUCCESS Allocate the global memory space to store guid and funciton tables.
43 @retval RETURN_OUT_OF_RESOURCES No enough memory to allocated.
44 **/
45 RETURN_STATUS
46 EFIAPI
47 DxeExtractGuidedSectionLibConstructor (
48 IN EFI_HANDLE ImageHandle,
49 IN EFI_SYSTEM_TABLE *SystemTable
50 )
51 {
52 //
53 // Allocate global pool space to store the registered handler and its guid value.
54 //
55 mExtractHandlerGuidTable = (GUID *) AllocatePool (PcdGet32 (PcdMaximumGuidedExtractHandler) * sizeof (GUID));
56 if (mExtractHandlerGuidTable == NULL) {
57 return RETURN_OUT_OF_RESOURCES;
58 }
59
60 mExtractDecodeHandlerTable = (EXTRACT_GUIDED_SECTION_DECODE_HANDLER *) AllocatePool (PcdGet32 (PcdMaximumGuidedExtractHandler) * sizeof (EXTRACT_GUIDED_SECTION_DECODE_HANDLER));
61 if (mExtractDecodeHandlerTable == NULL) {
62 return RETURN_OUT_OF_RESOURCES;
63 }
64
65 mExtractGetInfoHandlerTable = (EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER *) AllocatePool (PcdGet32 (PcdMaximumGuidedExtractHandler) * sizeof (EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER));
66 if (mExtractGetInfoHandlerTable == NULL) {
67 return RETURN_OUT_OF_RESOURCES;
68 }
69
70 //
71 // the initialized number is Zero.
72 //
73 mNumberOfExtractHandler = 0;
74
75 return RETURN_SUCCESS;
76 }
77
78 /**
79 Get the supported exract guided section Handler guid list.
80 If ExtractHandlerGuidTable = NULL, then ASSERT.
81
82 @param[in, out] ExtractHandlerGuidTable The extract Handler guid pointer list.
83
84 @retval return the number of the supported extract guided Handler.
85 **/
86 UINTN
87 EFIAPI
88 ExtractGuidedSectionGetGuidList (
89 IN OUT GUID **ExtractHandlerGuidTable
90 )
91 {
92 ASSERT (ExtractHandlerGuidTable != NULL);
93
94 *ExtractHandlerGuidTable = mExtractHandlerGuidTable;
95 return mNumberOfExtractHandler;
96 }
97
98 /**
99 Register Guided Section Extract and GetInfo handler.
100
101 @param[in] SectionGuid The guid matches this Extraction function.
102 @param[in] GetInfoHandler Function to get info from guided section.
103 @param[in] DecodeHandler Function to extract guided section.
104
105 @retval RETURN_SUCCESS Register Guided Section Extract function successfully.
106 @retval RETURN_OUT_OF_RESOURCES Resource is not enough to register new function.
107 @retval RETURN_INVALID_PARAMETER Input pointer to Guid value is not valid.
108 **/
109 RETURN_STATUS
110 EFIAPI
111 ExtractGuidedSectionRegisterHandlers (
112 IN CONST GUID *SectionGuid,
113 IN EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER GetInfoHandler,
114 IN EXTRACT_GUIDED_SECTION_DECODE_HANDLER DecodeHandler
115 )
116 {
117 UINT32 Index;
118 //
119 // Check input paramter.
120 //
121 if (SectionGuid == NULL) {
122 return RETURN_INVALID_PARAMETER;
123 }
124
125 //
126 // Search the match registered GetInfo handler for the input guided section.
127 //
128 for (Index = 0; Index < mNumberOfExtractHandler; Index ++) {
129 if (CompareGuid (&mExtractHandlerGuidTable[Index], SectionGuid)) {
130 break;
131 }
132 }
133
134 //
135 // If the guided handler has been registered before, only update its handler.
136 //
137 if (Index < mNumberOfExtractHandler) {
138 mExtractDecodeHandlerTable [Index] = DecodeHandler;
139 mExtractGetInfoHandlerTable [Index] = GetInfoHandler;
140 return RETURN_SUCCESS;
141 }
142
143 //
144 // Check the global table is enough to contain new Handler.
145 //
146 if (mNumberOfExtractHandler >= PcdGet32 (PcdMaximumGuidedExtractHandler)) {
147 return RETURN_OUT_OF_RESOURCES;
148 }
149
150 //
151 // Register new Handler and guid value.
152 //
153 CopyGuid (&mExtractHandlerGuidTable [mNumberOfExtractHandler], SectionGuid);
154 mExtractDecodeHandlerTable [mNumberOfExtractHandler] = DecodeHandler;
155 mExtractGetInfoHandlerTable [mNumberOfExtractHandler++] = GetInfoHandler;
156
157 return RETURN_SUCCESS;
158 }
159
160 /**
161 Get information from the guided section. This function first gets the guid value
162 from guided section header, then match this guid in the registered extract Handler list
163 to its corresponding getinfo Handler.
164 If not found, RETURN_INVALID_PARAMETER will be return.
165 If found, it will call the getinfo Handler to get the required size and attribute.
166
167 It will ASSERT () if the pointer to OutputBufferSize is NULL.
168 It will ASSERT () if the pointer to ScratchBufferSize is NULL.
169 It will ASSERT () if the pointer to SectionAttribute is NULL.
170
171 @param[in] InputSection Buffer containing the input GUIDed section to be processed.
172 @param[out] OutputBufferSize The size of OutputBuffer.
173 @param[out] ScratchBufferSize The size of ScratchBuffer.
174 @param[out] SectionAttribute The attribute of the input guided section.
175
176 @retval RETURN_SUCCESS Get the required information successfully.
177 @retval RETURN_INVALID_PARAMETER The input data can't be parsed correctly.
178 The GUID in InputSection does not match any registered guid list.
179
180 **/
181 RETURN_STATUS
182 EFIAPI
183 ExtractGuidedSectionGetInfo (
184 IN CONST VOID *InputSection,
185 OUT UINT32 *OutputBufferSize,
186 OUT UINT32 *ScratchBufferSize,
187 OUT UINT16 *SectionAttribute
188 )
189 {
190 UINT32 Index;
191
192 if (InputSection == NULL) {
193 return RETURN_INVALID_PARAMETER;
194 }
195
196 ASSERT (OutputBufferSize != NULL);
197 ASSERT (ScratchBufferSize != NULL);
198 ASSERT (SectionAttribute != NULL);
199
200 //
201 // Search the match registered GetInfo handler for the input guided section.
202 //
203 for (Index = 0; Index < mNumberOfExtractHandler; Index ++) {
204 if (CompareGuid (&mExtractHandlerGuidTable[Index], &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) {
205 break;
206 }
207 }
208
209 //
210 // Not found, the input guided section is not supported.
211 //
212 if (Index == mNumberOfExtractHandler) {
213 return RETURN_INVALID_PARAMETER;
214 }
215
216 //
217 // Call the match handler to getinfo for the input section data.
218 //
219 return mExtractGetInfoHandlerTable [Index] (
220 InputSection,
221 OutputBufferSize,
222 ScratchBufferSize,
223 SectionAttribute
224 );
225 }
226
227 /**
228 Extract data from the guided section. This function first gets the guid value
229 from guided section header, then match this guid in the registered extract Handler list
230 to its corresponding extract Handler.
231 If not found, RETURN_INVALID_PARAMETER will be return.
232 If found, it will call this extract Handler to get output data and AuthenticationStatus.
233
234 It will ASSERT () if the pointer to OutputBuffer is NULL.
235 It will ASSERT () if the pointer to AuthenticationStatus is NULL.
236
237 @param[in] InputSection Buffer containing the input GUIDed section to be processed.
238 @param[out] OutputBuffer OutputBuffer to point the start of the section's contents
239 if guided data is not required prcessing. Otherwise,
240 OutputBuffer to contain the output data, which is
241 allocated by the caller.
242 @param[out] ScratchBuffer A pointer to a caller-allocated buffer for function internal use.
243 @param[out] AuthenticationStatus
244 A pointer to a caller-allocated UINT32 that indicates the
245 authentication status of the output buffer.
246
247 @retval RETURN_SUCCESS Get the output data, size and AuthenticationStatus successfully.
248 @retval RETURN_INVALID_PARAMETER The input data can't be parsed correctly.
249 The GUID in InputSection does not match any registered guid.
250
251 **/
252 RETURN_STATUS
253 EFIAPI
254 ExtractGuidedSectionDecode (
255 IN CONST VOID *InputSection,
256 OUT VOID **OutputBuffer,
257 OUT VOID *ScratchBuffer, OPTIONAL
258 OUT UINT32 *AuthenticationStatus
259 )
260 {
261 UINT32 Index;
262
263 if (InputSection == NULL) {
264 return RETURN_INVALID_PARAMETER;
265 }
266
267 ASSERT (OutputBuffer != NULL);
268 ASSERT (AuthenticationStatus != NULL);
269
270 //
271 // Search the match registered GetInfo handler for the input guided section.
272 //
273 for (Index = 0; Index < mNumberOfExtractHandler; Index ++) {
274 if (CompareGuid (&mExtractHandlerGuidTable[Index], &(((EFI_GUID_DEFINED_SECTION *) InputSection)->SectionDefinitionGuid))) {
275 break;
276 }
277 }
278
279 //
280 // Not found, the input guided section is not supported.
281 //
282 if (Index == mNumberOfExtractHandler) {
283 return RETURN_INVALID_PARAMETER;
284 }
285
286 //
287 // Call the match handler to getinfo for the input section data.
288 //
289 return mExtractDecodeHandlerTable [Index] (
290 InputSection,
291 OutputBuffer,
292 ScratchBuffer,
293 AuthenticationStatus
294 );
295 }