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