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