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