]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Core/PiSmmCore/Dispatcher.c
Fixed a bug for OPTION_NII(62) defined in RFC 5970.
[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
785 return EFI_SUCCESS;\r
786 }\r
787 }\r
788 return EFI_NOT_FOUND;\r
789}\r
790\r
791/**\r
792 This is the main Dispatcher for SMM and it exits when there are no more\r
793 drivers to run. Drain the mScheduledQueue and load and start a PE\r
794 image for each driver. Search the mDiscoveredList to see if any driver can\r
795 be placed on the mScheduledQueue. If no drivers are placed on the\r
796 mScheduledQueue exit the function. On exit it is assumed the Bds()\r
797 will be called, and when the Bds() exits the Dispatcher will be called\r
798 again.\r
799\r
800 @retval EFI_ALREADY_STARTED The SMM Dispatcher is already running\r
801 @retval EFI_NOT_FOUND No SMM Drivers were dispatched\r
802 @retval EFI_SUCCESS One or more SMM Drivers were dispatched\r
803\r
804**/\r
805EFI_STATUS\r
806SmmDispatcher (\r
807 VOID\r
808 )\r
809{\r
810 EFI_STATUS Status;\r
811 EFI_STATUS ReturnStatus;\r
812 LIST_ENTRY *Link;\r
813 EFI_SMM_DRIVER_ENTRY *DriverEntry;\r
814 BOOLEAN ReadyToRun;\r
815\r
816 if (!gRequestDispatch) {\r
817 return EFI_NOT_FOUND;\r
818 }\r
819\r
820 if (gDispatcherRunning) {\r
821 //\r
822 // If the dispatcher is running don't let it be restarted.\r
823 //\r
824 return EFI_ALREADY_STARTED;\r
825 }\r
826\r
827 gDispatcherRunning = TRUE;\r
828\r
829 ReturnStatus = EFI_NOT_FOUND;\r
830 do {\r
831 //\r
832 // Drain the Scheduled Queue\r
833 //\r
834 while (!IsListEmpty (&mScheduledQueue)) {\r
835 DriverEntry = CR (\r
836 mScheduledQueue.ForwardLink,\r
837 EFI_SMM_DRIVER_ENTRY,\r
838 ScheduledLink,\r
839 EFI_SMM_DRIVER_ENTRY_SIGNATURE\r
840 );\r
841\r
842 //\r
843 // Load the SMM Driver image into memory. If the Driver was transitioned from\r
844 // Untrused to Scheduled it would have already been loaded so we may need to\r
845 // skip the LoadImage\r
846 //\r
847 if (DriverEntry->ImageHandle == NULL) {\r
848 Status = SmmLoadImage (DriverEntry);\r
849\r
850 //\r
851 // Update the driver state to reflect that it's been loaded\r
852 //\r
853 if (EFI_ERROR (Status)) {\r
854\r
855 if (Status == EFI_SECURITY_VIOLATION) {\r
856 //\r
857 // Take driver from Scheduled to Untrused state\r
858 //\r
859 DriverEntry->Untrusted = TRUE;\r
860 } else {\r
861 //\r
862 // The SMM Driver could not be loaded, and do not attempt to load or start it again.\r
863 // Take driver from Scheduled to Initialized.\r
864 //\r
865 // This case include the Never Trusted state if EFI_ACCESS_DENIED is returned\r
866 //\r
867 DriverEntry->Initialized = TRUE;\r
868 }\r
869\r
870 DriverEntry->Scheduled = FALSE;\r
871 RemoveEntryList (&DriverEntry->ScheduledLink);\r
872\r
873 //\r
874 // If it's an error don't try the StartImage\r
875 //\r
876 continue;\r
877 }\r
878 }\r
879\r
880 DriverEntry->Scheduled = FALSE;\r
881 DriverEntry->Initialized = TRUE;\r
882 RemoveEntryList (&DriverEntry->ScheduledLink);\r
883\r
884 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (\r
885 EFI_PROGRESS_CODE,\r
886 EFI_SOFTWARE_SMM_DRIVER | EFI_SW_PC_INIT_BEGIN,\r
887 &DriverEntry->ImageHandle,\r
888 sizeof (DriverEntry->ImageHandle)\r
889 );\r
890\r
891 //\r
892 // For each SMM driver, pass NULL as ImageHandle\r
893 //\r
894 Status = ((EFI_IMAGE_ENTRY_POINT)(UINTN)DriverEntry->ImageEntryPoint)(DriverEntry->ImageHandle, gST);\r
895 if (EFI_ERROR(Status)){\r
896 SmmFreePages(DriverEntry->ImageBuffer, DriverEntry->NumberOfPage);\r
897 }\r
898\r
899 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (\r
900 EFI_PROGRESS_CODE,\r
901 EFI_SOFTWARE_SMM_DRIVER | EFI_SW_PC_INIT_END,\r
902 &DriverEntry->ImageHandle,\r
903 sizeof (DriverEntry->ImageHandle)\r
904 );\r
905\r
906 ReturnStatus = EFI_SUCCESS;\r
907 }\r
908\r
909 //\r
910 // Search DriverList for items to place on Scheduled Queue\r
911 //\r
912 ReadyToRun = FALSE;\r
913 for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {\r
914 DriverEntry = CR (Link, EFI_SMM_DRIVER_ENTRY, Link, EFI_SMM_DRIVER_ENTRY_SIGNATURE);\r
915\r
916 if (DriverEntry->DepexProtocolError){\r
917 //\r
918 // If Section Extraction Protocol did not let the Depex be read before retry the read\r
919 //\r
920 Status = SmmGetDepexSectionAndPreProccess (DriverEntry);\r
921 }\r
922\r
923 if (DriverEntry->Dependent) {\r
924 if (SmmIsSchedulable (DriverEntry)) {\r
925 SmmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry);\r
926 ReadyToRun = TRUE;\r
927 }\r
928 }\r
929 }\r
930 } while (ReadyToRun);\r
931\r
932 //\r
933 // If there is no more SMM driver to dispatch, stop the dispatch request\r
934 //\r
935 gRequestDispatch = FALSE;\r
936 for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {\r
937 DriverEntry = CR (Link, EFI_SMM_DRIVER_ENTRY, Link, EFI_SMM_DRIVER_ENTRY_SIGNATURE);\r
938\r
939 if (!DriverEntry->Initialized){\r
940 //\r
941 // We have SMM driver pending to dispatch\r
942 //\r
943 gRequestDispatch = TRUE;\r
944 break;\r
945 }\r
946 }\r
947\r
948 gDispatcherRunning = FALSE;\r
949\r
950 return ReturnStatus;\r
951}\r
952\r
953/**\r
954 Insert InsertedDriverEntry onto the mScheduledQueue. To do this you\r
955 must add any driver with a before dependency on InsertedDriverEntry first.\r
956 You do this by recursively calling this routine. After all the Befores are\r
957 processed you can add InsertedDriverEntry to the mScheduledQueue.\r
958 Then you can add any driver with an After dependency on InsertedDriverEntry\r
959 by recursively calling this routine.\r
960\r
961 @param InsertedDriverEntry The driver to insert on the ScheduledLink Queue\r
962\r
963**/\r
964VOID\r
965SmmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (\r
966 IN EFI_SMM_DRIVER_ENTRY *InsertedDriverEntry\r
967 )\r
968{\r
969 LIST_ENTRY *Link;\r
970 EFI_SMM_DRIVER_ENTRY *DriverEntry;\r
971\r
972 //\r
973 // Process Before Dependency\r
974 //\r
975 for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {\r
976 DriverEntry = CR(Link, EFI_SMM_DRIVER_ENTRY, Link, EFI_SMM_DRIVER_ENTRY_SIGNATURE);\r
977 if (DriverEntry->Before && DriverEntry->Dependent) {\r
978 if (CompareGuid (&InsertedDriverEntry->FileName, &DriverEntry->BeforeAfterGuid)) {\r
979 //\r
980 // Recursively process BEFORE\r
981 //\r
982 SmmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry);\r
983 }\r
984 }\r
985 }\r
986\r
987 //\r
988 // Convert driver from Dependent to Scheduled state\r
989 //\r
990\r
991 InsertedDriverEntry->Dependent = FALSE;\r
992 InsertedDriverEntry->Scheduled = TRUE;\r
993 InsertTailList (&mScheduledQueue, &InsertedDriverEntry->ScheduledLink);\r
994\r
995\r
996 //\r
997 // Process After Dependency\r
998 //\r
999 for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {\r
1000 DriverEntry = CR(Link, EFI_SMM_DRIVER_ENTRY, Link, EFI_SMM_DRIVER_ENTRY_SIGNATURE);\r
1001 if (DriverEntry->After && DriverEntry->Dependent) {\r
1002 if (CompareGuid (&InsertedDriverEntry->FileName, &DriverEntry->BeforeAfterGuid)) {\r
1003 //\r
1004 // Recursively process AFTER\r
1005 //\r
1006 SmmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry);\r
1007 }\r
1008 }\r
1009 }\r
1010}\r
1011\r
1012/**\r
1013 Return TRUE if the Fv has been processed, FALSE if not.\r
1014\r
1015 @param FvHandle The handle of a FV that's being tested\r
1016\r
1017 @retval TRUE Fv protocol on FvHandle has been processed\r
1018 @retval FALSE Fv protocol on FvHandle has not yet been\r
1019 processed\r
1020\r
1021**/\r
1022BOOLEAN\r
1023FvHasBeenProcessed (\r
1024 IN EFI_HANDLE FvHandle\r
1025 )\r
1026{\r
1027 LIST_ENTRY *Link;\r
1028 KNOWN_HANDLE *KnownHandle;\r
1029\r
1030 for (Link = mFvHandleList.ForwardLink; Link != &mFvHandleList; Link = Link->ForwardLink) {\r
1031 KnownHandle = CR(Link, KNOWN_HANDLE, Link, KNOWN_HANDLE_SIGNATURE);\r
1032 if (KnownHandle->Handle == FvHandle) {\r
1033 return TRUE;\r
1034 }\r
1035 }\r
1036 return FALSE;\r
1037}\r
1038\r
1039/**\r
1040 Remember that Fv protocol on FvHandle has had it's drivers placed on the\r
1041 mDiscoveredList. This fucntion adds entries on the mFvHandleList. Items are\r
1042 never removed/freed from the mFvHandleList.\r
1043\r
1044 @param FvHandle The handle of a FV that has been processed\r
1045\r
1046**/\r
1047VOID\r
1048FvIsBeingProcesssed (\r
1049 IN EFI_HANDLE FvHandle\r
1050 )\r
1051{\r
1052 KNOWN_HANDLE *KnownHandle;\r
1053\r
1054 KnownHandle = AllocatePool (sizeof (KNOWN_HANDLE));\r
1055 ASSERT (KnownHandle != NULL);\r
1056\r
1057 KnownHandle->Signature = KNOWN_HANDLE_SIGNATURE;\r
1058 KnownHandle->Handle = FvHandle;\r
1059 InsertTailList (&mFvHandleList, &KnownHandle->Link);\r
1060}\r
1061\r
1062/**\r
1063 Convert FvHandle and DriverName into an EFI device path\r
1064\r
1065 @param Fv Fv protocol, needed to read Depex info out of\r
1066 FLASH.\r
1067 @param FvHandle Handle for Fv, needed in the\r
1068 EFI_SMM_DRIVER_ENTRY so that the PE image can be\r
1069 read out of the FV at a later time.\r
1070 @param DriverName Name of driver to add to mDiscoveredList.\r
1071\r
1072 @return Pointer to device path constructed from FvHandle and DriverName\r
1073\r
1074**/\r
1075EFI_DEVICE_PATH_PROTOCOL *\r
1076SmmFvToDevicePath (\r
1077 IN EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv,\r
1078 IN EFI_HANDLE FvHandle,\r
1079 IN EFI_GUID *DriverName\r
1080 )\r
1081{\r
1082 EFI_STATUS Status;\r
1083 EFI_DEVICE_PATH_PROTOCOL *FvDevicePath;\r
1084 EFI_DEVICE_PATH_PROTOCOL *FileNameDevicePath;\r
1085\r
1086 //\r
1087 // Remember the device path of the FV\r
1088 //\r
1089 Status = gBS->HandleProtocol (FvHandle, &gEfiDevicePathProtocolGuid, (VOID **)&FvDevicePath);\r
1090 if (EFI_ERROR (Status)) {\r
1091 FileNameDevicePath = NULL;\r
1092 } else {\r
1093 //\r
1094 // Build a device path to the file in the FV to pass into gBS->LoadImage\r
1095 //\r
1096 EfiInitializeFwVolDevicepathNode (&mFvDevicePath.File, DriverName);\r
1097 SetDevicePathEndNode (&mFvDevicePath.End);\r
1098\r
1099 //\r
1100 // Note: FileNameDevicePath is in DXE memory\r
1101 //\r
1102 FileNameDevicePath = AppendDevicePath (\r
1103 FvDevicePath,\r
1104 (EFI_DEVICE_PATH_PROTOCOL *)&mFvDevicePath\r
1105 );\r
1106 }\r
1107 return FileNameDevicePath;\r
1108}\r
1109\r
1110/**\r
1111 Add an entry to the mDiscoveredList. Allocate memory to store the DriverEntry,\r
1112 and initilize any state variables. Read the Depex from the FV and store it\r
1113 in DriverEntry. Pre-process the Depex to set the SOR, Before and After state.\r
1114 The Discovered list is never free'ed and contains booleans that represent the\r
1115 other possible SMM driver states.\r
1116\r
1117 @param Fv Fv protocol, needed to read Depex info out of\r
1118 FLASH.\r
1119 @param FvHandle Handle for Fv, needed in the\r
1120 EFI_SMM_DRIVER_ENTRY so that the PE image can be\r
1121 read out of the FV at a later time.\r
1122 @param DriverName Name of driver to add to mDiscoveredList.\r
1123\r
1124 @retval EFI_SUCCESS If driver was added to the mDiscoveredList.\r
1125 @retval EFI_ALREADY_STARTED The driver has already been started. Only one\r
1126 DriverName may be active in the system at any one\r
1127 time.\r
1128\r
1129**/\r
1130EFI_STATUS\r
1131SmmAddToDriverList (\r
1132 IN EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv,\r
1133 IN EFI_HANDLE FvHandle,\r
1134 IN EFI_GUID *DriverName\r
1135 )\r
1136{\r
1137 EFI_SMM_DRIVER_ENTRY *DriverEntry;\r
1138\r
1139 //\r
1140 // Create the Driver Entry for the list. ZeroPool initializes lots of variables to\r
1141 // NULL or FALSE.\r
1142 //\r
1143 DriverEntry = AllocateZeroPool (sizeof (EFI_SMM_DRIVER_ENTRY));\r
1144 ASSERT (DriverEntry != NULL);\r
1145\r
1146 DriverEntry->Signature = EFI_SMM_DRIVER_ENTRY_SIGNATURE;\r
1147 CopyGuid (&DriverEntry->FileName, DriverName);\r
1148 DriverEntry->FvHandle = FvHandle;\r
1149 DriverEntry->Fv = Fv;\r
1150 DriverEntry->FvFileDevicePath = SmmFvToDevicePath (Fv, FvHandle, DriverName);\r
1151\r
1152 SmmGetDepexSectionAndPreProccess (DriverEntry);\r
1153\r
1154 InsertTailList (&mDiscoveredList, &DriverEntry->Link);\r
1155 gRequestDispatch = TRUE;\r
1156\r
1157 return EFI_SUCCESS;\r
1158}\r
1159\r
1160/**\r
1161 This function is the main entry point for an SMM handler dispatch\r
1162 or communicate-based callback.\r
1163\r
1164 Event notification that is fired every time a FV dispatch protocol is added.\r
1165 More than one protocol may have been added when this event is fired, so you\r
1166 must loop on SmmLocateHandle () to see how many protocols were added and\r
1167 do the following to each FV:\r
1168 If the Fv has already been processed, skip it. If the Fv has not been\r
1169 processed then mark it as being processed, as we are about to process it.\r
1170 Read the Fv and add any driver in the Fv to the mDiscoveredList.The\r
1171 mDiscoveredList is never free'ed and contains variables that define\r
1172 the other states the SMM driver transitions to..\r
1173 While you are at it read the A Priori file into memory.\r
1174 Place drivers in the A Priori list onto the mScheduledQueue.\r
1175\r
1176 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().\r
1177 @param Context Points to an optional handler context which was specified when the handler was registered.\r
1178 @param CommBuffer A pointer to a collection of data in memory that will\r
1179 be conveyed from a non-SMM environment into an SMM environment.\r
1180 @param CommBufferSize The size of the CommBuffer.\r
1181\r
1182 @return Status Code\r
1183\r
1184**/\r
1185EFI_STATUS\r
1186EFIAPI\r
1187SmmDriverDispatchHandler (\r
1188 IN EFI_HANDLE DispatchHandle,\r
1189 IN CONST VOID *Context, OPTIONAL\r
1190 IN OUT VOID *CommBuffer, OPTIONAL\r
1191 IN OUT UINTN *CommBufferSize OPTIONAL\r
1192 )\r
1193{\r
1194 EFI_STATUS Status;\r
1195 UINTN HandleCount;\r
1196 EFI_HANDLE *HandleBuffer;\r
1197 EFI_STATUS GetNextFileStatus;\r
1198 EFI_STATUS SecurityStatus;\r
1199 EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;\r
1200 EFI_DEVICE_PATH_PROTOCOL *FvDevicePath;\r
1201 EFI_HANDLE FvHandle;\r
1202 EFI_GUID NameGuid;\r
1203 UINTN Key;\r
1204 EFI_FV_FILETYPE Type;\r
1205 EFI_FV_FILE_ATTRIBUTES Attributes;\r
1206 UINTN Size;\r
1207 EFI_SMM_DRIVER_ENTRY *DriverEntry;\r
1208 EFI_GUID *AprioriFile;\r
1209 UINTN AprioriEntryCount;\r
1210 UINTN Index;\r
1211 LIST_ENTRY *Link;\r
1212 UINT32 AuthenticationStatus;\r
1213 UINTN SizeOfBuffer;\r
1214\r
1215 HandleBuffer = NULL;\r
1216 Status = gBS->LocateHandleBuffer (\r
1217 ByProtocol,\r
1218 &gEfiFirmwareVolume2ProtocolGuid,\r
1219 NULL,\r
1220 &HandleCount,\r
1221 &HandleBuffer\r
1222 );\r
1223 if (EFI_ERROR (Status)) {\r
1224 return EFI_NOT_FOUND;\r
1225 }\r
1226\r
1227 for (Index = 0; Index < HandleCount; Index++) {\r
1228 FvHandle = HandleBuffer[Index];\r
1229\r
1230 if (FvHasBeenProcessed (FvHandle)) {\r
1231 //\r
1232 // This Fv has already been processed so lets skip it!\r
1233 //\r
1234 continue;\r
1235 }\r
1236\r
1237 //\r
1238 // Since we are about to process this Fv mark it as processed.\r
1239 //\r
1240 FvIsBeingProcesssed (FvHandle);\r
1241\r
1242 Status = gBS->HandleProtocol (FvHandle, &gEfiFirmwareVolume2ProtocolGuid, (VOID **)&Fv);\r
1243 if (EFI_ERROR (Status)) {\r
1244 //\r
1245 // FvHandle must have a Firmware Volume2 Protocol thus we should never get here.\r
1246 //\r
1247 ASSERT (FALSE);\r
1248 continue;\r
1249 }\r
1250\r
1251 Status = gBS->HandleProtocol (FvHandle, &gEfiDevicePathProtocolGuid, (VOID **)&FvDevicePath);\r
1252 if (EFI_ERROR (Status)) {\r
1253 //\r
1254 // The Firmware volume doesn't have device path, can't be dispatched.\r
1255 //\r
1256 continue;\r
1257 }\r
1258\r
1259 //\r
1260 // If the Security Architectural Protocol has not been located yet, then attempt to locate it\r
1261 //\r
1262 if (mSecurity == NULL) {\r
1263 gBS->LocateProtocol (&gEfiSecurityArchProtocolGuid, NULL, (VOID**)&mSecurity);\r
1264 }\r
1265\r
1266 //\r
1267 // Evaluate the authentication status of the Firmware Volume through\r
1268 // Security Architectural Protocol\r
1269 //\r
1270 if (mSecurity != NULL) {\r
1271 SecurityStatus = mSecurity->FileAuthenticationState (\r
1272 mSecurity,\r
1273 0,\r
1274 FvDevicePath\r
1275 );\r
1276 if (SecurityStatus != EFI_SUCCESS) {\r
1277 //\r
1278 // Security check failed. The firmware volume should not be used for any purpose.\r
1279 //\r
1280 continue;\r
1281 }\r
1282 }\r
1283\r
1284 //\r
1285 // Discover Drivers in FV and add them to the Discovered Driver List.\r
1286 // Process EFI_FV_FILETYPE_SMM type and then EFI_FV_FILETYPE_COMBINED_SMM_DXE\r
1287 //\r
1288 for (Index = 0; Index < sizeof (mSmmFileTypes)/sizeof (EFI_FV_FILETYPE); Index++) {\r
1289 //\r
1290 // Initialize the search key\r
1291 //\r
1292 Key = 0;\r
1293 do {\r
1294 Type = mSmmFileTypes[Index];\r
1295 GetNextFileStatus = Fv->GetNextFile (\r
1296 Fv,\r
1297 &Key,\r
1298 &Type,\r
1299 &NameGuid,\r
1300 &Attributes,\r
1301 &Size\r
1302 );\r
1303 if (!EFI_ERROR (GetNextFileStatus)) {\r
1304 SmmAddToDriverList (Fv, FvHandle, &NameGuid);\r
1305 }\r
1306 } while (!EFI_ERROR (GetNextFileStatus));\r
1307 }\r
1308\r
1309 //\r
1310 // Read the array of GUIDs from the Apriori file if it is present in the firmware volume\r
1311 // (Note: AprioriFile is in DXE memory)\r
1312 //\r
1313 AprioriFile = NULL;\r
1314 Status = Fv->ReadSection (\r
1315 Fv,\r
1316 &gAprioriGuid,\r
1317 EFI_SECTION_RAW,\r
1318 0,\r
1319 (VOID **)&AprioriFile,\r
1320 &SizeOfBuffer,\r
1321 &AuthenticationStatus\r
1322 );\r
1323 if (!EFI_ERROR (Status)) {\r
1324 AprioriEntryCount = SizeOfBuffer / sizeof (EFI_GUID);\r
1325 } else {\r
1326 AprioriEntryCount = 0;\r
1327 }\r
1328\r
1329 //\r
1330 // Put drivers on Apriori List on the Scheduled queue. The Discovered List includes\r
1331 // drivers not in the current FV and these must be skipped since the a priori list\r
1332 // is only valid for the FV that it resided in.\r
1333 //\r
1334\r
1335 for (Index = 0; Index < AprioriEntryCount; Index++) {\r
1336 for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {\r
1337 DriverEntry = CR(Link, EFI_SMM_DRIVER_ENTRY, Link, EFI_SMM_DRIVER_ENTRY_SIGNATURE);\r
1338 if (CompareGuid (&DriverEntry->FileName, &AprioriFile[Index]) &&\r
1339 (FvHandle == DriverEntry->FvHandle)) {\r
1340 DriverEntry->Dependent = FALSE;\r
1341 DriverEntry->Scheduled = TRUE;\r
1342 InsertTailList (&mScheduledQueue, &DriverEntry->ScheduledLink);\r
1343 break;\r
1344 }\r
1345 }\r
1346 }\r
1347\r
1348 //\r
1349 // Free data allocated by Fv->ReadSection ()\r
1350 //\r
1351 // The UEFI Boot Services FreePool() function must be used because Fv->ReadSection \r
1352 // used the UEFI Boot Services AllocatePool() function\r
1353 //\r
1354 gBS->FreePool (AprioriFile);\r
1355 }\r
1356\r
1357 //\r
1358 // Execute the SMM Dispatcher on any newly discovered FVs and previously \r
1359 // discovered SMM drivers that have been discovered but not dispatched.\r
1360 //\r
1361 return SmmDispatcher ();\r
1362}\r
1363\r
1364/**\r
1365 Traverse the discovered list for any drivers that were discovered but not loaded\r
1366 because the dependency experessions evaluated to false.\r
1367\r
1368**/\r
1369VOID\r
1370SmmDisplayDiscoveredNotDispatched (\r
1371 VOID\r
1372 )\r
1373{\r
1374 LIST_ENTRY *Link;\r
1375 EFI_SMM_DRIVER_ENTRY *DriverEntry;\r
1376\r
1377 for (Link = mDiscoveredList.ForwardLink;Link !=&mDiscoveredList; Link = Link->ForwardLink) {\r
1378 DriverEntry = CR(Link, EFI_SMM_DRIVER_ENTRY, Link, EFI_SMM_DRIVER_ENTRY_SIGNATURE);\r
1379 if (DriverEntry->Dependent) {\r
1380 DEBUG ((DEBUG_LOAD, "SMM Driver %g was discovered but not loaded!!\n", &DriverEntry->FileName));\r
1381 }\r
1382 }\r
1383}\r