]> git.proxmox.com Git - mirror_edk2.git/blame - Nt32Pkg/Library/PeiNt32PeCoffExtraActionLib/PeiNt32PeCoffExtraActionLib.c
Add empty line
[mirror_edk2.git] / Nt32Pkg / Library / PeiNt32PeCoffExtraActionLib / PeiNt32PeCoffExtraActionLib.c
CommitLineData
ffdd18bb 1/**@file\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 PeiNt32PeCoffExtraActionLib.c\r
15\r
16Abstract:\r
17\r
18 Provides services to perform additional actions to relocate and unload\r
19 PE/Coff image for NT32 environment specific purpose such as souce level debug.\r
20 This version only works for PEI phase \r
21\r
22\r
23**/\r
24//\r
25// The package level header files this module uses\r
26//\r
27#include <FrameworkPei.h>\r
28#include <FrameworkModulePei.h>\r
29#include <WinNtPeim.h>\r
30\r
31//\r
32// The protocols, PPI and GUID defintions for this module\r
33//\r
34#include <Ppi/NtThunk.h>\r
35\r
36#include <PiPei.h>\r
37#include <Library/PeCoffLib.h>\r
38#include <Library/PeiServicesLib.h>\r
39#include <Library/DebugLib.h>\r
40#include <Library/BaseLib.h>\r
41#include <Library/PeCoffExtraActionLib.h>\r
42\r
43//\r
44// Cache of WinNtThunk protocol\r
45//\r
46EFI_WIN_NT_THUNK_PROTOCOL *mWinNt = NULL;\r
47\r
48/**\r
49 The function caches the pointer of the WinNT thunk functions\r
50 It will ASSERT() if NT thunk ppi is not installed.\r
51\r
52 @retval EFI_SUCCESS WinNT thunk protocol is found and cached.\r
53\r
54**/\r
55EFI_STATUS\r
56EFIAPI\r
57Nt32PeCoffGetWinNtThunkStucture (\r
58 )\r
59{\r
60 PEI_NT_THUNK_PPI *NtThunkPpi;\r
61 EFI_STATUS Status;\r
62\r
63\r
64 //\r
65 // Locate NtThunkPpi for retrieving standard output handle\r
66 //\r
67 Status = PeiServicesLocatePpi (\r
68 &gPeiNtThunkPpiGuid,\r
69 0,\r
70 NULL,\r
71 (VOID **) &NtThunkPpi\r
72 );\r
73\r
74 ASSERT_EFI_ERROR (Status);\r
75\r
76 mWinNt = (EFI_WIN_NT_THUNK_PROTOCOL *) NtThunkPpi->NtThunk ();\r
77 \r
78 return EFI_SUCCESS;\r
79}\r
80\r
81/**\r
82 Convert the passed in Ascii string to Unicode.\r
83 \r
84 This function Convert the passed in Ascii string to Unicode.Optionally return\r
85 the length of the strings..\r
86\r
87 @param AsciiString Pointer to an AscII string\r
88 @param StrLen Length of string\r
89\r
90 @return Pointer to malloc'ed Unicode version of Ascii\r
91\r
92**/\r
93CHAR16 *\r
94AsciiToUnicode (\r
95 IN CHAR8 *Ascii,\r
96 IN UINTN *StrLen OPTIONAL\r
97 )\r
98{\r
99 UINTN Index;\r
100 CHAR16 *Unicode;\r
101\r
102 //\r
103 // Allocate a buffer for unicode string\r
104 //\r
105 for (Index = 0; Ascii[Index] != '\0'; Index++)\r
106 ;\r
107 Unicode = mWinNt->HeapAlloc ( mWinNt->GetProcessHeap (),\r
108 HEAP_ZERO_MEMORY,\r
109 ((Index + 1) * sizeof (CHAR16))\r
110 ); \r
111 if (Unicode == NULL) {\r
112 return NULL;\r
113 }\r
114\r
115 for (Index = 0; Ascii[Index] != '\0'; Index++) {\r
116 Unicode[Index] = (CHAR16) Ascii[Index];\r
117 }\r
118\r
119 Unicode[Index] = '\0';\r
120\r
121 if (StrLen != NULL) {\r
122 *StrLen = Index;\r
123 }\r
124\r
125 return Unicode;\r
126}\r
127\r
128/**\r
129 Applies additional actions to relocate fixups to a PE/COFF image.\r
130\r
131 Generally this function is called after sucessfully Applying relocation fixups \r
132 to a PE/COFF image for some specicial purpose. \r
133 As a example, For NT32 emulator, the function should be implemented and called\r
134 to support source level debug. \r
135 \r
136 @param ImageContext Pointer to the image context structure that describes the PE/COFF\r
137 image that is being relocated.\r
138\r
139**/\r
140VOID\r
141EFIAPI\r
142PeCoffLoaderRelocateImageExtraAction (\r
143 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r
144 )\r
145{\r
146 VOID *DllEntryPoint;\r
147 CHAR16 *DllFileName;\r
148 HMODULE Library;\r
149 UINTN Index;\r
150 \r
151 if (mWinNt == NULL) {\r
152 Nt32PeCoffGetWinNtThunkStucture ();\r
153 }\r
154 //\r
155 // If we load our own PE COFF images the Windows debugger can not source\r
156 // level debug our code. If a valid PDB pointer exists usw it to load\r
157 // the *.dll file as a library using Windows* APIs. This allows \r
158 // source level debug. The image is still loaded and reloaced\r
159 // in the Framework memory space like on a real system (by the code above),\r
160 // but the entry point points into the DLL loaded by the code bellow. \r
161 //\r
162\r
163 DllEntryPoint = NULL;\r
164\r
165 //\r
166 // Load the DLL if it's not an EBC image.\r
167 //\r
168 if ((ImageContext->PdbPointer != NULL) &&\r
169 (ImageContext->Machine != EFI_IMAGE_MACHINE_EBC)) {\r
170 //\r
171 // Convert filename from ASCII to Unicode\r
172 //\r
173 DllFileName = AsciiToUnicode (ImageContext->PdbPointer, &Index);\r
174\r
175 //\r
176 // Check that we have a valid filename\r
177 //\r
178 if (Index < 5 || DllFileName[Index - 4] != '.') {\r
179 mWinNt->HeapFree (mWinNt->GetProcessHeap (), 0, DllFileName);\r
180\r
181 //\r
182 // Never return an error if PeCoffLoaderRelocateImage() succeeded.\r
183 // The image will run, but we just can't source level debug. If we\r
184 // return an error the image will not run.\r
185 //\r
186 return;\r
187 }\r
188 //\r
189 // Replace .PDB with .DLL on the filename\r
190 //\r
191 DllFileName[Index - 3] = 'D';\r
192 DllFileName[Index - 2] = 'L';\r
193 DllFileName[Index - 1] = 'L';\r
194\r
195 //\r
196 // Load the .DLL file into the user process's address space for source \r
197 // level debug\r
198 //\r
199 Library = mWinNt->LoadLibraryEx (DllFileName, NULL, DONT_RESOLVE_DLL_REFERENCES);\r
200 if (Library != NULL) {\r
201 //\r
202 // InitializeDriver is the entry point we put in all our EFI DLL's. The\r
203 // DONT_RESOLVE_DLL_REFERENCES argument to LoadLIbraryEx() supresses the \r
204 // normal DLL entry point of DllMain, and prevents other modules that are\r
205 // referenced in side the DllFileName from being loaded. There is no error \r
206 // checking as the we can point to the PE32 image loaded by Tiano. This \r
207 // step is only needed for source level debuging\r
208 //\r
209 DllEntryPoint = (VOID *) (UINTN) mWinNt->GetProcAddress (Library, "InitializeDriver");\r
210\r
211 }\r
212\r
213 if ((Library != NULL) && (DllEntryPoint != NULL)) {\r
214 ImageContext->EntryPoint = (EFI_PHYSICAL_ADDRESS) (UINTN) DllEntryPoint;\r
215 DEBUG ((EFI_D_INFO, "LoadLibraryEx (%s,\n NULL, DONT_RESOLVE_DLL_REFERENCES)\n", DllFileName));\r
216 } else {\r
217 DEBUG ((EFI_D_ERROR, "WARNING: No source level debug %s. \n", DllFileName));\r
218 }\r
219\r
220 mWinNt->HeapFree (mWinNt->GetProcessHeap (), 0, DllFileName);\r
221 }\r
222\r
223 //\r
224 // Never return an error if PeCoffLoaderRelocateImage() succeeded.\r
225 // The image will run, but we just can't source level debug. If we\r
226 // return an error the image will not run.\r
227 //\r
228 return;\r
229} \r
230\r
231/**\r
232 Unloads a loaded PE/COFF image from memory and releases its taken resource.\r
233 \r
234 Releases any environment specific resources that were allocated when the image \r
235 specified by ImageContext was loaded using PeCoffLoaderLoadImage(). \r
236 For NT32 emulator, the PE/COFF image loaded by system needs to release.\r
237 For real platform, the PE/COFF image loaded by Core doesn't needs to be unloaded, \r
238 \r
239 If ImageContext is NULL, then ASSERT().\r
240 \r
241 @param ImageContext Pointer to the image context structure that describes the PE/COFF\r
242 image to be unloaded.\r
243\r
244**/\r
245VOID\r
246EFIAPI\r
247PeCoffLoaderUnloadImageExtraAction (\r
248 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r
249 )\r
250{\r
ee31b443 251}\r