]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseExtractGuidedSectionLib/BaseExtractGuidedSectionLib.c
MdePkg: Apply uncrustify changes
[mirror_edk2.git] / MdePkg / Library / BaseExtractGuidedSectionLib / BaseExtractGuidedSectionLib.c
1 /** @file
2 Provide generic extract guided section functions.
3
4 Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9 #include <PiPei.h>
10
11 #include <Library/DebugLib.h>
12 #include <Library/PcdLib.h>
13 #include <Library/BaseMemoryLib.h>
14 #include <Library/ExtractGuidedSectionLib.h>
15
16 #define EXTRACT_HANDLER_INFO_SIGNATURE SIGNATURE_32 ('E', 'G', 'S', 'I')
17
18 typedef struct {
19 UINT32 Signature;
20 UINT32 NumberOfExtractHandler;
21 GUID *ExtractHandlerGuidTable;
22 EXTRACT_GUIDED_SECTION_DECODE_HANDLER *ExtractDecodeHandlerTable;
23 EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER *ExtractGetInfoHandlerTable;
24 } EXTRACT_GUIDED_SECTION_HANDLER_INFO;
25
26 /**
27 HandlerInfo table address is set by PcdGuidedExtractHandlerTableAddress, which is used to store
28 the registered guid and Handler list. When it is initialized, it will be directly returned.
29 Or, HandlerInfo table will be initialized in this function.
30
31 @param[in, out] InfoPointer The pointer to the handler information structure.
32
33 @retval RETURN_SUCCESS HandlerInfo table can be used to store guid and function tables.
34 @retval RETURN_OUT_OF_RESOURCES HandlerInfo table address is not writable.
35 **/
36 RETURN_STATUS
37 GetExtractGuidedSectionHandlerInfo (
38 IN OUT EXTRACT_GUIDED_SECTION_HANDLER_INFO **InfoPointer
39 )
40 {
41 EXTRACT_GUIDED_SECTION_HANDLER_INFO *HandlerInfo;
42
43 //
44 // Set the available memory address to handler info.
45 //
46 HandlerInfo = (EXTRACT_GUIDED_SECTION_HANDLER_INFO *)(VOID *)(UINTN)PcdGet64 (PcdGuidedExtractHandlerTableAddress);
47 if (HandlerInfo == NULL) {
48 *InfoPointer = NULL;
49 return EFI_OUT_OF_RESOURCES;
50 }
51
52 //
53 // First check whether the handler information structure is initialized.
54 //
55 if (HandlerInfo->Signature == EXTRACT_HANDLER_INFO_SIGNATURE) {
56 //
57 // The handler information has been initialized and is returned.
58 //
59 *InfoPointer = HandlerInfo;
60 return RETURN_SUCCESS;
61 }
62
63 //
64 // Try to initialize the handler information structure
65 //
66 HandlerInfo->Signature = EXTRACT_HANDLER_INFO_SIGNATURE;
67 if (HandlerInfo->Signature != EXTRACT_HANDLER_INFO_SIGNATURE) {
68 //
69 // The handler information structure was not writeable because the memory is not ready.
70 //
71 *InfoPointer = NULL;
72 return RETURN_OUT_OF_RESOURCES;
73 }
74
75 //
76 // Init HandlerInfo structure
77 //
78 HandlerInfo->NumberOfExtractHandler = 0;
79 HandlerInfo->ExtractHandlerGuidTable = (GUID *)(HandlerInfo + 1);
80 HandlerInfo->ExtractDecodeHandlerTable = (EXTRACT_GUIDED_SECTION_DECODE_HANDLER *)(
81 (UINT8 *)HandlerInfo->ExtractHandlerGuidTable +
82 PcdGet32 (PcdMaximumGuidedExtractHandler) * sizeof (GUID)
83 );
84 HandlerInfo->ExtractGetInfoHandlerTable = (EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER *)(
85 (UINT8 *)HandlerInfo->ExtractDecodeHandlerTable +
86 PcdGet32 (PcdMaximumGuidedExtractHandler) *
87 sizeof (EXTRACT_GUIDED_SECTION_DECODE_HANDLER)
88 );
89 *InfoPointer = HandlerInfo;
90 return RETURN_SUCCESS;
91 }
92
93 /**
94 Retrieve the list GUIDs that have been registered through ExtractGuidedSectionRegisterHandlers().
95
96 Sets ExtractHandlerGuidTable so it points at a callee allocated array of registered GUIDs.
97 The total number of GUIDs in the array are returned. Since the array of GUIDs is callee allocated
98 and caller must treat this array of GUIDs as read-only data.
99 If ExtractHandlerGuidTable is NULL, then ASSERT().
100
101 @param[out] ExtractHandlerGuidTable A pointer to the array of GUIDs that have been registered through
102 ExtractGuidedSectionRegisterHandlers().
103
104 @return The number of the supported extract guided Handler.
105
106 **/
107 UINTN
108 EFIAPI
109 ExtractGuidedSectionGetGuidList (
110 OUT GUID **ExtractHandlerGuidTable
111 )
112 {
113 RETURN_STATUS Status;
114 EXTRACT_GUIDED_SECTION_HANDLER_INFO *HandlerInfo;
115
116 ASSERT (ExtractHandlerGuidTable != NULL);
117
118 //
119 // Get all registered handler information
120 //
121 Status = GetExtractGuidedSectionHandlerInfo (&HandlerInfo);
122 if (RETURN_ERROR (Status)) {
123 *ExtractHandlerGuidTable = NULL;
124 return 0;
125 }
126
127 //
128 // Get GuidTable and Table Number
129 //
130 ASSERT (HandlerInfo != NULL);
131 *ExtractHandlerGuidTable = HandlerInfo->ExtractHandlerGuidTable;
132 return HandlerInfo->NumberOfExtractHandler;
133 }
134
135 /**
136 Registers handlers of type EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER and EXTRACT_GUIDED_SECTION_DECODE_HANDLER
137 for a specific GUID section type.
138
139 Registers the handlers specified by GetInfoHandler and DecodeHandler with the GUID specified by SectionGuid.
140 If the GUID value specified by SectionGuid has already been registered, then return RETURN_ALREADY_STARTED.
141 If there are not enough resources available to register the handlers then RETURN_OUT_OF_RESOURCES is returned.
142
143 If SectionGuid is NULL, then ASSERT().
144 If GetInfoHandler is NULL, then ASSERT().
145 If DecodeHandler is NULL, then ASSERT().
146
147 @param[in] SectionGuid A pointer to the GUID associated with the the handlers
148 of the GUIDed section type being registered.
149 @param[in] GetInfoHandler The pointer to a function that examines a GUIDed section and returns the
150 size of the decoded buffer and the size of an optional scratch buffer
151 required to actually decode the data in a GUIDed section.
152 @param[in] DecodeHandler The pointer to a function that decodes a GUIDed section into a caller
153 allocated output buffer.
154
155 @retval RETURN_SUCCESS The handlers were registered.
156 @retval RETURN_OUT_OF_RESOURCES There are not enough resources available to register the handlers.
157
158 **/
159 RETURN_STATUS
160 EFIAPI
161 ExtractGuidedSectionRegisterHandlers (
162 IN CONST GUID *SectionGuid,
163 IN EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER GetInfoHandler,
164 IN EXTRACT_GUIDED_SECTION_DECODE_HANDLER DecodeHandler
165 )
166 {
167 UINT32 Index;
168 RETURN_STATUS Status;
169 EXTRACT_GUIDED_SECTION_HANDLER_INFO *HandlerInfo;
170
171 //
172 // Check input parameter
173 //
174 ASSERT (SectionGuid != NULL);
175 ASSERT (GetInfoHandler != NULL);
176 ASSERT (DecodeHandler != NULL);
177
178 //
179 // Get the registered handler information
180 //
181 Status = GetExtractGuidedSectionHandlerInfo (&HandlerInfo);
182 if (RETURN_ERROR (Status)) {
183 return Status;
184 }
185
186 //
187 // Search the match registered GetInfo handler for the input guided section.
188 //
189 ASSERT (HandlerInfo != NULL);
190 for (Index = 0; Index < HandlerInfo->NumberOfExtractHandler; Index++) {
191 if (CompareGuid (HandlerInfo->ExtractHandlerGuidTable + Index, SectionGuid)) {
192 //
193 // If the guided handler has been registered before, only update its handler.
194 //
195 HandlerInfo->ExtractDecodeHandlerTable[Index] = DecodeHandler;
196 HandlerInfo->ExtractGetInfoHandlerTable[Index] = GetInfoHandler;
197 return RETURN_SUCCESS;
198 }
199 }
200
201 //
202 // Check the global table is enough to contain new Handler.
203 //
204 if (HandlerInfo->NumberOfExtractHandler >= PcdGet32 (PcdMaximumGuidedExtractHandler)) {
205 return RETURN_OUT_OF_RESOURCES;
206 }
207
208 //
209 // Register new Handler and guid value.
210 //
211 CopyGuid (HandlerInfo->ExtractHandlerGuidTable + HandlerInfo->NumberOfExtractHandler, SectionGuid);
212 HandlerInfo->ExtractDecodeHandlerTable[HandlerInfo->NumberOfExtractHandler] = DecodeHandler;
213 HandlerInfo->ExtractGetInfoHandlerTable[HandlerInfo->NumberOfExtractHandler++] = GetInfoHandler;
214
215 return RETURN_SUCCESS;
216 }
217
218 /**
219 Retrieves a GUID from a GUIDed section and uses that GUID to select an associated handler of type
220 EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER that was registered with ExtractGuidedSectionRegisterHandlers().
221 The selected handler is used to retrieve and return the size of the decoded buffer and the size of an
222 optional scratch buffer required to actually decode the data in a GUIDed section.
223
224 Examines a GUIDed section specified by InputSection.
225 If GUID for InputSection does not match any of the GUIDs registered through ExtractGuidedSectionRegisterHandlers(),
226 then RETURN_UNSUPPORTED is returned.
227 If the GUID of InputSection does match the GUID that this handler supports, then the the associated handler
228 of type EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER that was registered with ExtractGuidedSectionRegisterHandlers()
229 is used to retrieve the OututBufferSize, ScratchSize, and Attributes values. The return status from the handler of
230 type EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER is returned.
231
232 If InputSection is NULL, then ASSERT().
233 If OutputBufferSize is NULL, then ASSERT().
234 If ScratchBufferSize is NULL, then ASSERT().
235 If SectionAttribute is NULL, then ASSERT().
236
237 @param[in] InputSection A pointer to a GUIDed section of an FFS formatted file.
238 @param[out] OutputBufferSize A pointer to the size, in bytes, of an output buffer required if the buffer
239 specified by InputSection were decoded.
240 @param[out] ScratchBufferSize A pointer to the size, in bytes, required as scratch space if the buffer specified by
241 InputSection were decoded.
242 @param[out] SectionAttribute A pointer to the attributes of the GUIDed section. See the Attributes field of
243 EFI_GUID_DEFINED_SECTION in the PI Specification.
244
245 @retval RETURN_SUCCESS Successfully retrieved the required information.
246 @retval RETURN_UNSUPPORTED The GUID from the section specified by InputSection does not match any of
247 the GUIDs registered with ExtractGuidedSectionRegisterHandlers().
248 @retval Others The return status from the handler associated with the GUID retrieved from
249 the section specified by InputSection.
250
251 **/
252 RETURN_STATUS
253 EFIAPI
254 ExtractGuidedSectionGetInfo (
255 IN CONST VOID *InputSection,
256 OUT UINT32 *OutputBufferSize,
257 OUT UINT32 *ScratchBufferSize,
258 OUT UINT16 *SectionAttribute
259 )
260 {
261 UINT32 Index;
262 RETURN_STATUS Status;
263 EXTRACT_GUIDED_SECTION_HANDLER_INFO *HandlerInfo;
264 EFI_GUID *SectionDefinitionGuid;
265
266 //
267 // Check input parameter
268 //
269 ASSERT (InputSection != NULL);
270 ASSERT (OutputBufferSize != NULL);
271 ASSERT (ScratchBufferSize != NULL);
272 ASSERT (SectionAttribute != NULL);
273
274 //
275 // Get all registered handler information.
276 //
277 Status = GetExtractGuidedSectionHandlerInfo (&HandlerInfo);
278 if (RETURN_ERROR (Status)) {
279 return Status;
280 }
281
282 if (IS_SECTION2 (InputSection)) {
283 SectionDefinitionGuid = &(((EFI_GUID_DEFINED_SECTION2 *)InputSection)->SectionDefinitionGuid);
284 } else {
285 SectionDefinitionGuid = &(((EFI_GUID_DEFINED_SECTION *)InputSection)->SectionDefinitionGuid);
286 }
287
288 //
289 // Search the match registered GetInfo handler for the input guided section.
290 //
291 ASSERT (HandlerInfo != NULL);
292 for (Index = 0; Index < HandlerInfo->NumberOfExtractHandler; Index++) {
293 if (CompareGuid (HandlerInfo->ExtractHandlerGuidTable + Index, SectionDefinitionGuid)) {
294 //
295 // Call the match handler to get information for the input section data.
296 //
297 return HandlerInfo->ExtractGetInfoHandlerTable[Index](
298 InputSection,
299 OutputBufferSize,
300 ScratchBufferSize,
301 SectionAttribute
302 );
303 }
304 }
305
306 //
307 // Not found, the input guided section is not supported.
308 //
309 return RETURN_UNSUPPORTED;
310 }
311
312 /**
313 Retrieves the GUID from a GUIDed section and uses that GUID to select an associated handler of type
314 EXTRACT_GUIDED_SECTION_DECODE_HANDLER that was registered with ExtractGuidedSectionRegisterHandlers().
315 The selected handler is used to decode the data in a GUIDed section and return the result in a caller
316 allocated output buffer.
317
318 Decodes the GUIDed section specified by InputSection.
319 If GUID for InputSection does not match any of the GUIDs registered through ExtractGuidedSectionRegisterHandlers(),
320 then RETURN_UNSUPPORTED is returned.
321 If the GUID of InputSection does match the GUID that this handler supports, then the the associated handler
322 of type EXTRACT_GUIDED_SECTION_DECODE_HANDLER that was registered with ExtractGuidedSectionRegisterHandlers()
323 is used to decode InputSection into the buffer specified by OutputBuffer and the authentication status of this
324 decode operation is returned in AuthenticationStatus. If the decoded buffer is identical to the data in InputSection,
325 then OutputBuffer is set to point at the data in InputSection. Otherwise, the decoded data will be placed in a caller
326 allocated buffer specified by OutputBuffer. This function is responsible for computing the EFI_AUTH_STATUS_PLATFORM_OVERRIDE
327 bit of in AuthenticationStatus. The return status from the handler of type EXTRACT_GUIDED_SECTION_DECODE_HANDLER is returned.
328
329 If InputSection is NULL, then ASSERT().
330 If OutputBuffer is NULL, then ASSERT().
331 If ScratchBuffer is NULL and this decode operation requires a scratch buffer, then ASSERT().
332 If AuthenticationStatus is NULL, then ASSERT().
333
334 @param[in] InputSection A pointer to a GUIDed section of an FFS formatted file.
335 @param[out] OutputBuffer A pointer to a buffer that contains the result of a decode operation.
336 @param[in] ScratchBuffer A caller allocated buffer that may be required by this function as a scratch buffer to perform the decode operation.
337 @param[out] AuthenticationStatus
338 A pointer to the authentication status of the decoded output buffer. See the definition
339 of authentication status in the EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI section of the PI
340 Specification.
341
342 @retval RETURN_SUCCESS The buffer specified by InputSection was decoded.
343 @retval RETURN_UNSUPPORTED The section specified by InputSection does not match the GUID this handler supports.
344 @retval RETURN_INVALID_PARAMETER The section specified by InputSection can not be decoded.
345
346 **/
347 RETURN_STATUS
348 EFIAPI
349 ExtractGuidedSectionDecode (
350 IN CONST VOID *InputSection,
351 OUT VOID **OutputBuffer,
352 IN VOID *ScratchBuffer OPTIONAL,
353 OUT UINT32 *AuthenticationStatus
354 )
355 {
356 UINT32 Index;
357 RETURN_STATUS Status;
358 EXTRACT_GUIDED_SECTION_HANDLER_INFO *HandlerInfo;
359 EFI_GUID *SectionDefinitionGuid;
360
361 //
362 // Check input parameter
363 //
364 ASSERT (InputSection != NULL);
365 ASSERT (OutputBuffer != NULL);
366 ASSERT (AuthenticationStatus != NULL);
367
368 //
369 // Get all registered handler information.
370 //
371 Status = GetExtractGuidedSectionHandlerInfo (&HandlerInfo);
372 if (RETURN_ERROR (Status)) {
373 return Status;
374 }
375
376 if (IS_SECTION2 (InputSection)) {
377 SectionDefinitionGuid = &(((EFI_GUID_DEFINED_SECTION2 *)InputSection)->SectionDefinitionGuid);
378 } else {
379 SectionDefinitionGuid = &(((EFI_GUID_DEFINED_SECTION *)InputSection)->SectionDefinitionGuid);
380 }
381
382 //
383 // Search the match registered Extract handler for the input guided section.
384 //
385 ASSERT (HandlerInfo != NULL);
386 for (Index = 0; Index < HandlerInfo->NumberOfExtractHandler; Index++) {
387 if (CompareGuid (HandlerInfo->ExtractHandlerGuidTable + Index, SectionDefinitionGuid)) {
388 //
389 // Call the match handler to extract raw data for the input guided section.
390 //
391 return HandlerInfo->ExtractDecodeHandlerTable[Index](
392 InputSection,
393 OutputBuffer,
394 ScratchBuffer,
395 AuthenticationStatus
396 );
397 }
398 }
399
400 //
401 // Not found, the input guided section is not supported.
402 //
403 return RETURN_UNSUPPORTED;
404 }
405
406 /**
407 Retrieves handlers of type EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER and
408 EXTRACT_GUIDED_SECTION_DECODE_HANDLER for a specific GUID section type.
409
410 Retrieves the handlers associated with SectionGuid and returns them in
411 GetInfoHandler and DecodeHandler.
412
413 If the GUID value specified by SectionGuid has not been registered, then
414 return RETURN_NOT_FOUND.
415
416 If SectionGuid is NULL, then ASSERT().
417
418 @param[in] SectionGuid A pointer to the GUID associated with the handlersof the GUIDed
419 section type being retrieved.
420 @param[out] GetInfoHandler Pointer to a function that examines a GUIDed section and returns
421 the size of the decoded buffer and the size of an optional scratch
422 buffer required to actually decode the data in a GUIDed section.
423 This is an optional parameter that may be NULL. If it is NULL, then
424 the previously registered handler is not returned.
425 @param[out] DecodeHandler Pointer to a function that decodes a GUIDed section into a caller
426 allocated output buffer. This is an optional parameter that may be NULL.
427 If it is NULL, then the previously registered handler is not returned.
428
429 @retval RETURN_SUCCESS The handlers were retrieved.
430 @retval RETURN_NOT_FOUND No handlers have been registered with the specified GUID.
431
432 **/
433 RETURN_STATUS
434 EFIAPI
435 ExtractGuidedSectionGetHandlers (
436 IN CONST GUID *SectionGuid,
437 OUT EXTRACT_GUIDED_SECTION_GET_INFO_HANDLER *GetInfoHandler OPTIONAL,
438 OUT EXTRACT_GUIDED_SECTION_DECODE_HANDLER *DecodeHandler OPTIONAL
439 )
440 {
441 UINT32 Index;
442 RETURN_STATUS Status;
443 EXTRACT_GUIDED_SECTION_HANDLER_INFO *HandlerInfo;
444
445 //
446 // Check input parameter
447 //
448 ASSERT (SectionGuid != NULL);
449
450 //
451 // Get the registered handler information
452 //
453 Status = GetExtractGuidedSectionHandlerInfo (&HandlerInfo);
454 if (RETURN_ERROR (Status)) {
455 return Status;
456 }
457
458 //
459 // Search the match registered GetInfo handler for the input guided section.
460 //
461 ASSERT (HandlerInfo != NULL);
462 for (Index = 0; Index < HandlerInfo->NumberOfExtractHandler; Index++) {
463 if (CompareGuid (HandlerInfo->ExtractHandlerGuidTable + Index, SectionGuid)) {
464 //
465 // If the guided handler has been registered before, then return the registered handlers.
466 //
467 if (GetInfoHandler != NULL) {
468 *GetInfoHandler = HandlerInfo->ExtractGetInfoHandlerTable[Index];
469 }
470
471 if (DecodeHandler != NULL) {
472 *DecodeHandler = HandlerInfo->ExtractDecodeHandlerTable[Index];
473 }
474
475 return RETURN_SUCCESS;
476 }
477 }
478
479 return RETURN_NOT_FOUND;
480 }