]> git.proxmox.com Git - mirror_edk2.git/blame - EmulatorPkg/Win/Host/WinHost.c
EmulatorPkg/Win: Unload DLLs before reset
[mirror_edk2.git] / EmulatorPkg / Win / Host / WinHost.c
CommitLineData
3c859dfe
RN
1/**@file\r
2 WinNt emulator of pre-SEC phase. It's really a Win32 application, but this is\r
3 Ok since all the other modules for NT32 are NOT Win32 applications.\r
4\r
5 This program gets NT32 PCD setting and figures out what the memory layout\r
6 will be, how may FD's will be loaded and also what the boot mode is.\r
7\r
8 This code produces 128 K of temporary memory for the SEC stack by directly\r
9 allocate memory space with ReadWrite and Execute attribute.\r
10\r
d2842bb6 11Copyright (c) 2006 - 2022, Intel Corporation. All rights reserved.<BR>\r
bb78cfbe 12(C) Copyright 2016-2020 Hewlett Packard Enterprise Development LP<BR>\r
e3ba31da 13SPDX-License-Identifier: BSD-2-Clause-Patent\r
3c859dfe
RN
14**/\r
15\r
16#include "WinHost.h"\r
17\r
18#ifndef SE_TIME_ZONE_NAME\r
a550d468 19#define SE_TIME_ZONE_NAME TEXT("SeTimeZonePrivilege")\r
3c859dfe
RN
20#endif\r
21\r
10ccc27c
MK
22//\r
23// The growth size for array of module handle entries\r
24//\r
a550d468 25#define MAX_PDB_NAME_TO_MOD_HANDLE_ARRAY_SIZE 0x100\r
10ccc27c
MK
26\r
27//\r
28// Module handle entry structure\r
29//\r
30typedef struct {\r
a550d468
MK
31 CHAR8 *PdbPointer;\r
32 VOID *ModHandle;\r
10ccc27c
MK
33} PDB_NAME_TO_MOD_HANDLE;\r
34\r
35//\r
36// An Array to hold the module handles\r
37//\r
a550d468 38PDB_NAME_TO_MOD_HANDLE *mPdbNameModHandleArray = NULL;\r
10ccc27c
MK
39UINTN mPdbNameModHandleArraySize = 0;\r
40\r
3c859dfe
RN
41//\r
42// Default information about where the FD is located.\r
43// This array gets filled in with information from PcdWinNtFirmwareVolume\r
44// The number of array elements is allocated base on parsing\r
45// PcdWinNtFirmwareVolume and the memory is never freed.\r
46//\r
a550d468
MK
47UINTN gFdInfoCount = 0;\r
48NT_FD_INFO *gFdInfo;\r
3c859dfe
RN
49\r
50//\r
3d6b7fd3 51// Array that supports separate memory ranges.\r
3c859dfe
RN
52// The memory ranges are set by PcdWinNtMemorySizeForSecMain.\r
53// The number of array elements is allocated base on parsing\r
54// PcdWinNtMemorySizeForSecMain value and the memory is never freed.\r
55//\r
a550d468
MK
56UINTN gSystemMemoryCount = 0;\r
57NT_SYSTEM_MEMORY *gSystemMemory;\r
3c859dfe 58\r
7bee2498
RN
59BASE_LIBRARY_JUMP_BUFFER mResetJumpBuffer;\r
60CHAR8 *mResetTypeStr[] = {\r
61 "EfiResetCold",\r
62 "EfiResetWarm",\r
63 "EfiResetShutdown",\r
64 "EfiResetPlatformSpecific"\r
65};\r
66\r
3c859dfe
RN
67/*++\r
68\r
69Routine Description:\r
70 This service is called from Index == 0 until it returns EFI_UNSUPPORTED.\r
71 It allows discontinuous memory regions to be supported by the emulator.\r
72 It uses gSystemMemory[] and gSystemMemoryCount that were created by\r
73 parsing the host environment variable EFI_MEMORY_SIZE.\r
74 The size comes from the varaible and the address comes from the call to\r
75 UnixOpenFile.\r
76\r
77Arguments:\r
78 Index - Which memory region to use\r
79 MemoryBase - Return Base address of memory region\r
80 MemorySize - Return size in bytes of the memory region\r
81\r
82Returns:\r
83 EFI_SUCCESS - If memory region was mapped\r
84 EFI_UNSUPPORTED - If Index is not supported\r
85\r
86**/\r
87EFI_STATUS\r
88WinPeiAutoScan (\r
89 IN UINTN Index,\r
90 OUT EFI_PHYSICAL_ADDRESS *MemoryBase,\r
91 OUT UINT64 *MemorySize\r
92 )\r
93{\r
94 if (Index >= gSystemMemoryCount) {\r
95 return EFI_UNSUPPORTED;\r
96 }\r
97\r
3c859dfe
RN
98 *MemoryBase = gSystemMemory[Index].Memory;\r
99 *MemorySize = gSystemMemory[Index].Size;\r
100\r
101 return EFI_SUCCESS;\r
102}\r
103\r
104/*++\r
105\r
106Routine Description:\r
107 Return the FD Size and base address. Since the FD is loaded from a\r
3d6b7fd3 108 file into host memory only the SEC will know its address.\r
3c859dfe
RN
109\r
110Arguments:\r
111 Index - Which FD, starts at zero.\r
112 FdSize - Size of the FD in bytes\r
113 FdBase - Start address of the FD. Assume it points to an FV Header\r
114 FixUp - Difference between actual FD address and build address\r
115\r
116Returns:\r
117 EFI_SUCCESS - Return the Base address and size of the FV\r
118 EFI_UNSUPPORTED - Index does nto map to an FD in the system\r
119\r
120**/\r
121EFI_STATUS\r
122WinFdAddress (\r
123 IN UINTN Index,\r
124 IN OUT EFI_PHYSICAL_ADDRESS *FdBase,\r
125 IN OUT UINT64 *FdSize,\r
126 IN OUT EFI_PHYSICAL_ADDRESS *FixUp\r
127 )\r
128{\r
129 if (Index >= gFdInfoCount) {\r
130 return EFI_UNSUPPORTED;\r
131 }\r
132\r
3c859dfe
RN
133 *FdBase = (EFI_PHYSICAL_ADDRESS)(UINTN)gFdInfo[Index].Address;\r
134 *FdSize = (UINT64)gFdInfo[Index].Size;\r
135 *FixUp = 0;\r
136\r
a550d468 137 if ((*FdBase == 0) && (*FdSize == 0)) {\r
3c859dfe
RN
138 return EFI_UNSUPPORTED;\r
139 }\r
140\r
141 if (Index == 0) {\r
142 //\r
143 // FD 0 has XIP code and well known PCD values\r
144 // If the memory buffer could not be allocated at the FD build address\r
145 // the Fixup is the difference.\r
146 //\r
147 *FixUp = *FdBase - PcdGet64 (PcdEmuFdBaseAddress);\r
148 }\r
149\r
150 return EFI_SUCCESS;\r
151}\r
152\r
153/*++\r
154\r
155Routine Description:\r
156 Since the SEC is the only Unix program in stack it must export\r
157 an interface to do POSIX calls. gUnix is initialized in UnixThunk.c.\r
158\r
159Arguments:\r
160 InterfaceSize - sizeof (EFI_WIN_NT_THUNK_PROTOCOL);\r
161 InterfaceBase - Address of the gUnix global\r
162\r
163Returns:\r
164 EFI_SUCCESS - Data returned\r
165\r
166**/\r
167VOID *\r
168WinThunk (\r
169 VOID\r
170 )\r
171{\r
172 return &gEmuThunkProtocol;\r
173}\r
174\r
a550d468 175EMU_THUNK_PPI mSecEmuThunkPpi = {\r
3c859dfe
RN
176 WinPeiAutoScan,\r
177 WinFdAddress,\r
178 WinThunk\r
179};\r
180\r
181VOID\r
182SecPrint (\r
183 CHAR8 *Format,\r
184 ...\r
185 )\r
186{\r
187 va_list Marker;\r
188 UINTN CharCount;\r
189 CHAR8 Buffer[0x1000];\r
190\r
191 va_start (Marker, Format);\r
192\r
193 _vsnprintf (Buffer, sizeof (Buffer), Format, Marker);\r
194\r
195 va_end (Marker);\r
196\r
197 CharCount = strlen (Buffer);\r
198 WriteFile (\r
199 GetStdHandle (STD_OUTPUT_HANDLE),\r
200 Buffer,\r
201 (DWORD)CharCount,\r
202 (LPDWORD)&CharCount,\r
203 NULL\r
204 );\r
205}\r
206\r
7bee2498
RN
207/**\r
208 Resets the entire platform.\r
209\r
210 @param[in] ResetType The type of reset to perform.\r
211 @param[in] ResetStatus The status code for the reset.\r
212 @param[in] DataSize The size, in bytes, of ResetData.\r
213 @param[in] ResetData For a ResetType of EfiResetCold, EfiResetWarm, or EfiResetShutdown\r
214 the data buffer starts with a Null-terminated string, optionally\r
215 followed by additional binary data. The string is a description\r
216 that the caller may use to further indicate the reason for the\r
217 system reset.\r
218\r
219**/\r
220VOID\r
221EFIAPI\r
222WinReset (\r
223 IN EFI_RESET_TYPE ResetType,\r
224 IN EFI_STATUS ResetStatus,\r
225 IN UINTN DataSize,\r
226 IN VOID *ResetData OPTIONAL\r
227 )\r
228{\r
4e17aba4
RN
229 UINTN Index;\r
230\r
7bee2498
RN
231 ASSERT (ResetType <= EfiResetPlatformSpecific);\r
232 SecPrint (" Emu ResetSystem is called: ResetType = %s\n", mResetTypeStr[ResetType]);\r
233\r
234 if (ResetType == EfiResetShutdown) {\r
235 exit (0);\r
236 } else {\r
4e17aba4
RN
237 //\r
238 // Unload all DLLs\r
239 //\r
240 for (Index = 0; Index < mPdbNameModHandleArraySize; Index++) {\r
241 if (mPdbNameModHandleArray[Index].PdbPointer != NULL) {\r
242 SecPrint (" Emu Unload DLL: %s\n", mPdbNameModHandleArray[Index].PdbPointer);\r
243 FreeLibrary (mPdbNameModHandleArray[Index].ModHandle);\r
244 HeapFree (GetProcessHeap (), 0, mPdbNameModHandleArray[Index].PdbPointer);\r
245 mPdbNameModHandleArray[Index].PdbPointer = NULL;\r
246 }\r
247 }\r
248\r
7bee2498
RN
249 //\r
250 // Jump back to SetJump with jump code = ResetType + 1\r
251 //\r
252 LongJump (&mResetJumpBuffer, ResetType + 1);\r
253 }\r
254}\r
255\r
256EFI_PEI_RESET2_PPI mEmuReset2Ppi = {\r
257 WinReset\r
258};\r
259\r
3c859dfe
RN
260/*++\r
261\r
262Routine Description:\r
263 Check to see if an address range is in the EFI GCD memory map.\r
264\r
265 This is all of GCD for system memory passed to DXE Core. FV\r
266 mapping and other device mapped into system memory are not\r
267 inlcuded in the check.\r
268\r
269Arguments:\r
270 Index - Which memory region to use\r
271 MemoryBase - Return Base address of memory region\r
272 MemorySize - Return size in bytes of the memory region\r
273\r
274Returns:\r
275 TRUE - Address is in the EFI GCD memory map\r
276 FALSE - Address is NOT in memory map\r
277\r
278**/\r
279BOOLEAN\r
280EfiSystemMemoryRange (\r
a550d468 281 IN VOID *MemoryAddress\r
3c859dfe
RN
282 )\r
283{\r
284 UINTN Index;\r
285 EFI_PHYSICAL_ADDRESS MemoryBase;\r
286\r
287 MemoryBase = (EFI_PHYSICAL_ADDRESS)(UINTN)MemoryAddress;\r
288 for (Index = 0; Index < gSystemMemoryCount; Index++) {\r
289 if ((MemoryBase >= gSystemMemory[Index].Memory) &&\r
a550d468
MK
290 (MemoryBase < (gSystemMemory[Index].Memory + gSystemMemory[Index].Size)))\r
291 {\r
3c859dfe
RN
292 return TRUE;\r
293 }\r
294 }\r
295\r
296 return FALSE;\r
297}\r
298\r
3c859dfe
RN
299EFI_STATUS\r
300WinNtOpenFile (\r
a550d468
MK
301 IN CHAR16 *FileName OPTIONAL,\r
302 IN UINT32 MapSize,\r
303 IN DWORD CreationDisposition,\r
304 IN OUT VOID **BaseAddress,\r
305 OUT UINTN *Length\r
3c859dfe 306 )\r
a550d468 307\r
3c859dfe
RN
308/*++\r
309\r
310Routine Description:\r
311 Opens and memory maps a file using WinNt services. If *BaseAddress is non zero\r
312 the process will try and allocate the memory starting at BaseAddress.\r
313\r
314Arguments:\r
315 FileName - The name of the file to open and map\r
316 MapSize - The amount of the file to map in bytes\r
317 CreationDisposition - The flags to pass to CreateFile(). Use to create new files for\r
318 memory emulation, and exiting files for firmware volume emulation\r
319 BaseAddress - The base address of the mapped file in the user address space.\r
320 If *BaseAddress is 0, the new memory region is used.\r
321 If *BaseAddress is not 0, the request memory region is used for\r
322 the mapping of the file into the process space.\r
323 Length - The size of the mapped region in bytes\r
324\r
325Returns:\r
326 EFI_SUCCESS - The file was opened and mapped.\r
327 EFI_NOT_FOUND - FileName was not found in the current directory\r
3d6b7fd3 328 EFI_DEVICE_ERROR - An error occurred attempting to map the opened file\r
3c859dfe
RN
329\r
330--*/\r
331{\r
332 HANDLE NtFileHandle;\r
333 HANDLE NtMapHandle;\r
334 VOID *VirtualAddress;\r
335 UINTN FileSize;\r
336\r
337 //\r
338 // Use Win API to open/create a file\r
339 //\r
340 NtFileHandle = INVALID_HANDLE_VALUE;\r
341 if (FileName != NULL) {\r
342 NtFileHandle = CreateFile (\r
343 FileName,\r
344 GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE,\r
345 FILE_SHARE_READ,\r
346 NULL,\r
347 CreationDisposition,\r
348 FILE_ATTRIBUTE_NORMAL,\r
349 NULL\r
350 );\r
351 if (NtFileHandle == INVALID_HANDLE_VALUE) {\r
352 return EFI_NOT_FOUND;\r
353 }\r
354 }\r
a550d468 355\r
3c859dfe
RN
356 //\r
357 // Map the open file into a memory range\r
358 //\r
359 NtMapHandle = CreateFileMapping (\r
360 NtFileHandle,\r
361 NULL,\r
362 PAGE_EXECUTE_READWRITE,\r
363 0,\r
364 MapSize,\r
365 NULL\r
366 );\r
367 if (NtMapHandle == NULL) {\r
368 return EFI_DEVICE_ERROR;\r
369 }\r
a550d468 370\r
3c859dfe
RN
371 //\r
372 // Get the virtual address (address in the emulator) of the mapped file\r
373 //\r
374 VirtualAddress = MapViewOfFileEx (\r
a550d468
MK
375 NtMapHandle,\r
376 FILE_MAP_EXECUTE | FILE_MAP_ALL_ACCESS,\r
377 0,\r
378 0,\r
379 MapSize,\r
380 *BaseAddress\r
381 );\r
3c859dfe
RN
382 if (VirtualAddress == NULL) {\r
383 return EFI_DEVICE_ERROR;\r
384 }\r
385\r
386 if (MapSize == 0) {\r
387 //\r
388 // Seek to the end of the file to figure out the true file size.\r
389 //\r
390 FileSize = SetFilePointer (\r
a550d468
MK
391 NtFileHandle,\r
392 0,\r
393 NULL,\r
394 FILE_END\r
395 );\r
3c859dfe
RN
396 if (FileSize == -1) {\r
397 return EFI_DEVICE_ERROR;\r
398 }\r
399\r
400 *Length = FileSize;\r
401 } else {\r
402 *Length = MapSize;\r
403 }\r
404\r
405 *BaseAddress = VirtualAddress;\r
406\r
407 return EFI_SUCCESS;\r
408}\r
409\r
410INTN\r
411EFIAPI\r
412main (\r
a550d468
MK
413 IN INT Argc,\r
414 IN CHAR8 **Argv,\r
415 IN CHAR8 **Envp\r
3c859dfe 416 )\r
a550d468 417\r
3c859dfe
RN
418/*++\r
419\r
420Routine Description:\r
421 Main entry point to SEC for WinNt. This is a Windows program\r
422\r
423Arguments:\r
424 Argc - Number of command line arguments\r
425 Argv - Array of command line argument strings\r
426 Envp - Array of environment variable strings\r
427\r
428Returns:\r
429 0 - Normal exit\r
430 1 - Abnormal exit\r
431\r
432--*/\r
433{\r
a550d468
MK
434 EFI_STATUS Status;\r
435 HANDLE Token;\r
436 TOKEN_PRIVILEGES TokenPrivileges;\r
437 VOID *TemporaryRam;\r
438 UINT32 TemporaryRamSize;\r
439 VOID *EmuMagicPage;\r
440 UINTN Index;\r
441 UINTN Index1;\r
442 CHAR16 *FileName;\r
443 CHAR16 *FileNamePtr;\r
444 BOOLEAN Done;\r
445 EFI_PEI_FILE_HANDLE FileHandle;\r
446 VOID *SecFile;\r
447 CHAR16 *MemorySizeStr;\r
448 CHAR16 *FirmwareVolumesStr;\r
449 UINTN ProcessAffinityMask;\r
450 UINTN SystemAffinityMask;\r
451 INT32 LowBit;\r
7bee2498 452 UINTN ResetJumpCode;\r
3c859dfe
RN
453\r
454 //\r
455 // Enable the privilege so that RTC driver can successfully run SetTime()\r
456 //\r
a550d468
MK
457 OpenProcessToken (GetCurrentProcess (), TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY, &Token);\r
458 if (LookupPrivilegeValue (NULL, SE_TIME_ZONE_NAME, &TokenPrivileges.Privileges[0].Luid)) {\r
459 TokenPrivileges.PrivilegeCount = 1;\r
3c859dfe 460 TokenPrivileges.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;\r
a550d468 461 AdjustTokenPrivileges (Token, FALSE, &TokenPrivileges, 0, (PTOKEN_PRIVILEGES)NULL, 0);\r
3c859dfe
RN
462 }\r
463\r
a550d468
MK
464 MemorySizeStr = (CHAR16 *)PcdGetPtr (PcdEmuMemorySize);\r
465 FirmwareVolumesStr = (CHAR16 *)PcdGetPtr (PcdEmuFirmwareVolume);\r
3c859dfe 466\r
30b4abc6 467 SecPrint ("\n\rEDK II WIN Host Emulation Environment from http://www.tianocore.org/edk2/\n\r");\r
3c859dfe
RN
468\r
469 //\r
470 // Determine the first thread available to this process.\r
471 //\r
472 if (GetProcessAffinityMask (GetCurrentProcess (), &ProcessAffinityMask, &SystemAffinityMask)) {\r
2737037a 473 LowBit = (INT32)LowBitSet32 ((UINT32)ProcessAffinityMask);\r
3c859dfe
RN
474 if (LowBit != -1) {\r
475 //\r
476 // Force the system to bind the process to a single thread to work\r
477 // around odd semaphore type crashes.\r
478 //\r
479 SetProcessAffinityMask (GetCurrentProcess (), (INTN)(BIT0 << LowBit));\r
480 }\r
481 }\r
482\r
483 //\r
484 // Make some Windows calls to Set the process to the highest priority in the\r
485 // idle class. We need this to have good performance.\r
486 //\r
487 SetPriorityClass (GetCurrentProcess (), IDLE_PRIORITY_CLASS);\r
488 SetThreadPriority (GetCurrentThread (), THREAD_PRIORITY_HIGHEST);\r
489\r
490 SecInitializeThunk ();\r
491 //\r
492 // PPIs pased into PEI_CORE\r
493 //\r
494 AddThunkPpi (EFI_PEI_PPI_DESCRIPTOR_PPI, &gEmuThunkPpiGuid, &mSecEmuThunkPpi);\r
7bee2498 495 AddThunkPpi (EFI_PEI_PPI_DESCRIPTOR_PPI, &gEfiPeiReset2PpiGuid, &mEmuReset2Ppi);\r
3c859dfe 496\r
7a465451
RN
497 //\r
498 // Emulator Bus Driver Thunks\r
499 //\r
500 AddThunkProtocol (&mWinNtWndThunkIo, (CHAR16 *)PcdGetPtr (PcdEmuGop), TRUE);\r
56502bf1 501 AddThunkProtocol (&mWinNtFileSystemThunkIo, (CHAR16 *)PcdGetPtr (PcdEmuFileSystem), TRUE);\r
8f819697 502 AddThunkProtocol (&mWinNtBlockIoThunkIo, (CHAR16 *)PcdGetPtr (PcdEmuVirtualDisk), TRUE);\r
bb78cfbe 503 AddThunkProtocol (&mWinNtSnpThunkIo, (CHAR16 *)PcdGetPtr (PcdEmuNetworkInterface), TRUE);\r
7a465451 504\r
3c859dfe
RN
505 //\r
506 // Allocate space for gSystemMemory Array\r
507 //\r
a550d468
MK
508 gSystemMemoryCount = CountSeparatorsInString (MemorySizeStr, '!') + 1;\r
509 gSystemMemory = calloc (gSystemMemoryCount, sizeof (NT_SYSTEM_MEMORY));\r
3c859dfe 510 if (gSystemMemory == NULL) {\r
30b4abc6 511 SecPrint ("ERROR : Can not allocate memory for %S. Exiting.\n\r", MemorySizeStr);\r
3c859dfe
RN
512 exit (1);\r
513 }\r
514\r
d2842bb6
RN
515 //\r
516 // Allocate "physical" memory space for emulator. It will be reported out later throuth MemoryAutoScan()\r
517 //\r
518 for (Index = 0, Done = FALSE; !Done; Index++) {\r
519 ASSERT (Index < gSystemMemoryCount);\r
520 gSystemMemory[Index].Size = ((UINT64)_wtoi (MemorySizeStr)) * ((UINT64)SIZE_1MB);\r
521 gSystemMemory[Index].Memory = (EFI_PHYSICAL_ADDRESS)(UINTN)VirtualAlloc (NULL, (SIZE_T)(gSystemMemory[Index].Size), MEM_COMMIT, PAGE_EXECUTE_READWRITE);\r
522 if (gSystemMemory[Index].Memory == 0) {\r
523 return EFI_OUT_OF_RESOURCES;\r
524 }\r
525\r
526 //\r
527 // Find the next region\r
528 //\r
529 for (Index1 = 0; MemorySizeStr[Index1] != '!' && MemorySizeStr[Index1] != 0; Index1++) {\r
530 }\r
531\r
532 if (MemorySizeStr[Index1] == 0) {\r
533 Done = TRUE;\r
534 }\r
535\r
536 MemorySizeStr = MemorySizeStr + Index1 + 1;\r
537 }\r
538\r
3c859dfe
RN
539 //\r
540 // Allocate space for gSystemMemory Array\r
541 //\r
a550d468
MK
542 gFdInfoCount = CountSeparatorsInString (FirmwareVolumesStr, '!') + 1;\r
543 gFdInfo = calloc (gFdInfoCount, sizeof (NT_FD_INFO));\r
3c859dfe 544 if (gFdInfo == NULL) {\r
30b4abc6 545 SecPrint ("ERROR : Can not allocate memory for %S. Exiting.\n\r", FirmwareVolumesStr);\r
3c859dfe
RN
546 exit (1);\r
547 }\r
a550d468 548\r
3c859dfe
RN
549 //\r
550 // Setup Boot Mode.\r
551 //\r
30b4abc6 552 SecPrint (" BootMode 0x%02x\n\r", PcdGet32 (PcdEmuBootMode));\r
3c859dfe
RN
553\r
554 //\r
555 // Allocate 128K memory to emulate temp memory for PEI.\r
556 // on a real platform this would be SRAM, or using the cache as RAM.\r
557 // Set TemporaryRam to zero so WinNtOpenFile will allocate a new mapping\r
558 //\r
559 TemporaryRamSize = TEMPORARY_RAM_SIZE;\r
a550d468 560 TemporaryRam = VirtualAlloc (NULL, (SIZE_T)(TemporaryRamSize), MEM_COMMIT, PAGE_EXECUTE_READWRITE);\r
3c859dfe 561 if (TemporaryRam == NULL) {\r
30b4abc6 562 SecPrint ("ERROR : Can not allocate enough space for SecStack\n\r");\r
3c859dfe
RN
563 exit (1);\r
564 }\r
a550d468 565\r
3c859dfe
RN
566 //\r
567 // If enabled use the magic page to communicate between modules\r
568 // This replaces the PI PeiServicesTable pointer mechanism that\r
569 // deos not work in the emulator. It also allows the removal of\r
570 // writable globals from SEC, PEI_CORE (libraries), PEIMs\r
571 //\r
572 EmuMagicPage = (VOID *)(UINTN)(FixedPcdGet64 (PcdPeiServicesTablePage) & MAX_UINTN);\r
573 if (EmuMagicPage != NULL) {\r
574 UINT64 Size;\r
575 Status = WinNtOpenFile (\r
a550d468
MK
576 NULL,\r
577 SIZE_4KB,\r
578 0,\r
579 &EmuMagicPage,\r
580 &Size\r
581 );\r
3c859dfe 582 if (EFI_ERROR (Status)) {\r
30b4abc6 583 SecPrint ("ERROR : Could not allocate PeiServicesTablePage @ %p\n\r", EmuMagicPage);\r
3c859dfe
RN
584 return EFI_DEVICE_ERROR;\r
585 }\r
586 }\r
587\r
588 //\r
589 // Open All the firmware volumes and remember the info in the gFdInfo global\r
590 // Meanwhile, find the SEC Core.\r
591 //\r
592 FileNamePtr = AllocateCopyPool (StrSize (FirmwareVolumesStr), FirmwareVolumesStr);\r
593 if (FileNamePtr == NULL) {\r
30b4abc6 594 SecPrint ("ERROR : Can not allocate memory for firmware volume string\n\r");\r
3c859dfe
RN
595 exit (1);\r
596 }\r
597\r
598 for (Done = FALSE, Index = 0, SecFile = NULL; !Done; Index++) {\r
599 FileName = FileNamePtr;\r
a550d468
MK
600 for (Index1 = 0; (FileNamePtr[Index1] != '!') && (FileNamePtr[Index1] != 0); Index1++) {\r
601 }\r
602\r
3c859dfe
RN
603 if (FileNamePtr[Index1] == 0) {\r
604 Done = TRUE;\r
605 } else {\r
a550d468
MK
606 FileNamePtr[Index1] = '\0';\r
607 FileNamePtr = &FileNamePtr[Index1 + 1];\r
3c859dfe
RN
608 }\r
609\r
610 //\r
611 // Open the FD and remember where it got mapped into our processes address space\r
612 //\r
613 Status = WinNtOpenFile (\r
a550d468
MK
614 FileName,\r
615 0,\r
616 OPEN_EXISTING,\r
617 &gFdInfo[Index].Address,\r
618 &gFdInfo[Index].Size\r
619 );\r
3c859dfe 620 if (EFI_ERROR (Status)) {\r
30b4abc6 621 SecPrint ("ERROR : Can not open Firmware Device File %S (0x%X). Exiting.\n\r", FileName, Status);\r
3c859dfe
RN
622 exit (1);\r
623 }\r
624\r
30b4abc6 625 SecPrint (" FD loaded from %S", FileName);\r
3c859dfe
RN
626\r
627 if (SecFile == NULL) {\r
628 //\r
629 // Assume the beginning of the FD is an FV and look for the SEC Core.\r
630 // Load the first one we find.\r
631 //\r
632 FileHandle = NULL;\r
a550d468
MK
633 Status = PeiServicesFfsFindNextFile (\r
634 EFI_FV_FILETYPE_SECURITY_CORE,\r
635 (EFI_PEI_FV_HANDLE)gFdInfo[Index].Address,\r
636 &FileHandle\r
637 );\r
3c859dfe
RN
638 if (!EFI_ERROR (Status)) {\r
639 Status = PeiServicesFfsFindSectionData (EFI_SECTION_PE32, FileHandle, &SecFile);\r
640 if (!EFI_ERROR (Status)) {\r
641 SecPrint (" contains SEC Core");\r
642 }\r
643 }\r
644 }\r
645\r
30b4abc6 646 SecPrint ("\n\r");\r
3c859dfe 647 }\r
a550d468 648\r
7bee2498
RN
649 ResetJumpCode = SetJump (&mResetJumpBuffer);\r
650\r
651 //\r
652 // Do not clear memory content for warm reset.\r
653 //\r
654 if (ResetJumpCode != EfiResetWarm + 1) {\r
655 SecPrint (" OS Emulator clearing temp RAM and physical RAM (to be discovered later)......\n\r");\r
656 SetMem32 (TemporaryRam, TemporaryRamSize, PcdGet32 (PcdInitValueInTempStack));\r
657 for (Index = 0; Index < gSystemMemoryCount; Index++) {\r
658 SetMem32 ((VOID *)(UINTN)gSystemMemory[Index].Memory, (UINTN)gSystemMemory[Index].Size, PcdGet32 (PcdInitValueInTempStack));\r
659 }\r
660 }\r
661\r
662 SecPrint (\r
663 " OS Emulator passing in %u KB of temp RAM at 0x%08lx to SEC\n\r",\r
664 TemporaryRamSize / SIZE_1KB,\r
665 TemporaryRam\r
666 );\r
3c859dfe
RN
667 //\r
668 // Hand off to SEC Core\r
669 //\r
670 SecLoadSecCore ((UINTN)TemporaryRam, TemporaryRamSize, gFdInfo[0].Address, gFdInfo[0].Size, SecFile);\r
671\r
672 //\r
673 // If we get here, then the SEC Core returned. This is an error as SEC should\r
674 // always hand off to PEI Core and then on to DXE Core.\r
675 //\r
30b4abc6 676 SecPrint ("ERROR : SEC returned\n\r");\r
3c859dfe
RN
677 exit (1);\r
678}\r
679\r
680VOID\r
681SecLoadSecCore (\r
a550d468
MK
682 IN UINTN TemporaryRam,\r
683 IN UINTN TemporaryRamSize,\r
684 IN VOID *BootFirmwareVolumeBase,\r
685 IN UINTN BootFirmwareVolumeSize,\r
686 IN VOID *SecCorePe32File\r
3c859dfe 687 )\r
a550d468 688\r
3c859dfe
RN
689/*++\r
690\r
691Routine Description:\r
692 This is the service to load the SEC Core from the Firmware Volume\r
693\r
694Arguments:\r
695 TemporaryRam - Memory to use for SEC.\r
696 TemporaryRamSize - Size of Memory to use for SEC\r
697 BootFirmwareVolumeBase - Start of the Boot FV\r
698 SecCorePe32File - SEC Core PE32\r
699\r
700Returns:\r
3d6b7fd3 701 Success means control is transferred and thus we should never return\r
3c859dfe
RN
702\r
703--*/\r
704{\r
a550d468
MK
705 EFI_STATUS Status;\r
706 VOID *TopOfStack;\r
707 VOID *SecCoreEntryPoint;\r
708 EFI_SEC_PEI_HAND_OFF *SecCoreData;\r
709 UINTN SecStackSize;\r
3c859dfe
RN
710\r
711 //\r
712 // Compute Top Of Memory for Stack and PEI Core Allocations\r
713 //\r
714 SecStackSize = TemporaryRamSize >> 1;\r
715\r
716 //\r
717 // |-----------| <---- TemporaryRamBase + TemporaryRamSize\r
718 // | Heap |\r
719 // | |\r
720 // |-----------| <---- StackBase / PeiTemporaryMemoryBase\r
721 // | |\r
722 // | Stack |\r
723 // |-----------| <---- TemporaryRamBase\r
724 //\r
a550d468 725 TopOfStack = (VOID *)(TemporaryRam + SecStackSize);\r
3c859dfe
RN
726\r
727 //\r
728 // Reservet space for storing PeiCore's parament in stack.\r
729 //\r
a550d468
MK
730 TopOfStack = (VOID *)((UINTN)TopOfStack - sizeof (EFI_SEC_PEI_HAND_OFF) - CPU_STACK_ALIGNMENT);\r
731 TopOfStack = ALIGN_POINTER (TopOfStack, CPU_STACK_ALIGNMENT);\r
3c859dfe
RN
732\r
733 //\r
734 // Bind this information into the SEC hand-off state\r
735 //\r
a550d468 736 SecCoreData = (EFI_SEC_PEI_HAND_OFF *)(UINTN)TopOfStack;\r
3c859dfe
RN
737 SecCoreData->DataSize = sizeof (EFI_SEC_PEI_HAND_OFF);\r
738 SecCoreData->BootFirmwareVolumeBase = BootFirmwareVolumeBase;\r
739 SecCoreData->BootFirmwareVolumeSize = BootFirmwareVolumeSize;\r
a550d468 740 SecCoreData->TemporaryRamBase = (VOID *)TemporaryRam;\r
3c859dfe
RN
741 SecCoreData->TemporaryRamSize = TemporaryRamSize;\r
742 SecCoreData->StackBase = SecCoreData->TemporaryRamBase;\r
743 SecCoreData->StackSize = SecStackSize;\r
a550d468 744 SecCoreData->PeiTemporaryRamBase = (VOID *)((UINTN)SecCoreData->TemporaryRamBase + SecStackSize);\r
3c859dfe
RN
745 SecCoreData->PeiTemporaryRamSize = TemporaryRamSize - SecStackSize;\r
746\r
747 //\r
748 // Load the PEI Core from a Firmware Volume\r
749 //\r
750 Status = SecPeCoffGetEntryPoint (\r
a550d468
MK
751 SecCorePe32File,\r
752 &SecCoreEntryPoint\r
753 );\r
3c859dfe 754 if (EFI_ERROR (Status)) {\r
a550d468 755 return;\r
3c859dfe
RN
756 }\r
757\r
758 //\r
759 // Transfer control to the SEC Core\r
760 //\r
761 SwitchStack (\r
f4eaaf1a 762 (SWITCH_STACK_ENTRY_POINT)(UINTN)SecCoreEntryPoint,\r
3c859dfe
RN
763 SecCoreData,\r
764 GetThunkPpiList (),\r
765 TopOfStack\r
766 );\r
767 //\r
768 // If we get here, then the SEC Core returned. This is an error\r
769 //\r
a550d468 770 return;\r
3c859dfe
RN
771}\r
772\r
773RETURN_STATUS\r
774EFIAPI\r
775SecPeCoffGetEntryPoint (\r
776 IN VOID *Pe32Data,\r
777 IN OUT VOID **EntryPoint\r
778 )\r
779{\r
a550d468
MK
780 EFI_STATUS Status;\r
781 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;\r
3c859dfe
RN
782\r
783 ZeroMem (&ImageContext, sizeof (ImageContext));\r
a550d468 784 ImageContext.Handle = Pe32Data;\r
3c859dfe 785\r
a550d468 786 ImageContext.ImageRead = (PE_COFF_LOADER_READ_FILE)SecImageRead;\r
3c859dfe 787\r
a550d468 788 Status = PeCoffLoaderGetImageInfo (&ImageContext);\r
3c859dfe
RN
789 if (EFI_ERROR (Status)) {\r
790 return Status;\r
791 }\r
a550d468 792\r
3c859dfe 793 //\r
a121165e 794 // XIP for SEC and PEI_CORE\r
3c859dfe 795 //\r
a121165e 796 ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)(UINTN)Pe32Data;\r
3c859dfe
RN
797\r
798 Status = PeCoffLoaderLoadImage (&ImageContext);\r
799 if (EFI_ERROR (Status)) {\r
800 return Status;\r
801 }\r
802\r
803 Status = PeCoffLoaderRelocateImage (&ImageContext);\r
804 if (EFI_ERROR (Status)) {\r
805 return Status;\r
806 }\r
807\r
a550d468 808 *EntryPoint = (VOID *)(UINTN)ImageContext.EntryPoint;\r
3c859dfe
RN
809\r
810 return EFI_SUCCESS;\r
811}\r
812\r
813EFI_STATUS\r
814EFIAPI\r
815SecImageRead (\r
a550d468
MK
816 IN VOID *FileHandle,\r
817 IN UINTN FileOffset,\r
818 IN OUT UINTN *ReadSize,\r
819 OUT VOID *Buffer\r
3c859dfe 820 )\r
a550d468 821\r
3c859dfe
RN
822/*++\r
823\r
824Routine Description:\r
825 Support routine for the PE/COFF Loader that reads a buffer from a PE/COFF file\r
826\r
827Arguments:\r
828 FileHandle - The handle to the PE/COFF file\r
829 FileOffset - The offset, in bytes, into the file to read\r
830 ReadSize - The number of bytes to read from the file starting at FileOffset\r
831 Buffer - A pointer to the buffer to read the data into.\r
832\r
833Returns:\r
834 EFI_SUCCESS - ReadSize bytes of data were read into Buffer from the PE/COFF file starting at FileOffset\r
835\r
836--*/\r
837{\r
a550d468
MK
838 CHAR8 *Destination8;\r
839 CHAR8 *Source8;\r
840 UINTN Length;\r
3c859dfe 841\r
a550d468
MK
842 Destination8 = Buffer;\r
843 Source8 = (CHAR8 *)((UINTN)FileHandle + FileOffset);\r
844 Length = *ReadSize;\r
3c859dfe
RN
845 while (Length--) {\r
846 *(Destination8++) = *(Source8++);\r
847 }\r
848\r
849 return EFI_SUCCESS;\r
850}\r
851\r
852CHAR16 *\r
853AsciiToUnicode (\r
a550d468
MK
854 IN CHAR8 *Ascii,\r
855 IN UINTN *StrLen OPTIONAL\r
3c859dfe 856 )\r
a550d468 857\r
3c859dfe
RN
858/*++\r
859\r
860Routine Description:\r
861 Convert the passed in Ascii string to Unicode.\r
862 Optionally return the length of the strings.\r
863\r
864Arguments:\r
865 Ascii - Ascii string to convert\r
866 StrLen - Length of string\r
867\r
868Returns:\r
869 Pointer to malloc'ed Unicode version of Ascii\r
870\r
871--*/\r
872{\r
873 UINTN Index;\r
874 CHAR16 *Unicode;\r
875\r
876 //\r
877 // Allocate a buffer for unicode string\r
878 //\r
a550d468
MK
879 for (Index = 0; Ascii[Index] != '\0'; Index++) {\r
880 }\r
881\r
3c859dfe
RN
882 Unicode = malloc ((Index + 1) * sizeof (CHAR16));\r
883 if (Unicode == NULL) {\r
884 return NULL;\r
885 }\r
886\r
887 for (Index = 0; Ascii[Index] != '\0'; Index++) {\r
a550d468 888 Unicode[Index] = (CHAR16)Ascii[Index];\r
3c859dfe
RN
889 }\r
890\r
891 Unicode[Index] = '\0';\r
892\r
893 if (StrLen != NULL) {\r
894 *StrLen = Index;\r
895 }\r
896\r
897 return Unicode;\r
898}\r
899\r
900UINTN\r
901CountSeparatorsInString (\r
a550d468
MK
902 IN CONST CHAR16 *String,\r
903 IN CHAR16 Separator\r
3c859dfe 904 )\r
a550d468 905\r
3c859dfe
RN
906/*++\r
907\r
908Routine Description:\r
909 Count the number of separators in String\r
910\r
911Arguments:\r
912 String - String to process\r
913 Separator - Item to count\r
914\r
915Returns:\r
916 Number of Separator in String\r
917\r
918--*/\r
919{\r
a550d468 920 UINTN Count;\r
3c859dfe
RN
921\r
922 for (Count = 0; *String != '\0'; String++) {\r
923 if (*String == Separator) {\r
924 Count++;\r
925 }\r
926 }\r
927\r
928 return Count;\r
929}\r
930\r
10ccc27c
MK
931/**\r
932 Store the ModHandle in an array indexed by the Pdb File name.\r
933 The ModHandle is needed to unload the image.\r
934 @param ImageContext - Input data returned from PE Laoder Library. Used to find the\r
935 .PDB file name of the PE Image.\r
936 @param ModHandle - Returned from LoadLibraryEx() and stored for call to\r
937 FreeLibrary().\r
938 @return return EFI_SUCCESS when ModHandle was stored.\r
939--*/\r
940EFI_STATUS\r
941AddModHandle (\r
a550d468
MK
942 IN PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext,\r
943 IN VOID *ModHandle\r
10ccc27c
MK
944 )\r
945\r
946{\r
947 UINTN Index;\r
948 PDB_NAME_TO_MOD_HANDLE *Array;\r
949 UINTN PreviousSize;\r
950 PDB_NAME_TO_MOD_HANDLE *TempArray;\r
951 HANDLE Handle;\r
952 UINTN Size;\r
953\r
954 //\r
955 // Return EFI_ALREADY_STARTED if this DLL has already been loaded\r
956 //\r
957 Array = mPdbNameModHandleArray;\r
958 for (Index = 0; Index < mPdbNameModHandleArraySize; Index++, Array++) {\r
a550d468 959 if ((Array->PdbPointer != NULL) && (Array->ModHandle == ModHandle)) {\r
10ccc27c
MK
960 return EFI_ALREADY_STARTED;\r
961 }\r
962 }\r
963\r
964 Array = mPdbNameModHandleArray;\r
965 for (Index = 0; Index < mPdbNameModHandleArraySize; Index++, Array++) {\r
966 if (Array->PdbPointer == NULL) {\r
967 //\r
968 // Make a copy of the stirng and store the ModHandle\r
969 //\r
a550d468
MK
970 Handle = GetProcessHeap ();\r
971 Size = AsciiStrLen (ImageContext->PdbPointer) + 1;\r
972 Array->PdbPointer = HeapAlloc (Handle, HEAP_ZERO_MEMORY, Size);\r
10ccc27c
MK
973 ASSERT (Array->PdbPointer != NULL);\r
974\r
975 AsciiStrCpyS (Array->PdbPointer, Size, ImageContext->PdbPointer);\r
976 Array->ModHandle = ModHandle;\r
977 return EFI_SUCCESS;\r
978 }\r
979 }\r
980\r
981 //\r
982 // No free space in mPdbNameModHandleArray so grow it by\r
983 // MAX_PDB_NAME_TO_MOD_HANDLE_ARRAY_SIZE entires.\r
984 //\r
a550d468 985 PreviousSize = mPdbNameModHandleArraySize * sizeof (PDB_NAME_TO_MOD_HANDLE);\r
10ccc27c
MK
986 mPdbNameModHandleArraySize += MAX_PDB_NAME_TO_MOD_HANDLE_ARRAY_SIZE;\r
987 //\r
988 // re-allocate a new buffer and copy the old values to the new locaiton.\r
989 //\r
a550d468
MK
990 TempArray = HeapAlloc (\r
991 GetProcessHeap (),\r
992 HEAP_ZERO_MEMORY,\r
993 mPdbNameModHandleArraySize * sizeof (PDB_NAME_TO_MOD_HANDLE)\r
994 );\r
10ccc27c 995\r
a550d468 996 CopyMem ((VOID *)(UINTN)TempArray, (VOID *)(UINTN)mPdbNameModHandleArray, PreviousSize);\r
10ccc27c
MK
997\r
998 HeapFree (GetProcessHeap (), 0, mPdbNameModHandleArray);\r
999\r
1000 mPdbNameModHandleArray = TempArray;\r
1001 if (mPdbNameModHandleArray == NULL) {\r
1002 ASSERT (FALSE);\r
1003 return EFI_OUT_OF_RESOURCES;\r
1004 }\r
1005\r
1006 return AddModHandle (ImageContext, ModHandle);\r
1007}\r
1008\r
1009/**\r
1010 Return the ModHandle and delete the entry in the array.\r
1011 @param ImageContext - Input data returned from PE Laoder Library. Used to find the\r
1012 .PDB file name of the PE Image.\r
1013 @return\r
1014 ModHandle - ModHandle assoicated with ImageContext is returned\r
1015 NULL - No ModHandle associated with ImageContext\r
1016**/\r
1017VOID *\r
1018RemoveModHandle (\r
a550d468 1019 IN PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r
10ccc27c
MK
1020 )\r
1021{\r
1022 UINTN Index;\r
1023 PDB_NAME_TO_MOD_HANDLE *Array;\r
1024\r
1025 if (ImageContext->PdbPointer == NULL) {\r
1026 //\r
1027 // If no PDB pointer there is no ModHandle so return NULL\r
1028 //\r
1029 return NULL;\r
1030 }\r
1031\r
1032 Array = mPdbNameModHandleArray;\r
1033 for (Index = 0; Index < mPdbNameModHandleArraySize; Index++, Array++) {\r
a550d468 1034 if ((Array->PdbPointer != NULL) && (AsciiStrCmp (Array->PdbPointer, ImageContext->PdbPointer) == 0)) {\r
10ccc27c
MK
1035 //\r
1036 // If you find a match return it and delete the entry\r
1037 //\r
1038 HeapFree (GetProcessHeap (), 0, Array->PdbPointer);\r
1039 Array->PdbPointer = NULL;\r
1040 return Array->ModHandle;\r
1041 }\r
1042 }\r
1043\r
1044 return NULL;\r
1045}\r
3c859dfe
RN
1046\r
1047VOID\r
1048EFIAPI\r
1049PeCoffLoaderRelocateImageExtraAction (\r
a550d468 1050 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r
3c859dfe
RN
1051 )\r
1052{\r
a550d468
MK
1053 EFI_STATUS Status;\r
1054 VOID *DllEntryPoint;\r
1055 CHAR16 *DllFileName;\r
1056 HMODULE Library;\r
1057 UINTN Index;\r
3c859dfe
RN
1058\r
1059 ASSERT (ImageContext != NULL);\r
1060 //\r
1061 // If we load our own PE COFF images the Windows debugger can not source\r
10ccc27c 1062 // level debug our code. If a valid PDB pointer exists use it to load\r
3c859dfe
RN
1063 // the *.dll file as a library using Windows* APIs. This allows\r
1064 // source level debug. The image is still loaded and relocated\r
1065 // in the Framework memory space like on a real system (by the code above),\r
3d6b7fd3 1066 // but the entry point points into the DLL loaded by the code below.\r
3c859dfe
RN
1067 //\r
1068\r
1069 DllEntryPoint = NULL;\r
1070\r
1071 //\r
1072 // Load the DLL if it's not an EBC image.\r
1073 //\r
1074 if ((ImageContext->PdbPointer != NULL) &&\r
a550d468
MK
1075 (ImageContext->Machine != EFI_IMAGE_MACHINE_EBC))\r
1076 {\r
3c859dfe
RN
1077 //\r
1078 // Convert filename from ASCII to Unicode\r
1079 //\r
1080 DllFileName = AsciiToUnicode (ImageContext->PdbPointer, &Index);\r
1081\r
1082 //\r
1083 // Check that we have a valid filename\r
1084 //\r
a550d468 1085 if ((Index < 5) || (DllFileName[Index - 4] != '.')) {\r
3c859dfe
RN
1086 free (DllFileName);\r
1087\r
1088 //\r
1089 // Never return an error if PeCoffLoaderRelocateImage() succeeded.\r
1090 // The image will run, but we just can't source level debug. If we\r
1091 // return an error the image will not run.\r
1092 //\r
1093 return;\r
1094 }\r
a550d468 1095\r
3c859dfe
RN
1096 //\r
1097 // Replace .PDB with .DLL on the filename\r
1098 //\r
a550d468
MK
1099 DllFileName[Index - 3] = 'D';\r
1100 DllFileName[Index - 2] = 'L';\r
1101 DllFileName[Index - 1] = 'L';\r
3c859dfe
RN
1102\r
1103 //\r
1104 // Load the .DLL file into the user process's address space for source\r
1105 // level debug\r
1106 //\r
1107 Library = LoadLibraryEx (DllFileName, NULL, DONT_RESOLVE_DLL_REFERENCES);\r
1108 if (Library != NULL) {\r
1109 //\r
1110 // InitializeDriver is the entry point we put in all our EFI DLL's. The\r
1111 // DONT_RESOLVE_DLL_REFERENCES argument to LoadLIbraryEx() suppresses the\r
1112 // normal DLL entry point of DllMain, and prevents other modules that are\r
1113 // referenced in side the DllFileName from being loaded. There is no error\r
1114 // checking as the we can point to the PE32 image loaded by Tiano. This\r
1115 // step is only needed for source level debugging\r
1116 //\r
a550d468 1117 DllEntryPoint = (VOID *)(UINTN)GetProcAddress (Library, "InitializeDriver");\r
3c859dfe
RN
1118 }\r
1119\r
1120 if ((Library != NULL) && (DllEntryPoint != NULL)) {\r
10ccc27c
MK
1121 Status = AddModHandle (ImageContext, Library);\r
1122 if (Status == EFI_ALREADY_STARTED) {\r
1123 //\r
1124 // If the DLL has already been loaded before, then this instance of the DLL can not be debugged.\r
1125 //\r
1126 ImageContext->PdbPointer = NULL;\r
1127 SecPrint ("WARNING: DLL already loaded. No source level debug %S.\n\r", DllFileName);\r
1128 } else {\r
1129 //\r
1130 // This DLL is not already loaded, so source level debugging is supported.\r
1131 //\r
a550d468 1132 ImageContext->EntryPoint = (EFI_PHYSICAL_ADDRESS)(UINTN)DllEntryPoint;\r
10ccc27c
MK
1133 SecPrint ("LoadLibraryEx (\n\r %S,\n\r NULL, DONT_RESOLVE_DLL_REFERENCES)\n\r", DllFileName);\r
1134 }\r
3c859dfe 1135 } else {\r
10ccc27c 1136 SecPrint ("WARNING: No source level debug %S. \n\r", DllFileName);\r
3c859dfe
RN
1137 }\r
1138\r
1139 free (DllFileName);\r
1140 }\r
1141}\r
1142\r
1143VOID\r
1144EFIAPI\r
1145PeCoffLoaderUnloadImageExtraAction (\r
10ccc27c 1146 IN PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r
a550d468 1147 )\r
3c859dfe 1148{\r
10ccc27c
MK
1149 VOID *ModHandle;\r
1150\r
3c859dfe 1151 ASSERT (ImageContext != NULL);\r
3c859dfe 1152\r
10ccc27c
MK
1153 ModHandle = RemoveModHandle (ImageContext);\r
1154 if (ModHandle != NULL) {\r
1155 FreeLibrary (ModHandle);\r
1156 SecPrint ("FreeLibrary (\n\r %s)\n\r", ImageContext->PdbPointer);\r
1157 } else {\r
1158 SecPrint ("WARNING: Unload image without source level debug\n\r");\r
1159 }\r
1160}\r
3c859dfe
RN
1161\r
1162VOID\r
1163_ModuleEntryPoint (\r
1164 VOID\r
1165 )\r
1166{\r
1167}\r