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