]> git.proxmox.com Git - mirror_edk2.git/blame - Nt32Pkg/Sec/SecMain.c
We enabled X64 native version NT32, and made it works on Windows 7 X64 OS.
[mirror_edk2.git] / Nt32Pkg / Sec / SecMain.c
CommitLineData
6ae81428 1/**@file\r
2e19fd0f 2\r
3bbe68a3 3Copyright (c) 2006 - 2012, Intel Corporation. All rights reserved.<BR>\r
8f2a5f80 4This program and the accompanying materials\r
2e19fd0f 5are licensed and made available under the terms and conditions of the BSD License\r
6which accompanies this distribution. The full text of the license may be found at\r
7http://opensource.org/licenses/bsd-license.php\r
8\r
9THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
10WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
11\r
12Module Name:\r
13\r
14 SecMain.c\r
15\r
16Abstract:\r
17 WinNt emulator of SEC phase. It's really a Win32 application, but this is\r
18 Ok since all the other modules for NT32 are NOT Win32 applications.\r
19\r
952261d5
LG
20 This program gets NT32 PCD setting and figures out what the memory layout \r
21 will be, how may FD's will be loaded and also what the boot mode is.\r
2e19fd0f 22\r
23 The SEC registers a set of services with the SEC core. gPrivateDispatchTable\r
24 is a list of PPI's produced by the SEC that are availble for usage in PEI.\r
25\r
952261d5
LG
26 This code produces 128 K of temporary memory for the PEI stack by directly\r
27 allocate memory space with ReadWrite and Execute attribute.\r
2e19fd0f 28\r
6ae81428 29**/\r
2e19fd0f 30\r
31#include "SecMain.h"\r
d0dc913e 32\r
24e12917
RN
33#ifndef SE_TIME_ZONE_NAME\r
34#define SE_TIME_ZONE_NAME TEXT("SeTimeZonePrivilege")\r
35#endif\r
2e19fd0f 36\r
37NT_PEI_LOAD_FILE_PPI mSecNtLoadFilePpi = { SecWinNtPeiLoadFile };\r
38\r
39PEI_NT_AUTOSCAN_PPI mSecNtAutoScanPpi = { SecWinNtPeiAutoScan };\r
40\r
41PEI_NT_THUNK_PPI mSecWinNtThunkPpi = { SecWinNtWinNtThunkAddress };\r
42\r
43EFI_PEI_PROGRESS_CODE_PPI mSecStatusCodePpi = { SecPeiReportStatusCode };\r
44\r
45NT_FWH_PPI mSecFwhInformationPpi = { SecWinNtFdAddress };\r
46\r
d0a3ead2 47EFI_PEI_TEMPORARY_RAM_SUPPORT_PPI mSecTemporaryRamSupportPpi = {SecTemporaryRamSupport};\r
2e19fd0f 48\r
49EFI_PEI_PPI_DESCRIPTOR gPrivateDispatchTable[] = {\r
2e19fd0f 50 {\r
51 EFI_PEI_PPI_DESCRIPTOR_PPI,\r
52 &gNtPeiLoadFilePpiGuid,\r
53 &mSecNtLoadFilePpi\r
54 },\r
55 {\r
56 EFI_PEI_PPI_DESCRIPTOR_PPI,\r
57 &gPeiNtAutoScanPpiGuid,\r
58 &mSecNtAutoScanPpi\r
59 },\r
60 {\r
61 EFI_PEI_PPI_DESCRIPTOR_PPI,\r
62 &gPeiNtThunkPpiGuid,\r
63 &mSecWinNtThunkPpi\r
64 },\r
65 {\r
66 EFI_PEI_PPI_DESCRIPTOR_PPI,\r
67 &gEfiPeiStatusCodePpiGuid,\r
68 &mSecStatusCodePpi\r
69 },\r
58dcdada 70 {\r
71 EFI_PEI_PPI_DESCRIPTOR_PPI,\r
72 &gEfiTemporaryRamSupportPpiGuid,\r
73 &mSecTemporaryRamSupportPpi\r
74 },\r
2e19fd0f 75 {\r
76 EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST,\r
77 &gNtFwhPpiGuid,\r
78 &mSecFwhInformationPpi\r
79 }\r
80};\r
81\r
82\r
83//\r
84// Default information about where the FD is located.\r
952261d5 85// This array gets filled in with information from PcdWinNtFirmwareVolume\r
2e19fd0f 86// The number of array elements is allocated base on parsing\r
952261d5 87// PcdWinNtFirmwareVolume and the memory is never freed.\r
2e19fd0f 88//\r
89UINTN gFdInfoCount = 0;\r
90NT_FD_INFO *gFdInfo;\r
91\r
92//\r
93// Array that supports seperate memory rantes.\r
952261d5 94// The memory ranges are set by PcdWinNtMemorySizeForSecMain.\r
2e19fd0f 95// The number of array elements is allocated base on parsing\r
952261d5 96// PcdWinNtMemorySizeForSecMain value and the memory is never freed.\r
2e19fd0f 97//\r
98UINTN gSystemMemoryCount = 0;\r
99NT_SYSTEM_MEMORY *gSystemMemory;\r
100\r
58dcdada 101VOID\r
102EFIAPI\r
103SecSwitchStack (\r
104 UINT32 TemporaryMemoryBase,\r
105 UINT32 PermenentMemoryBase\r
106 );\r
ffdd18bb 107EFI_STATUS\r
108SecNt32PeCoffRelocateImage (\r
109 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r
110 );\r
1f53f7bb 111\r
f66a43b2
JM
112VOID\r
113EFIAPI\r
114PeiSwitchStacks (\r
115 IN SWITCH_STACK_ENTRY_POINT EntryPoint,\r
116 IN VOID *Context1, OPTIONAL\r
117 IN VOID *Context2, OPTIONAL\r
118 IN VOID *Context3, OPTIONAL\r
119 IN VOID *NewStack\r
120 );\r
121\r
1f53f7bb 122VOID\r
123SecPrint (\r
124 CHAR8 *Format,\r
125 ...\r
126 )\r
127{\r
128 va_list Marker;\r
129 UINTN CharCount;\r
130 CHAR8 Buffer[EFI_STATUS_CODE_DATA_MAX_SIZE];\r
131\r
132 va_start (Marker, Format);\r
133 \r
1f53f7bb 134 _vsnprintf (Buffer, sizeof (Buffer), Format, Marker);\r
135\r
3bbe68a3 136 va_end (Marker);\r
137\r
1f53f7bb 138 CharCount = strlen (Buffer);\r
139 WriteFile (\r
140 GetStdHandle (STD_OUTPUT_HANDLE), \r
141 Buffer,\r
f66a43b2 142 (DWORD)CharCount,\r
1f53f7bb 143 (LPDWORD)&CharCount,\r
144 NULL\r
145 );\r
146}\r
147\r
2e19fd0f 148INTN\r
149EFIAPI\r
150main (\r
151 IN INTN Argc,\r
152 IN CHAR8 **Argv,\r
153 IN CHAR8 **Envp\r
154 )\r
155/*++\r
156\r
157Routine Description:\r
158 Main entry point to SEC for WinNt. This is a Windows program\r
159\r
160Arguments:\r
161 Argc - Number of command line arguments\r
162 Argv - Array of command line argument strings\r
163 Envp - Array of environmemt variable strings\r
164\r
165Returns:\r
166 0 - Normal exit\r
167 1 - Abnormal exit\r
168\r
169--*/\r
170{\r
171 EFI_STATUS Status;\r
24e12917
RN
172 HANDLE Token;\r
173 TOKEN_PRIVILEGES TokenPrivileges;\r
2e19fd0f 174 EFI_PHYSICAL_ADDRESS InitialStackMemory;\r
175 UINT64 InitialStackMemorySize;\r
176 UINTN Index;\r
177 UINTN Index1;\r
178 UINTN Index2;\r
2e19fd0f 179 CHAR16 *FileName;\r
180 CHAR16 *FileNamePtr;\r
181 BOOLEAN Done;\r
182 VOID *PeiCoreFile;\r
183 CHAR16 *MemorySizeStr;\r
184 CHAR16 *FirmwareVolumesStr;\r
85e43be1 185 UINTN *StackPointer;\r
706e7534 186\r
24e12917
RN
187 //\r
188 // Enable the privilege so that RTC driver can successfully run SetTime()\r
189 //\r
190 OpenProcessToken (GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY, &Token);\r
191 if (LookupPrivilegeValue(NULL, SE_TIME_ZONE_NAME, &TokenPrivileges.Privileges[0].Luid)) {\r
192 TokenPrivileges.PrivilegeCount = 1;\r
193 TokenPrivileges.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;\r
194 AdjustTokenPrivileges(Token, FALSE, &TokenPrivileges, 0, (PTOKEN_PRIVILEGES) NULL, 0);\r
195 }\r
196\r
f7c58a94 197 MemorySizeStr = (CHAR16 *) PcdGetPtr (PcdWinNtMemorySizeForSecMain);\r
198 FirmwareVolumesStr = (CHAR16 *) PcdGetPtr (PcdWinNtFirmwareVolume);\r
2e19fd0f 199\r
1f53f7bb 200 SecPrint ("\nEDK II SEC Main NT Emulation Environment from www.TianoCore.org\n");\r
2e19fd0f 201\r
202 //\r
203 // Make some Windows calls to Set the process to the highest priority in the\r
204 // idle class. We need this to have good performance.\r
205 //\r
206 SetPriorityClass (GetCurrentProcess (), IDLE_PRIORITY_CLASS);\r
207 SetThreadPriority (GetCurrentThread (), THREAD_PRIORITY_HIGHEST);\r
208\r
209 //\r
210 // Allocate space for gSystemMemory Array\r
211 //\r
212 gSystemMemoryCount = CountSeperatorsInString (MemorySizeStr, '!') + 1;\r
213 gSystemMemory = calloc (gSystemMemoryCount, sizeof (NT_SYSTEM_MEMORY));\r
214 if (gSystemMemory == NULL) {\r
1f53f7bb 215 SecPrint ("ERROR : Can not allocate memory for %S. Exiting.\n", MemorySizeStr);\r
2e19fd0f 216 exit (1);\r
217 }\r
218 //\r
219 // Allocate space for gSystemMemory Array\r
220 //\r
221 gFdInfoCount = CountSeperatorsInString (FirmwareVolumesStr, '!') + 1;\r
222 gFdInfo = calloc (gFdInfoCount, sizeof (NT_FD_INFO));\r
223 if (gFdInfo == NULL) {\r
1f53f7bb 224 SecPrint ("ERROR : Can not allocate memory for %S. Exiting.\n", FirmwareVolumesStr);\r
2e19fd0f 225 exit (1);\r
226 }\r
227 //\r
228 // Setup Boot Mode. If BootModeStr == "" then BootMode = 0 (BOOT_WITH_FULL_CONFIGURATION)\r
229 //\r
f7c58a94 230 SecPrint (" BootMode 0x%02x\n", PcdGet32 (PcdWinNtBootMode));\r
2e19fd0f 231\r
232 //\r
952261d5 233 // Allocate 128K memory to emulate temp memory for PEI.\r
2e19fd0f 234 // on a real platform this would be SRAM, or using the cache as RAM.\r
235 // Set InitialStackMemory to zero so WinNtOpenFile will allocate a new mapping\r
236 //\r
952261d5
LG
237 InitialStackMemorySize = STACK_SIZE;\r
238 InitialStackMemory = (EFI_PHYSICAL_ADDRESS) (UINTN) VirtualAlloc (NULL, (SIZE_T) (InitialStackMemorySize), MEM_COMMIT, PAGE_EXECUTE_READWRITE);\r
239 if (InitialStackMemory == 0) {\r
1f53f7bb 240 SecPrint ("ERROR : Can not allocate enough space for SecStack\n");\r
2e19fd0f 241 exit (1);\r
242 }\r
243\r
85e43be1 244 for (StackPointer = (UINTN*) (UINTN) InitialStackMemory;\r
245 StackPointer < (UINTN*) ((UINTN)InitialStackMemory + (SIZE_T) InitialStackMemorySize);\r
246 StackPointer ++) {\r
247 *StackPointer = 0x5AA55AA5;\r
248 }\r
249 \r
1f53f7bb 250 SecPrint (" SEC passing in %d bytes of temp RAM to PEI\n", InitialStackMemorySize);\r
2e19fd0f 251\r
252 //\r
253 // Open All the firmware volumes and remember the info in the gFdInfo global\r
254 //\r
255 FileNamePtr = (CHAR16 *)malloc (StrLen ((CHAR16 *)FirmwareVolumesStr) * sizeof(CHAR16));\r
256 if (FileNamePtr == NULL) {\r
1f53f7bb 257 SecPrint ("ERROR : Can not allocate memory for firmware volume string\n");\r
2e19fd0f 258 exit (1);\r
259 }\r
260\r
261 StrCpy (FileNamePtr, (CHAR16*)FirmwareVolumesStr);\r
262\r
0b94e319 263 for (Done = FALSE, Index = 0, PeiCoreFile = NULL; !Done; Index++) {\r
2e19fd0f 264 FileName = FileNamePtr;\r
265 for (Index1 = 0; (FileNamePtr[Index1] != '!') && (FileNamePtr[Index1] != 0); Index1++)\r
266 ;\r
267 if (FileNamePtr[Index1] == 0) {\r
268 Done = TRUE;\r
269 } else {\r
270 FileNamePtr[Index1] = '\0';\r
271 FileNamePtr = FileNamePtr + Index1 + 1;\r
272 }\r
273\r
274 //\r
275 // Open the FD and remmeber where it got mapped into our processes address space\r
276 //\r
277 Status = WinNtOpenFile (\r
278 FileName,\r
279 0,\r
280 OPEN_EXISTING,\r
281 &gFdInfo[Index].Address,\r
282 &gFdInfo[Index].Size\r
283 );\r
284 if (EFI_ERROR (Status)) {\r
1f53f7bb 285 SecPrint ("ERROR : Can not open Firmware Device File %S (0x%X). Exiting.\n", FileName, Status);\r
2e19fd0f 286 exit (1);\r
287 }\r
288\r
1f53f7bb 289 SecPrint (" FD loaded from");\r
2e19fd0f 290 //\r
291 // printf can't print filenames directly as the \ gets interperted as an\r
292 // escape character.\r
293 //\r
294 for (Index2 = 0; FileName[Index2] != '\0'; Index2++) {\r
1f53f7bb 295 SecPrint ("%c", FileName[Index2]);\r
2e19fd0f 296 }\r
297\r
298 if (PeiCoreFile == NULL) {\r
299 //\r
300 // Assume the beginning of the FD is an FV and look for the PEI Core.\r
301 // Load the first one we find.\r
302 //\r
303 Status = SecFfsFindPeiCore ((EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) gFdInfo[Index].Address, &PeiCoreFile);\r
304 if (!EFI_ERROR (Status)) {\r
1f53f7bb 305 SecPrint (" contains SEC Core");\r
2e19fd0f 306 }\r
307 }\r
308\r
1f53f7bb 309 SecPrint ("\n");\r
2e19fd0f 310 }\r
311 //\r
312 // Calculate memory regions and store the information in the gSystemMemory\r
313 // global for later use. The autosizing code will use this data to\r
314 // map this memory into the SEC process memory space.\r
315 //\r
316 for (Index = 0, Done = FALSE; !Done; Index++) {\r
317 //\r
318 // Save the size of the memory and make a Unicode filename SystemMemory00, ...\r
319 //\r
320 gSystemMemory[Index].Size = _wtoi (MemorySizeStr) * 0x100000;\r
2e19fd0f 321\r
322 //\r
323 // Find the next region\r
324 //\r
325 for (Index1 = 0; MemorySizeStr[Index1] != '!' && MemorySizeStr[Index1] != 0; Index1++)\r
326 ;\r
327 if (MemorySizeStr[Index1] == 0) {\r
328 Done = TRUE;\r
329 }\r
330\r
331 MemorySizeStr = MemorySizeStr + Index1 + 1;\r
332 }\r
333\r
1f53f7bb 334 SecPrint ("\n");\r
2e19fd0f 335\r
336 //\r
337 // Hand off to PEI Core\r
338 //\r
339 SecLoadFromCore ((UINTN) InitialStackMemory, (UINTN) InitialStackMemorySize, (UINTN) gFdInfo[0].Address, PeiCoreFile);\r
340\r
341 //\r
342 // If we get here, then the PEI Core returned. This is an error as PEI should\r
343 // always hand off to DXE.\r
344 //\r
1f53f7bb 345 SecPrint ("ERROR : PEI Core returned\n");\r
2e19fd0f 346 exit (1);\r
347}\r
348\r
349EFI_STATUS\r
350WinNtOpenFile (\r
351 IN CHAR16 *FileName,\r
352 IN UINT32 MapSize,\r
353 IN DWORD CreationDisposition,\r
354 IN OUT EFI_PHYSICAL_ADDRESS *BaseAddress,\r
355 OUT UINT64 *Length\r
356 )\r
357/*++\r
358\r
359Routine Description:\r
360 Opens and memory maps a file using WinNt services. If BaseAddress is non zero\r
361 the process will try and allocate the memory starting at BaseAddress.\r
362\r
363Arguments:\r
364 FileName - The name of the file to open and map\r
365 MapSize - The amount of the file to map in bytes\r
366 CreationDisposition - The flags to pass to CreateFile(). Use to create new files for\r
367 memory emulation, and exiting files for firmware volume emulation\r
368 BaseAddress - The base address of the mapped file in the user address space.\r
369 If passed in as NULL the a new memory region is used.\r
370 If passed in as non NULL the request memory region is used for\r
371 the mapping of the file into the process space.\r
372 Length - The size of the mapped region in bytes\r
373\r
374Returns:\r
375 EFI_SUCCESS - The file was opened and mapped.\r
376 EFI_NOT_FOUND - FileName was not found in the current directory\r
377 EFI_DEVICE_ERROR - An error occured attempting to map the opened file\r
378\r
379--*/\r
380{\r
381 HANDLE NtFileHandle;\r
382 HANDLE NtMapHandle;\r
383 VOID *VirtualAddress;\r
384 UINTN FileSize;\r
385\r
386 //\r
387 // Use Win API to open/create a file\r
388 //\r
389 NtFileHandle = CreateFile (\r
390 FileName,\r
75e55c54 391 GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE,\r
2e19fd0f 392 FILE_SHARE_READ,\r
393 NULL,\r
394 CreationDisposition,\r
395 FILE_ATTRIBUTE_NORMAL,\r
396 NULL\r
397 );\r
398 if (NtFileHandle == INVALID_HANDLE_VALUE) {\r
399 return EFI_NOT_FOUND;\r
400 }\r
401 //\r
402 // Map the open file into a memory range\r
403 //\r
404 NtMapHandle = CreateFileMapping (\r
405 NtFileHandle,\r
406 NULL,\r
75e55c54 407 PAGE_EXECUTE_READWRITE,\r
2e19fd0f 408 0,\r
409 MapSize,\r
410 NULL\r
411 );\r
412 if (NtMapHandle == NULL) {\r
413 return EFI_DEVICE_ERROR;\r
414 }\r
415 //\r
416 // Get the virtual address (address in the emulator) of the mapped file\r
417 //\r
418 VirtualAddress = MapViewOfFileEx (\r
419 NtMapHandle,\r
75e55c54 420 FILE_MAP_EXECUTE | FILE_MAP_ALL_ACCESS,\r
2e19fd0f 421 0,\r
422 0,\r
423 MapSize,\r
424 (LPVOID) (UINTN) *BaseAddress\r
425 );\r
426 if (VirtualAddress == NULL) {\r
427 return EFI_DEVICE_ERROR;\r
428 }\r
429\r
430 if (MapSize == 0) {\r
431 //\r
432 // Seek to the end of the file to figure out the true file size.\r
433 //\r
434 FileSize = SetFilePointer (\r
435 NtFileHandle,\r
436 0,\r
437 NULL,\r
438 FILE_END\r
439 );\r
440 if (FileSize == -1) {\r
441 return EFI_DEVICE_ERROR;\r
442 }\r
443\r
444 *Length = (UINT64) FileSize;\r
445 } else {\r
446 *Length = (UINT64) MapSize;\r
447 }\r
448\r
449 *BaseAddress = (EFI_PHYSICAL_ADDRESS) (UINTN) VirtualAddress;\r
450\r
451 return EFI_SUCCESS;\r
452}\r
453\r
2e19fd0f 454\r
d0dc913e 455#define BYTES_PER_RECORD 512\r
2e19fd0f 456\r
457EFI_STATUS\r
458EFIAPI\r
459SecPeiReportStatusCode (\r
e1001af1 460 IN CONST EFI_PEI_SERVICES **PeiServices,\r
2e19fd0f 461 IN EFI_STATUS_CODE_TYPE CodeType,\r
462 IN EFI_STATUS_CODE_VALUE Value,\r
463 IN UINT32 Instance,\r
e1001af1 464 IN CONST EFI_GUID *CallerId,\r
465 IN CONST EFI_STATUS_CODE_DATA *Data OPTIONAL\r
2e19fd0f 466 )\r
467/*++\r
468\r
469Routine Description:\r
470\r
471 This routine produces the ReportStatusCode PEI service. It's passed\r
472 up to the PEI Core via a PPI. T\r
473\r
474 This code currently uses the NT clib printf. This does not work the same way\r
475 as the EFI Print (), as %t, %g, %s as Unicode are not supported.\r
476\r
477Arguments:\r
478 (see EFI_PEI_REPORT_STATUS_CODE)\r
479\r
480Returns:\r
481 EFI_SUCCESS - Always return success\r
482\r
483--*/\r
484// TODO: PeiServices - add argument and description to function comment\r
485// TODO: CodeType - add argument and description to function comment\r
486// TODO: Value - add argument and description to function comment\r
487// TODO: Instance - add argument and description to function comment\r
488// TODO: CallerId - add argument and description to function comment\r
489// TODO: Data - add argument and description to function comment\r
490{\r
491 CHAR8 *Format;\r
a776476a 492 BASE_LIST Marker;\r
2e19fd0f 493 CHAR8 PrintBuffer[BYTES_PER_RECORD * 2];\r
494 CHAR8 *Filename;\r
495 CHAR8 *Description;\r
496 UINT32 LineNumber;\r
d0dc913e 497 UINT32 ErrorLevel;\r
2e19fd0f 498\r
d0dc913e
A
499\r
500 if (Data == NULL) {\r
501 } else if (ReportStatusCodeExtractAssertInfo (CodeType, Value, Data, &Filename, &Description, &LineNumber)) {\r
2e19fd0f 502 //\r
d0dc913e 503 // Processes ASSERT ()\r
2e19fd0f 504 //\r
1f53f7bb 505 SecPrint ("ASSERT %s(%d): %s\n", Filename, (int)LineNumber, Description);\r
2e19fd0f 506\r
d0dc913e
A
507 } else if (ReportStatusCodeExtractDebugInfo (Data, &ErrorLevel, &Marker, &Format)) {\r
508 //\r
509 // Process DEBUG () macro \r
510 //\r
a776476a 511 AsciiBSPrint (PrintBuffer, BYTES_PER_RECORD, Format, Marker);\r
1f53f7bb 512 SecPrint (PrintBuffer);\r
2e19fd0f 513 }\r
514\r
515 return EFI_SUCCESS;\r
516}\r
517\r
f66a43b2 518#if defined (MDE_CPU_IA32)\r
5aae0aa7 519/**\r
520 Transfers control to a function starting with a new stack.\r
521\r
522 Transfers control to the function specified by EntryPoint using the new stack\r
523 specified by NewStack and passing in the parameters specified by Context1 and\r
524 Context2. Context1 and Context2 are optional and may be NULL. The function\r
525 EntryPoint must never return.\r
526\r
527 If EntryPoint is NULL, then ASSERT().\r
528 If NewStack is NULL, then ASSERT().\r
529\r
530 @param EntryPoint A pointer to function to call with the new stack.\r
531 @param Context1 A pointer to the context to pass into the EntryPoint\r
532 function.\r
533 @param Context2 A pointer to the context to pass into the EntryPoint\r
534 function.\r
535 @param NewStack A pointer to the new stack to use for the EntryPoint\r
536 function.\r
537 @param NewBsp A pointer to the new BSP for the EntryPoint on IPF. It's\r
538 Reserved on other architectures.\r
539\r
540**/\r
541VOID\r
542EFIAPI\r
543PeiSwitchStacks (\r
544 IN SWITCH_STACK_ENTRY_POINT EntryPoint,\r
545 IN VOID *Context1, OPTIONAL\r
546 IN VOID *Context2, OPTIONAL\r
547 IN VOID *Context3, OPTIONAL\r
548 IN VOID *NewStack\r
549 )\r
550{\r
551 BASE_LIBRARY_JUMP_BUFFER JumpBuffer;\r
552 \r
553 ASSERT (EntryPoint != NULL);\r
554 ASSERT (NewStack != NULL);\r
555\r
556 //\r
557 // Stack should be aligned with CPU_STACK_ALIGNMENT\r
558 //\r
559 ASSERT (((UINTN)NewStack & (CPU_STACK_ALIGNMENT - 1)) == 0);\r
560\r
561 JumpBuffer.Eip = (UINTN)EntryPoint;\r
562 JumpBuffer.Esp = (UINTN)NewStack - sizeof (VOID*);\r
563 JumpBuffer.Esp -= sizeof (Context1) + sizeof (Context2) + sizeof(Context3);\r
564 ((VOID**)JumpBuffer.Esp)[1] = Context1;\r
565 ((VOID**)JumpBuffer.Esp)[2] = Context2;\r
566 ((VOID**)JumpBuffer.Esp)[3] = Context3;\r
567\r
568 LongJump (&JumpBuffer, (UINTN)-1);\r
569 \r
570\r
571 //\r
572 // InternalSwitchStack () will never return\r
573 //\r
574 ASSERT (FALSE); \r
575}\r
f66a43b2 576#endif\r
2e19fd0f 577\r
578VOID\r
579SecLoadFromCore (\r
580 IN UINTN LargestRegion,\r
581 IN UINTN LargestRegionSize,\r
582 IN UINTN BootFirmwareVolumeBase,\r
583 IN VOID *PeiCorePe32File\r
584 )\r
585/*++\r
586\r
587Routine Description:\r
588 This is the service to load the PEI Core from the Firmware Volume\r
589\r
590Arguments:\r
591 LargestRegion - Memory to use for PEI.\r
592 LargestRegionSize - Size of Memory to use for PEI\r
593 BootFirmwareVolumeBase - Start of the Boot FV\r
594 PeiCorePe32File - PEI Core PE32\r
595\r
596Returns:\r
597 Success means control is transfered and thus we should never return\r
598\r
599--*/\r
600{\r
601 EFI_STATUS Status;\r
2e19fd0f 602 VOID *TopOfStack;\r
603 UINT64 PeiCoreSize;\r
604 EFI_PHYSICAL_ADDRESS PeiCoreEntryPoint;\r
605 EFI_PHYSICAL_ADDRESS PeiImageAddress;\r
5aae0aa7 606 EFI_SEC_PEI_HAND_OFF *SecCoreData;\r
58dcdada 607 UINTN PeiStackSize;\r
2e19fd0f 608\r
609 //\r
610 // Compute Top Of Memory for Stack and PEI Core Allocations\r
611 //\r
58dcdada 612 PeiStackSize = (UINTN)RShiftU64((UINT64)STACK_SIZE,1);\r
2e19fd0f 613\r
614 //\r
58dcdada 615 // |-----------| <---- TemporaryRamBase + TemporaryRamSize\r
616 // | Heap |\r
617 // | |\r
618 // |-----------| <---- StackBase / PeiTemporaryMemoryBase\r
619 // | |\r
620 // | Stack |\r
621 // |-----------| <---- TemporaryRamBase\r
622 // \r
623 TopOfStack = (VOID *)(LargestRegion + PeiStackSize);\r
58dcdada 624\r
2e19fd0f 625 //\r
58dcdada 626 // Reservet space for storing PeiCore's parament in stack.\r
627 // \r
628 TopOfStack = (VOID *)((UINTN)TopOfStack - sizeof (EFI_SEC_PEI_HAND_OFF) - CPU_STACK_ALIGNMENT);\r
2e19fd0f 629 TopOfStack = ALIGN_POINTER (TopOfStack, CPU_STACK_ALIGNMENT);\r
2e19fd0f 630\r
2e19fd0f 631 //\r
632 // Bind this information into the SEC hand-off state\r
633 //\r
5aae0aa7 634 SecCoreData = (EFI_SEC_PEI_HAND_OFF*)(UINTN) TopOfStack;\r
635 SecCoreData->DataSize = sizeof(EFI_SEC_PEI_HAND_OFF);\r
636 SecCoreData->BootFirmwareVolumeBase = (VOID*)BootFirmwareVolumeBase;\r
f7c58a94 637 SecCoreData->BootFirmwareVolumeSize = PcdGet32(PcdWinNtFirmwareFdSize);\r
58dcdada 638 SecCoreData->TemporaryRamBase = (VOID*)(UINTN)LargestRegion; \r
5aae0aa7 639 SecCoreData->TemporaryRamSize = STACK_SIZE;\r
ff33b87d 640 SecCoreData->StackBase = SecCoreData->TemporaryRamBase;\r
58dcdada 641 SecCoreData->StackSize = PeiStackSize;\r
ff33b87d 642 SecCoreData->PeiTemporaryRamBase = (VOID*) ((UINTN) SecCoreData->TemporaryRamBase + PeiStackSize);\r
58dcdada 643 SecCoreData->PeiTemporaryRamSize = STACK_SIZE - PeiStackSize;\r
2e19fd0f 644\r
645 //\r
646 // Load the PEI Core from a Firmware Volume\r
647 //\r
648 Status = SecWinNtPeiLoadFile (\r
649 PeiCorePe32File,\r
650 &PeiImageAddress,\r
651 &PeiCoreSize,\r
652 &PeiCoreEntryPoint\r
653 );\r
654 if (EFI_ERROR (Status)) {\r
655 return ;\r
656 }\r
5aae0aa7 657 \r
2e19fd0f 658 //\r
659 // Transfer control to the PEI Core\r
660 //\r
5aae0aa7 661 PeiSwitchStacks (\r
2e19fd0f 662 (SWITCH_STACK_ENTRY_POINT) (UINTN) PeiCoreEntryPoint,\r
5aae0aa7 663 SecCoreData,\r
664 (VOID *) (UINTN) ((EFI_PEI_PPI_DESCRIPTOR *) &gPrivateDispatchTable),\r
2e19fd0f 665 NULL,\r
666 TopOfStack\r
667 );\r
668 //\r
669 // If we get here, then the PEI Core returned. This is an error\r
670 //\r
671 return ;\r
672}\r
673\r
674EFI_STATUS\r
675EFIAPI\r
676SecWinNtPeiAutoScan (\r
677 IN UINTN Index,\r
678 OUT EFI_PHYSICAL_ADDRESS *MemoryBase,\r
679 OUT UINT64 *MemorySize\r
680 )\r
681/*++\r
682\r
683Routine Description:\r
684 This service is called from Index == 0 until it returns EFI_UNSUPPORTED.\r
685 It allows discontiguous memory regions to be supported by the emulator.\r
686 It uses gSystemMemory[] and gSystemMemoryCount that were created by\r
952261d5
LG
687 parsing PcdWinNtMemorySizeForSecMain value.\r
688 The size comes from the Pcd value and the address comes from the memory space \r
689 with ReadWrite and Execute attributes allocated by VirtualAlloc() API.\r
2e19fd0f 690\r
691Arguments:\r
692 Index - Which memory region to use\r
693 MemoryBase - Return Base address of memory region\r
694 MemorySize - Return size in bytes of the memory region\r
695\r
696Returns:\r
697 EFI_SUCCESS - If memory region was mapped\r
698 EFI_UNSUPPORTED - If Index is not supported\r
699\r
700--*/\r
701{\r
2e19fd0f 702 if (Index >= gSystemMemoryCount) {\r
703 return EFI_UNSUPPORTED;\r
704 }\r
952261d5
LG
705 \r
706 //\r
707 // Allocate enough memory space for emulator \r
708 //\r
709 gSystemMemory[Index].Memory = (EFI_PHYSICAL_ADDRESS) (UINTN) VirtualAlloc (NULL, (SIZE_T) (gSystemMemory[Index].Size), MEM_COMMIT, PAGE_EXECUTE_READWRITE);\r
710 if (gSystemMemory[Index].Memory == 0) {\r
711 return EFI_OUT_OF_RESOURCES;\r
712 }\r
713 \r
714 *MemoryBase = gSystemMemory[Index].Memory;\r
715 *MemorySize = gSystemMemory[Index].Size;\r
2e19fd0f 716\r
952261d5 717 return EFI_SUCCESS;\r
2e19fd0f 718}\r
719\r
720VOID *\r
721EFIAPI\r
722SecWinNtWinNtThunkAddress (\r
723 VOID\r
724 )\r
725/*++\r
726\r
727Routine Description:\r
728 Since the SEC is the only Windows program in stack it must export\r
729 an interface to do Win API calls. That's what the WinNtThunk address\r
730 is for. gWinNt is initailized in WinNtThunk.c.\r
731\r
732Arguments:\r
733 InterfaceSize - sizeof (EFI_WIN_NT_THUNK_PROTOCOL);\r
734 InterfaceBase - Address of the gWinNt global\r
735\r
736Returns:\r
737 EFI_SUCCESS - Data returned\r
738\r
739--*/\r
740{\r
741 return gWinNt;\r
742}\r
743\r
744\r
745EFI_STATUS\r
746EFIAPI\r
747SecWinNtPeiLoadFile (\r
748 IN VOID *Pe32Data,\r
749 IN EFI_PHYSICAL_ADDRESS *ImageAddress,\r
750 IN UINT64 *ImageSize,\r
751 IN EFI_PHYSICAL_ADDRESS *EntryPoint\r
752 )\r
753/*++\r
754\r
755Routine Description:\r
756 Loads and relocates a PE/COFF image into memory.\r
757\r
758Arguments:\r
759 Pe32Data - The base address of the PE/COFF file that is to be loaded and relocated\r
760 ImageAddress - The base address of the relocated PE/COFF image\r
761 ImageSize - The size of the relocated PE/COFF image\r
762 EntryPoint - The entry point of the relocated PE/COFF image\r
763\r
764Returns:\r
765 EFI_SUCCESS - The file was loaded and relocated\r
766 EFI_OUT_OF_RESOURCES - There was not enough memory to load and relocate the PE/COFF file\r
767\r
768--*/\r
769{\r
770 EFI_STATUS Status;\r
771 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;\r
772\r
773 ZeroMem (&ImageContext, sizeof (ImageContext));\r
774 ImageContext.Handle = Pe32Data;\r
775\r
776 ImageContext.ImageRead = (PE_COFF_LOADER_READ_FILE) SecImageRead;\r
777\r
ffdd18bb 778 Status = PeCoffLoaderGetImageInfo (&ImageContext);\r
2e19fd0f 779 if (EFI_ERROR (Status)) {\r
780 return Status;\r
781 }\r
782 //\r
952261d5
LG
783 // Allocate space in NT (not emulator) memory with ReadWrite and Execute attribue. \r
784 // Extra space is for alignment\r
2e19fd0f 785 //\r
952261d5 786 ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS) (UINTN) VirtualAlloc (NULL, (SIZE_T) (ImageContext.ImageSize + (ImageContext.SectionAlignment * 2)), MEM_COMMIT, PAGE_EXECUTE_READWRITE);\r
2e19fd0f 787 if (ImageContext.ImageAddress == 0) {\r
788 return EFI_OUT_OF_RESOURCES;\r
789 }\r
790 //\r
791 // Align buffer on section boundry\r
792 //\r
0d10bfc8 793 ImageContext.ImageAddress += ImageContext.SectionAlignment - 1;\r
2e19fd0f 794 ImageContext.ImageAddress &= ~(ImageContext.SectionAlignment - 1);\r
795\r
ffdd18bb 796 Status = PeCoffLoaderLoadImage (&ImageContext);\r
2e19fd0f 797 if (EFI_ERROR (Status)) {\r
798 return Status;\r
799 }\r
800\r
ffdd18bb 801 Status = SecNt32PeCoffRelocateImage (&ImageContext);\r
2e19fd0f 802 if (EFI_ERROR (Status)) {\r
803 return Status;\r
804 }\r
805\r
806 //\r
807 // BugBug: Flush Instruction Cache Here when CPU Lib is ready\r
808 //\r
809\r
810 *ImageAddress = ImageContext.ImageAddress;\r
811 *ImageSize = ImageContext.ImageSize;\r
812 *EntryPoint = ImageContext.EntryPoint;\r
813\r
814 return EFI_SUCCESS;\r
815}\r
816\r
817EFI_STATUS\r
818EFIAPI\r
819SecWinNtFdAddress (\r
820 IN UINTN Index,\r
821 IN OUT EFI_PHYSICAL_ADDRESS *FdBase,\r
822 IN OUT UINT64 *FdSize\r
823 )\r
824/*++\r
825\r
826Routine Description:\r
827 Return the FD Size and base address. Since the FD is loaded from a\r
828 file into Windows memory only the SEC will know it's address.\r
829\r
830Arguments:\r
831 Index - Which FD, starts at zero.\r
832 FdSize - Size of the FD in bytes\r
833 FdBase - Start address of the FD. Assume it points to an FV Header\r
834\r
835Returns:\r
836 EFI_SUCCESS - Return the Base address and size of the FV\r
837 EFI_UNSUPPORTED - Index does nto map to an FD in the system\r
838\r
839--*/\r
840{\r
841 if (Index >= gFdInfoCount) {\r
842 return EFI_UNSUPPORTED;\r
843 }\r
844\r
845 *FdBase = gFdInfo[Index].Address;\r
846 *FdSize = gFdInfo[Index].Size;\r
847\r
848 if (*FdBase == 0 && *FdSize == 0) {\r
849 return EFI_UNSUPPORTED;\r
850 }\r
851\r
852 return EFI_SUCCESS;\r
853}\r
854\r
855EFI_STATUS\r
856EFIAPI\r
857SecImageRead (\r
858 IN VOID *FileHandle,\r
859 IN UINTN FileOffset,\r
860 IN OUT UINTN *ReadSize,\r
861 OUT VOID *Buffer\r
862 )\r
863/*++\r
864\r
865Routine Description:\r
866 Support routine for the PE/COFF Loader that reads a buffer from a PE/COFF file\r
867\r
868Arguments:\r
869 FileHandle - The handle to the PE/COFF file\r
870 FileOffset - The offset, in bytes, into the file to read\r
871 ReadSize - The number of bytes to read from the file starting at FileOffset\r
872 Buffer - A pointer to the buffer to read the data into.\r
873\r
874Returns:\r
875 EFI_SUCCESS - ReadSize bytes of data were read into Buffer from the PE/COFF file starting at FileOffset\r
876\r
877--*/\r
878{\r
879 CHAR8 *Destination8;\r
880 CHAR8 *Source8;\r
881 UINTN Length;\r
882\r
883 Destination8 = Buffer;\r
884 Source8 = (CHAR8 *) ((UINTN) FileHandle + FileOffset);\r
885 Length = *ReadSize;\r
886 while (Length--) {\r
887 *(Destination8++) = *(Source8++);\r
888 }\r
889\r
890 return EFI_SUCCESS;\r
891}\r
892\r
893CHAR16 *\r
894AsciiToUnicode (\r
895 IN CHAR8 *Ascii,\r
896 IN UINTN *StrLen OPTIONAL\r
897 )\r
898/*++\r
899\r
900Routine Description:\r
901 Convert the passed in Ascii string to Unicode.\r
902 Optionally return the length of the strings.\r
903\r
904Arguments:\r
905 Ascii - Ascii string to convert\r
906 StrLen - Length of string\r
907\r
908Returns:\r
909 Pointer to malloc'ed Unicode version of Ascii\r
910\r
911--*/\r
912{\r
913 UINTN Index;\r
914 CHAR16 *Unicode;\r
915\r
916 //\r
917 // Allocate a buffer for unicode string\r
918 //\r
919 for (Index = 0; Ascii[Index] != '\0'; Index++)\r
920 ;\r
921 Unicode = malloc ((Index + 1) * sizeof (CHAR16));\r
922 if (Unicode == NULL) {\r
923 return NULL;\r
924 }\r
925\r
926 for (Index = 0; Ascii[Index] != '\0'; Index++) {\r
927 Unicode[Index] = (CHAR16) Ascii[Index];\r
928 }\r
929\r
930 Unicode[Index] = '\0';\r
931\r
932 if (StrLen != NULL) {\r
933 *StrLen = Index;\r
934 }\r
935\r
936 return Unicode;\r
937}\r
938\r
939UINTN\r
940CountSeperatorsInString (\r
03c19e06 941 IN CONST CHAR16 *String,\r
2e19fd0f 942 IN CHAR16 Seperator\r
943 )\r
944/*++\r
945\r
946Routine Description:\r
947 Count the number of seperators in String\r
948\r
949Arguments:\r
950 String - String to process\r
951 Seperator - Item to count\r
952\r
953Returns:\r
954 Number of Seperator in String\r
955\r
956--*/\r
957{\r
958 UINTN Count;\r
959\r
960 for (Count = 0; *String != '\0'; String++) {\r
961 if (*String == Seperator) {\r
962 Count++;\r
963 }\r
964 }\r
965\r
966 return Count;\r
967}\r
968\r
969\r
970EFI_STATUS\r
2e19fd0f 971SecNt32PeCoffRelocateImage (\r
2e19fd0f 972 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r
973 )\r
974{\r
975 EFI_STATUS Status;\r
976 VOID *DllEntryPoint;\r
977 CHAR16 *DllFileName;\r
978 HMODULE Library;\r
979 UINTN Index;\r
980\r
981\r
982 Status = PeCoffLoaderRelocateImage (ImageContext);\r
983 if (EFI_ERROR (Status)) {\r
984 //\r
985 // We could not relocated the image in memory properly\r
986 //\r
987 return Status;\r
988 }\r
989\r
990 //\r
991 // If we load our own PE COFF images the Windows debugger can not source\r
992 // level debug our code. If a valid PDB pointer exists usw it to load\r
993 // the *.dll file as a library using Windows* APIs. This allows \r
994 // source level debug. The image is still loaded and reloaced\r
995 // in the Framework memory space like on a real system (by the code above),\r
996 // but the entry point points into the DLL loaded by the code bellow. \r
997 //\r
998\r
999 DllEntryPoint = NULL;\r
1000\r
1001 //\r
1002 // Load the DLL if it's not an EBC image.\r
1003 //\r
1004 if ((ImageContext->PdbPointer != NULL) &&\r
1005 (ImageContext->Machine != EFI_IMAGE_MACHINE_EBC)) {\r
1006 //\r
1007 // Convert filename from ASCII to Unicode\r
1008 //\r
1009 DllFileName = AsciiToUnicode (ImageContext->PdbPointer, &Index);\r
1010\r
1011 //\r
1012 // Check that we have a valid filename\r
1013 //\r
1014 if (Index < 5 || DllFileName[Index - 4] != '.') {\r
1015 free (DllFileName);\r
1016\r
1017 //\r
1018 // Never return an error if PeCoffLoaderRelocateImage() succeeded.\r
1019 // The image will run, but we just can't source level debug. If we\r
1020 // return an error the image will not run.\r
1021 //\r
1022 return EFI_SUCCESS;\r
1023 }\r
1024 //\r
1025 // Replace .PDB with .DLL on the filename\r
1026 //\r
1027 DllFileName[Index - 3] = 'D';\r
1028 DllFileName[Index - 2] = 'L';\r
1029 DllFileName[Index - 1] = 'L';\r
1030\r
1031 //\r
1032 // Load the .DLL file into the user process's address space for source \r
1033 // level debug\r
1034 //\r
1035 Library = LoadLibraryEx (DllFileName, NULL, DONT_RESOLVE_DLL_REFERENCES);\r
1036 if (Library != NULL) {\r
1037 //\r
1038 // InitializeDriver is the entry point we put in all our EFI DLL's. The\r
1039 // DONT_RESOLVE_DLL_REFERENCES argument to LoadLIbraryEx() supresses the \r
1040 // normal DLL entry point of DllMain, and prevents other modules that are\r
1041 // referenced in side the DllFileName from being loaded. There is no error \r
1042 // checking as the we can point to the PE32 image loaded by Tiano. This \r
1043 // step is only needed for source level debuging\r
1044 //\r
1045 DllEntryPoint = (VOID *) (UINTN) GetProcAddress (Library, "InitializeDriver");\r
1046\r
1047 }\r
1048\r
1049 if ((Library != NULL) && (DllEntryPoint != NULL)) {\r
2e19fd0f 1050 ImageContext->EntryPoint = (EFI_PHYSICAL_ADDRESS) (UINTN) DllEntryPoint;\r
1f53f7bb 1051 SecPrint ("LoadLibraryEx (%S,\n NULL, DONT_RESOLVE_DLL_REFERENCES)\n", DllFileName);\r
2e19fd0f 1052 } else {\r
1f53f7bb 1053 SecPrint ("WARNING: No source level debug %S. \n", DllFileName);\r
2e19fd0f 1054 }\r
1055\r
1056 free (DllFileName);\r
1057 }\r
1058\r
1059 //\r
1060 // Never return an error if PeCoffLoaderRelocateImage() succeeded.\r
1061 // The image will run, but we just can't source level debug. If we\r
1062 // return an error the image will not run.\r
1063 //\r
1064 return EFI_SUCCESS;\r
1065}\r
1066\r
1067\r
2e19fd0f 1068\r
2e19fd0f 1069\r
1070VOID\r
1071_ModuleEntryPoint (\r
1072 VOID\r
1073 )\r
1074{\r
1075}\r
1076\r
58dcdada 1077EFI_STATUS\r
1078EFIAPI\r
1079SecTemporaryRamSupport (\r
1080 IN CONST EFI_PEI_SERVICES **PeiServices,\r
1081 IN EFI_PHYSICAL_ADDRESS TemporaryMemoryBase,\r
1082 IN EFI_PHYSICAL_ADDRESS PermanentMemoryBase,\r
1083 IN UINTN CopySize\r
1084 )\r
1085{\r
1086 //\r
1087 // Migrate the whole temporary memory to permenent memory.\r
1088 // \r
1089 CopyMem (\r
1090 (VOID*)(UINTN)PermanentMemoryBase, \r
1091 (VOID*)(UINTN)TemporaryMemoryBase, \r
1092 CopySize\r
1093 );\r
1094\r
1095 //\r
1096 // SecSwitchStack function must be invoked after the memory migration\r
1097 // immediatly, also we need fixup the stack change caused by new call into \r
1098 // permenent memory.\r
1099 // \r
1100 SecSwitchStack (\r
1101 (UINT32) TemporaryMemoryBase,\r
1102 (UINT32) PermanentMemoryBase\r
1103 );\r
1104\r
1105 //\r
1106 // We need *not* fix the return address because currently, \r
1107 // The PeiCore is excuted in flash.\r
1108 //\r
1109\r
1110 //\r
84a7fd1e 1111 // Simulate to invalid temporary memory, terminate temporary memory\r
58dcdada 1112 // \r
1113 //ZeroMem ((VOID*)(UINTN)TemporaryMemoryBase, CopySize);\r
1114 \r
1115 return EFI_SUCCESS;\r
1116}\r
1117\r