]> git.proxmox.com Git - mirror_edk2.git/blob - EdkModulePkg/Core/Pei/Image/Image.c
Fix one bug in PeiMain to make it output correct ImageStartAddress. And in DxeIplX64P...
[mirror_edk2.git] / EdkModulePkg / Core / Pei / Image / Image.c
1 /*++
2
3 Copyright (c) 2006, 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
8
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.
11
12 Module Name:
13
14 Image.c
15
16 Abstract:
17
18 Pei Core Load Image Support
19
20 --*/
21
22 #include <PeiMain.h>
23
24
25
26 EFI_STATUS
27 PeiLoadImage (
28 IN EFI_PEI_SERVICES **PeiServices,
29 IN EFI_FFS_FILE_HEADER *PeimFileHeader,
30 OUT VOID **EntryPoint
31 )
32 /*++
33
34 Routine Description:
35
36 Routine for loading file image.
37
38 Arguments:
39
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.
43
44 Returns:
45
46 Status - EFI_SUCCESS - Image is successfully loaded.
47 EFI_NOT_FOUND - Fail to locate necessary PPI
48 Others - Fail to load file.
49
50 --*/
51 {
52 EFI_STATUS Status;
53 VOID *Pe32Data;
54 EFI_PEI_FV_FILE_LOADER_PPI *FvLoadFilePpi;
55 EFI_PHYSICAL_ADDRESS ImageAddress;
56 UINT64 ImageSize;
57 EFI_PHYSICAL_ADDRESS ImageEntryPoint;
58 EFI_TE_IMAGE_HEADER *TEImageHeader;
59
60 *EntryPoint = NULL;
61 TEImageHeader = NULL;
62
63 //
64 // Try to find a PE32 section.
65 //
66 Status = PeiServicesFfsFindSectionData (
67 EFI_SECTION_PE32,
68 PeimFileHeader,
69 &Pe32Data
70 );
71 //
72 // If we didn't find a PE32 section, try to find a TE section.
73 //
74 if (EFI_ERROR (Status)) {
75 Status = PeiServicesFfsFindSectionData (
76 EFI_SECTION_TE,
77 PeimFileHeader,
78 (VOID **) &TEImageHeader
79 );
80 if (EFI_ERROR (Status) || TEImageHeader == NULL) {
81 //
82 // There was not a PE32 or a TE section, so assume that it's a Compressed section
83 // and use the LoadFile
84 //
85 Status = PeiServicesLocatePpi (
86 &gEfiPeiFvFileLoaderPpiGuid,
87 0,
88 NULL,
89 (VOID **)&FvLoadFilePpi
90 );
91 if (EFI_ERROR (Status)) {
92 return EFI_NOT_FOUND;
93 }
94
95 Status = FvLoadFilePpi->FvLoadFile (
96 FvLoadFilePpi,
97 PeimFileHeader,
98 &ImageAddress,
99 &ImageSize,
100 &ImageEntryPoint
101 );
102
103 if (EFI_ERROR (Status)) {
104 return EFI_NOT_FOUND;
105 }
106
107 //
108 // Got the entry point from ImageEntryPoint
109 //
110 *EntryPoint = (VOID *) ((UINTN) ImageEntryPoint);
111 return EFI_SUCCESS;
112 } else {
113 //
114 // Retrieve the entry point from the TE image header
115 //
116 ImageAddress = (EFI_PHYSICAL_ADDRESS) (UINTN) TEImageHeader;
117 *EntryPoint = (VOID *)((UINTN) TEImageHeader + sizeof (EFI_TE_IMAGE_HEADER) +
118 TEImageHeader->AddressOfEntryPoint - TEImageHeader->StrippedSize);
119 }
120 } else {
121 //
122 // Retrieve the entry point from the PE/COFF image header
123 //
124 ImageAddress = (EFI_PHYSICAL_ADDRESS) (UINTN) Pe32Data;
125 Status = PeCoffLoaderGetEntryPoint (Pe32Data, EntryPoint);
126 if (EFI_ERROR (Status)) {
127 return EFI_NOT_FOUND;
128 }
129 }
130
131 //
132 // Print debug message: Loading PEIM at 0x12345678 EntryPoint=0x12345688 Driver.efi
133 //
134 DEBUG ((EFI_D_INFO | EFI_D_LOAD, "Loading PEIM at 0x%08x EntryPoint=0x%08x ", (UINTN) ImageAddress, *EntryPoint));
135 DEBUG_CODE_BEGIN ();
136 EFI_IMAGE_DATA_DIRECTORY *DirectoryEntry;
137 EFI_IMAGE_DEBUG_DIRECTORY_ENTRY *DebugEntry;
138 UINTN DirCount;
139 UINTN Index;
140 UINTN Index1;
141 BOOLEAN FileNameFound;
142 CHAR8 *AsciiString;
143 CHAR8 AsciiBuffer[512];
144 VOID *CodeViewEntryPointer;
145 INTN TEImageAdjust;
146 EFI_IMAGE_DOS_HEADER *DosHeader;
147 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;
148 UINT32 NumberOfRvaAndSizes;
149
150 Hdr.Pe32 = NULL;
151 if (TEImageHeader == NULL) {
152 DosHeader = (EFI_IMAGE_DOS_HEADER *)Pe32Data;
153 if (DosHeader->e_magic == EFI_IMAGE_DOS_SIGNATURE) {
154 //
155 // DOS image header is present, so read the PE header after the DOS image header
156 //
157 Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)((UINTN)Pe32Data + (UINTN)((DosHeader->e_lfanew) & 0x0ffff));
158 } else {
159 //
160 // DOS image header is not present, so PE header is at the image base
161 //
162 Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)Pe32Data;
163 }
164 }
165
166 //
167 // Find the codeview info in the image and display the file name
168 // being loaded.
169 //
170 // Per the PE/COFF spec, you can't assume that a given data directory
171 // is present in the image. You have to check the NumberOfRvaAndSizes in
172 // the optional header to verify a desired directory entry is there.
173 //
174 DebugEntry = NULL;
175 DirectoryEntry = NULL;
176 TEImageAdjust = 0;
177 if (TEImageHeader == NULL) {
178 if (Hdr.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
179 //
180 // Use PE32 offset
181 //
182 NumberOfRvaAndSizes = Hdr.Pe32->OptionalHeader.NumberOfRvaAndSizes;
183 DirectoryEntry = (EFI_IMAGE_DATA_DIRECTORY *)&(Hdr.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG]);
184 } else {
185 //
186 // Use PE32+ offset
187 //
188 NumberOfRvaAndSizes = Hdr.Pe32Plus->OptionalHeader.NumberOfRvaAndSizes;
189 DirectoryEntry = (EFI_IMAGE_DATA_DIRECTORY *)&(Hdr.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG]);
190 }
191
192 if (NumberOfRvaAndSizes <= EFI_IMAGE_DIRECTORY_ENTRY_DEBUG) {
193 DirectoryEntry = NULL;
194 DebugEntry = NULL;
195 }
196 } else {
197 if (TEImageHeader->DataDirectory[EFI_TE_IMAGE_DIRECTORY_ENTRY_DEBUG].VirtualAddress != 0) {
198 DirectoryEntry = &TEImageHeader->DataDirectory[EFI_TE_IMAGE_DIRECTORY_ENTRY_DEBUG];
199 TEImageAdjust = sizeof (EFI_TE_IMAGE_HEADER) - TEImageHeader->StrippedSize;
200 DebugEntry = (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY *)((UINTN) TEImageHeader +
201 TEImageHeader->DataDirectory[EFI_TE_IMAGE_DIRECTORY_ENTRY_DEBUG].VirtualAddress +
202 TEImageAdjust);
203 }
204 }
205
206 if (DebugEntry != NULL && DirectoryEntry != NULL) {
207 for (DirCount = 0; DirCount < DirectoryEntry->Size; DirCount++, DebugEntry++) {
208 if (DebugEntry->Type == EFI_IMAGE_DEBUG_TYPE_CODEVIEW) {
209 if (DebugEntry->SizeOfData > 0) {
210 CodeViewEntryPointer = (VOID *) ((UINTN) DebugEntry->RVA + (UINTN) ImageAddress + (UINTN)TEImageAdjust);
211 switch (* (UINT32 *) CodeViewEntryPointer) {
212 case CODEVIEW_SIGNATURE_NB10:
213 AsciiString = (CHAR8 *)CodeViewEntryPointer + sizeof (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY);
214 break;
215
216 case CODEVIEW_SIGNATURE_RSDS:
217 AsciiString = (CHAR8 *)CodeViewEntryPointer + sizeof (EFI_IMAGE_DEBUG_CODEVIEW_RSDS_ENTRY);
218 break;
219
220 default:
221 AsciiString = NULL;
222 break;
223 }
224 if (AsciiString != NULL) {
225 FileNameFound = FALSE;
226 for (Index = 0, Index1 = 0; (AsciiString[Index] != 0) && (Index < sizeof (AsciiString)); Index++) {
227 if (AsciiString[Index] == '\\') {
228 Index1 = Index;
229 FileNameFound = TRUE;
230 }
231 }
232
233 if (FileNameFound) {
234 for (Index = Index1 + 1; AsciiString[Index] != '.'; Index++) {
235 AsciiBuffer[Index - (Index1 + 1)] = AsciiString[Index];
236 }
237 AsciiBuffer[Index - (Index1 + 1)] = 0;
238 DEBUG ((EFI_D_INFO | EFI_D_LOAD, "%a.efi", AsciiBuffer));
239 break;
240 }
241 }
242 }
243 }
244 }
245 }
246 DEBUG_CODE_END ();
247
248 DEBUG ((EFI_D_INFO | EFI_D_LOAD, "\n"));
249
250 return EFI_SUCCESS;
251 }