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