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