]> git.proxmox.com Git - mirror_edk2.git/blame - Nt32Pkg/Library/PeiNt32PeCoffExtraActionLib/PeiNt32PeCoffExtraActionLib.c
Nt32Pkg: Platform BDS should test the untested memory
[mirror_edk2.git] / Nt32Pkg / Library / PeiNt32PeCoffExtraActionLib / PeiNt32PeCoffExtraActionLib.c
CommitLineData
ffdd18bb 1/**@file\r
2\r
8f2a5f80
HT
3Copyright (c) 2006, 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 PEI phase \r
21\r
22\r
23**/\r
24//\r
25// The package level header files this module uses\r
26//\r
5e335a52 27#include <PiPei.h>\r
ffdd18bb 28#include <WinNtPeim.h>\r
29\r
30//\r
31// The protocols, PPI and GUID defintions for this module\r
32//\r
33#include <Ppi/NtThunk.h>\r
34\r
35#include <PiPei.h>\r
36#include <Library/PeCoffLib.h>\r
37#include <Library/PeiServicesLib.h>\r
38#include <Library/DebugLib.h>\r
39#include <Library/BaseLib.h>\r
40#include <Library/PeCoffExtraActionLib.h>\r
41\r
42//\r
43// Cache of WinNtThunk protocol\r
44//\r
45EFI_WIN_NT_THUNK_PROTOCOL *mWinNt = NULL;\r
46\r
47/**\r
48 The function caches the pointer of the WinNT thunk functions\r
49 It will ASSERT() if NT thunk ppi is not installed.\r
50\r
51 @retval EFI_SUCCESS WinNT thunk protocol is found and cached.\r
52\r
53**/\r
54EFI_STATUS\r
55EFIAPI\r
56Nt32PeCoffGetWinNtThunkStucture (\r
57 )\r
58{\r
59 PEI_NT_THUNK_PPI *NtThunkPpi;\r
60 EFI_STATUS Status;\r
61\r
62\r
63 //\r
64 // Locate NtThunkPpi for retrieving standard output handle\r
65 //\r
66 Status = PeiServicesLocatePpi (\r
67 &gPeiNtThunkPpiGuid,\r
68 0,\r
69 NULL,\r
70 (VOID **) &NtThunkPpi\r
71 );\r
72\r
73 ASSERT_EFI_ERROR (Status);\r
74\r
75 mWinNt = (EFI_WIN_NT_THUNK_PROTOCOL *) NtThunkPpi->NtThunk ();\r
76 \r
77 return EFI_SUCCESS;\r
78}\r
79\r
80/**\r
81 Convert the passed in Ascii string to Unicode.\r
82 \r
83 This function Convert the passed in Ascii string to Unicode.Optionally return\r
84 the length of the strings..\r
85\r
86 @param AsciiString Pointer to an AscII string\r
87 @param StrLen Length of string\r
88\r
89 @return Pointer to malloc'ed Unicode version of Ascii\r
90\r
91**/\r
92CHAR16 *\r
93AsciiToUnicode (\r
94 IN CHAR8 *Ascii,\r
95 IN UINTN *StrLen OPTIONAL\r
96 )\r
97{\r
98 UINTN Index;\r
99 CHAR16 *Unicode;\r
100\r
101 //\r
102 // Allocate a buffer for unicode string\r
103 //\r
104 for (Index = 0; Ascii[Index] != '\0'; Index++)\r
105 ;\r
106 Unicode = mWinNt->HeapAlloc ( mWinNt->GetProcessHeap (),\r
107 HEAP_ZERO_MEMORY,\r
108 ((Index + 1) * sizeof (CHAR16))\r
109 ); \r
110 if (Unicode == NULL) {\r
111 return NULL;\r
112 }\r
113\r
114 for (Index = 0; Ascii[Index] != '\0'; Index++) {\r
115 Unicode[Index] = (CHAR16) Ascii[Index];\r
116 }\r
117\r
118 Unicode[Index] = '\0';\r
119\r
120 if (StrLen != NULL) {\r
121 *StrLen = Index;\r
122 }\r
123\r
124 return Unicode;\r
125}\r
126\r
127/**\r
51f1a2b5 128 Performs additional actions after a PE/COFF image has been loaded and relocated.\r
ffdd18bb 129\r
51f1a2b5 130 For NT32, this function load symbols to support source level debugging.\r
131\r
132 If ImageContext is NULL, then ASSERT().\r
133\r
134 @param ImageContext Pointer to the image context structure that describes the\r
135 PE/COFF image that has already been loaded and relocated.\r
ffdd18bb 136\r
137**/\r
138VOID\r
139EFIAPI\r
140PeCoffLoaderRelocateImageExtraAction (\r
141 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r
142 )\r
143{\r
144 VOID *DllEntryPoint;\r
145 CHAR16 *DllFileName;\r
146 HMODULE Library;\r
147 UINTN Index;\r
148 \r
51f1a2b5 149 ASSERT (ImageContext != NULL);\r
150\r
ffdd18bb 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
51f1a2b5 232 Performs additional actions just before a PE/COFF image is unloaded. Any resources\r
233 that were allocated by PeCoffLoaderRelocateImageExtraAction() must be freed.\r
ffdd18bb 234 \r
51f1a2b5 235 For NT32, this function unloads symbols for source level debugging.\r
236\r
ffdd18bb 237 If ImageContext is NULL, then ASSERT().\r
238 \r
51f1a2b5 239 @param ImageContext Pointer to the image context structure that describes the\r
240 PE/COFF image that is being unloaded.\r
ffdd18bb 241\r
242**/\r
243VOID\r
244EFIAPI\r
245PeCoffLoaderUnloadImageExtraAction (\r
246 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r
247 )\r
248{\r
51f1a2b5 249 ASSERT (ImageContext != NULL);\r
ee31b443 250}\r