]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Core/PiSmmCore/Dispatcher.c
EmulatorPkg/CpuRuntimeDxe: Fix typos in MpService
[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
022ff6bb 30 Copyright (c) 2009 - 2013, Intel Corporation. All rights reserved.<BR>\r
e42e9404 31 This program and the accompanying materials are licensed and made available \r
32 under the terms and conditions of the BSD License which accompanies this \r
33 distribution. The full text of the license may be found at \r
34 http://opensource.org/licenses/bsd-license.php \r
35\r
36 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
37 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
38\r
39**/\r
40\r
41#include "PiSmmCore.h"\r
42\r
43//\r
44// SMM Dispatcher Data structures\r
45//\r
46#define KNOWN_HANDLE_SIGNATURE SIGNATURE_32('k','n','o','w')\r
47typedef struct {\r
48 UINTN Signature;\r
49 LIST_ENTRY Link; // mFvHandleList\r
50 EFI_HANDLE Handle;\r
51} KNOWN_HANDLE;\r
52\r
53//\r
54// Function Prototypes\r
55//\r
56\r
57/**\r
58 Insert InsertedDriverEntry onto the mScheduledQueue. To do this you\r
59 must add any driver with a before dependency on InsertedDriverEntry first.\r
60 You do this by recursively calling this routine. After all the Befores are\r
61 processed you can add InsertedDriverEntry to the mScheduledQueue.\r
62 Then you can add any driver with an After dependency on InsertedDriverEntry\r
63 by recursively calling this routine.\r
64\r
65 @param InsertedDriverEntry The driver to insert on the ScheduledLink Queue\r
66\r
67**/\r
68VOID\r
69SmmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (\r
70 IN EFI_SMM_DRIVER_ENTRY *InsertedDriverEntry\r
71 );\r
72\r
73//\r
74// The Driver List contains one copy of every driver that has been discovered.\r
75// Items are never removed from the driver list. List of EFI_SMM_DRIVER_ENTRY\r
76//\r
77LIST_ENTRY mDiscoveredList = INITIALIZE_LIST_HEAD_VARIABLE (mDiscoveredList);\r
78\r
79//\r
80// Queue of drivers that are ready to dispatch. This queue is a subset of the\r
81// mDiscoveredList.list of EFI_SMM_DRIVER_ENTRY.\r
82//\r
83LIST_ENTRY mScheduledQueue = INITIALIZE_LIST_HEAD_VARIABLE (mScheduledQueue);\r
84\r
85//\r
86// List of handles who's Fv's have been parsed and added to the mFwDriverList.\r
87//\r
88LIST_ENTRY mFvHandleList = INITIALIZE_LIST_HEAD_VARIABLE (mFvHandleList);\r
89\r
90//\r
91// Flag for the SMM Dispacher. TRUE if dispatcher is execuing.\r
92//\r
93BOOLEAN gDispatcherRunning = FALSE;\r
94\r
95//\r
96// Flag for the SMM Dispacher. TRUE if there is one or more SMM drivers ready to be dispatched\r
97//\r
98BOOLEAN gRequestDispatch = FALSE;\r
99\r
100//\r
101// List of file types supported by dispatcher\r
102//\r
103EFI_FV_FILETYPE mSmmFileTypes[] = {\r
104 EFI_FV_FILETYPE_SMM,\r
105 EFI_FV_FILETYPE_COMBINED_SMM_DXE\r
106 //\r
107 // Note: DXE core will process the FV image file, so skip it in SMM core\r
108 // EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE\r
109 //\r
110};\r
111\r
112typedef struct {\r
113 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH File;\r
114 EFI_DEVICE_PATH_PROTOCOL End;\r
115} FV_FILEPATH_DEVICE_PATH;\r
116\r
117FV_FILEPATH_DEVICE_PATH mFvDevicePath;\r
118\r
119//\r
120// DXE Architecture Protocols\r
121//\r
122EFI_SECURITY_ARCH_PROTOCOL *mSecurity = NULL;\r
bc2dfdbc 123EFI_SECURITY2_ARCH_PROTOCOL *mSecurity2 = NULL;\r
e42e9404 124\r
3c447c27 125//\r
126// The global variable is defined for Loading modules at fixed address feature to track the SMM code\r
127// memory range usage. It is a bit mapped array in which every bit indicates the correspoding \r
128// memory page available or not. \r
129//\r
130GLOBAL_REMOVE_IF_UNREFERENCED UINT64 *mSmmCodeMemoryRangeUsageBitMap=NULL;\r
131\r
132/**\r
133 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
134 memory range is avaliable, the function will mark the correponding bits to 1 which indicates the memory range is used.\r
135 The function is only invoked when load modules at fixed address feature is enabled. \r
136 \r
137 @param ImageBase The base addres the image will be loaded at.\r
138 @param ImageSize The size of the image\r
139 \r
140 @retval EFI_SUCCESS The memory range the image will be loaded in is available\r
141 @retval EFI_NOT_FOUND The memory range the image will be loaded in is not available\r
142**/\r
143EFI_STATUS\r
144CheckAndMarkFixLoadingMemoryUsageBitMap (\r
145 IN EFI_PHYSICAL_ADDRESS ImageBase,\r
146 IN UINTN ImageSize\r
147 )\r
148{\r
149 UINT32 SmmCodePageNumber;\r
150 UINT64 SmmCodeSize; \r
151 EFI_PHYSICAL_ADDRESS SmmCodeBase;\r
152 UINTN BaseOffsetPageNumber;\r
153 UINTN TopOffsetPageNumber;\r
154 UINTN Index;\r
155 //\r
156 // Build tool will calculate the smm code size and then patch the PcdLoadFixAddressSmmCodePageNumber\r
157 //\r
158 SmmCodePageNumber = PcdGet32(PcdLoadFixAddressSmmCodePageNumber);\r
159 SmmCodeSize = EFI_PAGES_TO_SIZE (SmmCodePageNumber);\r
160 SmmCodeBase = gLoadModuleAtFixAddressSmramBase;\r
161 \r
162 //\r
163 // If the memory usage bit map is not initialized, do it. Every bit in the array \r
164 // indicate the status of the corresponding memory page, available or not\r
165 // \r
166 if (mSmmCodeMemoryRangeUsageBitMap == NULL) {\r
167 mSmmCodeMemoryRangeUsageBitMap = AllocateZeroPool(((SmmCodePageNumber / 64) + 1)*sizeof(UINT64));\r
168 }\r
169 //\r
170 // If the Dxe code memory range is not allocated or the bit map array allocation failed, return EFI_NOT_FOUND\r
171 //\r
172 if (mSmmCodeMemoryRangeUsageBitMap == NULL) {\r
173 return EFI_NOT_FOUND;\r
174 }\r
175 //\r
176 // see if the memory range for loading the image is in the SMM code range.\r
177 //\r
178 if (SmmCodeBase + SmmCodeSize < ImageBase + ImageSize || SmmCodeBase > ImageBase) {\r
179 return EFI_NOT_FOUND; \r
180 } \r
181 //\r
182 // Test if the memory is avalaible or not.\r
183 // \r
184 BaseOffsetPageNumber = (UINTN)EFI_SIZE_TO_PAGES((UINT32)(ImageBase - SmmCodeBase));\r
185 TopOffsetPageNumber = (UINTN)EFI_SIZE_TO_PAGES((UINT32)(ImageBase + ImageSize - SmmCodeBase));\r
186 for (Index = BaseOffsetPageNumber; Index < TopOffsetPageNumber; Index ++) {\r
187 if ((mSmmCodeMemoryRangeUsageBitMap[Index / 64] & LShiftU64(1, (Index % 64))) != 0) {\r
188 //\r
189 // This page is already used.\r
190 //\r
191 return EFI_NOT_FOUND; \r
192 }\r
193 }\r
194 \r
195 //\r
196 // Being here means the memory range is available. So mark the bits for the memory range\r
197 // \r
198 for (Index = BaseOffsetPageNumber; Index < TopOffsetPageNumber; Index ++) {\r
199 mSmmCodeMemoryRangeUsageBitMap[Index / 64] |= LShiftU64(1, (Index % 64));\r
200 }\r
201 return EFI_SUCCESS; \r
202}\r
203/**\r
204 Get the fixed loadding address from image header assigned by build tool. This function only be called \r
205 when Loading module at Fixed address feature enabled.\r
206 \r
207 @param ImageContext Pointer to the image context structure that describes the PE/COFF\r
208 image that needs to be examined by this function.\r
209 @retval EFI_SUCCESS An fixed loading address is assigned to this image by build tools .\r
210 @retval EFI_NOT_FOUND The image has no assigned fixed loadding address.\r
211\r
212**/\r
213EFI_STATUS\r
214GetPeCoffImageFixLoadingAssignedAddress(\r
215 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r
216 )\r
217{\r
218 UINTN SectionHeaderOffset;\r
219 EFI_STATUS Status;\r
220 EFI_IMAGE_SECTION_HEADER SectionHeader;\r
221 EFI_IMAGE_OPTIONAL_HEADER_UNION *ImgHdr;\r
222 EFI_PHYSICAL_ADDRESS FixLoaddingAddress;\r
223 UINT16 Index;\r
224 UINTN Size; \r
225 UINT16 NumberOfSections;\r
226 UINT64 ValueInSectionHeader;\r
227 \r
228 FixLoaddingAddress = 0;\r
229 Status = EFI_NOT_FOUND;\r
230 \r
231 //\r
232 // Get PeHeader pointer\r
233 //\r
234 ImgHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)((CHAR8* )ImageContext->Handle + ImageContext->PeCoffHeaderOffset);\r
235 SectionHeaderOffset = (UINTN)(\r
236 ImageContext->PeCoffHeaderOffset +\r
237 sizeof (UINT32) +\r
238 sizeof (EFI_IMAGE_FILE_HEADER) +\r
239 ImgHdr->Pe32.FileHeader.SizeOfOptionalHeader\r
240 );\r
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
252 ImageContext->Handle,\r
253 SectionHeaderOffset,\r
254 &Size,\r
255 &SectionHeader\r
256 );\r
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 fileds 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 uild tool saves the\r
274 // offset to SMRAM base as image base in PointerToRelocations & PointerToLineNumbers fields\r
275 // \r
276 FixLoaddingAddress = (EFI_PHYSICAL_ADDRESS)(gLoadModuleAtFixAddressSmramBase + (INT64)ValueInSectionHeader);\r
277 //\r
278 // Check if the memory range is avaliable.\r
279 //\r
280 Status = CheckAndMarkFixLoadingMemoryUsageBitMap (FixLoaddingAddress, (UINTN)(ImageContext->ImageSize + ImageContext->SectionAlignment));\r
281 if (!EFI_ERROR(Status)) {\r
282 //\r
283 // The assigned address is valid. Return the specified loadding address\r
284 //\r
285 ImageContext->ImageAddress = FixLoaddingAddress;\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", FixLoaddingAddress, Status));\r
293 return Status;\r
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
518 // Align buffer on section boundry\r
519 //\r
520 ImageContext.ImageAddress += ImageContext.SectionAlignment - 1;\r
521 ImageContext.ImageAddress &= ~(ImageContext.SectionAlignment - 1);\r
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
571 //\r
572 // Fill in the remaining fields of the Loaded Image Protocol instance.\r
573 // Note: ImageBase is an SMRAM address that can not be accessed outside of SMRAM if SMRAM window is closed.\r
574 //\r
575 DriverEntry->LoadedImage->Revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;\r
576 DriverEntry->LoadedImage->ParentHandle = gSmmCorePrivate->SmmIplImageHandle;\r
577 DriverEntry->LoadedImage->SystemTable = gST;\r
578 DriverEntry->LoadedImage->DeviceHandle = DeviceHandle;\r
579\r
580 //\r
581 // Make an EfiBootServicesData buffer copy of FilePath\r
582 //\r
583 Status = gBS->AllocatePool (EfiBootServicesData, GetDevicePathSize (FilePath), (VOID **)&DriverEntry->LoadedImage->FilePath);\r
584 if (EFI_ERROR (Status)) {\r
585 if (Buffer != NULL) {\r
b08b7ba4 586 gBS->FreePool (Buffer);\r
e42e9404 587 }\r
588 SmmFreePages (DstBuffer, PageCount);\r
589 return Status;\r
590 }\r
591 CopyMem (DriverEntry->LoadedImage->FilePath, FilePath, GetDevicePathSize (FilePath));\r
592\r
593 DriverEntry->LoadedImage->ImageBase = (VOID *)(UINTN)DriverEntry->ImageBuffer;\r
594 DriverEntry->LoadedImage->ImageSize = ImageContext.ImageSize;\r
595 DriverEntry->LoadedImage->ImageCodeType = EfiRuntimeServicesCode;\r
596 DriverEntry->LoadedImage->ImageDataType = EfiRuntimeServicesData;\r
597\r
598 //\r
599 // Create a new image handle in the UEFI handle database for the SMM Driver\r
600 //\r
601 DriverEntry->ImageHandle = NULL;\r
602 Status = gBS->InstallMultipleProtocolInterfaces (\r
603 &DriverEntry->ImageHandle,\r
604 &gEfiLoadedImageProtocolGuid, DriverEntry->LoadedImage,\r
605 NULL\r
606 );\r
607\r
c2cb08df
LG
608 PERF_START (DriverEntry->ImageHandle, "LoadImage:", NULL, Tick);\r
609 PERF_END (DriverEntry->ImageHandle, "LoadImage:", NULL, 0);\r
610\r
e42e9404 611 //\r
612 // Print the load address and the PDB file name if it is available\r
613 //\r
614\r
615 DEBUG_CODE_BEGIN ();\r
616\r
617 UINTN Index;\r
618 UINTN StartIndex;\r
619 CHAR8 EfiFileName[256];\r
620\r
621\r
622 DEBUG ((DEBUG_INFO | DEBUG_LOAD,\r
9a33a65e 623 "Loading SMM driver at 0x%11p EntryPoint=0x%11p ",\r
e42e9404 624 (VOID *)(UINTN) ImageContext.ImageAddress,\r
625 FUNCTION_ENTRY_POINT (ImageContext.EntryPoint)));\r
626\r
627\r
628 //\r
629 // Print Module Name by Pdb file path.\r
630 // Windows and Unix style file path are all trimmed correctly.\r
631 //\r
632 if (ImageContext.PdbPointer != NULL) {\r
633 StartIndex = 0;\r
634 for (Index = 0; ImageContext.PdbPointer[Index] != 0; Index++) {\r
635 if ((ImageContext.PdbPointer[Index] == '\\') || (ImageContext.PdbPointer[Index] == '/')) {\r
636 StartIndex = Index + 1;\r
637 }\r
638 }\r
639 //\r
640 // Copy the PDB file name to our temporary string, and replace .pdb with .efi\r
641 // The PDB file name is limited in the range of 0~255.\r
642 // If the length is bigger than 255, trim the redudant characters to avoid overflow in array boundary.\r
643 //\r
644 for (Index = 0; Index < sizeof (EfiFileName) - 4; Index++) {\r
645 EfiFileName[Index] = ImageContext.PdbPointer[Index + StartIndex];\r
646 if (EfiFileName[Index] == 0) {\r
647 EfiFileName[Index] = '.';\r
648 }\r
649 if (EfiFileName[Index] == '.') {\r
650 EfiFileName[Index + 1] = 'e';\r
651 EfiFileName[Index + 2] = 'f';\r
652 EfiFileName[Index + 3] = 'i';\r
653 EfiFileName[Index + 4] = 0;\r
654 break;\r
655 }\r
656 }\r
657\r
658 if (Index == sizeof (EfiFileName) - 4) {\r
659 EfiFileName[Index] = 0;\r
660 }\r
661 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "%a", EfiFileName)); // &Image->ImageContext.PdbPointer[StartIndex]));\r
662 }\r
663 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "\n"));\r
664\r
665 DEBUG_CODE_END ();\r
666\r
667 //\r
668 // Free buffer allocated by Fv->ReadSection.\r
669 //\r
670 // The UEFI Boot Services FreePool() function must be used because Fv->ReadSection \r
671 // used the UEFI Boot Services AllocatePool() function\r
672 //\r
673 Status = gBS->FreePool(Buffer);\r
022ff6bb
SZ
674 if (!EFI_ERROR (Status) && EFI_ERROR (SecurityStatus)) {\r
675 Status = SecurityStatus;\r
676 }\r
e42e9404 677 return Status; \r
678}\r
679\r
680/**\r
681 Preprocess dependency expression and update DriverEntry to reflect the\r
fa542a1e 682 state of Before and After dependencies. If DriverEntry->Before\r
683 or DriverEntry->After is set it will never be cleared. \r
e42e9404 684\r
685 @param DriverEntry DriverEntry element to update .\r
686\r
687 @retval EFI_SUCCESS It always works.\r
688\r
689**/\r
690EFI_STATUS\r
691SmmPreProcessDepex (\r
692 IN EFI_SMM_DRIVER_ENTRY *DriverEntry\r
693 )\r
694{\r
695 UINT8 *Iterator;\r
696\r
697 Iterator = DriverEntry->Depex;\r
fa542a1e 698 DriverEntry->Dependent = TRUE;\r
e42e9404 699\r
700 if (*Iterator == EFI_DEP_BEFORE) {\r
701 DriverEntry->Before = TRUE;\r
702 } else if (*Iterator == EFI_DEP_AFTER) {\r
703 DriverEntry->After = TRUE;\r
704 }\r
705\r
706 if (DriverEntry->Before || DriverEntry->After) {\r
707 CopyMem (&DriverEntry->BeforeAfterGuid, Iterator + 1, sizeof (EFI_GUID));\r
708 }\r
709\r
710 return EFI_SUCCESS;\r
711}\r
712\r
713/**\r
714 Read Depex and pre-process the Depex for Before and After. If Section Extraction\r
715 protocol returns an error via ReadSection defer the reading of the Depex.\r
716\r
717 @param DriverEntry Driver to work on.\r
718\r
719 @retval EFI_SUCCESS Depex read and preprossesed\r
720 @retval EFI_PROTOCOL_ERROR The section extraction protocol returned an error\r
721 and Depex reading needs to be retried.\r
722 @retval Error DEPEX not found.\r
723\r
724**/\r
725EFI_STATUS\r
726SmmGetDepexSectionAndPreProccess (\r
727 IN EFI_SMM_DRIVER_ENTRY *DriverEntry\r
728 )\r
729{\r
730 EFI_STATUS Status;\r
731 EFI_SECTION_TYPE SectionType;\r
732 UINT32 AuthenticationStatus;\r
733 EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;\r
734\r
735 Fv = DriverEntry->Fv;\r
736\r
737 //\r
738 // Grab Depex info, it will never be free'ed.\r
739 // (Note: DriverEntry->Depex is in DXE memory)\r
740 //\r
741 SectionType = EFI_SECTION_SMM_DEPEX;\r
742 Status = Fv->ReadSection (\r
743 DriverEntry->Fv,\r
744 &DriverEntry->FileName,\r
745 SectionType,\r
746 0,\r
747 &DriverEntry->Depex,\r
748 (UINTN *)&DriverEntry->DepexSize,\r
749 &AuthenticationStatus\r
750 );\r
751 if (EFI_ERROR (Status)) {\r
752 if (Status == EFI_PROTOCOL_ERROR) {\r
753 //\r
754 // The section extraction protocol failed so set protocol error flag\r
755 //\r
756 DriverEntry->DepexProtocolError = TRUE;\r
757 } else {\r
758 //\r
759 // If no Depex assume depend on all architectural protocols\r
760 //\r
761 DriverEntry->Depex = NULL;\r
762 DriverEntry->Dependent = TRUE;\r
763 DriverEntry->DepexProtocolError = FALSE;\r
764 }\r
765 } else {\r
766 //\r
fa542a1e 767 // Set Before and After state information based on Depex\r
768 // Driver will be put in Dependent state\r
e42e9404 769 //\r
770 SmmPreProcessDepex (DriverEntry);\r
771 DriverEntry->DepexProtocolError = FALSE;\r
772 }\r
773\r
774 return Status;\r
775}\r
776\r
e42e9404 777/**\r
778 This is the main Dispatcher for SMM and it exits when there are no more\r
779 drivers to run. Drain the mScheduledQueue and load and start a PE\r
780 image for each driver. Search the mDiscoveredList to see if any driver can\r
781 be placed on the mScheduledQueue. If no drivers are placed on the\r
5657b268 782 mScheduledQueue exit the function. \r
783\r
784 @retval EFI_SUCCESS All of the SMM Drivers that could be dispatched\r
785 have been run and the SMM Entry Point has been\r
786 registered.\r
787 @retval EFI_NOT_READY The SMM Driver that registered the SMM Entry Point\r
788 was just dispatched.\r
789 @retval EFI_NOT_FOUND There are no SMM Drivers available to be dispatched.\r
e42e9404 790 @retval EFI_ALREADY_STARTED The SMM Dispatcher is already running\r
e42e9404 791\r
792**/\r
793EFI_STATUS\r
794SmmDispatcher (\r
795 VOID\r
796 )\r
797{\r
798 EFI_STATUS Status;\r
e42e9404 799 LIST_ENTRY *Link;\r
800 EFI_SMM_DRIVER_ENTRY *DriverEntry;\r
801 BOOLEAN ReadyToRun;\r
5657b268 802 BOOLEAN PreviousSmmEntryPointRegistered;\r
e42e9404 803\r
804 if (!gRequestDispatch) {\r
805 return EFI_NOT_FOUND;\r
806 }\r
807\r
808 if (gDispatcherRunning) {\r
809 //\r
810 // If the dispatcher is running don't let it be restarted.\r
811 //\r
812 return EFI_ALREADY_STARTED;\r
813 }\r
814\r
815 gDispatcherRunning = TRUE;\r
816\r
e42e9404 817 do {\r
818 //\r
819 // Drain the Scheduled Queue\r
820 //\r
821 while (!IsListEmpty (&mScheduledQueue)) {\r
822 DriverEntry = CR (\r
823 mScheduledQueue.ForwardLink,\r
824 EFI_SMM_DRIVER_ENTRY,\r
825 ScheduledLink,\r
826 EFI_SMM_DRIVER_ENTRY_SIGNATURE\r
827 );\r
828\r
829 //\r
830 // Load the SMM Driver image into memory. If the Driver was transitioned from\r
831 // Untrused to Scheduled it would have already been loaded so we may need to\r
832 // skip the LoadImage\r
833 //\r
834 if (DriverEntry->ImageHandle == NULL) {\r
835 Status = SmmLoadImage (DriverEntry);\r
836\r
837 //\r
838 // Update the driver state to reflect that it's been loaded\r
839 //\r
840 if (EFI_ERROR (Status)) {\r
fa542a1e 841 //\r
842 // The SMM Driver could not be loaded, and do not attempt to load or start it again.\r
843 // Take driver from Scheduled to Initialized.\r
844 //\r
845 DriverEntry->Initialized = TRUE;\r
e42e9404 846 DriverEntry->Scheduled = FALSE;\r
847 RemoveEntryList (&DriverEntry->ScheduledLink);\r
848\r
849 //\r
850 // If it's an error don't try the StartImage\r
851 //\r
852 continue;\r
853 }\r
854 }\r
855\r
856 DriverEntry->Scheduled = FALSE;\r
857 DriverEntry->Initialized = TRUE;\r
858 RemoveEntryList (&DriverEntry->ScheduledLink);\r
859\r
860 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (\r
861 EFI_PROGRESS_CODE,\r
862 EFI_SOFTWARE_SMM_DRIVER | EFI_SW_PC_INIT_BEGIN,\r
863 &DriverEntry->ImageHandle,\r
864 sizeof (DriverEntry->ImageHandle)\r
865 );\r
866\r
5657b268 867 //\r
868 // Cache state of SmmEntryPointRegistered before calling entry point\r
869 //\r
870 PreviousSmmEntryPointRegistered = gSmmCorePrivate->SmmEntryPointRegistered;\r
871\r
e42e9404 872 //\r
873 // For each SMM driver, pass NULL as ImageHandle\r
874 //\r
c2cb08df 875 PERF_START (DriverEntry->ImageHandle, "StartImage:", NULL, 0);\r
e42e9404 876 Status = ((EFI_IMAGE_ENTRY_POINT)(UINTN)DriverEntry->ImageEntryPoint)(DriverEntry->ImageHandle, gST);\r
c2cb08df 877 PERF_END (DriverEntry->ImageHandle, "StartImage:", NULL, 0);\r
e42e9404 878 if (EFI_ERROR(Status)){\r
879 SmmFreePages(DriverEntry->ImageBuffer, DriverEntry->NumberOfPage);\r
880 }\r
881\r
882 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (\r
883 EFI_PROGRESS_CODE,\r
884 EFI_SOFTWARE_SMM_DRIVER | EFI_SW_PC_INIT_END,\r
885 &DriverEntry->ImageHandle,\r
886 sizeof (DriverEntry->ImageHandle)\r
887 );\r
888\r
5657b268 889 if (!PreviousSmmEntryPointRegistered && gSmmCorePrivate->SmmEntryPointRegistered) {\r
890 //\r
891 // Return immediately if the SMM Entry Point was registered by the SMM \r
892 // Driver that was just dispatched. The SMM IPL will reinvoke the SMM\r
893 // Core Dispatcher. This is required so SMM Mode may be enabled as soon \r
894 // as all the dependent SMM Drivers for SMM Mode have been dispatched. \r
895 // Once the SMM Entry Point has been registered, then SMM Mode will be \r
896 // used.\r
897 //\r
898 gRequestDispatch = TRUE;\r
899 gDispatcherRunning = FALSE;\r
900 return EFI_NOT_READY;\r
901 }\r
e42e9404 902 }\r
903\r
904 //\r
905 // Search DriverList for items to place on Scheduled Queue\r
906 //\r
907 ReadyToRun = FALSE;\r
908 for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {\r
909 DriverEntry = CR (Link, EFI_SMM_DRIVER_ENTRY, Link, EFI_SMM_DRIVER_ENTRY_SIGNATURE);\r
910\r
911 if (DriverEntry->DepexProtocolError){\r
912 //\r
913 // If Section Extraction Protocol did not let the Depex be read before retry the read\r
914 //\r
915 Status = SmmGetDepexSectionAndPreProccess (DriverEntry);\r
916 }\r
917\r
918 if (DriverEntry->Dependent) {\r
919 if (SmmIsSchedulable (DriverEntry)) {\r
920 SmmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry);\r
921 ReadyToRun = TRUE;\r
922 }\r
923 }\r
924 }\r
925 } while (ReadyToRun);\r
926\r
927 //\r
928 // If there is no more SMM driver to dispatch, stop the dispatch request\r
929 //\r
930 gRequestDispatch = FALSE;\r
931 for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {\r
932 DriverEntry = CR (Link, EFI_SMM_DRIVER_ENTRY, Link, EFI_SMM_DRIVER_ENTRY_SIGNATURE);\r
933\r
934 if (!DriverEntry->Initialized){\r
935 //\r
936 // We have SMM driver pending to dispatch\r
937 //\r
938 gRequestDispatch = TRUE;\r
939 break;\r
940 }\r
941 }\r
942\r
943 gDispatcherRunning = FALSE;\r
944\r
5657b268 945 return EFI_SUCCESS;\r
e42e9404 946}\r
947\r
948/**\r
949 Insert InsertedDriverEntry onto the mScheduledQueue. To do this you\r
950 must add any driver with a before dependency on InsertedDriverEntry first.\r
951 You do this by recursively calling this routine. After all the Befores are\r
952 processed you can add InsertedDriverEntry to the mScheduledQueue.\r
953 Then you can add any driver with an After dependency on InsertedDriverEntry\r
954 by recursively calling this routine.\r
955\r
956 @param InsertedDriverEntry The driver to insert on the ScheduledLink Queue\r
957\r
958**/\r
959VOID\r
960SmmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (\r
961 IN EFI_SMM_DRIVER_ENTRY *InsertedDriverEntry\r
962 )\r
963{\r
964 LIST_ENTRY *Link;\r
965 EFI_SMM_DRIVER_ENTRY *DriverEntry;\r
966\r
967 //\r
968 // Process Before Dependency\r
969 //\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
6a55eea3 972 if (DriverEntry->Before && DriverEntry->Dependent && DriverEntry != InsertedDriverEntry) {\r
973 DEBUG ((DEBUG_DISPATCH, "Evaluate SMM DEPEX for FFS(%g)\n", &DriverEntry->FileName));\r
974 DEBUG ((DEBUG_DISPATCH, " BEFORE FFS(%g) = ", &DriverEntry->BeforeAfterGuid));\r
e42e9404 975 if (CompareGuid (&InsertedDriverEntry->FileName, &DriverEntry->BeforeAfterGuid)) {\r
976 //\r
977 // Recursively process BEFORE\r
978 //\r
6a55eea3 979 DEBUG ((DEBUG_DISPATCH, "TRUE\n END\n RESULT = TRUE\n"));\r
e42e9404 980 SmmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry);\r
6a55eea3 981 } else {\r
982 DEBUG ((DEBUG_DISPATCH, "FALSE\n END\n RESULT = FALSE\n"));\r
e42e9404 983 }\r
984 }\r
985 }\r
986\r
987 //\r
988 // Convert driver from Dependent to Scheduled state\r
989 //\r
990\r
991 InsertedDriverEntry->Dependent = FALSE;\r
992 InsertedDriverEntry->Scheduled = TRUE;\r
993 InsertTailList (&mScheduledQueue, &InsertedDriverEntry->ScheduledLink);\r
994\r
995\r
996 //\r
997 // Process After Dependency\r
998 //\r
999 for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {\r
1000 DriverEntry = CR(Link, EFI_SMM_DRIVER_ENTRY, Link, EFI_SMM_DRIVER_ENTRY_SIGNATURE);\r
6a55eea3 1001 if (DriverEntry->After && DriverEntry->Dependent && DriverEntry != InsertedDriverEntry) {\r
1002 DEBUG ((DEBUG_DISPATCH, "Evaluate SMM DEPEX for FFS(%g)\n", &DriverEntry->FileName));\r
1003 DEBUG ((DEBUG_DISPATCH, " AFTER FFS(%g) = ", &DriverEntry->BeforeAfterGuid));\r
e42e9404 1004 if (CompareGuid (&InsertedDriverEntry->FileName, &DriverEntry->BeforeAfterGuid)) {\r
1005 //\r
1006 // Recursively process AFTER\r
1007 //\r
6a55eea3 1008 DEBUG ((DEBUG_DISPATCH, "TRUE\n END\n RESULT = TRUE\n"));\r
e42e9404 1009 SmmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry);\r
6a55eea3 1010 } else {\r
1011 DEBUG ((DEBUG_DISPATCH, "FALSE\n END\n RESULT = FALSE\n"));\r
e42e9404 1012 }\r
1013 }\r
1014 }\r
1015}\r
1016\r
1017/**\r
1018 Return TRUE if the Fv has been processed, FALSE if not.\r
1019\r
1020 @param FvHandle The handle of a FV that's being tested\r
1021\r
1022 @retval TRUE Fv protocol on FvHandle has been processed\r
1023 @retval FALSE Fv protocol on FvHandle has not yet been\r
1024 processed\r
1025\r
1026**/\r
1027BOOLEAN\r
1028FvHasBeenProcessed (\r
1029 IN EFI_HANDLE FvHandle\r
1030 )\r
1031{\r
1032 LIST_ENTRY *Link;\r
1033 KNOWN_HANDLE *KnownHandle;\r
1034\r
1035 for (Link = mFvHandleList.ForwardLink; Link != &mFvHandleList; Link = Link->ForwardLink) {\r
1036 KnownHandle = CR(Link, KNOWN_HANDLE, Link, KNOWN_HANDLE_SIGNATURE);\r
1037 if (KnownHandle->Handle == FvHandle) {\r
1038 return TRUE;\r
1039 }\r
1040 }\r
1041 return FALSE;\r
1042}\r
1043\r
1044/**\r
1045 Remember that Fv protocol on FvHandle has had it's drivers placed on the\r
1046 mDiscoveredList. This fucntion adds entries on the mFvHandleList. Items are\r
1047 never removed/freed from the mFvHandleList.\r
1048\r
1049 @param FvHandle The handle of a FV that has been processed\r
1050\r
1051**/\r
1052VOID\r
1053FvIsBeingProcesssed (\r
1054 IN EFI_HANDLE FvHandle\r
1055 )\r
1056{\r
1057 KNOWN_HANDLE *KnownHandle;\r
1058\r
1059 KnownHandle = AllocatePool (sizeof (KNOWN_HANDLE));\r
1060 ASSERT (KnownHandle != NULL);\r
1061\r
1062 KnownHandle->Signature = KNOWN_HANDLE_SIGNATURE;\r
1063 KnownHandle->Handle = FvHandle;\r
1064 InsertTailList (&mFvHandleList, &KnownHandle->Link);\r
1065}\r
1066\r
1067/**\r
1068 Convert FvHandle and DriverName into an EFI device path\r
1069\r
1070 @param Fv Fv protocol, needed to read Depex info out of\r
1071 FLASH.\r
1072 @param FvHandle Handle for Fv, needed in the\r
1073 EFI_SMM_DRIVER_ENTRY so that the PE image can be\r
1074 read out of the FV at a later time.\r
1075 @param DriverName Name of driver to add to mDiscoveredList.\r
1076\r
1077 @return Pointer to device path constructed from FvHandle and DriverName\r
1078\r
1079**/\r
1080EFI_DEVICE_PATH_PROTOCOL *\r
1081SmmFvToDevicePath (\r
1082 IN EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv,\r
1083 IN EFI_HANDLE FvHandle,\r
1084 IN EFI_GUID *DriverName\r
1085 )\r
1086{\r
1087 EFI_STATUS Status;\r
1088 EFI_DEVICE_PATH_PROTOCOL *FvDevicePath;\r
1089 EFI_DEVICE_PATH_PROTOCOL *FileNameDevicePath;\r
1090\r
1091 //\r
1092 // Remember the device path of the FV\r
1093 //\r
1094 Status = gBS->HandleProtocol (FvHandle, &gEfiDevicePathProtocolGuid, (VOID **)&FvDevicePath);\r
1095 if (EFI_ERROR (Status)) {\r
1096 FileNameDevicePath = NULL;\r
1097 } else {\r
1098 //\r
1099 // Build a device path to the file in the FV to pass into gBS->LoadImage\r
1100 //\r
1101 EfiInitializeFwVolDevicepathNode (&mFvDevicePath.File, DriverName);\r
1102 SetDevicePathEndNode (&mFvDevicePath.End);\r
1103\r
1104 //\r
1105 // Note: FileNameDevicePath is in DXE memory\r
1106 //\r
1107 FileNameDevicePath = AppendDevicePath (\r
1108 FvDevicePath,\r
1109 (EFI_DEVICE_PATH_PROTOCOL *)&mFvDevicePath\r
1110 );\r
1111 }\r
1112 return FileNameDevicePath;\r
1113}\r
1114\r
1115/**\r
1116 Add an entry to the mDiscoveredList. Allocate memory to store the DriverEntry,\r
1117 and initilize any state variables. Read the Depex from the FV and store it\r
fa542a1e 1118 in DriverEntry. Pre-process the Depex to set the Before and After state.\r
e42e9404 1119 The Discovered list is never free'ed and contains booleans that represent the\r
1120 other possible SMM driver states.\r
1121\r
1122 @param Fv Fv protocol, needed to read Depex info out of\r
1123 FLASH.\r
1124 @param FvHandle Handle for Fv, needed in the\r
1125 EFI_SMM_DRIVER_ENTRY so that the PE image can be\r
1126 read out of the FV at a later time.\r
1127 @param DriverName Name of driver to add to mDiscoveredList.\r
1128\r
1129 @retval EFI_SUCCESS If driver was added to the mDiscoveredList.\r
1130 @retval EFI_ALREADY_STARTED The driver has already been started. Only one\r
1131 DriverName may be active in the system at any one\r
1132 time.\r
1133\r
1134**/\r
1135EFI_STATUS\r
1136SmmAddToDriverList (\r
1137 IN EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv,\r
1138 IN EFI_HANDLE FvHandle,\r
1139 IN EFI_GUID *DriverName\r
1140 )\r
1141{\r
1142 EFI_SMM_DRIVER_ENTRY *DriverEntry;\r
1143\r
1144 //\r
1145 // Create the Driver Entry for the list. ZeroPool initializes lots of variables to\r
1146 // NULL or FALSE.\r
1147 //\r
1148 DriverEntry = AllocateZeroPool (sizeof (EFI_SMM_DRIVER_ENTRY));\r
1149 ASSERT (DriverEntry != NULL);\r
1150\r
1151 DriverEntry->Signature = EFI_SMM_DRIVER_ENTRY_SIGNATURE;\r
1152 CopyGuid (&DriverEntry->FileName, DriverName);\r
1153 DriverEntry->FvHandle = FvHandle;\r
1154 DriverEntry->Fv = Fv;\r
1155 DriverEntry->FvFileDevicePath = SmmFvToDevicePath (Fv, FvHandle, DriverName);\r
1156\r
1157 SmmGetDepexSectionAndPreProccess (DriverEntry);\r
1158\r
1159 InsertTailList (&mDiscoveredList, &DriverEntry->Link);\r
1160 gRequestDispatch = TRUE;\r
1161\r
1162 return EFI_SUCCESS;\r
1163}\r
1164\r
1165/**\r
1166 This function is the main entry point for an SMM handler dispatch\r
1167 or communicate-based callback.\r
1168\r
1169 Event notification that is fired every time a FV dispatch protocol is added.\r
1170 More than one protocol may have been added when this event is fired, so you\r
1171 must loop on SmmLocateHandle () to see how many protocols were added and\r
1172 do the following to each FV:\r
1173 If the Fv has already been processed, skip it. If the Fv has not been\r
1174 processed then mark it as being processed, as we are about to process it.\r
1175 Read the Fv and add any driver in the Fv to the mDiscoveredList.The\r
1176 mDiscoveredList is never free'ed and contains variables that define\r
1177 the other states the SMM driver transitions to..\r
1178 While you are at it read the A Priori file into memory.\r
1179 Place drivers in the A Priori list onto the mScheduledQueue.\r
1180\r
1181 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().\r
1182 @param Context Points to an optional handler context which was specified when the handler was registered.\r
1183 @param CommBuffer A pointer to a collection of data in memory that will\r
1184 be conveyed from a non-SMM environment into an SMM environment.\r
1185 @param CommBufferSize The size of the CommBuffer.\r
1186\r
1187 @return Status Code\r
1188\r
1189**/\r
1190EFI_STATUS\r
1191EFIAPI\r
1192SmmDriverDispatchHandler (\r
1193 IN EFI_HANDLE DispatchHandle,\r
1194 IN CONST VOID *Context, OPTIONAL\r
1195 IN OUT VOID *CommBuffer, OPTIONAL\r
1196 IN OUT UINTN *CommBufferSize OPTIONAL\r
1197 )\r
1198{\r
1199 EFI_STATUS Status;\r
1200 UINTN HandleCount;\r
1201 EFI_HANDLE *HandleBuffer;\r
1202 EFI_STATUS GetNextFileStatus;\r
e42e9404 1203 EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;\r
1204 EFI_DEVICE_PATH_PROTOCOL *FvDevicePath;\r
1205 EFI_HANDLE FvHandle;\r
1206 EFI_GUID NameGuid;\r
1207 UINTN Key;\r
1208 EFI_FV_FILETYPE Type;\r
1209 EFI_FV_FILE_ATTRIBUTES Attributes;\r
1210 UINTN Size;\r
1211 EFI_SMM_DRIVER_ENTRY *DriverEntry;\r
1212 EFI_GUID *AprioriFile;\r
1213 UINTN AprioriEntryCount;\r
1214 UINTN Index;\r
1215 LIST_ENTRY *Link;\r
1216 UINT32 AuthenticationStatus;\r
1217 UINTN SizeOfBuffer;\r
1218\r
1219 HandleBuffer = NULL;\r
1220 Status = gBS->LocateHandleBuffer (\r
1221 ByProtocol,\r
1222 &gEfiFirmwareVolume2ProtocolGuid,\r
1223 NULL,\r
1224 &HandleCount,\r
1225 &HandleBuffer\r
1226 );\r
1227 if (EFI_ERROR (Status)) {\r
1228 return EFI_NOT_FOUND;\r
1229 }\r
1230\r
1231 for (Index = 0; Index < HandleCount; Index++) {\r
1232 FvHandle = HandleBuffer[Index];\r
1233\r
1234 if (FvHasBeenProcessed (FvHandle)) {\r
1235 //\r
1236 // This Fv has already been processed so lets skip it!\r
1237 //\r
1238 continue;\r
1239 }\r
1240\r
1241 //\r
1242 // Since we are about to process this Fv mark it as processed.\r
1243 //\r
1244 FvIsBeingProcesssed (FvHandle);\r
1245\r
1246 Status = gBS->HandleProtocol (FvHandle, &gEfiFirmwareVolume2ProtocolGuid, (VOID **)&Fv);\r
1247 if (EFI_ERROR (Status)) {\r
1248 //\r
1249 // FvHandle must have a Firmware Volume2 Protocol thus we should never get here.\r
1250 //\r
1251 ASSERT (FALSE);\r
1252 continue;\r
1253 }\r
1254\r
1255 Status = gBS->HandleProtocol (FvHandle, &gEfiDevicePathProtocolGuid, (VOID **)&FvDevicePath);\r
1256 if (EFI_ERROR (Status)) {\r
1257 //\r
1258 // The Firmware volume doesn't have device path, can't be dispatched.\r
1259 //\r
1260 continue;\r
1261 }\r
1262\r
e42e9404 1263 //\r
1264 // Discover Drivers in FV and add them to the Discovered Driver List.\r
1265 // Process EFI_FV_FILETYPE_SMM type and then EFI_FV_FILETYPE_COMBINED_SMM_DXE\r
1266 //\r
1267 for (Index = 0; Index < sizeof (mSmmFileTypes)/sizeof (EFI_FV_FILETYPE); Index++) {\r
1268 //\r
1269 // Initialize the search key\r
1270 //\r
1271 Key = 0;\r
1272 do {\r
1273 Type = mSmmFileTypes[Index];\r
1274 GetNextFileStatus = Fv->GetNextFile (\r
1275 Fv,\r
1276 &Key,\r
1277 &Type,\r
1278 &NameGuid,\r
1279 &Attributes,\r
1280 &Size\r
1281 );\r
1282 if (!EFI_ERROR (GetNextFileStatus)) {\r
1283 SmmAddToDriverList (Fv, FvHandle, &NameGuid);\r
1284 }\r
1285 } while (!EFI_ERROR (GetNextFileStatus));\r
1286 }\r
1287\r
1288 //\r
1289 // Read the array of GUIDs from the Apriori file if it is present in the firmware volume\r
1290 // (Note: AprioriFile is in DXE memory)\r
1291 //\r
1292 AprioriFile = NULL;\r
1293 Status = Fv->ReadSection (\r
1294 Fv,\r
1295 &gAprioriGuid,\r
1296 EFI_SECTION_RAW,\r
1297 0,\r
1298 (VOID **)&AprioriFile,\r
1299 &SizeOfBuffer,\r
1300 &AuthenticationStatus\r
1301 );\r
1302 if (!EFI_ERROR (Status)) {\r
1303 AprioriEntryCount = SizeOfBuffer / sizeof (EFI_GUID);\r
1304 } else {\r
1305 AprioriEntryCount = 0;\r
1306 }\r
1307\r
1308 //\r
1309 // Put drivers on Apriori List on the Scheduled queue. The Discovered List includes\r
1310 // drivers not in the current FV and these must be skipped since the a priori list\r
1311 // is only valid for the FV that it resided in.\r
1312 //\r
1313\r
1314 for (Index = 0; Index < AprioriEntryCount; Index++) {\r
1315 for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {\r
1316 DriverEntry = CR(Link, EFI_SMM_DRIVER_ENTRY, Link, EFI_SMM_DRIVER_ENTRY_SIGNATURE);\r
1317 if (CompareGuid (&DriverEntry->FileName, &AprioriFile[Index]) &&\r
1318 (FvHandle == DriverEntry->FvHandle)) {\r
1319 DriverEntry->Dependent = FALSE;\r
1320 DriverEntry->Scheduled = TRUE;\r
1321 InsertTailList (&mScheduledQueue, &DriverEntry->ScheduledLink);\r
6a55eea3 1322 DEBUG ((DEBUG_DISPATCH, "Evaluate SMM DEPEX for FFS(%g)\n", &DriverEntry->FileName));\r
1323 DEBUG ((DEBUG_DISPATCH, " RESULT = TRUE (Apriori)\n"));\r
e42e9404 1324 break;\r
1325 }\r
1326 }\r
1327 }\r
1328\r
1329 //\r
1330 // Free data allocated by Fv->ReadSection ()\r
1331 //\r
1332 // The UEFI Boot Services FreePool() function must be used because Fv->ReadSection \r
1333 // used the UEFI Boot Services AllocatePool() function\r
1334 //\r
1335 gBS->FreePool (AprioriFile);\r
1336 }\r
1337\r
1338 //\r
1339 // Execute the SMM Dispatcher on any newly discovered FVs and previously \r
1340 // discovered SMM drivers that have been discovered but not dispatched.\r
1341 //\r
5657b268 1342 Status = SmmDispatcher ();\r
1343\r
1344 //\r
1345 // Check to see if CommBuffer and CommBufferSize are valid\r
1346 //\r
1347 if (CommBuffer != NULL && CommBufferSize != NULL) {\r
1348 if (*CommBufferSize > 0) {\r
1349 if (Status == EFI_NOT_READY) {\r
1350 //\r
1351 // If a the SMM Core Entry Point was just registered, then set flag to \r
1352 // request the SMM Dispatcher to be restarted.\r
1353 //\r
1354 *(UINT8 *)CommBuffer = COMM_BUFFER_SMM_DISPATCH_RESTART;\r
1355 } else if (!EFI_ERROR (Status)) {\r
1356 //\r
1357 // Set the flag to show that the SMM Dispatcher executed without errors\r
1358 //\r
1359 *(UINT8 *)CommBuffer = COMM_BUFFER_SMM_DISPATCH_SUCCESS;\r
1360 } else {\r
1361 //\r
1362 // Set the flag to show that the SMM Dispatcher encountered an error\r
1363 //\r
1364 *(UINT8 *)CommBuffer = COMM_BUFFER_SMM_DISPATCH_ERROR;\r
1365 }\r
1366 }\r
1367 }\r
1368\r
1369 return EFI_SUCCESS;\r
e42e9404 1370}\r
1371\r
1372/**\r
1373 Traverse the discovered list for any drivers that were discovered but not loaded\r
1374 because the dependency experessions evaluated to false.\r
1375\r
1376**/\r
1377VOID\r
1378SmmDisplayDiscoveredNotDispatched (\r
1379 VOID\r
1380 )\r
1381{\r
1382 LIST_ENTRY *Link;\r
1383 EFI_SMM_DRIVER_ENTRY *DriverEntry;\r
1384\r
1385 for (Link = mDiscoveredList.ForwardLink;Link !=&mDiscoveredList; Link = Link->ForwardLink) {\r
1386 DriverEntry = CR(Link, EFI_SMM_DRIVER_ENTRY, Link, EFI_SMM_DRIVER_ENTRY_SIGNATURE);\r
1387 if (DriverEntry->Dependent) {\r
1388 DEBUG ((DEBUG_LOAD, "SMM Driver %g was discovered but not loaded!!\n", &DriverEntry->FileName));\r
1389 }\r
1390 }\r
1391}\r