]> git.proxmox.com Git - mirror_edk2.git/blame - EmulatorPkg/Win/Host/WinHost.c
EmulatorPkg/Win: Enable native OS console as firmware console
[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
11Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r
12(C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>\r
13This program and the accompanying materials\r
14are licensed and made available under the terms and conditions of the BSD License\r
15which accompanies this distribution. The full text of the license may be found at\r
16http://opensource.org/licenses/bsd-license.php\r
17\r
18THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
19WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
20**/\r
21\r
22#include "WinHost.h"\r
23\r
24#ifndef SE_TIME_ZONE_NAME\r
25#define SE_TIME_ZONE_NAME TEXT("SeTimeZonePrivilege")\r
26#endif\r
27\r
28//\r
29// Default information about where the FD is located.\r
30// This array gets filled in with information from PcdWinNtFirmwareVolume\r
31// The number of array elements is allocated base on parsing\r
32// PcdWinNtFirmwareVolume and the memory is never freed.\r
33//\r
34UINTN gFdInfoCount = 0;\r
35NT_FD_INFO *gFdInfo;\r
36\r
37//\r
38// Array that supports seperate memory rantes.\r
39// The memory ranges are set by PcdWinNtMemorySizeForSecMain.\r
40// The number of array elements is allocated base on parsing\r
41// PcdWinNtMemorySizeForSecMain value and the memory is never freed.\r
42//\r
43UINTN gSystemMemoryCount = 0;\r
44NT_SYSTEM_MEMORY *gSystemMemory;\r
45\r
46/*++\r
47\r
48Routine Description:\r
49 This service is called from Index == 0 until it returns EFI_UNSUPPORTED.\r
50 It allows discontinuous memory regions to be supported by the emulator.\r
51 It uses gSystemMemory[] and gSystemMemoryCount that were created by\r
52 parsing the host environment variable EFI_MEMORY_SIZE.\r
53 The size comes from the varaible and the address comes from the call to\r
54 UnixOpenFile.\r
55\r
56Arguments:\r
57 Index - Which memory region to use\r
58 MemoryBase - Return Base address of memory region\r
59 MemorySize - Return size in bytes of the memory region\r
60\r
61Returns:\r
62 EFI_SUCCESS - If memory region was mapped\r
63 EFI_UNSUPPORTED - If Index is not supported\r
64\r
65**/\r
66EFI_STATUS\r
67WinPeiAutoScan (\r
68 IN UINTN Index,\r
69 OUT EFI_PHYSICAL_ADDRESS *MemoryBase,\r
70 OUT UINT64 *MemorySize\r
71 )\r
72{\r
73 if (Index >= gSystemMemoryCount) {\r
74 return EFI_UNSUPPORTED;\r
75 }\r
76\r
77 //\r
78 // Allocate enough memory space for emulator\r
79 //\r
80 gSystemMemory[Index].Memory = (EFI_PHYSICAL_ADDRESS) (UINTN) VirtualAlloc (NULL, (SIZE_T) (gSystemMemory[Index].Size), MEM_COMMIT, PAGE_EXECUTE_READWRITE);\r
81 if (gSystemMemory[Index].Memory == 0) {\r
82 return EFI_OUT_OF_RESOURCES;\r
83 }\r
84\r
85 *MemoryBase = gSystemMemory[Index].Memory;\r
86 *MemorySize = gSystemMemory[Index].Size;\r
87\r
88 return EFI_SUCCESS;\r
89}\r
90\r
91/*++\r
92\r
93Routine Description:\r
94 Return the FD Size and base address. Since the FD is loaded from a\r
95 file into host memory only the SEC will know it's address.\r
96\r
97Arguments:\r
98 Index - Which FD, starts at zero.\r
99 FdSize - Size of the FD in bytes\r
100 FdBase - Start address of the FD. Assume it points to an FV Header\r
101 FixUp - Difference between actual FD address and build address\r
102\r
103Returns:\r
104 EFI_SUCCESS - Return the Base address and size of the FV\r
105 EFI_UNSUPPORTED - Index does nto map to an FD in the system\r
106\r
107**/\r
108EFI_STATUS\r
109WinFdAddress (\r
110 IN UINTN Index,\r
111 IN OUT EFI_PHYSICAL_ADDRESS *FdBase,\r
112 IN OUT UINT64 *FdSize,\r
113 IN OUT EFI_PHYSICAL_ADDRESS *FixUp\r
114 )\r
115{\r
116 if (Index >= gFdInfoCount) {\r
117 return EFI_UNSUPPORTED;\r
118 }\r
119\r
120\r
121 *FdBase = (EFI_PHYSICAL_ADDRESS)(UINTN)gFdInfo[Index].Address;\r
122 *FdSize = (UINT64)gFdInfo[Index].Size;\r
123 *FixUp = 0;\r
124\r
125 if (*FdBase == 0 && *FdSize == 0) {\r
126 return EFI_UNSUPPORTED;\r
127 }\r
128\r
129 if (Index == 0) {\r
130 //\r
131 // FD 0 has XIP code and well known PCD values\r
132 // If the memory buffer could not be allocated at the FD build address\r
133 // the Fixup is the difference.\r
134 //\r
135 *FixUp = *FdBase - PcdGet64 (PcdEmuFdBaseAddress);\r
136 }\r
137\r
138 return EFI_SUCCESS;\r
139}\r
140\r
141/*++\r
142\r
143Routine Description:\r
144 Since the SEC is the only Unix program in stack it must export\r
145 an interface to do POSIX calls. gUnix is initialized in UnixThunk.c.\r
146\r
147Arguments:\r
148 InterfaceSize - sizeof (EFI_WIN_NT_THUNK_PROTOCOL);\r
149 InterfaceBase - Address of the gUnix global\r
150\r
151Returns:\r
152 EFI_SUCCESS - Data returned\r
153\r
154**/\r
155VOID *\r
156WinThunk (\r
157 VOID\r
158 )\r
159{\r
160 return &gEmuThunkProtocol;\r
161}\r
162\r
163\r
164EMU_THUNK_PPI mSecEmuThunkPpi = {\r
165 WinPeiAutoScan,\r
166 WinFdAddress,\r
167 WinThunk\r
168};\r
169\r
170VOID\r
171SecPrint (\r
172 CHAR8 *Format,\r
173 ...\r
174 )\r
175{\r
176 va_list Marker;\r
177 UINTN CharCount;\r
178 CHAR8 Buffer[0x1000];\r
179\r
180 va_start (Marker, Format);\r
181\r
182 _vsnprintf (Buffer, sizeof (Buffer), Format, Marker);\r
183\r
184 va_end (Marker);\r
185\r
186 CharCount = strlen (Buffer);\r
187 WriteFile (\r
188 GetStdHandle (STD_OUTPUT_HANDLE),\r
189 Buffer,\r
190 (DWORD)CharCount,\r
191 (LPDWORD)&CharCount,\r
192 NULL\r
193 );\r
194}\r
195\r
196/*++\r
197\r
198Routine Description:\r
199 Check to see if an address range is in the EFI GCD memory map.\r
200\r
201 This is all of GCD for system memory passed to DXE Core. FV\r
202 mapping and other device mapped into system memory are not\r
203 inlcuded in the check.\r
204\r
205Arguments:\r
206 Index - Which memory region to use\r
207 MemoryBase - Return Base address of memory region\r
208 MemorySize - Return size in bytes of the memory region\r
209\r
210Returns:\r
211 TRUE - Address is in the EFI GCD memory map\r
212 FALSE - Address is NOT in memory map\r
213\r
214**/\r
215BOOLEAN\r
216EfiSystemMemoryRange (\r
217 IN VOID *MemoryAddress\r
218 )\r
219{\r
220 UINTN Index;\r
221 EFI_PHYSICAL_ADDRESS MemoryBase;\r
222\r
223 MemoryBase = (EFI_PHYSICAL_ADDRESS)(UINTN)MemoryAddress;\r
224 for (Index = 0; Index < gSystemMemoryCount; Index++) {\r
225 if ((MemoryBase >= gSystemMemory[Index].Memory) &&\r
226 (MemoryBase < (gSystemMemory[Index].Memory + gSystemMemory[Index].Size)) ) {\r
227 return TRUE;\r
228 }\r
229 }\r
230\r
231 return FALSE;\r
232}\r
233\r
234\r
235EFI_STATUS\r
236WinNtOpenFile (\r
237 IN CHAR16 *FileName, OPTIONAL\r
238 IN UINT32 MapSize,\r
239 IN DWORD CreationDisposition,\r
240 IN OUT VOID **BaseAddress,\r
241 OUT UINTN *Length\r
242 )\r
243/*++\r
244\r
245Routine Description:\r
246 Opens and memory maps a file using WinNt services. If *BaseAddress is non zero\r
247 the process will try and allocate the memory starting at BaseAddress.\r
248\r
249Arguments:\r
250 FileName - The name of the file to open and map\r
251 MapSize - The amount of the file to map in bytes\r
252 CreationDisposition - The flags to pass to CreateFile(). Use to create new files for\r
253 memory emulation, and exiting files for firmware volume emulation\r
254 BaseAddress - The base address of the mapped file in the user address space.\r
255 If *BaseAddress is 0, the new memory region is used.\r
256 If *BaseAddress is not 0, the request memory region is used for\r
257 the mapping of the file into the process space.\r
258 Length - The size of the mapped region in bytes\r
259\r
260Returns:\r
261 EFI_SUCCESS - The file was opened and mapped.\r
262 EFI_NOT_FOUND - FileName was not found in the current directory\r
263 EFI_DEVICE_ERROR - An error occured attempting to map the opened file\r
264\r
265--*/\r
266{\r
267 HANDLE NtFileHandle;\r
268 HANDLE NtMapHandle;\r
269 VOID *VirtualAddress;\r
270 UINTN FileSize;\r
271\r
272 //\r
273 // Use Win API to open/create a file\r
274 //\r
275 NtFileHandle = INVALID_HANDLE_VALUE;\r
276 if (FileName != NULL) {\r
277 NtFileHandle = CreateFile (\r
278 FileName,\r
279 GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE,\r
280 FILE_SHARE_READ,\r
281 NULL,\r
282 CreationDisposition,\r
283 FILE_ATTRIBUTE_NORMAL,\r
284 NULL\r
285 );\r
286 if (NtFileHandle == INVALID_HANDLE_VALUE) {\r
287 return EFI_NOT_FOUND;\r
288 }\r
289 }\r
290 //\r
291 // Map the open file into a memory range\r
292 //\r
293 NtMapHandle = CreateFileMapping (\r
294 NtFileHandle,\r
295 NULL,\r
296 PAGE_EXECUTE_READWRITE,\r
297 0,\r
298 MapSize,\r
299 NULL\r
300 );\r
301 if (NtMapHandle == NULL) {\r
302 return EFI_DEVICE_ERROR;\r
303 }\r
304 //\r
305 // Get the virtual address (address in the emulator) of the mapped file\r
306 //\r
307 VirtualAddress = MapViewOfFileEx (\r
308 NtMapHandle,\r
309 FILE_MAP_EXECUTE | FILE_MAP_ALL_ACCESS,\r
310 0,\r
311 0,\r
312 MapSize,\r
313 *BaseAddress\r
314 );\r
315 if (VirtualAddress == NULL) {\r
316 return EFI_DEVICE_ERROR;\r
317 }\r
318\r
319 if (MapSize == 0) {\r
320 //\r
321 // Seek to the end of the file to figure out the true file size.\r
322 //\r
323 FileSize = SetFilePointer (\r
324 NtFileHandle,\r
325 0,\r
326 NULL,\r
327 FILE_END\r
328 );\r
329 if (FileSize == -1) {\r
330 return EFI_DEVICE_ERROR;\r
331 }\r
332\r
333 *Length = FileSize;\r
334 } else {\r
335 *Length = MapSize;\r
336 }\r
337\r
338 *BaseAddress = VirtualAddress;\r
339\r
340 return EFI_SUCCESS;\r
341}\r
342\r
343INTN\r
344EFIAPI\r
345main (\r
346 IN INTN Argc,\r
347 IN CHAR8 **Argv,\r
348 IN CHAR8 **Envp\r
349 )\r
350/*++\r
351\r
352Routine Description:\r
353 Main entry point to SEC for WinNt. This is a Windows program\r
354\r
355Arguments:\r
356 Argc - Number of command line arguments\r
357 Argv - Array of command line argument strings\r
358 Envp - Array of environment variable strings\r
359\r
360Returns:\r
361 0 - Normal exit\r
362 1 - Abnormal exit\r
363\r
364--*/\r
365{\r
366 EFI_STATUS Status;\r
367 HANDLE Token;\r
368 TOKEN_PRIVILEGES TokenPrivileges;\r
369 VOID *TemporaryRam;\r
370 UINT32 TemporaryRamSize;\r
371 VOID *EmuMagicPage;\r
372 UINTN Index;\r
373 UINTN Index1;\r
374 CHAR16 *FileName;\r
375 CHAR16 *FileNamePtr;\r
376 BOOLEAN Done;\r
377 EFI_PEI_FILE_HANDLE FileHandle;\r
378 VOID *SecFile;\r
379 CHAR16 *MemorySizeStr;\r
380 CHAR16 *FirmwareVolumesStr;\r
381 UINT32 ProcessAffinityMask;\r
382 UINT32 SystemAffinityMask;\r
383 INT32 LowBit;\r
384\r
385 //\r
386 // Enable the privilege so that RTC driver can successfully run SetTime()\r
387 //\r
388 OpenProcessToken (GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY, &Token);\r
389 if (LookupPrivilegeValue(NULL, SE_TIME_ZONE_NAME, &TokenPrivileges.Privileges[0].Luid)) {\r
390 TokenPrivileges.PrivilegeCount = 1;\r
391 TokenPrivileges.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;\r
392 AdjustTokenPrivileges(Token, FALSE, &TokenPrivileges, 0, (PTOKEN_PRIVILEGES) NULL, 0);\r
393 }\r
394\r
395 MemorySizeStr = (CHAR16 *) PcdGetPtr (PcdEmuMemorySize);\r
396 FirmwareVolumesStr = (CHAR16 *) PcdGetPtr (PcdEmuFirmwareVolume);\r
397\r
398 SecPrint ("\nEDK II WIN Host Emulation Environment from http://www.tianocore.org/edk2/\n");\r
399\r
400 //\r
401 // Determine the first thread available to this process.\r
402 //\r
403 if (GetProcessAffinityMask (GetCurrentProcess (), &ProcessAffinityMask, &SystemAffinityMask)) {\r
404 LowBit = (INT32)LowBitSet32 (ProcessAffinityMask);\r
405 if (LowBit != -1) {\r
406 //\r
407 // Force the system to bind the process to a single thread to work\r
408 // around odd semaphore type crashes.\r
409 //\r
410 SetProcessAffinityMask (GetCurrentProcess (), (INTN)(BIT0 << LowBit));\r
411 }\r
412 }\r
413\r
414 //\r
415 // Make some Windows calls to Set the process to the highest priority in the\r
416 // idle class. We need this to have good performance.\r
417 //\r
418 SetPriorityClass (GetCurrentProcess (), IDLE_PRIORITY_CLASS);\r
419 SetThreadPriority (GetCurrentThread (), THREAD_PRIORITY_HIGHEST);\r
420\r
421 SecInitializeThunk ();\r
422 //\r
423 // PPIs pased into PEI_CORE\r
424 //\r
425 AddThunkPpi (EFI_PEI_PPI_DESCRIPTOR_PPI, &gEmuThunkPpiGuid, &mSecEmuThunkPpi);\r
426\r
427 //\r
428 // Allocate space for gSystemMemory Array\r
429 //\r
430 gSystemMemoryCount = CountSeparatorsInString (MemorySizeStr, '!') + 1;\r
431 gSystemMemory = calloc (gSystemMemoryCount, sizeof (NT_SYSTEM_MEMORY));\r
432 if (gSystemMemory == NULL) {\r
433 SecPrint ("ERROR : Can not allocate memory for %S. Exiting.\n", MemorySizeStr);\r
434 exit (1);\r
435 }\r
436\r
437 //\r
438 // Allocate space for gSystemMemory Array\r
439 //\r
440 gFdInfoCount = CountSeparatorsInString (FirmwareVolumesStr, '!') + 1;\r
441 gFdInfo = calloc (gFdInfoCount, sizeof (NT_FD_INFO));\r
442 if (gFdInfo == NULL) {\r
443 SecPrint ("ERROR : Can not allocate memory for %S. Exiting.\n", FirmwareVolumesStr);\r
444 exit (1);\r
445 }\r
446 //\r
447 // Setup Boot Mode.\r
448 //\r
449 SecPrint (" BootMode 0x%02x\n", PcdGet32 (PcdEmuBootMode));\r
450\r
451 //\r
452 // Allocate 128K memory to emulate temp memory for PEI.\r
453 // on a real platform this would be SRAM, or using the cache as RAM.\r
454 // Set TemporaryRam to zero so WinNtOpenFile will allocate a new mapping\r
455 //\r
456 TemporaryRamSize = TEMPORARY_RAM_SIZE;\r
457 TemporaryRam = VirtualAlloc (NULL, (SIZE_T) (TemporaryRamSize), MEM_COMMIT, PAGE_EXECUTE_READWRITE);\r
458 if (TemporaryRam == NULL) {\r
459 SecPrint ("ERROR : Can not allocate enough space for SecStack\n");\r
460 exit (1);\r
461 }\r
462 SetMemN (TemporaryRam, TemporaryRamSize, PcdGet32 (PcdInitValueInTempStack));\r
463\r
464 SecPrint (" OS Emulator passing in %u KB of temp RAM at 0x%08lx to SEC\n",\r
465 TemporaryRamSize / SIZE_1KB,\r
466 TemporaryRam\r
467 );\r
468\r
469 //\r
470 // If enabled use the magic page to communicate between modules\r
471 // This replaces the PI PeiServicesTable pointer mechanism that\r
472 // deos not work in the emulator. It also allows the removal of\r
473 // writable globals from SEC, PEI_CORE (libraries), PEIMs\r
474 //\r
475 EmuMagicPage = (VOID *)(UINTN)(FixedPcdGet64 (PcdPeiServicesTablePage) & MAX_UINTN);\r
476 if (EmuMagicPage != NULL) {\r
477 UINT64 Size;\r
478 Status = WinNtOpenFile (\r
479 NULL,\r
480 SIZE_4KB,\r
481 0,\r
482 &EmuMagicPage,\r
483 &Size\r
484 );\r
485 if (EFI_ERROR (Status)) {\r
486 SecPrint ("ERROR : Could not allocate PeiServicesTablePage @ %p\n", EmuMagicPage);\r
487 return EFI_DEVICE_ERROR;\r
488 }\r
489 }\r
490\r
491 //\r
492 // Open All the firmware volumes and remember the info in the gFdInfo global\r
493 // Meanwhile, find the SEC Core.\r
494 //\r
495 FileNamePtr = AllocateCopyPool (StrSize (FirmwareVolumesStr), FirmwareVolumesStr);\r
496 if (FileNamePtr == NULL) {\r
497 SecPrint ("ERROR : Can not allocate memory for firmware volume string\n");\r
498 exit (1);\r
499 }\r
500\r
501 for (Done = FALSE, Index = 0, SecFile = NULL; !Done; Index++) {\r
502 FileName = FileNamePtr;\r
503 for (Index1 = 0; (FileNamePtr[Index1] != '!') && (FileNamePtr[Index1] != 0); Index1++)\r
504 ;\r
505 if (FileNamePtr[Index1] == 0) {\r
506 Done = TRUE;\r
507 } else {\r
508 FileNamePtr[Index1] = '\0';\r
509 FileNamePtr = &FileNamePtr[Index1 + 1];\r
510 }\r
511\r
512 //\r
513 // Open the FD and remember where it got mapped into our processes address space\r
514 //\r
515 Status = WinNtOpenFile (\r
516 FileName,\r
517 0,\r
518 OPEN_EXISTING,\r
519 &gFdInfo[Index].Address,\r
520 &gFdInfo[Index].Size\r
521 );\r
522 if (EFI_ERROR (Status)) {\r
523 SecPrint ("ERROR : Can not open Firmware Device File %S (0x%X). Exiting.\n", FileName, Status);\r
524 exit (1);\r
525 }\r
526\r
527 SecPrint (" FD loaded from %S\n", FileName);\r
528\r
529 if (SecFile == NULL) {\r
530 //\r
531 // Assume the beginning of the FD is an FV and look for the SEC Core.\r
532 // Load the first one we find.\r
533 //\r
534 FileHandle = NULL;\r
535 Status = PeiServicesFfsFindNextFile (\r
536 EFI_FV_FILETYPE_SECURITY_CORE,\r
537 (EFI_PEI_FV_HANDLE)gFdInfo[Index].Address,\r
538 &FileHandle\r
539 );\r
540 if (!EFI_ERROR (Status)) {\r
541 Status = PeiServicesFfsFindSectionData (EFI_SECTION_PE32, FileHandle, &SecFile);\r
542 if (!EFI_ERROR (Status)) {\r
543 SecPrint (" contains SEC Core");\r
544 }\r
545 }\r
546 }\r
547\r
548 SecPrint ("\n");\r
549 }\r
550 //\r
551 // Calculate memory regions and store the information in the gSystemMemory\r
552 // global for later use. The autosizing code will use this data to\r
553 // map this memory into the SEC process memory space.\r
554 //\r
555 for (Index = 0, Done = FALSE; !Done; Index++) {\r
556 //\r
557 // Save the size of the memory and make a Unicode filename SystemMemory00, ...\r
558 //\r
559 gSystemMemory[Index].Size = _wtoi (MemorySizeStr) * SIZE_1MB;\r
560\r
561 //\r
562 // Find the next region\r
563 //\r
564 for (Index1 = 0; MemorySizeStr[Index1] != '!' && MemorySizeStr[Index1] != 0; Index1++)\r
565 ;\r
566 if (MemorySizeStr[Index1] == 0) {\r
567 Done = TRUE;\r
568 }\r
569\r
570 MemorySizeStr = MemorySizeStr + Index1 + 1;\r
571 }\r
572\r
573 SecPrint ("\n");\r
574\r
575 //\r
576 // Hand off to SEC Core\r
577 //\r
578 SecLoadSecCore ((UINTN)TemporaryRam, TemporaryRamSize, gFdInfo[0].Address, gFdInfo[0].Size, SecFile);\r
579\r
580 //\r
581 // If we get here, then the SEC Core returned. This is an error as SEC should\r
582 // always hand off to PEI Core and then on to DXE Core.\r
583 //\r
584 SecPrint ("ERROR : SEC returned\n");\r
585 exit (1);\r
586}\r
587\r
588VOID\r
589SecLoadSecCore (\r
590 IN UINTN TemporaryRam,\r
591 IN UINTN TemporaryRamSize,\r
592 IN VOID *BootFirmwareVolumeBase,\r
593 IN UINTN BootFirmwareVolumeSize,\r
594 IN VOID *SecCorePe32File\r
595 )\r
596/*++\r
597\r
598Routine Description:\r
599 This is the service to load the SEC Core from the Firmware Volume\r
600\r
601Arguments:\r
602 TemporaryRam - Memory to use for SEC.\r
603 TemporaryRamSize - Size of Memory to use for SEC\r
604 BootFirmwareVolumeBase - Start of the Boot FV\r
605 SecCorePe32File - SEC Core PE32\r
606\r
607Returns:\r
608 Success means control is transfered and thus we should never return\r
609\r
610--*/\r
611{\r
612 EFI_STATUS Status;\r
613 VOID *TopOfStack;\r
614 VOID *SecCoreEntryPoint;\r
615 EFI_SEC_PEI_HAND_OFF *SecCoreData;\r
616 UINTN SecStackSize;\r
617\r
618 //\r
619 // Compute Top Of Memory for Stack and PEI Core Allocations\r
620 //\r
621 SecStackSize = TemporaryRamSize >> 1;\r
622\r
623 //\r
624 // |-----------| <---- TemporaryRamBase + TemporaryRamSize\r
625 // | Heap |\r
626 // | |\r
627 // |-----------| <---- StackBase / PeiTemporaryMemoryBase\r
628 // | |\r
629 // | Stack |\r
630 // |-----------| <---- TemporaryRamBase\r
631 //\r
632 TopOfStack = (VOID *)(TemporaryRam + SecStackSize);\r
633\r
634 //\r
635 // Reservet space for storing PeiCore's parament in stack.\r
636 //\r
637 TopOfStack = (VOID *)((UINTN)TopOfStack - sizeof (EFI_SEC_PEI_HAND_OFF) - CPU_STACK_ALIGNMENT);\r
638 TopOfStack = ALIGN_POINTER (TopOfStack, CPU_STACK_ALIGNMENT);\r
639\r
640 //\r
641 // Bind this information into the SEC hand-off state\r
642 //\r
643 SecCoreData = (EFI_SEC_PEI_HAND_OFF*)(UINTN)TopOfStack;\r
644 SecCoreData->DataSize = sizeof (EFI_SEC_PEI_HAND_OFF);\r
645 SecCoreData->BootFirmwareVolumeBase = BootFirmwareVolumeBase;\r
646 SecCoreData->BootFirmwareVolumeSize = BootFirmwareVolumeSize;\r
647 SecCoreData->TemporaryRamBase = (VOID*)TemporaryRam;\r
648 SecCoreData->TemporaryRamSize = TemporaryRamSize;\r
649 SecCoreData->StackBase = SecCoreData->TemporaryRamBase;\r
650 SecCoreData->StackSize = SecStackSize;\r
651 SecCoreData->PeiTemporaryRamBase = (VOID*) ((UINTN) SecCoreData->TemporaryRamBase + SecStackSize);\r
652 SecCoreData->PeiTemporaryRamSize = TemporaryRamSize - SecStackSize;\r
653\r
654 //\r
655 // Load the PEI Core from a Firmware Volume\r
656 //\r
657 Status = SecPeCoffGetEntryPoint (\r
658 SecCorePe32File,\r
659 &SecCoreEntryPoint\r
660 );\r
661 if (EFI_ERROR (Status)) {\r
662 return ;\r
663 }\r
664\r
665 //\r
666 // Transfer control to the SEC Core\r
667 //\r
668 SwitchStack (\r
669 (SWITCH_STACK_ENTRY_POINT)SecCoreEntryPoint,\r
670 SecCoreData,\r
671 GetThunkPpiList (),\r
672 TopOfStack\r
673 );\r
674 //\r
675 // If we get here, then the SEC Core returned. This is an error\r
676 //\r
677 return ;\r
678}\r
679\r
680RETURN_STATUS\r
681EFIAPI\r
682SecPeCoffGetEntryPoint (\r
683 IN VOID *Pe32Data,\r
684 IN OUT VOID **EntryPoint\r
685 )\r
686{\r
687 EFI_STATUS Status;\r
688 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;\r
689\r
690 ZeroMem (&ImageContext, sizeof (ImageContext));\r
691 ImageContext.Handle = Pe32Data;\r
692\r
693 ImageContext.ImageRead = (PE_COFF_LOADER_READ_FILE) SecImageRead;\r
694\r
695 Status = PeCoffLoaderGetImageInfo (&ImageContext);\r
696 if (EFI_ERROR (Status)) {\r
697 return Status;\r
698 }\r
699 //\r
700 // Allocate space in NT (not emulator) memory with ReadWrite and Execute attribute.\r
701 // Extra space is for alignment\r
702 //\r
703 ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS) (UINTN) VirtualAlloc (NULL, (SIZE_T) (ImageContext.ImageSize + (ImageContext.SectionAlignment * 2)), MEM_COMMIT, PAGE_EXECUTE_READWRITE);\r
704 if (ImageContext.ImageAddress == 0) {\r
705 return EFI_OUT_OF_RESOURCES;\r
706 }\r
707 //\r
708 // Align buffer on section boundary\r
709 //\r
710 ImageContext.ImageAddress += ImageContext.SectionAlignment - 1;\r
711 ImageContext.ImageAddress &= ~((EFI_PHYSICAL_ADDRESS)ImageContext.SectionAlignment - 1);\r
712\r
713 Status = PeCoffLoaderLoadImage (&ImageContext);\r
714 if (EFI_ERROR (Status)) {\r
715 return Status;\r
716 }\r
717\r
718 Status = PeCoffLoaderRelocateImage (&ImageContext);\r
719 if (EFI_ERROR (Status)) {\r
720 return Status;\r
721 }\r
722\r
723 *EntryPoint = (VOID *)(UINTN)ImageContext.EntryPoint;\r
724\r
725 return EFI_SUCCESS;\r
726}\r
727\r
728EFI_STATUS\r
729EFIAPI\r
730SecImageRead (\r
731 IN VOID *FileHandle,\r
732 IN UINTN FileOffset,\r
733 IN OUT UINTN *ReadSize,\r
734 OUT VOID *Buffer\r
735 )\r
736/*++\r
737\r
738Routine Description:\r
739 Support routine for the PE/COFF Loader that reads a buffer from a PE/COFF file\r
740\r
741Arguments:\r
742 FileHandle - The handle to the PE/COFF file\r
743 FileOffset - The offset, in bytes, into the file to read\r
744 ReadSize - The number of bytes to read from the file starting at FileOffset\r
745 Buffer - A pointer to the buffer to read the data into.\r
746\r
747Returns:\r
748 EFI_SUCCESS - ReadSize bytes of data were read into Buffer from the PE/COFF file starting at FileOffset\r
749\r
750--*/\r
751{\r
752 CHAR8 *Destination8;\r
753 CHAR8 *Source8;\r
754 UINTN Length;\r
755\r
756 Destination8 = Buffer;\r
757 Source8 = (CHAR8 *) ((UINTN) FileHandle + FileOffset);\r
758 Length = *ReadSize;\r
759 while (Length--) {\r
760 *(Destination8++) = *(Source8++);\r
761 }\r
762\r
763 return EFI_SUCCESS;\r
764}\r
765\r
766CHAR16 *\r
767AsciiToUnicode (\r
768 IN CHAR8 *Ascii,\r
769 IN UINTN *StrLen OPTIONAL\r
770 )\r
771/*++\r
772\r
773Routine Description:\r
774 Convert the passed in Ascii string to Unicode.\r
775 Optionally return the length of the strings.\r
776\r
777Arguments:\r
778 Ascii - Ascii string to convert\r
779 StrLen - Length of string\r
780\r
781Returns:\r
782 Pointer to malloc'ed Unicode version of Ascii\r
783\r
784--*/\r
785{\r
786 UINTN Index;\r
787 CHAR16 *Unicode;\r
788\r
789 //\r
790 // Allocate a buffer for unicode string\r
791 //\r
792 for (Index = 0; Ascii[Index] != '\0'; Index++)\r
793 ;\r
794 Unicode = malloc ((Index + 1) * sizeof (CHAR16));\r
795 if (Unicode == NULL) {\r
796 return NULL;\r
797 }\r
798\r
799 for (Index = 0; Ascii[Index] != '\0'; Index++) {\r
800 Unicode[Index] = (CHAR16) Ascii[Index];\r
801 }\r
802\r
803 Unicode[Index] = '\0';\r
804\r
805 if (StrLen != NULL) {\r
806 *StrLen = Index;\r
807 }\r
808\r
809 return Unicode;\r
810}\r
811\r
812UINTN\r
813CountSeparatorsInString (\r
814 IN CONST CHAR16 *String,\r
815 IN CHAR16 Separator\r
816 )\r
817/*++\r
818\r
819Routine Description:\r
820 Count the number of separators in String\r
821\r
822Arguments:\r
823 String - String to process\r
824 Separator - Item to count\r
825\r
826Returns:\r
827 Number of Separator in String\r
828\r
829--*/\r
830{\r
831 UINTN Count;\r
832\r
833 for (Count = 0; *String != '\0'; String++) {\r
834 if (*String == Separator) {\r
835 Count++;\r
836 }\r
837 }\r
838\r
839 return Count;\r
840}\r
841\r
842\r
843VOID\r
844EFIAPI\r
845PeCoffLoaderRelocateImageExtraAction (\r
846 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r
847 )\r
848{\r
849 VOID *DllEntryPoint;\r
850 CHAR16 *DllFileName;\r
851 HMODULE Library;\r
852 UINTN Index;\r
853\r
854 ASSERT (ImageContext != NULL);\r
855 //\r
856 // If we load our own PE COFF images the Windows debugger can not source\r
857 // level debug our code. If a valid PDB pointer exists usw it to load\r
858 // the *.dll file as a library using Windows* APIs. This allows\r
859 // source level debug. The image is still loaded and relocated\r
860 // in the Framework memory space like on a real system (by the code above),\r
861 // but the entry point points into the DLL loaded by the code bellow.\r
862 //\r
863\r
864 DllEntryPoint = NULL;\r
865\r
866 //\r
867 // Load the DLL if it's not an EBC image.\r
868 //\r
869 if ((ImageContext->PdbPointer != NULL) &&\r
870 (ImageContext->Machine != EFI_IMAGE_MACHINE_EBC)) {\r
871 //\r
872 // Convert filename from ASCII to Unicode\r
873 //\r
874 DllFileName = AsciiToUnicode (ImageContext->PdbPointer, &Index);\r
875\r
876 //\r
877 // Check that we have a valid filename\r
878 //\r
879 if (Index < 5 || DllFileName[Index - 4] != '.') {\r
880 free (DllFileName);\r
881\r
882 //\r
883 // Never return an error if PeCoffLoaderRelocateImage() succeeded.\r
884 // The image will run, but we just can't source level debug. If we\r
885 // return an error the image will not run.\r
886 //\r
887 return;\r
888 }\r
889 //\r
890 // Replace .PDB with .DLL on the filename\r
891 //\r
892 DllFileName[Index - 3] = 'D';\r
893 DllFileName[Index - 2] = 'L';\r
894 DllFileName[Index - 1] = 'L';\r
895\r
896 //\r
897 // Load the .DLL file into the user process's address space for source\r
898 // level debug\r
899 //\r
900 Library = LoadLibraryEx (DllFileName, NULL, DONT_RESOLVE_DLL_REFERENCES);\r
901 if (Library != NULL) {\r
902 //\r
903 // InitializeDriver is the entry point we put in all our EFI DLL's. The\r
904 // DONT_RESOLVE_DLL_REFERENCES argument to LoadLIbraryEx() suppresses the\r
905 // normal DLL entry point of DllMain, and prevents other modules that are\r
906 // referenced in side the DllFileName from being loaded. There is no error\r
907 // checking as the we can point to the PE32 image loaded by Tiano. This\r
908 // step is only needed for source level debugging\r
909 //\r
910 DllEntryPoint = (VOID *) (UINTN) GetProcAddress (Library, "InitializeDriver");\r
911\r
912 }\r
913\r
914 if ((Library != NULL) && (DllEntryPoint != NULL)) {\r
915 ImageContext->EntryPoint = (EFI_PHYSICAL_ADDRESS) (UINTN) DllEntryPoint;\r
916 SecPrint ("LoadLibraryEx (%S,\n NULL, DONT_RESOLVE_DLL_REFERENCES)\n", DllFileName);\r
917 } else {\r
918 SecPrint ("WARNING: No source level debug %S. \n", DllFileName);\r
919 }\r
920\r
921 free (DllFileName);\r
922 }\r
923}\r
924\r
925VOID\r
926EFIAPI\r
927PeCoffLoaderUnloadImageExtraAction (\r
928 IN PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r
929)\r
930{\r
931 ASSERT (ImageContext != NULL);\r
932}\r
933\r
934\r
935VOID\r
936_ModuleEntryPoint (\r
937 VOID\r
938 )\r
939{\r
940}\r