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