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