]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Core/Pei/Image/Image.c
Fix a UI hang bug when adding Boot Option or Driver Option.
[mirror_edk2.git] / MdeModulePkg / Core / Pei / Image / Image.c
CommitLineData
615c6dd0 1/** @file\r
b1f6a7c6 2 Pei Core Load Image Support\r
3 \r
8c519a56 4Copyright (c) 2006 - 2009, Intel Corporation \r
192f6d4c 5All rights reserved. This program and the accompanying materials \r
6are licensed and made available under the terms and conditions of the BSD License \r
7which accompanies this distribution. The full text of the license may be found at \r
8http://opensource.org/licenses/bsd-license.php \r
9 \r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
12\r
615c6dd0 13**/\r
192f6d4c 14\r
0d516397 15#include "PeiMain.h"\r
192f6d4c 16\r
b0d803fe 17\r
fe1e36e5 18EFI_PEI_LOAD_FILE_PPI mPeiLoadImagePpi = {\r
b0d803fe 19 PeiLoadImageLoadImageWrapper\r
20};\r
21\r
22\r
fe1e36e5 23EFI_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
31 Support routine for the PE/COFF Loader that reads a buffer from a PE/COFF file\r
32\r
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 42EFI_STATUS\r
43EFIAPI\r
44PeiImageRead (\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
53 UINTN Length;\r
54\r
55 Destination8 = Buffer;\r
56 Source8 = (CHAR8 *) ((UINTN) FileHandle + FileOffset);\r
43ada17c 57 if (Destination8 != Source8) {\r
58 Length = *ReadSize;\r
59 while ((Length--) > 0) {\r
60 *(Destination8++) = *(Source8++);\r
61 }\r
a9bfd802
LG
62 }\r
63\r
b0d803fe 64 return EFI_SUCCESS;\r
65}\r
66\r
b1f6a7c6 67/**\r
b0d803fe 68\r
ed299e3c 69 Support routine to get the Image read file function.\r
b0d803fe 70\r
b1f6a7c6 71 @param ImageContext - The context of the image being loaded\r
b0d803fe 72\r
b1f6a7c6 73 @retval EFI_SUCCESS - If Image function location is found\r
b0d803fe 74\r
b1f6a7c6 75**/\r
76EFI_STATUS\r
77GetImageReadFunction (\r
78 IN PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r
79 )\r
b0d803fe 80{\r
43ada17c 81 PEI_CORE_INSTANCE *Private;\r
b0d803fe 82 VOID* MemoryBuffer;\r
83\r
43ada17c 84 Private = PEI_CORE_INSTANCE_FROM_PS_THIS (GetPeiServicesTablePointer ());\r
b0d803fe 85\r
43ada17c 86 if (!Private->PeiMemoryInstalled) {\r
87 ImageContext->ImageRead = PeiImageRead;\r
88 } else {\r
89 MemoryBuffer = AllocatePages (0x400 / EFI_PAGE_SIZE + 1);\r
90 ASSERT (MemoryBuffer != NULL);\r
b0d803fe 91\r
43ada17c 92 CopyMem (MemoryBuffer, (CONST VOID *) (UINTN) PeiImageRead, 0x400);\r
93\r
94 ImageContext->ImageRead = (PE_COFF_LOADER_READ_FILE) (UINTN) MemoryBuffer;\r
95 }\r
b0d803fe 96\r
97 return EFI_SUCCESS;\r
98}\r
99\r
b1f6a7c6 100/**\r
101\r
102 Loads and relocates a PE/COFF image into memory.\r
103\r
104\r
105 @param Pe32Data - The base address of the PE/COFF file that is to be loaded and relocated\r
106 @param ImageAddress - The base address of the relocated PE/COFF image\r
107 @param ImageSize - The size of the relocated PE/COFF image\r
108 @param EntryPoint - The entry point of the relocated PE/COFF image\r
109\r
110 @retval EFI_SUCCESS The file was loaded and relocated\r
111 @retval EFI_OUT_OF_RESOURCES There was not enough memory to load and relocate the PE/COFF file\r
ed299e3c 112 @retval EFI_INVALID_PARAMETER The image withou .reloc section can't be relocated.\r
b1f6a7c6 113\r
114**/\r
b0d803fe 115EFI_STATUS\r
116LoadAndRelocatePeCoffImage (\r
b0d803fe 117 IN VOID *Pe32Data,\r
118 OUT EFI_PHYSICAL_ADDRESS *ImageAddress,\r
119 OUT UINT64 *ImageSize,\r
120 OUT EFI_PHYSICAL_ADDRESS *EntryPoint\r
121 )\r
b0d803fe 122{\r
123 EFI_STATUS Status;\r
124 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;\r
43ada17c 125 PEI_CORE_INSTANCE *Private;\r
126\r
127 Private = PEI_CORE_INSTANCE_FROM_PS_THIS (GetPeiServicesTablePointer ());\r
b0d803fe 128\r
b0d803fe 129 ZeroMem (&ImageContext, sizeof (ImageContext));\r
130 ImageContext.Handle = Pe32Data;\r
131 Status = GetImageReadFunction (&ImageContext);\r
132\r
133 ASSERT_EFI_ERROR (Status);\r
134\r
3d7b0992 135 Status = PeCoffLoaderGetImageInfo (&ImageContext);\r
b0d803fe 136 if (EFI_ERROR (Status)) {\r
137 return Status;\r
138 }\r
139 //\r
9626a87e
LG
140 // When Image has no reloc section, it can't be relocated into memory.\r
141 //\r
142 if (ImageContext.RelocationsStripped) {\r
288f9b38 143 DEBUG ((EFI_D_ERROR, "The image at 0x%08x without reloc section can't be loaded into memory\n", (UINTN) Pe32Data));\r
9626a87e
LG
144 }\r
145 //\r
b0d803fe 146 // Allocate Memory for the image\r
147 //\r
43ada17c 148 if (Private->PeiMemoryInstalled) {\r
149 ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)(UINTN) AllocatePages (EFI_SIZE_TO_PAGES ((UINT32) ImageContext.ImageSize));\r
150 ASSERT (ImageContext.ImageAddress != 0);\r
151 \r
152 //\r
153 // Skip the reserved space for the stripped PeHeader when load TeImage into memory.\r
154 //\r
155 if (ImageContext.IsTeImage) {\r
156 ImageContext.ImageAddress = ImageContext.ImageAddress + \r
157 ((EFI_TE_IMAGE_HEADER *) Pe32Data)->StrippedSize -\r
158 sizeof (EFI_TE_IMAGE_HEADER);\r
159 }\r
160 } else {\r
161 ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)(UINTN) Pe32Data;\r
4e844595 162 }\r
b0d803fe 163\r
164 //\r
165 // Load the image to our new buffer\r
166 //\r
3d7b0992 167 Status = PeCoffLoaderLoadImage (&ImageContext);\r
b0d803fe 168 if (EFI_ERROR (Status)) {\r
169 return Status;\r
170 }\r
171 //\r
172 // Relocate the image in our new buffer\r
173 //\r
3d7b0992 174 Status = PeCoffLoaderRelocateImage (&ImageContext);\r
b0d803fe 175 if (EFI_ERROR (Status)) {\r
176 return Status;\r
177 }\r
178\r
179 //\r
180 // Flush the instruction cache so the image data is written before we execute it\r
181 //\r
43ada17c 182 if (Private->PeiMemoryInstalled) {\r
183 InvalidateInstructionCacheRange ((VOID *)(UINTN)ImageContext.ImageAddress, (UINTN)ImageContext.ImageSize);\r
184 }\r
b0d803fe 185\r
186 *ImageAddress = ImageContext.ImageAddress;\r
187 *ImageSize = ImageContext.ImageSize;\r
188 *EntryPoint = ImageContext.EntryPoint;\r
189\r
190 return EFI_SUCCESS;\r
191}\r
192\r
b1f6a7c6 193/**\r
ed299e3c
LG
194 Loads a PEIM into memory for subsequent execution. If there are compressed \r
195 images or images that need to be relocated into memory for performance reasons, \r
196 this service performs that transformation.\r
b1f6a7c6 197\r
ed299e3c 198 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation\r
b1f6a7c6 199 @param FileHandle Pointer to the FFS file header of the image.\r
200 @param ImageAddressArg Pointer to PE/TE image.\r
201 @param ImageSizeArg Size of PE/TE image.\r
202 @param EntryPoint Pointer to entry point of specified image file for output.\r
203 @param AuthenticationState - Pointer to attestation authentication state of image.\r
204\r
ed299e3c
LG
205 @retval EFI_SUCCESS Image is successfully loaded.\r
206 @retval EFI_NOT_FOUND Fail to locate necessary PPI.\r
207 @retval EFI_UNSUPPORTED Image Machine Type is not supported.\r
b1f6a7c6 208\r
209**/\r
b0d803fe 210EFI_STATUS\r
211PeiLoadImageLoadImage (\r
284c8400 212 IN CONST EFI_PEI_SERVICES **PeiServices,\r
b0d803fe 213 IN EFI_PEI_FILE_HANDLE FileHandle,\r
214 OUT EFI_PHYSICAL_ADDRESS *ImageAddressArg, OPTIONAL\r
215 OUT UINT64 *ImageSizeArg, OPTIONAL\r
216 OUT EFI_PHYSICAL_ADDRESS *EntryPoint,\r
217 OUT UINT32 *AuthenticationState\r
218 )\r
192f6d4c 219{\r
220 EFI_STATUS Status;\r
221 VOID *Pe32Data;\r
192f6d4c 222 EFI_PHYSICAL_ADDRESS ImageAddress;\r
223 UINT64 ImageSize;\r
224 EFI_PHYSICAL_ADDRESS ImageEntryPoint;\r
192f6d4c 225 UINT16 Machine;\r
3076397e 226 EFI_SECTION_TYPE SearchType1;\r
227 EFI_SECTION_TYPE SearchType2;\r
192f6d4c 228\r
3d7b0992
LG
229 *EntryPoint = 0;\r
230 ImageSize = 0;\r
b0d803fe 231 *AuthenticationState = 0;\r
192f6d4c 232\r
3076397e 233 if (FeaturePcdGet (PcdPeiCoreImageLoaderSearchTeSectionFirst)) {\r
234 SearchType1 = EFI_SECTION_TE;\r
235 SearchType2 = EFI_SECTION_PE32;\r
236 } else {\r
237 SearchType1 = EFI_SECTION_PE32;\r
238 SearchType2 = EFI_SECTION_TE;\r
239 }\r
192f6d4c 240 //\r
3076397e 241 // Try to find a first exe section (if PcdPeiCoreImageLoaderSearchTeSectionFirst \r
242 // is true, TE will be searched first).\r
192f6d4c 243 //\r
244 Status = PeiServicesFfsFindSectionData (\r
3076397e 245 SearchType1,\r
b0d803fe 246 FileHandle,\r
192f6d4c 247 &Pe32Data\r
248 );\r
249 //\r
3076397e 250 // If we didn't find a first exe section, try to find the second exe section.\r
192f6d4c 251 //\r
252 if (EFI_ERROR (Status)) {\r
253 Status = PeiServicesFfsFindSectionData (\r
3076397e 254 SearchType2,\r
b0d803fe 255 FileHandle,\r
256 &Pe32Data\r
192f6d4c 257 );\r
b0d803fe 258 if (EFI_ERROR (Status)) {\r
192f6d4c 259 //\r
b0d803fe 260 // PEI core only carry the loader function fro TE and PE32 executables\r
261 // If this two section does not exist, just return.\r
192f6d4c 262 //\r
b0d803fe 263 return Status;\r
264 }\r
265 }\r
266 \r
43ada17c 267 //\r
268 // If memory is installed, perform the shadow operations\r
269 //\r
270 Status = LoadAndRelocatePeCoffImage (\r
271 Pe32Data,\r
272 &ImageAddress,\r
273 &ImageSize,\r
274 &ImageEntryPoint\r
275 );\r
192f6d4c 276\r
43ada17c 277 ASSERT_EFI_ERROR (Status);\r
3d7b0992 278\r
43ada17c 279\r
280 if (EFI_ERROR (Status)) {\r
281 return Status;\r
192f6d4c 282 }\r
43ada17c 283\r
284 //\r
285 // Got the entry point from the loaded Pe32Data\r
286 //\r
287 Pe32Data = (VOID *) ((UINTN) ImageAddress);\r
288 *EntryPoint = ImageEntryPoint;\r
3d7b0992
LG
289 \r
290 Machine = PeCoffLoaderGetMachineType (Pe32Data);\r
192f6d4c 291 \r
292 if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Machine)) {\r
8c519a56 293 if (!EFI_IMAGE_MACHINE_CROSS_TYPE_SUPPORTED (Machine)) {\r
294 return EFI_UNSUPPORTED;\r
295 }\r
192f6d4c 296 }\r
297\r
b0d803fe 298 if (ImageAddressArg != NULL) {\r
299 *ImageAddressArg = ImageAddress;\r
300 }\r
301\r
302 if (ImageSizeArg != NULL) {\r
303 *ImageSizeArg = ImageSize;\r
304 }\r
305 \r
192f6d4c 306 DEBUG_CODE_BEGIN ();\r
3d7b0992
LG
307 CHAR8 *AsciiString;\r
308 CHAR8 AsciiBuffer[512];\r
309 INT32 Index;\r
310 INT32 Index1;\r
e98cd821
LG
311\r
312 //\r
313 // Print debug message: Loading PEIM at 0x12345678 EntryPoint=0x12345688 Driver.efi\r
314 //\r
7cf02714 315 if (Machine != EFI_IMAGE_MACHINE_IA64) {\r
91136124 316 DEBUG ((EFI_D_INFO | EFI_D_LOAD, "Loading PEIM at 0x%11p EntryPoint=0x%11p ", (VOID *)(UINTN)ImageAddress, (VOID *)(UINTN)*EntryPoint));\r
e98cd821
LG
317 } else {\r
318 //\r
319 // For IPF Image, the real entry point should be print.\r
320 //\r
91136124 321 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 322 }\r
192f6d4c 323 \r
e98cd821
LG
324 //\r
325 // Print Module Name by PeImage PDB file name.\r
326 //\r
3d7b0992
LG
327 AsciiString = PeCoffLoaderGetPdbPointer (Pe32Data);\r
328 \r
329 if (AsciiString != NULL) {\r
19ea58a1 330 for (Index = (INT32) AsciiStrLen (AsciiString) - 1; Index >= 0; Index --) {\r
3d7b0992
LG
331 if (AsciiString[Index] == '\\') {\r
332 break;\r
333 }\r
192f6d4c 334 }\r
192f6d4c 335\r
3d7b0992
LG
336 if (Index != 0) {\r
337 for (Index1 = 0; AsciiString[Index + 1 + Index1] != '.'; Index1 ++) {\r
338 AsciiBuffer [Index1] = AsciiString[Index + 1 + Index1];\r
192f6d4c 339 }\r
3d7b0992
LG
340 AsciiBuffer [Index1] = '\0';\r
341 DEBUG ((EFI_D_INFO | EFI_D_LOAD, "%a.efi", AsciiBuffer));\r
192f6d4c 342 }\r
343 }\r
3d7b0992 344\r
192f6d4c 345 DEBUG_CODE_END ();\r
346\r
347 DEBUG ((EFI_D_INFO | EFI_D_LOAD, "\n"));\r
348\r
349 return EFI_SUCCESS;\r
b0d803fe 350\r
351}\r
352\r
353\r
b1f6a7c6 354/**\r
355 The wrapper function of PeiLoadImageLoadImage().\r
356\r
357 @param This - Pointer to EFI_PEI_LOAD_FILE_PPI.\r
358 @param FileHandle - Pointer to the FFS file header of the image.\r
359 @param ImageAddressArg - Pointer to PE/TE image.\r
360 @param ImageSizeArg - Size of PE/TE image.\r
361 @param EntryPoint - Pointer to entry point of specified image file for output.\r
362 @param AuthenticationState - Pointer to attestation authentication state of image.\r
363\r
364 @return Status of PeiLoadImageLoadImage().\r
365\r
366**/\r
b0d803fe 367EFI_STATUS\r
368EFIAPI\r
369PeiLoadImageLoadImageWrapper (\r
370 IN CONST EFI_PEI_LOAD_FILE_PPI *This,\r
371 IN EFI_PEI_FILE_HANDLE FileHandle,\r
372 OUT EFI_PHYSICAL_ADDRESS *ImageAddressArg, OPTIONAL\r
373 OUT UINT64 *ImageSizeArg, OPTIONAL\r
374 OUT EFI_PHYSICAL_ADDRESS *EntryPoint,\r
375 OUT UINT32 *AuthenticationState\r
376 )\r
b0d803fe 377{\r
378 return PeiLoadImageLoadImage (\r
379 GetPeiServicesTablePointer (),\r
380 FileHandle,\r
381 ImageAddressArg,\r
382 ImageSizeArg,\r
383 EntryPoint,\r
384 AuthenticationState\r
385 );\r
192f6d4c 386}\r
b0d803fe 387\r
b1f6a7c6 388/**\r
ed299e3c
LG
389 Routine to load image file for subsequent execution by LoadFile Ppi.\r
390 If any LoadFile Ppi is not found, the build-in support function for the PE32+/TE \r
391 XIP image format is used.\r
b1f6a7c6 392\r
ed299e3c
LG
393 @param PeiServices - An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation\r
394 @param FileHandle - Pointer to the FFS file header of the image.\r
395 @param EntryPoint - Pointer to entry point of specified image file for output.\r
396 @param AuthenticationState - Pointer to attestation authentication state of image.\r
b1f6a7c6 397\r
ed299e3c
LG
398 @retval EFI_SUCCESS - Image is successfully loaded.\r
399 @retval EFI_NOT_FOUND - Fail to locate necessary PPI\r
400 @retval Others - Fail to load file.\r
b1f6a7c6 401\r
402**/\r
b0d803fe 403EFI_STATUS\r
404PeiLoadImage (\r
6c7a807a 405 IN CONST EFI_PEI_SERVICES **PeiServices,\r
b0d803fe 406 IN EFI_PEI_FILE_HANDLE FileHandle,\r
407 OUT EFI_PHYSICAL_ADDRESS *EntryPoint,\r
408 OUT UINT32 *AuthenticationState\r
409 )\r
b0d803fe 410{\r
411 EFI_STATUS PpiStatus;\r
412 EFI_STATUS Status;\r
413 UINTN Index;\r
414 EFI_PEI_LOAD_FILE_PPI *LoadFile;\r
415 EFI_PHYSICAL_ADDRESS ImageAddress;\r
416 UINT64 ImageSize;\r
417\r
418 //\r
419 // If any instances of PEI_LOAD_FILE_PPI are installed, they are called.\r
420 // one at a time, until one reports EFI_SUCCESS.\r
421 //\r
422 Index = 0;\r
423 do {\r
424 PpiStatus = PeiServicesLocatePpi (\r
425 &gEfiPeiLoadFilePpiGuid,\r
426 Index,\r
427 NULL,\r
428 (VOID **)&LoadFile\r
429 );\r
430 if (!EFI_ERROR (PpiStatus)) {\r
431 Status = LoadFile->LoadFile (\r
432 LoadFile, \r
433 FileHandle, \r
434 &ImageAddress, \r
435 &ImageSize,\r
436 EntryPoint,\r
437 AuthenticationState\r
438 );\r
439 if (!EFI_ERROR (Status)) {\r
db0b7ad5
LG
440 //\r
441 // The image to be started must have the machine type supported by PeiCore.\r
442 //\r
443 ASSERT (EFI_IMAGE_MACHINE_TYPE_SUPPORTED (PeCoffLoaderGetMachineType ((VOID *) (UINTN) ImageAddress)));\r
919df8e6 444 if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (PeCoffLoaderGetMachineType ((VOID *) (UINTN) ImageAddress))) {\r
919df8e6
LG
445 return EFI_UNSUPPORTED;\r
446 }\r
b0d803fe 447 return Status;\r
448 }\r
449 }\r
450 Index++;\r
451 } while (!EFI_ERROR (PpiStatus));\r
452\r
8c519a56 453 return PpiStatus;\r
b0d803fe 454}\r
455\r
456\r
b1f6a7c6 457/**\r
b0d803fe 458\r
ed299e3c
LG
459 Install Pei Load File PPI.\r
460\r
461\r
462 @param PrivateData - Pointer to PEI_CORE_INSTANCE.\r
463 @param OldCoreData - Pointer to PEI_CORE_INSTANCE.\r
b0d803fe 464\r
b1f6a7c6 465**/\r
466VOID\r
467InitializeImageServices (\r
468 IN PEI_CORE_INSTANCE *PrivateData,\r
469 IN PEI_CORE_INSTANCE *OldCoreData\r
470 )\r
b0d803fe 471{\r
b0d803fe 472 if (OldCoreData == NULL) {\r
473 //\r
474 // The first time we are XIP (running from FLASH). We need to remember the\r
475 // FLASH address so we can reinstall the memory version that runs faster\r
476 //\r
477 PrivateData->XipLoadFile = &gPpiLoadFilePpiList;\r
478 PeiServicesInstallPpi (PrivateData->XipLoadFile);\r
479 } else {\r
480 //\r
481 // 2nd time we are running from memory so replace the XIP version with the \r
482 // new memory version. \r
483 //\r
484 PeiServicesReInstallPpi (PrivateData->XipLoadFile, &gPpiLoadFilePpiList); \r
485 }\r
486}\r
487\r
488\r
489\r