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