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