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