3 Copyright (c) 2006 - 2007, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 Pei Core Load Image Support
28 IN EFI_PEI_SERVICES
**PeiServices
,
29 IN EFI_FFS_FILE_HEADER
*PeimFileHeader
,
36 Routine for loading file image.
40 PeiServices - The PEI core services table.
41 PeimFileHeader - Pointer to the FFS file header of the image.
42 EntryPoint - Pointer to entry point of specified image file for output.
46 Status - EFI_SUCCESS - Image is successfully loaded.
47 EFI_NOT_FOUND - Fail to locate necessary PPI
48 Others - Fail to load file.
54 EFI_PEI_FV_FILE_LOADER_PPI
*FvLoadFilePpi
;
55 EFI_PHYSICAL_ADDRESS ImageAddress
;
57 EFI_PHYSICAL_ADDRESS ImageEntryPoint
;
58 EFI_TE_IMAGE_HEADER
*TEImageHeader
;
65 // Try to find a PE32 section.
67 Status
= PeiServicesFfsFindSectionData (
73 // If we didn't find a PE32 section, try to find a TE section.
75 if (EFI_ERROR (Status
)) {
76 Status
= PeiServicesFfsFindSectionData (
79 (VOID
**) &TEImageHeader
81 if (EFI_ERROR (Status
) || TEImageHeader
== NULL
) {
83 // There was not a PE32 or a TE section, so assume that it's a Compressed section
84 // and use the LoadFile
86 Status
= PeiServicesLocatePpi (
87 &gEfiPeiFvFileLoaderPpiGuid
,
90 (VOID
**)&FvLoadFilePpi
92 if (EFI_ERROR (Status
)) {
96 Status
= FvLoadFilePpi
->FvLoadFile (
104 if (EFI_ERROR (Status
)) {
105 return EFI_NOT_FOUND
;
109 // Got the entry point from ImageEntryPoint and ImageStartAddress
111 Pe32Data
= (VOID
*) ((UINTN
) ImageAddress
);
112 *EntryPoint
= (VOID
*) ((UINTN
) ImageEntryPoint
);
115 // Retrieve the entry point from the TE image header
117 ImageAddress
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) TEImageHeader
;
118 *EntryPoint
= (VOID
*)((UINTN
) TEImageHeader
+ sizeof (EFI_TE_IMAGE_HEADER
) +
119 TEImageHeader
->AddressOfEntryPoint
- TEImageHeader
->StrippedSize
);
123 // Retrieve the entry point from the PE/COFF image header
125 ImageAddress
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) Pe32Data
;
126 Status
= PeCoffLoaderGetEntryPoint (Pe32Data
, EntryPoint
);
127 if (EFI_ERROR (Status
)) {
128 return EFI_NOT_FOUND
;
132 if (((EFI_TE_IMAGE_HEADER
*) (UINTN
) ImageAddress
)->Signature
== EFI_TE_IMAGE_HEADER_SIGNATURE
) {
133 TEImageHeader
= (EFI_TE_IMAGE_HEADER
*) (UINTN
) ImageAddress
;
134 Machine
= TEImageHeader
->Machine
;
136 Machine
= PeCoffLoaderGetMachineType (Pe32Data
);
139 if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Machine
)) {
140 return EFI_UNSUPPORTED
;
144 // Print debug message: Loading PEIM at 0x12345678 EntryPoint=0x12345688 Driver.efi
146 DEBUG ((EFI_D_INFO
| EFI_D_LOAD
, "Loading PEIM at 0x%08x EntryPoint=0x%08x ", (UINTN
) ImageAddress
, *EntryPoint
));
148 EFI_IMAGE_DATA_DIRECTORY
*DirectoryEntry
;
149 EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*DebugEntry
;
153 BOOLEAN FileNameFound
;
155 CHAR8 AsciiBuffer
[512];
156 VOID
*CodeViewEntryPointer
;
158 EFI_IMAGE_DOS_HEADER
*DosHeader
;
159 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
160 UINT32 NumberOfRvaAndSizes
;
163 if (TEImageHeader
== NULL
) {
164 DosHeader
= (EFI_IMAGE_DOS_HEADER
*)Pe32Data
;
165 if (DosHeader
->e_magic
== EFI_IMAGE_DOS_SIGNATURE
) {
167 // DOS image header is present, so read the PE header after the DOS image header
169 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)((UINTN
)Pe32Data
+ (UINTN
)((DosHeader
->e_lfanew
) & 0x0ffff));
172 // DOS image header is not present, so PE header is at the image base
174 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)Pe32Data
;
179 // Find the codeview info in the image and display the file name
182 // Per the PE/COFF spec, you can't assume that a given data directory
183 // is present in the image. You have to check the NumberOfRvaAndSizes in
184 // the optional header to verify a desired directory entry is there.
187 DirectoryEntry
= NULL
;
188 NumberOfRvaAndSizes
= 0;
191 if (TEImageHeader
== NULL
) {
192 if (Hdr
.Pe32
->OptionalHeader
.Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
194 // Use PE32 offset get Debug Directory Entry
196 NumberOfRvaAndSizes
= Hdr
.Pe32
->OptionalHeader
.NumberOfRvaAndSizes
;
197 DirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&(Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
]);
198 DebugEntry
= (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*) ((UINTN
) Pe32Data
+ DirectoryEntry
->VirtualAddress
);
199 } else if (Hdr
.Pe32
->OptionalHeader
.Magic
== EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
) {
201 // Use PE32+ offset get Debug Directory Entry
203 NumberOfRvaAndSizes
= Hdr
.Pe32Plus
->OptionalHeader
.NumberOfRvaAndSizes
;
204 DirectoryEntry
= (EFI_IMAGE_DATA_DIRECTORY
*)&(Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
]);
205 DebugEntry
= (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*) ((UINTN
) Pe32Data
+ DirectoryEntry
->VirtualAddress
);
208 if (NumberOfRvaAndSizes
<= EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
) {
209 DirectoryEntry
= NULL
;
213 if (TEImageHeader
->DataDirectory
[EFI_TE_IMAGE_DIRECTORY_ENTRY_DEBUG
].VirtualAddress
!= 0) {
214 DirectoryEntry
= &TEImageHeader
->DataDirectory
[EFI_TE_IMAGE_DIRECTORY_ENTRY_DEBUG
];
215 TEImageAdjust
= sizeof (EFI_TE_IMAGE_HEADER
) - TEImageHeader
->StrippedSize
;
216 DebugEntry
= (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*)((UINTN
) TEImageHeader
+
217 TEImageHeader
->DataDirectory
[EFI_TE_IMAGE_DIRECTORY_ENTRY_DEBUG
].VirtualAddress
+
222 if (DebugEntry
!= NULL
&& DirectoryEntry
!= NULL
) {
223 for (DirCount
= 0; DirCount
< DirectoryEntry
->Size
; DirCount
+= sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
), DebugEntry
++) {
224 if (DebugEntry
->Type
== EFI_IMAGE_DEBUG_TYPE_CODEVIEW
) {
225 if (DebugEntry
->SizeOfData
> 0) {
226 CodeViewEntryPointer
= (VOID
*) ((UINTN
) DebugEntry
->RVA
+ (UINTN
) ImageAddress
+ (UINTN
)TEImageAdjust
);
227 switch (* (UINT32
*) CodeViewEntryPointer
) {
228 case CODEVIEW_SIGNATURE_NB10
:
229 AsciiString
= (CHAR8
*)CodeViewEntryPointer
+ sizeof (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
);
232 case CODEVIEW_SIGNATURE_RSDS
:
233 AsciiString
= (CHAR8
*)CodeViewEntryPointer
+ sizeof (EFI_IMAGE_DEBUG_CODEVIEW_RSDS_ENTRY
);
240 if (AsciiString
!= NULL
) {
241 FileNameFound
= FALSE
;
242 for (Index
= 0, Index1
= 0; AsciiString
[Index
] != '\0'; Index
++) {
243 if (AsciiString
[Index
] == '\\') {
245 FileNameFound
= TRUE
;
250 for (Index
= Index1
+ 1; AsciiString
[Index
] != '.'; Index
++) {
251 AsciiBuffer
[Index
- (Index1
+ 1)] = AsciiString
[Index
];
253 AsciiBuffer
[Index
- (Index1
+ 1)] = 0;
254 DEBUG ((EFI_D_INFO
| EFI_D_LOAD
, "%a.efi", AsciiBuffer
));
264 DEBUG ((EFI_D_INFO
| EFI_D_LOAD
, "\n"));