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