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