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