Add check for a NULL Data parameter in SecPeiReportStatusCode(). The Report Status...
[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
21802505 476 if (Data != NULL) {\r
477 DebugInfo = (EFI_DEBUG_INFO *) (Data + 1);\r
478 Marker = (VA_LIST) (DebugInfo + 1);\r
479 Format = (CHAR8 *) (((UINT64 *) Marker) + 12);\r
878ddf1f 480\r
21802505 481 AsciiVSPrint (PrintBuffer, BYTES_PER_RECORD, Format, Marker);\r
482 printf (PrintBuffer);\r
483 } else {\r
484 printf ("DEBUG <null>\n");\r
485 }\r
878ddf1f 486 }\r
487\r
488 if (((CodeType & EFI_STATUS_CODE_TYPE_MASK) == EFI_ERROR_CODE) &&\r
489 ((CodeType & EFI_STATUS_CODE_SEVERITY_MASK) == EFI_ERROR_UNRECOVERED)\r
490 ) {\r
21802505 491 if (Data != NULL && ReportStatusCodeExtractAssertInfo (CodeType, Value, Data, &Filename, &Description, &LineNumber)) {\r
878ddf1f 492 //\r
493 // Support ASSERT () macro\r
494 //\r
495 printf ("ASSERT %s(%d): %s\n", Filename, LineNumber, Description);\r
21802505 496 } else {\r
497 printf ("ASSERT <null>\n");\r
878ddf1f 498 }\r
21802505 499 CpuBreakpoint ();\r
878ddf1f 500 }\r
501\r
502 return EFI_SUCCESS;\r
503}\r
504\r
505\r
506VOID\r
507SecLoadFromCore (\r
508 IN UINTN LargestRegion,\r
509 IN UINTN LargestRegionSize,\r
510 IN UINTN BootFirmwareVolumeBase,\r
511 IN VOID *PeiCorePe32File\r
512 )\r
513/*++\r
514\r
515Routine Description:\r
516 This is the service to load the PEI Core from the Firmware Volume\r
517\r
518Arguments:\r
519 LargestRegion - Memory to use for PEI.\r
520 LargestRegionSize - Size of Memory to use for PEI\r
521 BootFirmwareVolumeBase - Start of the Boot FV\r
522 PeiCorePe32File - PEI Core PE32\r
523\r
524Returns:\r
525 Success means control is transfered and thus we should never return\r
526\r
527--*/\r
528{\r
529 EFI_STATUS Status;\r
530 EFI_PHYSICAL_ADDRESS TopOfMemory;\r
531 VOID *TopOfStack;\r
532 UINT64 PeiCoreSize;\r
533 EFI_PHYSICAL_ADDRESS PeiCoreEntryPoint;\r
534 EFI_PHYSICAL_ADDRESS PeiImageAddress;\r
535 EFI_PEI_STARTUP_DESCRIPTOR *PeiStartup;\r
536\r
537 //\r
538 // Compute Top Of Memory for Stack and PEI Core Allocations\r
539 //\r
540 TopOfMemory = LargestRegion + ((LargestRegionSize) & (~15));\r
541\r
542 //\r
543 // Allocate 128KB for the Stack\r
544 //\r
545 TopOfStack = (VOID *) (UINTN) (TopOfMemory - sizeof (EFI_PEI_STARTUP_DESCRIPTOR));\r
546 TopOfMemory = TopOfMemory - STACK_SIZE;\r
547\r
548 //\r
549 // Patch value in dispatch table values\r
550 //\r
551 gPrivateDispatchTable[0].Ppi = gPeiEfiPeiPeCoffLoader;\r
552\r
553 //\r
554 // Bind this information into the SEC hand-off state\r
555 //\r
556 PeiStartup = (EFI_PEI_STARTUP_DESCRIPTOR *) (UINTN) TopOfStack;\r
557 PeiStartup->DispatchTable = (EFI_PEI_PPI_DESCRIPTOR *) &gPrivateDispatchTable;\r
558 PeiStartup->SizeOfCacheAsRam = STACK_SIZE;\r
559 PeiStartup->BootFirmwareVolume = BootFirmwareVolumeBase;\r
560\r
561 //\r
562 // Load the PEI Core from a Firmware Volume\r
563 //\r
564 Status = SecWinNtPeiLoadFile (\r
565 PeiCorePe32File,\r
566 &PeiImageAddress,\r
567 &PeiCoreSize,\r
568 &PeiCoreEntryPoint\r
569 );\r
570 if (EFI_ERROR (Status)) {\r
571 return ;\r
572 }\r
573 //\r
574 // Transfer control to the PEI Core\r
575 //\r
576 SwitchStack (\r
577 (SWITCH_STACK_ENTRY_POINT) (UINTN) PeiCoreEntryPoint,\r
578 PeiStartup,\r
579 NULL,\r
580 TopOfStack\r
581 );\r
582 //\r
583 // If we get here, then the PEI Core returned. This is an error\r
584 //\r
585 return ;\r
586}\r
587\r
588EFI_STATUS\r
589EFIAPI\r
590SecWinNtPeiAutoScan (\r
591 IN UINTN Index,\r
592 OUT EFI_PHYSICAL_ADDRESS *MemoryBase,\r
593 OUT UINT64 *MemorySize\r
594 )\r
595/*++\r
596\r
597Routine Description:\r
598 This service is called from Index == 0 until it returns EFI_UNSUPPORTED.\r
599 It allows discontiguous memory regions to be supported by the emulator.\r
600 It uses gSystemMemory[] and gSystemMemoryCount that were created by\r
601 parsing the Windows environment variable EFI_MEMORY_SIZE.\r
602 The size comes from the varaible and the address comes from the call to\r
603 WinNtOpenFile.\r
604\r
605Arguments:\r
606 Index - Which memory region to use\r
607 MemoryBase - Return Base address of memory region\r
608 MemorySize - Return size in bytes of the memory region\r
609\r
610Returns:\r
611 EFI_SUCCESS - If memory region was mapped\r
612 EFI_UNSUPPORTED - If Index is not supported\r
613\r
614--*/\r
615{\r
616 EFI_STATUS Status;\r
617\r
618 if (Index >= gSystemMemoryCount) {\r
619 return EFI_UNSUPPORTED;\r
620 }\r
621\r
622 *MemoryBase = 0;\r
623 Status = WinNtOpenFile (\r
624 gSystemMemory[Index].FileName,\r
625 (UINT32) gSystemMemory[Index].Size,\r
626 OPEN_ALWAYS,\r
627 MemoryBase,\r
628 MemorySize\r
629 );\r
630\r
631 gSystemMemory[Index].Memory = *MemoryBase;\r
632\r
633 return Status;\r
634}\r
635\r
636VOID *\r
637EFIAPI\r
638SecWinNtWinNtThunkAddress (\r
639 VOID\r
640 )\r
641/*++\r
642\r
643Routine Description:\r
644 Since the SEC is the only Windows program in stack it must export\r
645 an interface to do Win API calls. That's what the WinNtThunk address\r
646 is for. gWinNt is initailized in WinNtThunk.c.\r
647\r
648Arguments:\r
649 InterfaceSize - sizeof (EFI_WIN_NT_THUNK_PROTOCOL);\r
650 InterfaceBase - Address of the gWinNt global\r
651\r
652Returns:\r
653 EFI_SUCCESS - Data returned\r
654\r
655--*/\r
656{\r
657 return gWinNt;\r
658}\r
659\r
660\r
661EFI_STATUS\r
662EFIAPI\r
663SecWinNtPeiLoadFile (\r
664 IN VOID *Pe32Data,\r
665 IN EFI_PHYSICAL_ADDRESS *ImageAddress,\r
666 IN UINT64 *ImageSize,\r
667 IN EFI_PHYSICAL_ADDRESS *EntryPoint\r
668 )\r
669/*++\r
670\r
671Routine Description:\r
672 Loads and relocates a PE/COFF image into memory.\r
673\r
674Arguments:\r
675 Pe32Data - The base address of the PE/COFF file that is to be loaded and relocated\r
676 ImageAddress - The base address of the relocated PE/COFF image\r
677 ImageSize - The size of the relocated PE/COFF image\r
678 EntryPoint - The entry point of the relocated PE/COFF image\r
679\r
680Returns:\r
681 EFI_SUCCESS - The file was loaded and relocated\r
682 EFI_OUT_OF_RESOURCES - There was not enough memory to load and relocate the PE/COFF file\r
683\r
684--*/\r
685{\r
686 EFI_STATUS Status;\r
687 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;\r
688\r
689 ZeroMem (&ImageContext, sizeof (ImageContext));\r
690 ImageContext.Handle = Pe32Data;\r
691\r
692 ImageContext.ImageRead = (PE_COFF_LOADER_READ_FILE) SecImageRead;\r
693\r
694 Status = gPeiEfiPeiPeCoffLoader->GetImageInfo (gPeiEfiPeiPeCoffLoader, &ImageContext);\r
695 if (EFI_ERROR (Status)) {\r
696 return Status;\r
697 }\r
698 //\r
699 // Allocate space in NT (not emulator) memory. Extra space is for alignment\r
700 //\r
701 ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS) (UINTN) malloc ((UINTN) (ImageContext.ImageSize + (ImageContext.SectionAlignment * 2)));\r
702 if (ImageContext.ImageAddress == 0) {\r
703 return EFI_OUT_OF_RESOURCES;\r
704 }\r
705 //\r
706 // Align buffer on section boundry\r
707 //\r
708 ImageContext.ImageAddress += ImageContext.SectionAlignment;\r
709 ImageContext.ImageAddress &= ~(ImageContext.SectionAlignment - 1);\r
710\r
711 Status = gPeiEfiPeiPeCoffLoader->LoadImage (gPeiEfiPeiPeCoffLoader, &ImageContext);\r
712 if (EFI_ERROR (Status)) {\r
713 return Status;\r
714 }\r
715\r
716 Status = gPeiEfiPeiPeCoffLoader->RelocateImage (gPeiEfiPeiPeCoffLoader, &ImageContext);\r
717 if (EFI_ERROR (Status)) {\r
718 return Status;\r
719 }\r
720\r
721 //\r
722 // BugBug: Flush Instruction Cache Here when CPU Lib is ready\r
723 //\r
724\r
725 *ImageAddress = ImageContext.ImageAddress;\r
726 *ImageSize = ImageContext.ImageSize;\r
727 *EntryPoint = ImageContext.EntryPoint;\r
728\r
729 return EFI_SUCCESS;\r
730}\r
731\r
732EFI_STATUS\r
733EFIAPI\r
734SecWinNtFdAddress (\r
735 IN UINTN Index,\r
736 IN OUT EFI_PHYSICAL_ADDRESS *FdBase,\r
737 IN OUT UINT64 *FdSize\r
738 )\r
739/*++\r
740\r
741Routine Description:\r
742 Return the FD Size and base address. Since the FD is loaded from a\r
743 file into Windows memory only the SEC will know it's address.\r
744\r
745Arguments:\r
746 Index - Which FD, starts at zero.\r
747 FdSize - Size of the FD in bytes\r
748 FdBase - Start address of the FD. Assume it points to an FV Header\r
749\r
750Returns:\r
751 EFI_SUCCESS - Return the Base address and size of the FV\r
752 EFI_UNSUPPORTED - Index does nto map to an FD in the system\r
753\r
754--*/\r
755{\r
756 if (Index >= gFdInfoCount) {\r
757 return EFI_UNSUPPORTED;\r
758 }\r
759\r
760 *FdBase = gFdInfo[Index].Address;\r
761 *FdSize = gFdInfo[Index].Size;\r
762\r
763 if (*FdBase == 0 && *FdSize == 0) {\r
764 return EFI_UNSUPPORTED;\r
765 }\r
766\r
767 return EFI_SUCCESS;\r
768}\r
769\r
770EFI_STATUS\r
771EFIAPI\r
772SecImageRead (\r
773 IN VOID *FileHandle,\r
774 IN UINTN FileOffset,\r
775 IN OUT UINTN *ReadSize,\r
776 OUT VOID *Buffer\r
777 )\r
778/*++\r
779\r
780Routine Description:\r
781 Support routine for the PE/COFF Loader that reads a buffer from a PE/COFF file\r
782\r
783Arguments:\r
784 FileHandle - The handle to the PE/COFF file\r
785 FileOffset - The offset, in bytes, into the file to read\r
786 ReadSize - The number of bytes to read from the file starting at FileOffset\r
787 Buffer - A pointer to the buffer to read the data into.\r
788\r
789Returns:\r
790 EFI_SUCCESS - ReadSize bytes of data were read into Buffer from the PE/COFF file starting at FileOffset\r
791\r
792--*/\r
793{\r
794 CHAR8 *Destination8;\r
795 CHAR8 *Source8;\r
796 UINTN Length;\r
797\r
798 Destination8 = Buffer;\r
799 Source8 = (CHAR8 *) ((UINTN) FileHandle + FileOffset);\r
800 Length = *ReadSize;\r
801 while (Length--) {\r
802 *(Destination8++) = *(Source8++);\r
803 }\r
804\r
805 return EFI_SUCCESS;\r
806}\r
807\r
808CHAR16 *\r
809AsciiToUnicode (\r
810 IN CHAR8 *Ascii,\r
811 IN UINTN *StrLen OPTIONAL\r
812 )\r
813/*++\r
814\r
815Routine Description:\r
816 Convert the passed in Ascii string to Unicode.\r
817 Optionally return the length of the strings.\r
818\r
819Arguments:\r
820 Ascii - Ascii string to convert\r
821 StrLen - Length of string\r
822\r
823Returns:\r
824 Pointer to malloc'ed Unicode version of Ascii\r
825\r
826--*/\r
827{\r
828 UINTN Index;\r
829 CHAR16 *Unicode;\r
830\r
831 //\r
832 // Allocate a buffer for unicode string\r
833 //\r
834 for (Index = 0; Ascii[Index] != '\0'; Index++)\r
835 ;\r
836 Unicode = malloc ((Index + 1) * sizeof (CHAR16));\r
837 if (Unicode == NULL) {\r
838 return NULL;\r
839 }\r
840\r
841 for (Index = 0; Ascii[Index] != '\0'; Index++) {\r
842 Unicode[Index] = (CHAR16) Ascii[Index];\r
843 }\r
844\r
845 Unicode[Index] = '\0';\r
846\r
847 if (StrLen != NULL) {\r
848 *StrLen = Index;\r
849 }\r
850\r
851 return Unicode;\r
852}\r
853\r
854UINTN\r
855CountSeperatorsInString (\r
856 IN const CHAR16 *String,\r
857 IN CHAR16 Seperator\r
858 )\r
859/*++\r
860\r
861Routine Description:\r
862 Count the number of seperators in String\r
863\r
864Arguments:\r
865 String - String to process\r
866 Seperator - Item to count\r
867\r
868Returns:\r
869 Number of Seperator in String\r
870\r
871--*/\r
872{\r
873 UINTN Count;\r
874\r
875 for (Count = 0; *String != '\0'; String++) {\r
876 if (*String == Seperator) {\r
877 Count++;\r
878 }\r
879 }\r
880\r
881 return Count;\r
882}\r
883\r
884\r
885EFI_STATUS\r
886AddModHandle (\r
887 IN PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext,\r
888 IN VOID *ModHandle\r
889 )\r
890/*++\r
891\r
892Routine Description:\r
893 Store the ModHandle in an array indexed by the Pdb File name.\r
894 The ModHandle is needed to unload the image. \r
895\r
896Arguments:\r
897 ImageContext - Input data returned from PE Laoder Library. Used to find the \r
898 .PDB file name of the PE Image.\r
899 ModHandle - Returned from LoadLibraryEx() and stored for call to \r
900 FreeLibrary().\r
901\r
902Returns:\r
903 EFI_SUCCESS - ModHandle was stored. \r
904\r
905--*/\r
906{\r
907 UINTN Index;\r
908 PDB_NAME_TO_MOD_HANDLE *Array;\r
909 UINTN PreviousSize;\r
910\r
911\r
912 Array = mPdbNameModHandleArray;\r
913 for (Index = 0; Index < mPdbNameModHandleArraySize; Index++, Array++) {\r
914 if (Array->PdbPointer == NULL) {\r
915 //\r
916 // Make a copy of the stirng and store the ModHandle\r
917 //\r
918 Array->PdbPointer = malloc (strlen (ImageContext->PdbPointer) + 1);\r
919 ASSERT (Array->PdbPointer != NULL);\r
920\r
921 strcpy (Array->PdbPointer, ImageContext->PdbPointer);\r
922 Array->ModHandle = ModHandle;\r
923 return EFI_SUCCESS;\r
924 }\r
925 }\r
926 \r
927 //\r
928 // No free space in mPdbNameModHandleArray so grow it by \r
929 // MAX_PDB_NAME_TO_MOD_HANDLE_ARRAY_SIZE entires. realloc will\r
930 // copy the old values to the new locaiton. But it does\r
931 // not zero the new memory area.\r
932 //\r
933 PreviousSize = mPdbNameModHandleArraySize * sizeof (PDB_NAME_TO_MOD_HANDLE);\r
934 mPdbNameModHandleArraySize += MAX_PDB_NAME_TO_MOD_HANDLE_ARRAY_SIZE;\r
935\r
936 mPdbNameModHandleArray = realloc (mPdbNameModHandleArray, mPdbNameModHandleArraySize * sizeof (PDB_NAME_TO_MOD_HANDLE));\r
937 if (mPdbNameModHandleArray == NULL) {\r
938 ASSERT (FALSE);\r
939 return EFI_OUT_OF_RESOURCES;\r
940 }\r
941 \r
942 memset (mPdbNameModHandleArray + PreviousSize, 0, MAX_PDB_NAME_TO_MOD_HANDLE_ARRAY_SIZE * sizeof (PDB_NAME_TO_MOD_HANDLE));\r
943 \r
944 return AddModHandle (ImageContext, ModHandle);\r
945}\r
946\r
947\r
948VOID *\r
949RemoveModeHandle (\r
950 IN PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r
951 )\r
952/*++\r
953\r
954Routine Description:\r
955 Return the ModHandle and delete the entry in the array.\r
956\r
957Arguments:\r
958 ImageContext - Input data returned from PE Laoder Library. Used to find the \r
959 .PDB file name of the PE Image.\r
960\r
961Returns:\r
962 ModHandle - ModHandle assoicated with ImageContext is returned\r
963 NULL - No ModHandle associated with ImageContext\r
964\r
965--*/\r
966{\r
967 UINTN Index;\r
968 PDB_NAME_TO_MOD_HANDLE *Array;\r
969\r
970 if (ImageContext->PdbPointer == NULL) {\r
971 //\r
972 // If no PDB pointer there is no ModHandle so return NULL\r
973 //\r
974 return NULL;\r
975 }\r
976\r
977 Array = mPdbNameModHandleArray;\r
978 for (Index = 0; Index < mPdbNameModHandleArraySize; Index++, Array++) {\r
979 if ((Array->PdbPointer != NULL) && (strcmp(Array->PdbPointer, ImageContext->PdbPointer) == 0)) {\r
980 //\r
981 // If you find a match return it and delete the entry\r
982 //\r
983 free (Array->PdbPointer);\r
984 Array->PdbPointer = NULL;\r
985 return Array->ModHandle;\r
986 }\r
987 }\r
988\r
989 return NULL;\r
990}\r
991\r
992\r
993\r
994EFI_STATUS\r
995EFIAPI\r
996SecNt32PeCoffGetImageInfo (\r
997 IN EFI_PEI_PE_COFF_LOADER_PROTOCOL *This,\r
998 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r
999 )\r
1000{\r
1001 EFI_STATUS Status;\r
1002\r
1003 Status = PeCoffLoaderGetImageInfo (ImageContext);\r
1004 if (EFI_ERROR (Status)) {\r
1005 return Status;\r
1006 }\r
1007\r
1008 switch (ImageContext->ImageType) {\r
1009\r
1010 case EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION:\r
1011 ImageContext->ImageCodeMemoryType = EfiLoaderCode;\r
1012 ImageContext->ImageDataMemoryType = EfiLoaderData;\r
1013 break;\r
1014\r
1015 case EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER:\r
1016 ImageContext->ImageCodeMemoryType = EfiBootServicesCode;\r
1017 ImageContext->ImageDataMemoryType = EfiBootServicesData;\r
1018 break;\r
1019\r
1020 case EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER:\r
1021 case EFI_IMAGE_SUBSYSTEM_SAL_RUNTIME_DRIVER:\r
1022 ImageContext->ImageCodeMemoryType = EfiRuntimeServicesCode;\r
1023 ImageContext->ImageDataMemoryType = EfiRuntimeServicesData;\r
1024 break;\r
1025\r
1026 default:\r
1027 ImageContext->ImageError = IMAGE_ERROR_INVALID_SUBSYSTEM;\r
1028 return RETURN_UNSUPPORTED;\r
1029 }\r
1030\r
1031 return Status;\r
1032}\r
1033\r
1034EFI_STATUS\r
1035EFIAPI\r
1036SecNt32PeCoffLoadImage (\r
1037 IN EFI_PEI_PE_COFF_LOADER_PROTOCOL *This,\r
1038 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r
1039 )\r
1040{\r
1041 EFI_STATUS Status;\r
1042\r
1043 Status = PeCoffLoaderLoadImage (ImageContext);\r
1044 return Status;\r
1045}\r
1046\r
1047EFI_STATUS\r
1048EFIAPI\r
1049SecNt32PeCoffRelocateImage (\r
1050 IN EFI_PEI_PE_COFF_LOADER_PROTOCOL *This,\r
1051 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r
1052 )\r
1053{\r
1054 EFI_STATUS Status;\r
1055 VOID *DllEntryPoint;\r
1056 CHAR16 *DllFileName;\r
1057 HMODULE Library;\r
1058 UINTN Index;\r
1059\r
1060\r
1061 Status = PeCoffLoaderRelocateImage (ImageContext);\r
1062 if (EFI_ERROR (Status)) {\r
1063 //\r
1064 // We could not relocated the image in memory properly\r
1065 //\r
1066 return Status;\r
1067 }\r
1068\r
1069 //\r
1070 // If we load our own PE COFF images the Windows debugger can not source\r
1071 // level debug our code. If a valid PDB pointer exists usw it to load\r
1072 // the *.dll file as a library using Windows* APIs. This allows \r
1073 // source level debug. The image is still loaded and reloaced\r
1074 // in the Framework memory space like on a real system (by the code above),\r
1075 // but the entry point points into the DLL loaded by the code bellow. \r
1076 //\r
1077\r
1078 DllEntryPoint = NULL;\r
1079\r
1080 //\r
1081 // Load the DLL if it's not an EBC image.\r
1082 //\r
1083 if ((ImageContext->PdbPointer != NULL) &&\r
1084 (ImageContext->Machine != EFI_IMAGE_MACHINE_EBC)) {\r
1085 //\r
1086 // Convert filename from ASCII to Unicode\r
1087 //\r
1088 DllFileName = AsciiToUnicode (ImageContext->PdbPointer, &Index);\r
1089\r
1090 //\r
1091 // Check that we have a valid filename\r
1092 //\r
1093 if (Index < 5 || DllFileName[Index - 4] != '.') {\r
1094 free (DllFileName);\r
1095\r
1096 //\r
1097 // Never return an error if PeCoffLoaderRelocateImage() succeeded.\r
1098 // The image will run, but we just can't source level debug. If we\r
1099 // return an error the image will not run.\r
1100 //\r
1101 return EFI_SUCCESS;\r
1102 }\r
1103 //\r
1104 // Replace .PDB with .DLL on the filename\r
1105 //\r
1106 DllFileName[Index - 3] = 'D';\r
1107 DllFileName[Index - 2] = 'L';\r
1108 DllFileName[Index - 1] = 'L';\r
1109\r
1110 //\r
1111 // Load the .DLL file into the user process's address space for source \r
1112 // level debug\r
1113 //\r
1114 Library = LoadLibraryEx (DllFileName, NULL, DONT_RESOLVE_DLL_REFERENCES);\r
1115 if (Library != NULL) {\r
1116 //\r
1117 // InitializeDriver is the entry point we put in all our EFI DLL's. The\r
1118 // DONT_RESOLVE_DLL_REFERENCES argument to LoadLIbraryEx() supresses the \r
1119 // normal DLL entry point of DllMain, and prevents other modules that are\r
1120 // referenced in side the DllFileName from being loaded. There is no error \r
1121 // checking as the we can point to the PE32 image loaded by Tiano. This \r
1122 // step is only needed for source level debuging\r
1123 //\r
1124 DllEntryPoint = (VOID *) (UINTN) GetProcAddress (Library, "InitializeDriver");\r
1125\r
1126 }\r
1127\r
1128 if ((Library != NULL) && (DllEntryPoint != NULL)) {\r
1129 AddModHandle (ImageContext, Library);\r
1130 ImageContext->EntryPoint = (EFI_PHYSICAL_ADDRESS) (UINTN) DllEntryPoint;\r
1131 wprintf (L"LoadLibraryEx (%s,\n NULL, DONT_RESOLVE_DLL_REFERENCES)\n", DllFileName);\r
1132 } else {\r
1133 wprintf (L"WARNING: No source level debug %s. \n", DllFileName);\r
1134 }\r
1135\r
1136 free (DllFileName);\r
1137 }\r
1138\r
1139 //\r
1140 // Never return an error if PeCoffLoaderRelocateImage() succeeded.\r
1141 // The image will run, but we just can't source level debug. If we\r
1142 // return an error the image will not run.\r
1143 //\r
1144 return EFI_SUCCESS;\r
1145}\r
1146\r
1147\r
1148EFI_STATUS\r
1149EFIAPI\r
1150SecNt32PeCoffUnloadimage (\r
1151 IN EFI_PEI_PE_COFF_LOADER_PROTOCOL *This,\r
1152 IN PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r
1153 )\r
1154{\r
1155 VOID *ModHandle;\r
1156\r
1157 ModHandle = RemoveModeHandle (ImageContext);\r
1158 if (ModHandle != NULL) {\r
1159 FreeLibrary (ModHandle);\r
1160 }\r
1161 return EFI_SUCCESS;\r
1162}\r
1163\r
1164VOID\r
1165_ModuleEntryPoint (\r
1166 VOID\r
1167 )\r
1168{\r
1169}\r