]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Core/PiSmmCore/Dispatcher.c
Change the EFI_BOOT_KEY_DATA definition to use macro instead of bit fields.
[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
b08b7ba4 30 Copyright (c) 2009 - 2012, 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
c2cb08df
LG
323 UINT64 Tick;\r
324\r
325 Tick = 0;\r
326 PERF_CODE (\r
327 Tick = GetPerformanceCounter ();\r
328 );\r
e42e9404 329 \r
330 Buffer = NULL;\r
331 Size = 0;\r
332 Fv = DriverEntry->Fv;\r
333 NameGuid = &DriverEntry->FileName;\r
334 FilePath = DriverEntry->FvFileDevicePath;\r
335\r
336 OriginalFilePath = FilePath;\r
337 HandleFilePath = FilePath;\r
338 DeviceHandle = NULL;\r
339 SecurityStatus = EFI_SUCCESS;\r
340 Status = EFI_SUCCESS;\r
341 AuthenticationStatus = 0;\r
342\r
343 //\r
344 // Try to get the image device handle by checking the match protocol.\r
345 //\r
346 Status = gBS->LocateDevicePath (&gEfiFirmwareVolume2ProtocolGuid, &HandleFilePath, &DeviceHandle);\r
347 if (EFI_ERROR(Status)) {\r
348 return Status;\r
349 }\r
350\r
351 //\r
352 // If the Security Architectural Protocol has not been located yet, then attempt to locate it\r
353 //\r
354 if (mSecurity == NULL) {\r
355 gBS->LocateProtocol (&gEfiSecurityArchProtocolGuid, NULL, (VOID**)&mSecurity);\r
356 }\r
357\r
358 //\r
359 // Verify the Authentication Status through the Security Architectural Protocol\r
360 //\r
361 if ((mSecurity != NULL) && (OriginalFilePath != NULL)) {\r
362 SecurityStatus = mSecurity->FileAuthenticationState (\r
363 mSecurity,\r
364 AuthenticationStatus,\r
365 OriginalFilePath\r
366 );\r
367 if (EFI_ERROR (SecurityStatus) && SecurityStatus != EFI_SECURITY_VIOLATION) {\r
368 Status = SecurityStatus;\r
369 return Status;\r
370 }\r
371 }\r
372 \r
373 //\r
374 // Pull out just the file portion of the DevicePath for the LoadedImage FilePath\r
375 //\r
376 FilePath = OriginalFilePath;\r
377 Status = gBS->HandleProtocol (DeviceHandle, &gEfiDevicePathProtocolGuid, (VOID **)&HandleFilePath);\r
378 if (!EFI_ERROR (Status)) {\r
379 FilePathSize = GetDevicePathSize (HandleFilePath) - sizeof(EFI_DEVICE_PATH_PROTOCOL);\r
380 FilePath = (EFI_DEVICE_PATH_PROTOCOL *) (((UINT8 *)FilePath) + FilePathSize );\r
381 }\r
382\r
383 //\r
384 // Try reading PE32 section firstly\r
385 //\r
386 Status = Fv->ReadSection (\r
387 Fv,\r
388 NameGuid,\r
389 EFI_SECTION_PE32,\r
390 0,\r
391 &Buffer,\r
392 &Size,\r
393 &AuthenticationStatus\r
394 );\r
395\r
396 if (EFI_ERROR (Status)) {\r
397 //\r
398 // Try reading TE section secondly\r
399 //\r
400 Buffer = NULL;\r
401 Size = 0;\r
402 Status = Fv->ReadSection (\r
403 Fv,\r
404 NameGuid,\r
405 EFI_SECTION_TE,\r
406 0,\r
407 &Buffer,\r
408 &Size,\r
409 &AuthenticationStatus\r
410 );\r
411 }\r
412 \r
413 if (EFI_ERROR (Status)) {\r
414 if (Buffer != NULL) {\r
b08b7ba4 415 gBS->FreePool (Buffer);\r
e42e9404 416 }\r
417 return Status;\r
418 }\r
419\r
420 //\r
421 // Initialize ImageContext\r
422 //\r
423 ImageContext.Handle = Buffer;\r
424 ImageContext.ImageRead = PeCoffLoaderImageReadFromMemory;\r
425\r
426 //\r
427 // Get information about the image being loaded\r
428 //\r
429 Status = PeCoffLoaderGetImageInfo (&ImageContext);\r
430 if (EFI_ERROR (Status)) {\r
431 if (Buffer != NULL) {\r
b08b7ba4 432 gBS->FreePool (Buffer);\r
e42e9404 433 }\r
434 return Status;\r
435 }\r
3c447c27 436 //\r
437 // if Loading module at Fixed Address feature is enabled, then cut out a memory range started from TESG BASE\r
438 // to hold the Smm driver code\r
439 //\r
440 if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0) {\r
441 //\r
442 // Get the fixed loading address assigned by Build tool\r
443 //\r
444 Status = GetPeCoffImageFixLoadingAssignedAddress (&ImageContext);\r
445 if (!EFI_ERROR (Status)) {\r
446 //\r
447 // Since the memory range to load Smm core alreay been cut out, so no need to allocate and free this range\r
448 // following statements is to bypass SmmFreePages\r
449 //\r
450 PageCount = 0;\r
451 DstBuffer = (UINTN)gLoadModuleAtFixAddressSmramBase; \r
452 } else {\r
453 DEBUG ((EFI_D_INFO|EFI_D_LOAD, "LOADING MODULE FIXED ERROR: Failed to load module at fixed address. \n"));\r
454 //\r
455 // allocate the memory to load the SMM driver\r
456 //\r
e0e7f80c 457 PageCount = (UINTN)EFI_SIZE_TO_PAGES((UINTN)ImageContext.ImageSize + ImageContext.SectionAlignment);\r
3c447c27 458 DstBuffer = (UINTN)(-1);\r
459 \r
460 Status = SmmAllocatePages (\r
461 AllocateMaxAddress,\r
462 EfiRuntimeServicesCode,\r
463 PageCount,\r
464 &DstBuffer\r
465 );\r
466 if (EFI_ERROR (Status)) {\r
467 if (Buffer != NULL) {\r
b08b7ba4 468 gBS->FreePool (Buffer);\r
3c447c27 469 } \r
470 return Status;\r
471 } \r
472 ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)DstBuffer;\r
e42e9404 473 }\r
3c447c27 474 } else {\r
e0e7f80c 475 PageCount = (UINTN)EFI_SIZE_TO_PAGES((UINTN)ImageContext.ImageSize + ImageContext.SectionAlignment);\r
3c447c27 476 DstBuffer = (UINTN)(-1);\r
477 \r
478 Status = SmmAllocatePages (\r
479 AllocateMaxAddress,\r
480 EfiRuntimeServicesCode,\r
481 PageCount,\r
482 &DstBuffer\r
483 );\r
484 if (EFI_ERROR (Status)) {\r
485 if (Buffer != NULL) {\r
b08b7ba4 486 gBS->FreePool (Buffer);\r
3c447c27 487 }\r
488 return Status;\r
489 }\r
490 \r
491 ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)DstBuffer;\r
e42e9404 492 }\r
e42e9404 493 //\r
494 // Align buffer on section boundry\r
495 //\r
496 ImageContext.ImageAddress += ImageContext.SectionAlignment - 1;\r
497 ImageContext.ImageAddress &= ~(ImageContext.SectionAlignment - 1);\r
498\r
499 //\r
500 // Load the image to our new buffer\r
501 //\r
502 Status = PeCoffLoaderLoadImage (&ImageContext);\r
503 if (EFI_ERROR (Status)) {\r
504 if (Buffer != NULL) {\r
b08b7ba4 505 gBS->FreePool (Buffer);\r
e42e9404 506 }\r
507 SmmFreePages (DstBuffer, PageCount);\r
508 return Status;\r
509 }\r
510\r
511 //\r
512 // Relocate the image in our new buffer\r
513 //\r
514 Status = PeCoffLoaderRelocateImage (&ImageContext);\r
515 if (EFI_ERROR (Status)) {\r
516 if (Buffer != NULL) {\r
b08b7ba4 517 gBS->FreePool (Buffer);\r
e42e9404 518 }\r
519 SmmFreePages (DstBuffer, PageCount);\r
520 return Status;\r
521 }\r
522\r
523 //\r
524 // Flush the instruction cache so the image data are written before we execute it\r
525 //\r
526 InvalidateInstructionCacheRange ((VOID *)(UINTN) ImageContext.ImageAddress, (UINTN) ImageContext.ImageSize);\r
527\r
528 //\r
529 // Save Image EntryPoint in DriverEntry\r
530 //\r
531 DriverEntry->ImageEntryPoint = ImageContext.EntryPoint;\r
532 DriverEntry->ImageBuffer = DstBuffer; \r
533 DriverEntry->NumberOfPage = PageCount;\r
534\r
535 //\r
536 // Allocate a Loaded Image Protocol in EfiBootServicesData\r
537 //\r
538 Status = gBS->AllocatePool (EfiBootServicesData, sizeof (EFI_LOADED_IMAGE_PROTOCOL), (VOID **)&DriverEntry->LoadedImage);\r
539 if (EFI_ERROR (Status)) {\r
540 if (Buffer != NULL) {\r
b08b7ba4 541 gBS->FreePool (Buffer);\r
e42e9404 542 }\r
543 SmmFreePages (DstBuffer, PageCount);\r
544 return Status;\r
545 }\r
546\r
547 //\r
548 // Fill in the remaining fields of the Loaded Image Protocol instance.\r
549 // Note: ImageBase is an SMRAM address that can not be accessed outside of SMRAM if SMRAM window is closed.\r
550 //\r
551 DriverEntry->LoadedImage->Revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;\r
552 DriverEntry->LoadedImage->ParentHandle = gSmmCorePrivate->SmmIplImageHandle;\r
553 DriverEntry->LoadedImage->SystemTable = gST;\r
554 DriverEntry->LoadedImage->DeviceHandle = DeviceHandle;\r
555\r
556 //\r
557 // Make an EfiBootServicesData buffer copy of FilePath\r
558 //\r
559 Status = gBS->AllocatePool (EfiBootServicesData, GetDevicePathSize (FilePath), (VOID **)&DriverEntry->LoadedImage->FilePath);\r
560 if (EFI_ERROR (Status)) {\r
561 if (Buffer != NULL) {\r
b08b7ba4 562 gBS->FreePool (Buffer);\r
e42e9404 563 }\r
564 SmmFreePages (DstBuffer, PageCount);\r
565 return Status;\r
566 }\r
567 CopyMem (DriverEntry->LoadedImage->FilePath, FilePath, GetDevicePathSize (FilePath));\r
568\r
569 DriverEntry->LoadedImage->ImageBase = (VOID *)(UINTN)DriverEntry->ImageBuffer;\r
570 DriverEntry->LoadedImage->ImageSize = ImageContext.ImageSize;\r
571 DriverEntry->LoadedImage->ImageCodeType = EfiRuntimeServicesCode;\r
572 DriverEntry->LoadedImage->ImageDataType = EfiRuntimeServicesData;\r
573\r
574 //\r
575 // Create a new image handle in the UEFI handle database for the SMM Driver\r
576 //\r
577 DriverEntry->ImageHandle = NULL;\r
578 Status = gBS->InstallMultipleProtocolInterfaces (\r
579 &DriverEntry->ImageHandle,\r
580 &gEfiLoadedImageProtocolGuid, DriverEntry->LoadedImage,\r
581 NULL\r
582 );\r
583\r
c2cb08df
LG
584 PERF_START (DriverEntry->ImageHandle, "LoadImage:", NULL, Tick);\r
585 PERF_END (DriverEntry->ImageHandle, "LoadImage:", NULL, 0);\r
586\r
e42e9404 587 //\r
588 // Print the load address and the PDB file name if it is available\r
589 //\r
590\r
591 DEBUG_CODE_BEGIN ();\r
592\r
593 UINTN Index;\r
594 UINTN StartIndex;\r
595 CHAR8 EfiFileName[256];\r
596\r
597\r
598 DEBUG ((DEBUG_INFO | DEBUG_LOAD,\r
9a33a65e 599 "Loading SMM driver at 0x%11p EntryPoint=0x%11p ",\r
e42e9404 600 (VOID *)(UINTN) ImageContext.ImageAddress,\r
601 FUNCTION_ENTRY_POINT (ImageContext.EntryPoint)));\r
602\r
603\r
604 //\r
605 // Print Module Name by Pdb file path.\r
606 // Windows and Unix style file path are all trimmed correctly.\r
607 //\r
608 if (ImageContext.PdbPointer != NULL) {\r
609 StartIndex = 0;\r
610 for (Index = 0; ImageContext.PdbPointer[Index] != 0; Index++) {\r
611 if ((ImageContext.PdbPointer[Index] == '\\') || (ImageContext.PdbPointer[Index] == '/')) {\r
612 StartIndex = Index + 1;\r
613 }\r
614 }\r
615 //\r
616 // Copy the PDB file name to our temporary string, and replace .pdb with .efi\r
617 // The PDB file name is limited in the range of 0~255.\r
618 // If the length is bigger than 255, trim the redudant characters to avoid overflow in array boundary.\r
619 //\r
620 for (Index = 0; Index < sizeof (EfiFileName) - 4; Index++) {\r
621 EfiFileName[Index] = ImageContext.PdbPointer[Index + StartIndex];\r
622 if (EfiFileName[Index] == 0) {\r
623 EfiFileName[Index] = '.';\r
624 }\r
625 if (EfiFileName[Index] == '.') {\r
626 EfiFileName[Index + 1] = 'e';\r
627 EfiFileName[Index + 2] = 'f';\r
628 EfiFileName[Index + 3] = 'i';\r
629 EfiFileName[Index + 4] = 0;\r
630 break;\r
631 }\r
632 }\r
633\r
634 if (Index == sizeof (EfiFileName) - 4) {\r
635 EfiFileName[Index] = 0;\r
636 }\r
637 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "%a", EfiFileName)); // &Image->ImageContext.PdbPointer[StartIndex]));\r
638 }\r
639 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "\n"));\r
640\r
641 DEBUG_CODE_END ();\r
642\r
643 //\r
644 // Free buffer allocated by Fv->ReadSection.\r
645 //\r
646 // The UEFI Boot Services FreePool() function must be used because Fv->ReadSection \r
647 // used the UEFI Boot Services AllocatePool() function\r
648 //\r
649 Status = gBS->FreePool(Buffer);\r
650 return Status; \r
651}\r
652\r
653/**\r
654 Preprocess dependency expression and update DriverEntry to reflect the\r
fa542a1e 655 state of Before and After dependencies. If DriverEntry->Before\r
656 or DriverEntry->After is set it will never be cleared. \r
e42e9404 657\r
658 @param DriverEntry DriverEntry element to update .\r
659\r
660 @retval EFI_SUCCESS It always works.\r
661\r
662**/\r
663EFI_STATUS\r
664SmmPreProcessDepex (\r
665 IN EFI_SMM_DRIVER_ENTRY *DriverEntry\r
666 )\r
667{\r
668 UINT8 *Iterator;\r
669\r
670 Iterator = DriverEntry->Depex;\r
fa542a1e 671 DriverEntry->Dependent = TRUE;\r
e42e9404 672\r
673 if (*Iterator == EFI_DEP_BEFORE) {\r
674 DriverEntry->Before = TRUE;\r
675 } else if (*Iterator == EFI_DEP_AFTER) {\r
676 DriverEntry->After = TRUE;\r
677 }\r
678\r
679 if (DriverEntry->Before || DriverEntry->After) {\r
680 CopyMem (&DriverEntry->BeforeAfterGuid, Iterator + 1, sizeof (EFI_GUID));\r
681 }\r
682\r
683 return EFI_SUCCESS;\r
684}\r
685\r
686/**\r
687 Read Depex and pre-process the Depex for Before and After. If Section Extraction\r
688 protocol returns an error via ReadSection defer the reading of the Depex.\r
689\r
690 @param DriverEntry Driver to work on.\r
691\r
692 @retval EFI_SUCCESS Depex read and preprossesed\r
693 @retval EFI_PROTOCOL_ERROR The section extraction protocol returned an error\r
694 and Depex reading needs to be retried.\r
695 @retval Error DEPEX not found.\r
696\r
697**/\r
698EFI_STATUS\r
699SmmGetDepexSectionAndPreProccess (\r
700 IN EFI_SMM_DRIVER_ENTRY *DriverEntry\r
701 )\r
702{\r
703 EFI_STATUS Status;\r
704 EFI_SECTION_TYPE SectionType;\r
705 UINT32 AuthenticationStatus;\r
706 EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;\r
707\r
708 Fv = DriverEntry->Fv;\r
709\r
710 //\r
711 // Grab Depex info, it will never be free'ed.\r
712 // (Note: DriverEntry->Depex is in DXE memory)\r
713 //\r
714 SectionType = EFI_SECTION_SMM_DEPEX;\r
715 Status = Fv->ReadSection (\r
716 DriverEntry->Fv,\r
717 &DriverEntry->FileName,\r
718 SectionType,\r
719 0,\r
720 &DriverEntry->Depex,\r
721 (UINTN *)&DriverEntry->DepexSize,\r
722 &AuthenticationStatus\r
723 );\r
724 if (EFI_ERROR (Status)) {\r
725 if (Status == EFI_PROTOCOL_ERROR) {\r
726 //\r
727 // The section extraction protocol failed so set protocol error flag\r
728 //\r
729 DriverEntry->DepexProtocolError = TRUE;\r
730 } else {\r
731 //\r
732 // If no Depex assume depend on all architectural protocols\r
733 //\r
734 DriverEntry->Depex = NULL;\r
735 DriverEntry->Dependent = TRUE;\r
736 DriverEntry->DepexProtocolError = FALSE;\r
737 }\r
738 } else {\r
739 //\r
fa542a1e 740 // Set Before and After state information based on Depex\r
741 // Driver will be put in Dependent state\r
e42e9404 742 //\r
743 SmmPreProcessDepex (DriverEntry);\r
744 DriverEntry->DepexProtocolError = FALSE;\r
745 }\r
746\r
747 return Status;\r
748}\r
749\r
e42e9404 750/**\r
751 This is the main Dispatcher for SMM and it exits when there are no more\r
752 drivers to run. Drain the mScheduledQueue and load and start a PE\r
753 image for each driver. Search the mDiscoveredList to see if any driver can\r
754 be placed on the mScheduledQueue. If no drivers are placed on the\r
5657b268 755 mScheduledQueue exit the function. \r
756\r
757 @retval EFI_SUCCESS All of the SMM Drivers that could be dispatched\r
758 have been run and the SMM Entry Point has been\r
759 registered.\r
760 @retval EFI_NOT_READY The SMM Driver that registered the SMM Entry Point\r
761 was just dispatched.\r
762 @retval EFI_NOT_FOUND There are no SMM Drivers available to be dispatched.\r
e42e9404 763 @retval EFI_ALREADY_STARTED The SMM Dispatcher is already running\r
e42e9404 764\r
765**/\r
766EFI_STATUS\r
767SmmDispatcher (\r
768 VOID\r
769 )\r
770{\r
771 EFI_STATUS Status;\r
e42e9404 772 LIST_ENTRY *Link;\r
773 EFI_SMM_DRIVER_ENTRY *DriverEntry;\r
774 BOOLEAN ReadyToRun;\r
5657b268 775 BOOLEAN PreviousSmmEntryPointRegistered;\r
e42e9404 776\r
777 if (!gRequestDispatch) {\r
778 return EFI_NOT_FOUND;\r
779 }\r
780\r
781 if (gDispatcherRunning) {\r
782 //\r
783 // If the dispatcher is running don't let it be restarted.\r
784 //\r
785 return EFI_ALREADY_STARTED;\r
786 }\r
787\r
788 gDispatcherRunning = TRUE;\r
789\r
e42e9404 790 do {\r
791 //\r
792 // Drain the Scheduled Queue\r
793 //\r
794 while (!IsListEmpty (&mScheduledQueue)) {\r
795 DriverEntry = CR (\r
796 mScheduledQueue.ForwardLink,\r
797 EFI_SMM_DRIVER_ENTRY,\r
798 ScheduledLink,\r
799 EFI_SMM_DRIVER_ENTRY_SIGNATURE\r
800 );\r
801\r
802 //\r
803 // Load the SMM Driver image into memory. If the Driver was transitioned from\r
804 // Untrused to Scheduled it would have already been loaded so we may need to\r
805 // skip the LoadImage\r
806 //\r
807 if (DriverEntry->ImageHandle == NULL) {\r
808 Status = SmmLoadImage (DriverEntry);\r
809\r
810 //\r
811 // Update the driver state to reflect that it's been loaded\r
812 //\r
813 if (EFI_ERROR (Status)) {\r
fa542a1e 814 //\r
815 // The SMM Driver could not be loaded, and do not attempt to load or start it again.\r
816 // Take driver from Scheduled to Initialized.\r
817 //\r
818 DriverEntry->Initialized = TRUE;\r
e42e9404 819 DriverEntry->Scheduled = FALSE;\r
820 RemoveEntryList (&DriverEntry->ScheduledLink);\r
821\r
822 //\r
823 // If it's an error don't try the StartImage\r
824 //\r
825 continue;\r
826 }\r
827 }\r
828\r
829 DriverEntry->Scheduled = FALSE;\r
830 DriverEntry->Initialized = TRUE;\r
831 RemoveEntryList (&DriverEntry->ScheduledLink);\r
832\r
833 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (\r
834 EFI_PROGRESS_CODE,\r
835 EFI_SOFTWARE_SMM_DRIVER | EFI_SW_PC_INIT_BEGIN,\r
836 &DriverEntry->ImageHandle,\r
837 sizeof (DriverEntry->ImageHandle)\r
838 );\r
839\r
5657b268 840 //\r
841 // Cache state of SmmEntryPointRegistered before calling entry point\r
842 //\r
843 PreviousSmmEntryPointRegistered = gSmmCorePrivate->SmmEntryPointRegistered;\r
844\r
e42e9404 845 //\r
846 // For each SMM driver, pass NULL as ImageHandle\r
847 //\r
c2cb08df 848 PERF_START (DriverEntry->ImageHandle, "StartImage:", NULL, 0);\r
e42e9404 849 Status = ((EFI_IMAGE_ENTRY_POINT)(UINTN)DriverEntry->ImageEntryPoint)(DriverEntry->ImageHandle, gST);\r
c2cb08df 850 PERF_END (DriverEntry->ImageHandle, "StartImage:", NULL, 0);\r
e42e9404 851 if (EFI_ERROR(Status)){\r
852 SmmFreePages(DriverEntry->ImageBuffer, DriverEntry->NumberOfPage);\r
853 }\r
854\r
855 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (\r
856 EFI_PROGRESS_CODE,\r
857 EFI_SOFTWARE_SMM_DRIVER | EFI_SW_PC_INIT_END,\r
858 &DriverEntry->ImageHandle,\r
859 sizeof (DriverEntry->ImageHandle)\r
860 );\r
861\r
5657b268 862 if (!PreviousSmmEntryPointRegistered && gSmmCorePrivate->SmmEntryPointRegistered) {\r
863 //\r
864 // Return immediately if the SMM Entry Point was registered by the SMM \r
865 // Driver that was just dispatched. The SMM IPL will reinvoke the SMM\r
866 // Core Dispatcher. This is required so SMM Mode may be enabled as soon \r
867 // as all the dependent SMM Drivers for SMM Mode have been dispatched. \r
868 // Once the SMM Entry Point has been registered, then SMM Mode will be \r
869 // used.\r
870 //\r
871 gRequestDispatch = TRUE;\r
872 gDispatcherRunning = FALSE;\r
873 return EFI_NOT_READY;\r
874 }\r
e42e9404 875 }\r
876\r
877 //\r
878 // Search DriverList for items to place on Scheduled Queue\r
879 //\r
880 ReadyToRun = FALSE;\r
881 for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {\r
882 DriverEntry = CR (Link, EFI_SMM_DRIVER_ENTRY, Link, EFI_SMM_DRIVER_ENTRY_SIGNATURE);\r
883\r
884 if (DriverEntry->DepexProtocolError){\r
885 //\r
886 // If Section Extraction Protocol did not let the Depex be read before retry the read\r
887 //\r
888 Status = SmmGetDepexSectionAndPreProccess (DriverEntry);\r
889 }\r
890\r
891 if (DriverEntry->Dependent) {\r
892 if (SmmIsSchedulable (DriverEntry)) {\r
893 SmmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry);\r
894 ReadyToRun = TRUE;\r
895 }\r
896 }\r
897 }\r
898 } while (ReadyToRun);\r
899\r
900 //\r
901 // If there is no more SMM driver to dispatch, stop the dispatch request\r
902 //\r
903 gRequestDispatch = FALSE;\r
904 for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {\r
905 DriverEntry = CR (Link, EFI_SMM_DRIVER_ENTRY, Link, EFI_SMM_DRIVER_ENTRY_SIGNATURE);\r
906\r
907 if (!DriverEntry->Initialized){\r
908 //\r
909 // We have SMM driver pending to dispatch\r
910 //\r
911 gRequestDispatch = TRUE;\r
912 break;\r
913 }\r
914 }\r
915\r
916 gDispatcherRunning = FALSE;\r
917\r
5657b268 918 return EFI_SUCCESS;\r
e42e9404 919}\r
920\r
921/**\r
922 Insert InsertedDriverEntry onto the mScheduledQueue. To do this you\r
923 must add any driver with a before dependency on InsertedDriverEntry first.\r
924 You do this by recursively calling this routine. After all the Befores are\r
925 processed you can add InsertedDriverEntry to the mScheduledQueue.\r
926 Then you can add any driver with an After dependency on InsertedDriverEntry\r
927 by recursively calling this routine.\r
928\r
929 @param InsertedDriverEntry The driver to insert on the ScheduledLink Queue\r
930\r
931**/\r
932VOID\r
933SmmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (\r
934 IN EFI_SMM_DRIVER_ENTRY *InsertedDriverEntry\r
935 )\r
936{\r
937 LIST_ENTRY *Link;\r
938 EFI_SMM_DRIVER_ENTRY *DriverEntry;\r
939\r
940 //\r
941 // Process Before Dependency\r
942 //\r
943 for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {\r
944 DriverEntry = CR(Link, EFI_SMM_DRIVER_ENTRY, Link, EFI_SMM_DRIVER_ENTRY_SIGNATURE);\r
6a55eea3 945 if (DriverEntry->Before && DriverEntry->Dependent && DriverEntry != InsertedDriverEntry) {\r
946 DEBUG ((DEBUG_DISPATCH, "Evaluate SMM DEPEX for FFS(%g)\n", &DriverEntry->FileName));\r
947 DEBUG ((DEBUG_DISPATCH, " BEFORE FFS(%g) = ", &DriverEntry->BeforeAfterGuid));\r
e42e9404 948 if (CompareGuid (&InsertedDriverEntry->FileName, &DriverEntry->BeforeAfterGuid)) {\r
949 //\r
950 // Recursively process BEFORE\r
951 //\r
6a55eea3 952 DEBUG ((DEBUG_DISPATCH, "TRUE\n END\n RESULT = TRUE\n"));\r
e42e9404 953 SmmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry);\r
6a55eea3 954 } else {\r
955 DEBUG ((DEBUG_DISPATCH, "FALSE\n END\n RESULT = FALSE\n"));\r
e42e9404 956 }\r
957 }\r
958 }\r
959\r
960 //\r
961 // Convert driver from Dependent to Scheduled state\r
962 //\r
963\r
964 InsertedDriverEntry->Dependent = FALSE;\r
965 InsertedDriverEntry->Scheduled = TRUE;\r
966 InsertTailList (&mScheduledQueue, &InsertedDriverEntry->ScheduledLink);\r
967\r
968\r
969 //\r
970 // Process After Dependency\r
971 //\r
972 for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {\r
973 DriverEntry = CR(Link, EFI_SMM_DRIVER_ENTRY, Link, EFI_SMM_DRIVER_ENTRY_SIGNATURE);\r
6a55eea3 974 if (DriverEntry->After && DriverEntry->Dependent && DriverEntry != InsertedDriverEntry) {\r
975 DEBUG ((DEBUG_DISPATCH, "Evaluate SMM DEPEX for FFS(%g)\n", &DriverEntry->FileName));\r
976 DEBUG ((DEBUG_DISPATCH, " AFTER FFS(%g) = ", &DriverEntry->BeforeAfterGuid));\r
e42e9404 977 if (CompareGuid (&InsertedDriverEntry->FileName, &DriverEntry->BeforeAfterGuid)) {\r
978 //\r
979 // Recursively process AFTER\r
980 //\r
6a55eea3 981 DEBUG ((DEBUG_DISPATCH, "TRUE\n END\n RESULT = TRUE\n"));\r
e42e9404 982 SmmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry);\r
6a55eea3 983 } else {\r
984 DEBUG ((DEBUG_DISPATCH, "FALSE\n END\n RESULT = FALSE\n"));\r
e42e9404 985 }\r
986 }\r
987 }\r
988}\r
989\r
990/**\r
991 Return TRUE if the Fv has been processed, FALSE if not.\r
992\r
993 @param FvHandle The handle of a FV that's being tested\r
994\r
995 @retval TRUE Fv protocol on FvHandle has been processed\r
996 @retval FALSE Fv protocol on FvHandle has not yet been\r
997 processed\r
998\r
999**/\r
1000BOOLEAN\r
1001FvHasBeenProcessed (\r
1002 IN EFI_HANDLE FvHandle\r
1003 )\r
1004{\r
1005 LIST_ENTRY *Link;\r
1006 KNOWN_HANDLE *KnownHandle;\r
1007\r
1008 for (Link = mFvHandleList.ForwardLink; Link != &mFvHandleList; Link = Link->ForwardLink) {\r
1009 KnownHandle = CR(Link, KNOWN_HANDLE, Link, KNOWN_HANDLE_SIGNATURE);\r
1010 if (KnownHandle->Handle == FvHandle) {\r
1011 return TRUE;\r
1012 }\r
1013 }\r
1014 return FALSE;\r
1015}\r
1016\r
1017/**\r
1018 Remember that Fv protocol on FvHandle has had it's drivers placed on the\r
1019 mDiscoveredList. This fucntion adds entries on the mFvHandleList. Items are\r
1020 never removed/freed from the mFvHandleList.\r
1021\r
1022 @param FvHandle The handle of a FV that has been processed\r
1023\r
1024**/\r
1025VOID\r
1026FvIsBeingProcesssed (\r
1027 IN EFI_HANDLE FvHandle\r
1028 )\r
1029{\r
1030 KNOWN_HANDLE *KnownHandle;\r
1031\r
1032 KnownHandle = AllocatePool (sizeof (KNOWN_HANDLE));\r
1033 ASSERT (KnownHandle != NULL);\r
1034\r
1035 KnownHandle->Signature = KNOWN_HANDLE_SIGNATURE;\r
1036 KnownHandle->Handle = FvHandle;\r
1037 InsertTailList (&mFvHandleList, &KnownHandle->Link);\r
1038}\r
1039\r
1040/**\r
1041 Convert FvHandle and DriverName into an EFI device path\r
1042\r
1043 @param Fv Fv protocol, needed to read Depex info out of\r
1044 FLASH.\r
1045 @param FvHandle Handle for Fv, needed in the\r
1046 EFI_SMM_DRIVER_ENTRY so that the PE image can be\r
1047 read out of the FV at a later time.\r
1048 @param DriverName Name of driver to add to mDiscoveredList.\r
1049\r
1050 @return Pointer to device path constructed from FvHandle and DriverName\r
1051\r
1052**/\r
1053EFI_DEVICE_PATH_PROTOCOL *\r
1054SmmFvToDevicePath (\r
1055 IN EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv,\r
1056 IN EFI_HANDLE FvHandle,\r
1057 IN EFI_GUID *DriverName\r
1058 )\r
1059{\r
1060 EFI_STATUS Status;\r
1061 EFI_DEVICE_PATH_PROTOCOL *FvDevicePath;\r
1062 EFI_DEVICE_PATH_PROTOCOL *FileNameDevicePath;\r
1063\r
1064 //\r
1065 // Remember the device path of the FV\r
1066 //\r
1067 Status = gBS->HandleProtocol (FvHandle, &gEfiDevicePathProtocolGuid, (VOID **)&FvDevicePath);\r
1068 if (EFI_ERROR (Status)) {\r
1069 FileNameDevicePath = NULL;\r
1070 } else {\r
1071 //\r
1072 // Build a device path to the file in the FV to pass into gBS->LoadImage\r
1073 //\r
1074 EfiInitializeFwVolDevicepathNode (&mFvDevicePath.File, DriverName);\r
1075 SetDevicePathEndNode (&mFvDevicePath.End);\r
1076\r
1077 //\r
1078 // Note: FileNameDevicePath is in DXE memory\r
1079 //\r
1080 FileNameDevicePath = AppendDevicePath (\r
1081 FvDevicePath,\r
1082 (EFI_DEVICE_PATH_PROTOCOL *)&mFvDevicePath\r
1083 );\r
1084 }\r
1085 return FileNameDevicePath;\r
1086}\r
1087\r
1088/**\r
1089 Add an entry to the mDiscoveredList. Allocate memory to store the DriverEntry,\r
1090 and initilize any state variables. Read the Depex from the FV and store it\r
fa542a1e 1091 in DriverEntry. Pre-process the Depex to set the Before and After state.\r
e42e9404 1092 The Discovered list is never free'ed and contains booleans that represent the\r
1093 other possible SMM driver states.\r
1094\r
1095 @param Fv Fv protocol, needed to read Depex info out of\r
1096 FLASH.\r
1097 @param FvHandle Handle for Fv, needed in the\r
1098 EFI_SMM_DRIVER_ENTRY so that the PE image can be\r
1099 read out of the FV at a later time.\r
1100 @param DriverName Name of driver to add to mDiscoveredList.\r
1101\r
1102 @retval EFI_SUCCESS If driver was added to the mDiscoveredList.\r
1103 @retval EFI_ALREADY_STARTED The driver has already been started. Only one\r
1104 DriverName may be active in the system at any one\r
1105 time.\r
1106\r
1107**/\r
1108EFI_STATUS\r
1109SmmAddToDriverList (\r
1110 IN EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv,\r
1111 IN EFI_HANDLE FvHandle,\r
1112 IN EFI_GUID *DriverName\r
1113 )\r
1114{\r
1115 EFI_SMM_DRIVER_ENTRY *DriverEntry;\r
1116\r
1117 //\r
1118 // Create the Driver Entry for the list. ZeroPool initializes lots of variables to\r
1119 // NULL or FALSE.\r
1120 //\r
1121 DriverEntry = AllocateZeroPool (sizeof (EFI_SMM_DRIVER_ENTRY));\r
1122 ASSERT (DriverEntry != NULL);\r
1123\r
1124 DriverEntry->Signature = EFI_SMM_DRIVER_ENTRY_SIGNATURE;\r
1125 CopyGuid (&DriverEntry->FileName, DriverName);\r
1126 DriverEntry->FvHandle = FvHandle;\r
1127 DriverEntry->Fv = Fv;\r
1128 DriverEntry->FvFileDevicePath = SmmFvToDevicePath (Fv, FvHandle, DriverName);\r
1129\r
1130 SmmGetDepexSectionAndPreProccess (DriverEntry);\r
1131\r
1132 InsertTailList (&mDiscoveredList, &DriverEntry->Link);\r
1133 gRequestDispatch = TRUE;\r
1134\r
1135 return EFI_SUCCESS;\r
1136}\r
1137\r
1138/**\r
1139 This function is the main entry point for an SMM handler dispatch\r
1140 or communicate-based callback.\r
1141\r
1142 Event notification that is fired every time a FV dispatch protocol is added.\r
1143 More than one protocol may have been added when this event is fired, so you\r
1144 must loop on SmmLocateHandle () to see how many protocols were added and\r
1145 do the following to each FV:\r
1146 If the Fv has already been processed, skip it. If the Fv has not been\r
1147 processed then mark it as being processed, as we are about to process it.\r
1148 Read the Fv and add any driver in the Fv to the mDiscoveredList.The\r
1149 mDiscoveredList is never free'ed and contains variables that define\r
1150 the other states the SMM driver transitions to..\r
1151 While you are at it read the A Priori file into memory.\r
1152 Place drivers in the A Priori list onto the mScheduledQueue.\r
1153\r
1154 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().\r
1155 @param Context Points to an optional handler context which was specified when the handler was registered.\r
1156 @param CommBuffer A pointer to a collection of data in memory that will\r
1157 be conveyed from a non-SMM environment into an SMM environment.\r
1158 @param CommBufferSize The size of the CommBuffer.\r
1159\r
1160 @return Status Code\r
1161\r
1162**/\r
1163EFI_STATUS\r
1164EFIAPI\r
1165SmmDriverDispatchHandler (\r
1166 IN EFI_HANDLE DispatchHandle,\r
1167 IN CONST VOID *Context, OPTIONAL\r
1168 IN OUT VOID *CommBuffer, OPTIONAL\r
1169 IN OUT UINTN *CommBufferSize OPTIONAL\r
1170 )\r
1171{\r
1172 EFI_STATUS Status;\r
1173 UINTN HandleCount;\r
1174 EFI_HANDLE *HandleBuffer;\r
1175 EFI_STATUS GetNextFileStatus;\r
1176 EFI_STATUS SecurityStatus;\r
1177 EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;\r
1178 EFI_DEVICE_PATH_PROTOCOL *FvDevicePath;\r
1179 EFI_HANDLE FvHandle;\r
1180 EFI_GUID NameGuid;\r
1181 UINTN Key;\r
1182 EFI_FV_FILETYPE Type;\r
1183 EFI_FV_FILE_ATTRIBUTES Attributes;\r
1184 UINTN Size;\r
1185 EFI_SMM_DRIVER_ENTRY *DriverEntry;\r
1186 EFI_GUID *AprioriFile;\r
1187 UINTN AprioriEntryCount;\r
1188 UINTN Index;\r
1189 LIST_ENTRY *Link;\r
1190 UINT32 AuthenticationStatus;\r
1191 UINTN SizeOfBuffer;\r
1192\r
1193 HandleBuffer = NULL;\r
1194 Status = gBS->LocateHandleBuffer (\r
1195 ByProtocol,\r
1196 &gEfiFirmwareVolume2ProtocolGuid,\r
1197 NULL,\r
1198 &HandleCount,\r
1199 &HandleBuffer\r
1200 );\r
1201 if (EFI_ERROR (Status)) {\r
1202 return EFI_NOT_FOUND;\r
1203 }\r
1204\r
1205 for (Index = 0; Index < HandleCount; Index++) {\r
1206 FvHandle = HandleBuffer[Index];\r
1207\r
1208 if (FvHasBeenProcessed (FvHandle)) {\r
1209 //\r
1210 // This Fv has already been processed so lets skip it!\r
1211 //\r
1212 continue;\r
1213 }\r
1214\r
1215 //\r
1216 // Since we are about to process this Fv mark it as processed.\r
1217 //\r
1218 FvIsBeingProcesssed (FvHandle);\r
1219\r
1220 Status = gBS->HandleProtocol (FvHandle, &gEfiFirmwareVolume2ProtocolGuid, (VOID **)&Fv);\r
1221 if (EFI_ERROR (Status)) {\r
1222 //\r
1223 // FvHandle must have a Firmware Volume2 Protocol thus we should never get here.\r
1224 //\r
1225 ASSERT (FALSE);\r
1226 continue;\r
1227 }\r
1228\r
1229 Status = gBS->HandleProtocol (FvHandle, &gEfiDevicePathProtocolGuid, (VOID **)&FvDevicePath);\r
1230 if (EFI_ERROR (Status)) {\r
1231 //\r
1232 // The Firmware volume doesn't have device path, can't be dispatched.\r
1233 //\r
1234 continue;\r
1235 }\r
1236\r
1237 //\r
1238 // If the Security Architectural Protocol has not been located yet, then attempt to locate it\r
1239 //\r
1240 if (mSecurity == NULL) {\r
1241 gBS->LocateProtocol (&gEfiSecurityArchProtocolGuid, NULL, (VOID**)&mSecurity);\r
1242 }\r
1243\r
1244 //\r
1245 // Evaluate the authentication status of the Firmware Volume through\r
1246 // Security Architectural Protocol\r
1247 //\r
1248 if (mSecurity != NULL) {\r
1249 SecurityStatus = mSecurity->FileAuthenticationState (\r
1250 mSecurity,\r
1251 0,\r
1252 FvDevicePath\r
1253 );\r
1254 if (SecurityStatus != EFI_SUCCESS) {\r
1255 //\r
1256 // Security check failed. The firmware volume should not be used for any purpose.\r
1257 //\r
1258 continue;\r
1259 }\r
1260 }\r
1261\r
1262 //\r
1263 // Discover Drivers in FV and add them to the Discovered Driver List.\r
1264 // Process EFI_FV_FILETYPE_SMM type and then EFI_FV_FILETYPE_COMBINED_SMM_DXE\r
1265 //\r
1266 for (Index = 0; Index < sizeof (mSmmFileTypes)/sizeof (EFI_FV_FILETYPE); Index++) {\r
1267 //\r
1268 // Initialize the search key\r
1269 //\r
1270 Key = 0;\r
1271 do {\r
1272 Type = mSmmFileTypes[Index];\r
1273 GetNextFileStatus = Fv->GetNextFile (\r
1274 Fv,\r
1275 &Key,\r
1276 &Type,\r
1277 &NameGuid,\r
1278 &Attributes,\r
1279 &Size\r
1280 );\r
1281 if (!EFI_ERROR (GetNextFileStatus)) {\r
1282 SmmAddToDriverList (Fv, FvHandle, &NameGuid);\r
1283 }\r
1284 } while (!EFI_ERROR (GetNextFileStatus));\r
1285 }\r
1286\r
1287 //\r
1288 // Read the array of GUIDs from the Apriori file if it is present in the firmware volume\r
1289 // (Note: AprioriFile is in DXE memory)\r
1290 //\r
1291 AprioriFile = NULL;\r
1292 Status = Fv->ReadSection (\r
1293 Fv,\r
1294 &gAprioriGuid,\r
1295 EFI_SECTION_RAW,\r
1296 0,\r
1297 (VOID **)&AprioriFile,\r
1298 &SizeOfBuffer,\r
1299 &AuthenticationStatus\r
1300 );\r
1301 if (!EFI_ERROR (Status)) {\r
1302 AprioriEntryCount = SizeOfBuffer / sizeof (EFI_GUID);\r
1303 } else {\r
1304 AprioriEntryCount = 0;\r
1305 }\r
1306\r
1307 //\r
1308 // Put drivers on Apriori List on the Scheduled queue. The Discovered List includes\r
1309 // drivers not in the current FV and these must be skipped since the a priori list\r
1310 // is only valid for the FV that it resided in.\r
1311 //\r
1312\r
1313 for (Index = 0; Index < AprioriEntryCount; Index++) {\r
1314 for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {\r
1315 DriverEntry = CR(Link, EFI_SMM_DRIVER_ENTRY, Link, EFI_SMM_DRIVER_ENTRY_SIGNATURE);\r
1316 if (CompareGuid (&DriverEntry->FileName, &AprioriFile[Index]) &&\r
1317 (FvHandle == DriverEntry->FvHandle)) {\r
1318 DriverEntry->Dependent = FALSE;\r
1319 DriverEntry->Scheduled = TRUE;\r
1320 InsertTailList (&mScheduledQueue, &DriverEntry->ScheduledLink);\r
6a55eea3 1321 DEBUG ((DEBUG_DISPATCH, "Evaluate SMM DEPEX for FFS(%g)\n", &DriverEntry->FileName));\r
1322 DEBUG ((DEBUG_DISPATCH, " RESULT = TRUE (Apriori)\n"));\r
e42e9404 1323 break;\r
1324 }\r
1325 }\r
1326 }\r
1327\r
1328 //\r
1329 // Free data allocated by Fv->ReadSection ()\r
1330 //\r
1331 // The UEFI Boot Services FreePool() function must be used because Fv->ReadSection \r
1332 // used the UEFI Boot Services AllocatePool() function\r
1333 //\r
1334 gBS->FreePool (AprioriFile);\r
1335 }\r
1336\r
1337 //\r
1338 // Execute the SMM Dispatcher on any newly discovered FVs and previously \r
1339 // discovered SMM drivers that have been discovered but not dispatched.\r
1340 //\r
5657b268 1341 Status = SmmDispatcher ();\r
1342\r
1343 //\r
1344 // Check to see if CommBuffer and CommBufferSize are valid\r
1345 //\r
1346 if (CommBuffer != NULL && CommBufferSize != NULL) {\r
1347 if (*CommBufferSize > 0) {\r
1348 if (Status == EFI_NOT_READY) {\r
1349 //\r
1350 // If a the SMM Core Entry Point was just registered, then set flag to \r
1351 // request the SMM Dispatcher to be restarted.\r
1352 //\r
1353 *(UINT8 *)CommBuffer = COMM_BUFFER_SMM_DISPATCH_RESTART;\r
1354 } else if (!EFI_ERROR (Status)) {\r
1355 //\r
1356 // Set the flag to show that the SMM Dispatcher executed without errors\r
1357 //\r
1358 *(UINT8 *)CommBuffer = COMM_BUFFER_SMM_DISPATCH_SUCCESS;\r
1359 } else {\r
1360 //\r
1361 // Set the flag to show that the SMM Dispatcher encountered an error\r
1362 //\r
1363 *(UINT8 *)CommBuffer = COMM_BUFFER_SMM_DISPATCH_ERROR;\r
1364 }\r
1365 }\r
1366 }\r
1367\r
1368 return EFI_SUCCESS;\r
e42e9404 1369}\r
1370\r
1371/**\r
1372 Traverse the discovered list for any drivers that were discovered but not loaded\r
1373 because the dependency experessions evaluated to false.\r
1374\r
1375**/\r
1376VOID\r
1377SmmDisplayDiscoveredNotDispatched (\r
1378 VOID\r
1379 )\r
1380{\r
1381 LIST_ENTRY *Link;\r
1382 EFI_SMM_DRIVER_ENTRY *DriverEntry;\r
1383\r
1384 for (Link = mDiscoveredList.ForwardLink;Link !=&mDiscoveredList; Link = Link->ForwardLink) {\r
1385 DriverEntry = CR(Link, EFI_SMM_DRIVER_ENTRY, Link, EFI_SMM_DRIVER_ENTRY_SIGNATURE);\r
1386 if (DriverEntry->Dependent) {\r
1387 DEBUG ((DEBUG_LOAD, "SMM Driver %g was discovered but not loaded!!\n", &DriverEntry->FileName));\r
1388 }\r
1389 }\r
1390}\r