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