]> git.proxmox.com Git - mirror_edk2.git/blame - EmulatorPkg/Win/Host/WinHost.c
EmulatorPkg/Win: Add SimpleFileSystem support
[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
7a465451
RN
427 //\r
428 // Emulator Bus Driver Thunks\r
429 //\r
430 AddThunkProtocol (&mWinNtWndThunkIo, (CHAR16 *)PcdGetPtr (PcdEmuGop), TRUE);\r
56502bf1 431 AddThunkProtocol (&mWinNtFileSystemThunkIo, (CHAR16 *)PcdGetPtr (PcdEmuFileSystem), TRUE);\r
7a465451 432\r
3c859dfe
RN
433 //\r
434 // Allocate space for gSystemMemory Array\r
435 //\r
436 gSystemMemoryCount = CountSeparatorsInString (MemorySizeStr, '!') + 1;\r
437 gSystemMemory = calloc (gSystemMemoryCount, sizeof (NT_SYSTEM_MEMORY));\r
438 if (gSystemMemory == NULL) {\r
439 SecPrint ("ERROR : Can not allocate memory for %S. Exiting.\n", MemorySizeStr);\r
440 exit (1);\r
441 }\r
442\r
443 //\r
444 // Allocate space for gSystemMemory Array\r
445 //\r
446 gFdInfoCount = CountSeparatorsInString (FirmwareVolumesStr, '!') + 1;\r
447 gFdInfo = calloc (gFdInfoCount, sizeof (NT_FD_INFO));\r
448 if (gFdInfo == NULL) {\r
449 SecPrint ("ERROR : Can not allocate memory for %S. Exiting.\n", FirmwareVolumesStr);\r
450 exit (1);\r
451 }\r
452 //\r
453 // Setup Boot Mode.\r
454 //\r
455 SecPrint (" BootMode 0x%02x\n", PcdGet32 (PcdEmuBootMode));\r
456\r
457 //\r
458 // Allocate 128K memory to emulate temp memory for PEI.\r
459 // on a real platform this would be SRAM, or using the cache as RAM.\r
460 // Set TemporaryRam to zero so WinNtOpenFile will allocate a new mapping\r
461 //\r
462 TemporaryRamSize = TEMPORARY_RAM_SIZE;\r
463 TemporaryRam = VirtualAlloc (NULL, (SIZE_T) (TemporaryRamSize), MEM_COMMIT, PAGE_EXECUTE_READWRITE);\r
464 if (TemporaryRam == NULL) {\r
465 SecPrint ("ERROR : Can not allocate enough space for SecStack\n");\r
466 exit (1);\r
467 }\r
468 SetMemN (TemporaryRam, TemporaryRamSize, PcdGet32 (PcdInitValueInTempStack));\r
469\r
470 SecPrint (" OS Emulator passing in %u KB of temp RAM at 0x%08lx to SEC\n",\r
471 TemporaryRamSize / SIZE_1KB,\r
472 TemporaryRam\r
473 );\r
474\r
475 //\r
476 // If enabled use the magic page to communicate between modules\r
477 // This replaces the PI PeiServicesTable pointer mechanism that\r
478 // deos not work in the emulator. It also allows the removal of\r
479 // writable globals from SEC, PEI_CORE (libraries), PEIMs\r
480 //\r
481 EmuMagicPage = (VOID *)(UINTN)(FixedPcdGet64 (PcdPeiServicesTablePage) & MAX_UINTN);\r
482 if (EmuMagicPage != NULL) {\r
483 UINT64 Size;\r
484 Status = WinNtOpenFile (\r
485 NULL,\r
486 SIZE_4KB,\r
487 0,\r
488 &EmuMagicPage,\r
489 &Size\r
490 );\r
491 if (EFI_ERROR (Status)) {\r
492 SecPrint ("ERROR : Could not allocate PeiServicesTablePage @ %p\n", EmuMagicPage);\r
493 return EFI_DEVICE_ERROR;\r
494 }\r
495 }\r
496\r
497 //\r
498 // Open All the firmware volumes and remember the info in the gFdInfo global\r
499 // Meanwhile, find the SEC Core.\r
500 //\r
501 FileNamePtr = AllocateCopyPool (StrSize (FirmwareVolumesStr), FirmwareVolumesStr);\r
502 if (FileNamePtr == NULL) {\r
503 SecPrint ("ERROR : Can not allocate memory for firmware volume string\n");\r
504 exit (1);\r
505 }\r
506\r
507 for (Done = FALSE, Index = 0, SecFile = NULL; !Done; Index++) {\r
508 FileName = FileNamePtr;\r
509 for (Index1 = 0; (FileNamePtr[Index1] != '!') && (FileNamePtr[Index1] != 0); Index1++)\r
510 ;\r
511 if (FileNamePtr[Index1] == 0) {\r
512 Done = TRUE;\r
513 } else {\r
514 FileNamePtr[Index1] = '\0';\r
515 FileNamePtr = &FileNamePtr[Index1 + 1];\r
516 }\r
517\r
518 //\r
519 // Open the FD and remember where it got mapped into our processes address space\r
520 //\r
521 Status = WinNtOpenFile (\r
522 FileName,\r
523 0,\r
524 OPEN_EXISTING,\r
525 &gFdInfo[Index].Address,\r
526 &gFdInfo[Index].Size\r
527 );\r
528 if (EFI_ERROR (Status)) {\r
529 SecPrint ("ERROR : Can not open Firmware Device File %S (0x%X). Exiting.\n", FileName, Status);\r
530 exit (1);\r
531 }\r
532\r
533 SecPrint (" FD loaded from %S\n", FileName);\r
534\r
535 if (SecFile == NULL) {\r
536 //\r
537 // Assume the beginning of the FD is an FV and look for the SEC Core.\r
538 // Load the first one we find.\r
539 //\r
540 FileHandle = NULL;\r
541 Status = PeiServicesFfsFindNextFile (\r
542 EFI_FV_FILETYPE_SECURITY_CORE,\r
543 (EFI_PEI_FV_HANDLE)gFdInfo[Index].Address,\r
544 &FileHandle\r
545 );\r
546 if (!EFI_ERROR (Status)) {\r
547 Status = PeiServicesFfsFindSectionData (EFI_SECTION_PE32, FileHandle, &SecFile);\r
548 if (!EFI_ERROR (Status)) {\r
549 SecPrint (" contains SEC Core");\r
550 }\r
551 }\r
552 }\r
553\r
554 SecPrint ("\n");\r
555 }\r
556 //\r
557 // Calculate memory regions and store the information in the gSystemMemory\r
558 // global for later use. The autosizing code will use this data to\r
559 // map this memory into the SEC process memory space.\r
560 //\r
561 for (Index = 0, Done = FALSE; !Done; Index++) {\r
562 //\r
563 // Save the size of the memory and make a Unicode filename SystemMemory00, ...\r
564 //\r
565 gSystemMemory[Index].Size = _wtoi (MemorySizeStr) * SIZE_1MB;\r
566\r
567 //\r
568 // Find the next region\r
569 //\r
570 for (Index1 = 0; MemorySizeStr[Index1] != '!' && MemorySizeStr[Index1] != 0; Index1++)\r
571 ;\r
572 if (MemorySizeStr[Index1] == 0) {\r
573 Done = TRUE;\r
574 }\r
575\r
576 MemorySizeStr = MemorySizeStr + Index1 + 1;\r
577 }\r
578\r
579 SecPrint ("\n");\r
580\r
581 //\r
582 // Hand off to SEC Core\r
583 //\r
584 SecLoadSecCore ((UINTN)TemporaryRam, TemporaryRamSize, gFdInfo[0].Address, gFdInfo[0].Size, SecFile);\r
585\r
586 //\r
587 // If we get here, then the SEC Core returned. This is an error as SEC should\r
588 // always hand off to PEI Core and then on to DXE Core.\r
589 //\r
590 SecPrint ("ERROR : SEC returned\n");\r
591 exit (1);\r
592}\r
593\r
594VOID\r
595SecLoadSecCore (\r
596 IN UINTN TemporaryRam,\r
597 IN UINTN TemporaryRamSize,\r
598 IN VOID *BootFirmwareVolumeBase,\r
599 IN UINTN BootFirmwareVolumeSize,\r
600 IN VOID *SecCorePe32File\r
601 )\r
602/*++\r
603\r
604Routine Description:\r
605 This is the service to load the SEC Core from the Firmware Volume\r
606\r
607Arguments:\r
608 TemporaryRam - Memory to use for SEC.\r
609 TemporaryRamSize - Size of Memory to use for SEC\r
610 BootFirmwareVolumeBase - Start of the Boot FV\r
611 SecCorePe32File - SEC Core PE32\r
612\r
613Returns:\r
614 Success means control is transfered and thus we should never return\r
615\r
616--*/\r
617{\r
618 EFI_STATUS Status;\r
619 VOID *TopOfStack;\r
620 VOID *SecCoreEntryPoint;\r
621 EFI_SEC_PEI_HAND_OFF *SecCoreData;\r
622 UINTN SecStackSize;\r
623\r
624 //\r
625 // Compute Top Of Memory for Stack and PEI Core Allocations\r
626 //\r
627 SecStackSize = TemporaryRamSize >> 1;\r
628\r
629 //\r
630 // |-----------| <---- TemporaryRamBase + TemporaryRamSize\r
631 // | Heap |\r
632 // | |\r
633 // |-----------| <---- StackBase / PeiTemporaryMemoryBase\r
634 // | |\r
635 // | Stack |\r
636 // |-----------| <---- TemporaryRamBase\r
637 //\r
638 TopOfStack = (VOID *)(TemporaryRam + SecStackSize);\r
639\r
640 //\r
641 // Reservet space for storing PeiCore's parament in stack.\r
642 //\r
643 TopOfStack = (VOID *)((UINTN)TopOfStack - sizeof (EFI_SEC_PEI_HAND_OFF) - CPU_STACK_ALIGNMENT);\r
644 TopOfStack = ALIGN_POINTER (TopOfStack, CPU_STACK_ALIGNMENT);\r
645\r
646 //\r
647 // Bind this information into the SEC hand-off state\r
648 //\r
649 SecCoreData = (EFI_SEC_PEI_HAND_OFF*)(UINTN)TopOfStack;\r
650 SecCoreData->DataSize = sizeof (EFI_SEC_PEI_HAND_OFF);\r
651 SecCoreData->BootFirmwareVolumeBase = BootFirmwareVolumeBase;\r
652 SecCoreData->BootFirmwareVolumeSize = BootFirmwareVolumeSize;\r
653 SecCoreData->TemporaryRamBase = (VOID*)TemporaryRam;\r
654 SecCoreData->TemporaryRamSize = TemporaryRamSize;\r
655 SecCoreData->StackBase = SecCoreData->TemporaryRamBase;\r
656 SecCoreData->StackSize = SecStackSize;\r
657 SecCoreData->PeiTemporaryRamBase = (VOID*) ((UINTN) SecCoreData->TemporaryRamBase + SecStackSize);\r
658 SecCoreData->PeiTemporaryRamSize = TemporaryRamSize - SecStackSize;\r
659\r
660 //\r
661 // Load the PEI Core from a Firmware Volume\r
662 //\r
663 Status = SecPeCoffGetEntryPoint (\r
664 SecCorePe32File,\r
665 &SecCoreEntryPoint\r
666 );\r
667 if (EFI_ERROR (Status)) {\r
668 return ;\r
669 }\r
670\r
671 //\r
672 // Transfer control to the SEC Core\r
673 //\r
674 SwitchStack (\r
675 (SWITCH_STACK_ENTRY_POINT)SecCoreEntryPoint,\r
676 SecCoreData,\r
677 GetThunkPpiList (),\r
678 TopOfStack\r
679 );\r
680 //\r
681 // If we get here, then the SEC Core returned. This is an error\r
682 //\r
683 return ;\r
684}\r
685\r
686RETURN_STATUS\r
687EFIAPI\r
688SecPeCoffGetEntryPoint (\r
689 IN VOID *Pe32Data,\r
690 IN OUT VOID **EntryPoint\r
691 )\r
692{\r
693 EFI_STATUS Status;\r
694 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;\r
695\r
696 ZeroMem (&ImageContext, sizeof (ImageContext));\r
697 ImageContext.Handle = Pe32Data;\r
698\r
699 ImageContext.ImageRead = (PE_COFF_LOADER_READ_FILE) SecImageRead;\r
700\r
701 Status = PeCoffLoaderGetImageInfo (&ImageContext);\r
702 if (EFI_ERROR (Status)) {\r
703 return Status;\r
704 }\r
705 //\r
706 // Allocate space in NT (not emulator) memory with ReadWrite and Execute attribute.\r
707 // Extra space is for alignment\r
708 //\r
709 ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS) (UINTN) VirtualAlloc (NULL, (SIZE_T) (ImageContext.ImageSize + (ImageContext.SectionAlignment * 2)), MEM_COMMIT, PAGE_EXECUTE_READWRITE);\r
710 if (ImageContext.ImageAddress == 0) {\r
711 return EFI_OUT_OF_RESOURCES;\r
712 }\r
713 //\r
714 // Align buffer on section boundary\r
715 //\r
716 ImageContext.ImageAddress += ImageContext.SectionAlignment - 1;\r
717 ImageContext.ImageAddress &= ~((EFI_PHYSICAL_ADDRESS)ImageContext.SectionAlignment - 1);\r
718\r
719 Status = PeCoffLoaderLoadImage (&ImageContext);\r
720 if (EFI_ERROR (Status)) {\r
721 return Status;\r
722 }\r
723\r
724 Status = PeCoffLoaderRelocateImage (&ImageContext);\r
725 if (EFI_ERROR (Status)) {\r
726 return Status;\r
727 }\r
728\r
729 *EntryPoint = (VOID *)(UINTN)ImageContext.EntryPoint;\r
730\r
731 return EFI_SUCCESS;\r
732}\r
733\r
734EFI_STATUS\r
735EFIAPI\r
736SecImageRead (\r
737 IN VOID *FileHandle,\r
738 IN UINTN FileOffset,\r
739 IN OUT UINTN *ReadSize,\r
740 OUT VOID *Buffer\r
741 )\r
742/*++\r
743\r
744Routine Description:\r
745 Support routine for the PE/COFF Loader that reads a buffer from a PE/COFF file\r
746\r
747Arguments:\r
748 FileHandle - The handle to the PE/COFF file\r
749 FileOffset - The offset, in bytes, into the file to read\r
750 ReadSize - The number of bytes to read from the file starting at FileOffset\r
751 Buffer - A pointer to the buffer to read the data into.\r
752\r
753Returns:\r
754 EFI_SUCCESS - ReadSize bytes of data were read into Buffer from the PE/COFF file starting at FileOffset\r
755\r
756--*/\r
757{\r
758 CHAR8 *Destination8;\r
759 CHAR8 *Source8;\r
760 UINTN Length;\r
761\r
762 Destination8 = Buffer;\r
763 Source8 = (CHAR8 *) ((UINTN) FileHandle + FileOffset);\r
764 Length = *ReadSize;\r
765 while (Length--) {\r
766 *(Destination8++) = *(Source8++);\r
767 }\r
768\r
769 return EFI_SUCCESS;\r
770}\r
771\r
772CHAR16 *\r
773AsciiToUnicode (\r
774 IN CHAR8 *Ascii,\r
775 IN UINTN *StrLen OPTIONAL\r
776 )\r
777/*++\r
778\r
779Routine Description:\r
780 Convert the passed in Ascii string to Unicode.\r
781 Optionally return the length of the strings.\r
782\r
783Arguments:\r
784 Ascii - Ascii string to convert\r
785 StrLen - Length of string\r
786\r
787Returns:\r
788 Pointer to malloc'ed Unicode version of Ascii\r
789\r
790--*/\r
791{\r
792 UINTN Index;\r
793 CHAR16 *Unicode;\r
794\r
795 //\r
796 // Allocate a buffer for unicode string\r
797 //\r
798 for (Index = 0; Ascii[Index] != '\0'; Index++)\r
799 ;\r
800 Unicode = malloc ((Index + 1) * sizeof (CHAR16));\r
801 if (Unicode == NULL) {\r
802 return NULL;\r
803 }\r
804\r
805 for (Index = 0; Ascii[Index] != '\0'; Index++) {\r
806 Unicode[Index] = (CHAR16) Ascii[Index];\r
807 }\r
808\r
809 Unicode[Index] = '\0';\r
810\r
811 if (StrLen != NULL) {\r
812 *StrLen = Index;\r
813 }\r
814\r
815 return Unicode;\r
816}\r
817\r
818UINTN\r
819CountSeparatorsInString (\r
820 IN CONST CHAR16 *String,\r
821 IN CHAR16 Separator\r
822 )\r
823/*++\r
824\r
825Routine Description:\r
826 Count the number of separators in String\r
827\r
828Arguments:\r
829 String - String to process\r
830 Separator - Item to count\r
831\r
832Returns:\r
833 Number of Separator in String\r
834\r
835--*/\r
836{\r
837 UINTN Count;\r
838\r
839 for (Count = 0; *String != '\0'; String++) {\r
840 if (*String == Separator) {\r
841 Count++;\r
842 }\r
843 }\r
844\r
845 return Count;\r
846}\r
847\r
848\r
849VOID\r
850EFIAPI\r
851PeCoffLoaderRelocateImageExtraAction (\r
852 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r
853 )\r
854{\r
855 VOID *DllEntryPoint;\r
856 CHAR16 *DllFileName;\r
857 HMODULE Library;\r
858 UINTN Index;\r
859\r
860 ASSERT (ImageContext != NULL);\r
861 //\r
862 // If we load our own PE COFF images the Windows debugger can not source\r
863 // level debug our code. If a valid PDB pointer exists usw it to load\r
864 // the *.dll file as a library using Windows* APIs. This allows\r
865 // source level debug. The image is still loaded and relocated\r
866 // in the Framework memory space like on a real system (by the code above),\r
867 // but the entry point points into the DLL loaded by the code bellow.\r
868 //\r
869\r
870 DllEntryPoint = NULL;\r
871\r
872 //\r
873 // Load the DLL if it's not an EBC image.\r
874 //\r
875 if ((ImageContext->PdbPointer != NULL) &&\r
876 (ImageContext->Machine != EFI_IMAGE_MACHINE_EBC)) {\r
877 //\r
878 // Convert filename from ASCII to Unicode\r
879 //\r
880 DllFileName = AsciiToUnicode (ImageContext->PdbPointer, &Index);\r
881\r
882 //\r
883 // Check that we have a valid filename\r
884 //\r
885 if (Index < 5 || DllFileName[Index - 4] != '.') {\r
886 free (DllFileName);\r
887\r
888 //\r
889 // Never return an error if PeCoffLoaderRelocateImage() succeeded.\r
890 // The image will run, but we just can't source level debug. If we\r
891 // return an error the image will not run.\r
892 //\r
893 return;\r
894 }\r
895 //\r
896 // Replace .PDB with .DLL on the filename\r
897 //\r
898 DllFileName[Index - 3] = 'D';\r
899 DllFileName[Index - 2] = 'L';\r
900 DllFileName[Index - 1] = 'L';\r
901\r
902 //\r
903 // Load the .DLL file into the user process's address space for source\r
904 // level debug\r
905 //\r
906 Library = LoadLibraryEx (DllFileName, NULL, DONT_RESOLVE_DLL_REFERENCES);\r
907 if (Library != NULL) {\r
908 //\r
909 // InitializeDriver is the entry point we put in all our EFI DLL's. The\r
910 // DONT_RESOLVE_DLL_REFERENCES argument to LoadLIbraryEx() suppresses the\r
911 // normal DLL entry point of DllMain, and prevents other modules that are\r
912 // referenced in side the DllFileName from being loaded. There is no error\r
913 // checking as the we can point to the PE32 image loaded by Tiano. This\r
914 // step is only needed for source level debugging\r
915 //\r
916 DllEntryPoint = (VOID *) (UINTN) GetProcAddress (Library, "InitializeDriver");\r
917\r
918 }\r
919\r
920 if ((Library != NULL) && (DllEntryPoint != NULL)) {\r
921 ImageContext->EntryPoint = (EFI_PHYSICAL_ADDRESS) (UINTN) DllEntryPoint;\r
922 SecPrint ("LoadLibraryEx (%S,\n NULL, DONT_RESOLVE_DLL_REFERENCES)\n", DllFileName);\r
923 } else {\r
924 SecPrint ("WARNING: No source level debug %S. \n", DllFileName);\r
925 }\r
926\r
927 free (DllFileName);\r
928 }\r
929}\r
930\r
931VOID\r
932EFIAPI\r
933PeCoffLoaderUnloadImageExtraAction (\r
934 IN PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r
935)\r
936{\r
937 ASSERT (ImageContext != NULL);\r
938}\r
939\r
940\r
941VOID\r
942_ModuleEntryPoint (\r
943 VOID\r
944 )\r
945{\r
946}\r