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