]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Core/PiSmmCore/Dispatcher.c
MdeModulePkg/Core/PiSmmCore: Fix various typos
[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
84edd20b 905 UnregisterSmramProfileImage (DriverEntry, TRUE);\r
e42e9404 906 SmmFreePages(DriverEntry->ImageBuffer, DriverEntry->NumberOfPage);\r
02f02b16
JY
907 //\r
908 // Uninstall LoadedImage\r
909 //\r
910 Status = gBS->UninstallProtocolInterface (\r
911 DriverEntry->ImageHandle,\r
912 &gEfiLoadedImageProtocolGuid,\r
913 DriverEntry->LoadedImage\r
914 );\r
915 if (!EFI_ERROR (Status)) {\r
916 if (DriverEntry->LoadedImage->FilePath != NULL) {\r
917 gBS->FreePool (DriverEntry->LoadedImage->FilePath);\r
918 }\r
919 gBS->FreePool (DriverEntry->LoadedImage);\r
920 }\r
285a682c
JY
921 Status = SmmUninstallProtocolInterface (\r
922 DriverEntry->SmmImageHandle,\r
923 &gEfiLoadedImageProtocolGuid,\r
924 &DriverEntry->SmmLoadedImage\r
925 );\r
926 if (!EFI_ERROR(Status)) {\r
927 if (DriverEntry->SmmLoadedImage.FilePath != NULL) {\r
928 SmmFreePool (DriverEntry->SmmLoadedImage.FilePath);\r
929 }\r
930 }\r
e42e9404 931 }\r
932\r
933 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (\r
934 EFI_PROGRESS_CODE,\r
935 EFI_SOFTWARE_SMM_DRIVER | EFI_SW_PC_INIT_END,\r
936 &DriverEntry->ImageHandle,\r
937 sizeof (DriverEntry->ImageHandle)\r
938 );\r
939\r
5657b268 940 if (!PreviousSmmEntryPointRegistered && gSmmCorePrivate->SmmEntryPointRegistered) {\r
941 //\r
d1102dba 942 // Return immediately if the SMM Entry Point was registered by the SMM\r
5657b268 943 // Driver that was just dispatched. The SMM IPL will reinvoke the SMM\r
d1102dba
LG
944 // Core Dispatcher. This is required so SMM Mode may be enabled as soon\r
945 // as all the dependent SMM Drivers for SMM Mode have been dispatched.\r
946 // Once the SMM Entry Point has been registered, then SMM Mode will be\r
5657b268 947 // used.\r
948 //\r
949 gRequestDispatch = TRUE;\r
950 gDispatcherRunning = FALSE;\r
951 return EFI_NOT_READY;\r
952 }\r
e42e9404 953 }\r
954\r
955 //\r
956 // Search DriverList for items to place on Scheduled Queue\r
957 //\r
958 ReadyToRun = FALSE;\r
959 for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {\r
960 DriverEntry = CR (Link, EFI_SMM_DRIVER_ENTRY, Link, EFI_SMM_DRIVER_ENTRY_SIGNATURE);\r
961\r
962 if (DriverEntry->DepexProtocolError){\r
963 //\r
964 // If Section Extraction Protocol did not let the Depex be read before retry the read\r
965 //\r
966 Status = SmmGetDepexSectionAndPreProccess (DriverEntry);\r
967 }\r
968\r
969 if (DriverEntry->Dependent) {\r
970 if (SmmIsSchedulable (DriverEntry)) {\r
971 SmmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry);\r
972 ReadyToRun = TRUE;\r
973 }\r
974 }\r
975 }\r
976 } while (ReadyToRun);\r
977\r
978 //\r
979 // If there is no more SMM driver to dispatch, stop the dispatch request\r
980 //\r
981 gRequestDispatch = FALSE;\r
982 for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {\r
983 DriverEntry = CR (Link, EFI_SMM_DRIVER_ENTRY, Link, EFI_SMM_DRIVER_ENTRY_SIGNATURE);\r
984\r
985 if (!DriverEntry->Initialized){\r
986 //\r
987 // We have SMM driver pending to dispatch\r
988 //\r
989 gRequestDispatch = TRUE;\r
990 break;\r
991 }\r
992 }\r
993\r
994 gDispatcherRunning = FALSE;\r
995\r
5657b268 996 return EFI_SUCCESS;\r
e42e9404 997}\r
998\r
999/**\r
1000 Insert InsertedDriverEntry onto the mScheduledQueue. To do this you\r
1001 must add any driver with a before dependency on InsertedDriverEntry first.\r
1002 You do this by recursively calling this routine. After all the Befores are\r
1003 processed you can add InsertedDriverEntry to the mScheduledQueue.\r
1004 Then you can add any driver with an After dependency on InsertedDriverEntry\r
1005 by recursively calling this routine.\r
1006\r
1007 @param InsertedDriverEntry The driver to insert on the ScheduledLink Queue\r
1008\r
1009**/\r
1010VOID\r
1011SmmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (\r
1012 IN EFI_SMM_DRIVER_ENTRY *InsertedDriverEntry\r
1013 )\r
1014{\r
1015 LIST_ENTRY *Link;\r
1016 EFI_SMM_DRIVER_ENTRY *DriverEntry;\r
1017\r
1018 //\r
1019 // Process Before Dependency\r
1020 //\r
1021 for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {\r
1022 DriverEntry = CR(Link, EFI_SMM_DRIVER_ENTRY, Link, EFI_SMM_DRIVER_ENTRY_SIGNATURE);\r
6a55eea3 1023 if (DriverEntry->Before && DriverEntry->Dependent && DriverEntry != InsertedDriverEntry) {\r
1024 DEBUG ((DEBUG_DISPATCH, "Evaluate SMM DEPEX for FFS(%g)\n", &DriverEntry->FileName));\r
1025 DEBUG ((DEBUG_DISPATCH, " BEFORE FFS(%g) = ", &DriverEntry->BeforeAfterGuid));\r
e42e9404 1026 if (CompareGuid (&InsertedDriverEntry->FileName, &DriverEntry->BeforeAfterGuid)) {\r
1027 //\r
1028 // Recursively process BEFORE\r
1029 //\r
6a55eea3 1030 DEBUG ((DEBUG_DISPATCH, "TRUE\n END\n RESULT = TRUE\n"));\r
e42e9404 1031 SmmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry);\r
6a55eea3 1032 } else {\r
1033 DEBUG ((DEBUG_DISPATCH, "FALSE\n END\n RESULT = FALSE\n"));\r
e42e9404 1034 }\r
1035 }\r
1036 }\r
1037\r
1038 //\r
1039 // Convert driver from Dependent to Scheduled state\r
1040 //\r
1041\r
1042 InsertedDriverEntry->Dependent = FALSE;\r
1043 InsertedDriverEntry->Scheduled = TRUE;\r
1044 InsertTailList (&mScheduledQueue, &InsertedDriverEntry->ScheduledLink);\r
1045\r
1046\r
1047 //\r
1048 // Process After Dependency\r
1049 //\r
1050 for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {\r
1051 DriverEntry = CR(Link, EFI_SMM_DRIVER_ENTRY, Link, EFI_SMM_DRIVER_ENTRY_SIGNATURE);\r
6a55eea3 1052 if (DriverEntry->After && DriverEntry->Dependent && DriverEntry != InsertedDriverEntry) {\r
1053 DEBUG ((DEBUG_DISPATCH, "Evaluate SMM DEPEX for FFS(%g)\n", &DriverEntry->FileName));\r
1054 DEBUG ((DEBUG_DISPATCH, " AFTER FFS(%g) = ", &DriverEntry->BeforeAfterGuid));\r
e42e9404 1055 if (CompareGuid (&InsertedDriverEntry->FileName, &DriverEntry->BeforeAfterGuid)) {\r
1056 //\r
1057 // Recursively process AFTER\r
1058 //\r
6a55eea3 1059 DEBUG ((DEBUG_DISPATCH, "TRUE\n END\n RESULT = TRUE\n"));\r
e42e9404 1060 SmmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry);\r
6a55eea3 1061 } else {\r
1062 DEBUG ((DEBUG_DISPATCH, "FALSE\n END\n RESULT = FALSE\n"));\r
e42e9404 1063 }\r
1064 }\r
1065 }\r
1066}\r
1067\r
1068/**\r
1069 Return TRUE if the Fv has been processed, FALSE if not.\r
1070\r
1071 @param FvHandle The handle of a FV that's being tested\r
1072\r
1073 @retval TRUE Fv protocol on FvHandle has been processed\r
1074 @retval FALSE Fv protocol on FvHandle has not yet been\r
1075 processed\r
1076\r
1077**/\r
1078BOOLEAN\r
1079FvHasBeenProcessed (\r
1080 IN EFI_HANDLE FvHandle\r
1081 )\r
1082{\r
1083 LIST_ENTRY *Link;\r
1084 KNOWN_HANDLE *KnownHandle;\r
1085\r
1086 for (Link = mFvHandleList.ForwardLink; Link != &mFvHandleList; Link = Link->ForwardLink) {\r
1087 KnownHandle = CR(Link, KNOWN_HANDLE, Link, KNOWN_HANDLE_SIGNATURE);\r
1088 if (KnownHandle->Handle == FvHandle) {\r
1089 return TRUE;\r
1090 }\r
1091 }\r
1092 return FALSE;\r
1093}\r
1094\r
1095/**\r
4be497df
AC
1096 Remember that Fv protocol on FvHandle has had its drivers placed on the\r
1097 mDiscoveredList. This function adds entries on the mFvHandleList. Items are\r
e42e9404 1098 never removed/freed from the mFvHandleList.\r
1099\r
1100 @param FvHandle The handle of a FV that has been processed\r
1101\r
1102**/\r
1103VOID\r
d4fa02a8 1104FvIsBeingProcessed (\r
e42e9404 1105 IN EFI_HANDLE FvHandle\r
1106 )\r
1107{\r
1108 KNOWN_HANDLE *KnownHandle;\r
1109\r
1110 KnownHandle = AllocatePool (sizeof (KNOWN_HANDLE));\r
1111 ASSERT (KnownHandle != NULL);\r
1112\r
1113 KnownHandle->Signature = KNOWN_HANDLE_SIGNATURE;\r
1114 KnownHandle->Handle = FvHandle;\r
1115 InsertTailList (&mFvHandleList, &KnownHandle->Link);\r
1116}\r
1117\r
1118/**\r
1119 Convert FvHandle and DriverName into an EFI device path\r
1120\r
1121 @param Fv Fv protocol, needed to read Depex info out of\r
1122 FLASH.\r
1123 @param FvHandle Handle for Fv, needed in the\r
1124 EFI_SMM_DRIVER_ENTRY so that the PE image can be\r
1125 read out of the FV at a later time.\r
1126 @param DriverName Name of driver to add to mDiscoveredList.\r
1127\r
1128 @return Pointer to device path constructed from FvHandle and DriverName\r
1129\r
1130**/\r
1131EFI_DEVICE_PATH_PROTOCOL *\r
1132SmmFvToDevicePath (\r
1133 IN EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv,\r
1134 IN EFI_HANDLE FvHandle,\r
1135 IN EFI_GUID *DriverName\r
1136 )\r
1137{\r
1138 EFI_STATUS Status;\r
1139 EFI_DEVICE_PATH_PROTOCOL *FvDevicePath;\r
1140 EFI_DEVICE_PATH_PROTOCOL *FileNameDevicePath;\r
1141\r
1142 //\r
1143 // Remember the device path of the FV\r
1144 //\r
1145 Status = gBS->HandleProtocol (FvHandle, &gEfiDevicePathProtocolGuid, (VOID **)&FvDevicePath);\r
1146 if (EFI_ERROR (Status)) {\r
1147 FileNameDevicePath = NULL;\r
1148 } else {\r
1149 //\r
1150 // Build a device path to the file in the FV to pass into gBS->LoadImage\r
1151 //\r
1152 EfiInitializeFwVolDevicepathNode (&mFvDevicePath.File, DriverName);\r
1153 SetDevicePathEndNode (&mFvDevicePath.End);\r
1154\r
1155 //\r
1156 // Note: FileNameDevicePath is in DXE memory\r
1157 //\r
1158 FileNameDevicePath = AppendDevicePath (\r
1159 FvDevicePath,\r
1160 (EFI_DEVICE_PATH_PROTOCOL *)&mFvDevicePath\r
1161 );\r
1162 }\r
1163 return FileNameDevicePath;\r
1164}\r
1165\r
1166/**\r
1167 Add an entry to the mDiscoveredList. Allocate memory to store the DriverEntry,\r
4be497df 1168 and initialize any state variables. Read the Depex from the FV and store it\r
fa542a1e 1169 in DriverEntry. Pre-process the Depex to set the Before and After state.\r
e42e9404 1170 The Discovered list is never free'ed and contains booleans that represent the\r
1171 other possible SMM driver states.\r
1172\r
1173 @param Fv Fv protocol, needed to read Depex info out of\r
1174 FLASH.\r
1175 @param FvHandle Handle for Fv, needed in the\r
1176 EFI_SMM_DRIVER_ENTRY so that the PE image can be\r
1177 read out of the FV at a later time.\r
1178 @param DriverName Name of driver to add to mDiscoveredList.\r
1179\r
1180 @retval EFI_SUCCESS If driver was added to the mDiscoveredList.\r
1181 @retval EFI_ALREADY_STARTED The driver has already been started. Only one\r
1182 DriverName may be active in the system at any one\r
1183 time.\r
1184\r
1185**/\r
1186EFI_STATUS\r
1187SmmAddToDriverList (\r
1188 IN EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv,\r
1189 IN EFI_HANDLE FvHandle,\r
1190 IN EFI_GUID *DriverName\r
1191 )\r
1192{\r
1193 EFI_SMM_DRIVER_ENTRY *DriverEntry;\r
1194\r
1195 //\r
1196 // Create the Driver Entry for the list. ZeroPool initializes lots of variables to\r
1197 // NULL or FALSE.\r
1198 //\r
1199 DriverEntry = AllocateZeroPool (sizeof (EFI_SMM_DRIVER_ENTRY));\r
1200 ASSERT (DriverEntry != NULL);\r
1201\r
1202 DriverEntry->Signature = EFI_SMM_DRIVER_ENTRY_SIGNATURE;\r
1203 CopyGuid (&DriverEntry->FileName, DriverName);\r
1204 DriverEntry->FvHandle = FvHandle;\r
1205 DriverEntry->Fv = Fv;\r
1206 DriverEntry->FvFileDevicePath = SmmFvToDevicePath (Fv, FvHandle, DriverName);\r
1207\r
1208 SmmGetDepexSectionAndPreProccess (DriverEntry);\r
1209\r
1210 InsertTailList (&mDiscoveredList, &DriverEntry->Link);\r
1211 gRequestDispatch = TRUE;\r
1212\r
1213 return EFI_SUCCESS;\r
1214}\r
1215\r
1216/**\r
1217 This function is the main entry point for an SMM handler dispatch\r
1218 or communicate-based callback.\r
1219\r
1220 Event notification that is fired every time a FV dispatch protocol is added.\r
1221 More than one protocol may have been added when this event is fired, so you\r
1222 must loop on SmmLocateHandle () to see how many protocols were added and\r
1223 do the following to each FV:\r
1224 If the Fv has already been processed, skip it. If the Fv has not been\r
1225 processed then mark it as being processed, as we are about to process it.\r
1226 Read the Fv and add any driver in the Fv to the mDiscoveredList.The\r
1227 mDiscoveredList is never free'ed and contains variables that define\r
1228 the other states the SMM driver transitions to..\r
1229 While you are at it read the A Priori file into memory.\r
1230 Place drivers in the A Priori list onto the mScheduledQueue.\r
1231\r
1232 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().\r
1233 @param Context Points to an optional handler context which was specified when the handler was registered.\r
1234 @param CommBuffer A pointer to a collection of data in memory that will\r
1235 be conveyed from a non-SMM environment into an SMM environment.\r
1236 @param CommBufferSize The size of the CommBuffer.\r
1237\r
1238 @return Status Code\r
1239\r
1240**/\r
1241EFI_STATUS\r
1242EFIAPI\r
1243SmmDriverDispatchHandler (\r
1244 IN EFI_HANDLE DispatchHandle,\r
1245 IN CONST VOID *Context, OPTIONAL\r
1246 IN OUT VOID *CommBuffer, OPTIONAL\r
1247 IN OUT UINTN *CommBufferSize OPTIONAL\r
1248 )\r
1249{\r
1250 EFI_STATUS Status;\r
1251 UINTN HandleCount;\r
1252 EFI_HANDLE *HandleBuffer;\r
1253 EFI_STATUS GetNextFileStatus;\r
e42e9404 1254 EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;\r
1255 EFI_DEVICE_PATH_PROTOCOL *FvDevicePath;\r
1256 EFI_HANDLE FvHandle;\r
1257 EFI_GUID NameGuid;\r
1258 UINTN Key;\r
1259 EFI_FV_FILETYPE Type;\r
1260 EFI_FV_FILE_ATTRIBUTES Attributes;\r
1261 UINTN Size;\r
1262 EFI_SMM_DRIVER_ENTRY *DriverEntry;\r
1263 EFI_GUID *AprioriFile;\r
1264 UINTN AprioriEntryCount;\r
5d832d62
ZB
1265 UINTN HandleIndex;\r
1266 UINTN SmmTypeIndex;\r
1267 UINTN AprioriIndex;\r
e42e9404 1268 LIST_ENTRY *Link;\r
1269 UINT32 AuthenticationStatus;\r
1270 UINTN SizeOfBuffer;\r
1271\r
1272 HandleBuffer = NULL;\r
1273 Status = gBS->LocateHandleBuffer (\r
1274 ByProtocol,\r
1275 &gEfiFirmwareVolume2ProtocolGuid,\r
1276 NULL,\r
1277 &HandleCount,\r
1278 &HandleBuffer\r
1279 );\r
1280 if (EFI_ERROR (Status)) {\r
1281 return EFI_NOT_FOUND;\r
1282 }\r
1283\r
5d832d62
ZB
1284 for (HandleIndex = 0; HandleIndex < HandleCount; HandleIndex++) {\r
1285 FvHandle = HandleBuffer[HandleIndex];\r
e42e9404 1286\r
1287 if (FvHasBeenProcessed (FvHandle)) {\r
1288 //\r
1289 // This Fv has already been processed so lets skip it!\r
1290 //\r
1291 continue;\r
1292 }\r
1293\r
1294 //\r
1295 // Since we are about to process this Fv mark it as processed.\r
1296 //\r
d4fa02a8 1297 FvIsBeingProcessed (FvHandle);\r
e42e9404 1298\r
1299 Status = gBS->HandleProtocol (FvHandle, &gEfiFirmwareVolume2ProtocolGuid, (VOID **)&Fv);\r
1300 if (EFI_ERROR (Status)) {\r
1301 //\r
1302 // FvHandle must have a Firmware Volume2 Protocol thus we should never get here.\r
1303 //\r
1304 ASSERT (FALSE);\r
1305 continue;\r
1306 }\r
1307\r
1308 Status = gBS->HandleProtocol (FvHandle, &gEfiDevicePathProtocolGuid, (VOID **)&FvDevicePath);\r
1309 if (EFI_ERROR (Status)) {\r
1310 //\r
1311 // The Firmware volume doesn't have device path, can't be dispatched.\r
1312 //\r
1313 continue;\r
1314 }\r
1315\r
e42e9404 1316 //\r
1317 // Discover Drivers in FV and add them to the Discovered Driver List.\r
1318 // Process EFI_FV_FILETYPE_SMM type and then EFI_FV_FILETYPE_COMBINED_SMM_DXE\r
0b256fb1 1319 // EFI_FV_FILETYPE_SMM_CORE is processed to produce a Loaded Image protocol for the core\r
e42e9404 1320 //\r
5d832d62 1321 for (SmmTypeIndex = 0; SmmTypeIndex < sizeof (mSmmFileTypes)/sizeof (EFI_FV_FILETYPE); SmmTypeIndex++) {\r
e42e9404 1322 //\r
1323 // Initialize the search key\r
1324 //\r
1325 Key = 0;\r
1326 do {\r
5d832d62 1327 Type = mSmmFileTypes[SmmTypeIndex];\r
e42e9404 1328 GetNextFileStatus = Fv->GetNextFile (\r
1329 Fv,\r
1330 &Key,\r
1331 &Type,\r
1332 &NameGuid,\r
1333 &Attributes,\r
1334 &Size\r
1335 );\r
1336 if (!EFI_ERROR (GetNextFileStatus)) {\r
0b256fb1
JY
1337 if (Type == EFI_FV_FILETYPE_SMM_CORE) {\r
1338 //\r
1339 // If this is the SMM core fill in it's DevicePath & DeviceHandle\r
1340 //\r
1341 if (mSmmCoreLoadedImage->FilePath == NULL) {\r
1342 //\r
1343 // Maybe one special FV contains only one SMM_CORE module, so its device path must\r
1344 // be initialized completely.\r
1345 //\r
1346 EfiInitializeFwVolDevicepathNode (&mFvDevicePath.File, &NameGuid);\r
1347 SetDevicePathEndNode (&mFvDevicePath.End);\r
1348\r
1349 //\r
1350 // Make an EfiBootServicesData buffer copy of FilePath\r
1351 //\r
1352 Status = gBS->AllocatePool (\r
1353 EfiBootServicesData,\r
1354 GetDevicePathSize ((EFI_DEVICE_PATH_PROTOCOL *)&mFvDevicePath),\r
1355 (VOID **)&mSmmCoreLoadedImage->FilePath\r
1356 );\r
1357 ASSERT_EFI_ERROR (Status);\r
1358 CopyMem (mSmmCoreLoadedImage->FilePath, &mFvDevicePath, GetDevicePathSize ((EFI_DEVICE_PATH_PROTOCOL *)&mFvDevicePath));\r
1359\r
1360 mSmmCoreLoadedImage->DeviceHandle = FvHandle;\r
1361 }\r
285a682c
JY
1362 if (mSmmCoreDriverEntry->SmmLoadedImage.FilePath == NULL) {\r
1363 //\r
1364 // Maybe one special FV contains only one SMM_CORE module, so its device path must\r
1365 // be initialized completely.\r
1366 //\r
1367 EfiInitializeFwVolDevicepathNode (&mFvDevicePath.File, &NameGuid);\r
1368 SetDevicePathEndNode (&mFvDevicePath.End);\r
1369\r
1370 //\r
1371 // Make a buffer copy FilePath\r
1372 //\r
1373 Status = SmmAllocatePool (\r
1374 EfiRuntimeServicesData,\r
1375 GetDevicePathSize ((EFI_DEVICE_PATH_PROTOCOL *)&mFvDevicePath),\r
1376 (VOID **)&mSmmCoreDriverEntry->SmmLoadedImage.FilePath\r
1377 );\r
1378 ASSERT_EFI_ERROR (Status);\r
1379 CopyMem (mSmmCoreDriverEntry->SmmLoadedImage.FilePath, &mFvDevicePath, GetDevicePathSize((EFI_DEVICE_PATH_PROTOCOL *)&mFvDevicePath));\r
1380\r
1381 mSmmCoreDriverEntry->SmmLoadedImage.DeviceHandle = FvHandle;\r
1382 }\r
0b256fb1
JY
1383 } else {\r
1384 SmmAddToDriverList (Fv, FvHandle, &NameGuid);\r
1385 }\r
e42e9404 1386 }\r
1387 } while (!EFI_ERROR (GetNextFileStatus));\r
1388 }\r
1389\r
1390 //\r
1391 // Read the array of GUIDs from the Apriori file if it is present in the firmware volume\r
1392 // (Note: AprioriFile is in DXE memory)\r
1393 //\r
1394 AprioriFile = NULL;\r
1395 Status = Fv->ReadSection (\r
1396 Fv,\r
1397 &gAprioriGuid,\r
1398 EFI_SECTION_RAW,\r
1399 0,\r
1400 (VOID **)&AprioriFile,\r
1401 &SizeOfBuffer,\r
1402 &AuthenticationStatus\r
1403 );\r
1404 if (!EFI_ERROR (Status)) {\r
1405 AprioriEntryCount = SizeOfBuffer / sizeof (EFI_GUID);\r
1406 } else {\r
1407 AprioriEntryCount = 0;\r
1408 }\r
1409\r
1410 //\r
1411 // Put drivers on Apriori List on the Scheduled queue. The Discovered List includes\r
1412 // drivers not in the current FV and these must be skipped since the a priori list\r
1413 // is only valid for the FV that it resided in.\r
1414 //\r
1415\r
5d832d62 1416 for (AprioriIndex = 0; AprioriIndex < AprioriEntryCount; AprioriIndex++) {\r
e42e9404 1417 for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {\r
1418 DriverEntry = CR(Link, EFI_SMM_DRIVER_ENTRY, Link, EFI_SMM_DRIVER_ENTRY_SIGNATURE);\r
5d832d62 1419 if (CompareGuid (&DriverEntry->FileName, &AprioriFile[AprioriIndex]) &&\r
e42e9404 1420 (FvHandle == DriverEntry->FvHandle)) {\r
1421 DriverEntry->Dependent = FALSE;\r
1422 DriverEntry->Scheduled = TRUE;\r
1423 InsertTailList (&mScheduledQueue, &DriverEntry->ScheduledLink);\r
6a55eea3 1424 DEBUG ((DEBUG_DISPATCH, "Evaluate SMM DEPEX for FFS(%g)\n", &DriverEntry->FileName));\r
1425 DEBUG ((DEBUG_DISPATCH, " RESULT = TRUE (Apriori)\n"));\r
e42e9404 1426 break;\r
1427 }\r
1428 }\r
1429 }\r
1430\r
1431 //\r
1432 // Free data allocated by Fv->ReadSection ()\r
1433 //\r
d1102dba 1434 // The UEFI Boot Services FreePool() function must be used because Fv->ReadSection\r
e42e9404 1435 // used the UEFI Boot Services AllocatePool() function\r
1436 //\r
1437 gBS->FreePool (AprioriFile);\r
1438 }\r
1439\r
1440 //\r
d1102dba 1441 // Execute the SMM Dispatcher on any newly discovered FVs and previously\r
e42e9404 1442 // discovered SMM drivers that have been discovered but not dispatched.\r
1443 //\r
5657b268 1444 Status = SmmDispatcher ();\r
1445\r
1446 //\r
1447 // Check to see if CommBuffer and CommBufferSize are valid\r
1448 //\r
1449 if (CommBuffer != NULL && CommBufferSize != NULL) {\r
1450 if (*CommBufferSize > 0) {\r
1451 if (Status == EFI_NOT_READY) {\r
1452 //\r
d1102dba 1453 // If a the SMM Core Entry Point was just registered, then set flag to\r
5657b268 1454 // request the SMM Dispatcher to be restarted.\r
1455 //\r
1456 *(UINT8 *)CommBuffer = COMM_BUFFER_SMM_DISPATCH_RESTART;\r
1457 } else if (!EFI_ERROR (Status)) {\r
1458 //\r
1459 // Set the flag to show that the SMM Dispatcher executed without errors\r
1460 //\r
1461 *(UINT8 *)CommBuffer = COMM_BUFFER_SMM_DISPATCH_SUCCESS;\r
1462 } else {\r
1463 //\r
1464 // Set the flag to show that the SMM Dispatcher encountered an error\r
1465 //\r
1466 *(UINT8 *)CommBuffer = COMM_BUFFER_SMM_DISPATCH_ERROR;\r
1467 }\r
1468 }\r
1469 }\r
1470\r
1471 return EFI_SUCCESS;\r
e42e9404 1472}\r
1473\r
1474/**\r
1475 Traverse the discovered list for any drivers that were discovered but not loaded\r
4be497df 1476 because the dependency expressions evaluated to false.\r
e42e9404 1477\r
1478**/\r
1479VOID\r
1480SmmDisplayDiscoveredNotDispatched (\r
1481 VOID\r
1482 )\r
1483{\r
1484 LIST_ENTRY *Link;\r
1485 EFI_SMM_DRIVER_ENTRY *DriverEntry;\r
1486\r
1487 for (Link = mDiscoveredList.ForwardLink;Link !=&mDiscoveredList; Link = Link->ForwardLink) {\r
1488 DriverEntry = CR(Link, EFI_SMM_DRIVER_ENTRY, Link, EFI_SMM_DRIVER_ENTRY_SIGNATURE);\r
1489 if (DriverEntry->Dependent) {\r
1490 DEBUG ((DEBUG_LOAD, "SMM Driver %g was discovered but not loaded!!\n", &DriverEntry->FileName));\r
1491 }\r
1492 }\r
1493}\r