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