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