]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Core/Dxe/SectionExtraction/CoreSectionExtraction.c
MdeModulePkg/Core/Dxe/SectionExtraction/CoreSectionExtraction.c:
[mirror_edk2.git] / MdeModulePkg / Core / Dxe / SectionExtraction / CoreSectionExtraction.c
1 /**@file
2 Section Extraction Protocol implementation.
3
4 Stream database is implemented as a linked list of section streams,
5 where each stream contains a linked list of children, which may be leaves or
6 encapsulations.
7
8 Children that are encapsulations generate new stream entries
9 when they are created. Streams can also be created by calls to
10 SEP->OpenSectionStream().
11
12 The database is only created far enough to return the requested data from
13 any given stream, or to determine that the requested data is not found.
14
15 If a GUIDed encapsulation is encountered, there are three possiblilites.
16
17 1) A support protocol is found, in which the stream is simply processed with
18 the support protocol.
19
20 2) A support protocol is not found, but the data is available to be read
21 without processing. In this case, the database is built up through the
22 recursions to return the data, and a RPN event is set that will enable
23 the stream in question to be refreshed if and when the required section
24 extraction protocol is published.This insures the AuthenticationStatus
25 does not become stale in the cache.
26
27 3) A support protocol is not found, and the data is not available to be read
28 without it. This results in EFI_PROTOCOL_ERROR.
29
30 Copyright (c) 2006 - 2007, Intel Corporation
31 All rights reserved. This program and the accompanying materials
32 are licensed and made available under the terms and conditions of the BSD License
33 which accompanies this distribution. The full text of the license may be found at
34 http://opensource.org/licenses/bsd-license.php
35
36 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
37 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
38
39 **/
40
41 #include <DxeMain.h>
42
43 //
44 // Local defines and typedefs
45 //
46 #define CORE_SECTION_CHILD_SIGNATURE EFI_SIGNATURE_32('S','X','C','S')
47 #define CHILD_SECTION_NODE_FROM_LINK(Node) \
48 CR (Node, CORE_SECTION_CHILD_NODE, Link, CORE_SECTION_CHILD_SIGNATURE)
49
50 typedef struct {
51 UINT32 Signature;
52 LIST_ENTRY Link;
53 UINT32 Type;
54 UINT32 Size;
55 //
56 // StreamBase + OffsetInStream == pointer to section header in stream. The
57 // stream base is always known when walking the sections within.
58 //
59 UINT32 OffsetInStream;
60 //
61 // Then EncapsulatedStreamHandle below is always 0 if the section is NOT an
62 // encapsulating section. Otherwise, it contains the stream handle
63 // of the encapsulated stream. This handle is ALWAYS produced any time an
64 // encapsulating child is encountered, irrespective of whether the
65 // encapsulated stream is processed further.
66 //
67 UINTN EncapsulatedStreamHandle;
68 EFI_GUID *EncapsulationGuid;
69 } CORE_SECTION_CHILD_NODE;
70
71 #define CORE_SECTION_STREAM_SIGNATURE EFI_SIGNATURE_32('S','X','S','S')
72 #define STREAM_NODE_FROM_LINK(Node) \
73 CR (Node, CORE_SECTION_STREAM_NODE, Link, CORE_SECTION_STREAM_SIGNATURE)
74
75 typedef struct {
76 UINT32 Signature;
77 LIST_ENTRY Link;
78 UINTN StreamHandle;
79 UINT8 *StreamBuffer;
80 UINTN StreamLength;
81 LIST_ENTRY Children;
82 //
83 // Authentication status is from GUIDed encapsulations.
84 //
85 UINT32 AuthenticationStatus;
86 } CORE_SECTION_STREAM_NODE;
87
88 #define NULL_STREAM_HANDLE 0
89
90 typedef struct {
91 CORE_SECTION_CHILD_NODE *ChildNode;
92 CORE_SECTION_STREAM_NODE *ParentStream;
93 VOID *Registration;
94 EFI_EVENT Event;
95 } RPN_EVENT_CONTEXT;
96
97
98
99 //
100 // Local prototypes
101 //
102
103 STATIC
104 BOOLEAN
105 ChildIsType (
106 IN CORE_SECTION_STREAM_NODE *Stream,
107 IN CORE_SECTION_CHILD_NODE *Child,
108 IN EFI_SECTION_TYPE SearchType,
109 IN EFI_GUID *SectionDefinitionGuid
110 );
111
112 #if 0
113 STATIC
114 VOID
115 EFIAPI
116 NotifyGuidedExtraction (
117 IN EFI_EVENT Event,
118 IN VOID *RpnContext
119 );
120 #endif
121
122 #if 0
123 STATIC
124 VOID
125 CreateGuidedExtractionRpnEvent (
126 IN CORE_SECTION_STREAM_NODE *ParentStream,
127 IN CORE_SECTION_CHILD_NODE *ChildNode
128 );
129 #endif
130
131 STATIC
132 EFI_STATUS
133 FindStreamNode (
134 IN UINTN SearchHandle,
135 OUT CORE_SECTION_STREAM_NODE **FoundStream
136 );
137
138 STATIC
139 EFI_STATUS
140 FindChildNode (
141 IN CORE_SECTION_STREAM_NODE *SourceStream,
142 IN EFI_SECTION_TYPE SearchType,
143 IN UINTN *SectionInstance,
144 IN EFI_GUID *SectionDefinitionGuid,
145 OUT CORE_SECTION_CHILD_NODE **FoundChild,
146 OUT CORE_SECTION_STREAM_NODE **FoundStream,
147 OUT UINT32 *AuthenticationStatus
148 );
149
150 STATIC
151 EFI_STATUS
152 CreateChildNode (
153 IN CORE_SECTION_STREAM_NODE *Stream,
154 IN UINT32 ChildOffset,
155 OUT CORE_SECTION_CHILD_NODE **ChildNode
156 );
157
158 STATIC
159 VOID
160 FreeChildNode (
161 IN CORE_SECTION_CHILD_NODE *ChildNode
162 );
163
164 STATIC
165 EFI_STATUS
166 OpenSectionStreamEx (
167 IN UINTN SectionStreamLength,
168 IN VOID *SectionStream,
169 IN BOOLEAN AllocateBuffer,
170 IN UINT32 AuthenticationStatus,
171 OUT UINTN *SectionStreamHandle
172 );
173
174 STATIC
175 BOOLEAN
176 IsValidSectionStream (
177 IN VOID *SectionStream,
178 IN UINTN SectionStreamLength
179 );
180
181 EFI_STATUS
182 EFIAPI
183 CustomGuidedSectionExtract (
184 IN CONST EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL *This,
185 IN CONST VOID *InputSection,
186 OUT VOID **OutputBuffer,
187 OUT UINTN *OutputSize,
188 OUT UINT32 *AuthenticationStatus
189 );
190 //
191 // Module globals
192 //
193 LIST_ENTRY mStreamRoot = INITIALIZE_LIST_HEAD_VARIABLE (mStreamRoot);
194
195 EFI_HANDLE mSectionExtractionHandle = NULL;
196
197 EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL mCustomGuidedSectionExtractionProtocol = {
198 CustomGuidedSectionExtract
199 };
200
201 EFI_STATUS
202 EFIAPI
203 InitializeSectionExtraction (
204 IN EFI_HANDLE ImageHandle,
205 IN EFI_SYSTEM_TABLE *SystemTable
206 )
207 /*++
208
209 Routine Description:
210 Entry point of the section extraction code. Initializes an instance of the
211 section extraction interface and installs it on a new handle.
212
213 Arguments:
214 ImageHandle EFI_HANDLE: A handle for the image that is initializing this driver
215 SystemTable EFI_SYSTEM_TABLE: A pointer to the EFI system table
216
217 Returns:
218 EFI_SUCCESS: Driver initialized successfully
219 EFI_OUT_OF_RESOURCES: Could not allocate needed resources
220
221 --*/
222 {
223 EFI_STATUS Status;
224 EFI_GUID *ExtractHandlerGuidTable;
225 UINTN ExtractHandlerNumber;
226
227 //
228 // Get custom extract guided section method guid list
229 //
230 ExtractHandlerNumber = ExtractGuidedSectionGetGuidList (&ExtractHandlerGuidTable);
231
232 Status = EFI_SUCCESS;
233 //
234 // Install custom guided extraction protocol
235 //
236 while (ExtractHandlerNumber-- > 0) {
237 Status = CoreInstallProtocolInterface (
238 &mSectionExtractionHandle,
239 &ExtractHandlerGuidTable [ExtractHandlerNumber],
240 EFI_NATIVE_INTERFACE,
241 &mCustomGuidedSectionExtractionProtocol
242 );
243 ASSERT_EFI_ERROR (Status);
244 }
245
246 return Status;
247 }
248
249 EFI_STATUS
250 EFIAPI
251 OpenSectionStream (
252 IN UINTN SectionStreamLength,
253 IN VOID *SectionStream,
254 OUT UINTN *SectionStreamHandle
255 )
256 /*++
257
258 Routine Description:
259 SEP member function. This function creates and returns a new section stream
260 handle to represent the new section stream.
261
262 Arguments:
263 This - Indicates the calling context.
264 SectionStreamLength - Size in bytes of the section stream.
265 SectionStream - Buffer containing the new section stream.
266 SectionStreamHandle - A pointer to a caller allocated UINTN that on output
267 contains the new section stream handle.
268
269 Returns:
270 EFI_SUCCESS
271 EFI_OUT_OF_RESOURCES - memory allocation failed.
272 EFI_INVALID_PARAMETER - section stream does not end concident with end of
273 last section.
274
275 --*/
276 {
277 //
278 // Check to see section stream looks good...
279 //
280 if (!IsValidSectionStream (SectionStream, SectionStreamLength)) {
281 return EFI_INVALID_PARAMETER;
282 }
283
284 return OpenSectionStreamEx (
285 SectionStreamLength,
286 SectionStream,
287 TRUE,
288 0,
289 SectionStreamHandle
290 );
291 }
292
293 EFI_STATUS
294 EFIAPI
295 GetSection (
296 IN UINTN SectionStreamHandle,
297 IN EFI_SECTION_TYPE *SectionType,
298 IN EFI_GUID *SectionDefinitionGuid,
299 IN UINTN SectionInstance,
300 IN VOID **Buffer,
301 IN OUT UINTN *BufferSize,
302 OUT UINT32 *AuthenticationStatus
303 )
304 /*++
305
306 Routine Description:
307 SEP member function. Retrieves requested section from section stream.
308
309 Arguments:
310 SectionStreamHandle: The section stream from which to extract the requested
311 section.
312 SectionType: A pointer to the type of section to search for.
313 SectionDefinitionGuid: If the section type is EFI_SECTION_GUID_DEFINED, then
314 SectionDefinitionGuid indicates which of these types
315 of sections to search for.
316 SectionInstance: Indicates which instance of the requested section to
317 return.
318 Buffer: Double indirection to buffer. If *Buffer is non-null on
319 input, then the buffer is caller allocated. If
320 *Buffer is NULL, then the buffer is callee allocated.
321 In either case, the requried buffer size is returned
322 in *BufferSize.
323 BufferSize: On input, indicates the size of *Buffer if *Buffer is
324 non-null on input. On output, indicates the required
325 size (allocated size if callee allocated) of *Buffer.
326 AuthenticationStatus: Indicates the authentication status of the retrieved
327 section.
328
329 Returns:
330 EFI_SUCCESS: Section was retrieved successfully
331 EFI_PROTOCOL_ERROR: A GUID defined section was encountered in the section
332 stream with its EFI_GUIDED_SECTION_PROCESSING_REQUIRED
333 bit set, but there was no corresponding GUIDed Section
334 Extraction Protocol in the handle database. *Buffer is
335 unmodified.
336 EFI_NOT_FOUND: An error was encountered when parsing the SectionStream.
337 This indicates the SectionStream is not correctly
338 formatted.
339 EFI_NOT_FOUND: The requested section does not exist.
340 EFI_OUT_OF_RESOURCES: The system has insufficient resources to process the
341 request.
342 EFI_INVALID_PARAMETER: The SectionStreamHandle does not exist.
343 EFI_WARN_TOO_SMALL: The size of the caller allocated input buffer is
344 insufficient to contain the requested section. The
345 input buffer is filled and contents are section contents
346 are truncated.
347
348 --*/
349 {
350 CORE_SECTION_STREAM_NODE *StreamNode;
351 EFI_TPL OldTpl;
352 EFI_STATUS Status;
353 CORE_SECTION_CHILD_NODE *ChildNode;
354 CORE_SECTION_STREAM_NODE *ChildStreamNode;
355 UINTN CopySize;
356 UINT32 ExtractedAuthenticationStatus;
357 UINTN Instance;
358 UINT8 *CopyBuffer;
359 UINTN SectionSize;
360
361
362 OldTpl = CoreRaiseTpl (TPL_NOTIFY);
363 Instance = SectionInstance + 1;
364
365 //
366 // Locate target stream
367 //
368 Status = FindStreamNode (SectionStreamHandle, &StreamNode);
369 if (EFI_ERROR (Status)) {
370 Status = EFI_INVALID_PARAMETER;
371 goto GetSection_Done;
372 }
373
374 //
375 // Found the stream, now locate and return the appropriate section
376 //
377 if (SectionType == NULL) {
378 //
379 // SectionType == NULL means return the WHOLE section stream...
380 //
381 CopySize = StreamNode->StreamLength;
382 CopyBuffer = StreamNode->StreamBuffer;
383 *AuthenticationStatus = StreamNode->AuthenticationStatus;
384 } else {
385 //
386 // There's a requested section type, so go find it and return it...
387 //
388 Status = FindChildNode (
389 StreamNode,
390 *SectionType,
391 &Instance,
392 SectionDefinitionGuid,
393 &ChildNode,
394 &ChildStreamNode,
395 &ExtractedAuthenticationStatus
396 );
397 if (EFI_ERROR (Status)) {
398 goto GetSection_Done;
399 }
400 CopySize = ChildNode->Size - sizeof (EFI_COMMON_SECTION_HEADER);
401 CopyBuffer = ChildStreamNode->StreamBuffer + ChildNode->OffsetInStream + sizeof (EFI_COMMON_SECTION_HEADER);
402 *AuthenticationStatus = ExtractedAuthenticationStatus;
403 }
404
405 SectionSize = CopySize;
406 if (*Buffer != NULL) {
407 //
408 // Caller allocated buffer. Fill to size and return required size...
409 //
410 if (*BufferSize < CopySize) {
411 Status = EFI_WARN_BUFFER_TOO_SMALL;
412 CopySize = *BufferSize;
413 }
414 } else {
415 //
416 // Callee allocated buffer. Allocate buffer and return size.
417 //
418 *Buffer = CoreAllocateBootServicesPool (CopySize);
419 if (*Buffer == NULL) {
420 Status = EFI_OUT_OF_RESOURCES;
421 goto GetSection_Done;
422 }
423 }
424 CopyMem (*Buffer, CopyBuffer, CopySize);
425 *BufferSize = SectionSize;
426
427 GetSection_Done:
428 CoreRestoreTpl (OldTpl);
429 return Status;
430 }
431
432
433 EFI_STATUS
434 EFIAPI
435 CloseSectionStream (
436 IN UINTN StreamHandleToClose
437 )
438 /*++
439
440 Routine Description:
441 SEP member function. Deletes an existing section stream
442
443 Arguments:
444 This - Indicates the calling context.
445 StreamHandleToClose - Indicates the stream to close
446
447 Returns:
448 EFI_SUCCESS
449 EFI_OUT_OF_RESOURCES - memory allocation failed.
450 EFI_INVALID_PARAMETER - section stream does not end concident with end of
451 last section.
452
453 --*/
454 {
455 CORE_SECTION_STREAM_NODE *StreamNode;
456 EFI_TPL OldTpl;
457 EFI_STATUS Status;
458 LIST_ENTRY *Link;
459 CORE_SECTION_CHILD_NODE *ChildNode;
460
461 OldTpl = CoreRaiseTpl (TPL_NOTIFY);
462
463 //
464 // Locate target stream
465 //
466 Status = FindStreamNode (StreamHandleToClose, &StreamNode);
467 if (!EFI_ERROR (Status)) {
468 //
469 // Found the stream, so close it
470 //
471 RemoveEntryList (&StreamNode->Link);
472 while (!IsListEmpty (&StreamNode->Children)) {
473 Link = GetFirstNode (&StreamNode->Children);
474 ChildNode = CHILD_SECTION_NODE_FROM_LINK (Link);
475 FreeChildNode (ChildNode);
476 }
477 CoreFreePool (StreamNode->StreamBuffer);
478 CoreFreePool (StreamNode);
479 Status = EFI_SUCCESS;
480 } else {
481 Status = EFI_INVALID_PARAMETER;
482 }
483
484 CoreRestoreTpl (OldTpl);
485 return Status;
486 }
487
488
489 STATIC
490 BOOLEAN
491 ChildIsType (
492 IN CORE_SECTION_STREAM_NODE *Stream,
493 IN CORE_SECTION_CHILD_NODE *Child,
494 IN EFI_SECTION_TYPE SearchType,
495 IN EFI_GUID *SectionDefinitionGuid
496 )
497 /*++
498
499 Routine Description:
500 Worker function. Determine if the input stream:child matches the input type.
501
502 Arguments:
503 Stream - Indicates the section stream associated with the child
504 Child - Indicates the child to check
505 SearchType - Indicates the type of section to check against for
506 SectionDefinitionGuid - Indicates the GUID to check against if the type is
507 EFI_SECTION_GUID_DEFINED
508 Returns:
509 TRUE - The child matches
510 FALSE - The child doesn't match
511
512 --*/
513 {
514 EFI_GUID_DEFINED_SECTION *GuidedSection;
515
516 if (SearchType == EFI_SECTION_ALL) {
517 return TRUE;
518 }
519 if (Child->Type != SearchType) {
520 return FALSE;
521 }
522 if (SearchType != EFI_SECTION_GUID_DEFINED) {
523 return TRUE;
524 }
525 GuidedSection = (EFI_GUID_DEFINED_SECTION * )(Stream->StreamBuffer + Child->OffsetInStream);
526 return CompareGuid (&GuidedSection->SectionDefinitionGuid, SectionDefinitionGuid);
527 }
528
529
530 STATIC
531 EFI_STATUS
532 FindChildNode (
533 IN CORE_SECTION_STREAM_NODE *SourceStream,
534 IN EFI_SECTION_TYPE SearchType,
535 IN OUT UINTN *SectionInstance,
536 IN EFI_GUID *SectionDefinitionGuid,
537 OUT CORE_SECTION_CHILD_NODE **FoundChild,
538 OUT CORE_SECTION_STREAM_NODE **FoundStream,
539 OUT UINT32 *AuthenticationStatus
540 )
541 /*++
542
543 Routine Description:
544 Worker function Recursively searches / builds section stream database
545 looking for requested section.
546
547 Arguments:
548 SourceStream - Indicates the section stream in which to do the search.
549 SearchType - Indicates the type of section to search for.
550 SectionInstance - Indicates which instance of section to find. This is
551 an in/out parameter to deal with recursions.
552 SectionDefinitionGuid - Guid of section definition
553 FoundChild - Output indicating the child node that is found.
554 FoundStream - Output indicating which section stream the child was
555 found in. If this stream was generated as a result of
556 an encapsulation section, the streamhandle is visible
557 within the SEP driver only.
558 AuthenticationStatus- Indicates the authentication status of the found section.
559
560 Returns:
561 EFI_SUCCESS - Child node was found and returned.
562 EFI_OUT_OF_RESOURCES- Memory allocation failed.
563 EFI_NOT_FOUND - Requested child node does not exist.
564 EFI_PROTOCOL_ERROR - a required GUIDED section extraction protocol does not
565 exist
566
567 --*/
568 {
569 CORE_SECTION_CHILD_NODE *CurrentChildNode;
570 CORE_SECTION_CHILD_NODE *RecursedChildNode;
571 CORE_SECTION_STREAM_NODE *RecursedFoundStream;
572 UINT32 NextChildOffset;
573 EFI_STATUS ErrorStatus;
574 EFI_STATUS Status;
575
576 CurrentChildNode = NULL;
577 ErrorStatus = EFI_NOT_FOUND;
578
579 if (SourceStream->StreamLength == 0) {
580 return EFI_NOT_FOUND;
581 }
582
583 if (IsListEmpty (&SourceStream->Children) &&
584 SourceStream->StreamLength > sizeof (EFI_COMMON_SECTION_HEADER)) {
585 //
586 // This occurs when a section stream exists, but no child sections
587 // have been parsed out yet. Therefore, extract the first child and add it
588 // to the list of children so we can get started.
589 //
590 Status = CreateChildNode (SourceStream, 0, &CurrentChildNode);
591 if (EFI_ERROR (Status)) {
592 return Status;
593 }
594 }
595
596 //
597 // At least one child has been parsed out of the section stream. So, walk
598 // through the sections that have already been parsed out looking for the
599 // requested section, if necessary, continue parsing section stream and
600 // adding children until either the requested section is found, or we run
601 // out of data
602 //
603 CurrentChildNode = CHILD_SECTION_NODE_FROM_LINK (GetFirstNode(&SourceStream->Children));
604
605 for (;;) {
606 if (ChildIsType (SourceStream, CurrentChildNode, SearchType, SectionDefinitionGuid)) {
607 //
608 // The type matches, so check the instance count to see if it's the one we want
609 //
610 (*SectionInstance)--;
611 if (*SectionInstance == 0) {
612 //
613 // Got it!
614 //
615 *FoundChild = CurrentChildNode;
616 *FoundStream = SourceStream;
617 *AuthenticationStatus = SourceStream->AuthenticationStatus;
618 return EFI_SUCCESS;
619 }
620 }
621
622 if (CurrentChildNode->EncapsulatedStreamHandle != NULL_STREAM_HANDLE) {
623 //
624 // If the current node is an encapsulating node, recurse into it...
625 //
626 Status = FindChildNode (
627 (CORE_SECTION_STREAM_NODE *)CurrentChildNode->EncapsulatedStreamHandle,
628 SearchType,
629 SectionInstance,
630 SectionDefinitionGuid,
631 &RecursedChildNode,
632 &RecursedFoundStream,
633 AuthenticationStatus
634 );
635 //
636 // If the status is not EFI_SUCCESS, just save the error code and continue
637 // to find the request child node in the rest stream.
638 //
639 if (*SectionInstance == 0) {
640 ASSERT_EFI_ERROR (Status);
641 *FoundChild = RecursedChildNode;
642 *FoundStream = RecursedFoundStream;
643 return EFI_SUCCESS;
644 } else {
645 ErrorStatus = Status;
646 }
647 }
648
649 if (!IsNodeAtEnd (&SourceStream->Children, &CurrentChildNode->Link)) {
650 //
651 // We haven't found the child node we're interested in yet, but there's
652 // still more nodes that have already been parsed so get the next one
653 // and continue searching..
654 //
655 CurrentChildNode = CHILD_SECTION_NODE_FROM_LINK (GetNextNode (&SourceStream->Children, &CurrentChildNode->Link));
656 } else {
657 //
658 // We've exhausted children that have already been parsed, so see if
659 // there's any more data and continue parsing out more children if there
660 // is.
661 //
662 NextChildOffset = CurrentChildNode->OffsetInStream + CurrentChildNode->Size;
663 //
664 // Round up to 4 byte boundary
665 //
666 NextChildOffset += 3;
667 NextChildOffset &= ~(UINTN)3;
668 if (NextChildOffset <= SourceStream->StreamLength - sizeof (EFI_COMMON_SECTION_HEADER)) {
669 //
670 // There's an unparsed child remaining in the stream, so create a new child node
671 //
672 Status = CreateChildNode (SourceStream, NextChildOffset, &CurrentChildNode);
673 if (EFI_ERROR (Status)) {
674 return Status;
675 }
676 } else {
677 ASSERT (EFI_ERROR (ErrorStatus));
678 return ErrorStatus;
679 }
680 }
681 }
682 }
683
684
685 STATIC
686 EFI_STATUS
687 CreateChildNode (
688 IN CORE_SECTION_STREAM_NODE *Stream,
689 IN UINT32 ChildOffset,
690 OUT CORE_SECTION_CHILD_NODE **ChildNode
691 )
692 /*++
693
694 Routine Description:
695 Worker function. Constructor for new child nodes.
696
697 Arguments:
698 Stream - Indicates the section stream in which to add the child.
699 ChildOffset - Indicates the offset in Stream that is the beginning
700 of the child section.
701 ChildNode - Indicates the Callee allocated and initialized child.
702
703 Returns:
704 EFI_SUCCESS - Child node was found and returned.
705 EFI_OUT_OF_RESOURCES- Memory allocation failed.
706 EFI_PROTOCOL_ERROR - Encapsulation sections produce new stream handles when
707 the child node is created. If the section type is GUID
708 defined, and the extraction GUID does not exist, and
709 producing the stream requires the GUID, then a protocol
710 error is generated and no child is produced.
711 Values returned by OpenSectionStreamEx.
712
713 --*/
714 {
715 EFI_STATUS Status;
716 EFI_COMMON_SECTION_HEADER *SectionHeader;
717 EFI_COMPRESSION_SECTION *CompressionHeader;
718 EFI_GUID_DEFINED_SECTION *GuidedHeader;
719 EFI_DECOMPRESS_PROTOCOL *Decompress;
720 EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL *GuidedExtraction;
721 VOID *NewStreamBuffer;
722 VOID *ScratchBuffer;
723 UINT32 ScratchSize;
724 UINTN NewStreamBufferSize;
725 UINT32 AuthenticationStatus;
726 UINT32 SectionLength;
727
728 CORE_SECTION_CHILD_NODE *Node;
729
730 SectionHeader = (EFI_COMMON_SECTION_HEADER *) (Stream->StreamBuffer + ChildOffset);
731
732 //
733 // Allocate a new node
734 //
735 *ChildNode = CoreAllocateBootServicesPool (sizeof (CORE_SECTION_CHILD_NODE));
736 Node = *ChildNode;
737 if (Node == NULL) {
738 return EFI_OUT_OF_RESOURCES;
739 }
740
741 //
742 // Now initialize it
743 //
744 Node->Signature = CORE_SECTION_CHILD_SIGNATURE;
745 Node->Type = SectionHeader->Type;
746 Node->Size = SECTION_SIZE (SectionHeader);
747 Node->OffsetInStream = ChildOffset;
748 Node->EncapsulatedStreamHandle = NULL_STREAM_HANDLE;
749 Node->EncapsulationGuid = NULL;
750
751 //
752 // If it's an encapsulating section, then create the new section stream also
753 //
754 switch (Node->Type) {
755 case EFI_SECTION_COMPRESSION:
756 //
757 // Get the CompressionSectionHeader
758 //
759 ASSERT (Node->Size >= sizeof (EFI_COMPRESSION_SECTION));
760
761 CompressionHeader = (EFI_COMPRESSION_SECTION *) SectionHeader;
762
763 //
764 // Allocate space for the new stream
765 //
766 if (CompressionHeader->UncompressedLength > 0) {
767 NewStreamBufferSize = CompressionHeader->UncompressedLength;
768 NewStreamBuffer = CoreAllocateBootServicesPool (NewStreamBufferSize);
769 if (NewStreamBuffer == NULL) {
770 CoreFreePool (Node);
771 return EFI_OUT_OF_RESOURCES;
772 }
773
774 if (CompressionHeader->CompressionType == EFI_NOT_COMPRESSED) {
775 //
776 // stream is not actually compressed, just encapsulated. So just copy it.
777 //
778 CopyMem (NewStreamBuffer, CompressionHeader + 1, NewStreamBufferSize);
779 } else if (CompressionHeader->CompressionType == EFI_STANDARD_COMPRESSION) {
780 //
781 // Only support the EFI_SATNDARD_COMPRESSION algorithm.
782 //
783
784 //
785 // Decompress the stream
786 //
787 Status = CoreLocateProtocol (&gEfiDecompressProtocolGuid, NULL, (VOID **)&Decompress);
788
789 ASSERT_EFI_ERROR (Status);
790
791 Status = Decompress->GetInfo (
792 Decompress,
793 CompressionHeader + 1,
794 Node->Size - sizeof (EFI_COMPRESSION_SECTION),
795 (UINT32 *)&NewStreamBufferSize,
796 &ScratchSize
797 );
798 ASSERT_EFI_ERROR (Status);
799 ASSERT (NewStreamBufferSize == CompressionHeader->UncompressedLength);
800
801 ScratchBuffer = CoreAllocateBootServicesPool (ScratchSize);
802 if (ScratchBuffer == NULL) {
803 CoreFreePool (Node);
804 CoreFreePool (NewStreamBuffer);
805 return EFI_OUT_OF_RESOURCES;
806 }
807
808 Status = Decompress->Decompress (
809 Decompress,
810 CompressionHeader + 1,
811 Node->Size - sizeof (EFI_COMPRESSION_SECTION),
812 NewStreamBuffer,
813 (UINT32)NewStreamBufferSize,
814 ScratchBuffer,
815 ScratchSize
816 );
817 ASSERT_EFI_ERROR (Status);
818 CoreFreePool (ScratchBuffer);
819 }
820 } else {
821 NewStreamBuffer = NULL;
822 NewStreamBufferSize = 0;
823 }
824
825 Status = OpenSectionStreamEx (
826 NewStreamBufferSize,
827 NewStreamBuffer,
828 FALSE,
829 Stream->AuthenticationStatus,
830 &Node->EncapsulatedStreamHandle
831 );
832 if (EFI_ERROR (Status)) {
833 CoreFreePool (Node);
834 CoreFreePool (NewStreamBuffer);
835 return Status;
836 }
837 break;
838
839 case EFI_SECTION_GUID_DEFINED:
840 GuidedHeader = (EFI_GUID_DEFINED_SECTION *) SectionHeader;
841 Node->EncapsulationGuid = &GuidedHeader->SectionDefinitionGuid;
842 Status = CoreLocateProtocol (Node->EncapsulationGuid, NULL, (VOID **)&GuidedExtraction);
843 if (!EFI_ERROR (Status)) {
844 //
845 // NewStreamBuffer is always allocated by ExtractSection... No caller
846 // allocation here.
847 //
848 Status = GuidedExtraction->ExtractSection (
849 GuidedExtraction,
850 GuidedHeader,
851 &NewStreamBuffer,
852 &NewStreamBufferSize,
853 &AuthenticationStatus
854 );
855 if (EFI_ERROR (Status)) {
856 CoreFreePool (*ChildNode);
857 return EFI_PROTOCOL_ERROR;
858 }
859
860 //
861 // Make sure we initialize the new stream with the correct
862 // authentication status for both aggregate and local status fields.
863 //
864 if (GuidedHeader->Attributes & EFI_GUIDED_SECTION_AUTH_STATUS_VALID) {
865 //
866 // OR in the parent stream's aggregate status.
867 //
868 AuthenticationStatus |= Stream->AuthenticationStatus & EFI_AUTH_STATUS_ALL;
869 } else {
870 //
871 // since there's no authentication data contributed by the section,
872 // just inherit the full value from our immediate parent.
873 //
874 AuthenticationStatus = Stream->AuthenticationStatus;
875 }
876
877 Status = OpenSectionStreamEx (
878 NewStreamBufferSize,
879 NewStreamBuffer,
880 FALSE,
881 AuthenticationStatus,
882 &Node->EncapsulatedStreamHandle
883 );
884 if (EFI_ERROR (Status)) {
885 CoreFreePool (*ChildNode);
886 CoreFreePool (NewStreamBuffer);
887 return Status;
888 }
889 } else {
890 //
891 // There's no GUIDed section extraction protocol available.
892 //
893 if (GuidedHeader->Attributes & EFI_GUIDED_SECTION_PROCESSING_REQUIRED) {
894 //
895 // If the section REQUIRES an extraction protocol, then we're toast
896 //
897 CoreFreePool (*ChildNode);
898 return EFI_PROTOCOL_ERROR;
899 }
900
901 //
902 // Figure out the proper authentication status
903 //
904 AuthenticationStatus = Stream->AuthenticationStatus;
905
906 SectionLength = SECTION_SIZE (GuidedHeader);
907 Status = OpenSectionStreamEx (
908 SectionLength - GuidedHeader->DataOffset,
909 (UINT8 *) GuidedHeader + GuidedHeader->DataOffset,
910 TRUE,
911 AuthenticationStatus,
912 &Node->EncapsulatedStreamHandle
913 );
914 if (EFI_ERROR (Status)) {
915 CoreFreePool (Node);
916 return Status;
917 }
918 }
919
920 break;
921
922 default:
923
924 //
925 // Nothing to do if it's a leaf
926 //
927 break;
928 }
929
930 //
931 // Last, add the new child node to the stream
932 //
933 InsertTailList (&Stream->Children, &Node->Link);
934
935 return EFI_SUCCESS;
936 }
937
938
939 #if 0
940 STATIC
941 VOID
942 CreateGuidedExtractionRpnEvent (
943 IN CORE_SECTION_STREAM_NODE *ParentStream,
944 IN CORE_SECTION_CHILD_NODE *ChildNode
945 )
946 /*++
947
948 Routine Description:
949 Worker function. Constructor for RPN event if needed to keep AuthenticationStatus
950 cache correct when a missing GUIDED_SECTION_EXTRACTION_PROTOCOL appears...
951
952 Arguments:
953 ParentStream - Indicates the parent of the ecnapsulation section (child)
954 ChildNode - Indicates the child node that is the encapsulation section.
955
956 Returns:
957 None
958
959 --*/
960 {
961 RPN_EVENT_CONTEXT *Context;
962
963 //
964 // Allocate new event structure and context
965 //
966 Context = CoreAllocateBootServicesPool (sizeof (RPN_EVENT_CONTEXT));
967 ASSERT (Context != NULL);
968
969 Context->ChildNode = ChildNode;
970 Context->ParentStream = ParentStream;
971
972 Context->Event = CoreCreateProtocolNotifyEvent (
973 Context->ChildNode->EncapsulationGuid,
974 TPL_NOTIFY,
975 NotifyGuidedExtraction,
976 Context,
977 &Context->Registration,
978 FALSE
979 );
980 }
981 #endif
982
983
984 #if 0
985 STATIC
986 VOID
987 EFIAPI
988 NotifyGuidedExtraction (
989 IN EFI_EVENT Event,
990 IN VOID *RpnContext
991 )
992 /*++
993
994 Routine Description:
995 RPN callback function. Removes a stale section stream and re-initializes it
996 with an updated AuthenticationStatus.
997
998 Arguments:
999 Event - The event that fired
1000 RpnContext - A pointer to the context that allows us to identify
1001 the relevent encapsulation...
1002
1003 Returns:
1004 None
1005
1006 --*/
1007 {
1008 EFI_STATUS Status;
1009 EFI_GUID_DEFINED_SECTION *GuidedHeader;
1010 EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL *GuidedExtraction;
1011 VOID *NewStreamBuffer;
1012 UINTN NewStreamBufferSize;
1013 UINT32 AuthenticationStatus;
1014 RPN_EVENT_CONTEXT *Context;
1015
1016 Context = RpnContext;
1017
1018 Status = CloseSectionStream (Context->ChildNode->EncapsulatedStreamHandle);
1019 if (!EFI_ERROR (Status)) {
1020 //
1021 // The stream closed successfully, so re-open the stream with correct AuthenticationStatus
1022 //
1023
1024 GuidedHeader = (EFI_GUID_DEFINED_SECTION *)
1025 (Context->ParentStream->StreamBuffer + Context->ChildNode->OffsetInStream);
1026 ASSERT (GuidedHeader->CommonHeader.Type == EFI_SECTION_GUID_DEFINED);
1027
1028 Status = CoreLocateProtocol (Context->ChildNode->EncapsulationGuid, NULL, (VOID **)&GuidedExtraction);
1029 ASSERT_EFI_ERROR (Status);
1030
1031
1032 Status = GuidedExtraction->ExtractSection (
1033 GuidedExtraction,
1034 GuidedHeader,
1035 &NewStreamBuffer,
1036 &NewStreamBufferSize,
1037 &AuthenticationStatus
1038 );
1039 ASSERT_EFI_ERROR (Status);
1040 //
1041 // OR in the parent stream's aggregagate status.
1042 //
1043 AuthenticationStatus |= Context->ParentStream->AuthenticationStatus & EFI_AUTH_STATUS_ALL;
1044 Status = OpenSectionStreamEx (
1045 NewStreamBufferSize,
1046 NewStreamBuffer,
1047 FALSE,
1048 AuthenticationStatus,
1049 &Context->ChildNode->EncapsulatedStreamHandle
1050 );
1051 ASSERT_EFI_ERROR (Status);
1052 }
1053
1054 //
1055 // If above, the stream did not close successfully, it indicates it's
1056 // alread been closed by someone, so just destroy the event and be done with
1057 // it.
1058 //
1059
1060 CoreCloseEvent (Event);
1061 CoreFreePool (Context);
1062 }
1063 #endif
1064
1065
1066 STATIC
1067 VOID
1068 FreeChildNode (
1069 IN CORE_SECTION_CHILD_NODE *ChildNode
1070 )
1071 /*++
1072
1073 Routine Description:
1074 Worker function. Destructor for child nodes.
1075
1076 Arguments:
1077 ChildNode - Indicates the node to destroy
1078
1079 Returns:
1080 none
1081
1082 --*/
1083 {
1084 ASSERT (ChildNode->Signature == CORE_SECTION_CHILD_SIGNATURE);
1085 //
1086 // Remove the child from it's list
1087 //
1088 RemoveEntryList (&ChildNode->Link);
1089
1090 if (ChildNode->EncapsulatedStreamHandle != NULL_STREAM_HANDLE) {
1091 //
1092 // If it's an encapsulating section, we close the resulting section stream.
1093 // CloseSectionStream will free all memory associated with the stream.
1094 //
1095 CloseSectionStream (ChildNode->EncapsulatedStreamHandle);
1096 }
1097 //
1098 // Last, free the child node itself
1099 //
1100 CoreFreePool (ChildNode);
1101 }
1102
1103
1104 STATIC
1105 EFI_STATUS
1106 OpenSectionStreamEx (
1107 IN UINTN SectionStreamLength,
1108 IN VOID *SectionStream,
1109 IN BOOLEAN AllocateBuffer,
1110 IN UINT32 AuthenticationStatus,
1111 OUT UINTN *SectionStreamHandle
1112 )
1113 /*++
1114
1115 Routine Description:
1116 Worker function. Constructor for section streams.
1117
1118 Arguments:
1119 SectionStreamLength - Size in bytes of the section stream.
1120 SectionStream - Buffer containing the new section stream.
1121 AllocateBuffer - Indicates whether the stream buffer is to be copied
1122 or the input buffer is to be used in place.
1123 AuthenticationStatus- Indicates the default authentication status for the
1124 new stream.
1125 SectionStreamHandle - A pointer to a caller allocated section stream handle.
1126
1127 Returns:
1128 EFI_SUCCESS - Stream was added to stream database.
1129 EFI_OUT_OF_RESOURCES - memory allocation failed.
1130
1131 --*/
1132 {
1133 CORE_SECTION_STREAM_NODE *NewStream;
1134 EFI_TPL OldTpl;
1135
1136 //
1137 // Allocate a new stream
1138 //
1139 NewStream = CoreAllocateBootServicesPool (sizeof (CORE_SECTION_STREAM_NODE));
1140 if (NewStream == NULL) {
1141 return EFI_OUT_OF_RESOURCES;
1142 }
1143
1144 if (AllocateBuffer) {
1145 //
1146 // if we're here, we're double buffering, allocate the buffer and copy the
1147 // data in
1148 //
1149 if (SectionStreamLength > 0) {
1150 NewStream->StreamBuffer = CoreAllocateBootServicesPool (SectionStreamLength);
1151 if (NewStream->StreamBuffer == NULL) {
1152 CoreFreePool (NewStream);
1153 return EFI_OUT_OF_RESOURCES;
1154 }
1155 //
1156 // Copy in stream data
1157 //
1158 CopyMem (NewStream->StreamBuffer, SectionStream, SectionStreamLength);
1159 } else {
1160 //
1161 // It's possible to have a zero length section stream.
1162 //
1163 NewStream->StreamBuffer = NULL;
1164 }
1165 } else {
1166 //
1167 // If were here, the caller has supplied the buffer (it's an internal call)
1168 // so just assign the buffer. This happens when we open section streams
1169 // as a result of expanding an encapsulating section.
1170 //
1171 NewStream->StreamBuffer = SectionStream;
1172 }
1173
1174 //
1175 // Initialize the rest of the section stream
1176 //
1177 NewStream->Signature = CORE_SECTION_STREAM_SIGNATURE;
1178 NewStream->StreamHandle = (UINTN) NewStream;
1179 NewStream->StreamLength = SectionStreamLength;
1180 InitializeListHead (&NewStream->Children);
1181 NewStream->AuthenticationStatus = AuthenticationStatus;
1182
1183 //
1184 // Add new stream to stream list
1185 //
1186 OldTpl = CoreRaiseTpl (TPL_NOTIFY);
1187 InsertTailList (&mStreamRoot, &NewStream->Link);
1188 CoreRestoreTpl (OldTpl);
1189
1190 *SectionStreamHandle = NewStream->StreamHandle;
1191
1192 return EFI_SUCCESS;
1193 }
1194
1195
1196 STATIC
1197 EFI_STATUS
1198 FindStreamNode (
1199 IN UINTN SearchHandle,
1200 OUT CORE_SECTION_STREAM_NODE **FoundStream
1201 )
1202 /*++
1203
1204 Routine Description:
1205 Worker function. Search stream database for requested stream handle.
1206
1207 Arguments:
1208 SearchHandle - Indicates which stream to look for.
1209 FoundStream - Output pointer to the found stream.
1210
1211 Returns:
1212 EFI_SUCCESS - StreamHandle was found and *FoundStream contains
1213 the stream node.
1214 EFI_NOT_FOUND - SearchHandle was not found in the stream database.
1215
1216 --*/
1217 {
1218 CORE_SECTION_STREAM_NODE *StreamNode;
1219
1220 if (!IsListEmpty (&mStreamRoot)) {
1221 StreamNode = STREAM_NODE_FROM_LINK (GetFirstNode (&mStreamRoot));
1222 for (;;) {
1223 if (StreamNode->StreamHandle == SearchHandle) {
1224 *FoundStream = StreamNode;
1225 return EFI_SUCCESS;
1226 } else if (IsNodeAtEnd (&mStreamRoot, &StreamNode->Link)) {
1227 break;
1228 } else {
1229 StreamNode = STREAM_NODE_FROM_LINK (GetNextNode (&mStreamRoot, &StreamNode->Link));
1230 }
1231 }
1232 }
1233
1234 return EFI_NOT_FOUND;
1235 }
1236
1237
1238 STATIC
1239 BOOLEAN
1240 IsValidSectionStream (
1241 IN VOID *SectionStream,
1242 IN UINTN SectionStreamLength
1243 )
1244 /*++
1245
1246 Routine Description:
1247 Check if a stream is valid.
1248
1249 Arguments:
1250 SectionStream - The section stream to be checked
1251 SectionStreamLength - The length of section stream
1252
1253 Returns:
1254 TRUE
1255 FALSE
1256
1257 --*/
1258 {
1259 UINTN TotalLength;
1260 UINTN SectionLength;
1261 EFI_COMMON_SECTION_HEADER *SectionHeader;
1262 EFI_COMMON_SECTION_HEADER *NextSectionHeader;
1263
1264 TotalLength = 0;
1265 SectionHeader = (EFI_COMMON_SECTION_HEADER *)SectionStream;
1266
1267 while (TotalLength < SectionStreamLength) {
1268 SectionLength = SECTION_SIZE (SectionHeader);
1269 TotalLength += SectionLength;
1270
1271 if (TotalLength == SectionStreamLength) {
1272 return TRUE;
1273 }
1274
1275 //
1276 // Move to the next byte following the section...
1277 //
1278 SectionHeader = (EFI_COMMON_SECTION_HEADER *) ((UINT8 *) SectionHeader + SectionLength);
1279
1280 //
1281 // Figure out where the next section begins
1282 //
1283 NextSectionHeader = (EFI_COMMON_SECTION_HEADER *) ((UINTN) SectionHeader + 3);
1284 NextSectionHeader = (EFI_COMMON_SECTION_HEADER *) ((UINTN) NextSectionHeader & ~(UINTN)3);
1285 TotalLength += (UINTN) NextSectionHeader - (UINTN) SectionHeader;
1286 SectionHeader = NextSectionHeader;
1287 }
1288
1289 ASSERT (FALSE);
1290 return FALSE;
1291 }
1292
1293 /**
1294 The ExtractSection() function processes the input section and
1295 allocates a buffer from the pool in which it returns the section
1296 contents. If the section being extracted contains
1297 authentication information (the section's
1298 GuidedSectionHeader.Attributes field has the
1299 EFI_GUIDED_SECTION_AUTH_STATUS_VALID bit set), the values
1300 returned in AuthenticationStatus must reflect the results of
1301 the authentication operation. Depending on the algorithm and
1302 size of the encapsulated data, the time that is required to do
1303 a full authentication may be prohibitively long for some
1304 classes of systems. To indicate this, use
1305 EFI_SECURITY_POLICY_PROTOCOL_GUID, which may be published by
1306 the security policy driver (see the Platform Initialization
1307 Driver Execution Environment Core Interface Specification for
1308 more details and the GUID definition). If the
1309 EFI_SECURITY_POLICY_PROTOCOL_GUID exists in the handle
1310 database, then, if possible, full authentication should be
1311 skipped and the section contents simply returned in the
1312 OutputBuffer. In this case, the
1313 EFI_AUTH_STATUS_PLATFORM_OVERRIDE bit AuthenticationStatus
1314 must be set on return. ExtractSection() is callable only from
1315 TPL_NOTIFY and below. Behavior of ExtractSection() at any
1316 EFI_TPL above TPL_NOTIFY is undefined. Type EFI_TPL is
1317 defined in RaiseTPL() in the UEFI 2.0 specification.
1318
1319
1320 @param This Indicates the
1321 EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL instance.
1322
1323 @param InputSection Buffer containing the input GUIDed section
1324 to be processed. OutputBuffer OutputBuffer
1325 is allocated from boot services pool
1326 memory and contains the new section
1327 stream. The caller is responsible for
1328 freeing this buffer.
1329
1330 @param OutputSize A pointer to a caller-allocated UINTN in
1331 which the size of OutputBuffer allocation
1332 is stored. If the function returns
1333 anything other than EFI_SUCCESS, the value
1334 of OutputSize is undefined.
1335
1336 @param AuthenticationStatus A pointer to a caller-allocated
1337 UINT32 that indicates the
1338 authentication status of the
1339 output buffer. If the input
1340 section's
1341 GuidedSectionHeader.Attributes
1342 field has the
1343 EFI_GUIDED_SECTION_AUTH_STATUS_VAL
1344 bit as clear, AuthenticationStatus
1345 must return zero. Both local bits
1346 (19:16) and aggregate bits (3:0)
1347 in AuthenticationStatus are
1348 returned by ExtractSection().
1349 These bits reflect the status of
1350 the extraction operation. The bit
1351 pattern in both regions must be
1352 the same, as the local and
1353 aggregate authentication statuses
1354 have equivalent meaning at this
1355 level. If the function returns
1356 anything other than EFI_SUCCESS,
1357 the value of AuthenticationStatus
1358 is undefined.
1359
1360
1361 @retval EFI_SUCCESS The InputSection was successfully
1362 processed and the section contents were
1363 returned.
1364
1365 @retval EFI_OUT_OF_RESOURCES The system has insufficient
1366 resources to process the
1367 request.
1368
1369 @retval EFI_INVALID_PARAMETER The GUID in InputSection does
1370 not match this instance of the
1371 GUIDed Section Extraction
1372 Protocol.
1373
1374 **/
1375 EFI_STATUS
1376 EFIAPI
1377 CustomGuidedSectionExtract (
1378 IN CONST EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL *This,
1379 IN CONST VOID *InputSection,
1380 OUT VOID **OutputBuffer,
1381 OUT UINTN *OutputSize,
1382 OUT UINT32 *AuthenticationStatus
1383 )
1384 {
1385 EFI_STATUS Status;
1386 VOID *ScratchBuffer;
1387 VOID *AllocatedOutputBuffer;
1388 UINT32 OutputBufferSize;
1389 UINT32 ScratchBufferSize;
1390 UINT16 SectionAttribute;
1391
1392 //
1393 // Init local variable
1394 //
1395 ScratchBuffer = NULL;
1396 AllocatedOutputBuffer = NULL;
1397
1398 //
1399 // Call GetInfo to get the size and attribute of input guided section data.
1400 //
1401 Status = ExtractGuidedSectionGetInfo (
1402 InputSection,
1403 &OutputBufferSize,
1404 &ScratchBufferSize,
1405 &SectionAttribute
1406 );
1407
1408 if (EFI_ERROR (Status)) {
1409 DEBUG ((EFI_D_ERROR, "GetInfo from guided section Failed - %r\n", Status));
1410 return Status;
1411 }
1412
1413 if (ScratchBufferSize != 0) {
1414 //
1415 // Allocate scratch buffer
1416 //
1417 ScratchBuffer = CoreAllocateBootServicesPool (ScratchBufferSize);
1418 if (ScratchBuffer == NULL) {
1419 return EFI_OUT_OF_RESOURCES;
1420 }
1421 }
1422
1423 if (OutputBufferSize > 0) {
1424 //
1425 // Allocate output buffer
1426 //
1427 AllocatedOutputBuffer = CoreAllocateBootServicesPool (OutputBufferSize);
1428 if (AllocatedOutputBuffer == NULL) {
1429 return EFI_OUT_OF_RESOURCES;
1430 }
1431 *OutputBuffer = AllocatedOutputBuffer;
1432 }
1433
1434 //
1435 // Call decode function to extract raw data from the guided section.
1436 //
1437 Status = ExtractGuidedSectionDecode (
1438 InputSection,
1439 OutputBuffer,
1440 ScratchBuffer,
1441 AuthenticationStatus
1442 );
1443 if (EFI_ERROR (Status)) {
1444 //
1445 // Decode failed
1446 //
1447 if (AllocatedOutputBuffer != NULL) {
1448 CoreFreePool (AllocatedOutputBuffer);
1449 }
1450 if (ScratchBuffer != NULL) {
1451 CoreFreePool (ScratchBuffer);
1452 }
1453 DEBUG ((EFI_D_ERROR, "Extract guided section Failed - %r\n", Status));
1454 return Status;
1455 }
1456
1457 if (*OutputBuffer != AllocatedOutputBuffer) {
1458 //
1459 // OutputBuffer was returned as a different value,
1460 // so copy section contents to the allocated memory buffer.
1461 //
1462 CopyMem (AllocatedOutputBuffer, *OutputBuffer, OutputBufferSize);
1463 *OutputBuffer = AllocatedOutputBuffer;
1464 }
1465
1466 //
1467 // Set real size of output buffer.
1468 //
1469 *OutputSize = (UINTN) OutputBufferSize;
1470
1471 //
1472 // Free unused scratch buffer.
1473 //
1474 if (ScratchBuffer != NULL) {
1475 CoreFreePool (ScratchBuffer);
1476 }
1477
1478 return EFI_SUCCESS;
1479 }