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