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