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