]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Core/PiSmmCore/Dispatcher.c
Update PEI/DXE/SMM dispatchers to include DEBUG ((DEBUG_DISPATCH, )) macros to log...
[mirror_edk2.git] / MdeModulePkg / Core / PiSmmCore / Dispatcher.c
1 /** @file
2 SMM Driver Dispatcher.
3
4 Step #1 - When a FV protocol is added to the system every driver in the FV
5 is added to the mDiscoveredList. The SOR, Before, and After Depex are
6 pre-processed as drivers are added to the mDiscoveredList. If an Apriori
7 file exists in the FV those drivers are addeded to the
8 mScheduledQueue. The mFvHandleList is used to make sure a
9 FV is only processed once.
10
11 Step #2 - Dispatch. Remove driver from the mScheduledQueue and load and
12 start it. After mScheduledQueue is drained check the
13 mDiscoveredList to see if any item has a Depex that is ready to
14 be placed on the mScheduledQueue.
15
16 Step #3 - Adding to the mScheduledQueue requires that you process Before
17 and After dependencies. This is done recursively as the call to add
18 to the mScheduledQueue checks for Before and recursively adds
19 all Befores. It then addes the item that was passed in and then
20 processess the After dependecies by recursively calling the routine.
21
22 Dispatcher Rules:
23 The rules for the dispatcher are similar to the DXE dispatcher.
24
25 The rules for DXE dispatcher are in chapter 10 of the DXE CIS. Figure 10-3
26 is the state diagram for the DXE dispatcher
27
28 Depex - Dependency Expresion.
29 SOR - Schedule On Request - Don't schedule if this bit is set.
30
31 Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.<BR>
32 This program and the accompanying materials are licensed and made available
33 under the terms and conditions of the BSD License which accompanies this
34 distribution. The full text of the license may be found at
35 http://opensource.org/licenses/bsd-license.php
36
37 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
38 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
39
40 **/
41
42 #include "PiSmmCore.h"
43
44 //
45 // SMM Dispatcher Data structures
46 //
47 #define KNOWN_HANDLE_SIGNATURE SIGNATURE_32('k','n','o','w')
48 typedef struct {
49 UINTN Signature;
50 LIST_ENTRY Link; // mFvHandleList
51 EFI_HANDLE Handle;
52 } KNOWN_HANDLE;
53
54 //
55 // Function Prototypes
56 //
57
58 /**
59 Insert InsertedDriverEntry onto the mScheduledQueue. To do this you
60 must add any driver with a before dependency on InsertedDriverEntry first.
61 You do this by recursively calling this routine. After all the Befores are
62 processed you can add InsertedDriverEntry to the mScheduledQueue.
63 Then you can add any driver with an After dependency on InsertedDriverEntry
64 by recursively calling this routine.
65
66 @param InsertedDriverEntry The driver to insert on the ScheduledLink Queue
67
68 **/
69 VOID
70 SmmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (
71 IN EFI_SMM_DRIVER_ENTRY *InsertedDriverEntry
72 );
73
74 //
75 // The Driver List contains one copy of every driver that has been discovered.
76 // Items are never removed from the driver list. List of EFI_SMM_DRIVER_ENTRY
77 //
78 LIST_ENTRY mDiscoveredList = INITIALIZE_LIST_HEAD_VARIABLE (mDiscoveredList);
79
80 //
81 // Queue of drivers that are ready to dispatch. This queue is a subset of the
82 // mDiscoveredList.list of EFI_SMM_DRIVER_ENTRY.
83 //
84 LIST_ENTRY mScheduledQueue = INITIALIZE_LIST_HEAD_VARIABLE (mScheduledQueue);
85
86 //
87 // List of handles who's Fv's have been parsed and added to the mFwDriverList.
88 //
89 LIST_ENTRY mFvHandleList = INITIALIZE_LIST_HEAD_VARIABLE (mFvHandleList);
90
91 //
92 // Flag for the SMM Dispacher. TRUE if dispatcher is execuing.
93 //
94 BOOLEAN gDispatcherRunning = FALSE;
95
96 //
97 // Flag for the SMM Dispacher. TRUE if there is one or more SMM drivers ready to be dispatched
98 //
99 BOOLEAN gRequestDispatch = FALSE;
100
101 //
102 // List of file types supported by dispatcher
103 //
104 EFI_FV_FILETYPE mSmmFileTypes[] = {
105 EFI_FV_FILETYPE_SMM,
106 EFI_FV_FILETYPE_COMBINED_SMM_DXE
107 //
108 // Note: DXE core will process the FV image file, so skip it in SMM core
109 // EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE
110 //
111 };
112
113 typedef struct {
114 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH File;
115 EFI_DEVICE_PATH_PROTOCOL End;
116 } FV_FILEPATH_DEVICE_PATH;
117
118 FV_FILEPATH_DEVICE_PATH mFvDevicePath;
119
120 //
121 // DXE Architecture Protocols
122 //
123 EFI_SECURITY_ARCH_PROTOCOL *mSecurity = NULL;
124
125 //
126 // The global variable is defined for Loading modules at fixed address feature to track the SMM code
127 // memory range usage. It is a bit mapped array in which every bit indicates the correspoding
128 // memory page available or not.
129 //
130 GLOBAL_REMOVE_IF_UNREFERENCED UINT64 *mSmmCodeMemoryRangeUsageBitMap=NULL;
131
132 /**
133 To check memory usage bit map array to figure out if the memory range in which the image will be loaded is available or not. If
134 memory range is avaliable, the function will mark the correponding bits to 1 which indicates the memory range is used.
135 The function is only invoked when load modules at fixed address feature is enabled.
136
137 @param ImageBase The base addres the image will be loaded at.
138 @param ImageSize The size of the image
139
140 @retval EFI_SUCCESS The memory range the image will be loaded in is available
141 @retval EFI_NOT_FOUND The memory range the image will be loaded in is not available
142 **/
143 EFI_STATUS
144 CheckAndMarkFixLoadingMemoryUsageBitMap (
145 IN EFI_PHYSICAL_ADDRESS ImageBase,
146 IN UINTN ImageSize
147 )
148 {
149 UINT32 SmmCodePageNumber;
150 UINT64 SmmCodeSize;
151 EFI_PHYSICAL_ADDRESS SmmCodeBase;
152 UINTN BaseOffsetPageNumber;
153 UINTN TopOffsetPageNumber;
154 UINTN Index;
155 //
156 // Build tool will calculate the smm code size and then patch the PcdLoadFixAddressSmmCodePageNumber
157 //
158 SmmCodePageNumber = PcdGet32(PcdLoadFixAddressSmmCodePageNumber);
159 SmmCodeSize = EFI_PAGES_TO_SIZE (SmmCodePageNumber);
160 SmmCodeBase = gLoadModuleAtFixAddressSmramBase;
161
162 //
163 // If the memory usage bit map is not initialized, do it. Every bit in the array
164 // indicate the status of the corresponding memory page, available or not
165 //
166 if (mSmmCodeMemoryRangeUsageBitMap == NULL) {
167 mSmmCodeMemoryRangeUsageBitMap = AllocateZeroPool(((SmmCodePageNumber / 64) + 1)*sizeof(UINT64));
168 }
169 //
170 // If the Dxe code memory range is not allocated or the bit map array allocation failed, return EFI_NOT_FOUND
171 //
172 if (mSmmCodeMemoryRangeUsageBitMap == NULL) {
173 return EFI_NOT_FOUND;
174 }
175 //
176 // see if the memory range for loading the image is in the SMM code range.
177 //
178 if (SmmCodeBase + SmmCodeSize < ImageBase + ImageSize || SmmCodeBase > ImageBase) {
179 return EFI_NOT_FOUND;
180 }
181 //
182 // Test if the memory is avalaible or not.
183 //
184 BaseOffsetPageNumber = (UINTN)EFI_SIZE_TO_PAGES((UINT32)(ImageBase - SmmCodeBase));
185 TopOffsetPageNumber = (UINTN)EFI_SIZE_TO_PAGES((UINT32)(ImageBase + ImageSize - SmmCodeBase));
186 for (Index = BaseOffsetPageNumber; Index < TopOffsetPageNumber; Index ++) {
187 if ((mSmmCodeMemoryRangeUsageBitMap[Index / 64] & LShiftU64(1, (Index % 64))) != 0) {
188 //
189 // This page is already used.
190 //
191 return EFI_NOT_FOUND;
192 }
193 }
194
195 //
196 // Being here means the memory range is available. So mark the bits for the memory range
197 //
198 for (Index = BaseOffsetPageNumber; Index < TopOffsetPageNumber; Index ++) {
199 mSmmCodeMemoryRangeUsageBitMap[Index / 64] |= LShiftU64(1, (Index % 64));
200 }
201 return EFI_SUCCESS;
202 }
203 /**
204 Get the fixed loadding address from image header assigned by build tool. This function only be called
205 when Loading module at Fixed address feature enabled.
206
207 @param ImageContext Pointer to the image context structure that describes the PE/COFF
208 image that needs to be examined by this function.
209 @retval EFI_SUCCESS An fixed loading address is assigned to this image by build tools .
210 @retval EFI_NOT_FOUND The image has no assigned fixed loadding address.
211
212 **/
213 EFI_STATUS
214 GetPeCoffImageFixLoadingAssignedAddress(
215 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
216 )
217 {
218 UINTN SectionHeaderOffset;
219 EFI_STATUS Status;
220 EFI_IMAGE_SECTION_HEADER SectionHeader;
221 EFI_IMAGE_OPTIONAL_HEADER_UNION *ImgHdr;
222 EFI_PHYSICAL_ADDRESS FixLoaddingAddress;
223 UINT16 Index;
224 UINTN Size;
225 UINT16 NumberOfSections;
226 UINT64 ValueInSectionHeader;
227
228 FixLoaddingAddress = 0;
229 Status = EFI_NOT_FOUND;
230
231 //
232 // Get PeHeader pointer
233 //
234 ImgHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)((CHAR8* )ImageContext->Handle + ImageContext->PeCoffHeaderOffset);
235 SectionHeaderOffset = (UINTN)(
236 ImageContext->PeCoffHeaderOffset +
237 sizeof (UINT32) +
238 sizeof (EFI_IMAGE_FILE_HEADER) +
239 ImgHdr->Pe32.FileHeader.SizeOfOptionalHeader
240 );
241 NumberOfSections = ImgHdr->Pe32.FileHeader.NumberOfSections;
242
243 //
244 // Get base address from the first section header that doesn't point to code section.
245 //
246 for (Index = 0; Index < NumberOfSections; Index++) {
247 //
248 // Read section header from file
249 //
250 Size = sizeof (EFI_IMAGE_SECTION_HEADER);
251 Status = ImageContext->ImageRead (
252 ImageContext->Handle,
253 SectionHeaderOffset,
254 &Size,
255 &SectionHeader
256 );
257 if (EFI_ERROR (Status)) {
258 return Status;
259 }
260
261 Status = EFI_NOT_FOUND;
262
263 if ((SectionHeader.Characteristics & EFI_IMAGE_SCN_CNT_CODE) == 0) {
264 //
265 // Build tool will save the address in PointerToRelocations & PointerToLineNumbers fields in the first section header
266 // that doesn't point to code section in image header.So there is an assumption that when the feature is enabled,
267 // if a module with a loading address assigned by tools, the PointerToRelocations & PointerToLineNumbers fields
268 // should not be Zero, or else, these 2 fileds should be set to Zero
269 //
270 ValueInSectionHeader = ReadUnaligned64((UINT64*)&SectionHeader.PointerToRelocations);
271 if (ValueInSectionHeader != 0) {
272 //
273 // Found first section header that doesn't point to code section in which uild tool saves the
274 // offset to SMRAM base as image base in PointerToRelocations & PointerToLineNumbers fields
275 //
276 FixLoaddingAddress = (EFI_PHYSICAL_ADDRESS)(gLoadModuleAtFixAddressSmramBase + (INT64)ValueInSectionHeader);
277 //
278 // Check if the memory range is avaliable.
279 //
280 Status = CheckAndMarkFixLoadingMemoryUsageBitMap (FixLoaddingAddress, (UINTN)(ImageContext->ImageSize + ImageContext->SectionAlignment));
281 if (!EFI_ERROR(Status)) {
282 //
283 // The assigned address is valid. Return the specified loadding address
284 //
285 ImageContext->ImageAddress = FixLoaddingAddress;
286 }
287 }
288 break;
289 }
290 SectionHeaderOffset += sizeof (EFI_IMAGE_SECTION_HEADER);
291 }
292 DEBUG ((EFI_D_INFO|EFI_D_LOAD, "LOADING MODULE FIXED INFO: Loading module at fixed address %x, Status = %r\n", FixLoaddingAddress, Status));
293 return Status;
294 }
295 /**
296 Loads an EFI image into SMRAM.
297
298 @param DriverEntry EFI_SMM_DRIVER_ENTRY instance
299
300 @return EFI_STATUS
301
302 **/
303 EFI_STATUS
304 EFIAPI
305 SmmLoadImage (
306 IN OUT EFI_SMM_DRIVER_ENTRY *DriverEntry
307 )
308 {
309 UINT32 AuthenticationStatus;
310 UINTN FilePathSize;
311 VOID *Buffer;
312 UINTN Size;
313 UINTN PageCount;
314 EFI_GUID *NameGuid;
315 EFI_STATUS Status;
316 EFI_STATUS SecurityStatus;
317 EFI_HANDLE DeviceHandle;
318 EFI_PHYSICAL_ADDRESS DstBuffer;
319 EFI_DEVICE_PATH_PROTOCOL *FilePath;
320 EFI_DEVICE_PATH_PROTOCOL *OriginalFilePath;
321 EFI_DEVICE_PATH_PROTOCOL *HandleFilePath;
322 EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;
323 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;
324
325 Buffer = NULL;
326 Size = 0;
327 Fv = DriverEntry->Fv;
328 NameGuid = &DriverEntry->FileName;
329 FilePath = DriverEntry->FvFileDevicePath;
330
331 OriginalFilePath = FilePath;
332 HandleFilePath = FilePath;
333 DeviceHandle = NULL;
334 SecurityStatus = EFI_SUCCESS;
335 Status = EFI_SUCCESS;
336 AuthenticationStatus = 0;
337
338 //
339 // Try to get the image device handle by checking the match protocol.
340 //
341 Status = gBS->LocateDevicePath (&gEfiFirmwareVolume2ProtocolGuid, &HandleFilePath, &DeviceHandle);
342 if (EFI_ERROR(Status)) {
343 return Status;
344 }
345
346 //
347 // If the Security Architectural Protocol has not been located yet, then attempt to locate it
348 //
349 if (mSecurity == NULL) {
350 gBS->LocateProtocol (&gEfiSecurityArchProtocolGuid, NULL, (VOID**)&mSecurity);
351 }
352
353 //
354 // Verify the Authentication Status through the Security Architectural Protocol
355 //
356 if ((mSecurity != NULL) && (OriginalFilePath != NULL)) {
357 SecurityStatus = mSecurity->FileAuthenticationState (
358 mSecurity,
359 AuthenticationStatus,
360 OriginalFilePath
361 );
362 if (EFI_ERROR (SecurityStatus) && SecurityStatus != EFI_SECURITY_VIOLATION) {
363 Status = SecurityStatus;
364 return Status;
365 }
366 }
367
368 //
369 // Pull out just the file portion of the DevicePath for the LoadedImage FilePath
370 //
371 FilePath = OriginalFilePath;
372 Status = gBS->HandleProtocol (DeviceHandle, &gEfiDevicePathProtocolGuid, (VOID **)&HandleFilePath);
373 if (!EFI_ERROR (Status)) {
374 FilePathSize = GetDevicePathSize (HandleFilePath) - sizeof(EFI_DEVICE_PATH_PROTOCOL);
375 FilePath = (EFI_DEVICE_PATH_PROTOCOL *) (((UINT8 *)FilePath) + FilePathSize );
376 }
377
378 //
379 // Try reading PE32 section firstly
380 //
381 Status = Fv->ReadSection (
382 Fv,
383 NameGuid,
384 EFI_SECTION_PE32,
385 0,
386 &Buffer,
387 &Size,
388 &AuthenticationStatus
389 );
390
391 if (EFI_ERROR (Status)) {
392 //
393 // Try reading TE section secondly
394 //
395 Buffer = NULL;
396 Size = 0;
397 Status = Fv->ReadSection (
398 Fv,
399 NameGuid,
400 EFI_SECTION_TE,
401 0,
402 &Buffer,
403 &Size,
404 &AuthenticationStatus
405 );
406 }
407
408 if (EFI_ERROR (Status)) {
409 if (Buffer != NULL) {
410 Status = gBS->FreePool (Buffer);
411 }
412 return Status;
413 }
414
415 //
416 // Initialize ImageContext
417 //
418 ImageContext.Handle = Buffer;
419 ImageContext.ImageRead = PeCoffLoaderImageReadFromMemory;
420
421 //
422 // Get information about the image being loaded
423 //
424 Status = PeCoffLoaderGetImageInfo (&ImageContext);
425 if (EFI_ERROR (Status)) {
426 if (Buffer != NULL) {
427 Status = gBS->FreePool (Buffer);
428 }
429 return Status;
430 }
431 //
432 // if Loading module at Fixed Address feature is enabled, then cut out a memory range started from TESG BASE
433 // to hold the Smm driver code
434 //
435 if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0) {
436 //
437 // Get the fixed loading address assigned by Build tool
438 //
439 Status = GetPeCoffImageFixLoadingAssignedAddress (&ImageContext);
440 if (!EFI_ERROR (Status)) {
441 //
442 // Since the memory range to load Smm core alreay been cut out, so no need to allocate and free this range
443 // following statements is to bypass SmmFreePages
444 //
445 PageCount = 0;
446 DstBuffer = (UINTN)gLoadModuleAtFixAddressSmramBase;
447 } else {
448 DEBUG ((EFI_D_INFO|EFI_D_LOAD, "LOADING MODULE FIXED ERROR: Failed to load module at fixed address. \n"));
449 //
450 // allocate the memory to load the SMM driver
451 //
452 PageCount = (UINTN)EFI_SIZE_TO_PAGES(ImageContext.ImageSize + ImageContext.SectionAlignment);
453 DstBuffer = (UINTN)(-1);
454
455 Status = SmmAllocatePages (
456 AllocateMaxAddress,
457 EfiRuntimeServicesCode,
458 PageCount,
459 &DstBuffer
460 );
461 if (EFI_ERROR (Status)) {
462 if (Buffer != NULL) {
463 Status = gBS->FreePool (Buffer);
464 }
465 return Status;
466 }
467 ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)DstBuffer;
468 }
469 } else {
470 PageCount = (UINTN)EFI_SIZE_TO_PAGES(ImageContext.ImageSize + ImageContext.SectionAlignment);
471 DstBuffer = (UINTN)(-1);
472
473 Status = SmmAllocatePages (
474 AllocateMaxAddress,
475 EfiRuntimeServicesCode,
476 PageCount,
477 &DstBuffer
478 );
479 if (EFI_ERROR (Status)) {
480 if (Buffer != NULL) {
481 Status = gBS->FreePool (Buffer);
482 }
483 return Status;
484 }
485
486 ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)DstBuffer;
487 }
488 //
489 // Align buffer on section boundry
490 //
491 ImageContext.ImageAddress += ImageContext.SectionAlignment - 1;
492 ImageContext.ImageAddress &= ~(ImageContext.SectionAlignment - 1);
493
494 //
495 // Load the image to our new buffer
496 //
497 Status = PeCoffLoaderLoadImage (&ImageContext);
498 if (EFI_ERROR (Status)) {
499 if (Buffer != NULL) {
500 Status = gBS->FreePool (Buffer);
501 }
502 SmmFreePages (DstBuffer, PageCount);
503 return Status;
504 }
505
506 //
507 // Relocate the image in our new buffer
508 //
509 Status = PeCoffLoaderRelocateImage (&ImageContext);
510 if (EFI_ERROR (Status)) {
511 if (Buffer != NULL) {
512 Status = gBS->FreePool (Buffer);
513 }
514 SmmFreePages (DstBuffer, PageCount);
515 return Status;
516 }
517
518 //
519 // Flush the instruction cache so the image data are written before we execute it
520 //
521 InvalidateInstructionCacheRange ((VOID *)(UINTN) ImageContext.ImageAddress, (UINTN) ImageContext.ImageSize);
522
523 //
524 // Save Image EntryPoint in DriverEntry
525 //
526 DriverEntry->ImageEntryPoint = ImageContext.EntryPoint;
527 DriverEntry->ImageBuffer = DstBuffer;
528 DriverEntry->NumberOfPage = PageCount;
529
530 //
531 // Allocate a Loaded Image Protocol in EfiBootServicesData
532 //
533 Status = gBS->AllocatePool (EfiBootServicesData, sizeof (EFI_LOADED_IMAGE_PROTOCOL), (VOID **)&DriverEntry->LoadedImage);
534 if (EFI_ERROR (Status)) {
535 if (Buffer != NULL) {
536 Status = gBS->FreePool (Buffer);
537 }
538 SmmFreePages (DstBuffer, PageCount);
539 return Status;
540 }
541
542 //
543 // Fill in the remaining fields of the Loaded Image Protocol instance.
544 // Note: ImageBase is an SMRAM address that can not be accessed outside of SMRAM if SMRAM window is closed.
545 //
546 DriverEntry->LoadedImage->Revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;
547 DriverEntry->LoadedImage->ParentHandle = gSmmCorePrivate->SmmIplImageHandle;
548 DriverEntry->LoadedImage->SystemTable = gST;
549 DriverEntry->LoadedImage->DeviceHandle = DeviceHandle;
550
551 //
552 // Make an EfiBootServicesData buffer copy of FilePath
553 //
554 Status = gBS->AllocatePool (EfiBootServicesData, GetDevicePathSize (FilePath), (VOID **)&DriverEntry->LoadedImage->FilePath);
555 if (EFI_ERROR (Status)) {
556 if (Buffer != NULL) {
557 Status = gBS->FreePool (Buffer);
558 }
559 SmmFreePages (DstBuffer, PageCount);
560 return Status;
561 }
562 CopyMem (DriverEntry->LoadedImage->FilePath, FilePath, GetDevicePathSize (FilePath));
563
564 DriverEntry->LoadedImage->ImageBase = (VOID *)(UINTN)DriverEntry->ImageBuffer;
565 DriverEntry->LoadedImage->ImageSize = ImageContext.ImageSize;
566 DriverEntry->LoadedImage->ImageCodeType = EfiRuntimeServicesCode;
567 DriverEntry->LoadedImage->ImageDataType = EfiRuntimeServicesData;
568
569 //
570 // Create a new image handle in the UEFI handle database for the SMM Driver
571 //
572 DriverEntry->ImageHandle = NULL;
573 Status = gBS->InstallMultipleProtocolInterfaces (
574 &DriverEntry->ImageHandle,
575 &gEfiLoadedImageProtocolGuid, DriverEntry->LoadedImage,
576 NULL
577 );
578
579 //
580 // Print the load address and the PDB file name if it is available
581 //
582
583 DEBUG_CODE_BEGIN ();
584
585 UINTN Index;
586 UINTN StartIndex;
587 CHAR8 EfiFileName[256];
588
589
590 DEBUG ((DEBUG_INFO | DEBUG_LOAD,
591 "Loading driver at 0x%11p EntryPoint=0x%11p ",
592 (VOID *)(UINTN) ImageContext.ImageAddress,
593 FUNCTION_ENTRY_POINT (ImageContext.EntryPoint)));
594
595
596 //
597 // Print Module Name by Pdb file path.
598 // Windows and Unix style file path are all trimmed correctly.
599 //
600 if (ImageContext.PdbPointer != NULL) {
601 StartIndex = 0;
602 for (Index = 0; ImageContext.PdbPointer[Index] != 0; Index++) {
603 if ((ImageContext.PdbPointer[Index] == '\\') || (ImageContext.PdbPointer[Index] == '/')) {
604 StartIndex = Index + 1;
605 }
606 }
607 //
608 // Copy the PDB file name to our temporary string, and replace .pdb with .efi
609 // The PDB file name is limited in the range of 0~255.
610 // If the length is bigger than 255, trim the redudant characters to avoid overflow in array boundary.
611 //
612 for (Index = 0; Index < sizeof (EfiFileName) - 4; Index++) {
613 EfiFileName[Index] = ImageContext.PdbPointer[Index + StartIndex];
614 if (EfiFileName[Index] == 0) {
615 EfiFileName[Index] = '.';
616 }
617 if (EfiFileName[Index] == '.') {
618 EfiFileName[Index + 1] = 'e';
619 EfiFileName[Index + 2] = 'f';
620 EfiFileName[Index + 3] = 'i';
621 EfiFileName[Index + 4] = 0;
622 break;
623 }
624 }
625
626 if (Index == sizeof (EfiFileName) - 4) {
627 EfiFileName[Index] = 0;
628 }
629 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "%a", EfiFileName)); // &Image->ImageContext.PdbPointer[StartIndex]));
630 }
631 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "\n"));
632
633 DEBUG_CODE_END ();
634
635 //
636 // Free buffer allocated by Fv->ReadSection.
637 //
638 // The UEFI Boot Services FreePool() function must be used because Fv->ReadSection
639 // used the UEFI Boot Services AllocatePool() function
640 //
641 Status = gBS->FreePool(Buffer);
642 return Status;
643 }
644
645 /**
646 Preprocess dependency expression and update DriverEntry to reflect the
647 state of Before, After, and SOR dependencies. If DriverEntry->Before
648 or DriverEntry->After is set it will never be cleared. If SOR is set
649 it will be cleared by SmmSchedule(), and then the driver can be
650 dispatched.
651
652 @param DriverEntry DriverEntry element to update .
653
654 @retval EFI_SUCCESS It always works.
655
656 **/
657 EFI_STATUS
658 SmmPreProcessDepex (
659 IN EFI_SMM_DRIVER_ENTRY *DriverEntry
660 )
661 {
662 UINT8 *Iterator;
663
664 Iterator = DriverEntry->Depex;
665 if (*Iterator == EFI_DEP_SOR) {
666 DriverEntry->Unrequested = TRUE;
667 } else {
668 DriverEntry->Dependent = TRUE;
669 }
670
671 if (*Iterator == EFI_DEP_BEFORE) {
672 DriverEntry->Before = TRUE;
673 } else if (*Iterator == EFI_DEP_AFTER) {
674 DriverEntry->After = TRUE;
675 }
676
677 if (DriverEntry->Before || DriverEntry->After) {
678 CopyMem (&DriverEntry->BeforeAfterGuid, Iterator + 1, sizeof (EFI_GUID));
679 }
680
681 return EFI_SUCCESS;
682 }
683
684 /**
685 Read Depex and pre-process the Depex for Before and After. If Section Extraction
686 protocol returns an error via ReadSection defer the reading of the Depex.
687
688 @param DriverEntry Driver to work on.
689
690 @retval EFI_SUCCESS Depex read and preprossesed
691 @retval EFI_PROTOCOL_ERROR The section extraction protocol returned an error
692 and Depex reading needs to be retried.
693 @retval Error DEPEX not found.
694
695 **/
696 EFI_STATUS
697 SmmGetDepexSectionAndPreProccess (
698 IN EFI_SMM_DRIVER_ENTRY *DriverEntry
699 )
700 {
701 EFI_STATUS Status;
702 EFI_SECTION_TYPE SectionType;
703 UINT32 AuthenticationStatus;
704 EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;
705
706 Fv = DriverEntry->Fv;
707
708 //
709 // Grab Depex info, it will never be free'ed.
710 // (Note: DriverEntry->Depex is in DXE memory)
711 //
712 SectionType = EFI_SECTION_SMM_DEPEX;
713 Status = Fv->ReadSection (
714 DriverEntry->Fv,
715 &DriverEntry->FileName,
716 SectionType,
717 0,
718 &DriverEntry->Depex,
719 (UINTN *)&DriverEntry->DepexSize,
720 &AuthenticationStatus
721 );
722 if (EFI_ERROR (Status)) {
723 if (Status == EFI_PROTOCOL_ERROR) {
724 //
725 // The section extraction protocol failed so set protocol error flag
726 //
727 DriverEntry->DepexProtocolError = TRUE;
728 } else {
729 //
730 // If no Depex assume depend on all architectural protocols
731 //
732 DriverEntry->Depex = NULL;
733 DriverEntry->Dependent = TRUE;
734 DriverEntry->DepexProtocolError = FALSE;
735 }
736 } else {
737 //
738 // Set Before, After, and Unrequested state information based on Depex
739 // Driver will be put in Dependent or Unrequested state
740 //
741 SmmPreProcessDepex (DriverEntry);
742 DriverEntry->DepexProtocolError = FALSE;
743 }
744
745 return Status;
746 }
747
748 /**
749 Check every driver and locate a matching one. If the driver is found, the Unrequested
750 state flag is cleared.
751
752 @param FirmwareVolumeHandle The handle of the Firmware Volume that contains
753 the firmware file specified by DriverName.
754 @param DriverName The Driver name to put in the Dependent state.
755
756 @retval EFI_SUCCESS The DriverName was found and it's SOR bit was
757 cleared
758 @retval EFI_NOT_FOUND The DriverName does not exist or it's SOR bit was
759 not set.
760
761 **/
762 EFI_STATUS
763 SmmSchedule (
764 IN EFI_HANDLE FirmwareVolumeHandle,
765 IN EFI_GUID *DriverName
766 )
767 {
768 LIST_ENTRY *Link;
769 EFI_SMM_DRIVER_ENTRY *DriverEntry;
770
771 //
772 // Check every driver
773 //
774 for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {
775 DriverEntry = CR(Link, EFI_SMM_DRIVER_ENTRY, Link, EFI_SMM_DRIVER_ENTRY_SIGNATURE);
776 if (DriverEntry->FvHandle == FirmwareVolumeHandle &&
777 DriverEntry->Unrequested &&
778 CompareGuid (DriverName, &DriverEntry->FileName)) {
779 //
780 // Move the driver from the Unrequested to the Dependent state
781 //
782 DriverEntry->Unrequested = FALSE;
783 DriverEntry->Dependent = TRUE;
784
785 DEBUG ((DEBUG_DISPATCH, "Schedule FFS(%g) - EFI_SUCCESS\n", DriverName));
786
787 return EFI_SUCCESS;
788 }
789 }
790
791 DEBUG ((DEBUG_DISPATCH, "Schedule FFS(%g) - EFI_NOT_FOUND\n", DriverName));
792
793 return EFI_NOT_FOUND;
794 }
795
796 /**
797 This is the main Dispatcher for SMM and it exits when there are no more
798 drivers to run. Drain the mScheduledQueue and load and start a PE
799 image for each driver. Search the mDiscoveredList to see if any driver can
800 be placed on the mScheduledQueue. If no drivers are placed on the
801 mScheduledQueue exit the function. On exit it is assumed the Bds()
802 will be called, and when the Bds() exits the Dispatcher will be called
803 again.
804
805 @retval EFI_ALREADY_STARTED The SMM Dispatcher is already running
806 @retval EFI_NOT_FOUND No SMM Drivers were dispatched
807 @retval EFI_SUCCESS One or more SMM Drivers were dispatched
808
809 **/
810 EFI_STATUS
811 SmmDispatcher (
812 VOID
813 )
814 {
815 EFI_STATUS Status;
816 EFI_STATUS ReturnStatus;
817 LIST_ENTRY *Link;
818 EFI_SMM_DRIVER_ENTRY *DriverEntry;
819 BOOLEAN ReadyToRun;
820
821 if (!gRequestDispatch) {
822 return EFI_NOT_FOUND;
823 }
824
825 if (gDispatcherRunning) {
826 //
827 // If the dispatcher is running don't let it be restarted.
828 //
829 return EFI_ALREADY_STARTED;
830 }
831
832 gDispatcherRunning = TRUE;
833
834 ReturnStatus = EFI_NOT_FOUND;
835 do {
836 //
837 // Drain the Scheduled Queue
838 //
839 while (!IsListEmpty (&mScheduledQueue)) {
840 DriverEntry = CR (
841 mScheduledQueue.ForwardLink,
842 EFI_SMM_DRIVER_ENTRY,
843 ScheduledLink,
844 EFI_SMM_DRIVER_ENTRY_SIGNATURE
845 );
846
847 //
848 // Load the SMM Driver image into memory. If the Driver was transitioned from
849 // Untrused to Scheduled it would have already been loaded so we may need to
850 // skip the LoadImage
851 //
852 if (DriverEntry->ImageHandle == NULL) {
853 Status = SmmLoadImage (DriverEntry);
854
855 //
856 // Update the driver state to reflect that it's been loaded
857 //
858 if (EFI_ERROR (Status)) {
859
860 if (Status == EFI_SECURITY_VIOLATION) {
861 //
862 // Take driver from Scheduled to Untrused state
863 //
864 DriverEntry->Untrusted = TRUE;
865 } else {
866 //
867 // The SMM Driver could not be loaded, and do not attempt to load or start it again.
868 // Take driver from Scheduled to Initialized.
869 //
870 // This case include the Never Trusted state if EFI_ACCESS_DENIED is returned
871 //
872 DriverEntry->Initialized = TRUE;
873 }
874
875 DriverEntry->Scheduled = FALSE;
876 RemoveEntryList (&DriverEntry->ScheduledLink);
877
878 //
879 // If it's an error don't try the StartImage
880 //
881 continue;
882 }
883 }
884
885 DriverEntry->Scheduled = FALSE;
886 DriverEntry->Initialized = TRUE;
887 RemoveEntryList (&DriverEntry->ScheduledLink);
888
889 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (
890 EFI_PROGRESS_CODE,
891 EFI_SOFTWARE_SMM_DRIVER | EFI_SW_PC_INIT_BEGIN,
892 &DriverEntry->ImageHandle,
893 sizeof (DriverEntry->ImageHandle)
894 );
895
896 //
897 // For each SMM driver, pass NULL as ImageHandle
898 //
899 Status = ((EFI_IMAGE_ENTRY_POINT)(UINTN)DriverEntry->ImageEntryPoint)(DriverEntry->ImageHandle, gST);
900 if (EFI_ERROR(Status)){
901 SmmFreePages(DriverEntry->ImageBuffer, DriverEntry->NumberOfPage);
902 }
903
904 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (
905 EFI_PROGRESS_CODE,
906 EFI_SOFTWARE_SMM_DRIVER | EFI_SW_PC_INIT_END,
907 &DriverEntry->ImageHandle,
908 sizeof (DriverEntry->ImageHandle)
909 );
910
911 ReturnStatus = EFI_SUCCESS;
912 }
913
914 //
915 // Search DriverList for items to place on Scheduled Queue
916 //
917 ReadyToRun = FALSE;
918 for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {
919 DriverEntry = CR (Link, EFI_SMM_DRIVER_ENTRY, Link, EFI_SMM_DRIVER_ENTRY_SIGNATURE);
920
921 if (DriverEntry->DepexProtocolError){
922 //
923 // If Section Extraction Protocol did not let the Depex be read before retry the read
924 //
925 Status = SmmGetDepexSectionAndPreProccess (DriverEntry);
926 }
927
928 if (DriverEntry->Dependent) {
929 if (SmmIsSchedulable (DriverEntry)) {
930 SmmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry);
931 ReadyToRun = TRUE;
932 }
933 } else {
934 if (DriverEntry->Unrequested) {
935 DEBUG ((DEBUG_DISPATCH, "Evaluate SMM DEPEX for FFS(%g)\n", &DriverEntry->FileName));
936 DEBUG ((DEBUG_DISPATCH, " SOR = Not Requested\n"));
937 DEBUG ((DEBUG_DISPATCH, " RESULT = FALSE\n"));
938 }
939 }
940 }
941 } while (ReadyToRun);
942
943 //
944 // If there is no more SMM driver to dispatch, stop the dispatch request
945 //
946 gRequestDispatch = FALSE;
947 for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {
948 DriverEntry = CR (Link, EFI_SMM_DRIVER_ENTRY, Link, EFI_SMM_DRIVER_ENTRY_SIGNATURE);
949
950 if (!DriverEntry->Initialized){
951 //
952 // We have SMM driver pending to dispatch
953 //
954 gRequestDispatch = TRUE;
955 break;
956 }
957 }
958
959 gDispatcherRunning = FALSE;
960
961 return ReturnStatus;
962 }
963
964 /**
965 Insert InsertedDriverEntry onto the mScheduledQueue. To do this you
966 must add any driver with a before dependency on InsertedDriverEntry first.
967 You do this by recursively calling this routine. After all the Befores are
968 processed you can add InsertedDriverEntry to the mScheduledQueue.
969 Then you can add any driver with an After dependency on InsertedDriverEntry
970 by recursively calling this routine.
971
972 @param InsertedDriverEntry The driver to insert on the ScheduledLink Queue
973
974 **/
975 VOID
976 SmmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (
977 IN EFI_SMM_DRIVER_ENTRY *InsertedDriverEntry
978 )
979 {
980 LIST_ENTRY *Link;
981 EFI_SMM_DRIVER_ENTRY *DriverEntry;
982
983 //
984 // Process Before Dependency
985 //
986 for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {
987 DriverEntry = CR(Link, EFI_SMM_DRIVER_ENTRY, Link, EFI_SMM_DRIVER_ENTRY_SIGNATURE);
988 if (DriverEntry->Before && DriverEntry->Dependent && DriverEntry != InsertedDriverEntry) {
989 DEBUG ((DEBUG_DISPATCH, "Evaluate SMM DEPEX for FFS(%g)\n", &DriverEntry->FileName));
990 DEBUG ((DEBUG_DISPATCH, " BEFORE FFS(%g) = ", &DriverEntry->BeforeAfterGuid));
991 if (CompareGuid (&InsertedDriverEntry->FileName, &DriverEntry->BeforeAfterGuid)) {
992 //
993 // Recursively process BEFORE
994 //
995 DEBUG ((DEBUG_DISPATCH, "TRUE\n END\n RESULT = TRUE\n"));
996 SmmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry);
997 } else {
998 DEBUG ((DEBUG_DISPATCH, "FALSE\n END\n RESULT = FALSE\n"));
999 }
1000 }
1001 }
1002
1003 //
1004 // Convert driver from Dependent to Scheduled state
1005 //
1006
1007 InsertedDriverEntry->Dependent = FALSE;
1008 InsertedDriverEntry->Scheduled = TRUE;
1009 InsertTailList (&mScheduledQueue, &InsertedDriverEntry->ScheduledLink);
1010
1011
1012 //
1013 // Process After Dependency
1014 //
1015 for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {
1016 DriverEntry = CR(Link, EFI_SMM_DRIVER_ENTRY, Link, EFI_SMM_DRIVER_ENTRY_SIGNATURE);
1017 if (DriverEntry->After && DriverEntry->Dependent && DriverEntry != InsertedDriverEntry) {
1018 DEBUG ((DEBUG_DISPATCH, "Evaluate SMM DEPEX for FFS(%g)\n", &DriverEntry->FileName));
1019 DEBUG ((DEBUG_DISPATCH, " AFTER FFS(%g) = ", &DriverEntry->BeforeAfterGuid));
1020 if (CompareGuid (&InsertedDriverEntry->FileName, &DriverEntry->BeforeAfterGuid)) {
1021 //
1022 // Recursively process AFTER
1023 //
1024 DEBUG ((DEBUG_DISPATCH, "TRUE\n END\n RESULT = TRUE\n"));
1025 SmmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry);
1026 } else {
1027 DEBUG ((DEBUG_DISPATCH, "FALSE\n END\n RESULT = FALSE\n"));
1028 }
1029 }
1030 }
1031 }
1032
1033 /**
1034 Return TRUE if the Fv has been processed, FALSE if not.
1035
1036 @param FvHandle The handle of a FV that's being tested
1037
1038 @retval TRUE Fv protocol on FvHandle has been processed
1039 @retval FALSE Fv protocol on FvHandle has not yet been
1040 processed
1041
1042 **/
1043 BOOLEAN
1044 FvHasBeenProcessed (
1045 IN EFI_HANDLE FvHandle
1046 )
1047 {
1048 LIST_ENTRY *Link;
1049 KNOWN_HANDLE *KnownHandle;
1050
1051 for (Link = mFvHandleList.ForwardLink; Link != &mFvHandleList; Link = Link->ForwardLink) {
1052 KnownHandle = CR(Link, KNOWN_HANDLE, Link, KNOWN_HANDLE_SIGNATURE);
1053 if (KnownHandle->Handle == FvHandle) {
1054 return TRUE;
1055 }
1056 }
1057 return FALSE;
1058 }
1059
1060 /**
1061 Remember that Fv protocol on FvHandle has had it's drivers placed on the
1062 mDiscoveredList. This fucntion adds entries on the mFvHandleList. Items are
1063 never removed/freed from the mFvHandleList.
1064
1065 @param FvHandle The handle of a FV that has been processed
1066
1067 **/
1068 VOID
1069 FvIsBeingProcesssed (
1070 IN EFI_HANDLE FvHandle
1071 )
1072 {
1073 KNOWN_HANDLE *KnownHandle;
1074
1075 KnownHandle = AllocatePool (sizeof (KNOWN_HANDLE));
1076 ASSERT (KnownHandle != NULL);
1077
1078 KnownHandle->Signature = KNOWN_HANDLE_SIGNATURE;
1079 KnownHandle->Handle = FvHandle;
1080 InsertTailList (&mFvHandleList, &KnownHandle->Link);
1081 }
1082
1083 /**
1084 Convert FvHandle and DriverName into an EFI device path
1085
1086 @param Fv Fv protocol, needed to read Depex info out of
1087 FLASH.
1088 @param FvHandle Handle for Fv, needed in the
1089 EFI_SMM_DRIVER_ENTRY so that the PE image can be
1090 read out of the FV at a later time.
1091 @param DriverName Name of driver to add to mDiscoveredList.
1092
1093 @return Pointer to device path constructed from FvHandle and DriverName
1094
1095 **/
1096 EFI_DEVICE_PATH_PROTOCOL *
1097 SmmFvToDevicePath (
1098 IN EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv,
1099 IN EFI_HANDLE FvHandle,
1100 IN EFI_GUID *DriverName
1101 )
1102 {
1103 EFI_STATUS Status;
1104 EFI_DEVICE_PATH_PROTOCOL *FvDevicePath;
1105 EFI_DEVICE_PATH_PROTOCOL *FileNameDevicePath;
1106
1107 //
1108 // Remember the device path of the FV
1109 //
1110 Status = gBS->HandleProtocol (FvHandle, &gEfiDevicePathProtocolGuid, (VOID **)&FvDevicePath);
1111 if (EFI_ERROR (Status)) {
1112 FileNameDevicePath = NULL;
1113 } else {
1114 //
1115 // Build a device path to the file in the FV to pass into gBS->LoadImage
1116 //
1117 EfiInitializeFwVolDevicepathNode (&mFvDevicePath.File, DriverName);
1118 SetDevicePathEndNode (&mFvDevicePath.End);
1119
1120 //
1121 // Note: FileNameDevicePath is in DXE memory
1122 //
1123 FileNameDevicePath = AppendDevicePath (
1124 FvDevicePath,
1125 (EFI_DEVICE_PATH_PROTOCOL *)&mFvDevicePath
1126 );
1127 }
1128 return FileNameDevicePath;
1129 }
1130
1131 /**
1132 Add an entry to the mDiscoveredList. Allocate memory to store the DriverEntry,
1133 and initilize any state variables. Read the Depex from the FV and store it
1134 in DriverEntry. Pre-process the Depex to set the SOR, Before and After state.
1135 The Discovered list is never free'ed and contains booleans that represent the
1136 other possible SMM driver states.
1137
1138 @param Fv Fv protocol, needed to read Depex info out of
1139 FLASH.
1140 @param FvHandle Handle for Fv, needed in the
1141 EFI_SMM_DRIVER_ENTRY so that the PE image can be
1142 read out of the FV at a later time.
1143 @param DriverName Name of driver to add to mDiscoveredList.
1144
1145 @retval EFI_SUCCESS If driver was added to the mDiscoveredList.
1146 @retval EFI_ALREADY_STARTED The driver has already been started. Only one
1147 DriverName may be active in the system at any one
1148 time.
1149
1150 **/
1151 EFI_STATUS
1152 SmmAddToDriverList (
1153 IN EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv,
1154 IN EFI_HANDLE FvHandle,
1155 IN EFI_GUID *DriverName
1156 )
1157 {
1158 EFI_SMM_DRIVER_ENTRY *DriverEntry;
1159
1160 //
1161 // Create the Driver Entry for the list. ZeroPool initializes lots of variables to
1162 // NULL or FALSE.
1163 //
1164 DriverEntry = AllocateZeroPool (sizeof (EFI_SMM_DRIVER_ENTRY));
1165 ASSERT (DriverEntry != NULL);
1166
1167 DriverEntry->Signature = EFI_SMM_DRIVER_ENTRY_SIGNATURE;
1168 CopyGuid (&DriverEntry->FileName, DriverName);
1169 DriverEntry->FvHandle = FvHandle;
1170 DriverEntry->Fv = Fv;
1171 DriverEntry->FvFileDevicePath = SmmFvToDevicePath (Fv, FvHandle, DriverName);
1172
1173 SmmGetDepexSectionAndPreProccess (DriverEntry);
1174
1175 InsertTailList (&mDiscoveredList, &DriverEntry->Link);
1176 gRequestDispatch = TRUE;
1177
1178 return EFI_SUCCESS;
1179 }
1180
1181 /**
1182 This function is the main entry point for an SMM handler dispatch
1183 or communicate-based callback.
1184
1185 Event notification that is fired every time a FV dispatch protocol is added.
1186 More than one protocol may have been added when this event is fired, so you
1187 must loop on SmmLocateHandle () to see how many protocols were added and
1188 do the following to each FV:
1189 If the Fv has already been processed, skip it. If the Fv has not been
1190 processed then mark it as being processed, as we are about to process it.
1191 Read the Fv and add any driver in the Fv to the mDiscoveredList.The
1192 mDiscoveredList is never free'ed and contains variables that define
1193 the other states the SMM driver transitions to..
1194 While you are at it read the A Priori file into memory.
1195 Place drivers in the A Priori list onto the mScheduledQueue.
1196
1197 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
1198 @param Context Points to an optional handler context which was specified when the handler was registered.
1199 @param CommBuffer A pointer to a collection of data in memory that will
1200 be conveyed from a non-SMM environment into an SMM environment.
1201 @param CommBufferSize The size of the CommBuffer.
1202
1203 @return Status Code
1204
1205 **/
1206 EFI_STATUS
1207 EFIAPI
1208 SmmDriverDispatchHandler (
1209 IN EFI_HANDLE DispatchHandle,
1210 IN CONST VOID *Context, OPTIONAL
1211 IN OUT VOID *CommBuffer, OPTIONAL
1212 IN OUT UINTN *CommBufferSize OPTIONAL
1213 )
1214 {
1215 EFI_STATUS Status;
1216 UINTN HandleCount;
1217 EFI_HANDLE *HandleBuffer;
1218 EFI_STATUS GetNextFileStatus;
1219 EFI_STATUS SecurityStatus;
1220 EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;
1221 EFI_DEVICE_PATH_PROTOCOL *FvDevicePath;
1222 EFI_HANDLE FvHandle;
1223 EFI_GUID NameGuid;
1224 UINTN Key;
1225 EFI_FV_FILETYPE Type;
1226 EFI_FV_FILE_ATTRIBUTES Attributes;
1227 UINTN Size;
1228 EFI_SMM_DRIVER_ENTRY *DriverEntry;
1229 EFI_GUID *AprioriFile;
1230 UINTN AprioriEntryCount;
1231 UINTN Index;
1232 LIST_ENTRY *Link;
1233 UINT32 AuthenticationStatus;
1234 UINTN SizeOfBuffer;
1235
1236 HandleBuffer = NULL;
1237 Status = gBS->LocateHandleBuffer (
1238 ByProtocol,
1239 &gEfiFirmwareVolume2ProtocolGuid,
1240 NULL,
1241 &HandleCount,
1242 &HandleBuffer
1243 );
1244 if (EFI_ERROR (Status)) {
1245 return EFI_NOT_FOUND;
1246 }
1247
1248 for (Index = 0; Index < HandleCount; Index++) {
1249 FvHandle = HandleBuffer[Index];
1250
1251 if (FvHasBeenProcessed (FvHandle)) {
1252 //
1253 // This Fv has already been processed so lets skip it!
1254 //
1255 continue;
1256 }
1257
1258 //
1259 // Since we are about to process this Fv mark it as processed.
1260 //
1261 FvIsBeingProcesssed (FvHandle);
1262
1263 Status = gBS->HandleProtocol (FvHandle, &gEfiFirmwareVolume2ProtocolGuid, (VOID **)&Fv);
1264 if (EFI_ERROR (Status)) {
1265 //
1266 // FvHandle must have a Firmware Volume2 Protocol thus we should never get here.
1267 //
1268 ASSERT (FALSE);
1269 continue;
1270 }
1271
1272 Status = gBS->HandleProtocol (FvHandle, &gEfiDevicePathProtocolGuid, (VOID **)&FvDevicePath);
1273 if (EFI_ERROR (Status)) {
1274 //
1275 // The Firmware volume doesn't have device path, can't be dispatched.
1276 //
1277 continue;
1278 }
1279
1280 //
1281 // If the Security Architectural Protocol has not been located yet, then attempt to locate it
1282 //
1283 if (mSecurity == NULL) {
1284 gBS->LocateProtocol (&gEfiSecurityArchProtocolGuid, NULL, (VOID**)&mSecurity);
1285 }
1286
1287 //
1288 // Evaluate the authentication status of the Firmware Volume through
1289 // Security Architectural Protocol
1290 //
1291 if (mSecurity != NULL) {
1292 SecurityStatus = mSecurity->FileAuthenticationState (
1293 mSecurity,
1294 0,
1295 FvDevicePath
1296 );
1297 if (SecurityStatus != EFI_SUCCESS) {
1298 //
1299 // Security check failed. The firmware volume should not be used for any purpose.
1300 //
1301 continue;
1302 }
1303 }
1304
1305 //
1306 // Discover Drivers in FV and add them to the Discovered Driver List.
1307 // Process EFI_FV_FILETYPE_SMM type and then EFI_FV_FILETYPE_COMBINED_SMM_DXE
1308 //
1309 for (Index = 0; Index < sizeof (mSmmFileTypes)/sizeof (EFI_FV_FILETYPE); Index++) {
1310 //
1311 // Initialize the search key
1312 //
1313 Key = 0;
1314 do {
1315 Type = mSmmFileTypes[Index];
1316 GetNextFileStatus = Fv->GetNextFile (
1317 Fv,
1318 &Key,
1319 &Type,
1320 &NameGuid,
1321 &Attributes,
1322 &Size
1323 );
1324 if (!EFI_ERROR (GetNextFileStatus)) {
1325 SmmAddToDriverList (Fv, FvHandle, &NameGuid);
1326 }
1327 } while (!EFI_ERROR (GetNextFileStatus));
1328 }
1329
1330 //
1331 // Read the array of GUIDs from the Apriori file if it is present in the firmware volume
1332 // (Note: AprioriFile is in DXE memory)
1333 //
1334 AprioriFile = NULL;
1335 Status = Fv->ReadSection (
1336 Fv,
1337 &gAprioriGuid,
1338 EFI_SECTION_RAW,
1339 0,
1340 (VOID **)&AprioriFile,
1341 &SizeOfBuffer,
1342 &AuthenticationStatus
1343 );
1344 if (!EFI_ERROR (Status)) {
1345 AprioriEntryCount = SizeOfBuffer / sizeof (EFI_GUID);
1346 } else {
1347 AprioriEntryCount = 0;
1348 }
1349
1350 //
1351 // Put drivers on Apriori List on the Scheduled queue. The Discovered List includes
1352 // drivers not in the current FV and these must be skipped since the a priori list
1353 // is only valid for the FV that it resided in.
1354 //
1355
1356 for (Index = 0; Index < AprioriEntryCount; Index++) {
1357 for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {
1358 DriverEntry = CR(Link, EFI_SMM_DRIVER_ENTRY, Link, EFI_SMM_DRIVER_ENTRY_SIGNATURE);
1359 if (CompareGuid (&DriverEntry->FileName, &AprioriFile[Index]) &&
1360 (FvHandle == DriverEntry->FvHandle)) {
1361 DriverEntry->Dependent = FALSE;
1362 DriverEntry->Scheduled = TRUE;
1363 InsertTailList (&mScheduledQueue, &DriverEntry->ScheduledLink);
1364 DEBUG ((DEBUG_DISPATCH, "Evaluate SMM DEPEX for FFS(%g)\n", &DriverEntry->FileName));
1365 DEBUG ((DEBUG_DISPATCH, " RESULT = TRUE (Apriori)\n"));
1366 break;
1367 }
1368 }
1369 }
1370
1371 //
1372 // Free data allocated by Fv->ReadSection ()
1373 //
1374 // The UEFI Boot Services FreePool() function must be used because Fv->ReadSection
1375 // used the UEFI Boot Services AllocatePool() function
1376 //
1377 gBS->FreePool (AprioriFile);
1378 }
1379
1380 //
1381 // Execute the SMM Dispatcher on any newly discovered FVs and previously
1382 // discovered SMM drivers that have been discovered but not dispatched.
1383 //
1384 return SmmDispatcher ();
1385 }
1386
1387 /**
1388 Traverse the discovered list for any drivers that were discovered but not loaded
1389 because the dependency experessions evaluated to false.
1390
1391 **/
1392 VOID
1393 SmmDisplayDiscoveredNotDispatched (
1394 VOID
1395 )
1396 {
1397 LIST_ENTRY *Link;
1398 EFI_SMM_DRIVER_ENTRY *DriverEntry;
1399
1400 for (Link = mDiscoveredList.ForwardLink;Link !=&mDiscoveredList; Link = Link->ForwardLink) {
1401 DriverEntry = CR(Link, EFI_SMM_DRIVER_ENTRY, Link, EFI_SMM_DRIVER_ENTRY_SIGNATURE);
1402 if (DriverEntry->Dependent) {
1403 DEBUG ((DEBUG_LOAD, "SMM Driver %g was discovered but not loaded!!\n", &DriverEntry->FileName));
1404 }
1405 }
1406 }