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