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