]> git.proxmox.com Git - mirror_edk2.git/blame - EmulatorPkg/Win/Host/WinHost.c
EmulatorPkg: Add persistent memory in EmuThunkPpi
[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
22f73b6d 453 EMU_THUNK_PPI *SecEmuThunkPpi;\r
3c859dfe
RN
454\r
455 //\r
456 // Enable the privilege so that RTC driver can successfully run SetTime()\r
457 //\r
a550d468
MK
458 OpenProcessToken (GetCurrentProcess (), TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY, &Token);\r
459 if (LookupPrivilegeValue (NULL, SE_TIME_ZONE_NAME, &TokenPrivileges.Privileges[0].Luid)) {\r
460 TokenPrivileges.PrivilegeCount = 1;\r
3c859dfe 461 TokenPrivileges.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;\r
a550d468 462 AdjustTokenPrivileges (Token, FALSE, &TokenPrivileges, 0, (PTOKEN_PRIVILEGES)NULL, 0);\r
3c859dfe
RN
463 }\r
464\r
a550d468
MK
465 MemorySizeStr = (CHAR16 *)PcdGetPtr (PcdEmuMemorySize);\r
466 FirmwareVolumesStr = (CHAR16 *)PcdGetPtr (PcdEmuFirmwareVolume);\r
3c859dfe 467\r
30b4abc6 468 SecPrint ("\n\rEDK II WIN Host Emulation Environment from http://www.tianocore.org/edk2/\n\r");\r
3c859dfe
RN
469\r
470 //\r
471 // Determine the first thread available to this process.\r
472 //\r
473 if (GetProcessAffinityMask (GetCurrentProcess (), &ProcessAffinityMask, &SystemAffinityMask)) {\r
2737037a 474 LowBit = (INT32)LowBitSet32 ((UINT32)ProcessAffinityMask);\r
3c859dfe
RN
475 if (LowBit != -1) {\r
476 //\r
477 // Force the system to bind the process to a single thread to work\r
478 // around odd semaphore type crashes.\r
479 //\r
480 SetProcessAffinityMask (GetCurrentProcess (), (INTN)(BIT0 << LowBit));\r
481 }\r
482 }\r
483\r
484 //\r
485 // Make some Windows calls to Set the process to the highest priority in the\r
486 // idle class. We need this to have good performance.\r
487 //\r
488 SetPriorityClass (GetCurrentProcess (), IDLE_PRIORITY_CLASS);\r
489 SetThreadPriority (GetCurrentThread (), THREAD_PRIORITY_HIGHEST);\r
490\r
491 SecInitializeThunk ();\r
492 //\r
493 // PPIs pased into PEI_CORE\r
494 //\r
22f73b6d
ZL
495 SecEmuThunkPpi = AllocateZeroPool (sizeof (EMU_THUNK_PPI) + FixedPcdGet32 (PcdPersistentMemorySize));\r
496 if (SecEmuThunkPpi == NULL) {\r
497 SecPrint ("ERROR : Can not allocate memory for SecEmuThunkPpi. Exiting.\n");\r
498 exit (1);\r
499 }\r
500\r
501 CopyMem (SecEmuThunkPpi, &mSecEmuThunkPpi, sizeof (EMU_THUNK_PPI));\r
502 SecEmuThunkPpi->PersistentMemorySize = FixedPcdGet32 (PcdPersistentMemorySize);\r
503 AddThunkPpi (EFI_PEI_PPI_DESCRIPTOR_PPI, &gEmuThunkPpiGuid, SecEmuThunkPpi);\r
7bee2498 504 AddThunkPpi (EFI_PEI_PPI_DESCRIPTOR_PPI, &gEfiPeiReset2PpiGuid, &mEmuReset2Ppi);\r
3c859dfe 505\r
7a465451
RN
506 //\r
507 // Emulator Bus Driver Thunks\r
508 //\r
509 AddThunkProtocol (&mWinNtWndThunkIo, (CHAR16 *)PcdGetPtr (PcdEmuGop), TRUE);\r
56502bf1 510 AddThunkProtocol (&mWinNtFileSystemThunkIo, (CHAR16 *)PcdGetPtr (PcdEmuFileSystem), TRUE);\r
8f819697 511 AddThunkProtocol (&mWinNtBlockIoThunkIo, (CHAR16 *)PcdGetPtr (PcdEmuVirtualDisk), TRUE);\r
bb78cfbe 512 AddThunkProtocol (&mWinNtSnpThunkIo, (CHAR16 *)PcdGetPtr (PcdEmuNetworkInterface), TRUE);\r
7a465451 513\r
3c859dfe
RN
514 //\r
515 // Allocate space for gSystemMemory Array\r
516 //\r
a550d468
MK
517 gSystemMemoryCount = CountSeparatorsInString (MemorySizeStr, '!') + 1;\r
518 gSystemMemory = calloc (gSystemMemoryCount, sizeof (NT_SYSTEM_MEMORY));\r
3c859dfe 519 if (gSystemMemory == NULL) {\r
30b4abc6 520 SecPrint ("ERROR : Can not allocate memory for %S. Exiting.\n\r", MemorySizeStr);\r
3c859dfe
RN
521 exit (1);\r
522 }\r
523\r
d2842bb6
RN
524 //\r
525 // Allocate "physical" memory space for emulator. It will be reported out later throuth MemoryAutoScan()\r
526 //\r
527 for (Index = 0, Done = FALSE; !Done; Index++) {\r
528 ASSERT (Index < gSystemMemoryCount);\r
529 gSystemMemory[Index].Size = ((UINT64)_wtoi (MemorySizeStr)) * ((UINT64)SIZE_1MB);\r
530 gSystemMemory[Index].Memory = (EFI_PHYSICAL_ADDRESS)(UINTN)VirtualAlloc (NULL, (SIZE_T)(gSystemMemory[Index].Size), MEM_COMMIT, PAGE_EXECUTE_READWRITE);\r
531 if (gSystemMemory[Index].Memory == 0) {\r
532 return EFI_OUT_OF_RESOURCES;\r
533 }\r
534\r
535 //\r
536 // Find the next region\r
537 //\r
538 for (Index1 = 0; MemorySizeStr[Index1] != '!' && MemorySizeStr[Index1] != 0; Index1++) {\r
539 }\r
540\r
541 if (MemorySizeStr[Index1] == 0) {\r
542 Done = TRUE;\r
543 }\r
544\r
545 MemorySizeStr = MemorySizeStr + Index1 + 1;\r
546 }\r
547\r
3c859dfe
RN
548 //\r
549 // Allocate space for gSystemMemory Array\r
550 //\r
a550d468
MK
551 gFdInfoCount = CountSeparatorsInString (FirmwareVolumesStr, '!') + 1;\r
552 gFdInfo = calloc (gFdInfoCount, sizeof (NT_FD_INFO));\r
3c859dfe 553 if (gFdInfo == NULL) {\r
30b4abc6 554 SecPrint ("ERROR : Can not allocate memory for %S. Exiting.\n\r", FirmwareVolumesStr);\r
3c859dfe
RN
555 exit (1);\r
556 }\r
a550d468 557\r
3c859dfe
RN
558 //\r
559 // Setup Boot Mode.\r
560 //\r
30b4abc6 561 SecPrint (" BootMode 0x%02x\n\r", PcdGet32 (PcdEmuBootMode));\r
3c859dfe
RN
562\r
563 //\r
564 // Allocate 128K memory to emulate temp memory for PEI.\r
565 // on a real platform this would be SRAM, or using the cache as RAM.\r
566 // Set TemporaryRam to zero so WinNtOpenFile will allocate a new mapping\r
567 //\r
568 TemporaryRamSize = TEMPORARY_RAM_SIZE;\r
a550d468 569 TemporaryRam = VirtualAlloc (NULL, (SIZE_T)(TemporaryRamSize), MEM_COMMIT, PAGE_EXECUTE_READWRITE);\r
3c859dfe 570 if (TemporaryRam == NULL) {\r
30b4abc6 571 SecPrint ("ERROR : Can not allocate enough space for SecStack\n\r");\r
3c859dfe
RN
572 exit (1);\r
573 }\r
a550d468 574\r
3c859dfe
RN
575 //\r
576 // If enabled use the magic page to communicate between modules\r
577 // This replaces the PI PeiServicesTable pointer mechanism that\r
578 // deos not work in the emulator. It also allows the removal of\r
579 // writable globals from SEC, PEI_CORE (libraries), PEIMs\r
580 //\r
581 EmuMagicPage = (VOID *)(UINTN)(FixedPcdGet64 (PcdPeiServicesTablePage) & MAX_UINTN);\r
582 if (EmuMagicPage != NULL) {\r
583 UINT64 Size;\r
584 Status = WinNtOpenFile (\r
a550d468
MK
585 NULL,\r
586 SIZE_4KB,\r
587 0,\r
588 &EmuMagicPage,\r
589 &Size\r
590 );\r
3c859dfe 591 if (EFI_ERROR (Status)) {\r
30b4abc6 592 SecPrint ("ERROR : Could not allocate PeiServicesTablePage @ %p\n\r", EmuMagicPage);\r
3c859dfe
RN
593 return EFI_DEVICE_ERROR;\r
594 }\r
595 }\r
596\r
597 //\r
598 // Open All the firmware volumes and remember the info in the gFdInfo global\r
599 // Meanwhile, find the SEC Core.\r
600 //\r
601 FileNamePtr = AllocateCopyPool (StrSize (FirmwareVolumesStr), FirmwareVolumesStr);\r
602 if (FileNamePtr == NULL) {\r
30b4abc6 603 SecPrint ("ERROR : Can not allocate memory for firmware volume string\n\r");\r
3c859dfe
RN
604 exit (1);\r
605 }\r
606\r
607 for (Done = FALSE, Index = 0, SecFile = NULL; !Done; Index++) {\r
608 FileName = FileNamePtr;\r
a550d468
MK
609 for (Index1 = 0; (FileNamePtr[Index1] != '!') && (FileNamePtr[Index1] != 0); Index1++) {\r
610 }\r
611\r
3c859dfe
RN
612 if (FileNamePtr[Index1] == 0) {\r
613 Done = TRUE;\r
614 } else {\r
a550d468
MK
615 FileNamePtr[Index1] = '\0';\r
616 FileNamePtr = &FileNamePtr[Index1 + 1];\r
3c859dfe
RN
617 }\r
618\r
619 //\r
620 // Open the FD and remember where it got mapped into our processes address space\r
621 //\r
622 Status = WinNtOpenFile (\r
a550d468
MK
623 FileName,\r
624 0,\r
625 OPEN_EXISTING,\r
626 &gFdInfo[Index].Address,\r
627 &gFdInfo[Index].Size\r
628 );\r
3c859dfe 629 if (EFI_ERROR (Status)) {\r
30b4abc6 630 SecPrint ("ERROR : Can not open Firmware Device File %S (0x%X). Exiting.\n\r", FileName, Status);\r
3c859dfe
RN
631 exit (1);\r
632 }\r
633\r
30b4abc6 634 SecPrint (" FD loaded from %S", FileName);\r
3c859dfe
RN
635\r
636 if (SecFile == NULL) {\r
637 //\r
638 // Assume the beginning of the FD is an FV and look for the SEC Core.\r
639 // Load the first one we find.\r
640 //\r
641 FileHandle = NULL;\r
a550d468
MK
642 Status = PeiServicesFfsFindNextFile (\r
643 EFI_FV_FILETYPE_SECURITY_CORE,\r
644 (EFI_PEI_FV_HANDLE)gFdInfo[Index].Address,\r
645 &FileHandle\r
646 );\r
3c859dfe
RN
647 if (!EFI_ERROR (Status)) {\r
648 Status = PeiServicesFfsFindSectionData (EFI_SECTION_PE32, FileHandle, &SecFile);\r
649 if (!EFI_ERROR (Status)) {\r
650 SecPrint (" contains SEC Core");\r
651 }\r
652 }\r
653 }\r
654\r
30b4abc6 655 SecPrint ("\n\r");\r
3c859dfe 656 }\r
a550d468 657\r
7bee2498
RN
658 ResetJumpCode = SetJump (&mResetJumpBuffer);\r
659\r
660 //\r
661 // Do not clear memory content for warm reset.\r
662 //\r
663 if (ResetJumpCode != EfiResetWarm + 1) {\r
664 SecPrint (" OS Emulator clearing temp RAM and physical RAM (to be discovered later)......\n\r");\r
665 SetMem32 (TemporaryRam, TemporaryRamSize, PcdGet32 (PcdInitValueInTempStack));\r
666 for (Index = 0; Index < gSystemMemoryCount; Index++) {\r
667 SetMem32 ((VOID *)(UINTN)gSystemMemory[Index].Memory, (UINTN)gSystemMemory[Index].Size, PcdGet32 (PcdInitValueInTempStack));\r
668 }\r
669 }\r
670\r
671 SecPrint (\r
672 " OS Emulator passing in %u KB of temp RAM at 0x%08lx to SEC\n\r",\r
673 TemporaryRamSize / SIZE_1KB,\r
674 TemporaryRam\r
675 );\r
3c859dfe
RN
676 //\r
677 // Hand off to SEC Core\r
678 //\r
679 SecLoadSecCore ((UINTN)TemporaryRam, TemporaryRamSize, gFdInfo[0].Address, gFdInfo[0].Size, SecFile);\r
680\r
681 //\r
682 // If we get here, then the SEC Core returned. This is an error as SEC should\r
683 // always hand off to PEI Core and then on to DXE Core.\r
684 //\r
30b4abc6 685 SecPrint ("ERROR : SEC returned\n\r");\r
3c859dfe
RN
686 exit (1);\r
687}\r
688\r
689VOID\r
690SecLoadSecCore (\r
a550d468
MK
691 IN UINTN TemporaryRam,\r
692 IN UINTN TemporaryRamSize,\r
693 IN VOID *BootFirmwareVolumeBase,\r
694 IN UINTN BootFirmwareVolumeSize,\r
695 IN VOID *SecCorePe32File\r
3c859dfe 696 )\r
a550d468 697\r
3c859dfe
RN
698/*++\r
699\r
700Routine Description:\r
701 This is the service to load the SEC Core from the Firmware Volume\r
702\r
703Arguments:\r
704 TemporaryRam - Memory to use for SEC.\r
705 TemporaryRamSize - Size of Memory to use for SEC\r
706 BootFirmwareVolumeBase - Start of the Boot FV\r
707 SecCorePe32File - SEC Core PE32\r
708\r
709Returns:\r
3d6b7fd3 710 Success means control is transferred and thus we should never return\r
3c859dfe
RN
711\r
712--*/\r
713{\r
a550d468
MK
714 EFI_STATUS Status;\r
715 VOID *TopOfStack;\r
716 VOID *SecCoreEntryPoint;\r
717 EFI_SEC_PEI_HAND_OFF *SecCoreData;\r
718 UINTN SecStackSize;\r
3c859dfe
RN
719\r
720 //\r
721 // Compute Top Of Memory for Stack and PEI Core Allocations\r
722 //\r
723 SecStackSize = TemporaryRamSize >> 1;\r
724\r
725 //\r
726 // |-----------| <---- TemporaryRamBase + TemporaryRamSize\r
727 // | Heap |\r
728 // | |\r
729 // |-----------| <---- StackBase / PeiTemporaryMemoryBase\r
730 // | |\r
731 // | Stack |\r
732 // |-----------| <---- TemporaryRamBase\r
733 //\r
a550d468 734 TopOfStack = (VOID *)(TemporaryRam + SecStackSize);\r
3c859dfe
RN
735\r
736 //\r
737 // Reservet space for storing PeiCore's parament in stack.\r
738 //\r
a550d468
MK
739 TopOfStack = (VOID *)((UINTN)TopOfStack - sizeof (EFI_SEC_PEI_HAND_OFF) - CPU_STACK_ALIGNMENT);\r
740 TopOfStack = ALIGN_POINTER (TopOfStack, CPU_STACK_ALIGNMENT);\r
3c859dfe
RN
741\r
742 //\r
743 // Bind this information into the SEC hand-off state\r
744 //\r
a550d468 745 SecCoreData = (EFI_SEC_PEI_HAND_OFF *)(UINTN)TopOfStack;\r
3c859dfe
RN
746 SecCoreData->DataSize = sizeof (EFI_SEC_PEI_HAND_OFF);\r
747 SecCoreData->BootFirmwareVolumeBase = BootFirmwareVolumeBase;\r
748 SecCoreData->BootFirmwareVolumeSize = BootFirmwareVolumeSize;\r
a550d468 749 SecCoreData->TemporaryRamBase = (VOID *)TemporaryRam;\r
3c859dfe
RN
750 SecCoreData->TemporaryRamSize = TemporaryRamSize;\r
751 SecCoreData->StackBase = SecCoreData->TemporaryRamBase;\r
752 SecCoreData->StackSize = SecStackSize;\r
a550d468 753 SecCoreData->PeiTemporaryRamBase = (VOID *)((UINTN)SecCoreData->TemporaryRamBase + SecStackSize);\r
3c859dfe
RN
754 SecCoreData->PeiTemporaryRamSize = TemporaryRamSize - SecStackSize;\r
755\r
756 //\r
757 // Load the PEI Core from a Firmware Volume\r
758 //\r
759 Status = SecPeCoffGetEntryPoint (\r
a550d468
MK
760 SecCorePe32File,\r
761 &SecCoreEntryPoint\r
762 );\r
3c859dfe 763 if (EFI_ERROR (Status)) {\r
a550d468 764 return;\r
3c859dfe
RN
765 }\r
766\r
767 //\r
768 // Transfer control to the SEC Core\r
769 //\r
770 SwitchStack (\r
f4eaaf1a 771 (SWITCH_STACK_ENTRY_POINT)(UINTN)SecCoreEntryPoint,\r
3c859dfe
RN
772 SecCoreData,\r
773 GetThunkPpiList (),\r
774 TopOfStack\r
775 );\r
776 //\r
777 // If we get here, then the SEC Core returned. This is an error\r
778 //\r
a550d468 779 return;\r
3c859dfe
RN
780}\r
781\r
782RETURN_STATUS\r
783EFIAPI\r
784SecPeCoffGetEntryPoint (\r
785 IN VOID *Pe32Data,\r
786 IN OUT VOID **EntryPoint\r
787 )\r
788{\r
a550d468
MK
789 EFI_STATUS Status;\r
790 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;\r
3c859dfe
RN
791\r
792 ZeroMem (&ImageContext, sizeof (ImageContext));\r
a550d468 793 ImageContext.Handle = Pe32Data;\r
3c859dfe 794\r
a550d468 795 ImageContext.ImageRead = (PE_COFF_LOADER_READ_FILE)SecImageRead;\r
3c859dfe 796\r
a550d468 797 Status = PeCoffLoaderGetImageInfo (&ImageContext);\r
3c859dfe
RN
798 if (EFI_ERROR (Status)) {\r
799 return Status;\r
800 }\r
a550d468 801\r
3c859dfe 802 //\r
a121165e 803 // XIP for SEC and PEI_CORE\r
3c859dfe 804 //\r
a121165e 805 ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)(UINTN)Pe32Data;\r
3c859dfe
RN
806\r
807 Status = PeCoffLoaderLoadImage (&ImageContext);\r
808 if (EFI_ERROR (Status)) {\r
809 return Status;\r
810 }\r
811\r
812 Status = PeCoffLoaderRelocateImage (&ImageContext);\r
813 if (EFI_ERROR (Status)) {\r
814 return Status;\r
815 }\r
816\r
a550d468 817 *EntryPoint = (VOID *)(UINTN)ImageContext.EntryPoint;\r
3c859dfe
RN
818\r
819 return EFI_SUCCESS;\r
820}\r
821\r
822EFI_STATUS\r
823EFIAPI\r
824SecImageRead (\r
a550d468
MK
825 IN VOID *FileHandle,\r
826 IN UINTN FileOffset,\r
827 IN OUT UINTN *ReadSize,\r
828 OUT VOID *Buffer\r
3c859dfe 829 )\r
a550d468 830\r
3c859dfe
RN
831/*++\r
832\r
833Routine Description:\r
834 Support routine for the PE/COFF Loader that reads a buffer from a PE/COFF file\r
835\r
836Arguments:\r
837 FileHandle - The handle to the PE/COFF file\r
838 FileOffset - The offset, in bytes, into the file to read\r
839 ReadSize - The number of bytes to read from the file starting at FileOffset\r
840 Buffer - A pointer to the buffer to read the data into.\r
841\r
842Returns:\r
843 EFI_SUCCESS - ReadSize bytes of data were read into Buffer from the PE/COFF file starting at FileOffset\r
844\r
845--*/\r
846{\r
a550d468
MK
847 CHAR8 *Destination8;\r
848 CHAR8 *Source8;\r
849 UINTN Length;\r
3c859dfe 850\r
a550d468
MK
851 Destination8 = Buffer;\r
852 Source8 = (CHAR8 *)((UINTN)FileHandle + FileOffset);\r
853 Length = *ReadSize;\r
3c859dfe
RN
854 while (Length--) {\r
855 *(Destination8++) = *(Source8++);\r
856 }\r
857\r
858 return EFI_SUCCESS;\r
859}\r
860\r
861CHAR16 *\r
862AsciiToUnicode (\r
a550d468
MK
863 IN CHAR8 *Ascii,\r
864 IN UINTN *StrLen OPTIONAL\r
3c859dfe 865 )\r
a550d468 866\r
3c859dfe
RN
867/*++\r
868\r
869Routine Description:\r
870 Convert the passed in Ascii string to Unicode.\r
871 Optionally return the length of the strings.\r
872\r
873Arguments:\r
874 Ascii - Ascii string to convert\r
875 StrLen - Length of string\r
876\r
877Returns:\r
878 Pointer to malloc'ed Unicode version of Ascii\r
879\r
880--*/\r
881{\r
882 UINTN Index;\r
883 CHAR16 *Unicode;\r
884\r
885 //\r
886 // Allocate a buffer for unicode string\r
887 //\r
a550d468
MK
888 for (Index = 0; Ascii[Index] != '\0'; Index++) {\r
889 }\r
890\r
3c859dfe
RN
891 Unicode = malloc ((Index + 1) * sizeof (CHAR16));\r
892 if (Unicode == NULL) {\r
893 return NULL;\r
894 }\r
895\r
896 for (Index = 0; Ascii[Index] != '\0'; Index++) {\r
a550d468 897 Unicode[Index] = (CHAR16)Ascii[Index];\r
3c859dfe
RN
898 }\r
899\r
900 Unicode[Index] = '\0';\r
901\r
902 if (StrLen != NULL) {\r
903 *StrLen = Index;\r
904 }\r
905\r
906 return Unicode;\r
907}\r
908\r
909UINTN\r
910CountSeparatorsInString (\r
a550d468
MK
911 IN CONST CHAR16 *String,\r
912 IN CHAR16 Separator\r
3c859dfe 913 )\r
a550d468 914\r
3c859dfe
RN
915/*++\r
916\r
917Routine Description:\r
918 Count the number of separators in String\r
919\r
920Arguments:\r
921 String - String to process\r
922 Separator - Item to count\r
923\r
924Returns:\r
925 Number of Separator in String\r
926\r
927--*/\r
928{\r
a550d468 929 UINTN Count;\r
3c859dfe
RN
930\r
931 for (Count = 0; *String != '\0'; String++) {\r
932 if (*String == Separator) {\r
933 Count++;\r
934 }\r
935 }\r
936\r
937 return Count;\r
938}\r
939\r
10ccc27c
MK
940/**\r
941 Store the ModHandle in an array indexed by the Pdb File name.\r
942 The ModHandle is needed to unload the image.\r
943 @param ImageContext - Input data returned from PE Laoder Library. Used to find the\r
944 .PDB file name of the PE Image.\r
945 @param ModHandle - Returned from LoadLibraryEx() and stored for call to\r
946 FreeLibrary().\r
947 @return return EFI_SUCCESS when ModHandle was stored.\r
948--*/\r
949EFI_STATUS\r
950AddModHandle (\r
a550d468
MK
951 IN PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext,\r
952 IN VOID *ModHandle\r
10ccc27c
MK
953 )\r
954\r
955{\r
956 UINTN Index;\r
957 PDB_NAME_TO_MOD_HANDLE *Array;\r
958 UINTN PreviousSize;\r
959 PDB_NAME_TO_MOD_HANDLE *TempArray;\r
960 HANDLE Handle;\r
961 UINTN Size;\r
962\r
963 //\r
964 // Return EFI_ALREADY_STARTED if this DLL has already been loaded\r
965 //\r
966 Array = mPdbNameModHandleArray;\r
967 for (Index = 0; Index < mPdbNameModHandleArraySize; Index++, Array++) {\r
a550d468 968 if ((Array->PdbPointer != NULL) && (Array->ModHandle == ModHandle)) {\r
10ccc27c
MK
969 return EFI_ALREADY_STARTED;\r
970 }\r
971 }\r
972\r
973 Array = mPdbNameModHandleArray;\r
974 for (Index = 0; Index < mPdbNameModHandleArraySize; Index++, Array++) {\r
975 if (Array->PdbPointer == NULL) {\r
976 //\r
977 // Make a copy of the stirng and store the ModHandle\r
978 //\r
a550d468
MK
979 Handle = GetProcessHeap ();\r
980 Size = AsciiStrLen (ImageContext->PdbPointer) + 1;\r
981 Array->PdbPointer = HeapAlloc (Handle, HEAP_ZERO_MEMORY, Size);\r
10ccc27c
MK
982 ASSERT (Array->PdbPointer != NULL);\r
983\r
984 AsciiStrCpyS (Array->PdbPointer, Size, ImageContext->PdbPointer);\r
985 Array->ModHandle = ModHandle;\r
986 return EFI_SUCCESS;\r
987 }\r
988 }\r
989\r
990 //\r
991 // No free space in mPdbNameModHandleArray so grow it by\r
992 // MAX_PDB_NAME_TO_MOD_HANDLE_ARRAY_SIZE entires.\r
993 //\r
a550d468 994 PreviousSize = mPdbNameModHandleArraySize * sizeof (PDB_NAME_TO_MOD_HANDLE);\r
10ccc27c
MK
995 mPdbNameModHandleArraySize += MAX_PDB_NAME_TO_MOD_HANDLE_ARRAY_SIZE;\r
996 //\r
997 // re-allocate a new buffer and copy the old values to the new locaiton.\r
998 //\r
a550d468
MK
999 TempArray = HeapAlloc (\r
1000 GetProcessHeap (),\r
1001 HEAP_ZERO_MEMORY,\r
1002 mPdbNameModHandleArraySize * sizeof (PDB_NAME_TO_MOD_HANDLE)\r
1003 );\r
10ccc27c 1004\r
a550d468 1005 CopyMem ((VOID *)(UINTN)TempArray, (VOID *)(UINTN)mPdbNameModHandleArray, PreviousSize);\r
10ccc27c
MK
1006\r
1007 HeapFree (GetProcessHeap (), 0, mPdbNameModHandleArray);\r
1008\r
1009 mPdbNameModHandleArray = TempArray;\r
1010 if (mPdbNameModHandleArray == NULL) {\r
1011 ASSERT (FALSE);\r
1012 return EFI_OUT_OF_RESOURCES;\r
1013 }\r
1014\r
1015 return AddModHandle (ImageContext, ModHandle);\r
1016}\r
1017\r
1018/**\r
1019 Return the ModHandle and delete the entry in the array.\r
1020 @param ImageContext - Input data returned from PE Laoder Library. Used to find the\r
1021 .PDB file name of the PE Image.\r
1022 @return\r
1023 ModHandle - ModHandle assoicated with ImageContext is returned\r
1024 NULL - No ModHandle associated with ImageContext\r
1025**/\r
1026VOID *\r
1027RemoveModHandle (\r
a550d468 1028 IN PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r
10ccc27c
MK
1029 )\r
1030{\r
1031 UINTN Index;\r
1032 PDB_NAME_TO_MOD_HANDLE *Array;\r
1033\r
1034 if (ImageContext->PdbPointer == NULL) {\r
1035 //\r
1036 // If no PDB pointer there is no ModHandle so return NULL\r
1037 //\r
1038 return NULL;\r
1039 }\r
1040\r
1041 Array = mPdbNameModHandleArray;\r
1042 for (Index = 0; Index < mPdbNameModHandleArraySize; Index++, Array++) {\r
a550d468 1043 if ((Array->PdbPointer != NULL) && (AsciiStrCmp (Array->PdbPointer, ImageContext->PdbPointer) == 0)) {\r
10ccc27c
MK
1044 //\r
1045 // If you find a match return it and delete the entry\r
1046 //\r
1047 HeapFree (GetProcessHeap (), 0, Array->PdbPointer);\r
1048 Array->PdbPointer = NULL;\r
1049 return Array->ModHandle;\r
1050 }\r
1051 }\r
1052\r
1053 return NULL;\r
1054}\r
3c859dfe
RN
1055\r
1056VOID\r
1057EFIAPI\r
1058PeCoffLoaderRelocateImageExtraAction (\r
a550d468 1059 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r
3c859dfe
RN
1060 )\r
1061{\r
a550d468
MK
1062 EFI_STATUS Status;\r
1063 VOID *DllEntryPoint;\r
1064 CHAR16 *DllFileName;\r
1065 HMODULE Library;\r
1066 UINTN Index;\r
3c859dfe
RN
1067\r
1068 ASSERT (ImageContext != NULL);\r
1069 //\r
1070 // If we load our own PE COFF images the Windows debugger can not source\r
10ccc27c 1071 // level debug our code. If a valid PDB pointer exists use it to load\r
3c859dfe
RN
1072 // the *.dll file as a library using Windows* APIs. This allows\r
1073 // source level debug. The image is still loaded and relocated\r
1074 // in the Framework memory space like on a real system (by the code above),\r
3d6b7fd3 1075 // but the entry point points into the DLL loaded by the code below.\r
3c859dfe
RN
1076 //\r
1077\r
1078 DllEntryPoint = NULL;\r
1079\r
1080 //\r
1081 // Load the DLL if it's not an EBC image.\r
1082 //\r
1083 if ((ImageContext->PdbPointer != NULL) &&\r
a550d468
MK
1084 (ImageContext->Machine != EFI_IMAGE_MACHINE_EBC))\r
1085 {\r
3c859dfe
RN
1086 //\r
1087 // Convert filename from ASCII to Unicode\r
1088 //\r
1089 DllFileName = AsciiToUnicode (ImageContext->PdbPointer, &Index);\r
1090\r
1091 //\r
1092 // Check that we have a valid filename\r
1093 //\r
a550d468 1094 if ((Index < 5) || (DllFileName[Index - 4] != '.')) {\r
3c859dfe
RN
1095 free (DllFileName);\r
1096\r
1097 //\r
1098 // Never return an error if PeCoffLoaderRelocateImage() succeeded.\r
1099 // The image will run, but we just can't source level debug. If we\r
1100 // return an error the image will not run.\r
1101 //\r
1102 return;\r
1103 }\r
a550d468 1104\r
3c859dfe
RN
1105 //\r
1106 // Replace .PDB with .DLL on the filename\r
1107 //\r
a550d468
MK
1108 DllFileName[Index - 3] = 'D';\r
1109 DllFileName[Index - 2] = 'L';\r
1110 DllFileName[Index - 1] = 'L';\r
3c859dfe
RN
1111\r
1112 //\r
1113 // Load the .DLL file into the user process's address space for source\r
1114 // level debug\r
1115 //\r
1116 Library = LoadLibraryEx (DllFileName, NULL, DONT_RESOLVE_DLL_REFERENCES);\r
1117 if (Library != NULL) {\r
1118 //\r
1119 // InitializeDriver is the entry point we put in all our EFI DLL's. The\r
1120 // DONT_RESOLVE_DLL_REFERENCES argument to LoadLIbraryEx() suppresses the\r
1121 // normal DLL entry point of DllMain, and prevents other modules that are\r
1122 // referenced in side the DllFileName from being loaded. There is no error\r
1123 // checking as the we can point to the PE32 image loaded by Tiano. This\r
1124 // step is only needed for source level debugging\r
1125 //\r
a550d468 1126 DllEntryPoint = (VOID *)(UINTN)GetProcAddress (Library, "InitializeDriver");\r
3c859dfe
RN
1127 }\r
1128\r
1129 if ((Library != NULL) && (DllEntryPoint != NULL)) {\r
10ccc27c
MK
1130 Status = AddModHandle (ImageContext, Library);\r
1131 if (Status == EFI_ALREADY_STARTED) {\r
1132 //\r
1133 // If the DLL has already been loaded before, then this instance of the DLL can not be debugged.\r
1134 //\r
1135 ImageContext->PdbPointer = NULL;\r
1136 SecPrint ("WARNING: DLL already loaded. No source level debug %S.\n\r", DllFileName);\r
1137 } else {\r
1138 //\r
1139 // This DLL is not already loaded, so source level debugging is supported.\r
1140 //\r
a550d468 1141 ImageContext->EntryPoint = (EFI_PHYSICAL_ADDRESS)(UINTN)DllEntryPoint;\r
10ccc27c
MK
1142 SecPrint ("LoadLibraryEx (\n\r %S,\n\r NULL, DONT_RESOLVE_DLL_REFERENCES)\n\r", DllFileName);\r
1143 }\r
3c859dfe 1144 } else {\r
10ccc27c 1145 SecPrint ("WARNING: No source level debug %S. \n\r", DllFileName);\r
3c859dfe
RN
1146 }\r
1147\r
1148 free (DllFileName);\r
1149 }\r
1150}\r
1151\r
1152VOID\r
1153EFIAPI\r
1154PeCoffLoaderUnloadImageExtraAction (\r
10ccc27c 1155 IN PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r
a550d468 1156 )\r
3c859dfe 1157{\r
10ccc27c
MK
1158 VOID *ModHandle;\r
1159\r
3c859dfe 1160 ASSERT (ImageContext != NULL);\r
3c859dfe 1161\r
10ccc27c
MK
1162 ModHandle = RemoveModHandle (ImageContext);\r
1163 if (ModHandle != NULL) {\r
1164 FreeLibrary (ModHandle);\r
1165 SecPrint ("FreeLibrary (\n\r %s)\n\r", ImageContext->PdbPointer);\r
1166 } else {\r
1167 SecPrint ("WARNING: Unload image without source level debug\n\r");\r
1168 }\r
1169}\r
3c859dfe
RN
1170\r
1171VOID\r
1172_ModuleEntryPoint (\r
1173 VOID\r
1174 )\r
1175{\r
1176}\r