]> git.proxmox.com Git - mirror_edk2.git/blame - EdkModulePkg/Core/Pei/Image/Image.c
Added back safety check for PE/COFF image walking. I think the safety check is broken...
[mirror_edk2.git] / EdkModulePkg / Core / Pei / Image / Image.c
CommitLineData
a0b7c09f 1/*++\r
2\r
3Copyright (c) 2006, Intel Corporation \r
4All rights reserved. This program and the accompanying materials \r
5are licensed and made available under the terms and conditions of the BSD License \r
6which accompanies this distribution. The full text of the license may be found at \r
7http://opensource.org/licenses/bsd-license.php \r
8 \r
9THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
10WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
11\r
12Module Name:\r
13\r
14 Image.c\r
15\r
16Abstract:\r
17\r
18 Pei Core Load Image Support\r
19\r
20--*/\r
21\r
22#include <PeiMain.h>\r
23\r
2ce31132 24\r
25\r
a0b7c09f 26EFI_STATUS\r
27PeiLoadImage (\r
28 IN EFI_PEI_SERVICES **PeiServices,\r
29 IN EFI_FFS_FILE_HEADER *PeimFileHeader,\r
30 OUT VOID **EntryPoint\r
31 )\r
32/*++\r
33\r
34Routine Description:\r
35\r
36 Routine for loading file image.\r
37\r
38Arguments:\r
39\r
40 PeiServices - The PEI core services table.\r
41 PeimFileHeader - Pointer to the FFS file header of the image.\r
42 EntryPoint - Pointer to entry point of specified image file for output.\r
43\r
44Returns:\r
45\r
46 Status - EFI_SUCCESS - Image is successfully loaded.\r
47 EFI_NOT_FOUND - Fail to locate necessary PPI\r
48 Others - Fail to load file.\r
49\r
50--*/\r
51{\r
52 EFI_STATUS Status;\r
53 VOID *Pe32Data;\r
54 EFI_PEI_FV_FILE_LOADER_PPI *FvLoadFilePpi;\r
a0b7c09f 55 EFI_PHYSICAL_ADDRESS ImageAddress;\r
56 UINT64 ImageSize;\r
57 EFI_PHYSICAL_ADDRESS ImageEntryPoint;\r
58 EFI_TE_IMAGE_HEADER *TEImageHeader;\r
59\r
60 *EntryPoint = NULL;\r
61 TEImageHeader = NULL;\r
62\r
63 //\r
64 // Try to find a PE32 section.\r
65 //\r
84a99d48 66 Status = PeiServicesFfsFindSectionData (\r
a0b7c09f 67 EFI_SECTION_PE32,\r
68 PeimFileHeader,\r
69 &Pe32Data\r
70 );\r
71 //\r
72 // If we didn't find a PE32 section, try to find a TE section.\r
73 //\r
74 if (EFI_ERROR (Status)) {\r
84a99d48 75 Status = PeiServicesFfsFindSectionData (\r
a0b7c09f 76 EFI_SECTION_TE,\r
77 PeimFileHeader,\r
78 (VOID **) &TEImageHeader\r
79 );\r
80 if (EFI_ERROR (Status) || TEImageHeader == NULL) {\r
81 //\r
82 // There was not a PE32 or a TE section, so assume that it's a Compressed section\r
83 // and use the LoadFile\r
84 //\r
84a99d48 85 Status = PeiServicesLocatePpi (\r
a0b7c09f 86 &gEfiPeiFvFileLoaderPpiGuid,\r
87 0,\r
88 NULL,\r
89 (VOID **)&FvLoadFilePpi\r
90 );\r
91 if (EFI_ERROR (Status)) {\r
92 return EFI_NOT_FOUND;\r
93 }\r
94\r
95 Status = FvLoadFilePpi->FvLoadFile (\r
96 FvLoadFilePpi,\r
97 PeimFileHeader,\r
98 &ImageAddress,\r
99 &ImageSize,\r
100 &ImageEntryPoint\r
101 );\r
102\r
103 if (EFI_ERROR (Status)) {\r
104 return EFI_NOT_FOUND;\r
105 }\r
106\r
107 //\r
2d10530b 108 // Got the entry point from ImageEntryPoint\r
a0b7c09f 109 //\r
2d10530b
LG
110 *EntryPoint = (VOID *) ((UINTN) ImageEntryPoint);\r
111 return EFI_SUCCESS;\r
a0b7c09f 112 } else {\r
113 //\r
114 // Retrieve the entry point from the TE image header\r
115 //\r
116 ImageAddress = (EFI_PHYSICAL_ADDRESS) (UINTN) TEImageHeader;\r
117 *EntryPoint = (VOID *)((UINTN) TEImageHeader + sizeof (EFI_TE_IMAGE_HEADER) +\r
118 TEImageHeader->AddressOfEntryPoint - TEImageHeader->StrippedSize);\r
119 }\r
120 } else {\r
121 //\r
122 // Retrieve the entry point from the PE/COFF image header\r
123 //\r
124 ImageAddress = (EFI_PHYSICAL_ADDRESS) (UINTN) Pe32Data;\r
125 Status = PeCoffLoaderGetEntryPoint (Pe32Data, EntryPoint);\r
126 if (EFI_ERROR (Status)) {\r
127 return EFI_NOT_FOUND;\r
128 }\r
129 }\r
130\r
131 //\r
132 // Print debug message: Loading PEIM at 0x12345678 EntryPoint=0x12345688 Driver.efi\r
133 //\r
134 DEBUG ((EFI_D_INFO | EFI_D_LOAD, "Loading PEIM at 0x%08x EntryPoint=0x%08x ", Pe32Data, *EntryPoint));\r
2ce31132 135 DEBUG_CODE_BEGIN ();\r
136 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;\r
137 UINTN Index;\r
138 CHAR8 *PdbStr;\r
139 CHAR8 AsciiBuffer[512];\r
a0b7c09f 140\r
a0b7c09f 141\r
2ce31132 142 ZeroMem (&ImageContext, sizeof (ImageContext));\r
143 ImageContext.Handle = Pe32Data;\r
144 ImageContext.ImageRead = PeCoffLoaderImageReadFromMemory;\r
a0b7c09f 145\r
2ce31132 146 PeCoffLoaderGetImageInfo (&ImageContext);\r
147 \r
148 if (ImageContext.PdbPointer != NULL) {\r
149 //\r
150 // Copy PDB pointer to AsciiBuffer and replace .PDB with .EFI\r
151 //\r
152 PdbStr = ImageContext.PdbPointer;\r
153 for (Index = 0; PdbStr != 0; Index++, PdbStr++) {\r
154 AsciiBuffer[Index] = *PdbStr;\r
155 if (*PdbStr == '.') {\r
156 AsciiBuffer[Index] = '\0'; \r
a0b7c09f 157 }\r
158 }\r
2ce31132 159 \r
160 DEBUG ((EFI_D_INFO | EFI_D_LOAD, "%a.efi", AsciiBuffer));\r
a0b7c09f 161 }\r
2ce31132 162\r
163 DEBUG_CODE_END ();\r
a0b7c09f 164\r
165 DEBUG ((EFI_D_INFO | EFI_D_LOAD, "\n"));\r
166\r
167 return EFI_SUCCESS;\r
168}\r