]> git.proxmox.com Git - mirror_edk2.git/blob - EdkModulePkg/Core/Pei/Image/Image.c
Remove autogen.h from all dxs files, because autogen.h file has been included by...
[mirror_edk2.git] / EdkModulePkg / Core / Pei / Image / Image.c
1 /*++
2
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
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 UINT16 Machine;
60
61 *EntryPoint = NULL;
62 TEImageHeader = NULL;
63
64 //
65 // Try to find a PE32 section.
66 //
67 Status = PeiServicesFfsFindSectionData (
68 EFI_SECTION_PE32,
69 PeimFileHeader,
70 &Pe32Data
71 );
72 //
73 // If we didn't find a PE32 section, try to find a TE section.
74 //
75 if (EFI_ERROR (Status)) {
76 Status = PeiServicesFfsFindSectionData (
77 EFI_SECTION_TE,
78 PeimFileHeader,
79 (VOID **) &TEImageHeader
80 );
81 if (EFI_ERROR (Status) || TEImageHeader == NULL) {
82 //
83 // There was not a PE32 or a TE section, so assume that it's a Compressed section
84 // and use the LoadFile
85 //
86 Status = PeiServicesLocatePpi (
87 &gEfiPeiFvFileLoaderPpiGuid,
88 0,
89 NULL,
90 (VOID **)&FvLoadFilePpi
91 );
92 if (EFI_ERROR (Status)) {
93 return EFI_NOT_FOUND;
94 }
95
96 Status = FvLoadFilePpi->FvLoadFile (
97 FvLoadFilePpi,
98 PeimFileHeader,
99 &ImageAddress,
100 &ImageSize,
101 &ImageEntryPoint
102 );
103
104 if (EFI_ERROR (Status)) {
105 return EFI_NOT_FOUND;
106 }
107
108 //
109 // Got the entry point from ImageEntryPoint and ImageStartAddress
110 //
111 Pe32Data = (VOID *) ((UINTN) ImageAddress);
112 *EntryPoint = (VOID *) ((UINTN) ImageEntryPoint);
113 } else {
114 //
115 // Retrieve the entry point from the TE image header
116 //
117 ImageAddress = (EFI_PHYSICAL_ADDRESS) (UINTN) TEImageHeader;
118 *EntryPoint = (VOID *)((UINTN) TEImageHeader + sizeof (EFI_TE_IMAGE_HEADER) +
119 TEImageHeader->AddressOfEntryPoint - TEImageHeader->StrippedSize);
120 }
121 } else {
122 //
123 // Retrieve the entry point from the PE/COFF image header
124 //
125 ImageAddress = (EFI_PHYSICAL_ADDRESS) (UINTN) Pe32Data;
126 Status = PeCoffLoaderGetEntryPoint (Pe32Data, EntryPoint);
127 if (EFI_ERROR (Status)) {
128 return EFI_NOT_FOUND;
129 }
130 }
131
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;
135 } else {
136 Machine = PeCoffLoaderGetMachineType (Pe32Data);
137 }
138
139 if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Machine)) {
140 return EFI_UNSUPPORTED;
141 }
142
143 //
144 // Print debug message: Loading PEIM at 0x12345678 EntryPoint=0x12345688 Driver.efi
145 //
146 DEBUG ((EFI_D_INFO | EFI_D_LOAD, "Loading PEIM at 0x%08x EntryPoint=0x%08x ", (UINTN) ImageAddress, *EntryPoint));
147 DEBUG_CODE_BEGIN ();
148 EFI_IMAGE_DATA_DIRECTORY *DirectoryEntry;
149 EFI_IMAGE_DEBUG_DIRECTORY_ENTRY *DebugEntry;
150 UINTN DirCount;
151 UINTN Index;
152 UINTN Index1;
153 BOOLEAN FileNameFound;
154 CHAR8 *AsciiString;
155 CHAR8 AsciiBuffer[512];
156 VOID *CodeViewEntryPointer;
157 INTN TEImageAdjust;
158 EFI_IMAGE_DOS_HEADER *DosHeader;
159 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;
160 UINT32 NumberOfRvaAndSizes;
161
162 Hdr.Pe32 = NULL;
163 if (TEImageHeader == NULL) {
164 DosHeader = (EFI_IMAGE_DOS_HEADER *)Pe32Data;
165 if (DosHeader->e_magic == EFI_IMAGE_DOS_SIGNATURE) {
166 //
167 // DOS image header is present, so read the PE header after the DOS image header
168 //
169 Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)((UINTN)Pe32Data + (UINTN)((DosHeader->e_lfanew) & 0x0ffff));
170 } else {
171 //
172 // DOS image header is not present, so PE header is at the image base
173 //
174 Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)Pe32Data;
175 }
176 }
177
178 //
179 // Find the codeview info in the image and display the file name
180 // being loaded.
181 //
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.
185 //
186 DebugEntry = NULL;
187 DirectoryEntry = NULL;
188 NumberOfRvaAndSizes = 0;
189 TEImageAdjust = 0;
190
191 if (TEImageHeader == NULL) {
192 if (Hdr.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
193 //
194 // Use PE32 offset get Debug Directory Entry
195 //
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) {
200 //
201 // Use PE32+ offset get Debug Directory Entry
202 //
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);
206 }
207
208 if (NumberOfRvaAndSizes <= EFI_IMAGE_DIRECTORY_ENTRY_DEBUG) {
209 DirectoryEntry = NULL;
210 DebugEntry = NULL;
211 }
212 } else {
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 +
218 TEImageAdjust);
219 }
220 }
221
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);
230 break;
231
232 case CODEVIEW_SIGNATURE_RSDS:
233 AsciiString = (CHAR8 *)CodeViewEntryPointer + sizeof (EFI_IMAGE_DEBUG_CODEVIEW_RSDS_ENTRY);
234 break;
235
236 default:
237 AsciiString = NULL;
238 break;
239 }
240 if (AsciiString != NULL) {
241 FileNameFound = FALSE;
242 for (Index = 0, Index1 = 0; AsciiString[Index] != '\0'; Index++) {
243 if (AsciiString[Index] == '\\') {
244 Index1 = Index;
245 FileNameFound = TRUE;
246 }
247 }
248
249 if (FileNameFound) {
250 for (Index = Index1 + 1; AsciiString[Index] != '.'; Index++) {
251 AsciiBuffer[Index - (Index1 + 1)] = AsciiString[Index];
252 }
253 AsciiBuffer[Index - (Index1 + 1)] = 0;
254 DEBUG ((EFI_D_INFO | EFI_D_LOAD, "%a.efi", AsciiBuffer));
255 break;
256 }
257 }
258 }
259 }
260 }
261 }
262 DEBUG_CODE_END ();
263
264 DEBUG ((EFI_D_INFO | EFI_D_LOAD, "\n"));
265
266 return EFI_SUCCESS;
267 }