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