]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Core/PiSmmCore/Dispatcher.c
MdeModulePkg PiSmmCore: Retrieve Smram base address from system table
[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
285a682c
JY
583 DriverEntry->SmmLoadedImage.Revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;\r
584 DriverEntry->SmmLoadedImage.ParentHandle = gSmmCorePrivate->SmmIplImageHandle;\r
585 DriverEntry->SmmLoadedImage.SystemTable = gST;\r
586 DriverEntry->SmmLoadedImage.DeviceHandle = DeviceHandle;\r
587\r
e42e9404 588 //\r
589 // Make an EfiBootServicesData buffer copy of FilePath\r
590 //\r
591 Status = gBS->AllocatePool (EfiBootServicesData, GetDevicePathSize (FilePath), (VOID **)&DriverEntry->LoadedImage->FilePath);\r
592 if (EFI_ERROR (Status)) {\r
593 if (Buffer != NULL) {\r
b08b7ba4 594 gBS->FreePool (Buffer);\r
e42e9404 595 }\r
596 SmmFreePages (DstBuffer, PageCount);\r
597 return Status;\r
598 }\r
599 CopyMem (DriverEntry->LoadedImage->FilePath, FilePath, GetDevicePathSize (FilePath));\r
600\r
601 DriverEntry->LoadedImage->ImageBase = (VOID *)(UINTN)DriverEntry->ImageBuffer;\r
602 DriverEntry->LoadedImage->ImageSize = ImageContext.ImageSize;\r
603 DriverEntry->LoadedImage->ImageCodeType = EfiRuntimeServicesCode;\r
604 DriverEntry->LoadedImage->ImageDataType = EfiRuntimeServicesData;\r
605\r
285a682c
JY
606 //\r
607 // Make a buffer copy of FilePath\r
608 //\r
609 Status = SmmAllocatePool (EfiRuntimeServicesData, GetDevicePathSize(FilePath), (VOID **)&DriverEntry->SmmLoadedImage.FilePath);\r
610 if (EFI_ERROR (Status)) {\r
611 if (Buffer != NULL) {\r
612 gBS->FreePool (Buffer);\r
613 }\r
614 gBS->FreePool (DriverEntry->LoadedImage->FilePath);\r
615 SmmFreePages (DstBuffer, PageCount);\r
616 return Status;\r
617 }\r
618 CopyMem (DriverEntry->SmmLoadedImage.FilePath, FilePath, GetDevicePathSize(FilePath));\r
619\r
620 DriverEntry->SmmLoadedImage.ImageBase = (VOID *)(UINTN)DriverEntry->ImageBuffer;\r
621 DriverEntry->SmmLoadedImage.ImageSize = ImageContext.ImageSize;\r
622 DriverEntry->SmmLoadedImage.ImageCodeType = EfiRuntimeServicesCode;\r
623 DriverEntry->SmmLoadedImage.ImageDataType = EfiRuntimeServicesData;\r
624\r
e42e9404 625 //\r
626 // Create a new image handle in the UEFI handle database for the SMM Driver\r
627 //\r
628 DriverEntry->ImageHandle = NULL;\r
629 Status = gBS->InstallMultipleProtocolInterfaces (\r
630 &DriverEntry->ImageHandle,\r
631 &gEfiLoadedImageProtocolGuid, DriverEntry->LoadedImage,\r
632 NULL\r
633 );\r
634\r
285a682c
JY
635 //\r
636 // Create a new image handle in the SMM handle database for the SMM Driver\r
637 //\r
638 DriverEntry->SmmImageHandle = NULL;\r
639 Status = SmmInstallProtocolInterface (\r
640 &DriverEntry->SmmImageHandle,\r
641 &gEfiLoadedImageProtocolGuid,\r
642 EFI_NATIVE_INTERFACE,\r
643 &DriverEntry->SmmLoadedImage\r
644 );\r
645\r
c2cb08df
LG
646 PERF_START (DriverEntry->ImageHandle, "LoadImage:", NULL, Tick);\r
647 PERF_END (DriverEntry->ImageHandle, "LoadImage:", NULL, 0);\r
648\r
e42e9404 649 //\r
650 // Print the load address and the PDB file name if it is available\r
651 //\r
652\r
653 DEBUG_CODE_BEGIN ();\r
654\r
655 UINTN Index;\r
656 UINTN StartIndex;\r
657 CHAR8 EfiFileName[256];\r
658\r
659\r
660 DEBUG ((DEBUG_INFO | DEBUG_LOAD,\r
9a33a65e 661 "Loading SMM driver at 0x%11p EntryPoint=0x%11p ",\r
e42e9404 662 (VOID *)(UINTN) ImageContext.ImageAddress,\r
663 FUNCTION_ENTRY_POINT (ImageContext.EntryPoint)));\r
664\r
665\r
666 //\r
667 // Print Module Name by Pdb file path.\r
668 // Windows and Unix style file path are all trimmed correctly.\r
669 //\r
670 if (ImageContext.PdbPointer != NULL) {\r
671 StartIndex = 0;\r
672 for (Index = 0; ImageContext.PdbPointer[Index] != 0; Index++) {\r
673 if ((ImageContext.PdbPointer[Index] == '\\') || (ImageContext.PdbPointer[Index] == '/')) {\r
674 StartIndex = Index + 1;\r
675 }\r
676 }\r
677 //\r
678 // Copy the PDB file name to our temporary string, and replace .pdb with .efi\r
679 // The PDB file name is limited in the range of 0~255.\r
680 // If the length is bigger than 255, trim the redudant characters to avoid overflow in array boundary.\r
681 //\r
682 for (Index = 0; Index < sizeof (EfiFileName) - 4; Index++) {\r
683 EfiFileName[Index] = ImageContext.PdbPointer[Index + StartIndex];\r
684 if (EfiFileName[Index] == 0) {\r
685 EfiFileName[Index] = '.';\r
686 }\r
687 if (EfiFileName[Index] == '.') {\r
688 EfiFileName[Index + 1] = 'e';\r
689 EfiFileName[Index + 2] = 'f';\r
690 EfiFileName[Index + 3] = 'i';\r
691 EfiFileName[Index + 4] = 0;\r
692 break;\r
693 }\r
694 }\r
695\r
696 if (Index == sizeof (EfiFileName) - 4) {\r
697 EfiFileName[Index] = 0;\r
698 }\r
699 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "%a", EfiFileName)); // &Image->ImageContext.PdbPointer[StartIndex]));\r
700 }\r
701 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "\n"));\r
702\r
703 DEBUG_CODE_END ();\r
704\r
705 //\r
706 // Free buffer allocated by Fv->ReadSection.\r
707 //\r
708 // The UEFI Boot Services FreePool() function must be used because Fv->ReadSection \r
709 // used the UEFI Boot Services AllocatePool() function\r
710 //\r
711 Status = gBS->FreePool(Buffer);\r
022ff6bb
SZ
712 if (!EFI_ERROR (Status) && EFI_ERROR (SecurityStatus)) {\r
713 Status = SecurityStatus;\r
714 }\r
e42e9404 715 return Status; \r
716}\r
717\r
718/**\r
719 Preprocess dependency expression and update DriverEntry to reflect the\r
fa542a1e 720 state of Before and After dependencies. If DriverEntry->Before\r
721 or DriverEntry->After is set it will never be cleared. \r
e42e9404 722\r
723 @param DriverEntry DriverEntry element to update .\r
724\r
725 @retval EFI_SUCCESS It always works.\r
726\r
727**/\r
728EFI_STATUS\r
729SmmPreProcessDepex (\r
730 IN EFI_SMM_DRIVER_ENTRY *DriverEntry\r
731 )\r
732{\r
733 UINT8 *Iterator;\r
734\r
735 Iterator = DriverEntry->Depex;\r
fa542a1e 736 DriverEntry->Dependent = TRUE;\r
e42e9404 737\r
738 if (*Iterator == EFI_DEP_BEFORE) {\r
739 DriverEntry->Before = TRUE;\r
740 } else if (*Iterator == EFI_DEP_AFTER) {\r
741 DriverEntry->After = TRUE;\r
742 }\r
743\r
744 if (DriverEntry->Before || DriverEntry->After) {\r
745 CopyMem (&DriverEntry->BeforeAfterGuid, Iterator + 1, sizeof (EFI_GUID));\r
746 }\r
747\r
748 return EFI_SUCCESS;\r
749}\r
750\r
751/**\r
752 Read Depex and pre-process the Depex for Before and After. If Section Extraction\r
753 protocol returns an error via ReadSection defer the reading of the Depex.\r
754\r
755 @param DriverEntry Driver to work on.\r
756\r
757 @retval EFI_SUCCESS Depex read and preprossesed\r
758 @retval EFI_PROTOCOL_ERROR The section extraction protocol returned an error\r
759 and Depex reading needs to be retried.\r
760 @retval Error DEPEX not found.\r
761\r
762**/\r
763EFI_STATUS\r
764SmmGetDepexSectionAndPreProccess (\r
765 IN EFI_SMM_DRIVER_ENTRY *DriverEntry\r
766 )\r
767{\r
768 EFI_STATUS Status;\r
769 EFI_SECTION_TYPE SectionType;\r
770 UINT32 AuthenticationStatus;\r
771 EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;\r
772\r
773 Fv = DriverEntry->Fv;\r
774\r
775 //\r
776 // Grab Depex info, it will never be free'ed.\r
777 // (Note: DriverEntry->Depex is in DXE memory)\r
778 //\r
779 SectionType = EFI_SECTION_SMM_DEPEX;\r
780 Status = Fv->ReadSection (\r
781 DriverEntry->Fv,\r
782 &DriverEntry->FileName,\r
783 SectionType,\r
784 0,\r
785 &DriverEntry->Depex,\r
786 (UINTN *)&DriverEntry->DepexSize,\r
787 &AuthenticationStatus\r
788 );\r
789 if (EFI_ERROR (Status)) {\r
790 if (Status == EFI_PROTOCOL_ERROR) {\r
791 //\r
792 // The section extraction protocol failed so set protocol error flag\r
793 //\r
794 DriverEntry->DepexProtocolError = TRUE;\r
795 } else {\r
796 //\r
797 // If no Depex assume depend on all architectural protocols\r
798 //\r
799 DriverEntry->Depex = NULL;\r
800 DriverEntry->Dependent = TRUE;\r
801 DriverEntry->DepexProtocolError = FALSE;\r
802 }\r
803 } else {\r
804 //\r
fa542a1e 805 // Set Before and After state information based on Depex\r
806 // Driver will be put in Dependent state\r
e42e9404 807 //\r
808 SmmPreProcessDepex (DriverEntry);\r
809 DriverEntry->DepexProtocolError = FALSE;\r
810 }\r
811\r
812 return Status;\r
813}\r
814\r
e42e9404 815/**\r
816 This is the main Dispatcher for SMM and it exits when there are no more\r
817 drivers to run. Drain the mScheduledQueue and load and start a PE\r
818 image for each driver. Search the mDiscoveredList to see if any driver can\r
819 be placed on the mScheduledQueue. If no drivers are placed on the\r
5657b268 820 mScheduledQueue exit the function. \r
821\r
822 @retval EFI_SUCCESS All of the SMM Drivers that could be dispatched\r
823 have been run and the SMM Entry Point has been\r
824 registered.\r
825 @retval EFI_NOT_READY The SMM Driver that registered the SMM Entry Point\r
826 was just dispatched.\r
827 @retval EFI_NOT_FOUND There are no SMM Drivers available to be dispatched.\r
e42e9404 828 @retval EFI_ALREADY_STARTED The SMM Dispatcher is already running\r
e42e9404 829\r
830**/\r
831EFI_STATUS\r
832SmmDispatcher (\r
833 VOID\r
834 )\r
835{\r
836 EFI_STATUS Status;\r
e42e9404 837 LIST_ENTRY *Link;\r
838 EFI_SMM_DRIVER_ENTRY *DriverEntry;\r
839 BOOLEAN ReadyToRun;\r
5657b268 840 BOOLEAN PreviousSmmEntryPointRegistered;\r
e42e9404 841\r
842 if (!gRequestDispatch) {\r
843 return EFI_NOT_FOUND;\r
844 }\r
845\r
846 if (gDispatcherRunning) {\r
847 //\r
848 // If the dispatcher is running don't let it be restarted.\r
849 //\r
850 return EFI_ALREADY_STARTED;\r
851 }\r
852\r
853 gDispatcherRunning = TRUE;\r
854\r
e42e9404 855 do {\r
856 //\r
857 // Drain the Scheduled Queue\r
858 //\r
859 while (!IsListEmpty (&mScheduledQueue)) {\r
860 DriverEntry = CR (\r
861 mScheduledQueue.ForwardLink,\r
862 EFI_SMM_DRIVER_ENTRY,\r
863 ScheduledLink,\r
864 EFI_SMM_DRIVER_ENTRY_SIGNATURE\r
865 );\r
866\r
867 //\r
868 // Load the SMM Driver image into memory. If the Driver was transitioned from\r
869 // Untrused to Scheduled it would have already been loaded so we may need to\r
870 // skip the LoadImage\r
871 //\r
872 if (DriverEntry->ImageHandle == NULL) {\r
873 Status = SmmLoadImage (DriverEntry);\r
874\r
875 //\r
876 // Update the driver state to reflect that it's been loaded\r
877 //\r
878 if (EFI_ERROR (Status)) {\r
fa542a1e 879 //\r
880 // The SMM Driver could not be loaded, and do not attempt to load or start it again.\r
881 // Take driver from Scheduled to Initialized.\r
882 //\r
883 DriverEntry->Initialized = TRUE;\r
e42e9404 884 DriverEntry->Scheduled = FALSE;\r
885 RemoveEntryList (&DriverEntry->ScheduledLink);\r
886\r
887 //\r
888 // If it's an error don't try the StartImage\r
889 //\r
890 continue;\r
891 }\r
892 }\r
893\r
894 DriverEntry->Scheduled = FALSE;\r
895 DriverEntry->Initialized = TRUE;\r
896 RemoveEntryList (&DriverEntry->ScheduledLink);\r
897\r
898 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (\r
899 EFI_PROGRESS_CODE,\r
900 EFI_SOFTWARE_SMM_DRIVER | EFI_SW_PC_INIT_BEGIN,\r
901 &DriverEntry->ImageHandle,\r
902 sizeof (DriverEntry->ImageHandle)\r
903 );\r
904\r
5657b268 905 //\r
906 // Cache state of SmmEntryPointRegistered before calling entry point\r
907 //\r
908 PreviousSmmEntryPointRegistered = gSmmCorePrivate->SmmEntryPointRegistered;\r
909\r
e42e9404 910 //\r
911 // For each SMM driver, pass NULL as ImageHandle\r
912 //\r
84edd20b 913 RegisterSmramProfileImage (DriverEntry, TRUE);\r
c2cb08df 914 PERF_START (DriverEntry->ImageHandle, "StartImage:", NULL, 0);\r
e42e9404 915 Status = ((EFI_IMAGE_ENTRY_POINT)(UINTN)DriverEntry->ImageEntryPoint)(DriverEntry->ImageHandle, gST);\r
c2cb08df 916 PERF_END (DriverEntry->ImageHandle, "StartImage:", NULL, 0);\r
e42e9404 917 if (EFI_ERROR(Status)){\r
84edd20b 918 UnregisterSmramProfileImage (DriverEntry, TRUE);\r
e42e9404 919 SmmFreePages(DriverEntry->ImageBuffer, DriverEntry->NumberOfPage);\r
02f02b16
JY
920 //\r
921 // Uninstall LoadedImage\r
922 //\r
923 Status = gBS->UninstallProtocolInterface (\r
924 DriverEntry->ImageHandle,\r
925 &gEfiLoadedImageProtocolGuid,\r
926 DriverEntry->LoadedImage\r
927 );\r
928 if (!EFI_ERROR (Status)) {\r
929 if (DriverEntry->LoadedImage->FilePath != NULL) {\r
930 gBS->FreePool (DriverEntry->LoadedImage->FilePath);\r
931 }\r
932 gBS->FreePool (DriverEntry->LoadedImage);\r
933 }\r
285a682c
JY
934 Status = SmmUninstallProtocolInterface (\r
935 DriverEntry->SmmImageHandle,\r
936 &gEfiLoadedImageProtocolGuid,\r
937 &DriverEntry->SmmLoadedImage\r
938 );\r
939 if (!EFI_ERROR(Status)) {\r
940 if (DriverEntry->SmmLoadedImage.FilePath != NULL) {\r
941 SmmFreePool (DriverEntry->SmmLoadedImage.FilePath);\r
942 }\r
943 }\r
e42e9404 944 }\r
945\r
946 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (\r
947 EFI_PROGRESS_CODE,\r
948 EFI_SOFTWARE_SMM_DRIVER | EFI_SW_PC_INIT_END,\r
949 &DriverEntry->ImageHandle,\r
950 sizeof (DriverEntry->ImageHandle)\r
951 );\r
952\r
5657b268 953 if (!PreviousSmmEntryPointRegistered && gSmmCorePrivate->SmmEntryPointRegistered) {\r
954 //\r
955 // Return immediately if the SMM Entry Point was registered by the SMM \r
956 // Driver that was just dispatched. The SMM IPL will reinvoke the SMM\r
957 // Core Dispatcher. This is required so SMM Mode may be enabled as soon \r
958 // as all the dependent SMM Drivers for SMM Mode have been dispatched. \r
959 // Once the SMM Entry Point has been registered, then SMM Mode will be \r
960 // used.\r
961 //\r
962 gRequestDispatch = TRUE;\r
963 gDispatcherRunning = FALSE;\r
964 return EFI_NOT_READY;\r
965 }\r
e42e9404 966 }\r
967\r
968 //\r
969 // Search DriverList for items to place on Scheduled Queue\r
970 //\r
971 ReadyToRun = FALSE;\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
974\r
975 if (DriverEntry->DepexProtocolError){\r
976 //\r
977 // If Section Extraction Protocol did not let the Depex be read before retry the read\r
978 //\r
979 Status = SmmGetDepexSectionAndPreProccess (DriverEntry);\r
980 }\r
981\r
982 if (DriverEntry->Dependent) {\r
983 if (SmmIsSchedulable (DriverEntry)) {\r
984 SmmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry);\r
985 ReadyToRun = TRUE;\r
986 }\r
987 }\r
988 }\r
989 } while (ReadyToRun);\r
990\r
991 //\r
992 // If there is no more SMM driver to dispatch, stop the dispatch request\r
993 //\r
994 gRequestDispatch = FALSE;\r
995 for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {\r
996 DriverEntry = CR (Link, EFI_SMM_DRIVER_ENTRY, Link, EFI_SMM_DRIVER_ENTRY_SIGNATURE);\r
997\r
998 if (!DriverEntry->Initialized){\r
999 //\r
1000 // We have SMM driver pending to dispatch\r
1001 //\r
1002 gRequestDispatch = TRUE;\r
1003 break;\r
1004 }\r
1005 }\r
1006\r
1007 gDispatcherRunning = FALSE;\r
1008\r
5657b268 1009 return EFI_SUCCESS;\r
e42e9404 1010}\r
1011\r
1012/**\r
1013 Insert InsertedDriverEntry onto the mScheduledQueue. To do this you\r
1014 must add any driver with a before dependency on InsertedDriverEntry first.\r
1015 You do this by recursively calling this routine. After all the Befores are\r
1016 processed you can add InsertedDriverEntry to the mScheduledQueue.\r
1017 Then you can add any driver with an After dependency on InsertedDriverEntry\r
1018 by recursively calling this routine.\r
1019\r
1020 @param InsertedDriverEntry The driver to insert on the ScheduledLink Queue\r
1021\r
1022**/\r
1023VOID\r
1024SmmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (\r
1025 IN EFI_SMM_DRIVER_ENTRY *InsertedDriverEntry\r
1026 )\r
1027{\r
1028 LIST_ENTRY *Link;\r
1029 EFI_SMM_DRIVER_ENTRY *DriverEntry;\r
1030\r
1031 //\r
1032 // Process Before Dependency\r
1033 //\r
1034 for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {\r
1035 DriverEntry = CR(Link, EFI_SMM_DRIVER_ENTRY, Link, EFI_SMM_DRIVER_ENTRY_SIGNATURE);\r
6a55eea3 1036 if (DriverEntry->Before && DriverEntry->Dependent && DriverEntry != InsertedDriverEntry) {\r
1037 DEBUG ((DEBUG_DISPATCH, "Evaluate SMM DEPEX for FFS(%g)\n", &DriverEntry->FileName));\r
1038 DEBUG ((DEBUG_DISPATCH, " BEFORE FFS(%g) = ", &DriverEntry->BeforeAfterGuid));\r
e42e9404 1039 if (CompareGuid (&InsertedDriverEntry->FileName, &DriverEntry->BeforeAfterGuid)) {\r
1040 //\r
1041 // Recursively process BEFORE\r
1042 //\r
6a55eea3 1043 DEBUG ((DEBUG_DISPATCH, "TRUE\n END\n RESULT = TRUE\n"));\r
e42e9404 1044 SmmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry);\r
6a55eea3 1045 } else {\r
1046 DEBUG ((DEBUG_DISPATCH, "FALSE\n END\n RESULT = FALSE\n"));\r
e42e9404 1047 }\r
1048 }\r
1049 }\r
1050\r
1051 //\r
1052 // Convert driver from Dependent to Scheduled state\r
1053 //\r
1054\r
1055 InsertedDriverEntry->Dependent = FALSE;\r
1056 InsertedDriverEntry->Scheduled = TRUE;\r
1057 InsertTailList (&mScheduledQueue, &InsertedDriverEntry->ScheduledLink);\r
1058\r
1059\r
1060 //\r
1061 // Process After Dependency\r
1062 //\r
1063 for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {\r
1064 DriverEntry = CR(Link, EFI_SMM_DRIVER_ENTRY, Link, EFI_SMM_DRIVER_ENTRY_SIGNATURE);\r
6a55eea3 1065 if (DriverEntry->After && DriverEntry->Dependent && DriverEntry != InsertedDriverEntry) {\r
1066 DEBUG ((DEBUG_DISPATCH, "Evaluate SMM DEPEX for FFS(%g)\n", &DriverEntry->FileName));\r
1067 DEBUG ((DEBUG_DISPATCH, " AFTER FFS(%g) = ", &DriverEntry->BeforeAfterGuid));\r
e42e9404 1068 if (CompareGuid (&InsertedDriverEntry->FileName, &DriverEntry->BeforeAfterGuid)) {\r
1069 //\r
1070 // Recursively process AFTER\r
1071 //\r
6a55eea3 1072 DEBUG ((DEBUG_DISPATCH, "TRUE\n END\n RESULT = TRUE\n"));\r
e42e9404 1073 SmmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (DriverEntry);\r
6a55eea3 1074 } else {\r
1075 DEBUG ((DEBUG_DISPATCH, "FALSE\n END\n RESULT = FALSE\n"));\r
e42e9404 1076 }\r
1077 }\r
1078 }\r
1079}\r
1080\r
1081/**\r
1082 Return TRUE if the Fv has been processed, FALSE if not.\r
1083\r
1084 @param FvHandle The handle of a FV that's being tested\r
1085\r
1086 @retval TRUE Fv protocol on FvHandle has been processed\r
1087 @retval FALSE Fv protocol on FvHandle has not yet been\r
1088 processed\r
1089\r
1090**/\r
1091BOOLEAN\r
1092FvHasBeenProcessed (\r
1093 IN EFI_HANDLE FvHandle\r
1094 )\r
1095{\r
1096 LIST_ENTRY *Link;\r
1097 KNOWN_HANDLE *KnownHandle;\r
1098\r
1099 for (Link = mFvHandleList.ForwardLink; Link != &mFvHandleList; Link = Link->ForwardLink) {\r
1100 KnownHandle = CR(Link, KNOWN_HANDLE, Link, KNOWN_HANDLE_SIGNATURE);\r
1101 if (KnownHandle->Handle == FvHandle) {\r
1102 return TRUE;\r
1103 }\r
1104 }\r
1105 return FALSE;\r
1106}\r
1107\r
1108/**\r
1109 Remember that Fv protocol on FvHandle has had it's drivers placed on the\r
1110 mDiscoveredList. This fucntion adds entries on the mFvHandleList. Items are\r
1111 never removed/freed from the mFvHandleList.\r
1112\r
1113 @param FvHandle The handle of a FV that has been processed\r
1114\r
1115**/\r
1116VOID\r
1117FvIsBeingProcesssed (\r
1118 IN EFI_HANDLE FvHandle\r
1119 )\r
1120{\r
1121 KNOWN_HANDLE *KnownHandle;\r
1122\r
1123 KnownHandle = AllocatePool (sizeof (KNOWN_HANDLE));\r
1124 ASSERT (KnownHandle != NULL);\r
1125\r
1126 KnownHandle->Signature = KNOWN_HANDLE_SIGNATURE;\r
1127 KnownHandle->Handle = FvHandle;\r
1128 InsertTailList (&mFvHandleList, &KnownHandle->Link);\r
1129}\r
1130\r
1131/**\r
1132 Convert FvHandle and DriverName into an EFI device path\r
1133\r
1134 @param Fv Fv protocol, needed to read Depex info out of\r
1135 FLASH.\r
1136 @param FvHandle Handle for Fv, needed in the\r
1137 EFI_SMM_DRIVER_ENTRY so that the PE image can be\r
1138 read out of the FV at a later time.\r
1139 @param DriverName Name of driver to add to mDiscoveredList.\r
1140\r
1141 @return Pointer to device path constructed from FvHandle and DriverName\r
1142\r
1143**/\r
1144EFI_DEVICE_PATH_PROTOCOL *\r
1145SmmFvToDevicePath (\r
1146 IN EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv,\r
1147 IN EFI_HANDLE FvHandle,\r
1148 IN EFI_GUID *DriverName\r
1149 )\r
1150{\r
1151 EFI_STATUS Status;\r
1152 EFI_DEVICE_PATH_PROTOCOL *FvDevicePath;\r
1153 EFI_DEVICE_PATH_PROTOCOL *FileNameDevicePath;\r
1154\r
1155 //\r
1156 // Remember the device path of the FV\r
1157 //\r
1158 Status = gBS->HandleProtocol (FvHandle, &gEfiDevicePathProtocolGuid, (VOID **)&FvDevicePath);\r
1159 if (EFI_ERROR (Status)) {\r
1160 FileNameDevicePath = NULL;\r
1161 } else {\r
1162 //\r
1163 // Build a device path to the file in the FV to pass into gBS->LoadImage\r
1164 //\r
1165 EfiInitializeFwVolDevicepathNode (&mFvDevicePath.File, DriverName);\r
1166 SetDevicePathEndNode (&mFvDevicePath.End);\r
1167\r
1168 //\r
1169 // Note: FileNameDevicePath is in DXE memory\r
1170 //\r
1171 FileNameDevicePath = AppendDevicePath (\r
1172 FvDevicePath,\r
1173 (EFI_DEVICE_PATH_PROTOCOL *)&mFvDevicePath\r
1174 );\r
1175 }\r
1176 return FileNameDevicePath;\r
1177}\r
1178\r
1179/**\r
1180 Add an entry to the mDiscoveredList. Allocate memory to store the DriverEntry,\r
1181 and initilize any state variables. Read the Depex from the FV and store it\r
fa542a1e 1182 in DriverEntry. Pre-process the Depex to set the Before and After state.\r
e42e9404 1183 The Discovered list is never free'ed and contains booleans that represent the\r
1184 other possible SMM driver states.\r
1185\r
1186 @param Fv Fv protocol, needed to read Depex info out of\r
1187 FLASH.\r
1188 @param FvHandle Handle for Fv, needed in the\r
1189 EFI_SMM_DRIVER_ENTRY so that the PE image can be\r
1190 read out of the FV at a later time.\r
1191 @param DriverName Name of driver to add to mDiscoveredList.\r
1192\r
1193 @retval EFI_SUCCESS If driver was added to the mDiscoveredList.\r
1194 @retval EFI_ALREADY_STARTED The driver has already been started. Only one\r
1195 DriverName may be active in the system at any one\r
1196 time.\r
1197\r
1198**/\r
1199EFI_STATUS\r
1200SmmAddToDriverList (\r
1201 IN EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv,\r
1202 IN EFI_HANDLE FvHandle,\r
1203 IN EFI_GUID *DriverName\r
1204 )\r
1205{\r
1206 EFI_SMM_DRIVER_ENTRY *DriverEntry;\r
1207\r
1208 //\r
1209 // Create the Driver Entry for the list. ZeroPool initializes lots of variables to\r
1210 // NULL or FALSE.\r
1211 //\r
1212 DriverEntry = AllocateZeroPool (sizeof (EFI_SMM_DRIVER_ENTRY));\r
1213 ASSERT (DriverEntry != NULL);\r
1214\r
1215 DriverEntry->Signature = EFI_SMM_DRIVER_ENTRY_SIGNATURE;\r
1216 CopyGuid (&DriverEntry->FileName, DriverName);\r
1217 DriverEntry->FvHandle = FvHandle;\r
1218 DriverEntry->Fv = Fv;\r
1219 DriverEntry->FvFileDevicePath = SmmFvToDevicePath (Fv, FvHandle, DriverName);\r
1220\r
1221 SmmGetDepexSectionAndPreProccess (DriverEntry);\r
1222\r
1223 InsertTailList (&mDiscoveredList, &DriverEntry->Link);\r
1224 gRequestDispatch = TRUE;\r
1225\r
1226 return EFI_SUCCESS;\r
1227}\r
1228\r
1229/**\r
1230 This function is the main entry point for an SMM handler dispatch\r
1231 or communicate-based callback.\r
1232\r
1233 Event notification that is fired every time a FV dispatch protocol is added.\r
1234 More than one protocol may have been added when this event is fired, so you\r
1235 must loop on SmmLocateHandle () to see how many protocols were added and\r
1236 do the following to each FV:\r
1237 If the Fv has already been processed, skip it. If the Fv has not been\r
1238 processed then mark it as being processed, as we are about to process it.\r
1239 Read the Fv and add any driver in the Fv to the mDiscoveredList.The\r
1240 mDiscoveredList is never free'ed and contains variables that define\r
1241 the other states the SMM driver transitions to..\r
1242 While you are at it read the A Priori file into memory.\r
1243 Place drivers in the A Priori list onto the mScheduledQueue.\r
1244\r
1245 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().\r
1246 @param Context Points to an optional handler context which was specified when the handler was registered.\r
1247 @param CommBuffer A pointer to a collection of data in memory that will\r
1248 be conveyed from a non-SMM environment into an SMM environment.\r
1249 @param CommBufferSize The size of the CommBuffer.\r
1250\r
1251 @return Status Code\r
1252\r
1253**/\r
1254EFI_STATUS\r
1255EFIAPI\r
1256SmmDriverDispatchHandler (\r
1257 IN EFI_HANDLE DispatchHandle,\r
1258 IN CONST VOID *Context, OPTIONAL\r
1259 IN OUT VOID *CommBuffer, OPTIONAL\r
1260 IN OUT UINTN *CommBufferSize OPTIONAL\r
1261 )\r
1262{\r
1263 EFI_STATUS Status;\r
1264 UINTN HandleCount;\r
1265 EFI_HANDLE *HandleBuffer;\r
1266 EFI_STATUS GetNextFileStatus;\r
e42e9404 1267 EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;\r
1268 EFI_DEVICE_PATH_PROTOCOL *FvDevicePath;\r
1269 EFI_HANDLE FvHandle;\r
1270 EFI_GUID NameGuid;\r
1271 UINTN Key;\r
1272 EFI_FV_FILETYPE Type;\r
1273 EFI_FV_FILE_ATTRIBUTES Attributes;\r
1274 UINTN Size;\r
1275 EFI_SMM_DRIVER_ENTRY *DriverEntry;\r
1276 EFI_GUID *AprioriFile;\r
1277 UINTN AprioriEntryCount;\r
5d832d62
ZB
1278 UINTN HandleIndex;\r
1279 UINTN SmmTypeIndex;\r
1280 UINTN AprioriIndex;\r
e42e9404 1281 LIST_ENTRY *Link;\r
1282 UINT32 AuthenticationStatus;\r
1283 UINTN SizeOfBuffer;\r
1284\r
1285 HandleBuffer = NULL;\r
1286 Status = gBS->LocateHandleBuffer (\r
1287 ByProtocol,\r
1288 &gEfiFirmwareVolume2ProtocolGuid,\r
1289 NULL,\r
1290 &HandleCount,\r
1291 &HandleBuffer\r
1292 );\r
1293 if (EFI_ERROR (Status)) {\r
1294 return EFI_NOT_FOUND;\r
1295 }\r
1296\r
5d832d62
ZB
1297 for (HandleIndex = 0; HandleIndex < HandleCount; HandleIndex++) {\r
1298 FvHandle = HandleBuffer[HandleIndex];\r
e42e9404 1299\r
1300 if (FvHasBeenProcessed (FvHandle)) {\r
1301 //\r
1302 // This Fv has already been processed so lets skip it!\r
1303 //\r
1304 continue;\r
1305 }\r
1306\r
1307 //\r
1308 // Since we are about to process this Fv mark it as processed.\r
1309 //\r
1310 FvIsBeingProcesssed (FvHandle);\r
1311\r
1312 Status = gBS->HandleProtocol (FvHandle, &gEfiFirmwareVolume2ProtocolGuid, (VOID **)&Fv);\r
1313 if (EFI_ERROR (Status)) {\r
1314 //\r
1315 // FvHandle must have a Firmware Volume2 Protocol thus we should never get here.\r
1316 //\r
1317 ASSERT (FALSE);\r
1318 continue;\r
1319 }\r
1320\r
1321 Status = gBS->HandleProtocol (FvHandle, &gEfiDevicePathProtocolGuid, (VOID **)&FvDevicePath);\r
1322 if (EFI_ERROR (Status)) {\r
1323 //\r
1324 // The Firmware volume doesn't have device path, can't be dispatched.\r
1325 //\r
1326 continue;\r
1327 }\r
1328\r
e42e9404 1329 //\r
1330 // Discover Drivers in FV and add them to the Discovered Driver List.\r
1331 // Process EFI_FV_FILETYPE_SMM type and then EFI_FV_FILETYPE_COMBINED_SMM_DXE\r
0b256fb1 1332 // EFI_FV_FILETYPE_SMM_CORE is processed to produce a Loaded Image protocol for the core\r
e42e9404 1333 //\r
5d832d62 1334 for (SmmTypeIndex = 0; SmmTypeIndex < sizeof (mSmmFileTypes)/sizeof (EFI_FV_FILETYPE); SmmTypeIndex++) {\r
e42e9404 1335 //\r
1336 // Initialize the search key\r
1337 //\r
1338 Key = 0;\r
1339 do {\r
5d832d62 1340 Type = mSmmFileTypes[SmmTypeIndex];\r
e42e9404 1341 GetNextFileStatus = Fv->GetNextFile (\r
1342 Fv,\r
1343 &Key,\r
1344 &Type,\r
1345 &NameGuid,\r
1346 &Attributes,\r
1347 &Size\r
1348 );\r
1349 if (!EFI_ERROR (GetNextFileStatus)) {\r
0b256fb1
JY
1350 if (Type == EFI_FV_FILETYPE_SMM_CORE) {\r
1351 //\r
1352 // If this is the SMM core fill in it's DevicePath & DeviceHandle\r
1353 //\r
1354 if (mSmmCoreLoadedImage->FilePath == NULL) {\r
1355 //\r
1356 // Maybe one special FV contains only one SMM_CORE module, so its device path must\r
1357 // be initialized completely.\r
1358 //\r
1359 EfiInitializeFwVolDevicepathNode (&mFvDevicePath.File, &NameGuid);\r
1360 SetDevicePathEndNode (&mFvDevicePath.End);\r
1361\r
1362 //\r
1363 // Make an EfiBootServicesData buffer copy of FilePath\r
1364 //\r
1365 Status = gBS->AllocatePool (\r
1366 EfiBootServicesData,\r
1367 GetDevicePathSize ((EFI_DEVICE_PATH_PROTOCOL *)&mFvDevicePath),\r
1368 (VOID **)&mSmmCoreLoadedImage->FilePath\r
1369 );\r
1370 ASSERT_EFI_ERROR (Status);\r
1371 CopyMem (mSmmCoreLoadedImage->FilePath, &mFvDevicePath, GetDevicePathSize ((EFI_DEVICE_PATH_PROTOCOL *)&mFvDevicePath));\r
1372\r
1373 mSmmCoreLoadedImage->DeviceHandle = FvHandle;\r
1374 }\r
285a682c
JY
1375 if (mSmmCoreDriverEntry->SmmLoadedImage.FilePath == NULL) {\r
1376 //\r
1377 // Maybe one special FV contains only one SMM_CORE module, so its device path must\r
1378 // be initialized completely.\r
1379 //\r
1380 EfiInitializeFwVolDevicepathNode (&mFvDevicePath.File, &NameGuid);\r
1381 SetDevicePathEndNode (&mFvDevicePath.End);\r
1382\r
1383 //\r
1384 // Make a buffer copy FilePath\r
1385 //\r
1386 Status = SmmAllocatePool (\r
1387 EfiRuntimeServicesData,\r
1388 GetDevicePathSize ((EFI_DEVICE_PATH_PROTOCOL *)&mFvDevicePath),\r
1389 (VOID **)&mSmmCoreDriverEntry->SmmLoadedImage.FilePath\r
1390 );\r
1391 ASSERT_EFI_ERROR (Status);\r
1392 CopyMem (mSmmCoreDriverEntry->SmmLoadedImage.FilePath, &mFvDevicePath, GetDevicePathSize((EFI_DEVICE_PATH_PROTOCOL *)&mFvDevicePath));\r
1393\r
1394 mSmmCoreDriverEntry->SmmLoadedImage.DeviceHandle = FvHandle;\r
1395 }\r
0b256fb1
JY
1396 } else {\r
1397 SmmAddToDriverList (Fv, FvHandle, &NameGuid);\r
1398 }\r
e42e9404 1399 }\r
1400 } while (!EFI_ERROR (GetNextFileStatus));\r
1401 }\r
1402\r
1403 //\r
1404 // Read the array of GUIDs from the Apriori file if it is present in the firmware volume\r
1405 // (Note: AprioriFile is in DXE memory)\r
1406 //\r
1407 AprioriFile = NULL;\r
1408 Status = Fv->ReadSection (\r
1409 Fv,\r
1410 &gAprioriGuid,\r
1411 EFI_SECTION_RAW,\r
1412 0,\r
1413 (VOID **)&AprioriFile,\r
1414 &SizeOfBuffer,\r
1415 &AuthenticationStatus\r
1416 );\r
1417 if (!EFI_ERROR (Status)) {\r
1418 AprioriEntryCount = SizeOfBuffer / sizeof (EFI_GUID);\r
1419 } else {\r
1420 AprioriEntryCount = 0;\r
1421 }\r
1422\r
1423 //\r
1424 // Put drivers on Apriori List on the Scheduled queue. The Discovered List includes\r
1425 // drivers not in the current FV and these must be skipped since the a priori list\r
1426 // is only valid for the FV that it resided in.\r
1427 //\r
1428\r
5d832d62 1429 for (AprioriIndex = 0; AprioriIndex < AprioriEntryCount; AprioriIndex++) {\r
e42e9404 1430 for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {\r
1431 DriverEntry = CR(Link, EFI_SMM_DRIVER_ENTRY, Link, EFI_SMM_DRIVER_ENTRY_SIGNATURE);\r
5d832d62 1432 if (CompareGuid (&DriverEntry->FileName, &AprioriFile[AprioriIndex]) &&\r
e42e9404 1433 (FvHandle == DriverEntry->FvHandle)) {\r
1434 DriverEntry->Dependent = FALSE;\r
1435 DriverEntry->Scheduled = TRUE;\r
1436 InsertTailList (&mScheduledQueue, &DriverEntry->ScheduledLink);\r
6a55eea3 1437 DEBUG ((DEBUG_DISPATCH, "Evaluate SMM DEPEX for FFS(%g)\n", &DriverEntry->FileName));\r
1438 DEBUG ((DEBUG_DISPATCH, " RESULT = TRUE (Apriori)\n"));\r
e42e9404 1439 break;\r
1440 }\r
1441 }\r
1442 }\r
1443\r
1444 //\r
1445 // Free data allocated by Fv->ReadSection ()\r
1446 //\r
1447 // The UEFI Boot Services FreePool() function must be used because Fv->ReadSection \r
1448 // used the UEFI Boot Services AllocatePool() function\r
1449 //\r
1450 gBS->FreePool (AprioriFile);\r
1451 }\r
1452\r
1453 //\r
1454 // Execute the SMM Dispatcher on any newly discovered FVs and previously \r
1455 // discovered SMM drivers that have been discovered but not dispatched.\r
1456 //\r
5657b268 1457 Status = SmmDispatcher ();\r
1458\r
1459 //\r
1460 // Check to see if CommBuffer and CommBufferSize are valid\r
1461 //\r
1462 if (CommBuffer != NULL && CommBufferSize != NULL) {\r
1463 if (*CommBufferSize > 0) {\r
1464 if (Status == EFI_NOT_READY) {\r
1465 //\r
1466 // If a the SMM Core Entry Point was just registered, then set flag to \r
1467 // request the SMM Dispatcher to be restarted.\r
1468 //\r
1469 *(UINT8 *)CommBuffer = COMM_BUFFER_SMM_DISPATCH_RESTART;\r
1470 } else if (!EFI_ERROR (Status)) {\r
1471 //\r
1472 // Set the flag to show that the SMM Dispatcher executed without errors\r
1473 //\r
1474 *(UINT8 *)CommBuffer = COMM_BUFFER_SMM_DISPATCH_SUCCESS;\r
1475 } else {\r
1476 //\r
1477 // Set the flag to show that the SMM Dispatcher encountered an error\r
1478 //\r
1479 *(UINT8 *)CommBuffer = COMM_BUFFER_SMM_DISPATCH_ERROR;\r
1480 }\r
1481 }\r
1482 }\r
1483\r
1484 return EFI_SUCCESS;\r
e42e9404 1485}\r
1486\r
1487/**\r
1488 Traverse the discovered list for any drivers that were discovered but not loaded\r
1489 because the dependency experessions evaluated to false.\r
1490\r
1491**/\r
1492VOID\r
1493SmmDisplayDiscoveredNotDispatched (\r
1494 VOID\r
1495 )\r
1496{\r
1497 LIST_ENTRY *Link;\r
1498 EFI_SMM_DRIVER_ENTRY *DriverEntry;\r
1499\r
1500 for (Link = mDiscoveredList.ForwardLink;Link !=&mDiscoveredList; Link = Link->ForwardLink) {\r
1501 DriverEntry = CR(Link, EFI_SMM_DRIVER_ENTRY, Link, EFI_SMM_DRIVER_ENTRY_SIGNATURE);\r
1502 if (DriverEntry->Dependent) {\r
1503 DEBUG ((DEBUG_LOAD, "SMM Driver %g was discovered but not loaded!!\n", &DriverEntry->FileName));\r
1504 }\r
1505 }\r
1506}\r