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