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