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