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