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