]>
Commit | Line | Data |
---|---|---|
615c6dd0 | 1 | /** @file\r |
b1f6a7c6 | 2 | Pei Core Load Image Support\r |
54ea99a7 | 3 | \r |
63c677e2 | 4 | Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r |
cd5ebaa0 | 5 | This program and the accompanying materials\r |
54ea99a7 | 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 | |
192f6d4c | 12 | \r |
615c6dd0 | 13 | **/\r |
192f6d4c | 14 | \r |
0d516397 | 15 | #include "PeiMain.h"\r |
192f6d4c | 16 | \r |
b0d803fe | 17 | \r |
fe1e36e5 | 18 | EFI_PEI_LOAD_FILE_PPI mPeiLoadImagePpi = {\r |
b0d803fe | 19 | PeiLoadImageLoadImageWrapper\r |
20 | };\r | |
21 | \r | |
22 | \r | |
fe1e36e5 | 23 | EFI_PEI_PPI_DESCRIPTOR gPpiLoadFilePpiList = {\r |
b0d803fe | 24 | (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),\r |
25 | &gEfiPeiLoadFilePpiGuid,\r | |
26 | &mPeiLoadImagePpi\r | |
27 | };\r | |
28 | \r | |
b1f6a7c6 | 29 | /**\r |
30 | \r | |
25973fc3 | 31 | Support routine for the PE/COFF Loader that reads a buffer from a PE/COFF file.\r |
32 | The function is used for XIP code to have optimized memory copy.\r | |
b1f6a7c6 | 33 | \r |
34 | @param FileHandle - The handle to the PE/COFF file\r | |
35 | @param FileOffset - The offset, in bytes, into the file to read\r | |
36 | @param ReadSize - The number of bytes to read from the file starting at FileOffset\r | |
37 | @param Buffer - A pointer to the buffer to read the data into.\r | |
38 | \r | |
39 | @return EFI_SUCCESS - ReadSize bytes of data were read into Buffer from the PE/COFF file starting at FileOffset\r | |
40 | \r | |
41 | **/\r | |
b0d803fe | 42 | EFI_STATUS\r |
43 | EFIAPI\r | |
44 | PeiImageRead (\r | |
45 | IN VOID *FileHandle,\r | |
46 | IN UINTN FileOffset,\r | |
ed299e3c | 47 | IN UINTN *ReadSize,\r |
b0d803fe | 48 | OUT VOID *Buffer\r |
49 | )\r | |
b0d803fe | 50 | {\r |
a9bfd802 LG |
51 | CHAR8 *Destination8;\r |
52 | CHAR8 *Source8;\r | |
25973fc3 | 53 | \r |
54 | Destination8 = Buffer;\r | |
55 | Source8 = (CHAR8 *) ((UINTN) FileHandle + FileOffset);\r | |
56 | if (Destination8 != Source8) {\r | |
57 | CopyMem (Destination8, Source8, *ReadSize);\r | |
58 | }\r | |
59 | \r | |
60 | return EFI_SUCCESS;\r | |
61 | }\r | |
62 | \r | |
63 | /**\r | |
64 | \r | |
65 | Support routine for the PE/COFF Loader that reads a buffer from a PE/COFF file.\r | |
66 | The function is implemented as PIC so as to support shadowing.\r | |
67 | \r | |
68 | @param FileHandle - The handle to the PE/COFF file\r | |
69 | @param FileOffset - The offset, in bytes, into the file to read\r | |
70 | @param ReadSize - The number of bytes to read from the file starting at FileOffset\r | |
71 | @param Buffer - A pointer to the buffer to read the data into.\r | |
72 | \r | |
73 | @return EFI_SUCCESS - ReadSize bytes of data were read into Buffer from the PE/COFF file starting at FileOffset\r | |
74 | \r | |
75 | **/\r | |
76 | EFI_STATUS\r | |
77 | EFIAPI\r | |
78 | PeiImageReadForShadow (\r | |
79 | IN VOID *FileHandle,\r | |
80 | IN UINTN FileOffset,\r | |
81 | IN UINTN *ReadSize,\r | |
82 | OUT VOID *Buffer\r | |
83 | )\r | |
84 | {\r | |
4da30589 | 85 | volatile CHAR8 *Destination8;\r |
86 | CHAR8 *Source8;\r | |
87 | UINTN Length;\r | |
a9bfd802 LG |
88 | \r |
89 | Destination8 = Buffer;\r | |
90 | Source8 = (CHAR8 *) ((UINTN) FileHandle + FileOffset);\r | |
43ada17c | 91 | if (Destination8 != Source8) {\r |
92 | Length = *ReadSize;\r | |
93 | while ((Length--) > 0) {\r | |
94 | *(Destination8++) = *(Source8++);\r | |
95 | }\r | |
a9bfd802 LG |
96 | }\r |
97 | \r | |
b0d803fe | 98 | return EFI_SUCCESS;\r |
99 | }\r | |
100 | \r | |
b1f6a7c6 | 101 | /**\r |
b0d803fe | 102 | \r |
ed299e3c | 103 | Support routine to get the Image read file function.\r |
b0d803fe | 104 | \r |
b1f6a7c6 | 105 | @param ImageContext - The context of the image being loaded\r |
b0d803fe | 106 | \r |
b1f6a7c6 | 107 | @retval EFI_SUCCESS - If Image function location is found\r |
b0d803fe | 108 | \r |
b1f6a7c6 | 109 | **/\r |
110 | EFI_STATUS\r | |
111 | GetImageReadFunction (\r | |
112 | IN PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r | |
113 | )\r | |
b0d803fe | 114 | {\r |
6e7ec25a | 115 | #if defined (MDE_CPU_IA32) || defined (MDE_CPU_X64)\r |
a0ffd7a9 AB |
116 | PEI_CORE_INSTANCE *Private;\r |
117 | EFI_PHYSICAL_ADDRESS MemoryBuffer;\r | |
b0d803fe | 118 | \r |
43ada17c | 119 | Private = PEI_CORE_INSTANCE_FROM_PS_THIS (GetPeiServicesTablePointer ());\r |
a0ffd7a9 AB |
120 | MemoryBuffer = 0;\r |
121 | \r | |
3d44658c | 122 | if (Private->PeiMemoryInstalled && (((Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME) && PcdGetBool (PcdShadowPeimOnBoot)) || \r |
6e7ec25a | 123 | ((Private->HobList.HandoffInformationTable->BootMode == BOOT_ON_S3_RESUME) && PcdGetBool (PcdShadowPeimOnS3Boot)))) {\r |
1c8029e3 | 124 | // \r |
125 | // Shadow algorithm makes lots of non ANSI C assumptions and only works for IA32 and X64 \r | |
126 | // compilers that have been tested\r | |
27172c0f | 127 | //\r |
25973fc3 | 128 | if (Private->ShadowedImageRead == NULL) {\r |
a0ffd7a9 AB |
129 | PeiServicesAllocatePages (EfiBootServicesCode, 0x400 / EFI_PAGE_SIZE + 1, &MemoryBuffer);\r |
130 | ASSERT (MemoryBuffer != 0);\r | |
131 | CopyMem ((VOID *)(UINTN)MemoryBuffer, (CONST VOID *) (UINTN) PeiImageReadForShadow, 0x400);\r | |
25973fc3 | 132 | Private->ShadowedImageRead = (PE_COFF_LOADER_READ_FILE) (UINTN) MemoryBuffer;\r |
133 | }\r | |
43ada17c | 134 | \r |
25973fc3 | 135 | ImageContext->ImageRead = Private->ShadowedImageRead;\r |
1c8029e3 | 136 | } else {\r |
137 | ImageContext->ImageRead = PeiImageRead;\r | |
43ada17c | 138 | }\r |
6e7ec25a AB |
139 | #else\r |
140 | ImageContext->ImageRead = PeiImageRead;\r | |
141 | #endif\r | |
b0d803fe | 142 | return EFI_SUCCESS;\r |
143 | }\r | |
54ea99a7 | 144 | /**\r |
2048c585 GM |
145 | To check memory usage bit map array to figure out if the memory range the image will be loaded in is available or not. If\r |
146 | memory range is available, the function will mark the corresponding bits to 1 which indicates the memory range is used.\r | |
54ea99a7 | 147 | The function is only invoked when load modules at fixed address feature is enabled. \r |
148 | \r | |
149 | @param Private Pointer to the private data passed in from caller\r | |
2048c585 | 150 | @param ImageBase The base address the image will be loaded at.\r |
54ea99a7 | 151 | @param ImageSize The size of the image\r |
152 | \r | |
153 | @retval EFI_SUCCESS The memory range the image will be loaded in is available\r | |
154 | @retval EFI_NOT_FOUND The memory range the image will be loaded in is not available\r | |
155 | **/\r | |
156 | EFI_STATUS\r | |
157 | CheckAndMarkFixLoadingMemoryUsageBitMap (\r | |
158 | IN PEI_CORE_INSTANCE *Private,\r | |
159 | IN EFI_PHYSICAL_ADDRESS ImageBase,\r | |
160 | IN UINT32 ImageSize\r | |
161 | )\r | |
162 | {\r | |
163 | UINT32 DxeCodePageNumber;\r | |
164 | UINT64 ReservedCodeSize;\r | |
165 | EFI_PHYSICAL_ADDRESS PeiCodeBase;\r | |
166 | UINT32 BaseOffsetPageNumber;\r | |
167 | UINT32 TopOffsetPageNumber;\r | |
168 | UINT32 Index;\r | |
169 | UINT64 *MemoryUsageBitMap;\r | |
170 | \r | |
171 | \r | |
172 | //\r | |
173 | // The reserved code range includes RuntimeCodePage range, Boot time code range and PEI code range.\r | |
174 | //\r | |
175 | DxeCodePageNumber = PcdGet32(PcdLoadFixAddressBootTimeCodePageNumber);\r | |
176 | DxeCodePageNumber += PcdGet32(PcdLoadFixAddressRuntimeCodePageNumber);\r | |
177 | ReservedCodeSize = EFI_PAGES_TO_SIZE(DxeCodePageNumber + PcdGet32(PcdLoadFixAddressPeiCodePageNumber));\r | |
178 | PeiCodeBase = Private->LoadModuleAtFixAddressTopAddress - ReservedCodeSize;\r | |
179 | \r | |
180 | //\r | |
181 | // Test the memory range for loading the image in the PEI code range.\r | |
182 | //\r | |
183 | if ((Private->LoadModuleAtFixAddressTopAddress - EFI_PAGES_TO_SIZE(DxeCodePageNumber)) < (ImageBase + ImageSize) ||\r | |
184 | (PeiCodeBase > ImageBase)) { \r | |
185 | return EFI_NOT_FOUND; \r | |
186 | }\r | |
187 | \r | |
188 | //\r | |
189 | // Test if the memory is avalaible or not.\r | |
190 | //\r | |
191 | MemoryUsageBitMap = Private->PeiCodeMemoryRangeUsageBitMap; \r | |
192 | BaseOffsetPageNumber = EFI_SIZE_TO_PAGES((UINT32)(ImageBase - PeiCodeBase));\r | |
193 | TopOffsetPageNumber = EFI_SIZE_TO_PAGES((UINT32)(ImageBase + ImageSize - PeiCodeBase));\r | |
194 | for (Index = BaseOffsetPageNumber; Index < TopOffsetPageNumber; Index ++) {\r | |
195 | if ((MemoryUsageBitMap[Index / 64] & LShiftU64(1, (Index % 64))) != 0) {\r | |
196 | //\r | |
197 | // This page is already used.\r | |
198 | //\r | |
199 | return EFI_NOT_FOUND; \r | |
200 | }\r | |
201 | }\r | |
202 | \r | |
203 | //\r | |
204 | // Being here means the memory range is available. So mark the bits for the memory range\r | |
205 | // \r | |
206 | for (Index = BaseOffsetPageNumber; Index < TopOffsetPageNumber; Index ++) {\r | |
207 | MemoryUsageBitMap[Index / 64] |= LShiftU64(1, (Index % 64));\r | |
208 | }\r | |
209 | return EFI_SUCCESS; \r | |
210 | }\r | |
211 | /**\r | |
212 | \r | |
2048c585 | 213 | Get the fixed loading address from image header assigned by build tool. This function only be called\r |
54ea99a7 | 214 | when Loading module at Fixed address feature enabled.\r |
215 | \r | |
216 | @param ImageContext Pointer to the image context structure that describes the PE/COFF\r | |
217 | image that needs to be examined by this function.\r | |
218 | @param Private Pointer to the private data passed in from caller\r | |
219 | \r | |
220 | @retval EFI_SUCCESS An fixed loading address is assigned to this image by build tools .\r | |
2048c585 | 221 | @retval EFI_NOT_FOUND The image has no assigned fixed loading address.\r |
b0d803fe | 222 | \r |
54ea99a7 | 223 | **/\r |
224 | EFI_STATUS\r | |
225 | GetPeCoffImageFixLoadingAssignedAddress(\r | |
226 | IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext,\r | |
227 | IN PEI_CORE_INSTANCE *Private\r | |
228 | )\r | |
229 | {\r | |
230 | UINTN SectionHeaderOffset;\r | |
231 | EFI_STATUS Status;\r | |
232 | EFI_IMAGE_SECTION_HEADER SectionHeader;\r | |
233 | EFI_IMAGE_OPTIONAL_HEADER_UNION *ImgHdr;\r | |
2048c585 | 234 | EFI_PHYSICAL_ADDRESS FixLoadingAddress;\r |
54ea99a7 | 235 | UINT16 Index;\r |
236 | UINTN Size;\r | |
237 | UINT16 NumberOfSections;\r | |
238 | UINT64 ValueInSectionHeader;\r | |
239 | \r | |
240 | \r | |
2048c585 | 241 | FixLoadingAddress = 0;\r |
54ea99a7 | 242 | Status = EFI_NOT_FOUND;\r |
243 | \r | |
244 | //\r | |
245 | // Get PeHeader pointer\r | |
246 | //\r | |
247 | ImgHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)((CHAR8* )ImageContext->Handle + ImageContext->PeCoffHeaderOffset);\r | |
248 | if (ImageContext->IsTeImage) {\r | |
249 | //\r | |
2048c585 | 250 | // for TE image, the fix loading address is saved in first section header that doesn't point\r |
54ea99a7 | 251 | // to code section.\r |
252 | //\r | |
253 | SectionHeaderOffset = sizeof (EFI_TE_IMAGE_HEADER);\r | |
254 | NumberOfSections = ImgHdr->Te.NumberOfSections;\r | |
255 | } else {\r | |
16f69227 HW |
256 | SectionHeaderOffset = ImageContext->PeCoffHeaderOffset +\r |
257 | sizeof (UINT32) +\r | |
258 | sizeof (EFI_IMAGE_FILE_HEADER) +\r | |
259 | ImgHdr->Pe32.FileHeader.SizeOfOptionalHeader;\r | |
54ea99a7 | 260 | NumberOfSections = ImgHdr->Pe32.FileHeader.NumberOfSections;\r |
261 | }\r | |
262 | //\r | |
263 | // Get base address from the first section header that doesn't point to code section.\r | |
264 | //\r | |
265 | for (Index = 0; Index < NumberOfSections; Index++) {\r | |
266 | //\r | |
267 | // Read section header from file\r | |
268 | //\r | |
269 | Size = sizeof (EFI_IMAGE_SECTION_HEADER);\r | |
270 | Status = ImageContext->ImageRead (\r | |
271 | ImageContext->Handle,\r | |
272 | SectionHeaderOffset,\r | |
273 | &Size,\r | |
274 | &SectionHeader\r | |
275 | );\r | |
276 | if (EFI_ERROR (Status)) {\r | |
277 | return Status;\r | |
278 | }\r | |
279 | \r | |
280 | Status = EFI_NOT_FOUND;\r | |
281 | \r | |
282 | if ((SectionHeader.Characteristics & EFI_IMAGE_SCN_CNT_CODE) == 0) {\r | |
283 | //\r | |
284 | // Build tool will save the address in PointerToRelocations & PointerToLineNumbers fields in the first section header\r | |
285 | // that doesn't point to code section in image header, as well as ImageBase field of image header. A notable thing is\r | |
286 | // that for PEIM, the value in ImageBase field may not be equal to the value in PointerToRelocations & PointerToLineNumbers because\r | |
287 | // for XIP PEIM, ImageBase field holds the image base address running on the Flash. And PointerToRelocations & PointerToLineNumbers\r | |
288 | // hold the image base address when it is shadow to the memory. And there is an assumption that when the feature is enabled, if a\r | |
289 | // module is assigned a loading address by tools, PointerToRelocations & PointerToLineNumbers fields should NOT be Zero, or\r | |
2048c585 | 290 | // else, these 2 fields should be set to Zero\r |
54ea99a7 | 291 | //\r |
292 | ValueInSectionHeader = ReadUnaligned64((UINT64*)&SectionHeader.PointerToRelocations);\r | |
293 | if (ValueInSectionHeader != 0) {\r | |
294 | //\r | |
295 | // Found first section header that doesn't point to code section.\r | |
296 | //\r | |
852081fc | 297 | if ((INT64)PcdGet64(PcdLoadModuleAtFixAddressEnable) > 0) {\r |
54ea99a7 | 298 | //\r |
299 | // When LMFA feature is configured as Load Module at Fixed Absolute Address mode, PointerToRelocations & PointerToLineNumbers field\r | |
2048c585 | 300 | // hold the absolute address of image base running in memory\r |
54ea99a7 | 301 | //\r |
2048c585 | 302 | FixLoadingAddress = ValueInSectionHeader;\r |
54ea99a7 | 303 | } else {\r |
304 | //\r | |
305 | // When LMFA feature is configured as Load Module at Fixed offset mode, PointerToRelocations & PointerToLineNumbers field\r | |
306 | // hold the offset relative to a platform-specific top address.\r | |
307 | //\r | |
2048c585 | 308 | FixLoadingAddress = (EFI_PHYSICAL_ADDRESS)(Private->LoadModuleAtFixAddressTopAddress + (INT64)ValueInSectionHeader);\r |
54ea99a7 | 309 | }\r |
310 | //\r | |
2048c585 | 311 | // Check if the memory range is available.\r |
54ea99a7 | 312 | //\r |
2048c585 | 313 | Status = CheckAndMarkFixLoadingMemoryUsageBitMap (Private, FixLoadingAddress, (UINT32) ImageContext->ImageSize);\r |
54ea99a7 | 314 | if (!EFI_ERROR(Status)) {\r |
315 | //\r | |
2048c585 | 316 | // The assigned address is valid. Return the specified loading address\r |
54ea99a7 | 317 | //\r |
2048c585 | 318 | ImageContext->ImageAddress = FixLoadingAddress;\r |
54ea99a7 | 319 | }\r |
320 | }\r | |
321 | break;\r | |
322 | }\r | |
323 | SectionHeaderOffset += sizeof (EFI_IMAGE_SECTION_HEADER);\r | |
324 | }\r | |
2048c585 | 325 | DEBUG ((EFI_D_INFO|EFI_D_LOAD, "LOADING MODULE FIXED INFO: Loading module at fixed address 0x%11p. Status= %r \n", (VOID *)(UINTN)FixLoadingAddress, Status));\r |
54ea99a7 | 326 | return Status;\r |
327 | }\r | |
b1f6a7c6 | 328 | /**\r |
329 | \r | |
330 | Loads and relocates a PE/COFF image into memory.\r | |
341a658f | 331 | If the image is not relocatable, it will not be loaded into memory and be loaded as XIP image.\r |
b1f6a7c6 | 332 | \r |
3d44658c | 333 | @param FileHandle - Pointer to the FFS file header of the image.\r |
b1f6a7c6 | 334 | @param Pe32Data - The base address of the PE/COFF file that is to be loaded and relocated\r |
335 | @param ImageAddress - The base address of the relocated PE/COFF image\r | |
336 | @param ImageSize - The size of the relocated PE/COFF image\r | |
337 | @param EntryPoint - The entry point of the relocated PE/COFF image\r | |
338 | \r | |
339 | @retval EFI_SUCCESS The file was loaded and relocated\r | |
340 | @retval EFI_OUT_OF_RESOURCES There was not enough memory to load and relocate the PE/COFF file\r | |
5d7f3126 LG |
341 | @retval EFI_WARN_BUFFER_TOO_SMALL \r |
342 | There is not enough heap to allocate the requested size.\r | |
343 | This will not prevent the XIP image from being invoked.\r | |
b1f6a7c6 | 344 | \r |
345 | **/\r | |
b0d803fe | 346 | EFI_STATUS\r |
347 | LoadAndRelocatePeCoffImage (\r | |
3d44658c | 348 | IN EFI_PEI_FILE_HANDLE FileHandle,\r |
b0d803fe | 349 | IN VOID *Pe32Data,\r |
350 | OUT EFI_PHYSICAL_ADDRESS *ImageAddress,\r | |
351 | OUT UINT64 *ImageSize,\r | |
352 | OUT EFI_PHYSICAL_ADDRESS *EntryPoint\r | |
353 | )\r | |
b0d803fe | 354 | {\r |
355 | EFI_STATUS Status;\r | |
356 | PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;\r | |
43ada17c | 357 | PEI_CORE_INSTANCE *Private;\r |
935efc21 | 358 | UINT64 AlignImageSize;\r |
5d7f3126 LG |
359 | BOOLEAN IsXipImage;\r |
360 | EFI_STATUS ReturnStatus;\r | |
3d44658c | 361 | BOOLEAN IsS3Boot;\r |
609730ef | 362 | BOOLEAN IsPeiModule;\r |
3d44658c | 363 | BOOLEAN IsRegisterForShadow;\r |
609730ef | 364 | EFI_FV_FILE_INFO FileInfo;\r |
43ada17c | 365 | \r |
366 | Private = PEI_CORE_INSTANCE_FROM_PS_THIS (GetPeiServicesTablePointer ());\r | |
b0d803fe | 367 | \r |
5d7f3126 LG |
368 | ReturnStatus = EFI_SUCCESS;\r |
369 | IsXipImage = FALSE;\r | |
b0d803fe | 370 | ZeroMem (&ImageContext, sizeof (ImageContext));\r |
371 | ImageContext.Handle = Pe32Data;\r | |
372 | Status = GetImageReadFunction (&ImageContext);\r | |
373 | \r | |
374 | ASSERT_EFI_ERROR (Status);\r | |
375 | \r | |
3d7b0992 | 376 | Status = PeCoffLoaderGetImageInfo (&ImageContext);\r |
b0d803fe | 377 | if (EFI_ERROR (Status)) {\r |
378 | return Status;\r | |
379 | }\r | |
5d7f3126 | 380 | \r |
3d44658c LG |
381 | //\r |
382 | // Initilize local IsS3Boot and IsRegisterForShadow variable\r | |
383 | //\r | |
384 | IsS3Boot = FALSE;\r | |
385 | if (Private->HobList.HandoffInformationTable->BootMode == BOOT_ON_S3_RESUME) {\r | |
386 | IsS3Boot = TRUE;\r | |
387 | }\r | |
388 | IsRegisterForShadow = FALSE;\r | |
389 | if ((Private->CurrentFileHandle == FileHandle) \r | |
c2c4199b | 390 | && (Private->Fv[Private->CurrentPeimFvCount].PeimState[Private->CurrentPeimCount] == PEIM_STATE_REGISTER_FOR_SHADOW)) {\r |
3d44658c LG |
391 | IsRegisterForShadow = TRUE;\r |
392 | }\r | |
393 | \r | |
5d7f3126 LG |
394 | //\r |
395 | // XIP image that ImageAddress is same to Image handle.\r | |
396 | //\r | |
397 | if (ImageContext.ImageAddress == (EFI_PHYSICAL_ADDRESS)(UINTN) Pe32Data) {\r | |
398 | IsXipImage = TRUE;\r | |
399 | }\r | |
400 | \r | |
609730ef LG |
401 | //\r |
402 | // Get file type first\r | |
403 | //\r | |
404 | Status = PeiServicesFfsGetFileInfo (FileHandle, &FileInfo);\r | |
405 | ASSERT_EFI_ERROR (Status);\r | |
406 | \r | |
407 | //\r | |
408 | // Check whether the file type is PEI module.\r | |
409 | //\r | |
410 | IsPeiModule = FALSE;\r | |
411 | if (FileInfo.FileType == EFI_FV_FILETYPE_PEI_CORE || \r | |
412 | FileInfo.FileType == EFI_FV_FILETYPE_PEIM || \r | |
413 | FileInfo.FileType == EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER) {\r | |
414 | IsPeiModule = TRUE;\r | |
415 | }\r | |
416 | \r | |
b0d803fe | 417 | //\r |
9626a87e LG |
418 | // When Image has no reloc section, it can't be relocated into memory.\r |
419 | //\r | |
609730ef | 420 | if (ImageContext.RelocationsStripped && (Private->PeiMemoryInstalled) && ((!IsPeiModule) ||\r |
3d44658c | 421 | (!IsS3Boot && (PcdGetBool (PcdShadowPeimOnBoot) || IsRegisterForShadow)) || (IsS3Boot && PcdGetBool (PcdShadowPeimOnS3Boot)))) {\r |
54ea99a7 | 422 | DEBUG ((EFI_D_INFO|EFI_D_LOAD, "The image at 0x%08x without reloc section can't be loaded into memory\n", (UINTN) Pe32Data));\r |
9626a87e | 423 | }\r |
414bdfb6 | 424 | \r |
341a658f LG |
425 | //\r |
426 | // Set default base address to current image address.\r | |
427 | //\r | |
428 | ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)(UINTN) Pe32Data;\r | |
54ea99a7 | 429 | \r |
9626a87e | 430 | //\r |
3d44658c LG |
431 | // Allocate Memory for the image when memory is ready, and image is relocatable.\r |
432 | // On normal boot, PcdShadowPeimOnBoot decides whether load PEIM or PeiCore into memory.\r | |
433 | // On S3 boot, PcdShadowPeimOnS3Boot decides whether load PEIM or PeiCore into memory.\r | |
b0d803fe | 434 | //\r |
609730ef | 435 | if ((!ImageContext.RelocationsStripped) && (Private->PeiMemoryInstalled) && ((!IsPeiModule) ||\r |
3d44658c | 436 | (!IsS3Boot && (PcdGetBool (PcdShadowPeimOnBoot) || IsRegisterForShadow)) || (IsS3Boot && PcdGetBool (PcdShadowPeimOnS3Boot)))) {\r |
935efc21 ED |
437 | //\r |
438 | // Allocate more buffer to avoid buffer overflow.\r | |
439 | //\r | |
440 | if (ImageContext.IsTeImage) {\r | |
441 | AlignImageSize = ImageContext.ImageSize + ((EFI_TE_IMAGE_HEADER *) Pe32Data)->StrippedSize - sizeof (EFI_TE_IMAGE_HEADER);\r | |
442 | } else {\r | |
443 | AlignImageSize = ImageContext.ImageSize;\r | |
444 | }\r | |
445 | \r | |
446 | if (ImageContext.SectionAlignment > EFI_PAGE_SIZE) {\r | |
447 | AlignImageSize += ImageContext.SectionAlignment;\r | |
448 | }\r | |
449 | \r | |
5d7f3126 | 450 | if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0 && (Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME)) {\r |
54ea99a7 | 451 | Status = GetPeCoffImageFixLoadingAssignedAddress(&ImageContext, Private);\r |
452 | if (EFI_ERROR (Status)){\r | |
453 | DEBUG ((EFI_D_INFO|EFI_D_LOAD, "LOADING MODULE FIXED ERROR: Failed to load module at fixed address. \n"));\r | |
454 | //\r | |
455 | // The PEIM is not assiged valid address, try to allocate page to load it.\r | |
456 | //\r | |
a0ffd7a9 AB |
457 | Status = PeiServicesAllocatePages (EfiBootServicesCode,\r |
458 | EFI_SIZE_TO_PAGES ((UINT32) AlignImageSize),\r | |
459 | &ImageContext.ImageAddress);\r | |
54ea99a7 | 460 | }\r |
461 | } else {\r | |
a0ffd7a9 AB |
462 | Status = PeiServicesAllocatePages (EfiBootServicesCode,\r |
463 | EFI_SIZE_TO_PAGES ((UINT32) AlignImageSize),\r | |
464 | &ImageContext.ImageAddress);\r | |
54ea99a7 | 465 | }\r |
a0ffd7a9 | 466 | if (!EFI_ERROR (Status)) {\r |
5d7f3126 LG |
467 | //\r |
468 | // Adjust the Image Address to make sure it is section alignment.\r | |
469 | //\r | |
470 | if (ImageContext.SectionAlignment > EFI_PAGE_SIZE) {\r | |
471 | ImageContext.ImageAddress =\r | |
472 | (ImageContext.ImageAddress + ImageContext.SectionAlignment - 1) &\r | |
473 | ~((UINTN)ImageContext.SectionAlignment - 1);\r | |
474 | }\r | |
475 | //\r | |
476 | // Fix alignment requirement when Load IPF TeImage into memory.\r | |
477 | // Skip the reserved space for the stripped PeHeader when load TeImage into memory.\r | |
478 | //\r | |
479 | if (ImageContext.IsTeImage) {\r | |
480 | ImageContext.ImageAddress = ImageContext.ImageAddress +\r | |
481 | ((EFI_TE_IMAGE_HEADER *) Pe32Data)->StrippedSize -\r | |
482 | sizeof (EFI_TE_IMAGE_HEADER);\r | |
483 | }\r | |
484 | } else {\r | |
485 | //\r | |
486 | // No enough memory resource.\r | |
487 | //\r | |
488 | if (IsXipImage) {\r | |
489 | //\r | |
490 | // XIP image can still be invoked.\r | |
491 | //\r | |
492 | ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)(UINTN) Pe32Data;\r | |
493 | ReturnStatus = EFI_WARN_BUFFER_TOO_SMALL;\r | |
494 | } else {\r | |
495 | //\r | |
496 | // Non XIP image can't be loaded because no enough memory is allocated.\r | |
497 | //\r | |
498 | ASSERT (FALSE);\r | |
499 | return EFI_OUT_OF_RESOURCES;\r | |
500 | }\r | |
43ada17c | 501 | }\r |
4e844595 | 502 | }\r |
b0d803fe | 503 | \r |
504 | //\r | |
505 | // Load the image to our new buffer\r | |
506 | //\r | |
3d7b0992 | 507 | Status = PeCoffLoaderLoadImage (&ImageContext);\r |
b0d803fe | 508 | if (EFI_ERROR (Status)) {\r |
63c677e2 LG |
509 | if (ImageContext.ImageError == IMAGE_ERROR_INVALID_SECTION_ALIGNMENT) {\r |
510 | DEBUG ((DEBUG_ERROR, "PEIM Image Address 0x%11p doesn't meet with section alignment 0x%x.\n", (VOID*)(UINTN)ImageContext.ImageAddress, ImageContext.SectionAlignment));\r | |
511 | }\r | |
b0d803fe | 512 | return Status;\r |
513 | }\r | |
514 | //\r | |
515 | // Relocate the image in our new buffer\r | |
516 | //\r | |
3d7b0992 | 517 | Status = PeCoffLoaderRelocateImage (&ImageContext);\r |
b0d803fe | 518 | if (EFI_ERROR (Status)) {\r |
519 | return Status;\r | |
520 | }\r | |
521 | \r | |
522 | //\r | |
523 | // Flush the instruction cache so the image data is written before we execute it\r | |
524 | //\r | |
5d7f3126 | 525 | if (ImageContext.ImageAddress != (EFI_PHYSICAL_ADDRESS)(UINTN) Pe32Data) {\r |
43ada17c | 526 | InvalidateInstructionCacheRange ((VOID *)(UINTN)ImageContext.ImageAddress, (UINTN)ImageContext.ImageSize);\r |
527 | }\r | |
b0d803fe | 528 | \r |
529 | *ImageAddress = ImageContext.ImageAddress;\r | |
530 | *ImageSize = ImageContext.ImageSize;\r | |
531 | *EntryPoint = ImageContext.EntryPoint;\r | |
532 | \r | |
5d7f3126 | 533 | return ReturnStatus;\r |
b0d803fe | 534 | }\r |
535 | \r | |
b1f6a7c6 | 536 | /**\r |
54ea99a7 | 537 | Loads a PEIM into memory for subsequent execution. If there are compressed\r |
538 | images or images that need to be relocated into memory for performance reasons,\r | |
ed299e3c | 539 | this service performs that transformation.\r |
b1f6a7c6 | 540 | \r |
ed299e3c | 541 | @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation\r |
b1f6a7c6 | 542 | @param FileHandle Pointer to the FFS file header of the image.\r |
543 | @param ImageAddressArg Pointer to PE/TE image.\r | |
544 | @param ImageSizeArg Size of PE/TE image.\r | |
545 | @param EntryPoint Pointer to entry point of specified image file for output.\r | |
546 | @param AuthenticationState - Pointer to attestation authentication state of image.\r | |
547 | \r | |
ed299e3c LG |
548 | @retval EFI_SUCCESS Image is successfully loaded.\r |
549 | @retval EFI_NOT_FOUND Fail to locate necessary PPI.\r | |
550 | @retval EFI_UNSUPPORTED Image Machine Type is not supported.\r | |
5d7f3126 LG |
551 | @retval EFI_WARN_BUFFER_TOO_SMALL \r |
552 | There is not enough heap to allocate the requested size.\r | |
553 | This will not prevent the XIP image from being invoked.\r | |
b1f6a7c6 | 554 | \r |
555 | **/\r | |
b0d803fe | 556 | EFI_STATUS\r |
557 | PeiLoadImageLoadImage (\r | |
284c8400 | 558 | IN CONST EFI_PEI_SERVICES **PeiServices,\r |
b0d803fe | 559 | IN EFI_PEI_FILE_HANDLE FileHandle,\r |
560 | OUT EFI_PHYSICAL_ADDRESS *ImageAddressArg, OPTIONAL\r | |
561 | OUT UINT64 *ImageSizeArg, OPTIONAL\r | |
562 | OUT EFI_PHYSICAL_ADDRESS *EntryPoint,\r | |
563 | OUT UINT32 *AuthenticationState\r | |
564 | )\r | |
192f6d4c | 565 | {\r |
566 | EFI_STATUS Status;\r | |
567 | VOID *Pe32Data;\r | |
192f6d4c | 568 | EFI_PHYSICAL_ADDRESS ImageAddress;\r |
569 | UINT64 ImageSize;\r | |
570 | EFI_PHYSICAL_ADDRESS ImageEntryPoint;\r | |
192f6d4c | 571 | UINT16 Machine;\r |
3076397e | 572 | EFI_SECTION_TYPE SearchType1;\r |
573 | EFI_SECTION_TYPE SearchType2;\r | |
192f6d4c | 574 | \r |
3d7b0992 LG |
575 | *EntryPoint = 0;\r |
576 | ImageSize = 0;\r | |
b0d803fe | 577 | *AuthenticationState = 0;\r |
192f6d4c | 578 | \r |
3076397e | 579 | if (FeaturePcdGet (PcdPeiCoreImageLoaderSearchTeSectionFirst)) {\r |
580 | SearchType1 = EFI_SECTION_TE;\r | |
581 | SearchType2 = EFI_SECTION_PE32;\r | |
582 | } else {\r | |
583 | SearchType1 = EFI_SECTION_PE32;\r | |
584 | SearchType2 = EFI_SECTION_TE;\r | |
585 | }\r | |
1b620adb | 586 | \r |
192f6d4c | 587 | //\r |
54ea99a7 | 588 | // Try to find a first exe section (if PcdPeiCoreImageLoaderSearchTeSectionFirst\r |
3076397e | 589 | // is true, TE will be searched first).\r |
192f6d4c | 590 | //\r |
c7935105 | 591 | Status = PeiServicesFfsFindSectionData3 (\r |
3076397e | 592 | SearchType1,\r |
c7935105 | 593 | 0,\r |
b0d803fe | 594 | FileHandle,\r |
c7935105 SZ |
595 | &Pe32Data,\r |
596 | AuthenticationState\r | |
192f6d4c | 597 | );\r |
598 | //\r | |
3076397e | 599 | // If we didn't find a first exe section, try to find the second exe section.\r |
192f6d4c | 600 | //\r |
601 | if (EFI_ERROR (Status)) {\r | |
c7935105 | 602 | Status = PeiServicesFfsFindSectionData3 (\r |
3076397e | 603 | SearchType2,\r |
c7935105 | 604 | 0,\r |
b0d803fe | 605 | FileHandle,\r |
c7935105 SZ |
606 | &Pe32Data,\r |
607 | AuthenticationState\r | |
192f6d4c | 608 | );\r |
b0d803fe | 609 | if (EFI_ERROR (Status)) {\r |
192f6d4c | 610 | //\r |
c7935105 | 611 | // PEI core only carry the loader function for TE and PE32 executables\r |
b0d803fe | 612 | // If this two section does not exist, just return.\r |
192f6d4c | 613 | //\r |
b0d803fe | 614 | return Status;\r |
615 | }\r | |
616 | }\r | |
54ea99a7 | 617 | \r |
63c677e2 LG |
618 | DEBUG ((DEBUG_INFO, "Loading PEIM %g\n", FileHandle));\r |
619 | \r | |
43ada17c | 620 | //\r |
621 | // If memory is installed, perform the shadow operations\r | |
622 | //\r | |
623 | Status = LoadAndRelocatePeCoffImage (\r | |
3d44658c | 624 | FileHandle,\r |
43ada17c | 625 | Pe32Data,\r |
626 | &ImageAddress,\r | |
627 | &ImageSize,\r | |
628 | &ImageEntryPoint\r | |
629 | );\r | |
192f6d4c | 630 | \r |
43ada17c | 631 | ASSERT_EFI_ERROR (Status);\r |
3d7b0992 | 632 | \r |
43ada17c | 633 | \r |
634 | if (EFI_ERROR (Status)) {\r | |
635 | return Status;\r | |
192f6d4c | 636 | }\r |
43ada17c | 637 | \r |
638 | //\r | |
639 | // Got the entry point from the loaded Pe32Data\r | |
640 | //\r | |
641 | Pe32Data = (VOID *) ((UINTN) ImageAddress);\r | |
642 | *EntryPoint = ImageEntryPoint;\r | |
54ea99a7 | 643 | \r |
3d7b0992 | 644 | Machine = PeCoffLoaderGetMachineType (Pe32Data);\r |
54ea99a7 | 645 | \r |
192f6d4c | 646 | if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Machine)) {\r |
8c519a56 | 647 | if (!EFI_IMAGE_MACHINE_CROSS_TYPE_SUPPORTED (Machine)) {\r |
648 | return EFI_UNSUPPORTED;\r | |
649 | }\r | |
192f6d4c | 650 | }\r |
651 | \r | |
b0d803fe | 652 | if (ImageAddressArg != NULL) {\r |
653 | *ImageAddressArg = ImageAddress;\r | |
654 | }\r | |
655 | \r | |
656 | if (ImageSizeArg != NULL) {\r | |
657 | *ImageSizeArg = ImageSize;\r | |
658 | }\r | |
54ea99a7 | 659 | \r |
192f6d4c | 660 | DEBUG_CODE_BEGIN ();\r |
3d7b0992 | 661 | CHAR8 *AsciiString;\r |
77a750a5 | 662 | CHAR8 EfiFileName[512];\r |
3d7b0992 | 663 | INT32 Index;\r |
77a750a5 | 664 | INT32 StartIndex;\r |
e98cd821 LG |
665 | \r |
666 | //\r | |
667 | // Print debug message: Loading PEIM at 0x12345678 EntryPoint=0x12345688 Driver.efi\r | |
668 | //\r | |
7cf02714 | 669 | if (Machine != EFI_IMAGE_MACHINE_IA64) {\r |
91136124 | 670 | DEBUG ((EFI_D_INFO | EFI_D_LOAD, "Loading PEIM at 0x%11p EntryPoint=0x%11p ", (VOID *)(UINTN)ImageAddress, (VOID *)(UINTN)*EntryPoint));\r |
e98cd821 LG |
671 | } else {\r |
672 | //\r | |
673 | // For IPF Image, the real entry point should be print.\r | |
674 | //\r | |
91136124 | 675 | DEBUG ((EFI_D_INFO | EFI_D_LOAD, "Loading PEIM at 0x%11p EntryPoint=0x%11p ", (VOID *)(UINTN)ImageAddress, (VOID *)(UINTN)(*(UINT64 *)(UINTN)*EntryPoint)));\r |
e98cd821 | 676 | }\r |
54ea99a7 | 677 | \r |
e98cd821 LG |
678 | //\r |
679 | // Print Module Name by PeImage PDB file name.\r | |
680 | //\r | |
3d7b0992 | 681 | AsciiString = PeCoffLoaderGetPdbPointer (Pe32Data);\r |
54ea99a7 | 682 | \r |
3d7b0992 | 683 | if (AsciiString != NULL) {\r |
77a750a5 LG |
684 | StartIndex = 0;\r |
685 | for (Index = 0; AsciiString[Index] != 0; Index++) {\r | |
1e21413c | 686 | if (AsciiString[Index] == '\\' || AsciiString[Index] == '/') {\r |
77a750a5 | 687 | StartIndex = Index + 1;\r |
3d7b0992 | 688 | }\r |
192f6d4c | 689 | }\r |
192f6d4c | 690 | \r |
77a750a5 LG |
691 | //\r |
692 | // Copy the PDB file name to our temporary string, and replace .pdb with .efi\r | |
693 | // The PDB file name is limited in the range of 0~511.\r | |
694 | // If the length is bigger than 511, trim the redudant characters to avoid overflow in array boundary.\r | |
695 | //\r | |
696 | for (Index = 0; Index < sizeof (EfiFileName) - 4; Index++) {\r | |
697 | EfiFileName[Index] = AsciiString[Index + StartIndex];\r | |
698 | if (EfiFileName[Index] == 0) {\r | |
699 | EfiFileName[Index] = '.';\r | |
700 | }\r | |
701 | if (EfiFileName[Index] == '.') {\r | |
702 | EfiFileName[Index + 1] = 'e';\r | |
703 | EfiFileName[Index + 2] = 'f';\r | |
704 | EfiFileName[Index + 3] = 'i';\r | |
705 | EfiFileName[Index + 4] = 0;\r | |
706 | break;\r | |
192f6d4c | 707 | }\r |
708 | }\r | |
77a750a5 LG |
709 | \r |
710 | if (Index == sizeof (EfiFileName) - 4) {\r | |
711 | EfiFileName[Index] = 0;\r | |
712 | }\r | |
713 | \r | |
714 | DEBUG ((EFI_D_INFO | EFI_D_LOAD, "%a", EfiFileName));\r | |
192f6d4c | 715 | }\r |
3d7b0992 | 716 | \r |
192f6d4c | 717 | DEBUG_CODE_END ();\r |
718 | \r | |
719 | DEBUG ((EFI_D_INFO | EFI_D_LOAD, "\n"));\r | |
720 | \r | |
721 | return EFI_SUCCESS;\r | |
b0d803fe | 722 | \r |
723 | }\r | |
724 | \r | |
725 | \r | |
b1f6a7c6 | 726 | /**\r |
727 | The wrapper function of PeiLoadImageLoadImage().\r | |
728 | \r | |
729 | @param This - Pointer to EFI_PEI_LOAD_FILE_PPI.\r | |
730 | @param FileHandle - Pointer to the FFS file header of the image.\r | |
731 | @param ImageAddressArg - Pointer to PE/TE image.\r | |
732 | @param ImageSizeArg - Size of PE/TE image.\r | |
733 | @param EntryPoint - Pointer to entry point of specified image file for output.\r | |
734 | @param AuthenticationState - Pointer to attestation authentication state of image.\r | |
735 | \r | |
736 | @return Status of PeiLoadImageLoadImage().\r | |
737 | \r | |
738 | **/\r | |
b0d803fe | 739 | EFI_STATUS\r |
740 | EFIAPI\r | |
741 | PeiLoadImageLoadImageWrapper (\r | |
742 | IN CONST EFI_PEI_LOAD_FILE_PPI *This,\r | |
743 | IN EFI_PEI_FILE_HANDLE FileHandle,\r | |
744 | OUT EFI_PHYSICAL_ADDRESS *ImageAddressArg, OPTIONAL\r | |
745 | OUT UINT64 *ImageSizeArg, OPTIONAL\r | |
746 | OUT EFI_PHYSICAL_ADDRESS *EntryPoint,\r | |
747 | OUT UINT32 *AuthenticationState\r | |
748 | )\r | |
b0d803fe | 749 | {\r |
750 | return PeiLoadImageLoadImage (\r | |
751 | GetPeiServicesTablePointer (),\r | |
752 | FileHandle,\r | |
753 | ImageAddressArg,\r | |
754 | ImageSizeArg,\r | |
755 | EntryPoint,\r | |
756 | AuthenticationState\r | |
757 | );\r | |
192f6d4c | 758 | }\r |
b0d803fe | 759 | \r |
341a658f LG |
760 | /**\r |
761 | Check whether the input image has the relocation.\r | |
762 | \r | |
763 | @param Pe32Data Pointer to the PE/COFF or TE image.\r | |
764 | \r | |
765 | @retval TRUE Relocation is stripped.\r | |
766 | @retval FALSE Relocation is not stripped.\r | |
767 | \r | |
768 | **/\r | |
769 | BOOLEAN\r | |
770 | RelocationIsStrip (\r | |
771 | IN VOID *Pe32Data\r | |
772 | )\r | |
773 | {\r | |
774 | EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;\r | |
775 | EFI_IMAGE_DOS_HEADER *DosHdr;\r | |
776 | \r | |
777 | ASSERT (Pe32Data != NULL);\r | |
778 | \r | |
779 | DosHdr = (EFI_IMAGE_DOS_HEADER *)Pe32Data;\r | |
780 | if (DosHdr->e_magic == EFI_IMAGE_DOS_SIGNATURE) {\r | |
781 | //\r | |
782 | // DOS image header is present, so read the PE header after the DOS image header.\r | |
783 | //\r | |
784 | Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)((UINTN) Pe32Data + (UINTN) ((DosHdr->e_lfanew) & 0x0ffff));\r | |
785 | } else {\r | |
786 | //\r | |
787 | // DOS image header is not present, so PE header is at the image base.\r | |
788 | //\r | |
789 | Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)Pe32Data;\r | |
790 | }\r | |
791 | \r | |
792 | //\r | |
793 | // Three cases with regards to relocations:\r | |
794 | // - Image has base relocs, RELOCS_STRIPPED==0 => image is relocatable\r | |
795 | // - Image has no base relocs, RELOCS_STRIPPED==1 => Image is not relocatable\r | |
796 | // - Image has no base relocs, RELOCS_STRIPPED==0 => Image is relocatable but\r | |
797 | // has no base relocs to apply\r | |
798 | // Obviously having base relocations with RELOCS_STRIPPED==1 is invalid.\r | |
799 | //\r | |
800 | // Look at the file header to determine if relocations have been stripped, and\r | |
801 | // save this info in the image context for later use.\r | |
802 | //\r | |
803 | if (Hdr.Te->Signature == EFI_TE_IMAGE_HEADER_SIGNATURE) {\r | |
804 | if ((Hdr.Te->DataDirectory[0].Size == 0) && (Hdr.Te->DataDirectory[0].VirtualAddress == 0)) {\r | |
805 | return TRUE;\r | |
806 | } else {\r | |
807 | return FALSE;\r | |
808 | }\r | |
809 | } else if (Hdr.Pe32->Signature == EFI_IMAGE_NT_SIGNATURE) {\r | |
810 | if ((Hdr.Pe32->FileHeader.Characteristics & EFI_IMAGE_FILE_RELOCS_STRIPPED) != 0) {\r | |
811 | return TRUE;\r | |
812 | } else {\r | |
813 | return FALSE;\r | |
814 | }\r | |
815 | }\r | |
816 | \r | |
817 | return FALSE;\r | |
818 | }\r | |
819 | \r | |
b1f6a7c6 | 820 | /**\r |
ed299e3c | 821 | Routine to load image file for subsequent execution by LoadFile Ppi.\r |
54ea99a7 | 822 | If any LoadFile Ppi is not found, the build-in support function for the PE32+/TE\r |
ed299e3c | 823 | XIP image format is used.\r |
b1f6a7c6 | 824 | \r |
ed299e3c LG |
825 | @param PeiServices - An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation\r |
826 | @param FileHandle - Pointer to the FFS file header of the image.\r | |
341a658f | 827 | @param PeimState - The dispatch state of the input PEIM handle.\r |
ed299e3c LG |
828 | @param EntryPoint - Pointer to entry point of specified image file for output.\r |
829 | @param AuthenticationState - Pointer to attestation authentication state of image.\r | |
b1f6a7c6 | 830 | \r |
ed299e3c LG |
831 | @retval EFI_SUCCESS - Image is successfully loaded.\r |
832 | @retval EFI_NOT_FOUND - Fail to locate necessary PPI\r | |
833 | @retval Others - Fail to load file.\r | |
b1f6a7c6 | 834 | \r |
835 | **/\r | |
b0d803fe | 836 | EFI_STATUS\r |
837 | PeiLoadImage (\r | |
6c7a807a | 838 | IN CONST EFI_PEI_SERVICES **PeiServices,\r |
b0d803fe | 839 | IN EFI_PEI_FILE_HANDLE FileHandle,\r |
341a658f | 840 | IN UINT8 PeimState,\r |
b0d803fe | 841 | OUT EFI_PHYSICAL_ADDRESS *EntryPoint,\r |
842 | OUT UINT32 *AuthenticationState\r | |
843 | )\r | |
b0d803fe | 844 | {\r |
845 | EFI_STATUS PpiStatus;\r | |
846 | EFI_STATUS Status;\r | |
847 | UINTN Index;\r | |
848 | EFI_PEI_LOAD_FILE_PPI *LoadFile;\r | |
849 | EFI_PHYSICAL_ADDRESS ImageAddress;\r | |
850 | UINT64 ImageSize;\r | |
341a658f | 851 | BOOLEAN IsStrip;\r |
b0d803fe | 852 | \r |
341a658f | 853 | IsStrip = FALSE;\r |
b0d803fe | 854 | //\r |
855 | // If any instances of PEI_LOAD_FILE_PPI are installed, they are called.\r | |
856 | // one at a time, until one reports EFI_SUCCESS.\r | |
857 | //\r | |
858 | Index = 0;\r | |
859 | do {\r | |
860 | PpiStatus = PeiServicesLocatePpi (\r | |
861 | &gEfiPeiLoadFilePpiGuid,\r | |
862 | Index,\r | |
863 | NULL,\r | |
864 | (VOID **)&LoadFile\r | |
865 | );\r | |
866 | if (!EFI_ERROR (PpiStatus)) {\r | |
867 | Status = LoadFile->LoadFile (\r | |
54ea99a7 | 868 | LoadFile,\r |
869 | FileHandle,\r | |
870 | &ImageAddress,\r | |
b0d803fe | 871 | &ImageSize,\r |
872 | EntryPoint,\r | |
873 | AuthenticationState\r | |
874 | );\r | |
5d7f3126 | 875 | if (!EFI_ERROR (Status) || Status == EFI_WARN_BUFFER_TOO_SMALL) {\r |
341a658f LG |
876 | //\r |
877 | // The shadowed PEIM must be relocatable.\r | |
878 | //\r | |
c2c4199b | 879 | if (PeimState == PEIM_STATE_REGISTER_FOR_SHADOW) {\r |
341a658f LG |
880 | IsStrip = RelocationIsStrip ((VOID *) (UINTN) ImageAddress);\r |
881 | ASSERT (!IsStrip);\r | |
882 | if (IsStrip) {\r | |
883 | return EFI_UNSUPPORTED;\r | |
884 | }\r | |
885 | }\r | |
886 | \r | |
db0b7ad5 LG |
887 | //\r |
888 | // The image to be started must have the machine type supported by PeiCore.\r | |
889 | //\r | |
890 | ASSERT (EFI_IMAGE_MACHINE_TYPE_SUPPORTED (PeCoffLoaderGetMachineType ((VOID *) (UINTN) ImageAddress)));\r | |
919df8e6 | 891 | if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (PeCoffLoaderGetMachineType ((VOID *) (UINTN) ImageAddress))) {\r |
919df8e6 LG |
892 | return EFI_UNSUPPORTED;\r |
893 | }\r | |
5d7f3126 | 894 | return EFI_SUCCESS;\r |
b0d803fe | 895 | }\r |
896 | }\r | |
897 | Index++;\r | |
898 | } while (!EFI_ERROR (PpiStatus));\r | |
899 | \r | |
8c519a56 | 900 | return PpiStatus;\r |
b0d803fe | 901 | }\r |
902 | \r | |
903 | \r | |
b1f6a7c6 | 904 | /**\r |
b0d803fe | 905 | \r |
ed299e3c LG |
906 | Install Pei Load File PPI.\r |
907 | \r | |
908 | \r | |
909 | @param PrivateData - Pointer to PEI_CORE_INSTANCE.\r | |
910 | @param OldCoreData - Pointer to PEI_CORE_INSTANCE.\r | |
b0d803fe | 911 | \r |
b1f6a7c6 | 912 | **/\r |
913 | VOID\r | |
914 | InitializeImageServices (\r | |
915 | IN PEI_CORE_INSTANCE *PrivateData,\r | |
916 | IN PEI_CORE_INSTANCE *OldCoreData\r | |
917 | )\r | |
b0d803fe | 918 | {\r |
b0d803fe | 919 | if (OldCoreData == NULL) {\r |
920 | //\r | |
921 | // The first time we are XIP (running from FLASH). We need to remember the\r | |
922 | // FLASH address so we can reinstall the memory version that runs faster\r | |
923 | //\r | |
924 | PrivateData->XipLoadFile = &gPpiLoadFilePpiList;\r | |
925 | PeiServicesInstallPpi (PrivateData->XipLoadFile);\r | |
926 | } else {\r | |
927 | //\r | |
54ea99a7 | 928 | // 2nd time we are running from memory so replace the XIP version with the\r |
929 | // new memory version.\r | |
b0d803fe | 930 | //\r |
54ea99a7 | 931 | PeiServicesReInstallPpi (PrivateData->XipLoadFile, &gPpiLoadFilePpiList);\r |
b0d803fe | 932 | }\r |
933 | }\r | |
934 | \r | |
935 | \r | |
936 | \r | |
341a658f | 937 | \r |