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