]> git.proxmox.com Git - mirror_edk2.git/blame - Nt32Pkg/Library/DxeNt32PeCoffExtraActionLib/DxeNt32PeCoffExtraActionLib.c
Do not set gBS to NULL when the ExitBootServices event notification for the UEFI...
[mirror_edk2.git] / Nt32Pkg / Library / DxeNt32PeCoffExtraActionLib / DxeNt32PeCoffExtraActionLib.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 DXE phase \r
21\r
22\r
23**/\r
24//\r
25// The package level header files this module uses\r
26//\r
27#include <FrameworkDxe.h>\r
ffdd18bb 28#include <WinNtDxe.h>\r
29\r
30//\r
31// The protocols, PPI and GUID defintions for this module\r
32//\r
33#include <Protocol/WinNtThunk.h>\r
34\r
35#include <Library/PeCoffLib.h>\r
ffdd18bb 36\r
37#include <Library/BaseLib.h>\r
38#include <Library/DebugLib.h>\r
39#include <Library/HobLib.h>\r
40#include <Library/BaseMemoryLib.h>\r
41#include <Library/PeCoffExtraActionLib.h>\r
42\r
43#define MAX_PDB_NAME_TO_MOD_HANDLE_ARRAY_SIZE 0x100\r
44\r
45typedef struct {\r
46 CHAR8 *PdbPointer;\r
47 VOID *ModHandle;\r
48} PDB_NAME_TO_MOD_HANDLE;\r
49\r
50\r
51//\r
52// Cache of WinNtThunk protocol\r
53//\r
54EFI_WIN_NT_THUNK_PROTOCOL *mWinNt = NULL;\r
55\r
56//\r
57// An Array to hold the ModHandle\r
58//\r
59PDB_NAME_TO_MOD_HANDLE *mPdbNameModHandleArray = NULL;\r
60UINTN mPdbNameModHandleArraySize = 0;\r
61\r
62\r
63/**\r
64 The constructor function gets the pointer of the WinNT thunk functions\r
65 It will ASSERT() if NT thunk protocol is not installed.\r
66\r
67 @retval EFI_SUCCESS WinNT thunk protocol is found and cached.\r
68\r
69**/\r
70EFI_STATUS\r
71EFIAPI\r
72DxeNt32PeCoffLibExtraActionConstructor (\r
73 IN EFI_HANDLE ImageHandle,\r
74 IN EFI_SYSTEM_TABLE *SystemTable\r
75 )\r
76{\r
77 EFI_HOB_GUID_TYPE *GuidHob;\r
78\r
79 //\r
80 // Retrieve WinNtThunkProtocol from GUID'ed HOB\r
81 //\r
82 GuidHob = GetFirstGuidHob (&gEfiWinNtThunkProtocolGuid);\r
83 ASSERT (GuidHob != NULL);\r
84 mWinNt = (EFI_WIN_NT_THUNK_PROTOCOL *)(*(UINTN *)(GET_GUID_HOB_DATA (GuidHob)));\r
85 ASSERT (mWinNt != NULL);\r
86\r
87\r
88 return EFI_SUCCESS;\r
89}\r
90\r
91/**\r
92 Convert the passed in Ascii string to Unicode.\r
93 \r
94 This function Convert the passed in Ascii string to Unicode.Optionally return\r
95 the length of the strings..\r
96\r
97 @param AsciiString Pointer to an AscII string\r
98 @param StrLen Length of string\r
99\r
100 @return Pointer to malloc'ed Unicode version of Ascii\r
101\r
102**/\r
103CHAR16 *\r
104AsciiToUnicode (\r
105 IN CHAR8 *Ascii,\r
106 IN UINTN *StrLen OPTIONAL\r
107 )\r
108{\r
109 UINTN Index;\r
110 CHAR16 *Unicode;\r
111\r
112 //\r
113 // Allocate a buffer for unicode string\r
114 //\r
115 for (Index = 0; Ascii[Index] != '\0'; Index++)\r
116 ;\r
117 Unicode = mWinNt->HeapAlloc ( mWinNt->GetProcessHeap (),\r
118 HEAP_ZERO_MEMORY,\r
119 ((Index + 1) * sizeof (CHAR16))\r
120 ); \r
121 if (Unicode == NULL) {\r
122 return NULL;\r
123 }\r
124\r
125 for (Index = 0; Ascii[Index] != '\0'; Index++) {\r
126 Unicode[Index] = (CHAR16) Ascii[Index];\r
127 }\r
128\r
129 Unicode[Index] = '\0';\r
130\r
131 if (StrLen != NULL) {\r
132 *StrLen = Index;\r
133 }\r
134\r
135 return Unicode;\r
136}\r
137/**\r
138 Store the ModHandle in an array indexed by the Pdb File name.\r
139 The ModHandle is needed to unload the image. \r
140\r
141\r
142 @param ImageContext - Input data returned from PE Laoder Library. Used to find the \r
143 .PDB file name of the PE Image.\r
144 @param ModHandle - Returned from LoadLibraryEx() and stored for call to \r
145 FreeLibrary().\r
146\r
147 @return return EFI_SUCCESS when ModHandle was stored. \r
148\r
149--*/\r
150EFI_STATUS\r
151AddModHandle (\r
152 IN PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext,\r
153 IN VOID *ModHandle\r
154 )\r
155\r
156{\r
157 UINTN Index;\r
158 PDB_NAME_TO_MOD_HANDLE *Array;\r
159 UINTN PreviousSize;\r
160 PDB_NAME_TO_MOD_HANDLE *TempArray;\r
a00ec39b 161 HANDLE Handle;\r
ffdd18bb 162\r
163 Array = mPdbNameModHandleArray;\r
164 for (Index = 0; Index < mPdbNameModHandleArraySize; Index++, Array++) {\r
165 if (Array->PdbPointer == NULL) {\r
166 //\r
167 // Make a copy of the stirng and store the ModHandle\r
168 //\r
a00ec39b 169 Handle = mWinNt->GetProcessHeap ();\r
170 Array->PdbPointer = mWinNt->HeapAlloc ( Handle,\r
ffdd18bb 171 HEAP_ZERO_MEMORY,\r
172 AsciiStrLen (ImageContext->PdbPointer) + 1\r
173 ); \r
174 \r
175 ASSERT (Array->PdbPointer != NULL);\r
176\r
177 AsciiStrCpy (Array->PdbPointer, ImageContext->PdbPointer);\r
178 Array->ModHandle = ModHandle;\r
179 return EFI_SUCCESS;\r
180 }\r
181 }\r
182 \r
183 //\r
184 // No free space in mPdbNameModHandleArray so grow it by \r
185 // MAX_PDB_NAME_TO_MOD_HANDLE_ARRAY_SIZE entires. \r
186 //\r
187 PreviousSize = mPdbNameModHandleArraySize * sizeof (PDB_NAME_TO_MOD_HANDLE);\r
188 mPdbNameModHandleArraySize += MAX_PDB_NAME_TO_MOD_HANDLE_ARRAY_SIZE;\r
189 //\r
190 // re-allocate a new buffer and copy the old values to the new locaiton. \r
191 //\r
192 TempArray = mWinNt->HeapAlloc ( mWinNt->GetProcessHeap (),\r
193 HEAP_ZERO_MEMORY,\r
194 mPdbNameModHandleArraySize * sizeof (PDB_NAME_TO_MOD_HANDLE)\r
195 ); \r
196 \r
197 CopyMem ((VOID *) (UINTN) TempArray, (VOID *) (UINTN)mPdbNameModHandleArray, PreviousSize);\r
198 \r
199 mWinNt->HeapFree (mWinNt->GetProcessHeap (), 0, mPdbNameModHandleArray);\r
200 \r
201 mPdbNameModHandleArray = TempArray;\r
202 if (mPdbNameModHandleArray == NULL) {\r
203 ASSERT (FALSE);\r
204 return EFI_OUT_OF_RESOURCES;\r
205 }\r
206 \r
207 \r
208 return AddModHandle (ImageContext, ModHandle);\r
209}\r
210/**\r
211 Return the ModHandle and delete the entry in the array.\r
212\r
213\r
214 @param ImageContext - Input data returned from PE Laoder Library. Used to find the \r
215 .PDB file name of the PE Image.\r
216\r
217 @return \r
218 ModHandle - ModHandle assoicated with ImageContext is returned\r
219 NULL - No ModHandle associated with ImageContext\r
220\r
221**/\r
222VOID *\r
223RemoveModeHandle (\r
224 IN PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r
225 )\r
226{\r
227 UINTN Index;\r
228 PDB_NAME_TO_MOD_HANDLE *Array;\r
229\r
230 if (ImageContext->PdbPointer == NULL) {\r
231 //\r
232 // If no PDB pointer there is no ModHandle so return NULL\r
233 //\r
234 return NULL;\r
235 }\r
236\r
237 Array = mPdbNameModHandleArray;\r
238 for (Index = 0; Index < mPdbNameModHandleArraySize; Index++, Array++) {\r
239 if ((Array->PdbPointer != NULL) && (AsciiStrCmp(Array->PdbPointer, ImageContext->PdbPointer) == 0)) {\r
240 //\r
241 // If you find a match return it and delete the entry\r
242 //\r
243 mWinNt->HeapFree (mWinNt->GetProcessHeap (), 0, Array->PdbPointer);\r
244 Array->PdbPointer = NULL;\r
245 return Array->ModHandle;\r
246 }\r
247 }\r
248\r
249 return NULL;\r
250}\r
251\r
252/**\r
51f1a2b5 253 Performs additional actions after a PE/COFF image has been loaded and relocated.\r
ffdd18bb 254\r
51f1a2b5 255 For NT32, this function load symbols to support source level debugging.\r
256\r
257 If ImageContext is NULL, then ASSERT().\r
258\r
259 @param ImageContext Pointer to the image context structure that describes the\r
260 PE/COFF image that has already been loaded and relocated.\r
ffdd18bb 261\r
262**/\r
263VOID\r
264EFIAPI\r
265PeCoffLoaderRelocateImageExtraAction (\r
266 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r
267 )\r
268{\r
269 VOID *DllEntryPoint;\r
270 CHAR16 *DllFileName;\r
271 HMODULE Library;\r
272 UINTN Index;\r
51f1a2b5 273\r
274 ASSERT (ImageContext != NULL);\r
275\r
ffdd18bb 276 //\r
277 // If we load our own PE COFF images the Windows debugger can not source\r
278 // level debug our code. If a valid PDB pointer exists usw it to load\r
279 // the *.dll file as a library using Windows* APIs. This allows \r
280 // source level debug. The image is still loaded and reloaced\r
281 // in the Framework memory space like on a real system (by the code above),\r
282 // but the entry point points into the DLL loaded by the code bellow. \r
283 //\r
284\r
285 DllEntryPoint = NULL;\r
286\r
287 //\r
288 // Load the DLL if it's not an EBC image.\r
289 //\r
290 if ((ImageContext->PdbPointer != NULL) &&\r
291 (ImageContext->Machine != EFI_IMAGE_MACHINE_EBC)) {\r
292 //\r
293 // Convert filename from ASCII to Unicode\r
294 //\r
295 DllFileName = AsciiToUnicode (ImageContext->PdbPointer, &Index);\r
296\r
297 //\r
298 // Check that we have a valid filename\r
299 //\r
300 if (Index < 5 || DllFileName[Index - 4] != '.') {\r
301 mWinNt->HeapFree (mWinNt->GetProcessHeap (), 0, DllFileName);\r
302\r
303 //\r
304 // Never return an error if PeCoffLoaderRelocateImage() succeeded.\r
305 // The image will run, but we just can't source level debug. If we\r
306 // return an error the image will not run.\r
307 //\r
308 return;\r
309 }\r
310 //\r
311 // Replace .PDB with .DLL on the filename\r
312 //\r
313 DllFileName[Index - 3] = 'D';\r
314 DllFileName[Index - 2] = 'L';\r
315 DllFileName[Index - 1] = 'L';\r
316\r
317 //\r
318 // Load the .DLL file into the user process's address space for source \r
319 // level debug\r
320 //\r
321 Library = mWinNt->LoadLibraryEx (DllFileName, NULL, DONT_RESOLVE_DLL_REFERENCES);\r
322 if (Library != NULL) {\r
323 //\r
324 // InitializeDriver is the entry point we put in all our EFI DLL's. The\r
325 // DONT_RESOLVE_DLL_REFERENCES argument to LoadLIbraryEx() supresses the \r
326 // normal DLL entry point of DllMain, and prevents other modules that are\r
327 // referenced in side the DllFileName from being loaded. There is no error \r
328 // checking as the we can point to the PE32 image loaded by Tiano. This \r
329 // step is only needed for source level debuging\r
330 //\r
331 DllEntryPoint = (VOID *) (UINTN) mWinNt->GetProcAddress (Library, "InitializeDriver");\r
332\r
333 }\r
334\r
335 if ((Library != NULL) && (DllEntryPoint != NULL)) {\r
336 AddModHandle (ImageContext, Library);\r
337 ImageContext->EntryPoint = (EFI_PHYSICAL_ADDRESS) (UINTN) DllEntryPoint;\r
338 DEBUG ((EFI_D_INFO, "LoadLibraryEx (%s,\n NULL, DONT_RESOLVE_DLL_REFERENCES)\n", DllFileName));\r
339 } else {\r
340 DEBUG ((EFI_D_ERROR, "WARNING: No source level debug %s. \n", DllFileName));\r
341 }\r
342\r
343 mWinNt->HeapFree (mWinNt->GetProcessHeap (), 0, DllFileName);\r
344 }\r
345\r
346 //\r
347 // Never return an error if PeCoffLoaderRelocateImage() succeeded.\r
348 // The image will run, but we just can't source level debug. If we\r
349 // return an error the image will not run.\r
350 //\r
351 return;\r
352} \r
353\r
354/**\r
51f1a2b5 355 Performs additional actions just before a PE/COFF image is unloaded. Any resources\r
356 that were allocated by PeCoffLoaderRelocateImageExtraAction() must be freed.\r
ffdd18bb 357 \r
51f1a2b5 358 For NT32, this function unloads symbols for source level debugging.\r
359\r
ffdd18bb 360 If ImageContext is NULL, then ASSERT().\r
361 \r
51f1a2b5 362 @param ImageContext Pointer to the image context structure that describes the\r
363 PE/COFF image that is being unloaded.\r
ffdd18bb 364\r
365**/\r
366VOID\r
367EFIAPI\r
368PeCoffLoaderUnloadImageExtraAction (\r
369 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r
370 )\r
371{\r
372 VOID *ModHandle;\r
373\r
51f1a2b5 374 ASSERT (ImageContext != NULL);\r
375\r
ffdd18bb 376 ModHandle = RemoveModeHandle (ImageContext);\r
377 if (ModHandle != NULL) {\r
378 mWinNt->FreeLibrary (ModHandle);\r
379 }\r
380 return;\r
ee31b443 381}\r