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