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