]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Core/Dxe/SectionExtraction/CoreSectionExtraction.c
A leaf section type may contains an array of zero or more bytes. Support the section...
[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 // Section stream may contain an array of zero or more bytes.
590 // So, its size should be >= the size of commen section header.
591 //
592 Status = CreateChildNode (SourceStream, 0, &CurrentChildNode);
593 if (EFI_ERROR (Status)) {
594 return Status;
595 }
596 }
597
598 //
599 // At least one child has been parsed out of the section stream. So, walk
600 // through the sections that have already been parsed out looking for the
601 // requested section, if necessary, continue parsing section stream and
602 // adding children until either the requested section is found, or we run
603 // out of data
604 //
605 CurrentChildNode = CHILD_SECTION_NODE_FROM_LINK (GetFirstNode(&SourceStream->Children));
606
607 for (;;) {
608 if (ChildIsType (SourceStream, CurrentChildNode, SearchType, SectionDefinitionGuid)) {
609 //
610 // The type matches, so check the instance count to see if it's the one we want
611 //
612 (*SectionInstance)--;
613 if (*SectionInstance == 0) {
614 //
615 // Got it!
616 //
617 *FoundChild = CurrentChildNode;
618 *FoundStream = SourceStream;
619 *AuthenticationStatus = SourceStream->AuthenticationStatus;
620 return EFI_SUCCESS;
621 }
622 }
623
624 if (CurrentChildNode->EncapsulatedStreamHandle != NULL_STREAM_HANDLE) {
625 //
626 // If the current node is an encapsulating node, recurse into it...
627 //
628 Status = FindChildNode (
629 (CORE_SECTION_STREAM_NODE *)CurrentChildNode->EncapsulatedStreamHandle,
630 SearchType,
631 SectionInstance,
632 SectionDefinitionGuid,
633 &RecursedChildNode,
634 &RecursedFoundStream,
635 AuthenticationStatus
636 );
637 //
638 // If the status is not EFI_SUCCESS, just save the error code and continue
639 // to find the request child node in the rest stream.
640 //
641 if (*SectionInstance == 0) {
642 ASSERT_EFI_ERROR (Status);
643 *FoundChild = RecursedChildNode;
644 *FoundStream = RecursedFoundStream;
645 return EFI_SUCCESS;
646 } else {
647 ErrorStatus = Status;
648 }
649 }
650
651 if (!IsNodeAtEnd (&SourceStream->Children, &CurrentChildNode->Link)) {
652 //
653 // We haven't found the child node we're interested in yet, but there's
654 // still more nodes that have already been parsed so get the next one
655 // and continue searching..
656 //
657 CurrentChildNode = CHILD_SECTION_NODE_FROM_LINK (GetNextNode (&SourceStream->Children, &CurrentChildNode->Link));
658 } else {
659 //
660 // We've exhausted children that have already been parsed, so see if
661 // there's any more data and continue parsing out more children if there
662 // is.
663 //
664 NextChildOffset = CurrentChildNode->OffsetInStream + CurrentChildNode->Size;
665 //
666 // Round up to 4 byte boundary
667 //
668 NextChildOffset += 3;
669 NextChildOffset &= ~(UINTN)3;
670 if (NextChildOffset <= SourceStream->StreamLength - sizeof (EFI_COMMON_SECTION_HEADER)) {
671 //
672 // There's an unparsed child remaining in the stream, so create a new child node
673 //
674 Status = CreateChildNode (SourceStream, NextChildOffset, &CurrentChildNode);
675 if (EFI_ERROR (Status)) {
676 return Status;
677 }
678 } else {
679 ASSERT (EFI_ERROR (ErrorStatus));
680 return ErrorStatus;
681 }
682 }
683 }
684 }
685
686
687 STATIC
688 EFI_STATUS
689 CreateChildNode (
690 IN CORE_SECTION_STREAM_NODE *Stream,
691 IN UINT32 ChildOffset,
692 OUT CORE_SECTION_CHILD_NODE **ChildNode
693 )
694 /*++
695
696 Routine Description:
697 Worker function. Constructor for new child nodes.
698
699 Arguments:
700 Stream - Indicates the section stream in which to add the child.
701 ChildOffset - Indicates the offset in Stream that is the beginning
702 of the child section.
703 ChildNode - Indicates the Callee allocated and initialized child.
704
705 Returns:
706 EFI_SUCCESS - Child node was found and returned.
707 EFI_OUT_OF_RESOURCES- Memory allocation failed.
708 EFI_PROTOCOL_ERROR - Encapsulation sections produce new stream handles when
709 the child node is created. If the section type is GUID
710 defined, and the extraction GUID does not exist, and
711 producing the stream requires the GUID, then a protocol
712 error is generated and no child is produced.
713 Values returned by OpenSectionStreamEx.
714
715 --*/
716 {
717 EFI_STATUS Status;
718 EFI_COMMON_SECTION_HEADER *SectionHeader;
719 EFI_COMPRESSION_SECTION *CompressionHeader;
720 EFI_GUID_DEFINED_SECTION *GuidedHeader;
721 EFI_DECOMPRESS_PROTOCOL *Decompress;
722 EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL *GuidedExtraction;
723 VOID *NewStreamBuffer;
724 VOID *ScratchBuffer;
725 UINT32 ScratchSize;
726 UINTN NewStreamBufferSize;
727 UINT32 AuthenticationStatus;
728 UINT32 SectionLength;
729
730 CORE_SECTION_CHILD_NODE *Node;
731
732 SectionHeader = (EFI_COMMON_SECTION_HEADER *) (Stream->StreamBuffer + ChildOffset);
733
734 //
735 // Allocate a new node
736 //
737 *ChildNode = CoreAllocateBootServicesPool (sizeof (CORE_SECTION_CHILD_NODE));
738 Node = *ChildNode;
739 if (Node == NULL) {
740 return EFI_OUT_OF_RESOURCES;
741 }
742
743 //
744 // Now initialize it
745 //
746 Node->Signature = CORE_SECTION_CHILD_SIGNATURE;
747 Node->Type = SectionHeader->Type;
748 Node->Size = SECTION_SIZE (SectionHeader);
749 Node->OffsetInStream = ChildOffset;
750 Node->EncapsulatedStreamHandle = NULL_STREAM_HANDLE;
751 Node->EncapsulationGuid = NULL;
752
753 //
754 // If it's an encapsulating section, then create the new section stream also
755 //
756 switch (Node->Type) {
757 case EFI_SECTION_COMPRESSION:
758 //
759 // Get the CompressionSectionHeader
760 //
761 ASSERT (Node->Size >= sizeof (EFI_COMPRESSION_SECTION));
762
763 CompressionHeader = (EFI_COMPRESSION_SECTION *) SectionHeader;
764
765 //
766 // Allocate space for the new stream
767 //
768 if (CompressionHeader->UncompressedLength > 0) {
769 NewStreamBufferSize = CompressionHeader->UncompressedLength;
770 NewStreamBuffer = CoreAllocateBootServicesPool (NewStreamBufferSize);
771 if (NewStreamBuffer == NULL) {
772 CoreFreePool (Node);
773 return EFI_OUT_OF_RESOURCES;
774 }
775
776 if (CompressionHeader->CompressionType == EFI_NOT_COMPRESSED) {
777 //
778 // stream is not actually compressed, just encapsulated. So just copy it.
779 //
780 CopyMem (NewStreamBuffer, CompressionHeader + 1, NewStreamBufferSize);
781 } else if (CompressionHeader->CompressionType == EFI_STANDARD_COMPRESSION) {
782 //
783 // Only support the EFI_SATNDARD_COMPRESSION algorithm.
784 //
785
786 //
787 // Decompress the stream
788 //
789 Status = CoreLocateProtocol (&gEfiDecompressProtocolGuid, NULL, (VOID **)&Decompress);
790
791 ASSERT_EFI_ERROR (Status);
792
793 Status = Decompress->GetInfo (
794 Decompress,
795 CompressionHeader + 1,
796 Node->Size - sizeof (EFI_COMPRESSION_SECTION),
797 (UINT32 *)&NewStreamBufferSize,
798 &ScratchSize
799 );
800 ASSERT_EFI_ERROR (Status);
801 ASSERT (NewStreamBufferSize == CompressionHeader->UncompressedLength);
802
803 ScratchBuffer = CoreAllocateBootServicesPool (ScratchSize);
804 if (ScratchBuffer == NULL) {
805 CoreFreePool (Node);
806 CoreFreePool (NewStreamBuffer);
807 return EFI_OUT_OF_RESOURCES;
808 }
809
810 Status = Decompress->Decompress (
811 Decompress,
812 CompressionHeader + 1,
813 Node->Size - sizeof (EFI_COMPRESSION_SECTION),
814 NewStreamBuffer,
815 (UINT32)NewStreamBufferSize,
816 ScratchBuffer,
817 ScratchSize
818 );
819 ASSERT_EFI_ERROR (Status);
820 CoreFreePool (ScratchBuffer);
821 }
822 } else {
823 NewStreamBuffer = NULL;
824 NewStreamBufferSize = 0;
825 }
826
827 Status = OpenSectionStreamEx (
828 NewStreamBufferSize,
829 NewStreamBuffer,
830 FALSE,
831 Stream->AuthenticationStatus,
832 &Node->EncapsulatedStreamHandle
833 );
834 if (EFI_ERROR (Status)) {
835 CoreFreePool (Node);
836 CoreFreePool (NewStreamBuffer);
837 return Status;
838 }
839 break;
840
841 case EFI_SECTION_GUID_DEFINED:
842 GuidedHeader = (EFI_GUID_DEFINED_SECTION *) SectionHeader;
843 Node->EncapsulationGuid = &GuidedHeader->SectionDefinitionGuid;
844 Status = CoreLocateProtocol (Node->EncapsulationGuid, NULL, (VOID **)&GuidedExtraction);
845 if (!EFI_ERROR (Status)) {
846 //
847 // NewStreamBuffer is always allocated by ExtractSection... No caller
848 // allocation here.
849 //
850 Status = GuidedExtraction->ExtractSection (
851 GuidedExtraction,
852 GuidedHeader,
853 &NewStreamBuffer,
854 &NewStreamBufferSize,
855 &AuthenticationStatus
856 );
857 if (EFI_ERROR (Status)) {
858 CoreFreePool (*ChildNode);
859 return EFI_PROTOCOL_ERROR;
860 }
861
862 //
863 // Make sure we initialize the new stream with the correct
864 // authentication status for both aggregate and local status fields.
865 //
866 if (GuidedHeader->Attributes & EFI_GUIDED_SECTION_AUTH_STATUS_VALID) {
867 //
868 // OR in the parent stream's aggregate status.
869 //
870 AuthenticationStatus |= Stream->AuthenticationStatus & EFI_AUTH_STATUS_ALL;
871 } else {
872 //
873 // since there's no authentication data contributed by the section,
874 // just inherit the full value from our immediate parent.
875 //
876 AuthenticationStatus = Stream->AuthenticationStatus;
877 }
878
879 Status = OpenSectionStreamEx (
880 NewStreamBufferSize,
881 NewStreamBuffer,
882 FALSE,
883 AuthenticationStatus,
884 &Node->EncapsulatedStreamHandle
885 );
886 if (EFI_ERROR (Status)) {
887 CoreFreePool (*ChildNode);
888 CoreFreePool (NewStreamBuffer);
889 return Status;
890 }
891 } else {
892 //
893 // There's no GUIDed section extraction protocol available.
894 //
895 if (GuidedHeader->Attributes & EFI_GUIDED_SECTION_PROCESSING_REQUIRED) {
896 //
897 // If the section REQUIRES an extraction protocol, then we're toast
898 //
899 CoreFreePool (*ChildNode);
900 return EFI_PROTOCOL_ERROR;
901 }
902
903 //
904 // Figure out the proper authentication status
905 //
906 AuthenticationStatus = Stream->AuthenticationStatus;
907
908 SectionLength = SECTION_SIZE (GuidedHeader);
909 Status = OpenSectionStreamEx (
910 SectionLength - GuidedHeader->DataOffset,
911 (UINT8 *) GuidedHeader + GuidedHeader->DataOffset,
912 TRUE,
913 AuthenticationStatus,
914 &Node->EncapsulatedStreamHandle
915 );
916 if (EFI_ERROR (Status)) {
917 CoreFreePool (Node);
918 return Status;
919 }
920 }
921
922 break;
923
924 default:
925
926 //
927 // Nothing to do if it's a leaf
928 //
929 break;
930 }
931
932 //
933 // Last, add the new child node to the stream
934 //
935 InsertTailList (&Stream->Children, &Node->Link);
936
937 return EFI_SUCCESS;
938 }
939
940
941 #if 0
942 STATIC
943 VOID
944 CreateGuidedExtractionRpnEvent (
945 IN CORE_SECTION_STREAM_NODE *ParentStream,
946 IN CORE_SECTION_CHILD_NODE *ChildNode
947 )
948 /*++
949
950 Routine Description:
951 Worker function. Constructor for RPN event if needed to keep AuthenticationStatus
952 cache correct when a missing GUIDED_SECTION_EXTRACTION_PROTOCOL appears...
953
954 Arguments:
955 ParentStream - Indicates the parent of the ecnapsulation section (child)
956 ChildNode - Indicates the child node that is the encapsulation section.
957
958 Returns:
959 None
960
961 --*/
962 {
963 RPN_EVENT_CONTEXT *Context;
964
965 //
966 // Allocate new event structure and context
967 //
968 Context = CoreAllocateBootServicesPool (sizeof (RPN_EVENT_CONTEXT));
969 ASSERT (Context != NULL);
970
971 Context->ChildNode = ChildNode;
972 Context->ParentStream = ParentStream;
973
974 Context->Event = CoreCreateProtocolNotifyEvent (
975 Context->ChildNode->EncapsulationGuid,
976 TPL_NOTIFY,
977 NotifyGuidedExtraction,
978 Context,
979 &Context->Registration,
980 FALSE
981 );
982 }
983 #endif
984
985
986 #if 0
987 STATIC
988 VOID
989 EFIAPI
990 NotifyGuidedExtraction (
991 IN EFI_EVENT Event,
992 IN VOID *RpnContext
993 )
994 /*++
995
996 Routine Description:
997 RPN callback function. Removes a stale section stream and re-initializes it
998 with an updated AuthenticationStatus.
999
1000 Arguments:
1001 Event - The event that fired
1002 RpnContext - A pointer to the context that allows us to identify
1003 the relevent encapsulation...
1004
1005 Returns:
1006 None
1007
1008 --*/
1009 {
1010 EFI_STATUS Status;
1011 EFI_GUID_DEFINED_SECTION *GuidedHeader;
1012 EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL *GuidedExtraction;
1013 VOID *NewStreamBuffer;
1014 UINTN NewStreamBufferSize;
1015 UINT32 AuthenticationStatus;
1016 RPN_EVENT_CONTEXT *Context;
1017
1018 Context = RpnContext;
1019
1020 Status = CloseSectionStream (Context->ChildNode->EncapsulatedStreamHandle);
1021 if (!EFI_ERROR (Status)) {
1022 //
1023 // The stream closed successfully, so re-open the stream with correct AuthenticationStatus
1024 //
1025
1026 GuidedHeader = (EFI_GUID_DEFINED_SECTION *)
1027 (Context->ParentStream->StreamBuffer + Context->ChildNode->OffsetInStream);
1028 ASSERT (GuidedHeader->CommonHeader.Type == EFI_SECTION_GUID_DEFINED);
1029
1030 Status = CoreLocateProtocol (Context->ChildNode->EncapsulationGuid, NULL, (VOID **)&GuidedExtraction);
1031 ASSERT_EFI_ERROR (Status);
1032
1033
1034 Status = GuidedExtraction->ExtractSection (
1035 GuidedExtraction,
1036 GuidedHeader,
1037 &NewStreamBuffer,
1038 &NewStreamBufferSize,
1039 &AuthenticationStatus
1040 );
1041 ASSERT_EFI_ERROR (Status);
1042 //
1043 // OR in the parent stream's aggregagate status.
1044 //
1045 AuthenticationStatus |= Context->ParentStream->AuthenticationStatus & EFI_AUTH_STATUS_ALL;
1046 Status = OpenSectionStreamEx (
1047 NewStreamBufferSize,
1048 NewStreamBuffer,
1049 FALSE,
1050 AuthenticationStatus,
1051 &Context->ChildNode->EncapsulatedStreamHandle
1052 );
1053 ASSERT_EFI_ERROR (Status);
1054 }
1055
1056 //
1057 // If above, the stream did not close successfully, it indicates it's
1058 // alread been closed by someone, so just destroy the event and be done with
1059 // it.
1060 //
1061
1062 CoreCloseEvent (Event);
1063 CoreFreePool (Context);
1064 }
1065 #endif
1066
1067
1068 STATIC
1069 VOID
1070 FreeChildNode (
1071 IN CORE_SECTION_CHILD_NODE *ChildNode
1072 )
1073 /*++
1074
1075 Routine Description:
1076 Worker function. Destructor for child nodes.
1077
1078 Arguments:
1079 ChildNode - Indicates the node to destroy
1080
1081 Returns:
1082 none
1083
1084 --*/
1085 {
1086 ASSERT (ChildNode->Signature == CORE_SECTION_CHILD_SIGNATURE);
1087 //
1088 // Remove the child from it's list
1089 //
1090 RemoveEntryList (&ChildNode->Link);
1091
1092 if (ChildNode->EncapsulatedStreamHandle != NULL_STREAM_HANDLE) {
1093 //
1094 // If it's an encapsulating section, we close the resulting section stream.
1095 // CloseSectionStream will free all memory associated with the stream.
1096 //
1097 CloseSectionStream (ChildNode->EncapsulatedStreamHandle);
1098 }
1099 //
1100 // Last, free the child node itself
1101 //
1102 CoreFreePool (ChildNode);
1103 }
1104
1105
1106 STATIC
1107 EFI_STATUS
1108 OpenSectionStreamEx (
1109 IN UINTN SectionStreamLength,
1110 IN VOID *SectionStream,
1111 IN BOOLEAN AllocateBuffer,
1112 IN UINT32 AuthenticationStatus,
1113 OUT UINTN *SectionStreamHandle
1114 )
1115 /*++
1116
1117 Routine Description:
1118 Worker function. Constructor for section streams.
1119
1120 Arguments:
1121 SectionStreamLength - Size in bytes of the section stream.
1122 SectionStream - Buffer containing the new section stream.
1123 AllocateBuffer - Indicates whether the stream buffer is to be copied
1124 or the input buffer is to be used in place.
1125 AuthenticationStatus- Indicates the default authentication status for the
1126 new stream.
1127 SectionStreamHandle - A pointer to a caller allocated section stream handle.
1128
1129 Returns:
1130 EFI_SUCCESS - Stream was added to stream database.
1131 EFI_OUT_OF_RESOURCES - memory allocation failed.
1132
1133 --*/
1134 {
1135 CORE_SECTION_STREAM_NODE *NewStream;
1136 EFI_TPL OldTpl;
1137
1138 //
1139 // Allocate a new stream
1140 //
1141 NewStream = CoreAllocateBootServicesPool (sizeof (CORE_SECTION_STREAM_NODE));
1142 if (NewStream == NULL) {
1143 return EFI_OUT_OF_RESOURCES;
1144 }
1145
1146 if (AllocateBuffer) {
1147 //
1148 // if we're here, we're double buffering, allocate the buffer and copy the
1149 // data in
1150 //
1151 if (SectionStreamLength > 0) {
1152 NewStream->StreamBuffer = CoreAllocateBootServicesPool (SectionStreamLength);
1153 if (NewStream->StreamBuffer == NULL) {
1154 CoreFreePool (NewStream);
1155 return EFI_OUT_OF_RESOURCES;
1156 }
1157 //
1158 // Copy in stream data
1159 //
1160 CopyMem (NewStream->StreamBuffer, SectionStream, SectionStreamLength);
1161 } else {
1162 //
1163 // It's possible to have a zero length section stream.
1164 //
1165 NewStream->StreamBuffer = NULL;
1166 }
1167 } else {
1168 //
1169 // If were here, the caller has supplied the buffer (it's an internal call)
1170 // so just assign the buffer. This happens when we open section streams
1171 // as a result of expanding an encapsulating section.
1172 //
1173 NewStream->StreamBuffer = SectionStream;
1174 }
1175
1176 //
1177 // Initialize the rest of the section stream
1178 //
1179 NewStream->Signature = CORE_SECTION_STREAM_SIGNATURE;
1180 NewStream->StreamHandle = (UINTN) NewStream;
1181 NewStream->StreamLength = SectionStreamLength;
1182 InitializeListHead (&NewStream->Children);
1183 NewStream->AuthenticationStatus = AuthenticationStatus;
1184
1185 //
1186 // Add new stream to stream list
1187 //
1188 OldTpl = CoreRaiseTpl (TPL_NOTIFY);
1189 InsertTailList (&mStreamRoot, &NewStream->Link);
1190 CoreRestoreTpl (OldTpl);
1191
1192 *SectionStreamHandle = NewStream->StreamHandle;
1193
1194 return EFI_SUCCESS;
1195 }
1196
1197
1198 STATIC
1199 EFI_STATUS
1200 FindStreamNode (
1201 IN UINTN SearchHandle,
1202 OUT CORE_SECTION_STREAM_NODE **FoundStream
1203 )
1204 /*++
1205
1206 Routine Description:
1207 Worker function. Search stream database for requested stream handle.
1208
1209 Arguments:
1210 SearchHandle - Indicates which stream to look for.
1211 FoundStream - Output pointer to the found stream.
1212
1213 Returns:
1214 EFI_SUCCESS - StreamHandle was found and *FoundStream contains
1215 the stream node.
1216 EFI_NOT_FOUND - SearchHandle was not found in the stream database.
1217
1218 --*/
1219 {
1220 CORE_SECTION_STREAM_NODE *StreamNode;
1221
1222 if (!IsListEmpty (&mStreamRoot)) {
1223 StreamNode = STREAM_NODE_FROM_LINK (GetFirstNode (&mStreamRoot));
1224 for (;;) {
1225 if (StreamNode->StreamHandle == SearchHandle) {
1226 *FoundStream = StreamNode;
1227 return EFI_SUCCESS;
1228 } else if (IsNodeAtEnd (&mStreamRoot, &StreamNode->Link)) {
1229 break;
1230 } else {
1231 StreamNode = STREAM_NODE_FROM_LINK (GetNextNode (&mStreamRoot, &StreamNode->Link));
1232 }
1233 }
1234 }
1235
1236 return EFI_NOT_FOUND;
1237 }
1238
1239
1240 STATIC
1241 BOOLEAN
1242 IsValidSectionStream (
1243 IN VOID *SectionStream,
1244 IN UINTN SectionStreamLength
1245 )
1246 /*++
1247
1248 Routine Description:
1249 Check if a stream is valid.
1250
1251 Arguments:
1252 SectionStream - The section stream to be checked
1253 SectionStreamLength - The length of section stream
1254
1255 Returns:
1256 TRUE
1257 FALSE
1258
1259 --*/
1260 {
1261 UINTN TotalLength;
1262 UINTN SectionLength;
1263 EFI_COMMON_SECTION_HEADER *SectionHeader;
1264 EFI_COMMON_SECTION_HEADER *NextSectionHeader;
1265
1266 TotalLength = 0;
1267 SectionHeader = (EFI_COMMON_SECTION_HEADER *)SectionStream;
1268
1269 while (TotalLength < SectionStreamLength) {
1270 SectionLength = SECTION_SIZE (SectionHeader);
1271 TotalLength += SectionLength;
1272
1273 if (TotalLength == SectionStreamLength) {
1274 return TRUE;
1275 }
1276
1277 //
1278 // Move to the next byte following the section...
1279 //
1280 SectionHeader = (EFI_COMMON_SECTION_HEADER *) ((UINT8 *) SectionHeader + SectionLength);
1281
1282 //
1283 // Figure out where the next section begins
1284 //
1285 NextSectionHeader = (EFI_COMMON_SECTION_HEADER *) ((UINTN) SectionHeader + 3);
1286 NextSectionHeader = (EFI_COMMON_SECTION_HEADER *) ((UINTN) NextSectionHeader & ~(UINTN)3);
1287 TotalLength += (UINTN) NextSectionHeader - (UINTN) SectionHeader;
1288 SectionHeader = NextSectionHeader;
1289 }
1290
1291 ASSERT (FALSE);
1292 return FALSE;
1293 }
1294
1295 /**
1296 The ExtractSection() function processes the input section and
1297 allocates a buffer from the pool in which it returns the section
1298 contents. If the section being extracted contains
1299 authentication information (the section's
1300 GuidedSectionHeader.Attributes field has the
1301 EFI_GUIDED_SECTION_AUTH_STATUS_VALID bit set), the values
1302 returned in AuthenticationStatus must reflect the results of
1303 the authentication operation. Depending on the algorithm and
1304 size of the encapsulated data, the time that is required to do
1305 a full authentication may be prohibitively long for some
1306 classes of systems. To indicate this, use
1307 EFI_SECURITY_POLICY_PROTOCOL_GUID, which may be published by
1308 the security policy driver (see the Platform Initialization
1309 Driver Execution Environment Core Interface Specification for
1310 more details and the GUID definition). If the
1311 EFI_SECURITY_POLICY_PROTOCOL_GUID exists in the handle
1312 database, then, if possible, full authentication should be
1313 skipped and the section contents simply returned in the
1314 OutputBuffer. In this case, the
1315 EFI_AUTH_STATUS_PLATFORM_OVERRIDE bit AuthenticationStatus
1316 must be set on return. ExtractSection() is callable only from
1317 TPL_NOTIFY and below. Behavior of ExtractSection() at any
1318 EFI_TPL above TPL_NOTIFY is undefined. Type EFI_TPL is
1319 defined in RaiseTPL() in the UEFI 2.0 specification.
1320
1321
1322 @param This Indicates the
1323 EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL instance.
1324
1325 @param InputSection Buffer containing the input GUIDed section
1326 to be processed. OutputBuffer OutputBuffer
1327 is allocated from boot services pool
1328 memory and contains the new section
1329 stream. The caller is responsible for
1330 freeing this buffer.
1331
1332 @param OutputSize A pointer to a caller-allocated UINTN in
1333 which the size of OutputBuffer allocation
1334 is stored. If the function returns
1335 anything other than EFI_SUCCESS, the value
1336 of OutputSize is undefined.
1337
1338 @param AuthenticationStatus A pointer to a caller-allocated
1339 UINT32 that indicates the
1340 authentication status of the
1341 output buffer. If the input
1342 section's
1343 GuidedSectionHeader.Attributes
1344 field has the
1345 EFI_GUIDED_SECTION_AUTH_STATUS_VAL
1346 bit as clear, AuthenticationStatus
1347 must return zero. Both local bits
1348 (19:16) and aggregate bits (3:0)
1349 in AuthenticationStatus are
1350 returned by ExtractSection().
1351 These bits reflect the status of
1352 the extraction operation. The bit
1353 pattern in both regions must be
1354 the same, as the local and
1355 aggregate authentication statuses
1356 have equivalent meaning at this
1357 level. If the function returns
1358 anything other than EFI_SUCCESS,
1359 the value of AuthenticationStatus
1360 is undefined.
1361
1362
1363 @retval EFI_SUCCESS The InputSection was successfully
1364 processed and the section contents were
1365 returned.
1366
1367 @retval EFI_OUT_OF_RESOURCES The system has insufficient
1368 resources to process the
1369 request.
1370
1371 @retval EFI_INVALID_PARAMETER The GUID in InputSection does
1372 not match this instance of the
1373 GUIDed Section Extraction
1374 Protocol.
1375
1376 **/
1377 EFI_STATUS
1378 EFIAPI
1379 CustomGuidedSectionExtract (
1380 IN CONST EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL *This,
1381 IN CONST VOID *InputSection,
1382 OUT VOID **OutputBuffer,
1383 OUT UINTN *OutputSize,
1384 OUT UINT32 *AuthenticationStatus
1385 )
1386 {
1387 EFI_STATUS Status;
1388 VOID *ScratchBuffer;
1389 VOID *AllocatedOutputBuffer;
1390 UINT32 OutputBufferSize;
1391 UINT32 ScratchBufferSize;
1392 UINT16 SectionAttribute;
1393
1394 //
1395 // Init local variable
1396 //
1397 ScratchBuffer = NULL;
1398 AllocatedOutputBuffer = NULL;
1399
1400 //
1401 // Call GetInfo to get the size and attribute of input guided section data.
1402 //
1403 Status = ExtractGuidedSectionGetInfo (
1404 InputSection,
1405 &OutputBufferSize,
1406 &ScratchBufferSize,
1407 &SectionAttribute
1408 );
1409
1410 if (EFI_ERROR (Status)) {
1411 DEBUG ((EFI_D_ERROR, "GetInfo from guided section Failed - %r\n", Status));
1412 return Status;
1413 }
1414
1415 if (ScratchBufferSize != 0) {
1416 //
1417 // Allocate scratch buffer
1418 //
1419 ScratchBuffer = CoreAllocateBootServicesPool (ScratchBufferSize);
1420 if (ScratchBuffer == NULL) {
1421 return EFI_OUT_OF_RESOURCES;
1422 }
1423 }
1424
1425 if (OutputBufferSize > 0) {
1426 //
1427 // Allocate output buffer
1428 //
1429 AllocatedOutputBuffer = CoreAllocateBootServicesPool (OutputBufferSize);
1430 if (AllocatedOutputBuffer == NULL) {
1431 return EFI_OUT_OF_RESOURCES;
1432 }
1433 *OutputBuffer = AllocatedOutputBuffer;
1434 }
1435
1436 //
1437 // Call decode function to extract raw data from the guided section.
1438 //
1439 Status = ExtractGuidedSectionDecode (
1440 InputSection,
1441 OutputBuffer,
1442 ScratchBuffer,
1443 AuthenticationStatus
1444 );
1445 if (EFI_ERROR (Status)) {
1446 //
1447 // Decode failed
1448 //
1449 if (AllocatedOutputBuffer != NULL) {
1450 CoreFreePool (AllocatedOutputBuffer);
1451 }
1452 if (ScratchBuffer != NULL) {
1453 CoreFreePool (ScratchBuffer);
1454 }
1455 DEBUG ((EFI_D_ERROR, "Extract guided section Failed - %r\n", Status));
1456 return Status;
1457 }
1458
1459 if (*OutputBuffer != AllocatedOutputBuffer) {
1460 //
1461 // OutputBuffer was returned as a different value,
1462 // so copy section contents to the allocated memory buffer.
1463 //
1464 CopyMem (AllocatedOutputBuffer, *OutputBuffer, OutputBufferSize);
1465 *OutputBuffer = AllocatedOutputBuffer;
1466 }
1467
1468 //
1469 // Set real size of output buffer.
1470 //
1471 *OutputSize = (UINTN) OutputBufferSize;
1472
1473 //
1474 // Free unused scratch buffer.
1475 //
1476 if (ScratchBuffer != NULL) {
1477 CoreFreePool (ScratchBuffer);
1478 }
1479
1480 return EFI_SUCCESS;
1481 }