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