]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Core/PiSmmCore/Dispatcher.c
MdeModulePkg/PiSmmCore: log SMM image start failure
[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
4be497df 20 processes the After dependencies by recursively calling the routine.\r
e42e9404 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
4be497df 28 Depex - Dependency Expression.\r
e42e9404 29\r
891d8445 30 Copyright (c) 2014, Hewlett-Packard Development Company, L.P.\r
d1102dba 31 Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>\r
9d510e61 32 SPDX-License-Identifier: BSD-2-Clause-Patent\r
e42e9404 33\r
34**/\r
35\r
36#include "PiSmmCore.h"\r
37\r
38//\r
39// SMM Dispatcher Data structures\r
40//\r
41#define KNOWN_HANDLE_SIGNATURE SIGNATURE_32('k','n','o','w')\r
42typedef struct {\r
43 UINTN Signature;\r
44 LIST_ENTRY Link; // mFvHandleList\r
45 EFI_HANDLE Handle;\r
46} KNOWN_HANDLE;\r
47\r
48//\r
49// Function Prototypes\r
50//\r
51\r
52/**\r
53 Insert InsertedDriverEntry onto the mScheduledQueue. To do this you\r
54 must add any driver with a before dependency on InsertedDriverEntry first.\r
55 You do this by recursively calling this routine. After all the Befores are\r
56 processed you can add InsertedDriverEntry to the mScheduledQueue.\r
57 Then you can add any driver with an After dependency on InsertedDriverEntry\r
58 by recursively calling this routine.\r
59\r
60 @param InsertedDriverEntry The driver to insert on the ScheduledLink Queue\r
61\r
62**/\r
63VOID\r
64SmmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (\r
65 IN EFI_SMM_DRIVER_ENTRY *InsertedDriverEntry\r
66 );\r
67\r
68//\r
69// The Driver List contains one copy of every driver that has been discovered.\r
70// Items are never removed from the driver list. List of EFI_SMM_DRIVER_ENTRY\r
71//\r
72LIST_ENTRY mDiscoveredList = INITIALIZE_LIST_HEAD_VARIABLE (mDiscoveredList);\r
73\r
74//\r
75// Queue of drivers that are ready to dispatch. This queue is a subset of the\r
76// mDiscoveredList.list of EFI_SMM_DRIVER_ENTRY.\r
77//\r
78LIST_ENTRY mScheduledQueue = INITIALIZE_LIST_HEAD_VARIABLE (mScheduledQueue);\r
79\r
80//\r
81// List of handles who's Fv's have been parsed and added to the mFwDriverList.\r
82//\r
83LIST_ENTRY mFvHandleList = INITIALIZE_LIST_HEAD_VARIABLE (mFvHandleList);\r
84\r
85//\r
4be497df 86// Flag for the SMM Dispatcher. TRUE if dispatcher is executing.\r
e42e9404 87//\r
88BOOLEAN gDispatcherRunning = FALSE;\r
89\r
90//\r
4be497df 91// Flag for the SMM Dispatcher. TRUE if there is one or more SMM drivers ready to be dispatched\r
e42e9404 92//\r
93BOOLEAN gRequestDispatch = FALSE;\r
94\r
95//\r
96// List of file types supported by dispatcher\r
97//\r
98EFI_FV_FILETYPE mSmmFileTypes[] = {\r
99 EFI_FV_FILETYPE_SMM,\r
0b256fb1
JY
100 EFI_FV_FILETYPE_COMBINED_SMM_DXE,\r
101 EFI_FV_FILETYPE_SMM_CORE,\r
e42e9404 102 //\r
103 // Note: DXE core will process the FV image file, so skip it in SMM core\r
104 // EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE\r
105 //\r
106};\r
107\r
108typedef struct {\r
109 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH File;\r
110 EFI_DEVICE_PATH_PROTOCOL End;\r
111} FV_FILEPATH_DEVICE_PATH;\r
112\r
113FV_FILEPATH_DEVICE_PATH mFvDevicePath;\r
114\r
115//\r
116// DXE Architecture Protocols\r
117//\r
118EFI_SECURITY_ARCH_PROTOCOL *mSecurity = NULL;\r
bc2dfdbc 119EFI_SECURITY2_ARCH_PROTOCOL *mSecurity2 = NULL;\r
e42e9404 120\r
3c447c27 121//\r
122// The global variable is defined for Loading modules at fixed address feature to track the SMM code\r
2048c585 123// memory range usage. It is a bit mapped array in which every bit indicates the corresponding\r
d1102dba 124// memory page available or not.\r
3c447c27 125//\r
126GLOBAL_REMOVE_IF_UNREFERENCED UINT64 *mSmmCodeMemoryRangeUsageBitMap=NULL;\r
127\r
128/**\r
2048c585
GM
129 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
130 memory range is available, the function will mark the corresponding bits to 1 which indicates the memory range is used.\r
d1102dba
LG
131 The function is only invoked when load modules at fixed address feature is enabled.\r
132\r
2048c585 133 @param ImageBase The base address the image will be loaded at.\r
3c447c27 134 @param ImageSize The size of the image\r
d1102dba 135\r
3c447c27 136 @retval EFI_SUCCESS The memory range the image will be loaded in is available\r
137 @retval EFI_NOT_FOUND The memory range the image will be loaded in is not available\r
138**/\r
139EFI_STATUS\r
140CheckAndMarkFixLoadingMemoryUsageBitMap (\r
141 IN EFI_PHYSICAL_ADDRESS ImageBase,\r
142 IN UINTN ImageSize\r
143 )\r
144{\r
145 UINT32 SmmCodePageNumber;\r
d1102dba 146 UINT64 SmmCodeSize;\r
3c447c27 147 EFI_PHYSICAL_ADDRESS SmmCodeBase;\r
148 UINTN BaseOffsetPageNumber;\r
149 UINTN TopOffsetPageNumber;\r
150 UINTN Index;\r
151 //\r
152 // Build tool will calculate the smm code size and then patch the PcdLoadFixAddressSmmCodePageNumber\r
153 //\r
154 SmmCodePageNumber = PcdGet32(PcdLoadFixAddressSmmCodePageNumber);\r
155 SmmCodeSize = EFI_PAGES_TO_SIZE (SmmCodePageNumber);\r
156 SmmCodeBase = gLoadModuleAtFixAddressSmramBase;\r
d1102dba 157\r
3c447c27 158 //\r
d1102dba 159 // If the memory usage bit map is not initialized, do it. Every bit in the array\r
3c447c27 160 // indicate the status of the corresponding memory page, available or not\r
d1102dba 161 //\r
3c447c27 162 if (mSmmCodeMemoryRangeUsageBitMap == NULL) {\r
163 mSmmCodeMemoryRangeUsageBitMap = AllocateZeroPool(((SmmCodePageNumber / 64) + 1)*sizeof(UINT64));\r
164 }\r
165 //\r
166 // If the Dxe code memory range is not allocated or the bit map array allocation failed, return EFI_NOT_FOUND\r
167 //\r
168 if (mSmmCodeMemoryRangeUsageBitMap == NULL) {\r
169 return EFI_NOT_FOUND;\r
170 }\r
171 //\r
172 // see if the memory range for loading the image is in the SMM code range.\r
173 //\r
174 if (SmmCodeBase + SmmCodeSize < ImageBase + ImageSize || SmmCodeBase > ImageBase) {\r
d1102dba
LG
175 return EFI_NOT_FOUND;\r
176 }\r
3c447c27 177 //\r
4be497df 178 // Test if the memory is available or not.\r
d1102dba 179 //\r
16f69227
HW
180 BaseOffsetPageNumber = EFI_SIZE_TO_PAGES((UINT32)(ImageBase - SmmCodeBase));\r
181 TopOffsetPageNumber = EFI_SIZE_TO_PAGES((UINT32)(ImageBase + ImageSize - SmmCodeBase));\r
3c447c27 182 for (Index = BaseOffsetPageNumber; Index < TopOffsetPageNumber; Index ++) {\r
183 if ((mSmmCodeMemoryRangeUsageBitMap[Index / 64] & LShiftU64(1, (Index % 64))) != 0) {\r
184 //\r
185 // This page is already used.\r
186 //\r
d1102dba 187 return EFI_NOT_FOUND;\r
3c447c27 188 }\r
189 }\r
d1102dba 190\r
3c447c27 191 //\r
192 // Being here means the memory range is available. So mark the bits for the memory range\r
d1102dba 193 //\r
3c447c27 194 for (Index = BaseOffsetPageNumber; Index < TopOffsetPageNumber; Index ++) {\r
195 mSmmCodeMemoryRangeUsageBitMap[Index / 64] |= LShiftU64(1, (Index % 64));\r
196 }\r
d1102dba 197 return EFI_SUCCESS;\r
3c447c27 198}\r
199/**\r
2048c585 200 Get the fixed loading address from image header assigned by build tool. This function only be called\r
3c447c27 201 when Loading module at Fixed address feature enabled.\r
d1102dba 202\r
3c447c27 203 @param ImageContext Pointer to the image context structure that describes the PE/COFF\r
204 image that needs to be examined by this function.\r
205 @retval EFI_SUCCESS An fixed loading address is assigned to this image by build tools .\r
2048c585 206 @retval EFI_NOT_FOUND The image has no assigned fixed loading address.\r
3c447c27 207\r
208**/\r
209EFI_STATUS\r
210GetPeCoffImageFixLoadingAssignedAddress(\r
211 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r
212 )\r
213{\r
2048c585
GM
214 UINTN SectionHeaderOffset;\r
215 EFI_STATUS Status;\r
216 EFI_IMAGE_SECTION_HEADER SectionHeader;\r
217 EFI_IMAGE_OPTIONAL_HEADER_UNION *ImgHdr;\r
218 EFI_PHYSICAL_ADDRESS FixLoadingAddress;\r
219 UINT16 Index;\r
220 UINTN Size;\r
221 UINT16 NumberOfSections;\r
222 UINT64 ValueInSectionHeader;\r
223\r
224 FixLoadingAddress = 0;\r
225 Status = EFI_NOT_FOUND;\r
226\r
227 //\r
228 // Get PeHeader pointer\r
229 //\r
230 ImgHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)((CHAR8* )ImageContext->Handle + ImageContext->PeCoffHeaderOffset);\r
16f69227
HW
231 SectionHeaderOffset = ImageContext->PeCoffHeaderOffset +\r
232 sizeof (UINT32) +\r
233 sizeof (EFI_IMAGE_FILE_HEADER) +\r
234 ImgHdr->Pe32.FileHeader.SizeOfOptionalHeader;\r
2048c585
GM
235 NumberOfSections = ImgHdr->Pe32.FileHeader.NumberOfSections;\r
236\r
237 //\r
238 // Get base address from the first section header that doesn't point to code section.\r
239 //\r
240 for (Index = 0; Index < NumberOfSections; Index++) {\r
241 //\r
242 // Read section header from file\r
243 //\r
244 Size = sizeof (EFI_IMAGE_SECTION_HEADER);\r
245 Status = ImageContext->ImageRead (\r
3c447c27 246 ImageContext->Handle,\r
247 SectionHeaderOffset,\r
248 &Size,\r
249 &SectionHeader\r
250 );\r
2048c585
GM
251 if (EFI_ERROR (Status)) {\r
252 return Status;\r
253 }\r
254\r
255 Status = EFI_NOT_FOUND;\r
256\r
257 if ((SectionHeader.Characteristics & EFI_IMAGE_SCN_CNT_CODE) == 0) {\r
258 //\r
259 // Build tool will save the address in PointerToRelocations & PointerToLineNumbers fields in the first section header\r
260 // that doesn't point to code section in image header.So there is an assumption that when the feature is enabled,\r
261 // if a module with a loading address assigned by tools, the PointerToRelocations & PointerToLineNumbers fields\r
262 // should not be Zero, or else, these 2 fields should be set to Zero\r
263 //\r
264 ValueInSectionHeader = ReadUnaligned64((UINT64*)&SectionHeader.PointerToRelocations);\r
265 if (ValueInSectionHeader != 0) {\r
266 //\r
267 // Found first section header that doesn't point to code section in which build tool saves the\r
268 // offset to SMRAM base as image base in PointerToRelocations & PointerToLineNumbers fields\r
269 //\r
270 FixLoadingAddress = (EFI_PHYSICAL_ADDRESS)(gLoadModuleAtFixAddressSmramBase + (INT64)ValueInSectionHeader);\r
271 //\r
272 // Check if the memory range is available.\r
273 //\r
274 Status = CheckAndMarkFixLoadingMemoryUsageBitMap (FixLoadingAddress, (UINTN)(ImageContext->ImageSize + ImageContext->SectionAlignment));\r
275 if (!EFI_ERROR(Status)) {\r
276 //\r
277 // The assigned address is valid. Return the specified loading address\r
278 //\r
279 ImageContext->ImageAddress = FixLoadingAddress;\r
280 }\r
281 }\r
282 break;\r
283 }\r
284 SectionHeaderOffset += sizeof (EFI_IMAGE_SECTION_HEADER);\r
285 }\r
286 DEBUG ((EFI_D_INFO|EFI_D_LOAD, "LOADING MODULE FIXED INFO: Loading module at fixed address %x, Status = %r\n", FixLoadingAddress, Status));\r
287 return Status;\r
3c447c27 288}\r
e42e9404 289/**\r
290 Loads an EFI image into SMRAM.\r
291\r
292 @param DriverEntry EFI_SMM_DRIVER_ENTRY instance\r
293\r
294 @return EFI_STATUS\r
295\r
296**/\r
297EFI_STATUS\r
298EFIAPI\r
299SmmLoadImage (\r
300 IN OUT EFI_SMM_DRIVER_ENTRY *DriverEntry\r
301 )\r
302{\r
303 UINT32 AuthenticationStatus;\r
304 UINTN FilePathSize;\r
305 VOID *Buffer;\r
306 UINTN Size;\r
307 UINTN PageCount;\r
308 EFI_GUID *NameGuid;\r
309 EFI_STATUS Status;\r
310 EFI_STATUS SecurityStatus;\r
311 EFI_HANDLE DeviceHandle;\r
312 EFI_PHYSICAL_ADDRESS DstBuffer;\r
313 EFI_DEVICE_PATH_PROTOCOL *FilePath;\r
314 EFI_DEVICE_PATH_PROTOCOL *OriginalFilePath;\r
315 EFI_DEVICE_PATH_PROTOCOL *HandleFilePath;\r
316 EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;\r
317 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;\r
c2cb08df 318\r
67e9ab84
BD
319 PERF_LOAD_IMAGE_BEGIN (DriverEntry->ImageHandle);\r
320\r
e42e9404 321 Buffer = NULL;\r
322 Size = 0;\r
323 Fv = DriverEntry->Fv;\r
324 NameGuid = &DriverEntry->FileName;\r
325 FilePath = DriverEntry->FvFileDevicePath;\r
326\r
327 OriginalFilePath = FilePath;\r
328 HandleFilePath = FilePath;\r
329 DeviceHandle = NULL;\r
330 SecurityStatus = EFI_SUCCESS;\r
331 Status = EFI_SUCCESS;\r
332 AuthenticationStatus = 0;\r
333\r
334 //\r
335 // Try to get the image device handle by checking the match protocol.\r
336 //\r
337 Status = gBS->LocateDevicePath (&gEfiFirmwareVolume2ProtocolGuid, &HandleFilePath, &DeviceHandle);\r
338 if (EFI_ERROR(Status)) {\r
339 return Status;\r
340 }\r
341\r
342 //\r
bc2dfdbc 343 // If the Security2 and Security Architectural Protocol has not been located yet, then attempt to locate it\r
e42e9404 344 //\r
bc2dfdbc
LG
345 if (mSecurity2 == NULL) {\r
346 gBS->LocateProtocol (&gEfiSecurity2ArchProtocolGuid, NULL, (VOID**)&mSecurity2);\r
347 }\r
e42e9404 348 if (mSecurity == NULL) {\r
349 gBS->LocateProtocol (&gEfiSecurityArchProtocolGuid, NULL, (VOID**)&mSecurity);\r
350 }\r
e42e9404 351 //\r
bc2dfdbc 352 // When Security2 is installed, Security Architectural Protocol must be published.\r
e42e9404 353 //\r
bc2dfdbc
LG
354 ASSERT (mSecurity2 == NULL || mSecurity != NULL);\r
355\r
e42e9404 356 //\r
357 // Pull out just the file portion of the DevicePath for the LoadedImage FilePath\r
358 //\r
359 FilePath = OriginalFilePath;\r
360 Status = gBS->HandleProtocol (DeviceHandle, &gEfiDevicePathProtocolGuid, (VOID **)&HandleFilePath);\r
361 if (!EFI_ERROR (Status)) {\r
362 FilePathSize = GetDevicePathSize (HandleFilePath) - sizeof(EFI_DEVICE_PATH_PROTOCOL);\r
363 FilePath = (EFI_DEVICE_PATH_PROTOCOL *) (((UINT8 *)FilePath) + FilePathSize );\r
364 }\r
365\r
366 //\r
367 // Try reading PE32 section firstly\r
368 //\r
369 Status = Fv->ReadSection (\r
370 Fv,\r
371 NameGuid,\r
372 EFI_SECTION_PE32,\r
373 0,\r
374 &Buffer,\r
375 &Size,\r
376 &AuthenticationStatus\r
377 );\r
378\r
379 if (EFI_ERROR (Status)) {\r
380 //\r
381 // Try reading TE section secondly\r
382 //\r
383 Buffer = NULL;\r
384 Size = 0;\r
385 Status = Fv->ReadSection (\r
386 Fv,\r
387 NameGuid,\r
388 EFI_SECTION_TE,\r
389 0,\r
390 &Buffer,\r
391 &Size,\r
392 &AuthenticationStatus\r
393 );\r
394 }\r
d1102dba 395\r
e42e9404 396 if (EFI_ERROR (Status)) {\r
397 if (Buffer != NULL) {\r
b08b7ba4 398 gBS->FreePool (Buffer);\r
e42e9404 399 }\r
400 return Status;\r
401 }\r
402\r
bc2dfdbc
LG
403 //\r
404 // Verify File Authentication through the Security2 Architectural Protocol\r
405 //\r
406 if (mSecurity2 != NULL) {\r
407 SecurityStatus = mSecurity2->FileAuthentication (\r
408 mSecurity2,\r
409 OriginalFilePath,\r
410 Buffer,\r
411 Size,\r
412 FALSE\r
413 );\r
414 }\r
415\r
416 //\r
417 // Verify the Authentication Status through the Security Architectural Protocol\r
418 // Only on images that have been read using Firmware Volume protocol.\r
d1102dba 419 // All SMM images are from FV protocol.\r
bc2dfdbc
LG
420 //\r
421 if (!EFI_ERROR (SecurityStatus) && (mSecurity != NULL)) {\r
422 SecurityStatus = mSecurity->FileAuthenticationState (\r
423 mSecurity,\r
424 AuthenticationStatus,\r
425 OriginalFilePath\r
426 );\r
427 }\r
428\r
429 if (EFI_ERROR (SecurityStatus) && SecurityStatus != EFI_SECURITY_VIOLATION) {\r
430 Status = SecurityStatus;\r
431 return Status;\r
432 }\r
d1102dba 433\r
e42e9404 434 //\r
435 // Initialize ImageContext\r
436 //\r
437 ImageContext.Handle = Buffer;\r
438 ImageContext.ImageRead = PeCoffLoaderImageReadFromMemory;\r
439\r
440 //\r
441 // Get information about the image being loaded\r
442 //\r
443 Status = PeCoffLoaderGetImageInfo (&ImageContext);\r
444 if (EFI_ERROR (Status)) {\r
445 if (Buffer != NULL) {\r
b08b7ba4 446 gBS->FreePool (Buffer);\r
e42e9404 447 }\r
448 return Status;\r
449 }\r
3c447c27 450 //\r
451 // if Loading module at Fixed Address feature is enabled, then cut out a memory range started from TESG BASE\r
452 // to hold the Smm driver code\r
453 //\r
454 if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0) {\r
455 //\r
456 // Get the fixed loading address assigned by Build tool\r
457 //\r
458 Status = GetPeCoffImageFixLoadingAssignedAddress (&ImageContext);\r
459 if (!EFI_ERROR (Status)) {\r
460 //\r
4be497df 461 // Since the memory range to load Smm core already been cut out, so no need to allocate and free this range\r
3c447c27 462 // following statements is to bypass SmmFreePages\r
463 //\r
464 PageCount = 0;\r
d1102dba 465 DstBuffer = (UINTN)gLoadModuleAtFixAddressSmramBase;\r
3c447c27 466 } else {\r
467 DEBUG ((EFI_D_INFO|EFI_D_LOAD, "LOADING MODULE FIXED ERROR: Failed to load module at fixed address. \n"));\r
468 //\r
469 // allocate the memory to load the SMM driver\r
470 //\r
e0e7f80c 471 PageCount = (UINTN)EFI_SIZE_TO_PAGES((UINTN)ImageContext.ImageSize + ImageContext.SectionAlignment);\r
3c447c27 472 DstBuffer = (UINTN)(-1);\r
d1102dba 473\r
3c447c27 474 Status = SmmAllocatePages (\r
475 AllocateMaxAddress,\r
476 EfiRuntimeServicesCode,\r
477 PageCount,\r
478 &DstBuffer\r
479 );\r
480 if (EFI_ERROR (Status)) {\r
481 if (Buffer != NULL) {\r
b08b7ba4 482 gBS->FreePool (Buffer);\r
d1102dba 483 }\r
3c447c27 484 return Status;\r
d1102dba 485 }\r
3c447c27 486 ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)DstBuffer;\r
e42e9404 487 }\r
3c447c27 488 } else {\r
e0e7f80c 489 PageCount = (UINTN)EFI_SIZE_TO_PAGES((UINTN)ImageContext.ImageSize + ImageContext.SectionAlignment);\r
3c447c27 490 DstBuffer = (UINTN)(-1);\r
d1102dba 491\r
3c447c27 492 Status = SmmAllocatePages (\r
493 AllocateMaxAddress,\r
494 EfiRuntimeServicesCode,\r
495 PageCount,\r
496 &DstBuffer\r
497 );\r
498 if (EFI_ERROR (Status)) {\r
499 if (Buffer != NULL) {\r
b08b7ba4 500 gBS->FreePool (Buffer);\r
3c447c27 501 }\r
502 return Status;\r
503 }\r
d1102dba 504\r
3c447c27 505 ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)DstBuffer;\r
e42e9404 506 }\r
e42e9404 507 //\r
6393d9c8 508 // Align buffer on section boundary\r
e42e9404 509 //\r
510 ImageContext.ImageAddress += ImageContext.SectionAlignment - 1;\r
16f69227 511 ImageContext.ImageAddress &= ~((EFI_PHYSICAL_ADDRESS)ImageContext.SectionAlignment - 1);\r
e42e9404 512\r
513 //\r
514 // Load the image to our new buffer\r
515 //\r
516 Status = PeCoffLoaderLoadImage (&ImageContext);\r
517 if (EFI_ERROR (Status)) {\r
518 if (Buffer != NULL) {\r
b08b7ba4 519 gBS->FreePool (Buffer);\r
e42e9404 520 }\r
521 SmmFreePages (DstBuffer, PageCount);\r
522 return Status;\r
523 }\r
524\r
525 //\r
526 // Relocate the image in our new buffer\r
527 //\r
528 Status = PeCoffLoaderRelocateImage (&ImageContext);\r
529 if (EFI_ERROR (Status)) {\r
530 if (Buffer != NULL) {\r
b08b7ba4 531 gBS->FreePool (Buffer);\r
e42e9404 532 }\r
533 SmmFreePages (DstBuffer, PageCount);\r
534 return Status;\r
535 }\r
536\r
537 //\r
538 // Flush the instruction cache so the image data are written before we execute it\r
539 //\r
540 InvalidateInstructionCacheRange ((VOID *)(UINTN) ImageContext.ImageAddress, (UINTN) ImageContext.ImageSize);\r
541\r
542 //\r
543 // Save Image EntryPoint in DriverEntry\r
544 //\r
545 DriverEntry->ImageEntryPoint = ImageContext.EntryPoint;\r
d1102dba 546 DriverEntry->ImageBuffer = DstBuffer;\r
e42e9404 547 DriverEntry->NumberOfPage = PageCount;\r
548\r
549 //\r
550 // Allocate a Loaded Image Protocol in EfiBootServicesData\r
551 //\r
552 Status = gBS->AllocatePool (EfiBootServicesData, sizeof (EFI_LOADED_IMAGE_PROTOCOL), (VOID **)&DriverEntry->LoadedImage);\r
553 if (EFI_ERROR (Status)) {\r
554 if (Buffer != NULL) {\r
b08b7ba4 555 gBS->FreePool (Buffer);\r
e42e9404 556 }\r
557 SmmFreePages (DstBuffer, PageCount);\r
558 return Status;\r
559 }\r
560\r
891d8445 561 ZeroMem (DriverEntry->LoadedImage, sizeof (EFI_LOADED_IMAGE_PROTOCOL));\r
e42e9404 562 //\r
563 // Fill in the remaining fields of the Loaded Image Protocol instance.\r
564 // Note: ImageBase is an SMRAM address that can not be accessed outside of SMRAM if SMRAM window is closed.\r
565 //\r
566 DriverEntry->LoadedImage->Revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;\r
567 DriverEntry->LoadedImage->ParentHandle = gSmmCorePrivate->SmmIplImageHandle;\r
568 DriverEntry->LoadedImage->SystemTable = gST;\r
569 DriverEntry->LoadedImage->DeviceHandle = DeviceHandle;\r
570\r
285a682c
JY
571 DriverEntry->SmmLoadedImage.Revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;\r
572 DriverEntry->SmmLoadedImage.ParentHandle = gSmmCorePrivate->SmmIplImageHandle;\r
573 DriverEntry->SmmLoadedImage.SystemTable = gST;\r
574 DriverEntry->SmmLoadedImage.DeviceHandle = DeviceHandle;\r
575\r
e42e9404 576 //\r
577 // Make an EfiBootServicesData buffer copy of FilePath\r
578 //\r
579 Status = gBS->AllocatePool (EfiBootServicesData, GetDevicePathSize (FilePath), (VOID **)&DriverEntry->LoadedImage->FilePath);\r
580 if (EFI_ERROR (Status)) {\r
581 if (Buffer != NULL) {\r
b08b7ba4 582 gBS->FreePool (Buffer);\r
e42e9404 583 }\r
584 SmmFreePages (DstBuffer, PageCount);\r
585 return Status;\r
586 }\r
587 CopyMem (DriverEntry->LoadedImage->FilePath, FilePath, GetDevicePathSize (FilePath));\r
588\r
d5a67b3d 589 DriverEntry->LoadedImage->ImageBase = (VOID *)(UINTN) ImageContext.ImageAddress;\r
e42e9404 590 DriverEntry->LoadedImage->ImageSize = ImageContext.ImageSize;\r
591 DriverEntry->LoadedImage->ImageCodeType = EfiRuntimeServicesCode;\r
592 DriverEntry->LoadedImage->ImageDataType = EfiRuntimeServicesData;\r
593\r
285a682c
JY
594 //\r
595 // Make a buffer copy of FilePath\r
596 //\r
597 Status = SmmAllocatePool (EfiRuntimeServicesData, GetDevicePathSize(FilePath), (VOID **)&DriverEntry->SmmLoadedImage.FilePath);\r
598 if (EFI_ERROR (Status)) {\r
599 if (Buffer != NULL) {\r
600 gBS->FreePool (Buffer);\r
601 }\r
602 gBS->FreePool (DriverEntry->LoadedImage->FilePath);\r
603 SmmFreePages (DstBuffer, PageCount);\r
604 return Status;\r
605 }\r
606 CopyMem (DriverEntry->SmmLoadedImage.FilePath, FilePath, GetDevicePathSize(FilePath));\r
607\r
d5a67b3d 608 DriverEntry->SmmLoadedImage.ImageBase = (VOID *)(UINTN) ImageContext.ImageAddress;\r
285a682c
JY
609 DriverEntry->SmmLoadedImage.ImageSize = ImageContext.ImageSize;\r
610 DriverEntry->SmmLoadedImage.ImageCodeType = EfiRuntimeServicesCode;\r
611 DriverEntry->SmmLoadedImage.ImageDataType = EfiRuntimeServicesData;\r
612\r
e42e9404 613 //\r
614 // Create a new image handle in the UEFI handle database for the SMM Driver\r
615 //\r
616 DriverEntry->ImageHandle = NULL;\r
617 Status = gBS->InstallMultipleProtocolInterfaces (\r
618 &DriverEntry->ImageHandle,\r
619 &gEfiLoadedImageProtocolGuid, DriverEntry->LoadedImage,\r
620 NULL\r
621 );\r
622\r
285a682c
JY
623 //\r
624 // Create a new image handle in the SMM handle database for the SMM Driver\r
625 //\r
626 DriverEntry->SmmImageHandle = NULL;\r
627 Status = SmmInstallProtocolInterface (\r
628 &DriverEntry->SmmImageHandle,\r
629 &gEfiLoadedImageProtocolGuid,\r
630 EFI_NATIVE_INTERFACE,\r
631 &DriverEntry->SmmLoadedImage\r
632 );\r
633\r
67e9ab84 634 PERF_LOAD_IMAGE_END (DriverEntry->ImageHandle);\r
c2cb08df 635\r
e42e9404 636 //\r
637 // Print the load address and the PDB file name if it is available\r
638 //\r
639\r
640 DEBUG_CODE_BEGIN ();\r
641\r
642 UINTN Index;\r
643 UINTN StartIndex;\r
644 CHAR8 EfiFileName[256];\r
645\r
646\r
647 DEBUG ((DEBUG_INFO | DEBUG_LOAD,\r
9a33a65e 648 "Loading SMM driver at 0x%11p EntryPoint=0x%11p ",\r
e42e9404 649 (VOID *)(UINTN) ImageContext.ImageAddress,\r
650 FUNCTION_ENTRY_POINT (ImageContext.EntryPoint)));\r
651\r
652\r
653 //\r
654 // Print Module Name by Pdb file path.\r
655 // Windows and Unix style file path are all trimmed correctly.\r
656 //\r
657 if (ImageContext.PdbPointer != NULL) {\r
658 StartIndex = 0;\r
659 for (Index = 0; ImageContext.PdbPointer[Index] != 0; Index++) {\r
660 if ((ImageContext.PdbPointer[Index] == '\\') || (ImageContext.PdbPointer[Index] == '/')) {\r
661 StartIndex = Index + 1;\r
662 }\r
663 }\r
664 //\r
665 // Copy the PDB file name to our temporary string, and replace .pdb with .efi\r
666 // The PDB file name is limited in the range of 0~255.\r
4be497df 667 // If the length is bigger than 255, trim the redundant characters to avoid overflow in array boundary.\r
e42e9404 668 //\r
669 for (Index = 0; Index < sizeof (EfiFileName) - 4; Index++) {\r
670 EfiFileName[Index] = ImageContext.PdbPointer[Index + StartIndex];\r
671 if (EfiFileName[Index] == 0) {\r
672 EfiFileName[Index] = '.';\r
673 }\r
674 if (EfiFileName[Index] == '.') {\r
675 EfiFileName[Index + 1] = 'e';\r
676 EfiFileName[Index + 2] = 'f';\r
677 EfiFileName[Index + 3] = 'i';\r
678 EfiFileName[Index + 4] = 0;\r
679 break;\r
680 }\r
681 }\r
682\r
683 if (Index == sizeof (EfiFileName) - 4) {\r
684 EfiFileName[Index] = 0;\r
685 }\r
686 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "%a", EfiFileName)); // &Image->ImageContext.PdbPointer[StartIndex]));\r
687 }\r
688 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "\n"));\r
689\r
690 DEBUG_CODE_END ();\r
691\r
692 //\r
693 // Free buffer allocated by Fv->ReadSection.\r
694 //\r
d1102dba 695 // The UEFI Boot Services FreePool() function must be used because Fv->ReadSection\r
e42e9404 696 // used the UEFI Boot Services AllocatePool() function\r
697 //\r
698 Status = gBS->FreePool(Buffer);\r
022ff6bb
SZ
699 if (!EFI_ERROR (Status) && EFI_ERROR (SecurityStatus)) {\r
700 Status = SecurityStatus;\r
701 }\r
d1102dba 702 return Status;\r
e42e9404 703}\r
704\r
705/**\r
706 Preprocess dependency expression and update DriverEntry to reflect the\r
fa542a1e 707 state of Before and After dependencies. If DriverEntry->Before\r
d1102dba 708 or DriverEntry->After is set it will never be cleared.\r
e42e9404 709\r
710 @param DriverEntry DriverEntry element to update .\r
711\r
712 @retval EFI_SUCCESS It always works.\r
713\r
714**/\r
715EFI_STATUS\r
716SmmPreProcessDepex (\r
717 IN EFI_SMM_DRIVER_ENTRY *DriverEntry\r
718 )\r
719{\r
720 UINT8 *Iterator;\r
721\r
722 Iterator = DriverEntry->Depex;\r
fa542a1e 723 DriverEntry->Dependent = TRUE;\r
e42e9404 724\r
725 if (*Iterator == EFI_DEP_BEFORE) {\r
726 DriverEntry->Before = TRUE;\r
727 } else if (*Iterator == EFI_DEP_AFTER) {\r
728 DriverEntry->After = TRUE;\r
729 }\r
730\r
731 if (DriverEntry->Before || DriverEntry->After) {\r
732 CopyMem (&DriverEntry->BeforeAfterGuid, Iterator + 1, sizeof (EFI_GUID));\r
733 }\r
734\r
735 return EFI_SUCCESS;\r
736}\r
737\r
738/**\r
739 Read Depex and pre-process the Depex for Before and After. If Section Extraction\r
740 protocol returns an error via ReadSection defer the reading of the Depex.\r
741\r
742 @param DriverEntry Driver to work on.\r
743\r
4be497df 744 @retval EFI_SUCCESS Depex read and preprocessed\r
e42e9404 745 @retval EFI_PROTOCOL_ERROR The section extraction protocol returned an error\r
746 and Depex reading needs to be retried.\r
747 @retval Error DEPEX not found.\r
748\r
749**/\r
750EFI_STATUS\r
751SmmGetDepexSectionAndPreProccess (\r
752 IN EFI_SMM_DRIVER_ENTRY *DriverEntry\r
753 )\r
754{\r
755 EFI_STATUS Status;\r
756 EFI_SECTION_TYPE SectionType;\r
757 UINT32 AuthenticationStatus;\r
758 EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;\r
759\r
760 Fv = DriverEntry->Fv;\r
761\r
762 //\r
763 // Grab Depex info, it will never be free'ed.\r
764 // (Note: DriverEntry->Depex is in DXE memory)\r
765 //\r
766 SectionType = EFI_SECTION_SMM_DEPEX;\r
767 Status = Fv->ReadSection (\r
768 DriverEntry->Fv,\r
769 &DriverEntry->FileName,\r
770 SectionType,\r
771 0,\r
772 &DriverEntry->Depex,\r
773 (UINTN *)&DriverEntry->DepexSize,\r
774 &AuthenticationStatus\r
775 );\r
776 if (EFI_ERROR (Status)) {\r
777 if (Status == EFI_PROTOCOL_ERROR) {\r
778 //\r
779 // The section extraction protocol failed so set protocol error flag\r
780 //\r
781 DriverEntry->DepexProtocolError = TRUE;\r
782 } else {\r
783 //\r
784 // If no Depex assume depend on all architectural protocols\r
785 //\r
786 DriverEntry->Depex = NULL;\r
787 DriverEntry->Dependent = TRUE;\r
788 DriverEntry->DepexProtocolError = FALSE;\r
789 }\r
790 } else {\r
791 //\r
fa542a1e 792 // Set Before and After state information based on Depex\r
793 // Driver will be put in Dependent state\r
e42e9404 794 //\r
795 SmmPreProcessDepex (DriverEntry);\r
796 DriverEntry->DepexProtocolError = FALSE;\r
797 }\r
798\r
799 return Status;\r
800}\r
801\r
e42e9404 802/**\r
803 This is the main Dispatcher for SMM and it exits when there are no more\r
804 drivers to run. Drain the mScheduledQueue and load and start a PE\r
805 image for each driver. Search the mDiscoveredList to see if any driver can\r
806 be placed on the mScheduledQueue. If no drivers are placed on the\r
d1102dba 807 mScheduledQueue exit the function.\r
5657b268 808\r
809 @retval EFI_SUCCESS All of the SMM Drivers that could be dispatched\r
810 have been run and the SMM Entry Point has been\r
811 registered.\r
812 @retval EFI_NOT_READY The SMM Driver that registered the SMM Entry Point\r
813 was just dispatched.\r
814 @retval EFI_NOT_FOUND There are no SMM Drivers available to be dispatched.\r
e42e9404 815 @retval EFI_ALREADY_STARTED The SMM Dispatcher is already running\r
e42e9404 816\r
817**/\r
818EFI_STATUS\r
819SmmDispatcher (\r
820 VOID\r
821 )\r
822{\r
823 EFI_STATUS Status;\r
e42e9404 824 LIST_ENTRY *Link;\r
825 EFI_SMM_DRIVER_ENTRY *DriverEntry;\r
826 BOOLEAN ReadyToRun;\r
5657b268 827 BOOLEAN PreviousSmmEntryPointRegistered;\r
e42e9404 828\r
829 if (!gRequestDispatch) {\r
830 return EFI_NOT_FOUND;\r
831 }\r
832\r
833 if (gDispatcherRunning) {\r
834 //\r
835 // If the dispatcher is running don't let it be restarted.\r
836 //\r
837 return EFI_ALREADY_STARTED;\r
838 }\r
839\r
840 gDispatcherRunning = TRUE;\r
841\r
e42e9404 842 do {\r
843 //\r
844 // Drain the Scheduled Queue\r
845 //\r
846 while (!IsListEmpty (&mScheduledQueue)) {\r
847 DriverEntry = CR (\r
848 mScheduledQueue.ForwardLink,\r
849 EFI_SMM_DRIVER_ENTRY,\r
850 ScheduledLink,\r
851 EFI_SMM_DRIVER_ENTRY_SIGNATURE\r
852 );\r
853\r
854 //\r
855 // Load the SMM Driver image into memory. If the Driver was transitioned from\r
856 // Untrused to Scheduled it would have already been loaded so we may need to\r
857 // skip the LoadImage\r
858 //\r
859 if (DriverEntry->ImageHandle == NULL) {\r
860 Status = SmmLoadImage (DriverEntry);\r
861\r
862 //\r
863 // Update the driver state to reflect that it's been loaded\r
864 //\r
865 if (EFI_ERROR (Status)) {\r
fa542a1e 866 //\r
867 // The SMM Driver could not be loaded, and do not attempt to load or start it again.\r
868 // Take driver from Scheduled to Initialized.\r
869 //\r
870 DriverEntry->Initialized = TRUE;\r
e42e9404 871 DriverEntry->Scheduled = FALSE;\r
872 RemoveEntryList (&DriverEntry->ScheduledLink);\r
873\r
874 //\r
875 // If it's an error don't try the StartImage\r
876 //\r
877 continue;\r
878 }\r
879 }\r
880\r
881 DriverEntry->Scheduled = FALSE;\r
882 DriverEntry->Initialized = TRUE;\r
883 RemoveEntryList (&DriverEntry->ScheduledLink);\r
884\r
885 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (\r
886 EFI_PROGRESS_CODE,\r
887 EFI_SOFTWARE_SMM_DRIVER | EFI_SW_PC_INIT_BEGIN,\r
888 &DriverEntry->ImageHandle,\r
889 sizeof (DriverEntry->ImageHandle)\r
890 );\r
891\r
5657b268 892 //\r
893 // Cache state of SmmEntryPointRegistered before calling entry point\r
894 //\r
895 PreviousSmmEntryPointRegistered = gSmmCorePrivate->SmmEntryPointRegistered;\r
896\r
e42e9404 897 //\r
898 // For each SMM driver, pass NULL as ImageHandle\r
899 //\r
84edd20b 900 RegisterSmramProfileImage (DriverEntry, TRUE);\r
67e9ab84 901 PERF_START_IMAGE_BEGIN (DriverEntry->ImageHandle);\r
e42e9404 902 Status = ((EFI_IMAGE_ENTRY_POINT)(UINTN)DriverEntry->ImageEntryPoint)(DriverEntry->ImageHandle, gST);\r
67e9ab84 903 PERF_START_IMAGE_END (DriverEntry->ImageHandle);\r
e42e9404 904 if (EFI_ERROR(Status)){\r
a1ddad95
LE
905 DEBUG ((\r
906 DEBUG_ERROR,\r
907 "Error: SMM image at %11p start failed: %r\n",\r
908 DriverEntry->SmmLoadedImage.ImageBase,\r
909 Status\r
910 ));\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
4be497df
AC
1102 Remember that Fv protocol on FvHandle has had its drivers placed on the\r
1103 mDiscoveredList. This function adds entries on the mFvHandleList. Items are\r
e42e9404 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
d4fa02a8 1110FvIsBeingProcessed (\r
e42e9404 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
4be497df 1174 and initialize 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
d4fa02a8 1303 FvIsBeingProcessed (FvHandle);\r
e42e9404 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
4be497df 1482 because the dependency expressions evaluated to false.\r
e42e9404 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