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