]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Core/Pei/Dispatcher/Dispatcher.c
ffeab4e391a4dea53b9ac80101b03a55138cd51e
[mirror_edk2.git] / MdeModulePkg / Core / Pei / Dispatcher / Dispatcher.c
1 /** @file
2 EFI PEI Core dispatch services
3
4 Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include "PeiMain.h"
16
17 ///
18 /// temporary memory is filled with this initial value during SEC phase
19 ///
20 #define INIT_CAR_VALUE 0x5AA55AA5
21
22 typedef struct {
23 EFI_STATUS_CODE_DATA DataHeader;
24 EFI_HANDLE Handle;
25 } PEIM_FILE_HANDLE_EXTENDED_DATA;
26
27 /**
28
29 Discover all Peims and optional Apriori file in one FV. There is at most one
30 Apriori file in one FV.
31
32
33 @param Private Pointer to the private data passed in from caller
34 @param CoreFileHandle The instance of PEI_CORE_FV_HANDLE.
35
36 **/
37 VOID
38 DiscoverPeimsAndOrderWithApriori (
39 IN PEI_CORE_INSTANCE *Private,
40 IN PEI_CORE_FV_HANDLE *CoreFileHandle
41 )
42 {
43 EFI_STATUS Status;
44 EFI_PEI_FILE_HANDLE FileHandle;
45 EFI_PEI_FILE_HANDLE AprioriFileHandle;
46 EFI_GUID *Apriori;
47 UINTN Index;
48 UINTN Index2;
49 UINTN PeimIndex;
50 UINTN PeimCount;
51 EFI_GUID *Guid;
52 EFI_PEI_FILE_HANDLE TempFileHandles[FixedPcdGet32 (PcdPeiCoreMaxPeimPerFv)];
53 EFI_GUID FileGuid[FixedPcdGet32 (PcdPeiCoreMaxPeimPerFv)];
54 EFI_PEI_FIRMWARE_VOLUME_PPI *FvPpi;
55 EFI_FV_FILE_INFO FileInfo;
56
57 FvPpi = CoreFileHandle->FvPpi;
58
59 //
60 // Walk the FV and find all the PEIMs and the Apriori file.
61 //
62 AprioriFileHandle = NULL;
63 Private->CurrentFvFileHandles[0] = NULL;
64 Guid = NULL;
65 FileHandle = NULL;
66
67 //
68 // If the current Fv has been scanned, directly get its cachable record.
69 //
70 if (Private->Fv[Private->CurrentPeimFvCount].ScanFv) {
71 CopyMem (Private->CurrentFvFileHandles, Private->Fv[Private->CurrentPeimFvCount].FvFileHandles, sizeof (Private->CurrentFvFileHandles));
72 return;
73 }
74
75 //
76 // Go ahead to scan this Fv, and cache FileHandles within it.
77 //
78 for (PeimCount = 0; PeimCount < FixedPcdGet32 (PcdPeiCoreMaxPeimPerFv); PeimCount++) {
79 Status = FvPpi->FindFileByType (FvPpi, PEI_CORE_INTERNAL_FFS_FILE_DISPATCH_TYPE, CoreFileHandle->FvHandle, &FileHandle);
80 if (Status != EFI_SUCCESS) {
81 break;
82 }
83
84 Private->CurrentFvFileHandles[PeimCount] = FileHandle;
85 }
86
87 //
88 // Check whether the count of Peims exceeds the max support PEIMs in a FV image
89 // If more Peims are required in a FV image, PcdPeiCoreMaxPeimPerFv can be set to a larger value in DSC file.
90 //
91 ASSERT (PeimCount < FixedPcdGet32 (PcdPeiCoreMaxPeimPerFv));
92
93 //
94 // Get Apriori File handle
95 //
96 Private->AprioriCount = 0;
97 Status = FvPpi->FindFileByName (FvPpi, &gPeiAprioriFileNameGuid, &CoreFileHandle->FvHandle, &AprioriFileHandle);
98 if (!EFI_ERROR(Status) && AprioriFileHandle != NULL) {
99 //
100 // Read the Apriori file
101 //
102 Status = FvPpi->FindSectionByType (FvPpi, EFI_SECTION_RAW, AprioriFileHandle, (VOID **) &Apriori);
103 if (!EFI_ERROR (Status)) {
104 //
105 // Calculate the number of PEIMs in the A Priori list
106 //
107 Status = FvPpi->GetFileInfo (FvPpi, AprioriFileHandle, &FileInfo);
108 ASSERT_EFI_ERROR (Status);
109 Private->AprioriCount = FileInfo.BufferSize;
110 if (IS_SECTION2 (FileInfo.Buffer)) {
111 Private->AprioriCount -= sizeof (EFI_COMMON_SECTION_HEADER2);
112 } else {
113 Private->AprioriCount -= sizeof (EFI_COMMON_SECTION_HEADER);
114 }
115 Private->AprioriCount /= sizeof (EFI_GUID);
116
117 ZeroMem (FileGuid, sizeof (FileGuid));
118 for (Index = 0; Index < PeimCount; Index++) {
119 //
120 // Make an array of file name guids that matches the FileHandle array so we can convert
121 // quickly from file name to file handle
122 //
123 Status = FvPpi->GetFileInfo (FvPpi, Private->CurrentFvFileHandles[Index], &FileInfo);
124 CopyMem (&FileGuid[Index], &FileInfo.FileName, sizeof(EFI_GUID));
125 }
126
127 //
128 // Walk through FileGuid array to find out who is invalid PEIM guid in Apriori file.
129 // Add available PEIMs in Apriori file into TempFileHandles array at first.
130 //
131 Index2 = 0;
132 for (Index = 0; Index2 < Private->AprioriCount; Index++) {
133 while (Index2 < Private->AprioriCount) {
134 Guid = ScanGuid (FileGuid, PeimCount * sizeof (EFI_GUID), &Apriori[Index2++]);
135 if (Guid != NULL) {
136 break;
137 }
138 }
139 if (Guid == NULL) {
140 break;
141 }
142 PeimIndex = ((UINTN)Guid - (UINTN)&FileGuid[0])/sizeof (EFI_GUID);
143 TempFileHandles[Index] = Private->CurrentFvFileHandles[PeimIndex];
144
145 //
146 // Since we have copied the file handle we can remove it from this list.
147 //
148 Private->CurrentFvFileHandles[PeimIndex] = NULL;
149 }
150
151 //
152 // Update valid Aprioricount
153 //
154 Private->AprioriCount = Index;
155
156 //
157 // Add in any PEIMs not in the Apriori file
158 //
159 for (;Index < PeimCount; Index++) {
160 for (Index2 = 0; Index2 < PeimCount; Index2++) {
161 if (Private->CurrentFvFileHandles[Index2] != NULL) {
162 TempFileHandles[Index] = Private->CurrentFvFileHandles[Index2];
163 Private->CurrentFvFileHandles[Index2] = NULL;
164 break;
165 }
166 }
167 }
168 //
169 //Index the end of array contains re-range Pei moudle.
170 //
171 TempFileHandles[Index] = NULL;
172
173 //
174 // Private->CurrentFvFileHandles is currently in PEIM in the FV order.
175 // We need to update it to start with files in the A Priori list and
176 // then the remaining files in PEIM order.
177 //
178 CopyMem (Private->CurrentFvFileHandles, TempFileHandles, sizeof (Private->CurrentFvFileHandles));
179 }
180 }
181 //
182 // Cache the current Fv File Handle. So that we don't have to scan the Fv again.
183 // Instead, we can retrieve the file handles within this Fv from cachable data.
184 //
185 Private->Fv[Private->CurrentPeimFvCount].ScanFv = TRUE;
186 CopyMem (Private->Fv[Private->CurrentPeimFvCount].FvFileHandles, Private->CurrentFvFileHandles, sizeof (Private->CurrentFvFileHandles));
187
188 }
189
190 //
191 // This is the minimum memory required by DxeCore initialization. When LMFA feature enabled,
192 // This part of memory still need reserved on the very top of memory so that the DXE Core could
193 // use these memory for data initialization. This macro should be sync with the same marco
194 // defined in DXE Core.
195 //
196 #define MINIMUM_INITIAL_MEMORY_SIZE 0x10000
197 /**
198 This function is to test if the memory range described in resource HOB is available or not.
199
200 This function should only be invoked when Loading Module at Fixed Address(LMFA) feature is enabled. Some platform may allocate the
201 memory before PeiLoadFixAddressHook in invoked. so this function is to test if the memory range described by the input resource HOB is
202 available or not.
203
204 @param PrivateData Pointer to the private data passed in from caller
205 @param ResourceHob Pointer to a resource HOB which described the memory range described by the input resource HOB
206 **/
207 BOOLEAN
208 PeiLoadFixAddressIsMemoryRangeAvailable (
209 IN PEI_CORE_INSTANCE *PrivateData,
210 IN EFI_HOB_RESOURCE_DESCRIPTOR *ResourceHob
211 )
212 {
213 EFI_HOB_MEMORY_ALLOCATION *MemoryHob;
214 BOOLEAN IsAvailable;
215 EFI_PEI_HOB_POINTERS Hob;
216
217 IsAvailable = TRUE;
218 if (PrivateData == NULL || ResourceHob == NULL) {
219 return FALSE;
220 }
221 //
222 // test if the memory range describe in the HOB is already allocated.
223 //
224 for (Hob.Raw = PrivateData->HobList.Raw; !END_OF_HOB_LIST(Hob); Hob.Raw = GET_NEXT_HOB(Hob)) {
225 //
226 // See if this is a memory allocation HOB
227 //
228 if (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_MEMORY_ALLOCATION) {
229 MemoryHob = Hob.MemoryAllocation;
230 if(MemoryHob->AllocDescriptor.MemoryBaseAddress == ResourceHob->PhysicalStart &&
231 MemoryHob->AllocDescriptor.MemoryBaseAddress + MemoryHob->AllocDescriptor.MemoryLength == ResourceHob->PhysicalStart + ResourceHob->ResourceLength) {
232 IsAvailable = FALSE;
233 break;
234 }
235 }
236 }
237
238 return IsAvailable;
239
240 }
241 /**
242 Hook function for Loading Module at Fixed Address feature
243
244 This function should only be invoked when Loading Module at Fixed Address(LMFA) feature is enabled. When feature is
245 configured as Load Modules at Fix Absolute Address, this function is to validate the top address assigned by user. When
246 feature is configured as Load Modules at Fixed Offset, the functino is to find the top address which is TOLM-TSEG in general.
247 And also the function will re-install PEI memory.
248
249 @param PrivateData Pointer to the private data passed in from caller
250
251 **/
252 VOID
253 PeiLoadFixAddressHook(
254 IN PEI_CORE_INSTANCE *PrivateData
255 )
256 {
257 EFI_PHYSICAL_ADDRESS TopLoadingAddress;
258 UINT64 PeiMemorySize;
259 UINT64 TotalReservedMemorySize;
260 UINT64 MemoryRangeEnd;
261 EFI_PHYSICAL_ADDRESS HighAddress;
262 EFI_HOB_RESOURCE_DESCRIPTOR *ResourceHob;
263 EFI_HOB_RESOURCE_DESCRIPTOR *NextResourceHob;
264 EFI_HOB_RESOURCE_DESCRIPTOR *CurrentResourceHob;
265 EFI_PEI_HOB_POINTERS CurrentHob;
266 EFI_PEI_HOB_POINTERS Hob;
267 EFI_PEI_HOB_POINTERS NextHob;
268 EFI_PHYSICAL_ADDRESS MaxMemoryBaseAddress;
269 UINT64 MaxMemoryLength;
270 EFI_HOB_MEMORY_ALLOCATION *MemoryHob;
271 //
272 // Initialize Local Variables
273 //
274 CurrentResourceHob = NULL;
275 ResourceHob = NULL;
276 NextResourceHob = NULL;
277 MaxMemoryBaseAddress = 0;
278 MaxMemoryLength = 0;
279 HighAddress = 0;
280 TopLoadingAddress = 0;
281 MemoryRangeEnd = 0;
282 CurrentHob.Raw = PrivateData->HobList.Raw;
283 PeiMemorySize = PrivateData->PhysicalMemoryLength;
284 //
285 // The top reserved memory include 3 parts: the topest range is for DXE core initialization with the size MINIMUM_INITIAL_MEMORY_SIZE
286 // then RuntimeCodePage range and Boot time code range.
287 //
288 TotalReservedMemorySize = MINIMUM_INITIAL_MEMORY_SIZE + EFI_PAGES_TO_SIZE(PcdGet32(PcdLoadFixAddressRuntimeCodePageNumber));
289 TotalReservedMemorySize+= EFI_PAGES_TO_SIZE(PcdGet32(PcdLoadFixAddressBootTimeCodePageNumber)) ;
290 //
291 // PEI memory range lies below the top reserved memory
292 //
293 TotalReservedMemorySize += PeiMemorySize;
294
295 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO: PcdLoadFixAddressRuntimeCodePageNumber= 0x%x.\n", PcdGet32(PcdLoadFixAddressRuntimeCodePageNumber)));
296 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO: PcdLoadFixAddressBootTimeCodePageNumber= 0x%x.\n", PcdGet32(PcdLoadFixAddressBootTimeCodePageNumber)));
297 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO: PcdLoadFixAddressPeiCodePageNumber= 0x%x.\n", PcdGet32(PcdLoadFixAddressPeiCodePageNumber)));
298 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO: Total Reserved Memory Size = 0x%lx.\n", TotalReservedMemorySize));
299 //
300 // Loop through the system memory typed hob to merge the adjacent memory range
301 //
302 for (Hob.Raw = PrivateData->HobList.Raw; !END_OF_HOB_LIST(Hob); Hob.Raw = GET_NEXT_HOB(Hob)) {
303 //
304 // See if this is a resource descriptor HOB
305 //
306 if (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {
307
308 ResourceHob = Hob.ResourceDescriptor;
309 //
310 // If range described in this hob is not system memory or heigher than MAX_ADDRESS, ignored.
311 //
312 if (ResourceHob->ResourceType != EFI_RESOURCE_SYSTEM_MEMORY &&
313 ResourceHob->PhysicalStart + ResourceHob->ResourceLength > MAX_ADDRESS) {
314 continue;
315 }
316
317 for (NextHob.Raw = PrivateData->HobList.Raw; !END_OF_HOB_LIST(NextHob); NextHob.Raw = GET_NEXT_HOB(NextHob)) {
318 if (NextHob.Raw == Hob.Raw){
319 continue;
320 }
321 //
322 // See if this is a resource descriptor HOB
323 //
324 if (GET_HOB_TYPE (NextHob) == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {
325
326 NextResourceHob = NextHob.ResourceDescriptor;
327 //
328 // test if range described in this NextResourceHob is system memory and have the same attribute.
329 // Note: Here is a assumption that system memory should always be healthy even without test.
330 //
331 if (NextResourceHob->ResourceType == EFI_RESOURCE_SYSTEM_MEMORY &&
332 (((NextResourceHob->ResourceAttribute^ResourceHob->ResourceAttribute)&(~EFI_RESOURCE_ATTRIBUTE_TESTED)) == 0)){
333
334 //
335 // See if the memory range described in ResourceHob and NextResourceHob is adjacent
336 //
337 if ((ResourceHob->PhysicalStart <= NextResourceHob->PhysicalStart &&
338 ResourceHob->PhysicalStart + ResourceHob->ResourceLength >= NextResourceHob->PhysicalStart)||
339 (ResourceHob->PhysicalStart >= NextResourceHob->PhysicalStart&&
340 ResourceHob->PhysicalStart <= NextResourceHob->PhysicalStart + NextResourceHob->ResourceLength)) {
341
342 MemoryRangeEnd = ((ResourceHob->PhysicalStart + ResourceHob->ResourceLength)>(NextResourceHob->PhysicalStart + NextResourceHob->ResourceLength)) ?
343 (ResourceHob->PhysicalStart + ResourceHob->ResourceLength):(NextResourceHob->PhysicalStart + NextResourceHob->ResourceLength);
344
345 ResourceHob->PhysicalStart = (ResourceHob->PhysicalStart < NextResourceHob->PhysicalStart) ?
346 ResourceHob->PhysicalStart : NextResourceHob->PhysicalStart;
347
348
349 ResourceHob->ResourceLength = (MemoryRangeEnd - ResourceHob->PhysicalStart);
350
351 ResourceHob->ResourceAttribute = ResourceHob->ResourceAttribute & (~EFI_RESOURCE_ATTRIBUTE_TESTED);
352 //
353 // Delete the NextResourceHob by marking it as unused.
354 //
355 GET_HOB_TYPE (NextHob) = EFI_HOB_TYPE_UNUSED;
356
357 }
358 }
359 }
360 }
361 }
362 }
363 //
364 // Some platform is already allocated pages before the HOB re-org. Here to build dedicated resource HOB to describe
365 // the allocated memory range
366 //
367 for (Hob.Raw = PrivateData->HobList.Raw; !END_OF_HOB_LIST(Hob); Hob.Raw = GET_NEXT_HOB(Hob)) {
368 //
369 // See if this is a memory allocation HOB
370 //
371 if (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_MEMORY_ALLOCATION) {
372 MemoryHob = Hob.MemoryAllocation;
373 for (NextHob.Raw = PrivateData->HobList.Raw; !END_OF_HOB_LIST(NextHob); NextHob.Raw = GET_NEXT_HOB(NextHob)) {
374 //
375 // See if this is a resource descriptor HOB
376 //
377 if (GET_HOB_TYPE (NextHob) == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {
378 NextResourceHob = NextHob.ResourceDescriptor;
379 //
380 // If range described in this hob is not system memory or heigher than MAX_ADDRESS, ignored.
381 //
382 if (NextResourceHob->ResourceType == EFI_RESOURCE_SYSTEM_MEMORY && NextResourceHob->PhysicalStart + NextResourceHob->ResourceLength > MAX_ADDRESS) {
383 continue;
384 }
385 //
386 // If the range describe in memory allocation HOB belongs to the memroy range described by the resource hob
387 //
388 if (MemoryHob->AllocDescriptor.MemoryBaseAddress >= NextResourceHob->PhysicalStart &&
389 MemoryHob->AllocDescriptor.MemoryBaseAddress + MemoryHob->AllocDescriptor.MemoryLength <= NextResourceHob->PhysicalStart + NextResourceHob->ResourceLength) {
390 //
391 // Build seperate resource hob for this allocated range
392 //
393 if (MemoryHob->AllocDescriptor.MemoryBaseAddress > NextResourceHob->PhysicalStart) {
394 BuildResourceDescriptorHob (
395 EFI_RESOURCE_SYSTEM_MEMORY,
396 NextResourceHob->ResourceAttribute,
397 NextResourceHob->PhysicalStart,
398 (MemoryHob->AllocDescriptor.MemoryBaseAddress - NextResourceHob->PhysicalStart)
399 );
400 }
401 if (MemoryHob->AllocDescriptor.MemoryBaseAddress + MemoryHob->AllocDescriptor.MemoryLength < NextResourceHob->PhysicalStart + NextResourceHob->ResourceLength) {
402 BuildResourceDescriptorHob (
403 EFI_RESOURCE_SYSTEM_MEMORY,
404 NextResourceHob->ResourceAttribute,
405 MemoryHob->AllocDescriptor.MemoryBaseAddress + MemoryHob->AllocDescriptor.MemoryLength,
406 (NextResourceHob->PhysicalStart + NextResourceHob->ResourceLength -(MemoryHob->AllocDescriptor.MemoryBaseAddress + MemoryHob->AllocDescriptor.MemoryLength))
407 );
408 }
409 NextResourceHob->PhysicalStart = MemoryHob->AllocDescriptor.MemoryBaseAddress;
410 NextResourceHob->ResourceLength = MemoryHob->AllocDescriptor.MemoryLength;
411 break;
412 }
413 }
414 }
415 }
416 }
417
418 //
419 // Try to find and validate the TOP address.
420 //
421 if ((INT64)PcdGet64(PcdLoadModuleAtFixAddressEnable) > 0 ) {
422 //
423 // The LMFA feature is enabled as load module at fixed absolute address.
424 //
425 TopLoadingAddress = (EFI_PHYSICAL_ADDRESS)PcdGet64(PcdLoadModuleAtFixAddressEnable);
426 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO: Loading module at fixed absolute address.\n"));
427 //
428 // validate the Address. Loop the resource descriptor HOB to make sure the address is in valid memory range
429 //
430 if ((TopLoadingAddress & EFI_PAGE_MASK) != 0) {
431 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED ERROR:Top Address 0x%lx is invalid since top address should be page align. \n", TopLoadingAddress));
432 ASSERT (FALSE);
433 }
434 //
435 // Search for a memory region that is below MAX_ADDRESS and in which TopLoadingAddress lies
436 //
437 for (Hob.Raw = PrivateData->HobList.Raw; !END_OF_HOB_LIST(Hob); Hob.Raw = GET_NEXT_HOB(Hob)) {
438 //
439 // See if this is a resource descriptor HOB
440 //
441 if (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {
442
443 ResourceHob = Hob.ResourceDescriptor;
444 //
445 // See if this resource descrior HOB describes tested system memory below MAX_ADDRESS
446 //
447 if (ResourceHob->ResourceType == EFI_RESOURCE_SYSTEM_MEMORY &&
448 ResourceHob->PhysicalStart + ResourceHob->ResourceLength <= MAX_ADDRESS) {
449 //
450 // See if Top address specified by user is valid.
451 //
452 if (ResourceHob->PhysicalStart + TotalReservedMemorySize < TopLoadingAddress &&
453 (ResourceHob->PhysicalStart + ResourceHob->ResourceLength - MINIMUM_INITIAL_MEMORY_SIZE) >= TopLoadingAddress &&
454 PeiLoadFixAddressIsMemoryRangeAvailable(PrivateData, ResourceHob)) {
455 CurrentResourceHob = ResourceHob;
456 CurrentHob = Hob;
457 break;
458 }
459 }
460 }
461 }
462 if (CurrentResourceHob != NULL) {
463 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO:Top Address 0x%lx is valid \n", TopLoadingAddress));
464 TopLoadingAddress += MINIMUM_INITIAL_MEMORY_SIZE;
465 } else {
466 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED ERROR:Top Address 0x%lx is invalid \n", TopLoadingAddress));
467 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED ERROR:The recommended Top Address for the platform is: \n"));
468 //
469 // Print the recomended Top address range.
470 //
471 for (Hob.Raw = PrivateData->HobList.Raw; !END_OF_HOB_LIST(Hob); Hob.Raw = GET_NEXT_HOB(Hob)) {
472 //
473 // See if this is a resource descriptor HOB
474 //
475 if (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {
476
477 ResourceHob = Hob.ResourceDescriptor;
478 //
479 // See if this resource descrior HOB describes tested system memory below MAX_ADDRESS
480 //
481 if (ResourceHob->ResourceType == EFI_RESOURCE_SYSTEM_MEMORY &&
482 ResourceHob->PhysicalStart + ResourceHob->ResourceLength <= MAX_ADDRESS) {
483 //
484 // See if Top address specified by user is valid.
485 //
486 if (ResourceHob->ResourceLength > TotalReservedMemorySize && PeiLoadFixAddressIsMemoryRangeAvailable(PrivateData, ResourceHob)) {
487 DEBUG ((EFI_D_INFO, "(0x%lx, 0x%lx)\n",
488 (ResourceHob->PhysicalStart + TotalReservedMemorySize -MINIMUM_INITIAL_MEMORY_SIZE),
489 (ResourceHob->PhysicalStart + ResourceHob->ResourceLength -MINIMUM_INITIAL_MEMORY_SIZE)
490 ));
491 }
492 }
493 }
494 }
495 //
496 // Assert here
497 //
498 ASSERT (FALSE);
499 return;
500 }
501 } else {
502 //
503 // The LMFA feature is enabled as load module at fixed offset relative to TOLM
504 // Parse the Hob list to find the topest available memory. Generally it is (TOLM - TSEG)
505 //
506 //
507 // Search for a tested memory region that is below MAX_ADDRESS
508 //
509 for (Hob.Raw = PrivateData->HobList.Raw; !END_OF_HOB_LIST(Hob); Hob.Raw = GET_NEXT_HOB(Hob)) {
510 //
511 // See if this is a resource descriptor HOB
512 //
513 if (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {
514
515 ResourceHob = Hob.ResourceDescriptor;
516 //
517 // See if this resource descrior HOB describes tested system memory below MAX_ADDRESS
518 //
519 if (ResourceHob->ResourceType == EFI_RESOURCE_SYSTEM_MEMORY &&
520 ResourceHob->PhysicalStart + ResourceHob->ResourceLength <= MAX_ADDRESS &&
521 ResourceHob->ResourceLength > TotalReservedMemorySize && PeiLoadFixAddressIsMemoryRangeAvailable(PrivateData, ResourceHob)) {
522 //
523 // See if this is the highest largest system memory region below MaxAddress
524 //
525 if (ResourceHob->PhysicalStart > HighAddress) {
526 CurrentResourceHob = ResourceHob;
527 CurrentHob = Hob;
528 HighAddress = CurrentResourceHob->PhysicalStart;
529 }
530 }
531 }
532 }
533 if (CurrentResourceHob == NULL) {
534 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED ERROR:The System Memory is too small\n"));
535 //
536 // Assert here
537 //
538 ASSERT (FALSE);
539 return;
540 } else {
541 TopLoadingAddress = CurrentResourceHob->PhysicalStart + CurrentResourceHob->ResourceLength ;
542 }
543 }
544
545 if (CurrentResourceHob != NULL) {
546 //
547 // rebuild resource HOB for PEI memmory and reserved memory
548 //
549 BuildResourceDescriptorHob (
550 EFI_RESOURCE_SYSTEM_MEMORY,
551 (
552 EFI_RESOURCE_ATTRIBUTE_PRESENT |
553 EFI_RESOURCE_ATTRIBUTE_INITIALIZED |
554 EFI_RESOURCE_ATTRIBUTE_TESTED |
555 EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE |
556 EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE |
557 EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE |
558 EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE
559 ),
560 (TopLoadingAddress - TotalReservedMemorySize),
561 TotalReservedMemorySize
562 );
563 //
564 // rebuild resource for the remain memory if necessary
565 //
566 if (CurrentResourceHob->PhysicalStart < TopLoadingAddress - TotalReservedMemorySize) {
567 BuildResourceDescriptorHob (
568 EFI_RESOURCE_SYSTEM_MEMORY,
569 (
570 EFI_RESOURCE_ATTRIBUTE_PRESENT |
571 EFI_RESOURCE_ATTRIBUTE_INITIALIZED |
572 EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE |
573 EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE |
574 EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE |
575 EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE
576 ),
577 CurrentResourceHob->PhysicalStart,
578 (TopLoadingAddress - TotalReservedMemorySize - CurrentResourceHob->PhysicalStart)
579 );
580 }
581 if (CurrentResourceHob->PhysicalStart + CurrentResourceHob->ResourceLength > TopLoadingAddress ) {
582 BuildResourceDescriptorHob (
583 EFI_RESOURCE_SYSTEM_MEMORY,
584 (
585 EFI_RESOURCE_ATTRIBUTE_PRESENT |
586 EFI_RESOURCE_ATTRIBUTE_INITIALIZED |
587 EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE |
588 EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE |
589 EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE |
590 EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE
591 ),
592 TopLoadingAddress,
593 (CurrentResourceHob->PhysicalStart + CurrentResourceHob->ResourceLength - TopLoadingAddress)
594 );
595 }
596 //
597 // Delete CurrentHob by marking it as unused since the the memory range described by is rebuilt.
598 //
599 GET_HOB_TYPE (CurrentHob) = EFI_HOB_TYPE_UNUSED;
600 }
601
602 //
603 // Cache the top address for Loading Module at Fixed Address feature
604 //
605 PrivateData->LoadModuleAtFixAddressTopAddress = TopLoadingAddress - MINIMUM_INITIAL_MEMORY_SIZE;
606 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO: Top address = 0x%lx\n", PrivateData->LoadModuleAtFixAddressTopAddress));
607 //
608 // reinstall the PEI memory relative to TopLoadingAddress
609 //
610 PrivateData->PhysicalMemoryBegin = TopLoadingAddress - TotalReservedMemorySize;
611 PrivateData->FreePhysicalMemoryTop = PrivateData->PhysicalMemoryBegin + PeiMemorySize;
612 }
613 /**
614 Conduct PEIM dispatch.
615
616 @param SecCoreData Points to a data structure containing information about the PEI core's operating
617 environment, such as the size and location of temporary RAM, the stack location and
618 the BFV location.
619 @param Private Pointer to the private data passed in from caller
620
621 **/
622 VOID
623 PeiDispatcher (
624 IN CONST EFI_SEC_PEI_HAND_OFF *SecCoreData,
625 IN PEI_CORE_INSTANCE *Private
626 )
627 {
628 EFI_STATUS Status;
629 UINT32 Index1;
630 UINT32 Index2;
631 CONST EFI_PEI_SERVICES **PeiServices;
632 EFI_PEI_FILE_HANDLE PeimFileHandle;
633 UINTN FvCount;
634 UINTN PeimCount;
635 UINT32 AuthenticationState;
636 EFI_PHYSICAL_ADDRESS EntryPoint;
637 EFI_PEIM_ENTRY_POINT2 PeimEntryPoint;
638 UINTN SaveCurrentPeimCount;
639 UINTN SaveCurrentFvCount;
640 EFI_PEI_FILE_HANDLE SaveCurrentFileHandle;
641 PEIM_FILE_HANDLE_EXTENDED_DATA ExtendedData;
642 EFI_PEI_TEMPORARY_RAM_SUPPORT_PPI *TemporaryRamSupportPpi;
643 UINT64 NewStackSize;
644 EFI_PHYSICAL_ADDRESS TopOfNewStack;
645 EFI_PHYSICAL_ADDRESS TopOfOldStack;
646 EFI_PHYSICAL_ADDRESS TemporaryRamBase;
647 UINTN TemporaryRamSize;
648 EFI_PHYSICAL_ADDRESS TemporaryStackSize;
649 UINTN StackOffset;
650 BOOLEAN StackOffsetPositive;
651 EFI_FV_FILE_INFO FvFileInfo;
652 PEI_CORE_FV_HANDLE *CoreFvHandle;
653 VOID *LoadFixPeiCodeBegin;
654
655 PeiServices = (CONST EFI_PEI_SERVICES **) &Private->Ps;
656 PeimEntryPoint = NULL;
657 PeimFileHandle = NULL;
658 EntryPoint = 0;
659
660 if ((Private->PeiMemoryInstalled) && (Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME)) {
661 //
662 // Once real memory is available, shadow the RegisterForShadow modules. And meanwhile
663 // update the modules' status from PEIM_STATE_REGISITER_FOR_SHADOW to PEIM_STATE_DONE.
664 //
665 SaveCurrentPeimCount = Private->CurrentPeimCount;
666 SaveCurrentFvCount = Private->CurrentPeimFvCount;
667 SaveCurrentFileHandle = Private->CurrentFileHandle;
668
669 for (Index1 = 0; Index1 <= SaveCurrentFvCount; Index1++) {
670 for (Index2 = 0; (Index2 < FixedPcdGet32 (PcdPeiCoreMaxPeimPerFv)) && (Private->Fv[Index1].FvFileHandles[Index2] != NULL); Index2++) {
671 if (Private->Fv[Index1].PeimState[Index2] == PEIM_STATE_REGISITER_FOR_SHADOW) {
672 PeimFileHandle = Private->Fv[Index1].FvFileHandles[Index2];
673 Status = PeiLoadImage (
674 (CONST EFI_PEI_SERVICES **) &Private->Ps,
675 PeimFileHandle,
676 PEIM_STATE_REGISITER_FOR_SHADOW,
677 &EntryPoint,
678 &AuthenticationState
679 );
680 if (Status == EFI_SUCCESS) {
681 //
682 // PEIM_STATE_REGISITER_FOR_SHADOW move to PEIM_STATE_DONE
683 //
684 Private->Fv[Index1].PeimState[Index2]++;
685 Private->CurrentFileHandle = PeimFileHandle;
686 Private->CurrentPeimFvCount = Index1;
687 Private->CurrentPeimCount = Index2;
688 //
689 // Call the PEIM entry point
690 //
691 PeimEntryPoint = (EFI_PEIM_ENTRY_POINT2)(UINTN)EntryPoint;
692
693 PERF_START (PeimFileHandle, "PEIM", NULL, 0);
694 PeimEntryPoint(PeimFileHandle, (const EFI_PEI_SERVICES **) &Private->Ps);
695 PERF_END (PeimFileHandle, "PEIM", NULL, 0);
696 }
697
698 //
699 // Process the Notify list and dispatch any notifies for
700 // newly installed PPIs.
701 //
702 ProcessNotifyList (Private);
703 }
704 }
705 }
706 Private->CurrentFileHandle = SaveCurrentFileHandle;
707 Private->CurrentPeimFvCount = SaveCurrentFvCount;
708 Private->CurrentPeimCount = SaveCurrentPeimCount;
709 }
710
711 //
712 // This is the main dispatch loop. It will search known FVs for PEIMs and
713 // attempt to dispatch them. If any PEIM gets dispatched through a single
714 // pass of the dispatcher, it will start over from the Bfv again to see
715 // if any new PEIMs dependencies got satisfied. With a well ordered
716 // FV where PEIMs are found in the order their dependencies are also
717 // satisfied, this dipatcher should run only once.
718 //
719 do {
720 //
721 // In case that reenter PeiCore happens, the last pass record is still available.
722 //
723 if (!Private->PeimDispatcherReenter) {
724 Private->PeimNeedingDispatch = FALSE;
725 Private->PeimDispatchOnThisPass = FALSE;
726 } else {
727 Private->PeimDispatcherReenter = FALSE;
728 }
729
730 for (FvCount = Private->CurrentPeimFvCount; FvCount < Private->FvCount; FvCount++) {
731 CoreFvHandle = FindNextCoreFvHandle (Private, FvCount);
732 ASSERT (CoreFvHandle != NULL);
733
734 //
735 // If the FV has corresponding EFI_PEI_FIRMWARE_VOLUME_PPI instance, then dispatch it.
736 //
737 if (CoreFvHandle->FvPpi == NULL) {
738 continue;
739 }
740
741 Private->CurrentPeimFvCount = FvCount;
742
743 if (Private->CurrentPeimCount == 0) {
744 //
745 // When going through each FV, at first, search Apriori file to
746 // reorder all PEIMs to ensure the PEIMs in Apriori file to get
747 // dispatch at first.
748 //
749 DiscoverPeimsAndOrderWithApriori (Private, CoreFvHandle);
750 }
751
752 //
753 // Start to dispatch all modules within the current Fv.
754 //
755 for (PeimCount = Private->CurrentPeimCount;
756 (PeimCount < FixedPcdGet32 (PcdPeiCoreMaxPeimPerFv)) && (Private->CurrentFvFileHandles[PeimCount] != NULL);
757 PeimCount++) {
758 Private->CurrentPeimCount = PeimCount;
759 PeimFileHandle = Private->CurrentFileHandle = Private->CurrentFvFileHandles[PeimCount];
760
761 if (Private->Fv[FvCount].PeimState[PeimCount] == PEIM_STATE_NOT_DISPATCHED) {
762 if (!DepexSatisfied (Private, PeimFileHandle, PeimCount)) {
763 Private->PeimNeedingDispatch = TRUE;
764 } else {
765 Status = CoreFvHandle->FvPpi->GetFileInfo (CoreFvHandle->FvPpi, PeimFileHandle, &FvFileInfo);
766 ASSERT_EFI_ERROR (Status);
767 if (FvFileInfo.FileType == EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE) {
768 //
769 // For Fv type file, Produce new FV PPI and FV hob
770 //
771 Status = ProcessFvFile (&Private->Fv[FvCount], PeimFileHandle);
772 AuthenticationState = 0;
773 } else {
774 //
775 // For PEIM driver, Load its entry point
776 //
777 Status = PeiLoadImage (
778 PeiServices,
779 PeimFileHandle,
780 PEIM_STATE_NOT_DISPATCHED,
781 &EntryPoint,
782 &AuthenticationState
783 );
784 }
785
786 if (Status == EFI_SUCCESS) {
787 //
788 // The PEIM has its dependencies satisfied, and its entry point
789 // has been found, so invoke it.
790 //
791 PERF_START (PeimFileHandle, "PEIM", NULL, 0);
792
793 ExtendedData.Handle = (EFI_HANDLE)PeimFileHandle;
794
795 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (
796 EFI_PROGRESS_CODE,
797 (EFI_SOFTWARE_PEI_CORE | EFI_SW_PC_INIT_BEGIN),
798 (VOID *)(&ExtendedData),
799 sizeof (ExtendedData)
800 );
801
802 Status = VerifyPeim (Private, CoreFvHandle->FvHandle, PeimFileHandle);
803 if (Status != EFI_SECURITY_VIOLATION && (AuthenticationState == 0)) {
804 //
805 // PEIM_STATE_NOT_DISPATCHED move to PEIM_STATE_DISPATCHED
806 //
807 Private->Fv[FvCount].PeimState[PeimCount]++;
808
809 if (FvFileInfo.FileType != EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE) {
810 //
811 // Call the PEIM entry point for PEIM driver
812 //
813 PeimEntryPoint = (EFI_PEIM_ENTRY_POINT2)(UINTN)EntryPoint;
814 PeimEntryPoint (PeimFileHandle, (const EFI_PEI_SERVICES **) PeiServices);
815 }
816
817 Private->PeimDispatchOnThisPass = TRUE;
818 }
819
820 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (
821 EFI_PROGRESS_CODE,
822 (EFI_SOFTWARE_PEI_CORE | EFI_SW_PC_INIT_END),
823 (VOID *)(&ExtendedData),
824 sizeof (ExtendedData)
825 );
826 PERF_END (PeimFileHandle, "PEIM", NULL, 0);
827
828 }
829
830 if (Private->SwitchStackSignal) {
831 //
832 // Before switch stack from temporary memory to permenent memory, caculate the heap and stack
833 // usage in temporary memory for debuging.
834 //
835 DEBUG_CODE_BEGIN ();
836 UINT32 *StackPointer;
837
838 for (StackPointer = (UINT32*)SecCoreData->StackBase;
839 (StackPointer < (UINT32*)((UINTN)SecCoreData->StackBase + SecCoreData->StackSize)) \
840 && (*StackPointer == INIT_CAR_VALUE);
841 StackPointer ++);
842
843 DEBUG ((EFI_D_INFO, "Temp Stack : BaseAddress=0x%p Length=0x%X\n", SecCoreData->StackBase, (UINT32)SecCoreData->StackSize));
844 DEBUG ((EFI_D_INFO, "Temp Heap : BaseAddress=0x%p Length=0x%X\n", Private->HobList.Raw, (UINT32)((UINTN) Private->HobList.HandoffInformationTable->EfiFreeMemoryBottom - (UINTN) Private->HobList.Raw)));
845 DEBUG ((EFI_D_INFO, "Total temporary memory: %d bytes.\n", (UINT32)SecCoreData->TemporaryRamSize));
846 DEBUG ((EFI_D_INFO, " temporary memory stack ever used: %d bytes.\n",
847 (UINT32)(SecCoreData->StackSize - ((UINTN) StackPointer - (UINTN)SecCoreData->StackBase))
848 ));
849 DEBUG ((EFI_D_INFO, " temporary memory heap used: %d bytes.\n",
850 (UINT32)((UINTN)Private->HobList.HandoffInformationTable->EfiFreeMemoryBottom - (UINTN)Private->HobList.Raw)
851 ));
852 DEBUG_CODE_END ();
853
854 if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0 && (Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME)) {
855 //
856 // Loading Module at Fixed Address is enabled
857 //
858 PeiLoadFixAddressHook (Private);
859
860 //
861 // If Loading Module at Fixed Address is enabled, Allocating memory range for Pei code range.
862 //
863 LoadFixPeiCodeBegin = AllocatePages((UINTN)PcdGet32(PcdLoadFixAddressPeiCodePageNumber));
864 DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO: PeiCodeBegin = 0x%lX, PeiCodeTop= 0x%lX\n", (UINT64)(UINTN)LoadFixPeiCodeBegin, (UINT64)((UINTN)LoadFixPeiCodeBegin + PcdGet32(PcdLoadFixAddressPeiCodePageNumber) * EFI_PAGE_SIZE)));
865 }
866
867 //
868 // Reserve the size of new stack at bottom of physical memory
869 //
870 // The size of new stack in permenent memory must be the same size
871 // or larger than the size of old stack in temporary memory.
872 // But if new stack is smaller than the size of old stack, we also reserve
873 // the size of old stack at bottom of permenent memory.
874 //
875 NewStackSize = RShiftU64 (Private->PhysicalMemoryLength, 1);
876 NewStackSize = ALIGN_VALUE (NewStackSize, EFI_PAGE_SIZE);
877 NewStackSize = MIN (PcdGet32(PcdPeiCoreMaxPeiStackSize), NewStackSize);
878 DEBUG ((EFI_D_INFO, "Old Stack size %d, New stack size %d\n", (UINT32)SecCoreData->StackSize, (UINT32)NewStackSize));
879 ASSERT (NewStackSize >= SecCoreData->StackSize);
880
881 //
882 // Caculate stack offset and heap offset between temporary memory and new permement
883 // memory seperately.
884 //
885 TopOfOldStack = (UINTN)SecCoreData->StackBase + SecCoreData->StackSize;
886 TopOfNewStack = Private->PhysicalMemoryBegin + NewStackSize;
887 if (TopOfNewStack >= (UINTN)SecCoreData->PeiTemporaryRamBase) {
888 Private->HeapOffsetPositive = TRUE;
889 Private->HeapOffset = (UINTN)(TopOfNewStack - (UINTN)SecCoreData->PeiTemporaryRamBase);
890 } else {
891 Private->HeapOffsetPositive = FALSE;
892 Private->HeapOffset = (UINTN)((UINTN)SecCoreData->PeiTemporaryRamBase - TopOfNewStack);
893 }
894 if (TopOfNewStack >= TopOfOldStack) {
895 StackOffsetPositive = TRUE;
896 StackOffset = (UINTN)(TopOfNewStack - TopOfOldStack);
897 } else {
898 StackOffsetPositive = FALSE;
899 StackOffset = (UINTN)(TopOfOldStack - TopOfNewStack);
900 }
901 Private->StackOffsetPositive = StackOffsetPositive;
902 Private->StackOffset = StackOffset;
903
904 DEBUG ((EFI_D_INFO, "Heap Offset = 0x%lX Stack Offset = 0x%lX\n", (UINT64)Private->HeapOffset, (UINT64)(StackOffset)));
905
906 //
907 // Build Stack HOB that describes the permanent memory stack
908 //
909 DEBUG ((EFI_D_INFO, "Stack Hob: BaseAddress=0x%lX Length=0x%lX\n", TopOfNewStack - NewStackSize, NewStackSize));
910 BuildStackHob (TopOfNewStack - NewStackSize, NewStackSize);
911
912 //
913 // Cache information from SecCoreData into locals before SecCoreData is converted to a permanent memory address
914 //
915 TemporaryRamBase = (EFI_PHYSICAL_ADDRESS)(UINTN)SecCoreData->TemporaryRamBase;
916 TemporaryRamSize = SecCoreData->TemporaryRamSize;
917 TemporaryStackSize = SecCoreData->StackSize;
918
919 //
920 // Caculate new HandOffTable and PrivateData address in permanent memory's stack
921 //
922 if (StackOffsetPositive) {
923 SecCoreData = (CONST EFI_SEC_PEI_HAND_OFF *)((UINTN)(VOID *)SecCoreData + StackOffset);
924 Private = (PEI_CORE_INSTANCE *)((UINTN)(VOID *)Private + StackOffset);
925 } else {
926 SecCoreData = (CONST EFI_SEC_PEI_HAND_OFF *)((UINTN)(VOID *)SecCoreData - StackOffset);
927 Private = (PEI_CORE_INSTANCE *)((UINTN)(VOID *)Private - StackOffset);
928 }
929
930 //
931 // TemporaryRamSupportPpi is produced by platform's SEC
932 //
933 Status = PeiServicesLocatePpi (
934 &gEfiTemporaryRamSupportPpiGuid,
935 0,
936 NULL,
937 (VOID**)&TemporaryRamSupportPpi
938 );
939 if (!EFI_ERROR (Status)) {
940 //
941 // Temporary Ram Support PPI is provided by platform, it will copy
942 // temporary memory to permenent memory and do stack switching.
943 // After invoking Temporary Ram Support PPI, the following code's
944 // stack is in permanent memory.
945 //
946 TemporaryRamSupportPpi->TemporaryRamMigration (
947 PeiServices,
948 TemporaryRamBase,
949 (EFI_PHYSICAL_ADDRESS)(UINTN)(TopOfNewStack - TemporaryStackSize),
950 TemporaryRamSize
951 );
952
953 } else {
954 //
955 // In IA32/x64/Itanium architecture, we need platform provide
956 // TEMPORARY_RAM_MIGRATION_PPI.
957 //
958 ASSERT (FALSE);
959 }
960
961 //
962 // Entry PEI Phase 2
963 //
964 PeiCore (SecCoreData, NULL, Private);
965
966 //
967 // Code should not come here
968 //
969 ASSERT (FALSE);
970 }
971
972 //
973 // Process the Notify list and dispatch any notifies for
974 // newly installed PPIs.
975 //
976 ProcessNotifyList (Private);
977
978 if ((Private->PeiMemoryInstalled) && (Private->Fv[FvCount].PeimState[PeimCount] == PEIM_STATE_REGISITER_FOR_SHADOW) && \
979 (Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME)) {
980 //
981 // If memory is availble we shadow images by default for performance reasons.
982 // We call the entry point a 2nd time so the module knows it's shadowed.
983 //
984 //PERF_START (PeiServices, L"PEIM", PeimFileHandle, 0);
985 ASSERT (PeimEntryPoint != NULL);
986 PeimEntryPoint (PeimFileHandle, (const EFI_PEI_SERVICES **) PeiServices);
987 //PERF_END (PeiServices, L"PEIM", PeimFileHandle, 0);
988
989 //
990 // PEIM_STATE_REGISITER_FOR_SHADOW move to PEIM_STATE_DONE
991 //
992 Private->Fv[FvCount].PeimState[PeimCount]++;
993
994 //
995 // Process the Notify list and dispatch any notifies for
996 // newly installed PPIs.
997 //
998 ProcessNotifyList (Private);
999 }
1000 }
1001 }
1002 }
1003
1004 //
1005 // We set to NULL here to optimize the 2nd entry to this routine after
1006 // memory is found. This reprevents rescanning of the FV. We set to
1007 // NULL here so we start at the begining of the next FV
1008 //
1009 Private->CurrentFileHandle = NULL;
1010 Private->CurrentPeimCount = 0;
1011 //
1012 // Before walking through the next FV,Private->CurrentFvFileHandles[]should set to NULL
1013 //
1014 SetMem (Private->CurrentFvFileHandles, sizeof (Private->CurrentFvFileHandles), 0);
1015 }
1016
1017 //
1018 // Before making another pass, we should set Private->CurrentPeimFvCount =0 to go
1019 // through all the FV.
1020 //
1021 Private->CurrentPeimFvCount = 0;
1022
1023 //
1024 // PeimNeedingDispatch being TRUE means we found a PEIM that did not get
1025 // dispatched. So we need to make another pass
1026 //
1027 // PeimDispatchOnThisPass being TRUE means we dispatched a PEIM on this
1028 // pass. If we did not dispatch a PEIM there is no point in trying again
1029 // as it will fail the next time too (nothing has changed).
1030 //
1031 } while (Private->PeimNeedingDispatch && Private->PeimDispatchOnThisPass);
1032
1033 }
1034
1035 /**
1036 Initialize the Dispatcher's data members
1037
1038 @param PrivateData PeiCore's private data structure
1039 @param OldCoreData Old data from SecCore
1040 NULL if being run in non-permament memory mode.
1041 @param SecCoreData Points to a data structure containing information about the PEI core's operating
1042 environment, such as the size and location of temporary RAM, the stack location and
1043 the BFV location.
1044
1045 @return None.
1046
1047 **/
1048 VOID
1049 InitializeDispatcherData (
1050 IN PEI_CORE_INSTANCE *PrivateData,
1051 IN PEI_CORE_INSTANCE *OldCoreData,
1052 IN CONST EFI_SEC_PEI_HAND_OFF *SecCoreData
1053 )
1054 {
1055 if (OldCoreData == NULL) {
1056 PrivateData->PeimDispatcherReenter = FALSE;
1057 PeiInitializeFv (PrivateData, SecCoreData);
1058 } else {
1059 PeiReinitializeFv (PrivateData);
1060 }
1061
1062 return;
1063 }
1064
1065 /**
1066 This routine parses the Dependency Expression, if available, and
1067 decides if the module can be executed.
1068
1069
1070 @param Private PeiCore's private data structure
1071 @param FileHandle PEIM's file handle
1072 @param PeimCount Peim count in all dispatched PEIMs.
1073
1074 @retval TRUE Can be dispatched
1075 @retval FALSE Cannot be dispatched
1076
1077 **/
1078 BOOLEAN
1079 DepexSatisfied (
1080 IN PEI_CORE_INSTANCE *Private,
1081 IN EFI_PEI_FILE_HANDLE FileHandle,
1082 IN UINTN PeimCount
1083 )
1084 {
1085 EFI_STATUS Status;
1086 VOID *DepexData;
1087 EFI_FV_FILE_INFO FileInfo;
1088
1089 Status = PeiServicesFfsGetFileInfo (FileHandle, &FileInfo);
1090 if (EFI_ERROR (Status)) {
1091 DEBUG ((DEBUG_DISPATCH, "Evaluate PEI DEPEX for FFS(Unknown)\n"));
1092 } else {
1093 DEBUG ((DEBUG_DISPATCH, "Evaluate PEI DEPEX for FFS(%g)\n", &FileInfo.FileName));
1094 }
1095
1096 if (PeimCount < Private->AprioriCount) {
1097 //
1098 // If its in the A priori file then we set Depex to TRUE
1099 //
1100 DEBUG ((DEBUG_DISPATCH, " RESULT = TRUE (Apriori)\n"));
1101 return TRUE;
1102 }
1103
1104 //
1105 // Depex section not in the encapsulated section.
1106 //
1107 Status = PeiServicesFfsFindSectionData (
1108 EFI_SECTION_PEI_DEPEX,
1109 FileHandle,
1110 (VOID **)&DepexData
1111 );
1112
1113 if (EFI_ERROR (Status)) {
1114 //
1115 // If there is no DEPEX, assume the module can be executed
1116 //
1117 DEBUG ((DEBUG_DISPATCH, " RESULT = TRUE (No DEPEX)\n"));
1118 return TRUE;
1119 }
1120
1121 //
1122 // Evaluate a given DEPEX
1123 //
1124 return PeimDispatchReadiness (&Private->Ps, DepexData);
1125 }
1126
1127 /**
1128 This routine enable a PEIM to register itself to shadow when PEI Foundation
1129 discovery permanent memory.
1130
1131 @param FileHandle File handle of a PEIM.
1132
1133 @retval EFI_NOT_FOUND The file handle doesn't point to PEIM itself.
1134 @retval EFI_ALREADY_STARTED Indicate that the PEIM has been registered itself.
1135 @retval EFI_SUCCESS Successfully to register itself.
1136
1137 **/
1138 EFI_STATUS
1139 EFIAPI
1140 PeiRegisterForShadow (
1141 IN EFI_PEI_FILE_HANDLE FileHandle
1142 )
1143 {
1144 PEI_CORE_INSTANCE *Private;
1145 Private = PEI_CORE_INSTANCE_FROM_PS_THIS (GetPeiServicesTablePointer ());
1146
1147 if (Private->CurrentFileHandle != FileHandle) {
1148 //
1149 // The FileHandle must be for the current PEIM
1150 //
1151 return EFI_NOT_FOUND;
1152 }
1153
1154 if (Private->Fv[Private->CurrentPeimFvCount].PeimState[Private->CurrentPeimCount] >= PEIM_STATE_REGISITER_FOR_SHADOW) {
1155 //
1156 // If the PEIM has already entered the PEIM_STATE_REGISTER_FOR_SHADOW or PEIM_STATE_DONE then it's already been started
1157 //
1158 return EFI_ALREADY_STARTED;
1159 }
1160
1161 Private->Fv[Private->CurrentPeimFvCount].PeimState[Private->CurrentPeimCount] = PEIM_STATE_REGISITER_FOR_SHADOW;
1162
1163 return EFI_SUCCESS;
1164 }
1165
1166
1167