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