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