]> git.proxmox.com Git - mirror_edk2.git/blame - Nt32Pkg/Library/PeiNt32PeCoffExtraActionLib/PeiNt32PeCoffExtraActionLib.c
Retire <FrameworkModuleBase.h>, <FrameworkModuleDxe.h>, and <FrameworkModulePei.h>
[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
51f1a2b5 129 Performs additional actions after a PE/COFF image has been loaded and relocated.\r
ffdd18bb 130\r
51f1a2b5 131 For NT32, this function load symbols to support source level debugging.\r
132\r
133 If ImageContext is NULL, then ASSERT().\r
134\r
135 @param ImageContext Pointer to the image context structure that describes the\r
136 PE/COFF image that has already been loaded and relocated.\r
ffdd18bb 137\r
138**/\r
139VOID\r
140EFIAPI\r
141PeCoffLoaderRelocateImageExtraAction (\r
142 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r
143 )\r
144{\r
145 VOID *DllEntryPoint;\r
146 CHAR16 *DllFileName;\r
147 HMODULE Library;\r
148 UINTN Index;\r
149 \r
51f1a2b5 150 ASSERT (ImageContext != NULL);\r
151\r
ffdd18bb 152 if (mWinNt == NULL) {\r
153 Nt32PeCoffGetWinNtThunkStucture ();\r
154 }\r
155 //\r
156 // If we load our own PE COFF images the Windows debugger can not source\r
157 // level debug our code. If a valid PDB pointer exists usw it to load\r
158 // the *.dll file as a library using Windows* APIs. This allows \r
159 // source level debug. The image is still loaded and reloaced\r
160 // in the Framework memory space like on a real system (by the code above),\r
161 // but the entry point points into the DLL loaded by the code bellow. \r
162 //\r
163\r
164 DllEntryPoint = NULL;\r
165\r
166 //\r
167 // Load the DLL if it's not an EBC image.\r
168 //\r
169 if ((ImageContext->PdbPointer != NULL) &&\r
170 (ImageContext->Machine != EFI_IMAGE_MACHINE_EBC)) {\r
171 //\r
172 // Convert filename from ASCII to Unicode\r
173 //\r
174 DllFileName = AsciiToUnicode (ImageContext->PdbPointer, &Index);\r
175\r
176 //\r
177 // Check that we have a valid filename\r
178 //\r
179 if (Index < 5 || DllFileName[Index - 4] != '.') {\r
180 mWinNt->HeapFree (mWinNt->GetProcessHeap (), 0, DllFileName);\r
181\r
182 //\r
183 // Never return an error if PeCoffLoaderRelocateImage() succeeded.\r
184 // The image will run, but we just can't source level debug. If we\r
185 // return an error the image will not run.\r
186 //\r
187 return;\r
188 }\r
189 //\r
190 // Replace .PDB with .DLL on the filename\r
191 //\r
192 DllFileName[Index - 3] = 'D';\r
193 DllFileName[Index - 2] = 'L';\r
194 DllFileName[Index - 1] = 'L';\r
195\r
196 //\r
197 // Load the .DLL file into the user process's address space for source \r
198 // level debug\r
199 //\r
200 Library = mWinNt->LoadLibraryEx (DllFileName, NULL, DONT_RESOLVE_DLL_REFERENCES);\r
201 if (Library != NULL) {\r
202 //\r
203 // InitializeDriver is the entry point we put in all our EFI DLL's. The\r
204 // DONT_RESOLVE_DLL_REFERENCES argument to LoadLIbraryEx() supresses the \r
205 // normal DLL entry point of DllMain, and prevents other modules that are\r
206 // referenced in side the DllFileName from being loaded. There is no error \r
207 // checking as the we can point to the PE32 image loaded by Tiano. This \r
208 // step is only needed for source level debuging\r
209 //\r
210 DllEntryPoint = (VOID *) (UINTN) mWinNt->GetProcAddress (Library, "InitializeDriver");\r
211\r
212 }\r
213\r
214 if ((Library != NULL) && (DllEntryPoint != NULL)) {\r
215 ImageContext->EntryPoint = (EFI_PHYSICAL_ADDRESS) (UINTN) DllEntryPoint;\r
216 DEBUG ((EFI_D_INFO, "LoadLibraryEx (%s,\n NULL, DONT_RESOLVE_DLL_REFERENCES)\n", DllFileName));\r
217 } else {\r
218 DEBUG ((EFI_D_ERROR, "WARNING: No source level debug %s. \n", DllFileName));\r
219 }\r
220\r
221 mWinNt->HeapFree (mWinNt->GetProcessHeap (), 0, DllFileName);\r
222 }\r
223\r
224 //\r
225 // Never return an error if PeCoffLoaderRelocateImage() succeeded.\r
226 // The image will run, but we just can't source level debug. If we\r
227 // return an error the image will not run.\r
228 //\r
229 return;\r
230} \r
231\r
232/**\r
51f1a2b5 233 Performs additional actions just before a PE/COFF image is unloaded. Any resources\r
234 that were allocated by PeCoffLoaderRelocateImageExtraAction() must be freed.\r
ffdd18bb 235 \r
51f1a2b5 236 For NT32, this function unloads symbols for source level debugging.\r
237\r
ffdd18bb 238 If ImageContext is NULL, then ASSERT().\r
239 \r
51f1a2b5 240 @param ImageContext Pointer to the image context structure that describes the\r
241 PE/COFF image that is being unloaded.\r
ffdd18bb 242\r
243**/\r
244VOID\r
245EFIAPI\r
246PeCoffLoaderUnloadImageExtraAction (\r
247 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r
248 )\r
249{\r
51f1a2b5 250 ASSERT (ImageContext != NULL);\r
ee31b443 251}\r