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