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